Use TESTDATA macro in declarative tests.
[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 ** 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/qsgitem.h>
47 #include <QtDeclarative/qdeclarativeproperty.h>
48 #include <QtDeclarative/qdeclarativeincubator.h>
49 #include <qcolor.h>
50 #include "../shared/util.h"
51 #include "../../../shared/util.h"
52
53 class MyIC : public QObject, public QDeclarativeIncubationController
54 {
55     Q_OBJECT
56 public:
57     MyIC() { startTimer(5); }
58 protected:
59     virtual void timerEvent(QTimerEvent*) {
60         incubateFor(5);
61     }
62 };
63
64 class tst_qdeclarativecomponent : public QObject
65 {
66     Q_OBJECT
67 public:
68     tst_qdeclarativecomponent() { engine.setIncubationController(&ic); }
69
70 private slots:
71     void null();
72     void loadEmptyUrl();
73     void qmlCreateObject();
74     void qmlCreateObjectWithProperties();
75     void qmlIncubateObject();
76
77 private:
78     QDeclarativeEngine engine;
79     MyIC ic;
80 };
81
82 void tst_qdeclarativecomponent::null()
83 {
84     {
85         QDeclarativeComponent c;
86         QVERIFY(c.isNull());
87     }
88
89     {
90         QDeclarativeComponent c(&engine);
91         QVERIFY(c.isNull());
92     }
93 }
94
95
96 void tst_qdeclarativecomponent::loadEmptyUrl()
97 {
98     QDeclarativeComponent c(&engine);
99     c.loadUrl(QUrl());
100
101     QVERIFY(c.isError());
102     QCOMPARE(c.errors().count(), 1);
103     QDeclarativeError error = c.errors().first();
104     QCOMPARE(error.url(), QUrl());
105     QCOMPARE(error.line(), -1);
106     QCOMPARE(error.column(), -1);
107     QCOMPARE(error.description(), QLatin1String("Invalid empty URL"));
108 }
109
110 void tst_qdeclarativecomponent::qmlIncubateObject()
111 {
112     QDeclarativeComponent component(&engine, QUrl::fromLocalFile(TESTDATA("incubateObject.qml")));
113     QObject *object = component.create();
114     QVERIFY(object != 0);
115     QCOMPARE(object->property("test1").toBool(), true);
116     QCOMPARE(object->property("test2").toBool(), false);
117
118     QTRY_VERIFY(object->property("test2").toBool() == true);
119
120     delete object;
121 }
122
123 void tst_qdeclarativecomponent::qmlCreateObject()
124 {
125     QDeclarativeEngine engine;
126     QDeclarativeComponent component(&engine, QUrl::fromLocalFile(TESTDATA("createObject.qml")));
127     QObject *object = component.create();
128     QVERIFY(object != 0);
129
130     QObject *testObject1 = object->property("qobject").value<QObject*>();
131     QVERIFY(testObject1);
132     QVERIFY(testObject1->parent() == object);
133
134     QObject *testObject2 = object->property("declarativeitem").value<QObject*>();
135     QVERIFY(testObject2);
136     QVERIFY(testObject2->parent() == object);
137     QCOMPARE(testObject2->metaObject()->className(), "QSGItem");
138 }
139
140 void tst_qdeclarativecomponent::qmlCreateObjectWithProperties()
141 {
142     QDeclarativeEngine engine;
143     QDeclarativeComponent component(&engine, QUrl::fromLocalFile(TESTDATA("createObjectWithScript.qml")));
144     QVERIFY2(component.errorString().isEmpty(), component.errorString().toUtf8());
145     QObject *object = component.create();
146     QVERIFY(object != 0);
147
148     QObject *testObject1 = object->property("declarativerectangle").value<QObject*>();
149     QVERIFY(testObject1);
150     QVERIFY(testObject1->parent() == object);
151     QCOMPARE(testObject1->property("x").value<int>(), 17);
152     QCOMPARE(testObject1->property("y").value<int>(), 17);
153     QCOMPARE(testObject1->property("color").value<QColor>(), QColor(255,255,255));
154     QCOMPARE(QDeclarativeProperty::read(testObject1,"border.width").toInt(), 3);
155     QCOMPARE(QDeclarativeProperty::read(testObject1,"innerRect.border.width").toInt(), 20);
156     delete testObject1;
157
158     QObject *testObject2 = object->property("declarativeitem").value<QObject*>();
159     QVERIFY(testObject2);
160     QVERIFY(testObject2->parent() == object);
161     //QCOMPARE(testObject2->metaObject()->className(), "QDeclarativeItem_QML_2");
162     QCOMPARE(testObject2->property("x").value<int>(), 17);
163     QCOMPARE(testObject2->property("y").value<int>(), 17);
164     QCOMPARE(testObject2->property("testBool").value<bool>(), true);
165     QCOMPARE(testObject2->property("testInt").value<int>(), 17);
166     QCOMPARE(testObject2->property("testObject").value<QObject*>(), object);
167     delete testObject2;
168
169     QObject *testBindingObj = object->property("bindingTestObject").value<QObject*>();
170     QVERIFY(testBindingObj);
171     QCOMPARE(testBindingObj->parent(), object);
172     QCOMPARE(testBindingObj->property("testValue").value<int>(), 300);
173     object->setProperty("width", 150);
174     QCOMPARE(testBindingObj->property("testValue").value<int>(), 150 * 3);
175     delete testBindingObj;
176
177     QObject *testBindingThisObj = object->property("bindingThisTestObject").value<QObject*>();
178     QVERIFY(testBindingThisObj);
179     QCOMPARE(testBindingThisObj->parent(), object);
180     QCOMPARE(testBindingThisObj->property("testValue").value<int>(), 900);
181     testBindingThisObj->setProperty("width", 200);
182     QCOMPARE(testBindingThisObj->property("testValue").value<int>(), 200 * 3);
183     delete testBindingThisObj;
184 }
185
186 QTEST_MAIN(tst_qdeclarativecomponent)
187
188 #include "tst_qdeclarativecomponent.moc"