1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
7 ** This file is part of the test suite of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
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>
51 class tst_QQuickView : public QDeclarativeDataTest
58 void resizemodeitem();
63 tst_QQuickView::tst_QQuickView()
67 void tst_QQuickView::resizemodeitem()
70 window.setGeometry(0, 0, 400, 400);
72 QQuickView *canvas = new QQuickView(&window);
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());
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());
90 // size update from view
91 canvas->resize(QSize(80,100));
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());
98 canvas->setResizeMode(QQuickView::SizeViewToRootObject);
100 // size update from view disabled
101 canvas->resize(QSize(60,80));
102 QCOMPARE(item->width(), 80.0);
103 QCOMPARE(item->height(), 100.0);
105 QCOMPARE(canvas->size(), QSize(60, 80));
107 // size update from root object
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());
119 canvas = new QQuickView(&window);
121 canvas->setResizeMode(QQuickView::SizeViewToRootObject);
122 canvas->setSource(testFileUrl("resizemodeitem.qml"));
123 item = qobject_cast<QQuickItem*>(canvas->rootObject());
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());
135 // size update from root object
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());
144 // size update from root object disabled
145 canvas->setResizeMode(QQuickView::SizeRootObjectToView);
148 QCOMPARE(canvas->width(), 80);
149 QCOMPARE(canvas->height(), 100);
150 QCOMPARE(QSize(item->width(), item->height()), canvas->sizeHint());
152 // size update from view
153 canvas->resize(QSize(200,300));
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());
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());
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
188 static void silentErrorsMsgHandler(QtMsgType, const char *)
192 void tst_QQuickView::errors()
194 QQuickView *canvas = new QQuickView;
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);
205 QTEST_MAIN(tst_QQuickView)
207 #include "tst_qquickview.moc"