From b7fe8bdad08cc87130a131c657af4cdeaa6efa79 Mon Sep 17 00:00:00 2001 From: Martin Jones Date: Tue, 17 Nov 2009 16:35:58 +1000 Subject: More repeater tests. --- .../graphicsitems/qmlgraphicsrepeater.cpp | 4 +- tests/auto/declarative/repeater/data/intmodel.qml | 18 ++- tests/auto/declarative/repeater/data/repeater2.qml | 35 +++++ tests/auto/declarative/repeater/tst_repeater.cpp | 160 ++++++++++++++++++++- 4 files changed, 211 insertions(+), 6 deletions(-) create mode 100644 tests/auto/declarative/repeater/data/repeater2.qml diff --git a/src/declarative/graphicsitems/qmlgraphicsrepeater.cpp b/src/declarative/graphicsitems/qmlgraphicsrepeater.cpp index 48758a7..7aed760 100644 --- a/src/declarative/graphicsitems/qmlgraphicsrepeater.cpp +++ b/src/declarative/graphicsitems/qmlgraphicsrepeater.cpp @@ -266,8 +266,10 @@ void QmlGraphicsRepeater::clear() { Q_D(QmlGraphicsRepeater); if (d->model) { - foreach (QmlGraphicsItem *item, d->deletables) + foreach (QmlGraphicsItem *item, d->deletables) { + item->setParentItem(this); d->model->release(item); + } } d->deletables.clear(); } diff --git a/tests/auto/declarative/repeater/data/intmodel.qml b/tests/auto/declarative/repeater/data/intmodel.qml index a779079..2d6eae8 100644 --- a/tests/auto/declarative/repeater/data/intmodel.qml +++ b/tests/auto/declarative/repeater/data/intmodel.qml @@ -6,12 +6,24 @@ Rectangle { width: 240 height: 320 color: "white" + + function checkProperties() { + testObject.error = false; + if (repeater.delegate != comp) { + print("delegate property incorrect"); + testObject.error = true; + } + } + + Component { + id: comp + Item{} + } + Repeater { id: repeater objectName: "repeater" model: testData - Component { - Item{} - } + delegate: comp } } diff --git a/tests/auto/declarative/repeater/data/repeater2.qml b/tests/auto/declarative/repeater/data/repeater2.qml new file mode 100644 index 0000000..c3c3260 --- /dev/null +++ b/tests/auto/declarative/repeater/data/repeater2.qml @@ -0,0 +1,35 @@ +import Qt 4.6 + +Rectangle { + width: 240 + height: 320 + color: "white" + Component { + id: myDelegate + Item { + objectName: "myDelegate" + height: 20 + Text { + y: index*20 + text: name + } + Text { + y: index*20 + x: 100 + text: number + } + } + } + Column { + id: container + objectName: "container" + Repeater { + id: repeater + objectName: "repeater" + width: 240 + height: 320 + delegate: myDelegate + model: testData + } + } +} diff --git a/tests/auto/declarative/repeater/tst_repeater.cpp b/tests/auto/declarative/repeater/tst_repeater.cpp index e002ee1..c0fa645 100644 --- a/tests/auto/declarative/repeater/tst_repeater.cpp +++ b/tests/auto/declarative/repeater/tst_repeater.cpp @@ -55,6 +55,8 @@ private slots: void numberModel(); void objectList(); void stringList(); + void dataModel(); + void itemModel(); private: QmlView *createView(const QString &filename); @@ -62,6 +64,91 @@ private: T *findItem(QObject *parent, const QString &id); }; +class TestObject : public QObject +{ + Q_OBJECT + + Q_PROPERTY(bool error READ error WRITE setError) + Q_PROPERTY(bool useModel READ useModel NOTIFY useModelChanged) + +public: + TestObject() : QObject(), mError(true), mUseModel(false) {} + + bool error() const { return mError; } + void setError(bool err) { mError = err; } + + bool useModel() const { return mUseModel; } + void setUseModel(bool use) { mUseModel = use; emit useModelChanged(); } + +signals: + void useModelChanged(); + +private: + bool mError; + bool mUseModel; +}; + +class TestModel : public QAbstractListModel +{ +public: + enum Roles { Name = Qt::UserRole+1, Number = Qt::UserRole+2 }; + + TestModel(QObject *parent=0) : QAbstractListModel(parent) { + QHash roles; + roles[Name] = "name"; + roles[Number] = "number"; + setRoleNames(roles); + } + + int rowCount(const QModelIndex &parent=QModelIndex()) const { return list.count(); } + QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const { + QVariant rv; + if (role == Name) + rv = list.at(index.row()).first; + else if (role == Number) + rv = list.at(index.row()).second; + + return rv; + } + + int count() const { return rowCount(); } + QString name(int index) const { return list.at(index).first; } + QString number(int index) const { return list.at(index).second; } + + void addItem(const QString &name, const QString &number) { + emit beginInsertRows(QModelIndex(), list.count(), list.count()); + list.append(QPair(name, number)); + emit endInsertRows(); + } + + void insertItem(int index, const QString &name, const QString &number) { + emit beginInsertRows(QModelIndex(), index, index); + list.insert(index, QPair(name, number)); + emit endInsertRows(); + } + + void removeItem(int index) { + emit beginRemoveRows(QModelIndex(), index, index); + list.removeAt(index); + emit endRemoveRows(); + } + + void moveItem(int from, int to) { + emit beginMoveRows(QModelIndex(), from, from, QModelIndex(), to); + list.move(from, to); + emit endMoveRows(); + } + + void modifyItem(int idx, const QString &name, const QString &number) { + list[idx] = QPair(name, number); + emit dataChanged(index(idx,0), index(idx,0)); + } + +private: + QList > list; +}; + + tst_QmlGraphicsRepeater::tst_QmlGraphicsRepeater() { } @@ -69,15 +156,23 @@ tst_QmlGraphicsRepeater::tst_QmlGraphicsRepeater() void tst_QmlGraphicsRepeater::numberModel() { QmlView *canvas = createView(SRCDIR "/data/intmodel.qml"); - canvas->execute(); - qApp->processEvents(); QmlContext *ctxt = canvas->rootContext(); ctxt->setContextProperty("testData", 5); + TestObject *testObject = new TestObject; + ctxt->setContextProperty("testObject", testObject); + + canvas->execute(); + qApp->processEvents(); QmlGraphicsRepeater *repeater = findItem(canvas->root(), "repeater"); QVERIFY(repeater != 0); QCOMPARE(repeater->parentItem()->childItems().count(), 5+1); + + QMetaObject::invokeMethod(canvas->root(), "checkProperties"); + QVERIFY(testObject->error() == false); + + delete canvas; } void tst_QmlGraphicsRepeater::objectList() @@ -159,6 +254,67 @@ void tst_QmlGraphicsRepeater::stringList() delete canvas; } +void tst_QmlGraphicsRepeater::dataModel() +{ + QmlView *canvas = createView(SRCDIR "/data/repeater2.qml"); + QmlContext *ctxt = canvas->rootContext(); + TestObject *testObject = new TestObject; + ctxt->setContextProperty("testObject", testObject); + + TestModel testModel; + testModel.addItem("one", "1"); + testModel.addItem("two", "2"); + testModel.addItem("three", "3"); + + ctxt->setContextProperty("testData", &testModel); + + canvas->execute(); + qApp->processEvents(); + + QmlGraphicsRepeater *repeater = findItem(canvas->root(), "repeater"); + QVERIFY(repeater != 0); + + QmlGraphicsItem *container = findItem(canvas->root(), "container"); + QVERIFY(container != 0); + + QCOMPARE(container->childItems().count(), 4); + + testModel.addItem("four", "4"); + + QCOMPARE(container->childItems().count(), 5); +} + +void tst_QmlGraphicsRepeater::itemModel() +{ + QmlView *canvas = createView(SRCDIR "/data/itemlist.qml"); + QmlContext *ctxt = canvas->rootContext(); + TestObject *testObject = new TestObject; + ctxt->setContextProperty("testObject", testObject); + + canvas->execute(); + qApp->processEvents(); + + QmlGraphicsRepeater *repeater = findItem(canvas->root(), "repeater"); + QVERIFY(repeater != 0); + + QmlGraphicsItem *container = findItem(canvas->root(), "container"); + QVERIFY(container != 0); + + QCOMPARE(container->childItems().count(), 1); + + testObject->setUseModel(true); + QMetaObject::invokeMethod(canvas->root(), "checkProperties"); + QVERIFY(testObject->error() == false); + + QCOMPARE(container->childItems().count(), 4); + QVERIFY(qobject_cast(container->childItems().at(0))->objectName() == "item1"); + QVERIFY(qobject_cast(container->childItems().at(1))->objectName() == "item2"); + QVERIFY(qobject_cast(container->childItems().at(2))->objectName() == "item3"); + QVERIFY(container->childItems().at(3) == repeater); + + delete canvas; +} + QmlView *tst_QmlGraphicsRepeater::createView(const QString &filename) { -- cgit v0.12