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 ****************************************************************************/
46 #include <private/qquickshadereffect_p.h>
48 class TestShaderEffect : public QQuickShaderEffect
51 Q_PROPERTY(QVariant source READ dummyRead NOTIFY dummyChanged)
52 Q_PROPERTY(QVariant _0aA9zZ READ dummyRead NOTIFY dummyChanged)
53 Q_PROPERTY(QVariant x86 READ dummyRead NOTIFY dummyChanged)
54 Q_PROPERTY(QVariant X READ dummyRead NOTIFY dummyChanged)
57 QVariant dummyRead() const { return QVariant(); }
58 bool isConnected(const char *signal) const { return m_signals.contains(signal); }
61 void connectNotify(const char *signal) { m_signals.append(signal); }
62 void disconnectNotify(const char *signal) { m_signals.removeOne(signal); }
68 QList<QByteArray> m_signals;
71 class tst_qquickshadereffect : public QObject
75 tst_qquickshadereffect();
79 void cleanupTestCase();
81 void lookThroughShaderCode_data();
82 void lookThroughShaderCode();
87 TexCoordPresent = 0x02,
89 OpacityPresent = 0x08,
90 PropertyPresent = 0x10
94 tst_qquickshadereffect::tst_qquickshadereffect()
98 void tst_qquickshadereffect::initTestCase()
102 void tst_qquickshadereffect::cleanupTestCase()
106 void tst_qquickshadereffect::lookThroughShaderCode_data()
108 QTest::addColumn<QByteArray>("vertexShader");
109 QTest::addColumn<QByteArray>("fragmentShader");
110 QTest::addColumn<int>("presenceFlags");
112 QTest::newRow("default")
113 << QByteArray("uniform highp mat4 qt_Matrix; \n"
114 "attribute highp vec4 qt_Vertex; \n"
115 "attribute highp vec2 qt_MultiTexCoord0; \n"
116 "varying highp vec2 qt_TexCoord0; \n"
118 " qt_TexCoord0 = qt_MultiTexCoord0; \n"
119 " gl_Position = qt_Matrix * qt_Vertex; \n"
121 << QByteArray("varying highp vec2 qt_TexCoord0; \n"
122 "uniform sampler2D source; \n"
123 "uniform lowp float qt_Opacity; \n"
125 " gl_FragColor = texture2D(source, qt_TexCoord0) * qt_Opacity; \n"
127 << (VertexPresent | TexCoordPresent | MatrixPresent | OpacityPresent | PropertyPresent);
129 QTest::newRow("empty")
130 << QByteArray(" ") // one space -- if completely empty, default will be used instead.
135 QTest::newRow("inside line comments")
136 << QByteArray("//uniform highp mat4 qt_Matrix;\n"
137 "attribute highp vec4 qt_Vertex;\n"
138 "// attribute highp vec2 qt_MultiTexCoord0;")
139 << QByteArray("uniform int source; // uniform lowp float qt_Opacity;")
140 << (VertexPresent | PropertyPresent);
142 QTest::newRow("inside block comments")
143 << QByteArray("/*uniform highp mat4 qt_Matrix;\n"
144 "*/attribute highp vec4 qt_Vertex;\n"
145 "/*/attribute highp vec2 qt_MultiTexCoord0;//**/")
146 << QByteArray("/**/uniform int source; /* uniform lowp float qt_Opacity; */")
147 << (VertexPresent | PropertyPresent);
149 QTest::newRow("inside preprocessor directive")
150 << QByteArray("#define uniform\nhighp mat4 qt_Matrix;\n"
151 "attribute highp vec4 qt_Vertex;\n"
152 "#if\\\nattribute highp vec2 qt_MultiTexCoord0;")
153 << QByteArray("uniform int source;\n"
154 " # undef uniform lowp float qt_Opacity;")
155 << (VertexPresent | PropertyPresent);
158 QTest::newRow("line comments between")
159 << QByteArray("uniform//foo\nhighp//bar\nmat4//baz\nqt_Matrix;\n"
160 "attribute//\nhighp//\nvec4//\nqt_Vertex;\n"
161 " //*/ uniform \n attribute //\\ \n highp //// \n vec2 //* \n qt_MultiTexCoord0;")
162 << QByteArray("uniform// lowp float qt_Opacity;\nsampler2D source;")
163 << (VertexPresent | TexCoordPresent | MatrixPresent | PropertyPresent);
165 QTest::newRow("block comments between")
166 << QByteArray("uniform/*foo*/highp/*/bar/*/mat4/**//**/qt_Matrix;\n"
167 "attribute/**/highp/**/vec4/**/qt_Vertex;\n"
168 " /* * */ attribute /*///*/ highp /****/ vec2 /**/ qt_MultiTexCoord0;")
169 << QByteArray("uniform/*/ uniform//lowp/*float qt_Opacity;*/sampler2D source;")
170 << (VertexPresent | TexCoordPresent | MatrixPresent | PropertyPresent);
172 QTest::newRow("preprocessor directive between")
173 << QByteArray("uniform\n#foo\nhighp\n#bar\nmat4\n#baz\\\nblimey\nqt_Matrix;\n"
174 "attribute\n#\nhighp\n#\nvec4\n#\nqt_Vertex;\n"
175 " #uniform \n attribute \n # foo \n highp \n # bar \n vec2 \n#baz \n qt_MultiTexCoord0;")
176 << QByteArray("uniform\n#if lowp float qt_Opacity;\nsampler2D source;")
177 << (VertexPresent | TexCoordPresent | MatrixPresent | PropertyPresent);
179 QTest::newRow("newline between")
180 << QByteArray("uniform\nhighp\nmat4\nqt_Matrix\n;\n"
181 "attribute \t\r\n highp \n vec4 \n\n qt_Vertex ;\n"
182 " \n attribute \n highp \n vec2 \n qt_Multi\nTexCoord0 \n ;")
183 << QByteArray("uniform\nsampler2D\nsource;"
184 "uniform lowp float qt_Opacity;")
185 << (VertexPresent | MatrixPresent | OpacityPresent | PropertyPresent);
188 QTest::newRow("extra characters #1")
189 << QByteArray("funiform highp mat4 qt_Matrix;\n"
190 "attribute highp vec4 qt_Vertex_;\n"
191 "attribute highp vec2 qqt_MultiTexCoord0;")
192 << QByteArray("uniformm int source;\n"
193 "uniform4 lowp float qt_Opacity;")
196 QTest::newRow("extra characters #2")
197 << QByteArray("attribute phighp vec4 qt_Vertex;\n"
198 "attribute highpi vec2 qt_MultiTexCoord0;"
199 "fattribute highp vec4 qt_Vertex;\n"
200 "attributed highp vec2 qt_MultiTexCoord0;")
204 QTest::newRow("missing characters #1")
205 << QByteArray("unifor highp mat4 qt_Matrix;\n"
206 "attribute highp vec4 qt_Vert;\n"
207 "attribute highp vec2 MultiTexCoord0;")
208 << QByteArray("niform int source;\n"
209 "uniform qt_Opacity;")
212 QTest::newRow("missing characters #2")
213 << QByteArray("attribute high vec4 qt_Vertex;\n"
214 "attribute ighp vec2 qt_MultiTexCoord0;"
215 "tribute highp vec4 qt_Vertex;\n"
216 "attrib highp vec2 qt_MultiTexCoord0;")
220 QTest::newRow("precision")
221 << QByteArray("uniform mat4 qt_Matrix;\n"
222 "attribute kindofhighp vec4 qt_Vertex;\n"
223 "attribute highp qt_MultiTexCoord0;\n")
224 << QByteArray("uniform lowp float qt_Opacity;\n"
225 "uniform mediump float source;\n")
226 << (MatrixPresent | OpacityPresent | PropertyPresent);
229 QTest::newRow("property name #1")
230 << QByteArray("uniform highp vec3 _0aA9zZ;")
232 << int(PropertyPresent);
234 QTest::newRow("property name #2")
235 << QByteArray("uniform mediump vec2 x86;")
237 << int(PropertyPresent);
239 QTest::newRow("property name #3")
240 << QByteArray("uniform lowp float X;")
242 << int(PropertyPresent);
245 void tst_qquickshadereffect::lookThroughShaderCode()
247 QFETCH(QByteArray, vertexShader);
248 QFETCH(QByteArray, fragmentShader);
249 QFETCH(int, presenceFlags);
251 TestShaderEffect item;
252 QVERIFY(!item.isConnected(SIGNAL(dummyChanged()))); // Nothing connected yet.
255 if ((presenceFlags & VertexPresent) == 0)
256 expected += "Warning: Missing reference to \'qt_Vertex\'.\n";
257 if ((presenceFlags & TexCoordPresent) == 0)
258 expected += "Warning: Missing reference to \'qt_MultiTexCoord0\'.\n";
259 if ((presenceFlags & MatrixPresent) == 0)
260 expected += "Warning: Missing reference to \'qt_Matrix\'.\n";
261 if ((presenceFlags & OpacityPresent) == 0)
262 expected += "Warning: Missing reference to \'qt_Opacity\'.\n";
264 item.setVertexShader(vertexShader);
265 item.setFragmentShader(fragmentShader);
266 item.ensureCompleted();
267 QCOMPARE(item.parseLog(), expected);
269 // If the uniform was successfully parsed, the notify signal has been connected to an update slot.
270 QCOMPARE(item.isConnected(SIGNAL(dummyChanged())), (presenceFlags & PropertyPresent) != 0);
273 QTEST_MAIN(tst_qquickshadereffect)
275 #include "tst_qquickshadereffect.moc"