GUITestUtils.h
171 lines
| 5.3 KiB
| text/x-c
|
CLexer
r1360 | #ifndef GUITESTUTILS_H | |||
#define GUITESTUTILS_H | ||||
#include <Common/cpp_utils.h> | ||||
r1406 | #include <QCoreApplication> | |||
r1360 | #include <QCursor> | |||
r1406 | #include <QDesktopWidget> | |||
r1360 | #include <QMouseEvent> | |||
r1406 | #include <QPoint> | |||
r1360 | #include <QtTest> | |||
r1370 | #include <SqpApplication.h> | |||
r1406 | #include <Variable/VariableController2.h> | |||
#include <qcustomplot.h> | ||||
r1370 | ||||
r1361 | template <typename T> | |||
QPoint center(T* widget) | ||||
r1360 | { | |||
r1406 | return QPoint { widget->width() / 2, widget->height() / 2 }; | |||
r1360 | } | |||
HAS_METHOD(viewport) | ||||
r1361 | template <typename T> | |||
r1406 | static inline constexpr bool is_QWidgetOrDerived = std::is_base_of<QWidget, T>::value; | |||
r1361 | ||||
r1406 | template <typename T> | |||
using viewport_type = decltype(std::declval<T>().viewport()); | ||||
r1361 | ||||
r1360 | HAS_METHOD(topLevelItem) | |||
r1406 | template <typename T> | |||
r1360 | void mouseMove(T* widget, QPoint pos, Qt::MouseButton mouseModifier) | |||
{ | ||||
QCursor::setPos(widget->mapToGlobal(pos)); | ||||
QMouseEvent event(QEvent::MouseMove, pos, Qt::NoButton, mouseModifier, Qt::NoModifier); | ||||
r1406 | if constexpr (has_viewport<T>) | |||
r1360 | { | |||
r1406 | if constexpr (is_QWidgetOrDerived<viewport_type<T>>) | |||
r1362 | { | |||
qApp->sendEvent(widget->viewport(), &event); | ||||
} | ||||
else | ||||
{ | ||||
qApp->sendEvent(widget, &event); | ||||
} | ||||
r1360 | } | |||
else | ||||
{ | ||||
qApp->sendEvent(widget, &event); | ||||
} | ||||
qApp->processEvents(); | ||||
} | ||||
template <typename T> | ||||
void setMouseTracking(T* widget) | ||||
{ | ||||
r1406 | if constexpr (has_viewport<T>) | |||
r1360 | { | |||
r1406 | if constexpr (is_QWidgetOrDerived<viewport_type<T>>) | |||
r1362 | { | |||
widget->viewport()->setMouseTracking(true); | ||||
} | ||||
else | ||||
{ | ||||
widget->setMouseTracking(true); | ||||
} | ||||
r1360 | } | |||
else | ||||
{ | ||||
widget->setMouseTracking(true); | ||||
} | ||||
} | ||||
template <typename T, typename T2> | ||||
auto getItem(T* widget, T2 itemIndex) | ||||
{ | ||||
r1406 | if constexpr (has_topLevelItem<T>) | |||
r1360 | { | |||
r1406 | return widget->topLevelItem(itemIndex); | |||
r1360 | } | |||
else | ||||
{ | ||||
r1406 | return widget->item(itemIndex); | |||
r1360 | } | |||
} | ||||
r1406 | #define SELECT_ITEM(widget, itemIndex, item) \ | |||
auto item = getItem(widget, itemIndex); \ | ||||
{ \ | ||||
auto itemCenterPos = widget->visualItemRect(item).center(); \ | ||||
QTest::mouseClick(widget->viewport(), Qt::LeftButton, Qt::NoModifier, itemCenterPos); \ | ||||
QVERIFY(widget->selectedItems().size() > 0); \ | ||||
QVERIFY(widget->selectedItems().contains(item)); \ | ||||
r1360 | } | |||
r1406 | #define GET_CHILD_WIDGET_FOR_GUI_TESTS(parent, child, childType, childName) \ | |||
childType* child = parent.findChild<childType*>(childName); \ | ||||
QVERIFY(child != Q_NULLPTR); \ | ||||
r1360 | setMouseTracking(child); | |||
r1406 | template <typename T1, typename T2, typename T3, typename T4 = void> | |||
void dragnDropItem(T1* sourceWidget, T2* destWidget, T3* item, T4* destItem = Q_NULLPTR) | ||||
r1360 | { | |||
auto itemCenterPos = sourceWidget->visualItemRect(item).center(); | ||||
r1406 | if constexpr (has_viewport<T1>) | |||
r1360 | { | |||
QTest::mousePress(sourceWidget->viewport(), Qt::LeftButton, Qt::NoModifier, itemCenterPos); | ||||
} | ||||
else | ||||
{ | ||||
QTest::mousePress(sourceWidget, Qt::LeftButton, Qt::NoModifier, itemCenterPos); | ||||
} | ||||
r1406 | mouseMove(sourceWidget, itemCenterPos, Qt::LeftButton); | |||
itemCenterPos += QPoint(0, -10); | ||||
QTimer::singleShot(100, [destWidget, destItem]() { | ||||
mouseMove(destWidget, destWidget->rect().center(), Qt::LeftButton); | ||||
mouseMove(destWidget, destWidget->rect().center() + QPoint(0, -10), Qt::LeftButton); | ||||
if constexpr (!std::is_same_v<void, T4>) | ||||
r1360 | { | |||
auto destItemCenterPos = destWidget->visualItemRect(destItem).center(); | ||||
QTest::mouseRelease(destWidget, Qt::LeftButton, Qt::NoModifier, destItemCenterPos); | ||||
} | ||||
r1406 | else if constexpr (has_viewport<T2>) | |||
r1360 | { | |||
QTest::mouseRelease(destWidget->viewport(), Qt::LeftButton); | ||||
} | ||||
else | ||||
{ | ||||
QTest::mouseRelease(destWidget, Qt::LeftButton); | ||||
} | ||||
QTest::mouseRelease(destWidget->viewport(), Qt::LeftButton); | ||||
}); | ||||
r1406 | mouseMove(sourceWidget, itemCenterPos, Qt::LeftButton); | |||
r1360 | } | |||
r1370 | template <typename T> | |||
void scroll_graph(T* w, int dx) | ||||
{ | ||||
auto cent = center(w); | ||||
QTest::mousePress(w, Qt::LeftButton, Qt::NoModifier, cent, 1); | ||||
r1406 | mouseMove(w, { cent.x() + dx, cent.y() }, Qt::LeftButton); | |||
r1370 | QTest::mouseRelease(w, Qt::LeftButton); | |||
} | ||||
r1406 | ALIAS_TEMPLATE_FUNCTION(isReady, static_cast<SqpApplication*>(qApp)->variableController().isReady) | |||
r1370 | ||||
r1420 | void waitForVar(std::shared_ptr<Variable2> var) | |||
r1370 | { | |||
while (!isReady(var)) | ||||
QCoreApplication::processEvents(); | ||||
} | ||||
r1406 | template <typename T> | |||
r1369 | bool prepare_gui_test(T* w) | |||
{ | ||||
r1406 | w->setGeometry(QRect( | |||
QPoint(QApplication::desktop()->geometry().center() - QPoint(250, 250)), QSize(500, 500))); | ||||
r1369 | w->show(); | |||
qApp->setActiveWindow(w); | ||||
return QTest::qWaitForWindowActive(w); | ||||
} | ||||
r1360 | ||||
r1406 | #define GET_CHILD_WIDGET_FOR_GUI_TESTS(parent, child, childType, childName) \ | |||
childType* child = parent.findChild<childType*>(childName); \ | ||||
QVERIFY(child != Q_NULLPTR); \ | ||||
r1360 | setMouseTracking(child); | |||
#endif | ||||