~ubuntu-branches/ubuntu/saucy/qtdeclarative-opensource-src/saucy

« back to all changes in this revision

Viewing changes to tests/benchmarks/qml/binding/tst_binding.cpp

  • Committer: Package Import Robot
  • Author(s): Timo Jyrinki
  • Date: 2013-02-05 14:17:19 UTC
  • Revision ID: package-import@ubuntu.com-20130205141719-qqeyml8wslpyez52
Tags: upstream-5.0.1
ImportĀ upstreamĀ versionĀ 5.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
 
4
** Contact: http://www.qt-project.org/legal
 
5
**
 
6
** This file is part of the test suite of the Qt Toolkit.
 
7
**
 
8
** $QT_BEGIN_LICENSE:LGPL$
 
9
** Commercial License Usage
 
10
** Licensees holding valid commercial Qt licenses may use this file in
 
11
** accordance with the commercial license agreement provided with the
 
12
** Software or, alternatively, in accordance with the terms contained in
 
13
** a written agreement between you and Digia.  For licensing terms and
 
14
** conditions see http://qt.digia.com/licensing.  For further information
 
15
** use the contact form at http://qt.digia.com/contact-us.
 
16
**
 
17
** GNU Lesser General Public License Usage
 
18
** Alternatively, this file may be used under the terms of the GNU Lesser
 
19
** General Public License version 2.1 as published by the Free Software
 
20
** Foundation and appearing in the file LICENSE.LGPL included in the
 
21
** packaging of this file.  Please review the following information to
 
22
** ensure the GNU Lesser General Public License version 2.1 requirements
 
23
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
 
24
**
 
25
** In addition, as a special exception, Digia gives you certain additional
 
26
** rights.  These rights are described in the Digia Qt LGPL Exception
 
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
 
28
**
 
29
** GNU General Public License Usage
 
30
** Alternatively, this file may be used under the terms of the GNU
 
31
** General Public License version 3.0 as published by the Free Software
 
32
** Foundation and appearing in the file LICENSE.GPL included in the
 
33
** packaging of this file.  Please review the following information to
 
34
** ensure the GNU General Public License version 3.0 requirements will be
 
35
** met: http://www.gnu.org/copyleft/gpl.html.
 
36
**
 
37
**
 
38
** $QT_END_LICENSE$
 
39
**
 
40
****************************************************************************/
 
41
 
 
42
#include <qtest.h>
 
43
#include <QQmlEngine>
 
44
#include <QQmlContext>
 
45
#include <QQmlComponent>
 
46
#include <QFile>
 
47
#include <QDebug>
 
48
#include "testtypes.h"
 
49
 
 
50
class tst_binding : public QObject
 
51
{
 
52
    Q_OBJECT
 
53
 
 
54
public:
 
55
    tst_binding();
 
56
    virtual ~tst_binding();
 
57
 
 
58
public slots:
 
59
    void initTestCase();
 
60
    void cleanupTestCase();
 
61
 
 
62
private slots:
 
63
    void objectproperty_data();
 
64
    void objectproperty();
 
65
    void basicproperty_data();
 
66
    void basicproperty();
 
67
    void creation_data();
 
68
    void creation();
 
69
 
 
70
private:
 
71
    QQmlEngine engine;
 
72
    MyQmlObject tstObject;
 
73
};
 
74
 
 
75
tst_binding::tst_binding()
 
76
{
 
77
}
 
78
 
 
79
tst_binding::~tst_binding()
 
80
{
 
81
}
 
82
 
 
83
void tst_binding::initTestCase()
 
84
{
 
85
    registerTypes();
 
86
    engine.rootContext()->setContextProperty("tstObject", &tstObject);
 
87
}
 
88
 
 
89
void tst_binding::cleanupTestCase()
 
90
{
 
91
}
 
92
 
 
93
#define COMPONENT(filename, binding) \
 
94
    QQmlComponent c(&engine); \
 
95
    { \
 
96
        QFile f(filename); \
 
97
        QVERIFY(f.open(QIODevice::ReadOnly)); \
 
98
        QByteArray data = f.readAll(); \
 
99
        data.replace("###", binding.toUtf8()); \
 
100
        c.setData(data, QUrl()); \
 
101
        QVERIFY(c.isReady()); \
 
102
    }
 
103
 
 
104
void tst_binding::objectproperty_data()
 
105
{
 
106
    QTest::addColumn<QString>("file");
 
107
    QTest::addColumn<QString>("binding");
 
108
 
 
109
    QTest::newRow("object.value") << SRCDIR "/data/objectproperty.txt" << "object.value";
 
110
    QTest::newRow("object.value + 10") << SRCDIR "/data/objectproperty.txt" << "object.value + 10";
 
111
}
 
112
 
 
113
void tst_binding::objectproperty()
 
114
{
 
115
    QFETCH(QString, file);
 
116
    QFETCH(QString, binding);
 
117
 
 
118
    COMPONENT(file, binding);
 
119
 
 
120
    MyQmlObject object1;
 
121
    MyQmlObject object2;
 
122
 
 
123
    MyQmlObject *object = qobject_cast<MyQmlObject *>(c.create());
 
124
    QVERIFY(object != 0);
 
125
    object->setObject(&object2);
 
126
 
 
127
    QBENCHMARK {
 
128
        object->setObject(&object1);
 
129
        object->setObject(&object2);
 
130
    }
 
131
}
 
132
 
 
133
void tst_binding::basicproperty_data()
 
134
{
 
135
    QTest::addColumn<QString>("file");
 
136
    QTest::addColumn<QString>("binding");
 
137
 
 
138
    QTest::newRow("value") << SRCDIR "/data/localproperty.txt" << "value";
 
139
    QTest::newRow("value + 10") << SRCDIR "/data/localproperty.txt" << "value + 10";
 
140
    QTest::newRow("value + value + 10") << SRCDIR "/data/localproperty.txt" << "value + value + 10";
 
141
 
 
142
    QTest::newRow("myObject.value") << SRCDIR "/data/idproperty.txt" << "myObject.value";
 
143
    QTest::newRow("myObject.value + 10") << SRCDIR "/data/idproperty.txt" << "myObject.value + 10";
 
144
    QTest::newRow("myObject.value + myObject.value + 10") << SRCDIR "/data/idproperty.txt" << "myObject.value + myObject.value + 10";
 
145
}
 
146
 
 
147
void tst_binding::basicproperty()
 
148
{
 
149
    QFETCH(QString, file);
 
150
    QFETCH(QString, binding);
 
151
 
 
152
    COMPONENT(file, binding);
 
153
 
 
154
    MyQmlObject *object = qobject_cast<MyQmlObject *>(c.create());
 
155
    QVERIFY(object != 0);
 
156
    object->setValue(10);
 
157
 
 
158
    QBENCHMARK {
 
159
        object->setValue(1);
 
160
    }
 
161
}
 
162
 
 
163
void tst_binding::creation_data()
 
164
{
 
165
    QTest::addColumn<QString>("file");
 
166
    QTest::addColumn<QString>("binding");
 
167
 
 
168
    QTest::newRow("constant") << SRCDIR "/data/creation.txt" << "10";
 
169
    QTest::newRow("ownProperty") << SRCDIR "/data/creation.txt" << "myObject.value";
 
170
    QTest::newRow("declaredProperty") << SRCDIR "/data/creation.txt" << "myObject.myValue";
 
171
    QTest::newRow("contextProperty") << SRCDIR "/data/creation.txt" << "tstObject.value";
 
172
}
 
173
 
 
174
void tst_binding::creation()
 
175
{
 
176
    QFETCH(QString, file);
 
177
    QFETCH(QString, binding);
 
178
 
 
179
    COMPONENT(file, binding);
 
180
 
 
181
    QBENCHMARK {
 
182
        QObject *o = c.create();
 
183
        delete o;
 
184
    }
 
185
}
 
186
 
 
187
QTEST_MAIN(tst_binding)
 
188
#include "tst_binding.moc"