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 <QtDeclarative/qdeclarativeengine.h>
43 #include <QtDeclarative/qdeclarativecomponent.h>
44 #include <private/qdeclarativeconnections_p.h>
45 #include <private/qsgitem_p.h>
46 #include "../../../shared/util.h"
47 #include <QtDeclarative/qdeclarativescriptstring.h>
50 // In Symbian OS test data is located in applications private dir
54 class tst_qdeclarativeconnection : public QObject
59 tst_qdeclarativeconnection();
67 void unknownSignals_data();
68 void unknownSignals();
71 void moduleApiTarget();
74 QDeclarativeEngine engine;
77 tst_qdeclarativeconnection::tst_qdeclarativeconnection()
81 void tst_qdeclarativeconnection::defaultValues()
83 QDeclarativeEngine engine;
84 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/test-connection3.qml"));
85 QDeclarativeConnections *item = qobject_cast<QDeclarativeConnections*>(c.create());
88 QVERIFY(item->target() == 0);
93 void tst_qdeclarativeconnection::properties()
95 QDeclarativeEngine engine;
96 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/test-connection2.qml"));
97 QDeclarativeConnections *item = qobject_cast<QDeclarativeConnections*>(c.create());
102 QVERIFY(item->target() == item);
107 void tst_qdeclarativeconnection::connection()
109 QDeclarativeEngine engine;
110 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/test-connection.qml"));
111 QSGItem *item = qobject_cast<QSGItem*>(c.create());
115 QCOMPARE(item->property("tested").toBool(), false);
116 QCOMPARE(item->width(), 50.);
117 emit item->setWidth(100.);
118 QCOMPARE(item->width(), 100.);
119 QCOMPARE(item->property("tested").toBool(), true);
124 void tst_qdeclarativeconnection::trimming()
126 QDeclarativeEngine engine;
127 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/trimming.qml"));
128 QSGItem *item = qobject_cast<QSGItem*>(c.create());
132 QCOMPARE(item->property("tested").toString(), QString(""));
133 int index = item->metaObject()->indexOfSignal("testMe(int,QString)");
134 QMetaMethod method = item->metaObject()->method(index);
136 Qt::DirectConnection,
138 Q_ARG(QString, "worked"));
139 QCOMPARE(item->property("tested").toString(), QString("worked5"));
144 // Confirm that target can be changed by one of our signal handlers
145 void tst_qdeclarativeconnection::targetChanged()
147 QDeclarativeEngine engine;
148 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/connection-targetchange.qml"));
149 QSGItem *item = qobject_cast<QSGItem*>(c.create());
152 QDeclarativeConnections *connections = item->findChild<QDeclarativeConnections*>("connections");
153 QVERIFY(connections);
155 QSGItem *item1 = item->findChild<QSGItem*>("item1");
158 item1->setWidth(200);
160 QSGItem *item2 = item->findChild<QSGItem*>("item2");
162 QVERIFY(connections->target() == item2);
164 // If we don't crash then we're OK
169 void tst_qdeclarativeconnection::unknownSignals_data()
171 QTest::addColumn<QString>("file");
172 QTest::addColumn<QString>("error");
174 QTest::newRow("basic") << "connection-unknownsignals.qml" << ":6:5: QML Connections: Cannot assign to non-existent property \"onFooBar\"";
175 QTest::newRow("parent") << "connection-unknownsignals-parent.qml" << ":6:5: QML Connections: Cannot assign to non-existent property \"onFooBar\"";
176 QTest::newRow("ignored") << "connection-unknownsignals-ignored.qml" << ""; // should be NO error
177 QTest::newRow("notarget") << "connection-unknownsignals-notarget.qml" << ""; // should be NO error
180 void tst_qdeclarativeconnection::unknownSignals()
182 QFETCH(QString, file);
183 QFETCH(QString, error);
185 QUrl url = QUrl::fromLocalFile(SRCDIR "/data/" + file);
186 if (!error.isEmpty()) {
187 QTest::ignoreMessage(QtWarningMsg, (url.toString() + error).toLatin1());
189 // QTest has no way to insist no message (i.e. fail)
192 QDeclarativeEngine engine;
193 QDeclarativeComponent c(&engine, url);
194 QSGItem *item = qobject_cast<QSGItem*>(c.create());
197 // check that connection is created (they are all runtime errors)
198 QDeclarativeConnections *connections = item->findChild<QDeclarativeConnections*>("connections");
199 QVERIFY(connections);
201 if (file == "connection-unknownsignals-ignored.qml")
202 QVERIFY(connections->ignoreUnknownSignals());
207 void tst_qdeclarativeconnection::errors_data()
209 QTest::addColumn<QString>("file");
210 QTest::addColumn<QString>("error");
212 QTest::newRow("no \"on\"") << "error-property.qml" << "Cannot assign to non-existent property \"fakeProperty\"";
213 QTest::newRow("3rd letter lowercase") << "error-property2.qml" << "Cannot assign to non-existent property \"onfakeProperty\"";
214 QTest::newRow("child object") << "error-object.qml" << "Connections: nested objects not allowed";
215 QTest::newRow("grouped object") << "error-syntax.qml" << "Connections: syntax error";
218 void tst_qdeclarativeconnection::errors()
220 QFETCH(QString, file);
221 QFETCH(QString, error);
223 QUrl url = QUrl::fromLocalFile(SRCDIR "/data/" + file);
225 QDeclarativeEngine engine;
226 QDeclarativeComponent c(&engine, url);
227 QVERIFY(c.isError() == true);
228 QList<QDeclarativeError> errors = c.errors();
229 QVERIFY(errors.count() == 1);
230 QCOMPARE(errors.at(0).description(), error);
234 class MyTestModuleApi : public QObject
237 Q_PROPERTY(int intProp READ intProp WRITE setIntProp NOTIFY intPropChanged)
240 MyTestModuleApi(QObject *parent = 0) : QObject(parent), m_intProp(0), m_changeCount(0) {}
241 ~MyTestModuleApi() {}
243 Q_INVOKABLE int otherMethod(int val) { return val + 4; }
245 int intProp() const { return m_intProp; }
246 void setIntProp(int val)
248 if (++m_changeCount % 3 == 0) emit otherSignal();
249 m_intProp = val; emit intPropChanged();
253 void intPropChanged();
261 static QObject *module_api_factory(QDeclarativeEngine *engine, QJSEngine *scriptEngine)
264 Q_UNUSED(scriptEngine)
265 MyTestModuleApi *api = new MyTestModuleApi();
270 void tst_qdeclarativeconnection::moduleApiTarget()
272 qmlRegisterModuleApi("MyTestModuleApi", 1, 0, module_api_factory);
273 QDeclarativeComponent component(&engine, QUrl(SRCDIR "/data/moduleapi-target.qml"));
274 QObject *object = component.create();
275 QVERIFY(object != 0);
277 QCOMPARE(object->property("moduleIntPropChangedCount").toInt(), 0);
278 QCOMPARE(object->property("moduleOtherSignalCount").toInt(), 0);
280 QMetaObject::invokeMethod(object, "setModuleIntProp");
281 QCOMPARE(object->property("moduleIntPropChangedCount").toInt(), 1);
282 QCOMPARE(object->property("moduleOtherSignalCount").toInt(), 0);
284 QMetaObject::invokeMethod(object, "setModuleIntProp");
285 QCOMPARE(object->property("moduleIntPropChangedCount").toInt(), 2);
286 QCOMPARE(object->property("moduleOtherSignalCount").toInt(), 0);
288 // the module API emits otherSignal every 3 times the int property changes.
289 QMetaObject::invokeMethod(object, "setModuleIntProp");
290 QCOMPARE(object->property("moduleIntPropChangedCount").toInt(), 3);
291 QCOMPARE(object->property("moduleOtherSignalCount").toInt(), 1);
296 QTEST_MAIN(tst_qdeclarativeconnection)
298 #include "tst_qdeclarativeconnection.moc"