aboutsummaryrefslogtreecommitdiffstats
path: root/tests/auto/qml/qqmltablemodel
diff options
context:
space:
mode:
authorMitch Curtis <mitch.curtis@qt.io>2019-02-13 18:19:08 +0100
committerMitch Curtis <mitch.curtis@qt.io>2019-02-28 08:11:19 +0000
commit368a1d918a49d01a380131205da2f205fb267a26 (patch)
treea2e7784ba42736bb36d33e54f07cc2ac476c0e7a /tests/auto/qml/qqmltablemodel
parentb4ee855eb10cf9bfa44ce8e5de8f9ee6c5917764 (diff)
TableModel: support built-in QML model roles
For the display role, we'll return the first role in that column if it wasn't explicitly specified. For every other role, we can just return an invalid QVariant. As usual, roleDataProvider can be used for any data that is missing. Before this patch, the extra roles were missing from roleNames, so they couldn't be used in delegates. Change-Id: I53ac5b75526bcddec44baf834f6a093115a70993 Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
Diffstat (limited to 'tests/auto/qml/qqmltablemodel')
-rw-r--r--tests/auto/qml/qqmltablemodel/data/TestModel.qml (renamed from tests/auto/qml/qqmltablemodel/data/defaultDisplayRoles.qml)42
-rw-r--r--tests/auto/qml/qqmltablemodel/data/TestUtils.js45
-rw-r--r--tests/auto/qml/qqmltablemodel/data/builtInRoles.qml47
-rw-r--r--tests/auto/qml/qqmltablemodel/data/common.qml18
-rw-r--r--tests/auto/qml/qqmltablemodel/data/empty.qml3
-rw-r--r--tests/auto/qml/qqmltablemodel/data/explicitDisplayRole.qml41
-rw-r--r--tests/auto/qml/qqmltablemodel/data/setDataThroughDelegate.qml32
-rw-r--r--tests/auto/qml/qqmltablemodel/data/setRowsMultipleTimes.qml18
-rw-r--r--tests/auto/qml/qqmltablemodel/tst_qqmltablemodel.cpp88
9 files changed, 238 insertions, 96 deletions
diff --git a/tests/auto/qml/qqmltablemodel/data/defaultDisplayRoles.qml b/tests/auto/qml/qqmltablemodel/data/TestModel.qml
index 32daea61f9..7aeb5d03f4 100644
--- a/tests/auto/qml/qqmltablemodel/data/defaultDisplayRoles.qml
+++ b/tests/auto/qml/qqmltablemodel/data/TestModel.qml
@@ -26,36 +26,22 @@
**
****************************************************************************/
-import QtQuick 2.12
import Qt.labs.qmlmodels 1.0
-Item {
- id: root
- width: 200
- height: 200
+import "TestUtils.js" as TestUtils
- property alias testModel: testModel
-
- TableModel {
- id: testModel
- objectName: "testModel"
- rows: [
- [
- { name: "John", someOtherRole1: "foo" }, // column 0
- { age: 22, someOtherRole2: "foo" } // column 1
- ],
- [
- { name: "Oliver", someOtherRole1: "foo" }, // column 0
- { age: 33, someOtherRole2: "foo" } // column 1
- ]
+TableModel {
+ id: testModel
+ objectName: "testModel"
+ roleDataProvider: TestUtils.testModelRoleDataProvider
+ rows: [
+ [
+ { name: "John" },
+ { age: 22 }
+ ],
+ [
+ { name: "Oliver" },
+ { age: 33 }
]
- }
- TableView {
- anchors.fill: parent
- model: testModel
- delegate: Text {
- id: textItem
- text: model.display
- }
- }
+ ]
}
diff --git a/tests/auto/qml/qqmltablemodel/data/TestUtils.js b/tests/auto/qml/qqmltablemodel/data/TestUtils.js
new file mode 100644
index 0000000000..0b92a377bb
--- /dev/null
+++ b/tests/auto/qml/qqmltablemodel/data/TestUtils.js
@@ -0,0 +1,45 @@
+/****************************************************************************
+**
+** Copyright (C) 2019 The Qt Company Ltd.
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the test suite of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:GPL-EXCEPT$
+** 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 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 as published by the Free Software
+** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
+** 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$
+**
+****************************************************************************/
+
+function testModelRoleDataProvider(index, role, cellData) {
+ switch (role) {
+ case "display":
+ switch (index.column) {
+ case 0:
+ return cellData.name
+ case 1:
+ return cellData.age
+ }
+ break
+ case "name":
+ return cellData.name
+ case "age":
+ return cellData.age
+ }
+ return cellData
+}
diff --git a/tests/auto/qml/qqmltablemodel/data/builtInRoles.qml b/tests/auto/qml/qqmltablemodel/data/builtInRoles.qml
new file mode 100644
index 0000000000..d9882e4dea
--- /dev/null
+++ b/tests/auto/qml/qqmltablemodel/data/builtInRoles.qml
@@ -0,0 +1,47 @@
+/****************************************************************************
+**
+** Copyright (C) 2019 The Qt Company Ltd.
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the test suite of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:GPL-EXCEPT$
+** 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 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 as published by the Free Software
+** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
+** 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 Qt.labs.qmlmodels 1.0
+
+import "TestUtils.js" as TestUtils
+
+TableModel {
+ id: testModel
+ objectName: "testModel"
+ roleDataProvider: TestUtils.testModelRoleDataProvider
+ rows: [
+ [
+ { name: "John", someOtherRole1: "foo" }, // column 0
+ { age: 22, someOtherRole2: "foo" } // column 1
+ ],
+ [
+ { name: "Oliver", someOtherRole1: "foo" }, // column 0
+ { age: 33, someOtherRole2: "foo" } // column 1
+ ]
+ ]
+}
diff --git a/tests/auto/qml/qqmltablemodel/data/common.qml b/tests/auto/qml/qqmltablemodel/data/common.qml
index 2ae9c50a1b..aec796bd4f 100644
--- a/tests/auto/qml/qqmltablemodel/data/common.qml
+++ b/tests/auto/qml/qqmltablemodel/data/common.qml
@@ -115,24 +115,12 @@ Item {
])
}
- TableModel {
- id: testModel
- objectName: "testModel"
- rows: [
- [
- { name: "John" },
- { age: 22 }
- ],
- [
- { name: "Oliver" },
- { age: 33 }
- ]
- ]
- }
TableView {
id: tableView
anchors.fill: parent
- model: testModel
+ model: TestModel {
+ id: testModel
+ }
delegate: Text {
text: model.display
}
diff --git a/tests/auto/qml/qqmltablemodel/data/empty.qml b/tests/auto/qml/qqmltablemodel/data/empty.qml
index 57f2f992d9..6e66b99145 100644
--- a/tests/auto/qml/qqmltablemodel/data/empty.qml
+++ b/tests/auto/qml/qqmltablemodel/data/empty.qml
@@ -29,6 +29,8 @@
import QtQuick 2.12
import Qt.labs.qmlmodels 1.0
+import "TestUtils.js" as TestUtils
+
Item {
id: root
width: 200
@@ -53,6 +55,7 @@ Item {
TableModel {
id: testModel
objectName: "testModel"
+ roleDataProvider: TestUtils.testModelRoleDataProvider
}
TableView {
id: tableView
diff --git a/tests/auto/qml/qqmltablemodel/data/explicitDisplayRole.qml b/tests/auto/qml/qqmltablemodel/data/explicitDisplayRole.qml
new file mode 100644
index 0000000000..510a62e74b
--- /dev/null
+++ b/tests/auto/qml/qqmltablemodel/data/explicitDisplayRole.qml
@@ -0,0 +1,41 @@
+/****************************************************************************
+**
+** Copyright (C) 2019 The Qt Company Ltd.
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the test suite of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:GPL-EXCEPT$
+** 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 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 as published by the Free Software
+** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
+** 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 Qt.labs.qmlmodels 1.0
+
+import "TestUtils.js" as TestUtils
+
+TableModel {
+ id: testModel
+ rows: [
+ [
+ { name: "John", display: "foo" },
+ { age: 22, display: "bar" }
+ ]
+ ]
+}
diff --git a/tests/auto/qml/qqmltablemodel/data/setDataThroughDelegate.qml b/tests/auto/qml/qqmltablemodel/data/setDataThroughDelegate.qml
index 7a419d81c6..5f849c3350 100644
--- a/tests/auto/qml/qqmltablemodel/data/setDataThroughDelegate.qml
+++ b/tests/auto/qml/qqmltablemodel/data/setDataThroughDelegate.qml
@@ -52,25 +52,27 @@ Item {
shouldModifyInvalidType()
}
- TableModel {
- id: testModel
- objectName: "testModel"
- rows: [
- [
- { name: "John" },
- { age: 22 }
- ],
- [
- { name: "Oliver" },
- { age: 33 }
- ]
- ]
- }
TableView {
anchors.fill: parent
- model: testModel
+ model: TableModel {
+ id: testModel
+ objectName: "testModel"
+ rows: [
+ [
+ { name: "John" },
+ { age: 22 }
+ ],
+ [
+ { name: "Oliver" },
+ { age: 33 }
+ ]
+ ]
+ }
+
delegate: Text {
id: textItem
+ // TODO: this is currently random when no roleDataProvider handles it
+ // we should allow roleDataProvider to be used to handle specific roles only
text: model.display
Connections {
diff --git a/tests/auto/qml/qqmltablemodel/data/setRowsMultipleTimes.qml b/tests/auto/qml/qqmltablemodel/data/setRowsMultipleTimes.qml
index 15d52f93a6..6aaf79f2d4 100644
--- a/tests/auto/qml/qqmltablemodel/data/setRowsMultipleTimes.qml
+++ b/tests/auto/qml/qqmltablemodel/data/setRowsMultipleTimes.qml
@@ -71,24 +71,12 @@ Item {
]
}
- TableModel {
- id: testModel
- objectName: "testModel"
- rows: [
- [
- { name: "John" },
- { age: 22 }
- ],
- [
- { name: "Oliver" },
- { age: 33 }
- ]
- ]
- }
TableView {
id: tableView
anchors.fill: parent
- model: testModel
+ model: TestModel {
+ id: testModel
+ }
delegate: Text {
text: model.display
}
diff --git a/tests/auto/qml/qqmltablemodel/tst_qqmltablemodel.cpp b/tests/auto/qml/qqmltablemodel/tst_qqmltablemodel.cpp
index eb1fbda45a..059ce082d9 100644
--- a/tests/auto/qml/qqmltablemodel/tst_qqmltablemodel.cpp
+++ b/tests/auto/qml/qqmltablemodel/tst_qqmltablemodel.cpp
@@ -55,11 +55,15 @@ private slots:
void setDataThroughDelegate();
void setRowsImperatively();
void setRowsMultipleTimes();
- void defaultDisplayRoles();
+ void builtInRoles_data();
+ void builtInRoles();
+ void explicitDisplayRole();
void roleDataProvider();
void dataAndEditing();
};
+static const int builtInRoleCount = 6;
+
void tst_QQmlTableModel::appendRemoveRow()
{
QQuickView view(testFileUrl("common.qml"));
@@ -80,11 +84,15 @@ void tst_QQmlTableModel::appendRemoveRow()
int heightSignalEmissions = 0;
const QHash<int, QByteArray> roleNames = model->roleNames();
- QCOMPARE(roleNames.size(), 3);
+ QCOMPARE(roleNames.size(), 2 + builtInRoleCount);
QVERIFY(roleNames.values().contains("name"));
QVERIFY(roleNames.values().contains("age"));
QVERIFY(roleNames.values().contains("display"));
-
+ QVERIFY(roleNames.values().contains("decoration"));
+ QVERIFY(roleNames.values().contains("edit"));
+ QVERIFY(roleNames.values().contains("toolTip"));
+ QVERIFY(roleNames.values().contains("statusTip"));
+ QVERIFY(roleNames.values().contains("whatsThis"));
QCOMPARE(model->data(model->index(0, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("John"));
QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("age")).toInt(), 22);
QCOMPARE(model->data(model->index(1, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("Oliver"));
@@ -208,10 +216,9 @@ void tst_QQmlTableModel::clear()
QVERIFY(rowCountSpy.isValid());
const QHash<int, QByteArray> roleNames = model->roleNames();
- QCOMPARE(roleNames.size(), 3);
QVERIFY(roleNames.values().contains("name"));
QVERIFY(roleNames.values().contains("age"));
- QVERIFY(roleNames.values().contains("display"));
+ QCOMPARE(roleNames.size(), 2 + builtInRoleCount);
QQuickTableView *tableView = view.rootObject()->property("tableView").value<QQuickTableView*>();
QVERIFY(tableView);
@@ -221,8 +228,8 @@ void tst_QQmlTableModel::clear()
QVERIFY(QMetaObject::invokeMethod(model, "clear"));
QCOMPARE(model->rowCount(), 0);
QCOMPARE(model->columnCount(), 2);
- QCOMPARE(model->data(model->index(0, 0, QModelIndex()), roleNames.key("name")), QVariant());
- QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("age")), QVariant());
+ QCOMPARE(model->data(model->index(0, 0, QModelIndex()), roleNames.key("display")), QVariant());
+ QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("display")), QVariant());
QCOMPARE(columnCountSpy.count(), 0);
QCOMPARE(rowCountSpy.count(), 1);
// Wait until updatePolish() gets called, which is where the size is recalculated.
@@ -690,10 +697,9 @@ void tst_QQmlTableModel::setDataThroughDelegate()
QVERIFY(rowCountSpy.isValid());
const QHash<int, QByteArray> roleNames = model->roleNames();
- QCOMPARE(roleNames.size(), 3);
+ QCOMPARE(roleNames.size(), 2 + builtInRoleCount);
QVERIFY(roleNames.values().contains("name"));
QVERIFY(roleNames.values().contains("age"));
- QVERIFY(roleNames.values().contains("display"));
QCOMPARE(model->data(model->index(0, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("John"));
QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("age")).toInt(), 22);
QCOMPARE(model->data(model->index(1, 0, QModelIndex()), roleNames.key("name")).toString(), QLatin1String("Oliver"));
@@ -836,29 +842,65 @@ void tst_QQmlTableModel::setRowsMultipleTimes()
QCOMPARE(tableView->columns(), 2);
}
-void tst_QQmlTableModel::defaultDisplayRoles()
+void tst_QQmlTableModel::builtInRoles_data()
{
- QQuickView view(testFileUrl("defaultDisplayRoles.qml"));
- QCOMPARE(view.status(), QQuickView::Ready);
- view.show();
- QVERIFY(QTest::qWaitForWindowActive(&view));
+ QTest::addColumn<int>("row");
+ QTest::addColumn<int>("column");
+ QTest::addColumn<QByteArray>("roleName");
+ QTest::addColumn<QVariant>("expectedValue");
- QQmlTableModel *model = view.rootObject()->property("testModel").value<QQmlTableModel*>();
+ const QByteArray displayRole = "display";
+
+ QTest::addRow("display(0,0)") << 0 << 0 << displayRole << QVariant(QLatin1String("John"));
+ QTest::addRow("display(0,1)") << 0 << 1 << displayRole << QVariant(QLatin1String("22"));
+ QTest::addRow("display(1,0)") << 1 << 0 << displayRole << QVariant(QLatin1String("Oliver"));
+ QTest::addRow("display(1,1)") << 1 << 1 << displayRole << QVariant(QLatin1String("33"));
+}
+
+void tst_QQmlTableModel::builtInRoles()
+{
+ QFETCH(int, row);
+ QFETCH(int, column);
+ QFETCH(QByteArray, roleName);
+ QFETCH(QVariant, expectedValue);
+
+ QQmlEngine engine;
+ QQmlComponent component(&engine, testFileUrl("builtInRoles.qml"));
+ QCOMPARE(component.status(), QQmlComponent::Ready);
+
+ QScopedPointer<QQmlTableModel> model(qobject_cast<QQmlTableModel*>(component.create()));
QVERIFY(model);
QCOMPARE(model->rowCount(), 2);
QCOMPARE(model->columnCount(), 2);
- QSignalSpy columnCountSpy(model, SIGNAL(columnCountChanged()));
- QVERIFY(columnCountSpy.isValid());
+ const QHash<int, QByteArray> roleNames = model->roleNames();
+ QCOMPARE(roleNames.size(), 4 + builtInRoleCount);
+ QVERIFY(roleNames.values().contains("display"));
+ QVERIFY(roleNames.values().contains("decoration"));
+ QVERIFY(roleNames.values().contains("edit"));
+ QVERIFY(roleNames.values().contains("toolTip"));
+ QVERIFY(roleNames.values().contains("statusTip"));
+ QVERIFY(roleNames.values().contains("whatsThis"));
+ QVERIFY(roleNames.values().contains("name"));
+ QVERIFY(roleNames.values().contains("age"));
+ QVERIFY(roleNames.values().contains("someOtherRole1"));
+ QVERIFY(roleNames.values().contains("someOtherRole2"));
+ QCOMPARE(model->data(model->index(row, column, QModelIndex()), roleNames.key(roleName)), expectedValue);
+}
- QSignalSpy rowCountSpy(model, SIGNAL(rowCountChanged()));
- QVERIFY(rowCountSpy.isValid());
+void tst_QQmlTableModel::explicitDisplayRole()
+{
+ QQmlEngine engine;
+ QQmlComponent component(&engine, testFileUrl("explicitDisplayRole.qml"));
+ QCOMPARE(component.status(), QQmlComponent::Ready);
+ QScopedPointer<QQmlTableModel> model(qobject_cast<QQmlTableModel*>(component.create()));
+ QVERIFY(model);
+ QCOMPARE(model->rowCount(), 1);
+ QCOMPARE(model->columnCount(), 2);
const QHash<int, QByteArray> roleNames = model->roleNames();
- QCOMPARE(model->data(model->index(0, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("John"));
- QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("display")).toInt(), 22);
- QCOMPARE(model->data(model->index(1, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("Oliver"));
- QCOMPARE(model->data(model->index(1, 1, QModelIndex()), roleNames.key("display")).toInt(), 33);
+ QCOMPARE(model->data(model->index(0, 0, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("foo"));
+ QCOMPARE(model->data(model->index(0, 1, QModelIndex()), roleNames.key("display")).toString(), QLatin1String("bar"));
}
void tst_QQmlTableModel::roleDataProvider()