3d54f078e8d696bdc4ce223bbf93fb8823e00d20
[profile/ivi/qtdeclarative.git] / tests / auto / declarative / v4 / tst_v4.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
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/qv4compiler_p.h>
50
51 #include "../../shared/util.h"
52 #include "testtypes.h"
53
54 class tst_v4 : public QDeclarativeDataTest
55 {
56     Q_OBJECT
57 public:
58     tst_v4() {}
59
60 private slots:
61     void initTestCase();
62
63     void unnecessaryReeval();
64     void logicalOr();
65     void conditionalExpr();
66     void qtscript();
67     void qtscript_data();
68     void nestedObjectAccess();
69     void subscriptionsInConditionalExpressions();
70     void qtbug_21883();
71     void qtbug_22816();
72
73 private:
74     QDeclarativeEngine engine;
75 };
76
77 void tst_v4::initTestCase()
78 {
79     QDeclarativeDataTest::initTestCase();
80     registerTypes();
81 }
82
83 static int v4ErrorsMsgCount = 0;
84 static void v4ErrorsMsgHandler(QtMsgType, const char *message)
85 {
86     QByteArray m(message);
87     if (m.contains("QV4"))
88         v4ErrorsMsgCount++;
89 }
90
91 void tst_v4::qtscript()
92 {
93     QFETCH(QString, file);
94     QV4Compiler::enableBindingsTest(true);
95
96     QDeclarativeComponent component(&engine, testFileUrl(file));
97
98     v4ErrorsMsgCount = 0;
99     QtMsgHandler old = qInstallMsgHandler(v4ErrorsMsgHandler);
100
101     QObject *o = component.create();
102     delete o;
103
104     qInstallMsgHandler(old);
105
106     QCOMPARE(v4ErrorsMsgCount, 0);
107
108     QV4Compiler::enableBindingsTest(false);
109 }
110
111 void tst_v4::qtscript_data()
112 {
113     QTest::addColumn<QString>("file");
114
115     QTest::newRow("qreal -> int rounding") << "qrealToIntRounding.qml";
116     QTest::newRow("exception on fetch") << "fetchException.qml";
117     QTest::newRow("logical or") << "logicalOr.qml";
118     QTest::newRow("conditional expressions") << "conditionalExpr.qml";
119     QTest::newRow("double bool jump") << "doubleBoolJump.qml";
120     QTest::newRow("unary minus") << "unaryMinus.qml";
121     QTest::newRow("null qobject") << "nullQObject.qml";
122 }
123
124 void tst_v4::unnecessaryReeval()
125 {
126     QDeclarativeComponent component(&engine, testFileUrl("unnecessaryReeval.qml"));
127
128     QObject *o = component.create();
129     QVERIFY(o != 0);
130
131     ResultObject *ro = qobject_cast<ResultObject *>(o);
132     QVERIFY(ro != 0);
133
134     QCOMPARE(ro->resultCounter(),  1);
135     QCOMPARE(ro->result(), 19);
136     ro->resetResultCounter();
137
138     ro->setProperty("b", 6);
139
140     QCOMPARE(ro->resultCounter(),  1);
141     QCOMPARE(ro->result(), 6);
142     ro->resetResultCounter();
143
144     ro->setProperty("a", 14);
145
146     QCOMPARE(ro->resultCounter(),  1);
147     QCOMPARE(ro->result(), 7);
148     ro->resetResultCounter();
149
150     ro->setProperty("b", 14);
151     QCOMPARE(ro->resultCounter(),  0);
152     QCOMPARE(ro->result(), 7);
153
154     delete o;
155 }
156
157 void tst_v4::logicalOr()
158 {
159     {
160         QDeclarativeComponent component(&engine, testFileUrl("logicalOr.qml"));
161
162         QObject *o = component.create();
163         QVERIFY(o != 0);
164
165         ResultObject *ro = qobject_cast<ResultObject *>(o);
166         QVERIFY(ro != 0);
167
168         QCOMPARE(ro->result(), 0);
169         delete o;
170     }
171
172     {
173         QDeclarativeComponent component(&engine, testFileUrl("logicalOr.2.qml"));
174
175         QObject *o = component.create();
176         QVERIFY(o != 0);
177
178         ResultObject *ro = qobject_cast<ResultObject *>(o);
179         QVERIFY(ro != 0);
180
181         QCOMPARE(ro->result(), 1);
182         delete o;
183     }
184 }
185
186 void tst_v4::conditionalExpr()
187 {
188     {
189         QDeclarativeComponent component(&engine, testFileUrl("conditionalExpr.qml"));
190
191         QObject *o = component.create();
192         QVERIFY(o != 0);
193
194         ResultObject *ro = qobject_cast<ResultObject *>(o);
195         QVERIFY(ro != 0);
196
197         QCOMPARE(ro->result(), 0);
198         delete o;
199     }
200 }
201
202 // This would previously use the metaObject of the root element to result the nested access.
203 // That is, the index for accessing "result" would have been RootObject::result, instead of
204 // NestedObject::result.
205 void tst_v4::nestedObjectAccess()
206 {
207     QDeclarativeComponent component(&engine, testFileUrl("nestedObjectAccess.qml"));
208
209     QObject *o = component.create();
210     QVERIFY(o != 0);
211
212     ResultObject *ro = qobject_cast<ResultObject *>(o);
213     QVERIFY(ro != 0);
214
215     QCOMPARE(ro->result(), 37);
216
217     delete o;
218 }
219
220 void tst_v4::subscriptionsInConditionalExpressions()
221 {
222     QDeclarativeComponent component(&engine, testFileUrl("subscriptionsInConditionalExpressions.qml"));
223
224     QObject *o = component.create();
225     QVERIFY(o != 0);
226
227     QObject *ro = qobject_cast<QObject *>(o);
228     QVERIFY(ro != 0);
229
230     QCOMPARE(ro->property("result").toReal(), qreal(2));
231
232     delete o;
233 }
234
235 // Crash test
236 void tst_v4::qtbug_21883()
237 {
238     QDeclarativeComponent component(&engine, testFileUrl("qtbug_21883.qml"));
239
240     QString warning = component.url().toString() + ":4: Unable to assign null to ResultObject*";
241     QTest::ignoreMessage(QtWarningMsg, warning.toLatin1().constData());
242
243     QObject *o = component.create();
244     QVERIFY(o != 0);
245     delete o;
246 }
247
248 void tst_v4::qtbug_22816()
249 {
250     QDeclarativeComponent component(&engine, testFileUrl("qtbug_22816.qml"));
251
252     QObject *o = component.create();
253     QVERIFY(o != 0);
254     QCOMPARE(o->property("test1").toBool(), false);
255     QCOMPARE(o->property("test2").toBool(), false);
256     delete o;
257 }
258
259 QTEST_MAIN(tst_v4)
260
261 #include "tst_v4.moc"