UTC coverage for new mesh
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / utc-Dali-Sampler.cpp
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.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://www.apache.org/licenses/LICENSE-2.0
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
18 #include <dali/public-api/dali-core.h>
19 #include <dali-test-suite-utils.h>
20
21 using namespace Dali;
22
23 #include <mesh-builder.h>
24
25 void sampler_test_startup(void)
26 {
27   test_return_value = TET_UNDEF;
28 }
29
30 void sampler_test_cleanup(void)
31 {
32   test_return_value = TET_PASS;
33 }
34
35 int UtcDaliSamplerNew01(void)
36 {
37   TestApplication application;
38
39   Image image = BufferImage::New( 64, 64, Pixel::RGBA8888 );
40   Sampler sampler = Sampler::New(image, "sTexture");
41
42   DALI_TEST_EQUALS( (bool)sampler, true, TEST_LOCATION );
43   END_TEST;
44 }
45
46 int UtcDaliSamplerNew02(void)
47 {
48   TestApplication application;
49   Sampler sampler;
50   DALI_TEST_EQUALS( (bool)sampler, false, TEST_LOCATION );
51   END_TEST;
52 }
53
54 int UtcDaliSamplerCopyConstructor(void)
55 {
56   TestApplication application;
57   tet_infoline("Testing Dali::Handle::Handle(const Handle&)");
58
59   // Initialize an object, ref count == 1
60   Image image = BufferImage::New( 64, 64, Pixel::RGBA8888 );
61   Sampler sampler = Sampler::New(image, "sTexture");
62
63   DALI_TEST_EQUALS(1, sampler.GetBaseObject().ReferenceCount(), TEST_LOCATION);
64
65   // Copy the object, ref count == 2
66   Sampler copy(sampler);
67   DALI_TEST_CHECK(copy);
68   if (copy)
69   {
70     DALI_TEST_EQUALS(2, copy.GetBaseObject().ReferenceCount(), TEST_LOCATION);
71   }
72
73   END_TEST;
74 }
75
76
77 int UtcDaliSamplerDownCast01(void)
78 {
79   TestApplication application;
80   Image image = BufferImage::New( 64, 64, Pixel::RGBA8888 );
81   Sampler sampler = Sampler::New(image, "sTexture");
82
83   BaseHandle handle(sampler);
84   Sampler sampler2 = Sampler::DownCast(handle);
85   DALI_TEST_EQUALS( (bool)sampler2, true, TEST_LOCATION );
86   END_TEST;
87 }
88
89 int UtcDaliSamplerDownCast02(void)
90 {
91   TestApplication application;
92
93   BaseHandle handle;
94   Sampler sampler = Sampler::DownCast(handle);
95   DALI_TEST_EQUALS( (bool)sampler, false, TEST_LOCATION );
96   END_TEST;
97 }
98
99 int UtcDaliSamplerAssignmentOperator(void)
100 {
101   TestApplication application;
102
103   Image image = BufferImage::New( 64, 64, Pixel::RGBA8888 );
104   Sampler sampler1 = Sampler::New(image, "sTexture");
105
106   Sampler sampler2;
107
108   DALI_TEST_CHECK(!(sampler1 == sampler2));
109
110   sampler2 = sampler1;
111
112   DALI_TEST_CHECK(sampler1 == sampler2);
113
114   sampler2 = Sampler::New(image, "sTexture");
115
116   DALI_TEST_CHECK(!(sampler1 == sampler2));
117
118   END_TEST;
119 }
120
121 int UtcDaliSamplerSetUniformName01(void)
122 {
123   TestApplication application;
124
125   Image image = BufferImage::New( 64, 64, Pixel::RGBA8888 );
126   Sampler sampler = Sampler::New(image, "sTexture");
127   sampler.SetUniformName( "sEffectTexture" );
128
129   Material material = CreateMaterial(1.0f);
130   material.AddSampler( sampler );
131
132   Geometry geometry = CreateQuadGeometry();
133   Renderer renderer = Renderer::New( geometry, material );
134   Actor actor = Actor::New();
135   actor.AddRenderer(renderer);
136   actor.SetParentOrigin( ParentOrigin::CENTER );
137   actor.SetSize(400, 400);
138
139   Stage::GetCurrent().Add( actor );
140
141   TestGlAbstraction& gl = application.GetGlAbstraction();
142
143   application.SendNotification();
144   application.Render();
145
146   int textureUnit=-1;
147   DALI_TEST_CHECK( gl.GetUniformValue<int>( "sEffectTexture", textureUnit ) );
148   DALI_TEST_EQUALS( textureUnit, 0, TEST_LOCATION );
149
150   END_TEST;
151 }
152
153 int UtcDaliSamplerSetUniformName02(void)
154 {
155   TestApplication application;
156
157   Image image = BufferImage::New( 64, 64, Pixel::RGBA8888 );
158   Image image2 = BufferImage::New( 64, 64, Pixel::RGBA8888 );
159   Sampler sampler1 = Sampler::New(image, "sTexture");
160   sampler1.SetUniformName( "sEffectTexture" );
161
162   Sampler sampler2 = Sampler::New(image2, "sTexture2");
163
164   Material material = CreateMaterial(1.0f);
165   material.AddSampler( sampler1 );
166   material.AddSampler( sampler2 );
167
168   Geometry geometry = CreateQuadGeometry();
169   Renderer renderer = Renderer::New( geometry, material );
170   Actor actor = Actor::New();
171   actor.AddRenderer(renderer);
172   actor.SetParentOrigin( ParentOrigin::CENTER );
173   actor.SetSize(400, 400);
174
175   Stage::GetCurrent().Add( actor );
176
177   TestGlAbstraction& gl = application.GetGlAbstraction();
178
179   application.SendNotification();
180   application.Render();
181
182   int textureUnit=-1;
183   DALI_TEST_CHECK( gl.GetUniformValue<int>( "sEffectTexture", textureUnit ) );
184   DALI_TEST_EQUALS( textureUnit, 0, TEST_LOCATION );
185
186   DALI_TEST_CHECK( gl.GetUniformValue<int>( "sTexture2", textureUnit ) );
187   DALI_TEST_EQUALS( textureUnit, 1, TEST_LOCATION );
188
189   END_TEST;
190 }
191
192 int UtcDaliSamplerSetGetImage(void)
193 {
194   TestApplication application;
195
196   Image image1 = BufferImage::New( 64, 64, Pixel::RGBA8888 );
197   Image image2 = BufferImage::New( 64, 64, Pixel::RGBA8888 );
198   Sampler sampler = Sampler::New(image1, "sTexture");
199
200   DALI_TEST_CHECK(image1 == sampler.GetImage());
201
202   sampler.SetImage( image2 );
203   DALI_TEST_CHECK(!(image1 == sampler.GetImage()));
204   DALI_TEST_CHECK(image2 == sampler.GetImage());
205
206   END_TEST;
207 }
208
209 int UtcSamplerSetFilterMode(void)
210 {
211   TestApplication application;
212
213   Image image = BufferImage::New( 64, 64, Pixel::RGBA8888 );
214   Sampler sampler = Sampler::New(image, "sTexture");
215
216   Material material = CreateMaterial(1.0f);
217   material.AddSampler( sampler );
218
219   Geometry geometry = CreateQuadGeometry();
220   Renderer renderer = Renderer::New( geometry, material );
221   Actor actor = Actor::New();
222   actor.AddRenderer(renderer);
223   actor.SetParentOrigin( ParentOrigin::CENTER );
224   actor.SetSize(400, 400);
225   Stage::GetCurrent().Add( actor );
226
227   float initialValue = 1.0f;
228   sampler.RegisterProperty("uWidthClamp", initialValue );
229
230   TestGlAbstraction& gl = application.GetGlAbstraction();
231
232   /**************************************************************/
233   // Default/Default
234   TraceCallStack& texParameterTrace = gl.GetTexParameterTrace();
235   texParameterTrace.Reset();
236   texParameterTrace.Enable( true );
237
238   sampler.SetFilterMode( Sampler::DEFAULT, Sampler::DEFAULT );
239   application.SendNotification();
240   application.Render();
241
242   texParameterTrace.Enable( false );
243
244   // Verify gl state
245
246   // There are two calls to TexParameteri when the texture is first created
247   // Texture mag filter is not called as the first time set it uses the system default
248   DALI_TEST_EQUALS( texParameterTrace.CountMethod( "TexParameteri" ), 3, TEST_LOCATION);
249
250   std::stringstream out;
251   out << GL_TEXTURE_2D << ", " << GL_TEXTURE_MIN_FILTER << ", " << GL_LINEAR;
252   DALI_TEST_EQUALS( texParameterTrace.TestMethodAndParams(2, "TexParameteri", out.str()), true, TEST_LOCATION);
253
254   /**************************************************************/
255   // Default/Default
256   texParameterTrace.Reset();
257   texParameterTrace.Enable( true );
258
259   sampler.SetFilterMode( Sampler::DEFAULT, Sampler::DEFAULT );
260
261   // Flush the queue and render once
262   application.SendNotification();
263   application.Render();
264
265   texParameterTrace.Enable( false );
266
267   // Verify gl state
268
269   // Should not make any calls when settings are the same
270   DALI_TEST_EQUALS( texParameterTrace.CountMethod( "TexParameteri" ), 0, TEST_LOCATION);
271
272   /**************************************************************/
273   // Nearest/Nearest
274   texParameterTrace.Reset();
275   texParameterTrace.Enable( true );
276
277   sampler.SetFilterMode( Sampler::NEAREST, Sampler::NEAREST );
278
279   // Flush the queue and render once
280   application.SendNotification();
281   application.Render();
282
283   texParameterTrace.Enable( false );
284
285   // Verify actor gl state
286   DALI_TEST_EQUALS( texParameterTrace.CountMethod( "TexParameteri" ), 2, TEST_LOCATION);
287
288   out.str("");
289   out << GL_TEXTURE_2D << ", " << GL_TEXTURE_MIN_FILTER << ", " << GL_NEAREST;
290   DALI_TEST_EQUALS( texParameterTrace.TestMethodAndParams(0, "TexParameteri", out.str()), true, TEST_LOCATION);
291
292   out.str("");
293   out << GL_TEXTURE_2D << ", " << GL_TEXTURE_MAG_FILTER << ", " << GL_NEAREST;
294   DALI_TEST_EQUALS( texParameterTrace.TestMethodAndParams(1, "TexParameteri", out.str()), true, TEST_LOCATION);
295
296   /**************************************************************/
297   // Nearest/Linear
298   texParameterTrace.Reset();
299   texParameterTrace.Enable( true );
300
301   sampler.SetFilterMode( Sampler::NEAREST, Sampler::LINEAR );
302
303   // Flush the queue and render once
304   application.SendNotification();
305   application.Render();
306
307   texParameterTrace.Enable( false );
308
309   // Verify actor gl state
310   DALI_TEST_EQUALS( texParameterTrace.CountMethod( "TexParameteri" ), 1, TEST_LOCATION);
311
312   out.str("");
313   out << GL_TEXTURE_2D << ", " << GL_TEXTURE_MAG_FILTER << ", " << GL_LINEAR;
314   DALI_TEST_EQUALS( texParameterTrace.TestMethodAndParams(0, "TexParameteri", out.str()), true, TEST_LOCATION);
315
316   /**************************************************************/
317   // NONE/NONE
318   texParameterTrace.Reset();
319   texParameterTrace.Enable( true );
320
321   sampler.SetFilterMode( Sampler::NONE, Sampler::NONE );
322
323   // Flush the queue and render once
324   application.SendNotification();
325   application.Render();
326
327   texParameterTrace.Enable( false );
328
329   // Verify actor gl state
330   DALI_TEST_EQUALS( texParameterTrace.CountMethod( "TexParameteri" ), 1, TEST_LOCATION);
331
332   out.str("");
333   out << GL_TEXTURE_2D << ", " << GL_TEXTURE_MIN_FILTER << ", " << GL_NEAREST_MIPMAP_LINEAR;
334   DALI_TEST_EQUALS( texParameterTrace.TestMethodAndParams(0, "TexParameteri", out.str()), true, TEST_LOCATION);
335
336   END_TEST;
337 }
338
339 int UtcSamplerSetWrapMode(void)
340 {
341   TestApplication application;
342
343   Image image = BufferImage::New( 64, 64, Pixel::RGBA8888 );
344   Sampler sampler = Sampler::New(image, "sTexture");
345
346   Material material = CreateMaterial(1.0f);
347   material.AddSampler( sampler );
348
349   Geometry geometry = CreateQuadGeometry();
350   Renderer renderer = Renderer::New( geometry, material );
351   Actor actor = Actor::New();
352   actor.AddRenderer(renderer);
353   actor.SetParentOrigin( ParentOrigin::CENTER );
354   actor.SetSize(400, 400);
355   Stage::GetCurrent().Add( actor );
356
357   TestGlAbstraction& gl = application.GetGlAbstraction();
358
359   //****************************************
360   // CLAMP_TO_EDGE / CLAMP_TO_EDGE
361   TraceCallStack& texParameterTrace = gl.GetTexParameterTrace();
362   texParameterTrace.Reset();
363   texParameterTrace.Enable( true );
364
365   application.SendNotification();
366   application.Render();
367
368   texParameterTrace.Enable( false );
369
370   // Verify gl state
371
372   // There are two calls to TexParameteri when the texture is first created
373   // Texture mag filter is not called as the first time set it uses the system default
374   DALI_TEST_EQUALS( texParameterTrace.CountMethod( "TexParameteri" ), 3, TEST_LOCATION);
375
376   std::stringstream out;
377   out << GL_TEXTURE_2D << ", " << GL_TEXTURE_WRAP_S << ", " << GL_CLAMP_TO_EDGE;
378   DALI_TEST_EQUALS( texParameterTrace.TestMethodAndParams(0, "TexParameteri", out.str()), true, TEST_LOCATION);
379
380   out.str("");
381   out << GL_TEXTURE_2D << ", " << GL_TEXTURE_WRAP_T << ", " << GL_CLAMP_TO_EDGE;
382   DALI_TEST_EQUALS( texParameterTrace.TestMethodAndParams(1, "TexParameteri", out.str()), true, TEST_LOCATION);
383
384   texParameterTrace.Reset();
385   texParameterTrace.Enable( true );
386
387   sampler.SetWrapMode( Sampler::CLAMP_TO_EDGE, Sampler::CLAMP_TO_EDGE );
388
389   // Flush the queue and render once
390   application.SendNotification();
391   application.Render();
392
393   texParameterTrace.Enable( false );
394
395   // Verify gl state
396
397   // Should not make any calls when settings are the same
398   DALI_TEST_EQUALS( texParameterTrace.CountMethod( "TexParameteri" ), 0, TEST_LOCATION);
399
400   //Todo: Test the other wrap mode ( REPEAT, MIRRORED_REPEAT )  , currently not support!!
401
402   END_TEST;
403 }
404
405 int UtcSamplerSetAffectsTransparency(void)
406 {
407   TestApplication application;
408
409   Image image = BufferImage::New( 64, 64, Pixel::RGBA8888 );
410   Sampler sampler = Sampler::New(image, "sTexture");
411
412   Material material = CreateMaterial(1.0f);
413   material.AddSampler( sampler );
414
415   Geometry geometry = CreateQuadGeometry();
416   Renderer renderer = Renderer::New( geometry, material );
417   Actor actor = Actor::New();
418   actor.AddRenderer(renderer);
419   actor.SetParentOrigin( ParentOrigin::CENTER );
420   actor.SetSize(400, 400);
421   Stage::GetCurrent().Add( actor );
422
423   TestGlAbstraction& gl = application.GetGlAbstraction();
424
425   // Test SetAffectsTransparency( false )
426   sampler.SetAffectsTransparency( false );
427
428   gl.EnableCullFaceCallTrace(true);
429   application.SendNotification();
430   application.Render();
431
432   TraceCallStack& glEnableStack = gl.GetCullFaceTrace();
433   std::ostringstream blendStr;
434   blendStr << GL_BLEND;
435   DALI_TEST_CHECK( ! glEnableStack.FindMethodAndParams( "Enable", blendStr.str().c_str() ) );
436
437   // Test SetAffectsTransparency( true )
438   sampler.SetAffectsTransparency( true );
439
440   glEnableStack.Reset();
441   gl.EnableCullFaceCallTrace(true);
442   application.SendNotification();
443   application.Render();
444
445   DALI_TEST_CHECK( glEnableStack.FindMethodAndParams( "Enable", blendStr.str().c_str() ) );
446
447   END_TEST;
448 }