1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the test suite of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
44 #include <QtQuick/qquickitem.h>
45 #include <QtQuick/qquickview.h>
47 #include "../../shared/util.h"
49 class tst_QQuickItemLayer: public QDeclarativeDataTest
53 tst_QQuickItemLayer();
55 QImage runTest(const QString &url)
58 view.setSource(QUrl(url));
61 QTest::qWaitForWindowShown(&view);
63 return view.grabFrameBuffer();
72 void layerVisibility_data();
73 void layerVisibility();
75 void layerSourceRect();
78 void layerZOrder_data();
81 void layerIsTextureProvider();
84 tst_QQuickItemLayer::tst_QQuickItemLayer()
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.
94 void tst_QQuickItemLayer::layerSmooth()
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));
100 uint pixel = fb.pixel(fb.width() / 2, 0);
101 QVERIFY(qRed(pixel) > 0);
102 QVERIFY(qBlue(pixel) > 0);
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
111 void tst_QQuickItemLayer::layerEnabled()
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));
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
125 void tst_QQuickItemLayer::layerMipmap()
127 QImage fb = runTest(testFile("Mipmap.qml"));
128 QVERIFY(fb.pixel(0, 0) != 0xff000000);
129 QVERIFY(fb.pixel(0, 0) != 0xffffffff);
134 // The test implements an rgb swapping effect sourced from a blue rectangle. The
135 // resulting pixel should be red
137 void tst_QQuickItemLayer::layerEffect()
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));
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()
151 QImage fb = runTest(testFile("SourceRect.qml"));
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));
159 // The center pixel should be red
160 QCOMPARE(fb.pixel(fb.width() / 2, fb.height() / 2), qRgb(0xff, 0, 0));
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()
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));
175 void tst_QQuickItemLayer::layerVisibility_data()
177 QTest::addColumn<bool>("visible");
178 QTest::addColumn<bool>("effect");
179 QTest::addColumn<qreal>("opacity");
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.;
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;
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.;
197 void tst_QQuickItemLayer::layerVisibility()
199 QFETCH(bool, visible);
200 QFETCH(bool, effect);
201 QFETCH(qreal, opacity);
204 view.setSource(testFile("Visible.qml"));
206 QQuickItem *child = view.rootItem()->childItems().at(0);
207 child->setProperty("layerVisible", visible);
208 child->setProperty("layerEffect", effect);
209 child->setProperty("layerOpacity", opacity);
213 QTest::qWaitForWindowShown(&view);
215 QImage fb = view.grabFrameBuffer();
216 uint pixel = fb.pixel(0, 0);
218 if (!visible || opacity == 0) {
219 QCOMPARE(pixel, qRgb(0xff, 0xff, 0xff));
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);
234 void tst_QQuickItemLayer::layerZOrder_data()
236 QTest::addColumn<bool>("effect");
238 QTest::newRow("!effect") << false;
239 QTest::newRow("effect") << true;
242 void tst_QQuickItemLayer::layerZOrder()
244 QFETCH(bool, effect);
247 view.setSource(testFile("ZOrder.qml"));
249 QQuickItem *child = view.rootItem()->childItems().at(0);
250 child->setProperty("layerEffect", effect);
254 QTest::qWaitForWindowShown(&view);
256 QImage fb = view.grabFrameBuffer();
258 QCOMPARE(fb.pixel(50, 50), qRgb(0, 0, 0xff));
259 QCOMPARE(fb.pixel(150, 150), qRgb(0, 0xff, 00));
265 QTEST_MAIN(tst_QQuickItemLayer)
267 #include "tst_qquickitemlayer.moc"