1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the test suite of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
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>
49 #include <private/qv4compiler_p.h>
51 #include "../../shared/util.h"
52 #include "testtypes.h"
54 class tst_v4 : public QDeclarativeDataTest
63 void unnecessaryReeval();
65 void conditionalExpr();
68 void nestedObjectAccess();
69 void subscriptionsInConditionalExpressions();
74 QDeclarativeEngine engine;
77 void tst_v4::initTestCase()
79 QDeclarativeDataTest::initTestCase();
83 static int v4ErrorsMsgCount = 0;
84 static void v4ErrorsMsgHandler(QtMsgType, const char *message)
86 QByteArray m(message);
87 if (m.contains("QV4"))
91 void tst_v4::qtscript()
93 QFETCH(QString, file);
94 QV4Compiler::enableBindingsTest(true);
96 QDeclarativeComponent component(&engine, testFileUrl(file));
99 QtMsgHandler old = qInstallMsgHandler(v4ErrorsMsgHandler);
101 QObject *o = component.create();
104 qInstallMsgHandler(old);
106 QCOMPARE(v4ErrorsMsgCount, 0);
108 QV4Compiler::enableBindingsTest(false);
111 void tst_v4::qtscript_data()
113 QTest::addColumn<QString>("file");
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";
124 void tst_v4::unnecessaryReeval()
126 QDeclarativeComponent component(&engine, testFileUrl("unnecessaryReeval.qml"));
128 QObject *o = component.create();
131 ResultObject *ro = qobject_cast<ResultObject *>(o);
134 QCOMPARE(ro->resultCounter(), 1);
135 QCOMPARE(ro->result(), 19);
136 ro->resetResultCounter();
138 ro->setProperty("b", 6);
140 QCOMPARE(ro->resultCounter(), 1);
141 QCOMPARE(ro->result(), 6);
142 ro->resetResultCounter();
144 ro->setProperty("a", 14);
146 QCOMPARE(ro->resultCounter(), 1);
147 QCOMPARE(ro->result(), 7);
148 ro->resetResultCounter();
150 ro->setProperty("b", 14);
151 QCOMPARE(ro->resultCounter(), 0);
152 QCOMPARE(ro->result(), 7);
157 void tst_v4::logicalOr()
160 QDeclarativeComponent component(&engine, testFileUrl("logicalOr.qml"));
162 QObject *o = component.create();
165 ResultObject *ro = qobject_cast<ResultObject *>(o);
168 QCOMPARE(ro->result(), 0);
173 QDeclarativeComponent component(&engine, testFileUrl("logicalOr.2.qml"));
175 QObject *o = component.create();
178 ResultObject *ro = qobject_cast<ResultObject *>(o);
181 QCOMPARE(ro->result(), 1);
186 void tst_v4::conditionalExpr()
189 QDeclarativeComponent component(&engine, testFileUrl("conditionalExpr.qml"));
191 QObject *o = component.create();
194 ResultObject *ro = qobject_cast<ResultObject *>(o);
197 QCOMPARE(ro->result(), 0);
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()
207 QDeclarativeComponent component(&engine, testFileUrl("nestedObjectAccess.qml"));
209 QObject *o = component.create();
212 ResultObject *ro = qobject_cast<ResultObject *>(o);
215 QCOMPARE(ro->result(), 37);
220 void tst_v4::subscriptionsInConditionalExpressions()
222 QDeclarativeComponent component(&engine, testFileUrl("subscriptionsInConditionalExpressions.qml"));
224 QObject *o = component.create();
227 QObject *ro = qobject_cast<QObject *>(o);
230 QCOMPARE(ro->property("result").toReal(), qreal(2));
236 void tst_v4::qtbug_21883()
238 QDeclarativeComponent component(&engine, testFileUrl("qtbug_21883.qml"));
240 QString warning = component.url().toString() + ":4: Unable to assign null to ResultObject*";
241 QTest::ignoreMessage(QtWarningMsg, warning.toLatin1().constData());
243 QObject *o = component.create();
248 void tst_v4::qtbug_22816()
250 QDeclarativeComponent component(&engine, testFileUrl("qtbug_22816.qml"));
252 QObject *o = component.create();
254 QCOMPARE(o->property("test1").toBool(), false);
255 QCOMPARE(o->property("test2").toBool(), false);
261 #include "tst_v4.moc"