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 <QtWidgets/QGraphicsWidget>
43 #include <QtWidgets/QGraphicsScene>
46 #include <QtDeclarative/qdeclarativeengine.h>
47 #include <QtDeclarative/qdeclarativecomponent.h>
48 #include <QtQuick1/private/qdeclarativeloader_p.h>
49 #include "testhttpserver.h"
51 #define SERVER_PORT 14450
53 inline QUrl TEST_FILE(const QString &filename)
55 return QUrl::fromLocalFile(QLatin1String(SRCDIR) + QLatin1String("/data/") + filename);
58 class tst_QDeclarative1Loader : public QObject
63 tst_QDeclarative1Loader();
66 void sourceOrComponent();
67 void sourceOrComponent_data();
69 void urlToComponent();
70 void componentToUrl();
71 void anchoredLoader();
72 void sizeLoaderToItem();
73 void sizeItemToLoader();
75 void sizeLoaderToGraphicsWidget();
76 void sizeGraphicsWidgetToLoader();
77 void noResizeGraphicsWidget();
78 void networkRequestUrl();
79 void failNetworkRequest();
80 // void networkComponent();
82 void deleteComponentCrash();
85 void creationContext();
91 QDeclarativeEngine engine;
95 tst_QDeclarative1Loader::tst_QDeclarative1Loader()
99 void tst_QDeclarative1Loader::sourceOrComponent()
101 QFETCH(QString, sourceDefinition);
102 QFETCH(QUrl, sourceUrl);
103 QFETCH(QString, errorString);
105 bool error = !errorString.isEmpty();
107 QTest::ignoreMessage(QtWarningMsg, errorString.toUtf8().constData());
109 QDeclarativeComponent component(&engine);
110 component.setData(QByteArray(
111 "import QtQuick 1.0\n"
113 " property int onItemChangedCount: 0\n"
114 " property int onSourceChangedCount: 0\n"
115 " property int onStatusChangedCount: 0\n"
116 " property int onProgressChangedCount: 0\n"
117 " property int onLoadedCount: 0\n")
118 + sourceDefinition.toUtf8()
120 " onItemChanged: onItemChangedCount += 1\n"
121 " onSourceChanged: onSourceChangedCount += 1\n"
122 " onStatusChanged: onStatusChangedCount += 1\n"
123 " onProgressChanged: onProgressChangedCount += 1\n"
124 " onLoaded: onLoadedCount += 1\n"
128 QDeclarative1Loader *loader = qobject_cast<QDeclarative1Loader*>(component.create());
129 QVERIFY(loader != 0);
130 QCOMPARE(loader->item() == 0, error);
131 QCOMPARE(loader->source(), sourceUrl);
132 QCOMPARE(loader->progress(), 1.0);
134 QCOMPARE(loader->status(), error ? QDeclarative1Loader::Error : QDeclarative1Loader::Ready);
135 QCOMPARE(static_cast<QGraphicsItem*>(loader)->children().count(), error ? 0: 1);
138 QDeclarativeComponent *c = qobject_cast<QDeclarativeComponent*>(loader->QGraphicsObject::children().at(0));
140 QCOMPARE(loader->sourceComponent(), c);
143 QCOMPARE(loader->property("onSourceChangedCount").toInt(), 1);
144 QCOMPARE(loader->property("onStatusChangedCount").toInt(), 1);
145 QCOMPARE(loader->property("onProgressChangedCount").toInt(), 1);
147 QCOMPARE(loader->property("onItemChangedCount").toInt(), error ? 0 : 1);
148 QCOMPARE(loader->property("onLoadedCount").toInt(), error ? 0 : 1);
153 void tst_QDeclarative1Loader::sourceOrComponent_data()
155 QTest::addColumn<QString>("sourceDefinition");
156 QTest::addColumn<QUrl>("sourceUrl");
157 QTest::addColumn<QString>("errorString");
159 QTest::newRow("source") << "source: 'Rect120x60.qml'\n" << QUrl::fromLocalFile(SRCDIR "/data/Rect120x60.qml") << "";
160 QTest::newRow("sourceComponent") << "Component { id: comp; Rectangle { width: 100; height: 50 } }\n sourceComponent: comp\n" << QUrl() << "";
162 QTest::newRow("invalid source") << "source: 'IDontExist.qml'\n" << QUrl::fromLocalFile(SRCDIR "/data/IDontExist.qml")
163 << QString(QUrl::fromLocalFile(SRCDIR "/data/IDontExist.qml").toString() + ": File not found");
166 void tst_QDeclarative1Loader::clear()
169 QDeclarativeComponent component(&engine);
170 component.setData(QByteArray(
171 "import QtQuick 1.0\n"
172 " Loader { id: loader\n"
173 " source: 'Rect120x60.qml'\n"
174 " Timer { interval: 200; running: true; onTriggered: loader.source = '' }\n"
177 QDeclarative1Loader *loader = qobject_cast<QDeclarative1Loader*>(component.create());
178 QVERIFY(loader != 0);
179 QVERIFY(loader->item());
180 QCOMPARE(loader->progress(), 1.0);
181 QCOMPARE(static_cast<QGraphicsItem*>(loader)->children().count(), 1);
183 QTRY_VERIFY(loader->item() == 0);
184 QCOMPARE(loader->progress(), 0.0);
185 QCOMPARE(loader->status(), QDeclarative1Loader::Null);
186 QCOMPARE(static_cast<QGraphicsItem*>(loader)->children().count(), 0);
191 QDeclarativeComponent component(&engine, TEST_FILE("/SetSourceComponent.qml"));
192 QDeclarativeItem *item = qobject_cast<QDeclarativeItem*>(component.create());
195 QDeclarative1Loader *loader = qobject_cast<QDeclarative1Loader*>(item->QGraphicsObject::children().at(1));
197 QVERIFY(loader->item());
198 QCOMPARE(loader->progress(), 1.0);
199 QCOMPARE(static_cast<QGraphicsItem*>(loader)->children().count(), 1);
201 loader->setSourceComponent(0);
203 QVERIFY(loader->item() == 0);
204 QCOMPARE(loader->progress(), 0.0);
205 QCOMPARE(loader->status(), QDeclarative1Loader::Null);
206 QCOMPARE(static_cast<QGraphicsItem*>(loader)->children().count(), 0);
211 QDeclarativeComponent component(&engine, TEST_FILE("/SetSourceComponent.qml"));
212 QDeclarativeItem *item = qobject_cast<QDeclarativeItem*>(component.create());
215 QDeclarative1Loader *loader = qobject_cast<QDeclarative1Loader*>(item->QGraphicsObject::children().at(1));
217 QVERIFY(loader->item());
218 QCOMPARE(loader->progress(), 1.0);
219 QCOMPARE(static_cast<QGraphicsItem*>(loader)->children().count(), 1);
221 QMetaObject::invokeMethod(item, "clear");
223 QVERIFY(loader->item() == 0);
224 QCOMPARE(loader->progress(), 0.0);
225 QCOMPARE(loader->status(), QDeclarative1Loader::Null);
226 QCOMPARE(static_cast<QGraphicsItem*>(loader)->children().count(), 0);
232 void tst_QDeclarative1Loader::urlToComponent()
234 QDeclarativeComponent component(&engine);
235 component.setData(QByteArray("import QtQuick 1.0\n"
238 " Component { id: myComp; Rectangle { width: 10; height: 10 } }\n"
239 " source: \"Rect120x60.qml\"\n"
240 " Timer { interval: 100; running: true; onTriggered: loader.sourceComponent = myComp }\n"
243 QDeclarative1Loader *loader = qobject_cast<QDeclarative1Loader*>(component.create());
245 QTRY_VERIFY(loader != 0);
246 QVERIFY(loader->item());
247 QCOMPARE(loader->progress(), 1.0);
248 QCOMPARE(static_cast<QGraphicsItem*>(loader)->children().count(), 1);
249 QCOMPARE(loader->width(), 10.0);
250 QCOMPARE(loader->height(), 10.0);
255 void tst_QDeclarative1Loader::componentToUrl()
257 QDeclarativeComponent component(&engine, TEST_FILE("/SetSourceComponent.qml"));
258 QDeclarativeItem *item = qobject_cast<QDeclarativeItem*>(component.create());
261 QDeclarative1Loader *loader = qobject_cast<QDeclarative1Loader*>(item->QGraphicsObject::children().at(1));
263 QVERIFY(loader->item());
264 QCOMPARE(loader->progress(), 1.0);
265 QCOMPARE(static_cast<QGraphicsItem*>(loader)->children().count(), 1);
267 loader->setSource(TEST_FILE("/Rect120x60.qml"));
268 QVERIFY(loader->item());
269 QCOMPARE(loader->progress(), 1.0);
270 QCOMPARE(static_cast<QGraphicsItem*>(loader)->children().count(), 1);
271 QCOMPARE(loader->width(), 120.0);
272 QCOMPARE(loader->height(), 60.0);
277 void tst_QDeclarative1Loader::anchoredLoader()
279 QDeclarativeComponent component(&engine, TEST_FILE("/AnchoredLoader.qml"));
280 QDeclarativeItem *rootItem = qobject_cast<QDeclarativeItem*>(component.create());
281 QVERIFY(rootItem != 0);
282 QDeclarativeItem *loader = rootItem->findChild<QDeclarativeItem*>("loader");
283 QDeclarativeItem *sourceElement = rootItem->findChild<QDeclarativeItem*>("sourceElement");
285 QVERIFY(loader != 0);
286 QVERIFY(sourceElement != 0);
288 QCOMPARE(rootItem->width(), 300.0);
289 QCOMPARE(rootItem->height(), 200.0);
291 QCOMPARE(loader->width(), 300.0);
292 QCOMPARE(loader->height(), 200.0);
294 QCOMPARE(sourceElement->width(), 300.0);
295 QCOMPARE(sourceElement->height(), 200.0);
298 void tst_QDeclarative1Loader::sizeLoaderToItem()
300 QDeclarativeComponent component(&engine, TEST_FILE("/SizeToItem.qml"));
301 QDeclarative1Loader *loader = qobject_cast<QDeclarative1Loader*>(component.create());
302 QVERIFY(loader != 0);
303 QCOMPARE(loader->width(), 120.0);
304 QCOMPARE(loader->height(), 60.0);
307 QDeclarativeItem *rect = qobject_cast<QDeclarativeItem*>(loader->item());
311 QCOMPARE(loader->width(), 150.0);
312 QCOMPARE(loader->height(), 45.0);
314 // Check explicit width
315 loader->setWidth(200.0);
316 QCOMPARE(loader->width(), 200.0);
317 QCOMPARE(rect->width(), 200.0);
318 rect->setWidth(100.0); // when rect changes ...
319 QCOMPARE(rect->width(), 100.0); // ... it changes
320 QCOMPARE(loader->width(), 200.0); // ... but loader stays the same
322 // Check explicit height
323 loader->setHeight(200.0);
324 QCOMPARE(loader->height(), 200.0);
325 QCOMPARE(rect->height(), 200.0);
326 rect->setHeight(100.0); // when rect changes ...
327 QCOMPARE(rect->height(), 100.0); // ... it changes
328 QCOMPARE(loader->height(), 200.0); // ... but loader stays the same
331 loader->setWidth(180);
332 loader->setHeight(30);
333 QCOMPARE(rect->width(), 180.0);
334 QCOMPARE(rect->height(), 30.0);
339 void tst_QDeclarative1Loader::sizeItemToLoader()
341 QDeclarativeComponent component(&engine, TEST_FILE("/SizeToLoader.qml"));
342 QDeclarative1Loader *loader = qobject_cast<QDeclarative1Loader*>(component.create());
343 QVERIFY(loader != 0);
344 QCOMPARE(loader->width(), 200.0);
345 QCOMPARE(loader->height(), 80.0);
347 QDeclarativeItem *rect = qobject_cast<QDeclarativeItem*>(loader->item());
349 QCOMPARE(rect->width(), 200.0);
350 QCOMPARE(rect->height(), 80.0);
353 loader->setWidth(180);
354 loader->setHeight(30);
355 QCOMPARE(rect->width(), 180.0);
356 QCOMPARE(rect->height(), 30.0);
359 loader->resetWidth(); // reset explicit size
360 loader->resetHeight();
363 QCOMPARE(loader->width(), 160.0);
364 QCOMPARE(loader->height(), 45.0);
369 void tst_QDeclarative1Loader::noResize()
371 QDeclarativeComponent component(&engine, TEST_FILE("/NoResize.qml"));
372 QDeclarativeItem* item = qobject_cast<QDeclarativeItem*>(component.create());
374 QCOMPARE(item->width(), 200.0);
375 QCOMPARE(item->height(), 80.0);
380 void tst_QDeclarative1Loader::sizeLoaderToGraphicsWidget()
382 QDeclarativeComponent component(&engine, TEST_FILE("/SizeLoaderToGraphicsWidget.qml"));
383 QDeclarative1Loader *loader = qobject_cast<QDeclarative1Loader*>(component.create());
384 QGraphicsScene scene;
385 scene.addItem(loader);
387 QVERIFY(loader != 0);
388 QCOMPARE(loader->width(), 250.0);
389 QCOMPARE(loader->height(), 250.0);
392 QGraphicsWidget *widget = qobject_cast<QGraphicsWidget*>(loader->item());
394 widget->resize(QSizeF(150,45));
395 QCOMPARE(loader->width(), 150.0);
396 QCOMPARE(loader->height(), 45.0);
399 loader->setWidth(180);
400 loader->setHeight(30);
401 QCOMPARE(widget->size().width(), 180.0);
402 QCOMPARE(widget->size().height(), 30.0);
407 void tst_QDeclarative1Loader::sizeGraphicsWidgetToLoader()
409 QDeclarativeComponent component(&engine, TEST_FILE("/SizeGraphicsWidgetToLoader.qml"));
410 QDeclarative1Loader *loader = qobject_cast<QDeclarative1Loader*>(component.create());
411 QGraphicsScene scene;
412 scene.addItem(loader);
414 QVERIFY(loader != 0);
415 QCOMPARE(loader->width(), 200.0);
416 QCOMPARE(loader->height(), 80.0);
418 QGraphicsWidget *widget = qobject_cast<QGraphicsWidget*>(loader->item());
420 QCOMPARE(widget->size().width(), 200.0);
421 QCOMPARE(widget->size().height(), 80.0);
424 loader->setWidth(180);
425 loader->setHeight(30);
426 QCOMPARE(widget->size().width(), 180.0);
427 QCOMPARE(widget->size().height(), 30.0);
430 loader->resetWidth(); // reset explicit size
431 loader->resetHeight();
432 widget->resize(QSizeF(160,45));
433 QCOMPARE(loader->width(), 160.0);
434 QCOMPARE(loader->height(), 45.0);
439 void tst_QDeclarative1Loader::noResizeGraphicsWidget()
441 QDeclarativeComponent component(&engine, TEST_FILE("/NoResizeGraphicsWidget.qml"));
442 QDeclarativeItem *item = qobject_cast<QDeclarativeItem*>(component.create());
443 QGraphicsScene scene;
447 QCOMPARE(item->width(), 200.0);
448 QCOMPARE(item->height(), 80.0);
453 void tst_QDeclarative1Loader::networkRequestUrl()
455 TestHTTPServer server(SERVER_PORT);
456 QVERIFY(server.isValid());
457 server.serveDirectory(SRCDIR "/data");
459 QDeclarativeComponent component(&engine);
460 component.setData(QByteArray("import QtQuick 1.0\nLoader { property int signalCount : 0; source: \"http://127.0.0.1:14450/Rect120x60.qml\"; onLoaded: signalCount += 1 }"), QUrl::fromLocalFile(SRCDIR "/dummy.qml"));
461 if (component.isError())
462 qDebug() << component.errors();
463 QDeclarative1Loader *loader = qobject_cast<QDeclarative1Loader*>(component.create());
464 QVERIFY(loader != 0);
466 QTRY_VERIFY(loader->status() == QDeclarative1Loader::Ready);
468 QVERIFY(loader->item());
469 QCOMPARE(loader->progress(), 1.0);
470 QCOMPARE(loader->property("signalCount").toInt(), 1);
471 QCOMPARE(static_cast<QGraphicsItem*>(loader)->children().count(), 1);
476 /* XXX Component waits until all dependencies are loaded. Is this actually possible?
477 void tst_QDeclarative1Loader::networkComponent()
479 TestHTTPServer server(SERVER_PORT);
480 QVERIFY(server.isValid());
481 server.serveDirectory("slowdata", TestHTTPServer::Delay);
483 QDeclarativeComponent component(&engine);
484 component.setData(QByteArray(
485 "import QtQuick 1.0\n"
486 "import \"http://127.0.0.1:14450/\" as NW\n"
488 " Component { id: comp; NW.SlowRect {} }\n"
489 " Loader { sourceComponent: comp } }")
492 QDeclarativeItem *item = qobject_cast<QDeclarativeItem*>(component.create());
495 QDeclarative1Loader *loader = qobject_cast<QDeclarative1Loader*>(item->QGraphicsObject::children().at(1));
497 QTRY_VERIFY(loader->status() == QDeclarative1Loader::Ready);
499 QVERIFY(loader->item());
500 QCOMPARE(loader->progress(), 1.0);
501 QCOMPARE(loader->status(), QDeclarative1Loader::Ready);
502 QCOMPARE(static_cast<QGraphicsItem*>(loader)->children().count(), 1);
508 void tst_QDeclarative1Loader::failNetworkRequest()
510 TestHTTPServer server(SERVER_PORT);
511 QVERIFY(server.isValid());
512 server.serveDirectory(SRCDIR "/data");
514 QTest::ignoreMessage(QtWarningMsg, "http://127.0.0.1:14450/IDontExist.qml: File not found");
516 QDeclarativeComponent component(&engine);
517 component.setData(QByteArray("import QtQuick 1.0\nLoader { property int did_load: 123; source: \"http://127.0.0.1:14450/IDontExist.qml\"; onLoaded: did_load=456 }"), QUrl::fromLocalFile("http://127.0.0.1:14450/dummy.qml"));
518 QDeclarative1Loader *loader = qobject_cast<QDeclarative1Loader*>(component.create());
519 QVERIFY(loader != 0);
521 QTRY_VERIFY(loader->status() == QDeclarative1Loader::Error);
523 QVERIFY(loader->item() == 0);
524 QCOMPARE(loader->progress(), 0.0);
525 QCOMPARE(loader->property("did_load").toInt(), 123);
526 QCOMPARE(static_cast<QGraphicsItem*>(loader)->children().count(), 0);
532 void tst_QDeclarative1Loader::deleteComponentCrash()
534 QDeclarativeComponent component(&engine, TEST_FILE("crash.qml"));
535 QDeclarativeItem *item = qobject_cast<QDeclarativeItem*>(component.create());
538 item->metaObject()->invokeMethod(item, "setLoaderSource");
540 QDeclarative1Loader *loader = qobject_cast<QDeclarative1Loader*>(item->QGraphicsObject::children().at(0));
542 QVERIFY(loader->item());
543 QCOMPARE(loader->item()->objectName(), QLatin1String("blue"));
544 QCOMPARE(loader->progress(), 1.0);
545 QCOMPARE(loader->status(), QDeclarative1Loader::Ready);
546 QCOMPARE(static_cast<QGraphicsItem*>(loader)->children().count(), 1);
547 QVERIFY(loader->source() == QUrl::fromLocalFile(SRCDIR "/data/BlueRect.qml"));
552 void tst_QDeclarative1Loader::nonItem()
554 QDeclarativeComponent component(&engine, TEST_FILE("nonItem.qml"));
555 QString err = QUrl::fromLocalFile(SRCDIR).toString() + "/data/nonItem.qml:3:1: QML Loader: Loader does not support loading non-visual elements.";
557 QTest::ignoreMessage(QtWarningMsg, err.toLatin1().constData());
558 QDeclarative1Loader *loader = qobject_cast<QDeclarative1Loader*>(component.create());
560 QVERIFY(loader->item() == 0);
565 void tst_QDeclarative1Loader::vmeErrors()
567 QDeclarativeComponent component(&engine, TEST_FILE("vmeErrors.qml"));
568 QString err = QUrl::fromLocalFile(SRCDIR).toString() + "/data/VmeError.qml:6: Cannot assign object type QObject with no default method";
569 QTest::ignoreMessage(QtWarningMsg, err.toLatin1().constData());
570 QDeclarative1Loader *loader = qobject_cast<QDeclarative1Loader*>(component.create());
572 QVERIFY(loader->item() == 0);
578 void tst_QDeclarative1Loader::creationContext()
580 QDeclarativeComponent component(&engine, TEST_FILE("creationContext.qml"));
582 QObject *o = component.create();
585 QCOMPARE(o->property("test").toBool(), true);
590 void tst_QDeclarative1Loader::QTBUG_16928()
592 QDeclarativeComponent component(&engine, TEST_FILE("QTBUG_16928.qml"));
593 QDeclarativeItem *item = qobject_cast<QDeclarativeItem*>(component.create());
596 QCOMPARE(item->width(), 250.);
597 QCOMPARE(item->height(), 250.);
602 void tst_QDeclarative1Loader::implicitSize()
604 QDeclarativeComponent component(&engine, TEST_FILE("implicitSize.qml"));
605 QDeclarativeItem *item = qobject_cast<QDeclarativeItem*>(component.create());
608 QCOMPARE(item->width(), 150.);
609 QCOMPARE(item->height(), 150.);
611 QCOMPARE(item->property("implHeight").toReal(), 100.);
612 QCOMPARE(item->property("implWidth").toReal(), 100.);
617 void tst_QDeclarative1Loader::QTBUG_17114()
619 QDeclarativeComponent component(&engine, TEST_FILE("QTBUG_17114.qml"));
620 QDeclarativeItem *item = qobject_cast<QDeclarativeItem*>(component.create());
623 QCOMPARE(item->property("loaderWidth").toReal(), 32.);
624 QCOMPARE(item->property("loaderHeight").toReal(), 32.);
629 QTEST_MAIN(tst_QDeclarative1Loader)
631 #include "tst_qdeclarativeloader.moc"