Merge "Added new API to Property::Map to enable method chaining" into devel/master
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / utc-Dali-PropertyMap.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 #include <string>
20 #include <stdlib.h>
21 #include <dali/public-api/dali-core.h>
22 #include <dali-test-suite-utils.h>
23
24 using namespace Dali;
25
26 void utc_dali_property_map_startup(void)
27 {
28   test_return_value = TET_UNDEF;
29 }
30
31 void utc_dali_property_map_cleanup(void)
32 {
33   test_return_value = TET_PASS;
34 }
35
36 int UtcDaliPropertyMapPopulate(void)
37 {
38   Property::Map map;
39   DALI_TEST_CHECK( map.Empty() );
40
41   map[ "hello" ] = 1;
42   map[ 10 ] = "DALi";
43   map[ "world" ] = "world";
44   map[ 100 ] = 9;
45   map[ "world" ] = 3; // same item as line above
46   DALI_TEST_CHECK( !map.Empty() ); // Should no longer be empty
47   DALI_TEST_CHECK( map.Count() == 4 ); // Should only have four items, not five!!
48   DALI_TEST_CHECK( map["hello"].Get<int>() == 1 );
49   DALI_TEST_CHECK( map["world"].Get<int>() == 3 );
50   DALI_TEST_EQUALS( "DALi", map[ 10 ].Get<std::string>(), TEST_LOCATION );
51   DALI_TEST_CHECK( map[100].Get<int>() == 9 );
52
53   map.Clear();
54   DALI_TEST_CHECK( map.Empty() );
55   END_TEST;
56 }
57
58 int UtcDaliPropertyMapCopyAndAssignment(void)
59 {
60   Property::Map map;
61   map[ "hello" ] = 1;
62   map[ "world" ] = 2;
63   map[ 10 ] = "DALi";
64
65   Property::Map assignedMap;
66   assignedMap[ "foo" ] = 3;
67   assignedMap[ 100 ] = 9;
68   DALI_TEST_CHECK( assignedMap.Count() == 2 );
69   assignedMap = map;
70   DALI_TEST_CHECK( assignedMap.Count() == 3 );
71
72   Property::Map copiedMap( map );
73   DALI_TEST_CHECK( copiedMap.Count() == 3 );
74
75   // Self assignment
76   DALI_TEST_CHECK( map.Count() == 3 );
77   map = map;
78   DALI_TEST_CHECK( map.Count() == 3 );
79
80   END_TEST;
81 }
82
83 int UtcDaliPropertyMapConstOperator(void)
84 {
85   Property::Map map;
86   map[ "hello" ] = 1;
87   map[ 10 ] = "DALi";
88   map[ "world" ] = 2;
89   DALI_TEST_CHECK( map.Count() == 3 );
90
91   const Property::Map& constMap( map );
92   DALI_TEST_CHECK( constMap[ "world" ].Get<int>() == 2 );
93   DALI_TEST_CHECK( constMap.Count() == 3 ); // Ensure count hasn't gone up
94
95   DALI_TEST_EQUALS( "DALi", map[ 10 ].Get<std::string>(), TEST_LOCATION );
96   DALI_TEST_CHECK( constMap.Count() == 3 ); // Ensure count hasn't gone up
97
98   // Invalid Key
99   try
100   {
101     constMap[ "invalidKey" ];
102     tet_result( TET_FAIL );
103   }
104   catch ( DaliException& e )
105   {
106     DALI_TEST_ASSERT( e, "! \"Invalid Key\"", TEST_LOCATION );
107   }
108
109   END_TEST;
110 }
111
112 // deprecated API, only retrieve the value from string-value pairs
113 int UtcDaliPropertyMapGetValue(void)
114 {
115   Property::Map map;
116   map[ "hello" ] = 1;
117   map[ "world" ] = 2;
118
119   Property::Value& value = map.GetValue( 0 );
120   DALI_TEST_CHECK( value.Get<int>() == 1 );
121   value = 10; // Allows the actual changing of the value as we have a ref
122   DALI_TEST_CHECK( map[ "hello" ].Get<int>() == 10 );
123
124   // Out of bounds
125   try
126   {
127     map.GetValue( 2 );
128     tet_result( TET_FAIL );
129   }
130   catch ( DaliException& e )
131   {
132     DALI_TEST_ASSERT( e, "position", TEST_LOCATION );
133   }
134
135   END_TEST;
136 }
137
138 // deprecated API, only retrieve the key from the string-value pairs
139 int UtcDaliPropertyMapGetKey(void)
140 {
141   Property::Map map;
142   map[ "hello" ] = 1;
143   map[ "world" ] = 2;
144
145   DALI_TEST_CHECK( map.GetKey( 0 ) == "hello" );
146   DALI_TEST_CHECK( map.GetKey( 1 ) == "world" );
147
148   // Out of bounds
149   try
150   {
151     map.GetKey( 2 );
152     tet_result( TET_FAIL );
153   }
154   catch ( DaliException& e )
155   {
156     DALI_TEST_ASSERT( e, "position", TEST_LOCATION );
157   }
158
159   END_TEST;
160 }
161
162 // deprecated API, only retrieve the string-value pairs
163 int UtcDaliPropertyMapGetPair(void)
164 {
165   Property::Map map;
166   map[ "hello" ] = 1;
167   map[ "world" ] = 2;
168
169   DALI_TEST_CHECK( map.GetPair( 0 ).first == "hello" );
170   DALI_TEST_CHECK( map.GetPair( 0 ).second.Get< int >() == 1 );
171   DALI_TEST_CHECK( map.GetPair( 1 ).first == "world" );
172   DALI_TEST_CHECK( map.GetPair( 1 ).second.Get< int >() == 2 );
173
174   // Out of bounds
175   try
176   {
177     map.GetPair( 2 );
178     tet_result( TET_FAIL );
179   }
180   catch ( DaliException& e )
181   {
182     DALI_TEST_ASSERT( e, "position", TEST_LOCATION );
183   }
184
185   END_TEST;
186 }
187
188 int UtcDaliPropertyMapFind(void)
189 {
190   Property::Map map;
191   map[ "hello" ] = 1;
192   map[ 10 ] = "DALi";
193   map[ "world" ] = 2;
194   map[ 100 ] = 9;
195
196   Property::Value* value = NULL;
197
198   value = map.Find( "hello" );
199   DALI_TEST_CHECK( value );
200   DALI_TEST_CHECK( value->Get<int>() == 1 );
201
202   const std::string world("world");
203   value = map.Find( world );
204   DALI_TEST_CHECK( value );
205   DALI_TEST_CHECK( value->Get<int>() == 2 );
206
207   value = map.Find( 100 );
208   DALI_TEST_CHECK( value );
209   DALI_TEST_CHECK( value->Get<int>() == 9 );
210
211   value = map.Find( 10, Property::STRING );
212   DALI_TEST_CHECK( value );
213   DALI_TEST_EQUALS( "DALi", value->Get<std::string>(), TEST_LOCATION );
214
215   value = map.Find( 10, Property::INTEGER );
216   DALI_TEST_CHECK( value == NULL );
217
218   value = map.Find( "invalidKey" );
219   DALI_TEST_CHECK( !value );
220
221   END_TEST;
222 }
223
224 int UtcDaliPropertyMapFindIndexThenString(void)
225 {
226   // Define the valid keys and values to test with.
227   std::string stringKeyValid = "bar";
228   std::string stringKeyInvalid = "aardvark";
229   int indexKeyValid = 100;
230   int indexKeyInvalid = 101;
231
232   // Define invalid key and value to test with.
233   std::string stringValueValid = "DALi";
234   int indexValueValid = 3;
235
236   // Set up a property map containing the valid keys and values defined above.
237   Property::Map map;
238   map[ "foo" ] = 1;
239   map[ 10 ] = "string";
240   map[ stringKeyValid ] = stringValueValid;
241   map[ indexKeyValid ] = indexValueValid;
242
243   Property::Value* value = NULL;
244
245   // TEST: If both index and string are valid, the Property::Value of the index is returned.
246   value = map.Find( indexKeyValid, stringKeyValid );
247
248   DALI_TEST_EQUALS( value->Get<int>(), indexValueValid, TEST_LOCATION );
249
250
251   // TEST: If only the index is valid, the Property::Value of the index is returned.
252   value = map.Find( indexKeyValid, stringKeyInvalid );
253
254   DALI_TEST_EQUALS( value->Get<int>(), indexValueValid, TEST_LOCATION );
255
256
257   // TEST: If only the string is valid, the Property::Value of the string is returned.
258   value = map.Find( indexKeyInvalid, stringKeyValid );
259
260   DALI_TEST_EQUALS( value->Get<std::string>(), stringValueValid, TEST_LOCATION );
261
262
263   // TEST: If neither the index or string are valid, then a NULL pointer is returned.
264   value = map.Find( indexKeyInvalid, stringKeyInvalid );
265
266   DALI_TEST_CHECK( value == NULL );
267
268   END_TEST;
269 }
270
271 int UtcDaliPropertyMapOperatorIndex(void)
272 {
273   Property::Map map;
274   map[ "hello" ] = 1;
275   map[ 10 ] = "DALi";
276   map[ "world" ] = 2;
277   map[ 100 ] = 9;
278
279   const Property::Map map2 = map;
280   const Property::Value& value10 = map2[10];
281   DALI_TEST_EQUALS( value10.Get<std::string>(), "DALi", TEST_LOCATION );
282
283   const Property::Value& value100 = map2[100];
284   DALI_TEST_EQUALS( value100.Get<int>(), 9, TEST_LOCATION );
285
286   const Property::Value& valueHello = map2["hello"];
287   DALI_TEST_EQUALS( valueHello.Get<int>(), 1, TEST_LOCATION );
288
289   END_TEST;
290 }
291
292 int UtcDaliPropertyMapInsertP(void)
293 {
294   Property::Map map;
295   DALI_TEST_EQUALS( 0u, map.Count(), TEST_LOCATION );
296   map.Insert( "foo", "bar");
297   DALI_TEST_EQUALS( 1u, map.Count(), TEST_LOCATION );
298   Property::Value* value = map.Find( "foo" );
299   DALI_TEST_CHECK( value );
300   DALI_TEST_EQUALS( "bar", value->Get<std::string>(), TEST_LOCATION );
301
302   map.Insert( std::string("foo2"), "testing" );
303   DALI_TEST_EQUALS( 2u, map.Count(), TEST_LOCATION );
304   value = map.Find( "foo2" );
305   DALI_TEST_CHECK( value );
306   DALI_TEST_EQUALS( "testing", value->Get<std::string>(), TEST_LOCATION );
307
308   map.Insert( 10, "DALi" );
309   DALI_TEST_EQUALS( 3u, map.Count(), TEST_LOCATION );
310   value = map.Find( 10 );
311   DALI_TEST_CHECK( value );
312   DALI_TEST_EQUALS( "DALi", value->Get<std::string>(), TEST_LOCATION );
313
314   map.Insert( 100, 9 );
315   DALI_TEST_EQUALS( 4u, map.Count(), TEST_LOCATION );
316   value = map.Find( 100 );
317   DALI_TEST_CHECK( value );
318   DALI_TEST_CHECK( value->Get<int>() == 9 );
319
320   END_TEST;
321 }
322
323
324 int UtcDaliPropertyMapAddP(void)
325 {
326   Property::Map map;
327   DALI_TEST_EQUALS( 0u, map.Count(), TEST_LOCATION );
328   map.Add( "foo", "bar");
329   DALI_TEST_EQUALS( 1u, map.Count(), TEST_LOCATION );
330   Property::Value* value = map.Find( "foo" );
331   DALI_TEST_CHECK( value );
332   DALI_TEST_EQUALS( "bar", value->Get<std::string>(), TEST_LOCATION );
333
334   map.Add( std::string("foo2"), "testing" );
335   DALI_TEST_EQUALS( 2u, map.Count(), TEST_LOCATION );
336   value = map.Find( "foo2" );
337   DALI_TEST_CHECK( value );
338   DALI_TEST_EQUALS( "testing", value->Get<std::string>(), TEST_LOCATION );
339
340   map.Add( 10, "DALi" );
341   DALI_TEST_EQUALS( 3u, map.Count(), TEST_LOCATION );
342   value = map.Find( 10 );
343   DALI_TEST_CHECK( value );
344   DALI_TEST_EQUALS( "DALi", value->Get<std::string>(), TEST_LOCATION );
345
346   map.Add( 100, 9 );
347   DALI_TEST_EQUALS( 4u, map.Count(), TEST_LOCATION );
348   value = map.Find( 100 );
349   DALI_TEST_CHECK( value );
350   DALI_TEST_CHECK( value->Get<int>() == 9 );
351
352   END_TEST;
353 }
354
355 int UtcDaliPropertyMapAddChainP(void)
356 {
357   Property::Map map;
358   DALI_TEST_EQUALS( 0u, map.Count(), TEST_LOCATION );
359   map
360     .Add( "foo", "bar")
361     .Add( std::string("foo2"), "testing" )
362     .Add( 10, "DALi" )
363     .Add( 100, 9 );
364
365   DALI_TEST_EQUALS( 4u, map.Count(), TEST_LOCATION );
366
367   Property::Value* value = map.Find( "foo" );
368   DALI_TEST_CHECK( value );
369   DALI_TEST_EQUALS( "bar", value->Get<std::string>(), TEST_LOCATION );
370
371   value = map.Find( "foo2" );
372   DALI_TEST_CHECK( value );
373   DALI_TEST_EQUALS( "testing", value->Get<std::string>(), TEST_LOCATION );
374
375   value = map.Find( 10 );
376   DALI_TEST_CHECK( value );
377   DALI_TEST_EQUALS( "DALi", value->Get<std::string>(), TEST_LOCATION );
378
379   value = map.Find( 100 );
380   DALI_TEST_CHECK( value );
381   DALI_TEST_CHECK( value->Get<int>() == 9 );
382
383   END_TEST;
384 }
385
386 int UtcDaliPropertyMapAnonymousAddChainP(void)
387 {
388   class TestMap
389   {
390   public:
391     TestMap(Property::Map map)
392     : mMap(map)
393     {
394     }
395     Property::Map mMap;
396   };
397
398   TestMap mapTest( Property::Map().Add( "foo", "bar")
399                                   .Add( std::string("foo2"), "testing" )
400                                   .Add( 10, "DALi" )
401                                   .Add( 100, 9 ));
402
403
404   Property::Value* value = mapTest.mMap.Find( "foo" );
405   DALI_TEST_CHECK( value );
406   DALI_TEST_EQUALS( "bar", value->Get<std::string>(), TEST_LOCATION );
407
408   value = mapTest.mMap.Find( "foo2" );
409   DALI_TEST_CHECK( value );
410   DALI_TEST_EQUALS( "testing", value->Get<std::string>(), TEST_LOCATION );
411
412   value = mapTest.mMap.Find( 10 );
413   DALI_TEST_CHECK( value );
414   DALI_TEST_EQUALS( "DALi", value->Get<std::string>(), TEST_LOCATION );
415
416   value = mapTest.mMap.Find( 100 );
417   DALI_TEST_CHECK( value );
418   DALI_TEST_CHECK( value->Get<int>() == 9 );
419
420   END_TEST;
421 }
422
423
424 int UtcDaliPropertyMapMerge(void)
425 {
426   Property::Map map;
427   map[ "hello" ] = 1;
428   map[ 10 ] = "DALi";
429   map[ "world" ] = 2;
430
431   DALI_TEST_CHECK( map.Count() == 3 );
432
433   // Create another map with the same keys but different values
434   Property::Map map2;
435   map2[ "hello" ] = 3;
436   map2[ "world" ] = 4;
437   map[ 10 ] = "3DEngine";
438
439   // Merge map2 into map1, count should still be 2, map values should be from map2
440   map.Merge( map2 );
441   DALI_TEST_CHECK( map.Count() == 3 );
442   DALI_TEST_CHECK( map[ "hello" ].Get< int >() == 3 );
443   DALI_TEST_CHECK( map[ "world"].Get< int >() == 4 );
444   DALI_TEST_EQUALS( "3DEngine", map[ 10 ].Get<std::string>(), TEST_LOCATION );
445
446   // Create another map with different keys
447   Property::Map map3;
448   map3[ "foo" ] = 5;
449   map3[ 100 ] = 6;
450
451   // Merge map3 into map1, count should increase, existing values should match previous and new values should match map3
452   map.Merge( map3 );
453   DALI_TEST_CHECK( map.Count() == 5 );
454   DALI_TEST_CHECK( map[ "hello" ].Get< int >() == 3 );
455   DALI_TEST_CHECK( map[ "world"].Get< int >() == 4 );
456   DALI_TEST_CHECK( map[ "foo"].Get< int >() == 5 );
457   DALI_TEST_EQUALS( "3DEngine", map[ 10 ].Get<std::string>(), TEST_LOCATION );
458   DALI_TEST_CHECK( map[ 100].Get< int >() == 6 );
459
460   // Create an empty map and attempt to merge, should be successful, nothing should change
461   Property::Map map4;
462   DALI_TEST_CHECK( map4.Empty() );
463   map.Merge( map4 );
464   DALI_TEST_CHECK( map4.Empty() );
465   DALI_TEST_CHECK( map.Count() == 5 );
466   DALI_TEST_CHECK( map[ "hello" ].Get< int >() == 3 );
467   DALI_TEST_CHECK( map[ "world"].Get< int >() == 4 );
468   DALI_TEST_CHECK( map[ "foo"].Get< int >() == 5 );
469   DALI_TEST_EQUALS( "3DEngine", map[ 10 ].Get<std::string>(), TEST_LOCATION );
470   DALI_TEST_CHECK( map[ 100 ].Get< int >() == 6 );
471
472   // Merge map into map4, map4 should be the same as map now.
473   map4.Merge( map );
474   DALI_TEST_CHECK( map4.Count() == 5 );
475   DALI_TEST_CHECK( map4[ "hello" ].Get< int >() == 3 );
476   DALI_TEST_CHECK( map4[ "world"].Get< int >() == 4 );
477   DALI_TEST_CHECK( map4[ "foo"].Get< int >() == 5 );
478   DALI_TEST_EQUALS( "3DEngine", map[ 10 ].Get<std::string>(), TEST_LOCATION );
479   DALI_TEST_CHECK( map4[ 100 ].Get< int >() == 6 );
480
481   // Attempt to merge into itself, should be successful, nothing should change
482   map.Merge( map );
483   DALI_TEST_CHECK( map.Count() == 5 );
484   DALI_TEST_CHECK( map[ "hello" ].Get< int >() == 3 );
485   DALI_TEST_CHECK( map[ "world"].Get< int >() == 4 );
486   DALI_TEST_CHECK( map[ "foo"].Get< int >() == 5 );
487   DALI_TEST_EQUALS( "3DEngine", map[ 10 ].Get<std::string>(), TEST_LOCATION );
488   DALI_TEST_CHECK( map[ 100 ].Get< int >() == 6 );
489
490   END_TEST;
491 }
492
493 int UtcDaliPropertyMapOstream01(void)
494 {
495   Property::Map map;
496
497   map.Insert("duration", 5.0f);
498   map.Insert( 10, "DALi" );
499   map.Insert("delay", 1.0f);
500   map.Insert( 100, 9 );
501   map.Insert("value", 100);
502
503   std::ostringstream oss;
504   oss << map;
505
506   tet_printf("Testing ouput of map: %s\n", oss.str().c_str());
507
508   // string-value pairs first, then index-value pairs
509   DALI_TEST_EQUALS( oss.str().compare("Map(5) = {duration:5, delay:1, value:100, 10:DALi, 100:9}"), 0, TEST_LOCATION );
510
511   END_TEST;
512 }
513
514
515 int UtcDaliPropertyMapOstream02(void)
516 {
517   Property::Map map, map2;
518
519   map2.Insert("duration", 5.0f);
520   map2.Insert("delay", 1.0f);
521   map2.Insert( 10, "DALi" );
522   map.Insert("timePeriod", map2);
523   map.Insert( 100, 9 );
524   map.Insert("value", 100);
525
526   std::ostringstream oss;
527   oss << map;
528
529   tet_printf("Testing ouput of map: %s\n", oss.str().c_str());
530
531   // string-value pairs first, then index-value pairs
532   DALI_TEST_EQUALS( oss.str().compare("Map(3) = {timePeriod:Map(3) = {duration:5, delay:1, 10:DALi}, value:100, 100:9}"), 0, TEST_LOCATION );
533
534   END_TEST;
535 }