1 /****************************************************************************
3 ** Copyright (C) 2011 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/qsgitem.h>
47 #include <QtDeclarative/qdeclarativeproperty.h>
48 #include <QtDeclarative/qdeclarativeincubator.h>
50 #include "../shared/util.h"
51 #include "../../../shared/util.h"
53 class MyIC : public QObject, public QDeclarativeIncubationController
57 MyIC() { startTimer(5); }
59 virtual void timerEvent(QTimerEvent*) {
64 class tst_qdeclarativecomponent : public QObject
68 tst_qdeclarativecomponent() { engine.setIncubationController(&ic); }
73 void qmlCreateObject();
74 void qmlCreateObjectWithProperties();
75 void qmlIncubateObject();
78 QDeclarativeEngine engine;
82 void tst_qdeclarativecomponent::null()
85 QDeclarativeComponent c;
90 QDeclarativeComponent c(&engine);
96 void tst_qdeclarativecomponent::loadEmptyUrl()
98 QDeclarativeComponent c(&engine);
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"));
110 void tst_qdeclarativecomponent::qmlIncubateObject()
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);
118 QTRY_VERIFY(object->property("test2").toBool() == true);
123 void tst_qdeclarativecomponent::qmlCreateObject()
125 QDeclarativeEngine engine;
126 QDeclarativeComponent component(&engine, QUrl::fromLocalFile(TESTDATA("createObject.qml")));
127 QObject *object = component.create();
128 QVERIFY(object != 0);
130 QObject *testObject1 = object->property("qobject").value<QObject*>();
131 QVERIFY(testObject1);
132 QVERIFY(testObject1->parent() == object);
134 QObject *testObject2 = object->property("declarativeitem").value<QObject*>();
135 QVERIFY(testObject2);
136 QVERIFY(testObject2->parent() == object);
137 QCOMPARE(testObject2->metaObject()->className(), "QSGItem");
140 void tst_qdeclarativecomponent::qmlCreateObjectWithProperties()
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);
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);
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);
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;
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;
186 QTEST_MAIN(tst_qdeclarativecomponent)
188 #include "tst_qdeclarativecomponent.moc"