tests/auto/declarative/qdeclarativecomponent/tst_qdeclarativecomponent.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 <QDebug>
       
    43 
       
    44 #include <QtGui/qgraphicsitem.h>
       
    45 #include <QtDeclarative/qdeclarativeengine.h>
       
    46 #include <QtDeclarative/qdeclarativecomponent.h>
       
    47 
       
    48 class tst_qdeclarativecomponent : public QObject
       
    49 {
       
    50     Q_OBJECT
       
    51 public:
       
    52     tst_qdeclarativecomponent() { }
       
    53 
       
    54 private slots:
       
    55     void loadEmptyUrl();
       
    56     void qmlCreateObject();
       
    57 
       
    58 private:
       
    59     QDeclarativeEngine engine;
       
    60 };
       
    61 
       
    62 void tst_qdeclarativecomponent::loadEmptyUrl()
       
    63 {
       
    64     QDeclarativeComponent c(&engine);
       
    65     c.loadUrl(QUrl());
       
    66 
       
    67     QVERIFY(c.isError());
       
    68     QCOMPARE(c.errors().count(), 1);
       
    69     QDeclarativeError error = c.errors().first();
       
    70     QCOMPARE(error.url(), QUrl());
       
    71     QCOMPARE(error.line(), -1);
       
    72     QCOMPARE(error.column(), -1);
       
    73     QCOMPARE(error.description(), QLatin1String("Invalid empty URL"));
       
    74 }
       
    75 
       
    76 void tst_qdeclarativecomponent::qmlCreateObject()
       
    77 {
       
    78     QDeclarativeEngine engine;
       
    79     QDeclarativeComponent component(&engine, QUrl::fromLocalFile(SRCDIR "/data/createObject.qml"));
       
    80     QObject *object = component.create();
       
    81     QVERIFY(object != 0);
       
    82 
       
    83     QObject *testObject1 = object->property("qobject").value<QObject*>();
       
    84     QVERIFY(testObject1);
       
    85     QVERIFY(testObject1->parent() == object);
       
    86 
       
    87     QObject *testObject2 = object->property("declarativeitem").value<QObject*>();
       
    88     QVERIFY(testObject2);
       
    89     QVERIFY(testObject2->parent() == object);
       
    90     QCOMPARE(testObject2->metaObject()->className(), "QDeclarativeItem");
       
    91 
       
    92     //Note that QGraphicsObjects are not exposed to QML for instantiation, and so can't be used in a component directly
       
    93     //Also this is actually the extended type QDeclarativeGraphicsWidget, but it still doesn't inherit QDeclarativeItem
       
    94     QGraphicsObject *testObject3 = qobject_cast<QGraphicsObject*>(object->property("graphicswidget").value<QObject*>());
       
    95     QVERIFY(testObject3);
       
    96     QVERIFY(testObject3->parent() == object);
       
    97     QVERIFY(testObject3->parentItem() == qobject_cast<QGraphicsObject*>(object));
       
    98     QCOMPARE(testObject3->metaObject()->className(), "QDeclarativeGraphicsWidget");
       
    99 }
       
   100 
       
   101 QTEST_MAIN(tst_qdeclarativecomponent)
       
   102 
       
   103 #include "tst_qdeclarativecomponent.moc"