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>
50 // In Symbian OS test data is located in applications private dir
54 class tst_qdeclarativeconnection : public QObject
59 tst_qdeclarativeconnection();
67 void unknownSignals_data();
68 void unknownSignals();
73 QDeclarativeEngine engine;
76 tst_qdeclarativeconnection::tst_qdeclarativeconnection()
80 void tst_qdeclarativeconnection::defaultValues()
82 QDeclarativeEngine engine;
83 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/test-connection3.qml"));
84 QDeclarative1Connections *item = qobject_cast<QDeclarative1Connections*>(c.create());
87 QVERIFY(item->target() == 0);
92 void tst_qdeclarativeconnection::properties()
94 QDeclarativeEngine engine;
95 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/test-connection2.qml"));
96 QDeclarative1Connections *item = qobject_cast<QDeclarative1Connections*>(c.create());
101 QVERIFY(item->target() == item);
106 void tst_qdeclarativeconnection::connection()
108 QDeclarativeEngine engine;
109 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/test-connection.qml"));
110 QDeclarativeItem *item = qobject_cast<QDeclarativeItem*>(c.create());
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);
123 void tst_qdeclarativeconnection::trimming()
125 QDeclarativeEngine engine;
126 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/trimming.qml"));
127 QDeclarativeItem *item = qobject_cast<QDeclarativeItem*>(c.create());
131 QCOMPARE(item->property("tested").toString(), QString(""));
132 int index = item->metaObject()->indexOfSignal("testMe(int,QString)");
133 QMetaMethod method = item->metaObject()->method(index);
135 Qt::DirectConnection,
137 Q_ARG(QString, "worked"));
138 QCOMPARE(item->property("tested").toString(), QString("worked5"));
143 // Confirm that target can be changed by one of our signal handlers
144 void tst_qdeclarativeconnection::targetChanged()
146 QDeclarativeEngine engine;
147 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/connection-targetchange.qml"));
148 QDeclarativeItem *item = qobject_cast<QDeclarativeItem*>(c.create());
151 QDeclarative1Connections *connections = item->findChild<QDeclarative1Connections*>("connections");
152 QVERIFY(connections);
154 QDeclarativeItem *item1 = item->findChild<QDeclarativeItem*>("item1");
157 item1->setWidth(200);
159 QDeclarativeItem *item2 = item->findChild<QDeclarativeItem*>("item2");
161 QVERIFY(connections->target() == item2);
163 // If we don't crash then we're OK
168 void tst_qdeclarativeconnection::unknownSignals_data()
170 QTest::addColumn<QString>("file");
171 QTest::addColumn<QString>("error");
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
179 void tst_qdeclarativeconnection::unknownSignals()
181 QFETCH(QString, file);
182 QFETCH(QString, error);
184 QUrl url = QUrl::fromLocalFile(SRCDIR "/data/" + file);
185 if (!error.isEmpty()) {
186 QTest::ignoreMessage(QtWarningMsg, (url.toString() + error).toLatin1());
188 // QTest has no way to insist no message (i.e. fail)
191 QDeclarativeEngine engine;
192 QDeclarativeComponent c(&engine, url);
193 QDeclarativeItem *item = qobject_cast<QDeclarativeItem*>(c.create());
196 // check that connection is created (they are all runtime errors)
197 QDeclarative1Connections *connections = item->findChild<QDeclarative1Connections*>("connections");
198 QVERIFY(connections);
200 if (file == "connection-unknownsignals-ignored.qml")
201 QVERIFY(connections->ignoreUnknownSignals());
206 void tst_qdeclarativeconnection::errors_data()
208 QTest::addColumn<QString>("file");
209 QTest::addColumn<QString>("error");
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";
217 void tst_qdeclarativeconnection::errors()
219 QFETCH(QString, file);
220 QFETCH(QString, error);
222 QUrl url = QUrl::fromLocalFile(SRCDIR "/data/" + file);
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);
232 QTEST_MAIN(tst_qdeclarativeconnection)
234 #include "tst_qdeclarativeconnection.moc"