tests/auto/declarative/qdeclarativeloader/tst_qdeclarativeloader.cpp
changeset 30 5dc02b23752f
child 33 3e2da88830cd
equal deleted inserted replaced
29:b72c6db6890b 30:5dc02b23752f
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the test suite of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 #include <qtest.h>
       
    42 #include <QtGui/QGraphicsWidget>
       
    43 #include <QtGui/QGraphicsScene>
       
    44 
       
    45 #include <QSignalSpy>
       
    46 #include <QtDeclarative/qdeclarativeengine.h>
       
    47 #include <QtDeclarative/qdeclarativecomponent.h>
       
    48 #include <private/qdeclarativeloader_p.h>
       
    49 #include "testhttpserver.h"
       
    50 
       
    51 #define SERVER_PORT 14450
       
    52 
       
    53 inline QUrl TEST_FILE(const QString &filename)
       
    54 {
       
    55     return QUrl::fromLocalFile(QLatin1String(SRCDIR) + QLatin1String("/data/") + filename);
       
    56 }
       
    57 
       
    58 #define TRY_WAIT(expr) \
       
    59     do { \
       
    60         for (int ii = 0; ii < 6; ++ii) { \
       
    61             if ((expr)) break; \
       
    62             QTest::qWait(50); \
       
    63         } \
       
    64         QVERIFY((expr)); \
       
    65     } while (false)
       
    66 
       
    67 class tst_QDeclarativeLoader : public QObject
       
    68 
       
    69 {
       
    70     Q_OBJECT
       
    71 public:
       
    72     tst_QDeclarativeLoader();
       
    73 
       
    74 private slots:
       
    75     void url();
       
    76     void invalidUrl();
       
    77     void component();
       
    78     void clear();
       
    79     void urlToComponent();
       
    80     void componentToUrl();
       
    81     void anchoredLoader();
       
    82     void sizeLoaderToItem();
       
    83     void sizeItemToLoader();
       
    84     void noResize();
       
    85     void sizeLoaderToGraphicsWidget();
       
    86     void sizeGraphicsWidgetToLoader();
       
    87     void noResizeGraphicsWidget();
       
    88     void networkRequestUrl();
       
    89     void failNetworkRequest();
       
    90 //    void networkComponent();
       
    91 
       
    92     void deleteComponentCrash();
       
    93     void nonItem();
       
    94     void vmeErrors();
       
    95 
       
    96 private:
       
    97     QDeclarativeEngine engine;
       
    98 };
       
    99 
       
   100 
       
   101 tst_QDeclarativeLoader::tst_QDeclarativeLoader()
       
   102 {
       
   103 }
       
   104 
       
   105 void tst_QDeclarativeLoader::url()
       
   106 {
       
   107     QDeclarativeComponent component(&engine);
       
   108     component.setData(QByteArray("import Qt 4.7\nLoader { property int did_load: 0; onLoaded: did_load=123; source: \"Rect120x60.qml\" }"), TEST_FILE(""));
       
   109     QDeclarativeLoader *loader = qobject_cast<QDeclarativeLoader*>(component.create());
       
   110     QVERIFY(loader != 0);
       
   111     QVERIFY(loader->item());
       
   112     QVERIFY(loader->source() == QUrl::fromLocalFile(SRCDIR "/data/Rect120x60.qml"));
       
   113     QCOMPARE(loader->progress(), 1.0);
       
   114     QCOMPARE(loader->status(), QDeclarativeLoader::Ready);
       
   115     QCOMPARE(loader->property("did_load").toInt(), 123);
       
   116     QCOMPARE(static_cast<QGraphicsItem*>(loader)->children().count(), 1);
       
   117 
       
   118     delete loader;
       
   119 }
       
   120 
       
   121 void tst_QDeclarativeLoader::component()
       
   122 {
       
   123     QDeclarativeComponent component(&engine, TEST_FILE("/SetSourceComponent.qml"));
       
   124     QDeclarativeItem *item = qobject_cast<QDeclarativeItem*>(component.create());
       
   125     QVERIFY(item);
       
   126 
       
   127     QDeclarativeLoader *loader = qobject_cast<QDeclarativeLoader*>(item->QGraphicsObject::children().at(1)); 
       
   128     QVERIFY(loader);
       
   129     QVERIFY(loader->item());
       
   130     QCOMPARE(loader->progress(), 1.0);
       
   131     QCOMPARE(loader->status(), QDeclarativeLoader::Ready);
       
   132     QCOMPARE(static_cast<QGraphicsItem*>(loader)->children().count(), 1);
       
   133 
       
   134     QDeclarativeComponent *c = qobject_cast<QDeclarativeComponent*>(item->QGraphicsObject::children().at(0));
       
   135     QVERIFY(c);
       
   136     QCOMPARE(loader->sourceComponent(), c);
       
   137 
       
   138     delete item;
       
   139 }
       
   140 
       
   141 void tst_QDeclarativeLoader::invalidUrl()
       
   142 {
       
   143     QTest::ignoreMessage(QtWarningMsg, QString("<Unknown File>: File error for URL " + QUrl::fromLocalFile(SRCDIR "/data/IDontExist.qml").toString()).toUtf8().constData());
       
   144 
       
   145     QDeclarativeComponent component(&engine);
       
   146     component.setData(QByteArray("import Qt 4.7\nLoader { source: \"IDontExist.qml\" }"), TEST_FILE(""));
       
   147     QDeclarativeLoader *loader = qobject_cast<QDeclarativeLoader*>(component.create());
       
   148     QVERIFY(loader != 0);
       
   149     QVERIFY(loader->item() == 0);
       
   150     QCOMPARE(loader->progress(), 1.0);
       
   151     QCOMPARE(loader->status(), QDeclarativeLoader::Error);
       
   152     QCOMPARE(static_cast<QGraphicsItem*>(loader)->children().count(), 0);
       
   153 
       
   154     delete loader;
       
   155 }
       
   156 
       
   157 void tst_QDeclarativeLoader::clear()
       
   158 {
       
   159     {
       
   160         QDeclarativeComponent component(&engine);
       
   161         component.setData(QByteArray(
       
   162                     "import Qt 4.7\n"
       
   163                     " Loader { id: loader\n"
       
   164                     "  source: 'Rect120x60.qml'\n"
       
   165                     "  Timer { interval: 200; running: true; onTriggered: loader.source = '' }\n"
       
   166                     " }")
       
   167                 , TEST_FILE(""));
       
   168         QDeclarativeLoader *loader = qobject_cast<QDeclarativeLoader*>(component.create());
       
   169         QVERIFY(loader != 0);
       
   170         QVERIFY(loader->item());
       
   171         QCOMPARE(loader->progress(), 1.0);
       
   172         QCOMPARE(static_cast<QGraphicsItem*>(loader)->children().count(), 1);
       
   173 
       
   174         QTest::qWait(500);
       
   175 
       
   176         QVERIFY(loader->item() == 0);
       
   177         QCOMPARE(loader->progress(), 0.0);
       
   178         QCOMPARE(loader->status(), QDeclarativeLoader::Null);
       
   179         QCOMPARE(static_cast<QGraphicsItem*>(loader)->children().count(), 0);
       
   180 
       
   181         delete loader;
       
   182     }
       
   183     {
       
   184         QDeclarativeComponent component(&engine, TEST_FILE("/SetSourceComponent.qml"));
       
   185         QDeclarativeItem *item = qobject_cast<QDeclarativeItem*>(component.create());
       
   186         QVERIFY(item);
       
   187 
       
   188         QDeclarativeLoader *loader = qobject_cast<QDeclarativeLoader*>(item->QGraphicsObject::children().at(1)); 
       
   189         QVERIFY(loader);
       
   190         QVERIFY(loader->item());
       
   191         QCOMPARE(loader->progress(), 1.0);
       
   192         QCOMPARE(static_cast<QGraphicsItem*>(loader)->children().count(), 1);
       
   193 
       
   194         loader->setSourceComponent(0);
       
   195 
       
   196         QVERIFY(loader->item() == 0);
       
   197         QCOMPARE(loader->progress(), 0.0);
       
   198         QCOMPARE(loader->status(), QDeclarativeLoader::Null);
       
   199         QCOMPARE(static_cast<QGraphicsItem*>(loader)->children().count(), 0);
       
   200 
       
   201         delete item;
       
   202     }
       
   203     {
       
   204         QDeclarativeComponent component(&engine, TEST_FILE("/SetSourceComponent.qml"));
       
   205         QDeclarativeItem *item = qobject_cast<QDeclarativeItem*>(component.create());
       
   206         QVERIFY(item);
       
   207 
       
   208         QDeclarativeLoader *loader = qobject_cast<QDeclarativeLoader*>(item->QGraphicsObject::children().at(1)); 
       
   209         QVERIFY(loader);
       
   210         QVERIFY(loader->item());
       
   211         QCOMPARE(loader->progress(), 1.0);
       
   212         QCOMPARE(static_cast<QGraphicsItem*>(loader)->children().count(), 1);
       
   213 
       
   214         QMetaObject::invokeMethod(item, "clear");
       
   215 
       
   216         QVERIFY(loader->item() == 0);
       
   217         QCOMPARE(loader->progress(), 0.0);
       
   218         QCOMPARE(loader->status(), QDeclarativeLoader::Null);
       
   219         QCOMPARE(static_cast<QGraphicsItem*>(loader)->children().count(), 0);
       
   220 
       
   221         delete item;
       
   222     }
       
   223 }
       
   224 
       
   225 void tst_QDeclarativeLoader::urlToComponent()
       
   226 {
       
   227     QDeclarativeComponent component(&engine);
       
   228     component.setData(QByteArray("import Qt 4.7\n"
       
   229                 "Loader {\n"
       
   230                 " id: loader\n"
       
   231                 " Component { id: myComp; Rectangle { width: 10; height: 10 } }\n"
       
   232                 " source: \"Rect120x60.qml\"\n"
       
   233                 " Timer { interval: 100; running: true; onTriggered: loader.sourceComponent = myComp }\n"
       
   234                 "}" )
       
   235             , TEST_FILE(""));
       
   236     QDeclarativeLoader *loader = qobject_cast<QDeclarativeLoader*>(component.create());
       
   237     QTest::qWait(500);
       
   238     QVERIFY(loader != 0);
       
   239     QVERIFY(loader->item());
       
   240     QCOMPARE(loader->progress(), 1.0);
       
   241     QCOMPARE(static_cast<QGraphicsItem*>(loader)->children().count(), 1);
       
   242     QCOMPARE(loader->width(), 10.0);
       
   243     QCOMPARE(loader->height(), 10.0);
       
   244 
       
   245     delete loader;
       
   246 }
       
   247 
       
   248 void tst_QDeclarativeLoader::componentToUrl()
       
   249 {
       
   250     QDeclarativeComponent component(&engine, TEST_FILE("/SetSourceComponent.qml"));
       
   251     QDeclarativeItem *item = qobject_cast<QDeclarativeItem*>(component.create());
       
   252     QVERIFY(item);
       
   253 
       
   254     QDeclarativeLoader *loader = qobject_cast<QDeclarativeLoader*>(item->QGraphicsObject::children().at(1)); 
       
   255     QVERIFY(loader);
       
   256     QVERIFY(loader->item());
       
   257     QCOMPARE(loader->progress(), 1.0);
       
   258     QCOMPARE(static_cast<QGraphicsItem*>(loader)->children().count(), 1);
       
   259 
       
   260     loader->setSource(TEST_FILE("/Rect120x60.qml"));
       
   261     QVERIFY(loader->item());
       
   262     QCOMPARE(loader->progress(), 1.0);
       
   263     QCOMPARE(static_cast<QGraphicsItem*>(loader)->children().count(), 1);
       
   264     QCOMPARE(loader->width(), 120.0);
       
   265     QCOMPARE(loader->height(), 60.0);
       
   266 
       
   267     delete item;
       
   268 }
       
   269 
       
   270 void tst_QDeclarativeLoader::anchoredLoader()
       
   271 {
       
   272     QDeclarativeComponent component(&engine, TEST_FILE("/AnchoredLoader.qml"));
       
   273     QDeclarativeItem *rootItem = qobject_cast<QDeclarativeItem*>(component.create());
       
   274     QVERIFY(rootItem != 0);
       
   275     QDeclarativeItem *loader = rootItem->findChild<QDeclarativeItem*>("loader");
       
   276     QDeclarativeItem *sourceElement = rootItem->findChild<QDeclarativeItem*>("sourceElement");
       
   277 
       
   278     QVERIFY(loader != 0);
       
   279     QVERIFY(sourceElement != 0);
       
   280 
       
   281     QCOMPARE(rootItem->width(), 300.0);
       
   282     QCOMPARE(rootItem->height(), 200.0);
       
   283 
       
   284     QCOMPARE(loader->width(), 300.0);
       
   285     QCOMPARE(loader->height(), 200.0);
       
   286 
       
   287     QCOMPARE(sourceElement->width(), 300.0);
       
   288     QCOMPARE(sourceElement->height(), 200.0);
       
   289 }
       
   290 
       
   291 void tst_QDeclarativeLoader::sizeLoaderToItem()
       
   292 {
       
   293     QDeclarativeComponent component(&engine, TEST_FILE("/SizeToItem.qml"));
       
   294     QDeclarativeLoader *loader = qobject_cast<QDeclarativeLoader*>(component.create());
       
   295     QVERIFY(loader != 0);
       
   296     QCOMPARE(loader->width(), 120.0);
       
   297     QCOMPARE(loader->height(), 60.0);
       
   298 
       
   299     // Check resize
       
   300     QDeclarativeItem *rect = qobject_cast<QDeclarativeItem*>(loader->item());
       
   301     QVERIFY(rect);
       
   302     rect->setWidth(150);
       
   303     rect->setHeight(45);
       
   304     QCOMPARE(loader->width(), 150.0);
       
   305     QCOMPARE(loader->height(), 45.0);
       
   306 
       
   307     // Check explicit width
       
   308     loader->setWidth(200.0);
       
   309     QCOMPARE(loader->width(), 200.0);
       
   310     QCOMPARE(rect->width(), 200.0);
       
   311     rect->setWidth(100.0); // when rect changes ...
       
   312     QCOMPARE(rect->width(), 100.0); // ... it changes
       
   313     QCOMPARE(loader->width(), 200.0); // ... but loader stays the same
       
   314 
       
   315     // Check explicit height
       
   316     loader->setHeight(200.0);
       
   317     QCOMPARE(loader->height(), 200.0);
       
   318     QCOMPARE(rect->height(), 200.0);
       
   319     rect->setHeight(100.0); // when rect changes ...
       
   320     QCOMPARE(rect->height(), 100.0); // ... it changes
       
   321     QCOMPARE(loader->height(), 200.0); // ... but loader stays the same
       
   322 
       
   323     // Switch mode
       
   324     loader->setWidth(180);
       
   325     loader->setHeight(30);
       
   326     QCOMPARE(rect->width(), 180.0);
       
   327     QCOMPARE(rect->height(), 30.0);
       
   328 
       
   329     delete loader;
       
   330 }
       
   331 
       
   332 void tst_QDeclarativeLoader::sizeItemToLoader()
       
   333 {
       
   334     QDeclarativeComponent component(&engine, TEST_FILE("/SizeToLoader.qml"));
       
   335     QDeclarativeLoader *loader = qobject_cast<QDeclarativeLoader*>(component.create());
       
   336     QVERIFY(loader != 0);
       
   337     QCOMPARE(loader->width(), 200.0);
       
   338     QCOMPARE(loader->height(), 80.0);
       
   339 
       
   340     QDeclarativeItem *rect = qobject_cast<QDeclarativeItem*>(loader->item());
       
   341     QVERIFY(rect);
       
   342     QCOMPARE(rect->width(), 200.0);
       
   343     QCOMPARE(rect->height(), 80.0);
       
   344 
       
   345     // Check resize
       
   346     loader->setWidth(180);
       
   347     loader->setHeight(30);
       
   348     QCOMPARE(rect->width(), 180.0);
       
   349     QCOMPARE(rect->height(), 30.0);
       
   350 
       
   351     // Switch mode
       
   352     loader->resetWidth(); // reset explicit size
       
   353     loader->resetHeight();
       
   354     rect->setWidth(160);
       
   355     rect->setHeight(45);
       
   356     QCOMPARE(loader->width(), 160.0);
       
   357     QCOMPARE(loader->height(), 45.0);
       
   358 
       
   359     delete loader;
       
   360 }
       
   361 
       
   362 void tst_QDeclarativeLoader::noResize()
       
   363 {
       
   364     QDeclarativeComponent component(&engine, TEST_FILE("/NoResize.qml"));
       
   365     QDeclarativeItem* item = qobject_cast<QDeclarativeItem*>(component.create());
       
   366     QVERIFY(item != 0);
       
   367     QCOMPARE(item->width(), 200.0);
       
   368     QCOMPARE(item->height(), 80.0);
       
   369 
       
   370     delete item;
       
   371 }
       
   372 
       
   373 void tst_QDeclarativeLoader::sizeLoaderToGraphicsWidget()
       
   374 {
       
   375     QDeclarativeComponent component(&engine, TEST_FILE("/SizeLoaderToGraphicsWidget.qml"));
       
   376     QDeclarativeLoader *loader = qobject_cast<QDeclarativeLoader*>(component.create());
       
   377     QGraphicsScene scene;
       
   378     scene.addItem(loader);
       
   379 
       
   380     QVERIFY(loader != 0);
       
   381     QCOMPARE(loader->width(), 250.0);
       
   382     QCOMPARE(loader->height(), 250.0);
       
   383 
       
   384     // Check resize
       
   385     QGraphicsWidget *widget = qobject_cast<QGraphicsWidget*>(loader->item());
       
   386     QVERIFY(widget);
       
   387     widget->resize(QSizeF(150,45));
       
   388     QCOMPARE(loader->width(), 150.0);
       
   389     QCOMPARE(loader->height(), 45.0);
       
   390 
       
   391     // Switch mode
       
   392     loader->setWidth(180);
       
   393     loader->setHeight(30);
       
   394     QCOMPARE(widget->size().width(), 180.0);
       
   395     QCOMPARE(widget->size().height(), 30.0);
       
   396 
       
   397     delete loader;
       
   398 }
       
   399 
       
   400 void tst_QDeclarativeLoader::sizeGraphicsWidgetToLoader()
       
   401 {
       
   402     QDeclarativeComponent component(&engine, TEST_FILE("/SizeGraphicsWidgetToLoader.qml"));
       
   403     QDeclarativeLoader *loader = qobject_cast<QDeclarativeLoader*>(component.create());
       
   404     QGraphicsScene scene;
       
   405     scene.addItem(loader);
       
   406 
       
   407     QVERIFY(loader != 0);
       
   408     QCOMPARE(loader->width(), 200.0);
       
   409     QCOMPARE(loader->height(), 80.0);
       
   410 
       
   411     QGraphicsWidget *widget = qobject_cast<QGraphicsWidget*>(loader->item());
       
   412     QVERIFY(widget);
       
   413     QCOMPARE(widget->size().width(), 200.0);
       
   414     QCOMPARE(widget->size().height(), 80.0);
       
   415 
       
   416     // Check resize
       
   417     loader->setWidth(180);
       
   418     loader->setHeight(30);
       
   419     QCOMPARE(widget->size().width(), 180.0);
       
   420     QCOMPARE(widget->size().height(), 30.0);
       
   421 
       
   422     // Switch mode
       
   423     loader->resetWidth(); // reset explicit size
       
   424     loader->resetHeight();
       
   425     widget->resize(QSizeF(160,45));
       
   426     QCOMPARE(loader->width(), 160.0);
       
   427     QCOMPARE(loader->height(), 45.0);
       
   428 
       
   429     delete loader;
       
   430 }
       
   431 
       
   432 void tst_QDeclarativeLoader::noResizeGraphicsWidget()
       
   433 {
       
   434     QDeclarativeComponent component(&engine, TEST_FILE("/NoResizeGraphicsWidget.qml"));
       
   435     QDeclarativeItem *item = qobject_cast<QDeclarativeItem*>(component.create());
       
   436     QGraphicsScene scene;
       
   437     scene.addItem(item);
       
   438 
       
   439     QVERIFY(item != 0);
       
   440     QCOMPARE(item->width(), 200.0);
       
   441     QCOMPARE(item->height(), 80.0);
       
   442 
       
   443     delete item;
       
   444 }
       
   445 
       
   446 void tst_QDeclarativeLoader::networkRequestUrl()
       
   447 {
       
   448     TestHTTPServer server(SERVER_PORT);
       
   449     QVERIFY(server.isValid());
       
   450     server.serveDirectory(SRCDIR "/data");
       
   451 
       
   452     QDeclarativeComponent component(&engine);
       
   453     component.setData(QByteArray("import Qt 4.7\nLoader { property int did_load : 0; source: \"http://127.0.0.1:14450/Rect120x60.qml\"; onLoaded: did_load=123 }"), QUrl::fromLocalFile(SRCDIR "/dummy.qml"));
       
   454     if (component.isError())
       
   455         qDebug() << component.errors();
       
   456     QDeclarativeLoader *loader = qobject_cast<QDeclarativeLoader*>(component.create());
       
   457     QVERIFY(loader != 0);
       
   458 
       
   459     TRY_WAIT(loader->status() == QDeclarativeLoader::Ready);
       
   460 
       
   461     QVERIFY(loader->item());
       
   462     QCOMPARE(loader->progress(), 1.0);
       
   463     QCOMPARE(loader->property("did_load").toInt(), 123);
       
   464     QCOMPARE(static_cast<QGraphicsItem*>(loader)->children().count(), 1);
       
   465 
       
   466     delete loader;
       
   467 }
       
   468 
       
   469 /* XXX Component waits until all dependencies are loaded.  Is this actually possible?
       
   470 void tst_QDeclarativeLoader::networkComponent()
       
   471 {
       
   472     TestHTTPServer server(SERVER_PORT);
       
   473     QVERIFY(server.isValid());
       
   474     server.serveDirectory("slowdata", TestHTTPServer::Delay);
       
   475 
       
   476     QDeclarativeComponent component(&engine);
       
   477     component.setData(QByteArray(
       
   478                 "import Qt 4.7\n"
       
   479                 "import \"http://127.0.0.1:14450/\" as NW\n"
       
   480                 "Item {\n"
       
   481                 " Component { id: comp; NW.SlowRect {} }\n"
       
   482                 " Loader { sourceComponent: comp } }")
       
   483             , TEST_FILE(""));
       
   484 
       
   485     QDeclarativeItem *item = qobject_cast<QDeclarativeItem*>(component.create());
       
   486     QVERIFY(item);
       
   487 
       
   488     QDeclarativeLoader *loader = qobject_cast<QDeclarativeLoader*>(item->QGraphicsObject::children().at(1)); 
       
   489     QVERIFY(loader);
       
   490     TRY_WAIT(loader->status() == QDeclarativeLoader::Ready);
       
   491 
       
   492     QVERIFY(loader->item());
       
   493     QCOMPARE(loader->progress(), 1.0);
       
   494     QCOMPARE(loader->status(), QDeclarativeLoader::Ready);
       
   495     QCOMPARE(static_cast<QGraphicsItem*>(loader)->children().count(), 1);
       
   496 
       
   497     delete loader;
       
   498 }
       
   499 */
       
   500 
       
   501 void tst_QDeclarativeLoader::failNetworkRequest()
       
   502 {
       
   503     TestHTTPServer server(SERVER_PORT);
       
   504     QVERIFY(server.isValid());
       
   505     server.serveDirectory(SRCDIR "/data");
       
   506 
       
   507     QTest::ignoreMessage(QtWarningMsg, "<Unknown File>: Network error for URL http://127.0.0.1:14450/IDontExist.qml");
       
   508 
       
   509     QDeclarativeComponent component(&engine);
       
   510     component.setData(QByteArray("import Qt 4.7\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"));
       
   511     QDeclarativeLoader *loader = qobject_cast<QDeclarativeLoader*>(component.create());
       
   512     QVERIFY(loader != 0);
       
   513 
       
   514     TRY_WAIT(loader->status() == QDeclarativeLoader::Error);
       
   515 
       
   516     QVERIFY(loader->item() == 0);
       
   517     QCOMPARE(loader->progress(), 0.0);
       
   518     QCOMPARE(loader->property("did_load").toInt(), 123);
       
   519     QCOMPARE(static_cast<QGraphicsItem*>(loader)->children().count(), 0);
       
   520 
       
   521     delete loader;
       
   522 }
       
   523 
       
   524 // QTBUG-9241
       
   525 void tst_QDeclarativeLoader::deleteComponentCrash()
       
   526 {
       
   527     QDeclarativeComponent component(&engine, TEST_FILE("crash.qml"));
       
   528     QDeclarativeItem *item = qobject_cast<QDeclarativeItem*>(component.create());
       
   529     QVERIFY(item);
       
   530 
       
   531     item->metaObject()->invokeMethod(item, "setLoaderSource");
       
   532 
       
   533     QDeclarativeLoader *loader = qobject_cast<QDeclarativeLoader*>(item->QGraphicsObject::children().at(0));
       
   534     QVERIFY(loader);
       
   535     QVERIFY(loader->item());
       
   536     QCOMPARE(loader->item()->objectName(), QLatin1String("blue"));
       
   537     QCOMPARE(loader->progress(), 1.0);
       
   538     QCOMPARE(loader->status(), QDeclarativeLoader::Ready);
       
   539     QCOMPARE(static_cast<QGraphicsItem*>(loader)->children().count(), 1);
       
   540     QVERIFY(loader->source() == QUrl::fromLocalFile(SRCDIR "/data/BlueRect.qml"));
       
   541 
       
   542     delete item;
       
   543 }
       
   544 
       
   545 void tst_QDeclarativeLoader::nonItem()
       
   546 {
       
   547     QDeclarativeComponent component(&engine, TEST_FILE("nonItem.qml"));
       
   548     QString err = QUrl::fromLocalFile(SRCDIR).toString() + "/data/nonItem.qml:3:1: QML Loader: Loader does not support loading non-visual elements.";
       
   549 
       
   550     QTest::ignoreMessage(QtWarningMsg, err.toLatin1().constData());
       
   551     QDeclarativeLoader *loader = qobject_cast<QDeclarativeLoader*>(component.create());
       
   552     QVERIFY(loader);
       
   553     QVERIFY(loader->item() == 0);
       
   554 
       
   555     delete loader;
       
   556 }
       
   557 
       
   558 void tst_QDeclarativeLoader::vmeErrors()
       
   559 {
       
   560     QDeclarativeComponent component(&engine, TEST_FILE("vmeErrors.qml"));
       
   561     QString err = QUrl::fromLocalFile(SRCDIR).toString() + "/data/VmeError.qml:6: Cannot assign object type QObject with no default method";
       
   562     QTest::ignoreMessage(QtWarningMsg, err.toLatin1().constData());
       
   563     QDeclarativeLoader *loader = qobject_cast<QDeclarativeLoader*>(component.create());
       
   564     QVERIFY(loader);
       
   565     QVERIFY(loader->item() == 0);
       
   566 
       
   567     delete loader;
       
   568 }
       
   569 
       
   570 QTEST_MAIN(tst_QDeclarativeLoader)
       
   571 
       
   572 #include "tst_qdeclarativeloader.moc"