tests/auto/declarative/qdeclarativeengine/tst_qdeclarativeengine.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 
       
    42 #include <qtest.h>
       
    43 #include <QDeclarativeEngine>
       
    44 #include <QDeclarativeContext>
       
    45 #include <QNetworkAccessManager>
       
    46 #include <QPointer>
       
    47 #include <QDir>
       
    48 #include <QDesktopServices>
       
    49 #include <QDebug>
       
    50 #include <QDeclarativeComponent>
       
    51 #include <QDeclarativeNetworkAccessManagerFactory>
       
    52 
       
    53 class tst_qdeclarativeengine : public QObject
       
    54 {
       
    55     Q_OBJECT
       
    56 public:
       
    57     tst_qdeclarativeengine() {}
       
    58 
       
    59 private slots:
       
    60     void rootContext();
       
    61     void networkAccessManager();
       
    62     void baseUrl();
       
    63     void contextForObject();
       
    64     void offlineStoragePath();
       
    65     void clearComponentCache();
       
    66     void outputWarningsToStandardError();
       
    67     void objectOwnership();
       
    68 };
       
    69 
       
    70 void tst_qdeclarativeengine::rootContext()
       
    71 {
       
    72     QDeclarativeEngine engine;
       
    73 
       
    74     QVERIFY(engine.rootContext());
       
    75 
       
    76     QCOMPARE(engine.rootContext()->engine(), &engine);
       
    77     QVERIFY(engine.rootContext()->parentContext() == 0);
       
    78 }
       
    79 
       
    80 class NetworkAccessManagerFactory : public QDeclarativeNetworkAccessManagerFactory
       
    81 {
       
    82 public:
       
    83     NetworkAccessManagerFactory() : manager(0) {}
       
    84 
       
    85     QNetworkAccessManager *create(QObject *parent) {
       
    86         manager = new QNetworkAccessManager(parent);
       
    87         return manager;
       
    88     }
       
    89 
       
    90     QNetworkAccessManager *manager;
       
    91 };
       
    92 
       
    93 void tst_qdeclarativeengine::networkAccessManager()
       
    94 {
       
    95     QDeclarativeEngine *engine = new QDeclarativeEngine;
       
    96 
       
    97     // Test QDeclarativeEngine created manager
       
    98     QPointer<QNetworkAccessManager> manager = engine->networkAccessManager();
       
    99     QVERIFY(manager != 0);
       
   100     delete engine;
       
   101 
       
   102     // Test factory created manager
       
   103     engine = new QDeclarativeEngine;
       
   104     NetworkAccessManagerFactory factory;
       
   105     engine->setNetworkAccessManagerFactory(&factory);
       
   106     QVERIFY(engine->networkAccessManagerFactory() == &factory);
       
   107     QVERIFY(engine->networkAccessManager() == factory.manager);
       
   108     delete engine;
       
   109 }
       
   110 
       
   111 void tst_qdeclarativeengine::baseUrl()
       
   112 {
       
   113     QDeclarativeEngine engine;
       
   114 
       
   115     QUrl cwd = QUrl::fromLocalFile(QDir::currentPath() + QDir::separator());
       
   116 
       
   117     QCOMPARE(engine.baseUrl(), cwd);
       
   118     QCOMPARE(engine.rootContext()->resolvedUrl(QUrl("main.qml")), cwd.resolved(QUrl("main.qml")));
       
   119 
       
   120     QDir dir = QDir::current();
       
   121     dir.cdUp();
       
   122     QVERIFY(dir != QDir::current());
       
   123     QDir::setCurrent(dir.path());
       
   124     QVERIFY(QDir::current() == dir);
       
   125 
       
   126     QUrl cwd2 = QUrl::fromLocalFile(QDir::currentPath() + QDir::separator());
       
   127     QCOMPARE(engine.baseUrl(), cwd2);
       
   128     QCOMPARE(engine.rootContext()->resolvedUrl(QUrl("main.qml")), cwd2.resolved(QUrl("main.qml")));
       
   129 
       
   130     engine.setBaseUrl(cwd);
       
   131     QCOMPARE(engine.baseUrl(), cwd);
       
   132     QCOMPARE(engine.rootContext()->resolvedUrl(QUrl("main.qml")), cwd.resolved(QUrl("main.qml")));
       
   133 }
       
   134 
       
   135 void tst_qdeclarativeengine::contextForObject()
       
   136 {
       
   137     QDeclarativeEngine *engine = new QDeclarativeEngine;
       
   138 
       
   139     // Test null-object
       
   140     QVERIFY(QDeclarativeEngine::contextForObject(0) == 0);
       
   141 
       
   142     // Test an object with no context
       
   143     QObject object;
       
   144     QVERIFY(QDeclarativeEngine::contextForObject(&object) == 0);
       
   145 
       
   146     // Test setting null-object
       
   147     QDeclarativeEngine::setContextForObject(0, engine->rootContext());
       
   148 
       
   149     // Test setting null-context
       
   150     QDeclarativeEngine::setContextForObject(&object, 0);
       
   151 
       
   152     // Test setting context
       
   153     QDeclarativeEngine::setContextForObject(&object, engine->rootContext());
       
   154     QVERIFY(QDeclarativeEngine::contextForObject(&object) == engine->rootContext());
       
   155 
       
   156     QDeclarativeContext context(engine->rootContext());
       
   157 
       
   158     // Try changing context
       
   159     QTest::ignoreMessage(QtWarningMsg, "QDeclarativeEngine::setContextForObject(): Object already has a QDeclarativeContext");
       
   160     QDeclarativeEngine::setContextForObject(&object, &context);
       
   161     QVERIFY(QDeclarativeEngine::contextForObject(&object) == engine->rootContext());
       
   162 
       
   163     // Delete context
       
   164     delete engine; engine = 0;
       
   165     QVERIFY(QDeclarativeEngine::contextForObject(&object) == 0);
       
   166 }
       
   167 
       
   168 void tst_qdeclarativeengine::offlineStoragePath()
       
   169 {
       
   170     // Without these set, QDesktopServices::storageLocation returns
       
   171     // strings with extra "//" at the end. We set them to ignore this problem.
       
   172     qApp->setApplicationName("tst_qdeclarativeengine");
       
   173     qApp->setOrganizationName("Nokia");
       
   174     qApp->setOrganizationDomain("nokia.com");
       
   175 
       
   176     QDeclarativeEngine engine;
       
   177 
       
   178     QDir dir(QDesktopServices::storageLocation(QDesktopServices::DataLocation));
       
   179     dir.mkpath("QML");
       
   180     dir.cd("QML");
       
   181     dir.mkpath("OfflineStorage");
       
   182     dir.cd("OfflineStorage");
       
   183 
       
   184     QCOMPARE(QDir::fromNativeSeparators(engine.offlineStoragePath()), dir.path());
       
   185 
       
   186     engine.setOfflineStoragePath(QDir::homePath());
       
   187     QCOMPARE(engine.offlineStoragePath(), QDir::homePath());
       
   188 }
       
   189 
       
   190 void tst_qdeclarativeengine::clearComponentCache()
       
   191 {
       
   192     QDeclarativeEngine engine;
       
   193 
       
   194     // Create original qml file
       
   195     {
       
   196         QFile file("temp.qml");
       
   197         QVERIFY(file.open(QIODevice::WriteOnly));
       
   198         file.write("import Qt 4.7\nQtObject {\nproperty int test: 10\n}\n");
       
   199         file.close();
       
   200     }
       
   201 
       
   202     // Test "test" property
       
   203     {
       
   204         QDeclarativeComponent component(&engine, "temp.qml");
       
   205         QObject *obj = component.create();
       
   206         QVERIFY(obj != 0);
       
   207         QCOMPARE(obj->property("test").toInt(), 10);
       
   208         delete obj;
       
   209     }
       
   210 
       
   211     // Modify qml file
       
   212     {
       
   213         QFile file("temp.qml");
       
   214         QVERIFY(file.open(QIODevice::WriteOnly));
       
   215         file.write("import Qt 4.7\nQtObject {\nproperty int test: 11\n}\n");
       
   216         file.close();
       
   217     }
       
   218 
       
   219     // Test cache hit
       
   220     {
       
   221         QDeclarativeComponent component(&engine, "temp.qml");
       
   222         QObject *obj = component.create();
       
   223         QVERIFY(obj != 0);
       
   224         QCOMPARE(obj->property("test").toInt(), 10);
       
   225         delete obj;
       
   226     }
       
   227 
       
   228     // Clear cache
       
   229     engine.clearComponentCache();
       
   230 
       
   231     // Test cache refresh
       
   232     {
       
   233         QDeclarativeComponent component(&engine, "temp.qml");
       
   234         QObject *obj = component.create();
       
   235         QVERIFY(obj != 0);
       
   236         QCOMPARE(obj->property("test").toInt(), 11);
       
   237         delete obj;
       
   238     }
       
   239 }
       
   240 
       
   241 static QStringList warnings;
       
   242 static void msgHandler(QtMsgType, const char *warning)
       
   243 {
       
   244     warnings << QString::fromUtf8(warning);
       
   245 }
       
   246 
       
   247 void tst_qdeclarativeengine::outputWarningsToStandardError()
       
   248 {
       
   249     QDeclarativeEngine engine;
       
   250 
       
   251     QCOMPARE(engine.outputWarningsToStandardError(), true);
       
   252 
       
   253     QDeclarativeComponent c(&engine);
       
   254     c.setData("import Qt 4.7; QtObject { property int a: undefined }", QUrl());
       
   255 
       
   256     QVERIFY(c.isReady() == true);
       
   257 
       
   258     warnings.clear();
       
   259     QtMsgHandler old = qInstallMsgHandler(msgHandler);
       
   260 
       
   261     QObject *o = c.create();
       
   262 
       
   263     qInstallMsgHandler(old);
       
   264 
       
   265     QVERIFY(o != 0);
       
   266     delete o;
       
   267 
       
   268     QCOMPARE(warnings.count(), 1);
       
   269     QCOMPARE(warnings.at(0), QLatin1String("<Unknown File>:1: Unable to assign [undefined] to int a"));
       
   270     warnings.clear();
       
   271 
       
   272 
       
   273     engine.setOutputWarningsToStandardError(false);
       
   274     QCOMPARE(engine.outputWarningsToStandardError(), false);
       
   275 
       
   276 
       
   277     old = qInstallMsgHandler(msgHandler);
       
   278 
       
   279     o = c.create();
       
   280 
       
   281     qInstallMsgHandler(old);
       
   282 
       
   283     QVERIFY(o != 0);
       
   284     delete o;
       
   285 
       
   286     QCOMPARE(warnings.count(), 0);
       
   287 }
       
   288 
       
   289 void tst_qdeclarativeengine::objectOwnership()
       
   290 {
       
   291     {
       
   292     QCOMPARE(QDeclarativeEngine::objectOwnership(0), QDeclarativeEngine::CppOwnership);
       
   293     QDeclarativeEngine::setObjectOwnership(0, QDeclarativeEngine::JavaScriptOwnership);
       
   294     QCOMPARE(QDeclarativeEngine::objectOwnership(0), QDeclarativeEngine::CppOwnership);
       
   295     }
       
   296 
       
   297     {
       
   298     QObject o;
       
   299     QCOMPARE(QDeclarativeEngine::objectOwnership(&o), QDeclarativeEngine::CppOwnership);
       
   300     QDeclarativeEngine::setObjectOwnership(&o, QDeclarativeEngine::CppOwnership);
       
   301     QCOMPARE(QDeclarativeEngine::objectOwnership(&o), QDeclarativeEngine::CppOwnership);
       
   302     QDeclarativeEngine::setObjectOwnership(&o, QDeclarativeEngine::JavaScriptOwnership);
       
   303     QCOMPARE(QDeclarativeEngine::objectOwnership(&o), QDeclarativeEngine::JavaScriptOwnership);
       
   304     QDeclarativeEngine::setObjectOwnership(&o, QDeclarativeEngine::CppOwnership);
       
   305     QCOMPARE(QDeclarativeEngine::objectOwnership(&o), QDeclarativeEngine::CppOwnership);
       
   306     }
       
   307 
       
   308     {
       
   309     QDeclarativeEngine engine;
       
   310     QDeclarativeComponent c(&engine);
       
   311     c.setData("import Qt 4.7; QtObject { property QtObject object: QtObject {} }", QUrl());
       
   312 
       
   313     QObject *o = c.create();
       
   314     QVERIFY(o != 0);
       
   315 
       
   316     QCOMPARE(QDeclarativeEngine::objectOwnership(o), QDeclarativeEngine::CppOwnership);
       
   317 
       
   318     QObject *o2 = qvariant_cast<QObject *>(o->property("object"));
       
   319     QCOMPARE(QDeclarativeEngine::objectOwnership(o2), QDeclarativeEngine::JavaScriptOwnership);
       
   320 
       
   321     delete o;
       
   322     }
       
   323 
       
   324 }
       
   325 
       
   326 QTEST_MAIN(tst_qdeclarativeengine)
       
   327 
       
   328 #include "tst_qdeclarativeengine.moc"