diff options
Diffstat (limited to 'tests/auto/quickcontrols2/shared')
-rw-r--r-- | tests/auto/quickcontrols2/shared/dialogtestutil.h | 144 | ||||
-rw-r--r-- | tests/auto/quickcontrols2/shared/qtest_quickcontrols.h | 90 | ||||
-rw-r--r-- | tests/auto/quickcontrols2/shared/util.cpp | 144 | ||||
-rw-r--r-- | tests/auto/quickcontrols2/shared/util.h | 108 | ||||
-rw-r--r-- | tests/auto/quickcontrols2/shared/util.pri | 14 | ||||
-rw-r--r-- | tests/auto/quickcontrols2/shared/visualtestutil.cpp | 272 | ||||
-rw-r--r-- | tests/auto/quickcontrols2/shared/visualtestutil.h | 252 |
7 files changed, 0 insertions, 1024 deletions
diff --git a/tests/auto/quickcontrols2/shared/dialogtestutil.h b/tests/auto/quickcontrols2/shared/dialogtestutil.h deleted file mode 100644 index c10c4001..00000000 --- a/tests/auto/quickcontrols2/shared/dialogtestutil.h +++ /dev/null @@ -1,144 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2021 The Qt Company Ltd. -** Contact: http://www.qt.io/licensing/ -** -** This file is part of the test suite of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL3$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see http://www.qt.io/terms-conditions. For further -** information use the contact form at http://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPLv3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or later as published by the Free -** Software Foundation and appearing in the file LICENSE.GPL included in -** the packaging of this file. Please review the following information to -** ensure the GNU General Public License version 2.0 requirements will be -** met: http://www.gnu.org/licenses/gpl-2.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#ifndef QQUICKDIALOGTESTUTIL_H -#define QQUICKDIALOGTESTUTIL_H - -#include <QtQuickTemplates2/private/qquickapplicationwindow_p.h> - -#include "util.h" -#include "visualtestutil.h" - -// We need these for Windows, because FolderListModel returns a lowercase drive letter; e.g.: -// "file:///c:/blah.txt", whereas other API returns "file:///C:/blah.txt". -#define COMPARE_URL(url1, url2) \ - QCOMPARE(QFileInfo(url1.toLocalFile()).absoluteFilePath(), QFileInfo(url2.toLocalFile()).absoluteFilePath()); - -// Store a copy of the arguments in case { ... } list initializer syntax is used as an argument, -// which could result in two different lists being created and passed to std::transform() -// (and would also require it to be enclosed in parentheses everywhere it's used). -#define COMPARE_URLS(actualUrls, expectedUrls) \ -{ \ - const QList<QUrl> actualUrlsCopy = actualUrls; \ - QList<QString> actualPaths; \ - std::transform(actualUrlsCopy.begin(), actualUrlsCopy.end(), std::back_insert_iterator(actualPaths), \ - [](const QUrl &url) { return QFileInfo(url.toLocalFile()).absoluteFilePath(); }); \ - const QList<QUrl> expectedUrlsCopy = expectedUrls; \ - QList<QString> expectedPaths; \ - std::transform(expectedUrlsCopy.begin(), expectedUrlsCopy.end(), std::back_insert_iterator(expectedPaths), \ - [](const QUrl &url) { return QFileInfo(url.toLocalFile()).absoluteFilePath(); }); \ - QCOMPARE(actualPaths, expectedPaths); \ -} - -namespace QQuickDialogTestUtil -{ - -// Saves duplicating a bunch of code in every test. -template<typename DialogType, typename QuickDialogType> -class DialogTestHelper -{ -public: - DialogTestHelper(QQmlDataTest *testCase, const QString &testFilePath, - const QStringList &qmlImportPaths = {}, const QVariantMap &initialProperties = {}) : - appHelper(testCase, testFilePath, qmlImportPaths, initialProperties) - { - if (!appHelper.ready) - return; - - dialog = appHelper.window->property("dialog").value<DialogType*>(); - if (!dialog) { - appHelper.errorMessage = "\"dialog\" property is not valid"; - return; - } - - appHelper.window->show(); - appHelper.window->requestActivate(); - } - - Q_REQUIRED_RESULT bool isWindowInitialized() const - { - return appHelper.ready; - } - - Q_REQUIRED_RESULT bool waitForWindowActive() - { - return QTest::qWaitForWindowActive(appHelper.window); - } - - bool openDialog() - { - dialog->open(); - if (!dialog->isVisible()) { - appHelper.errorMessage = "Dialog is not visible"; - return false; - } - - // We might want to call this function more than once, - // and we only need to get these members the first time. - if (!quickDialog) { - quickDialog = appHelper.window->findChild<QuickDialogType*>(); - if (!quickDialog) { - appHelper.errorMessage = "Can't find Qt Quick-based dialog"; - return false; - } - } - - return true; - } - - bool isQuickDialogOpen() const - { - return quickDialog->isOpened(); - } - - QQuickWindow *window() const - { - return appHelper.window; - } - - const char *failureMessage() const - { - return appHelper.errorMessage.constData(); - } - - QQuickVisualTestUtil::QQuickApplicationHelper appHelper; - DialogType *dialog = nullptr; - QuickDialogType *quickDialog = nullptr; -}; - -} - -#endif // QQUICKDIALOGTESTUTIL_H diff --git a/tests/auto/quickcontrols2/shared/qtest_quickcontrols.h b/tests/auto/quickcontrols2/shared/qtest_quickcontrols.h deleted file mode 100644 index 8eadd979..00000000 --- a/tests/auto/quickcontrols2/shared/qtest_quickcontrols.h +++ /dev/null @@ -1,90 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2017 The Qt Company Ltd. -** Contact: http://www.qt.io/licensing/ -** -** This file is part of the test suite of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL3$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see http://www.qt.io/terms-conditions. For further -** information use the contact form at http://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPLv3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or later as published by the Free -** Software Foundation and appearing in the file LICENSE.GPL included in -** the packaging of this file. Please review the following information to -** ensure the GNU General Public License version 2.0 requirements will be -** met: http://www.gnu.org/licenses/gpl-2.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#ifndef QTEST_QUICKCONTROLS_H -#define QTEST_QUICKCONTROLS_H - -#include <QtTest/qtest.h> -#include <QtTest/private/qtestresult_p.h> -#include <QtGui/qguiapplication.h> -#include <QtQml/qqml.h> -#include <QtQuickControls2/qquickstyle.h> -#include <QtQuickControls2/private/qquickstyle_p.h> - -static QStringList testStyles() -{ - // It's not enough to check if the name is empty, because since Qt 6 - // we set an appropriate style for the platform if no style was specified. - // Also, we need the name check to come first, as isUsingDefaultStyle() does not do any resolving, - // and so its return value wouldn't be correct otherwise. - if (QQuickStyle::name().isEmpty() || QQuickStylePrivate::isUsingDefaultStyle()) - return QQuickStylePrivate::builtInStyles(); - return QStringList(QQuickStyle::name()); -} - -static int runTests(QObject *testObject, int argc, char *argv[]) -{ - int res = 0; - QTest::qInit(testObject, argc, argv); - const QByteArray testObjectName = QTestResult::currentTestObjectName(); - // setCurrentTestObject() takes a C string, which means we must ensure - // that the string we pass in lives long enough (i.e until the next call - // to setCurrentTestObject()), so store the name outside of the loop. - QByteArray testName; - const QStringList styles = testStyles(); - for (const QString &style : styles) { - qmlClearTypeRegistrations(); - QQuickStyle::setStyle(style); - testName = testObjectName + "::" + style.toLocal8Bit(); - QTestResult::setCurrentTestObject(testName); - res += QTest::qRun(); - } - QTestResult::setCurrentTestObject(testObjectName); - QTest::qCleanup(); - return res; -} - -#define QTEST_QUICKCONTROLS_MAIN(TestCase) \ -int main(int argc, char *argv[]) \ -{ \ - qputenv("QML_NO_TOUCH_COMPRESSION", "1"); \ - QGuiApplication app(argc, argv); \ - TestCase tc; \ - QTEST_SET_MAIN_SOURCE_PATH \ - return runTests(&tc, argc, argv); \ -} - -#endif // QTEST_QUICKCONTROLS_H diff --git a/tests/auto/quickcontrols2/shared/util.cpp b/tests/auto/quickcontrols2/shared/util.cpp deleted file mode 100644 index 31647d69..00000000 --- a/tests/auto/quickcontrols2/shared/util.cpp +++ /dev/null @@ -1,144 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2017 The Qt Company Ltd. -** Contact: http://www.qt.io/licensing/ -** -** This file is part of the test suite of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL3$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see http://www.qt.io/terms-conditions. For further -** information use the contact form at http://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPLv3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or later as published by the Free -** Software Foundation and appearing in the file LICENSE.GPL included in -** the packaging of this file. Please review the following information to -** ensure the GNU General Public License version 2.0 requirements will be -** met: http://www.gnu.org/licenses/gpl-2.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include "util.h" - -#include <QtQml/QQmlComponent> -#include <QtQml/QQmlError> -#include <QtQml/QQmlContext> -#include <QtQml/QQmlEngine> -#include <QtCore/QTextStream> -#include <QtCore/QDebug> -#include <QtCore/QMutexLocker> - -QQmlDataTest *QQmlDataTest::m_instance = 0; - -QQmlDataTest::QQmlDataTest() : -#ifdef QT_TESTCASE_BUILDDIR - m_dataDirectory(QTest::qFindTestData("data", QT_QMLTEST_DATADIR, 0, QT_TESTCASE_BUILDDIR)), -#else - m_dataDirectory(QTest::qFindTestData("data", QT_QMLTEST_DATADIR, 0)), -#endif - - m_dataDirectoryUrl(m_dataDirectory.startsWith(QLatin1Char(':')) - ? QUrl(QLatin1String("qrc") + m_dataDirectory) - : QUrl::fromLocalFile(m_dataDirectory + QLatin1Char('/'))) -{ - m_instance = this; -} - -QQmlDataTest::~QQmlDataTest() -{ - m_instance = 0; -} - -void QQmlDataTest::initTestCase() -{ - QVERIFY2(!m_dataDirectory.isEmpty(), "'data' directory not found"); - m_directory = QFileInfo(m_dataDirectory).absolutePath(); - if (m_dataDirectoryUrl.scheme() != QLatin1String("qrc")) - QVERIFY2(QDir::setCurrent(m_directory), qPrintable(QLatin1String("Could not chdir to ") + m_directory)); -} - -QString QQmlDataTest::testFile(const QString &fileName) const -{ - if (m_directory.isEmpty()) - qFatal("QQmlDataTest::initTestCase() not called."); - QString result = m_dataDirectory; - result += QLatin1Char('/'); - result += fileName; - return result; -} - -QByteArray QQmlDataTest::msgComponentError(const QQmlComponent &c, - const QQmlEngine *engine /* = 0 */) -{ - QString result; - const QList<QQmlError> errors = c.errors(); - QTextStream str(&result); - str << "Component '" << c.url().toString() << "' has " << errors.size() - << " errors: '"; - for (int i = 0; i < errors.size(); ++i) { - if (i) - str << ", '"; - str << errors.at(i).toString() << '\''; - - } - if (!engine) - if (QQmlContext *context = c.creationContext()) - engine = context->engine(); - if (engine) { - str << " Import paths: (" << engine->importPathList().join(QStringLiteral(", ")) - << ") Plugin paths: (" << engine->pluginPathList().join(QStringLiteral(", ")) - << ')'; - } - return result.toLocal8Bit(); -} - -bool QQmlDataTest::canImportModule(const QString &importTestQmlSource) const -{ - QQmlEngine engine; - QQmlComponent component(&engine); - component.setData(importTestQmlSource.toLatin1(), QUrl()); - return !component.isError(); -} - -Q_GLOBAL_STATIC(QMutex, qQmlTestMessageHandlerMutex) - -QQmlTestMessageHandler *QQmlTestMessageHandler::m_instance = 0; - -void QQmlTestMessageHandler::messageHandler(QtMsgType, const QMessageLogContext &, const QString &message) -{ - QMutexLocker locker(qQmlTestMessageHandlerMutex()); - if (QQmlTestMessageHandler::m_instance) - QQmlTestMessageHandler::m_instance->m_messages.push_back(message); -} - -QQmlTestMessageHandler::QQmlTestMessageHandler() -{ - QMutexLocker locker(qQmlTestMessageHandlerMutex()); - Q_ASSERT(!QQmlTestMessageHandler::m_instance); - QQmlTestMessageHandler::m_instance = this; - m_oldHandler = qInstallMessageHandler(messageHandler); -} - -QQmlTestMessageHandler::~QQmlTestMessageHandler() -{ - QMutexLocker locker(qQmlTestMessageHandlerMutex()); - Q_ASSERT(QQmlTestMessageHandler::m_instance); - qInstallMessageHandler(m_oldHandler); - QQmlTestMessageHandler::m_instance = 0; -} diff --git a/tests/auto/quickcontrols2/shared/util.h b/tests/auto/quickcontrols2/shared/util.h deleted file mode 100644 index 50d161e0..00000000 --- a/tests/auto/quickcontrols2/shared/util.h +++ /dev/null @@ -1,108 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2017 The Qt Company Ltd. -** Contact: http://www.qt.io/licensing/ -** -** This file is part of the test suite of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL3$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see http://www.qt.io/terms-conditions. For further -** information use the contact form at http://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPLv3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or later as published by the Free -** Software Foundation and appearing in the file LICENSE.GPL included in -** the packaging of this file. Please review the following information to -** ensure the GNU General Public License version 2.0 requirements will be -** met: http://www.gnu.org/licenses/gpl-2.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#ifndef QQMLTESTUTILS_H -#define QQMLTESTUTILS_H - -#include <QtCore/QDir> -#include <QtCore/QUrl> -#include <QtCore/QCoreApplication> -#include <QtCore/QStringList> -#include <QtTest/QTest> - -QT_FORWARD_DECLARE_CLASS(QQmlComponent) -QT_FORWARD_DECLARE_CLASS(QQmlEngine) - -/* Base class for tests with data that are located in a "data" subfolder. */ - -class QQmlDataTest : public QObject -{ - Q_OBJECT -public: - QQmlDataTest(); - virtual ~QQmlDataTest(); - - QString testFile(const QString &fileName) const; - inline QString testFile(const char *fileName) const - { return testFile(QLatin1String(fileName)); } - inline QUrl testFileUrl(const QString &fileName) const - { return QUrl::fromLocalFile(testFile(fileName)); } - inline QUrl testFileUrl(const char *fileName) const - { return testFileUrl(QLatin1String(fileName)); } - - inline QString dataDirectory() const { return m_dataDirectory; } - inline QUrl dataDirectoryUrl() const { return m_dataDirectoryUrl; } - inline QString directory() const { return m_directory; } - - static inline QQmlDataTest *instance() { return m_instance; } - - static QByteArray msgComponentError(const QQmlComponent &, - const QQmlEngine *engine = 0); - - bool canImportModule(const QString &importTestQmlSource) const; - -public slots: - virtual void initTestCase(); - -private: - static QQmlDataTest *m_instance; - - const QString m_dataDirectory; - const QUrl m_dataDirectoryUrl; - QString m_directory; -}; - -class QQmlTestMessageHandler -{ - Q_DISABLE_COPY(QQmlTestMessageHandler) -public: - QQmlTestMessageHandler(); - ~QQmlTestMessageHandler(); - - const QStringList &messages() const { return m_messages; } - const QString messageString() const { return m_messages.join(QLatin1Char('\n')); } - - void clear() { m_messages.clear(); } - -private: - static void messageHandler(QtMsgType, const QMessageLogContext &, const QString &message); - - static QQmlTestMessageHandler *m_instance; - QStringList m_messages; - QtMessageHandler m_oldHandler; -}; - -#endif // QQMLTESTUTILS_H diff --git a/tests/auto/quickcontrols2/shared/util.pri b/tests/auto/quickcontrols2/shared/util.pri deleted file mode 100644 index 6366776b..00000000 --- a/tests/auto/quickcontrols2/shared/util.pri +++ /dev/null @@ -1,14 +0,0 @@ -QT += testlib-private core-private gui-private qml-private quick-private qmltest quicktemplates2-private quickcontrols2 quickcontrols2-private - -HEADERS += $$PWD/visualtestutil.h \ - $$PWD/util.h \ - $$PWD/qtest_quickcontrols.h -SOURCES += $$PWD/visualtestutil.cpp \ - $$PWD/util.cpp - -android|ios { - DEFINES += QT_QMLTEST_DATADIR=\\\":/data\\\" -} else { - DEFINES += QT_QMLTEST_DATADIR=\\\"$${_PRO_FILE_PWD_}/data\\\" -} -DEFINES += QQC2_IMPORT_PATH=\\\"$$QQC2_SOURCE_TREE/src\\\" diff --git a/tests/auto/quickcontrols2/shared/visualtestutil.cpp b/tests/auto/quickcontrols2/shared/visualtestutil.cpp deleted file mode 100644 index ff0d9e8e..00000000 --- a/tests/auto/quickcontrols2/shared/visualtestutil.cpp +++ /dev/null @@ -1,272 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2017 The Qt Company Ltd. -** Contact: http://www.qt.io/licensing/ -** -** This file is part of the test suite of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL3$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see http://www.qt.io/terms-conditions. For further -** information use the contact form at http://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPLv3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or later as published by the Free -** Software Foundation and appearing in the file LICENSE.GPL included in -** the packaging of this file. Please review the following information to -** ensure the GNU General Public License version 2.0 requirements will be -** met: http://www.gnu.org/licenses/gpl-2.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include "visualtestutil.h" - -#include <QtQuick/QQuickItem> -#include <QtCore/QDebug> -#include <QtGui/QCursor> -#include <QtCore/QCoreApplication> -#include <QtQml/QQmlFile> -#include <QtTest/qsignalspy.h> -#include <QtTest/QTest> - -bool QQuickVisualTestUtil::delegateVisible(QQuickItem *item) -{ - return item->isVisible() && !QQuickItemPrivate::get(item)->culled; -} - -QQuickItem *QQuickVisualTestUtil::findVisibleChild(QQuickItem *parent, const QString &objectName) -{ - QQuickItem *item = 0; - QList<QQuickItem*> items = parent->findChildren<QQuickItem*>(objectName); - for (int i = 0; i < items.count(); ++i) { - if (items.at(i)->isVisible() && !QQuickItemPrivate::get(items.at(i))->culled) { - item = items.at(i); - break; - } - } - return item; -} - -void QQuickVisualTestUtil::dumpTree(QQuickItem *parent, int depth) -{ - static QString padding(" "); - for (int i = 0; i < parent->childItems().count(); ++i) { - QQuickItem *item = qobject_cast<QQuickItem*>(parent->childItems().at(i)); - if (!item) - continue; - qDebug() << padding.left(depth*2) << item; - dumpTree(item, depth+1); - } -} - -void QQuickVisualTestUtil::moveMouseAway(QQuickWindow *window) -{ -#if QT_CONFIG(cursor) // Get the cursor out of the way. - // Using "bottomRight() + QPoint(100, 100)" was causing issues on Ubuntu, - // where the window was positioned at the bottom right corner of the window - // (even after centering the window on the screen), so we use another position. - QCursor::setPos(window->geometry().bottomLeft() + QPoint(0, 10)); -#endif - - // make sure hover events from QQuickWindowPrivate::flushFrameSynchronousEvents() - // do not interfere with the tests - QEvent leave(QEvent::Leave); - QCoreApplication::sendEvent(window, &leave); -} - -void QQuickVisualTestUtil::centerOnScreen(QQuickWindow *window) -{ - const QRect screenGeometry = window->screen()->availableGeometry(); - const QPoint offset = QPoint(window->width() / 2, window->height() / 2); - window->setFramePosition(screenGeometry.center() - offset); -} - -/*! - \internal - - Finds the delegate at \c index belonging to \c itemView, using the given \c flags. - - If the view needs to be polished, the function will wait for it to be done before continuing, - and returns \c nullptr if the polish didn't happen. -*/ -QQuickItem *QQuickVisualTestUtil::findViewDelegateItem(QQuickItemView *itemView, int index, FindViewDelegateItemFlags flags) -{ - if (QQuickTest::qIsPolishScheduled(itemView)) { - if (!QQuickTest::qWaitForItemPolished(itemView)) { - qWarning() << "failed to polish" << itemView; - return nullptr; - } - } - - // Do this after the polish, just in case the count changes after a polish... - if (index <= -1 || index >= itemView->count()) { - qWarning() << "index" << index << "is out of bounds for" << itemView; - return nullptr; - } - - if (flags.testFlag(FindViewDelegateItemFlag::PositionViewAtIndex)) - itemView->positionViewAtIndex(index, QQuickItemView::Center); - - return itemView->itemAtIndex(index); -} - -void QQuickVisualTestUtil::forEachControl(QQmlEngine *engine, const QString &sourcePath, const QString &targetPath, const QStringList &skipList, QQuickVisualTestUtil::ForEachCallback callback) -{ - // We cannot use QQmlComponent to load QML files directly from the source tree. - // For styles that use internal QML types (eg. material/Ripple.qml), the source - // dir would be added as an "implicit" import path overriding the actual import - // path (qtbase/qml/QtQuick/Controls.2/Material). => The QML engine fails to load - // the style C++ plugin from the implicit import path (the source dir). - // - // Therefore we only use the source tree for finding out the set of QML files that - // a particular style implements, and then we locate the respective QML files in - // the engine's import path. This way we can use QQmlComponent to load each QML file - // for benchmarking. - - const QFileInfoList entries = QDir(QQC2_IMPORT_PATH "/" + sourcePath).entryInfoList(QStringList("*.qml"), QDir::Files); - for (const QFileInfo &entry : entries) { - QString name = entry.baseName(); - if (!skipList.contains(name)) { - const auto importPathList = engine->importPathList(); - for (const QString &importPath : importPathList) { - QString name = entry.dir().dirName() + "/" + entry.fileName(); - QString filePath = importPath + "/" + targetPath + "/" + entry.fileName(); - if (filePath.startsWith(":")) - filePath.prepend("qrc"); - if (QFile::exists(filePath)) { - callback(name, QUrl::fromLocalFile(filePath)); - break; - } else { - QUrl url(http://code.qt.io/filePath); - filePath = QQmlFile::urlToLocalFileOrQrc(filePath); - if (!filePath.isEmpty() && QFile::exists(filePath)) { - callback(name, url); - break; - } - } - } - } - } -} - -void QQuickVisualTestUtil::addTestRowForEachControl(QQmlEngine *engine, const QString &sourcePath, const QString &targetPath, const QStringList &skipList) -{ - forEachControl(engine, sourcePath, targetPath, skipList, [&](const QString &relativePath, const QUrl &absoluteUrl) { - QTest::newRow(qPrintable(relativePath)) << absoluteUrl; - }); -} - -QQuickVisualTestUtil::MnemonicKeySimulator::MnemonicKeySimulator(QWindow *window) - : m_window(window), m_modifiers(Qt::NoModifier) -{ -} - -void QQuickVisualTestUtil::MnemonicKeySimulator::press(Qt::Key key) -{ - // QTest::keyPress() but not generating the press event for the modifier key. - if (key == Qt::Key_Alt) - m_modifiers |= Qt::AltModifier; - QTest::simulateEvent(m_window, true, key, m_modifiers, QString(), false); -} - -void QQuickVisualTestUtil::MnemonicKeySimulator::release(Qt::Key key) -{ - // QTest::keyRelease() but not generating the release event for the modifier key. - if (key == Qt::Key_Alt) - m_modifiers &= ~Qt::AltModifier; - QTest::simulateEvent(m_window, false, key, m_modifiers, QString(), false); -} - -void QQuickVisualTestUtil::MnemonicKeySimulator::click(Qt::Key key) -{ - press(key); - release(key); -} - - -bool QQuickVisualTestUtil::verifyButtonClickable(QQuickAbstractButton *button) -{ - if (!button->window()) { - qWarning() << "button" << button << "doesn't have an associated window"; - return false; - } - - if (!button->isEnabled()) { - qWarning() << "button" << button << "is not enabled"; - return false; - } - - if (!button->isVisible()) { - qWarning() << "button" << button << "is not visible"; - return false; - } - - if (button->width() <= 0.0) { - qWarning() << "button" << button << "must have a width greater than 0"; - return false; - } - - if (button->height() <= 0.0) { - qWarning() << "button" << button << "must have a height greater than 0"; - return false; - } - - return true; -} - -bool QQuickVisualTestUtil::clickButton(QQuickAbstractButton *button) -{ - if (!verifyButtonClickable(button)) - return false; - - QSignalSpy spy(button, &QQuickAbstractButton::clicked); - if (!spy.isValid()) { - qWarning() << "button" << button << "must have a valid clicked signal"; - return false; - } - - const QPoint buttonCenter = button->mapToScene(QPointF(button->width() / 2, button->height() / 2)).toPoint(); - QTest::mouseClick(button->window(), Qt::LeftButton, Qt::NoModifier, buttonCenter); - if (spy.count() != 1) { - qWarning() << "clicked signal of button" << button << "was not emitted after clicking"; - return false; - } - - return true; -} - -bool QQuickVisualTestUtil::doubleClickButton(QQuickAbstractButton *button) -{ - if (!verifyButtonClickable(button)) - return false; - - QSignalSpy spy(button, &QQuickAbstractButton::clicked); - if (!spy.isValid()) { - qWarning() << "button" << button << "must have a valid doubleClicked signal"; - return false; - } - - const QPoint buttonCenter = button->mapToScene(QPointF(button->width() / 2, button->height() / 2)).toPoint(); - QTest::mouseDClick(button->window(), Qt::LeftButton, Qt::NoModifier, buttonCenter); - if (spy.count() != 1) { - qWarning() << "doubleClicked signal of button" << button << "was not emitted after double-clicking"; - return false; - } - - return true; -} diff --git a/tests/auto/quickcontrols2/shared/visualtestutil.h b/tests/auto/quickcontrols2/shared/visualtestutil.h deleted file mode 100644 index 142aefd2..00000000 --- a/tests/auto/quickcontrols2/shared/visualtestutil.h +++ /dev/null @@ -1,252 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2017 The Qt Company Ltd. -** Contact: http://www.qt.io/licensing/ -** -** This file is part of the test suite of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL3$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see http://www.qt.io/terms-conditions. For further -** information use the contact form at http://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPLv3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or later as published by the Free -** Software Foundation and appearing in the file LICENSE.GPL included in -** the packaging of this file. Please review the following information to -** ensure the GNU General Public License version 2.0 requirements will be -** met: http://www.gnu.org/licenses/gpl-2.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#ifndef QQUICKVISUALTESTUTIL_H -#define QQUICKVISUALTESTUTIL_H - -#include <functional> - -#include <QtCore/QPointer> -#include <QtQuick/QQuickItem> -#include <QtQml/QQmlExpression> -#include <QtQuick/private/qquickitem_p.h> -#include <QtQuick/private/qquickitemview_p.h> -#include <QtQuickTest/quicktest.h> -#include <QtQuickControls2/qquickstyle.h> -#include <QtQuickTemplates2/private/qquickapplicationwindow_p.h> -#include <QtQuickTemplates2/private/qquickabstractbutton_p.h> - -#include "util.h" - -namespace QQuickVisualTestUtil -{ - QQuickItem *findVisibleChild(QQuickItem *parent, const QString &objectName); - - void dumpTree(QQuickItem *parent, int depth = 0); - - bool delegateVisible(QQuickItem *item); - - void centerOnScreen(QQuickWindow *window); - - void moveMouseAway(QQuickWindow *window); - - /* - Find an item with the specified objectName. If index is supplied then the - item must also evaluate the {index} expression equal to index - */ - template<typename T> - T *findItem(QQuickItem *parent, const QString &objectName, int index = -1) - { - const QMetaObject &mo = T::staticMetaObject; - for (int i = 0; i < parent->childItems().count(); ++i) { - QQuickItem *item = qobject_cast<QQuickItem*>(parent->childItems().at(i)); - if (!item) - continue; - if (mo.cast(item) && (objectName.isEmpty() || item->objectName() == objectName)) { - if (index != -1) { - QQmlExpression e(qmlContext(item), item, "index"); - if (e.evaluate().toInt() == index) - return static_cast<T*>(item); - } else { - return static_cast<T*>(item); - } - } - item = findItem<T>(item, objectName, index); - if (item) - return static_cast<T*>(item); - } - - return 0; - } - - template<typename T> - QList<T*> findItems(QQuickItem *parent, const QString &objectName, bool visibleOnly = true) - { - QList<T*> items; - const QMetaObject &mo = T::staticMetaObject; - for (int i = 0; i < parent->childItems().count(); ++i) { - QQuickItem *item = qobject_cast<QQuickItem*>(parent->childItems().at(i)); - if (!item || (visibleOnly && (!item->isVisible() || QQuickItemPrivate::get(item)->culled))) - continue; - if (mo.cast(item) && (objectName.isEmpty() || item->objectName() == objectName)) - items.append(static_cast<T*>(item)); - items += findItems<T>(item, objectName); - } - - return items; - } - - template<typename T> - QList<T*> findItems(QQuickItem *parent, const QString &objectName, const QList<int> &indexes) - { - QList<T*> items; - for (int i=0; i<indexes.count(); i++) - items << qobject_cast<QQuickItem*>(findItem<T>(parent, objectName, indexes[i])); - return items; - } - - enum class FindViewDelegateItemFlag { - None = 0x0, - PositionViewAtIndex = 0x01 - }; - Q_DECLARE_FLAGS(FindViewDelegateItemFlags, FindViewDelegateItemFlag) - - QQuickItem* findViewDelegateItem(QQuickItemView *itemView, int index, - FindViewDelegateItemFlags flags = FindViewDelegateItemFlag::PositionViewAtIndex); - - /*! - \internal - - Same as above except allows use in QTRY_* functions without having to call it again - afterwards to assign the delegate. - */ - template<typename T> - bool findViewDelegateItem(QQuickItemView *itemView, int index, T &delegateItem, - FindViewDelegateItemFlags flags = FindViewDelegateItemFlag::PositionViewAtIndex) - { - delegateItem = qobject_cast<T>(findViewDelegateItem(itemView, index, flags)); - return delegateItem != nullptr; - } - - class QQuickApplicationHelper - { - public: - QQuickApplicationHelper(QQmlDataTest *testCase, const QString &testFilePath, - const QStringList &qmlImportPaths = {}, - const QVariantMap &initialProperties = {}) - { - for (const auto &path : qmlImportPaths) - engine.addImportPath(path); - - QQmlComponent component(&engine); - - component.loadUrl(testCase->testFileUrl(testFilePath)); - QVERIFY2(component.isReady(), qPrintable(component.errorString())); - QObject *rootObject = component.createWithInitialProperties(initialProperties); - cleanup.reset(rootObject); - if (component.isError() || !rootObject) { - errorMessage = QString::fromUtf8("Failed to create window: %1").arg(component.errorString()).toUtf8(); - return; - } - - window = qobject_cast<QQuickWindow*>(rootObject); - appWindow = qobject_cast<QQuickApplicationWindow*>(rootObject); - if (!window) { - errorMessage = QString::fromUtf8("Root object %1 must be a QQuickWindow subclass").arg(QDebug::toString(window)).toUtf8(); - return; - } - - if (window->isVisible()) { - errorMessage = QString::fromUtf8("Expected window not to be visible, but it is").toUtf8(); - return; - } - - ready = true; - } - - // Return a C-style string instead of QString because that's what QTest uses for error messages, - // so it saves code at the calling site. - inline const char *failureMessage() const - { - return errorMessage.constData(); - } - - QQmlEngine engine; - QScopedPointer<QObject> cleanup; - QQuickApplicationWindow *appWindow = nullptr; - QQuickWindow *window = nullptr; - - bool ready = false; - // Store as a byte array so that we can return its raw data safely; - // using qPrintable() in failureMessage() will construct a throwaway QByteArray - // that is destroyed before the function returns. - QByteArray errorMessage; - }; - - struct QQuickStyleHelper - { - bool updateStyle(const QString &style) - { - // If it's not the first time a style has been set and the new style is not different, do nothing. - if (!currentStyle.isEmpty() && style == currentStyle) - return false; - - engine.reset(); - currentStyle = style; - qmlClearTypeRegistrations(); - engine.reset(new QQmlEngine); - QQuickStyle::setStyle(style); - - QQmlComponent component(engine.data()); - component.setData(QString("import QtQuick\nimport QtQuick.Controls\n Control { }").toUtf8(), QUrl()); - - return true; - } - - QString currentStyle; - QScopedPointer<QQmlEngine> engine; - }; - - typedef std::function<void(const QString &/*relativePath*/, const QUrl &/*absoluteUrl*/)> ForEachCallback; - - void forEachControl(QQmlEngine *engine, const QString &sourcePath, const QString &targetPath, const QStringList &skipList, ForEachCallback callback); - void addTestRowForEachControl(QQmlEngine *engine, const QString &sourcePath, const QString &targetPath, const QStringList &skipList = QStringList()); - - // Helper to simulate Alt itself and Alt+<key> events. - class MnemonicKeySimulator - { - Q_DISABLE_COPY(MnemonicKeySimulator) - public: - explicit MnemonicKeySimulator(QWindow *window); - - void press(Qt::Key key); - void release(Qt::Key key); - void click(Qt::Key key); - - private: - QPointer<QWindow> m_window; - Qt::KeyboardModifiers m_modifiers; - }; - - [[nodiscard]] bool verifyButtonClickable(QQuickAbstractButton *button); - [[nodiscard]] bool clickButton(QQuickAbstractButton *button); - [[nodiscard]] bool doubleClickButton(QQuickAbstractButton *button); -} - -#define QQUICK_VERIFY_POLISH(item) \ - QTRY_COMPARE(QQuickItemPrivate::get(item)->polishScheduled, false) - -#endif // QQUICKVISUALTESTUTIL_H |