1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
#include <QtTest/QtTest>
#include <qlistmodelinterface.h>
#include <qmlview.h>
#include <qfxrepeater.h>
#include <qfxtext.h>
#include <qmlcontext.h>
class tst_QFxRepeater : public QObject
{
Q_OBJECT
public:
tst_QFxRepeater();
private slots:
void stringList();
private:
QmlView *createView(const QString &filename);
template<typename T>
T *findItem(QObject *parent, const QString &id);
};
tst_QFxRepeater::tst_QFxRepeater()
{
}
/*
The Repeater element creates children at its own position in its parent's
stacking order. In this test we insert a repeater between two other Text
elements to test this.
*/
void tst_QFxRepeater::stringList()
{
QmlView *canvas = createView(SRCDIR "/data/repeater.qml");
QStringList data;
data << "One";
data << "Two";
data << "Three";
data << "Four";
QmlContext *ctxt = canvas->rootContext();
ctxt->setContextProperty("testData", data);
canvas->execute();
qApp->processEvents();
QFxRepeater *repeater = findItem<QFxRepeater>(canvas->root(), "repeater");
QVERIFY(repeater != 0);
QFxItem *container = findItem<QFxItem>(canvas->root(), "container");
QVERIFY(container != 0);
QCOMPARE(container->childItems().count(), data.count() + 3);
bool saw_repeater = false;
for (int i = 0; i < container->childItems().count(); ++i) {
if (i == 0) {
QFxText *name = qobject_cast<QFxText*>(container->childItems().at(i));
QVERIFY(name != 0);
QCOMPARE(name->text(), QLatin1String("Zero"));
} else if (i == container->childItems().count() - 2) {
// The repeater itself
QFxRepeater *rep = qobject_cast<QFxRepeater*>(container->childItems().at(i));
QCOMPARE(rep, repeater);
saw_repeater = true;
continue;
} else if (i == container->childItems().count() - 1) {
QFxText *name = qobject_cast<QFxText*>(container->childItems().at(i));
QVERIFY(name != 0);
QCOMPARE(name->text(), QLatin1String("Last"));
} else {
QFxText *name = qobject_cast<QFxText*>(container->childItems().at(i));
QVERIFY(name != 0);
QCOMPARE(name->text(), data.at(i-1));
}
}
QVERIFY(saw_repeater);
delete canvas;
}
QmlView *tst_QFxRepeater::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;
}
template<typename T>
T *tst_QFxRepeater::findItem(QObject *parent, const QString &objectName)
{
const QMetaObject &mo = T::staticMetaObject;
if (mo.cast(parent) && (objectName.isEmpty() || parent->objectName() == objectName))
return static_cast<T*>(parent);
for (int i = 0; i < parent->children().count(); ++i) {
QFxItem *item = findItem<T>(parent->children().at(i), objectName);
if (item)
return static_cast<T*>(item);
}
return 0;
}
QTEST_MAIN(tst_QFxRepeater)
#include "tst_repeater.moc"
|