Remove "All rights reserved" line from license headers.
[profile/ivi/qtdeclarative.git] / tests / auto / qtquick1 / qdeclarativeview / tst_qdeclarativeview.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
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
49 class tst_QDeclarativeView : public QObject
50 {
51     Q_OBJECT
52 public:
53     tst_QDeclarativeView();
54
55 private slots:
56     void scene();
57     void resizemodedeclarativeitem();
58     void resizemodegraphicswidget();
59     void errors();
60
61 private:
62     template<typename T>
63     T *findItem(QGraphicsObject *parent, const QString &objectName);
64 };
65
66
67 tst_QDeclarativeView::tst_QDeclarativeView()
68 {
69 }
70
71 void tst_QDeclarativeView::scene()
72 {
73     // QTBUG-14771
74     QGraphicsScene scene;
75     scene.setItemIndexMethod(QGraphicsScene::NoIndex);
76     scene.setStickyFocus(true);
77
78     QDeclarativeView *view = new QDeclarativeView();
79     QVERIFY(view);
80     QVERIFY(view->scene());
81     view->setScene(&scene);
82     QCOMPARE(view->scene(), &scene);
83
84     view->setSource(QUrl::fromLocalFile(SRCDIR "/data/resizemodedeclarativeitem.qml"));
85     QDeclarativeItem* declarativeItem = qobject_cast<QDeclarativeItem*>(view->rootObject());
86     QVERIFY(declarativeItem);
87     QVERIFY(scene.items().count() > 0);
88     QCOMPARE(scene.items().at(0), declarativeItem);
89
90     delete view;
91 }
92
93 void tst_QDeclarativeView::resizemodedeclarativeitem()
94 {
95     QWidget window;
96     QDeclarativeView *canvas = new QDeclarativeView(&window);
97     QVERIFY(canvas);
98     QSignalSpy sceneResizedSpy(canvas, SIGNAL(sceneResized(QSize)));
99     canvas->setResizeMode(QDeclarativeView::SizeRootObjectToView);
100     QCOMPARE(QSize(0,0), canvas->initialSize());
101     canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/resizemodedeclarativeitem.qml"));
102     QDeclarativeItem* declarativeItem = qobject_cast<QDeclarativeItem*>(canvas->rootObject());
103     QVERIFY(declarativeItem);
104     window.show();
105
106     // initial size from root object
107     QCOMPARE(declarativeItem->width(), 200.0);
108     QCOMPARE(declarativeItem->height(), 200.0);
109     QCOMPARE(canvas->size(), QSize(200, 200));
110     QCOMPARE(canvas->size(), canvas->sizeHint());
111     QCOMPARE(canvas->size(), canvas->initialSize());
112     QCOMPARE(sceneResizedSpy.count(), 1);
113
114     // size update from view
115     canvas->resize(QSize(80,100));
116     QCOMPARE(declarativeItem->width(), 80.0);
117     QCOMPARE(declarativeItem->height(), 100.0);
118     QCOMPARE(canvas->size(), QSize(80, 100));
119     QCOMPARE(canvas->size(), canvas->sizeHint());
120     QCOMPARE(sceneResizedSpy.count(), 2);
121
122     canvas->setResizeMode(QDeclarativeView::SizeViewToRootObject);
123
124     // size update from view disabled
125     canvas->resize(QSize(60,80));
126     QCOMPARE(declarativeItem->width(), 80.0);
127     QCOMPARE(declarativeItem->height(), 100.0);
128     QCOMPARE(canvas->size(), QSize(60, 80));
129     QCOMPARE(sceneResizedSpy.count(), 3);
130
131     // size update from root object
132     declarativeItem->setWidth(250);
133     declarativeItem->setHeight(350);
134     QCOMPARE(declarativeItem->width(), 250.0);
135     QCOMPARE(declarativeItem->height(), 350.0);
136     QTRY_COMPARE(canvas->size(), QSize(250, 350));
137     QCOMPARE(canvas->size(), QSize(250, 350));
138     QCOMPARE(canvas->size(), canvas->sizeHint());
139     QCOMPARE(sceneResizedSpy.count(), 4);
140
141     // reset canvas
142     window.hide();
143     delete canvas;
144     canvas = new QDeclarativeView(&window);
145     QVERIFY(canvas);
146     QSignalSpy sceneResizedSpy2(canvas, SIGNAL(sceneResized(QSize)));
147     canvas->setResizeMode(QDeclarativeView::SizeViewToRootObject);
148     canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/resizemodedeclarativeitem.qml"));
149     declarativeItem = qobject_cast<QDeclarativeItem*>(canvas->rootObject());
150     QVERIFY(declarativeItem);
151     window.show();
152
153     // initial size for root object
154     QCOMPARE(declarativeItem->width(), 200.0);
155     QCOMPARE(declarativeItem->height(), 200.0);
156     QCOMPARE(canvas->size(), canvas->sizeHint());
157     QCOMPARE(canvas->size(), canvas->initialSize());
158     QCOMPARE(sceneResizedSpy2.count(), 1);
159
160     // size update from root object
161     declarativeItem->setWidth(80);
162     declarativeItem->setHeight(100);
163     QCOMPARE(declarativeItem->width(), 80.0);
164     QCOMPARE(declarativeItem->height(), 100.0);
165     QTRY_COMPARE(canvas->size(), QSize(80, 100));
166     QCOMPARE(canvas->size(), QSize(80, 100));
167     QCOMPARE(canvas->size(), canvas->sizeHint());
168     QCOMPARE(sceneResizedSpy2.count(), 2);
169
170     // size update from root object disabled
171     canvas->setResizeMode(QDeclarativeView::SizeRootObjectToView);
172     declarativeItem->setWidth(60);
173     declarativeItem->setHeight(80);
174     QCOMPARE(canvas->width(), 80);
175     QCOMPARE(canvas->height(), 100);
176     QCOMPARE(QSize(declarativeItem->width(), declarativeItem->height()), canvas->sizeHint());
177     QCOMPARE(sceneResizedSpy2.count(), 2);
178
179     // size update from view
180     canvas->resize(QSize(200,300));
181     QCOMPARE(declarativeItem->width(), 200.0);
182     QCOMPARE(declarativeItem->height(), 300.0);
183     QCOMPARE(canvas->size(), QSize(200, 300));
184     QCOMPARE(canvas->size(), canvas->sizeHint());
185     QCOMPARE(sceneResizedSpy2.count(), 3);
186
187     delete canvas;
188
189     canvas = new QDeclarativeView(&window);
190     canvas->resize(300, 300);
191     canvas->setResizeMode(QDeclarativeView::SizeRootObjectToView);
192     QCOMPARE(QSize(0,0), canvas->initialSize());
193     canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/resizemodedeclarativeitem.qml"));
194     declarativeItem = qobject_cast<QDeclarativeItem*>(canvas->rootObject());
195     QVERIFY(declarativeItem);
196     window.show();
197
198     // initial size from root object
199     QCOMPARE(declarativeItem->width(), 300.0);
200     QCOMPARE(declarativeItem->height(), 300.0);
201     QCOMPARE(canvas->size(), QSize(300, 300));
202     QCOMPARE(canvas->size(), canvas->sizeHint());
203     QCOMPARE(canvas->initialSize(), QSize(200, 200));
204     delete canvas;
205 }
206
207 void tst_QDeclarativeView::resizemodegraphicswidget()
208 {
209     QWidget window;
210     QDeclarativeView *canvas = new QDeclarativeView(&window);
211     QVERIFY(canvas);
212     QSignalSpy sceneResizedSpy(canvas, SIGNAL(sceneResized(QSize)));
213     canvas->setResizeMode(QDeclarativeView::SizeRootObjectToView);
214     canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/resizemodegraphicswidget.qml"));
215     QGraphicsWidget* graphicsWidget = qobject_cast<QGraphicsWidget*>(canvas->rootObject());
216     QVERIFY(graphicsWidget);
217     window.show();
218
219     // initial size from root object
220     QCOMPARE(graphicsWidget->size(), QSizeF(200.0, 200.0));
221     QCOMPARE(canvas->size(), QSize(200, 200));
222     QCOMPARE(canvas->size(), QSize(200, 200));
223     QCOMPARE(canvas->size(), canvas->sizeHint());
224     QCOMPARE(canvas->size(), canvas->initialSize());
225     QCOMPARE(sceneResizedSpy.count(), 1);
226
227     // size update from view
228     canvas->resize(QSize(80,100));
229     QCOMPARE(graphicsWidget->size(), QSizeF(80.0,100.0));
230     QCOMPARE(canvas->size(), QSize(80,100));
231     QCOMPARE(canvas->size(), canvas->sizeHint());
232     QCOMPARE(sceneResizedSpy.count(), 2);
233
234     // size update from view disabled
235     canvas->setResizeMode(QDeclarativeView::SizeViewToRootObject);
236     canvas->resize(QSize(60,80));
237     QCOMPARE(graphicsWidget->size(), QSizeF(80.0,100.0));
238     QCOMPARE(canvas->size(), QSize(60, 80));
239     QCOMPARE(sceneResizedSpy.count(), 3);
240
241     // size update from root object
242     graphicsWidget->resize(QSizeF(250.0, 350.0));
243     QCOMPARE(graphicsWidget->size(), QSizeF(250.0,350.0));
244     QCOMPARE(canvas->size(), QSize(250, 350));
245     QCOMPARE(canvas->size(), canvas->sizeHint());
246     QCOMPARE(sceneResizedSpy.count(), 4);
247
248     // reset canvas
249     window.hide();
250     delete canvas;
251     canvas = new QDeclarativeView(&window);
252     QVERIFY(canvas);
253     QSignalSpy sceneResizedSpy2(canvas, SIGNAL(sceneResized(QSize)));
254     canvas->setResizeMode(QDeclarativeView::SizeViewToRootObject);
255     canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/resizemodegraphicswidget.qml"));
256     graphicsWidget = qobject_cast<QGraphicsWidget*>(canvas->rootObject());
257     QVERIFY(graphicsWidget);
258     window.show();
259
260     // initial size from root object
261     QCOMPARE(graphicsWidget->size(), QSizeF(200.0, 200.0));
262     QCOMPARE(canvas->size(), QSize(200, 200));
263     QCOMPARE(canvas->size(), canvas->sizeHint());
264     QCOMPARE(canvas->size(), canvas->initialSize());
265     QCOMPARE(sceneResizedSpy2.count(), 1);
266
267     // size update from root object
268     graphicsWidget->resize(QSizeF(80, 100));
269     QCOMPARE(graphicsWidget->size(), QSizeF(80.0, 100.0));
270     QCOMPARE(canvas->size(), QSize(80, 100));
271     QCOMPARE(canvas->size(), canvas->sizeHint());
272     QCOMPARE(sceneResizedSpy2.count(), 2);
273
274     // size update from root object disabled
275     canvas->setResizeMode(QDeclarativeView::SizeRootObjectToView);
276     graphicsWidget->resize(QSizeF(60,80));
277     QCOMPARE(canvas->size(), QSize(80,100));
278     QCOMPARE(QSize(graphicsWidget->size().width(), graphicsWidget->size().height()), canvas->sizeHint());
279     QCOMPARE(sceneResizedSpy2.count(), 2);
280
281     // size update from view
282     canvas->resize(QSize(200,300));
283     QCOMPARE(graphicsWidget->size(), QSizeF(200.0, 300.0));
284     QCOMPARE(canvas->size(), QSize(200, 300));
285     QCOMPARE(canvas->size(), canvas->sizeHint());
286     QCOMPARE(sceneResizedSpy2.count(), 3);
287
288     window.show();
289     delete canvas;
290 }
291
292 static void silentErrorsMsgHandler(QtMsgType, const char *)
293 {
294 }
295
296 void tst_QDeclarativeView::errors()
297 {
298     QDeclarativeView *canvas = new QDeclarativeView;
299     QVERIFY(canvas);
300     QtMsgHandler old = qInstallMsgHandler(silentErrorsMsgHandler);
301     canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/error1.qml"));
302     qInstallMsgHandler(old);
303     QVERIFY(canvas->status() == QDeclarativeView::Error);
304     QVERIFY(canvas->errors().count() == 1);
305     delete canvas;
306 }
307
308 template<typename T>
309 T *tst_QDeclarativeView::findItem(QGraphicsObject *parent, const QString &objectName)
310 {
311     if (!parent)
312         return 0;
313
314     const QMetaObject &mo = T::staticMetaObject;
315     //qDebug() << parent->QGraphicsObject::children().count() << "children";
316     for (int i = 0; i < parent->childItems().count(); ++i) {
317         QDeclarativeItem *item = qobject_cast<QDeclarativeItem*>(parent->childItems().at(i));
318         if(!item)
319             continue;
320         //qDebug() << "try" << item;
321         if (mo.cast(item) && (objectName.isEmpty() || item->objectName() == objectName))
322             return static_cast<T*>(item);
323         item = findItem<T>(item, objectName);
324         if (item)
325             return static_cast<T*>(item);
326     }
327
328     return 0;
329 }
330
331 QTEST_MAIN(tst_QDeclarativeView)
332
333 #include "tst_qdeclarativeview.moc"