ddf9254f664898ae58c16006d0ce4a182bcb9c4a
[profile/ivi/qtdeclarative.git] / tests / auto / declarative / qdeclarativecomponent / tst_qdeclarativecomponent.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
6 **
7 ** This file is part of the test suite of the Qt Toolkit.
8 **
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.
17 **
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.
21 **
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.
29 **
30 ** Other Usage
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.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 #include <qtest.h>
42 #include <QDebug>
43
44 #include <QtDeclarative/qdeclarativeengine.h>
45 #include <QtDeclarative/qdeclarativecomponent.h>
46 #include <QtDeclarative/qdeclarativeproperty.h>
47 #include <QtDeclarative/qdeclarativeincubator.h>
48 #include <qcolor.h>
49 #include "../../shared/util.h"
50
51 class MyIC : public QObject, public QDeclarativeIncubationController
52 {
53     Q_OBJECT
54 public:
55     MyIC() { startTimer(5); }
56 protected:
57     virtual void timerEvent(QTimerEvent*) {
58         incubateFor(5);
59     }
60 };
61
62 class tst_qdeclarativecomponent : public QDeclarativeDataTest
63 {
64     Q_OBJECT
65 public:
66     tst_qdeclarativecomponent() { engine.setIncubationController(&ic); }
67
68 private slots:
69     void null();
70     void loadEmptyUrl();
71     void qmlCreateObject();
72     void qmlCreateObjectWithProperties();
73     void qmlIncubateObject();
74
75 private:
76     QDeclarativeEngine engine;
77     MyIC ic;
78 };
79
80 void tst_qdeclarativecomponent::null()
81 {
82     {
83         QDeclarativeComponent c;
84         QVERIFY(c.isNull());
85     }
86
87     {
88         QDeclarativeComponent c(&engine);
89         QVERIFY(c.isNull());
90     }
91 }
92
93
94 void tst_qdeclarativecomponent::loadEmptyUrl()
95 {
96     QDeclarativeComponent c(&engine);
97     c.loadUrl(QUrl());
98
99     QVERIFY(c.isError());
100     QCOMPARE(c.errors().count(), 1);
101     QDeclarativeError error = c.errors().first();
102     QCOMPARE(error.url(), QUrl());
103     QCOMPARE(error.line(), -1);
104     QCOMPARE(error.column(), -1);
105     QCOMPARE(error.description(), QLatin1String("Invalid empty URL"));
106 }
107
108 void tst_qdeclarativecomponent::qmlIncubateObject()
109 {
110     QDeclarativeComponent component(&engine, testFileUrl("incubateObject.qml"));
111     QObject *object = component.create();
112     QVERIFY(object != 0);
113     QCOMPARE(object->property("test1").toBool(), true);
114     QCOMPARE(object->property("test2").toBool(), false);
115
116     QTRY_VERIFY(object->property("test2").toBool() == true);
117
118     delete object;
119 }
120
121 void tst_qdeclarativecomponent::qmlCreateObject()
122 {
123     QDeclarativeEngine engine;
124     QDeclarativeComponent component(&engine, testFileUrl("createObject.qml"));
125     QObject *object = component.create();
126     QVERIFY(object != 0);
127
128     QObject *testObject1 = object->property("qobject").value<QObject*>();
129     QVERIFY(testObject1);
130     QVERIFY(testObject1->parent() == object);
131
132     QObject *testObject2 = object->property("declarativeitem").value<QObject*>();
133     QVERIFY(testObject2);
134     QVERIFY(testObject2->parent() == object);
135     QCOMPARE(testObject2->metaObject()->className(), "QQuickItem");
136 }
137
138 void tst_qdeclarativecomponent::qmlCreateObjectWithProperties()
139 {
140     QDeclarativeEngine engine;
141     QDeclarativeComponent component(&engine, testFileUrl("createObjectWithScript.qml"));
142     QVERIFY2(component.errorString().isEmpty(), component.errorString().toUtf8());
143     QObject *object = component.create();
144     QVERIFY(object != 0);
145
146     QObject *testObject1 = object->property("declarativerectangle").value<QObject*>();
147     QVERIFY(testObject1);
148     QVERIFY(testObject1->parent() == object);
149     QCOMPARE(testObject1->property("x").value<int>(), 17);
150     QCOMPARE(testObject1->property("y").value<int>(), 17);
151     QCOMPARE(testObject1->property("color").value<QColor>(), QColor(255,255,255));
152     QCOMPARE(QDeclarativeProperty::read(testObject1,"border.width").toInt(), 3);
153     QCOMPARE(QDeclarativeProperty::read(testObject1,"innerRect.border.width").toInt(), 20);
154     delete testObject1;
155
156     QObject *testObject2 = object->property("declarativeitem").value<QObject*>();
157     QVERIFY(testObject2);
158     QVERIFY(testObject2->parent() == object);
159     //QCOMPARE(testObject2->metaObject()->className(), "QDeclarativeItem_QML_2");
160     QCOMPARE(testObject2->property("x").value<int>(), 17);
161     QCOMPARE(testObject2->property("y").value<int>(), 17);
162     QCOMPARE(testObject2->property("testBool").value<bool>(), true);
163     QCOMPARE(testObject2->property("testInt").value<int>(), 17);
164     QCOMPARE(testObject2->property("testObject").value<QObject*>(), object);
165     delete testObject2;
166
167     QObject *testBindingObj = object->property("bindingTestObject").value<QObject*>();
168     QVERIFY(testBindingObj);
169     QCOMPARE(testBindingObj->parent(), object);
170     QCOMPARE(testBindingObj->property("testValue").value<int>(), 300);
171     object->setProperty("width", 150);
172     QCOMPARE(testBindingObj->property("testValue").value<int>(), 150 * 3);
173     delete testBindingObj;
174
175     QObject *testBindingThisObj = object->property("bindingThisTestObject").value<QObject*>();
176     QVERIFY(testBindingThisObj);
177     QCOMPARE(testBindingThisObj->parent(), object);
178     QCOMPARE(testBindingThisObj->property("testValue").value<int>(), 900);
179     testBindingThisObj->setProperty("width", 200);
180     QCOMPARE(testBindingThisObj->property("testValue").value<int>(), 200 * 3);
181     delete testBindingThisObj;
182 }
183
184 QTEST_MAIN(tst_qdeclarativecomponent)
185
186 #include "tst_qdeclarativecomponent.moc"