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.cpp
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#include "visualtestutils_p.h"
5
6#include <QtCore/QCoreApplication>
7#include <QtCore/private/qvariantanimation_p.h>
8#include <QtCore/QDebug>
9#include <QtQuick/QQuickItem>
10#if QT_CONFIG(quick_itemview)
11#include <QtQuick/private/qquickitemview_p.h>
12#endif
13#include <QtQuickTest/QtQuickTest>
14#include <QtQuickTestUtils/private/viewtestutils_p.h>
15
17
19{
20 QQuickItem *item = nullptr;
21 QList<QQuickItem*> items = parent->findChildren<QQuickItem*>(objectName);
22 for (int i = 0; i < items.size(); ++i) {
23 if (items.at(i)->isVisible() && !QQuickItemPrivate::get(items.at(i))->culled) {
24 item = items.at(i);
25 break;
26 }
27 }
28 return item;
29}
30
32{
33 static QString padding = QStringLiteral(" ");
34 for (int i = 0; i < parent->childItems().size(); ++i) {
36 if (!item)
37 continue;
38 qDebug() << padding.left(depth*2) << item;
40 }
41}
42
44{
45#if QT_CONFIG(cursor) // Get the cursor out of the way.
46 // Using "bottomRight() + QPoint(100, 100)" was causing issues on Ubuntu,
47 // where the window was positioned at the bottom right corner of the window
48 // (even after centering the window on the screen), so we use another position.
49 QCursor::setPos(window->frameGeometry().bottomLeft() + QPoint(-10, 10));
50#endif
51
52 // make sure hover events from QQuickDeliveryAgentPrivate::flushFrameSynchronousEvents()
53 // do not interfere with the tests
56}
57
59{
60 const QRect screenGeometry = window->screen()->availableGeometry();
61 const QPoint offset = QPoint(window->width() / 2, window->height() / 2);
62 window->setFramePosition(screenGeometry.center() - offset);
63}
64
66{
67 return QPoint(_q_interpolate(point1.x(), point2.x(), t), _q_interpolate(point1.y(), point2.y(), t));
68};
69
87 : mWindow(window)
88 , mPointingDevice(pointingDevice)
89{
90}
91
100void QQuickVisualTestUtils::PointLerper::move(const QPoint &pos, int steps, int delayInMilliseconds)
101{
102 forEachStep(steps, [&](qreal progress) {
103 QQuickTest::pointerMove(mPointingDevice, mWindow, 0, lerpPoints(mFrom, pos, progress));
104 QTest::qWait(delayInMilliseconds);
105 });
106 mFrom = pos;
107};
108
109void QQuickVisualTestUtils::PointLerper::move(int x, int y, int steps, int delayInMilliseconds)
110{
111 move(QPoint(x, y), steps, delayInMilliseconds);
112};
113
124
138{
139 if (ia.size() != ib.size()) {
140 QDebug(errorMessage) << "Images are of different size:" << ia.size() << ib.size()
141 << "DPR:" << ia.devicePixelRatio() << ib.devicePixelRatio();
142 return false;
143 }
144 if (ia.format() != ib.format()) {
145 QDebug(errorMessage) << "Images are of different formats:" << ia.format() << ib.format();
146 return false;
147 }
148
149 int w = ia.width();
150 int h = ia.height();
151 const int tolerance = 5;
152 for (int y=0; y<h; ++y) {
153 const uint *as= (const uint *) ia.constScanLine(y);
154 const uint *bs= (const uint *) ib.constScanLine(y);
155 for (int x=0; x<w; ++x) {
156 uint a = as[x];
157 uint b = bs[x];
158
159 // No tolerance for error in the alpha.
160 if ((a & 0xff000000) != (b & 0xff000000)
161 || qAbs(qRed(a) - qRed(b)) > tolerance
162 || qAbs(qRed(a) - qRed(b)) > tolerance
163 || qAbs(qRed(a) - qRed(b)) > tolerance) {
164 QDebug(errorMessage) << "Mismatch at:" << x << y << ':'
165 << Qt::hex << Qt::showbase << a << b;
166 return false;
167 }
168 }
169 }
170 return true;
171}
172
173#if QT_CONFIG(quick_itemview)
182QQuickItem *QQuickVisualTestUtils::findViewDelegateItem(QQuickItemView *itemView, int index, FindViewDelegateItemFlags flags)
183{
184 if (QQuickTest::qIsPolishScheduled(itemView)) {
185 if (!QQuickTest::qWaitForPolish(itemView)) {
186 qWarning() << "failed to polish" << itemView;
187 return nullptr;
188 }
189 }
190
191 // Do this after the polish, just in case the count changes after a polish...
192 if (index <= -1 || index >= itemView->count()) {
193 qWarning() << "index" << index << "is out of bounds for" << itemView;
194 return nullptr;
195 }
196
199
200 return itemView->itemAtIndex(index);
201}
202#endif
203
205 const QString &testFilePath, const QVariantMap &initialProperties, const QStringList &qmlImportPaths)
206{
207 for (const auto &path : qmlImportPaths)
208 engine.addImportPath(path);
209
211
212 component.loadUrl(testCase->testFileUrl(testFilePath));
213 QVERIFY2(component.isReady(), qPrintable(component.errorString()));
214 QObject *rootObject = component.createWithInitialProperties(initialProperties);
215 cleanup.reset(rootObject);
216 if (component.isError() || !rootObject) {
217 errorMessage = QString::fromUtf8("Failed to create window: %1").arg(component.errorString()).toUtf8();
218 return;
219 }
220
221 window = qobject_cast<QQuickWindow*>(rootObject);
222 if (!window) {
223 errorMessage = QString::fromUtf8("Root object %1 must be a QQuickWindow subclass").arg(QDebug::toString(window)).toUtf8();
224 return;
225 }
226
227 if (window->isVisible()) {
228 errorMessage = QString::fromUtf8("Expected window not to be visible, but it is").toUtf8();
229 return;
230 }
231
232 ready = true;
233}
234
239
241{
242 // QTest::keyPress() but not generating the press event for the modifier key.
243 if (key == Qt::Key_Alt)
244 m_modifiers |= Qt::AltModifier;
245 QTest::simulateEvent(m_window, true, key, m_modifiers, QString(), false);
246}
247
249{
250 // QTest::keyRelease() but not generating the release event for the modifier key.
251 if (key == Qt::Key_Alt)
252 m_modifiers &= ~Qt::AltModifier;
253 QTest::simulateEvent(m_window, false, key, m_modifiers, QString(), false);
254}
255
261
263{
264 return item->mapToScene(QPointF(item->width() / 2, item->height() / 2)).toPoint();
265}
266
268{
269 return item->mapToScene(QPointF(relativeX, relativeY)).toPoint();
270}
271
273{
274 return mapToWindow(item, relativePos.x(), relativePos.y());
275}
276
278
279#include "moc_visualtestutils_p.cpp"
static bool sendEvent(QObject *receiver, QEvent *event)
Sends event event directly to receiver receiver, using the notify() function.
static void setPos(int x, int y)
Moves the cursor (hot spot) of the primary screen to the global screen position (x,...
Definition qcursor.cpp:240
\inmodule QtCore
\inmodule QtCore
Definition qcoreevent.h:45
qreal x() const
This convenience function is equivalent to calling pos().x().
QPointF mapToScene(const QPointF &point) const
Maps the point point, which is in this item's coordinate system, to the scene's coordinate system,...
bool isVisible() const
Returns true if the item is visible; otherwise, false is returned.
\inmodule QtGui
Definition qimage.h:37
QSize size() const
Returns the size of the image, i.e.
int width() const
Returns the width of the image.
int height() const
Returns the height of the image.
Format format() const
Returns the format of the image.
Definition qimage.cpp:2162
const uchar * constScanLine(int) const
Returns a pointer to the pixel data at the scanline with index i.
Definition qimage.cpp:1678
qreal devicePixelRatio() const
Returns the device pixel ratio for the image.
Definition qimage.cpp:1482
qsizetype size() const noexcept
Definition qlist.h:397
const_reference at(qsizetype i) const noexcept
Definition qlist.h:446
\inmodule QtCore
Definition qobject.h:103
QList< T > findChildren(QAnyStringView aName, Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
Returns all children of this object with the given name that can be cast to type T,...
Definition qobject.h:164
\inmodule QtCore\reentrant
Definition qpoint.h:217
constexpr QPoint toPoint() const
Rounds the coordinates of this point to the nearest integer, and returns a QPoint object with the rou...
Definition qpoint.h:404
\inmodule QtCore\reentrant
Definition qpoint.h:25
constexpr int x() const noexcept
Returns the x coordinate of this point.
Definition qpoint.h:130
constexpr int y() const noexcept
Returns the y coordinate of this point.
Definition qpoint.h:135
The QPointingDevice class describes a device from which mouse, touch or tablet events originate.
The QQmlComponent class encapsulates a QML component definition.
static QQuickItemPrivate * get(QQuickItem *item)
Q_INVOKABLE void positionViewAtIndex(int index, int mode)
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.
void move(const QPoint &pos, int steps=10, int delayInMilliseconds=1)
PointLerper(QQuickWindow *window, const QPointingDevice *pointingDevice=QPointingDevice::primaryPointingDevice())
QQuickApplicationHelper(QQmlDataTest *testCase, const QString &testFilePath, const QVariantMap &initialProperties={}, const QStringList &qmlImportPaths={})
\qmltype Window \instantiates QQuickWindow \inqmlmodule QtQuick
\inmodule QtCore\reentrant
Definition qrect.h:30
constexpr QPoint center() const noexcept
Returns the center point of the rectangle.
Definition qrect.h:233
\inmodule QtCore
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
QString left(qsizetype n) const &
Definition qstring.h:363
static QString fromUtf8(QByteArrayView utf8)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qstring.cpp:6018
\inmodule QtGui
Definition qwindow.h:63
Q_QMLTEST_EXPORT bool qWaitForPolish(const QQuickItem *item, int timeout=defaultTimeout)
Q_QMLTEST_EXPORT bool qIsPolishScheduled(const QQuickItem *item)
Definition quicktest.cpp:76
void pointerMove(const QPointingDevice *dev, QQuickWindow *window, int pointId, const QPoint &p)
void centerOnScreen(QQuickWindow *window)
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)
QPoint lerpPoints(const QPoint &point1, const QPoint &point2, qreal t)
void forEachStep(int steps, F &&func)
Combined button and popup list for selecting options.
Q_CORE_EXPORT void qWait(int ms)
This is an overloaded member function, provided for convenience. It differs from the above function o...
static void simulateEvent(QWindow *window, bool press, int code, Qt::KeyboardModifiers modifier, QString text, bool repeat, int delay=-1)
Definition qcompare.h:63
QTextStream & hex(QTextStream &stream)
Calls QTextStream::setIntegerBase(16) on stream and returns stream.
QTextStream & showbase(QTextStream &stream)
Calls QTextStream::setNumberFlags(QTextStream::numberFlags() | QTextStream::ShowBase) on stream and r...
@ Key_Alt
Definition qnamespace.h:686
@ AltModifier
#define qDebug
[1]
Definition qlogging.h:164
#define qWarning
Definition qlogging.h:166
constexpr T qAbs(const T &t)
Definition qnumeric.h:328
GLboolean GLboolean GLboolean b
GLint GLint GLint GLint GLint x
[0]
GLint GLenum GLsizei GLsizei GLsizei depth
GLuint64 key
GLfloat GLfloat GLfloat w
[0]
GLboolean GLboolean GLboolean GLboolean a
[7]
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLuint index
[2]
GLbitfield flags
GLenum GLuint GLintptr offset
GLint y
GLfloat GLfloat GLfloat GLfloat h
GLdouble GLdouble t
Definition qopenglext.h:243
GLsizei const GLchar *const * path
static qreal component(const QPointF &point, unsigned int i)
QQuickItem * qobject_cast< QQuickItem * >(QObject *o)
Definition qquickitem.h:492
constexpr int qRed(QRgb rgb)
Definition qrgb.h:18
#define qPrintable(string)
Definition qstring.h:1531
#define QStringLiteral(str)
#define QVERIFY2(statement, description)
Definition qtestcase.h:70
unsigned int uint
Definition qtypes.h:34
double qreal
Definition qtypes.h:187
static QString errorMessage(QUrlPrivate::ErrorCode errorCode, const QString &errorSource, qsizetype errorPosition)
Definition qurl.cpp:3517
#define leave(x)
Q_INLINE_TEMPLATE QRect _q_interpolate(const QRect &f, const QRect &t, qreal progress)
sem release()
QGraphicsItem * item
QList< QTreeWidgetItem * > items
aWidget window() -> setWindowTitle("New Window Title")
[2]
QJSEngine engine
[0]