0b575c67a1190418d2fbdd14f09e39d59bc16957
[profile/ivi/qtdeclarative.git] / tests / auto / qtquick2 / qquickview / tst_qquickview.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
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 <QtQuick/qquickview.h>
46 #include <QtQuick/qquickitem.h>
47 #include "../../shared/util.h"
48 #include <QtGui/QWindow>
49 #include <QtCore/QDebug>
50
51 class tst_QQuickView : public QDeclarativeDataTest
52 {
53     Q_OBJECT
54 public:
55     tst_QQuickView();
56
57 private slots:
58     void resizemodeitem();
59     void errors();
60 };
61
62
63 tst_QQuickView::tst_QQuickView()
64 {
65 }
66
67 void tst_QQuickView::resizemodeitem()
68 {
69     QWindow window;
70     window.setGeometry(0, 0, 400, 400);
71
72     QQuickView *canvas = new QQuickView(&window);
73     QVERIFY(canvas);
74     canvas->setResizeMode(QQuickView::SizeRootObjectToView);
75     QCOMPARE(QSize(0,0), canvas->initialSize());
76     canvas->setSource(testFileUrl("resizemodeitem.qml"));
77     QQuickItem* item = qobject_cast<QQuickItem*>(canvas->rootObject());
78     QVERIFY(item);
79     window.show();
80
81     canvas->show();
82
83     // initial size from root object
84     QCOMPARE(item->width(), 200.0);
85     QCOMPARE(item->height(), 200.0);
86     QCOMPARE(canvas->size(), QSize(200, 200));
87     QCOMPARE(canvas->size(), canvas->sizeHint());
88     QCOMPARE(canvas->size(), canvas->initialSize());
89
90     // size update from view
91     canvas->resize(QSize(80,100));
92
93     QTRY_COMPARE(item->width(), 80.0);
94     QCOMPARE(item->height(), 100.0);
95     QCOMPARE(canvas->size(), QSize(80, 100));
96     QCOMPARE(canvas->size(), canvas->sizeHint());
97
98     canvas->setResizeMode(QQuickView::SizeViewToRootObject);
99
100     // size update from view disabled
101     canvas->resize(QSize(60,80));
102     QCOMPARE(item->width(), 80.0);
103     QCOMPARE(item->height(), 100.0);
104     QTest::qWait(50);
105     QCOMPARE(canvas->size(), QSize(60, 80));
106
107     // size update from root object
108     item->setWidth(250);
109     item->setHeight(350);
110     QCOMPARE(item->width(), 250.0);
111     QCOMPARE(item->height(), 350.0);
112     QTRY_COMPARE(canvas->size(), QSize(250, 350));
113     QCOMPARE(canvas->size(), QSize(250, 350));
114     QCOMPARE(canvas->size(), canvas->sizeHint());
115
116     // reset canvas
117     window.hide();
118     delete canvas;
119     canvas = new QQuickView(&window);
120     QVERIFY(canvas);
121     canvas->setResizeMode(QQuickView::SizeViewToRootObject);
122     canvas->setSource(testFileUrl("resizemodeitem.qml"));
123     item = qobject_cast<QQuickItem*>(canvas->rootObject());
124     QVERIFY(item);
125     window.show();
126
127     canvas->show();
128
129     // initial size for root object
130     QCOMPARE(item->width(), 200.0);
131     QCOMPARE(item->height(), 200.0);
132     QCOMPARE(canvas->size(), canvas->sizeHint());
133     QCOMPARE(canvas->size(), canvas->initialSize());
134
135     // size update from root object
136     item->setWidth(80);
137     item->setHeight(100);
138     QCOMPARE(item->width(), 80.0);
139     QCOMPARE(item->height(), 100.0);
140     QTRY_COMPARE(canvas->size(), QSize(80, 100));
141     QCOMPARE(canvas->size(), QSize(80, 100));
142     QCOMPARE(canvas->size(), canvas->sizeHint());
143
144     // size update from root object disabled
145     canvas->setResizeMode(QQuickView::SizeRootObjectToView);
146     item->setWidth(60);
147     item->setHeight(80);
148     QCOMPARE(canvas->width(), 80);
149     QCOMPARE(canvas->height(), 100);
150     QCOMPARE(QSize(item->width(), item->height()), canvas->sizeHint());
151
152     // size update from view
153     canvas->resize(QSize(200,300));
154     QTest::qWait(50);
155     QCOMPARE(item->width(), 200.0);
156     QCOMPARE(item->height(), 300.0);
157     QCOMPARE(canvas->size(), QSize(200, 300));
158     QCOMPARE(canvas->size(), canvas->sizeHint());
159
160     window.hide();
161     delete canvas;
162
163     // if we set a specific size for the view then it should keep that size
164     // for SizeRootObjectToView mode.
165     canvas = new QQuickView(&window);
166     canvas->resize(300, 300);
167     canvas->setResizeMode(QQuickView::SizeRootObjectToView);
168     QCOMPARE(QSize(0,0), canvas->initialSize());
169     canvas->setSource(testFileUrl("resizemodeitem.qml"));
170     canvas->resize(300, 300);
171     item = qobject_cast<QQuickItem*>(canvas->rootObject());
172     QVERIFY(item);
173     window.show();
174
175     canvas->show();
176     QTest::qWait(50);
177
178     // initial size from root object
179     QCOMPARE(item->width(), 300.0);
180     QCOMPARE(item->height(), 300.0);
181     QCOMPARE(canvas->size(), QSize(300, 300));
182     QCOMPARE(canvas->size(), canvas->sizeHint());
183     QCOMPARE(canvas->initialSize(), QSize(200, 200)); // initial object size
184
185     delete canvas;
186 }
187
188 static void silentErrorsMsgHandler(QtMsgType, const char *)
189 {
190 }
191
192 void tst_QQuickView::errors()
193 {
194     QQuickView *canvas = new QQuickView;
195     QVERIFY(canvas);
196     QtMsgHandler old = qInstallMsgHandler(silentErrorsMsgHandler);
197     canvas->setSource(testFileUrl("error1.qml"));
198     qInstallMsgHandler(old);
199     QVERIFY(canvas->status() == QQuickView::Error);
200     QVERIFY(canvas->errors().count() == 1);
201     delete canvas;
202 }
203
204
205 QTEST_MAIN(tst_QQuickView)
206
207 #include "tst_qquickview.moc"