Allow conversion of QObject Module API to QVariant
[profile/ivi/qtdeclarative.git] / tests / auto / declarative / qdeclarativeconnection / tst_qdeclarativeconnection.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
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 <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>
48
49 #ifdef Q_OS_SYMBIAN
50 // In Symbian OS test data is located in applications private dir
51 #define SRCDIR "."
52 #endif
53
54 class tst_qdeclarativeconnection : public QObject
55
56 {
57     Q_OBJECT
58 public:
59     tst_qdeclarativeconnection();
60
61 private slots:
62     void defaultValues();
63     void properties();
64     void connection();
65     void trimming();
66     void targetChanged();
67     void unknownSignals_data();
68     void unknownSignals();
69     void errors_data();
70     void errors();
71     void moduleApiTarget();
72
73 private:
74     QDeclarativeEngine engine;
75 };
76
77 tst_qdeclarativeconnection::tst_qdeclarativeconnection()
78 {
79 }
80
81 void tst_qdeclarativeconnection::defaultValues()
82 {
83     QDeclarativeEngine engine;
84     QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/test-connection3.qml"));
85     QDeclarativeConnections *item = qobject_cast<QDeclarativeConnections*>(c.create());
86
87     QVERIFY(item != 0);
88     QVERIFY(item->target() == 0);
89
90     delete item;
91 }
92
93 void tst_qdeclarativeconnection::properties()
94 {
95     QDeclarativeEngine engine;
96     QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/test-connection2.qml"));
97     QDeclarativeConnections *item = qobject_cast<QDeclarativeConnections*>(c.create());
98
99     QVERIFY(item != 0);
100
101     QVERIFY(item != 0);
102     QVERIFY(item->target() == item);
103
104     delete item;
105 }
106
107 void tst_qdeclarativeconnection::connection()
108 {
109     QDeclarativeEngine engine;
110     QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/test-connection.qml"));
111     QSGItem *item = qobject_cast<QSGItem*>(c.create());
112
113     QVERIFY(item != 0);
114
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);
120
121     delete item;
122 }
123
124 void tst_qdeclarativeconnection::trimming()
125 {
126     QDeclarativeEngine engine;
127     QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/trimming.qml"));
128     QSGItem *item = qobject_cast<QSGItem*>(c.create());
129
130     QVERIFY(item != 0);
131
132     QCOMPARE(item->property("tested").toString(), QString(""));
133     int index = item->metaObject()->indexOfSignal("testMe(int,QString)");
134     QMetaMethod method = item->metaObject()->method(index);
135     method.invoke(item,
136                   Qt::DirectConnection,
137                   Q_ARG(int, 5),
138                   Q_ARG(QString, "worked"));
139     QCOMPARE(item->property("tested").toString(), QString("worked5"));
140
141     delete item;
142 }
143
144 // Confirm that target can be changed by one of our signal handlers
145 void tst_qdeclarativeconnection::targetChanged()
146 {
147     QDeclarativeEngine engine;
148     QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/connection-targetchange.qml"));
149     QSGItem *item = qobject_cast<QSGItem*>(c.create());
150     QVERIFY(item != 0);
151
152     QDeclarativeConnections *connections = item->findChild<QDeclarativeConnections*>("connections");
153     QVERIFY(connections);
154
155     QSGItem *item1 = item->findChild<QSGItem*>("item1");
156     QVERIFY(item1);
157
158     item1->setWidth(200);
159
160     QSGItem *item2 = item->findChild<QSGItem*>("item2");
161     QVERIFY(item2);
162     QVERIFY(connections->target() == item2);
163
164     // If we don't crash then we're OK
165
166     delete item;
167 }
168
169 void tst_qdeclarativeconnection::unknownSignals_data()
170 {
171     QTest::addColumn<QString>("file");
172     QTest::addColumn<QString>("error");
173
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
178 }
179
180 void tst_qdeclarativeconnection::unknownSignals()
181 {
182     QFETCH(QString, file);
183     QFETCH(QString, error);
184
185     QUrl url = QUrl::fromLocalFile(SRCDIR "/data/" + file);
186     if (!error.isEmpty()) {
187         QTest::ignoreMessage(QtWarningMsg, (url.toString() + error).toLatin1());
188     } else {
189         // QTest has no way to insist no message (i.e. fail)
190     }
191
192     QDeclarativeEngine engine;
193     QDeclarativeComponent c(&engine, url);
194     QSGItem *item = qobject_cast<QSGItem*>(c.create());
195     QVERIFY(item != 0);
196
197     // check that connection is created (they are all runtime errors)
198     QDeclarativeConnections *connections = item->findChild<QDeclarativeConnections*>("connections");
199     QVERIFY(connections);
200
201     if (file == "connection-unknownsignals-ignored.qml")
202         QVERIFY(connections->ignoreUnknownSignals());
203
204     delete item;
205 }
206
207 void tst_qdeclarativeconnection::errors_data()
208 {
209     QTest::addColumn<QString>("file");
210     QTest::addColumn<QString>("error");
211
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";
216 }
217
218 void tst_qdeclarativeconnection::errors()
219 {
220     QFETCH(QString, file);
221     QFETCH(QString, error);
222
223     QUrl url = QUrl::fromLocalFile(SRCDIR "/data/" + file);
224
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);
231 }
232
233
234 class MyTestModuleApi : public QObject
235 {
236 Q_OBJECT
237 Q_PROPERTY(int intProp READ intProp WRITE setIntProp NOTIFY intPropChanged)
238
239 public:
240     MyTestModuleApi(QObject *parent = 0) : QObject(parent), m_intProp(0), m_changeCount(0) {}
241     ~MyTestModuleApi() {}
242
243     Q_INVOKABLE int otherMethod(int val) { return val + 4; }
244
245     int intProp() const { return m_intProp; }
246     void setIntProp(int val)
247     {
248         if (++m_changeCount % 3 == 0) emit otherSignal();
249         m_intProp = val; emit intPropChanged();
250     }
251
252 signals:
253     void intPropChanged();
254     void otherSignal();
255
256 private:
257     int m_intProp;
258     int m_changeCount;
259 };
260
261 static QObject *module_api_factory(QDeclarativeEngine *engine, QJSEngine *scriptEngine)
262 {
263    Q_UNUSED(engine)
264    Q_UNUSED(scriptEngine)
265    MyTestModuleApi *api = new MyTestModuleApi();
266    return api;
267 }
268
269 // QTBUG-20937
270 void tst_qdeclarativeconnection::moduleApiTarget()
271 {
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);
276
277     QCOMPARE(object->property("moduleIntPropChangedCount").toInt(), 0);
278     QCOMPARE(object->property("moduleOtherSignalCount").toInt(), 0);
279
280     QMetaObject::invokeMethod(object, "setModuleIntProp");
281     QCOMPARE(object->property("moduleIntPropChangedCount").toInt(), 1);
282     QCOMPARE(object->property("moduleOtherSignalCount").toInt(), 0);
283
284     QMetaObject::invokeMethod(object, "setModuleIntProp");
285     QCOMPARE(object->property("moduleIntPropChangedCount").toInt(), 2);
286     QCOMPARE(object->property("moduleOtherSignalCount").toInt(), 0);
287
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);
292
293     delete object;
294 }
295
296 QTEST_MAIN(tst_qdeclarativeconnection)
297
298 #include "tst_qdeclarativeconnection.moc"