1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the test suite of the Qt Toolkit.
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.
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.
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.
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.
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/qdeclarativev4compiler_p.h>
51 #include "testtypes.h"
53 inline QUrl TEST_FILE(const QString &filename)
55 QFileInfo fileInfo(__FILE__);
56 return QUrl::fromLocalFile(fileInfo.absoluteDir().filePath("data/" + filename));
59 inline QUrl TEST_FILE(const char *filename)
61 return TEST_FILE(QLatin1String(filename));
64 class tst_qdeclarativev4 : public QObject
68 tst_qdeclarativev4() {}
73 void unnecessaryReeval();
75 void conditionalExpr();
78 void nestedObjectAccess();
79 void subscriptionsInConditionalExpressions();
82 QDeclarativeEngine engine;
85 void tst_qdeclarativev4::initTestCase()
90 static int v4ErrorsMsgCount = 0;
91 static void v4ErrorsMsgHandler(QtMsgType, const char *message)
93 QByteArray m(message);
94 if (m.contains("QDeclarativeV4"))
98 void tst_qdeclarativev4::qtscript()
100 QFETCH(QString, file);
101 QDeclarativeV4Compiler::enableBindingsTest(true);
103 QDeclarativeComponent component(&engine, TEST_FILE(file));
105 v4ErrorsMsgCount = 0;
106 QtMsgHandler old = qInstallMsgHandler(v4ErrorsMsgHandler);
108 QObject *o = component.create();
111 qInstallMsgHandler(old);
113 QCOMPARE(v4ErrorsMsgCount, 0);
115 QDeclarativeV4Compiler::enableBindingsTest(false);
118 void tst_qdeclarativev4::qtscript_data()
120 QTest::addColumn<QString>("file");
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";
131 void tst_qdeclarativev4::unnecessaryReeval()
133 QDeclarativeComponent component(&engine, TEST_FILE("unnecessaryReeval.qml"));
135 QObject *o = component.create();
138 ResultObject *ro = qobject_cast<ResultObject *>(o);
141 QCOMPARE(ro->resultCounter(), 1);
142 QCOMPARE(ro->result(), 19);
143 ro->resetResultCounter();
145 ro->setProperty("b", 6);
147 QCOMPARE(ro->resultCounter(), 1);
148 QCOMPARE(ro->result(), 6);
149 ro->resetResultCounter();
151 ro->setProperty("a", 14);
153 QCOMPARE(ro->resultCounter(), 1);
154 QCOMPARE(ro->result(), 7);
155 ro->resetResultCounter();
157 ro->setProperty("b", 14);
158 QCOMPARE(ro->resultCounter(), 0);
159 QCOMPARE(ro->result(), 7);
164 void tst_qdeclarativev4::logicalOr()
167 QDeclarativeComponent component(&engine, TEST_FILE("logicalOr.qml"));
169 QObject *o = component.create();
172 ResultObject *ro = qobject_cast<ResultObject *>(o);
175 QCOMPARE(ro->result(), 0);
180 QDeclarativeComponent component(&engine, TEST_FILE("logicalOr.2.qml"));
182 QObject *o = component.create();
185 ResultObject *ro = qobject_cast<ResultObject *>(o);
188 QCOMPARE(ro->result(), 1);
193 void tst_qdeclarativev4::conditionalExpr()
196 QDeclarativeComponent component(&engine, TEST_FILE("conditionalExpr.qml"));
198 QObject *o = component.create();
201 ResultObject *ro = qobject_cast<ResultObject *>(o);
204 QCOMPARE(ro->result(), 0);
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()
214 QDeclarativeComponent component(&engine, TEST_FILE("nestedObjectAccess.qml"));
216 QObject *o = component.create();
219 ResultObject *ro = qobject_cast<ResultObject *>(o);
222 QCOMPARE(ro->result(), 37);
227 void tst_qdeclarativev4::subscriptionsInConditionalExpressions()
229 QDeclarativeComponent component(&engine, TEST_FILE("subscriptionsInConditionalExpressions.qml"));
231 QObject *o = component.create();
234 QObject *ro = qobject_cast<QObject *>(o);
237 QCOMPARE(ro->property("result").toReal(), qreal(2));
242 QTEST_MAIN(tst_qdeclarativev4)
244 #include "tst_qdeclarativev4.moc"