1/****************************************************************************
2**
3** Copyright (C) 2017 The Qt Company Ltd.
4** Contact: http://www.qt.io/licensing/
5**
6** This file is part of the test suite of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL3$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see http://www.qt.io/terms-conditions. For further
15** information use the contact form at http://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPLv3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or later as published by the Free
28** Software Foundation and appearing in the file LICENSE.GPL included in
29** the packaging of this file. Please review the following information to
30** ensure the GNU General Public License version 2.0 requirements will be
31** met: http://www.gnu.org/licenses/gpl-2.0.html.
32**
33** $QT_END_LICENSE$
34**
35****************************************************************************/
36
37#ifndef QQUICKVISUALTESTUTIL_H
38#define QQUICKVISUALTESTUTIL_H
39
40#include <QtQuick/QQuickItem>
41#include <QtQml/QQmlExpression>
42
43#include <QtQuick/private/qquickitem_p.h>
44
45#include <QtQuickTemplates2/private/qquickapplicationwindow_p.h>
46
47#include "util.h"
48
49namespace QQuickVisualTestUtil
50{
51 QQuickItem *findVisibleChild(QQuickItem *parent, const QString &objectName);
52
53 void dumpTree(QQuickItem *parent, int depth = 0);
54
55 bool delegateVisible(QQuickItem *item);
56
57 void centerOnScreen(QQuickWindow *window);
58
59 void moveMouseAway(QQuickWindow *window);
60
61 /*
62 Find an item with the specified objectName. If index is supplied then the
63 item must also evaluate the {index} expression equal to index
64 */
65 template<typename T>
66 T *findItem(QQuickItem *parent, const QString &objectName, int index = -1)
67 {
68 const QMetaObject &mo = T::staticMetaObject;
69 for (int i = 0; i < parent->childItems().count(); ++i) {
70 QQuickItem *item = qobject_cast<QQuickItem*>(object: parent->childItems().at(i));
71 if (!item)
72 continue;
73 if (mo.cast(obj: item) && (objectName.isEmpty() || item->objectName() == objectName)) {
74 if (index != -1) {
75 QQmlExpression e(qmlContext(item), item, "index");
76 if (e.evaluate().toInt() == index)
77 return static_cast<T*>(item);
78 } else {
79 return static_cast<T*>(item);
80 }
81 }
82 item = findItem<T>(item, objectName, index);
83 if (item)
84 return static_cast<T*>(item);
85 }
86
87 return 0;
88 }
89
90 template<typename T>
91 QList<T*> findItems(QQuickItem *parent, const QString &objectName, bool visibleOnly = true)
92 {
93 QList<T*> items;
94 const QMetaObject &mo = T::staticMetaObject;
95 for (int i = 0; i < parent->childItems().count(); ++i) {
96 QQuickItem *item = qobject_cast<QQuickItem*>(object: parent->childItems().at(i));
97 if (!item || (visibleOnly && (!item->isVisible() || QQuickItemPrivate::get(item)->culled)))
98 continue;
99 if (mo.cast(obj: item) && (objectName.isEmpty() || item->objectName() == objectName))
100 items.append(static_cast<T*>(item));
101 items += findItems<T>(item, objectName);
102 }
103
104 return items;
105 }
106
107 template<typename T>
108 QList<T*> findItems(QQuickItem *parent, const QString &objectName, const QList<int> &indexes)
109 {
110 QList<T*> items;
111 for (int i=0; i<indexes.count(); i++)
112 items << qobject_cast<QQuickItem*>(findItem<T>(parent, objectName, indexes[i]));
113 return items;
114 }
115
116 class QQuickApplicationHelper
117 {
118 public:
119 QQuickApplicationHelper(QQmlDataTest *testCase, const QString &testFilePath) :
120 component(&engine)
121 {
122 component.loadUrl(url: testCase->testFileUrl(fileName: testFilePath));
123 QObject *rootObject = component.create();
124 cleanup.reset(other: rootObject);
125 if (!rootObject) {
126 errorMessage = QString::fromUtf8(str: "Failed to create window: %1").arg(a: component.errorString()).toUtf8();
127 return;
128 }
129
130 window = qobject_cast<QQuickWindow*>(object: rootObject);
131 appWindow = qobject_cast<QQuickApplicationWindow*>(object: rootObject);
132 if (!window) {
133 errorMessage = QString::fromUtf8(str: "Root object must be a QQuickWindow subclass").toUtf8();
134 return;
135 }
136
137 if (window->isVisible()) {
138 errorMessage = QString::fromUtf8(str: "Expected window not to be visible, but it is").toUtf8();
139 return;
140 }
141
142 ready = true;
143 }
144
145 // Return a C-style string instead of QString because that's what QTest uses for error messages,
146 // so it saves code at the calling site.
147 inline const char *failureMessage() const
148 {
149 return errorMessage.constData();
150 }
151
152 QQmlEngine engine;
153 QQmlComponent component;
154 QScopedPointer<QObject> cleanup;
155 QQuickApplicationWindow *appWindow = nullptr;
156 QQuickWindow *window = nullptr;
157
158 bool ready = false;
159 // Store as a byte array so that we can return its raw data safely;
160 // using qPrintable() in failureMessage() will construct a throwaway QByteArray
161 // that is destroyed before the function returns.
162 QByteArray errorMessage;
163 };
164
165 void addTestRowForEachControl(QQmlEngine *engine, const QString &sourcePath, const QString &targetPath, const QStringList &skiplist = QStringList());
166}
167
168#define QQUICK_VERIFY_POLISH(item) \
169 QTRY_COMPARE(QQuickItemPrivate::get(item)->polishScheduled, false)
170
171#endif // QQUICKVISUALTESTUTIL_H
172

source code of qtquickcontrols2/tests/auto/shared/visualtestutil.h