#include "Qt/plot/Latency_Eager_Plot.h"
#include <QApplication>
#include <QCoreApplication>
#include <QTimer>
#include <gtest/gtest.h>
#include <vector>
namespace renderive {
namespace {
TEST(Renderive_Qt, WidgetLifecycleDrivesAndStopsKernelScene) {
    auto* application = qobject_cast<QApplication*>(QCoreApplication::instance());
    ASSERT_NE(application, nullptr);
    Latency_Eager_Plot plot;
    plot.resize(240, 120);
    plot.init();
    const auto root = plot.root_renderable();
    renderive_Owner<Frequency_Axis> x_axis;
    renderive_Owner<Axis> y_axis;
    renderive_Owner<Spectrum> spectrum;
    {
        auto attach = plot.scene()->attach_builder();
        x_axis = Frequency_Axis::Builder(root, Orientation::Horizontal, &attach)
            .set_x(20)
            .set_y(100)
            .set_pixel_length(200)
            .set_coord_range({0.0, 10.0})
            .build();
        y_axis = Axis::Builder(root, Orientation::Vertical, &attach)
            .set_x(20)
            .set_y(10)
            .set_pixel_length(90)
            .set_coord_range({1.0, 0.0})
            .build();
        spectrum = Spectrum::Builder{&attach}
            .set<&Spectrum::Properties::frequency_range>(Range{0.0, 10.0})
            .set<&Spectrum::Properties::frequency_point_size>(8)
            .build(root, x_axis, y_axis);
    }
    spectrum->update_samples(std::vector<double>{0.1, 0.3, 0.8, 0.5, 0.9, 0.4, 0.2, 0.7});
    plot.show();
    QTimer::singleShot(150, application, &QCoreApplication::quit);
    EXPECT_EQ(application->exec(), 0);
    EXPECT_GT(plot.diagnostics().refresh.frame_count, 0u);
    plot.pause();
    plot.hide();
    application->processEvents();
    EXPECT_FALSE(plot.running());
}
}
}
int main(int argc, char** argv) {
    qputenv("QT_QPA_PLATFORM", "offscreen");
    QApplication application(argc, argv);
    testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}
