| 1 | // Copyright (C) 2018 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #include <private/qguiapplication_p.h> |
| 5 | #include <private/qeventpoint_p.h> |
| 6 | |
| 7 | #include <qpa/qplatformintegration.h> |
| 8 | |
| 9 | #include "qtestsupport_gui.h" |
| 10 | #include "qwindow.h" |
| 11 | |
| 12 | #include <QtCore/qtestsupport_core.h> |
| 13 | #include <QtCore/qthread.h> |
| 14 | #include <QtCore/QDebug> |
| 15 | |
| 16 | QT_BEGIN_NAMESPACE |
| 17 | |
| 18 | /*! |
| 19 | \since 5.0 |
| 20 | |
| 21 | Returns \c true, if \a window is active within \a timeout milliseconds. Otherwise returns \c false. |
| 22 | |
| 23 | The method is useful in tests that call QWindow::show() and rely on the window actually being |
| 24 | active (i.e. being visible and having focus) before proceeding. |
| 25 | |
| 26 | \note The method will time out and return \c false if another window prevents \a window from |
| 27 | becoming active. |
| 28 | |
| 29 | \note Since focus is an exclusive property, \a window may loose its focus to another window at |
| 30 | any time - even after the method has returned \c true. |
| 31 | |
| 32 | \sa qWaitForWindowExposed(), qWaitForWindowFocused(), QWindow::isActive() |
| 33 | */ |
| 34 | Q_GUI_EXPORT bool QTest::qWaitForWindowActive(QWindow *window, int timeout) |
| 35 | { |
| 36 | if (Q_UNLIKELY(!QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::WindowActivation))) { |
| 37 | qWarning() << "qWaitForWindowActive was called on a platform that doesn't support window" |
| 38 | << "activation. This means there is an error in the test and it should either" |
| 39 | << "check for the WindowActivation platform capability before calling" |
| 40 | << "qWaitForWindowActivate, use qWaitForWindowExposed instead, or skip the test." |
| 41 | << "Falling back to qWaitForWindowExposed." ; |
| 42 | return qWaitForWindowExposed(window, timeout); |
| 43 | } |
| 44 | return QTest::qWaitFor(predicate: [&]() { return window->isActive(); }, timeout); |
| 45 | } |
| 46 | |
| 47 | /*! |
| 48 | \since 6.7 |
| 49 | |
| 50 | Returns \c true, if \a window is the focus window within \a timeout. Otherwise returns \c false. |
| 51 | |
| 52 | The method is useful in tests that call QWindow::show() and rely on the window |
| 53 | having focus (for receiving keyboard events e.g.) before proceeding. |
| 54 | |
| 55 | \note The method will time out and return \c false if another window prevents \a window from |
| 56 | becoming focused. |
| 57 | |
| 58 | \note Since focus is an exclusive property, \a window may loose its focus to another window at |
| 59 | any time - even after the method has returned \c true. |
| 60 | |
| 61 | \sa qWaitForWindowExposed(), qWaitForWindowActive(), QGuiApplication::focusWindow() |
| 62 | */ |
| 63 | Q_GUI_EXPORT bool QTest::qWaitForWindowFocused(QWindow *window, QDeadlineTimer timeout) |
| 64 | { |
| 65 | return QTest::qWaitFor(predicate: [&]() { return qGuiApp->focusWindow() == window; }, deadline: timeout); |
| 66 | } |
| 67 | |
| 68 | /*! |
| 69 | \since 5.0 |
| 70 | |
| 71 | Returns \c true, if \a window is exposed within \a timeout milliseconds. Otherwise returns \c false. |
| 72 | |
| 73 | The method is useful in tests that call QWindow::show() and rely on the window actually being |
| 74 | being visible before proceeding. |
| 75 | |
| 76 | \note A window mapped to screen may still not be considered exposed, if the window client area is |
| 77 | not visible, e.g. because it is completely covered by other windows. |
| 78 | In such cases, the method will time out and return \c false. |
| 79 | |
| 80 | \sa qWaitForWindowActive(), QWindow::isExposed() |
| 81 | */ |
| 82 | Q_GUI_EXPORT bool QTest::qWaitForWindowExposed(QWindow *window, int timeout) |
| 83 | { |
| 84 | return QTest::qWaitFor(predicate: [&]() { return window->isExposed(); }, timeout); |
| 85 | } |
| 86 | |
| 87 | namespace QTest { |
| 88 | |
| 89 | QTouchEventSequence::~QTouchEventSequence() |
| 90 | { |
| 91 | if (commitWhenDestroyed) |
| 92 | QTouchEventSequence::commit(); |
| 93 | } |
| 94 | QTouchEventSequence& QTouchEventSequence::press(int touchId, const QPoint &pt, QWindow *window) |
| 95 | { |
| 96 | auto &p = point(touchId); |
| 97 | QMutableEventPoint::setGlobalPosition(p, arg: mapToScreen(window, pt)); |
| 98 | QMutableEventPoint::setState(p, arg: QEventPoint::State::Pressed); |
| 99 | return *this; |
| 100 | } |
| 101 | QTouchEventSequence& QTouchEventSequence::move(int touchId, const QPoint &pt, QWindow *window) |
| 102 | { |
| 103 | auto &p = point(touchId); |
| 104 | QMutableEventPoint::setGlobalPosition(p, arg: mapToScreen(window, pt)); |
| 105 | QMutableEventPoint::setState(p, arg: QEventPoint::State::Updated); |
| 106 | return *this; |
| 107 | } |
| 108 | QTouchEventSequence& QTouchEventSequence::release(int touchId, const QPoint &pt, QWindow *window) |
| 109 | { |
| 110 | auto &p = point(touchId); |
| 111 | QMutableEventPoint::setGlobalPosition(p, arg: mapToScreen(window, pt)); |
| 112 | QMutableEventPoint::setState(p, arg: QEventPoint::State::Released); |
| 113 | return *this; |
| 114 | } |
| 115 | QTouchEventSequence& QTouchEventSequence::stationary(int touchId) |
| 116 | { |
| 117 | auto &p = pointOrPreviousPoint(touchId); |
| 118 | QMutableEventPoint::setState(p, arg: QEventPoint::State::Stationary); |
| 119 | return *this; |
| 120 | } |
| 121 | |
| 122 | bool QTouchEventSequence::commit(bool processEvents) |
| 123 | { |
| 124 | if (points.isEmpty()) |
| 125 | return false; |
| 126 | QThread::sleep(nsec: std::chrono::milliseconds{1}); |
| 127 | bool ret = false; |
| 128 | if (targetWindow) |
| 129 | ret = qt_handleTouchEventv2(w: targetWindow, device, points: points.values()); |
| 130 | if (processEvents) |
| 131 | QCoreApplication::processEvents(); |
| 132 | previousPoints = points; |
| 133 | points.clear(); |
| 134 | return ret; |
| 135 | } |
| 136 | |
| 137 | QTouchEventSequence::QTouchEventSequence(QWindow *window, QPointingDevice *aDevice, bool autoCommit) |
| 138 | : targetWindow(window), device(aDevice), commitWhenDestroyed(autoCommit) |
| 139 | { |
| 140 | } |
| 141 | |
| 142 | QPoint QTouchEventSequence::mapToScreen(QWindow *window, const QPoint &pt) |
| 143 | { |
| 144 | if (window) |
| 145 | return window->mapToGlobal(pos: pt); |
| 146 | return targetWindow ? targetWindow->mapToGlobal(pos: pt) : pt; |
| 147 | } |
| 148 | |
| 149 | QEventPoint &QTouchEventSequence::point(int touchId) |
| 150 | { |
| 151 | if (!points.contains(key: touchId)) |
| 152 | points[touchId] = QEventPoint(touchId); |
| 153 | return points[touchId]; |
| 154 | } |
| 155 | |
| 156 | QEventPoint &QTouchEventSequence::pointOrPreviousPoint(int touchId) |
| 157 | { |
| 158 | if (!points.contains(key: touchId)) { |
| 159 | if (previousPoints.contains(key: touchId)) |
| 160 | points[touchId] = previousPoints.value(key: touchId); |
| 161 | else |
| 162 | points[touchId] = QEventPoint(touchId); |
| 163 | } |
| 164 | return points[touchId]; |
| 165 | } |
| 166 | |
| 167 | } // namespace QTest |
| 168 | |
| 169 | QT_END_NAMESPACE |
| 170 | |