| 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 | #include "visualtestutil.h" |
| 38 | |
| 39 | #include <QtQuick/QQuickItem> |
| 40 | #include <QtCore/QDebug> |
| 41 | #include <QtGui/QCursor> |
| 42 | #include <QtCore/QCoreApplication> |
| 43 | #include <QtQml/QQmlFile> |
| 44 | #include <QtTest/QTest> |
| 45 | |
| 46 | bool QQuickVisualTestUtil::delegateVisible(QQuickItem *item) |
| 47 | { |
| 48 | return item->isVisible() && !QQuickItemPrivate::get(item)->culled; |
| 49 | } |
| 50 | |
| 51 | QQuickItem *QQuickVisualTestUtil::findVisibleChild(QQuickItem *parent, const QString &objectName) |
| 52 | { |
| 53 | QQuickItem *item = 0; |
| 54 | QList<QQuickItem*> items = parent->findChildren<QQuickItem*>(aName: objectName); |
| 55 | for (int i = 0; i < items.count(); ++i) { |
| 56 | if (items.at(i)->isVisible() && !QQuickItemPrivate::get(item: items.at(i))->culled) { |
| 57 | item = items.at(i); |
| 58 | break; |
| 59 | } |
| 60 | } |
| 61 | return item; |
| 62 | } |
| 63 | |
| 64 | void QQuickVisualTestUtil::dumpTree(QQuickItem *parent, int depth) |
| 65 | { |
| 66 | static QString padding(" " ); |
| 67 | for (int i = 0; i < parent->childItems().count(); ++i) { |
| 68 | QQuickItem *item = qobject_cast<QQuickItem*>(object: parent->childItems().at(i)); |
| 69 | if (!item) |
| 70 | continue; |
| 71 | qDebug() << padding.left(n: depth*2) << item; |
| 72 | dumpTree(parent: item, depth: depth+1); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | void QQuickVisualTestUtil::moveMouseAway(QQuickWindow *window) |
| 77 | { |
| 78 | #if QT_CONFIG(cursor) // Get the cursor out of the way. |
| 79 | // Using "bottomRight() + QPoint(100, 100)" was causing issues on Ubuntu, |
| 80 | // where the window was positioned at the bottom right corner of the window |
| 81 | // (even after centering the window on the screen), so we use another position. |
| 82 | QCursor::setPos(window->geometry().bottomLeft() + QPoint(0, 10)); |
| 83 | #endif |
| 84 | |
| 85 | // make sure hover events from QQuickWindowPrivate::flushFrameSynchronousEvents() |
| 86 | // do not interfere with the tests |
| 87 | QEvent leave(QEvent::Leave); |
| 88 | QCoreApplication::sendEvent(receiver: window, event: &leave); |
| 89 | } |
| 90 | |
| 91 | void QQuickVisualTestUtil::centerOnScreen(QQuickWindow *window) |
| 92 | { |
| 93 | const QRect screenGeometry = window->screen()->availableGeometry(); |
| 94 | const QPoint offset = QPoint(window->width() / 2, window->height() / 2); |
| 95 | window->setFramePosition(screenGeometry.center() - offset); |
| 96 | } |
| 97 | |
| 98 | void QQuickVisualTestUtil::addTestRowForEachControl(QQmlEngine *engine, const QString &sourcePath, const QString &targetPath, const QStringList &skiplist) |
| 99 | { |
| 100 | // We cannot use QQmlComponent to load QML files directly from the source tree. |
| 101 | // For styles that use internal QML types (eg. material/Ripple.qml), the source |
| 102 | // dir would be added as an "implicit" import path overriding the actual import |
| 103 | // path (qtbase/qml/QtQuick/Controls.2/Material). => The QML engine fails to load |
| 104 | // the style C++ plugin from the implicit import path (the source dir). |
| 105 | // |
| 106 | // Therefore we only use the source tree for finding out the set of QML files that |
| 107 | // a particular style implements, and then we locate the respective QML files in |
| 108 | // the engine's import path. This way we can use QQmlComponent to load each QML file |
| 109 | // for benchmarking. |
| 110 | |
| 111 | const QFileInfoList entries = QDir(QQC2_IMPORT_PATH "/" + sourcePath).entryInfoList(nameFilters: QStringList("*.qml" ), filters: QDir::Files); |
| 112 | for (const QFileInfo &entry : entries) { |
| 113 | QString name = entry.baseName(); |
| 114 | if (!skiplist.contains(str: name)) { |
| 115 | const auto importPathList = engine->importPathList(); |
| 116 | for (const QString &importPath : importPathList) { |
| 117 | QString name = entry.dir().dirName() + "/" + entry.fileName(); |
| 118 | QString filePath = importPath + "/" + targetPath + "/" + entry.fileName(); |
| 119 | if (filePath.startsWith(s: ":" )) |
| 120 | filePath.prepend(s: "qrc" ); |
| 121 | if (QFile::exists(fileName: filePath)) { |
| 122 | QTest::newRow(qPrintable(name)) << QUrl::fromLocalFile(localfile: filePath); |
| 123 | break; |
| 124 | } else { |
| 125 | QUrl url(filePath); |
| 126 | filePath = QQmlFile::urlToLocalFileOrQrc(filePath); |
| 127 | if (!filePath.isEmpty() && QFile::exists(fileName: filePath)) { |
| 128 | QTest::newRow(qPrintable(name)) << url; |
| 129 | break; |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | |