Extend VisualDataModel tests to cover Package delegates.
authorAndrew den Exter <andrew.den-exter@nokia.com>
Tue, 8 Nov 2011 03:00:02 +0000 (13:00 +1000)
committerQt by Nokia <qt-info@nokia.com>
Wed, 9 Nov 2011 07:23:57 +0000 (08:23 +0100)
Verify the existing tests also pass if a package delegate is used.
Fixes incorrect changes being emitted when the group a part model is
filtered on changes and package items not being un-parented when
destroyed.

Change-Id: Ib4be42274b0a0460ae9b763a9737f4a6d3b94603
Reviewed-by: Martin Jones <martin.jones@nokia.com>
src/declarative/items/qquickvisualdatamodel.cpp
tests/auto/declarative/qquickvisualdatamodel/data/datalist-package.qml [new file with mode: 0644]
tests/auto/declarative/qquickvisualdatamodel/data/groups-package.qml [new file with mode: 0644]
tests/auto/declarative/qquickvisualdatamodel/data/groups.qml
tests/auto/declarative/qquickvisualdatamodel/tst_qquickvisualdatamodel.cpp

index b9d97e7..8852dce 100644 (file)
@@ -2375,7 +2375,7 @@ void QQuickVisualPartsModel::updateFilterGroup()
     if (m_inheritGroup)
         return;
 
-    QDeclarativeListCompositor::Group previousGroup = model->m_compositorGroup;
+    QDeclarativeListCompositor::Group previousGroup = m_compositorGroup;
     m_compositorGroup = Compositor::Default;
     QQuickVisualDataGroupPrivate::get(model->m_groups[Compositor::Default])->emitters.insert(this);
     for (int i = 1; i < model->m_groupCount; ++i) {
@@ -2477,8 +2477,10 @@ QQuickVisualModel::ReleaseFlags QQuickVisualPartsModel::release(QQuickItem *item
         m_packaged.erase(it);
         if (!m_packaged.contains(item))
             flags &= ~Referenced;
-        if (flags & Destroyed)
+        if (flags & Destroyed) {
             QQuickVisualDataModelPrivate::get(m_model)->emitDestroyingPackage(package);
+            item->setParentItem(0);
+        }
     }
     return flags;
 }
diff --git a/tests/auto/declarative/qquickvisualdatamodel/data/datalist-package.qml b/tests/auto/declarative/qquickvisualdatamodel/data/datalist-package.qml
new file mode 100644 (file)
index 0000000..ae3bd81
--- /dev/null
@@ -0,0 +1,20 @@
+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/groups-package.qml b/tests/auto/declarative/qquickvisualdatamodel/data/groups-package.qml
new file mode 100644 (file)
index 0000000..ea5ad5d
--- /dev/null
@@ -0,0 +1,52 @@
+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
+            }
+        }
+    }
+
+}
index a24e223..7502dd2 100644 (file)
@@ -11,7 +11,12 @@ ListView {
         return false
     }
 
-    model: VisualDataModel {
+    model: visualModel
+    VisualDataModel {
+        id: visualModel
+
+        objectName: "visualModel"
+
         groups: [
             VisualDataGroup { id: visibleItems; objectName: "visibleItems"; name: "visible"; includeByDefault: true },
             VisualDataGroup { id: selectedItems; objectName: "selectedItems"; name: "selected" }
@@ -20,6 +25,7 @@ ListView {
         model: myModel
         delegate: Item {
             id: delegate
+
             objectName: "delegate"
             width: 100
             height: 2
index aba760d..c997ec8 100644 (file)
@@ -122,16 +122,22 @@ 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 qaimRowsMoved();
     void qaimRowsMoved_data();
+    void remove_data();
     void remove();
+    void move_data();
     void move();
+    void groups_data();
     void groups();
     void get();
     void create();
@@ -262,8 +268,18 @@ void tst_qquickvisualdatamodel::rootIndex()
     delete obj;
 }
 
+void tst_qquickvisualdatamodel::updateLayout_data()
+{
+    QTest::addColumn<QUrl>("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;
@@ -271,7 +287,7 @@ void tst_qquickvisualdatamodel::updateLayout()
 
     view.rootContext()->setContextProperty("myModel", &model);
 
-    view.setSource(QUrl::fromLocalFile(TESTDATA("datalist.qml")));
+    view.setSource(source);
 
     QQuickListView *listview = qobject_cast<QQuickListView*>(view.rootObject());
     QVERIFY(listview != 0);
@@ -302,8 +318,18 @@ void tst_qquickvisualdatamodel::updateLayout()
     QCOMPARE(name->text(), QString("Row 1 Item"));
 }
 
+void tst_qquickvisualdatamodel::childChanged_data()
+{
+    QTest::addColumn<QUrl>("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;
@@ -311,7 +337,7 @@ void tst_qquickvisualdatamodel::childChanged()
 
     view.rootContext()->setContextProperty("myModel", &model);
 
-    view.setSource(QUrl::fromLocalFile(TESTDATA("datalist.qml")));
+    view.setSource(source);
 
     QQuickListView *listview = qobject_cast<QQuickListView*>(view.rootObject());
     QVERIFY(listview != 0);
@@ -567,8 +593,18 @@ void tst_qquickvisualdatamodel::modelProperties()
     //### should also test QStringList and QVariantList
 }
 
+void tst_qquickvisualdatamodel::noDelegate_data()
+{
+    QTest::addColumn<QUrl>("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;
@@ -576,7 +612,7 @@ void tst_qquickvisualdatamodel::noDelegate()
 
     view.rootContext()->setContextProperty("myModel", &model);
 
-    view.setSource(QUrl::fromLocalFile(TESTDATA("datalist.qml")));
+    view.setSource(source);
 
     QQuickListView *listview = qobject_cast<QQuickListView*>(view.rootObject());
     QVERIFY(listview != 0);
@@ -669,6 +705,19 @@ void tst_qquickvisualdatamodel::qaimRowsMoved_data()
         << 10 << 1 << 5;
 }
 
+void tst_qquickvisualdatamodel::remove_data()
+{
+    QTest::addColumn<QUrl>("source");
+    QTest::addColumn<QString>("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;
@@ -766,6 +815,19 @@ void tst_qquickvisualdatamodel::remove()
     }
 }
 
+void tst_qquickvisualdatamodel::move_data()
+{
+    QTest::addColumn<QUrl>("source");
+    QTest::addColumn<QString>("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;
@@ -906,6 +968,18 @@ void tst_qquickvisualdatamodel::move()
     }
 }
 
+void tst_qquickvisualdatamodel::groups_data()
+{
+    QTest::addColumn<QUrl>("source");
+    QTest::addColumn<QString>("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 <int N> void tst_qquickvisualdatamodel::groups_verify(
         const SingleRoleModel &model,
@@ -921,17 +995,17 @@ template <int N> void tst_qquickvisualdatamodel::groups_verify(
     for (int i = 0; i < N; ++i) {
         QQuickItem *delegate = findItem<QQuickItem>(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]);
-        QCOMPARE(delegate->property("test4").toBool(), true);
-        QCOMPARE(delegate->property("test5").toInt() , vIndex[i]);
-        QCOMPARE(delegate->property("test6").toBool(), vMember[i]);
-        QCOMPARE(delegate->property("test7").toInt() , sIndex[i]);
-        QCOMPARE(delegate->property("test8").toBool(), sMember[i]);
-        QCOMPARE(delegate->property("test9").toStringList().contains("items")   , QBool(true));
-        QCOMPARE(delegate->property("test9").toStringList().contains("visible") , QBool(vMember[i]));
-        QCOMPARE(delegate->property("test9").toStringList().contains("selected"), QBool(sMember[i]));
+        QCOMPARE(evaluate<QString>(delegate, "test1"), model.list.at(mIndex[i]));
+        QCOMPARE(evaluate<int>(delegate, "test2") , mIndex[i]);
+        QCOMPARE(evaluate<int>(delegate, "test3") , iIndex[i]);
+        QCOMPARE(evaluate<bool>(delegate, "test4"), true);
+        QCOMPARE(evaluate<int>(delegate, "test5") , vIndex[i]);
+        QCOMPARE(evaluate<bool>(delegate, "test6"), vMember[i]);
+        QCOMPARE(evaluate<int>(delegate, "test7") , sIndex[i]);
+        QCOMPARE(evaluate<bool>(delegate, "test8"), sMember[i]);
+        QCOMPARE(evaluate<QStringList>(delegate, "test9").contains("items")   , QBool(true));
+        QCOMPARE(evaluate<QStringList>(delegate, "test9").contains("visible") , QBool(vMember[i]));
+        QCOMPARE(evaluate<QStringList>(delegate, "test9").contains("selected"), QBool(sMember[i]));
     }
     failed = false;
 }
@@ -943,6 +1017,9 @@ template <int N> void tst_qquickvisualdatamodel::groups_verify(
 
 void tst_qquickvisualdatamodel::groups()
 {
+    QFETCH(QUrl, source);
+    QFETCH(QString, part);
+
     QQuickView view;
 
     SingleRoleModel model;
@@ -963,7 +1040,7 @@ void tst_qquickvisualdatamodel::groups()
     QDeclarativeContext *ctxt = view.rootContext();
     ctxt->setContextProperty("myModel", &model);
 
-    view.setSource(QUrl::fromLocalFile(TESTDATA("groups.qml")));
+    view.setSource(source);
 
     QQuickListView *listview = qobject_cast<QQuickListView*>(view.rootObject());
     QVERIFY(listview != 0);
@@ -971,13 +1048,13 @@ void tst_qquickvisualdatamodel::groups()
     QQuickItem *contentItem = listview->contentItem();
     QVERIFY(contentItem != 0);
 
-    QQuickVisualDataModel *visualModel = qobject_cast<QQuickVisualDataModel *>(qvariant_cast<QObject *>(listview->model()));
+    QQuickVisualDataModel *visualModel = listview->findChild<QQuickVisualDataModel *>("visualModel");
     QVERIFY(visualModel);
 
-    QQuickVisualDataGroup *visibleItems = visualModel->findChild<QQuickVisualDataGroup *>("visibleItems");
+    QQuickVisualDataGroup *visibleItems = listview->findChild<QQuickVisualDataGroup *>("visibleItems");
     QVERIFY(visibleItems);
 
-    QQuickVisualDataGroup *selectedItems = visualModel->findChild<QQuickVisualDataGroup *>("selectedItems");
+    QQuickVisualDataGroup *selectedItems = listview->findChild<QQuickVisualDataGroup *>("selectedItems");
     QVERIFY(selectedItems);
 
     const bool f = false;
@@ -1130,19 +1207,22 @@ void tst_qquickvisualdatamodel::groups()
         QCOMPARE(visibleItems->count(), 9);
         QCOMPARE(selectedItems->count(), 2);
     } {
-        evaluate<void>(visualModel, "filterOnGroup = \"visible\"");
+        evaluate<void>(visualModel, part + "filterOnGroup = \"visible\"");
+        qDebug() << "listview->count()" << listview->count();
         QCOMPARE(listview->count(), 9);
         QCOMPARE(visualModel->items()->count(), 12);
         QCOMPARE(visibleItems->count(), 9);
         QCOMPARE(selectedItems->count(), 2);
     } {
-        evaluate<void>(visualModel, "filterOnGroup = \"selected\"");
+        evaluate<void>(visualModel, part + "filterOnGroup = \"selected\"");
+        qDebug() << "listview->count()" << listview->count();
         QCOMPARE(listview->count(), 2);
         QCOMPARE(visualModel->items()->count(), 12);
         QCOMPARE(visibleItems->count(), 9);
         QCOMPARE(selectedItems->count(), 2);
     } {
-        evaluate<void>(visualModel, "filterOnGroup = \"items\"");
+        evaluate<void>(visualModel, part + "filterOnGroup = \"items\"");
+        qDebug() << "listview->count()" << listview->count();
         QCOMPARE(listview->count(), 12);
         QCOMPARE(visualModel->items()->count(), 12);
         QCOMPARE(visibleItems->count(), 9);