1
/****************************************************************************
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4
** Contact: http://www.qt-project.org/legal
6
** This file is part of the test suite of the Qt Toolkit.
8
** $QT_BEGIN_LICENSE:LGPL$
9
** Commercial License Usage
10
** Licensees holding valid commercial Qt licenses may use this file in
11
** accordance with the commercial license agreement provided with the
12
** Software or, alternatively, in accordance with the terms contained in
13
** a written agreement between you and Digia. For licensing terms and
14
** conditions see http://qt.digia.com/licensing. For further information
15
** use the contact form at http://qt.digia.com/contact-us.
17
** GNU Lesser General Public License Usage
18
** Alternatively, this file may be used under the terms of the GNU Lesser
19
** General Public License version 2.1 as published by the Free Software
20
** Foundation and appearing in the file LICENSE.LGPL included in the
21
** packaging of this file. Please review the following information to
22
** ensure the GNU Lesser General Public License version 2.1 requirements
23
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25
** In addition, as a special exception, Digia gives you certain additional
26
** rights. These rights are described in the Digia Qt LGPL Exception
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29
** GNU General Public License Usage
30
** Alternatively, this file may be used under the terms of the GNU
31
** General Public License version 3.0 as published by the Free Software
32
** Foundation and appearing in the file LICENSE.GPL included in the
33
** packaging of this file. Please review the following information to
34
** ensure the GNU General Public License version 3.0 requirements will be
35
** met: http://www.gnu.org/copyleft/gpl.html.
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 QMessageLogContext &, const QString &)
194
void tst_QQuickView::errors()
196
QQuickView *view = new QQuickView;
198
QQmlTestMessageHandler messageHandler;
199
view->setSource(testFileUrl("error1.qml"));
200
QVERIFY(view->status() == QQuickView::Error);
201
QVERIFY(view->errors().count() == 1);
205
void tst_QQuickView::engine()
207
QQmlEngine *engine = new QQmlEngine;
208
QVERIFY(!engine->incubationController());
210
QQuickView *view = new QQuickView(engine, 0);
212
QVERIFY(engine->incubationController() == view->incubationController());
214
QQuickView *view2 = new QQuickView(engine, 0);
216
QVERIFY(engine->incubationController() == view->incubationController());
218
QVERIFY(!engine->incubationController());
220
engine->setIncubationController(view2->incubationController());
221
QVERIFY(engine->incubationController() == view2->incubationController());
223
QVERIFY(!engine->incubationController());
225
QQuickView *view3 = new QQuickView;
226
QQuickView *view4 = new QQuickView(view3->engine(), 0);
228
QVERIFY(view3->engine());
229
QVERIFY(view4->engine());
230
QCOMPARE(view3->engine(), view4->engine());
232
QVERIFY(!view4->engine());
233
QTest::ignoreMessage(QtWarningMsg, "QQuickView: invalid qml engine. ");
234
view4->setSource(QUrl());
236
QCOMPARE(view4->status(), QQuickView::Error);
237
QVERIFY(!view4->errors().isEmpty());
238
QCOMPARE(view4->errors().back().description(), QLatin1String("QQuickView: invalid qml engine."));
242
QTEST_MAIN(tst_QQuickView)
244
#include "tst_qquickview.moc"