299d4b870ad19311639fd08e6b1b6b45d99e2ee0
[profile/ivi/qtdeclarative.git] / tests / auto / qtquick1 / qdeclarativeconnection / tst_qdeclarativeconnection.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
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 <QtQuick1/private/qdeclarativeconnections_p.h>
45 #include <QtQuick1/private/qdeclarativeitem_p.h>
46 #include <QtDeclarative/qdeclarativescriptstring.h>
47
48 class tst_qdeclarativeconnection : public QObject
49 {
50     Q_OBJECT
51 public:
52     tst_qdeclarativeconnection();
53
54 private slots:
55     void defaultValues();
56     void properties();
57     void connection();
58     void trimming();
59     void targetChanged();
60     void unknownSignals_data();
61     void unknownSignals();
62     void errors_data();
63     void errors();
64
65 private:
66     QDeclarativeEngine engine;
67 };
68
69 tst_qdeclarativeconnection::tst_qdeclarativeconnection()
70 {
71 }
72
73 void tst_qdeclarativeconnection::defaultValues()
74 {
75     QDeclarativeEngine engine;
76     QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/test-connection3.qml"));
77     QDeclarative1Connections *item = qobject_cast<QDeclarative1Connections*>(c.create());
78
79     QVERIFY(item != 0);
80     QVERIFY(item->target() == 0);
81
82     delete item;
83 }
84
85 void tst_qdeclarativeconnection::properties()
86 {
87     QDeclarativeEngine engine;
88     QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/test-connection2.qml"));
89     QDeclarative1Connections *item = qobject_cast<QDeclarative1Connections*>(c.create());
90
91     QVERIFY(item != 0);
92
93     QVERIFY(item != 0);
94     QVERIFY(item->target() == item);
95
96     delete item;
97 }
98
99 void tst_qdeclarativeconnection::connection()
100 {
101     QDeclarativeEngine engine;
102     QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/test-connection.qml"));
103     QDeclarativeItem *item = qobject_cast<QDeclarativeItem*>(c.create());
104
105     QVERIFY(item != 0);
106
107     QCOMPARE(item->property("tested").toBool(), false);
108     QCOMPARE(item->width(), 50.);
109     emit item->setWidth(100.);
110     QCOMPARE(item->width(), 100.);
111     QCOMPARE(item->property("tested").toBool(), true);
112
113     delete item;
114 }
115
116 void tst_qdeclarativeconnection::trimming()
117 {
118     QDeclarativeEngine engine;
119     QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/trimming.qml"));
120     QDeclarativeItem *item = qobject_cast<QDeclarativeItem*>(c.create());
121
122     QVERIFY(item != 0);
123
124     QCOMPARE(item->property("tested").toString(), QString(""));
125     int index = item->metaObject()->indexOfSignal("testMe(int,QString)");
126     QMetaMethod method = item->metaObject()->method(index);
127     method.invoke(item,
128                   Qt::DirectConnection,
129                   Q_ARG(int, 5),
130                   Q_ARG(QString, "worked"));
131     QCOMPARE(item->property("tested").toString(), QString("worked5"));
132
133     delete item;
134 }
135
136 // Confirm that target can be changed by one of our signal handlers
137 void tst_qdeclarativeconnection::targetChanged()
138 {
139     QDeclarativeEngine engine;
140     QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/connection-targetchange.qml"));
141     QDeclarativeItem *item = qobject_cast<QDeclarativeItem*>(c.create());
142     QVERIFY(item != 0);
143
144     QDeclarative1Connections *connections = item->findChild<QDeclarative1Connections*>("connections");
145     QVERIFY(connections);
146
147     QDeclarativeItem *item1 = item->findChild<QDeclarativeItem*>("item1");
148     QVERIFY(item1);
149
150     item1->setWidth(200);
151
152     QDeclarativeItem *item2 = item->findChild<QDeclarativeItem*>("item2");
153     QVERIFY(item2);
154     QVERIFY(connections->target() == item2);
155
156     // If we don't crash then we're OK
157
158     delete item;
159 }
160
161 void tst_qdeclarativeconnection::unknownSignals_data()
162 {
163     QTest::addColumn<QString>("file");
164     QTest::addColumn<QString>("error");
165
166     QTest::newRow("basic") << "connection-unknownsignals.qml" << ":6:5: QML Connections: Cannot assign to non-existent property \"onFooBar\"";
167     QTest::newRow("parent") << "connection-unknownsignals-parent.qml" << ":6:5: QML Connections: Cannot assign to non-existent property \"onFooBar\"";
168     QTest::newRow("ignored") << "connection-unknownsignals-ignored.qml" << ""; // should be NO error
169     QTest::newRow("notarget") << "connection-unknownsignals-notarget.qml" << ""; // should be NO error
170 }
171
172 void tst_qdeclarativeconnection::unknownSignals()
173 {
174     QFETCH(QString, file);
175     QFETCH(QString, error);
176
177     QUrl url = QUrl::fromLocalFile(SRCDIR "/data/" + file);
178     if (!error.isEmpty()) {
179         QTest::ignoreMessage(QtWarningMsg, (url.toString() + error).toLatin1());
180     } else {
181         // QTest has no way to insist no message (i.e. fail)
182     }
183
184     QDeclarativeEngine engine;
185     QDeclarativeComponent c(&engine, url);
186     QDeclarativeItem *item = qobject_cast<QDeclarativeItem*>(c.create());
187     QVERIFY(item != 0);
188
189     // check that connection is created (they are all runtime errors)
190     QDeclarative1Connections *connections = item->findChild<QDeclarative1Connections*>("connections");
191     QVERIFY(connections);
192
193     if (file == "connection-unknownsignals-ignored.qml")
194         QVERIFY(connections->ignoreUnknownSignals());
195
196     delete item;
197 }
198
199 void tst_qdeclarativeconnection::errors_data()
200 {
201     QTest::addColumn<QString>("file");
202     QTest::addColumn<QString>("error");
203
204     QTest::newRow("no \"on\"") << "error-property.qml" << "Cannot assign to non-existent property \"fakeProperty\"";
205     QTest::newRow("3rd letter lowercase") << "error-property2.qml" << "Cannot assign to non-existent property \"onfakeProperty\"";
206     QTest::newRow("child object") << "error-object.qml" << "Connections: nested objects not allowed";
207     QTest::newRow("grouped object") << "error-syntax.qml" << "Connections: syntax error";
208 }
209
210 void tst_qdeclarativeconnection::errors()
211 {
212     QFETCH(QString, file);
213     QFETCH(QString, error);
214
215     QUrl url = QUrl::fromLocalFile(SRCDIR "/data/" + file);
216
217     QDeclarativeEngine engine;
218     QDeclarativeComponent c(&engine, url);
219     QVERIFY(c.isError() == true);
220     QList<QDeclarativeError> errors = c.errors();
221     QVERIFY(errors.count() == 1);
222     QCOMPARE(errors.at(0).description(), error);
223 }
224
225 QTEST_MAIN(tst_qdeclarativeconnection)
226
227 #include "tst_qdeclarativeconnection.moc"