summaryrefslogtreecommitdiffstats
path: root/tests/auto/declarative/repeater/tst_repeater.cpp
blob: 08f91549d5e2b81270f09a5586014431f6836eb5 (plain)
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
#include <QtTest/QtTest>
#include <qlistmodelinterface.h>
#include <qfxview.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:
    QFxView *createView(const QString &filename);
    template<typename T>
    T *findItem(QObject *parent, const QString &id);
};

tst_QFxRepeater::tst_QFxRepeater()
{
}

void tst_QFxRepeater::stringList()
{
    QFxView *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() + 1);

    for (int i = 1; i < container->childItems().count(); ++i) {
        QFxText *name = qobject_cast<QFxText*>(container->childItems().at(i));
        QVERIFY(name != 0);
        QCOMPARE(name->text(), data.at(i-1));
    }

    delete canvas;
}


QFxView *tst_QFxRepeater::createView(const QString &filename)
{
    QFxView *canvas = new QFxView(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"