From 74d768ec05d5acfa8ef5b65535c908f2935f2783 Mon Sep 17 00:00:00 2001 From: Martin Jones Date: Fri, 6 Nov 2009 14:04:45 +1000 Subject: move ListView state to correct directory --- tests/auto/declarative/declarative.pro | 2 +- .../listview/data/listview-enforcerange.qml | 55 -- .../listview/data/listview-sections.qml | 59 -- tests/auto/declarative/listview/data/listview.qml | 46 -- tests/auto/declarative/listview/listview.pro | 8 - tests/auto/declarative/listview/tst_listview.cpp | 853 --------------------- .../data/listview-enforcerange.qml | 55 ++ .../qmlgraphicslistview/data/listview-sections.qml | 59 ++ .../qmlgraphicslistview/data/listview.qml | 46 ++ .../qmlgraphicslistview/qmlgraphicslistview.pro | 8 + .../tst_qmlgraphicslistview.cpp | 853 +++++++++++++++++++++ 11 files changed, 1022 insertions(+), 1022 deletions(-) delete mode 100644 tests/auto/declarative/listview/data/listview-enforcerange.qml delete mode 100644 tests/auto/declarative/listview/data/listview-sections.qml delete mode 100644 tests/auto/declarative/listview/data/listview.qml delete mode 100644 tests/auto/declarative/listview/listview.pro delete mode 100644 tests/auto/declarative/listview/tst_listview.cpp create mode 100644 tests/auto/declarative/qmlgraphicslistview/data/listview-enforcerange.qml create mode 100644 tests/auto/declarative/qmlgraphicslistview/data/listview-sections.qml create mode 100644 tests/auto/declarative/qmlgraphicslistview/data/listview.qml create mode 100644 tests/auto/declarative/qmlgraphicslistview/qmlgraphicslistview.pro create mode 100644 tests/auto/declarative/qmlgraphicslistview/tst_qmlgraphicslistview.cpp diff --git a/tests/auto/declarative/declarative.pro b/tests/auto/declarative/declarative.pro index 82b135f..64672f6 100644 --- a/tests/auto/declarative/declarative.pro +++ b/tests/auto/declarative/declarative.pro @@ -7,7 +7,7 @@ SUBDIRS += \ datetimeformatter \ # Cover examples \ layouts \ # Cover - listview \ # Cover + qmlgraphicslistview \ # Cover qmlgraphicsgridview \ # Cover numberformatter \ # Cover pathview \ # Cover diff --git a/tests/auto/declarative/listview/data/listview-enforcerange.qml b/tests/auto/declarative/listview/data/listview-enforcerange.qml deleted file mode 100644 index 46fddae..0000000 --- a/tests/auto/declarative/listview/data/listview-enforcerange.qml +++ /dev/null @@ -1,55 +0,0 @@ -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/data/listview-sections.qml b/tests/auto/declarative/listview/data/listview-sections.qml deleted file mode 100644 index 56700be..0000000 --- a/tests/auto/declarative/listview/data/listview-sections.qml +++ /dev/null @@ -1,59 +0,0 @@ -import Qt 4.6 - -Rectangle { - width: 240 - height: 320 - color: "#ffffff" - resources: [ - Component { - id: myDelegate - Item { - id: wrapper - objectName: "wrapper" - height: ListView.prevSection != ListView.section ? 40 : 20; - width: 240 - Rectangle { - y: wrapper.ListView.prevSection != wrapper.ListView.section ? 20 : 0 - height: 20 - width: parent.width - color: wrapper.ListView.isCurrentItem ? "lightsteelblue" : "white" - 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 - } - } - Rectangle { - color: "#99bb99" - height: wrapper.ListView.prevSection != wrapper.ListView.section ? 20 : 0 - width: parent.width - visible: wrapper.ListView.prevSection != wrapper.ListView.section ? true : false - Text { text: wrapper.ListView.section } - } - } - } - ] - ListView { - id: list - objectName: "list" - width: 240 - height: 320 - model: testModel - delegate: myDelegate - sectionExpression: "Math.floor(index/5)" - } -} diff --git a/tests/auto/declarative/listview/data/listview.qml b/tests/auto/declarative/listview/data/listview.qml deleted file mode 100644 index b7b838b..0000000 --- a/tests/auto/declarative/listview/data/listview.qml +++ /dev/null @@ -1,46 +0,0 @@ -import Qt 4.6 - -Rectangle { - width: 240 - height: 320 - color: "#ffffff" - resources: [ - Component { - id: myDelegate - Rectangle { - 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 - } - color: ListView.isCurrentItem ? "lightsteelblue" : "white" - } - } - ] - ListView { - id: list - objectName: "list" - width: 240 - height: 320 - model: testModel - delegate: myDelegate - } -} diff --git a/tests/auto/declarative/listview/listview.pro b/tests/auto/declarative/listview/listview.pro deleted file mode 100644 index 23b0706..0000000 --- a/tests/auto/declarative/listview/listview.pro +++ /dev/null @@ -1,8 +0,0 @@ -load(qttest_p4) -contains(QT_CONFIG,declarative): QT += declarative -macx:CONFIG -= app_bundle - -SOURCES += tst_listview.cpp - -# Define SRCDIR equal to test's source directory -DEFINES += SRCDIR=\\\"$$PWD\\\" diff --git a/tests/auto/declarative/listview/tst_listview.cpp b/tests/auto/declarative/listview/tst_listview.cpp deleted file mode 100644 index 5575ace..0000000 --- a/tests/auto/declarative/listview/tst_listview.cpp +++ /dev/null @@ -1,853 +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 - -class tst_QmlGraphicsListView : public QObject -{ - Q_OBJECT -public: - tst_QmlGraphicsListView(); - -private slots: - // Test both QListModelInterface and QAbstractItemModel model types - void qListModelInterface_items(); - void qAbstractItemModel_items(); - - void qListModelInterface_changed(); - void qAbstractItemModel_changed(); - - void qListModelInterface_inserted(); - void qAbstractItemModel_inserted(); - - void qListModelInterface_removed(); - void qAbstractItemModel_removed(); - - void qListModelInterface_moved(); - void qAbstractItemModel_moved(); - - void enforceRange(); - void spacing(); - void sections(); - -private: - template void items(); - template void changed(); - template void inserted(); - template void removed(); - template void moved(); - QmlView *createView(const QString &filename); - template - T *findItem(QmlGraphicsItem *parent, const QString &id, int index=-1); - template - QList findItems(QmlGraphicsItem *parent, const QString &objectName); -}; - -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; - } - - 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 moveItem(int from, int to) { - list.move(from, to); - emit itemsMoved(from, to, 1); - } - - void modifyItem(int index, const QString &name, const QString &number) { - list[index] = QPair(name, number); - emit itemsChanged(index, 1, roles()); - } - -private: - QList > list; -}; - - -class TestModel2 : public QAbstractListModel -{ -public: - enum Roles { Name = Qt::UserRole+1, Number = Qt::UserRole+2 }; - - TestModel2(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_QmlGraphicsListView::tst_QmlGraphicsListView() -{ -} - -template -void tst_QmlGraphicsListView::items() -{ - QmlView *canvas = createView(SRCDIR "/data/listview.qml"); - - T 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(); - - 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) { - QmlGraphicsText *name = findItem(viewport, "textName", i); - QVERIFY(name != 0); - QCOMPARE(name->text(), model.name(i)); - QmlGraphicsText *number = findItem(viewport, "textNumber", i); - QVERIFY(number != 0); - QCOMPARE(number->text(), model.number(i)); - } - - listview->incrementCurrentIndex(); - QCOMPARE(listview->currentIndex(), 1); - listview->decrementCurrentIndex(); - QCOMPARE(listview->currentIndex(), 0); - - // set an empty model and confirm that items are destroyed - T model2; - ctxt->setContextProperty("testModel", &model2); - - int itemCount = findItems(viewport, "wrapper").count(); - QVERIFY(itemCount == 0); - - delete canvas; -} - -template -void tst_QmlGraphicsListView::changed() -{ - QmlView *canvas = createView(SRCDIR "/data/listview.qml"); - - T 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(); - - QmlGraphicsFlickable *listview = findItem(canvas->root(), "list"); - QVERIFY(listview != 0); - - QmlGraphicsItem *viewport = listview->viewport(); - QVERIFY(viewport != 0); - - model.modifyItem(1, "Will", "9876"); - QmlGraphicsText *name = findItem(viewport, "textName", 1); - QVERIFY(name != 0); - QCOMPARE(name->text(), model.name(1)); - QmlGraphicsText *number = findItem(viewport, "textNumber", 1); - QVERIFY(number != 0); - QCOMPARE(number->text(), model.number(1)); - - delete canvas; -} - -template -void tst_QmlGraphicsListView::inserted() -{ - QmlView *canvas = createView(SRCDIR "/data/listview.qml"); - - T 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(); - - QmlGraphicsListView *listview = findItem(canvas->root(), "list"); - QVERIFY(listview != 0); - - QmlGraphicsItem *viewport = listview->viewport(); - QVERIFY(viewport != 0); - - model.insertItem(1, "Will", "9876"); - - // let transitions settle. - QTest::qWait(1000); - - QCOMPARE(viewport->childItems().count(), model.count()+1); // assumes all are visible, +1 for the (default) highlight item - - QmlGraphicsText *name = findItem(viewport, "textName", 1); - QVERIFY(name != 0); - QCOMPARE(name->text(), model.name(1)); - QmlGraphicsText *number = findItem(viewport, "textNumber", 1); - QVERIFY(number != 0); - QCOMPARE(number->text(), model.number(1)); - - // Confirm items positioned correctly - for (int i = 0; i < model.count(); ++i) { - QmlGraphicsItem *item = findItem(viewport, "wrapper", i); - QVERIFY(item->y() == i*20); - } - - model.insertItem(0, "Foo", "1111"); // zero index, and current item - - // let transitions settle. - QTest::qWait(1000); - - QCOMPARE(viewport->childItems().count(), model.count()+1); // assumes all are visible, +1 for the (default) highlight item - - name = findItem(viewport, "textName", 0); - QVERIFY(name != 0); - QCOMPARE(name->text(), model.name(0)); - number = findItem(viewport, "textNumber", 0); - QVERIFY(number != 0); - QCOMPARE(number->text(), model.number(0)); - - QCOMPARE(listview->currentIndex(), 1); - - // Confirm items positioned correctly - for (int i = 0; i < model.count(); ++i) { - QmlGraphicsItem *item = findItem(viewport, "wrapper", i); - QVERIFY(item->y() == i*20); - } - - for (int i = model.count(); i < 30; ++i) - model.insertItem(i, "Hello", QString::number(i)); - QTest::qWait(1000); - - listview->setViewportY(80); - QTest::qWait(1000); - - // Insert item outside visible area - model.insertItem(1, "Hello", "1324"); - QTest::qWait(1000); - - QVERIFY(listview->viewportY() == 80); - - // Confirm items positioned correctly - int itemCount = findItems(viewport, "wrapper").count() - 1; - for (int i = 5; i < 5+itemCount; ++i) { - QmlGraphicsItem *item = findItem(viewport, "wrapper", i); - QVERIFY(item->y() == i*20 - 20); - } - - delete canvas; -} - -template -void tst_QmlGraphicsListView::removed() -{ - QmlView *canvas = createView(SRCDIR "/data/listview.qml"); - - T 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); - - model.removeItem(1); - - // let transitions settle. - QTest::qWait(1000); - - QmlGraphicsText *name = findItem(viewport, "textName", 1); - QVERIFY(name != 0); - QCOMPARE(name->text(), model.name(1)); - QmlGraphicsText *number = findItem(viewport, "textNumber", 1); - QVERIFY(number != 0); - QCOMPARE(number->text(), model.number(1)); - - // Confirm items positioned correctly - int itemCount = findItems(viewport, "wrapper").count(); - for (int i = 0; i < model.count() && i < itemCount; ++i) { - QmlGraphicsItem *item = findItem(viewport, "wrapper", i); - if (!item) qWarning() << "Item" << i << "not found"; - QVERIFY(item); - QVERIFY(item->y() == i*20); - } - - // Remove first item (which is the current item); - model.removeItem(0); // post: top item starts at 20 - - // let transitions settle. - QTest::qWait(1000); - - name = findItem(viewport, "textName", 0); - QVERIFY(name != 0); - QCOMPARE(name->text(), model.name(0)); - number = findItem(viewport, "textNumber", 0); - QVERIFY(number != 0); - QCOMPARE(number->text(), model.number(0)); - - // Confirm items positioned correctly - itemCount = findItems(viewport, "wrapper").count(); - for (int i = 0; i < model.count() && i < itemCount; ++i) { - QmlGraphicsItem *item = findItem(viewport, "wrapper", i); - if (!item) qWarning() << "Item" << i << "not found"; - QVERIFY(item); - QCOMPARE(item->y(),i*20.0 + 20.0); - } - - // Remove items not visible - model.removeItem(18); - // let transitions settle. - QTest::qWait(1000); - - // Confirm items positioned correctly - itemCount = findItems(viewport, "wrapper").count(); - for (int i = 0; i < model.count() && i < itemCount; ++i) { - QmlGraphicsItem *item = findItem(viewport, "wrapper", i); - if (!item) qWarning() << "Item" << i << "not found"; - QVERIFY(item); - QCOMPARE(item->y(),i*20.0+20.0); - } - - // Remove items before visible - listview->setViewportY(80); - listview->setCurrentIndex(10); - - model.removeItem(1); // post: top item will be at 40 - // let transitions settle. - QTest::qWait(1000); - - // Confirm items positioned correctly - for (int i = 2; i < 18; ++i) { - QmlGraphicsItem *item = findItem(viewport, "wrapper", i); - if (!item) qWarning() << "Item" << i << "not found"; - QVERIFY(item); - QCOMPARE(item->y(),40+i*20.0); - } - - listview->setViewportY(40); // That's the top now - // let transitions settle. - QTest::qWait(1000); - - // Confirm items positioned correctly - itemCount = findItems(viewport, "wrapper").count(); - for (int i = 0; i < model.count() && i < itemCount; ++i) { - QmlGraphicsItem *item = findItem(viewport, "wrapper", i); - if (!item) qWarning() << "Item" << i << "not found"; - QVERIFY(item); - QCOMPARE(item->y(),40+i*20.0); - } - - delete canvas; -} - -template -void tst_QmlGraphicsListView::moved() -{ - QmlView *canvas = createView(SRCDIR "/data/listview.qml"); - - T 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); - - model.moveItem(1, 4); - - // let transitions settle. - QTest::qWait(1000); - - QmlGraphicsText *name = findItem(viewport, "textName", 1); - QVERIFY(name != 0); - QCOMPARE(name->text(), model.name(1)); - QmlGraphicsText *number = findItem(viewport, "textNumber", 1); - QVERIFY(number != 0); - QCOMPARE(number->text(), model.number(1)); - - name = findItem(viewport, "textName", 4); - QVERIFY(name != 0); - QCOMPARE(name->text(), model.name(4)); - number = findItem(viewport, "textNumber", 4); - QVERIFY(number != 0); - QCOMPARE(number->text(), model.number(4)); - - // Confirm items positioned correctly - int itemCount = findItems(viewport, "wrapper").count(); - for (int i = 0; i < model.count() && i < itemCount; ++i) { - QmlGraphicsItem *item = findItem(viewport, "wrapper", i); - if (!item) qWarning() << "Item" << i << "not found"; - QVERIFY(item); - QVERIFY(item->y() == i*20); - } - - listview->setViewportY(80); - - // move outside visible area - model.moveItem(1, 18); - - // let transitions settle. - QTest::qWait(1000); - - // Confirm items positioned correctly and indexes correct - for (int i = 3; i < model.count() && i < itemCount; ++i) { - QmlGraphicsItem *item = findItem(viewport, "wrapper", i); - if (!item) qWarning() << "Item" << i << "not found"; - QVERIFY(item); - QVERIFY(item->y() == i*20 + 20); - name = findItem(viewport, "textName", i); - QVERIFY(name != 0); - QCOMPARE(name->text(), model.name(i)); - number = findItem(viewport, "textNumber", i); - QVERIFY(number != 0); - QCOMPARE(number->text(), model.number(i)); - } - - // move from outside visible into visible - model.moveItem(20, 4); - - // let transitions settle. - QTest::qWait(1000); - - // Confirm items positioned correctly and indexes correct - for (int i = 3; i < model.count() && i < itemCount; ++i) { - QmlGraphicsItem *item = findItem(viewport, "wrapper", i); - if (!item) qWarning() << "Item" << i << "not found"; - QVERIFY(item); - QVERIFY(item->y() == i*20 + 20); - name = findItem(viewport, "textName", i); - QVERIFY(name != 0); - QCOMPARE(name->text(), model.name(i)); - number = findItem(viewport, "textNumber", i); - QVERIFY(number != 0); - QCOMPARE(number->text(), model.number(i)); - } - - 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::spacing() -{ - QmlView *canvas = createView(SRCDIR "/data/listview.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); - - // Confirm items positioned correctly - int itemCount = findItems(viewport, "wrapper").count(); - for (int i = 0; i < model.count() && i < itemCount; ++i) { - QmlGraphicsItem *item = findItem(viewport, "wrapper", i); - if (!item) qWarning() << "Item" << i << "not found"; - QVERIFY(item); - QVERIFY(item->y() == i*20); - } - - listview->setSpacing(10); - QVERIFY(listview->spacing() == 10); - - // Confirm items positioned correctly - itemCount = findItems(viewport, "wrapper").count(); - for (int i = 0; i < model.count() && i < itemCount; ++i) { - QmlGraphicsItem *item = findItem(viewport, "wrapper", i); - if (!item) qWarning() << "Item" << i << "not found"; - QVERIFY(item); - QVERIFY(item->y() == i*30); - } - - listview->setSpacing(0); - - // Confirm items positioned correctly - itemCount = findItems(viewport, "wrapper").count(); - for (int i = 0; i < model.count() && i < itemCount; ++i) { - QmlGraphicsItem *item = findItem(viewport, "wrapper", i); - if (!item) qWarning() << "Item" << i << "not found"; - QVERIFY(item); - QVERIFY(item->y() == i*20); - } - - delete canvas; -} - -void tst_QmlGraphicsListView::sections() -{ - QmlView *canvas = createView(SRCDIR "/data/listview-sections.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); - - // Confirm items positioned correctly - int itemCount = findItems(viewport, "wrapper").count(); - for (int i = 0; i < model.count() && i < itemCount; ++i) { - QmlGraphicsItem *item = findItem(viewport, "wrapper", i); - QVERIFY(item); - QCOMPARE(item->y(), qreal(i*20 + ((i+4)/5) * 20)); - } - - QVERIFY(listview->currentSection() == "0"); - - listview->setViewportY(140); - QVERIFY(listview->currentSection() == "1"); - - delete canvas; -} - -void tst_QmlGraphicsListView::qListModelInterface_items() -{ - items(); -} - -void tst_QmlGraphicsListView::qAbstractItemModel_items() -{ - items(); -} - -void tst_QmlGraphicsListView::qListModelInterface_changed() -{ - changed(); -} - -void tst_QmlGraphicsListView::qAbstractItemModel_changed() -{ - changed(); -} - -void tst_QmlGraphicsListView::qListModelInterface_inserted() -{ - inserted(); -} - -void tst_QmlGraphicsListView::qAbstractItemModel_inserted() -{ - inserted(); -} - -void tst_QmlGraphicsListView::qListModelInterface_removed() -{ - removed(); -} - -void tst_QmlGraphicsListView::qAbstractItemModel_removed() -{ - removed(); -} - -void tst_QmlGraphicsListView::qListModelInterface_moved() -{ - moved(); -} - -void tst_QmlGraphicsListView::qAbstractItemModel_moved() -{ - moved(); -} - - -QmlView *tst_QmlGraphicsListView::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_QmlGraphicsListView::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)); - if(!item) - continue; - //qDebug() << "try" << item; - 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; -} - -template -QList tst_QmlGraphicsListView::findItems(QmlGraphicsItem *parent, const QString &objectName) -{ - 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)); - if(!item) - continue; - //qDebug() << "try" << item; - if (mo.cast(item) && (objectName.isEmpty() || item->objectName() == objectName)) - items.append(static_cast(item)); - items += findItems(item, objectName); - } - - return items; -} - - -QTEST_MAIN(tst_QmlGraphicsListView) - -#include "tst_listview.moc" diff --git a/tests/auto/declarative/qmlgraphicslistview/data/listview-enforcerange.qml b/tests/auto/declarative/qmlgraphicslistview/data/listview-enforcerange.qml new file mode 100644 index 0000000..46fddae --- /dev/null +++ b/tests/auto/declarative/qmlgraphicslistview/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/qmlgraphicslistview/data/listview-sections.qml b/tests/auto/declarative/qmlgraphicslistview/data/listview-sections.qml new file mode 100644 index 0000000..56700be --- /dev/null +++ b/tests/auto/declarative/qmlgraphicslistview/data/listview-sections.qml @@ -0,0 +1,59 @@ +import Qt 4.6 + +Rectangle { + width: 240 + height: 320 + color: "#ffffff" + resources: [ + Component { + id: myDelegate + Item { + id: wrapper + objectName: "wrapper" + height: ListView.prevSection != ListView.section ? 40 : 20; + width: 240 + Rectangle { + y: wrapper.ListView.prevSection != wrapper.ListView.section ? 20 : 0 + height: 20 + width: parent.width + color: wrapper.ListView.isCurrentItem ? "lightsteelblue" : "white" + 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 + } + } + Rectangle { + color: "#99bb99" + height: wrapper.ListView.prevSection != wrapper.ListView.section ? 20 : 0 + width: parent.width + visible: wrapper.ListView.prevSection != wrapper.ListView.section ? true : false + Text { text: wrapper.ListView.section } + } + } + } + ] + ListView { + id: list + objectName: "list" + width: 240 + height: 320 + model: testModel + delegate: myDelegate + sectionExpression: "Math.floor(index/5)" + } +} diff --git a/tests/auto/declarative/qmlgraphicslistview/data/listview.qml b/tests/auto/declarative/qmlgraphicslistview/data/listview.qml new file mode 100644 index 0000000..b7b838b --- /dev/null +++ b/tests/auto/declarative/qmlgraphicslistview/data/listview.qml @@ -0,0 +1,46 @@ +import Qt 4.6 + +Rectangle { + width: 240 + height: 320 + color: "#ffffff" + resources: [ + Component { + id: myDelegate + Rectangle { + 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 + } + color: ListView.isCurrentItem ? "lightsteelblue" : "white" + } + } + ] + ListView { + id: list + objectName: "list" + width: 240 + height: 320 + model: testModel + delegate: myDelegate + } +} diff --git a/tests/auto/declarative/qmlgraphicslistview/qmlgraphicslistview.pro b/tests/auto/declarative/qmlgraphicslistview/qmlgraphicslistview.pro new file mode 100644 index 0000000..f00de39 --- /dev/null +++ b/tests/auto/declarative/qmlgraphicslistview/qmlgraphicslistview.pro @@ -0,0 +1,8 @@ +load(qttest_p4) +contains(QT_CONFIG,declarative): QT += declarative +macx:CONFIG -= app_bundle + +SOURCES += tst_qmlgraphicslistview.cpp + +# Define SRCDIR equal to test's source directory +DEFINES += SRCDIR=\\\"$$PWD\\\" diff --git a/tests/auto/declarative/qmlgraphicslistview/tst_qmlgraphicslistview.cpp b/tests/auto/declarative/qmlgraphicslistview/tst_qmlgraphicslistview.cpp new file mode 100644 index 0000000..5575ace --- /dev/null +++ b/tests/auto/declarative/qmlgraphicslistview/tst_qmlgraphicslistview.cpp @@ -0,0 +1,853 @@ +/**************************************************************************** +** +** 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 + +class tst_QmlGraphicsListView : public QObject +{ + Q_OBJECT +public: + tst_QmlGraphicsListView(); + +private slots: + // Test both QListModelInterface and QAbstractItemModel model types + void qListModelInterface_items(); + void qAbstractItemModel_items(); + + void qListModelInterface_changed(); + void qAbstractItemModel_changed(); + + void qListModelInterface_inserted(); + void qAbstractItemModel_inserted(); + + void qListModelInterface_removed(); + void qAbstractItemModel_removed(); + + void qListModelInterface_moved(); + void qAbstractItemModel_moved(); + + void enforceRange(); + void spacing(); + void sections(); + +private: + template void items(); + template void changed(); + template void inserted(); + template void removed(); + template void moved(); + QmlView *createView(const QString &filename); + template + T *findItem(QmlGraphicsItem *parent, const QString &id, int index=-1); + template + QList findItems(QmlGraphicsItem *parent, const QString &objectName); +}; + +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; + } + + 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 moveItem(int from, int to) { + list.move(from, to); + emit itemsMoved(from, to, 1); + } + + void modifyItem(int index, const QString &name, const QString &number) { + list[index] = QPair(name, number); + emit itemsChanged(index, 1, roles()); + } + +private: + QList > list; +}; + + +class TestModel2 : public QAbstractListModel +{ +public: + enum Roles { Name = Qt::UserRole+1, Number = Qt::UserRole+2 }; + + TestModel2(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_QmlGraphicsListView::tst_QmlGraphicsListView() +{ +} + +template +void tst_QmlGraphicsListView::items() +{ + QmlView *canvas = createView(SRCDIR "/data/listview.qml"); + + T 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(); + + 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) { + QmlGraphicsText *name = findItem(viewport, "textName", i); + QVERIFY(name != 0); + QCOMPARE(name->text(), model.name(i)); + QmlGraphicsText *number = findItem(viewport, "textNumber", i); + QVERIFY(number != 0); + QCOMPARE(number->text(), model.number(i)); + } + + listview->incrementCurrentIndex(); + QCOMPARE(listview->currentIndex(), 1); + listview->decrementCurrentIndex(); + QCOMPARE(listview->currentIndex(), 0); + + // set an empty model and confirm that items are destroyed + T model2; + ctxt->setContextProperty("testModel", &model2); + + int itemCount = findItems(viewport, "wrapper").count(); + QVERIFY(itemCount == 0); + + delete canvas; +} + +template +void tst_QmlGraphicsListView::changed() +{ + QmlView *canvas = createView(SRCDIR "/data/listview.qml"); + + T 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(); + + QmlGraphicsFlickable *listview = findItem(canvas->root(), "list"); + QVERIFY(listview != 0); + + QmlGraphicsItem *viewport = listview->viewport(); + QVERIFY(viewport != 0); + + model.modifyItem(1, "Will", "9876"); + QmlGraphicsText *name = findItem(viewport, "textName", 1); + QVERIFY(name != 0); + QCOMPARE(name->text(), model.name(1)); + QmlGraphicsText *number = findItem(viewport, "textNumber", 1); + QVERIFY(number != 0); + QCOMPARE(number->text(), model.number(1)); + + delete canvas; +} + +template +void tst_QmlGraphicsListView::inserted() +{ + QmlView *canvas = createView(SRCDIR "/data/listview.qml"); + + T 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(); + + QmlGraphicsListView *listview = findItem(canvas->root(), "list"); + QVERIFY(listview != 0); + + QmlGraphicsItem *viewport = listview->viewport(); + QVERIFY(viewport != 0); + + model.insertItem(1, "Will", "9876"); + + // let transitions settle. + QTest::qWait(1000); + + QCOMPARE(viewport->childItems().count(), model.count()+1); // assumes all are visible, +1 for the (default) highlight item + + QmlGraphicsText *name = findItem(viewport, "textName", 1); + QVERIFY(name != 0); + QCOMPARE(name->text(), model.name(1)); + QmlGraphicsText *number = findItem(viewport, "textNumber", 1); + QVERIFY(number != 0); + QCOMPARE(number->text(), model.number(1)); + + // Confirm items positioned correctly + for (int i = 0; i < model.count(); ++i) { + QmlGraphicsItem *item = findItem(viewport, "wrapper", i); + QVERIFY(item->y() == i*20); + } + + model.insertItem(0, "Foo", "1111"); // zero index, and current item + + // let transitions settle. + QTest::qWait(1000); + + QCOMPARE(viewport->childItems().count(), model.count()+1); // assumes all are visible, +1 for the (default) highlight item + + name = findItem(viewport, "textName", 0); + QVERIFY(name != 0); + QCOMPARE(name->text(), model.name(0)); + number = findItem(viewport, "textNumber", 0); + QVERIFY(number != 0); + QCOMPARE(number->text(), model.number(0)); + + QCOMPARE(listview->currentIndex(), 1); + + // Confirm items positioned correctly + for (int i = 0; i < model.count(); ++i) { + QmlGraphicsItem *item = findItem(viewport, "wrapper", i); + QVERIFY(item->y() == i*20); + } + + for (int i = model.count(); i < 30; ++i) + model.insertItem(i, "Hello", QString::number(i)); + QTest::qWait(1000); + + listview->setViewportY(80); + QTest::qWait(1000); + + // Insert item outside visible area + model.insertItem(1, "Hello", "1324"); + QTest::qWait(1000); + + QVERIFY(listview->viewportY() == 80); + + // Confirm items positioned correctly + int itemCount = findItems(viewport, "wrapper").count() - 1; + for (int i = 5; i < 5+itemCount; ++i) { + QmlGraphicsItem *item = findItem(viewport, "wrapper", i); + QVERIFY(item->y() == i*20 - 20); + } + + delete canvas; +} + +template +void tst_QmlGraphicsListView::removed() +{ + QmlView *canvas = createView(SRCDIR "/data/listview.qml"); + + T 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); + + model.removeItem(1); + + // let transitions settle. + QTest::qWait(1000); + + QmlGraphicsText *name = findItem(viewport, "textName", 1); + QVERIFY(name != 0); + QCOMPARE(name->text(), model.name(1)); + QmlGraphicsText *number = findItem(viewport, "textNumber", 1); + QVERIFY(number != 0); + QCOMPARE(number->text(), model.number(1)); + + // Confirm items positioned correctly + int itemCount = findItems(viewport, "wrapper").count(); + for (int i = 0; i < model.count() && i < itemCount; ++i) { + QmlGraphicsItem *item = findItem(viewport, "wrapper", i); + if (!item) qWarning() << "Item" << i << "not found"; + QVERIFY(item); + QVERIFY(item->y() == i*20); + } + + // Remove first item (which is the current item); + model.removeItem(0); // post: top item starts at 20 + + // let transitions settle. + QTest::qWait(1000); + + name = findItem(viewport, "textName", 0); + QVERIFY(name != 0); + QCOMPARE(name->text(), model.name(0)); + number = findItem(viewport, "textNumber", 0); + QVERIFY(number != 0); + QCOMPARE(number->text(), model.number(0)); + + // Confirm items positioned correctly + itemCount = findItems(viewport, "wrapper").count(); + for (int i = 0; i < model.count() && i < itemCount; ++i) { + QmlGraphicsItem *item = findItem(viewport, "wrapper", i); + if (!item) qWarning() << "Item" << i << "not found"; + QVERIFY(item); + QCOMPARE(item->y(),i*20.0 + 20.0); + } + + // Remove items not visible + model.removeItem(18); + // let transitions settle. + QTest::qWait(1000); + + // Confirm items positioned correctly + itemCount = findItems(viewport, "wrapper").count(); + for (int i = 0; i < model.count() && i < itemCount; ++i) { + QmlGraphicsItem *item = findItem(viewport, "wrapper", i); + if (!item) qWarning() << "Item" << i << "not found"; + QVERIFY(item); + QCOMPARE(item->y(),i*20.0+20.0); + } + + // Remove items before visible + listview->setViewportY(80); + listview->setCurrentIndex(10); + + model.removeItem(1); // post: top item will be at 40 + // let transitions settle. + QTest::qWait(1000); + + // Confirm items positioned correctly + for (int i = 2; i < 18; ++i) { + QmlGraphicsItem *item = findItem(viewport, "wrapper", i); + if (!item) qWarning() << "Item" << i << "not found"; + QVERIFY(item); + QCOMPARE(item->y(),40+i*20.0); + } + + listview->setViewportY(40); // That's the top now + // let transitions settle. + QTest::qWait(1000); + + // Confirm items positioned correctly + itemCount = findItems(viewport, "wrapper").count(); + for (int i = 0; i < model.count() && i < itemCount; ++i) { + QmlGraphicsItem *item = findItem(viewport, "wrapper", i); + if (!item) qWarning() << "Item" << i << "not found"; + QVERIFY(item); + QCOMPARE(item->y(),40+i*20.0); + } + + delete canvas; +} + +template +void tst_QmlGraphicsListView::moved() +{ + QmlView *canvas = createView(SRCDIR "/data/listview.qml"); + + T 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); + + model.moveItem(1, 4); + + // let transitions settle. + QTest::qWait(1000); + + QmlGraphicsText *name = findItem(viewport, "textName", 1); + QVERIFY(name != 0); + QCOMPARE(name->text(), model.name(1)); + QmlGraphicsText *number = findItem(viewport, "textNumber", 1); + QVERIFY(number != 0); + QCOMPARE(number->text(), model.number(1)); + + name = findItem(viewport, "textName", 4); + QVERIFY(name != 0); + QCOMPARE(name->text(), model.name(4)); + number = findItem(viewport, "textNumber", 4); + QVERIFY(number != 0); + QCOMPARE(number->text(), model.number(4)); + + // Confirm items positioned correctly + int itemCount = findItems(viewport, "wrapper").count(); + for (int i = 0; i < model.count() && i < itemCount; ++i) { + QmlGraphicsItem *item = findItem(viewport, "wrapper", i); + if (!item) qWarning() << "Item" << i << "not found"; + QVERIFY(item); + QVERIFY(item->y() == i*20); + } + + listview->setViewportY(80); + + // move outside visible area + model.moveItem(1, 18); + + // let transitions settle. + QTest::qWait(1000); + + // Confirm items positioned correctly and indexes correct + for (int i = 3; i < model.count() && i < itemCount; ++i) { + QmlGraphicsItem *item = findItem(viewport, "wrapper", i); + if (!item) qWarning() << "Item" << i << "not found"; + QVERIFY(item); + QVERIFY(item->y() == i*20 + 20); + name = findItem(viewport, "textName", i); + QVERIFY(name != 0); + QCOMPARE(name->text(), model.name(i)); + number = findItem(viewport, "textNumber", i); + QVERIFY(number != 0); + QCOMPARE(number->text(), model.number(i)); + } + + // move from outside visible into visible + model.moveItem(20, 4); + + // let transitions settle. + QTest::qWait(1000); + + // Confirm items positioned correctly and indexes correct + for (int i = 3; i < model.count() && i < itemCount; ++i) { + QmlGraphicsItem *item = findItem(viewport, "wrapper", i); + if (!item) qWarning() << "Item" << i << "not found"; + QVERIFY(item); + QVERIFY(item->y() == i*20 + 20); + name = findItem(viewport, "textName", i); + QVERIFY(name != 0); + QCOMPARE(name->text(), model.name(i)); + number = findItem(viewport, "textNumber", i); + QVERIFY(number != 0); + QCOMPARE(number->text(), model.number(i)); + } + + 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::spacing() +{ + QmlView *canvas = createView(SRCDIR "/data/listview.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); + + // Confirm items positioned correctly + int itemCount = findItems(viewport, "wrapper").count(); + for (int i = 0; i < model.count() && i < itemCount; ++i) { + QmlGraphicsItem *item = findItem(viewport, "wrapper", i); + if (!item) qWarning() << "Item" << i << "not found"; + QVERIFY(item); + QVERIFY(item->y() == i*20); + } + + listview->setSpacing(10); + QVERIFY(listview->spacing() == 10); + + // Confirm items positioned correctly + itemCount = findItems(viewport, "wrapper").count(); + for (int i = 0; i < model.count() && i < itemCount; ++i) { + QmlGraphicsItem *item = findItem(viewport, "wrapper", i); + if (!item) qWarning() << "Item" << i << "not found"; + QVERIFY(item); + QVERIFY(item->y() == i*30); + } + + listview->setSpacing(0); + + // Confirm items positioned correctly + itemCount = findItems(viewport, "wrapper").count(); + for (int i = 0; i < model.count() && i < itemCount; ++i) { + QmlGraphicsItem *item = findItem(viewport, "wrapper", i); + if (!item) qWarning() << "Item" << i << "not found"; + QVERIFY(item); + QVERIFY(item->y() == i*20); + } + + delete canvas; +} + +void tst_QmlGraphicsListView::sections() +{ + QmlView *canvas = createView(SRCDIR "/data/listview-sections.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); + + // Confirm items positioned correctly + int itemCount = findItems(viewport, "wrapper").count(); + for (int i = 0; i < model.count() && i < itemCount; ++i) { + QmlGraphicsItem *item = findItem(viewport, "wrapper", i); + QVERIFY(item); + QCOMPARE(item->y(), qreal(i*20 + ((i+4)/5) * 20)); + } + + QVERIFY(listview->currentSection() == "0"); + + listview->setViewportY(140); + QVERIFY(listview->currentSection() == "1"); + + delete canvas; +} + +void tst_QmlGraphicsListView::qListModelInterface_items() +{ + items(); +} + +void tst_QmlGraphicsListView::qAbstractItemModel_items() +{ + items(); +} + +void tst_QmlGraphicsListView::qListModelInterface_changed() +{ + changed(); +} + +void tst_QmlGraphicsListView::qAbstractItemModel_changed() +{ + changed(); +} + +void tst_QmlGraphicsListView::qListModelInterface_inserted() +{ + inserted(); +} + +void tst_QmlGraphicsListView::qAbstractItemModel_inserted() +{ + inserted(); +} + +void tst_QmlGraphicsListView::qListModelInterface_removed() +{ + removed(); +} + +void tst_QmlGraphicsListView::qAbstractItemModel_removed() +{ + removed(); +} + +void tst_QmlGraphicsListView::qListModelInterface_moved() +{ + moved(); +} + +void tst_QmlGraphicsListView::qAbstractItemModel_moved() +{ + moved(); +} + + +QmlView *tst_QmlGraphicsListView::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_QmlGraphicsListView::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)); + if(!item) + continue; + //qDebug() << "try" << item; + 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; +} + +template +QList tst_QmlGraphicsListView::findItems(QmlGraphicsItem *parent, const QString &objectName) +{ + 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)); + if(!item) + continue; + //qDebug() << "try" << item; + if (mo.cast(item) && (objectName.isEmpty() || item->objectName() == objectName)) + items.append(static_cast(item)); + items += findItems(item, objectName); + } + + return items; +} + + +QTEST_MAIN(tst_QmlGraphicsListView) + +#include "tst_listview.moc" -- cgit v0.12