##// END OF EJS Templates
Added GUI Test Utils lib to share boilerplate code for UI testing...
jeandet -
r1360:d88ad553a61d
parent child
Show More
@@ -0,0 +1,10
1 FILE (GLOB_RECURSE GUITestUtilsSources
2 GUITestUtils.h
3 GUITestUtils.cpp
4 )
5
6 add_library(GUITestUtils ${GUITestUtilsSources})
7 target_link_libraries(GUITestUtils Qt5::Gui Qt5::Widgets Qt5::Test sciqlopgui)
8 target_include_directories(GUITestUtils PUBLIC
9 $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>/
10 )
1 NO CONTENT: new file 100644
@@ -0,0 +1,134
1 #ifndef GUITESTUTILS_H
2 #define GUITESTUTILS_H
3
4 #include <Common/cpp_utils.h>
5 #include <QPoint>
6 #include <QCursor>
7 #include <QMouseEvent>
8 #include <QCoreApplication>
9 #include <QtTest>
10 #include <QDesktopWidget>
11
12 #include <qcustomplot.h>
13
14 QPoint center(QWidget* widget)
15 {
16 return QPoint{widget->width()/2,widget->height()/2};
17 }
18
19 HAS_METHOD(viewport)
20
21 HAS_METHOD(topLevelItem)
22
23 template<typename T>
24 void mouseMove(T* widget, QPoint pos, Qt::MouseButton mouseModifier)
25 {
26 QCursor::setPos(widget->mapToGlobal(pos));
27 QMouseEvent event(QEvent::MouseMove, pos, Qt::NoButton, mouseModifier, Qt::NoModifier);
28 if constexpr(has_viewport<T>)
29 {
30 qApp->sendEvent(widget->viewport(), &event);
31 }
32 else
33 {
34 qApp->sendEvent(widget, &event);
35 }
36 qApp->processEvents();
37 }
38
39
40 template <typename T>
41 void setMouseTracking(T* widget)
42 {
43 if constexpr(has_viewport<T>)
44 {
45 widget->viewport()->setMouseTracking(true);
46 }
47 else
48 {
49 widget->setMouseTracking(true);
50 }
51 }
52
53 template <>
54 void setMouseTracking<QCustomPlot>(QCustomPlot* widget)
55 {
56 widget->setMouseTracking(true);
57 }
58
59 template <typename T, typename T2>
60 auto getItem(T* widget, T2 itemIndex)
61 {
62 if constexpr(has_topLevelItem<T>)
63 {
64 return widget->topLevelItem(itemIndex);
65 }
66 else
67 {
68 return widget->item(itemIndex);
69 }
70 }
71
72 #define SELECT_ITEM(widget, itemIndex, item)\
73 auto item = getItem(widget, itemIndex);\
74 {\
75 auto itemCenterPos = widget->visualItemRect(item).center();\
76 QTest::mouseClick(widget->viewport(), Qt::LeftButton, Qt::NoModifier, itemCenterPos);\
77 QVERIFY(widget->selectedItems().size() > 0);\
78 QVERIFY(widget->selectedItems().contains(item));\
79 }
80
81
82 #define GET_CHILD_WIDGET_FOR_GUI_TESTS(parent, child, childType, childName)\
83 childType* child = parent.findChild<childType*>(childName); \
84 QVERIFY(child!=Q_NULLPTR); \
85 setMouseTracking(child);
86
87 template<typename T1, typename T2, typename T3, typename T4=void>
88 void dragnDropItem(T1* sourceWidget, T2* destWidget, T3* item, T4* destItem=Q_NULLPTR)
89 {
90 auto itemCenterPos = sourceWidget->visualItemRect(item).center();
91 if constexpr(has_viewport<T1>)
92 {
93 QTest::mousePress(sourceWidget->viewport(), Qt::LeftButton, Qt::NoModifier, itemCenterPos);
94 }
95 else
96 {
97 QTest::mousePress(sourceWidget, Qt::LeftButton, Qt::NoModifier, itemCenterPos);
98 }
99 mouseMove(sourceWidget,itemCenterPos, Qt::LeftButton);
100 itemCenterPos+=QPoint(0,-10);
101 QTimer::singleShot(100,[destWidget,destItem](){
102 mouseMove(destWidget, destWidget->rect().center(),Qt::LeftButton);
103 mouseMove(destWidget, destWidget->rect().center()+QPoint(0,-10),Qt::LeftButton);
104 if constexpr(!std::is_same_v<void, T4>)
105 {
106 auto destItemCenterPos = destWidget->visualItemRect(destItem).center();
107 QTest::mouseRelease(destWidget, Qt::LeftButton, Qt::NoModifier, destItemCenterPos);
108 }
109 else if constexpr(has_viewport<T2>)
110 {
111 QTest::mouseRelease(destWidget->viewport(), Qt::LeftButton);
112 }
113 else
114 {
115 QTest::mouseRelease(destWidget, Qt::LeftButton);
116 }
117 QTest::mouseRelease(destWidget->viewport(), Qt::LeftButton);
118 });
119 mouseMove(sourceWidget,itemCenterPos,Qt::LeftButton);
120 }
121
122 #define PREPARE_GUI_TEST(main_widget)\
123 main_widget.setGeometry(QRect(QPoint(QApplication::desktop()->geometry().center() - QPoint(250, 250)),\
124 QSize(500, 500)));\
125 main_widget.show();\
126 qApp->setActiveWindow(&main_widget);\
127 QVERIFY(QTest::qWaitForWindowActive(&main_widget))
128
129 #define GET_CHILD_WIDGET_FOR_GUI_TESTS(parent, child, childType, childName)\
130 childType* child = parent.findChild<childType*>(childName); \
131 QVERIFY(child!=Q_NULLPTR); \
132 setMouseTracking(child);
133
134 #endif
@@ -1,1 +1,2
1 declare_test(simple_graph simple_graph simple_graph/main.cpp "sciqlopgui;TestUtils;Qt5::Test")
1 subdirs(GUITestUtils)
2 declare_test(simple_graph simple_graph simple_graph/main.cpp "sciqlopgui;TestUtils;GUITestUtils;Qt5::Test")
@@ -3,14 +3,16
3 3 #include <QString>
4 4 #include <QScreen>
5 5 #include <QMainWindow>
6 #include <QWheelEvent>
6 7
8 #include <qcustomplot.h>
7 9
8 10 #include <SqpApplication.h>
9 11 #include <Variable/VariableController2.h>
10 12
11 13 #include <Visualization/VisualizationGraphWidget.h>
12 14 #include <TestProviders.h>
13
15 #include <GUITestUtils.h>
14 16
15 17 class A_SimpleGraph : public QObject {
16 18 Q_OBJECT
@@ -24,12 +26,22 public:
24 26 private slots:
25 27 void scrolls_with_mouse_wheel()
26 28 {
27 VisualizationGraphWidget w{Q_NULLPTR};
29 VisualizationGraphWidget w;
30 PREPARE_GUI_TEST(w);
28 31 auto provider = std::make_shared<SimpleRange<10>>();
29 32 auto range = DateTimeRange::fromDateTime(QDate(2018,8,7),QTime(14,00),
30 33 QDate(2018,8,7),QTime(16,00));
31 34 auto var = static_cast<SqpApplication*>(qApp)->variableController().createVariable("V1", {{"","scalar"}}, provider, range);
35 while(!static_cast<SqpApplication*>(qApp)->variableController().isReady(var))QCoreApplication::processEvents();
32 36 w.addVariable(var, range);
37 GET_CHILD_WIDGET_FOR_GUI_TESTS(w,plot,QCustomPlot,"widget");
38 auto cent = center(static_cast<QWidget*>(plot));
39 for(auto i=0;i<10;i++)
40 {
41 QTest::mousePress(plot, Qt::LeftButton, Qt::NoModifier, cent, 10);
42 QTest::mouseMove(plot, {cent.x()+100,cent.y()},10);
43 QTest::mouseRelease(plot,Qt::LeftButton);
44 }
33 45 while(!static_cast<SqpApplication*>(qApp)->variableController().isReady(var))QCoreApplication::processEvents();
34 46 }
35 47 };
General Comments 0
You need to be logged in to leave comments. Login now