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 <QtQuick1/private/qdeclarativeconnections_p.h>
45 #include <QtQuick1/private/qdeclarativeitem_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();
67 QDeclarativeEngine engine;
70 tst_qdeclarativeconnection::tst_qdeclarativeconnection()
74 void tst_qdeclarativeconnection::defaultValues()
76 QDeclarativeEngine engine;
77 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/test-connection3.qml"));
78 QDeclarative1Connections *item = qobject_cast<QDeclarative1Connections*>(c.create());
81 QVERIFY(item->target() == 0);
86 void tst_qdeclarativeconnection::properties()
88 QDeclarativeEngine engine;
89 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/test-connection2.qml"));
90 QDeclarative1Connections *item = qobject_cast<QDeclarative1Connections*>(c.create());
95 QVERIFY(item->target() == item);
100 void tst_qdeclarativeconnection::connection()
102 QDeclarativeEngine engine;
103 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/test-connection.qml"));
104 QDeclarativeItem *item = qobject_cast<QDeclarativeItem*>(c.create());
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);
117 void tst_qdeclarativeconnection::trimming()
119 QDeclarativeEngine engine;
120 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/trimming.qml"));
121 QDeclarativeItem *item = qobject_cast<QDeclarativeItem*>(c.create());
125 QCOMPARE(item->property("tested").toString(), QString(""));
126 int index = item->metaObject()->indexOfSignal("testMe(int,QString)");
127 QMetaMethod method = item->metaObject()->method(index);
129 Qt::DirectConnection,
131 Q_ARG(QString, "worked"));
132 QCOMPARE(item->property("tested").toString(), QString("worked5"));
137 // Confirm that target can be changed by one of our signal handlers
138 void tst_qdeclarativeconnection::targetChanged()
140 QDeclarativeEngine engine;
141 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/connection-targetchange.qml"));
142 QDeclarativeItem *item = qobject_cast<QDeclarativeItem*>(c.create());
145 QDeclarative1Connections *connections = item->findChild<QDeclarative1Connections*>("connections");
146 QVERIFY(connections);
148 QDeclarativeItem *item1 = item->findChild<QDeclarativeItem*>("item1");
151 item1->setWidth(200);
153 QDeclarativeItem *item2 = item->findChild<QDeclarativeItem*>("item2");
155 QVERIFY(connections->target() == item2);
157 // If we don't crash then we're OK
162 void tst_qdeclarativeconnection::unknownSignals_data()
164 QTest::addColumn<QString>("file");
165 QTest::addColumn<QString>("error");
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
173 void tst_qdeclarativeconnection::unknownSignals()
175 QFETCH(QString, file);
176 QFETCH(QString, error);
178 QUrl url = QUrl::fromLocalFile(SRCDIR "/data/" + file);
179 if (!error.isEmpty()) {
180 QTest::ignoreMessage(QtWarningMsg, (url.toString() + error).toLatin1());
182 // QTest has no way to insist no message (i.e. fail)
185 QDeclarativeEngine engine;
186 QDeclarativeComponent c(&engine, url);
187 QDeclarativeItem *item = qobject_cast<QDeclarativeItem*>(c.create());
190 // check that connection is created (they are all runtime errors)
191 QDeclarative1Connections *connections = item->findChild<QDeclarative1Connections*>("connections");
192 QVERIFY(connections);
194 if (file == "connection-unknownsignals-ignored.qml")
195 QVERIFY(connections->ignoreUnknownSignals());
200 void tst_qdeclarativeconnection::errors_data()
202 QTest::addColumn<QString>("file");
203 QTest::addColumn<QString>("error");
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";
211 void tst_qdeclarativeconnection::errors()
213 QFETCH(QString, file);
214 QFETCH(QString, error);
216 QUrl url = QUrl::fromLocalFile(SRCDIR "/data/" + file);
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);
226 QTEST_MAIN(tst_qdeclarativeconnection)
228 #include "tst_qdeclarativeconnection.moc"