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>
49 class tst_qdeclarativeconnection : public QObject
53 tst_qdeclarativeconnection();
61 void unknownSignals_data();
62 void unknownSignals();
65 void moduleApiTarget();
68 QDeclarativeEngine engine;
71 tst_qdeclarativeconnection::tst_qdeclarativeconnection()
75 void tst_qdeclarativeconnection::defaultValues()
77 QDeclarativeEngine engine;
78 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/test-connection3.qml"));
79 QDeclarativeConnections *item = qobject_cast<QDeclarativeConnections*>(c.create());
82 QVERIFY(item->target() == 0);
87 void tst_qdeclarativeconnection::properties()
89 QDeclarativeEngine engine;
90 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/test-connection2.qml"));
91 QDeclarativeConnections *item = qobject_cast<QDeclarativeConnections*>(c.create());
96 QVERIFY(item->target() == item);
101 void tst_qdeclarativeconnection::connection()
103 QDeclarativeEngine engine;
104 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/test-connection.qml"));
105 QSGItem *item = qobject_cast<QSGItem*>(c.create());
109 QCOMPARE(item->property("tested").toBool(), false);
110 QCOMPARE(item->width(), 50.);
111 emit item->setWidth(100.);
112 QCOMPARE(item->width(), 100.);
113 QCOMPARE(item->property("tested").toBool(), true);
118 void tst_qdeclarativeconnection::trimming()
120 QDeclarativeEngine engine;
121 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/trimming.qml"));
122 QSGItem *item = qobject_cast<QSGItem*>(c.create());
126 QCOMPARE(item->property("tested").toString(), QString(""));
127 int index = item->metaObject()->indexOfSignal("testMe(int,QString)");
128 QMetaMethod method = item->metaObject()->method(index);
130 Qt::DirectConnection,
132 Q_ARG(QString, "worked"));
133 QCOMPARE(item->property("tested").toString(), QString("worked5"));
138 // Confirm that target can be changed by one of our signal handlers
139 void tst_qdeclarativeconnection::targetChanged()
141 QDeclarativeEngine engine;
142 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/connection-targetchange.qml"));
143 QSGItem *item = qobject_cast<QSGItem*>(c.create());
146 QDeclarativeConnections *connections = item->findChild<QDeclarativeConnections*>("connections");
147 QVERIFY(connections);
149 QSGItem *item1 = item->findChild<QSGItem*>("item1");
152 item1->setWidth(200);
154 QSGItem *item2 = item->findChild<QSGItem*>("item2");
156 QVERIFY(connections->target() == item2);
158 // If we don't crash then we're OK
163 void tst_qdeclarativeconnection::unknownSignals_data()
165 QTest::addColumn<QString>("file");
166 QTest::addColumn<QString>("error");
168 QTest::newRow("basic") << "connection-unknownsignals.qml" << ":6:5: QML Connections: Cannot assign to non-existent property \"onFooBar\"";
169 QTest::newRow("parent") << "connection-unknownsignals-parent.qml" << ":6:5: QML Connections: Cannot assign to non-existent property \"onFooBar\"";
170 QTest::newRow("ignored") << "connection-unknownsignals-ignored.qml" << ""; // should be NO error
171 QTest::newRow("notarget") << "connection-unknownsignals-notarget.qml" << ""; // should be NO error
174 void tst_qdeclarativeconnection::unknownSignals()
176 QFETCH(QString, file);
177 QFETCH(QString, error);
179 QUrl url = QUrl::fromLocalFile(SRCDIR "/data/" + file);
180 if (!error.isEmpty()) {
181 QTest::ignoreMessage(QtWarningMsg, (url.toString() + error).toLatin1());
183 // QTest has no way to insist no message (i.e. fail)
186 QDeclarativeEngine engine;
187 QDeclarativeComponent c(&engine, url);
188 QSGItem *item = qobject_cast<QSGItem*>(c.create());
191 // check that connection is created (they are all runtime errors)
192 QDeclarativeConnections *connections = item->findChild<QDeclarativeConnections*>("connections");
193 QVERIFY(connections);
195 if (file == "connection-unknownsignals-ignored.qml")
196 QVERIFY(connections->ignoreUnknownSignals());
201 void tst_qdeclarativeconnection::errors_data()
203 QTest::addColumn<QString>("file");
204 QTest::addColumn<QString>("error");
206 QTest::newRow("no \"on\"") << "error-property.qml" << "Cannot assign to non-existent property \"fakeProperty\"";
207 QTest::newRow("3rd letter lowercase") << "error-property2.qml" << "Cannot assign to non-existent property \"onfakeProperty\"";
208 QTest::newRow("child object") << "error-object.qml" << "Connections: nested objects not allowed";
209 QTest::newRow("grouped object") << "error-syntax.qml" << "Connections: syntax error";
212 void tst_qdeclarativeconnection::errors()
214 QFETCH(QString, file);
215 QFETCH(QString, error);
217 QUrl url = QUrl::fromLocalFile(SRCDIR "/data/" + file);
219 QDeclarativeEngine engine;
220 QDeclarativeComponent c(&engine, url);
221 QVERIFY(c.isError() == true);
222 QList<QDeclarativeError> errors = c.errors();
223 QVERIFY(errors.count() == 1);
224 QCOMPARE(errors.at(0).description(), error);
228 class MyTestModuleApi : public QObject
231 Q_PROPERTY(int intProp READ intProp WRITE setIntProp NOTIFY intPropChanged)
234 MyTestModuleApi(QObject *parent = 0) : QObject(parent), m_intProp(0), m_changeCount(0) {}
235 ~MyTestModuleApi() {}
237 Q_INVOKABLE int otherMethod(int val) { return val + 4; }
239 int intProp() const { return m_intProp; }
240 void setIntProp(int val)
242 if (++m_changeCount % 3 == 0) emit otherSignal();
243 m_intProp = val; emit intPropChanged();
247 void intPropChanged();
255 static QObject *module_api_factory(QDeclarativeEngine *engine, QJSEngine *scriptEngine)
258 Q_UNUSED(scriptEngine)
259 MyTestModuleApi *api = new MyTestModuleApi();
264 void tst_qdeclarativeconnection::moduleApiTarget()
266 qmlRegisterModuleApi("MyTestModuleApi", 1, 0, module_api_factory);
267 QDeclarativeComponent component(&engine, QUrl(SRCDIR "/data/moduleapi-target.qml"));
268 QObject *object = component.create();
269 QVERIFY(object != 0);
271 QCOMPARE(object->property("moduleIntPropChangedCount").toInt(), 0);
272 QCOMPARE(object->property("moduleOtherSignalCount").toInt(), 0);
274 QMetaObject::invokeMethod(object, "setModuleIntProp");
275 QCOMPARE(object->property("moduleIntPropChangedCount").toInt(), 1);
276 QCOMPARE(object->property("moduleOtherSignalCount").toInt(), 0);
278 QMetaObject::invokeMethod(object, "setModuleIntProp");
279 QCOMPARE(object->property("moduleIntPropChangedCount").toInt(), 2);
280 QCOMPARE(object->property("moduleOtherSignalCount").toInt(), 0);
282 // the module API emits otherSignal every 3 times the int property changes.
283 QMetaObject::invokeMethod(object, "setModuleIntProp");
284 QCOMPARE(object->property("moduleIntPropChangedCount").toInt(), 3);
285 QCOMPARE(object->property("moduleOtherSignalCount").toInt(), 1);
290 QTEST_MAIN(tst_qdeclarativeconnection)
292 #include "tst_qdeclarativeconnection.moc"