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