c4bd7c12d8ca1187cee306cc4e2d9c7b715bc37a
[profile/ivi/qtdeclarative.git] / tests / auto / qtquick2 / qquickitemlayer / tst_qquickitemlayer.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
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
42 #include <qtest.h>
43
44 #include <QtQuick/qquickitem.h>
45 #include <QtQuick/qquickview.h>
46
47 #include "../../shared/util.h"
48
49 class tst_QQuickItemLayer: public QDeclarativeDataTest
50 {
51     Q_OBJECT
52 public:
53     tst_QQuickItemLayer();
54
55     QImage runTest(const QString &url)
56     {
57         QQuickView view;
58         view.setSource(QUrl(url));
59
60         view.show();
61         QTest::qWaitForWindowShown(&view);
62
63         return view.grabFrameBuffer();
64     }
65
66 private slots:
67     void layerEnabled();
68     void layerSmooth();
69     void layerMipmap();
70     void layerEffect();
71
72     void layerVisibility_data();
73     void layerVisibility();
74
75     void layerSourceRect();
76
77
78     void layerZOrder_data();
79     void layerZOrder();
80
81     void layerIsTextureProvider();
82 };
83
84 tst_QQuickItemLayer::tst_QQuickItemLayer()
85 {
86 }
87
88
89
90 // The test draws a red and a blue box next to each other and tests that the
91 // output is still red and blue on the left and right and a combination of
92 // the two in the middle.
93
94 void tst_QQuickItemLayer::layerSmooth()
95 {
96     QImage fb = runTest(testFile("Smooth.qml"));
97     QCOMPARE(fb.pixel(0, 0), qRgb(0xff, 0, 0));
98     QCOMPARE(fb.pixel(fb.width() - 1, 0), qRgb(0, 0, 0xff));
99
100     uint pixel = fb.pixel(fb.width() / 2, 0);
101     QVERIFY(qRed(pixel) > 0);
102     QVERIFY(qBlue(pixel) > 0);
103 }
104
105
106
107 // The test draws a gradient at a small size into a layer and scales the
108 // layer. If the layer is enabled there should be very visible bands in
109 // the gradient.
110
111 void tst_QQuickItemLayer::layerEnabled()
112 {
113     QImage fb = runTest(testFile("Enabled.qml"));
114     // Verify the banding
115     QCOMPARE(fb.pixel(0, 0), fb.pixel(0, 1));
116     // Verify the gradient
117     QVERIFY(fb.pixel(0, 0) != fb.pixel(0, fb.height() - 1));
118 }
119
120
121
122 // The test draws a one pixel wide line and scales it down by more than a a factor 2
123 // If mipmpping works, the pixels should be gray, not white or black
124
125 void tst_QQuickItemLayer::layerMipmap()
126 {
127     QImage fb = runTest(testFile("Mipmap.qml"));
128     QVERIFY(fb.pixel(0, 0) != 0xff000000);
129     QVERIFY(fb.pixel(0, 0) != 0xffffffff);
130 }
131
132
133
134 // The test implements an rgb swapping effect sourced from a blue rectangle. The
135 // resulting pixel should be red
136
137 void tst_QQuickItemLayer::layerEffect()
138 {
139     QImage fb = runTest(testFile("Effect.qml"));
140     QCOMPARE(fb.pixel(0, 0), qRgb(0xff, 0, 0));
141     QCOMPARE(fb.pixel(fb.width() - 1, 0), qRgb(0, 0xff, 0));
142 }
143
144
145
146 // The test draws a rectangle and verifies that there is padding on each side
147 // as the source rect spans outside the item. The padding is verified using
148 // a shader that pads transparent to blue. Everything else is red.
149 void tst_QQuickItemLayer::layerSourceRect()
150 {
151     QImage fb = runTest(testFile("SourceRect.qml"));
152
153     // Check that the edges are converted to blue
154     QCOMPARE(fb.pixel(0, 0), qRgb(0, 0, 0xff));
155     QCOMPARE(fb.pixel(fb.width() - 1, 0), qRgb(0, 0, 0xff));
156     QCOMPARE(fb.pixel(0, fb.height() - 1), qRgb(0, 0, 0xff));
157     QCOMPARE(fb.pixel(fb.width() - 1, fb.height() - 1), qRgb(0, 0, 0xff));
158
159     // The center pixel should be red
160     QCOMPARE(fb.pixel(fb.width() / 2, fb.height() / 2), qRgb(0xff, 0, 0));
161 }
162
163
164
165 // Same as the effect test up above, but this time use the item
166 // directly in a stand alone ShaderEffect
167 void tst_QQuickItemLayer::layerIsTextureProvider()
168 {
169     QImage fb = runTest(testFile("TextureProvider.qml"));
170     QCOMPARE(fb.pixel(0, 0), qRgb(0xff, 0, 0));
171     QCOMPARE(fb.pixel(fb.width() - 1, 0), qRgb(0, 0xff, 0));
172 }
173
174
175 void tst_QQuickItemLayer::layerVisibility_data()
176 {
177     QTest::addColumn<bool>("visible");
178     QTest::addColumn<bool>("effect");
179     QTest::addColumn<qreal>("opacity");
180
181     QTest::newRow("!effect, !visible, a=1") << false << false << 1.;
182     QTest::newRow("!effect, visible, a=1") << false << true << 1.;
183     QTest::newRow("effect, !visible, a=1") << true << false << 1.;
184     QTest::newRow("effect, visible, a=1") << true << true << 1.;
185
186     QTest::newRow("!effect, !visible, a=.5") << false << false << .5;
187     QTest::newRow("!effect, visible, a=.5") << false << true << .5;
188     QTest::newRow("effect, !visible, a=.5") << true << false << .5;
189     QTest::newRow("effect, visible, a=.5") << true << true << .5;
190
191     QTest::newRow("!effect, !visible, a=0") << false << false << 0.;
192     QTest::newRow("!effect, visible, a=0") << false << true << 0.;
193     QTest::newRow("effect, !visible, a=0") << true << false << 0.;
194     QTest::newRow("effect, visible, a=0") << true << true << 0.;
195 }
196
197 void tst_QQuickItemLayer::layerVisibility()
198 {
199     QFETCH(bool, visible);
200     QFETCH(bool, effect);
201     QFETCH(qreal, opacity);
202
203     QQuickView view;
204     view.setSource(testFile("Visible.qml"));
205
206     QQuickItem *child = view.rootItem()->childItems().at(0);
207     child->setProperty("layerVisible", visible);
208     child->setProperty("layerEffect", effect);
209     child->setProperty("layerOpacity", opacity);
210
211     view.show();
212
213     QTest::qWaitForWindowShown(&view);
214
215     QImage fb = view.grabFrameBuffer();
216     uint pixel = fb.pixel(0, 0);
217
218     if (!visible || opacity == 0) {
219         QCOMPARE(pixel, qRgb(0xff, 0xff, 0xff));
220     } else if (effect) {
221         QCOMPARE(qRed(pixel), 0xff);
222         QVERIFY(qGreen(pixel) < 0xff);
223         QVERIFY(qBlue(pixel) < 0xff);
224     } else { // no effect
225         QCOMPARE(qBlue(pixel), 0xff);
226         QVERIFY(qGreen(pixel) < 0xff);
227         QVERIFY(qRed(pixel) < 0xff);
228     }
229 }
230
231
232
233
234 void tst_QQuickItemLayer::layerZOrder_data()
235 {
236     QTest::addColumn<bool>("effect");
237
238     QTest::newRow("!effect") << false;
239     QTest::newRow("effect") << true;
240 }
241
242 void tst_QQuickItemLayer::layerZOrder()
243 {
244     QFETCH(bool, effect);
245
246     QQuickView view;
247     view.setSource(testFile("ZOrder.qml"));
248
249     QQuickItem *child = view.rootItem()->childItems().at(0);
250     child->setProperty("layerEffect", effect);
251
252     view.show();
253
254     QTest::qWaitForWindowShown(&view);
255
256     QImage fb = view.grabFrameBuffer();
257
258     QCOMPARE(fb.pixel(50, 50), qRgb(0, 0, 0xff));
259     QCOMPARE(fb.pixel(150, 150), qRgb(0, 0xff, 00));
260
261 }
262
263
264
265 QTEST_MAIN(tst_QQuickItemLayer)
266
267 #include "tst_qquickitemlayer.moc"