| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the test suite of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
| 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 https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
| 21 | ** included in the packaging of this file. Please review the following |
| 22 | ** information to ensure the GNU General Public License requirements will |
| 23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
| 24 | ** |
| 25 | ** $QT_END_LICENSE$ |
| 26 | ** |
| 27 | ****************************************************************************/ |
| 28 | |
| 29 | #ifndef QQUICKVISUALTESTUTIL_H |
| 30 | #define QQUICKVISUALTESTUTIL_H |
| 31 | |
| 32 | #include <QtQuick/QQuickItem> |
| 33 | #include <QtQml/QQmlExpression> |
| 34 | |
| 35 | #include <QtQuick/private/qquickitem_p.h> |
| 36 | |
| 37 | namespace QQuickVisualTestUtil |
| 38 | { |
| 39 | QQuickItem *findVisibleChild(QQuickItem *parent, const QString &objectName); |
| 40 | |
| 41 | void dumpTree(QQuickItem *parent, int depth = 0); |
| 42 | |
| 43 | bool delegateVisible(QQuickItem *item); |
| 44 | |
| 45 | /* |
| 46 | Find an item with the specified objectName. If index is supplied then the |
| 47 | item must also evaluate the {index} expression equal to index |
| 48 | */ |
| 49 | template<typename T> |
| 50 | T *findItem(QQuickItem *parent, const QString &objectName, int index = -1) |
| 51 | { |
| 52 | const QMetaObject &mo = T::staticMetaObject; |
| 53 | for (int i = 0; i < parent->childItems().count(); ++i) { |
| 54 | QQuickItem *item = qobject_cast<QQuickItem*>(object: parent->childItems().at(i)); |
| 55 | if (!item) |
| 56 | continue; |
| 57 | if (mo.cast(obj: item) && (objectName.isEmpty() || item->objectName() == objectName)) { |
| 58 | if (index != -1) { |
| 59 | QQmlExpression e(qmlContext(item), item, "index" ); |
| 60 | if (e.evaluate().toInt() == index) |
| 61 | return static_cast<T*>(item); |
| 62 | } else { |
| 63 | return static_cast<T*>(item); |
| 64 | } |
| 65 | } |
| 66 | item = findItem<T>(item, objectName, index); |
| 67 | if (item) |
| 68 | return static_cast<T*>(item); |
| 69 | } |
| 70 | |
| 71 | return 0; |
| 72 | } |
| 73 | |
| 74 | template<typename T> |
| 75 | QList<T*> findItems(QQuickItem *parent, const QString &objectName, bool visibleOnly = true) |
| 76 | { |
| 77 | QList<T*> items; |
| 78 | const QMetaObject &mo = T::staticMetaObject; |
| 79 | for (int i = 0; i < parent->childItems().count(); ++i) { |
| 80 | QQuickItem *item = qobject_cast<QQuickItem*>(object: parent->childItems().at(i)); |
| 81 | if (!item || (visibleOnly && (!item->isVisible() || QQuickItemPrivate::get(item)->culled))) |
| 82 | continue; |
| 83 | if (mo.cast(obj: item) && (objectName.isEmpty() || item->objectName() == objectName)) |
| 84 | items.append(static_cast<T*>(item)); |
| 85 | items += findItems<T>(item, objectName); |
| 86 | } |
| 87 | |
| 88 | return items; |
| 89 | } |
| 90 | |
| 91 | template<typename T> |
| 92 | QList<T*> findItems(QQuickItem *parent, const QString &objectName, const QList<int> &indexes) |
| 93 | { |
| 94 | QList<T*> items; |
| 95 | for (int i=0; i<indexes.count(); i++) |
| 96 | items << qobject_cast<QQuickItem*>(findItem<T>(parent, objectName, indexes[i])); |
| 97 | return items; |
| 98 | } |
| 99 | |
| 100 | bool compareImages(const QImage &ia, const QImage &ib, QString *errorMessage); |
| 101 | } |
| 102 | |
| 103 | #endif // QQUICKVISUALTESTUTIL_H |
| 104 | |