From 397595d93ca667f5ef9597974c405e339f538d4e Mon Sep 17 00:00:00 2001 From: Martin Jones Date: Wed, 10 Feb 2010 10:08:02 +1000 Subject: Consolidate the two pathview tests and fix them all. --- tests/auto/declarative/pathview/data/pathview.qml | 66 ----- tests/auto/declarative/pathview/tst_pathview.cpp | 309 --------------------- .../qmlgraphicspathview/data/datamodel.qml | 2 +- .../qmlgraphicspathview/data/pathview.qml | 66 +++++ .../tst_qmlgraphicspathview.cpp | 93 ++++++- 5 files changed, 155 insertions(+), 381 deletions(-) delete mode 100644 tests/auto/declarative/pathview/data/pathview.qml delete mode 100644 tests/auto/declarative/pathview/tst_pathview.cpp create mode 100644 tests/auto/declarative/qmlgraphicspathview/data/pathview.qml diff --git a/tests/auto/declarative/pathview/data/pathview.qml b/tests/auto/declarative/pathview/data/pathview.qml deleted file mode 100644 index 8fa8d59..0000000 --- a/tests/auto/declarative/pathview/data/pathview.qml +++ /dev/null @@ -1,66 +0,0 @@ -import Qt 4.6 - -Rectangle { - width: 240 - height: 320 - color: "#ffffff" - resources: [ - Component { - id: delegate - Rectangle { - id: wrapper - objectName: "wrapper" - height: 20 - width: 60 - color: "white" - border.color: "black" - Text { - text: index - } - Text { - x: 20 - id: textName - objectName: "textName" - text: name - } - Text { - x: 40 - id: textNumber - objectName: "textNumber" - text: number - } - } - } - ] - PathView { - id: view - objectName: "view" - width: 240 - height: 320 - model: testModel - delegate: delegate - snapPosition: 0.01 - path: Path { - startY: 120 - startX: 160 - PathQuad { - y: 120 - x: 80 - controlY: 330 - controlX: 100 - } - PathLine { - y: 160 - x: 20 - } - PathCubic { - y: 120 - x: 160 - control1Y: 0 - control1X: 100 - control2Y: 000 - control2X: 200 - } - } - } -} diff --git a/tests/auto/declarative/pathview/tst_pathview.cpp b/tests/auto/declarative/pathview/tst_pathview.cpp deleted file mode 100644 index 360228b..0000000 --- a/tests/auto/declarative/pathview/tst_pathview.cpp +++ /dev/null @@ -1,309 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2009 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$ -** No Commercial Usage -** This file contains pre-release code and may not be distributed. -** You may use this file in accordance with the terms and conditions -** contained in the Technology Preview License Agreement accompanying -** this package. -** -** GNU Lesser General Public License Usage -** Alternatively, 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. -** -** If you have questions regarding the use of this file, please contact -** Nokia at qt-info@nokia.com. -** -** -** -** -** -** -** -** -** $QT_END_LICENSE$ -** -****************************************************************************/ -#include -#include -#include -#include -#include -#include -#include -#include - -class tst_QmlGraphicsPathView : public QObject -{ - Q_OBJECT -public: - tst_QmlGraphicsPathView(); - -private slots: - void items(); - void pathMoved(); - void limitedItems(); - -private: - QmlView *createView(const QString &filename); - template - T *findItem(QmlGraphicsItem *parent, const QString &id, int index=-1); -}; - -class TestModel : public QListModelInterface -{ - Q_OBJECT -public: - TestModel(QObject *parent = 0) : QListModelInterface(parent) {} - ~TestModel() {} - - enum Roles { Name, Number }; - - QString name(int index) const { return list.at(index).first; } - QString number(int index) const { return list.at(index).second; } - - int count() const { return list.count(); } - - QList roles() const { return QList() << Name << Number; } - QString toString(int role) const { - switch(role) { - case Name: - return "name"; - case Number: - return "number"; - default: - return ""; - } - } - - QHash data(int index, const QList &roles) const { - QHash returnHash; - - for (int i = 0; i < roles.size(); ++i) { - int role = roles.at(i); - QVariant info; - switch(role) { - case Name: - info = list.at(index).first; - break; - case Number: - info = list.at(index).second; - break; - default: - break; - } - returnHash.insert(role, info); - } - return returnHash; - } - - QVariant data(int index, int role) const { - if (role == 0) - return list.at(index).first; - if (role == 1) - return list.at(index).second; - return QVariant(); - } - - void addItem(const QString &name, const QString &number) { - list.append(QPair(name, number)); - emit itemsInserted(list.count()-1, 1); - } - - void insertItem(int index, const QString &name, const QString &number) { - list.insert(index, QPair(name, number)); - emit itemsInserted(index, 1); - } - - void removeItem(int index) { - list.removeAt(index); - emit itemsRemoved(index, 1); - } - - void modifyItem(int index, const QString &name, const QString &number) { - list[index] = QPair(name, number); - emit itemsChanged(index, 1, roles()); - } - -private: - QList > list; -}; - -tst_QmlGraphicsPathView::tst_QmlGraphicsPathView() -{ -} - -void tst_QmlGraphicsPathView::items() -{ - QmlView *canvas = createView(SRCDIR "/data/pathview.qml"); - - TestModel model; - model.addItem("Fred", "12345"); - model.addItem("John", "2345"); - model.addItem("Bob", "54321"); - - QmlContext *ctxt = canvas->rootContext(); - ctxt->setContextProperty("testModel", &model); - - canvas->execute(); - qApp->processEvents(); - - QmlGraphicsPathView *pathview = findItem(canvas->root(), "view"); - QVERIFY(pathview != 0); - - QCOMPARE(pathview->childItems().count(), model.count()); // assumes all are visible - - for (int i = 0; i < model.count(); ++i) { - QmlGraphicsText *name = findItem(pathview, "textName", i); - QVERIFY(name != 0); - QCOMPARE(name->text(), model.name(i)); - QmlGraphicsText *number = findItem(pathview, "textNumber", i); - QVERIFY(number != 0); - QCOMPARE(number->text(), model.number(i)); - } - - delete canvas; -} - -void tst_QmlGraphicsPathView::pathMoved() -{ - QmlView *canvas = createView(SRCDIR "/data/pathview.qml"); - - TestModel model; - model.addItem("Ben", "12345"); - model.addItem("Bohn", "2345"); - model.addItem("Bob", "54321"); - model.addItem("Bill", "4321"); - - QmlContext *ctxt = canvas->rootContext(); - ctxt->setContextProperty("testModel", &model); - - canvas->execute(); - qApp->processEvents(); - - QmlGraphicsPathView *pathview = findItem(canvas->root(), "view"); - QVERIFY(pathview != 0); - - QmlGraphicsRectangle *firstItem = findItem(pathview, "wrapper", 0); - QVERIFY(firstItem); - QmlGraphicsPath *path = qobject_cast(pathview->path()); - QVERIFY(path); - QPointF start = path->pointAt(0.0); - QPointF offset;//Center of item is at point, but pos is from corner - offset.setX(firstItem->width()/2); - offset.setY(firstItem->height()/2); - QCOMPARE(firstItem->pos() + offset, start); - pathview->setOffset(10); - QTest::qWait(1000);//Moving is animated? - - for(int i=0; i(pathview, "wrapper", i); - QCOMPARE(curItem->pos() + offset, path->pointAt(0.1 + i*0.25)); - } - - pathview->setOffset(100); - QTest::qWait(1000);//Moving is animated? - QCOMPARE(firstItem->pos() + offset, start); - - delete canvas; -} - -void tst_QmlGraphicsPathView::limitedItems() -{ - QmlView *canvas = createView(SRCDIR "/data/pathview.qml"); - - TestModel model; - for(int i=0; i<100; i++) - model.addItem("Bob", QString::number(i)); - - QmlContext *ctxt = canvas->rootContext(); - ctxt->setContextProperty("testModel", &model); - - canvas->execute(); - qApp->processEvents(); - - QmlGraphicsPathView *pathview = findItem(canvas->root(), "view"); - QVERIFY(pathview != 0); - - pathview->setPathItemCount(10); - QCOMPARE(pathview->pathItemCount(), 10); - - QmlGraphicsRectangle *testItem = findItem(pathview, "wrapper", 0); - QVERIFY(testItem != 0); - testItem = findItem(pathview, "wrapper", 9); - QVERIFY(testItem != 0); - testItem = findItem(pathview, "wrapper", 10); - QVERIFY(testItem == 0); - - pathview->setCurrentIndex(50); - QTest::qWait(5100);//Moving is animated and it's travelling far - should be reconsidered. - testItem = findItem(pathview, "wrapper", 0); - QVERIFY(testItem == 0); - testItem = findItem(pathview, "wrapper", 1); - QVERIFY(testItem == 0); - testItem = findItem(pathview, "wrapper", 9); - QVERIFY(testItem == 0); - testItem = findItem(pathview, "wrapper", 50); - QVERIFY(testItem != 0); -} - -QmlView *tst_QmlGraphicsPathView::createView(const QString &filename) -{ - QmlView *canvas = new QmlView(0); - canvas->setFixedSize(240,320); - - QFile file(filename); - file.open(QFile::ReadOnly); - QString qml = file.readAll(); - canvas->setQml(qml, filename); - - return canvas; -} - -/* - Find an item with the specified objectName. If index is supplied then the - item must also evaluate the {index} expression equal to index -*/ -template -T *tst_QmlGraphicsPathView::findItem(QmlGraphicsItem *parent, const QString &objectName, int index) -{ - const QMetaObject &mo = T::staticMetaObject; - for (int i = 0; i < parent->children().count(); ++i) { - QmlGraphicsItem *item = qobject_cast(parent->children().at(i)); - if(!item) - continue; - if (mo.cast(item) && (objectName.isEmpty() || item->objectName() == objectName)) { - if (index != -1) { - QmlExpression e(qmlContext(item), "index", item); - e.setTrackChange(false); - if (e.value().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_QmlGraphicsPathView) - -#include "tst_pathview.moc" diff --git a/tests/auto/declarative/qmlgraphicspathview/data/datamodel.qml b/tests/auto/declarative/qmlgraphicspathview/data/datamodel.qml index 9d08e5d..8d07db2 100644 --- a/tests/auto/declarative/qmlgraphicspathview/data/datamodel.qml +++ b/tests/auto/declarative/qmlgraphicspathview/data/datamodel.qml @@ -8,7 +8,7 @@ PathView { function checkProperties() { testObject.error = false; - if (testObject.useModel && view.model != itemModel) { + if (testObject.useModel && pathview.model != testData) { console.log("model property incorrect"); testObject.error = true; } diff --git a/tests/auto/declarative/qmlgraphicspathview/data/pathview.qml b/tests/auto/declarative/qmlgraphicspathview/data/pathview.qml new file mode 100644 index 0000000..8fa8d59 --- /dev/null +++ b/tests/auto/declarative/qmlgraphicspathview/data/pathview.qml @@ -0,0 +1,66 @@ +import Qt 4.6 + +Rectangle { + width: 240 + height: 320 + color: "#ffffff" + resources: [ + Component { + id: delegate + Rectangle { + id: wrapper + objectName: "wrapper" + height: 20 + width: 60 + color: "white" + border.color: "black" + Text { + text: index + } + Text { + x: 20 + id: textName + objectName: "textName" + text: name + } + Text { + x: 40 + id: textNumber + objectName: "textNumber" + text: number + } + } + } + ] + PathView { + id: view + objectName: "view" + width: 240 + height: 320 + model: testModel + delegate: delegate + snapPosition: 0.01 + path: Path { + startY: 120 + startX: 160 + PathQuad { + y: 120 + x: 80 + controlY: 330 + controlX: 100 + } + PathLine { + y: 160 + x: 20 + } + PathCubic { + y: 120 + x: 160 + control1Y: 0 + control1X: 100 + control2Y: 000 + control2X: 200 + } + } + } +} diff --git a/tests/auto/declarative/qmlgraphicspathview/tst_qmlgraphicspathview.cpp b/tests/auto/declarative/qmlgraphicspathview/tst_qmlgraphicspathview.cpp index e60ac5c..09b12b8 100644 --- a/tests/auto/declarative/qmlgraphicspathview/tst_qmlgraphicspathview.cpp +++ b/tests/auto/declarative/qmlgraphicspathview/tst_qmlgraphicspathview.cpp @@ -47,6 +47,7 @@ #include #include #include +#include #include #include #include @@ -60,10 +61,12 @@ public: private slots: void initValues(); + void items(); void dataModel(); void pathview2(); void pathview3(); void path(); + void pathMoved(); private: QmlView *createView(const QString &filename); @@ -186,6 +189,38 @@ void tst_QmlGraphicsPathView::initValues() QCOMPARE(obj->pathItemCount(), -1); } +void tst_QmlGraphicsPathView::items() +{ + QmlView *canvas = createView(SRCDIR "/data/pathview.qml"); + + TestModel model; + model.addItem("Fred", "12345"); + model.addItem("John", "2345"); + model.addItem("Bob", "54321"); + + QmlContext *ctxt = canvas->rootContext(); + ctxt->setContextProperty("testModel", &model); + + canvas->execute(); + qApp->processEvents(); + + QmlGraphicsPathView *pathview = findItem(canvas->root(), "view"); + QVERIFY(pathview != 0); + + QCOMPARE(pathview->childItems().count(), model.count()); // assumes all are visible + + for (int i = 0; i < model.count(); ++i) { + QmlGraphicsText *name = findItem(pathview, "textName", i); + QVERIFY(name != 0); + QCOMPARE(name->text(), model.name(i)); + QmlGraphicsText *number = findItem(pathview, "textNumber", i); + QVERIFY(number != 0); + QCOMPARE(number->text(), model.number(i)); + } + + delete canvas; +} + void tst_QmlGraphicsPathView::pathview2() { QmlEngine engine; @@ -323,6 +358,11 @@ void tst_QmlGraphicsPathView::dataModel() itemCount = findItems(pathview, "wrapper").count(); QCOMPARE(itemCount, 5); + QmlGraphicsRectangle *testItem = findItem(pathview, "wrapper", 4); + QVERIFY(testItem != 0); + testItem = findItem(pathview, "wrapper", 5); + QVERIFY(testItem == 0); + model.insertItem(2, "pink", "2"); itemCount = findItems(pathview, "wrapper").count(); @@ -342,6 +382,49 @@ void tst_QmlGraphicsPathView::dataModel() delete canvas; } +void tst_QmlGraphicsPathView::pathMoved() +{ + QmlView *canvas = createView(SRCDIR "/data/pathview.qml"); + + TestModel model; + model.addItem("Ben", "12345"); + model.addItem("Bohn", "2345"); + model.addItem("Bob", "54321"); + model.addItem("Bill", "4321"); + + QmlContext *ctxt = canvas->rootContext(); + ctxt->setContextProperty("testModel", &model); + + canvas->execute(); + qApp->processEvents(); + + QmlGraphicsPathView *pathview = findItem(canvas->root(), "view"); + QVERIFY(pathview != 0); + + QmlGraphicsRectangle *firstItem = findItem(pathview, "wrapper", 0); + QVERIFY(firstItem); + QmlGraphicsPath *path = qobject_cast(pathview->path()); + QVERIFY(path); + QPointF start = path->pointAt(0.0); + QPointF offset;//Center of item is at point, but pos is from corner + offset.setX(firstItem->width()/2); + offset.setY(firstItem->height()/2); + QCOMPARE(firstItem->pos() + offset, start); + pathview->setOffset(10); + QTest::qWait(1000);//Moving is animated? + + for(int i=0; i(pathview, "wrapper", i); + QCOMPARE(curItem->pos() + offset, path->pointAt(0.1 + i*0.25)); + } + + pathview->setOffset(100); + QTest::qWait(1000);//Moving is animated? + QCOMPARE(firstItem->pos() + offset, start); + + delete canvas; +} + QmlView *tst_QmlGraphicsPathView::createView(const QString &filename) { QmlView *canvas = new QmlView(0); @@ -363,9 +446,9 @@ template T *tst_QmlGraphicsPathView::findItem(QmlGraphicsItem *parent, const QString &objectName, int index) { const QMetaObject &mo = T::staticMetaObject; - //qDebug() << parent->QGraphicsObject::children().count() << "children"; - for (int i = 0; i < parent->QGraphicsObject::children().count(); ++i) { - QmlGraphicsItem *item = qobject_cast(parent->QGraphicsObject::children().at(i)); + //qDebug() << parent->childItems().count() << "children"; + for (int i = 0; i < parent->childItems().count(); ++i) { + QmlGraphicsItem *item = qobject_cast(parent->childItems().at(i)); if(!item) continue; //qDebug() << "try" << item; @@ -393,8 +476,8 @@ QList tst_QmlGraphicsPathView::findItems(QmlGraphicsItem *parent, const QStr QList items; const QMetaObject &mo = T::staticMetaObject; //qDebug() << parent->QGraphicsObject::children().count() << "children"; - for (int i = 0; i < parent->QGraphicsObject::children().count(); ++i) { - QmlGraphicsItem *item = qobject_cast(parent->QGraphicsObject::children().at(i)); + for (int i = 0; i < parent->childItems().count(); ++i) { + QmlGraphicsItem *item = qobject_cast(parent->childItems().at(i)); if(!item) continue; //qDebug() << "try" << item; -- cgit v0.12