Add missing autotests to declarative.pro.
[profile/ivi/qtdeclarative.git] / tests / auto / declarative / qsgview / tst_qsgview.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the test suite of the Qt Toolkit.
8 **
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.
17 **
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.
21 **
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.
29 **
30 ** Other Usage
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.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 #include <qtest.h>
42 #include <QtTest/QSignalSpy>
43 #include <QtDeclarative/qdeclarativecomponent.h>
44 #include <QtDeclarative/qdeclarativecontext.h>
45 #include <QtDeclarative/qsgview.h>
46 #include <QtDeclarative/qsgitem.h>
47 #include "../shared/util.h"
48 #include "../../../shared/util.h"
49 #include <QtGui/QWindow>
50 #include <QtCore/QDebug>
51
52 class tst_QSGView : public QObject
53 {
54     Q_OBJECT
55 public:
56     tst_QSGView();
57
58 private slots:
59     void resizemodeitem();
60     void errors();
61 };
62
63
64 tst_QSGView::tst_QSGView()
65 {
66 }
67
68 void tst_QSGView::resizemodeitem()
69 {
70     QWindow window;
71     window.setGeometry(0, 0, 400, 400);
72
73     QSGView *canvas = new QSGView(&window);
74     QVERIFY(canvas);
75     canvas->setResizeMode(QSGView::SizeRootObjectToView);
76     QCOMPARE(QSize(0,0), canvas->initialSize());
77     canvas->setSource(QUrl::fromLocalFile(TESTDATA("resizemodeitem.qml")));
78     QSGItem* item = qobject_cast<QSGItem*>(canvas->rootObject());
79     QVERIFY(item);
80     window.show();
81
82     canvas->show();
83
84     // initial size from root object
85     QCOMPARE(item->width(), 200.0);
86     QCOMPARE(item->height(), 200.0);
87     QCOMPARE(canvas->size(), QSize(200, 200));
88     QCOMPARE(canvas->size(), canvas->sizeHint());
89     QCOMPARE(canvas->size(), canvas->initialSize());
90
91     // size update from view
92     canvas->resize(QSize(80,100));
93     QTest::qWait(50);
94
95     QCOMPARE(item->width(), 80.0);
96     QCOMPARE(item->height(), 100.0);
97     QCOMPARE(canvas->size(), QSize(80, 100));
98     QCOMPARE(canvas->size(), canvas->sizeHint());
99
100     canvas->setResizeMode(QSGView::SizeViewToRootObject);
101
102     // size update from view disabled
103     canvas->resize(QSize(60,80));
104     QCOMPARE(item->width(), 80.0);
105     QCOMPARE(item->height(), 100.0);
106     QTest::qWait(50);
107     QCOMPARE(canvas->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(canvas->size(), QSize(250, 350));
115     QCOMPARE(canvas->size(), QSize(250, 350));
116     QCOMPARE(canvas->size(), canvas->sizeHint());
117
118     // reset canvas
119     window.hide();
120     delete canvas;
121     canvas = new QSGView(&window);
122     QVERIFY(canvas);
123     canvas->setResizeMode(QSGView::SizeViewToRootObject);
124     canvas->setSource(QUrl::fromLocalFile(TESTDATA("resizemodeitem.qml")));
125     item = qobject_cast<QSGItem*>(canvas->rootObject());
126     QVERIFY(item);
127     window.show();
128
129     canvas->show();
130
131     // initial size for root object
132     QCOMPARE(item->width(), 200.0);
133     QCOMPARE(item->height(), 200.0);
134     QCOMPARE(canvas->size(), canvas->sizeHint());
135     QCOMPARE(canvas->size(), canvas->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(canvas->size(), QSize(80, 100));
143     QCOMPARE(canvas->size(), QSize(80, 100));
144     QCOMPARE(canvas->size(), canvas->sizeHint());
145
146     // size update from root object disabled
147     canvas->setResizeMode(QSGView::SizeRootObjectToView);
148     item->setWidth(60);
149     item->setHeight(80);
150     QCOMPARE(canvas->width(), 80);
151     QCOMPARE(canvas->height(), 100);
152     QCOMPARE(QSize(item->width(), item->height()), canvas->sizeHint());
153
154     // size update from view
155     canvas->resize(QSize(200,300));
156     QTest::qWait(50);
157     QCOMPARE(item->width(), 200.0);
158     QCOMPARE(item->height(), 300.0);
159     QCOMPARE(canvas->size(), QSize(200, 300));
160     QCOMPARE(canvas->size(), canvas->sizeHint());
161
162     window.hide();
163     delete canvas;
164
165     // if we set a specific size for the view then it should keep that size
166     // for SizeRootObjectToView mode.
167     canvas = new QSGView(&window);
168     canvas->resize(300, 300);
169     canvas->setResizeMode(QSGView::SizeRootObjectToView);
170     QCOMPARE(QSize(0,0), canvas->initialSize());
171     canvas->setSource(QUrl::fromLocalFile(TESTDATA("resizemodeitem.qml")));
172     canvas->resize(300, 300);
173     item = qobject_cast<QSGItem*>(canvas->rootObject());
174     QVERIFY(item);
175     window.show();
176
177     canvas->show();
178     QTest::qWait(50);
179
180     // initial size from root object
181     QEXPECT_FAIL("", "QTBUG-22019", Abort);
182     QCOMPARE(item->width(), 300.0);
183     QCOMPARE(item->height(), 300.0);
184     QCOMPARE(canvas->size(), QSize(300, 300));
185     QCOMPARE(canvas->size(), canvas->sizeHint());
186     QCOMPARE(canvas->initialSize(), QSize(200, 200)); // initial object size
187
188     delete canvas;
189 }
190
191 static void silentErrorsMsgHandler(QtMsgType, const char *)
192 {
193 }
194
195 void tst_QSGView::errors()
196 {
197     QSGView *canvas = new QSGView;
198     QVERIFY(canvas);
199     QtMsgHandler old = qInstallMsgHandler(silentErrorsMsgHandler);
200     canvas->setSource(QUrl::fromLocalFile(TESTDATA("error1.qml")));
201     qInstallMsgHandler(old);
202     QVERIFY(canvas->status() == QSGView::Error);
203     QVERIFY(canvas->errors().count() == 1);
204     delete canvas;
205 }
206
207
208 QTEST_MAIN(tst_QSGView)
209
210 #include "tst_qsgview.moc"