Initial import from the monolithic Qt.
[profile/ivi/qtdeclarative.git] / tests / auto / declarative / 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 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file.  Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23 **
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **
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 <QtDeclarative/qdeclarativeview.h>
46 #include <QtDeclarative/qdeclarativeitem.h>
47 #include <QtGui/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
98 void tst_QDeclarativeView::resizemodedeclarativeitem()
99 {
100     QWidget window;
101     QDeclarativeView *canvas = new QDeclarativeView(&window);
102     QVERIFY(canvas);
103     QSignalSpy sceneResizedSpy(canvas, SIGNAL(sceneResized(QSize)));
104     canvas->setResizeMode(QDeclarativeView::SizeRootObjectToView);
105     QCOMPARE(QSize(0,0), canvas->initialSize());
106     canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/resizemodedeclarativeitem.qml"));
107     QDeclarativeItem* declarativeItem = qobject_cast<QDeclarativeItem*>(canvas->rootObject());
108     QVERIFY(declarativeItem);
109     window.show();
110
111     // initial size from root object
112     QCOMPARE(declarativeItem->width(), 200.0);
113     QCOMPARE(declarativeItem->height(), 200.0);
114     QCOMPARE(canvas->size(), QSize(200, 200));
115     QCOMPARE(canvas->size(), canvas->sizeHint());
116     QCOMPARE(canvas->size(), canvas->initialSize());
117     QCOMPARE(sceneResizedSpy.count(), 1);
118
119     // size update from view
120     canvas->resize(QSize(80,100));
121     QCOMPARE(declarativeItem->width(), 80.0);
122     QCOMPARE(declarativeItem->height(), 100.0);
123     QCOMPARE(canvas->size(), QSize(80, 100));
124     QCOMPARE(canvas->size(), canvas->sizeHint());
125     QCOMPARE(sceneResizedSpy.count(), 2);
126
127     canvas->setResizeMode(QDeclarativeView::SizeViewToRootObject);
128
129     // size update from view disabled
130     canvas->resize(QSize(60,80));
131     QCOMPARE(declarativeItem->width(), 80.0);
132     QCOMPARE(declarativeItem->height(), 100.0);
133     QCOMPARE(canvas->size(), QSize(60, 80));
134     QCOMPARE(sceneResizedSpy.count(), 3);
135
136     // size update from root object
137     declarativeItem->setWidth(250);
138     declarativeItem->setHeight(350);
139     QCOMPARE(declarativeItem->width(), 250.0);
140     QCOMPARE(declarativeItem->height(), 350.0);
141     QTRY_COMPARE(canvas->size(), QSize(250, 350));
142     QCOMPARE(canvas->size(), QSize(250, 350));
143     QCOMPARE(canvas->size(), canvas->sizeHint());
144     QCOMPARE(sceneResizedSpy.count(), 4);
145
146     // reset canvas
147     window.hide();
148     delete canvas;
149     canvas = new QDeclarativeView(&window);
150     QVERIFY(canvas);
151     QSignalSpy sceneResizedSpy2(canvas, SIGNAL(sceneResized(QSize)));
152     canvas->setResizeMode(QDeclarativeView::SizeViewToRootObject);
153     canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/resizemodedeclarativeitem.qml"));
154     declarativeItem = qobject_cast<QDeclarativeItem*>(canvas->rootObject());
155     QVERIFY(declarativeItem);
156     window.show();
157
158     // initial size for root object
159     QCOMPARE(declarativeItem->width(), 200.0);
160     QCOMPARE(declarativeItem->height(), 200.0);
161     QCOMPARE(canvas->size(), canvas->sizeHint());
162     QCOMPARE(canvas->size(), canvas->initialSize());
163     QCOMPARE(sceneResizedSpy2.count(), 1);
164
165     // size update from root object
166     declarativeItem->setWidth(80);
167     declarativeItem->setHeight(100);
168     QCOMPARE(declarativeItem->width(), 80.0);
169     QCOMPARE(declarativeItem->height(), 100.0);
170     QTRY_COMPARE(canvas->size(), QSize(80, 100));
171     QCOMPARE(canvas->size(), QSize(80, 100));
172     QCOMPARE(canvas->size(), canvas->sizeHint());
173     QCOMPARE(sceneResizedSpy2.count(), 2);
174
175     // size update from root object disabled
176     canvas->setResizeMode(QDeclarativeView::SizeRootObjectToView);
177     declarativeItem->setWidth(60);
178     declarativeItem->setHeight(80);
179     QCOMPARE(canvas->width(), 80);
180     QCOMPARE(canvas->height(), 100);
181     QCOMPARE(QSize(declarativeItem->width(), declarativeItem->height()), canvas->sizeHint());
182     QCOMPARE(sceneResizedSpy2.count(), 2);
183
184     // size update from view
185     canvas->resize(QSize(200,300));
186     QCOMPARE(declarativeItem->width(), 200.0);
187     QCOMPARE(declarativeItem->height(), 300.0);
188     QCOMPARE(canvas->size(), QSize(200, 300));
189     QCOMPARE(canvas->size(), canvas->sizeHint());
190     QCOMPARE(sceneResizedSpy2.count(), 3);
191
192     delete canvas;
193 }
194
195 void tst_QDeclarativeView::resizemodegraphicswidget()
196 {
197     QWidget window;
198     QDeclarativeView *canvas = new QDeclarativeView(&window);
199     QVERIFY(canvas);
200     QSignalSpy sceneResizedSpy(canvas, SIGNAL(sceneResized(QSize)));
201     canvas->setResizeMode(QDeclarativeView::SizeRootObjectToView);
202     canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/resizemodegraphicswidget.qml"));
203     QGraphicsWidget* graphicsWidget = qobject_cast<QGraphicsWidget*>(canvas->rootObject());
204     QVERIFY(graphicsWidget);
205     window.show();
206
207     // initial size from root object
208     QCOMPARE(graphicsWidget->size(), QSizeF(200.0, 200.0));
209     QCOMPARE(canvas->size(), QSize(200, 200));
210     QCOMPARE(canvas->size(), QSize(200, 200));
211     QCOMPARE(canvas->size(), canvas->sizeHint());
212     QCOMPARE(canvas->size(), canvas->initialSize());
213     QCOMPARE(sceneResizedSpy.count(), 1);
214
215     // size update from view
216     canvas->resize(QSize(80,100));
217     QCOMPARE(graphicsWidget->size(), QSizeF(80.0,100.0));
218     QCOMPARE(canvas->size(), QSize(80,100));
219     QCOMPARE(canvas->size(), canvas->sizeHint());
220     QCOMPARE(sceneResizedSpy.count(), 2);
221
222     // size update from view disabled
223     canvas->setResizeMode(QDeclarativeView::SizeViewToRootObject);
224     canvas->resize(QSize(60,80));
225     QCOMPARE(graphicsWidget->size(), QSizeF(80.0,100.0));
226     QCOMPARE(canvas->size(), QSize(60, 80));
227     QCOMPARE(sceneResizedSpy.count(), 3);
228
229     // size update from root object
230     graphicsWidget->resize(QSizeF(250.0, 350.0));
231     QCOMPARE(graphicsWidget->size(), QSizeF(250.0,350.0));
232     QCOMPARE(canvas->size(), QSize(250, 350));
233     QCOMPARE(canvas->size(), canvas->sizeHint());
234     QCOMPARE(sceneResizedSpy.count(), 4);
235
236     // reset canvas
237     window.hide();
238     delete canvas;
239     canvas = new QDeclarativeView(&window);
240     QVERIFY(canvas);
241     QSignalSpy sceneResizedSpy2(canvas, SIGNAL(sceneResized(QSize)));
242     canvas->setResizeMode(QDeclarativeView::SizeViewToRootObject);
243     canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/resizemodegraphicswidget.qml"));
244     graphicsWidget = qobject_cast<QGraphicsWidget*>(canvas->rootObject());
245     QVERIFY(graphicsWidget);
246     window.show();
247
248     // initial size from root object
249     QCOMPARE(graphicsWidget->size(), QSizeF(200.0, 200.0));
250     QCOMPARE(canvas->size(), QSize(200, 200));
251     QCOMPARE(canvas->size(), canvas->sizeHint());
252     QCOMPARE(canvas->size(), canvas->initialSize());
253     QCOMPARE(sceneResizedSpy2.count(), 1);
254
255     // size update from root object
256     graphicsWidget->resize(QSizeF(80, 100));
257     QCOMPARE(graphicsWidget->size(), QSizeF(80.0, 100.0));
258     QCOMPARE(canvas->size(), QSize(80, 100));
259     QCOMPARE(canvas->size(), canvas->sizeHint());
260     QCOMPARE(sceneResizedSpy2.count(), 2);
261
262     // size update from root object disabled
263     canvas->setResizeMode(QDeclarativeView::SizeRootObjectToView);
264     graphicsWidget->resize(QSizeF(60,80));
265     QCOMPARE(canvas->size(), QSize(80,100));
266     QCOMPARE(QSize(graphicsWidget->size().width(), graphicsWidget->size().height()), canvas->sizeHint());
267     QCOMPARE(sceneResizedSpy2.count(), 2);
268
269     // size update from view
270     canvas->resize(QSize(200,300));
271     QCOMPARE(graphicsWidget->size(), QSizeF(200.0, 300.0));
272     QCOMPARE(canvas->size(), QSize(200, 300));
273     QCOMPARE(canvas->size(), canvas->sizeHint());
274     QCOMPARE(sceneResizedSpy2.count(), 3);
275
276     window.show();
277     delete canvas;
278 }
279
280 static void silentErrorsMsgHandler(QtMsgType, const char *)
281 {
282 }
283
284 void tst_QDeclarativeView::errors()
285 {
286     QDeclarativeView *canvas = new QDeclarativeView;
287     QVERIFY(canvas);
288     QtMsgHandler old = qInstallMsgHandler(silentErrorsMsgHandler);
289     canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/error1.qml"));
290     qInstallMsgHandler(old);
291     QVERIFY(canvas->status() == QDeclarativeView::Error);
292     QVERIFY(canvas->errors().count() == 1);
293     delete canvas;
294 }
295
296 template<typename T>
297 T *tst_QDeclarativeView::findItem(QGraphicsObject *parent, const QString &objectName)
298 {
299     if (!parent)
300         return 0;
301
302     const QMetaObject &mo = T::staticMetaObject;
303     //qDebug() << parent->QGraphicsObject::children().count() << "children";
304     for (int i = 0; i < parent->childItems().count(); ++i) {
305         QDeclarativeItem *item = qobject_cast<QDeclarativeItem*>(parent->childItems().at(i));
306         if(!item)
307             continue;
308         //qDebug() << "try" << item;
309         if (mo.cast(item) && (objectName.isEmpty() || item->objectName() == objectName))
310             return static_cast<T*>(item);
311         item = findItem<T>(item, objectName);
312         if (item)
313             return static_cast<T*>(item);
314     }
315
316     return 0;
317 }
318
319 QTEST_MAIN(tst_QDeclarativeView)
320
321 #include "tst_qdeclarativeview.moc"