1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the test suite of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
44 #include <QtDeclarative/qdeclarativeengine.h>
45 #include <QtDeclarative/qdeclarativecomponent.h>
46 #include <QtDeclarative/qdeclarativeproperty.h>
47 #include <QtDeclarative/qdeclarativeincubator.h>
49 #include "../../shared/util.h"
51 class MyIC : public QObject, public QDeclarativeIncubationController
55 MyIC() { startTimer(5); }
57 virtual void timerEvent(QTimerEvent*) {
62 class tst_qdeclarativecomponent : public QDeclarativeDataTest
66 tst_qdeclarativecomponent() { engine.setIncubationController(&ic); }
71 void qmlCreateObject();
72 void qmlCreateObjectWithProperties();
73 void qmlIncubateObject();
76 QDeclarativeEngine engine;
80 void tst_qdeclarativecomponent::null()
83 QDeclarativeComponent c;
88 QDeclarativeComponent c(&engine);
94 void tst_qdeclarativecomponent::loadEmptyUrl()
96 QDeclarativeComponent c(&engine);
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"));
108 void tst_qdeclarativecomponent::qmlIncubateObject()
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);
116 QTRY_VERIFY(object->property("test2").toBool() == true);
121 void tst_qdeclarativecomponent::qmlCreateObject()
123 QDeclarativeEngine engine;
124 QDeclarativeComponent component(&engine, testFileUrl("createObject.qml"));
125 QObject *object = component.create();
126 QVERIFY(object != 0);
128 QObject *testObject1 = object->property("qobject").value<QObject*>();
129 QVERIFY(testObject1);
130 QVERIFY(testObject1->parent() == object);
132 QObject *testObject2 = object->property("declarativeitem").value<QObject*>();
133 QVERIFY(testObject2);
134 QVERIFY(testObject2->parent() == object);
135 QCOMPARE(testObject2->metaObject()->className(), "QQuickItem");
138 void tst_qdeclarativecomponent::qmlCreateObjectWithProperties()
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);
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);
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);
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;
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;
184 QTEST_MAIN(tst_qdeclarativecomponent)
186 #include "tst_qdeclarativecomponent.moc"