From 5e42176970c05c015685194aecf954aaac5d9e1e Mon Sep 17 00:00:00 2001 From: Bea Lam Date: Tue, 31 Jan 2012 17:15:00 +1000 Subject: Separate out some of the common test code Moves findItem, findItems() etc. into shared/visualtestutil.h and moves duplicate list model test classes and other common view-specific code into shared/viewtestutil.h. Change-Id: Ia79bc4df1ab7fb618597755c0288a2e8f5054faf Reviewed-by: Martin Jones --- .../qtquick2/qquickrepeater/data/repeater1.qml | 42 +++---- .../qtquick2/qquickrepeater/qquickrepeater.pro | 1 + .../qtquick2/qquickrepeater/tst_qquickrepeater.cpp | 133 ++------------------- 3 files changed, 32 insertions(+), 144 deletions(-) (limited to 'tests/auto/qtquick2/qquickrepeater') diff --git a/tests/auto/qtquick2/qquickrepeater/data/repeater1.qml b/tests/auto/qtquick2/qquickrepeater/data/repeater1.qml index 596dc9131e..3e6f4071ff 100644 --- a/tests/auto/qtquick2/qquickrepeater/data/repeater1.qml +++ b/tests/auto/qtquick2/qquickrepeater/data/repeater1.qml @@ -1,28 +1,30 @@ import QtQuick 2.0 -Rectangle { - id: container - objectName: "container" - width: 240 - height: 320 - color: "white" - Text { - text: "Zero" - } - Repeater { - id: repeater - objectName: "repeater" +Item { + Rectangle { + id: container + objectName: "container" width: 240 height: 320 - model: testData - Component { - Text { - y: index*20 - text: modelData + color: "white" + Text { + text: "Zero" + } + Repeater { + id: repeater + objectName: "repeater" + width: 240 + height: 320 + model: testData + Component { + Text { + y: index*20 + text: modelData + } } } - } - Text { - text: "Last" + Text { + text: "Last" + } } } diff --git a/tests/auto/qtquick2/qquickrepeater/qquickrepeater.pro b/tests/auto/qtquick2/qquickrepeater/qquickrepeater.pro index fa1781fc44..b791faf78a 100644 --- a/tests/auto/qtquick2/qquickrepeater/qquickrepeater.pro +++ b/tests/auto/qtquick2/qquickrepeater/qquickrepeater.pro @@ -5,6 +5,7 @@ macx:CONFIG -= app_bundle SOURCES += tst_qquickrepeater.cpp include (../../shared/util.pri) +include (../shared/util.pri) testFiles.files = data testFiles.path = . diff --git a/tests/auto/qtquick2/qquickrepeater/tst_qquickrepeater.cpp b/tests/auto/qtquick2/qquickrepeater/tst_qquickrepeater.cpp index 518ea1843b..59322704f5 100644 --- a/tests/auto/qtquick2/qquickrepeater/tst_qquickrepeater.cpp +++ b/tests/auto/qtquick2/qquickrepeater/tst_qquickrepeater.cpp @@ -51,6 +51,12 @@ #include #include "../../shared/util.h" +#include "../shared/viewtestutil.h" +#include "../shared/visualtestutil.h" + +using namespace QQuickViewTestUtil; +using namespace QQuickVisualTestUtil; + class tst_QQuickRepeater : public QDeclarativeDataTest { @@ -71,13 +77,6 @@ private slots: void properties(); void asynchronous(); void initParent(); - -private: - QQuickView *createView(); - template - T *findItem(QObject *parent, const QString &objectName, int index); - template - T *findItem(QObject *parent, const QString &id); }; class TestObject : public QObject @@ -104,67 +103,6 @@ private: bool mUseModel; }; -class TestModel : public QAbstractListModel -{ -public: - enum Roles { Name = Qt::UserRole+1, Number = Qt::UserRole+2 }; - - TestModel(QObject *parent=0) : QAbstractListModel(parent) { - QHash roles; - roles[Name] = "name"; - roles[Number] = "number"; - setRoleNames(roles); - } - - int rowCount(const QModelIndex &parent=QModelIndex()) const { Q_UNUSED(parent); 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_QQuickRepeater::tst_QQuickRepeater() { } @@ -306,7 +244,7 @@ void tst_QQuickRepeater::dataModel_adding() TestObject *testObject = new TestObject; ctxt->setContextProperty("testObject", testObject); - TestModel testModel; + QaimModel testModel; ctxt->setContextProperty("testData", &testModel); canvas->setSource(testFileUrl("repeater2.qml")); qApp->processEvents(); @@ -370,7 +308,7 @@ void tst_QQuickRepeater::dataModel_removing() TestObject *testObject = new TestObject; ctxt->setContextProperty("testObject", testObject); - TestModel testModel; + QaimModel testModel; testModel.addItem("one", "1"); testModel.addItem("two", "2"); testModel.addItem("three", "3"); @@ -438,7 +376,7 @@ void tst_QQuickRepeater::dataModel_changes() TestObject *testObject = new TestObject; ctxt->setContextProperty("testObject", testObject); - TestModel testModel; + QaimModel testModel; testModel.addItem("one", "1"); testModel.addItem("two", "2"); testModel.addItem("three", "3"); @@ -701,59 +639,6 @@ void tst_QQuickRepeater::initParent() QCOMPARE(qvariant_cast(rootObject->property("parentItem")), rootObject); } -QQuickView *tst_QQuickRepeater::createView() -{ - QQuickView *canvas = new QQuickView(0); - canvas->setGeometry(0,0,240,320); - - return canvas; -} - -template -T *tst_QQuickRepeater::findItem(QObject *parent, const QString &objectName, int index) -{ - const QMetaObject &mo = T::staticMetaObject; - //qDebug() << parent->children().count() << "children"; - for (int i = 0; i < parent->children().count(); ++i) { - QQuickItem *item = qobject_cast(parent->children().at(i)); - if (!item) - continue; - //qDebug() << "try" << item; - if (mo.cast(item) && (objectName.isEmpty() || item->objectName() == objectName)) { - if (index != -1) { - QDeclarativeExpression e(qmlContext(item), item, "index"); - if (e.evaluate().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 -T *tst_QQuickRepeater::findItem(QObject *parent, const QString &objectName) -{ - const QMetaObject &mo = T::staticMetaObject; - if (mo.cast(parent) && (objectName.isEmpty() || parent->objectName() == objectName)) - return static_cast(parent); - for (int i = 0; i < parent->children().count(); ++i) { - QQuickItem *child = qobject_cast(parent->children().at(i)); - if (!child) - continue; - QQuickItem *item = findItem(child, objectName); - if (item) - return static_cast(item); - } - - return 0; -} - QTEST_MAIN(tst_QQuickRepeater) #include "tst_qquickrepeater.moc" -- cgit v1.2.3