1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the test suite of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
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>
52 class tst_QQuickView : public QQmlDataTest
59 void resizemodeitem();
65 tst_QQuickView::tst_QQuickView()
69 void tst_QQuickView::resizemodeitem()
72 window.setGeometry(0, 0, 400, 400);
74 QQuickView *view = new QQuickView(&window);
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());
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());
92 // size update from view
93 view->resize(QSize(80,100));
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());
100 view->setResizeMode(QQuickView::SizeViewToRootObject);
102 // size update from view disabled
103 view->resize(QSize(60,80));
104 QCOMPARE(item->width(), 80.0);
105 QCOMPARE(item->height(), 100.0);
107 QCOMPARE(view->size(), QSize(60, 80));
109 // size update from root object
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());
121 view = new QQuickView(&window);
123 view->setResizeMode(QQuickView::SizeViewToRootObject);
124 view->setSource(testFileUrl("resizemodeitem.qml"));
125 item = qobject_cast<QQuickItem*>(view->rootObject());
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());
137 // size update from root object
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());
146 // size update from root object disabled
147 view->setResizeMode(QQuickView::SizeRootObjectToView);
150 QCOMPARE(view->width(), 80);
151 QCOMPARE(view->height(), 100);
152 QCOMPARE(QSize(item->width(), item->height()), view->sizeHint());
154 // size update from view
155 view->resize(QSize(200,300));
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());
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());
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
190 static void silentErrorsMsgHandler(QtMsgType, const char *)
194 void tst_QQuickView::errors()
196 QQuickView *view = new QQuickView;
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);
206 void tst_QQuickView::engine()
208 QQmlEngine *engine = new QQmlEngine;
209 QVERIFY(!engine->incubationController());
211 QQuickView *view = new QQuickView(engine, 0);
213 QVERIFY(engine->incubationController() == view->incubationController());
215 QQuickView *view2 = new QQuickView(engine, 0);
217 QVERIFY(engine->incubationController() == view->incubationController());
219 QVERIFY(!engine->incubationController());
221 engine->setIncubationController(view2->incubationController());
222 QVERIFY(engine->incubationController() == view2->incubationController());
224 QVERIFY(!engine->incubationController());
226 QQuickView *view3 = new QQuickView;
227 QQuickView *view4 = new QQuickView(view3->engine(), 0);
229 QVERIFY(view3->engine());
230 QVERIFY(view4->engine());
231 QCOMPARE(view3->engine(), view4->engine());
233 QVERIFY(!view4->engine());
234 QTest::ignoreMessage(QtWarningMsg, "QQuickView: invalid qml engine. ");
235 view4->setSource(QUrl());
237 QCOMPARE(view4->status(), QQuickView::Error);
238 QVERIFY(!view4->errors().isEmpty());
239 QCOMPARE(view4->errors().back().description(), QLatin1String("QQuickView: invalid qml engine."));
243 QTEST_MAIN(tst_QQuickView)
245 #include "tst_qquickview.moc"