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