d08281933dffdc2c8146765368668df42f5a0665
[profile/ivi/qtdeclarative.git] / tests / auto / qtquick2 / qquickshadereffect / tst_qquickshadereffect.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 <QList>
45 #include <QByteArray>
46 #include <private/qquickshadereffect_p.h>
47
48 class TestShaderEffect : public QQuickShaderEffect
49 {
50     Q_OBJECT
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)
55
56 public:
57     QVariant dummyRead() const { return QVariant(); }
58     bool isConnected(const char *signal) const { return m_signals.contains(signal); }
59
60 protected:
61     void connectNotify(const char *signal) { m_signals.append(signal); }
62     void disconnectNotify(const char *signal) { m_signals.removeOne(signal); }
63
64 signals:
65     void dummyChanged();
66
67 private:
68     QList<QByteArray> m_signals;
69 };
70
71 class tst_qquickshadereffect : public QObject
72 {
73     Q_OBJECT
74 public:
75     tst_qquickshadereffect();
76
77 private slots:
78     void initTestCase();
79     void cleanupTestCase();
80
81     void lookThroughShaderCode_data();
82     void lookThroughShaderCode();
83
84 private:
85     enum PresenceFlags {
86         VertexPresent = 0x01,
87         TexCoordPresent = 0x02,
88         MatrixPresent = 0x04,
89         OpacityPresent = 0x08,
90         PropertyPresent = 0x10
91     };
92 };
93
94 tst_qquickshadereffect::tst_qquickshadereffect()
95 {
96 }
97
98 void tst_qquickshadereffect::initTestCase()
99 {
100 }
101
102 void tst_qquickshadereffect::cleanupTestCase()
103 {
104 }
105
106 void tst_qquickshadereffect::lookThroughShaderCode_data()
107 {
108     QTest::addColumn<QByteArray>("vertexShader");
109     QTest::addColumn<QByteArray>("fragmentShader");
110     QTest::addColumn<int>("presenceFlags");
111
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"
117                           "void main() {                                                  \n"
118                           "    qt_TexCoord0 = qt_MultiTexCoord0;                          \n"
119                           "    gl_Position = qt_Matrix * qt_Vertex;                       \n"
120                           "}")
121             << QByteArray("varying highp vec2 qt_TexCoord0;                                   \n"
122                           "uniform sampler2D source;                                          \n"
123                           "uniform lowp float qt_Opacity;                                     \n"
124                           "void main() {                                                      \n"
125                           "    gl_FragColor = texture2D(source, qt_TexCoord0) * qt_Opacity;   \n"
126                           "}")
127             << (VertexPresent | TexCoordPresent | MatrixPresent | OpacityPresent | PropertyPresent);
128
129     QTest::newRow("empty")
130             << QByteArray(" ") // one space -- if completely empty, default will be used instead.
131             << QByteArray(" ")
132             << 0;
133
134
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);
141
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);
148
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);
156
157
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);
164
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);
171
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);
178
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);
186
187
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;")
194             << 0;
195
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;")
201             << QByteArray(" ")
202             << 0;
203
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;")
210             << 0;
211
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;")
217             << QByteArray(" ")
218             << 0;
219
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);
227
228
229     QTest::newRow("property name #1")
230             << QByteArray("uniform highp vec3 _0aA9zZ;")
231             << QByteArray(" ")
232             << int(PropertyPresent);
233
234     QTest::newRow("property name #2")
235             << QByteArray("uniform mediump vec2 x86;")
236             << QByteArray(" ")
237             << int(PropertyPresent);
238
239     QTest::newRow("property name #3")
240             << QByteArray("uniform lowp float X;")
241             << QByteArray(" ")
242             << int(PropertyPresent);
243 }
244
245 void tst_qquickshadereffect::lookThroughShaderCode()
246 {
247     QFETCH(QByteArray, vertexShader);
248     QFETCH(QByteArray, fragmentShader);
249     QFETCH(int, presenceFlags);
250
251     TestShaderEffect item;
252     QVERIFY(!item.isConnected(SIGNAL(dummyChanged()))); // Nothing connected yet.
253
254     QString expected;
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";
263
264     item.setVertexShader(vertexShader);
265     item.setFragmentShader(fragmentShader);
266     item.ensureCompleted();
267     QCOMPARE(item.parseLog(), expected);
268
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);
271 }
272
273 QTEST_MAIN(tst_qquickshadereffect)
274
275 #include "tst_qquickshadereffect.moc"