Initial import from the monolithic Qt.
[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 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file.  Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23 **
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **
32 **
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/qdeclarativeitem_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
72 private:
73     QDeclarativeEngine engine;
74 };
75
76 tst_qdeclarativeconnection::tst_qdeclarativeconnection()
77 {
78 }
79
80 void tst_qdeclarativeconnection::defaultValues()
81 {
82     QDeclarativeEngine engine;
83     QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/test-connection3.qml"));
84     QDeclarativeConnections *item = qobject_cast<QDeclarativeConnections*>(c.create());
85
86     QVERIFY(item != 0);
87     QVERIFY(item->target() == 0);
88
89     delete item;
90 }
91
92 void tst_qdeclarativeconnection::properties()
93 {
94     QDeclarativeEngine engine;
95     QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/test-connection2.qml"));
96     QDeclarativeConnections *item = qobject_cast<QDeclarativeConnections*>(c.create());
97
98     QVERIFY(item != 0);
99
100     QVERIFY(item != 0);
101     QVERIFY(item->target() == item);
102
103     delete item;
104 }
105
106 void tst_qdeclarativeconnection::connection()
107 {
108     QDeclarativeEngine engine;
109     QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/test-connection.qml"));
110     QDeclarativeItem *item = qobject_cast<QDeclarativeItem*>(c.create());
111
112     QVERIFY(item != 0);
113
114     QCOMPARE(item->property("tested").toBool(), false);
115     QCOMPARE(item->width(), 50.);
116     emit item->setWidth(100.);
117     QCOMPARE(item->width(), 100.);
118     QCOMPARE(item->property("tested").toBool(), true);
119
120     delete item;
121 }
122
123 void tst_qdeclarativeconnection::trimming()
124 {
125     QDeclarativeEngine engine;
126     QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/trimming.qml"));
127     QDeclarativeItem *item = qobject_cast<QDeclarativeItem*>(c.create());
128
129     QVERIFY(item != 0);
130
131     QCOMPARE(item->property("tested").toString(), QString(""));
132     int index = item->metaObject()->indexOfSignal("testMe(int,QString)");
133     QMetaMethod method = item->metaObject()->method(index);
134     method.invoke(item,
135                   Qt::DirectConnection,
136                   Q_ARG(int, 5),
137                   Q_ARG(QString, "worked"));
138     QCOMPARE(item->property("tested").toString(), QString("worked5"));
139
140     delete item;
141 }
142
143 // Confirm that target can be changed by one of our signal handlers
144 void tst_qdeclarativeconnection::targetChanged()
145 {
146     QDeclarativeEngine engine;
147     QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/connection-targetchange.qml"));
148     QDeclarativeItem *item = qobject_cast<QDeclarativeItem*>(c.create());
149     QVERIFY(item != 0);
150
151     QDeclarativeConnections *connections = item->findChild<QDeclarativeConnections*>("connections");
152     QVERIFY(connections);
153
154     QDeclarativeItem *item1 = item->findChild<QDeclarativeItem*>("item1");
155     QVERIFY(item1);
156
157     item1->setWidth(200);
158
159     QDeclarativeItem *item2 = item->findChild<QDeclarativeItem*>("item2");
160     QVERIFY(item2);
161     QVERIFY(connections->target() == item2);
162
163     // If we don't crash then we're OK
164
165     delete item;
166 }
167
168 void tst_qdeclarativeconnection::unknownSignals_data()
169 {
170     QTest::addColumn<QString>("file");
171     QTest::addColumn<QString>("error");
172
173     QTest::newRow("basic") << "connection-unknownsignals.qml" << ":6:5: QML Connections: Cannot assign to non-existent property \"onFooBar\"";
174     QTest::newRow("parent") << "connection-unknownsignals-parent.qml" << ":6:5: QML Connections: Cannot assign to non-existent property \"onFooBar\"";
175     QTest::newRow("ignored") << "connection-unknownsignals-ignored.qml" << ""; // should be NO error
176     QTest::newRow("notarget") << "connection-unknownsignals-notarget.qml" << ""; // should be NO error
177 }
178
179 void tst_qdeclarativeconnection::unknownSignals()
180 {
181     QFETCH(QString, file);
182     QFETCH(QString, error);
183
184     QUrl url = QUrl::fromLocalFile(SRCDIR "/data/" + file);
185     if (!error.isEmpty()) {
186         QTest::ignoreMessage(QtWarningMsg, (url.toString() + error).toLatin1());
187     } else {
188         // QTest has no way to insist no message (i.e. fail)
189     }
190
191     QDeclarativeEngine engine;
192     QDeclarativeComponent c(&engine, url);
193     QDeclarativeItem *item = qobject_cast<QDeclarativeItem*>(c.create());
194     QVERIFY(item != 0);
195
196     // check that connection is created (they are all runtime errors)
197     QDeclarativeConnections *connections = item->findChild<QDeclarativeConnections*>("connections");
198     QVERIFY(connections);
199
200     if (file == "connection-unknownsignals-ignored.qml")
201         QVERIFY(connections->ignoreUnknownSignals());
202
203     delete item;
204 }
205
206 void tst_qdeclarativeconnection::errors_data()
207 {
208     QTest::addColumn<QString>("file");
209     QTest::addColumn<QString>("error");
210
211     QTest::newRow("no \"on\"") << "error-property.qml" << "Cannot assign to non-existent property \"fakeProperty\"";
212     QTest::newRow("3rd letter lowercase") << "error-property2.qml" << "Cannot assign to non-existent property \"onfakeProperty\"";
213     QTest::newRow("child object") << "error-object.qml" << "Connections: nested objects not allowed";
214     QTest::newRow("grouped object") << "error-syntax.qml" << "Connections: syntax error";
215 }
216
217 void tst_qdeclarativeconnection::errors()
218 {
219     QFETCH(QString, file);
220     QFETCH(QString, error);
221
222     QUrl url = QUrl::fromLocalFile(SRCDIR "/data/" + file);
223
224     QDeclarativeEngine engine;
225     QDeclarativeComponent c(&engine, url);
226     QVERIFY(c.isError() == true);
227     QList<QDeclarativeError> errors = c.errors();
228     QVERIFY(errors.count() == 1);
229     QCOMPARE(errors.at(0).description(), error);
230 }
231
232 QTEST_MAIN(tst_qdeclarativeconnection)
233
234 #include "tst_qdeclarativeconnection.moc"