Revert "License conversion from Flora to Apache 2.0"
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / utc-Dali-RenderableActor.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
22 #include <dali/integration-api/events/touch-event-integ.h>
23 #include <dali-test-suite-utils.h>
24
25 using namespace Dali;
26 using namespace std;
27
28 void renderable_actor_startup(void)
29 {
30   test_return_value = TET_UNDEF;
31 }
32
33 void renderable_actor_cleanup(void)
34 {
35   test_return_value = TET_PASS;
36 }
37
38 namespace
39 {
40
41 static const char* TestTextHelloWorld = "Hello World";
42 static bool gIsActor1SortModifierCorrect;
43 static bool gIsActor2SortModifierCorrect;
44 const float gActor1SortModifierValue = 96.0f;
45 const float gActor2SortModifierValue = 53.0f;
46
47 static float TestSortFunction(const Vector3& position, float sortModifier)
48 {
49   if ( fabs(sortModifier - gActor1SortModifierValue) < 0.01)
50     gIsActor1SortModifierCorrect = true;
51
52   if ( fabs(sortModifier - gActor2SortModifierValue) < 0.01)
53     gIsActor2SortModifierCorrect = true;
54
55   return 0.0f;
56 }
57
58 } // anon namespace
59
60 int UtcDaliRenderableActorDownCast(void)
61 {
62   TestApplication application;
63
64   tet_infoline("Testing Dali::RenderableActor::DownCast()");
65
66   TextActor textActor = TextActor::New(TestTextHelloWorld);
67
68   Actor anActor = Actor::New();
69   anActor.Add( textActor );
70
71   Actor child = anActor.GetChildAt(0);
72   RenderableActor renderableActor = RenderableActor::DownCast( child );
73   DALI_TEST_CHECK( renderableActor );
74
75   renderableActor = NULL;
76   DALI_TEST_CHECK( !renderableActor );
77
78   renderableActor = DownCast< RenderableActor >( child );
79   DALI_TEST_CHECK( renderableActor );
80
81   renderableActor = DownCast< RenderableActor >( anActor );
82   DALI_TEST_CHECK( !renderableActor );
83
84   Actor unInitialzedActor;
85   renderableActor = RenderableActor::DownCast( unInitialzedActor );
86   DALI_TEST_CHECK( !renderableActor );
87
88   renderableActor = DownCast< RenderableActor >( unInitialzedActor );
89   DALI_TEST_CHECK( !renderableActor );
90   END_TEST;
91 }
92
93 int UtcDaliRenderableActorSetSortModifier(void)
94 {
95   TestApplication application;
96
97   tet_infoline("Testing Dali::RenderableActor::SetSortModifier()");
98
99   float val = -500.0f;
100
101   TextActor actor = TextActor::New(TestTextHelloWorld);
102   Stage::GetCurrent().Add(actor);
103
104   actor.SetSortModifier( val );
105
106   float returnValue = actor.GetSortModifier();
107
108   //Verify through actor api
109   DALI_TEST_EQUALS(returnValue, val, TEST_LOCATION);
110
111   Stage::GetCurrent().Remove(actor);
112
113
114   //Verify through layer SetSortFunction
115   gIsActor1SortModifierCorrect = false;
116   gIsActor2SortModifierCorrect = false;
117   BitmapImage img = BitmapImage::New( 1,1 );
118   // create two transparent actors so there is something to sort
119   ImageActor actor1 = ImageActor::New( img );
120   ImageActor actor2 = ImageActor::New( img );
121   actor1.SetSize(1,1);
122   actor1.SetPosition( 0, 0, 0);
123   actor1.SetSortModifier( gActor1SortModifierValue );
124   actor1.SetColor( Vector4(1, 1, 1, 0.5f ) ); // 50% transparent
125   actor2.SetSize(1,1);
126   actor2.SetPosition( 0, 0, 1);
127   actor2.SetSortModifier( gActor2SortModifierValue );
128   actor2.SetColor( Vector4(1, 1, 1, 0.5f ) ); // 50% transparent
129
130   // add to stage
131    Stage::GetCurrent().Add( actor1 );
132    Stage::GetCurrent().Add( actor2 );
133
134    Layer root = Stage::GetCurrent().GetLayer( 0 );
135    root.SetSortFunction( TestSortFunction );
136
137    // flush the queue and render once
138    application.SendNotification();
139    application.Render();
140
141    DALI_TEST_CHECK( gIsActor1SortModifierCorrect && gIsActor2SortModifierCorrect );
142    END_TEST;
143
144 }
145
146 int UtcDaliRenderableActorGetSortModifier(void)
147 {
148   TestApplication application;
149
150   tet_infoline("Testing Dali::RenderableActor::GetSortModifier()");
151
152   TextActor actor = TextActor::New(TestTextHelloWorld);
153   Stage::GetCurrent().Add(actor);
154
155   DALI_TEST_EQUALS(actor.GetSortModifier(), 0.0f, TEST_LOCATION);
156
157   Stage::GetCurrent().Remove(actor);
158   END_TEST;
159 }
160
161 int UtcDaliRenderableActorSetGetBlendMode(void)
162 {
163   TestApplication application;
164
165   tet_infoline("Testing Dali::RenderableActor::SetBlendMode() / Dali::RenderableActor::GetBlendMode()");
166
167   TextActor actor = TextActor::New(TestTextHelloWorld);
168
169   actor.SetBlendMode( BlendingMode::OFF );
170   DALI_TEST_CHECK( BlendingMode::OFF == actor.GetBlendMode() );
171
172   actor.SetBlendMode( BlendingMode::AUTO );
173   DALI_TEST_CHECK( BlendingMode::AUTO == actor.GetBlendMode() );
174
175   actor.SetBlendMode( BlendingMode::ON );
176   DALI_TEST_CHECK( BlendingMode::ON == actor.GetBlendMode() );
177   END_TEST;
178 }
179
180 int UtcDaliRenderableActorSetCullFace(void)
181 {
182   TestApplication application;
183
184   tet_infoline("Testing Dali::RenderableActor::SetCullFace()");
185
186   BitmapImage img = BitmapImage::New( 1,1 );
187   ImageActor actor = ImageActor::New( img );
188   ImageActor actor2 = ImageActor::New( img );
189
190   actor.SetSize(100.0f, 100.0f);
191   actor.SetParentOrigin(ParentOrigin::CENTER);
192   actor.SetAnchorPoint(AnchorPoint::CENTER);
193
194   actor2.SetSize(100.0f, 100.0f);
195   actor2.SetParentOrigin(ParentOrigin::CENTER);
196   actor2.SetAnchorPoint(AnchorPoint::CENTER);
197
198   Stage::GetCurrent().Add(actor);
199   Stage::GetCurrent().Add(actor2);
200
201   //Verify whether the correct GL calls are made when actor is face culled in front and back, and
202   // face culling is disabled for actor2
203   TraceCallStack& cullFaceTrace = application.GetGlAbstraction().GetCullFaceTrace();
204   cullFaceTrace.Enable(true);
205   actor.SetCullFace( CullFrontAndBack );
206
207   // flush the queue and render once
208   application.SendNotification();
209   application.Render();
210
211   cullFaceTrace.Enable(false);
212   std::stringstream out;
213
214   //Verify actor gl state
215   out.str("");
216   out << GL_BLEND;
217   DALI_TEST_EQUALS( cullFaceTrace.TestMethodAndParams(0, "Enable", out.str()), true, TEST_LOCATION);
218
219   out.str("");
220   out << GL_CULL_FACE;
221   DALI_TEST_EQUALS( cullFaceTrace.TestMethodAndParams(1, "Enable", out.str()), true, TEST_LOCATION);
222
223   out.str("");
224   out << GL_FRONT_AND_BACK;
225   DALI_TEST_EQUALS( cullFaceTrace.TestMethodAndParams(2, "CullFace", out.str()), true, TEST_LOCATION);
226
227   //Verify actor2 gl state
228   out.str("");
229   out << GL_CULL_FACE;
230   DALI_TEST_EQUALS( cullFaceTrace.TestMethodAndParams(3, "Disable", out.str()), true, TEST_LOCATION);
231
232   //Verify state through the actor api
233   DALI_TEST_CHECK( CullFrontAndBack == actor.GetCullFace() );
234   DALI_TEST_CHECK( CullNone == actor2.GetCullFace() );
235
236   /**************************************************************/
237
238   //Verify whether the correct GL calls are made when actor2 is face culled in the front
239   cullFaceTrace.Reset();
240   cullFaceTrace.Enable(true);
241   actor2.SetCullFace( CullFront );
242
243   // flush the queue and render once
244   application.SendNotification();
245   application.Render();
246
247   cullFaceTrace.Enable(false);
248
249   //Verify actor gl state
250   out.str("");
251   out << GL_CULL_FACE;
252   DALI_TEST_EQUALS( cullFaceTrace.TestMethodAndParams(0, "Enable", out.str()), true, TEST_LOCATION);
253
254   out.str("");
255   out << GL_FRONT_AND_BACK;
256   DALI_TEST_EQUALS( cullFaceTrace.TestMethodAndParams(1, "CullFace", out.str()), true, TEST_LOCATION);
257
258   //Verify actor2 gl state
259   out.str("");
260   out << GL_CULL_FACE;
261   DALI_TEST_EQUALS( cullFaceTrace.TestMethodAndParams(2, "Enable", out.str()), true, TEST_LOCATION);
262
263   out.str("");
264   out << GL_FRONT;
265   DALI_TEST_EQUALS( cullFaceTrace.TestMethodAndParams(3, "CullFace", out.str()), true, TEST_LOCATION);
266
267   //Verify state through the actor api
268   DALI_TEST_CHECK( CullFrontAndBack == actor.GetCullFace() );
269   DALI_TEST_CHECK( CullFront == actor2.GetCullFace() );
270
271   /**************************************************************/
272   //Verify whether the correct GL calls are made when face culling is disabled for both actors
273   cullFaceTrace.Reset();
274   cullFaceTrace.Enable(true);
275   actor.SetCullFace( CullNone );
276   actor2.SetCullFace( CullNone );
277
278   // flush the queue and render once
279   application.SendNotification();
280   application.Render();
281
282   cullFaceTrace.Enable(false);
283
284   out.str("");
285   out << GL_CULL_FACE;
286   DALI_TEST_EQUALS( cullFaceTrace.TestMethodAndParams(0, "Disable", out.str()), true, TEST_LOCATION);
287
288   //Verify state through the actor api
289   DALI_TEST_CHECK( CullNone == actor.GetCullFace() );
290   DALI_TEST_CHECK( CullNone == actor2.GetCullFace() );
291
292   Stage::GetCurrent().Remove(actor);
293   Stage::GetCurrent().Remove(actor2);
294   END_TEST;
295 }
296
297 int UtcDaliRenderableActorGetCullFace(void)
298 {
299   TestApplication application;
300
301   tet_infoline("Testing Dali::RenderableActor::GetCullFace()");
302
303   TextActor textActor = TextActor::New(TestTextHelloWorld);
304
305   DALI_TEST_CHECK( CullNone == textActor.GetCullFace() );
306
307   ImageActor imageActor = ImageActor::New();
308
309   DALI_TEST_CHECK( CullNone == imageActor.GetCullFace() );
310
311   MeshActor meshActor = MeshActor::New();
312
313   DALI_TEST_CHECK( CullBack == meshActor.GetCullFace() );
314   END_TEST;
315 }
316
317 int UtcDaliRenderableActorSetGetBlendFunc(void)
318 {
319   TestApplication application;
320   TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
321
322   tet_infoline("Testing Dali::RenderableActor::UtcDaliRenderableActorSetGetBlendFunc()");
323
324   BitmapImage img = BitmapImage::New( 1,1 );
325   ImageActor actor = ImageActor::New( img );
326   Stage::GetCurrent().Add( actor );
327   application.SendNotification();
328   application.Render();
329
330   // Test the defaults as documented int blending.h
331   {
332     BlendingFactor::Type srcFactorRgb( BlendingFactor::ZERO );
333     BlendingFactor::Type destFactorRgb( BlendingFactor::ZERO );
334     BlendingFactor::Type srcFactorAlpha( BlendingFactor::ZERO );
335     BlendingFactor::Type destFactorAlpha( BlendingFactor::ZERO );
336     actor.GetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
337     DALI_TEST_EQUALS( BlendingFactor::SRC_ALPHA,           srcFactorRgb,    TEST_LOCATION );
338     DALI_TEST_EQUALS( BlendingFactor::ONE_MINUS_SRC_ALPHA, destFactorRgb,   TEST_LOCATION );
339     DALI_TEST_EQUALS( BlendingFactor::ONE,                 srcFactorAlpha,  TEST_LOCATION );
340     DALI_TEST_EQUALS( BlendingFactor::ONE_MINUS_SRC_ALPHA, destFactorAlpha, TEST_LOCATION );
341   }
342
343   // Set to non-default values
344   actor.SetBlendFunc( BlendingFactor::ONE_MINUS_SRC_COLOR, BlendingFactor::SRC_ALPHA_SATURATE );
345
346   // Test that Set was successful
347   {
348     BlendingFactor::Type srcFactorRgb( BlendingFactor::ZERO );
349     BlendingFactor::Type destFactorRgb( BlendingFactor::ZERO );
350     BlendingFactor::Type srcFactorAlpha( BlendingFactor::ZERO );
351     BlendingFactor::Type destFactorAlpha( BlendingFactor::ZERO );
352     actor.GetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
353     DALI_TEST_EQUALS( BlendingFactor::ONE_MINUS_SRC_COLOR, srcFactorRgb,    TEST_LOCATION );
354     DALI_TEST_EQUALS( BlendingFactor::SRC_ALPHA_SATURATE,  destFactorRgb,   TEST_LOCATION );
355     DALI_TEST_EQUALS( BlendingFactor::ONE_MINUS_SRC_COLOR, srcFactorAlpha,  TEST_LOCATION );
356     DALI_TEST_EQUALS( BlendingFactor::SRC_ALPHA_SATURATE,  destFactorAlpha, TEST_LOCATION );
357   }
358
359   // Render & check GL commands
360   application.SendNotification();
361   application.Render();
362   DALI_TEST_EQUALS( (GLenum)GL_ONE_MINUS_SRC_COLOR, glAbstraction.GetLastBlendFuncSrcRgb(),   TEST_LOCATION );
363   DALI_TEST_EQUALS( (GLenum)GL_SRC_ALPHA_SATURATE,  glAbstraction.GetLastBlendFuncDstRgb(),   TEST_LOCATION );
364   DALI_TEST_EQUALS( (GLenum)GL_ONE_MINUS_SRC_COLOR, glAbstraction.GetLastBlendFuncSrcAlpha(), TEST_LOCATION );
365   DALI_TEST_EQUALS( (GLenum)GL_SRC_ALPHA_SATURATE,  glAbstraction.GetLastBlendFuncDstAlpha(), TEST_LOCATION );
366
367   // Set using separate alpha settings
368   actor.SetBlendFunc( BlendingFactor::CONSTANT_COLOR, BlendingFactor::ONE_MINUS_CONSTANT_COLOR,
369                       BlendingFactor::CONSTANT_ALPHA, BlendingFactor::ONE_MINUS_CONSTANT_ALPHA );
370
371   // Test that Set was successful
372   {
373     BlendingFactor::Type srcFactorRgb( BlendingFactor::ZERO );
374     BlendingFactor::Type destFactorRgb( BlendingFactor::ZERO );
375     BlendingFactor::Type srcFactorAlpha( BlendingFactor::ZERO );
376     BlendingFactor::Type destFactorAlpha( BlendingFactor::ZERO );
377     actor.GetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
378     DALI_TEST_EQUALS( BlendingFactor::CONSTANT_COLOR,            srcFactorRgb,    TEST_LOCATION );
379     DALI_TEST_EQUALS( BlendingFactor::ONE_MINUS_CONSTANT_COLOR,  destFactorRgb,   TEST_LOCATION );
380     DALI_TEST_EQUALS( BlendingFactor::CONSTANT_ALPHA,            srcFactorAlpha,  TEST_LOCATION );
381     DALI_TEST_EQUALS( BlendingFactor::ONE_MINUS_CONSTANT_ALPHA,  destFactorAlpha, TEST_LOCATION );
382   }
383
384   // Render & check GL commands
385   application.SendNotification();
386   application.Render();
387   DALI_TEST_EQUALS( (GLenum)GL_CONSTANT_COLOR,           glAbstraction.GetLastBlendFuncSrcRgb(),   TEST_LOCATION );
388   DALI_TEST_EQUALS( (GLenum)GL_ONE_MINUS_CONSTANT_COLOR, glAbstraction.GetLastBlendFuncDstRgb(),   TEST_LOCATION );
389   DALI_TEST_EQUALS( (GLenum)GL_CONSTANT_ALPHA,           glAbstraction.GetLastBlendFuncSrcAlpha(), TEST_LOCATION );
390   DALI_TEST_EQUALS( (GLenum)GL_ONE_MINUS_CONSTANT_ALPHA, glAbstraction.GetLastBlendFuncDstAlpha(), TEST_LOCATION );
391   END_TEST;
392 }
393
394 int UtcDaliRenderableActorSetGetBlendEquation(void)
395 {
396   TestApplication application;
397   TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
398
399   tet_infoline("Testing Dali::RenderableActor::SetBlendEquation()");
400
401   BitmapImage img = BitmapImage::New( 1,1 );
402   ImageActor actor = ImageActor::New( img );
403   Stage::GetCurrent().Add( actor );
404   application.SendNotification();
405   application.Render();
406
407   // Test the defaults as documented int blending.h
408   {
409     BlendingEquation::Type equationRgb( BlendingEquation::SUBTRACT );
410     BlendingEquation::Type equationAlpha( BlendingEquation::SUBTRACT );
411     actor.GetBlendEquation( equationRgb, equationAlpha );
412     DALI_TEST_EQUALS( BlendingEquation::ADD, equationRgb, TEST_LOCATION );
413     DALI_TEST_EQUALS( BlendingEquation::ADD, equationAlpha, TEST_LOCATION );
414   }
415
416   // Test the single blending equation setting
417   {
418     actor.SetBlendEquation( BlendingEquation::REVERSE_SUBTRACT );
419     BlendingEquation::Type equationRgba( BlendingEquation::SUBTRACT );
420     actor.GetBlendEquation( equationRgba, equationRgba );
421     DALI_TEST_EQUALS( BlendingEquation::REVERSE_SUBTRACT, equationRgba, TEST_LOCATION );
422   }
423
424   actor.SetBlendEquation( BlendingEquation::REVERSE_SUBTRACT, BlendingEquation::REVERSE_SUBTRACT );
425
426   // Test that Set was successful
427   {
428     BlendingEquation::Type equationRgb( BlendingEquation::SUBTRACT );
429     BlendingEquation::Type equationAlpha( BlendingEquation::SUBTRACT );
430     actor.GetBlendEquation( equationRgb, equationAlpha );
431     DALI_TEST_EQUALS( BlendingEquation::REVERSE_SUBTRACT, equationRgb, TEST_LOCATION );
432     DALI_TEST_EQUALS( BlendingEquation::REVERSE_SUBTRACT, equationAlpha, TEST_LOCATION );
433   }
434
435   // Render & check GL commands
436   application.SendNotification();
437   application.Render();
438   DALI_TEST_EQUALS( (GLenum)GL_FUNC_REVERSE_SUBTRACT, glAbstraction.GetLastBlendEquationRgb(),   TEST_LOCATION );
439   DALI_TEST_EQUALS( (GLenum)GL_FUNC_REVERSE_SUBTRACT, glAbstraction.GetLastBlendEquationAlpha(), TEST_LOCATION );
440   END_TEST;
441 }
442
443 int UtcDaliRenderableActorSetGetBlendColor(void)
444 {
445   TestApplication application;
446   TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
447
448   tet_infoline("Testing Dali::RenderableActor::SetBlendColor()");
449
450   BitmapImage img = BitmapImage::New( 1,1 );
451   ImageActor actor = ImageActor::New( img );
452   Stage::GetCurrent().Add( actor );
453   application.SendNotification();
454   application.Render();
455
456   // Test the defaults as documented int blending.h
457   DALI_TEST_EQUALS( Vector4::ZERO, actor.GetBlendColor(), TEST_LOCATION );
458
459   actor.SetBlendColor( Color::RED );
460
461   // Test that Set was successful
462   DALI_TEST_EQUALS( Color::RED, actor.GetBlendColor(), TEST_LOCATION );
463
464   // Render & check GL commands
465   application.SendNotification();
466   application.Render();
467   DALI_TEST_EQUALS( Color::RED, glAbstraction.GetLastBlendColor(),   TEST_LOCATION );
468   END_TEST;
469 }
470
471 int UtcDaliRenderableActorSetGetAlpha(void)
472 {
473   TestApplication application;
474
475   tet_infoline("Testing Dali::RenderableActor::SetGetAlpha()");
476
477   BitmapImage img = BitmapImage::New( 1,1 );
478   ImageActor actor = ImageActor::New( img );
479   Stage::GetCurrent().Add( actor );
480   application.SendNotification();
481   application.Render();
482
483   // use the image alpha on actor
484   actor.SetBlendMode(BlendingMode::ON);
485
486   // Test that Set was successful
487   DALI_TEST_EQUALS( BlendingMode::ON, actor.GetBlendMode(), TEST_LOCATION );
488
489   // Now test that it can be set to false
490   actor.SetBlendMode(BlendingMode::OFF);
491   DALI_TEST_EQUALS(BlendingMode::OFF, actor.GetBlendMode(), TEST_LOCATION );
492   END_TEST;
493 }
494
495 int UtcDaliRenderableActorCreateDestroy(void)
496 {
497   tet_infoline("Testing Dali::RenderableActor::CreateDestroy()");
498   RenderableActor* ractor = new RenderableActor;
499   RenderableActor ractor2( *ractor );
500   DALI_TEST_CHECK( ractor );
501   delete ractor;
502   END_TEST;
503 }