1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the test suite of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** 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 <QtDeclarative/qdeclarativescriptstring.h>
48 class tst_qdeclarativeconnection : public QObject
52 tst_qdeclarativeconnection();
60 void unknownSignals_data();
61 void unknownSignals();
66 QDeclarativeEngine engine;
69 tst_qdeclarativeconnection::tst_qdeclarativeconnection()
73 void tst_qdeclarativeconnection::defaultValues()
75 QDeclarativeEngine engine;
76 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/test-connection3.qml"));
77 QDeclarative1Connections *item = qobject_cast<QDeclarative1Connections*>(c.create());
80 QVERIFY(item->target() == 0);
85 void tst_qdeclarativeconnection::properties()
87 QDeclarativeEngine engine;
88 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/test-connection2.qml"));
89 QDeclarative1Connections *item = qobject_cast<QDeclarative1Connections*>(c.create());
94 QVERIFY(item->target() == item);
99 void tst_qdeclarativeconnection::connection()
101 QDeclarativeEngine engine;
102 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/test-connection.qml"));
103 QDeclarativeItem *item = qobject_cast<QDeclarativeItem*>(c.create());
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);
116 void tst_qdeclarativeconnection::trimming()
118 QDeclarativeEngine engine;
119 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/trimming.qml"));
120 QDeclarativeItem *item = qobject_cast<QDeclarativeItem*>(c.create());
124 QCOMPARE(item->property("tested").toString(), QString(""));
125 int index = item->metaObject()->indexOfSignal("testMe(int,QString)");
126 QMetaMethod method = item->metaObject()->method(index);
128 Qt::DirectConnection,
130 Q_ARG(QString, "worked"));
131 QCOMPARE(item->property("tested").toString(), QString("worked5"));
136 // Confirm that target can be changed by one of our signal handlers
137 void tst_qdeclarativeconnection::targetChanged()
139 QDeclarativeEngine engine;
140 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/connection-targetchange.qml"));
141 QDeclarativeItem *item = qobject_cast<QDeclarativeItem*>(c.create());
144 QDeclarative1Connections *connections = item->findChild<QDeclarative1Connections*>("connections");
145 QVERIFY(connections);
147 QDeclarativeItem *item1 = item->findChild<QDeclarativeItem*>("item1");
150 item1->setWidth(200);
152 QDeclarativeItem *item2 = item->findChild<QDeclarativeItem*>("item2");
154 QVERIFY(connections->target() == item2);
156 // If we don't crash then we're OK
161 void tst_qdeclarativeconnection::unknownSignals_data()
163 QTest::addColumn<QString>("file");
164 QTest::addColumn<QString>("error");
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
172 void tst_qdeclarativeconnection::unknownSignals()
174 QFETCH(QString, file);
175 QFETCH(QString, error);
177 QUrl url = QUrl::fromLocalFile(SRCDIR "/data/" + file);
178 if (!error.isEmpty()) {
179 QTest::ignoreMessage(QtWarningMsg, (url.toString() + error).toLatin1());
181 // QTest has no way to insist no message (i.e. fail)
184 QDeclarativeEngine engine;
185 QDeclarativeComponent c(&engine, url);
186 QDeclarativeItem *item = qobject_cast<QDeclarativeItem*>(c.create());
189 // check that connection is created (they are all runtime errors)
190 QDeclarative1Connections *connections = item->findChild<QDeclarative1Connections*>("connections");
191 QVERIFY(connections);
193 if (file == "connection-unknownsignals-ignored.qml")
194 QVERIFY(connections->ignoreUnknownSignals());
199 void tst_qdeclarativeconnection::errors_data()
201 QTest::addColumn<QString>("file");
202 QTest::addColumn<QString>("error");
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";
210 void tst_qdeclarativeconnection::errors()
212 QFETCH(QString, file);
213 QFETCH(QString, error);
215 QUrl url = QUrl::fromLocalFile(SRCDIR "/data/" + file);
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);
225 QTEST_MAIN(tst_qdeclarativeconnection)
227 #include "tst_qdeclarativeconnection.moc"