Remove "All rights reserved" line from license headers.
[profile/ivi/qtdeclarative.git] / tests / benchmarks / declarative / binding / tst_binding.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include <qtest.h>
43 #include <QDeclarativeEngine>
44 #include <QDeclarativeContext>
45 #include <QDeclarativeComponent>
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     QDeclarativeEngine 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     QDeclarativeComponent 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"