From 69b4b690cc26b697675264298623d9974cc76796 Mon Sep 17 00:00:00 2001 From: Michael Brasser Date: Wed, 4 Nov 2009 15:03:02 +1000 Subject: More XmlListModel tests. --- .../declarative/qmlxmllistmodel/data/model.qml | 2 +- .../declarative/qmlxmllistmodel/data/model.xml | 42 +++++----- .../declarative/qmlxmllistmodel/data/recipes.qml | 10 +++ .../declarative/qmlxmllistmodel/data/recipes.xml | 90 ++++++++++++++++++++++ .../qmlxmllistmodel/tst_qmlxmllistmodel.cpp | 51 ++++++++++++ 5 files changed, 172 insertions(+), 23 deletions(-) create mode 100644 tests/auto/declarative/qmlxmllistmodel/data/recipes.qml create mode 100644 tests/auto/declarative/qmlxmllistmodel/data/recipes.xml diff --git a/tests/auto/declarative/qmlxmllistmodel/data/model.qml b/tests/auto/declarative/qmlxmllistmodel/data/model.qml index 199a447..2cbb027 100644 --- a/tests/auto/declarative/qmlxmllistmodel/data/model.qml +++ b/tests/auto/declarative/qmlxmllistmodel/data/model.qml @@ -2,7 +2,7 @@ import Qt 4.6 XmlListModel { source: "model.xml" - query: "/ListModel/ListElement" + query: "/Pets/Pet" XmlRole { name: "name"; query: "name/string()" } XmlRole { name: "type"; query: "type/string()" } XmlRole { name: "age"; query: "age/number()" } diff --git a/tests/auto/declarative/qmlxmllistmodel/data/model.xml b/tests/auto/declarative/qmlxmllistmodel/data/model.xml index f1fe742..40cd6d0 100644 --- a/tests/auto/declarative/qmlxmllistmodel/data/model.xml +++ b/tests/auto/declarative/qmlxmllistmodel/data/model.xml @@ -1,56 +1,54 @@ - - + + Polly Parrot 12 Small - - + + Penny Turtle 4 Small - - + + Warren Rabbit 2 Small - - + + Spot Dog 9 Medium - - + + Whiskers Cat 2 Medium - - + + Joey Kangaroo 1 - Medium - - + + Kimba Bunny 65 Large - - + + Rover Dog - 5 Large - - + + Tiny Elephant 15 Large - - + + diff --git a/tests/auto/declarative/qmlxmllistmodel/data/recipes.qml b/tests/auto/declarative/qmlxmllistmodel/data/recipes.qml new file mode 100644 index 0000000..13dea91 --- /dev/null +++ b/tests/auto/declarative/qmlxmllistmodel/data/recipes.qml @@ -0,0 +1,10 @@ +import Qt 4.6 + +XmlListModel { + source: "recipes.xml" + query: "/recipes/recipe" + XmlRole { name: "title"; query: "@title/string()" } + XmlRole { name: "picture"; query: "picture/string()" } + XmlRole { name: "ingredients"; query: "ingredients/string()" } + XmlRole { name: "preparation"; query: "method/string()" } +} diff --git a/tests/auto/declarative/qmlxmllistmodel/data/recipes.xml b/tests/auto/declarative/qmlxmllistmodel/data/recipes.xml new file mode 100644 index 0000000..d71de60 --- /dev/null +++ b/tests/auto/declarative/qmlxmllistmodel/data/recipes.xml @@ -0,0 +1,90 @@ + + + content/pics/pancakes.jpg + +
    +
  • 1 cup (150g) self-raising flour +
  • 1 tbs caster sugar +
  • 3/4 cup (185ml) milk +
  • 1 egg +
+ + ]]>
+ +
    +
  1. Sift flour and sugar together into a bowl. Add a pinch of salt. +
  2. Beat milk and egg together, then add to dry ingredients. Beat until smooth. +
  3. Pour mixture into a pan on medium heat and cook until bubbles appear on the surface. +
  4. Turn over and cook other side until golden. +
+ + ]]>
+
+ + content/pics/fruit-salad.jpg + + + + + content/pics/vegetable-soup.jpg + +
    +
  • 1 onion +
  • 1 turnip +
  • 1 potato +
  • 1 carrot +
  • 1 head of celery +
  • 1 1/2 litres of water +
+ + ]]>
+ +
    +
  1. Chop vegetables. +
  2. Boil in water until vegetables soften. +
  3. Season with salt and pepper to taste. +
+ + ]]>
+
+ + content/pics/hamburger.jpg + +
    +
  • 500g minced beef +
  • Seasoning +
  • lettuce, tomato, onion, cheese +
  • 1 hamburger bun for each burger +
+ + ]]>
+ +
    +
  1. Mix the beef, together with seasoning, in a food processor. +
  2. Shape the beef into burgers. +
  3. Grill the burgers for about 5 mins on each side (until cooked through) +
  4. Serve each burger on a bun with ketchup, cheese, lettuce, tomato and onion. +
+ + ]]>
+
+ + content/pics/lemonade.jpg + +
    +
  • 1 cup Lemon Juice +
  • 1 cup Sugar +
  • 6 Cups of Water (2 cups warm water, 4 cups cold water) +
+ + ]]>
+ +
    +
  1. Pour 2 cups of warm water into a pitcher and stir in sugar until it dissolves. +
  2. Pour in lemon juice, stir again, and add 4 cups of cold water. +
  3. Chill or serve over ice cubes. +
+ + ]]>
+
+
diff --git a/tests/auto/declarative/qmlxmllistmodel/tst_qmlxmllistmodel.cpp b/tests/auto/declarative/qmlxmllistmodel/tst_qmlxmllistmodel.cpp index 0fc9006..4898686 100644 --- a/tests/auto/declarative/qmlxmllistmodel/tst_qmlxmllistmodel.cpp +++ b/tests/auto/declarative/qmlxmllistmodel/tst_qmlxmllistmodel.cpp @@ -53,6 +53,9 @@ public: private slots: void buildModel(); + void missingFields(); + void cdata(); + void attributes(); private: QmlEngine engine; @@ -75,6 +78,54 @@ void tst_qmlxmllistmodel::buildModel() QCOMPARE(data.value(Qt::UserRole+3).toString(), QLatin1String("Medium")); } +void tst_qmlxmllistmodel::missingFields() +{ + QmlComponent component(&engine, QUrl("file://" SRCDIR "/data/model2.qml")); + QmlXmlListModel *listModel = qobject_cast(component.create()); + QVERIFY(listModel != 0); + QTRY_COMPARE(listModel->count(), 9); + + QList roles; + roles << Qt::UserRole << Qt::UserRole + 1 << Qt::UserRole + 2 << Qt::UserRole + 3 << Qt::UserRole + 4; + QHash data = listModel->data(5, roles); + QVERIFY(data.count() == 5); + QCOMPARE(data.value(Qt::UserRole+3).toString(), QLatin1String("")); + QCOMPARE(data.value(Qt::UserRole+4).toString(), QLatin1String("")); + + data = listModel->data(7, roles); + QVERIFY(data.count() == 5); + QCOMPARE(data.value(Qt::UserRole+2).toString(), QLatin1String("")); +} + +void tst_qmlxmllistmodel::cdata() +{ + QmlComponent component(&engine, QUrl("file://" SRCDIR "/data/recipes.qml")); + QmlXmlListModel *listModel = qobject_cast(component.create()); + QVERIFY(listModel != 0); + QTRY_COMPARE(listModel->count(), 5); + + QList roles; + roles << Qt::UserRole + 2; + QHash data = listModel->data(2, roles); + QVERIFY(data.count() == 1); + QVERIFY(data.value(Qt::UserRole+2).toString().startsWith(QLatin1String(""))); +} + +void tst_qmlxmllistmodel::attributes() +{ + QmlComponent component(&engine, QUrl("file://" SRCDIR "/data/recipes.qml")); + QmlXmlListModel *listModel = qobject_cast(component.create()); + QVERIFY(listModel != 0); + QTRY_COMPARE(listModel->count(), 5); + + QList roles; + roles << Qt::UserRole; + QHash data = listModel->data(2, roles); + QVERIFY(data.count() == 1); + QCOMPARE(data.value(Qt::UserRole).toString(), QLatin1String("Vegetable Soup")); +} + + QTEST_MAIN(tst_qmlxmllistmodel) #include "tst_qmlxmllistmodel.moc" -- cgit v0.12 From 3a369d590c04e9528265c6bf2dd33f8606df8ea5 Mon Sep 17 00:00:00 2001 From: Martin Jones Date: Wed, 4 Nov 2009 15:21:26 +1000 Subject: More listview testing and fixing. --- .../graphicsitems/qmlgraphicslistview.cpp | 29 ++++++++---- .../graphicsitems/qmlgraphicslistview_p.h | 1 + .../listview/data/listview-enforcerange.qml | 55 ++++++++++++++++++++++ tests/auto/declarative/listview/tst_listview.cpp | 51 +++++++++++++++++++- 4 files changed, 125 insertions(+), 11 deletions(-) create mode 100644 tests/auto/declarative/listview/data/listview-enforcerange.qml diff --git a/src/declarative/graphicsitems/qmlgraphicslistview.cpp b/src/declarative/graphicsitems/qmlgraphicslistview.cpp index a393cb1..efe047a 100644 --- a/src/declarative/graphicsitems/qmlgraphicslistview.cpp +++ b/src/declarative/graphicsitems/qmlgraphicslistview.cpp @@ -398,7 +398,7 @@ public: QmlComponent *highlightComponent; FxListItem *highlight; FxListItem *trackedItem; - enum MovementReason { Other, Key, Mouse }; + enum MovementReason { Other, SetIndex, Mouse }; MovementReason moveReason; int buffer; QmlEaseFollow *highlightPosAnimator; @@ -422,6 +422,7 @@ void QmlGraphicsListViewPrivate::init() q->setFlag(QGraphicsItem::ItemIsFocusScope); QObject::connect(q, SIGNAL(heightChanged()), q, SLOT(refill())); QObject::connect(q, SIGNAL(widthChanged()), q, SLOT(refill())); + QObject::connect(q, SIGNAL(movementEnded()), q, SLOT(animStopped())); } void QmlGraphicsListViewPrivate::clear() @@ -1178,8 +1179,8 @@ int QmlGraphicsListView::currentIndex() const void QmlGraphicsListView::setCurrentIndex(int index) { Q_D(QmlGraphicsListView); - d->moveReason = QmlGraphicsListViewPrivate::Other; if (d->isValid() && index != d->currentIndex && index < d->model->count() && index >= 0) { + d->moveReason = QmlGraphicsListViewPrivate::SetIndex; cancelFlick(); d->updateCurrent(index); } else { @@ -1516,18 +1517,20 @@ void QmlGraphicsListView::viewportMoved() refill(); if (isFlicking() || d->moving) d->moveReason = QmlGraphicsListViewPrivate::Mouse; - if (d->moveReason == QmlGraphicsListViewPrivate::Mouse) { + if (d->moveReason != QmlGraphicsListViewPrivate::SetIndex) { if (d->haveHighlightRange && d->highlightRange == StrictlyEnforceRange && d->highlight) { - int idx = d->snapIndex(); - if (idx >= 0 && idx != d->currentIndex) - d->updateCurrent(idx); - - qreal pos = d->currentItem->position(); + // reposition highlight + qreal pos = d->highlight->position(); if (pos > d->position() + d->highlightRangeEnd - 1 - d->highlight->size()) pos = d->position() + d->highlightRangeEnd - 1 - d->highlight->size(); if (pos < d->position() + d->highlightRangeStart) pos = d->position() + d->highlightRangeStart; d->highlight->setPosition(pos); + + // update current index + int idx = d->snapIndex(); + if (idx >= 0 && idx != d->currentIndex) + d->updateCurrent(idx); } } } @@ -1599,7 +1602,6 @@ void QmlGraphicsListView::keyPressEvent(QKeyEvent *event) if ((d->orient == QmlGraphicsListView::Horizontal && event->key() == Qt::Key_Left) || (d->orient == QmlGraphicsListView::Vertical && event->key() == Qt::Key_Up)) { if (currentIndex() > 0 || (d->wrap && !event->isAutoRepeat())) { - d->moveReason = QmlGraphicsListViewPrivate::Key; decrementCurrentIndex(); event->accept(); return; @@ -1610,7 +1612,6 @@ void QmlGraphicsListView::keyPressEvent(QKeyEvent *event) } else if ((d->orient == QmlGraphicsListView::Horizontal && event->key() == Qt::Key_Right) || (d->orient == QmlGraphicsListView::Vertical && event->key() == Qt::Key_Down)) { if (currentIndex() < d->model->count() - 1 || (d->wrap && !event->isAutoRepeat())) { - d->moveReason = QmlGraphicsListViewPrivate::Key; incrementCurrentIndex(); event->accept(); return; @@ -1634,6 +1635,7 @@ void QmlGraphicsListView::incrementCurrentIndex() { Q_D(QmlGraphicsListView); if (currentIndex() < d->model->count() - 1 || d->wrap) { + d->moveReason = QmlGraphicsListViewPrivate::SetIndex; int index = currentIndex()+1; cancelFlick(); d->updateCurrent(index < d->model->count() ? index : 0); @@ -1650,6 +1652,7 @@ void QmlGraphicsListView::decrementCurrentIndex() { Q_D(QmlGraphicsListView); if (currentIndex() > 0 || d->wrap) { + d->moveReason = QmlGraphicsListViewPrivate::SetIndex; int index = currentIndex()-1; cancelFlick(); d->updateCurrent(index >= 0 ? index : d->model->count()-1); @@ -2024,6 +2027,12 @@ void QmlGraphicsListView::destroyingItem(QmlGraphicsItem *item) d->unrequestedItems.remove(item); } +void QmlGraphicsListView::animStopped() +{ + Q_D(QmlGraphicsListView); + d->moveReason = QmlGraphicsListViewPrivate::Other; +} + QmlGraphicsListViewAttached *QmlGraphicsListView::qmlAttachedProperties(QObject *obj) { return QmlGraphicsListViewAttached::properties(obj); diff --git a/src/declarative/graphicsitems/qmlgraphicslistview_p.h b/src/declarative/graphicsitems/qmlgraphicslistview_p.h index 115cad9..446d71a 100644 --- a/src/declarative/graphicsitems/qmlgraphicslistview_p.h +++ b/src/declarative/graphicsitems/qmlgraphicslistview_p.h @@ -174,6 +174,7 @@ private Q_SLOTS: void destroyRemoved(); void createdItem(int index, QmlGraphicsItem *item); void destroyingItem(QmlGraphicsItem *item); + void animStopped(); }; QT_END_NAMESPACE diff --git a/tests/auto/declarative/listview/data/listview-enforcerange.qml b/tests/auto/declarative/listview/data/listview-enforcerange.qml new file mode 100644 index 0000000..46fddae --- /dev/null +++ b/tests/auto/declarative/listview/data/listview-enforcerange.qml @@ -0,0 +1,55 @@ +import Qt 4.6 + +Rectangle { + width: 240 + height: 320 + color: "#ffffff" + Component { + id: myDelegate + Item { + id: wrapper + objectName: "wrapper" + height: 20 + width: 240 + Text { + text: index + } + Text { + x: 30 + id: textName + objectName: "textName" + text: name + } + Text { + x: 120 + id: textNumber + objectName: "textNumber" + text: number + } + Text { + x: 200 + text: wrapper.y + } + } + } + + Component { + id: myHighlight + Rectangle { + color: "lightsteelblue" + } + } + + ListView { + id: list + objectName: "list" + width: 240 + height: 320 + model: testModel + delegate: myDelegate + highlight: myHighlight + preferredHighlightBegin: 100 + preferredHighlightEnd: 100 + highlightRangeMode: "StrictlyEnforceRange" + } +} diff --git a/tests/auto/declarative/listview/tst_listview.cpp b/tests/auto/declarative/listview/tst_listview.cpp index e61dd55..6bf1080 100644 --- a/tests/auto/declarative/listview/tst_listview.cpp +++ b/tests/auto/declarative/listview/tst_listview.cpp @@ -69,6 +69,8 @@ private slots: void qListModelInterface_moved(); void qAbstractItemModel_moved(); + void enforceRange(); + private: template void items(); template void changed(); @@ -239,12 +241,13 @@ void tst_QmlGraphicsListView::items() canvas->execute(); qApp->processEvents(); - QmlGraphicsFlickable *listview = findItem(canvas->root(), "list"); + QmlGraphicsListView *listview = findItem(canvas->root(), "list"); QVERIFY(listview != 0); QmlGraphicsItem *viewport = listview->viewport(); QVERIFY(viewport != 0); + QCOMPARE(listview->count(), model.count()); QCOMPARE(viewport->childItems().count(), model.count()+1); // assumes all are visible, +1 for the (default) highlight item for (int i = 0; i < model.count(); ++i) { @@ -256,6 +259,11 @@ void tst_QmlGraphicsListView::items() QCOMPARE(number->text(), model.number(i)); } + listview->incrementCurrentIndex(); + QCOMPARE(listview->currentIndex(), 1); + listview->decrementCurrentIndex(); + QCOMPARE(listview->currentIndex(), 0); + delete canvas; } @@ -583,6 +591,47 @@ void tst_QmlGraphicsListView::moved() delete canvas; } +void tst_QmlGraphicsListView::enforceRange() +{ + QmlView *canvas = createView(SRCDIR "/data/listview-enforcerange.qml"); + + TestModel model; + for (int i = 0; i < 30; i++) + model.addItem("Item" + QString::number(i), ""); + + QmlContext *ctxt = canvas->rootContext(); + ctxt->setContextProperty("testModel", &model); + + canvas->execute(); + qApp->processEvents(); + + QmlGraphicsListView *listview = findItem(canvas->root(), "list"); + QVERIFY(listview != 0); + + QmlGraphicsItem *viewport = listview->viewport(); + QVERIFY(viewport != 0); + + // view should be positioned at the top of the range. + QmlGraphicsItem *item = findItem(viewport, "wrapper", 0); + QVERIFY(item); + QCOMPARE(listview->viewportY(), -100.0); + + QmlGraphicsText *name = findItem(viewport, "textName", 0); + QVERIFY(name != 0); + QCOMPARE(name->text(), model.name(0)); + QmlGraphicsText *number = findItem(viewport, "textNumber", 0); + QVERIFY(number != 0); + QCOMPARE(number->text(), model.number(0)); + + // Check currentIndex is updated when viewport moves + listview->setViewportY(20); + QTest::qWait(1000); + + QCOMPARE(listview->currentIndex(), 6); + + delete canvas; +} + void tst_QmlGraphicsListView::qListModelInterface_items() { items(); -- cgit v0.12 From a59291393cc70a1f922e4796efbb72b29920da27 Mon Sep 17 00:00:00 2001 From: Yann Bodson Date: Wed, 4 Nov 2009 15:32:26 +1000 Subject: less compile warnings --- src/declarative/qml/qmlcontextscriptclass.cpp | 1 + src/declarative/qml/qmlglobalscriptclass.cpp | 19 ++++++++++--------- src/declarative/qml/qmllistscriptclass.cpp | 15 ++++++++++----- 3 files changed, 21 insertions(+), 14 deletions(-) diff --git a/src/declarative/qml/qmlcontextscriptclass.cpp b/src/declarative/qml/qmlcontextscriptclass.cpp index 4615764..fda284a 100644 --- a/src/declarative/qml/qmlcontextscriptclass.cpp +++ b/src/declarative/qml/qmlcontextscriptclass.cpp @@ -219,6 +219,7 @@ QScriptValue QmlContextScriptClass::property(Object *object, const Identifier &n void QmlContextScriptClass::setProperty(Object *object, const Identifier &name, const QScriptValue &value) { + Q_UNUSED(object); Q_ASSERT(lastScopeObject || lastDefaultObject != -1); QmlContext *bindContext = lastContext; diff --git a/src/declarative/qml/qmlglobalscriptclass.cpp b/src/declarative/qml/qmlglobalscriptclass.cpp index a9c5d3d..fd270e3 100644 --- a/src/declarative/qml/qmlglobalscriptclass.cpp +++ b/src/declarative/qml/qmlglobalscriptclass.cpp @@ -70,10 +70,10 @@ QmlGlobalScriptClass::queryProperty(const QScriptValue &object, const QScriptString &name, QueryFlags flags, uint *id) { - Q_UNUSED(object) - Q_UNUSED(name) - Q_UNUSED(flags) - Q_UNUSED(id) + Q_UNUSED(object); + Q_UNUSED(name); + Q_UNUSED(flags); + Q_UNUSED(id); return HandlesReadAccess | HandlesWriteAccess; } @@ -82,9 +82,9 @@ QmlGlobalScriptClass::property(const QScriptValue &object, const QScriptString &name, uint id) { - Q_UNUSED(object) - Q_UNUSED(name) - Q_UNUSED(id) + Q_UNUSED(object); + Q_UNUSED(name); + Q_UNUSED(id); return engine()->undefinedValue(); } @@ -92,8 +92,9 @@ void QmlGlobalScriptClass::setProperty(QScriptValue &object, const QScriptString &name, uint id, const QScriptValue &value) { - Q_UNUSED(object) - Q_UNUSED(value) + Q_UNUSED(object); + Q_UNUSED(id); + Q_UNUSED(value); QString error = QLatin1String("Invalid write to global property \"") + name.toString() + QLatin1String("\""); engine()->currentContext()->throwError(error); diff --git a/src/declarative/qml/qmllistscriptclass.cpp b/src/declarative/qml/qmllistscriptclass.cpp index f067db6..a180e49 100644 --- a/src/declarative/qml/qmllistscriptclass.cpp +++ b/src/declarative/qml/qmllistscriptclass.cpp @@ -54,6 +54,7 @@ QmlListScriptClass::QmlListScriptClass(QmlEngine *e) : QScriptDeclarativeClass(QmlEnginePrivate::getScriptEngine(e)), engine(e) { QScriptEngine *scriptEngine = QmlEnginePrivate::getScriptEngine(engine); + Q_UNUSED(scriptEngine); m_lengthId = createPersistentIdentifier(QLatin1String("length")); } @@ -81,6 +82,8 @@ QScriptClass::QueryFlags QmlListScriptClass::queryProperty(Object *object, const Identifier &name, QScriptClass::QueryFlags flags) { + Q_UNUSED(object); + Q_UNUSED(flags); if (name == m_lengthId.identifier) return QScriptClass::HandlesReadAccess; @@ -115,9 +118,11 @@ QScriptValue QmlListScriptClass::property(Object *obj, const Identifier &name) if (data->type == QListPtr) { const QList &qlist = *((QList*)list); - if (name == m_lengthId.identifier) - return qlist.count(); - else if (lastIndex < qlist.count()) + quint32 count = qlist.count(); + + if (name == m_lengthId.identifier) + return count; + else if (lastIndex < count) return enginePriv->objectClass->newQObject(qlist.at(lastIndex)); else return scriptEngine->undefinedValue(); @@ -126,9 +131,9 @@ QScriptValue QmlListScriptClass::property(Object *obj, const Identifier &name) Q_ASSERT(data->type == QmlListPtr); const QmlList &qmllist = *((QmlList*)list); - int count = qmllist.count(); + quint32 count = qmllist.count(); - if (name == m_lengthId.identifier) + if (name == m_lengthId.identifier) return count; else if (lastIndex < count) return enginePriv->objectClass->newQObject(qmllist.at(lastIndex)); -- cgit v0.12