QQuickCanvas renames
[profile/ivi/qtdeclarative.git] / tests / auto / quick / qquickview / tst_qquickview.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 <QtQml/qqmlcomponent.h>
44 #include <QtQml/qqmlcontext.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 #include <QtQml/qqmlengine.h>
51
52 class tst_QQuickView : public QQmlDataTest
53 {
54     Q_OBJECT
55 public:
56     tst_QQuickView();
57
58 private slots:
59     void resizemodeitem();
60     void errors();
61     void engine();
62 };
63
64
65 tst_QQuickView::tst_QQuickView()
66 {
67 }
68
69 void tst_QQuickView::resizemodeitem()
70 {
71     QWindow window;
72     window.setGeometry(0, 0, 400, 400);
73
74     QQuickView *view = new QQuickView(&window);
75     QVERIFY(view);
76     view->setResizeMode(QQuickView::SizeRootObjectToView);
77     QCOMPARE(QSize(0,0), view->initialSize());
78     view->setSource(testFileUrl("resizemodeitem.qml"));
79     QQuickItem* item = qobject_cast<QQuickItem*>(view->rootObject());
80     QVERIFY(item);
81     window.show();
82
83     view->show();
84
85     // initial size from root object
86     QCOMPARE(item->width(), 200.0);
87     QCOMPARE(item->height(), 200.0);
88     QCOMPARE(view->size(), QSize(200, 200));
89     QCOMPARE(view->size(), view->sizeHint());
90     QCOMPARE(view->size(), view->initialSize());
91
92     // size update from view
93     view->resize(QSize(80,100));
94
95     QTRY_COMPARE(item->width(), 80.0);
96     QCOMPARE(item->height(), 100.0);
97     QCOMPARE(view->size(), QSize(80, 100));
98     QCOMPARE(view->size(), view->sizeHint());
99
100     view->setResizeMode(QQuickView::SizeViewToRootObject);
101
102     // size update from view disabled
103     view->resize(QSize(60,80));
104     QCOMPARE(item->width(), 80.0);
105     QCOMPARE(item->height(), 100.0);
106     QTest::qWait(50);
107     QCOMPARE(view->size(), QSize(60, 80));
108
109     // size update from root object
110     item->setWidth(250);
111     item->setHeight(350);
112     QCOMPARE(item->width(), 250.0);
113     QCOMPARE(item->height(), 350.0);
114     QTRY_COMPARE(view->size(), QSize(250, 350));
115     QCOMPARE(view->size(), QSize(250, 350));
116     QCOMPARE(view->size(), view->sizeHint());
117
118     // reset window
119     window.hide();
120     delete view;
121     view = new QQuickView(&window);
122     QVERIFY(view);
123     view->setResizeMode(QQuickView::SizeViewToRootObject);
124     view->setSource(testFileUrl("resizemodeitem.qml"));
125     item = qobject_cast<QQuickItem*>(view->rootObject());
126     QVERIFY(item);
127     window.show();
128
129     view->show();
130
131     // initial size for root object
132     QCOMPARE(item->width(), 200.0);
133     QCOMPARE(item->height(), 200.0);
134     QCOMPARE(view->size(), view->sizeHint());
135     QCOMPARE(view->size(), view->initialSize());
136
137     // size update from root object
138     item->setWidth(80);
139     item->setHeight(100);
140     QCOMPARE(item->width(), 80.0);
141     QCOMPARE(item->height(), 100.0);
142     QTRY_COMPARE(view->size(), QSize(80, 100));
143     QCOMPARE(view->size(), QSize(80, 100));
144     QCOMPARE(view->size(), view->sizeHint());
145
146     // size update from root object disabled
147     view->setResizeMode(QQuickView::SizeRootObjectToView);
148     item->setWidth(60);
149     item->setHeight(80);
150     QCOMPARE(view->width(), 80);
151     QCOMPARE(view->height(), 100);
152     QCOMPARE(QSize(item->width(), item->height()), view->sizeHint());
153
154     // size update from view
155     view->resize(QSize(200,300));
156     QTest::qWait(50);
157     QCOMPARE(item->width(), 200.0);
158     QCOMPARE(item->height(), 300.0);
159     QCOMPARE(view->size(), QSize(200, 300));
160     QCOMPARE(view->size(), view->sizeHint());
161
162     window.hide();
163     delete view;
164
165     // if we set a specific size for the view then it should keep that size
166     // for SizeRootObjectToView mode.
167     view = new QQuickView(&window);
168     view->resize(300, 300);
169     view->setResizeMode(QQuickView::SizeRootObjectToView);
170     QCOMPARE(QSize(0,0), view->initialSize());
171     view->setSource(testFileUrl("resizemodeitem.qml"));
172     view->resize(300, 300);
173     item = qobject_cast<QQuickItem*>(view->rootObject());
174     QVERIFY(item);
175     window.show();
176
177     view->show();
178     QTest::qWait(50);
179
180     // initial size from root object
181     QCOMPARE(item->width(), 300.0);
182     QCOMPARE(item->height(), 300.0);
183     QCOMPARE(view->size(), QSize(300, 300));
184     QCOMPARE(view->size(), view->sizeHint());
185     QCOMPARE(view->initialSize(), QSize(200, 200)); // initial object size
186
187     delete view;
188 }
189
190 static void silentErrorsMsgHandler(QtMsgType, const char *)
191 {
192 }
193
194 void tst_QQuickView::errors()
195 {
196     QQuickView *view = new QQuickView;
197     QVERIFY(view);
198     QtMsgHandler old = qInstallMsgHandler(silentErrorsMsgHandler);
199     view->setSource(testFileUrl("error1.qml"));
200     qInstallMsgHandler(old);
201     QVERIFY(view->status() == QQuickView::Error);
202     QVERIFY(view->errors().count() == 1);
203     delete view;
204 }
205
206 void tst_QQuickView::engine()
207 {
208     QQmlEngine *engine = new QQmlEngine;
209     QVERIFY(!engine->incubationController());
210
211     QQuickView *view = new QQuickView(engine, 0);
212     QVERIFY(view);
213     QVERIFY(engine->incubationController() == view->incubationController());
214
215     QQuickView *view2 = new QQuickView(engine, 0);
216     QVERIFY(view);
217     QVERIFY(engine->incubationController() == view->incubationController());
218     delete view;
219     QVERIFY(!engine->incubationController());
220
221     engine->setIncubationController(view2->incubationController());
222     QVERIFY(engine->incubationController() == view2->incubationController());
223     delete view2;
224     QVERIFY(!engine->incubationController());
225
226     QQuickView *view3 = new QQuickView;
227     QQuickView *view4 = new QQuickView(view3->engine(), 0);
228
229     QVERIFY(view3->engine());
230     QVERIFY(view4->engine());
231     QCOMPARE(view3->engine(), view4->engine());
232     delete view3;
233     QVERIFY(!view4->engine());
234     QTest::ignoreMessage(QtWarningMsg, "QQuickView: invalid qml engine. ");
235     view4->setSource(QUrl());
236
237     QCOMPARE(view4->status(), QQuickView::Error);
238     QVERIFY(!view4->errors().isEmpty());
239     QCOMPARE(view4->errors().back().description(), QLatin1String("QQuickView: invalid qml engine."));
240     delete view4;
241 }
242
243 QTEST_MAIN(tst_QQuickView)
244
245 #include "tst_qquickview.moc"