From 6c8378eaf1edbbefe6aaa3672b0127816a004fd7 Mon Sep 17 00:00:00 2001 From: Kent Hansen Date: Wed, 23 Nov 2011 15:14:07 +0100 Subject: Say hello to QtQuick module This change moves the QtQuick 2 types and C++ API (including SceneGraph) to a new module (AKA library), QtQuick. 99% of this change is moving files from src/declarative to src/quick, and from tests/auto/declarative to tests/auto/qtquick2. The loading of QtQuick 2 ("import QtQuick 2.0") is now delegated to a plugin, src/imports/qtquick2, just like it's done for QtQuick 1. All tools, examples, and tests that use QtQuick C++ API have gotten "QT += quick" or "QT += quick-private" added to their .pro file. A few additional internal QtDeclarative classes had to be exported (via Q_DECLARATIVE_PRIVATE_EXPORT) since they're needed by the QtQuick 2 implementation. The old header locations (e.g. QtDeclarative/qquickitem.h) will still be supported for some time, but will produce compile-time warnings. (To avoid the QtQuick implementation using the compatibility headers (since QtDeclarative's includepath comes first), a few include statements were modified, e.g. from "#include " to "#include ".) There's a change in qtbase that automatically adds QtQuick to the module list if QtDeclarative is used. Together with the compatibility headers, this should help reduce the migration pain for existing projects. In theory, simply getting an existing QtDeclarative-based project to compile and link shouldn't require any changes for now -- but porting to the new scheme is of course recommended, and will eventually become mandatory. Task-number: QTBUG-22889 Reviewed-by: Lars Knoll Change-Id: Ia52be9373172ba2f37e7623231ecb060316c96a7 Reviewed-by: Kent Hansen Reviewed-by: Sergio Ahumada --- .../qquickvisualdatamodel/data/create.qml | 25 - .../data/datalist-package.qml | 20 - .../qquickvisualdatamodel/data/datalist.qml | 18 - .../qquickvisualdatamodel/data/groups-invalid.qml | 14 - .../qquickvisualdatamodel/data/groups-package.qml | 52 - .../qquickvisualdatamodel/data/groups.qml | 46 - .../data/itemsDestroyed_listView.qml | 13 - .../data/itemsDestroyed_package.qml | 42 - .../data/itemsDestroyed_pathView.qml | 18 - .../data/itemsDestroyed_repeater.qml | 15 - .../qquickvisualdatamodel/data/modelproperties.qml | 21 - .../data/modelproperties2.qml | 21 - .../qquickvisualdatamodel/data/objectlist.qml | 19 - .../qquickvisualdatamodel/data/onChanged.qml | 87 - .../qquickvisualdatamodel/data/singlerole1.qml | 10 - .../qquickvisualdatamodel/data/singlerole2.qml | 10 - .../qquickvisualdatamodel/data/visualdatamodel.qml | 12 - .../qquickvisualdatamodel.pro | 13 - .../tst_qquickvisualdatamodel.cpp | 1865 -------------------- 19 files changed, 2321 deletions(-) delete mode 100644 tests/auto/declarative/qquickvisualdatamodel/data/create.qml delete mode 100644 tests/auto/declarative/qquickvisualdatamodel/data/datalist-package.qml delete mode 100644 tests/auto/declarative/qquickvisualdatamodel/data/datalist.qml delete mode 100644 tests/auto/declarative/qquickvisualdatamodel/data/groups-invalid.qml delete mode 100644 tests/auto/declarative/qquickvisualdatamodel/data/groups-package.qml delete mode 100644 tests/auto/declarative/qquickvisualdatamodel/data/groups.qml delete mode 100644 tests/auto/declarative/qquickvisualdatamodel/data/itemsDestroyed_listView.qml delete mode 100644 tests/auto/declarative/qquickvisualdatamodel/data/itemsDestroyed_package.qml delete mode 100644 tests/auto/declarative/qquickvisualdatamodel/data/itemsDestroyed_pathView.qml delete mode 100644 tests/auto/declarative/qquickvisualdatamodel/data/itemsDestroyed_repeater.qml delete mode 100644 tests/auto/declarative/qquickvisualdatamodel/data/modelproperties.qml delete mode 100644 tests/auto/declarative/qquickvisualdatamodel/data/modelproperties2.qml delete mode 100644 tests/auto/declarative/qquickvisualdatamodel/data/objectlist.qml delete mode 100644 tests/auto/declarative/qquickvisualdatamodel/data/onChanged.qml delete mode 100644 tests/auto/declarative/qquickvisualdatamodel/data/singlerole1.qml delete mode 100644 tests/auto/declarative/qquickvisualdatamodel/data/singlerole2.qml delete mode 100644 tests/auto/declarative/qquickvisualdatamodel/data/visualdatamodel.qml delete mode 100644 tests/auto/declarative/qquickvisualdatamodel/qquickvisualdatamodel.pro delete mode 100644 tests/auto/declarative/qquickvisualdatamodel/tst_qquickvisualdatamodel.cpp (limited to 'tests/auto/declarative/qquickvisualdatamodel') diff --git a/tests/auto/declarative/qquickvisualdatamodel/data/create.qml b/tests/auto/declarative/qquickvisualdatamodel/data/create.qml deleted file mode 100644 index 3475a0dace..0000000000 --- a/tests/auto/declarative/qquickvisualdatamodel/data/create.qml +++ /dev/null @@ -1,25 +0,0 @@ -import QtQuick 2.0 - -ListView { - width: 200 - height: 200 - - model: VisualDataModel { - id: visualModel - - persistedItems.includeByDefault: true - - model: myModel - delegate: Item { - id: delegate - objectName: "delegate" - width: 200 - height: 20 - - property bool destroyed: false - - - Component.onDestruction: destroyed = true - } - } -} diff --git a/tests/auto/declarative/qquickvisualdatamodel/data/datalist-package.qml b/tests/auto/declarative/qquickvisualdatamodel/data/datalist-package.qml deleted file mode 100644 index ae3bd81d91..0000000000 --- a/tests/auto/declarative/qquickvisualdatamodel/data/datalist-package.qml +++ /dev/null @@ -1,20 +0,0 @@ -import QtQuick 2.0 - -ListView { - width: 100 - height: 100 - model: visualModel.parts.package - VisualDataModel { - id: visualModel - objectName: "visualModel" - model: myModel - delegate: Package { - Rectangle { - height: 25 - width: 100 - Package.name: "package" - Text { objectName: "display"; text: display } - } - } - } -} diff --git a/tests/auto/declarative/qquickvisualdatamodel/data/datalist.qml b/tests/auto/declarative/qquickvisualdatamodel/data/datalist.qml deleted file mode 100644 index 8ce59caddc..0000000000 --- a/tests/auto/declarative/qquickvisualdatamodel/data/datalist.qml +++ /dev/null @@ -1,18 +0,0 @@ -import QtQuick 2.0 - -ListView { - width: 100 - height: 100 - model: VisualDataModel { - id: visualModel - objectName: "visualModel" - model: myModel - delegate: Component { - Rectangle { - height: 25 - width: 100 - Text { objectName: "display"; text: display } - } - } - } -} diff --git a/tests/auto/declarative/qquickvisualdatamodel/data/groups-invalid.qml b/tests/auto/declarative/qquickvisualdatamodel/data/groups-invalid.qml deleted file mode 100644 index 70c6f9f995..0000000000 --- a/tests/auto/declarative/qquickvisualdatamodel/data/groups-invalid.qml +++ /dev/null @@ -1,14 +0,0 @@ -import QtQuick 2.0 - -VisualDataModel { - id: visualModel - - objectName: "visualModel" - - groups: [ - VisualDataGroup { id: visibleItems; objectName: "visibleItems"; name: "visible"; includeByDefault: true }, - VisualDataGroup { id: selectedItems; objectName: "selectedItems"; name: "selected" }, - VisualDataGroup { id: unnamed; objectName: "unnamed" }, - VisualDataGroup { id: capitalised; objectName: "capitalised"; name: "Capitalised" } - ] -} diff --git a/tests/auto/declarative/qquickvisualdatamodel/data/groups-package.qml b/tests/auto/declarative/qquickvisualdatamodel/data/groups-package.qml deleted file mode 100644 index ea5ad5d3bd..0000000000 --- a/tests/auto/declarative/qquickvisualdatamodel/data/groups-package.qml +++ /dev/null @@ -1,52 +0,0 @@ -import QtQuick 2.0 - -ListView { - width: 100 - height: 100 - - function contains(array, value) { - for (var i = 0; i < array.length; ++i) - if (array[i] == value) - return true - return false - } - model: visualModel.parts.package - - VisualDataModel { - id: visualModel - - objectName: "visualModel" - - groups: [ - VisualDataGroup { id: visibleItems; objectName: "visibleItems"; name: "visible"; includeByDefault: true }, - VisualDataGroup { id: selectedItems; objectName: "selectedItems"; name: "selected" } - ] - - model: myModel - delegate: Package { - id: delegate - - property variant test1: name - property variant test2: index - property variant test3: VisualDataModel.itemsIndex - property variant test4: VisualDataModel.inItems - property variant test5: VisualDataModel.visibleIndex - property variant test6: VisualDataModel.inVisible - property variant test7: VisualDataModel.selectedIndex - property variant test8: VisualDataModel.inSelected - property variant test9: VisualDataModel.groups - - function hide() { VisualDataModel.inVisible = false } - function select() { VisualDataModel.inSelected = true } - - Item { - Package.name: "package" - - objectName: "delegate" - width: 100 - height: 2 - } - } - } - -} diff --git a/tests/auto/declarative/qquickvisualdatamodel/data/groups.qml b/tests/auto/declarative/qquickvisualdatamodel/data/groups.qml deleted file mode 100644 index 7502dd2502..0000000000 --- a/tests/auto/declarative/qquickvisualdatamodel/data/groups.qml +++ /dev/null @@ -1,46 +0,0 @@ -import QtQuick 2.0 - -ListView { - width: 100 - height: 100 - - function contains(array, value) { - for (var i = 0; i < array.length; ++i) - if (array[i] == value) - return true - return false - } - - model: visualModel - VisualDataModel { - id: visualModel - - objectName: "visualModel" - - groups: [ - VisualDataGroup { id: visibleItems; objectName: "visibleItems"; name: "visible"; includeByDefault: true }, - VisualDataGroup { id: selectedItems; objectName: "selectedItems"; name: "selected" } - ] - - model: myModel - delegate: Item { - id: delegate - - objectName: "delegate" - width: 100 - height: 2 - property variant test1: name - property variant test2: index - property variant test3: VisualDataModel.itemsIndex - property variant test4: VisualDataModel.inItems - property variant test5: VisualDataModel.visibleIndex - property variant test6: VisualDataModel.inVisible - property variant test7: VisualDataModel.selectedIndex - property variant test8: VisualDataModel.inSelected - property variant test9: VisualDataModel.groups - - function hide() { VisualDataModel.inVisible = false } - function select() { VisualDataModel.inSelected = true } - } - } -} diff --git a/tests/auto/declarative/qquickvisualdatamodel/data/itemsDestroyed_listView.qml b/tests/auto/declarative/qquickvisualdatamodel/data/itemsDestroyed_listView.qml deleted file mode 100644 index 103c4d2eb6..0000000000 --- a/tests/auto/declarative/qquickvisualdatamodel/data/itemsDestroyed_listView.qml +++ /dev/null @@ -1,13 +0,0 @@ -import QtQuick 2.0 - -ListView { - width: 100 - height: 100 - - model: myModel - delegate: Item { - objectName: "delegate" - width: 100 - height: 20 - } -} diff --git a/tests/auto/declarative/qquickvisualdatamodel/data/itemsDestroyed_package.qml b/tests/auto/declarative/qquickvisualdatamodel/data/itemsDestroyed_package.qml deleted file mode 100644 index b47f22dc34..0000000000 --- a/tests/auto/declarative/qquickvisualdatamodel/data/itemsDestroyed_package.qml +++ /dev/null @@ -1,42 +0,0 @@ -import QtQuick 2.0 - -Item { - width: 100 - height: 100 - - ListView { - anchors.fill: parent - - model: visualModel.parts.list - } - VisualDataModel { - id: visualModel - - model: myModel - delegate: Package { - Item { - Package.name: "list" - width: 100 - height: 20 - } - - Item { - id: gridItem - Package.name: "grid" - width: 50 - height: 50 - } - Rectangle { - objectName: "delegate" - parent: gridItem - width: 20 - height: 20 - } - } - } - GridView { - anchors.fill: parent - - model: visualModel.parts.grid - } -} diff --git a/tests/auto/declarative/qquickvisualdatamodel/data/itemsDestroyed_pathView.qml b/tests/auto/declarative/qquickvisualdatamodel/data/itemsDestroyed_pathView.qml deleted file mode 100644 index bc619124fd..0000000000 --- a/tests/auto/declarative/qquickvisualdatamodel/data/itemsDestroyed_pathView.qml +++ /dev/null @@ -1,18 +0,0 @@ -import QtQuick 2.0 - -PathView { - width: 100 - height: 100 - - model: myModel - delegate: Item { - objectName: "delegate" - width: 100 - height: 20 - } - - path: Path { - startX: 50; startY: 0 - PathLine { x: 50; y: 100 } - } -} diff --git a/tests/auto/declarative/qquickvisualdatamodel/data/itemsDestroyed_repeater.qml b/tests/auto/declarative/qquickvisualdatamodel/data/itemsDestroyed_repeater.qml deleted file mode 100644 index e97e0dad2e..0000000000 --- a/tests/auto/declarative/qquickvisualdatamodel/data/itemsDestroyed_repeater.qml +++ /dev/null @@ -1,15 +0,0 @@ -import QtQuick 2.0 - -Grid { - Repeater { - width: 100 - height: 100 - - model: myModel - delegate: Item { - objectName: "delegate" - width: 50 - height: 50 - } - } -} diff --git a/tests/auto/declarative/qquickvisualdatamodel/data/modelproperties.qml b/tests/auto/declarative/qquickvisualdatamodel/data/modelproperties.qml deleted file mode 100644 index 73b766f1af..0000000000 --- a/tests/auto/declarative/qquickvisualdatamodel/data/modelproperties.qml +++ /dev/null @@ -1,21 +0,0 @@ -import QtQuick 2.0 - -ListView { - width: 100 - height: 100 - model: myModel - delegate: Item { - objectName: "delegate" - width: 100 - height: 2 - property variant test1: name - property variant test2: model.name - property variant test3: modelData - property variant test4: model.modelData - property variant test5: modelData.name - property variant test6: model - property variant test7: index - property variant test8: model.index - property variant test9: model.modelData.name - } -} diff --git a/tests/auto/declarative/qquickvisualdatamodel/data/modelproperties2.qml b/tests/auto/declarative/qquickvisualdatamodel/data/modelproperties2.qml deleted file mode 100644 index ea5c240b29..0000000000 --- a/tests/auto/declarative/qquickvisualdatamodel/data/modelproperties2.qml +++ /dev/null @@ -1,21 +0,0 @@ -import QtQuick 2.0 - -ListView { - width: 100 - height: 100 - model: myModel - delegate: Item { - objectName: "delegate" - property variant test1: display - property variant test2: model.display - property variant test3: modelData - property variant test4: model.modelData - property variant test5: modelData.display - property variant test6: model - property variant test7: index - property variant test8: model.index - property variant test9: model.modelData.display - width: 100 - height: 2 - } -} diff --git a/tests/auto/declarative/qquickvisualdatamodel/data/objectlist.qml b/tests/auto/declarative/qquickvisualdatamodel/data/objectlist.qml deleted file mode 100644 index b3952a8a4d..0000000000 --- a/tests/auto/declarative/qquickvisualdatamodel/data/objectlist.qml +++ /dev/null @@ -1,19 +0,0 @@ -import QtQuick 2.0 - -ListView { - width: 100 - height: 100 - anchors.fill: parent - model: myModel - delegate: Component { - Rectangle { - height: 25 - width: 100 - color: model.modelData.color - Text { objectName: "name"; text: name; function getText() { return name } } - Text { objectName: "section"; text: parent.ListView.section } - } - } - section.property: "name" - section.criteria: ViewSection.FullString -} diff --git a/tests/auto/declarative/qquickvisualdatamodel/data/onChanged.qml b/tests/auto/declarative/qquickvisualdatamodel/data/onChanged.qml deleted file mode 100644 index 71dc7d72d7..0000000000 --- a/tests/auto/declarative/qquickvisualdatamodel/data/onChanged.qml +++ /dev/null @@ -1,87 +0,0 @@ -import QtQuick 2.0 - -VisualDataModel { - id: vm - - property var inserted - property var removed - - Component.onCompleted: { - vm.inserted = [] - vm.removed = [] - vi.inserted = [] - vi.removed = [] - si.inserted = [] - si.removed = [] - } - - function verify(changes, indexes, counts, moveIds) { - if (changes.length != indexes.length - || changes.length != counts.length - || changes.length != moveIds.length) { - console.log("invalid length", changes.length, indexes.length, counts.length, moveIds.length) - return false - } - - var valid = true; - for (var i = 0; i < changes.length; ++i) { - if (changes[i].index != indexes[i]) { - console.log(i, "incorrect index. actual:", changes[i].index, "expected:", indexes[i]) - valid = false; - } - if (changes[i].count != counts[i]) { - console.log(i, "incorrect count. actual:", changes[i].count, "expected:", counts[i]) - valid = false; - } - if (changes[i].moveId != moveIds[i]) { - console.log(i, "incorrect moveId. actual:", changes[i].moveId, "expected:", moveIds[i]) - valid = false; - } - } - return valid - } - - groups: [ - VisualDataGroup { - id: vi; - - property var inserted - property var removed - - name: "visible" - includeByDefault: true - - onChanged: { - vi.inserted = inserted - vi.removed = removed - } - }, - VisualDataGroup { - id: si; - - property var inserted - property var removed - - name: "selected" - onChanged: { - si.inserted = inserted - si.removed = removed - } - } - ] - - model: ListModel { - id: listModel - ListElement { number: "one" } - ListElement { number: "two" } - ListElement { number: "three" } - ListElement { number: "four" } - } - - delegate: Item {} - - items.onChanged: { - vm.inserted = inserted - vm.removed = removed - } -} diff --git a/tests/auto/declarative/qquickvisualdatamodel/data/singlerole1.qml b/tests/auto/declarative/qquickvisualdatamodel/data/singlerole1.qml deleted file mode 100644 index c471893e1d..0000000000 --- a/tests/auto/declarative/qquickvisualdatamodel/data/singlerole1.qml +++ /dev/null @@ -1,10 +0,0 @@ -import QtQuick 2.0 - -ListView { - width: 100 - height: 100 - model: myModel - delegate: Component { - Text { objectName: "name"; text: name; function getText() { return name; } } - } -} diff --git a/tests/auto/declarative/qquickvisualdatamodel/data/singlerole2.qml b/tests/auto/declarative/qquickvisualdatamodel/data/singlerole2.qml deleted file mode 100644 index ab1798999d..0000000000 --- a/tests/auto/declarative/qquickvisualdatamodel/data/singlerole2.qml +++ /dev/null @@ -1,10 +0,0 @@ -import QtQuick 2.0 - -ListView { - width: 100 - height: 100 - model: myModel - delegate: Component { - Text { objectName: "name"; text: modelData; function getText() { return modelData } } - } -} diff --git a/tests/auto/declarative/qquickvisualdatamodel/data/visualdatamodel.qml b/tests/auto/declarative/qquickvisualdatamodel/data/visualdatamodel.qml deleted file mode 100644 index 0d4d9e2e46..0000000000 --- a/tests/auto/declarative/qquickvisualdatamodel/data/visualdatamodel.qml +++ /dev/null @@ -1,12 +0,0 @@ -import QtQuick 2.0 - -VisualDataModel { - function setRoot() { - rootIndex = modelIndex(0); - } - function setRootToParent() { - rootIndex = parentModelIndex(); - } - model: myModel - delegate: Item {} -} diff --git a/tests/auto/declarative/qquickvisualdatamodel/qquickvisualdatamodel.pro b/tests/auto/declarative/qquickvisualdatamodel/qquickvisualdatamodel.pro deleted file mode 100644 index 35e627c1ad..0000000000 --- a/tests/auto/declarative/qquickvisualdatamodel/qquickvisualdatamodel.pro +++ /dev/null @@ -1,13 +0,0 @@ -CONFIG += testcase -TARGET = tst_qquickvisualdatamodel -macx:CONFIG -= app_bundle - -SOURCES += tst_qquickvisualdatamodel.cpp - -testDataFiles.files = data -testDataFiles.path = . -DEPLOYMENT += testDataFiles - -CONFIG += parallel_test - -QT += core-private gui-private v8-private declarative-private widgets testlib diff --git a/tests/auto/declarative/qquickvisualdatamodel/tst_qquickvisualdatamodel.cpp b/tests/auto/declarative/qquickvisualdatamodel/tst_qquickvisualdatamodel.cpp deleted file mode 100644 index 0278b0cf2c..0000000000 --- a/tests/auto/declarative/qquickvisualdatamodel/tst_qquickvisualdatamodel.cpp +++ /dev/null @@ -1,1865 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). -** All rights reserved. -** Contact: Nokia Corporation (qt-info@nokia.com) -** -** This file is part of the test suite of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** GNU Lesser General Public License Usage -** This file may be used under the terms of the GNU Lesser General Public -** License version 2.1 as published by the Free Software Foundation and -** appearing in the file LICENSE.LGPL included in the packaging of this -** file. Please review the following information to ensure the GNU Lesser -** General Public License version 2.1 requirements will be met: -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain additional -** rights. These rights are described in the Nokia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU General -** Public License version 3.0 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 3.0 requirements will be met: -** http://www.gnu.org/copyleft/gpl.html. -** -** Other Usage -** Alternatively, this file may be used in accordance with the terms and -** conditions contained in a signed written agreement between you and Nokia. -** -** -** -** -** -** $QT_END_LICENSE$ -** -****************************************************************************/ -#include "../shared/util.h" -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -template int lengthOf(const T (&)[N]) { return N; } - -static void initStandardTreeModel(QStandardItemModel *model) -{ - QStandardItem *item; - item = new QStandardItem(QLatin1String("Row 1 Item")); - model->insertRow(0, item); - - item = new QStandardItem(QLatin1String("Row 2 Item")); - item->setCheckable(true); - model->insertRow(1, item); - - QStandardItem *childItem = new QStandardItem(QLatin1String("Row 2 Child Item")); - item->setChild(0, childItem); - - item = new QStandardItem(QLatin1String("Row 3 Item")); - item->setIcon(QIcon()); - model->insertRow(2, item); -} - -class SingleRoleModel : public QAbstractListModel -{ - Q_OBJECT - -public: - SingleRoleModel(const QByteArray &role = "name", QObject * /* parent */ = 0) { - QHash roles; - roles.insert(Qt::DisplayRole , role); - setRoleNames(roles); - list << "one" << "two" << "three" << "four"; - } - - void emitMove(int sourceFirst, int sourceLast, int destinationChild) { - emit beginMoveRows(QModelIndex(), sourceFirst, sourceLast, QModelIndex(), destinationChild); - emit endMoveRows(); - } - - QStringList list; - -public slots: - void set(int idx, QString string) { - list[idx] = string; - emit dataChanged(index(idx,0), index(idx,0)); - } - -protected: - int rowCount(const QModelIndex & /* parent */ = QModelIndex()) const { - return list.count(); - } - QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const { - if (role == Qt::DisplayRole) - return list.at(index.row()); - return QVariant(); - } -}; - - -class tst_qquickvisualdatamodel : public QObject -{ - Q_OBJECT -public: - tst_qquickvisualdatamodel(); - -private slots: - void initTestCase(); - void cleanupTestCase(); - void rootIndex(); - void updateLayout_data(); - void updateLayout(); - void childChanged_data(); - void childChanged(); - void objectListModel(); - void singleRole(); - void modelProperties(); - void noDelegate_data(); - void noDelegate(); - void itemsDestroyed_data(); - void itemsDestroyed(); - void qaimRowsMoved(); - void qaimRowsMoved_data(); - void remove_data(); - void remove(); - void move_data(); - void move(); - void groups_data(); - void groups(); - void invalidGroups(); - void get(); - void onChanged_data(); - void onChanged(); - void create(); - void incompleteModel(); - -private: - template void groups_verify( - const SingleRoleModel &model, - QQuickItem *contentItem, - const int (&mIndex)[N], - const int (&iIndex)[N], - const int (&vIndex)[N], - const int (&sIndex)[N], - const bool (&vMember)[N], - const bool (&sMember)[N]); - - template void get_verify( - const SingleRoleModel &model, - QQuickVisualDataModel *visualModel, - QQuickVisualDataGroup *visibleItems, - QQuickVisualDataGroup *selectedItems, - const int (&mIndex)[N], - const int (&iIndex)[N], - const int (&vIndex)[N], - const int (&sIndex)[N], - const bool (&vMember)[N], - const bool (&sMember)[N]); - - bool failed; - QDeclarativeEngine engine; - template - T *findItem(QQuickItem *parent, const QString &objectName, int index); -}; - -Q_DECLARE_METATYPE(QDeclarativeChangeSet) - -void tst_qquickvisualdatamodel::initTestCase() -{ - qRegisterMetaType(); -} - -void tst_qquickvisualdatamodel::cleanupTestCase() -{ - -} -class DataObject : public QObject -{ - Q_OBJECT - - Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged) - Q_PROPERTY(QString color READ color WRITE setColor NOTIFY colorChanged) - -public: - DataObject(QObject *parent=0) : QObject(parent) {} - DataObject(const QString &name, const QString &color, QObject *parent=0) - : QObject(parent), m_name(name), m_color(color) { } - - - QString name() const { return m_name; } - void setName(const QString &name) { - if (name != m_name) { - m_name = name; - emit nameChanged(); - } - } - - QString color() const { return m_color; } - void setColor(const QString &color) { - if (color != m_color) { - m_color = color; - emit colorChanged(); - } - } - -signals: - void nameChanged(); - void colorChanged(); - -private: - QString m_name; - QString m_color; -}; - -template static T evaluate(QObject *scope, const QString &expression) -{ - QDeclarativeExpression expr(qmlContext(scope), scope, expression); - T result = expr.evaluate().value(); - if (expr.hasError()) - qWarning() << expr.error().toString(); - return result; -} - -template <> void evaluate(QObject *scope, const QString &expression) -{ - QDeclarativeExpression expr(qmlContext(scope), scope, expression); - expr.evaluate(); - if (expr.hasError()) - qWarning() << expr.error().toString(); -} - -tst_qquickvisualdatamodel::tst_qquickvisualdatamodel() -{ -} - -void tst_qquickvisualdatamodel::rootIndex() -{ - QDeclarativeEngine engine; - QDeclarativeComponent c(&engine, QUrl::fromLocalFile(TESTDATA("visualdatamodel.qml"))); - - QStandardItemModel model; - initStandardTreeModel(&model); - - engine.rootContext()->setContextProperty("myModel", &model); - - QQuickVisualDataModel *obj = qobject_cast(c.create()); - QVERIFY(obj != 0); - - QMetaObject::invokeMethod(obj, "setRoot"); - QVERIFY(qvariant_cast(obj->rootIndex()) == model.index(0,0)); - - QMetaObject::invokeMethod(obj, "setRootToParent"); - QVERIFY(qvariant_cast(obj->rootIndex()) == QModelIndex()); - - QMetaObject::invokeMethod(obj, "setRoot"); - QVERIFY(qvariant_cast(obj->rootIndex()) == model.index(0,0)); - model.clear(); // will emit modelReset() - QVERIFY(qvariant_cast(obj->rootIndex()) == QModelIndex()); - - delete obj; -} - -void tst_qquickvisualdatamodel::updateLayout_data() -{ - QTest::addColumn("source"); - - QTest::newRow("item delegate") << QUrl::fromLocalFile(TESTDATA("datalist.qml")); - QTest::newRow("package delegate") << QUrl::fromLocalFile(TESTDATA("datalist-package.qml")); -} - -void tst_qquickvisualdatamodel::updateLayout() -{ - QFETCH(QUrl, source); - - QQuickView view; - - QStandardItemModel model; - initStandardTreeModel(&model); - - view.rootContext()->setContextProperty("myModel", &model); - - view.setSource(source); - - QQuickListView *listview = qobject_cast(view.rootObject()); - QVERIFY(listview != 0); - - QQuickItem *contentItem = listview->contentItem(); - QVERIFY(contentItem != 0); - - QQuickText *name = findItem(contentItem, "display", 0); - QVERIFY(name); - QCOMPARE(name->text(), QString("Row 1 Item")); - name = findItem(contentItem, "display", 1); - QVERIFY(name); - QCOMPARE(name->text(), QString("Row 2 Item")); - name = findItem(contentItem, "display", 2); - QVERIFY(name); - QCOMPARE(name->text(), QString("Row 3 Item")); - - model.invisibleRootItem()->sortChildren(0, Qt::DescendingOrder); - - name = findItem(contentItem, "display", 0); - QVERIFY(name); - QCOMPARE(name->text(), QString("Row 3 Item")); - name = findItem(contentItem, "display", 1); - QVERIFY(name); - QCOMPARE(name->text(), QString("Row 2 Item")); - name = findItem(contentItem, "display", 2); - QVERIFY(name); - QCOMPARE(name->text(), QString("Row 1 Item")); -} - -void tst_qquickvisualdatamodel::childChanged_data() -{ - QTest::addColumn("source"); - - QTest::newRow("item delegate") << QUrl::fromLocalFile(TESTDATA("datalist.qml")); - QTest::newRow("package delegate") << QUrl::fromLocalFile(TESTDATA("datalist-package.qml")); -} - -void tst_qquickvisualdatamodel::childChanged() -{ - QFETCH(QUrl, source); - - QQuickView view; - - QStandardItemModel model; - initStandardTreeModel(&model); - - view.rootContext()->setContextProperty("myModel", &model); - - view.setSource(source); - - QQuickListView *listview = qobject_cast(view.rootObject()); - QVERIFY(listview != 0); - - QQuickItem *contentItem = listview->contentItem(); - QVERIFY(contentItem != 0); - - QQuickVisualDataModel *vdm = listview->findChild("visualModel"); - vdm->setRootIndex(QVariant::fromValue(model.indexFromItem(model.item(1,0)))); - QCOMPARE(listview->count(), 1); - - QQuickText *name = findItem(contentItem, "display", 0); - QVERIFY(name); - QCOMPARE(name->text(), QString("Row 2 Child Item")); - - model.item(1,0)->child(0,0)->setText("Row 2 updated child"); - - name = findItem(contentItem, "display", 0); - QVERIFY(name); - QCOMPARE(name->text(), QString("Row 2 updated child")); - - model.item(1,0)->appendRow(new QStandardItem(QLatin1String("Row 2 Child Item 2"))); - QCOMPARE(listview->count(), 2); - - name = findItem(contentItem, "display", 1); - QVERIFY(name != 0); - QCOMPARE(name->text(), QString("Row 2 Child Item 2")); - - model.item(1,0)->takeRow(1); - name = findItem(contentItem, "display", 1); - QVERIFY(name == 0); - - vdm->setRootIndex(QVariant::fromValue(QModelIndex())); - QCOMPARE(listview->count(), 3); - name = findItem(contentItem, "display", 0); - QVERIFY(name); - QCOMPARE(name->text(), QString("Row 1 Item")); - name = findItem(contentItem, "display", 1); - QVERIFY(name); - QCOMPARE(name->text(), QString("Row 2 Item")); - name = findItem(contentItem, "display", 2); - QVERIFY(name); - QCOMPARE(name->text(), QString("Row 3 Item")); -} - -void tst_qquickvisualdatamodel::objectListModel() -{ - QQuickView view; - - QList dataList; - dataList.append(new DataObject("Item 1", "red")); - dataList.append(new DataObject("Item 2", "green")); - dataList.append(new DataObject("Item 3", "blue")); - dataList.append(new DataObject("Item 4", "yellow")); - - QDeclarativeContext *ctxt = view.rootContext(); - ctxt->setContextProperty("myModel", QVariant::fromValue(dataList)); - - view.setSource(QUrl::fromLocalFile(TESTDATA("objectlist.qml"))); - - QQuickListView *listview = qobject_cast(view.rootObject()); - QVERIFY(listview != 0); - - QQuickItem *contentItem = listview->contentItem(); - QVERIFY(contentItem != 0); - - QQuickText *name = findItem(contentItem, "name", 0); - QCOMPARE(name->text(), QString("Item 1")); - - QQuickText *section = findItem(contentItem, "section", 0); - QCOMPARE(section->text(), QString("Item 1")); - - dataList[0]->setProperty("name", QLatin1String("Changed")); - QCOMPARE(name->text(), QString("Changed")); -} - -void tst_qquickvisualdatamodel::singleRole() -{ - { - QQuickView view; - - SingleRoleModel model; - - QDeclarativeContext *ctxt = view.rootContext(); - ctxt->setContextProperty("myModel", &model); - - view.setSource(QUrl::fromLocalFile(TESTDATA("singlerole1.qml"))); - - QQuickListView *listview = qobject_cast(view.rootObject()); - QVERIFY(listview != 0); - - QQuickItem *contentItem = listview->contentItem(); - QVERIFY(contentItem != 0); - - QQuickText *name = findItem(contentItem, "name", 1); - QCOMPARE(name->text(), QString("two")); - - model.set(1, "Changed"); - QCOMPARE(name->text(), QString("Changed")); - } - { - QQuickView view; - - SingleRoleModel model; - - QDeclarativeContext *ctxt = view.rootContext(); - ctxt->setContextProperty("myModel", &model); - - view.setSource(QUrl::fromLocalFile(TESTDATA("singlerole2.qml"))); - - QQuickListView *listview = qobject_cast(view.rootObject()); - QVERIFY(listview != 0); - - QQuickItem *contentItem = listview->contentItem(); - QVERIFY(contentItem != 0); - - QQuickText *name = findItem(contentItem, "name", 1); - QCOMPARE(name->text(), QString("two")); - - model.set(1, "Changed"); - QCOMPARE(name->text(), QString("Changed")); - } - { - QQuickView view; - - SingleRoleModel model("modelData"); - - QDeclarativeContext *ctxt = view.rootContext(); - ctxt->setContextProperty("myModel", &model); - - view.setSource(QUrl::fromLocalFile(TESTDATA("singlerole2.qml"))); - - QQuickListView *listview = qobject_cast(view.rootObject()); - QVERIFY(listview != 0); - - QQuickItem *contentItem = listview->contentItem(); - QVERIFY(contentItem != 0); - - QQuickText *name = findItem(contentItem, "name", 1); - QCOMPARE(name->text(), QString("two")); - - model.set(1, "Changed"); - QCOMPARE(name->text(), QString("Changed")); - } -} - -void tst_qquickvisualdatamodel::modelProperties() -{ - { - QQuickView view; - - SingleRoleModel model; - - QDeclarativeContext *ctxt = view.rootContext(); - ctxt->setContextProperty("myModel", &model); - - view.setSource(QUrl::fromLocalFile(TESTDATA("modelproperties.qml"))); - - QQuickListView *listview = qobject_cast(view.rootObject()); - QVERIFY(listview != 0); - - QQuickItem *contentItem = listview->contentItem(); - QVERIFY(contentItem != 0); - - QQuickItem *delegate = findItem(contentItem, "delegate", 1); - QVERIFY(delegate); - QCOMPARE(delegate->property("test1").toString(),QString("two")); - QCOMPARE(delegate->property("test2").toString(),QString("two")); - QCOMPARE(delegate->property("test3").toString(),QString("two")); - QCOMPARE(delegate->property("test4").toString(),QString("two")); - QVERIFY(!delegate->property("test9").isValid()); - QCOMPARE(delegate->property("test5").toString(),QString("")); - QVERIFY(delegate->property("test6").value() != 0); - QCOMPARE(delegate->property("test7").toInt(),1); - QCOMPARE(delegate->property("test8").toInt(),1); - } - - { - QQuickView view; - - QList dataList; - dataList.append(new DataObject("Item 1", "red")); - dataList.append(new DataObject("Item 2", "green")); - dataList.append(new DataObject("Item 3", "blue")); - dataList.append(new DataObject("Item 4", "yellow")); - - QDeclarativeContext *ctxt = view.rootContext(); - ctxt->setContextProperty("myModel", QVariant::fromValue(dataList)); - - view.setSource(QUrl::fromLocalFile(TESTDATA("modelproperties.qml"))); - - QQuickListView *listview = qobject_cast(view.rootObject()); - QVERIFY(listview != 0); - - QQuickItem *contentItem = listview->contentItem(); - QVERIFY(contentItem != 0); - - QQuickItem *delegate = findItem(contentItem, "delegate", 1); - QVERIFY(delegate); - QCOMPARE(delegate->property("test1").toString(),QString("Item 2")); - QCOMPARE(delegate->property("test2").toString(),QString("Item 2")); - QVERIFY(qobject_cast(delegate->property("test3").value()) != 0); - QVERIFY(qobject_cast(delegate->property("test4").value()) != 0); - QCOMPARE(delegate->property("test5").toString(),QString("Item 2")); - QCOMPARE(delegate->property("test9").toString(),QString("Item 2")); - QVERIFY(delegate->property("test6").value() != 0); - QCOMPARE(delegate->property("test7").toInt(),1); - QCOMPARE(delegate->property("test8").toInt(),1); - } - - { - QQuickView view; - - QStandardItemModel model; - initStandardTreeModel(&model); - - view.rootContext()->setContextProperty("myModel", &model); - - QUrl source(QUrl::fromLocalFile(TESTDATA("modelproperties2.qml"))); - - //3 items, 3 i each - QTest::ignoreMessage(QtWarningMsg, source.toString().toLatin1() + ":13: ReferenceError: Can't find variable: modelData"); - QTest::ignoreMessage(QtWarningMsg, source.toString().toLatin1() + ":13: ReferenceError: Can't find variable: modelData"); - QTest::ignoreMessage(QtWarningMsg, source.toString().toLatin1() + ":13: ReferenceError: Can't find variable: modelData"); - QTest::ignoreMessage(QtWarningMsg, source.toString().toLatin1() + ":11: ReferenceError: Can't find variable: modelData"); - QTest::ignoreMessage(QtWarningMsg, source.toString().toLatin1() + ":11: ReferenceError: Can't find variable: modelData"); - QTest::ignoreMessage(QtWarningMsg, source.toString().toLatin1() + ":11: ReferenceError: Can't find variable: modelData"); - QTest::ignoreMessage(QtWarningMsg, source.toString().toLatin1() + ":17: TypeError: Cannot read property 'display' of undefined"); - QTest::ignoreMessage(QtWarningMsg, source.toString().toLatin1() + ":17: TypeError: Cannot read property 'display' of undefined"); - QTest::ignoreMessage(QtWarningMsg, source.toString().toLatin1() + ":17: TypeError: Cannot read property 'display' of undefined"); - - view.setSource(source); - - QQuickListView *listview = qobject_cast(view.rootObject()); - QVERIFY(listview != 0); - - QQuickItem *contentItem = listview->contentItem(); - QVERIFY(contentItem != 0); - - QQuickItem *delegate = findItem(contentItem, "delegate", 1); - QVERIFY(delegate); - QCOMPARE(delegate->property("test1").toString(),QString("Row 2 Item")); - QCOMPARE(delegate->property("test2").toString(),QString("Row 2 Item")); - QVERIFY(!delegate->property("test3").isValid()); - QVERIFY(!delegate->property("test4").isValid()); - QVERIFY(!delegate->property("test5").isValid()); - QVERIFY(!delegate->property("test9").isValid()); - QVERIFY(delegate->property("test6").value() != 0); - QCOMPARE(delegate->property("test7").toInt(),1); - QCOMPARE(delegate->property("test8").toInt(),1); - } - - //### should also test QStringList and QVariantList -} - -void tst_qquickvisualdatamodel::noDelegate_data() -{ - QTest::addColumn("source"); - - QTest::newRow("item delegate") << QUrl::fromLocalFile(TESTDATA("datalist.qml")); - QTest::newRow("package delegate") << QUrl::fromLocalFile(TESTDATA("datalist-package.qml")); -} - -void tst_qquickvisualdatamodel::noDelegate() -{ - QFETCH(QUrl, source); - - QQuickView view; - - QStandardItemModel model; - initStandardTreeModel(&model); - - view.rootContext()->setContextProperty("myModel", &model); - - view.setSource(source); - - QQuickListView *listview = qobject_cast(view.rootObject()); - QVERIFY(listview != 0); - - QQuickVisualDataModel *vdm = listview->findChild("visualModel"); - QVERIFY(vdm != 0); - QCOMPARE(vdm->count(), 3); - - vdm->setDelegate(0); - QCOMPARE(vdm->count(), 0); -} - -void tst_qquickvisualdatamodel::itemsDestroyed_data() -{ - QTest::addColumn("source"); - - QTest::newRow("listView") << QUrl::fromLocalFile(TESTDATA("itemsDestroyed_listView.qml")); - QTest::newRow("package") << QUrl::fromLocalFile(TESTDATA("itemsDestroyed_package.qml")); - QTest::newRow("pathView") << QUrl::fromLocalFile(TESTDATA("itemsDestroyed_pathView.qml")); - QTest::newRow("repeater") << QUrl::fromLocalFile(TESTDATA("itemsDestroyed_repeater.qml")); -} - -void tst_qquickvisualdatamodel::itemsDestroyed() -{ - QFETCH(QUrl, source); - - QDeclarativeGuard delegate; - - { - QQuickView view; - QStandardItemModel model; - initStandardTreeModel(&model); - view.rootContext()->setContextProperty("myModel", &model); - view.setSource(source); - - view.show(); - QTest::qWaitForWindowShown(&view); - - QVERIFY(delegate = findItem(view.rootItem(), "delegate", 1)); - } - QCoreApplication::sendPostedEvents(0, QEvent::DeferredDelete); - QVERIFY(!delegate); -} - -void tst_qquickvisualdatamodel::qaimRowsMoved() -{ - // Test parameters passed in QAIM::rowsMoved() signal are converted correctly - // when translated and emitted as the QListModelInterface::itemsMoved() signal - QFETCH(int, sourceFirst); - QFETCH(int, sourceLast); - QFETCH(int, destinationChild); - QFETCH(int, expectFrom); - QFETCH(int, expectTo); - QFETCH(int, expectCount); - - QDeclarativeEngine engine; - QDeclarativeComponent c(&engine, QUrl::fromLocalFile(TESTDATA("visualdatamodel.qml"))); - - SingleRoleModel model; - model.list.clear(); - for (int i=0; i<30; i++) - model.list << ("item " + i); - engine.rootContext()->setContextProperty("myModel", &model); - - QQuickVisualDataModel *obj = qobject_cast(c.create()); - QVERIFY(obj != 0); - - QSignalSpy spy(obj, SIGNAL(modelUpdated(QDeclarativeChangeSet,bool))); - model.emitMove(sourceFirst, sourceLast, destinationChild); - // QAbstractItemModel also emits the changed signal when items are moved. - QCOMPARE(spy.count(), 2); - - bool move = false; - for (int i = 0; i < 2; ++i) { - QCOMPARE(spy[1].count(), 2); - QDeclarativeChangeSet changeSet = spy[i][0].value(); - if (!changeSet.changes().isEmpty()) - continue; - move = true; - QCOMPARE(changeSet.removes().count(), 1); - QCOMPARE(changeSet.removes().at(0).index, expectFrom); - QCOMPARE(changeSet.removes().at(0).count, expectCount); - QCOMPARE(changeSet.inserts().count(), 1); - QCOMPARE(changeSet.inserts().at(0).index, expectTo); - QCOMPARE(changeSet.inserts().at(0).count, expectCount); - QCOMPARE(changeSet.removes().at(0).moveId, changeSet.inserts().at(0).moveId); - QCOMPARE(spy[i][1].toBool(), false); - } - QVERIFY(move); - - delete obj; -} - -void tst_qquickvisualdatamodel::qaimRowsMoved_data() -{ - QTest::addColumn("sourceFirst"); - QTest::addColumn("sourceLast"); - QTest::addColumn("destinationChild"); - QTest::addColumn("expectFrom"); - QTest::addColumn("expectTo"); - QTest::addColumn("expectCount"); - - QTest::newRow("move 1 forward") - << 1 << 1 << 6 - << 1 << 5 << 1; - - QTest::newRow("move 1 backwards") - << 4 << 4 << 1 - << 4 << 1 << 1; - - QTest::newRow("move multiple forwards") - << 0 << 2 << 13 - << 0 << 10 << 3; - - QTest::newRow("move multiple forwards, with same to") - << 0 << 1 << 3 - << 0 << 1 << 2; - - QTest::newRow("move multiple backwards") - << 10 << 14 << 1 - << 10 << 1 << 5; -} - -void tst_qquickvisualdatamodel::remove_data() -{ - QTest::addColumn("source"); - QTest::addColumn("package delegate"); - - QTest::newRow("item delegate") - << QUrl::fromLocalFile(TESTDATA("groups.qml")) - << QString(); - QTest::newRow("package") - << QUrl::fromLocalFile(TESTDATA("groups-package.qml")) - << QString("package."); -} - -void tst_qquickvisualdatamodel::remove() -{ - QQuickView view; - - SingleRoleModel model; - model.list = QStringList() - << "one" - << "two" - << "three" - << "four" - << "five" - << "six" - << "seven" - << "eight" - << "nine" - << "ten" - << "eleven" - << "twelve"; - - QDeclarativeContext *ctxt = view.rootContext(); - ctxt->setContextProperty("myModel", &model); - - view.setSource(QUrl::fromLocalFile(TESTDATA("groups.qml"))); - - QQuickListView *listview = qobject_cast(view.rootObject()); - QVERIFY(listview != 0); - - QQuickItem *contentItem = listview->contentItem(); - QVERIFY(contentItem != 0); - - QQuickVisualDataModel *visualModel = qobject_cast(qvariant_cast(listview->model())); - QVERIFY(visualModel); - - { - QCOMPARE(listview->count(), 12); - QCOMPARE(visualModel->items()->count(), 12); - static const int mIndex[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11 }; - static const int iIndex[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11 }; - - for (int i = 0; i < lengthOf(mIndex); ++i) { - QQuickItem *delegate = findItem(contentItem, "delegate", mIndex[i]); - QVERIFY(delegate); - QCOMPARE(delegate->property("test1").toString(), model.list.at(mIndex[i])); - QCOMPARE(delegate->property("test2").toInt(), mIndex[i]); - QCOMPARE(delegate->property("test3").toInt(), iIndex[i]); - } - } { - evaluate(visualModel, "items.remove(2)"); - QCOMPARE(listview->count(), 11); - QCOMPARE(visualModel->items()->count(), 11); - static const int mIndex[] = { 0, 1, 3, 4, 5, 6, 7, 8, 9,10,11 }; - static const int iIndex[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10 }; - - for (int i = 0; i < lengthOf(mIndex); ++i) { - QQuickItem *delegate = findItem(contentItem, "delegate", mIndex[i]); - QVERIFY(delegate); - QCOMPARE(delegate->property("test1").toString(), model.list.at(mIndex[i])); - QCOMPARE(delegate->property("test2").toInt(), mIndex[i]); - QCOMPARE(delegate->property("test3").toInt(), iIndex[i]); - } - } { - evaluate(visualModel, "items.remove(1, 4)"); - QCOMPARE(listview->count(), 7); - QCOMPARE(visualModel->items()->count(), 7); - static const int mIndex[] = { 0, 6, 7, 8, 9,10,11 }; - static const int iIndex[] = { 0, 1, 2, 3, 4, 5, 6 }; - - for (int i = 0; i < lengthOf(mIndex); ++i) { - QQuickItem *delegate = findItem(contentItem, "delegate", mIndex[i]); - QVERIFY(delegate); - QCOMPARE(delegate->property("test1").toString(), model.list.at(mIndex[i])); - QCOMPARE(delegate->property("test2").toInt(), mIndex[i]); - QCOMPARE(delegate->property("test3").toInt(), iIndex[i]); - } - } { - QTest::ignoreMessage(QtWarningMsg, ": QML VisualDataGroup: remove: index out of range"); - evaluate(visualModel, "items.remove(-8, 4)"); - QCOMPARE(listview->count(), 7); - QCOMPARE(visualModel->items()->count(), 7); - } { - QTest::ignoreMessage(QtWarningMsg, ": QML VisualDataGroup: remove: index out of range"); - evaluate(visualModel, "items.remove(12, 2)"); - QCOMPARE(listview->count(), 7); - QCOMPARE(visualModel->items()->count(), 7); - } { - QTest::ignoreMessage(QtWarningMsg, ": QML VisualDataGroup: remove: index out of range"); - evaluate(visualModel, "items.remove(5, 3)"); - QCOMPARE(listview->count(), 7); - QCOMPARE(visualModel->items()->count(), 7); - } { - QTest::ignoreMessage(QtWarningMsg, ": QML VisualDataGroup: remove: invalid count"); - evaluate(visualModel, "items.remove(5, -2)"); - QCOMPARE(listview->count(), 7); - QCOMPARE(visualModel->items()->count(), 7); - } -} - -void tst_qquickvisualdatamodel::move_data() -{ - QTest::addColumn("source"); - QTest::addColumn("package delegate"); - - QTest::newRow("item delegate") - << QUrl::fromLocalFile(TESTDATA("groups.qml")) - << QString(); - QTest::newRow("package") - << QUrl::fromLocalFile(TESTDATA("groups-package.qml")) - << QString("package."); -} - -void tst_qquickvisualdatamodel::move() -{ - QQuickView view; - - SingleRoleModel model; - model.list = QStringList() - << "one" - << "two" - << "three" - << "four" - << "five" - << "six" - << "seven" - << "eight" - << "nine" - << "ten" - << "eleven" - << "twelve"; - - QDeclarativeContext *ctxt = view.rootContext(); - ctxt->setContextProperty("myModel", &model); - - view.setSource(QUrl::fromLocalFile(TESTDATA("groups.qml"))); - - QQuickListView *listview = qobject_cast(view.rootObject()); - QVERIFY(listview != 0); - - QQuickItem *contentItem = listview->contentItem(); - QVERIFY(contentItem != 0); - - QQuickVisualDataModel *visualModel = qobject_cast(qvariant_cast(listview->model())); - QVERIFY(visualModel); - - { - QCOMPARE(listview->count(), 12); - QCOMPARE(visualModel->items()->count(), 12); - static const int mIndex[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11 }; - static const int iIndex[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11 }; - - for (int i = 0; i < lengthOf(mIndex); ++i) { - QQuickItem *delegate = findItem(contentItem, "delegate", mIndex[i]); - QVERIFY(delegate); - QCOMPARE(delegate->property("test1").toString(), model.list.at(mIndex[i])); - QCOMPARE(delegate->property("test2").toInt(), mIndex[i]); - QCOMPARE(delegate->property("test3").toInt(), iIndex[i]); - } - } { - evaluate(visualModel, "items.move(2, 4)"); - QCOMPARE(listview->count(), 12); - QCOMPARE(visualModel->items()->count(), 12); - static const int mIndex[] = { 0, 1, 3, 4, 2, 5, 6, 7, 8, 9,10,11 }; - static const int iIndex[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11 }; - - for (int i = 0; i < lengthOf(mIndex); ++i) { - QQuickItem *delegate = findItem(contentItem, "delegate", mIndex[i]); - QVERIFY(delegate); - QCOMPARE(delegate->property("test1").toString(), model.list.at(mIndex[i])); - QCOMPARE(delegate->property("test2").toInt(), mIndex[i]); - QCOMPARE(delegate->property("test3").toInt(), iIndex[i]); - } - } { - evaluate(visualModel, "items.move(4, 2)"); - QCOMPARE(listview->count(), 12); - QCOMPARE(visualModel->items()->count(), 12); - static const int mIndex[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11 }; - static const int iIndex[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11 }; - - for (int i = 0; i < lengthOf(mIndex); ++i) { - QQuickItem *delegate = findItem(contentItem, "delegate", mIndex[i]); - QVERIFY(delegate); - QCOMPARE(delegate->property("test1").toString(), model.list.at(mIndex[i])); - QCOMPARE(delegate->property("test2").toInt(), mIndex[i]); - QCOMPARE(delegate->property("test3").toInt(), iIndex[i]); - } - } { - evaluate(visualModel, "items.move(8, 0, 4)"); - QCOMPARE(listview->count(), 12); - QCOMPARE(visualModel->items()->count(), 12); - static const int mIndex[] = { 8, 9,10,11, 0, 1, 2, 3, 4, 5, 6, 7 }; - static const int iIndex[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11 }; - - for (int i = 0; i < lengthOf(mIndex); ++i) { - QQuickItem *delegate = findItem(contentItem, "delegate", mIndex[i]); - QVERIFY(delegate); - QCOMPARE(delegate->property("test1").toString(), model.list.at(mIndex[i])); - QCOMPARE(delegate->property("test2").toInt(), mIndex[i]); - QCOMPARE(delegate->property("test3").toInt(), iIndex[i]); - } - } { - evaluate(visualModel, "items.move(3, 4, 5)"); - QCOMPARE(listview->count(), 12); - QCOMPARE(visualModel->items()->count(), 12); - static const int mIndex[] = { 8, 9,10,4, 11, 0, 1, 2, 3, 5, 6, 7 }; - static const int iIndex[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11 }; - - for (int i = 0; i < lengthOf(mIndex); ++i) { - QQuickItem *delegate = findItem(contentItem, "delegate", mIndex[i]); - QVERIFY(delegate); - QCOMPARE(delegate->property("test1").toString(), model.list.at(mIndex[i])); - QCOMPARE(delegate->property("test2").toInt(), mIndex[i]); - QCOMPARE(delegate->property("test3").toInt(), iIndex[i]); - } - } { - QTest::ignoreMessage(QtWarningMsg, ": QML VisualDataGroup: move: invalid count"); - evaluate(visualModel, "items.move(5, 2, -2)"); - QCOMPARE(listview->count(), 12); - QCOMPARE(visualModel->items()->count(), 12); - } { - QTest::ignoreMessage(QtWarningMsg, ": QML VisualDataGroup: move: from index out of range"); - evaluate(visualModel, "items.move(-6, 2, 1)"); - QCOMPARE(listview->count(), 12); - QCOMPARE(visualModel->items()->count(), 12); - } { - QTest::ignoreMessage(QtWarningMsg, ": QML VisualDataGroup: move: from index out of range"); - evaluate(visualModel, "items.move(15, 2, 1)"); - QCOMPARE(listview->count(), 12); - QCOMPARE(visualModel->items()->count(), 12); - } { - QTest::ignoreMessage(QtWarningMsg, ": QML VisualDataGroup: move: from index out of range"); - evaluate(visualModel, "items.move(11, 1, 3)"); - QCOMPARE(listview->count(), 12); - QCOMPARE(visualModel->items()->count(), 12); - } { - QTest::ignoreMessage(QtWarningMsg, ": QML VisualDataGroup: move: to index out of range"); - evaluate(visualModel, "items.move(2, -5, 1)"); - QCOMPARE(listview->count(), 12); - QCOMPARE(visualModel->items()->count(), 12); - } { - QTest::ignoreMessage(QtWarningMsg, ": QML VisualDataGroup: move: to index out of range"); - evaluate(visualModel, "items.move(2, 14, 1)"); - QCOMPARE(listview->count(), 12); - QCOMPARE(visualModel->items()->count(), 12); - } { - QTest::ignoreMessage(QtWarningMsg, ": QML VisualDataGroup: move: to index out of range"); - evaluate(visualModel, "items.move(2, 11, 4)"); - QCOMPARE(listview->count(), 12); - QCOMPARE(visualModel->items()->count(), 12); - } -} - -void tst_qquickvisualdatamodel::groups_data() -{ - QTest::addColumn("source"); - QTest::addColumn("part"); - - QTest::newRow("item delegate") - << QUrl::fromLocalFile(TESTDATA("groups.qml")) - << QString(); - QTest::newRow("package") - << QUrl::fromLocalFile(TESTDATA("groups-package.qml")) - << QString("visualModel.parts.package."); -} - -template void tst_qquickvisualdatamodel::groups_verify( - const SingleRoleModel &model, - QQuickItem *contentItem, - const int (&mIndex)[N], - const int (&iIndex)[N], - const int (&vIndex)[N], - const int (&sIndex)[N], - const bool (&vMember)[N], - const bool (&sMember)[N]) -{ - failed = true; - for (int i = 0; i < N; ++i) { - QQuickItem *delegate = findItem(contentItem, "delegate", mIndex[i]); - QVERIFY(delegate); - QCOMPARE(evaluate(delegate, "test1"), model.list.at(mIndex[i])); - QCOMPARE(evaluate(delegate, "test2") , mIndex[i]); - QCOMPARE(evaluate(delegate, "test3") , iIndex[i]); - QCOMPARE(evaluate(delegate, "test4"), true); - QCOMPARE(evaluate(delegate, "test5") , vIndex[i]); - QCOMPARE(evaluate(delegate, "test6"), vMember[i]); - QCOMPARE(evaluate(delegate, "test7") , sIndex[i]); - QCOMPARE(evaluate(delegate, "test8"), sMember[i]); - QCOMPARE(evaluate(delegate, "test9").contains("items") , QBool(true)); - QCOMPARE(evaluate(delegate, "test9").contains("visible") , QBool(vMember[i])); - QCOMPARE(evaluate(delegate, "test9").contains("selected"), QBool(sMember[i])); - } - failed = false; -} - -#define VERIFY_GROUPS \ - groups_verify(model, contentItem, mIndex, iIndex, vIndex, sIndex, vMember, sMember); \ - QVERIFY(!failed) - - -void tst_qquickvisualdatamodel::groups() -{ - QFETCH(QUrl, source); - QFETCH(QString, part); - - QQuickView view; - - SingleRoleModel model; - model.list = QStringList() - << "one" - << "two" - << "three" - << "four" - << "five" - << "six" - << "seven" - << "eight" - << "nine" - << "ten" - << "eleven" - << "twelve"; - - QDeclarativeContext *ctxt = view.rootContext(); - ctxt->setContextProperty("myModel", &model); - - view.setSource(source); - - QQuickListView *listview = qobject_cast(view.rootObject()); - QVERIFY(listview != 0); - - QQuickItem *contentItem = listview->contentItem(); - QVERIFY(contentItem != 0); - - QQuickVisualDataModel *visualModel = listview->findChild("visualModel"); - QVERIFY(visualModel); - - QQuickVisualDataGroup *visibleItems = listview->findChild("visibleItems"); - QVERIFY(visibleItems); - - QQuickVisualDataGroup *selectedItems = listview->findChild("selectedItems"); - QVERIFY(selectedItems); - - const bool f = false; - const bool t = true; - - { - QCOMPARE(listview->count(), 12); - QCOMPARE(visualModel->items()->count(), 12); - QCOMPARE(visibleItems->count(), 12); - QCOMPARE(selectedItems->count(), 0); - static const int mIndex [] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11 }; - static const int iIndex [] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11 }; - static const int vIndex [] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11 }; - static const bool vMember[] = { t, t, t, t, t, t, t, t, t, t, t, t }; - static const int sIndex [] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; - static const bool sMember[] = { f, f, f, f, f, f, f, f, f, f, f, f }; - VERIFY_GROUPS; - } { - evaluate(visualModel, "items.addGroups(8, \"selected\")"); - QCOMPARE(listview->count(), 12); - QCOMPARE(visualModel->items()->count(), 12); - QCOMPARE(visibleItems->count(), 12); - QCOMPARE(selectedItems->count(), 1); - static const int mIndex [] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11 }; - static const int iIndex [] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11 }; - static const int vIndex [] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11 }; - static const bool vMember[] = { t, t, t, t, t, t, t, t, t, t, t, t }; - static const int sIndex [] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1 }; - static const bool sMember[] = { f, f, f, f, f, f, f, f, t, f, f, f }; - VERIFY_GROUPS; - } { - evaluate(visualModel, "items.addGroups(6, 4, [\"visible\", \"selected\"])"); - QCOMPARE(listview->count(), 12); - QCOMPARE(visualModel->items()->count(), 12); - QCOMPARE(visibleItems->count(), 12); - QCOMPARE(selectedItems->count(), 4); - static const int mIndex [] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11 }; - static const int iIndex [] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11 }; - static const int vIndex [] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11 }; - static const bool vMember[] = { t, t, t, t, t, t, t, t, t, t, t, t }; - static const int sIndex [] = { 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 4 }; - static const bool sMember[] = { f, f, f, f, f, f, t, t, t, t, f, f }; - VERIFY_GROUPS; - } { - evaluate(visualModel, "items.setGroups(2, [\"items\", \"selected\"])"); - QCOMPARE(listview->count(), 12); - QCOMPARE(visualModel->items()->count(), 12); - QCOMPARE(visibleItems->count(), 11); - QCOMPARE(selectedItems->count(), 5); - static const int mIndex [] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11 }; - static const int iIndex [] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11 }; - static const int vIndex [] = { 0, 1, 2, 2, 3, 4, 5, 6, 7, 8, 9,10 }; - static const bool vMember[] = { t, t, f, t, t, t, t, t, t, t, t, t }; - static const int sIndex [] = { 0, 0, 0, 1, 1, 1, 1, 2, 3, 4, 5, 5 }; - static const bool sMember[] = { f, f, t, f, f, f, t, t, t, t, f, f }; - VERIFY_GROUPS; - } { - evaluate(selectedItems, "setGroups(0, 3, \"items\")"); - QCOMPARE(listview->count(), 12); - QCOMPARE(visualModel->items()->count(), 12); - QCOMPARE(visibleItems->count(), 9); - QCOMPARE(selectedItems->count(), 2); - static const int mIndex [] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11 }; - static const int iIndex [] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11 }; - static const int vIndex [] = { 0, 1, 2, 2, 3, 4, 5, 5, 5, 6, 7, 8 }; - static const bool vMember[] = { t, t, f, t, t, t, f, f, t, t, t, t }; - static const int sIndex [] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 2 }; - static const bool sMember[] = { f, f, f, f, f, f, f, f, t, t, f, f }; - VERIFY_GROUPS; - } { - QTest::ignoreMessage(QtWarningMsg, ": QML VisualDataGroup: addGroups: invalid count"); - evaluate(visualModel, "items.addGroups(11, -4, \"items\")"); - QCOMPARE(listview->count(), 12); - QCOMPARE(visualModel->items()->count(), 12); - QCOMPARE(visibleItems->count(), 9); - QCOMPARE(selectedItems->count(), 2); - } { - QTest::ignoreMessage(QtWarningMsg, ": QML VisualDataGroup: addGroups: index out of range"); - evaluate(visualModel, "items.addGroups(-1, 3, \"items\")"); - QCOMPARE(listview->count(), 12); - QCOMPARE(visualModel->items()->count(), 12); - QCOMPARE(visibleItems->count(), 9); - QCOMPARE(selectedItems->count(), 2); - } { - QTest::ignoreMessage(QtWarningMsg, ": QML VisualDataGroup: addGroups: index out of range"); - evaluate(visualModel, "items.addGroups(14, 3, \"items\")"); - QCOMPARE(listview->count(), 12); - QCOMPARE(visualModel->items()->count(), 12); - QCOMPARE(visibleItems->count(), 9); - QCOMPARE(selectedItems->count(), 2); - } { - QTest::ignoreMessage(QtWarningMsg, ": QML VisualDataGroup: addGroups: index out of range"); - evaluate(visualModel, "items.addGroups(11, 5, \"items\")"); - QCOMPARE(listview->count(), 12); - QCOMPARE(visualModel->items()->count(), 12); - QCOMPARE(visibleItems->count(), 9); - QCOMPARE(selectedItems->count(), 2); - } { - QTest::ignoreMessage(QtWarningMsg, ": QML VisualDataGroup: setGroups: invalid count"); - evaluate(visualModel, "items.setGroups(11, -4, \"items\")"); - QCOMPARE(listview->count(), 12); - QCOMPARE(visualModel->items()->count(), 12); - QCOMPARE(visibleItems->count(), 9); - QCOMPARE(selectedItems->count(), 2); - } { - QTest::ignoreMessage(QtWarningMsg, ": QML VisualDataGroup: setGroups: index out of range"); - evaluate(visualModel, "items.setGroups(-1, 3, \"items\")"); - QCOMPARE(listview->count(), 12); - QCOMPARE(visualModel->items()->count(), 12); - QCOMPARE(visibleItems->count(), 9); - QCOMPARE(selectedItems->count(), 2); - } { - QTest::ignoreMessage(QtWarningMsg, ": QML VisualDataGroup: setGroups: index out of range"); - evaluate(visualModel, "items.setGroups(14, 3, \"items\")"); - QCOMPARE(listview->count(), 12); - QCOMPARE(visualModel->items()->count(), 12); - QCOMPARE(visibleItems->count(), 9); - QCOMPARE(selectedItems->count(), 2); - } { - QTest::ignoreMessage(QtWarningMsg, ": QML VisualDataGroup: setGroups: index out of range"); - evaluate(visualModel, "items.setGroups(11, 5, \"items\")"); - QCOMPARE(listview->count(), 12); - QCOMPARE(visualModel->items()->count(), 12); - QCOMPARE(visibleItems->count(), 9); - QCOMPARE(selectedItems->count(), 2); - } { - QTest::ignoreMessage(QtWarningMsg, ": QML VisualDataGroup: removeGroups: invalid count"); - evaluate(visualModel, "items.removeGroups(11, -4, \"items\")"); - QCOMPARE(listview->count(), 12); - QCOMPARE(visualModel->items()->count(), 12); - } { - QTest::ignoreMessage(QtWarningMsg, ": QML VisualDataGroup: removeGroups: index out of range"); - evaluate(visualModel, "items.removeGroups(-1, 3, \"items\")"); - QCOMPARE(listview->count(), 12); - QCOMPARE(visualModel->items()->count(), 12); - QCOMPARE(visibleItems->count(), 9); - QCOMPARE(selectedItems->count(), 2); - } { - QTest::ignoreMessage(QtWarningMsg, ": QML VisualDataGroup: removeGroups: index out of range"); - evaluate(visualModel, "items.removeGroups(14, 3, \"items\")"); - QCOMPARE(listview->count(), 12); - QCOMPARE(visualModel->items()->count(), 12); - QCOMPARE(visibleItems->count(), 9); - QCOMPARE(selectedItems->count(), 2); - } { - QTest::ignoreMessage(QtWarningMsg, ": QML VisualDataGroup: removeGroups: index out of range"); - evaluate(visualModel, "items.removeGroups(11, 5, \"items\")"); - QCOMPARE(listview->count(), 12); - QCOMPARE(visualModel->items()->count(), 12); - QCOMPARE(visibleItems->count(), 9); - QCOMPARE(selectedItems->count(), 2); - } { - evaluate(visualModel, part + "filterOnGroup = \"visible\""); - QCOMPARE(listview->count(), 9); - QCOMPARE(visualModel->items()->count(), 12); - QCOMPARE(visibleItems->count(), 9); - QCOMPARE(selectedItems->count(), 2); - QCOMPARE(evaluate(visualModel, part + "filterOnGroup"), QString("visible")); - } { - evaluate(visualModel, part + "filterOnGroup = \"selected\""); - QCOMPARE(listview->count(), 2); - QCOMPARE(visualModel->items()->count(), 12); - QCOMPARE(visibleItems->count(), 9); - QCOMPARE(selectedItems->count(), 2); - QCOMPARE(evaluate(visualModel, part + "filterOnGroup"), QString("selected")); - } { - evaluate(visualModel, part + "filterOnGroup = undefined"); - QCOMPARE(listview->count(), 12); - QCOMPARE(visualModel->items()->count(), 12); - QCOMPARE(visibleItems->count(), 9); - QCOMPARE(selectedItems->count(), 2); - QCOMPARE(evaluate(visualModel, part + "filterOnGroup"), QString("items")); - } { - QQuickItem *delegate = findItem(contentItem, "delegate", 5); - QVERIFY(delegate); - - evaluate(delegate, "hide()"); - QCOMPARE(listview->count(), 12); - QCOMPARE(visualModel->items()->count(), 12); - QCOMPARE(visibleItems->count(), 8); - QCOMPARE(selectedItems->count(), 2); - static const int mIndex [] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11 }; - static const int iIndex [] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11 }; - static const int vIndex [] = { 0, 1, 2, 2, 3, 4, 4, 4, 4, 5, 6, 7 }; - static const bool vMember[] = { t, t, f, t, t, f, f, f, t, t, t, t }; - static const int sIndex [] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 2 }; - static const bool sMember[] = { f, f, f, f, f, f, f, f, t, t, f, f }; - VERIFY_GROUPS; - } { - QQuickItem *delegate = findItem(contentItem, "delegate", 5); - QVERIFY(delegate); - - evaluate(delegate, "select()"); - QCOMPARE(listview->count(), 12); - QCOMPARE(visualModel->items()->count(), 12); - QCOMPARE(visibleItems->count(), 8); - QCOMPARE(selectedItems->count(), 3); - static const int mIndex [] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11 }; - static const int iIndex [] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11 }; - static const int vIndex [] = { 0, 1, 2, 2, 3, 4, 4, 4, 4, 5, 6, 7 }; - static const bool vMember[] = { t, t, f, t, t, f, f, f, t, t, t, t }; - static const int sIndex [] = { 0, 0, 0, 0, 0, 0, 1, 1, 1, 2, 3, 3 }; - static const bool sMember[] = { f, f, f, f, f, t, f, f, t, t, f, f }; - VERIFY_GROUPS; - } { - evaluate(visualModel, "items.move(2, 6, 3)"); - QCOMPARE(listview->count(), 12); - QCOMPARE(visualModel->items()->count(), 12); - QCOMPARE(visibleItems->count(), 8); - QCOMPARE(selectedItems->count(), 3); - static const int mIndex [] = { 0, 1, 5, 6, 7, 8, 2, 3, 4, 9,10,11 }; - static const int iIndex [] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11 }; - static const int vIndex [] = { 0, 1, 2, 2, 2, 2, 3, 3, 4, 5, 6, 7 }; - static const bool vMember[] = { t, t, f, f, f, t, f, t, t, t, t, t }; - static const int sIndex [] = { 0, 0, 0, 1, 1, 1, 2, 2, 2, 2, 3, 3 }; - static const bool sMember[] = { f, f, t, f, f, t, f, f, f, t, f, f }; - VERIFY_GROUPS; - } -} - -template void tst_qquickvisualdatamodel::get_verify( - const SingleRoleModel &model, - QQuickVisualDataModel *visualModel, - QQuickVisualDataGroup *visibleItems, - QQuickVisualDataGroup *selectedItems, - const int (&mIndex)[N], - const int (&iIndex)[N], - const int (&vIndex)[N], - const int (&sIndex)[N], - const bool (&vMember)[N], - const bool (&sMember)[N]) -{ - failed = true; - for (int i = 0; i < N; ++i) { - QCOMPARE(evaluate(visualModel, QString("items.get(%1).model.name").arg(i)), model.list.at(mIndex[i])); - QCOMPARE(evaluate(visualModel, QString("items.get(%1).model.modelData").arg(i)), model.list.at(mIndex[i])); - QCOMPARE(evaluate(visualModel, QString("items.get(%1).model.index").arg(i)), mIndex[i]); - QCOMPARE(evaluate(visualModel, QString("items.get(%1).itemsIndex").arg(i)), iIndex[i]); - QCOMPARE(evaluate(visualModel, QString("items.get(%1).inItems").arg(i)), true); - QCOMPARE(evaluate(visualModel, QString("items.get(%1).visibleIndex").arg(i)), vIndex[i]); - QCOMPARE(evaluate(visualModel, QString("items.get(%1).inVisible").arg(i)), vMember[i]); - QCOMPARE(evaluate(visualModel, QString("items.get(%1).selectedIndex").arg(i)), sIndex[i]); - QCOMPARE(evaluate(visualModel, QString("items.get(%1).inSelected").arg(i)), sMember[i]); - QCOMPARE(evaluate(visualModel, QString("contains(items.get(%1).groups, \"items\")").arg(i)), true); - QCOMPARE(evaluate(visualModel, QString("contains(items.get(%1).groups, \"visible\")").arg(i)), vMember[i]); - QCOMPARE(evaluate(visualModel, QString("contains(items.get(%1).groups, \"selected\")").arg(i)), sMember[i]); - - if (vMember[i]) { - QCOMPARE(evaluate(visibleItems, QString("get(%1).model.name").arg(vIndex[i])), model.list.at(mIndex[i])); - QCOMPARE(evaluate(visibleItems, QString("get(%1).model.modelData").arg(vIndex[i])), model.list.at(mIndex[i])); - QCOMPARE(evaluate(visibleItems, QString("get(%1).model.index").arg(vIndex[i])), mIndex[i]); - QCOMPARE(evaluate(visibleItems, QString("get(%1).itemsIndex").arg(vIndex[i])), iIndex[i]); - QCOMPARE(evaluate(visibleItems, QString("get(%1).inItems").arg(vIndex[i])), true); - QCOMPARE(evaluate(visibleItems, QString("get(%1).visibleIndex").arg(vIndex[i])), vIndex[i]); - QCOMPARE(evaluate(visibleItems, QString("get(%1).inVisible").arg(vIndex[i])), vMember[i]); - QCOMPARE(evaluate(visibleItems, QString("get(%1).selectedIndex").arg(vIndex[i])), sIndex[i]); - QCOMPARE(evaluate(visibleItems, QString("get(%1).inSelected").arg(vIndex[i])), sMember[i]); - - QCOMPARE(evaluate(visibleItems, QString("contains(get(%1).groups, \"items\")").arg(vIndex[i])), true); - QCOMPARE(evaluate(visibleItems, QString("contains(get(%1).groups, \"visible\")").arg(vIndex[i])), vMember[i]); - QCOMPARE(evaluate(visibleItems, QString("contains(get(%1).groups, \"selected\")").arg(vIndex[i])), sMember[i]); - } - if (sMember[i]) { - QCOMPARE(evaluate(selectedItems, QString("get(%1).model.name").arg(sIndex[i])), model.list.at(mIndex[i])); - QCOMPARE(evaluate(selectedItems, QString("get(%1).model.modelData").arg(sIndex[i])), model.list.at(mIndex[i])); - QCOMPARE(evaluate(selectedItems, QString("get(%1).model.index").arg(sIndex[i])), mIndex[i]); - QCOMPARE(evaluate(selectedItems, QString("get(%1).itemsIndex").arg(sIndex[i])), iIndex[i]); - QCOMPARE(evaluate(selectedItems, QString("get(%1).inItems").arg(sIndex[i])), true); - QCOMPARE(evaluate(selectedItems, QString("get(%1).visibleIndex").arg(sIndex[i])), vIndex[i]); - QCOMPARE(evaluate(selectedItems, QString("get(%1).inVisible").arg(sIndex[i])), vMember[i]); - QCOMPARE(evaluate(selectedItems, QString("get(%1).selectedIndex").arg(sIndex[i])), sIndex[i]); - QCOMPARE(evaluate(selectedItems, QString("get(%1).inSelected").arg(sIndex[i])), sMember[i]); - QCOMPARE(evaluate(selectedItems, QString("contains(get(%1).groups, \"items\")").arg(sIndex[i])), true); - QCOMPARE(evaluate(selectedItems, QString("contains(get(%1).groups, \"visible\")").arg(sIndex[i])), vMember[i]); - QCOMPARE(evaluate(selectedItems, QString("contains(get(%1).groups, \"selected\")").arg(sIndex[i])), sMember[i]); - } - } - failed = false; -} - -#define VERIFY_GET \ - get_verify(model, visualModel, visibleItems, selectedItems, mIndex, iIndex, vIndex, sIndex, vMember, sMember); \ - QVERIFY(!failed) - -void tst_qquickvisualdatamodel::get() -{ - QQuickView view; - - SingleRoleModel model; - model.list = QStringList() - << "one" - << "two" - << "three" - << "four" - << "five" - << "six" - << "seven" - << "eight" - << "nine" - << "ten" - << "eleven" - << "twelve"; - - QDeclarativeContext *ctxt = view.rootContext(); - ctxt->setContextProperty("myModel", &model); - - view.setSource(QUrl::fromLocalFile(TESTDATA("groups.qml"))); - - QQuickListView *listview = qobject_cast(view.rootObject()); - QVERIFY(listview != 0); - - QQuickItem *contentItem = listview->contentItem(); - QVERIFY(contentItem != 0); - - QQuickVisualDataModel *visualModel = qobject_cast(qvariant_cast(listview->model())); - QVERIFY(visualModel); - - QQuickVisualDataGroup *visibleItems = visualModel->findChild("visibleItems"); - QVERIFY(visibleItems); - - QQuickVisualDataGroup *selectedItems = visualModel->findChild("selectedItems"); - QVERIFY(selectedItems); - - QV8Engine *v8Engine = QDeclarativeEnginePrivate::getV8Engine(ctxt->engine()); - QVERIFY(v8Engine); - - const bool f = false; - const bool t = true; - - { - QCOMPARE(listview->count(), 12); - QCOMPARE(visualModel->items()->count(), 12); - QCOMPARE(visibleItems->count(), 12); - QCOMPARE(selectedItems->count(), 0); - static const int mIndex [] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11 }; - static const int iIndex [] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11 }; - static const int vIndex [] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11 }; - static const bool vMember[] = { t, t, t, t, t, t, t, t, t, t, t, t }; - static const int sIndex [] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; - static const bool sMember[] = { f, f, f, f, f, f, f, f, f, f, f, f }; - VERIFY_GET; - } { - evaluate(visualModel, "items.addGroups(8, \"selected\")"); - QCOMPARE(listview->count(), 12); - QCOMPARE(visualModel->items()->count(), 12); - QCOMPARE(visibleItems->count(), 12); - QCOMPARE(selectedItems->count(), 1); - static const int mIndex [] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11 }; - static const int iIndex [] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11 }; - static const int vIndex [] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11 }; - static const bool vMember[] = { t, t, t, t, t, t, t, t, t, t, t, t }; - static const int sIndex [] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1 }; - static const bool sMember[] = { f, f, f, f, f, f, f, f, t, f, f, f }; - VERIFY_GET; - } { - evaluate(visualModel, "items.addGroups(6, 4, [\"visible\", \"selected\"])"); - QCOMPARE(listview->count(), 12); - QCOMPARE(visualModel->items()->count(), 12); - QCOMPARE(visibleItems->count(), 12); - QCOMPARE(selectedItems->count(), 4); - static const int mIndex [] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11 }; - static const int iIndex [] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11 }; - static const int vIndex [] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11 }; - static const bool vMember[] = { t, t, t, t, t, t, t, t, t, t, t, t }; - static const int sIndex [] = { 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 4 }; - static const bool sMember[] = { f, f, f, f, f, f, t, t, t, t, f, f }; - VERIFY_GET; - } { - evaluate(visualModel, "items.setGroups(2, [\"items\", \"selected\"])"); - QCOMPARE(listview->count(), 12); - QCOMPARE(visualModel->items()->count(), 12); - QCOMPARE(visibleItems->count(), 11); - QCOMPARE(selectedItems->count(), 5); - static const int mIndex [] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11 }; - static const int iIndex [] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11 }; - static const int vIndex [] = { 0, 1, 2, 2, 3, 4, 5, 6, 7, 8, 9,10 }; - static const bool vMember[] = { t, t, f, t, t, t, t, t, t, t, t, t }; - static const int sIndex [] = { 0, 0, 0, 1, 1, 1, 1, 2, 3, 4, 5, 5 }; - static const bool sMember[] = { f, f, t, f, f, f, t, t, t, t, f, f }; - VERIFY_GET; - } { - evaluate(selectedItems, "setGroups(0, 3, \"items\")"); - QCOMPARE(listview->count(), 12); - QCOMPARE(visualModel->items()->count(), 12); - QCOMPARE(visibleItems->count(), 9); - QCOMPARE(selectedItems->count(), 2); - static const int mIndex [] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11 }; - static const int iIndex [] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11 }; - static const int vIndex [] = { 0, 1, 2, 2, 3, 4, 5, 5, 5, 6, 7, 8 }; - static const bool vMember[] = { t, t, f, t, t, t, f, f, t, t, t, t }; - static const int sIndex [] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 2 }; - static const bool sMember[] = { f, f, f, f, f, f, f, f, t, t, f, f }; - VERIFY_GET; - } { - evaluate(visualModel, "items.get(5).inVisible = false"); - QCOMPARE(listview->count(), 12); - QCOMPARE(visualModel->items()->count(), 12); - QCOMPARE(visibleItems->count(), 8); - QCOMPARE(selectedItems->count(), 2); - static const int mIndex [] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11 }; - static const int iIndex [] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11 }; - static const int vIndex [] = { 0, 1, 2, 2, 3, 4, 4, 4, 4, 5, 6, 7 }; - static const bool vMember[] = { t, t, f, t, t, f, f, f, t, t, t, t }; - static const int sIndex [] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 2 }; - static const bool sMember[] = { f, f, f, f, f, f, f, f, t, t, f, f }; - VERIFY_GET; - } { - evaluate(visualModel, "items.get(5).inSelected = true"); - QCOMPARE(listview->count(), 12); - QCOMPARE(visualModel->items()->count(), 12); - QCOMPARE(visibleItems->count(), 8); - QCOMPARE(selectedItems->count(), 3); - static const int mIndex [] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11 }; - static const int iIndex [] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11 }; - static const int vIndex [] = { 0, 1, 2, 2, 3, 4, 4, 4, 4, 5, 6, 7 }; - static const bool vMember[] = { t, t, f, t, t, f, f, f, t, t, t, t }; - static const int sIndex [] = { 0, 0, 0, 0, 0, 0, 1, 1, 1, 2, 3, 3 }; - static const bool sMember[] = { f, f, f, f, f, t, f, f, t, t, f, f }; - VERIFY_GET; - } { - evaluate(visualModel, "items.get(5).groups = [\"visible\", \"items\"]"); - QCOMPARE(listview->count(), 12); - QCOMPARE(visualModel->items()->count(), 12); - QCOMPARE(visibleItems->count(), 9); - QCOMPARE(selectedItems->count(), 2); - static const int mIndex [] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11 }; - static const int iIndex [] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11 }; - static const int vIndex [] = { 0, 1, 2, 2, 3, 4, 5, 5, 5, 6, 7, 8 }; - static const bool vMember[] = { t, t, f, t, t, t, f, f, t, t, t, t }; - static const int sIndex [] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 2 }; - static const bool sMember[] = { f, f, f, f, f, f, f, f, t, t, f, f }; - VERIFY_GET; - } -} - -void tst_qquickvisualdatamodel::invalidGroups() -{ - QUrl source = QUrl::fromLocalFile(TESTDATA("groups-invalid.qml")); - QTest::ignoreMessage(QtWarningMsg, (source.toString() + ":12:9: QML VisualDataGroup: " + QQuickVisualDataGroup::tr("Group names must start with a lower case letter")).toUtf8()); - - QDeclarativeComponent component(&engine, source); - QScopedPointer object(component.create()); - QVERIFY(object); - - QCOMPARE(evaluate(object.data(), "groups.length"), 4); - QCOMPARE(evaluate(object.data(), "groups[0].name"), QString("items")); - QCOMPARE(evaluate(object.data(), "groups[1].name"), QString("persistedItems")); - QCOMPARE(evaluate(object.data(), "groups[2].name"), QString("visible")); - QCOMPARE(evaluate(object.data(), "groups[3].name"), QString("selected")); -} - -void tst_qquickvisualdatamodel::onChanged_data() -{ - QTest::addColumn("expression"); - QTest::addColumn("tests"); - - QTest::newRow("item appended") - << QString("listModel.append({\"number\": \"five\"})") - << (QStringList() - << "verify(vm.removed, [], [], [])" - << "verify(vm.inserted, [4], [1], [undefined])" - << "verify(vi.removed, [], [], [])" - << "verify(vi.inserted, [4], [1], [undefined])" - << "verify(si.removed, [], [], [])" - << "verify(si.inserted, [], [], [])"); - QTest::newRow("item prepended") - << QString("listModel.insert(0, {\"number\": \"five\"})") - << (QStringList() - << "verify(vm.removed, [], [], [])" - << "verify(vm.inserted, [0], [1], [undefined])" - << "verify(vi.removed, [], [], [])" - << "verify(vi.inserted, [0], [1], [undefined])" - << "verify(si.removed, [], [], [])" - << "verify(si.inserted, [], [], [])"); - QTest::newRow("item inserted") - << QString("listModel.insert(2, {\"number\": \"five\"})") - << (QStringList() - << "verify(vm.removed, [], [], [])" - << "verify(vm.inserted, [2], [1], [undefined])" - << "verify(vi.removed, [], [], [])" - << "verify(vi.inserted, [2], [1], [undefined])" - << "verify(si.removed, [], [], [])" - << "verify(si.inserted, [], [], [])"); - - QTest::newRow("item removed tail") - << QString("listModel.remove(3)") - << (QStringList() - << "verify(vm.removed, [3], [1], [undefined])" - << "verify(vm.inserted, [], [], [])" - << "verify(vi.removed, [3], [1], [undefined])" - << "verify(vi.inserted, [], [], [])" - << "verify(si.removed, [], [], [])" - << "verify(si.inserted, [], [], [])"); - QTest::newRow("item removed head") - << QString("listModel.remove(0)") - << (QStringList() - << "verify(vm.removed, [0], [1], [undefined])" - << "verify(vm.inserted, [], [], [])" - << "verify(vi.removed, [0], [1], [undefined])" - << "verify(vi.inserted, [], [], [])" - << "verify(si.removed, [], [], [])" - << "verify(si.inserted, [], [], [])"); - QTest::newRow("item removed middle") - << QString("listModel.remove(1)") - << (QStringList() - << "verify(vm.removed, [1], [1], [undefined])" - << "verify(vm.inserted, [], [], [])" - << "verify(vi.removed, [1], [1], [undefined])" - << "verify(vi.inserted, [], [], [])" - << "verify(si.removed, [], [], [])" - << "verify(si.inserted, [], [], [])"); - - - QTest::newRow("item moved from tail") - << QString("listModel.move(3, 0, 1)") - << (QStringList() - << "verify(vm.removed, [3], [1], [vm.inserted[0].moveId])" - << "verify(vm.inserted, [0], [1], [vm.removed[0].moveId])" - << "verify(vi.removed, [3], [1], [vi.inserted[0].moveId])" - << "verify(vi.inserted, [0], [1], [vi.removed[0].moveId])" - << "verify(si.removed, [], [], [])" - << "verify(si.inserted, [], [], [])"); - QTest::newRow("item moved from head") - << QString("listModel.move(0, 2, 2)") - << (QStringList() - << "verify(vm.removed, [0], [2], [vm.inserted[0].moveId])" - << "verify(vm.inserted, [2], [2], [vm.removed[0].moveId])" - << "verify(vi.removed, [0], [2], [vi.inserted[0].moveId])" - << "verify(vi.inserted, [2], [2], [vi.removed[0].moveId])" - << "verify(si.removed, [], [], [])" - << "verify(si.inserted, [], [], [])"); - - QTest::newRow("groups changed") - << QString("items.setGroups(1, 2, [\"items\", \"selected\"])") - << (QStringList() - << "verify(vm.inserted, [], [], [])" - << "verify(vm.removed, [], [], [])" - << "verify(vi.removed, [1], [2], [undefined])" - << "verify(vi.inserted, [], [], [])" - << "verify(si.removed, [], [], [])" - << "verify(si.inserted, [0], [2], [undefined])"); - - QTest::newRow("multiple removes") - << QString("{ vi.remove(1, 1); " - "vi.removeGroups(0, 2, \"items\") }") - << (QStringList() - << "verify(vm.removed, [0, 1], [1, 1], [undefined, undefined])" - << "verify(vm.inserted, [], [], [])" - << "verify(vi.removed, [1], [1], [undefined])" - << "verify(vi.inserted, [], [], [])" - << "verify(si.removed, [], [], [])" - << "verify(si.inserted, [], [], [])"); -} - -void tst_qquickvisualdatamodel::onChanged() -{ - QFETCH(QString, expression); - QFETCH(QStringList, tests); - - QDeclarativeComponent component(&engine, QUrl::fromLocalFile(TESTDATA("onChanged.qml"))); - QScopedPointer object(component.create()); - QVERIFY(object); - - evaluate(object.data(), expression); - - foreach (const QString &test, tests) { - bool passed = evaluate(object.data(), test); - if (!passed) - qWarning() << test; - QVERIFY(passed); - } -} - -void tst_qquickvisualdatamodel::create() -{ - QQuickView view; - - SingleRoleModel model; - model.list = QStringList() - << "one" - << "two" - << "three" - << "four" - << "five" - << "six" - << "seven" - << "eight" - << "nine" - << "ten" - << "eleven" - << "twelve" - << "thirteen" - << "fourteen" - << "fifteen" - << "sixteen" - << "seventeen" - << "eighteen" - << "nineteen" - << "twenty"; - - QDeclarativeContext *ctxt = view.rootContext(); - ctxt->setContextProperty("myModel", &model); - - view.setSource(QUrl::fromLocalFile(TESTDATA("create.qml"))); - - QQuickListView *listview = qobject_cast(view.rootObject()); - QVERIFY(listview != 0); - - QQuickItem *contentItem = listview->contentItem(); - QVERIFY(contentItem != 0); - - QQuickVisualDataModel *visualModel = qobject_cast(qvariant_cast(listview->model())); - QVERIFY(visualModel); - - QCOMPARE(listview->count(), 20); - - QQuickItem *delegate; - - // persistedItems.includeByDefault is true, so all items belong to persistedItems initially. - QVERIFY(delegate = findItem(contentItem, "delegate", 1)); - QCOMPARE(evaluate(delegate, "VisualDataModel.inPersistedItems"), true); - - // changing include by default doesn't remove persistance. - evaluate(visualModel, "persistedItems.includeByDefault = false"); - QCOMPARE(evaluate(delegate, "VisualDataModel.inPersistedItems"), true); - - // removing from persistedItems does. - evaluate(visualModel, "persistedItems.remove(0, 20)"); - QCOMPARE(listview->count(), 20); - QCOMPARE(evaluate(delegate, "VisualDataModel.inPersistedItems"), false); - - // Request an item instantiated by the view. - QVERIFY(delegate = qobject_cast(evaluate(visualModel, "items.create(1)"))); - QCOMPARE(delegate, findItem(contentItem, "delegate", 1)); - QCOMPARE(evaluate(delegate, "VisualDataModel.inPersistedItems"), true); - QCOMPARE(evaluate(visualModel, "persistedItems.count"), 1); - - evaluate(delegate, "VisualDataModel.inPersistedItems = false"); - QCOMPARE(listview->count(), 20); - QCOMPARE(evaluate(delegate, "destroyed"), false); - QCOMPARE(evaluate(delegate, "VisualDataModel.inPersistedItems"), false); - QCOMPARE(evaluate(visualModel, "persistedItems.count"), 0); - - // Request an item not instantiated by the view. - QVERIFY(!findItem(contentItem, "delegate", 15)); - QVERIFY(delegate = qobject_cast(evaluate(visualModel, "items.create(15)"))); - QCOMPARE(delegate, findItem(contentItem, "delegate", 15)); - QCOMPARE(evaluate(delegate, "VisualDataModel.inPersistedItems"), true); - QCOMPARE(evaluate(visualModel, "persistedItems.count"), 1); - - evaluate(visualModel, "persistedItems.remove(0)"); - QCOMPARE(evaluate(delegate, "destroyed"), true); - QCOMPARE(evaluate(visualModel, "persistedItems.count"), 0); - - // Request an item not instantiated by the view, then scroll the view so it will request it. - QVERIFY(!findItem(contentItem, "delegate", 16)); - QVERIFY(delegate = qobject_cast(evaluate(visualModel, "items.create(16)"))); - QCOMPARE(delegate, findItem(contentItem, "delegate", 16)); - QCOMPARE(evaluate(delegate, "VisualDataModel.inPersistedItems"), true); - QCOMPARE(evaluate(visualModel, "persistedItems.count"), 1); - - evaluate(listview, "positionViewAtIndex(19, ListView.End)"); - QCOMPARE(listview->count(), 20); - evaluate(delegate, "VisualDataModel.groups = [\"items\"]"); - QCOMPARE(evaluate(delegate, "destroyed"), false); - QCOMPARE(evaluate(delegate, "VisualDataModel.inPersistedItems"), false); - QCOMPARE(evaluate(visualModel, "persistedItems.count"), 0); - - // Request and release an item instantiated by the view, then scroll the view so it releases it. - QVERIFY(findItem(contentItem, "delegate", 17)); - QVERIFY(delegate = qobject_cast(evaluate(visualModel, "items.create(17)"))); - QCOMPARE(delegate, findItem(contentItem, "delegate", 17)); - QCOMPARE(evaluate(delegate, "VisualDataModel.inPersistedItems"), true); - QCOMPARE(evaluate(visualModel, "persistedItems.count"), 1); - - evaluate(visualModel, "items.removeGroups(17, \"persistedItems\")"); - QCOMPARE(evaluate(delegate, "destroyed"), false); - QCOMPARE(evaluate(delegate, "VisualDataModel.inPersistedItems"), false); - QCOMPARE(evaluate(visualModel, "persistedItems.count"), 0); - evaluate(listview, "positionViewAtIndex(1, ListView.Beginning)"); - QCOMPARE(listview->count(), 20); - QCOMPARE(evaluate(delegate, "destroyed"), true); - - // Adding an item to the persistedItems group won't instantiate it, but if later requested by - // the view it will be persisted. - evaluate(visualModel, "items.addGroups(18, \"persistedItems\")"); - QCOMPARE(evaluate(visualModel, "persistedItems.count"), 1); - QVERIFY(!findItem(contentItem, "delegate", 18)); - evaluate(listview, "positionViewAtIndex(19, ListView.End)"); - QCOMPARE(listview->count(), 20); - QVERIFY(delegate = findItem(contentItem, "delegate", 18)); - QCOMPARE(evaluate(delegate, "VisualDataModel.inPersistedItems"), true); - QCOMPARE(evaluate(delegate, "destroyed"), false); - evaluate(listview, "positionViewAtIndex(1, ListView.Beginning)"); - QCOMPARE(listview->count(), 20); - QCOMPARE(evaluate(delegate, "destroyed"), false); -} - - -void tst_qquickvisualdatamodel::incompleteModel() -{ - // VisualDataModel is first populated in componentComplete. Verify various functions are - // harmlessly ignored until then. - - QDeclarativeComponent component(&engine); - component.setData("import QtQuick 2.0\n VisualDataModel {}", QUrl::fromLocalFile(TESTDATA(""))); - - QScopedPointer object(component.beginCreate(engine.rootContext())); - - QQuickVisualDataModel *model = qobject_cast(object.data()); - QVERIFY(model); - - QSignalSpy itemsSpy(model->items(), SIGNAL(countChanged())); - QSignalSpy persistedItemsSpy(model->items(), SIGNAL(countChanged())); - - evaluate(model, "items.removeGroups(0, items.count, \"items\")"); - QCOMPARE(itemsSpy.count(), 0); - QCOMPARE(persistedItemsSpy.count(), 0); - - evaluate(model, "items.setGroups(0, items.count, \"persistedItems\")"); - QCOMPARE(itemsSpy.count(), 0); - QCOMPARE(persistedItemsSpy.count(), 0); - - evaluate(model, "items.addGroups(0, items.count, \"persistedItems\")"); - QCOMPARE(itemsSpy.count(), 0); - QCOMPARE(persistedItemsSpy.count(), 0); - - evaluate(model, "items.remove(0, items.count)"); - QCOMPARE(itemsSpy.count(), 0); - QCOMPARE(persistedItemsSpy.count(), 0); - - evaluate(model, "items.insert([ \"color\": \"blue\" ])"); - QCOMPARE(itemsSpy.count(), 0); - QCOMPARE(persistedItemsSpy.count(), 0); - - QTest::ignoreMessage(QtWarningMsg, ": QML VisualDataGroup: get: index out of range"); - QVERIFY(evaluate(model, "items.get(0) === undefined")); - - component.completeCreate(); -} - -template -T *tst_qquickvisualdatamodel::findItem(QQuickItem *parent, const QString &objectName, int index) -{ - const QMetaObject &mo = T::staticMetaObject; - //qDebug() << parent->childItems().count() << "children"; - for (int i = 0; i < parent->childItems().count(); ++i) { - QQuickItem *item = qobject_cast(parent->childItems().at(i)); - if (!item) - continue; - //qDebug() << "try" << item; - if (mo.cast(item) && (objectName.isEmpty() || item->objectName() == objectName)) { - if (index != -1) { - QDeclarativeExpression e(qmlContext(item), item, "index"); - if (e.evaluate().toInt() == index) - return static_cast(item); - } else { - return static_cast(item); - } - } - item = findItem(item, objectName, index); - if (item) - return static_cast(item); - } - - return 0; -} - -QTEST_MAIN(tst_qquickvisualdatamodel) - -#include "tst_qquickvisualdatamodel.moc" -- cgit v1.2.3