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