Initial import from the monolithic Qt.
[profile/ivi/qtdeclarative.git] / tests / benchmarks / declarative / binding / tst_binding.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the test suite of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file.  Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23 **
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include <qtest.h>
43 #include <QDeclarativeEngine>
44 #include <QDeclarativeComponent>
45 #include <QFile>
46 #include <QDebug>
47 #include "testtypes.h"
48
49 //TESTED_FILES=
50
51 #ifdef Q_OS_SYMBIAN
52 // In Symbian OS test data is located in applications private dir
53 #define SRCDIR "."
54 #endif
55
56 class tst_binding : public QObject
57 {
58     Q_OBJECT
59
60 public:
61     tst_binding();
62     virtual ~tst_binding();
63
64 public slots:
65     void initTestCase();
66     void cleanupTestCase();
67
68 private slots:
69     void objectproperty_data();
70     void objectproperty();
71     void basicproperty_data();
72     void basicproperty();
73
74 private:
75     QDeclarativeEngine engine;
76 };
77
78 tst_binding::tst_binding()
79 {
80 }
81
82 tst_binding::~tst_binding()
83 {
84 }
85
86 void tst_binding::initTestCase()
87 {
88     registerTypes();
89 }
90
91 void tst_binding::cleanupTestCase()
92 {
93 }
94
95 #define COMPONENT(filename, binding) \
96     QDeclarativeComponent c(&engine); \
97     { \
98         QFile f(filename); \
99         QVERIFY(f.open(QIODevice::ReadOnly)); \
100         QByteArray data = f.readAll(); \
101         data.replace("###", binding.toUtf8()); \
102         c.setData(data, QUrl()); \
103         QVERIFY(c.isReady()); \
104     }
105
106 void tst_binding::objectproperty_data()
107 {
108     QTest::addColumn<QString>("file");
109     QTest::addColumn<QString>("binding");
110
111     QTest::newRow("object.value") << SRCDIR "/data/objectproperty.txt" << "object.value";
112     QTest::newRow("object.value + 10") << SRCDIR "/data/objectproperty.txt" << "object.value + 10";
113 }
114
115 void tst_binding::objectproperty()
116 {
117     QFETCH(QString, file);
118     QFETCH(QString, binding);
119
120     COMPONENT(file, binding);
121
122     MyQmlObject object1;
123     MyQmlObject object2;
124
125     MyQmlObject *object = qobject_cast<MyQmlObject *>(c.create());
126     QVERIFY(object != 0);
127     object->setObject(&object2);
128
129     QBENCHMARK {
130         object->setObject(&object1);
131         object->setObject(&object2);
132     }
133 }
134
135 void tst_binding::basicproperty_data()
136 {
137     QTest::addColumn<QString>("file");
138     QTest::addColumn<QString>("binding");
139
140     QTest::newRow("value") << SRCDIR "/data/localproperty.txt" << "value";
141     QTest::newRow("value + 10") << SRCDIR "/data/localproperty.txt" << "value + 10";
142     QTest::newRow("value + value + 10") << SRCDIR "/data/localproperty.txt" << "value + value + 10";
143
144     QTest::newRow("myObject.value") << SRCDIR "/data/idproperty.txt" << "myObject.value";
145     QTest::newRow("myObject.value + 10") << SRCDIR "/data/idproperty.txt" << "myObject.value + 10";
146     QTest::newRow("myObject.value + myObject.value + 10") << SRCDIR "/data/idproperty.txt" << "myObject.value + myObject.value + 10";
147 }
148
149 void tst_binding::basicproperty()
150 {
151     QFETCH(QString, file);
152     QFETCH(QString, binding);
153
154     COMPONENT(file, binding);
155
156     MyQmlObject *object = qobject_cast<MyQmlObject *>(c.create());
157     QVERIFY(object != 0);
158     object->setValue(10);
159
160     QBENCHMARK {
161         object->setValue(1);
162     }
163 }
164
165 QTEST_MAIN(tst_binding)
166 #include "tst_binding.moc"