| 1 | // Copyright (C) 2021 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | #ifndef QQUICKVISUALTESTUTILS_P_H |
| 5 | #define QQUICKVISUALTESTUTILS_P_H |
| 6 | |
| 7 | // |
| 8 | // W A R N I N G |
| 9 | // ------------- |
| 10 | // |
| 11 | // This file is not part of the Qt API. It exists purely as an |
| 12 | // implementation detail. This header file may change from version to |
| 13 | // version without notice, or even be removed. |
| 14 | // |
| 15 | // We mean it. |
| 16 | // |
| 17 | |
| 18 | #include <QtGui/private/qguiapplication_p.h> |
| 19 | #include <QtGui/qpa/qplatformintegration.h> |
| 20 | #include <QtQml/qqmlexpression.h> |
| 21 | #include <QtQuick/private/qquickitem_p.h> |
| 22 | |
| 23 | #include <private/qmlutils_p.h> |
| 24 | |
| 25 | #include <QtCore/qpointer.h> |
| 26 | |
| 27 | QT_BEGIN_NAMESPACE |
| 28 | |
| 29 | class QQuickItemView; |
| 30 | class QQuickWindow; |
| 31 | |
| 32 | namespace QQuickVisualTestUtils |
| 33 | { |
| 34 | QQuickItem *findVisibleChild(QQuickItem *parent, const QString &objectName); |
| 35 | |
| 36 | void dumpTree(QQuickItem *parent, int depth = 0); |
| 37 | |
| 38 | void moveMouseAway(QQuickWindow *window); |
| 39 | void centerOnScreen(QQuickWindow *window); |
| 40 | |
| 41 | template<typename F> |
| 42 | void forEachStep(int steps, F &&func) |
| 43 | { |
| 44 | for (int i = 0; i < steps; ++i) { |
| 45 | const qreal progress = qreal(i) / steps; |
| 46 | func(progress); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | [[nodiscard]] QPoint lerpPoints(const QPoint &point1, const QPoint &point2, qreal t); |
| 51 | |
| 52 | class [[nodiscard]] PointLerper |
| 53 | { |
| 54 | public: |
| 55 | PointLerper(QQuickWindow *window, |
| 56 | const QPointingDevice *pointingDevice = QPointingDevice::primaryPointingDevice()); |
| 57 | |
| 58 | void move(const QPoint &pos, int steps = 10, int delayInMilliseconds = 1); |
| 59 | void move(int x, int y, int steps = 10, int delayInMilliseconds = 1); |
| 60 | |
| 61 | private: |
| 62 | QQuickWindow *mWindow = nullptr; |
| 63 | const QPointingDevice *mPointingDevice = nullptr; |
| 64 | QPoint mFrom; |
| 65 | }; |
| 66 | |
| 67 | [[nodiscard]] bool isDelegateVisible(QQuickItem *item); |
| 68 | |
| 69 | /* |
| 70 | Find an item with the specified objectName. If index is supplied then the |
| 71 | item must also evaluate the {index} expression equal to index |
| 72 | */ |
| 73 | template<typename T> |
| 74 | T *findItem(QQuickItem *parent, const QString &objectName, int index = -1) |
| 75 | { |
| 76 | using namespace Qt::StringLiterals; |
| 77 | |
| 78 | const QMetaObject &mo = T::staticMetaObject; |
| 79 | for (int i = 0; i < parent->childItems().size(); ++i) { |
| 80 | QQuickItem *item = qobject_cast<QQuickItem*>(o: parent->childItems().at(i)); |
| 81 | if (!item) |
| 82 | continue; |
| 83 | if (mo.cast(obj: item) && (objectName.isEmpty() || item->objectName() == objectName)) { |
| 84 | if (index != -1) { |
| 85 | QQmlContext *context = qmlContext(item); |
| 86 | if (!context->isValid()) |
| 87 | continue; |
| 88 | QQmlExpression e(context, item, u"index"_s ); |
| 89 | if (e.evaluate().toInt() == index) |
| 90 | return static_cast<T*>(item); |
| 91 | } else { |
| 92 | return static_cast<T*>(item); |
| 93 | } |
| 94 | } |
| 95 | item = findItem<T>(item, objectName, index); |
| 96 | if (item) |
| 97 | return static_cast<T*>(item); |
| 98 | } |
| 99 | |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | template<typename T> |
| 104 | QList<T*> findItems(QQuickItem *parent, const QString &objectName, bool visibleOnly = true) |
| 105 | { |
| 106 | QList<T*> items; |
| 107 | const QMetaObject &mo = T::staticMetaObject; |
| 108 | for (int i = 0; i < parent->childItems().size(); ++i) { |
| 109 | QQuickItem *item = qobject_cast<QQuickItem*>(o: parent->childItems().at(i)); |
| 110 | if (!item || (visibleOnly && (!item->isVisible() || QQuickItemPrivate::get(item)->culled))) |
| 111 | continue; |
| 112 | if (mo.cast(obj: item) && (objectName.isEmpty() || item->objectName() == objectName)) |
| 113 | items.append(static_cast<T*>(item)); |
| 114 | items += findItems<T>(item, objectName); |
| 115 | } |
| 116 | |
| 117 | return items; |
| 118 | } |
| 119 | |
| 120 | template<typename T> |
| 121 | QList<T*> findItems(QQuickItem *parent, const QString &objectName, const QList<int> &indexes) |
| 122 | { |
| 123 | QList<T*> items; |
| 124 | for (int i=0; i<indexes.size(); i++) |
| 125 | items << qobject_cast<QQuickItem*>(findItem<T>(parent, objectName, indexes[i])); |
| 126 | return items; |
| 127 | } |
| 128 | |
| 129 | bool compareImages(const QImage &ia, const QImage &ib, QString *errorMessage); |
| 130 | |
| 131 | struct SignalMultiSpy : public QObject |
| 132 | { |
| 133 | Q_OBJECT |
| 134 | public: |
| 135 | QList<QObject *> senders; |
| 136 | QList<QByteArray> signalNames; |
| 137 | |
| 138 | template <typename Func1> |
| 139 | QMetaObject::Connection connectToSignal(const typename QtPrivate::FunctionPointer<Func1>::Object *obj, Func1 signal, |
| 140 | Qt::ConnectionType type = Qt::AutoConnection) |
| 141 | { |
| 142 | return connect(obj, signal, this, &SignalMultiSpy::receive, type); |
| 143 | } |
| 144 | |
| 145 | void clear() { |
| 146 | senders.clear(); |
| 147 | signalNames.clear(); |
| 148 | } |
| 149 | |
| 150 | public Q_SLOTS: |
| 151 | void receive() { |
| 152 | QMetaMethod m = sender()->metaObject()->method(index: senderSignalIndex()); |
| 153 | senders << sender(); |
| 154 | signalNames << m.name(); |
| 155 | } |
| 156 | }; |
| 157 | |
| 158 | enum class FindViewDelegateItemFlag { |
| 159 | None = 0x0, |
| 160 | PositionViewAtIndex = 0x01 |
| 161 | }; |
| 162 | Q_DECLARE_FLAGS(FindViewDelegateItemFlags, FindViewDelegateItemFlag) |
| 163 | |
| 164 | #if QT_CONFIG(quick_itemview) |
| 165 | QQuickItem* findViewDelegateItem(QQuickItemView *itemView, int index, |
| 166 | FindViewDelegateItemFlags flags = FindViewDelegateItemFlag::PositionViewAtIndex); |
| 167 | #endif |
| 168 | |
| 169 | /*! |
| 170 | \internal |
| 171 | |
| 172 | Same as above except allows use in QTRY_* functions without having to call it again |
| 173 | afterwards to assign the delegate. |
| 174 | */ |
| 175 | template<typename T> |
| 176 | [[nodiscard]] bool findViewDelegateItem(QQuickItemView *itemView, int index, T &delegateItem, |
| 177 | FindViewDelegateItemFlags flags = FindViewDelegateItemFlag::PositionViewAtIndex) |
| 178 | { |
| 179 | delegateItem = qobject_cast<T>(findViewDelegateItem(itemView, index, flags)); |
| 180 | return delegateItem != nullptr; |
| 181 | } |
| 182 | |
| 183 | class QQuickApplicationHelper |
| 184 | { |
| 185 | public: |
| 186 | QQuickApplicationHelper(QQmlDataTest *testCase, const QString &testFilePath, |
| 187 | const QVariantMap &initialProperties = {}, |
| 188 | const QStringList &qmlImportPaths = {}); |
| 189 | |
| 190 | // Return a C-style string instead of QString because that's what QTest uses for error messages, |
| 191 | // so it saves code at the calling site. |
| 192 | inline const char *failureMessage() const |
| 193 | { |
| 194 | return errorMessage.constData(); |
| 195 | } |
| 196 | |
| 197 | QQmlEngine engine; |
| 198 | QScopedPointer<QObject> cleanup; |
| 199 | QQuickWindow *window = nullptr; |
| 200 | |
| 201 | bool ready = false; |
| 202 | // Store as a byte array so that we can return its raw data safely; |
| 203 | // using qPrintable() in failureMessage() will construct a throwaway QByteArray |
| 204 | // that is destroyed before the function returns. |
| 205 | QByteArray errorMessage; |
| 206 | }; |
| 207 | |
| 208 | class MnemonicKeySimulator |
| 209 | { |
| 210 | Q_DISABLE_COPY(MnemonicKeySimulator) |
| 211 | public: |
| 212 | explicit MnemonicKeySimulator(QWindow *window); |
| 213 | |
| 214 | void press(Qt::Key key); |
| 215 | void release(Qt::Key key); |
| 216 | void click(Qt::Key key); |
| 217 | |
| 218 | private: |
| 219 | QPointer<QWindow> m_window; |
| 220 | Qt::KeyboardModifiers m_modifiers; |
| 221 | }; |
| 222 | |
| 223 | QPoint mapCenterToWindow(const QQuickItem *item); |
| 224 | QPoint mapToWindow(const QQuickItem *item, qreal relativeX, qreal relativeY); |
| 225 | QPoint mapToWindow(const QQuickItem *item, const QPointF &relativePos); |
| 226 | } |
| 227 | |
| 228 | #define QQUICK_VERIFY_POLISH(item) \ |
| 229 | QTRY_COMPARE(QQuickItemPrivate::get(item)->polishScheduled, false) |
| 230 | |
| 231 | #define SKIP_IF_NO_WINDOW_ACTIVATION \ |
| 232 | if (!(QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::WindowActivation))) \ |
| 233 | QSKIP("Window activation is not supported on this platform"); |
| 234 | |
| 235 | #define SKIP_IF_NO_MOUSE_HOVER \ |
| 236 | do { \ |
| 237 | if ((QGuiApplication::platformName() == QLatin1String("offscreen")) \ |
| 238 | || (QGuiApplication::platformName() == QLatin1String("minimal"))) \ |
| 239 | QSKIP("Mouse hovering is not supported on the offscreen/minimal platforms"); \ |
| 240 | } while (false) |
| 241 | |
| 242 | QT_END_NAMESPACE |
| 243 | |
| 244 | #endif // QQUICKVISUALTESTUTILS_P_H |
| 245 | |