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 <QtGui/qgraphicsitem.h>
45 #include <QtDeclarative/qdeclarativeengine.h>
46 #include <QtDeclarative/qdeclarativecomponent.h>
47 #include <QtDeclarative/qdeclarativeitem.h>
48 #include <QtDeclarative/qdeclarativeproperty.h>
52 // In Symbian OS test data is located in applications private dir
56 class tst_qdeclarativecomponent : public QObject
60 tst_qdeclarativecomponent() { }
65 void qmlCreateObject();
66 void qmlCreateObjectWithProperties();
69 QDeclarativeEngine engine;
72 void tst_qdeclarativecomponent::null()
75 QDeclarativeComponent c;
80 QDeclarativeComponent c(&engine);
86 void tst_qdeclarativecomponent::loadEmptyUrl()
88 QDeclarativeComponent c(&engine);
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"));
100 void tst_qdeclarativecomponent::qmlCreateObject()
102 QDeclarativeEngine engine;
103 QDeclarativeComponent component(&engine, QUrl::fromLocalFile(SRCDIR "/data/createObject.qml"));
104 QObject *object = component.create();
105 QVERIFY(object != 0);
107 QObject *testObject1 = object->property("qobject").value<QObject*>();
108 QVERIFY(testObject1);
109 QVERIFY(testObject1->parent() == object);
111 QObject *testObject2 = object->property("declarativeitem").value<QObject*>();
112 QVERIFY(testObject2);
113 QVERIFY(testObject2->parent() == object);
114 QCOMPARE(testObject2->metaObject()->className(), "QDeclarativeItem");
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");
125 void tst_qdeclarativecomponent::qmlCreateObjectWithProperties()
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);
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);
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);
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;
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;
171 QTEST_MAIN(tst_qdeclarativecomponent)
173 #include "tst_qdeclarativecomponent.moc"