diff options
author | Richard Moe Gustavsen <richard.gustavsen@qt.io> | 2021-02-17 12:57:48 +0100 |
---|---|---|
committer | Richard Moe Gustavsen <richard.gustavsen@qt.io> | 2021-02-22 10:29:26 +0100 |
commit | db1ba4c0d3878c0d719580caebc77dc385b34bc7 (patch) | |
tree | 85e49f6822e896f361d1832b1afa091e663b6c11 | |
parent | ec074133d9c652a4bb9bf03acf57343f9e7015e5 (diff) |
Add autotest boilerplate code
This patch will add the boilerplate code for writing auto tests.
In addition to the QTestLib code, it contains a QAIM (TestModel)
that implements a tree, as well as a QML file with a TreeView that
uses it.
Change-Id: I1810e12ed34f71083fac159bbb87701c46184a6c
Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
-rw-r--r-- | CMakeLists.txt | 2 | ||||
-rw-r--r-- | tests/auto/CMakeLists.txt | 28 | ||||
-rw-r--r-- | tests/auto/auto.pro | 19 | ||||
-rw-r--r-- | tests/auto/data/normaltreeview.qml | 51 | ||||
-rw-r--r-- | tests/auto/testmodel.cpp | 110 | ||||
-rw-r--r-- | tests/auto/testmodel.h | 72 | ||||
-rw-r--r-- | tests/auto/tst_treeview.cpp | 65 |
7 files changed, 340 insertions, 7 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 9b34df3..4e43ec4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,7 +24,7 @@ project(QtTreeView LANGUAGES CXX C ) -find_package(Qt6 ${PROJECT_VERSION} CONFIG REQUIRED COMPONENTS BuildInternals Core Quick QuickControls2) +find_package(Qt6 ${PROJECT_VERSION} CONFIG REQUIRED COMPONENTS BuildInternals Core Quick QuickControls2 QuickTest) find_package(Qt6 ${PROJECT_VERSION} CONFIG OPTIONAL_COMPONENTS Widgets) if(NOT TARGET Qt::Quick) diff --git a/tests/auto/CMakeLists.txt b/tests/auto/CMakeLists.txt index 93850ca..f2b0cd8 100644 --- a/tests/auto/CMakeLists.txt +++ b/tests/auto/CMakeLists.txt @@ -6,9 +6,37 @@ qt_add_test(tst_treeview SOURCES + testmodel.cpp testmodel.h tst_treeview.cpp PUBLIC_LIBRARIES Qt::Gui Qt::Qml Qt::Quick + Qt::QuickPrivate + Qt::QuickTest ) + +# Resources: +set(qmake_immediate_resource_files + "data" +) + +qt_add_resource(tst_treeview "qmake_immediate" + PREFIX + "/" + FILES + ${qmake_immediate_resource_files} +) + + +#### Keys ignored in scope 1:.:.:auto.pro:<TRUE>: +# QML_IMPORT_MAJOR_VERSION = "1" +# QML_IMPORT_NAME = "TestModel" +# TEMPLATE = "app" + +set_target_properties(tst_treeview PROPERTIES + QT_QML_MODULE_VERSION 1.0 + QT_QML_MODULE_URI TestModel +) + +qt6_qml_type_registration(tst_treeview) diff --git a/tests/auto/auto.pro b/tests/auto/auto.pro index 4082382..1949375 100644 --- a/tests/auto/auto.pro +++ b/tests/auto/auto.pro @@ -1,8 +1,19 @@ - +TEMPLATE = app +TARGET = tst_treeview CONFIG += testcase -QT += qml testlib quick +CONFIG += qmltypes +QML_IMPORT_NAME = TestModel +QML_IMPORT_MAJOR_VERSION = 1 -TARGET = tst_treeview +QT += qml testlib quick quick-private qmltest + +SOURCES += \ + tst_treeview.cpp \ + testmodel.cpp + +OTHER_FILES += $$files($$PWD/data) +RESOURCES += $$OTHER_FILES -SOURCES += tst_treeview.cpp +HEADERS += \ + testmodel.h diff --git a/tests/auto/data/normaltreeview.qml b/tests/auto/data/normaltreeview.qml new file mode 100644 index 0000000..2dc10dd --- /dev/null +++ b/tests/auto/data/normaltreeview.qml @@ -0,0 +1,51 @@ +/**************************************************************************** +** +** Copyright (C) 2021 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of TreeView. +** +** $QT_BEGIN_LICENSE:GPL-MARKETPLACE-QT$ +** +** Marketplace License Usage +** Users, who have licensed the Software under the Qt Marketplace license +** agreement, may use this file in accordance with the Qt Marketplace license +** agreement provided with the Software or, alternatively, in accordance with +** the terms contained in a written agreement between the licensee and The Qt +** Company. For licensing terms and conditions see +** https://www.qt.io/terms-conditions/#marketplace and +** https://www.qt.io/terms-conditions. For further information use the contact +** form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 or (at your option) any later version +** approved by the KDE Free Qt Foundation. The licenses are as published by +** the Free Software Foundation and appearing in the file LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +import QtQuick +import QtQuick.Window +import QtQuick.TreeView +import TestModel + +Item { + width: 800 + height: 600 + + property alias treeView: treeView + + TreeView { + id: treeView + anchors.fill:parent + anchors.margins: 10 + model: TestModel {} + clip: true + } +} diff --git a/tests/auto/testmodel.cpp b/tests/auto/testmodel.cpp new file mode 100644 index 0000000..472dc7c --- /dev/null +++ b/tests/auto/testmodel.cpp @@ -0,0 +1,110 @@ + +/**************************************************************************** +** +** Copyright (C) 2021 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of TreeView. +** +** $QT_BEGIN_LICENSE:GPL-MARKETPLACE-QT$ +** +** Marketplace License Usage +** Users, who have licensed the Software under the Qt Marketplace license +** agreement, may use this file in accordance with the Qt Marketplace license +** agreement provided with the Software or, alternatively, in accordance with +** the terms contained in a written agreement between the licensee and The Qt +** Company. For licensing terms and conditions see +** https://www.qt.io/terms-conditions/#marketplace and +** https://www.qt.io/terms-conditions. For further information use the contact +** form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 or (at your option) any later version +** approved by the KDE Free Qt Foundation. The licenses are as published by +** the Free Software Foundation and appearing in the file LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "testmodel.h" + +TreeItem::TreeItem(TreeItem *parent) + : m_parentItem(parent) +{} + +TreeItem::~TreeItem() +{ + qDeleteAll(m_childItems); +} + +int TreeItem::row() const +{ + if (!m_parentItem) + return 0; + return m_parentItem->m_childItems.indexOf(const_cast<TreeItem *>(this)); +} + +TestModel::TestModel(QObject *parent) + : QAbstractItemModel(parent) +{ + m_rootItem.reset(new TreeItem()); + createTreeRecursive(m_rootItem.data(), 4, 0, 4); +} + +void TestModel::createTreeRecursive(TreeItem *item, int childCount, int currentDepth, int maxDepth) +{ + for (int row = 0; row < childCount; ++row) { + auto childItem = new TreeItem(item); + item->m_childItems.append(childItem); + if (currentDepth < maxDepth && row == childCount - 1) + createTreeRecursive(childItem, childCount, currentDepth + 1, maxDepth); + } +} + +TreeItem *TestModel::treeItem(const QModelIndex &index) const +{ + if (!index.isValid()) + return m_rootItem.data(); + return static_cast<TreeItem *>(index.internalPointer()); +} + +int TestModel::rowCount(const QModelIndex &parent) const +{ + if (!parent.isValid()) + return 1; // root of the tree + return treeItem(parent)->m_childItems.count(); +} + +int TestModel::columnCount(const QModelIndex &) const +{ + return 1; +} + +QVariant TestModel::data(const QModelIndex &index, int role) const +{ + Q_UNUSED(role) + return QVariant::fromValue(QString("%1, %2").arg(index.row()).arg(index.column())); +} + +QModelIndex TestModel::index(int row, int column, const QModelIndex &parent) const +{ + Q_UNUSED(column) + if (!hasIndex(row, column, parent)) + return QModelIndex(); + if (!parent.isValid()) + return createIndex(0, 0, m_rootItem.data()); + return createIndex(row, 0, treeItem(parent)->m_childItems.at(row)); +} + +QModelIndex TestModel::parent(const QModelIndex &index) const +{ + const TreeItem *parent = treeItem(index)->m_parentItem; + if (!parent) + return QModelIndex(); + return createIndex(parent->row(), 0, parent); +} diff --git a/tests/auto/testmodel.h b/tests/auto/testmodel.h new file mode 100644 index 0000000..799a569 --- /dev/null +++ b/tests/auto/testmodel.h @@ -0,0 +1,72 @@ +/**************************************************************************** +** +** Copyright (C) 2021 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of TreeView. +** +** $QT_BEGIN_LICENSE:GPL-MARKETPLACE-QT$ +** +** Marketplace License Usage +** Users, who have licensed the Software under the Qt Marketplace license +** agreement, may use this file in accordance with the Qt Marketplace license +** agreement provided with the Software or, alternatively, in accordance with +** the terms contained in a written agreement between the licensee and The Qt +** Company. For licensing terms and conditions see +** https://www.qt.io/terms-conditions/#marketplace and +** https://www.qt.io/terms-conditions. For further information use the contact +** form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 or (at your option) any later version +** approved by the KDE Free Qt Foundation. The licenses are as published by +** the Free Software Foundation and appearing in the file LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef TESTMODEL_H +#define TESTMODEL_H + +#include <QtCore/qabstractitemmodel.h> +#include <QtQuick/qquickview.h> + +class TreeItem +{ +public: + explicit TreeItem(TreeItem *parent = nullptr); + ~TreeItem(); + + int row() const; + QVector<TreeItem *> m_childItems; + TreeItem *m_parentItem; +}; + +// ######################################################## + +class TestModel : public QAbstractItemModel +{ + Q_OBJECT + QML_ELEMENT + +public: + explicit TestModel(QObject *parent = nullptr); + + void createTreeRecursive(TreeItem *item, int childCount, int currentDepth, int maxDepth); + TreeItem *treeItem(const QModelIndex &index) const; + int rowCount(const QModelIndex &parent = QModelIndex()) const override; + int columnCount(const QModelIndex & = QModelIndex()) const override; + QVariant data(const QModelIndex &index, int role) const override; + QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override; + QModelIndex parent(const QModelIndex &index) const override; + +private: + QScopedPointer<TreeItem> m_rootItem; +}; + +#endif // TESTMODEL_H diff --git a/tests/auto/tst_treeview.cpp b/tests/auto/tst_treeview.cpp index d25c029..b9a72bd 100644 --- a/tests/auto/tst_treeview.cpp +++ b/tests/auto/tst_treeview.cpp @@ -32,16 +32,77 @@ #include <qtest.h> +#include <QtQuickTest/quicktest.h> + +#include <QtQuick/qquickview.h> +#include <QtQml/qqmlapplicationengine.h> +#include <QtQml/qqmlcontext.h> + +#include <QtQuick/private/qquicktableview_p.h> +#include <QtQuick/private/qquicktableview_p.h> +#include <QtQuick/private/qquicktableview_p_p.h> + +// Note that QQuickTreeView is not directly accessible from this test since +// it's defined inside a QML plugin, and not inside a library we link to. +// But since it inherits from QQuickTableView, we cast it to that instead +#define LOAD_TREEVIEW(FILENAME) \ + QQuickView *view = createView(FILENAME); \ + QVERIFY(QTest::qWaitForWindowActive(view)); \ + auto treeView = view->rootObject()->property("treeView").value<QQuickTableView *>(); \ + QVERIFY(treeView); \ + auto treeViewPrivate = QQuickTableViewPrivate::get(treeView); \ + Q_UNUSED(treeViewPrivate) + +#define WAIT_UNTIL_POLISHED_ARG(item) \ + QVERIFY(QQuickTest::qIsPolishScheduled(item)); \ + QVERIFY(QQuickTest::qWaitForItemPolished(item)) +#define WAIT_UNTIL_POLISHED WAIT_UNTIL_POLISHED_ARG(treeView) + +// ######################################################## + class tst_treeview : public QObject { Q_OBJECT + QQuickView *createView(const QString &filename); + private slots: - void initTestCase(); + void showTreeView(); + void expandAndCollapseRoot(); }; -void tst_treeview::initTestCase() +QQuickView *tst_treeview::createView(const QString &filename) { + QQuickView *window = new QQuickView(nullptr); + window->setFramePosition(QPoint(100, 100)); + window->setSource(QUrl(QStringLiteral("qrc:///data/") + filename)); + window->show(); + return window; +} + +void tst_treeview::showTreeView() +{ + LOAD_TREEVIEW("normaltreeview.qml"); + // Check that the view is showing the root of the tree + QCOMPARE(treeViewPrivate->loadedRows.count(), 1); +} + +void tst_treeview::expandAndCollapseRoot() +{ + LOAD_TREEVIEW("normaltreeview.qml"); + // Check that the view only has one row loaded so far (the root of the tree) + QCOMPARE(treeViewPrivate->loadedRows.count(), 1); + + // Expand the root + QMetaObject::invokeMethod(treeView, "expand", Q_ARG(int, 0)); + WAIT_UNTIL_POLISHED; + // We now expect 5 rows, the root pluss it's 4 children + QCOMPARE(treeViewPrivate->loadedRows.count(), 5); + + QMetaObject::invokeMethod(treeView, "collapse", Q_ARG(int, 0)); + WAIT_UNTIL_POLISHED; + // Check that the view only has one row loaded again (the root of the tree) + QCOMPARE(treeViewPrivate->loadedRows.count(), 1); } QTEST_MAIN(tst_treeview) |