433520fb403ea9df1cd53148f726ed7ae9e96268
[platform/core/uifw/dali-core.git] / automated-tests / src / dali-devel / utc-Dali-Material.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 namespace
26 {
27 void TestConstraintNoBlue( Vector4& current, const PropertyInputContainer& inputs )
28 {
29   current.b = 0.0f;
30 }
31 }
32
33
34 void material_test_startup(void)
35 {
36   test_return_value = TET_UNDEF;
37 }
38
39 void material_test_cleanup(void)
40 {
41   test_return_value = TET_PASS;
42 }
43
44 int UtcDaliMaterialNew01(void)
45 {
46   TestApplication application;
47
48   Shader shader = Shader::New("vertexSrc", "fragmentSrc");
49   Material material = Material::New(shader);
50
51   DALI_TEST_CHECK( material );
52   END_TEST;
53 }
54
55 int UtcDaliMaterialNew02(void)
56 {
57   TestApplication application;
58   Material material;
59   DALI_TEST_CHECK( !material );
60   END_TEST;
61 }
62
63 int UtcDaliMaterialCopyConstructor(void)
64 {
65   TestApplication application;
66
67   Shader shader = Shader::New("vertexSrc", "fragmentSrc");
68   Image image = BufferImage::New(32, 32, Pixel::RGBA8888);
69   Sampler sampler = Sampler::New(image, "sTexture");
70   Material material = Material::New(shader);
71   material.AddSampler( sampler );
72
73   Material materialCopy(material);
74
75   DALI_TEST_CHECK( materialCopy );
76   DALI_TEST_EQUALS( materialCopy.GetSamplerAt(0), sampler, TEST_LOCATION );
77
78   END_TEST;
79 }
80
81 int UtcDaliMaterialAssignmentOperator(void)
82 {
83   TestApplication application;
84
85   Shader shader = Shader::New("vertexSrc", "fragmentSrc");
86   Image image = BufferImage::New(32, 32, Pixel::RGBA8888);
87   Sampler sampler = Sampler::New(image, "sTexture");
88   Material material = Material::New(shader);
89   material.AddSampler( sampler );
90
91   Material material2;
92   DALI_TEST_CHECK( !material2 );
93
94   material2 = material;
95   DALI_TEST_CHECK( material2 );
96   DALI_TEST_EQUALS( material2.GetSamplerAt(0), sampler, TEST_LOCATION );
97
98   END_TEST;
99 }
100
101 int UtcDaliMaterialDownCast01(void)
102 {
103   TestApplication application;
104   Shader shader = Shader::New("vertexSrc", "fragmentSrc");
105   Material material = Material::New(shader);
106
107   BaseHandle handle(material);
108   Material material2 = Material::DownCast(handle);
109   DALI_TEST_CHECK( material2 );
110
111   END_TEST;
112 }
113
114 int UtcDaliMaterialDownCast02(void)
115 {
116   TestApplication application;
117
118   Handle handle = Handle::New(); // Create a custom object
119   Material material = Material::DownCast(handle);
120   DALI_TEST_CHECK( !material );
121   END_TEST;
122 }
123
124 int UtcDaliMaterialSetShader(void)
125 {
126   TestApplication application;
127
128   tet_infoline("Test SetShader(shader) ");
129
130   Shader shader1 = Shader::New( "vertexSrc1", "fragmentSrc1" );
131   shader1.RegisterProperty( "uFadeColor", Color::CYAN );
132
133   Shader shader2 = Shader::New( "vertexSrc1", "fragmentSrc1" );
134   shader2.RegisterProperty( "uFadeColor", Color::MAGENTA );
135
136   // shader1
137   Material material = Material::New(shader1);
138
139   Geometry geometry = CreateQuadGeometry();
140   Renderer renderer = Renderer::New( geometry, material );
141
142   Actor actor = Actor::New();
143   actor.AddRenderer(renderer);
144   actor.SetSize(400, 400);
145   Stage::GetCurrent().Add(actor);
146
147   TestGlAbstraction& gl = application.GetGlAbstraction();
148   application.SendNotification();
149   application.Render(0);
150   Vector4 actualValue(Vector4::ZERO);
151   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
152   DALI_TEST_EQUALS( actualValue, Color::CYAN, TEST_LOCATION );
153
154   // shader2
155   material.SetShader( shader2 );
156
157   application.SendNotification();
158   application.Render(0);
159   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
160   DALI_TEST_EQUALS( actualValue, Color::MAGENTA, TEST_LOCATION );
161
162   // shader1
163   material.SetShader( shader1 );
164
165   application.SendNotification();
166   application.Render(0);
167   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
168   DALI_TEST_EQUALS( actualValue, Color::CYAN, TEST_LOCATION );
169
170   END_TEST;
171 }
172
173 int UtcDaliMaterialGetShader(void)
174 {
175   TestApplication application;
176
177   tet_infoline("Test GetShader() ");
178
179   Shader shader1 = Shader::New( "vertexSrc1", "fragmentSrc1" );
180   Shader shader2 = Shader::New( "vertexSrc1", "fragmentSrc1" );
181
182   // shader1
183   Material material = Material::New(shader1);
184   DALI_TEST_EQUALS( shader1, material.GetShader(), TEST_LOCATION );
185
186   // shader2
187   material.SetShader( shader2 );
188   DALI_TEST_EQUALS( shader2, material.GetShader(), TEST_LOCATION );
189
190   // shader1
191   material.SetShader( shader1 );
192   DALI_TEST_EQUALS( shader1, material.GetShader(), TEST_LOCATION );
193
194   END_TEST;
195 }
196
197 int UtcDaliMaterialAddSampler(void)
198 {
199   TestApplication application;
200
201   tet_infoline("Test AddSampler(sampler)");
202
203   Image image = BufferImage::New(32, 32, Pixel::RGBA8888);
204   Sampler sampler1 = Sampler::New(image, "sTexture1");
205   Sampler sampler2 = Sampler::New(image, "sTexture2");
206
207   Material material = CreateMaterial(0.5f);
208
209   Geometry geometry = CreateQuadGeometry();
210   Renderer renderer = Renderer::New( geometry, material );
211   Actor actor = Actor::New();
212   actor.AddRenderer(renderer);
213   actor.SetParentOrigin( ParentOrigin::CENTER );
214   actor.SetSize(400, 400);
215   Stage::GetCurrent().Add( actor );
216
217   TestGlAbstraction& gl = application.GetGlAbstraction();
218   int textureUnit=-1;
219
220   material.AddSampler( sampler1 );
221   application.SendNotification();
222   application.Render();
223   DALI_TEST_CHECK( gl.GetUniformValue<int>( "sTexture1", textureUnit ) );
224   DALI_TEST_EQUALS( textureUnit, 0, TEST_LOCATION );
225
226   material.AddSampler( sampler2 );
227   application.SendNotification();
228   application.Render();
229   DALI_TEST_CHECK( gl.GetUniformValue<int>( "sTexture2", textureUnit ) );
230   DALI_TEST_EQUALS( textureUnit, 1, TEST_LOCATION );
231
232   END_TEST;
233 }
234
235 int UtcDaliMaterialGetNumberOfSampler(void)
236 {
237   TestApplication application;
238
239   tet_infoline("Test GetNumberOfSampler()");
240
241   Image image = BufferImage::New(32, 32, Pixel::RGBA8888);
242   Sampler sampler0 = Sampler::New(image, "sTexture0");
243   Sampler sampler1 = Sampler::New(image, "sTexture1");
244   Sampler sampler2 = Sampler::New(image, "sTexture2");
245   Sampler sampler3 = Sampler::New(image, "sTexture3");
246   Sampler sampler4 = Sampler::New(image, "sTexture4");
247
248   Material material = CreateMaterial(0.5f);
249
250   Geometry geometry = CreateQuadGeometry();
251   Renderer renderer = Renderer::New( geometry, material );
252   Actor actor = Actor::New();
253   actor.AddRenderer(renderer);
254   actor.SetParentOrigin( ParentOrigin::CENTER );
255   actor.SetSize(400, 400);
256   Stage::GetCurrent().Add( actor );
257
258   material.AddSampler( sampler0 );
259   material.AddSampler( sampler1 );
260   DALI_TEST_EQUALS( material.GetNumberOfSamplers(), 2u, TEST_LOCATION );
261
262   material.AddSampler( sampler2 );
263   material.AddSampler( sampler3 );
264   material.AddSampler( sampler4 );
265   DALI_TEST_EQUALS( material.GetNumberOfSamplers(), 5u, TEST_LOCATION );
266
267   material.RemoveSampler(3); // remove sampler3
268   DALI_TEST_EQUALS( material.GetNumberOfSamplers(), 4u, TEST_LOCATION );
269
270   material.RemoveSampler(3); // remove sampler4
271   material.RemoveSampler(0); // remove sampler0
272   DALI_TEST_EQUALS( material.GetNumberOfSamplers(), 2u, TEST_LOCATION );
273
274   END_TEST;
275 }
276
277 int UtcDaliMaterialRemoveSampler(void)
278 {
279   TestApplication application;
280
281   tet_infoline("Test RemoveSampler(index)");
282   Image image = BufferImage::New(32, 32, Pixel::RGBA8888);
283   Sampler sampler1 = Sampler::New(image, "sTexture1");
284   Sampler sampler2 = Sampler::New(image, "sTexture2");
285
286   Material material = CreateMaterial(0.5f);
287
288   Geometry geometry = CreateQuadGeometry();
289   Renderer renderer = Renderer::New( geometry, material );
290   Actor actor = Actor::New();
291   actor.AddRenderer(renderer);
292   actor.SetParentOrigin( ParentOrigin::CENTER );
293   actor.SetSize(400, 400);
294   Stage::GetCurrent().Add( actor );
295
296   material.AddSampler( sampler1 );
297   material.AddSampler( sampler2 );
298
299   TestGlAbstraction& gl = application.GetGlAbstraction();
300   int textureUnit=-1;
301   application.SendNotification();
302   application.Render();
303   DALI_TEST_CHECK( gl.GetUniformValue<int>( "sTexture1", textureUnit ) );
304   DALI_TEST_EQUALS( textureUnit, 0, TEST_LOCATION );
305   DALI_TEST_CHECK( gl.GetUniformValue<int>( "sTexture2", textureUnit ) );
306   DALI_TEST_EQUALS( textureUnit, 1, TEST_LOCATION );
307
308   material.RemoveSampler(0); // remove sampler1
309   application.SendNotification();
310   application.Render();
311   // Todo: test the sampler is removed from gl, cannot pass this test with current implementation
312   //DALI_TEST_CHECK( ! gl.GetUniformValue<int>( "sTexture1", textureUnit ) );
313   DALI_TEST_EQUALS( material.GetNumberOfSamplers(), 1u, TEST_LOCATION );
314
315   material.RemoveSampler(0); // remove sampler2
316   application.SendNotification();
317   application.Render();
318   // Todo: test the sampler is removed from gl, cannot pass this test with current implementation
319   //DALI_TEST_CHECK( ! gl.GetUniformValue<int>( "sTexture2", textureUnit ) );
320   DALI_TEST_EQUALS( material.GetNumberOfSamplers(), 0u, TEST_LOCATION );
321
322   END_TEST;
323 }
324
325 int UtcDaliMaterialGetSamplerAt(void)
326 {
327   TestApplication application;
328
329   tet_infoline("Test GetSamplerAt(index)");
330
331   Image image = BufferImage::New(16, 16, Pixel::RGBA8888);
332   Sampler sampler1 = Sampler::New(image, "sTexture1");
333   Sampler sampler2 = Sampler::New(image, "sTexture2");
334   Sampler sampler3 = Sampler::New(image, "sTexture3");
335
336   Material material = CreateMaterial(0.5f);
337   material.AddSampler( sampler1 );
338   material.AddSampler( sampler2 );
339   material.AddSampler( sampler3 );
340
341   Geometry geometry = CreateQuadGeometry();
342   Renderer renderer = Renderer::New( geometry, material );
343   Actor actor = Actor::New();
344   actor.AddRenderer(renderer);
345   actor.SetParentOrigin( ParentOrigin::CENTER );
346   actor.SetSize(400, 400);
347   Stage::GetCurrent().Add( actor );
348
349   application.SendNotification();
350   application.Render();
351
352   DALI_TEST_EQUALS( material.GetSamplerAt( 0 ), sampler1, TEST_LOCATION );
353   DALI_TEST_EQUALS( material.GetSamplerAt( 1 ), sampler2, TEST_LOCATION );
354   DALI_TEST_EQUALS( material.GetSamplerAt( 2 ), sampler3, TEST_LOCATION );
355
356   Sampler sampler = material.GetSamplerAt( 1 );
357   DALI_TEST_EQUALS( sampler.GetImage().GetWidth(), 16u, TEST_LOCATION );
358
359   END_TEST;
360 }
361
362 int UtcDaliMaterialSetFaceCullingMode(void)
363 {
364   TestApplication application;
365
366   tet_infoline("Test SetFaceCullingMode(cullingMode)");
367   Geometry geometry = CreateQuadGeometry();
368   Material material = CreateMaterial(0.5f);
369   Renderer renderer = Renderer::New( geometry, material );
370
371   Actor actor = Actor::New();
372   actor.AddRenderer(renderer);
373   actor.SetSize(400, 400);
374   Stage::GetCurrent().Add(actor);
375
376   TestGlAbstraction& gl = application.GetGlAbstraction();
377   TraceCallStack& cullFaceStack = gl.GetCullFaceTrace();
378   cullFaceStack.Reset();
379   gl.EnableCullFaceCallTrace(true);
380
381   material.SetFaceCullingMode( Material::CULL_BACK_AND_FRONT);
382   application.SendNotification();
383   application.Render();
384
385   // Todo: test the glCullFace(GL_FRONT_AND_BACK) is actually been called, cannot pass this test with current implementation
386   DALI_TEST_EQUALS( cullFaceStack.CountMethod( "CullFace" ), 0, TEST_LOCATION);
387   //string parameter("GL_FRONT_AND_BACK" );
388   //DALI_TEST_CHECK( cullFaceStack.TestMethodAndParams(0, "CullFace", parameter) );
389
390   END_TEST;
391 }
392
393 int UtcDaliMaterialBlendingOptions01(void)
394 {
395   TestApplication application;
396
397   tet_infoline("Test SetBlendFunc(src, dest) ");
398
399   Geometry geometry = CreateQuadGeometry();
400   Material material = CreateMaterial(0.5f);
401   Renderer renderer = Renderer::New( geometry, material );
402
403   Actor actor = Actor::New();
404   actor.AddRenderer(renderer);
405   actor.SetSize(400, 400);
406   Stage::GetCurrent().Add(actor);
407
408   material.SetBlendFunc(BlendingFactor::ONE_MINUS_SRC_COLOR, BlendingFactor::SRC_ALPHA_SATURATE);
409
410   // Test that Set was successful:
411   {
412     BlendingFactor::Type srcFactorRgb( BlendingFactor::ZERO );
413     BlendingFactor::Type destFactorRgb( BlendingFactor::ZERO );
414     BlendingFactor::Type srcFactorAlpha( BlendingFactor::ZERO );
415     BlendingFactor::Type destFactorAlpha( BlendingFactor::ZERO );
416     material.GetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
417
418     DALI_TEST_EQUALS( BlendingFactor::ONE_MINUS_SRC_COLOR, srcFactorRgb,    TEST_LOCATION );
419     DALI_TEST_EQUALS( BlendingFactor::SRC_ALPHA_SATURATE,  destFactorRgb,   TEST_LOCATION );
420     DALI_TEST_EQUALS( BlendingFactor::ONE_MINUS_SRC_COLOR, srcFactorAlpha,  TEST_LOCATION );
421     DALI_TEST_EQUALS( BlendingFactor::SRC_ALPHA_SATURATE,  destFactorAlpha, TEST_LOCATION );
422   }
423
424   application.SendNotification();
425   application.Render();
426
427   TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
428
429   DALI_TEST_EQUALS( (GLenum)GL_ONE_MINUS_SRC_COLOR, glAbstraction.GetLastBlendFuncSrcRgb(),   TEST_LOCATION );
430   DALI_TEST_EQUALS( (GLenum)GL_SRC_ALPHA_SATURATE,  glAbstraction.GetLastBlendFuncDstRgb(),   TEST_LOCATION );
431   DALI_TEST_EQUALS( (GLenum)GL_ONE_MINUS_SRC_COLOR, glAbstraction.GetLastBlendFuncSrcAlpha(), TEST_LOCATION );
432   DALI_TEST_EQUALS( (GLenum)GL_SRC_ALPHA_SATURATE,  glAbstraction.GetLastBlendFuncDstAlpha(), TEST_LOCATION );
433
434   END_TEST;
435 }
436
437 int UtcDaliMaterialBlendingOptions02(void)
438 {
439   TestApplication application;
440
441   tet_infoline("Test SetBlendFunc(srcRgb, destRgb, srcAlpha, destAlpha) ");
442
443   Geometry geometry = CreateQuadGeometry();
444   Material material = CreateMaterial(0.5f);
445   Renderer renderer = Renderer::New( geometry, material );
446
447   Actor actor = Actor::New();
448   actor.AddRenderer(renderer);
449   actor.SetSize(400, 400);
450   Stage::GetCurrent().Add(actor);
451
452   material.SetBlendFunc( BlendingFactor::CONSTANT_COLOR, BlendingFactor::ONE_MINUS_CONSTANT_COLOR,
453                          BlendingFactor::CONSTANT_ALPHA, BlendingFactor::ONE_MINUS_CONSTANT_ALPHA );
454
455   // Test that Set was successful:
456   {
457     BlendingFactor::Type srcFactorRgb( BlendingFactor::ZERO );
458     BlendingFactor::Type destFactorRgb( BlendingFactor::ZERO );
459     BlendingFactor::Type srcFactorAlpha( BlendingFactor::ZERO );
460     BlendingFactor::Type destFactorAlpha( BlendingFactor::ZERO );
461     material.GetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
462
463     DALI_TEST_EQUALS( BlendingFactor::CONSTANT_COLOR,            srcFactorRgb,    TEST_LOCATION );
464     DALI_TEST_EQUALS( BlendingFactor::ONE_MINUS_CONSTANT_COLOR,  destFactorRgb,   TEST_LOCATION );
465     DALI_TEST_EQUALS( BlendingFactor::CONSTANT_ALPHA,            srcFactorAlpha,  TEST_LOCATION );
466     DALI_TEST_EQUALS( BlendingFactor::ONE_MINUS_CONSTANT_ALPHA,  destFactorAlpha, TEST_LOCATION );
467   }
468
469   application.SendNotification();
470   application.Render();
471
472   TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
473   DALI_TEST_EQUALS( (GLenum)GL_CONSTANT_COLOR,           glAbstraction.GetLastBlendFuncSrcRgb(),   TEST_LOCATION );
474   DALI_TEST_EQUALS( (GLenum)GL_ONE_MINUS_CONSTANT_COLOR, glAbstraction.GetLastBlendFuncDstRgb(),   TEST_LOCATION );
475   DALI_TEST_EQUALS( (GLenum)GL_CONSTANT_ALPHA,           glAbstraction.GetLastBlendFuncSrcAlpha(), TEST_LOCATION );
476   DALI_TEST_EQUALS( (GLenum)GL_ONE_MINUS_CONSTANT_ALPHA, glAbstraction.GetLastBlendFuncDstAlpha(), TEST_LOCATION );
477
478   END_TEST;
479 }
480
481
482
483 int UtcDaliMaterialBlendingOptions03(void)
484 {
485   TestApplication application;
486
487   tet_infoline("Test GetBlendEquation() defaults ");
488
489   Geometry geometry = CreateQuadGeometry();
490   Material material = CreateMaterial(0.5f);
491   Renderer renderer = Renderer::New( geometry, material );
492
493   Actor actor = Actor::New();
494   actor.AddRenderer(renderer);
495   actor.SetSize(400, 400);
496   Stage::GetCurrent().Add(actor);
497
498   // Test the defaults as documented in blending.h
499   {
500     BlendingEquation::Type equationRgb( BlendingEquation::SUBTRACT );
501     BlendingEquation::Type equationAlpha( BlendingEquation::SUBTRACT );
502     material.GetBlendEquation( equationRgb, equationAlpha );
503     DALI_TEST_EQUALS( BlendingEquation::ADD, equationRgb, TEST_LOCATION );
504     DALI_TEST_EQUALS( BlendingEquation::ADD, equationAlpha, TEST_LOCATION );
505   }
506
507   END_TEST;
508 }
509
510
511 int UtcDaliMaterialBlendingOptions04(void)
512 {
513   TestApplication application;
514
515   tet_infoline("Test SetBlendEquation() ");
516
517   Geometry geometry = CreateQuadGeometry();
518   Material material = CreateMaterial(0.5f);
519   Renderer renderer = Renderer::New( geometry, material );
520
521   Actor actor = Actor::New();
522   actor.AddRenderer(renderer);
523   actor.SetSize(400, 400);
524   Stage::GetCurrent().Add(actor);
525
526   // Test the single blending equation setting
527   {
528     material.SetBlendEquation( BlendingEquation::REVERSE_SUBTRACT );
529     BlendingEquation::Type equationRgba( BlendingEquation::SUBTRACT );
530     material.GetBlendEquation( equationRgba, equationRgba );
531     DALI_TEST_EQUALS( BlendingEquation::REVERSE_SUBTRACT, equationRgba, TEST_LOCATION );
532   }
533
534   material.SetBlendEquation( BlendingEquation::REVERSE_SUBTRACT, BlendingEquation::REVERSE_SUBTRACT );
535
536   // Test that Set was successful
537   {
538     BlendingEquation::Type equationRgb( BlendingEquation::SUBTRACT );
539     BlendingEquation::Type equationAlpha( BlendingEquation::SUBTRACT );
540     material.GetBlendEquation( equationRgb, equationAlpha );
541     DALI_TEST_EQUALS( BlendingEquation::REVERSE_SUBTRACT, equationRgb, TEST_LOCATION );
542     DALI_TEST_EQUALS( BlendingEquation::REVERSE_SUBTRACT, equationAlpha, TEST_LOCATION );
543   }
544
545   // Render & check GL commands
546   application.SendNotification();
547   application.Render();
548
549   TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
550   DALI_TEST_EQUALS( (GLenum)GL_FUNC_REVERSE_SUBTRACT, glAbstraction.GetLastBlendEquationRgb(),   TEST_LOCATION );
551   DALI_TEST_EQUALS( (GLenum)GL_FUNC_REVERSE_SUBTRACT, glAbstraction.GetLastBlendEquationAlpha(), TEST_LOCATION );
552
553   END_TEST;
554 }
555
556 int UtcDaliMaterialSetBlendMode01(void)
557 {
558   TestApplication application;
559
560   tet_infoline("Test setting the blend mode to on with an opaque color renders with blending enabled");
561
562   Geometry geometry = CreateQuadGeometry();
563   Material material = CreateMaterial(1.0f);
564   Renderer renderer = Renderer::New( geometry, material );
565
566   Actor actor = Actor::New();
567   actor.AddRenderer(renderer);
568   actor.SetSize(400, 400);
569   Stage::GetCurrent().Add(actor);
570
571   material.SetBlendMode(BlendingMode::ON);
572
573   TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
574   glAbstraction.EnableCullFaceCallTrace(true);
575
576   application.SendNotification();
577   application.Render();
578
579   TraceCallStack& glEnableStack = glAbstraction.GetCullFaceTrace();
580   std::ostringstream blendStr;
581   blendStr << GL_BLEND;
582   DALI_TEST_CHECK( glEnableStack.FindMethodAndParams( "Enable", blendStr.str().c_str() ) );
583
584   END_TEST;
585 }
586
587
588 int UtcDaliMaterialSetBlendMode02(void)
589 {
590   TestApplication application;
591
592   tet_infoline("Test setting the blend mode to off with a transparent color renders with blending disabled (and not enabled)");
593
594   Geometry geometry = CreateQuadGeometry();
595   Material material = CreateMaterial(0.5f);
596   Renderer renderer = Renderer::New( geometry, material );
597
598   Actor actor = Actor::New();
599   actor.AddRenderer(renderer);
600   actor.SetSize(400, 400);
601   Stage::GetCurrent().Add(actor);
602
603   material.SetBlendMode(BlendingMode::OFF);
604
605   TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
606   glAbstraction.EnableCullFaceCallTrace(true);
607
608   application.SendNotification();
609   application.Render();
610
611   TraceCallStack& glEnableStack = glAbstraction.GetCullFaceTrace();
612   std::ostringstream blendStr;
613   blendStr << GL_BLEND;
614   DALI_TEST_CHECK( ! glEnableStack.FindMethodAndParams( "Enable", blendStr.str().c_str() ) );
615
616   END_TEST;
617 }
618
619 int UtcDaliMaterialSetBlendMode03(void)
620 {
621   TestApplication application;
622
623   tet_infoline("Test setting the blend mode to auto with a transparent material color renders with blending enabled");
624
625   Geometry geometry = CreateQuadGeometry();
626   Material material = CreateMaterial(0.5f);
627   Renderer renderer = Renderer::New( geometry, material );
628
629   Actor actor = Actor::New();
630   actor.AddRenderer(renderer);
631   actor.SetSize(400, 400);
632   Stage::GetCurrent().Add(actor);
633
634   material.SetBlendMode(BlendingMode::AUTO);
635
636   TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
637   glAbstraction.EnableCullFaceCallTrace(true);
638
639   application.SendNotification();
640   application.Render();
641
642   TraceCallStack& glEnableStack = glAbstraction.GetCullFaceTrace();
643   std::ostringstream blendStr;
644   blendStr << GL_BLEND;
645   DALI_TEST_CHECK( glEnableStack.FindMethodAndParams( "Enable", blendStr.str().c_str() ) );
646
647   END_TEST;
648 }
649
650 int UtcDaliMaterialSetBlendMode04(void)
651 {
652   TestApplication application;
653
654   tet_infoline("Test setting the blend mode to auto with an opaque color renders with blending disabled");
655
656   Geometry geometry = CreateQuadGeometry();
657   Material material = CreateMaterial(1.0f);
658   Renderer renderer = Renderer::New( geometry, material );
659
660   Actor actor = Actor::New();
661   actor.AddRenderer(renderer);
662   actor.SetSize(400, 400);
663   Stage::GetCurrent().Add(actor);
664
665   material.SetBlendMode(BlendingMode::AUTO);
666
667   TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
668   glAbstraction.EnableCullFaceCallTrace(true);
669
670   application.SendNotification();
671   application.Render();
672
673   TraceCallStack& glEnableStack = glAbstraction.GetCullFaceTrace();
674   std::ostringstream blendStr;
675   blendStr << GL_BLEND;
676   DALI_TEST_CHECK( ! glEnableStack.FindMethodAndParams( "Enable", blendStr.str().c_str() ) );
677
678   END_TEST;
679 }
680
681 int UtcDaliMaterialSetBlendMode04b(void)
682 {
683   TestApplication application;
684
685   tet_infoline("Test setting the blend mode to auto with an opaque material color and a transparent actor color renders with blending enabled");
686
687   Geometry geometry = CreateQuadGeometry();
688   Material material = CreateMaterial(1.0f);
689   Renderer renderer = Renderer::New( geometry, material );
690
691   Actor actor = Actor::New();
692   actor.AddRenderer(renderer);
693   actor.SetSize(400, 400);
694   actor.SetColor( Vector4(1.0f, 0.0f, 1.0f, 0.5f) );
695   Stage::GetCurrent().Add(actor);
696
697   material.SetBlendMode(BlendingMode::AUTO);
698
699   TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
700   glAbstraction.EnableCullFaceCallTrace(true);
701
702   application.SendNotification();
703   application.Render();
704
705   TraceCallStack& glEnableStack = glAbstraction.GetCullFaceTrace();
706   std::ostringstream blendStr;
707   blendStr << GL_BLEND;
708   DALI_TEST_CHECK( glEnableStack.FindMethodAndParams( "Enable", blendStr.str().c_str() ) );
709
710   END_TEST;
711 }
712
713 int UtcDaliMaterialSetBlendMode04c(void)
714 {
715   TestApplication application;
716
717   tet_infoline("Test setting the blend mode to auto with an opaque material color and an opaque actor color renders with blending disabled");
718
719   Geometry geometry = CreateQuadGeometry();
720   Material material = CreateMaterial(1.0f);
721   Renderer renderer = Renderer::New( geometry, material );
722
723   Actor actor = Actor::New();
724   actor.AddRenderer(renderer);
725   actor.SetSize(400, 400);
726   actor.SetColor( Color::MAGENTA );
727   Stage::GetCurrent().Add(actor);
728
729   material.SetBlendMode(BlendingMode::AUTO);
730
731   TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
732   glAbstraction.EnableCullFaceCallTrace(true);
733
734   application.SendNotification();
735   application.Render();
736
737   TraceCallStack& glEnableStack = glAbstraction.GetCullFaceTrace();
738   std::ostringstream blendStr;
739   blendStr << GL_BLEND;
740   DALI_TEST_CHECK( ! glEnableStack.FindMethodAndParams( "Enable", blendStr.str().c_str() ) );
741
742   END_TEST;
743 }
744
745 int UtcDaliMaterialSetBlendMode05(void)
746 {
747   TestApplication application;
748
749   tet_infoline("Test setting the blend mode to auto with an opaque color and an image with an alpha channel renders with blending enabled");
750
751   Geometry geometry = CreateQuadGeometry();
752   BufferImage image = BufferImage::New( 40, 40, Pixel::RGBA8888 );
753   Material material = CreateMaterial(1.0f, image);
754   Renderer renderer = Renderer::New( geometry, material );
755
756   Actor actor = Actor::New();
757   actor.AddRenderer(renderer);
758   actor.SetSize(400, 400);
759   Stage::GetCurrent().Add(actor);
760
761   material.SetBlendMode(BlendingMode::AUTO);
762
763   TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
764   glAbstraction.EnableCullFaceCallTrace(true);
765
766   application.SendNotification();
767   application.Render();
768
769   TraceCallStack& glEnableStack = glAbstraction.GetCullFaceTrace();
770   std::ostringstream blendStr;
771   blendStr << GL_BLEND;
772   DALI_TEST_CHECK( glEnableStack.FindMethodAndParams( "Enable", blendStr.str().c_str() ) );
773
774   END_TEST;
775 }
776
777 int UtcDaliMaterialSetBlendMode06(void)
778 {
779   TestApplication application;
780   tet_infoline("Test setting the blend mode to auto with an opaque color and an image without an alpha channel and a shader with the hint OUTPUT_IS_TRANSPARENT renders with blending enabled");
781
782   Geometry geometry = CreateQuadGeometry();
783   Shader shader = Shader::New( "vertexSrc", "fragmentSrc", Shader::HINT_OUTPUT_IS_TRANSPARENT );
784   Material material = Material::New(shader);
785   material.SetProperty(Material::Property::COLOR, Color::WHITE);
786
787   Renderer renderer = Renderer::New( geometry, material );
788
789   Actor actor = Actor::New();
790   actor.AddRenderer(renderer);
791   actor.SetSize(400, 400);
792   Stage::GetCurrent().Add(actor);
793
794   material.SetBlendMode(BlendingMode::AUTO);
795
796   TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
797   glAbstraction.EnableCullFaceCallTrace(true);
798
799   application.SendNotification();
800   application.Render();
801
802   TraceCallStack& glEnableStack = glAbstraction.GetCullFaceTrace();
803   std::ostringstream blendStr;
804   blendStr << GL_BLEND;
805   DALI_TEST_CHECK( glEnableStack.FindMethodAndParams( "Enable", blendStr.str().c_str() ) );
806
807   END_TEST;
808 }
809
810
811 //Todo: test the Shader::HINT_OUTPUT_IS_OPAQUE would disable the blending, the test cannot pass with current implementation
812 /*int UtcDaliMaterialSetBlendMode07(void)
813 {
814   TestApplication application;
815   tet_infoline("Test setting the blend mode to auto with a transparent color and an image without an alpha channel and a shader with the hint OUTPUT_IS_OPAQUE renders with blending disabled");
816   Geometry geometry = CreateQuadGeometry();
817   Shader shader = Shader::New( "vertexSrc", "fragmentSrc", Shader::HINT_OUTPUT_IS_OPAQUE );
818   Material material = Material::New(shader);
819   material.SetProperty(Material::Property::COLOR, Color::TRANSPARENT);
820
821   Renderer renderer = Renderer::New( geometry, material );
822
823   Actor actor = Actor::New();
824   actor.AddRenderer(renderer);
825   actor.SetSize(400, 400);
826   Stage::GetCurrent().Add(actor);
827
828   material.SetBlendMode(BlendingMode::AUTO);
829
830   TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
831   glAbstraction.EnableCullFaceCallTrace(true);
832
833   application.SendNotification();
834   application.Render();
835
836   TraceCallStack& glEnableStack = glAbstraction.GetCullFaceTrace();
837   std::ostringstream blendStr;
838   blendStr << GL_BLEND;
839   DALI_TEST_CHECK( ! glEnableStack.FindMethodAndParams( "Enable", blendStr.str().c_str() ) );
840
841   END_TEST;
842 }*/
843
844 int UtcDaliMaterialSetBlendMode08(void)
845 {
846   TestApplication application;
847   tet_infoline("Test setting the blend mode to auto with an opaque color and an image without an alpha channel and a shader with the hint OUTPUT_IS_OPAQUE renders with blending disabled");
848
849   Geometry geometry = CreateQuadGeometry();
850   Shader shader = Shader::New( "vertexSrc", "fragmentSrc", Shader::HINT_OUTPUT_IS_OPAQUE );
851   Material material = Material::New(shader);
852   material.SetProperty(Material::Property::COLOR, Color::WHITE);
853   BufferImage image = BufferImage::New( 50, 50, Pixel::RGB888 );
854   Sampler sampler = Sampler::New( image, "sTexture" );
855   material.AddSampler( sampler );
856   Renderer renderer = Renderer::New( geometry, material );
857
858   Actor actor = Actor::New();
859   actor.AddRenderer(renderer);
860   actor.SetSize(400, 400);
861   Stage::GetCurrent().Add(actor);
862
863   material.SetBlendMode(BlendingMode::AUTO);
864
865   TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
866   glAbstraction.EnableCullFaceCallTrace(true);
867
868   application.SendNotification();
869   application.Render();
870
871   TraceCallStack& glEnableStack = glAbstraction.GetCullFaceTrace();
872   std::ostringstream blendStr;
873   blendStr << GL_BLEND;
874   DALI_TEST_CHECK( ! glEnableStack.FindMethodAndParams( "Enable", blendStr.str().c_str() ) );
875
876   END_TEST;
877 }
878
879 int UtcDaliMaterialGetBlendMode(void)
880 {
881   TestApplication application;
882
883   tet_infoline("Test GetBlendMode()");
884
885   Shader shader = Shader::New( "vertexSrc", "fragmentSrc", Shader::HINT_OUTPUT_IS_OPAQUE );
886   Material material = Material::New(shader);
887
888   // default value
889   DALI_TEST_EQUALS( material.GetBlendMode(), BlendingMode::OFF, TEST_LOCATION );
890
891   // AUTO
892   material.SetBlendMode(BlendingMode::AUTO);
893   DALI_TEST_EQUALS( material.GetBlendMode(), BlendingMode::AUTO, TEST_LOCATION );
894
895   // ON
896   material.SetBlendMode(BlendingMode::ON);
897   DALI_TEST_EQUALS( material.GetBlendMode(), BlendingMode::ON, TEST_LOCATION );
898
899   // OFF
900   material.SetBlendMode(BlendingMode::OFF);
901   DALI_TEST_EQUALS( material.GetBlendMode(), BlendingMode::OFF, TEST_LOCATION );
902
903   END_TEST;
904 }
905
906 int UtcDaliMaterialSetBlendColor(void)
907 {
908   TestApplication application;
909
910   tet_infoline("Test SetBlendColor(color)");
911
912   Geometry geometry = CreateQuadGeometry();
913   Shader shader = Shader::New( "vertexSrc", "fragmentSrc", Shader::HINT_OUTPUT_IS_OPAQUE );
914   Material material = Material::New(shader);
915   material.SetProperty(Material::Property::COLOR, Color::WHITE);
916   BufferImage image = BufferImage::New( 50, 50, Pixel::RGBA8888 );
917   Sampler sampler = Sampler::New( image, "sTexture" );
918   material.AddSampler( sampler );
919   Renderer renderer = Renderer::New( geometry, material );
920
921   Actor actor = Actor::New();
922   actor.AddRenderer(renderer);
923   actor.SetSize(400, 400);
924   Stage::GetCurrent().Add(actor);
925
926   TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
927
928   application.SendNotification();
929   application.Render();
930   DALI_TEST_EQUALS( glAbstraction.GetLastBlendColor(), Color::TRANSPARENT, TEST_LOCATION );
931
932   material.SetBlendColor( Color::MAGENTA );
933   application.SendNotification();
934   application.Render();
935   DALI_TEST_EQUALS( glAbstraction.GetLastBlendColor(), Color::MAGENTA, TEST_LOCATION );
936
937   Vector4 color( 0.1f, 0.2f, 0.3f, 0.4f );
938   material.SetBlendColor( color );
939   application.SendNotification();
940   application.Render();
941   DALI_TEST_EQUALS( glAbstraction.GetLastBlendColor(), color, TEST_LOCATION );
942
943   END_TEST;
944 }
945
946 int UtcDaliMaterialGetBlendColor(void)
947 {
948   TestApplication application;
949
950   tet_infoline("Test GetBlendColor()");
951
952   Shader shader = Shader::New( "vertexSrc", "fragmentSrc", Shader::HINT_OUTPUT_IS_OPAQUE );
953   Material material = Material::New(shader);
954
955   DALI_TEST_EQUALS( material.GetBlendColor(), Color::TRANSPARENT, TEST_LOCATION );
956
957   material.SetBlendColor( Color::MAGENTA );
958   application.SendNotification();
959   application.Render();
960   DALI_TEST_EQUALS( material.GetBlendColor(), Color::MAGENTA, TEST_LOCATION );
961
962   Vector4 color( 0.1f, 0.2f, 0.3f, 0.4f );
963   material.SetBlendColor( color );
964   application.SendNotification();
965   application.Render();
966   DALI_TEST_EQUALS( material.GetBlendColor(), color, TEST_LOCATION );
967
968   END_TEST;
969 }
970
971 int UtcDaliMaterialConstraint(void)
972 {
973   TestApplication application;
974
975   tet_infoline("Test that a custom material property can be constrained");
976
977   Shader shader = Shader::New( "VertexSource", "FragmentSource");
978   Material material = Material::New( shader );
979   material.SetProperty(Material::Property::COLOR, Color::WHITE);
980
981   Geometry geometry = CreateQuadGeometry();
982   Renderer renderer = Renderer::New( geometry, material );
983
984   Actor actor = Actor::New();
985   actor.AddRenderer(renderer);
986   actor.SetSize(400, 400);
987   Stage::GetCurrent().Add(actor);
988
989   Vector4 initialColor = Color::WHITE;
990   Property::Index colorIndex = material.RegisterProperty( "uFadeColor", initialColor );
991
992   application.SendNotification();
993   application.Render(0);
994   DALI_TEST_EQUALS( material.GetProperty<Vector4>(colorIndex), initialColor, TEST_LOCATION );
995
996   // Apply constraint
997   Constraint constraint = Constraint::New<Vector4>( material, colorIndex, TestConstraintNoBlue );
998   constraint.Apply();
999   application.SendNotification();
1000   application.Render(0);
1001
1002   // Expect no blue component in either buffer - yellow
1003   DALI_TEST_EQUALS( material.GetProperty<Vector4>(colorIndex), Color::YELLOW, TEST_LOCATION );
1004   application.Render(0);
1005   DALI_TEST_EQUALS( material.GetProperty<Vector4>(colorIndex), Color::YELLOW, TEST_LOCATION );
1006
1007   material.RemoveConstraints();
1008   material.SetProperty(colorIndex, Color::WHITE );
1009   application.SendNotification();
1010   application.Render(0);
1011   DALI_TEST_EQUALS( material.GetProperty<Vector4>(colorIndex), Color::WHITE, TEST_LOCATION );
1012
1013   END_TEST;
1014 }
1015
1016 int UtcDaliMaterialConstraint02(void)
1017 {
1018   TestApplication application;
1019
1020   tet_infoline("Test that a uniform map material property can be constrained");
1021
1022   Shader shader = Shader::New( "VertexSource", "FragmentSource");
1023   Material material = Material::New( shader );
1024   material.SetProperty(Material::Property::COLOR, Color::WHITE);
1025
1026   Geometry geometry = CreateQuadGeometry();
1027   Renderer renderer = Renderer::New( geometry, material );
1028
1029   Actor actor = Actor::New();
1030   actor.AddRenderer(renderer);
1031   actor.SetSize(400, 400);
1032   Stage::GetCurrent().Add(actor);
1033   application.SendNotification();
1034   application.Render(0);
1035
1036   Vector4 initialColor = Color::WHITE;
1037   Property::Index colorIndex = material.RegisterProperty( "uFadeColor", initialColor );
1038
1039   TestGlAbstraction& gl = application.GetGlAbstraction();
1040
1041   application.SendNotification();
1042   application.Render(0);
1043
1044   Vector4 actualValue(Vector4::ZERO);
1045   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
1046   DALI_TEST_EQUALS( actualValue, initialColor, TEST_LOCATION );
1047
1048   // Apply constraint
1049   Constraint constraint = Constraint::New<Vector4>( material, colorIndex, TestConstraintNoBlue );
1050   constraint.Apply();
1051   application.SendNotification();
1052   application.Render(0);
1053
1054    // Expect no blue component in either buffer - yellow
1055   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
1056   DALI_TEST_EQUALS( actualValue, Color::YELLOW, TEST_LOCATION );
1057
1058   application.Render(0);
1059   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
1060   DALI_TEST_EQUALS( actualValue, Color::YELLOW, TEST_LOCATION );
1061
1062   material.RemoveConstraints();
1063   material.SetProperty(colorIndex, Color::WHITE );
1064   application.SendNotification();
1065   application.Render(0);
1066
1067   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
1068   DALI_TEST_EQUALS( actualValue, Color::WHITE, TEST_LOCATION );
1069
1070   END_TEST;
1071 }
1072
1073
1074
1075 int UtcDaliMaterialAnimatedProperty01(void)
1076 {
1077   TestApplication application;
1078
1079   tet_infoline("Test that a non-uniform material property can be animated");
1080
1081   Shader shader = Shader::New( "VertexSource", "FragmentSource");
1082   Material material = Material::New( shader );
1083   material.SetProperty(Material::Property::COLOR, Color::WHITE);
1084
1085   Geometry geometry = CreateQuadGeometry();
1086   Renderer renderer = Renderer::New( geometry, material );
1087
1088   Actor actor = Actor::New();
1089   actor.AddRenderer(renderer);
1090   actor.SetSize(400, 400);
1091   Stage::GetCurrent().Add(actor);
1092
1093   Vector4 initialColor = Color::WHITE;
1094   Property::Index colorIndex = material.RegisterProperty( "uFadeColor", initialColor );
1095
1096   application.SendNotification();
1097   application.Render(0);
1098   DALI_TEST_EQUALS( material.GetProperty<Vector4>(colorIndex), initialColor, TEST_LOCATION );
1099
1100   Animation  animation = Animation::New(1.0f);
1101   KeyFrames keyFrames = KeyFrames::New();
1102   keyFrames.Add(0.0f, initialColor);
1103   keyFrames.Add(1.0f, Color::TRANSPARENT);
1104   animation.AnimateBetween( Property( material, colorIndex ), keyFrames );
1105   animation.Play();
1106
1107   application.SendNotification();
1108   application.Render(500);
1109
1110   DALI_TEST_EQUALS( material.GetProperty<Vector4>(colorIndex), Color::WHITE * 0.5f, TEST_LOCATION );
1111
1112   application.Render(500);
1113
1114   DALI_TEST_EQUALS( material.GetProperty<Vector4>(colorIndex), Color::TRANSPARENT, TEST_LOCATION );
1115
1116   END_TEST;
1117 }
1118
1119 int UtcDaliMaterialAnimatedProperty02(void)
1120 {
1121   TestApplication application;
1122
1123   tet_infoline("Test that a uniform map material property can be animated");
1124
1125   Shader shader = Shader::New( "VertexSource", "FragmentSource");
1126   Material material = Material::New( shader );
1127   material.SetProperty(Material::Property::COLOR, Color::WHITE);
1128
1129   Geometry geometry = CreateQuadGeometry();
1130   Renderer renderer = Renderer::New( geometry, material );
1131
1132   Actor actor = Actor::New();
1133   actor.AddRenderer(renderer);
1134   actor.SetSize(400, 400);
1135   Stage::GetCurrent().Add(actor);
1136   application.SendNotification();
1137   application.Render(0);
1138
1139   Vector4 initialColor = Color::WHITE;
1140   Property::Index colorIndex = material.RegisterProperty( "uFadeColor", initialColor );
1141
1142   TestGlAbstraction& gl = application.GetGlAbstraction();
1143
1144   application.SendNotification();
1145   application.Render(0);
1146
1147   Vector4 actualValue(Vector4::ZERO);
1148   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
1149   DALI_TEST_EQUALS( actualValue, initialColor, TEST_LOCATION );
1150
1151   Animation  animation = Animation::New(1.0f);
1152   KeyFrames keyFrames = KeyFrames::New();
1153   keyFrames.Add(0.0f, initialColor);
1154   keyFrames.Add(1.0f, Color::TRANSPARENT);
1155   animation.AnimateBetween( Property( material, colorIndex ), keyFrames );
1156   animation.Play();
1157
1158   application.SendNotification();
1159   application.Render(500);
1160
1161   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
1162   DALI_TEST_EQUALS( actualValue, Color::WHITE * 0.5f, TEST_LOCATION );
1163
1164   application.Render(500);
1165   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uFadeColor", actualValue ) );
1166   DALI_TEST_EQUALS( actualValue, Color::TRANSPARENT, TEST_LOCATION );
1167
1168   END_TEST;
1169 }