aboutsummaryrefslogtreecommitdiffstats
path: root/tests/auto
diff options
context:
space:
mode:
authorMitch Curtis <mitch.curtis@qt.io>2020-11-16 16:33:19 +0100
committerMitch Curtis <mitch.curtis@qt.io>2021-04-22 15:56:42 +0200
commit2a65d8127dd53266342584a862ed7836d118c95e (patch)
tree34974806c04b3f8b1492079bb362e5c929f64a24 /tests/auto
parenta372b35d93d4deb14ba9b855c49bcbd328b32381 (diff)
Add clickButton and doubleClickButton functions to visualtestutils.h
These helper functions save the hassle of calculating the click position within the window manually, and do a lot of other sanity checks in addition. Change-Id: I8cb53fab9c1addea15e8066e347a5c4af2700c2e Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Diffstat (limited to 'tests/auto')
-rw-r--r--tests/auto/shared/visualtestutil.cpp74
-rw-r--r--tests/auto/shared/visualtestutil.h5
2 files changed, 79 insertions, 0 deletions
diff --git a/tests/auto/shared/visualtestutil.cpp b/tests/auto/shared/visualtestutil.cpp
index 4e33c6f1..48201832 100644
--- a/tests/auto/shared/visualtestutil.cpp
+++ b/tests/auto/shared/visualtestutil.cpp
@@ -41,6 +41,7 @@
#include <QtGui/QCursor>
#include <QtCore/QCoreApplication>
#include <QtQml/QQmlFile>
+#include <QtTest/qsignalspy.h>
#include <QtTest/QTest>
bool QQuickVisualTestUtil::delegateVisible(QQuickItem *item)
@@ -167,3 +168,76 @@ void QQuickVisualTestUtil::MnemonicKeySimulator::click(Qt::Key key)
press(key);
release(key);
}
+
+
+bool QQuickVisualTestUtil::verifyButtonClickable(QQuickAbstractButton *button)
+{
+ if (!button->window()) {
+ qWarning() << "button" << button << "doesn't have an associated window";
+ return false;
+ }
+
+ if (!button->isEnabled()) {
+ qWarning() << "button" << button << "is not enabled";
+ return false;
+ }
+
+ if (!button->isVisible()) {
+ qWarning() << "button" << button << "is not visible";
+ return false;
+ }
+
+ if (button->width() <= 0.0) {
+ qWarning() << "button" << button << "must have a width greater than 0";
+ return false;
+ }
+
+ if (button->height() <= 0.0) {
+ qWarning() << "button" << button << "must have a height greater than 0";
+ return false;
+ }
+
+ return true;
+}
+
+bool QQuickVisualTestUtil::clickButton(QQuickAbstractButton *button)
+{
+ if (!verifyButtonClickable(button))
+ return false;
+
+ QSignalSpy spy(button, &QQuickAbstractButton::clicked);
+ if (!spy.isValid()) {
+ qWarning() << "button" << button << "must have a valid clicked signal";
+ return false;
+ }
+
+ const QPoint buttonCenter = button->mapToScene(QPointF(button->width() / 2, button->height() / 2)).toPoint();
+ QTest::mouseClick(button->window(), Qt::LeftButton, Qt::NoModifier, buttonCenter);
+ if (spy.count() != 1) {
+ qWarning() << "clicked signal of button" << button << "was not emitted after clicking";
+ return false;
+ }
+
+ return true;
+}
+
+bool QQuickVisualTestUtil::doubleClickButton(QQuickAbstractButton *button)
+{
+ if (!verifyButtonClickable(button))
+ return false;
+
+ QSignalSpy spy(button, &QQuickAbstractButton::clicked);
+ if (!spy.isValid()) {
+ qWarning() << "button" << button << "must have a valid doubleClicked signal";
+ return false;
+ }
+
+ const QPoint buttonCenter = button->mapToScene(QPointF(button->width() / 2, button->height() / 2)).toPoint();
+ QTest::mouseDClick(button->window(), Qt::LeftButton, Qt::NoModifier, buttonCenter);
+ if (spy.count() != 1) {
+ qWarning() << "doubleClicked signal of button" << button << "was not emitted after double-clicking";
+ return false;
+ }
+
+ return true;
+}
diff --git a/tests/auto/shared/visualtestutil.h b/tests/auto/shared/visualtestutil.h
index 78c625ea..83c47b2d 100644
--- a/tests/auto/shared/visualtestutil.h
+++ b/tests/auto/shared/visualtestutil.h
@@ -45,6 +45,7 @@
#include <QtQuick/private/qquickitem_p.h>
#include <QtQuickControls2/qquickstyle.h>
#include <QtQuickTemplates2/private/qquickapplicationwindow_p.h>
+#include <QtQuickTemplates2/private/qquickabstractbutton_p.h>
#include "util.h"
@@ -211,6 +212,10 @@ namespace QQuickVisualTestUtil
QPointer<QWindow> m_window;
Qt::KeyboardModifiers m_modifiers;
};
+
+ [[nodiscard]] bool verifyButtonClickable(QQuickAbstractButton *button);
+ [[nodiscard]] bool clickButton(QQuickAbstractButton *button);
+ [[nodiscard]] bool doubleClickButton(QQuickAbstractButton *button);
}
#define QQUICK_VERIFY_POLISH(item) \