2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include <dali/public-api/dali-core.h>
22 #include <dali-test-suite-utils.h>
26 void utc_dali_property_map_startup(void)
28 test_return_value = TET_UNDEF;
31 void utc_dali_property_map_cleanup(void)
33 test_return_value = TET_PASS;
36 int UtcDaliPropertyMapPopulate(void)
39 DALI_TEST_CHECK( map.Empty() );
43 map[ "world" ] = "world";
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 );
54 DALI_TEST_CHECK( map.Empty() );
58 int UtcDaliPropertyMapCopyAndAssignment(void)
65 Property::Map assignedMap;
66 assignedMap[ "foo" ] = 3;
67 assignedMap[ 100 ] = 9;
68 DALI_TEST_CHECK( assignedMap.Count() == 2 );
70 DALI_TEST_CHECK( assignedMap.Count() == 3 );
72 Property::Map copiedMap( map );
73 DALI_TEST_CHECK( copiedMap.Count() == 3 );
76 DALI_TEST_CHECK( map.Count() == 3 );
78 DALI_TEST_CHECK( map.Count() == 3 );
83 int UtcDaliPropertyMapConstOperator(void)
89 DALI_TEST_CHECK( map.Count() == 3 );
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
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
101 constMap[ "invalidKey" ];
102 tet_result( TET_FAIL );
104 catch ( DaliException& e )
106 DALI_TEST_ASSERT( e, "! \"Invalid Key\"", TEST_LOCATION );
112 int UtcDaliPropertyMapGetValue(void)
117 map[ Actor::Property::COLOR ] = Color::MAGENTA;
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 );
124 Property::Value& value2 = map.GetValue( 2 );
125 DALI_TEST_CHECK( value2.Get<Vector4>() == Color::MAGENTA );
126 value2 = Color::CYAN;
127 DALI_TEST_EQUALS( map[ Actor::Property::COLOR].Get<Vector4>(), Color::CYAN, TEST_LOCATION);
133 tet_result( TET_FAIL );
135 catch ( DaliException& e )
137 DALI_TEST_ASSERT( e, "position", TEST_LOCATION );
143 // deprecated API, only retrieve the key from the string-value pairs
144 int UtcDaliPropertyMapGetKey(void)
149 map[ Actor::Property::COLOR ] = Color::MAGENTA;
151 DALI_TEST_CHECK( map.GetKey( 0 ) == "hello" );
152 DALI_TEST_CHECK( map.GetKey( 1 ) == "world" );
158 tet_result( TET_FAIL );
160 catch ( DaliException& e )
162 DALI_TEST_ASSERT( e, "position", TEST_LOCATION );
169 tet_result( TET_FAIL );
171 catch ( DaliException& e )
173 DALI_TEST_ASSERT( e, "position", TEST_LOCATION );
180 int UtcDaliPropertyMapGetKeyAt(void)
185 map[ Actor::Property::COLOR ] = Color::MAGENTA;
187 DALI_TEST_CHECK( map.GetKeyAt( 0 ) == "hello" );
188 DALI_TEST_CHECK( map.GetKeyAt( 1 ) == "world" );
189 DALI_TEST_CHECK( map.GetKeyAt( 2 ) == Actor::Property::COLOR );
195 tet_result( TET_FAIL );
197 catch ( DaliException& e )
199 DALI_TEST_ASSERT( e, "position", TEST_LOCATION );
205 int UtcDaliPropertyMapGetPair(void)
210 map[ Actor::Property::COLOR ] = Color::MAGENTA;
212 DALI_TEST_CHECK( map.GetPair( 0 ).first == "hello" );
213 DALI_TEST_CHECK( map.GetPair( 0 ).second.Get< int >() == 1 );
214 DALI_TEST_CHECK( map.GetPair( 1 ).first == "world" );
215 DALI_TEST_CHECK( map.GetPair( 1 ).second.Get< int >() == 2 );
221 tet_result( TET_FAIL );
223 catch ( DaliException& e )
225 DALI_TEST_ASSERT( e, "position", TEST_LOCATION );
232 tet_result( TET_FAIL );
234 catch ( DaliException& e )
236 DALI_TEST_ASSERT( e, "position", TEST_LOCATION );
242 int UtcDaliPropertyMapGetKeyValue(void)
247 map[ Actor::Property::COLOR ] = Color::MAGENTA;
249 DALI_TEST_CHECK( map.GetKeyValue( 0 ).first == "hello" );
250 DALI_TEST_CHECK( map.GetKeyValue( 0 ).second.Get< int >() == 1 );
251 DALI_TEST_CHECK( map.GetKeyValue( 1 ).first == "world" );
252 DALI_TEST_CHECK( map.GetKeyValue( 1 ).second.Get< int >() == 2 );
253 DALI_TEST_CHECK( map.GetKeyValue( 2 ).first == Actor::Property::COLOR );
254 DALI_TEST_CHECK( map.GetKeyValue( 2 ).second.Get< Vector4 >() == Color::MAGENTA );
260 tet_result( TET_FAIL );
262 catch ( DaliException& e )
264 DALI_TEST_ASSERT( e, "position", TEST_LOCATION );
270 int UtcDaliPropertyMapFind(void)
278 Property::Value* value = NULL;
280 value = map.Find( "hello" );
281 DALI_TEST_CHECK( value );
282 DALI_TEST_CHECK( value->Get<int>() == 1 );
284 const std::string world("world");
285 value = map.Find( world );
286 DALI_TEST_CHECK( value );
287 DALI_TEST_CHECK( value->Get<int>() == 2 );
289 value = map.Find( 100 );
290 DALI_TEST_CHECK( value );
291 DALI_TEST_CHECK( value->Get<int>() == 9 );
293 value = map.Find( 10, Property::STRING );
294 DALI_TEST_CHECK( value );
295 DALI_TEST_EQUALS( "DALi", value->Get<std::string>(), TEST_LOCATION );
297 value = map.Find( 10, Property::INTEGER );
298 DALI_TEST_CHECK( value == NULL );
300 value = map.Find( "invalidKey" );
301 DALI_TEST_CHECK( !value );
306 int UtcDaliPropertyMapFindIndexThenString(void)
308 // Define the valid keys and values to test with.
309 std::string stringKeyValid = "bar";
310 std::string stringKeyInvalid = "aardvark";
311 int indexKeyValid = 100;
312 int indexKeyInvalid = 101;
314 // Define invalid key and value to test with.
315 std::string stringValueValid = "DALi";
316 int indexValueValid = 3;
318 // Set up a property map containing the valid keys and values defined above.
321 map[ 10 ] = "string";
322 map[ stringKeyValid ] = stringValueValid;
323 map[ indexKeyValid ] = indexValueValid;
325 Property::Value* value = NULL;
327 // TEST: If both index and string are valid, the Property::Value of the index is returned.
328 value = map.Find( indexKeyValid, stringKeyValid );
330 DALI_TEST_EQUALS( value->Get<int>(), indexValueValid, TEST_LOCATION );
333 // TEST: If only the index is valid, the Property::Value of the index is returned.
334 value = map.Find( indexKeyValid, stringKeyInvalid );
336 DALI_TEST_EQUALS( value->Get<int>(), indexValueValid, TEST_LOCATION );
339 // TEST: If only the string is valid, the Property::Value of the string is returned.
340 value = map.Find( indexKeyInvalid, stringKeyValid );
342 DALI_TEST_EQUALS( value->Get<std::string>(), stringValueValid, TEST_LOCATION );
345 // TEST: If neither the index or string are valid, then a NULL pointer is returned.
346 value = map.Find( indexKeyInvalid, stringKeyInvalid );
348 DALI_TEST_CHECK( value == NULL );
353 int UtcDaliPropertyMapOperatorIndex(void)
361 const Property::Map map2 = map;
362 const Property::Value& value10 = map2[10];
363 DALI_TEST_EQUALS( value10.Get<std::string>(), "DALi", TEST_LOCATION );
365 const Property::Value& value100 = map2[100];
366 DALI_TEST_EQUALS( value100.Get<int>(), 9, TEST_LOCATION );
368 const Property::Value& valueHello = map2["hello"];
369 DALI_TEST_EQUALS( valueHello.Get<int>(), 1, TEST_LOCATION );
374 int UtcDaliPropertyMapInsertP(void)
377 DALI_TEST_EQUALS( 0u, map.Count(), TEST_LOCATION );
378 map.Insert( "foo", "bar");
379 DALI_TEST_EQUALS( 1u, map.Count(), TEST_LOCATION );
380 Property::Value* value = map.Find( "foo" );
381 DALI_TEST_CHECK( value );
382 DALI_TEST_EQUALS( "bar", value->Get<std::string>(), TEST_LOCATION );
384 map.Insert( std::string("foo2"), "testing" );
385 DALI_TEST_EQUALS( 2u, map.Count(), TEST_LOCATION );
386 value = map.Find( "foo2" );
387 DALI_TEST_CHECK( value );
388 DALI_TEST_EQUALS( "testing", value->Get<std::string>(), TEST_LOCATION );
390 map.Insert( 10, "DALi" );
391 DALI_TEST_EQUALS( 3u, map.Count(), TEST_LOCATION );
392 value = map.Find( 10 );
393 DALI_TEST_CHECK( value );
394 DALI_TEST_EQUALS( "DALi", value->Get<std::string>(), TEST_LOCATION );
396 map.Insert( 100, 9 );
397 DALI_TEST_EQUALS( 4u, map.Count(), TEST_LOCATION );
398 value = map.Find( 100 );
399 DALI_TEST_CHECK( value );
400 DALI_TEST_CHECK( value->Get<int>() == 9 );
406 int UtcDaliPropertyMapAddP(void)
409 DALI_TEST_EQUALS( 0u, map.Count(), TEST_LOCATION );
410 map.Add( "foo", "bar");
411 DALI_TEST_EQUALS( 1u, map.Count(), TEST_LOCATION );
412 Property::Value* value = map.Find( "foo" );
413 DALI_TEST_CHECK( value );
414 DALI_TEST_EQUALS( "bar", value->Get<std::string>(), TEST_LOCATION );
416 map.Add( std::string("foo2"), "testing" );
417 DALI_TEST_EQUALS( 2u, map.Count(), TEST_LOCATION );
418 value = map.Find( "foo2" );
419 DALI_TEST_CHECK( value );
420 DALI_TEST_EQUALS( "testing", value->Get<std::string>(), TEST_LOCATION );
422 map.Add( 10, "DALi" );
423 DALI_TEST_EQUALS( 3u, map.Count(), TEST_LOCATION );
424 value = map.Find( 10 );
425 DALI_TEST_CHECK( value );
426 DALI_TEST_EQUALS( "DALi", value->Get<std::string>(), TEST_LOCATION );
429 DALI_TEST_EQUALS( 4u, map.Count(), TEST_LOCATION );
430 value = map.Find( 100 );
431 DALI_TEST_CHECK( value );
432 DALI_TEST_CHECK( value->Get<int>() == 9 );
437 int UtcDaliPropertyMapAddChainP(void)
440 DALI_TEST_EQUALS( 0u, map.Count(), TEST_LOCATION );
443 .Add( std::string("foo2"), "testing" )
447 DALI_TEST_EQUALS( 4u, map.Count(), TEST_LOCATION );
449 Property::Value* value = map.Find( "foo" );
450 DALI_TEST_CHECK( value );
451 DALI_TEST_EQUALS( "bar", value->Get<std::string>(), TEST_LOCATION );
453 value = map.Find( "foo2" );
454 DALI_TEST_CHECK( value );
455 DALI_TEST_EQUALS( "testing", value->Get<std::string>(), TEST_LOCATION );
457 value = map.Find( 10 );
458 DALI_TEST_CHECK( value );
459 DALI_TEST_EQUALS( "DALi", value->Get<std::string>(), TEST_LOCATION );
461 value = map.Find( 100 );
462 DALI_TEST_CHECK( value );
463 DALI_TEST_CHECK( value->Get<int>() == 9 );
468 int UtcDaliPropertyMapAnonymousAddChainP(void)
473 TestMap(Property::Map map)
480 TestMap mapTest( Property::Map().Add( "foo", "bar")
481 .Add( std::string("foo2"), "testing" )
486 Property::Value* value = mapTest.mMap.Find( "foo" );
487 DALI_TEST_CHECK( value );
488 DALI_TEST_EQUALS( "bar", value->Get<std::string>(), TEST_LOCATION );
490 value = mapTest.mMap.Find( "foo2" );
491 DALI_TEST_CHECK( value );
492 DALI_TEST_EQUALS( "testing", value->Get<std::string>(), TEST_LOCATION );
494 value = mapTest.mMap.Find( 10 );
495 DALI_TEST_CHECK( value );
496 DALI_TEST_EQUALS( "DALi", value->Get<std::string>(), TEST_LOCATION );
498 value = mapTest.mMap.Find( 100 );
499 DALI_TEST_CHECK( value );
500 DALI_TEST_CHECK( value->Get<int>() == 9 );
506 int UtcDaliPropertyMapMerge(void)
513 DALI_TEST_CHECK( map.Count() == 3 );
515 // Create another map with the same keys but different values
519 map[ 10 ] = "3DEngine";
521 // Merge map2 into map1, count should still be 2, map values should be from map2
523 DALI_TEST_CHECK( map.Count() == 3 );
524 DALI_TEST_CHECK( map[ "hello" ].Get< int >() == 3 );
525 DALI_TEST_CHECK( map[ "world"].Get< int >() == 4 );
526 DALI_TEST_EQUALS( "3DEngine", map[ 10 ].Get<std::string>(), TEST_LOCATION );
528 // Create another map with different keys
533 // Merge map3 into map1, count should increase, existing values should match previous and new values should match map3
535 DALI_TEST_CHECK( map.Count() == 5 );
536 DALI_TEST_CHECK( map[ "hello" ].Get< int >() == 3 );
537 DALI_TEST_CHECK( map[ "world"].Get< int >() == 4 );
538 DALI_TEST_CHECK( map[ "foo"].Get< int >() == 5 );
539 DALI_TEST_EQUALS( "3DEngine", map[ 10 ].Get<std::string>(), TEST_LOCATION );
540 DALI_TEST_CHECK( map[ 100].Get< int >() == 6 );
542 // Create an empty map and attempt to merge, should be successful, nothing should change
544 DALI_TEST_CHECK( map4.Empty() );
546 DALI_TEST_CHECK( map4.Empty() );
547 DALI_TEST_CHECK( map.Count() == 5 );
548 DALI_TEST_CHECK( map[ "hello" ].Get< int >() == 3 );
549 DALI_TEST_CHECK( map[ "world"].Get< int >() == 4 );
550 DALI_TEST_CHECK( map[ "foo"].Get< int >() == 5 );
551 DALI_TEST_EQUALS( "3DEngine", map[ 10 ].Get<std::string>(), TEST_LOCATION );
552 DALI_TEST_CHECK( map[ 100 ].Get< int >() == 6 );
554 // Merge map into map4, map4 should be the same as map now.
556 DALI_TEST_CHECK( map4.Count() == 5 );
557 DALI_TEST_CHECK( map4[ "hello" ].Get< int >() == 3 );
558 DALI_TEST_CHECK( map4[ "world"].Get< int >() == 4 );
559 DALI_TEST_CHECK( map4[ "foo"].Get< int >() == 5 );
560 DALI_TEST_EQUALS( "3DEngine", map[ 10 ].Get<std::string>(), TEST_LOCATION );
561 DALI_TEST_CHECK( map4[ 100 ].Get< int >() == 6 );
563 // Attempt to merge into itself, should be successful, nothing should change
565 DALI_TEST_CHECK( map.Count() == 5 );
566 DALI_TEST_CHECK( map[ "hello" ].Get< int >() == 3 );
567 DALI_TEST_CHECK( map[ "world"].Get< int >() == 4 );
568 DALI_TEST_CHECK( map[ "foo"].Get< int >() == 5 );
569 DALI_TEST_EQUALS( "3DEngine", map[ 10 ].Get<std::string>(), TEST_LOCATION );
570 DALI_TEST_CHECK( map[ 100 ].Get< int >() == 6 );
575 int UtcDaliPropertyMapOstream01(void)
579 map.Insert("duration", 5.0f);
580 map.Insert( 10, "DALi" );
581 map.Insert("delay", 1.0f);
582 map.Insert( 100, 9 );
583 map.Insert("value", 100);
585 std::ostringstream oss;
588 tet_printf("Testing ouput of map: %s\n", oss.str().c_str());
590 // string-value pairs first, then index-value pairs
591 DALI_TEST_EQUALS( oss.str().compare("Map(5) = {duration:5, delay:1, value:100, 10:DALi, 100:9}"), 0, TEST_LOCATION );
597 int UtcDaliPropertyMapOstream02(void)
599 Property::Map map, map2;
601 map2.Insert("duration", 5.0f);
602 map2.Insert("delay", 1.0f);
603 map2.Insert( 10, "DALi" );
604 map.Insert("timePeriod", map2);
605 map.Insert( 100, 9 );
606 map.Insert("value", 100);
608 std::ostringstream oss;
611 tet_printf("Testing ouput of map: %s\n", oss.str().c_str());
613 // string-value pairs first, then index-value pairs
614 DALI_TEST_EQUALS( oss.str().compare("Map(3) = {timePeriod:Map(3) = {duration:5, delay:1, 10:DALi}, value:100, 100:9}"), 0, TEST_LOCATION );
619 int UtcDaliPropertyKeyConstructorP(void)
621 Property::Key key1( "aKey" );
622 DALI_TEST_EQUALS( key1.type, Property::Key::STRING, TEST_LOCATION );
623 DALI_TEST_EQUALS( key1.stringKey, "aKey", TEST_LOCATION );
624 DALI_TEST_EQUALS( key1.indexKey, Property::INVALID_INDEX, TEST_LOCATION );
626 Property::Key key2( Actor::Property::COLOR );
627 DALI_TEST_EQUALS( key2.type, Property::Key::INDEX, TEST_LOCATION );
628 DALI_TEST_EQUALS( key2.indexKey, (Dali::Property::Index)Actor::Property::COLOR, TEST_LOCATION );
632 int UtcDaliPropertyKeyEqualityOperatorP(void)
634 Property::Key key1( "aKey" );
635 Property::Key key2( 113 );
637 DALI_TEST_CHECK( key1 == "aKey" );
638 DALI_TEST_CHECK( ! (key1 == "notTheKey") );
639 DALI_TEST_CHECK( ! (key1 == 1) );
641 DALI_TEST_CHECK( key2 == 113 );
642 DALI_TEST_CHECK( ! (key2 == 0) );
643 DALI_TEST_CHECK( ! (key2 == "One hundred and thirteen" ) );
645 DALI_TEST_CHECK( ! (key1 == key2) );
646 DALI_TEST_CHECK( key1 != key2 );
648 Property::Key key1B( "aKey" );
649 Property::Key key2B( 113 );
651 DALI_TEST_CHECK( key1 == key1B );
652 DALI_TEST_CHECK( key2 == key2B );
657 int UtcDaliPropertyKeyInequalityOperatorP(void)
659 Property::Key key1( "aKey" );
660 Property::Key key2( 113 );
662 DALI_TEST_CHECK( key1 != "notTheKey" );
663 DALI_TEST_CHECK( key1 != 1 );
665 DALI_TEST_CHECK( key2 != 0 );
666 DALI_TEST_CHECK( key2 != "One hundred and thirteen" );
668 DALI_TEST_CHECK( key1 != key2 );
674 int UtcDaliPropertyKeyOutputStream(void)
676 Property::Key key1( "aKey" );
677 Property::Key key2( 113 );
679 std::ostringstream oss;
681 DALI_TEST_EQUALS( oss.str(), "aKey", TEST_LOCATION );
683 std::ostringstream oss2;
685 DALI_TEST_EQUALS( oss2.str(), "113", TEST_LOCATION );