f0571f1891341fc87592e9b172057569b0f3a3e9
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / utc-Dali-Scripting.cpp
1 /*
2  * Copyright (c) 2020 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/public-api/dali-core.h>
22 #include <dali/devel-api/scripting/scripting.h>
23 #include <dali-test-suite-utils.h>
24
25 using namespace Dali;
26 using namespace Dali::Scripting;
27
28 namespace
29 {
30
31 const StringEnum COLOR_MODE_VALUES[] =
32 {
33     { "USE_OWN_COLOR", USE_OWN_COLOR },
34     { "USE_PARENT_COLOR", USE_PARENT_COLOR },
35     { "USE_OWN_MULTIPLY_PARENT_COLOR", USE_OWN_MULTIPLY_PARENT_COLOR },
36     { "USE_OWN_MULTIPLY_PARENT_ALPHA", USE_OWN_MULTIPLY_PARENT_ALPHA },
37 };
38 const unsigned int COLOR_MODE_VALUES_COUNT = sizeof( COLOR_MODE_VALUES ) / sizeof( COLOR_MODE_VALUES[0] );
39
40 const StringEnum DRAW_MODE_VALUES[] =
41 {
42     { "NORMAL", DrawMode::NORMAL },
43     { "OVERLAY_2D", DrawMode::OVERLAY_2D }
44 };
45 const unsigned int DRAW_MODE_VALUES_COUNT = sizeof( DRAW_MODE_VALUES ) / sizeof( DRAW_MODE_VALUES[0] );
46
47
48 ////////////////////////////////////////////////////////////////////////////////
49 // Helpers for string to enum comparisons for Image and Image loading parameters
50 ////////////////////////////////////////////////////////////////////////////////
51
52 /**
53  * Template to check enumerations of type T, with a class of type X
54  */
55 template< typename T, typename X >
56 void TestEnumStrings(
57   Property::Map& map,                       // The map used to create instance of type X
58   const char * const keyName,               // the name of the key to iterate through
59   const StringEnum* values,                 // An array of string values
60   unsigned int num,                         // Number of items in the array
61   T ( X::*method )() const,                 // The member method of X to call to get the enum
62   X ( *creator ) ( const Property::Value& ) // The method which creates an instance of type X
63 )
64 {
65   // get the key reference so we can change its value
66   Property::Value* value = map.Find( keyName );
67   for ( unsigned int i = 0; i < num; ++i )
68   {
69     *value = values[i].string;
70     tet_printf("Checking: %s: %s\n", keyName, values[i].string );
71     X instance = creator( map );
72     DALI_TEST_EQUALS( values[i].value, (int)( instance.*method )(), TEST_LOCATION );
73   }
74 }
75
76 //////////////////////////////////////////////////////////////////////////////
77 // Helpers for string to enum comparisons for Actor to Property::Map
78 //////////////////////////////////////////////////////////////////////////////
79
80 /**
81  * Template to check enumerations of type T
82  */
83 template< typename T >
84 void TestEnumStrings(
85   const char * const keyName,               // The name of the key to check
86   TestApplication& application,             // Reference to the application class
87   const StringEnum* values,                 // An array of string values
88   unsigned int num,                         // Number of items in the array
89   void ( Actor::*method )( T )              // The Actor member method to set the enumeration
90 )
91 {
92   for ( unsigned int i = 0; i < num; ++i )
93   {
94     tet_printf("Checking: %s: %s\n", keyName, values[i].string );
95
96     Actor actor = Actor::New();
97     (actor.*method)( ( T ) values[i].value );
98
99     application.GetScene().Add( actor );
100     application.SendNotification();
101     application.Render();
102
103     Property::Map map;
104     CreatePropertyMap( actor, map );
105
106     DALI_TEST_CHECK( 0 < map.Count() );
107     DALI_TEST_CHECK( NULL != map.Find( keyName ) );
108     DALI_TEST_EQUALS( map.Find( keyName )->Get< std::string >(), values[i].string, TEST_LOCATION );
109
110     application.GetScene().Remove( actor );
111   }
112 }
113
114 //////////////////////////////////////////////////////////////////////////////
115
116
117 } // anon namespace
118
119 int UtcDaliValueFromEnum(void)
120 {
121   enum class T {
122     None, V1 = 1, V2 = 2
123   };
124
125   Property::Value v1 = T::V1;
126   Property::Value v2 = T::V2;
127
128   T t = T::None;
129   DALI_TEST_CHECK( v1.Get<T>() == T::V1 );
130   DALI_TEST_CHECK( v2.Get<T>() == T::V2 );
131   DALI_TEST_CHECK( v1.Get(t) && t == T::V1 );
132   DALI_TEST_CHECK( v2.Get(t) && t == T::V2 );
133
134   END_TEST;
135 }
136
137 int UtcDaliScriptingNewActorNegative(void)
138 {
139   TestApplication application;
140
141   // Empty map
142   {
143     Actor handle = NewActor( Property::Map() );
144     DALI_TEST_CHECK( !handle );
145   }
146
147   // Map with only properties
148   {
149     Property::Map map;
150     map[ "parentOrigin" ] = ParentOrigin::TOP_CENTER;
151     map[ "anchorPoint" ] = AnchorPoint::TOP_CENTER;
152     Actor handle = NewActor( map );
153     DALI_TEST_CHECK( !handle );
154   }
155
156   // Add some signals to the map, we should have no signal connections as its not yet supported
157   {
158     Property::Map map;
159     map[ "type" ] = "Actor";
160     map[ "signals" ] = Property::MAP;
161     Actor handle = NewActor( map );
162     DALI_TEST_CHECK( handle );
163     DALI_TEST_CHECK( !handle.WheelEventSignal().GetConnectionCount() );
164     DALI_TEST_CHECK( !handle.OffSceneSignal().GetConnectionCount() );
165     DALI_TEST_CHECK( !handle.OnSceneSignal().GetConnectionCount() );
166     DALI_TEST_CHECK( !handle.TouchSignal().GetConnectionCount() );
167   }
168   END_TEST;
169 }
170
171 int UtcDaliScriptingNewActorProperties(void)
172 {
173   TestApplication application;
174
175   Property::Map map;
176   map[ "type" ] = "Actor";
177   map[ "size" ] = Vector3::ONE;
178   map[ "position" ] = Vector3::XAXIS;
179   map[ "scale" ] = Vector3::ONE;
180   map[ "visible" ] = false;
181   map[ "color" ] = Color::MAGENTA;
182   map[ "name" ] = "MyActor";
183   map[ "colorMode" ] = "USE_PARENT_COLOR";
184   map[ "sensitive" ] = false;
185   map[ "leaveRequired" ] = true;
186   map[ "drawMode" ] = "OVERLAY_2D";
187   map[ "inheritOrientation" ] = false;
188   map[ "inheritScale" ] = false;
189
190   // Default properties
191   {
192     Actor handle = NewActor( map );
193     DALI_TEST_CHECK( handle );
194
195     application.GetScene().Add( handle );
196     application.SendNotification();
197     application.Render();
198
199     DALI_TEST_EQUALS( handle.GetCurrentProperty< Vector3 >( Actor::Property::SIZE ), Vector3::ONE, TEST_LOCATION );
200     DALI_TEST_EQUALS( handle.GetCurrentProperty< Vector3 >( Actor::Property::POSITION ), Vector3::XAXIS, TEST_LOCATION );
201     DALI_TEST_EQUALS( handle.GetCurrentProperty< Vector3 >( Actor::Property::SCALE ), Vector3::ONE, TEST_LOCATION );
202     DALI_TEST_EQUALS( handle.GetCurrentProperty< bool >( Actor::Property::VISIBLE ), false, TEST_LOCATION );
203     DALI_TEST_EQUALS( handle.GetCurrentProperty< Vector4 >( Actor::Property::COLOR ), Color::MAGENTA, TEST_LOCATION );
204     DALI_TEST_EQUALS( handle.GetProperty< std::string >( Actor::Property::NAME ), "MyActor", TEST_LOCATION );
205     DALI_TEST_EQUALS( handle.GetProperty< ColorMode >( Actor::Property::COLOR_MODE ), USE_PARENT_COLOR, TEST_LOCATION );
206     DALI_TEST_EQUALS( handle.GetProperty< bool >( Actor::Property::SENSITIVE ), false, TEST_LOCATION );
207     DALI_TEST_EQUALS( handle.GetProperty< bool >( Actor::Property::LEAVE_REQUIRED ), true, TEST_LOCATION );
208     DALI_TEST_EQUALS( handle.GetProperty< DrawMode::Type >( Actor::Property::DRAW_MODE ), DrawMode::OVERLAY_2D, TEST_LOCATION );
209     DALI_TEST_EQUALS( handle.GetProperty< bool >( Actor::Property::INHERIT_ORIENTATION ), false, TEST_LOCATION );
210     DALI_TEST_EQUALS( handle.GetProperty< bool >( Actor::Property::INHERIT_SCALE ), false, TEST_LOCATION );
211
212     application.GetScene().Remove( handle );
213   }
214
215   // Check Anchor point and parent origin vector3s
216   map[ "parentOrigin" ] = ParentOrigin::TOP_CENTER;
217   map[ "anchorPoint" ] = AnchorPoint::TOP_LEFT;
218   {
219     Actor handle = NewActor( map );
220     DALI_TEST_CHECK( handle );
221
222     application.GetScene().Add( handle );
223     application.SendNotification();
224     application.Render();
225
226     DALI_TEST_EQUALS( handle.GetCurrentProperty< Vector3 >( Actor::Property::PARENT_ORIGIN ), ParentOrigin::TOP_CENTER, TEST_LOCATION );
227     DALI_TEST_EQUALS( handle.GetCurrentProperty< Vector3 >( Actor::Property::ANCHOR_POINT ), AnchorPoint::TOP_LEFT, TEST_LOCATION );
228
229     application.GetScene().Remove( handle );
230   }
231
232   // Check Anchor point and parent origin STRINGS
233   map[ "parentOrigin" ] = "TOP_LEFT";
234   map[ "anchorPoint" ] = "CENTER_LEFT";
235   {
236     Actor handle = NewActor( map );
237     DALI_TEST_CHECK( handle );
238
239     application.GetScene().Add( handle );
240     application.SendNotification();
241     application.Render();
242
243     DALI_TEST_EQUALS( handle.GetCurrentProperty< Vector3 >( Actor::Property::PARENT_ORIGIN ), ParentOrigin::TOP_LEFT, TEST_LOCATION );
244     DALI_TEST_EQUALS( handle.GetCurrentProperty< Vector3 >( Actor::Property::ANCHOR_POINT ), AnchorPoint::CENTER_LEFT, TEST_LOCATION );
245
246     application.GetScene().Remove( handle );
247   }
248   END_TEST;
249 }
250
251 int UtcDaliScriptingNewAnimation(void)
252 {
253   TestApplication application;
254
255   Property::Map map;
256   map["actor"] = "Actor1";
257   map["property"] = "color";
258   map["value"] = Color::MAGENTA;
259   map["alphaFunction"] = "EASE_IN_OUT";
260
261   Property::Map timePeriod;
262   timePeriod["delay"] = 0.5f;
263   timePeriod["duration"] = 1.0f;
264   map["timePeriod"] = timePeriod;
265
266   Dali::AnimationData data;
267   Scripting::NewAnimation( map, data );
268
269   Actor actor = Actor::New();
270   actor.SetProperty( Actor::Property::NAME,"Actor1");
271   actor.SetProperty( Actor::Property::COLOR,Color::CYAN);
272   application.GetScene().Add(actor);
273
274   Animation anim = data.CreateAnimation( actor, 0.5f );
275   anim.Play();
276
277   application.SendNotification();
278   application.Render(0);
279   application.Render(500); // Start animation
280   application.Render(500); // Halfway thru anim
281   application.SendNotification();
282   DALI_TEST_EQUALS( actor.GetCurrentProperty< Vector4 >( Actor::Property::COLOR ), (Color::MAGENTA+Color::CYAN)*0.5f, TEST_LOCATION);
283
284   application.Render(500); // Halfway thru anim
285   application.SendNotification();
286   DALI_TEST_EQUALS( actor.GetCurrentProperty< Vector4 >( Actor::Property::COLOR ), Color::MAGENTA, TEST_LOCATION );
287
288   END_TEST;
289 }
290
291 int UtcDaliScriptingNewActorChildren(void)
292 {
293   TestApplication application;
294
295   Property::Map map;
296   map[ "type" ] = "Actor";
297   map[ "position" ] = Vector3::XAXIS;
298
299   Property::Map child1Map;
300   child1Map[ "type" ] = "Layer";
301   child1Map[ "position" ] = Vector3::YAXIS;
302
303   Property::Array childArray;
304   childArray.PushBack( child1Map );
305   map[ "actors" ] = childArray;
306
307   // Create
308   Actor handle = NewActor( map );
309   DALI_TEST_CHECK( handle );
310
311   application.GetScene().Add( handle );
312   application.SendNotification();
313   application.Render();
314
315   DALI_TEST_EQUALS( handle.GetCurrentProperty< Vector3 >( Actor::Property::POSITION ), Vector3::XAXIS, TEST_LOCATION );
316   DALI_TEST_EQUALS( handle.GetChildCount(), 1u, TEST_LOCATION );
317
318   Actor child1 = handle.GetChildAt(0);
319   DALI_TEST_CHECK( child1 );
320   DALI_TEST_CHECK( Layer::DownCast( child1 ) );
321   DALI_TEST_EQUALS( child1.GetCurrentProperty< Vector3 >( Actor::Property::POSITION ), Vector3::YAXIS, TEST_LOCATION );
322   DALI_TEST_EQUALS( child1.GetChildCount(), 0u, TEST_LOCATION );
323
324   application.GetScene().Remove( handle );
325   END_TEST;
326 }
327
328
329 int UtcDaliScriptingCreatePropertyMapActor(void)
330 {
331   TestApplication application;
332
333   // Actor Type
334   {
335     Actor actor = Actor::New();
336
337     Property::Map map;
338     CreatePropertyMap( actor, map );
339     DALI_TEST_CHECK( !map.Empty() );
340     DALI_TEST_CHECK( NULL != map.Find( "type" ) );
341     DALI_TEST_EQUALS( map.Find( "type")->Get< std::string >(), "Actor", TEST_LOCATION );
342
343     application.GetScene().Remove( actor );
344   }
345
346   // Layer Type
347   {
348     Actor actor = Layer::New();
349
350     Property::Map map;
351     CreatePropertyMap( actor, map );
352     DALI_TEST_CHECK( !map.Empty() );
353     DALI_TEST_CHECK( NULL != map.Find( "type" ) );
354     DALI_TEST_EQUALS( map.Find( "type" )->Get< std::string >(), "Layer", TEST_LOCATION );
355
356     application.GetScene().Remove( actor );
357   }
358
359   // Default properties
360   {
361     Actor actor = Actor::New();
362     actor.SetProperty( Actor::Property::SIZE, Vector3::ONE );
363     actor.SetProperty( Actor::Property::POSITION, Vector3::XAXIS );
364     actor.SetProperty( Actor::Property::SCALE, Vector3::ZAXIS );
365     actor.SetProperty( Actor::Property::VISIBLE, false );
366     actor.SetProperty( Actor::Property::COLOR, Color::MAGENTA );
367     actor.SetProperty( Actor::Property::NAME, "MyActor" );
368     actor.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER_LEFT );
369     actor.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_RIGHT );
370     actor.SetProperty( Actor::Property::SENSITIVE, false );
371     actor.SetProperty( Actor::Property::LEAVE_REQUIRED, true );
372     actor.SetProperty( Actor::Property::INHERIT_ORIENTATION, false );
373     actor.SetProperty( Actor::Property::INHERIT_SCALE, false );
374     actor.SetProperty( Actor::Property::SIZE_MODE_FACTOR, Vector3::ONE );
375
376     application.GetScene().Add( actor );
377     application.SendNotification();
378     application.Render();
379
380     Property::Map map;
381     CreatePropertyMap( actor, map );
382
383     DALI_TEST_CHECK( !map.Empty() );
384     DALI_TEST_CHECK( NULL != map.Find( "size" ) );
385     DALI_TEST_EQUALS( map.Find( "size" )->Get< Vector3 >(), Vector3::ONE, TEST_LOCATION );
386     DALI_TEST_CHECK( NULL != map.Find( "position" ) );
387     DALI_TEST_EQUALS( map.Find( "position" )->Get< Vector3 >(), Vector3::XAXIS, TEST_LOCATION );
388     DALI_TEST_CHECK( NULL != map.Find( "scale" ) );
389     DALI_TEST_EQUALS( map.Find( "scale" )->Get< Vector3 >(), Vector3::ZAXIS, TEST_LOCATION );
390     DALI_TEST_CHECK( NULL != map.Find( "visible" ) );
391     DALI_TEST_EQUALS( map.Find( "visible" )->Get< bool >(), false, TEST_LOCATION );
392     DALI_TEST_CHECK( NULL != map.Find( "color" ) );
393     DALI_TEST_EQUALS( map.Find( "color" )->Get< Vector4 >(), Color::MAGENTA, TEST_LOCATION );
394     DALI_TEST_CHECK( NULL != map.Find( "name" ) );
395     DALI_TEST_EQUALS( map.Find( "name")->Get< std::string >(), "MyActor", TEST_LOCATION );
396     DALI_TEST_CHECK( NULL != map.Find( "anchorPoint" ) );
397     DALI_TEST_EQUALS( map.Find( "anchorPoint" )->Get< Vector3 >(), AnchorPoint::CENTER_LEFT, TEST_LOCATION );
398     DALI_TEST_CHECK( NULL != map.Find( "parentOrigin" ) );
399     DALI_TEST_EQUALS( map.Find( "parentOrigin" )->Get< Vector3 >(), ParentOrigin::TOP_RIGHT, TEST_LOCATION );
400     DALI_TEST_CHECK( NULL != map.Find( "sensitive" ) );
401     DALI_TEST_EQUALS( map.Find( "sensitive" )->Get< bool >(), false, TEST_LOCATION );
402     DALI_TEST_CHECK( NULL != map.Find( "leaveRequired" ) );
403     DALI_TEST_EQUALS( map.Find( "leaveRequired" )->Get< bool >(), true, TEST_LOCATION );
404     DALI_TEST_CHECK( NULL != map.Find( "inheritOrientation" ) );
405     DALI_TEST_EQUALS( map.Find( "inheritOrientation" )->Get< bool >(), false, TEST_LOCATION );
406     DALI_TEST_CHECK( NULL != map.Find( "inheritScale" ) );
407     DALI_TEST_EQUALS( map.Find( "inheritScale" )->Get< bool >(), false, TEST_LOCATION );
408     DALI_TEST_CHECK( NULL != map.Find( "sizeModeFactor" ) );
409     DALI_TEST_EQUALS( map.Find( "sizeModeFactor" )->Get< Vector3 >(), Vector3::ONE, TEST_LOCATION );
410
411     application.GetScene().Remove( actor );
412   }
413
414   // Children
415   {
416     Actor actor = Actor::New();
417     Actor child = Layer::New();
418     actor.Add( child );
419
420     application.GetScene().Add( actor );
421     application.SendNotification();
422     application.Render();
423
424     Property::Map map;
425     CreatePropertyMap( actor, map );
426     DALI_TEST_CHECK( !map.Empty() );
427
428     DALI_TEST_CHECK( NULL != map.Find( "type" ) );
429     DALI_TEST_EQUALS( map.Find( "type" )->Get< std::string >(), "Actor", TEST_LOCATION );
430
431     DALI_TEST_CHECK( NULL != map.Find( "actors" ) );
432     Property::Array children( map.Find( "actors")->Get< Property::Array >() );
433     DALI_TEST_CHECK( !children.Empty() );
434     Property::Map childMap( children[0].Get< Property::Map >() );
435     DALI_TEST_CHECK( !childMap.Empty() );
436     DALI_TEST_CHECK( childMap.Find( "type" ) );
437     DALI_TEST_EQUALS( childMap.Find( "type" )->Get< std::string >(), "Layer", TEST_LOCATION );
438
439     application.GetScene().Remove( actor );
440   }
441   END_TEST;
442 }
443
444 int UtcDaliScriptingGetEnumerationTemplates(void)
445 {
446   const Scripting::StringEnum myTable[] =
447   {
448     { "ONE",    1 },
449     { "TWO",    2 },
450     { "THREE",  3 },
451     { "FOUR",   4 },
452     { "FIVE",   5 },
453   };
454   const unsigned int myTableCount = sizeof( myTable ) / sizeof( myTable[0] );
455
456   for ( unsigned int i = 0; i < myTableCount; ++i )
457   {
458     tet_printf("Checking: %s\n", myTable[ i ].string );
459     int value;
460     DALI_TEST_CHECK( GetEnumeration<int>( myTable[ i ].string, myTable, myTableCount, value ) );
461     DALI_TEST_EQUALS( myTable[ i ].value, value, TEST_LOCATION );
462   }
463
464   for ( unsigned int i = 0; i < myTableCount; ++i )
465   {
466     tet_printf("Checking: %d\n", myTable[ i ].value );
467     DALI_TEST_EQUALS( myTable[ i ].string, GetEnumerationName( myTable[ i ].value, myTable, myTableCount ), TEST_LOCATION );
468   }
469
470   END_TEST;
471 }
472
473 int UtcDaliScriptingGetEnumerationNameN(void)
474 {
475   const char* value = GetEnumerationName( 10, NULL, 0 );
476   DALI_TEST_CHECK( NULL == value );
477
478   value = GetEnumerationName( 10, NULL, 1 );
479   DALI_TEST_CHECK( NULL == value );
480
481   END_TEST;
482 }
483
484 int UtcDaliScriptingGetLinearEnumerationNameN(void)
485 {
486   const char* value = GetLinearEnumerationName( 10, NULL, 0 );
487   DALI_TEST_CHECK( NULL == value );
488
489   value = GetLinearEnumerationName( 10, NULL, 1 );
490   DALI_TEST_CHECK( NULL == value );
491
492   END_TEST;
493 }
494
495 int UtcDaliScriptingGetEnumerationProperty(void)
496 {
497   /*
498    * This test function performs the following checks:
499    *  - An enum can be looked up from a Property::Value of type INTEGER.
500    *  - An enum can be looked up from a Property::Value of type STRING.
501    *  - An enum can NOT be looked up for other Property::Value types.
502    *  - The return value is "true" if the property can be successfully converted AND it has changed.
503    *  - The return value is "false" if the property can be successfully converted BUT it has NOT changed.
504    *  - The return value is "false" if the property can not be successfully converted.
505    *  - The result value is only updated if the return value is "true" (IE. successful conversion and property value has changed).
506    */
507
508   // String to Enum property table to test with (equivalent to ones used within DALi).
509   const Dali::Scripting::StringEnum  testTable[] = {
510       { "NONE",           FaceCullingMode::NONE },
511       { "FRONT",          FaceCullingMode::FRONT },
512       { "BACK",           FaceCullingMode::BACK },
513       { "FRONT_AND_BACK", FaceCullingMode::FRONT_AND_BACK }
514   }; const unsigned int testTableCount = sizeof( testTable ) / sizeof( testTable[0] );
515
516   // TEST: An enum can be looked up from a Property::Value of type INTEGER.
517   // Initialise to first element.
518   FaceCullingMode::Type result = FaceCullingMode::NONE;
519   // Set the input property value to a different value (to emulate a change).
520   Property::Value propertyValueInteger( FaceCullingMode::FRONT );
521
522   // Perform the lookup.
523   bool returnValue = GetEnumerationProperty< FaceCullingMode::Type >( propertyValueInteger, testTable, testTableCount, result );
524
525   // TEST: The return value is "true" if the property can be successfully converted AND it has changed
526   // Check the property could be converted.
527   DALI_TEST_CHECK( returnValue );
528
529   DALI_TEST_EQUALS( static_cast<int>( result ), static_cast<int>( FaceCullingMode::FRONT ), TEST_LOCATION );
530
531   // Now emulate a property-set with the same value. false should be returned.
532   returnValue = GetEnumerationProperty< FaceCullingMode::Type >( propertyValueInteger, testTable, testTableCount, result );
533
534   // TEST: The return value is "false" if the property can be successfully converted BUT it has NOT changed.
535   DALI_TEST_CHECK( !returnValue );
536
537   // The result should remain the same.
538   DALI_TEST_EQUALS( static_cast<int>( result ), static_cast<int>( FaceCullingMode::FRONT ), TEST_LOCATION );
539
540   // TEST: An enum can be looked up from a Property::Value of type STRING.
541   // Set the input property value to a different value (to emulate a change).
542   Property::Value propertyValueString( "BACK" );
543
544   returnValue = GetEnumerationProperty< FaceCullingMode::Type >( propertyValueString, testTable, testTableCount, result );
545
546   DALI_TEST_CHECK( returnValue );
547
548   // The result should remain the same.
549   DALI_TEST_EQUALS( static_cast<int>( result ), static_cast<int>( FaceCullingMode::BACK ), TEST_LOCATION );
550
551   returnValue = GetEnumerationProperty< FaceCullingMode::Type >( propertyValueString, testTable, testTableCount, result );
552
553   DALI_TEST_CHECK( !returnValue );
554
555   // The result should remain the same.
556   DALI_TEST_EQUALS( static_cast<int>( result ), static_cast<int>( FaceCullingMode::BACK ), TEST_LOCATION );
557
558   // TEST: An enum can NOT be looked up for other Property::Value types.
559   Property::Value propertyValueBoolean( true );
560
561   returnValue = GetEnumerationProperty< FaceCullingMode::Type >( propertyValueBoolean, testTable, testTableCount, result );
562
563   // TEST: The return value is "false" if the property can not be successfully converted.
564   // Return value should be false as Property::Value was of an unsupported type for enum properties.
565   DALI_TEST_CHECK( !returnValue );
566
567   // TEST: The result value is only updated if the return value is "true" (IE. successful conversion and property value has changed).
568   // The result should remain the same.
569   DALI_TEST_EQUALS( static_cast<int>( result ), static_cast<int>( FaceCullingMode::BACK ), TEST_LOCATION );
570
571   END_TEST;
572 }
573
574 int UtcDaliScriptingGetBitmaskEnumerationProperty(void)
575 {
576   /*
577    * This test function performs the following checks:
578    *  - An enum can be looked up from a Property::Value of type INTEGER.
579    *  - An enum can be looked up from a Property::Value of type STRING.
580    *  - An enum can NOT be looked up from other Property::Value types.
581    *  - The return value is "true" if the property can be successfully converted AND it has changed.
582    *  - The return value is "false" if the property can not be successfully converted.
583    *  - The result value is only updated if the return value is "true" (IE. successful conversion and property value has changed).
584    *  PropertyArrays:
585    *  - The return value when checking an array with 2 INTEGERS is "true" if the properties can be successfully converted.
586    *  - The result value when checking an array with 2 INTEGERS is the ORd value of the 2 integers.
587    *  - The return value when checking an array with 2 STRINGS is "true" if the properties can be successfully converted.
588    *  - The result value when checking an array with 2 STRINGS is the ORd value of the 2 integer equivalents of the strings.
589    *  - The return value when checking an array with an INTEGER and a STRING is "true" if the properties can be successfully converted.
590    *  - The result value when checking an array with an INTEGER and a STRING is the ORd value of the 2 integer equivalents of the strings.
591    *  - The return value when checking an array with an INTEGER and a Vector3 is "false" as the properties can not be successfully converted.
592    *  - The result value when checking an array with an INTEGER and a Vector3 is unchanged.
593    */
594
595   // String to Enum property table to test with (equivalent to ones used within DALi).
596   const Dali::Scripting::StringEnum  testTable[] = {
597       { "NONE",           FaceCullingMode::NONE },
598       { "FRONT",          FaceCullingMode::FRONT },
599       { "BACK",           FaceCullingMode::BACK },
600       { "FRONT_AND_BACK", FaceCullingMode::FRONT_AND_BACK }
601   }; const unsigned int testTableCount = sizeof( testTable ) / sizeof( testTable[0] );
602
603   // TEST: An enum can be looked up from a Property::Value of type INTEGER.
604   // Initialise to first element.
605   FaceCullingMode::Type result = FaceCullingMode::NONE;
606   // Set the input property value to a different value (to emulate a change).
607   Property::Value propertyValueInteger( FaceCullingMode::FRONT );
608
609   // Perform the lookup.
610   bool returnValue = GetBitmaskEnumerationProperty< FaceCullingMode::Type >( propertyValueInteger, testTable, testTableCount, result );
611
612   // TEST: The return value is "true" if the property can be successfully converted AND it has changed
613   // Check the property could be converted.
614   DALI_TEST_CHECK( returnValue );
615
616   DALI_TEST_EQUALS( static_cast<int>( result ), static_cast<int>( FaceCullingMode::FRONT ), TEST_LOCATION );
617
618   // TEST: An enum can be looked up from a Property::Value of type STRING.
619   // Set the input property value to a different value (to emulate a change).
620   Property::Value propertyValueString( "BACK" );
621
622   returnValue = GetBitmaskEnumerationProperty< FaceCullingMode::Type >( propertyValueString, testTable, testTableCount, result );
623
624   DALI_TEST_CHECK( returnValue );
625
626   DALI_TEST_EQUALS( static_cast<int>( result ), static_cast<int>( FaceCullingMode::BACK ), TEST_LOCATION );
627
628   // TEST: An enum can NOT be looked up from other Property::Value types.
629   Property::Value propertyValueVector( Vector3::ZERO );
630
631   returnValue = GetBitmaskEnumerationProperty< FaceCullingMode::Type >( propertyValueVector, testTable, testTableCount, result );
632
633   // TEST: The return value is "false" if the property can not be successfully converted.
634   // Return value should be false as Property::Value was of an unsupported type for enum properties.
635   DALI_TEST_CHECK( !returnValue );
636
637   // TEST: The result value is only updated if the return value is "true" (IE. successful conversion and property value has changed).
638   // The result should remain the same.
639   DALI_TEST_EQUALS( static_cast<int>( result ), static_cast<int>( FaceCullingMode::BACK ), TEST_LOCATION );
640
641   // Test PropertyArrays:
642
643   // Property array of 2 integers.
644   Property::Array propertyArrayIntegers;
645   propertyArrayIntegers.PushBack( FaceCullingMode::FRONT );
646   propertyArrayIntegers.PushBack( FaceCullingMode::BACK );
647   result = FaceCullingMode::NONE;
648
649   returnValue = GetBitmaskEnumerationProperty< FaceCullingMode::Type >( propertyArrayIntegers, testTable, testTableCount, result );
650
651   // TEST: The return value when checking an array with 2 INTEGERS is "true" if the properties can be successfully converted.
652   DALI_TEST_CHECK( returnValue );
653   // TEST: The result value when checking an array with 2 INTEGERS is the ORd value of the 2 integers.
654   DALI_TEST_CHECK( result == ( FaceCullingMode::FRONT | FaceCullingMode::BACK ) );
655
656   // Property array of 2 strings.
657   Property::Array propertyArrayStrings;
658   propertyArrayStrings.PushBack( "FRONT" );
659   propertyArrayStrings.PushBack( "BACK" );
660   result = FaceCullingMode::NONE;
661
662   returnValue = GetBitmaskEnumerationProperty< FaceCullingMode::Type >( propertyArrayStrings, testTable, testTableCount, result );
663
664   // TEST: The return value when checking an array with 2 STRINGS is "true" if the properties can be successfully converted.
665   DALI_TEST_CHECK( returnValue );
666   // TEST: The result value when checking an array with 2 STRINGS is the ORd value of the 2 integer equivalents of the strings.
667   DALI_TEST_CHECK( result == ( FaceCullingMode::FRONT | FaceCullingMode::BACK ) );
668
669   // Property array of an int and a string.
670   Property::Array propertyArrayMixed;
671   propertyArrayMixed.PushBack( FaceCullingMode::FRONT );
672   propertyArrayMixed.PushBack( "BACK" );
673   result = FaceCullingMode::NONE;
674
675   returnValue = GetBitmaskEnumerationProperty< FaceCullingMode::Type >( propertyArrayMixed, testTable, testTableCount, result );
676
677   // TEST: The return value when checking an array with an INTEGER and a STRING is "true" if the properties can be successfully converted.
678   DALI_TEST_CHECK( returnValue );
679   // TEST: The result value when checking an array with an INTEGER and a STRING is the ORd value of the 2 integer equivalents of the strings.
680   DALI_TEST_CHECK( result == ( FaceCullingMode::FRONT | FaceCullingMode::BACK ) );
681
682   // Property array of an int and a string.
683   Property::Array propertyArrayInvalid;
684   propertyArrayInvalid.PushBack( FaceCullingMode::FRONT );
685   propertyArrayInvalid.PushBack( Vector3::ZERO );
686
687   // Set the initial value to non-zero, so we can test it does not change.
688   result = FaceCullingMode::FRONT_AND_BACK;
689
690   returnValue = GetBitmaskEnumerationProperty< FaceCullingMode::Type >( propertyArrayInvalid, testTable, testTableCount, result );
691
692   // TEST: The return value when checking an array with an INTEGER and a Vector3 is "false" as the properties can not be successfully converted.
693   DALI_TEST_CHECK( !returnValue );
694   // TEST: The result value when checking an array with an INTEGER and a Vector3 is unchanged.
695   DALI_TEST_CHECK( result == FaceCullingMode::FRONT_AND_BACK );
696
697   END_TEST;
698 }
699
700 int UtcDaliScriptingFindEnumIndexN(void)
701 {
702   const Scripting::StringEnum myTable[] =
703     {
704       { "ONE",    (1<<1) },
705       { "TWO",    (1<<2) },
706       { "THREE",  (1<<3) },
707       { "FOUR",   (1<<4) },
708       { "FIVE",   (1<<5) },
709     };
710   const unsigned int myTableCount = sizeof( myTable ) / sizeof( myTable[0] );
711   DALI_TEST_EQUALS( myTableCount, FindEnumIndex( "Foo", myTable, myTableCount ), TEST_LOCATION );
712
713   END_TEST;
714 }
715
716 int UtcDaliScriptingEnumStringToIntegerP(void)
717 {
718   const Scripting::StringEnum myTable[] =
719     {
720       { "ONE",    (1<<1) },
721       { "TWO",    (1<<2) },
722       { "THREE",  (1<<3) },
723       { "FOUR",   (1<<4) },
724       { "FIVE",   (1<<5) },
725     };
726   const unsigned int myTableCount = sizeof( myTable ) / sizeof( myTable[0] );
727
728   int integerEnum = 0;
729   DALI_TEST_CHECK( EnumStringToInteger( "ONE", myTable, myTableCount, integerEnum ) );
730
731   DALI_TEST_EQUALS( integerEnum, (1<<1), TEST_LOCATION );
732
733   integerEnum = 0;
734   DALI_TEST_CHECK( EnumStringToInteger( "ONE,TWO", myTable, myTableCount, integerEnum ) );
735   DALI_TEST_EQUALS( integerEnum, (1<<1) | (1<<2), TEST_LOCATION );
736
737   DALI_TEST_CHECK( EnumStringToInteger( "ONE,,TWO", myTable, myTableCount, integerEnum ) );
738   DALI_TEST_EQUALS( integerEnum, (1<<1) | (1<<2), TEST_LOCATION );
739
740   DALI_TEST_CHECK( EnumStringToInteger( "ONE,TWO,THREE", myTable, myTableCount, integerEnum ) );
741   DALI_TEST_EQUALS( integerEnum, (1<<1) | (1<<2) | (1<<3), TEST_LOCATION );
742
743   DALI_TEST_CHECK( EnumStringToInteger( "ONE,TWO,THREE,FOUR,FIVE", myTable, myTableCount, integerEnum ) );
744   DALI_TEST_EQUALS( integerEnum, (1<<1) | (1<<2) | (1<<3) | (1<<4) | (1<<5), TEST_LOCATION );
745
746   DALI_TEST_CHECK( EnumStringToInteger( "TWO,ONE", myTable, myTableCount, integerEnum ) );
747   DALI_TEST_EQUALS( integerEnum, (1<<1) | (1<<2), TEST_LOCATION );
748
749   DALI_TEST_CHECK( EnumStringToInteger( "TWO,ONE,FOUR,THREE,FIVE", myTable, myTableCount, integerEnum ) );
750   DALI_TEST_EQUALS( integerEnum, (1<<1) | (1<<2) | (1<<3) | (1<<4) | (1<<5), TEST_LOCATION );
751
752   DALI_TEST_CHECK( EnumStringToInteger( "ONE,SEVEN", myTable, myTableCount, integerEnum ) );
753   DALI_TEST_EQUALS( integerEnum, (1<<1), TEST_LOCATION );
754
755   DALI_TEST_CHECK( EnumStringToInteger( "ONE,", myTable, myTableCount, integerEnum ) );
756   DALI_TEST_EQUALS( integerEnum, (1<<1), TEST_LOCATION );
757
758
759   END_TEST;
760 }
761
762 int UtcDaliScriptingEnumStringToIntegerN(void)
763 {
764   const Scripting::StringEnum myTable[] =
765   {
766     { "ONE",    1 },
767     { "TWO",    2 },
768     { "THREE",  3 },
769     { "FOUR",   4 },
770     { "FIVE",   5 },
771   };
772   const unsigned int myTableCount = sizeof( myTable ) / sizeof( myTable[0] );
773
774   int integerEnum = 0;
775   DALI_TEST_CHECK( !EnumStringToInteger( "Foo", myTable, myTableCount, integerEnum ) );
776
777   DALI_TEST_CHECK( !EnumStringToInteger( "", myTable, myTableCount, integerEnum ) );
778
779   DALI_TEST_CHECK( !EnumStringToInteger( ",", myTable, myTableCount, integerEnum ) );
780
781   DALI_TEST_CHECK( !EnumStringToInteger( ",ONE,SEVEN", myTable, myTableCount, integerEnum ) );
782
783   DALI_TEST_CHECK( !EnumStringToInteger( ",", myTable, myTableCount, integerEnum ) );
784
785   DALI_TEST_CHECK( !EnumStringToInteger( "ONE", myTable, 0, integerEnum ) );
786
787   DALI_TEST_EQUALS( integerEnum, 0, TEST_LOCATION );
788
789   END_TEST;
790 }
791
792 int UtcDaliScriptingEnumStringToIntegerInvalidEnumP(void)
793 {
794   const Scripting::StringEnum myTable[] =
795   {
796     { "",    1 },
797     { "",    2 },
798     { "",    3 },
799   };
800
801   const unsigned int myTableCount = sizeof( myTable ) / sizeof( myTable[0] );
802
803   int integerEnum = 0;
804   DALI_TEST_CHECK( EnumStringToInteger( "", myTable, myTableCount, integerEnum ) );
805   DALI_TEST_EQUALS( integerEnum, 1, TEST_LOCATION );
806
807   END_TEST;
808 }
809
810 int UtcDaliScriptingEnumStringToIntegerInvalidEnumN(void)
811 {
812   const Scripting::StringEnum myTable[] =
813   {
814     { "",    1 },
815     { "",    1 },
816     { "",    1 },
817   };
818
819   const unsigned int myTableCount = sizeof( myTable ) / sizeof( myTable[0] );
820
821   int integerEnum = 0;
822   DALI_TEST_CHECK( !EnumStringToInteger( NULL, NULL, 0, integerEnum ) );
823
824   DALI_TEST_CHECK( !EnumStringToInteger( "ONE", NULL, 0, integerEnum ) );
825
826   DALI_TEST_CHECK( !EnumStringToInteger( NULL, myTable, 0, integerEnum ) );
827
828   DALI_TEST_CHECK( !EnumStringToInteger( NULL, myTable, myTableCount, integerEnum ) );
829
830   DALI_TEST_CHECK( !EnumStringToInteger( "ONE", NULL, myTableCount, integerEnum ) );
831
832   DALI_TEST_EQUALS( integerEnum, 0, TEST_LOCATION );
833
834   END_TEST;
835 }