Merge branch 'master' into refactor
[profile/ivi/qtdeclarative.git] / tests / auto / qtquick1 / qdeclarativeview / tst_qdeclarativeview.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 <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"
49
50 #ifdef Q_OS_SYMBIAN
51 // In Symbian OS test data is located in applications private dir
52 #define SRCDIR "."
53 #endif
54
55 class tst_QDeclarativeView : public QObject
56
57 {
58     Q_OBJECT
59 public:
60     tst_QDeclarativeView();
61
62 private slots:
63     void scene();
64     void resizemodedeclarativeitem();
65     void resizemodegraphicswidget();
66     void errors();
67
68 private:
69     template<typename T>
70     T *findItem(QGraphicsObject *parent, const QString &objectName);
71 };
72
73
74 tst_QDeclarativeView::tst_QDeclarativeView()
75 {
76 }
77
78 void tst_QDeclarativeView::scene()
79 {
80     // QTBUG-14771
81     QGraphicsScene scene;
82     scene.setItemIndexMethod(QGraphicsScene::NoIndex);
83     scene.setStickyFocus(true);
84
85     QDeclarativeView *view = new QDeclarativeView();
86     QVERIFY(view);
87     QVERIFY(view->scene());
88     view->setScene(&scene);
89     QCOMPARE(view->scene(), &scene);
90
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);
96
97     delete view;
98 }
99
100 void tst_QDeclarativeView::resizemodedeclarativeitem()
101 {
102     QWidget window;
103     QDeclarativeView *canvas = new QDeclarativeView(&window);
104     QVERIFY(canvas);
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);
111     window.show();
112
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);
120
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);
128
129     canvas->setResizeMode(QDeclarativeView::SizeViewToRootObject);
130
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);
137
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);
147
148     // reset canvas
149     window.hide();
150     delete canvas;
151     canvas = new QDeclarativeView(&window);
152     QVERIFY(canvas);
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);
158     window.show();
159
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);
166
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);
176
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);
185
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);
193
194     delete canvas;
195
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);
203     window.show();
204
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));
211     delete canvas;
212 }
213
214 void tst_QDeclarativeView::resizemodegraphicswidget()
215 {
216     QWidget window;
217     QDeclarativeView *canvas = new QDeclarativeView(&window);
218     QVERIFY(canvas);
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);
224     window.show();
225
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);
233
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);
240
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);
247
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);
254
255     // reset canvas
256     window.hide();
257     delete canvas;
258     canvas = new QDeclarativeView(&window);
259     QVERIFY(canvas);
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);
265     window.show();
266
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);
273
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);
280
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);
287
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);
294
295     window.show();
296     delete canvas;
297 }
298
299 static void silentErrorsMsgHandler(QtMsgType, const char *)
300 {
301 }
302
303 void tst_QDeclarativeView::errors()
304 {
305     QDeclarativeView *canvas = new QDeclarativeView;
306     QVERIFY(canvas);
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);
312     delete canvas;
313 }
314
315 template<typename T>
316 T *tst_QDeclarativeView::findItem(QGraphicsObject *parent, const QString &objectName)
317 {
318     if (!parent)
319         return 0;
320
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));
325         if(!item)
326             continue;
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);
331         if (item)
332             return static_cast<T*>(item);
333     }
334
335     return 0;
336 }
337
338 QTEST_MAIN(tst_QDeclarativeView)
339
340 #include "tst_qdeclarativeview.moc"