From 6dc0ba50bab2fa72978a7b5ee63dae9d66e8ad40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Lind?= Date: Wed, 11 Mar 2015 14:39:40 +0100 Subject: Repeater: Don't rely on the createFrom variable since this breaks for asynchronous models, because item creation order is not guaranteed. We always have the index for what item to create, so we do not need it either. Change-Id: Ib8ce25ac342f5cce4784c56e6a91cf70136566b3 Task-number: QTBUG-38879 Task-number: QTBUG-39001 Task-number: QTBUG-44250 Reviewed-by: Ulf Hermann --- .../quick/qquickrepeater/data/destroycount.qml | 22 ++++++++++ .../quick/qquickrepeater/tst_qquickrepeater.cpp | 51 +++++++++++++++++++++- 2 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 tests/auto/quick/qquickrepeater/data/destroycount.qml (limited to 'tests/auto/quick/qquickrepeater') diff --git a/tests/auto/quick/qquickrepeater/data/destroycount.qml b/tests/auto/quick/qquickrepeater/data/destroycount.qml new file mode 100644 index 0000000000..6aaed8545c --- /dev/null +++ b/tests/auto/quick/qquickrepeater/data/destroycount.qml @@ -0,0 +1,22 @@ +import QtQuick 2.0 + +Item { + width: 360 + height: 480 + + Repeater { + id: repeater + objectName: "repeater" + anchors.fill: parent + property var componentCount: 0 + Component { + Text { + y: index*20 + text: index + Component.onCompleted: repeater.componentCount++; + Component.onDestruction: repeater.componentCount--; + } + } + } + +} diff --git a/tests/auto/quick/qquickrepeater/tst_qquickrepeater.cpp b/tests/auto/quick/qquickrepeater/tst_qquickrepeater.cpp index 81cd87180f..b2039bd323 100644 --- a/tests/auto/quick/qquickrepeater/tst_qquickrepeater.cpp +++ b/tests/auto/quick/qquickrepeater/tst_qquickrepeater.cpp @@ -75,6 +75,7 @@ private slots: void invalidContextCrash(); void jsArrayChange(); void clearRemovalOrder(); + void destroyCount(); }; class TestObject : public QObject @@ -293,6 +294,18 @@ void tst_QQuickRepeater::dataModel_adding() QCOMPARE(addedSpy.at(0).at(1).value(), container->childItems().at(2)); addedSpy.clear(); + //insert in middle multiple + int childItemsSize = container->childItems().size(); + QList > multiData; + multiData << qMakePair(QStringLiteral("five"), QStringLiteral("5")) << qMakePair(QStringLiteral("six"), QStringLiteral("6")) << qMakePair(QStringLiteral("seven"), QStringLiteral("7")); + testModel.insertItems(1, multiData); + QCOMPARE(countSpy.count(), 1); + QCOMPARE(addedSpy.count(), 3); + QCOMPARE(container->childItems().size(), childItemsSize + 3); + QCOMPARE(repeater->itemAt(2), container->childItems().at(2)); + addedSpy.clear(); + countSpy.clear(); + delete testObject; addedSpy.clear(); countSpy.clear(); @@ -676,7 +689,8 @@ void tst_QQuickRepeater::asynchronous() } // items will be created one at a time - for (int i = 0; i < 10; ++i) { + // the order is incubator/model specific + for (int i = 9; i >= 0; --i) { QString name("delegate"); name += QString::number(i); QVERIFY(findItem(container, name) == 0); @@ -845,6 +859,41 @@ void tst_QQuickRepeater::clearRemovalOrder() delete rootObject; } +void tst_QQuickRepeater::destroyCount() +{ + QQmlEngine engine; + QQmlComponent component(&engine, testFileUrl("destroycount.qml")); + + QQuickItem *rootObject = qobject_cast(component.create()); + QVERIFY(rootObject); + + QQuickRepeater *repeater = findItem(rootObject, "repeater"); + QVERIFY(repeater); + + repeater->setProperty("model", qVariantFromValue(3)); + QCOMPARE(repeater->property("componentCount").toInt(), 3); + repeater->setProperty("model", qVariantFromValue(0)); + QCOMPARE(repeater->property("componentCount").toInt(), 0); + repeater->setProperty("model", qVariantFromValue(4)); + QCOMPARE(repeater->property("componentCount").toInt(), 4); + + QStringListModel model; + repeater->setProperty("model", qVariantFromValue(&model)); + QCOMPARE(repeater->property("componentCount").toInt(), 0); + QStringList list; + list << "1" << "2" << "3" << "4"; + model.setStringList(list); + QCOMPARE(repeater->property("componentCount").toInt(), 4); + model.insertRows(2,1); + QModelIndex index = model.index(2); + model.setData(index, qVariantFromValue(QStringLiteral("foobar"))); + QCOMPARE(repeater->property("componentCount").toInt(), 5); + + model.removeRows(2,1); + QCOMPARE(model.rowCount(), 4); + QCOMPARE(repeater->property("componentCount").toInt(), 4); +} + QTEST_MAIN(tst_QQuickRepeater) #include "tst_qquickrepeater.moc" -- cgit v1.2.3