Update licenseheader text in source files for qtdeclarative Qt module
[profile/ivi/qtdeclarative.git] / tests / auto / declarative / qdeclarativev4 / tst_qdeclarativev4.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 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 **
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 **
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
29 **
30 ** Other Usage
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 #include <qtest.h>
42 #include <QtCore/qobject.h>
43 #include <QtCore/qfileinfo.h>
44 #include <QtCore/qdir.h>
45 #include <QtDeclarative/qdeclarativeengine.h>
46 #include <QtDeclarative/qdeclarativecomponent.h>
47 #include <QtCore/qdebug.h>
48
49 #include <private/qdeclarativev4compiler_p.h>
50
51 #include "testtypes.h"
52
53 inline QUrl TEST_FILE(const QString &filename)
54 {
55     QFileInfo fileInfo(__FILE__);
56     return QUrl::fromLocalFile(fileInfo.absoluteDir().filePath("data/" + filename));
57 }
58
59 inline QUrl TEST_FILE(const char *filename)
60 {
61     return TEST_FILE(QLatin1String(filename));
62 }
63
64 class tst_qdeclarativev4 : public QObject
65 {
66     Q_OBJECT
67 public:
68     tst_qdeclarativev4() {}
69
70 private slots:
71     void initTestCase();
72
73     void unnecessaryReeval();
74     void logicalOr();
75     void conditionalExpr();
76     void qtscript();
77     void qtscript_data();
78     void nestedObjectAccess();
79     void subscriptionsInConditionalExpressions();
80
81 private:
82     QDeclarativeEngine engine;
83 };
84
85 void tst_qdeclarativev4::initTestCase()
86 {
87     registerTypes();
88 }
89
90 static int v4ErrorsMsgCount = 0;
91 static void v4ErrorsMsgHandler(QtMsgType, const char *message)
92 {
93     QByteArray m(message);
94     if (m.contains("QDeclarativeV4"))
95         v4ErrorsMsgCount++;
96 }
97
98 void tst_qdeclarativev4::qtscript()
99 {
100     QFETCH(QString, file);
101     QDeclarativeV4Compiler::enableBindingsTest(true);
102
103     QDeclarativeComponent component(&engine, TEST_FILE(file));
104
105     v4ErrorsMsgCount = 0;
106     QtMsgHandler old = qInstallMsgHandler(v4ErrorsMsgHandler);
107
108     QObject *o = component.create();
109     delete o;
110
111     qInstallMsgHandler(old);
112
113     QCOMPARE(v4ErrorsMsgCount, 0);
114
115     QDeclarativeV4Compiler::enableBindingsTest(false);
116 }
117
118 void tst_qdeclarativev4::qtscript_data()
119 {
120     QTest::addColumn<QString>("file");
121
122     QTest::newRow("qreal -> int rounding") << "qrealToIntRounding.qml";
123     QTest::newRow("exception on fetch") << "fetchException.qml";
124     QTest::newRow("logical or") << "logicalOr.qml";
125     QTest::newRow("conditional expressions") << "conditionalExpr.qml";
126     QTest::newRow("double bool jump") << "doubleBoolJump.qml";
127     QTest::newRow("unary minus") << "unaryMinus.qml";
128     QTest::newRow("null qobject") << "nullQObject.qml";
129 }
130
131 void tst_qdeclarativev4::unnecessaryReeval()
132 {
133     QDeclarativeComponent component(&engine, TEST_FILE("unnecessaryReeval.qml"));
134
135     QObject *o = component.create();
136     QVERIFY(o != 0);
137
138     ResultObject *ro = qobject_cast<ResultObject *>(o);
139     QVERIFY(ro != 0);
140
141     QCOMPARE(ro->resultCounter(),  1);
142     QCOMPARE(ro->result(), 19);
143     ro->resetResultCounter();
144
145     ro->setProperty("b", 6);
146
147     QCOMPARE(ro->resultCounter(),  1);
148     QCOMPARE(ro->result(), 6);
149     ro->resetResultCounter();
150
151     ro->setProperty("a", 14);
152
153     QCOMPARE(ro->resultCounter(),  1);
154     QCOMPARE(ro->result(), 7);
155     ro->resetResultCounter();
156
157     ro->setProperty("b", 14);
158     QCOMPARE(ro->resultCounter(),  0);
159     QCOMPARE(ro->result(), 7);
160
161     delete o;
162 }
163
164 void tst_qdeclarativev4::logicalOr()
165 {
166     {
167         QDeclarativeComponent component(&engine, TEST_FILE("logicalOr.qml"));
168
169         QObject *o = component.create();
170         QVERIFY(o != 0);
171
172         ResultObject *ro = qobject_cast<ResultObject *>(o);
173         QVERIFY(ro != 0);
174
175         QCOMPARE(ro->result(), 0);
176         delete o;
177     }
178
179     {
180         QDeclarativeComponent component(&engine, TEST_FILE("logicalOr.2.qml"));
181
182         QObject *o = component.create();
183         QVERIFY(o != 0);
184
185         ResultObject *ro = qobject_cast<ResultObject *>(o);
186         QVERIFY(ro != 0);
187
188         QCOMPARE(ro->result(), 1);
189         delete o;
190     }
191 }
192
193 void tst_qdeclarativev4::conditionalExpr()
194 {
195     {
196         QDeclarativeComponent component(&engine, TEST_FILE("conditionalExpr.qml"));
197
198         QObject *o = component.create();
199         QVERIFY(o != 0);
200
201         ResultObject *ro = qobject_cast<ResultObject *>(o);
202         QVERIFY(ro != 0);
203
204         QCOMPARE(ro->result(), 0);
205         delete o;
206     }
207 }
208
209 // This would previously use the metaObject of the root element to result the nested access.
210 // That is, the index for accessing "result" would have been RootObject::result, instead of
211 // NestedObject::result.
212 void tst_qdeclarativev4::nestedObjectAccess()
213 {
214     QDeclarativeComponent component(&engine, TEST_FILE("nestedObjectAccess.qml"));
215
216     QObject *o = component.create();
217     QVERIFY(o != 0);
218
219     ResultObject *ro = qobject_cast<ResultObject *>(o);
220     QVERIFY(ro != 0);
221
222     QCOMPARE(ro->result(), 37);
223
224     delete o;
225 }
226
227 void tst_qdeclarativev4::subscriptionsInConditionalExpressions()
228 {
229     QDeclarativeComponent component(&engine, TEST_FILE("subscriptionsInConditionalExpressions.qml"));
230
231     QObject *o = component.create();
232     QVERIFY(o != 0);
233
234     QObject *ro = qobject_cast<QObject *>(o);
235     QVERIFY(ro != 0);
236
237     QCOMPARE(ro->property("result").toReal(), qreal(2));
238
239     delete o;
240 }
241
242 QTEST_MAIN(tst_qdeclarativev4)
243
244 #include "tst_qdeclarativev4.moc"