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
controlstestutils.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
5
6#include <QtTest/qsignalspy.h>
7#include <QtQml/qqmlcomponent.h>
8#include <QtQuickControls2/qquickstyle.h>
9#include <QtQuickTemplates2/private/qquickabstractbutton_p.h>
10#include <QtQuickTemplates2/private/qquickapplicationwindow_p.h>
11
13 const QString &testFilePath, const QVariantMap &initialProperties, const QStringList &qmlImportPaths)
14 : QQuickApplicationHelper(testCase, testFilePath, initialProperties, qmlImportPaths)
15{
16 if (ready)
17 appWindow = qobject_cast<QQuickApplicationWindow*>(cleanup.data());
18}
19
29{
30 // If it's not the first time a style has been set and the new style is not different, do nothing.
31 if (!currentStyle.isEmpty() && style == currentStyle)
32 return true;
33
34 engine.reset();
35 currentStyle = style;
37 engine.reset(new QQmlEngine);
39
41 component.setData(QString::fromUtf8("import QtQuick\nimport QtQuick.Controls\n Control { }").toUtf8(), QUrl());
42 if (!component.isReady())
43 qWarning() << "Failed to load component:" << component.errorString();
44 return component.isReady();
45}
46
48 const QString &sourcePath, const QString &targetPath, const QStringList &skipList,
50{
51 // We cannot use QQmlComponent to load QML files directly from the source tree.
52 // For styles that use internal QML types (eg. material/Ripple.qml), the source
53 // dir would be added as an "implicit" import path overriding the actual import
54 // path (qtbase/qml/QtQuick/Controls.2/Material). => The QML engine fails to load
55 // the style C++ plugin from the implicit import path (the source dir).
56 //
57 // Therefore we only use the source tree for finding out the set of QML files that
58 // a particular style implements, and then we locate the respective QML files in
59 // the engine's import path. This way we can use QQmlComponent to load each QML file
60 // for benchmarking.
61
62 const QFileInfoList entries = QDir(qqc2ImportPath + QLatin1Char('/') + sourcePath).entryInfoList(
64 for (const QFileInfo &entry : entries) {
65 QString name = entry.baseName();
66 if (!skipList.contains(name)) {
67 const auto importPathList = engine->importPathList();
68 for (const QString &importPath : importPathList) {
69 QString name = entry.dir().dirName() + QLatin1Char('/') + entry.fileName();
70 QString filePath = importPath + QLatin1Char('/') + targetPath + QLatin1Char('/') + entry.fileName();
71 if (filePath.startsWith(QLatin1Char(':')))
72 filePath.prepend(QStringLiteral("qrc"));
73 if (QFile::exists(filePath)) {
74 callback(name, QUrl::fromLocalFile(filePath));
75 break;
76 } else {
77 QUrl url(filePath);
78 filePath = QQmlFile::urlToLocalFileOrQrc(filePath);
79 if (!filePath.isEmpty() && QFile::exists(filePath)) {
80 callback(name, url);
81 break;
82 }
83 }
84 }
85 }
86 }
87}
88
90 const QString &sourcePath, const QString &targetPath, const QStringList &skipList)
91{
92 forEachControl(engine, qqc2ImportPath, sourcePath, targetPath, skipList, [&](const QString &relativePath, const QUrl &absoluteUrl) {
93 QTest::newRow(qPrintable(relativePath)) << absoluteUrl;
94 });
95}
96
98{
99 if (!button->window()) {
100 qWarning() << "button" << button << "doesn't have an associated window";
101 return false;
102 }
103
104 if (!button->isEnabled()) {
105 qWarning() << "button" << button << "is not enabled";
106 return false;
107 }
108
109 if (!button->isVisible()) {
110 qWarning() << "button" << button << "is not visible";
111 return false;
112 }
113
114 if (button->width() <= 0.0) {
115 qWarning() << "button" << button << "must have a width greater than 0";
116 return false;
117 }
118
119 if (button->height() <= 0.0) {
120 qWarning() << "button" << button << "must have a height greater than 0";
121 return false;
122 }
123
124 return true;
125}
126
128{
130 return false;
131
133 if (!spy.isValid()) {
134 qWarning() << "button" << button << "must have a valid clicked signal";
135 return false;
136 }
137
138 const QPoint buttonCenter = button->mapToScene(QPointF(button->width() / 2, button->height() / 2)).toPoint();
140 if (spy.size() != 1) {
141 qWarning() << "clicked signal of button" << button << "was not emitted after clicking";
142 return false;
143 }
144
145 return true;
146}
147
149{
151 return false;
152
154 if (!spy.isValid()) {
155 qWarning() << "button" << button << "must have a valid doubleClicked signal";
156 return false;
157 }
158
159 const QPoint buttonCenter = button->mapToScene(QPointF(button->width() / 2, button->height() / 2)).toPoint();
161 if (spy.size() != 1) {
162 qWarning() << "doubleClicked signal of button" << button << "was not emitted after double-clicking";
163 return false;
164 }
165
166 return true;
167}
168
174{
175 std::unique_ptr<QQmlComponent> component(new QQmlComponent(qmlEngine(this)));
176 component->setData(data, QUrl());
177 if (component->isError())
178 qmlWarning(this) << "Failed to create component from the following data:\n" << data;
179 return component.release();
180}
181
\inmodule QtCore
Definition qbytearray.h:57
\inmodule QtCore
Definition qdir.h:20
QFileInfoList entryInfoList(Filters filters=NoFilter, SortFlags sort=NoSort) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qdir.cpp:1388
@ Files
Definition qdir.h:23
bool exists() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qfile.cpp:351
qsizetype size() const noexcept
Definition qlist.h:397
\inmodule QtCore\reentrant
Definition qpoint.h:217
\inmodule QtCore\reentrant
Definition qpoint.h:25
The QQmlComponent class encapsulates a QML component definition.
The QQmlEngine class provides an environment for instantiating QML components.
Definition qqmlengine.h:57
static QString urlToLocalFileOrQrc(const QString &)
If url is a local file returns a path suitable for passing to \l{QFile}.
Definition qqmlfile.cpp:742
Q_INVOKABLE QQmlComponent * createComponent(const QByteArray &data)
Allows creating QQmlComponents in C++, which is useful for tests that need to check if items created ...
QQuickControlsApplicationHelper(QQmlDataTest *testCase, const QString &testFilePath, const QVariantMap &initialProperties={}, const QStringList &qmlImportPaths={})
static void setStyle(const QString &style)
Sets the application style to style.
static QString name()
Returns the name of the application style.
T * data() const noexcept
Returns the value of the pointer referenced by this object.
\inmodule QtTest
Definition qsignalspy.h:22
bool isValid() const noexcept
Returns true if the signal spy listens to a valid signal, otherwise false.
Definition qsignalspy.h:44
\inmodule QtCore
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
bool startsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string starts with s; otherwise returns false.
Definition qstring.cpp:5455
bool isEmpty() const noexcept
Returns true if the string has no characters; otherwise returns false.
Definition qstring.h:192
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
QString & prepend(QChar c)
Definition qstring.h:478
\inmodule QtCore
Definition qurl.h:94
static QUrl fromLocalFile(const QString &localfile)
Returns a QUrl representation of localFile, interpreted as a local file.
Definition qurl.cpp:3368
QWidget * window() const
Returns the window for this widget, i.e.
Definition qwidget.cpp:4313
int width
the width of the widget excluding any window frame
Definition qwidget.h:114
int height
the height of the widget excluding any window frame
Definition qwidget.h:115
bool isEnabled() const
Definition qwidget.h:814
bool isVisible() const
Definition qwidget.h:874
QPushButton * button
[2]
QSignalSpy spy(myCustomObject, SIGNAL(mySignal(int, QString, double)))
[0]
bool verifyButtonClickable(QQuickAbstractButton *button)
void addTestRowForEachControl(QQmlEngine *engine, const QString &qqc2ImportPath, const QString &sourcePath, const QString &targetPath, const QStringList &skipList=QStringList())
void forEachControl(QQmlEngine *engine, const QString &qqc2ImportPath, const QString &sourcePath, const QString &targetPath, const QStringList &skipList, ForEachCallback callback)
bool doubleClickButton(QQuickAbstractButton *button)
std::function< void(const QString &, const QUrl &) ForEachCallback)
bool clickButton(QQuickAbstractButton *button)
Q_TESTLIB_EXPORT QTestData & newRow(const char *dataTag)
Appends a new row to the current test data.
void mouseDClick(QWindow *window, Qt::MouseButton button, Qt::KeyboardModifiers stateKey=Qt::KeyboardModifiers(), QPoint pos=QPoint(), int delay=-1)
Definition qtestmouse.h:141
void mouseClick(QWindow *window, Qt::MouseButton button, Qt::KeyboardModifiers stateKey=Qt::KeyboardModifiers(), QPoint pos=QPoint(), int delay=-1)
Definition qtestmouse.h:137
@ LeftButton
Definition qnamespace.h:58
@ NoModifier
QList< QString > QStringList
Constructs a string list that contains the given string, str.
#define qWarning
Definition qlogging.h:166
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLuint name
GLuint entry
static qreal component(const QPointF &point, unsigned int i)
QQmlEngine * qmlEngine(const QObject *obj)
Definition qqml.cpp:80
void qmlClearTypeRegistrations()
Definition qqml.cpp:230
Q_QML_EXPORT QQmlInfo qmlWarning(const QObject *me)
#define qPrintable(string)
Definition qstring.h:1531
#define QStringLiteral(str)
#define Q_INVOKABLE
QUrl url("example.com")
[constructor-url-reference]
QJSEngine engine
[0]
\inmodule QtCore \reentrant
Definition qchar.h:18