Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
visualtestutils_p.h
Go to the documentation of this file.
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
28
29class QQuickItemView;
30class QQuickWindow;
31
33{
34 QQuickItem *findVisibleChild(QQuickItem *parent, const QString &objectName);
35
36 void dumpTree(QQuickItem *parent, int depth = 0);
37
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:
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) {
81 if (!item)
82 continue;
83 if (mo.cast(item) && (objectName.isEmpty() || item->objectName() == objectName)) {
84 if (index != -1) {
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) {
110 if (!item || (visibleOnly && (!item->isVisible() || QQuickItemPrivate::get(item)->culled)))
111 continue;
112 if (mo.cast(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 {
134 public:
135 QList<QObject *> senders;
136 QList<QByteArray> signalNames;
137
138 template <typename Func1>
144
145 void clear() {
146 senders.clear();
148 }
149
150 public Q_SLOTS:
151 void receive() {
152 QMetaMethod m = sender()->metaObject()->method(senderSignalIndex());
153 senders << sender();
154 signalNames << m.name();
155 }
156 };
157
159 None = 0x0,
161 };
162 Q_DECLARE_FLAGS(FindViewDelegateItemFlags, FindViewDelegateItemFlag)
163
164#if QT_CONFIG(quick_itemview)
166 FindViewDelegateItemFlags flags = FindViewDelegateItemFlag::PositionViewAtIndex);
167#endif
168
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
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
198 QScopedPointer<QObject> cleanup;
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.
206 };
207
209 {
210 Q_DISABLE_COPY(MnemonicKeySimulator)
211 public:
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
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 \
232if (!(QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::WindowActivation))) \
233 QSKIP("Window activation is not supported on this platform");
234
236
237#endif // QQUICKVISUALTESTUTILS_P_H
\inmodule QtCore
Definition qbytearray.h:57
const char * constData() const noexcept
Returns a pointer to the const data stored in the byte array.
Definition qbytearray.h:124
bool isVisible() const
Returns true if the item is visible; otherwise, false is returned.
\inmodule QtGui
Definition qimage.h:37
const_reference at(qsizetype i) const noexcept
Definition qlist.h:446
void append(parameter_type t)
Definition qlist.h:458
void clear()
Definition qlist.h:434
\inmodule QtCore
Definition qmetaobject.h:19
\inmodule QtCore Represents a handle to a signal-slot (or signal-functor) connection.
\inmodule QtCore
Definition qobject.h:103
int senderSignalIndex() const
Definition qobject.cpp:2700
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2960
QObject * sender() const
Returns a pointer to the object that sent the signal, if called in a slot activated by a signal; othe...
Definition qobject.cpp:2658
\inmodule QtCore\reentrant
Definition qpoint.h:217
\inmodule QtCore\reentrant
Definition qpoint.h:25
The QPointingDevice class describes a device from which mouse, touch or tablet events originate.
static const QPointingDevice * primaryPointingDevice(const QString &seatName=QString())
Returns the primary pointing device (the core pointer, traditionally assumed to be a mouse) on the gi...
The QQmlContext class defines a context within a QML engine.
Definition qqmlcontext.h:25
The QQmlEngine class provides an environment for instantiating QML components.
Definition qqmlengine.h:57
The QQmlExpression class evaluates JavaScript in a QML context.
QVariant evaluate(bool *valueIsUndefined=nullptr)
Evaulates the expression, returning the result of the evaluation, or an invalid QVariant if the expre...
static QQuickItemPrivate * get(QQuickItem *item)
The QQuickItem class provides the most basic of all visual items in \l {Qt Quick}.
Definition qquickitem.h:63
QList< QQuickItem * > childItems() const
Returns the children of this item.
QQuickApplicationHelper(QQmlDataTest *testCase, const QString &testFilePath, const QVariantMap &initialProperties={}, const QStringList &qmlImportPaths={})
\qmltype Window \instantiates QQuickWindow \inqmlmodule QtQuick
\inmodule QtCore
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
bool isEmpty() const noexcept
Returns true if the string has no characters; otherwise returns false.
Definition qstring.h:192
int toInt(bool *ok=nullptr) const
Returns the variant as an int if the variant has userType() \l QMetaType::Int, \l QMetaType::Bool,...
\inmodule QtGui
Definition qwindow.h:63
auto signal
auto mo
[7]
void centerOnScreen(QQuickWindow *window)
QList< T * > findItems(QQuickItem *parent, const QString &objectName, bool visibleOnly=true)
void dumpTree(QQuickItem *parent, int depth=0)
bool isDelegateVisible(QQuickItem *item)
QPoint mapToWindow(const QQuickItem *item, qreal relativeX, qreal relativeY)
QPoint mapCenterToWindow(const QQuickItem *item)
bool findViewDelegateItem(QQuickItemView *itemView, int index, T &delegateItem, FindViewDelegateItemFlags flags=FindViewDelegateItemFlag::PositionViewAtIndex)
QQuickItem * findVisibleChild(QQuickItem *parent, const QString &objectName)
void moveMouseAway(QQuickWindow *window)
bool compareImages(const QImage &ia, const QImage &ib, QString *errorMessage)
T * findItem(QQuickItem *parent, const QString &objectName, int index=-1)
QPoint lerpPoints(const QPoint &point1, const QPoint &point2, qreal t)
void forEachStep(int steps, F &&func)
Combined button and popup list for selecting options.
ConnectionType
@ AutoConnection
static void * context
#define Q_DECLARE_FLAGS(Flags, Enum)
Definition qflags.h:174
GLint GLint GLint GLint GLint x
[0]
GLint GLenum GLsizei GLsizei GLsizei depth
const GLfloat * m
GLuint64 key
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLuint index
[2]
GLenum type
GLbitfield flags
GLint y
GLhandleARB obj
[2]
GLenum func
Definition qopenglext.h:663
GLdouble GLdouble t
Definition qopenglext.h:243
QQmlContext * qmlContext(const QObject *obj)
Definition qqml.cpp:75
QQuickItem * qobject_cast< QQuickItem * >(QObject *o)
Definition qquickitem.h:492
#define Q_OBJECT
#define Q_SLOTS
double qreal
Definition qtypes.h:187
static QString errorMessage(QUrlPrivate::ErrorCode errorCode, const QString &errorSource, qsizetype errorPosition)
Definition qurl.cpp:3517
sem release()
QGraphicsItem * item
QList< QTreeWidgetItem * > items
aWidget window() -> setWindowTitle("New Window Title")
[2]
\inmodule QtCore
QMetaObject::Connection connectToSignal(const typename QtPrivate::FunctionPointer< Func1 >::Object *obj, Func1 signal, Qt::ConnectionType type=Qt::AutoConnection)