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 ****************************************************************************/
42 #include <QtTest/QSignalSpy>
43 #include <QtDeclarative/qdeclarativecomponent.h>
44 #include <QtDeclarative/qdeclarativecontext.h>
45 #include <QtQuick1/qdeclarativeview.h>
46 #include <QtQuick1/qdeclarativeitem.h>
47 #include <QtWidgets/qgraphicswidget.h>
48 #include "../../../shared/util.h"
51 // In Symbian OS test data is located in applications private dir
55 class tst_QDeclarativeView : public QObject
60 tst_QDeclarativeView();
64 void resizemodedeclarativeitem();
65 void resizemodegraphicswidget();
70 T *findItem(QGraphicsObject *parent, const QString &objectName);
74 tst_QDeclarativeView::tst_QDeclarativeView()
78 void tst_QDeclarativeView::scene()
82 scene.setItemIndexMethod(QGraphicsScene::NoIndex);
83 scene.setStickyFocus(true);
85 QDeclarativeView *view = new QDeclarativeView();
87 QVERIFY(view->scene());
88 view->setScene(&scene);
89 QCOMPARE(view->scene(), &scene);
91 view->setSource(QUrl::fromLocalFile(SRCDIR "/data/resizemodedeclarativeitem.qml"));
92 QDeclarativeItem* declarativeItem = qobject_cast<QDeclarativeItem*>(view->rootObject());
93 QVERIFY(declarativeItem);
94 QVERIFY(scene.items().count() > 0);
95 QCOMPARE(scene.items().at(0), declarativeItem);
100 void tst_QDeclarativeView::resizemodedeclarativeitem()
103 QDeclarativeView *canvas = new QDeclarativeView(&window);
105 QSignalSpy sceneResizedSpy(canvas, SIGNAL(sceneResized(QSize)));
106 canvas->setResizeMode(QDeclarativeView::SizeRootObjectToView);
107 QCOMPARE(QSize(0,0), canvas->initialSize());
108 canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/resizemodedeclarativeitem.qml"));
109 QDeclarativeItem* declarativeItem = qobject_cast<QDeclarativeItem*>(canvas->rootObject());
110 QVERIFY(declarativeItem);
113 // initial size from root object
114 QCOMPARE(declarativeItem->width(), 200.0);
115 QCOMPARE(declarativeItem->height(), 200.0);
116 QCOMPARE(canvas->size(), QSize(200, 200));
117 QCOMPARE(canvas->size(), canvas->sizeHint());
118 QCOMPARE(canvas->size(), canvas->initialSize());
119 QCOMPARE(sceneResizedSpy.count(), 1);
121 // size update from view
122 canvas->resize(QSize(80,100));
123 QCOMPARE(declarativeItem->width(), 80.0);
124 QCOMPARE(declarativeItem->height(), 100.0);
125 QCOMPARE(canvas->size(), QSize(80, 100));
126 QCOMPARE(canvas->size(), canvas->sizeHint());
127 QCOMPARE(sceneResizedSpy.count(), 2);
129 canvas->setResizeMode(QDeclarativeView::SizeViewToRootObject);
131 // size update from view disabled
132 canvas->resize(QSize(60,80));
133 QCOMPARE(declarativeItem->width(), 80.0);
134 QCOMPARE(declarativeItem->height(), 100.0);
135 QCOMPARE(canvas->size(), QSize(60, 80));
136 QCOMPARE(sceneResizedSpy.count(), 3);
138 // size update from root object
139 declarativeItem->setWidth(250);
140 declarativeItem->setHeight(350);
141 QCOMPARE(declarativeItem->width(), 250.0);
142 QCOMPARE(declarativeItem->height(), 350.0);
143 QTRY_COMPARE(canvas->size(), QSize(250, 350));
144 QCOMPARE(canvas->size(), QSize(250, 350));
145 QCOMPARE(canvas->size(), canvas->sizeHint());
146 QCOMPARE(sceneResizedSpy.count(), 4);
151 canvas = new QDeclarativeView(&window);
153 QSignalSpy sceneResizedSpy2(canvas, SIGNAL(sceneResized(QSize)));
154 canvas->setResizeMode(QDeclarativeView::SizeViewToRootObject);
155 canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/resizemodedeclarativeitem.qml"));
156 declarativeItem = qobject_cast<QDeclarativeItem*>(canvas->rootObject());
157 QVERIFY(declarativeItem);
160 // initial size for root object
161 QCOMPARE(declarativeItem->width(), 200.0);
162 QCOMPARE(declarativeItem->height(), 200.0);
163 QCOMPARE(canvas->size(), canvas->sizeHint());
164 QCOMPARE(canvas->size(), canvas->initialSize());
165 QCOMPARE(sceneResizedSpy2.count(), 1);
167 // size update from root object
168 declarativeItem->setWidth(80);
169 declarativeItem->setHeight(100);
170 QCOMPARE(declarativeItem->width(), 80.0);
171 QCOMPARE(declarativeItem->height(), 100.0);
172 QTRY_COMPARE(canvas->size(), QSize(80, 100));
173 QCOMPARE(canvas->size(), QSize(80, 100));
174 QCOMPARE(canvas->size(), canvas->sizeHint());
175 QCOMPARE(sceneResizedSpy2.count(), 2);
177 // size update from root object disabled
178 canvas->setResizeMode(QDeclarativeView::SizeRootObjectToView);
179 declarativeItem->setWidth(60);
180 declarativeItem->setHeight(80);
181 QCOMPARE(canvas->width(), 80);
182 QCOMPARE(canvas->height(), 100);
183 QCOMPARE(QSize(declarativeItem->width(), declarativeItem->height()), canvas->sizeHint());
184 QCOMPARE(sceneResizedSpy2.count(), 2);
186 // size update from view
187 canvas->resize(QSize(200,300));
188 QCOMPARE(declarativeItem->width(), 200.0);
189 QCOMPARE(declarativeItem->height(), 300.0);
190 QCOMPARE(canvas->size(), QSize(200, 300));
191 QCOMPARE(canvas->size(), canvas->sizeHint());
192 QCOMPARE(sceneResizedSpy2.count(), 3);
196 canvas = new QDeclarativeView(&window);
197 canvas->resize(300, 300);
198 canvas->setResizeMode(QDeclarativeView::SizeRootObjectToView);
199 QCOMPARE(QSize(0,0), canvas->initialSize());
200 canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/resizemodedeclarativeitem.qml"));
201 declarativeItem = qobject_cast<QDeclarativeItem*>(canvas->rootObject());
202 QVERIFY(declarativeItem);
205 // initial size from root object
206 QCOMPARE(declarativeItem->width(), 300.0);
207 QCOMPARE(declarativeItem->height(), 300.0);
208 QCOMPARE(canvas->size(), QSize(300, 300));
209 QCOMPARE(canvas->size(), canvas->sizeHint());
210 QCOMPARE(canvas->initialSize(), QSize(200, 200));
214 void tst_QDeclarativeView::resizemodegraphicswidget()
217 QDeclarativeView *canvas = new QDeclarativeView(&window);
219 QSignalSpy sceneResizedSpy(canvas, SIGNAL(sceneResized(QSize)));
220 canvas->setResizeMode(QDeclarativeView::SizeRootObjectToView);
221 canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/resizemodegraphicswidget.qml"));
222 QGraphicsWidget* graphicsWidget = qobject_cast<QGraphicsWidget*>(canvas->rootObject());
223 QVERIFY(graphicsWidget);
226 // initial size from root object
227 QCOMPARE(graphicsWidget->size(), QSizeF(200.0, 200.0));
228 QCOMPARE(canvas->size(), QSize(200, 200));
229 QCOMPARE(canvas->size(), QSize(200, 200));
230 QCOMPARE(canvas->size(), canvas->sizeHint());
231 QCOMPARE(canvas->size(), canvas->initialSize());
232 QCOMPARE(sceneResizedSpy.count(), 1);
234 // size update from view
235 canvas->resize(QSize(80,100));
236 QCOMPARE(graphicsWidget->size(), QSizeF(80.0,100.0));
237 QCOMPARE(canvas->size(), QSize(80,100));
238 QCOMPARE(canvas->size(), canvas->sizeHint());
239 QCOMPARE(sceneResizedSpy.count(), 2);
241 // size update from view disabled
242 canvas->setResizeMode(QDeclarativeView::SizeViewToRootObject);
243 canvas->resize(QSize(60,80));
244 QCOMPARE(graphicsWidget->size(), QSizeF(80.0,100.0));
245 QCOMPARE(canvas->size(), QSize(60, 80));
246 QCOMPARE(sceneResizedSpy.count(), 3);
248 // size update from root object
249 graphicsWidget->resize(QSizeF(250.0, 350.0));
250 QCOMPARE(graphicsWidget->size(), QSizeF(250.0,350.0));
251 QCOMPARE(canvas->size(), QSize(250, 350));
252 QCOMPARE(canvas->size(), canvas->sizeHint());
253 QCOMPARE(sceneResizedSpy.count(), 4);
258 canvas = new QDeclarativeView(&window);
260 QSignalSpy sceneResizedSpy2(canvas, SIGNAL(sceneResized(QSize)));
261 canvas->setResizeMode(QDeclarativeView::SizeViewToRootObject);
262 canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/resizemodegraphicswidget.qml"));
263 graphicsWidget = qobject_cast<QGraphicsWidget*>(canvas->rootObject());
264 QVERIFY(graphicsWidget);
267 // initial size from root object
268 QCOMPARE(graphicsWidget->size(), QSizeF(200.0, 200.0));
269 QCOMPARE(canvas->size(), QSize(200, 200));
270 QCOMPARE(canvas->size(), canvas->sizeHint());
271 QCOMPARE(canvas->size(), canvas->initialSize());
272 QCOMPARE(sceneResizedSpy2.count(), 1);
274 // size update from root object
275 graphicsWidget->resize(QSizeF(80, 100));
276 QCOMPARE(graphicsWidget->size(), QSizeF(80.0, 100.0));
277 QCOMPARE(canvas->size(), QSize(80, 100));
278 QCOMPARE(canvas->size(), canvas->sizeHint());
279 QCOMPARE(sceneResizedSpy2.count(), 2);
281 // size update from root object disabled
282 canvas->setResizeMode(QDeclarativeView::SizeRootObjectToView);
283 graphicsWidget->resize(QSizeF(60,80));
284 QCOMPARE(canvas->size(), QSize(80,100));
285 QCOMPARE(QSize(graphicsWidget->size().width(), graphicsWidget->size().height()), canvas->sizeHint());
286 QCOMPARE(sceneResizedSpy2.count(), 2);
288 // size update from view
289 canvas->resize(QSize(200,300));
290 QCOMPARE(graphicsWidget->size(), QSizeF(200.0, 300.0));
291 QCOMPARE(canvas->size(), QSize(200, 300));
292 QCOMPARE(canvas->size(), canvas->sizeHint());
293 QCOMPARE(sceneResizedSpy2.count(), 3);
299 static void silentErrorsMsgHandler(QtMsgType, const char *)
303 void tst_QDeclarativeView::errors()
305 QDeclarativeView *canvas = new QDeclarativeView;
307 QtMsgHandler old = qInstallMsgHandler(silentErrorsMsgHandler);
308 canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/error1.qml"));
309 qInstallMsgHandler(old);
310 QVERIFY(canvas->status() == QDeclarativeView::Error);
311 QVERIFY(canvas->errors().count() == 1);
316 T *tst_QDeclarativeView::findItem(QGraphicsObject *parent, const QString &objectName)
321 const QMetaObject &mo = T::staticMetaObject;
322 //qDebug() << parent->QGraphicsObject::children().count() << "children";
323 for (int i = 0; i < parent->childItems().count(); ++i) {
324 QDeclarativeItem *item = qobject_cast<QDeclarativeItem*>(parent->childItems().at(i));
327 //qDebug() << "try" << item;
328 if (mo.cast(item) && (objectName.isEmpty() || item->objectName() == objectName))
329 return static_cast<T*>(item);
330 item = findItem<T>(item, objectName);
332 return static_cast<T*>(item);
338 QTEST_MAIN(tst_QDeclarativeView)
340 #include "tst_qdeclarativeview.moc"