From 2a65d8127dd53266342584a862ed7836d118c95e Mon Sep 17 00:00:00 2001 From: Mitch Curtis Date: Mon, 16 Nov 2020 16:33:19 +0100 Subject: 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 --- tests/auto/shared/visualtestutil.cpp | 74 ++++++++++++++++++++++++++++++++++++ tests/auto/shared/visualtestutil.h | 5 +++ 2 files changed, 79 insertions(+) (limited to 'tests/auto') 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 #include #include +#include #include 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 #include #include +#include #include "util.h" @@ -211,6 +212,10 @@ namespace QQuickVisualTestUtil QPointer 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) \ -- cgit v1.2.3