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 <QtGui/QGraphicsWidget>
43 #include <QtGui/QGraphicsScene>
46 #include <QtDeclarative/qdeclarativeengine.h>
47 #include <QtDeclarative/qdeclarativecomponent.h>
48 #include <QtQuick1/private/qdeclarativeloader_p.h>
49 #include "testhttpserver.h"
50 #include "../../../shared/util.h"
52 #define SERVER_PORT 14450
55 // In Symbian OS test data is located in applications private dir
59 inline QUrl TEST_FILE(const QString &filename)
61 return QUrl::fromLocalFile(QLatin1String(SRCDIR) + QLatin1String("/data/") + filename);
64 class tst_QDeclarative1Loader : public QObject
69 tst_QDeclarative1Loader();
72 void sourceOrComponent();
73 void sourceOrComponent_data();
75 void urlToComponent();
76 void componentToUrl();
77 void anchoredLoader();
78 void sizeLoaderToItem();
79 void sizeItemToLoader();
81 void sizeLoaderToGraphicsWidget();
82 void sizeGraphicsWidgetToLoader();
83 void noResizeGraphicsWidget();
84 void networkRequestUrl();
85 void failNetworkRequest();
86 // void networkComponent();
88 void deleteComponentCrash();
91 void creationContext();
97 QDeclarativeEngine engine;
101 tst_QDeclarative1Loader::tst_QDeclarative1Loader()
105 void tst_QDeclarative1Loader::sourceOrComponent()
107 QFETCH(QString, sourceDefinition);
108 QFETCH(QUrl, sourceUrl);
109 QFETCH(QString, errorString);
111 bool error = !errorString.isEmpty();
113 QTest::ignoreMessage(QtWarningMsg, errorString.toUtf8().constData());
115 QDeclarativeComponent component(&engine);
116 component.setData(QByteArray(
117 "import QtQuick 1.0\n"
119 " property int onItemChangedCount: 0\n"
120 " property int onSourceChangedCount: 0\n"
121 " property int onStatusChangedCount: 0\n"
122 " property int onProgressChangedCount: 0\n"
123 " property int onLoadedCount: 0\n")
124 + sourceDefinition.toUtf8()
126 " onItemChanged: onItemChangedCount += 1\n"
127 " onSourceChanged: onSourceChangedCount += 1\n"
128 " onStatusChanged: onStatusChangedCount += 1\n"
129 " onProgressChanged: onProgressChangedCount += 1\n"
130 " onLoaded: onLoadedCount += 1\n"
134 QDeclarative1Loader *loader = qobject_cast<QDeclarative1Loader*>(component.create());
135 QVERIFY(loader != 0);
136 QCOMPARE(loader->item() == 0, error);
137 QCOMPARE(loader->source(), sourceUrl);
138 QCOMPARE(loader->progress(), 1.0);
140 QCOMPARE(loader->status(), error ? QDeclarative1Loader::Error : QDeclarative1Loader::Ready);
141 QCOMPARE(static_cast<QGraphicsItem*>(loader)->children().count(), error ? 0: 1);
144 QDeclarativeComponent *c = qobject_cast<QDeclarativeComponent*>(loader->QGraphicsObject::children().at(0));
146 QCOMPARE(loader->sourceComponent(), c);
149 QCOMPARE(loader->property("onSourceChangedCount").toInt(), 1);
150 QCOMPARE(loader->property("onStatusChangedCount").toInt(), 1);
151 QCOMPARE(loader->property("onProgressChangedCount").toInt(), 1);
153 QCOMPARE(loader->property("onItemChangedCount").toInt(), error ? 0 : 1);
154 QCOMPARE(loader->property("onLoadedCount").toInt(), error ? 0 : 1);
159 void tst_QDeclarative1Loader::sourceOrComponent_data()
161 QTest::addColumn<QString>("sourceDefinition");
162 QTest::addColumn<QUrl>("sourceUrl");
163 QTest::addColumn<QString>("errorString");
165 QTest::newRow("source") << "source: 'Rect120x60.qml'\n" << QUrl::fromLocalFile(SRCDIR "/data/Rect120x60.qml") << "";
166 QTest::newRow("sourceComponent") << "Component { id: comp; Rectangle { width: 100; height: 50 } }\n sourceComponent: comp\n" << QUrl() << "";
168 QTest::newRow("invalid source") << "source: 'IDontExist.qml'\n" << QUrl::fromLocalFile(SRCDIR "/data/IDontExist.qml")
169 << QString(QUrl::fromLocalFile(SRCDIR "/data/IDontExist.qml").toString() + ": File not found");
172 void tst_QDeclarative1Loader::clear()
175 QDeclarativeComponent component(&engine);
176 component.setData(QByteArray(
177 "import QtQuick 1.0\n"
178 " Loader { id: loader\n"
179 " source: 'Rect120x60.qml'\n"
180 " Timer { interval: 200; running: true; onTriggered: loader.source = '' }\n"
183 QDeclarative1Loader *loader = qobject_cast<QDeclarative1Loader*>(component.create());
184 QVERIFY(loader != 0);
185 QVERIFY(loader->item());
186 QCOMPARE(loader->progress(), 1.0);
187 QCOMPARE(static_cast<QGraphicsItem*>(loader)->children().count(), 1);
189 QTRY_VERIFY(loader->item() == 0);
190 QCOMPARE(loader->progress(), 0.0);
191 QCOMPARE(loader->status(), QDeclarative1Loader::Null);
192 QCOMPARE(static_cast<QGraphicsItem*>(loader)->children().count(), 0);
197 QDeclarativeComponent component(&engine, TEST_FILE("/SetSourceComponent.qml"));
198 QDeclarativeItem *item = qobject_cast<QDeclarativeItem*>(component.create());
201 QDeclarative1Loader *loader = qobject_cast<QDeclarative1Loader*>(item->QGraphicsObject::children().at(1));
203 QVERIFY(loader->item());
204 QCOMPARE(loader->progress(), 1.0);
205 QCOMPARE(static_cast<QGraphicsItem*>(loader)->children().count(), 1);
207 loader->setSourceComponent(0);
209 QVERIFY(loader->item() == 0);
210 QCOMPARE(loader->progress(), 0.0);
211 QCOMPARE(loader->status(), QDeclarative1Loader::Null);
212 QCOMPARE(static_cast<QGraphicsItem*>(loader)->children().count(), 0);
217 QDeclarativeComponent component(&engine, TEST_FILE("/SetSourceComponent.qml"));
218 QDeclarativeItem *item = qobject_cast<QDeclarativeItem*>(component.create());
221 QDeclarative1Loader *loader = qobject_cast<QDeclarative1Loader*>(item->QGraphicsObject::children().at(1));
223 QVERIFY(loader->item());
224 QCOMPARE(loader->progress(), 1.0);
225 QCOMPARE(static_cast<QGraphicsItem*>(loader)->children().count(), 1);
227 QMetaObject::invokeMethod(item, "clear");
229 QVERIFY(loader->item() == 0);
230 QCOMPARE(loader->progress(), 0.0);
231 QCOMPARE(loader->status(), QDeclarative1Loader::Null);
232 QCOMPARE(static_cast<QGraphicsItem*>(loader)->children().count(), 0);
238 void tst_QDeclarative1Loader::urlToComponent()
240 QDeclarativeComponent component(&engine);
241 component.setData(QByteArray("import QtQuick 1.0\n"
244 " Component { id: myComp; Rectangle { width: 10; height: 10 } }\n"
245 " source: \"Rect120x60.qml\"\n"
246 " Timer { interval: 100; running: true; onTriggered: loader.sourceComponent = myComp }\n"
249 QDeclarative1Loader *loader = qobject_cast<QDeclarative1Loader*>(component.create());
251 QTRY_VERIFY(loader != 0);
252 QVERIFY(loader->item());
253 QCOMPARE(loader->progress(), 1.0);
254 QCOMPARE(static_cast<QGraphicsItem*>(loader)->children().count(), 1);
255 QCOMPARE(loader->width(), 10.0);
256 QCOMPARE(loader->height(), 10.0);
261 void tst_QDeclarative1Loader::componentToUrl()
263 QDeclarativeComponent component(&engine, TEST_FILE("/SetSourceComponent.qml"));
264 QDeclarativeItem *item = qobject_cast<QDeclarativeItem*>(component.create());
267 QDeclarative1Loader *loader = qobject_cast<QDeclarative1Loader*>(item->QGraphicsObject::children().at(1));
269 QVERIFY(loader->item());
270 QCOMPARE(loader->progress(), 1.0);
271 QCOMPARE(static_cast<QGraphicsItem*>(loader)->children().count(), 1);
273 loader->setSource(TEST_FILE("/Rect120x60.qml"));
274 QVERIFY(loader->item());
275 QCOMPARE(loader->progress(), 1.0);
276 QCOMPARE(static_cast<QGraphicsItem*>(loader)->children().count(), 1);
277 QCOMPARE(loader->width(), 120.0);
278 QCOMPARE(loader->height(), 60.0);
283 void tst_QDeclarative1Loader::anchoredLoader()
285 QDeclarativeComponent component(&engine, TEST_FILE("/AnchoredLoader.qml"));
286 QDeclarativeItem *rootItem = qobject_cast<QDeclarativeItem*>(component.create());
287 QVERIFY(rootItem != 0);
288 QDeclarativeItem *loader = rootItem->findChild<QDeclarativeItem*>("loader");
289 QDeclarativeItem *sourceElement = rootItem->findChild<QDeclarativeItem*>("sourceElement");
291 QVERIFY(loader != 0);
292 QVERIFY(sourceElement != 0);
294 QCOMPARE(rootItem->width(), 300.0);
295 QCOMPARE(rootItem->height(), 200.0);
297 QCOMPARE(loader->width(), 300.0);
298 QCOMPARE(loader->height(), 200.0);
300 QCOMPARE(sourceElement->width(), 300.0);
301 QCOMPARE(sourceElement->height(), 200.0);
304 void tst_QDeclarative1Loader::sizeLoaderToItem()
306 QDeclarativeComponent component(&engine, TEST_FILE("/SizeToItem.qml"));
307 QDeclarative1Loader *loader = qobject_cast<QDeclarative1Loader*>(component.create());
308 QVERIFY(loader != 0);
309 QCOMPARE(loader->width(), 120.0);
310 QCOMPARE(loader->height(), 60.0);
313 QDeclarativeItem *rect = qobject_cast<QDeclarativeItem*>(loader->item());
317 QCOMPARE(loader->width(), 150.0);
318 QCOMPARE(loader->height(), 45.0);
320 // Check explicit width
321 loader->setWidth(200.0);
322 QCOMPARE(loader->width(), 200.0);
323 QCOMPARE(rect->width(), 200.0);
324 rect->setWidth(100.0); // when rect changes ...
325 QCOMPARE(rect->width(), 100.0); // ... it changes
326 QCOMPARE(loader->width(), 200.0); // ... but loader stays the same
328 // Check explicit height
329 loader->setHeight(200.0);
330 QCOMPARE(loader->height(), 200.0);
331 QCOMPARE(rect->height(), 200.0);
332 rect->setHeight(100.0); // when rect changes ...
333 QCOMPARE(rect->height(), 100.0); // ... it changes
334 QCOMPARE(loader->height(), 200.0); // ... but loader stays the same
337 loader->setWidth(180);
338 loader->setHeight(30);
339 QCOMPARE(rect->width(), 180.0);
340 QCOMPARE(rect->height(), 30.0);
345 void tst_QDeclarative1Loader::sizeItemToLoader()
347 QDeclarativeComponent component(&engine, TEST_FILE("/SizeToLoader.qml"));
348 QDeclarative1Loader *loader = qobject_cast<QDeclarative1Loader*>(component.create());
349 QVERIFY(loader != 0);
350 QCOMPARE(loader->width(), 200.0);
351 QCOMPARE(loader->height(), 80.0);
353 QDeclarativeItem *rect = qobject_cast<QDeclarativeItem*>(loader->item());
355 QCOMPARE(rect->width(), 200.0);
356 QCOMPARE(rect->height(), 80.0);
359 loader->setWidth(180);
360 loader->setHeight(30);
361 QCOMPARE(rect->width(), 180.0);
362 QCOMPARE(rect->height(), 30.0);
365 loader->resetWidth(); // reset explicit size
366 loader->resetHeight();
369 QCOMPARE(loader->width(), 160.0);
370 QCOMPARE(loader->height(), 45.0);
375 void tst_QDeclarative1Loader::noResize()
377 QDeclarativeComponent component(&engine, TEST_FILE("/NoResize.qml"));
378 QDeclarativeItem* item = qobject_cast<QDeclarativeItem*>(component.create());
380 QCOMPARE(item->width(), 200.0);
381 QCOMPARE(item->height(), 80.0);
386 void tst_QDeclarative1Loader::sizeLoaderToGraphicsWidget()
388 QDeclarativeComponent component(&engine, TEST_FILE("/SizeLoaderToGraphicsWidget.qml"));
389 QDeclarative1Loader *loader = qobject_cast<QDeclarative1Loader*>(component.create());
390 QGraphicsScene scene;
391 scene.addItem(loader);
393 QVERIFY(loader != 0);
394 QCOMPARE(loader->width(), 250.0);
395 QCOMPARE(loader->height(), 250.0);
398 QGraphicsWidget *widget = qobject_cast<QGraphicsWidget*>(loader->item());
400 widget->resize(QSizeF(150,45));
401 QCOMPARE(loader->width(), 150.0);
402 QCOMPARE(loader->height(), 45.0);
405 loader->setWidth(180);
406 loader->setHeight(30);
407 QCOMPARE(widget->size().width(), 180.0);
408 QCOMPARE(widget->size().height(), 30.0);
413 void tst_QDeclarative1Loader::sizeGraphicsWidgetToLoader()
415 QDeclarativeComponent component(&engine, TEST_FILE("/SizeGraphicsWidgetToLoader.qml"));
416 QDeclarative1Loader *loader = qobject_cast<QDeclarative1Loader*>(component.create());
417 QGraphicsScene scene;
418 scene.addItem(loader);
420 QVERIFY(loader != 0);
421 QCOMPARE(loader->width(), 200.0);
422 QCOMPARE(loader->height(), 80.0);
424 QGraphicsWidget *widget = qobject_cast<QGraphicsWidget*>(loader->item());
426 QCOMPARE(widget->size().width(), 200.0);
427 QCOMPARE(widget->size().height(), 80.0);
430 loader->setWidth(180);
431 loader->setHeight(30);
432 QCOMPARE(widget->size().width(), 180.0);
433 QCOMPARE(widget->size().height(), 30.0);
436 loader->resetWidth(); // reset explicit size
437 loader->resetHeight();
438 widget->resize(QSizeF(160,45));
439 QCOMPARE(loader->width(), 160.0);
440 QCOMPARE(loader->height(), 45.0);
445 void tst_QDeclarative1Loader::noResizeGraphicsWidget()
447 QDeclarativeComponent component(&engine, TEST_FILE("/NoResizeGraphicsWidget.qml"));
448 QDeclarativeItem *item = qobject_cast<QDeclarativeItem*>(component.create());
449 QGraphicsScene scene;
453 QCOMPARE(item->width(), 200.0);
454 QCOMPARE(item->height(), 80.0);
459 void tst_QDeclarative1Loader::networkRequestUrl()
461 TestHTTPServer server(SERVER_PORT);
462 QVERIFY(server.isValid());
463 server.serveDirectory(SRCDIR "/data");
465 QDeclarativeComponent component(&engine);
466 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"));
467 if (component.isError())
468 qDebug() << component.errors();
469 QDeclarative1Loader *loader = qobject_cast<QDeclarative1Loader*>(component.create());
470 QVERIFY(loader != 0);
472 QTRY_VERIFY(loader->status() == QDeclarative1Loader::Ready);
474 QVERIFY(loader->item());
475 QCOMPARE(loader->progress(), 1.0);
476 QCOMPARE(loader->property("signalCount").toInt(), 1);
477 QCOMPARE(static_cast<QGraphicsItem*>(loader)->children().count(), 1);
482 /* XXX Component waits until all dependencies are loaded. Is this actually possible?
483 void tst_QDeclarative1Loader::networkComponent()
485 TestHTTPServer server(SERVER_PORT);
486 QVERIFY(server.isValid());
487 server.serveDirectory("slowdata", TestHTTPServer::Delay);
489 QDeclarativeComponent component(&engine);
490 component.setData(QByteArray(
491 "import QtQuick 1.0\n"
492 "import \"http://127.0.0.1:14450/\" as NW\n"
494 " Component { id: comp; NW.SlowRect {} }\n"
495 " Loader { sourceComponent: comp } }")
498 QDeclarativeItem *item = qobject_cast<QDeclarativeItem*>(component.create());
501 QDeclarative1Loader *loader = qobject_cast<QDeclarative1Loader*>(item->QGraphicsObject::children().at(1));
503 QTRY_VERIFY(loader->status() == QDeclarative1Loader::Ready);
505 QVERIFY(loader->item());
506 QCOMPARE(loader->progress(), 1.0);
507 QCOMPARE(loader->status(), QDeclarative1Loader::Ready);
508 QCOMPARE(static_cast<QGraphicsItem*>(loader)->children().count(), 1);
514 void tst_QDeclarative1Loader::failNetworkRequest()
516 TestHTTPServer server(SERVER_PORT);
517 QVERIFY(server.isValid());
518 server.serveDirectory(SRCDIR "/data");
520 QTest::ignoreMessage(QtWarningMsg, "http://127.0.0.1:14450/IDontExist.qml: File not found");
522 QDeclarativeComponent component(&engine);
523 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"));
524 QDeclarative1Loader *loader = qobject_cast<QDeclarative1Loader*>(component.create());
525 QVERIFY(loader != 0);
527 QTRY_VERIFY(loader->status() == QDeclarative1Loader::Error);
529 QVERIFY(loader->item() == 0);
530 QCOMPARE(loader->progress(), 0.0);
531 QCOMPARE(loader->property("did_load").toInt(), 123);
532 QCOMPARE(static_cast<QGraphicsItem*>(loader)->children().count(), 0);
538 void tst_QDeclarative1Loader::deleteComponentCrash()
540 QDeclarativeComponent component(&engine, TEST_FILE("crash.qml"));
541 QDeclarativeItem *item = qobject_cast<QDeclarativeItem*>(component.create());
544 item->metaObject()->invokeMethod(item, "setLoaderSource");
546 QDeclarative1Loader *loader = qobject_cast<QDeclarative1Loader*>(item->QGraphicsObject::children().at(0));
548 QVERIFY(loader->item());
549 QCOMPARE(loader->item()->objectName(), QLatin1String("blue"));
550 QCOMPARE(loader->progress(), 1.0);
551 QCOMPARE(loader->status(), QDeclarative1Loader::Ready);
552 QCOMPARE(static_cast<QGraphicsItem*>(loader)->children().count(), 1);
553 QVERIFY(loader->source() == QUrl::fromLocalFile(SRCDIR "/data/BlueRect.qml"));
558 void tst_QDeclarative1Loader::nonItem()
560 QDeclarativeComponent component(&engine, TEST_FILE("nonItem.qml"));
561 QString err = QUrl::fromLocalFile(SRCDIR).toString() + "/data/nonItem.qml:3:1: QML Loader: Loader does not support loading non-visual elements.";
563 QTest::ignoreMessage(QtWarningMsg, err.toLatin1().constData());
564 QDeclarative1Loader *loader = qobject_cast<QDeclarative1Loader*>(component.create());
566 QVERIFY(loader->item() == 0);
571 void tst_QDeclarative1Loader::vmeErrors()
573 QDeclarativeComponent component(&engine, TEST_FILE("vmeErrors.qml"));
574 QString err = QUrl::fromLocalFile(SRCDIR).toString() + "/data/VmeError.qml:6: Cannot assign object type QObject with no default method";
575 QTest::ignoreMessage(QtWarningMsg, err.toLatin1().constData());
576 QDeclarative1Loader *loader = qobject_cast<QDeclarative1Loader*>(component.create());
578 QVERIFY(loader->item() == 0);
584 void tst_QDeclarative1Loader::creationContext()
586 QDeclarativeComponent component(&engine, TEST_FILE("creationContext.qml"));
588 QObject *o = component.create();
591 QCOMPARE(o->property("test").toBool(), true);
596 void tst_QDeclarative1Loader::QTBUG_16928()
598 QDeclarativeComponent component(&engine, TEST_FILE("QTBUG_16928.qml"));
599 QDeclarativeItem *item = qobject_cast<QDeclarativeItem*>(component.create());
602 QCOMPARE(item->width(), 250.);
603 QCOMPARE(item->height(), 250.);
608 void tst_QDeclarative1Loader::implicitSize()
610 QDeclarativeComponent component(&engine, TEST_FILE("implicitSize.qml"));
611 QDeclarativeItem *item = qobject_cast<QDeclarativeItem*>(component.create());
614 QCOMPARE(item->width(), 150.);
615 QCOMPARE(item->height(), 150.);
617 QCOMPARE(item->property("implHeight").toReal(), 100.);
618 QCOMPARE(item->property("implWidth").toReal(), 100.);
623 void tst_QDeclarative1Loader::QTBUG_17114()
625 QDeclarativeComponent component(&engine, TEST_FILE("QTBUG_17114.qml"));
626 QDeclarativeItem *item = qobject_cast<QDeclarativeItem*>(component.create());
629 QCOMPARE(item->property("loaderWidth").toReal(), 32.);
630 QCOMPARE(item->property("loaderHeight").toReal(), 32.);
635 QTEST_MAIN(tst_QDeclarative1Loader)
637 #include "tst_qdeclarativeloader.moc"