Files
Renderive/tests/Performance_Shower_Interaction.cpp
2026-07-31 20:11:39 +08:00

114 lines
4.7 KiB
C++

#include "../Demo_Gallery/base/Tool.h"
#include <QApplication>
#include <QElapsedTimer>
#include <QMouseEvent>
#include <QPushButton>
#include <QThread>
#include <QWheelEvent>
#include <gtest/gtest.h>
#include <memory>
namespace {
QPushButton* find_performance_button(QWidget* control) {
const auto buttons = control->findChildren<QPushButton*>();
for (QPushButton* button : buttons) {
if (button->text().contains("性能测试"))
return button;
}
return nullptr;
}
void click_button(QPushButton* button) {
QPoint pos = button->rect().center();
QMouseEvent press(QEvent::MouseButtonPress, pos, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
QApplication::sendEvent(button, &press);
QMouseEvent release(QEvent::MouseButtonRelease, pos, Qt::LeftButton, Qt::NoButton, Qt::NoModifier);
QApplication::sendEvent(button, &release);
}
void double_click_button(QPushButton* button) {
QPoint pos = button->rect().center();
QMouseEvent press(QEvent::MouseButtonPress, pos, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
QApplication::sendEvent(button, &press);
QMouseEvent release(QEvent::MouseButtonRelease, pos, Qt::LeftButton, Qt::NoButton, Qt::NoModifier);
QApplication::sendEvent(button, &release);
QMouseEvent double_click(QEvent::MouseButtonDblClick, pos, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
QApplication::sendEvent(button, &double_click);
QMouseEvent double_release(QEvent::MouseButtonRelease, pos, Qt::LeftButton, Qt::NoButton, Qt::NoModifier);
QApplication::sendEvent(button, &double_release);
}
void click_plot(QWidget* plot, const QPoint& pos, QEvent::Type type) {
QMouseEvent event(type, pos, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
QApplication::sendEvent(plot, &event);
}
void wheel_plot(QWidget* plot, const QPoint& pos, int delta) {
QWheelEvent event(pos, plot->mapToGlobal(pos), QPoint(), QPoint(0, delta), Qt::NoButton, Qt::NoModifier, Qt::NoScrollPhase, false);
QApplication::sendEvent(plot, &event);
}
bool pump(QApplication& app, renderive::Latency_Eager_Plot& plot, int milliseconds) {
QElapsedTimer timer;
timer.start();
while (timer.elapsed() < milliseconds) {
plot.mark_render_state_dirty();
app.processEvents(QEventLoop::AllEvents, 5);
QThread::msleep(1);
}
return true;
}
}
TEST(Performance_Shower_Interaction, RepeatedToggleDoubleClickAndWheelStayResponsive) {
QApplication& app = *qobject_cast<QApplication*>(QApplication::instance());
renderive::start_render_scheduler();
renderive::Latency_Eager_Plot plot;
plot.resize(320, 240);
plot.init();
plot.show();
plot.start_render(240);
std::shared_ptr<renderive::Performance_Shower> performance_shower;
std::unique_ptr<QWidget> control(create_control(&plot, {}, &performance_shower));
ASSERT_NE(performance_shower, nullptr);
control->show();
app.processEvents();
QPushButton* performance_button = find_performance_button(control.get());
ASSERT_NE(performance_button, nullptr);
QElapsedTimer total;
total.start();
for (int i = 0; i < 40; ++i) {
click_button(performance_button);
pump(app, plot, 15);
ASSERT_LE(total.elapsed(), 5000);
}
double_click_button(performance_button);
pump(app, plot, 200);
performance_shower->setEnabled(true);
pump(app, plot, 200);
click_plot(&plot, QPoint(8, 8), QEvent::MouseButtonPress);
click_plot(&plot, QPoint(8, 8), QEvent::MouseButtonRelease);
click_plot(&plot, QPoint(8, 8), QEvent::MouseButtonDblClick);
click_plot(&plot, QPoint(8, 8), QEvent::MouseButtonRelease);
for (int i = 0; i < 12; ++i) {
wheel_plot(&plot, QPoint(8, 8), -120);
pump(app, plot, 10);
}
for (int i = 0; i < 12; ++i) {
wheel_plot(&plot, QPoint(8, 8), 120);
pump(app, plot, 10);
}
pump(app, plot, 200);
EXPECT_TRUE(performance_shower->enabled());
renderive::set_performance_shower_enabled(plot, false);
EXPECT_FALSE(renderive::performance_shower_enabled(plot));
auto compatibility_shower = renderive::attach_performance_shower(plot);
ASSERT_NE(compatibility_shower, nullptr);
EXPECT_EQ(renderive::attach_performance_shower(plot), compatibility_shower);
renderive::set_performance_shower_enabled(plot, true);
EXPECT_TRUE(renderive::performance_shower_enabled(plot));
renderive::detach_performance_shower(plot);
EXPECT_FALSE(renderive::performance_shower_enabled(plot));
plot.pause_render();
}
int main(int argc, char** argv) {
qputenv("QT_QPA_PLATFORM", "offscreen");
qputenv("RENDERIVE_PERFORMANCE_LOG", "0");
QApplication app(argc, argv);
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}