Resynced test cases and removed TET framework
[platform/core/uifw/dali-core.git] / automated-tests / src / dali-unmanaged / utc-Dali-ShaderEffect.cpp
1 //
2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://floralicense.org/license/
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an AS IS BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 #include <iostream>
18
19 #include <stdlib.h>
20 #include <dali/dali.h>
21 #include <dali-test-suite-utils.h>
22
23 using namespace Dali;
24
25 namespace
26 {
27
28 static const char* VertexSource =
29 "void main()\n"
30 "{\n"
31 "  gl_Position = uProjection * uModelView * vec4(aPosition, 1.0);\n"
32 "  vTexCoord = aTexCoord;\n"
33 "}\n";
34
35 static const char* FragmentSource =
36 "void main()\n"
37 "{\n"
38 "  gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor;\n"
39 "}\n";
40
41 static const char* FragmentSourceUsingExtensions =
42 "void main()\n"
43 "{\n"
44 "  float floatValue = 0.5f;\n"
45 "  float test = fwidth(floatValue);\n"
46 "  gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor;\n"
47 "  gl_FragColor.a *= test;\n"
48 "}\n";
49
50 const int GETSOURCE_BUFFER_SIZE = 0x10000;
51
52 static const char* TestImageFilename = "icon_wrt.png";
53
54 } // Anonymous namespace
55
56
57 int UtcDaliShaderEffectFromProperties01(void)
58 {
59   TestApplication application;
60   tet_infoline("UtcDaliShaderEffectFromProperties01()");
61
62   std::string fragmentShaderPrefix = "#define TEST_FS 1\n#extension GL_OES_standard_derivatives : enable";
63   std::string vertexShaderPrefix = "#define TEST_VS 1";
64
65   // Call render to compile default shaders.
66   application.SendNotification();
67   application.Render();
68   application.Render();
69   application.Render();
70
71   GLuint lastShaderCompiledBefore = application.GetGlAbstraction().GetLastShaderCompiled();
72
73   // create from type registry
74
75   TypeInfo typeInfo = TypeRegistry::Get().GetTypeInfo( "ShaderEffect" );
76   DALI_TEST_CHECK( typeInfo );
77   ShaderEffect effect = ShaderEffect::DownCast( typeInfo.CreateInstance() );
78   DALI_TEST_CHECK( effect );
79
80   Property::Value programMap = Property::Value(Property::MAP);
81
82   programMap.SetValue("vertex", std::string(VertexSource));
83   programMap.SetValue("fragment", std::string(FragmentSource));
84
85   programMap.SetValue("vertex-prefix", std::string(fragmentShaderPrefix));
86   programMap.SetValue("fragment-prefix", std::string(vertexShaderPrefix));
87
88   programMap.SetValue("geometry-type", "GEOMETRY_TYPE_IMAGE");
89
90   effect.SetProperty(effect.GetPropertyIndex("program"), programMap);
91
92   Property::Value imageMap = Property::Value(Property::MAP);
93   imageMap.SetValue("filename", Property::Value(TestImageFilename));
94
95   effect.SetProperty(effect.GetPropertyIndex("image"), imageMap);
96
97   BitmapImage image(CreateBitmapImage());
98
99   ImageActor actor = ImageActor::New( image );
100   actor.SetSize( 100.0f, 100.0f );
101   actor.SetName("TestImageFilenameActor");
102   actor.SetShaderEffect(effect);
103   Stage::GetCurrent().Add(actor);
104
105   application.SendNotification();
106   application.Render();
107   GLuint lastShaderCompiledAfter = application.GetGlAbstraction().GetLastShaderCompiled();
108   bool testResult = false;
109
110   // we should have compiled 4 shaders.
111   DALI_TEST_CHECK(lastShaderCompiledAfter - lastShaderCompiledBefore == 4);
112   if (lastShaderCompiledAfter - lastShaderCompiledBefore == 4)
113   {
114     char testVertexSourceResult[GETSOURCE_BUFFER_SIZE];
115     char testFragmentSourceResult[GETSOURCE_BUFFER_SIZE];
116
117     // we are interested in the first two.
118     GLuint vertexShaderId = lastShaderCompiledBefore + 1;
119     GLuint fragmentShaderId = lastShaderCompiledBefore + 2;
120
121     GLsizei lengthVertexResult;
122     GLsizei lengthFragmentResult;
123
124     application.GetGlAbstraction().GetShaderSource(vertexShaderId, GETSOURCE_BUFFER_SIZE, &lengthVertexResult, testVertexSourceResult);
125     application.GetGlAbstraction().GetShaderSource(fragmentShaderId, GETSOURCE_BUFFER_SIZE, &lengthFragmentResult, testFragmentSourceResult);
126
127     int vertexShaderHasPrefix = strncmp(testVertexSourceResult, "#define ", strlen("#define "));
128     int fragmentShaderHasPrefix = strncmp(testFragmentSourceResult, "#define ", strlen("#define "));
129     testResult = (vertexShaderHasPrefix == 0) && (fragmentShaderHasPrefix == 0);
130   }
131   DALI_TEST_CHECK(testResult);
132   END_TEST;
133 }
134
135 int UtcDaliShaderEffectFromProperties02(void)
136 {
137   try
138   {
139     TestApplication application;
140     tet_infoline("UtcDaliShaderEffectFromProperties02()");
141
142     // Call render to compile default shaders.
143     application.SendNotification();
144     application.Render();
145     application.Render();
146     application.Render();
147
148     // create from type registry (currently only way to get ShaderEffect with no shader setup in constructor
149     TypeInfo typeInfo = TypeRegistry::Get().GetTypeInfo( "ShaderEffect" );
150     DALI_TEST_CHECK( typeInfo );
151     ShaderEffect effect = ShaderEffect::DownCast( typeInfo.CreateInstance() );
152     DALI_TEST_CHECK( effect );
153
154     Property::Value programMap = Property::Value(Property::MAP);
155
156     programMap.SetValue("vertex",   std::string(VertexSource));
157     programMap.SetValue("fragment", std::string(FragmentSource));
158
159     // programMap.SetValue("geometry-type", "GEOMETRY_TYPE_IMAGE");
160     // dont set by value
161     programMap.SetValue("geometry-type", GeometryType( GEOMETRY_TYPE_IMAGE ));
162
163     effect.SetProperty(effect.GetPropertyIndex("program"), programMap);
164
165     tet_result( TET_FAIL );
166   }
167   catch(Dali::DaliException& e)
168   {
169     tet_printf("Assertion %s failed at %s\n", e.mCondition.c_str(), e.mLocation.c_str());
170   }
171   END_TEST;
172 }
173
174 int UtcDaliShaderEffectFromProperties03(void)
175 {
176   try
177   {
178     TestApplication application;
179     tet_infoline("UtcDaliShaderEffectFromProperties03()");
180
181     // Call render to compile default shaders.
182     application.SendNotification();
183     application.Render();
184     application.Render();
185     application.Render();
186
187     // create from type registry (currently only way to get ShaderEffect with no shader setup in constructor
188     TypeInfo typeInfo = TypeRegistry::Get().GetTypeInfo( "ShaderEffect" );
189     DALI_TEST_CHECK( typeInfo );
190     ShaderEffect effect = ShaderEffect::DownCast( typeInfo.CreateInstance() );
191     DALI_TEST_CHECK( effect );
192
193     // dont set unknown
194     effect.SetProperty( effect.GetPropertyIndex("geometry-hints"), "HINT_2" );
195
196     tet_result( TET_FAIL );
197   }
198   catch(Dali::DaliException& e)
199   {
200     tet_printf("Assertion %s failed at %s\n", e.mCondition.c_str(), e.mLocation.c_str());
201   }
202   END_TEST;
203 }