Initial import from the monolithic Qt.
[profile/ivi/qtdeclarative.git] / tests / auto / declarative / qdeclarativecomponent / tst_qdeclarativecomponent.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 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 #include <QtDeclarative/qdeclarativeitem.h>
48 #include <QtDeclarative/qdeclarativeproperty.h>
49 #include <qcolor.h>
50
51 #ifdef Q_OS_SYMBIAN
52 // In Symbian OS test data is located in applications private dir
53 #define SRCDIR "."
54 #endif
55
56 class tst_qdeclarativecomponent : public QObject
57 {
58     Q_OBJECT
59 public:
60     tst_qdeclarativecomponent() { }
61
62 private slots:
63     void null();
64     void loadEmptyUrl();
65     void qmlCreateObject();
66     void qmlCreateObjectWithProperties();
67
68 private:
69     QDeclarativeEngine engine;
70 };
71
72 void tst_qdeclarativecomponent::null()
73 {
74     {
75         QDeclarativeComponent c;
76         QVERIFY(c.isNull());
77     }
78
79     {
80         QDeclarativeComponent c(&engine);
81         QVERIFY(c.isNull());
82     }
83 }
84
85
86 void tst_qdeclarativecomponent::loadEmptyUrl()
87 {
88     QDeclarativeComponent c(&engine);
89     c.loadUrl(QUrl());
90
91     QVERIFY(c.isError());
92     QCOMPARE(c.errors().count(), 1);
93     QDeclarativeError error = c.errors().first();
94     QCOMPARE(error.url(), QUrl());
95     QCOMPARE(error.line(), -1);
96     QCOMPARE(error.column(), -1);
97     QCOMPARE(error.description(), QLatin1String("Invalid empty URL"));
98 }
99
100 void tst_qdeclarativecomponent::qmlCreateObject()
101 {
102     QDeclarativeEngine engine;
103     QDeclarativeComponent component(&engine, QUrl::fromLocalFile(SRCDIR "/data/createObject.qml"));
104     QObject *object = component.create();
105     QVERIFY(object != 0);
106
107     QObject *testObject1 = object->property("qobject").value<QObject*>();
108     QVERIFY(testObject1);
109     QVERIFY(testObject1->parent() == object);
110
111     QObject *testObject2 = object->property("declarativeitem").value<QObject*>();
112     QVERIFY(testObject2);
113     QVERIFY(testObject2->parent() == object);
114     QCOMPARE(testObject2->metaObject()->className(), "QDeclarativeItem");
115
116     //Note that QGraphicsObjects are not exposed to QML for instantiation, and so can't be used in a component directly
117     //Also this is actually the extended type QDeclarativeGraphicsWidget, but it still doesn't inherit QDeclarativeItem
118     QGraphicsObject *testObject3 = qobject_cast<QGraphicsObject*>(object->property("graphicswidget").value<QObject*>());
119     QVERIFY(testObject3);
120     QVERIFY(testObject3->parent() == object);
121     QVERIFY(testObject3->parentItem() == qobject_cast<QGraphicsObject*>(object));
122     QCOMPARE(testObject3->metaObject()->className(), "QDeclarativeGraphicsWidget");
123 }
124
125 void tst_qdeclarativecomponent::qmlCreateObjectWithProperties()
126 {
127     QDeclarativeEngine engine;
128     QDeclarativeComponent component(&engine, QUrl::fromLocalFile(SRCDIR "/data/createObjectWithScript.qml"));
129     QVERIFY2(component.errorString().isEmpty(), component.errorString().toUtf8());
130     QObject *object = component.create();
131     QVERIFY(object != 0);
132
133     QObject *testObject1 = object->property("declarativerectangle").value<QObject*>();
134     QVERIFY(testObject1);
135     QVERIFY(testObject1->parent() == object);
136     QCOMPARE(testObject1->property("x").value<int>(), 17);
137     QCOMPARE(testObject1->property("y").value<int>(), 17);
138     QCOMPARE(testObject1->property("color").value<QColor>(), QColor(255,255,255));
139     QCOMPARE(QDeclarativeProperty::read(testObject1,"border.width").toInt(), 3);
140     QCOMPARE(QDeclarativeProperty::read(testObject1,"innerRect.border.width").toInt(), 20);
141     delete testObject1;
142
143     QObject *testObject2 = object->property("declarativeitem").value<QObject*>();
144     QVERIFY(testObject2);
145     QVERIFY(testObject2->parent() == object);
146     //QCOMPARE(testObject2->metaObject()->className(), "QDeclarativeItem_QML_2");
147     QCOMPARE(testObject2->property("x").value<int>(), 17);
148     QCOMPARE(testObject2->property("y").value<int>(), 17);
149     QCOMPARE(testObject2->property("testBool").value<bool>(), true);
150     QCOMPARE(testObject2->property("testInt").value<int>(), 17);
151     QCOMPARE(testObject2->property("testObject").value<QObject*>(), object);
152     delete testObject2;
153
154     QObject *testBindingObj = object->property("bindingTestObject").value<QObject*>();
155     QVERIFY(testBindingObj);
156     QCOMPARE(testBindingObj->parent(), object);
157     QCOMPARE(testBindingObj->property("testValue").value<int>(), 300);
158     object->setProperty("width", 150);
159     QCOMPARE(testBindingObj->property("testValue").value<int>(), 150 * 3);
160     delete testBindingObj;
161
162     QObject *testBindingThisObj = object->property("bindingThisTestObject").value<QObject*>();
163     QVERIFY(testBindingThisObj);
164     QCOMPARE(testBindingThisObj->parent(), object);
165     QCOMPARE(testBindingThisObj->property("testValue").value<int>(), 900);
166     testBindingThisObj->setProperty("width", 200);
167     QCOMPARE(testBindingThisObj->property("testValue").value<int>(), 200 * 3);
168     delete testBindingThisObj;
169 }
170
171 QTEST_MAIN(tst_qdeclarativecomponent)
172
173 #include "tst_qdeclarativecomponent.moc"