~ubuntu-branches/ubuntu/saucy/qtdeclarative-opensource-src/saucy

« back to all changes in this revision

Viewing changes to tests/auto/quick/qquickview/tst_qquickview.cpp

  • Committer: Package Import Robot
  • Author(s): Timo Jyrinki
  • Date: 2013-02-05 14:17:19 UTC
  • Revision ID: package-import@ubuntu.com-20130205141719-qqeyml8wslpyez52
Tags: upstream-5.0.1
ImportĀ upstreamĀ versionĀ 5.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
 
4
** Contact: http://www.qt-project.org/legal
 
5
**
 
6
** This file is part of the test suite of the Qt Toolkit.
 
7
**
 
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.
 
16
**
 
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.
 
24
**
 
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.
 
28
**
 
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.
 
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 QMessageLogContext &, const QString &)
 
191
{
 
192
}
 
193
 
 
194
void tst_QQuickView::errors()
 
195
{
 
196
    QQuickView *view = new QQuickView;
 
197
    QVERIFY(view);
 
198
    QQmlTestMessageHandler messageHandler;
 
199
    view->setSource(testFileUrl("error1.qml"));
 
200
    QVERIFY(view->status() == QQuickView::Error);
 
201
    QVERIFY(view->errors().count() == 1);
 
202
    delete view;
 
203
}
 
204
 
 
205
void tst_QQuickView::engine()
 
206
{
 
207
    QQmlEngine *engine = new QQmlEngine;
 
208
    QVERIFY(!engine->incubationController());
 
209
 
 
210
    QQuickView *view = new QQuickView(engine, 0);
 
211
    QVERIFY(view);
 
212
    QVERIFY(engine->incubationController() == view->incubationController());
 
213
 
 
214
    QQuickView *view2 = new QQuickView(engine, 0);
 
215
    QVERIFY(view);
 
216
    QVERIFY(engine->incubationController() == view->incubationController());
 
217
    delete view;
 
218
    QVERIFY(!engine->incubationController());
 
219
 
 
220
    engine->setIncubationController(view2->incubationController());
 
221
    QVERIFY(engine->incubationController() == view2->incubationController());
 
222
    delete view2;
 
223
    QVERIFY(!engine->incubationController());
 
224
 
 
225
    QQuickView *view3 = new QQuickView;
 
226
    QQuickView *view4 = new QQuickView(view3->engine(), 0);
 
227
 
 
228
    QVERIFY(view3->engine());
 
229
    QVERIFY(view4->engine());
 
230
    QCOMPARE(view3->engine(), view4->engine());
 
231
    delete view3;
 
232
    QVERIFY(!view4->engine());
 
233
    QTest::ignoreMessage(QtWarningMsg, "QQuickView: invalid qml engine. ");
 
234
    view4->setSource(QUrl());
 
235
 
 
236
    QCOMPARE(view4->status(), QQuickView::Error);
 
237
    QVERIFY(!view4->errors().isEmpty());
 
238
    QCOMPARE(view4->errors().back().description(), QLatin1String("QQuickView: invalid qml engine."));
 
239
    delete view4;
 
240
}
 
241
 
 
242
QTEST_MAIN(tst_QQuickView)
 
243
 
 
244
#include "tst_qquickview.moc"