From 28003647c6efe8183a0f7b4992edd983735637e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 22 Apr 2023 11:26:21 +0200 Subject: [PATCH] Add test --- test/cli/QML-Samples-TableView/README | 2 + .../cli/QML-Samples-TableView/TableColumn.qml | 11 +++ test/cli/QML-Samples-TableView/TableRow.qml | 25 +++++++ test/cli/QML-Samples-TableView/TableView.pro | 27 ++++++++ test/cli/QML-Samples-TableView/TableView.qml | 30 ++++++++ test/cli/QML-Samples-TableView/main.cpp | 25 +++++++ test/cli/QML-Samples-TableView/main.qml | 37 ++++++++++ test/cli/QML-Samples-TableView/qml.qrc | 8 +++ .../cli/QML-Samples-TableView/samplemodel.cpp | 68 +++++++++++++++++++ test/cli/QML-Samples-TableView/samplemodel.h | 30 ++++++++ test/cli/test-qml.py | 12 ++++ 11 files changed, 275 insertions(+) create mode 100644 test/cli/QML-Samples-TableView/README create mode 100644 test/cli/QML-Samples-TableView/TableColumn.qml create mode 100644 test/cli/QML-Samples-TableView/TableRow.qml create mode 100644 test/cli/QML-Samples-TableView/TableView.pro create mode 100644 test/cli/QML-Samples-TableView/TableView.qml create mode 100644 test/cli/QML-Samples-TableView/main.cpp create mode 100644 test/cli/QML-Samples-TableView/main.qml create mode 100644 test/cli/QML-Samples-TableView/qml.qrc create mode 100644 test/cli/QML-Samples-TableView/samplemodel.cpp create mode 100644 test/cli/QML-Samples-TableView/samplemodel.h create mode 100644 test/cli/test-qml.py diff --git a/test/cli/QML-Samples-TableView/README b/test/cli/QML-Samples-TableView/README new file mode 100644 index 00000000000..fed52488ade --- /dev/null +++ b/test/cli/QML-Samples-TableView/README @@ -0,0 +1,2 @@ +Downloaded from here: +https://github.com/HamedMasafi/QML-Samples diff --git a/test/cli/QML-Samples-TableView/TableColumn.qml b/test/cli/QML-Samples-TableView/TableColumn.qml new file mode 100644 index 00000000000..a29f2a7b109 --- /dev/null +++ b/test/cli/QML-Samples-TableView/TableColumn.qml @@ -0,0 +1,11 @@ +import QtQuick 2.0 +import QtQuick.Layouts 1.12 +import QtQuick.Controls 2.12 +import QtQml.Models 2.12 + +QtObject { + property string title + property string role + property bool fillWidth: false + property int size: -1 +} diff --git a/test/cli/QML-Samples-TableView/TableRow.qml b/test/cli/QML-Samples-TableView/TableRow.qml new file mode 100644 index 00000000000..2b1c7b0c46f --- /dev/null +++ b/test/cli/QML-Samples-TableView/TableRow.qml @@ -0,0 +1,25 @@ +import QtQuick 2.0 +import QtQuick.Layouts 1.12 +import QtQuick.Controls 2.12 +import QtQml.Models 2.12 + +ItemDelegate { + height: 40 + width: parent.width + property bool isHeader: false + RowLayout { + property var _d: model + anchors.fill: parent + anchors.leftMargin: 9 + + Repeater { + model: root.columns + Label { + text: parent.parent.isHeader ? modelData.title : parent._d[modelData.role] + Layout.fillWidth: modelData.fillWidth + Layout.preferredWidth: modelData.size !== '*' + ? modelData.size : 20 + } + } + } +} diff --git a/test/cli/QML-Samples-TableView/TableView.pro b/test/cli/QML-Samples-TableView/TableView.pro new file mode 100644 index 00000000000..4dc16d5dc3b --- /dev/null +++ b/test/cli/QML-Samples-TableView/TableView.pro @@ -0,0 +1,27 @@ +QT += quick + +CONFIG += c++11 + +# You can make your code fail to compile if it uses deprecated APIs. +# In order to do so, uncomment the following line. +#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 + +SOURCES += \ + main.cpp \ + samplemodel.cpp + +RESOURCES += qml.qrc + +# Additional import path used to resolve QML modules in Qt Creator's code model +QML_IMPORT_PATH = + +# Additional import path used to resolve QML modules just for Qt Quick Designer +QML_DESIGNER_IMPORT_PATH = + +# Default rules for deployment. +qnx: target.path = /tmp/$${TARGET}/bin +else: unix:!android: target.path = /opt/$${TARGET}/bin +!isEmpty(target.path): INSTALLS += target + +HEADERS += \ + samplemodel.h diff --git a/test/cli/QML-Samples-TableView/TableView.qml b/test/cli/QML-Samples-TableView/TableView.qml new file mode 100644 index 00000000000..a30b7c8d737 --- /dev/null +++ b/test/cli/QML-Samples-TableView/TableView.qml @@ -0,0 +1,30 @@ +import QtQuick 2.0 +import QtQuick.Layouts 1.12 +import QtQuick.Controls 2.12 +import QtQml.Models 2.12 + +Item { + id: root + + property alias model: tableView.model + default property list columns + + TableRow { + id: header + anchors.left: parent.left + anchors.right: parent.right + anchors.top: parent.top + background: null + isHeader: true + height: 60 + } + + ListView { + id: tableView + clip: true + + anchors.fill: parent + anchors.topMargin: header.height + delegate: TableRow {} + } +} diff --git a/test/cli/QML-Samples-TableView/main.cpp b/test/cli/QML-Samples-TableView/main.cpp new file mode 100644 index 00000000000..1c57799425c --- /dev/null +++ b/test/cli/QML-Samples-TableView/main.cpp @@ -0,0 +1,25 @@ +#include +#include +#include "samplemodel.h" + +int main(int argc, char *argv[]) +{ +#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) + QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); +#endif + + QGuiApplication app(argc, argv); + + QQmlApplicationEngine engine; + qmlRegisterType("Test", 1, 0, "SampleModel"); + + const QUrl url(QStringLiteral("qrc:/main.qml")); + QObject::connect(&engine, &QQmlApplicationEngine::objectCreated, + &app, [url](QObject *obj, const QUrl &objUrl) { + if (!obj && url == objUrl) + QCoreApplication::exit(-1); + }, Qt::QueuedConnection); + engine.load(url); + + return app.exec(); +} diff --git a/test/cli/QML-Samples-TableView/main.qml b/test/cli/QML-Samples-TableView/main.qml new file mode 100644 index 00000000000..6a8cc43f9cf --- /dev/null +++ b/test/cli/QML-Samples-TableView/main.qml @@ -0,0 +1,37 @@ +import QtQuick 2.12 +import QtQuick.Window 2.12 +import Test 1.0 +import '.' as Here + +Window { + width: 640 + height: 480 + visible: true + title: qsTr("Hello World") + + Component.onCompleted: sampleModel.fillSampleData(50) + SampleModel { + id: sampleModel + } + + Here.TableView { + anchors.fill: parent + model: sampleModel + + TableColumn { + title: qsTr("Id") + role: 'id' + size: 30 + } + TableColumn { + title: qsTr("Name") + role: 'name' + fillWidth: true + } + TableColumn { + title: qsTr("Grade") + role: 'grade' + size: 50 + } + } +} diff --git a/test/cli/QML-Samples-TableView/qml.qrc b/test/cli/QML-Samples-TableView/qml.qrc new file mode 100644 index 00000000000..cf94b15b8fb --- /dev/null +++ b/test/cli/QML-Samples-TableView/qml.qrc @@ -0,0 +1,8 @@ + + + main.qml + TableView.qml + TableRow.qml + TableColumn.qml + + diff --git a/test/cli/QML-Samples-TableView/samplemodel.cpp b/test/cli/QML-Samples-TableView/samplemodel.cpp new file mode 100644 index 00000000000..de7a5b1395a --- /dev/null +++ b/test/cli/QML-Samples-TableView/samplemodel.cpp @@ -0,0 +1,68 @@ +#include "samplemodel.h" + +#include +#include + +SampleModel::SampleModel(QObject *parent) : QAbstractListModel(parent) +{ + +} + +int SampleModel::rowCount(const QModelIndex &parent) const +{ + Q_UNUSED(parent) + return _data.size(); +} + +QVariant SampleModel::data(const QModelIndex &index, int role) const +{ + if (!index.isValid()) + return QVariant(); + if (index.row() < 0 || index.row() > _data.count() - 1) + return QVariant(); + + auto row = _data.at(index.row()); + + switch (role) { + case IdRole: + return index.row(); + + case NameRole: + return row.first; + + case GradeRole: + return row.second; + } + + return QVariant(); +} + +QHash SampleModel::roleNames() const +{ + return { + {IdRole, "id"}, + {NameRole, "name"}, + {GradeRole, "grade"} + }; +} + +void SampleModel::fillSampleData(int size) +{ + QString abs = "qwertyuiopasdfghjklzxcvbnm"; + QRandomGenerator r; + for (auto i = 0; i < size; i++) { + Row row; + auto nameLen = r.bounded(3, 8); + QString name; + for (int c = 0; c < nameLen; ++c) + name.append(abs.at(r.bounded(0, abs.size() - 1))); + + row.first = name; + row.second = r.bounded(0, 20); + _data.append(row); + } + + qDebug() << _data.size() << "item(s) added as sample data"; + beginInsertRows(QModelIndex(), 0, _data.size() - 1); + endInsertRows(); +} diff --git a/test/cli/QML-Samples-TableView/samplemodel.h b/test/cli/QML-Samples-TableView/samplemodel.h new file mode 100644 index 00000000000..2bb9107e55a --- /dev/null +++ b/test/cli/QML-Samples-TableView/samplemodel.h @@ -0,0 +1,30 @@ +#ifndef SAMPLEMODEL_H +#define SAMPLEMODEL_H + +#include + +class SampleModel : public QAbstractListModel +{ + Q_OBJECT + + typedef QPair Row; + QList _data; + +public: + enum Role { + IdRole = Qt::UserRole + 1, + NameRole, + GradeRole + }; + + SampleModel(QObject *parent = nullptr); + + int rowCount(const QModelIndex &parent) const; + QVariant data(const QModelIndex &index, int role) const; + QHash roleNames() const; + +public slots: + void fillSampleData(int size); +}; + +#endif // SAMPLEMODEL_H diff --git a/test/cli/test-qml.py b/test/cli/test-qml.py new file mode 100644 index 00000000000..4f2c406ae3f --- /dev/null +++ b/test/cli/test-qml.py @@ -0,0 +1,12 @@ + +# python3 -m pytest test-qml.py + +import os +from testutils import cppcheck + +PROJECT_DIR = 'QML-Samples-TableView' + +def test_unused_functions(): + ret, stdout, stderr = cppcheck(['--library=qt', '--enable=unusedFunction', PROJECT_DIR]) + assert ret == 0 + assert '[unusedFunction]' not in stderr