Merge "Added P/N test cases, fixed last 2 cases." into tizen
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / utc-Dali-PropertyValue.cpp
1 /*
2  * Copyright (c) 2015 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 <iomanip>
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 namespace
27 {
28
29 void CheckTypeName(const Property::Type& type)
30 {
31   switch(type)
32   {
33     case Property::NONE:
34     {
35       DALI_TEST_CHECK( "NONE" == std::string(PropertyTypes::GetName( type ) ) );
36       break;
37     }
38     case Property::BOOLEAN:
39     {
40       DALI_TEST_CHECK( "BOOLEAN" == std::string(PropertyTypes::GetName( type ) ) );
41       break;
42     }
43     case Property::FLOAT:
44     {
45       DALI_TEST_CHECK( "FLOAT" == std::string(PropertyTypes::GetName( type ) ) );
46       break;
47     }
48     case Property::INTEGER:
49     {
50       DALI_TEST_CHECK( "INTEGER" == std::string(PropertyTypes::GetName( type ) ) );
51       break;
52     }
53     case Property::UNSIGNED_INTEGER:
54     {
55       DALI_TEST_CHECK( "UNSIGNED_INTEGER" == std::string(PropertyTypes::GetName( type ) ) );
56       break;
57     }
58     case Property::VECTOR2:
59     {
60       DALI_TEST_CHECK( "VECTOR2" == std::string(PropertyTypes::GetName( type ) ) );
61       break;
62     }
63     case Property::VECTOR3:
64     {
65       DALI_TEST_CHECK( "VECTOR3" == std::string(PropertyTypes::GetName( type ) ) );
66       break;
67     }
68     case Property::VECTOR4:
69     {
70       DALI_TEST_CHECK( "VECTOR4" == std::string(PropertyTypes::GetName( type ) ) );
71       break;
72     }
73     case Property::MATRIX3:
74     {
75       DALI_TEST_CHECK( "MATRIX3" == std::string(PropertyTypes::GetName( type  ) ) );
76       break;
77     }
78     case Property::MATRIX:
79     {
80       DALI_TEST_CHECK( "MATRIX" == std::string(PropertyTypes::GetName( type ) ) );
81       break;
82     }
83     case Property::RECTANGLE:
84     {
85       DALI_TEST_CHECK( "RECTANGLE" == std::string(PropertyTypes::GetName( type ) ) );
86       break;
87     }
88     case Property::ROTATION:
89     {
90       DALI_TEST_CHECK( "ROTATION" == std::string(PropertyTypes::GetName( type ) ) );
91       break;
92     }
93     case Property::STRING:
94     {
95       DALI_TEST_CHECK( "STRING" == std::string(PropertyTypes::GetName( type ) ) );
96       break;
97     }
98     case Property::ARRAY:
99     {
100       DALI_TEST_CHECK( "ARRAY" == std::string(PropertyTypes::GetName( type ) ) );
101       break;
102     }
103     case Property::MAP:
104     {
105       DALI_TEST_CHECK( "MAP" == std::string(PropertyTypes::GetName( type ) ) );
106       break;
107     }
108     case Property::TYPE_COUNT:
109     {
110       DALI_TEST_CHECK( "NONE" == std::string(PropertyTypes::GetName( type ) ) );
111       break;
112     }
113   } // switch(type)
114
115 } // CheckTypeName
116
117 } // unnamed namespace
118
119 void utc_dali_property_value_startup(void)
120 {
121   test_return_value = TET_UNDEF;
122 }
123
124 void utc_dali_property_value_cleanup(void)
125 {
126   test_return_value = TET_PASS;
127 }
128
129 int UtcDaliPropertyValueConstructors01(void)
130 {
131   // Testing PropertyValue construction using Property::Type
132   // also testing the PropertyTypes::GetName is correct for each type
133
134   TestApplication application;
135   tet_infoline("Testing Property::Value construction using  Property::Type");
136
137   Property::Value value;
138   DALI_TEST_CHECK( value.GetType() == Property::NONE );
139
140   Property::Type type = Property::NONE;
141   CheckTypeName(type);
142   // Value(Value&) ctor and Value(Type&) ctor
143   DALI_TEST_CHECK( Property::Value(Property::Value(type)).GetType() == type );
144   DALI_TEST_CHECK( Property::NONE == type );
145
146   // PropertyTypes
147   type = Property::BOOLEAN;
148   CheckTypeName(type);
149   DALI_TEST_CHECK( Property::Value(Property::Value(type)).GetType() == type );
150   DALI_TEST_CHECK( PropertyTypes::Get<bool>()            == type );
151
152   type = Property::FLOAT;
153   CheckTypeName(type);
154   DALI_TEST_CHECK( Property::Value(Property::Value(type)).GetType() == type );
155   DALI_TEST_CHECK( PropertyTypes::Get<float>()           == type );
156
157   type = Property::INTEGER;
158   CheckTypeName(type);
159   DALI_TEST_CHECK( Property::Value(Property::Value(type)).GetType() == type );
160   DALI_TEST_CHECK( PropertyTypes::Get<int>()             == type );
161
162   type = Property::UNSIGNED_INTEGER;
163   CheckTypeName(type);
164   DALI_TEST_CHECK( Property::Value(Property::Value(type)).GetType() == type );
165   DALI_TEST_CHECK( PropertyTypes::Get<unsigned int>()    == type );
166
167   type = Property::VECTOR2;
168   CheckTypeName(type);
169   DALI_TEST_CHECK( Property::Value(Property::Value(type)).GetType() == type );
170   DALI_TEST_CHECK( PropertyTypes::Get<Vector2>()         == type );
171
172   type = Property::VECTOR3;
173   CheckTypeName(type);
174   DALI_TEST_CHECK( Property::Value(Property::Value(type)).GetType() == type );
175   DALI_TEST_CHECK( PropertyTypes::Get<Vector3>()         == type );
176
177   type = Property::VECTOR4;
178   CheckTypeName(type);
179   DALI_TEST_CHECK( Property::Value(Property::Value(type)).GetType() == type );
180   DALI_TEST_CHECK( PropertyTypes::Get<Vector4>()         == type );
181
182   type = Property::MATRIX3;
183   CheckTypeName(type);
184   DALI_TEST_CHECK( Property::Value(Property::Value(type)).GetType() == type );
185   DALI_TEST_CHECK( PropertyTypes::Get<Matrix3>()         == type );
186
187   type = Property::MATRIX;
188   CheckTypeName(type);
189   DALI_TEST_CHECK( Property::Value(Property::Value(type)).GetType() == type );
190   DALI_TEST_CHECK( PropertyTypes::Get<Matrix>()          == type );
191
192   typedef Dali::Rect<int> Rectangle;
193   type = Property::RECTANGLE;
194   CheckTypeName(type);
195   DALI_TEST_CHECK( Property::Value(Property::Value(type)).GetType() == type );
196   DALI_TEST_CHECK( PropertyTypes::Get<Rectangle>()       == type );
197
198   type = Property::ROTATION;
199   CheckTypeName(type);
200   DALI_TEST_CHECK( Property::Value(Property::Value(type)).GetType() == type );
201   DALI_TEST_CHECK( PropertyTypes::Get<Quaternion>()      == type );
202
203   type = Property::ROTATION;
204   CheckTypeName(type);
205   DALI_TEST_CHECK( Property::Value(Property::Value(type)).GetType() == type );
206   DALI_TEST_CHECK( PropertyTypes::Get<AngleAxis>()       == type );
207
208   type = Property::STRING;
209   CheckTypeName(type);
210   DALI_TEST_CHECK( Property::Value(Property::Value(type)).GetType() == type );
211   DALI_TEST_CHECK( PropertyTypes::Get<std::string>()     == type );
212
213   type = Property::ARRAY;
214   CheckTypeName(type);
215   DALI_TEST_CHECK( Property::Value(Property::Value(type)).GetType() == type );
216   DALI_TEST_CHECK( PropertyTypes::Get<Property::Array>() == type );
217
218   type = Property::MAP;
219   CheckTypeName(type);
220   DALI_TEST_CHECK( Property::Value(Property::Value(type)).GetType() == type );
221   DALI_TEST_CHECK( PropertyTypes::Get<Property::Map>()   == type );
222   END_TEST;
223
224 }
225
226 int UtcDaliPropertyValueConstructors02(void)
227 {
228   // Testing PropertyValue construction using value / reference types
229   // also testing the PropertyTypes::Get return same value as constructed
230
231   TestApplication application;
232   tet_infoline("Testing Property::Value construction using values / references");
233
234   Property::Value value;
235
236   bool b = false;
237   value = Property::Value(true);
238   value.Get(b);
239   DALI_TEST_CHECK( true == b );
240
241   float f = 5.f;
242   value = Property::Value(10.f);
243   value.Get(f);
244   DALI_TEST_CHECK( Dali::Equals(10.f, f) );
245
246   int i = 5;
247   value = Property::Value(10);
248   value.Get(i);
249   DALI_TEST_CHECK( 10 == i );
250
251   unsigned int ui = 5;
252   value = Property::Value(10U);
253   value.Get(ui);
254   DALI_TEST_CHECK( 10 == ui );
255
256   Vector2 v2 = Vector2(0,0);
257   value = Property::Value( Vector2(1,1) );
258   value.Get(v2);
259   DALI_TEST_CHECK( Vector2(1,1) == v2 );
260
261   Vector3 v3 = Vector3(0.f,0.f,0.f);
262   value = Property::Value( Vector3(1.f,1.f,1.f) );
263   value.Get(v3);
264   DALI_TEST_CHECK( Vector3(1.f,1.f,1.f) == v3 );
265
266   Vector4 v4 = Vector4(0,0,0,0);
267   value = Property::Value( Vector4(1,1,1,1) );
268   value.Get(v4);
269   DALI_TEST_CHECK( Vector4(1,1,1,1) == v4 );
270
271   Matrix3 m3 = Matrix3(0.f,0.f,0.f,0.f,0.f,0.f,0.f,0.f,0.f);
272   value = Property::Value( Matrix3::IDENTITY );
273   value.Get(m3);
274   DALI_TEST_CHECK( Matrix3::IDENTITY == m3 );
275
276   Matrix m = Matrix(true);
277   value = Property::Value( Matrix::IDENTITY );
278   value.Get(m);
279   DALI_TEST_CHECK( Matrix::IDENTITY == m );
280
281   typedef Dali::Rect<int> Rectangle;
282   Rectangle r = Rectangle(0,0,0,0);
283   value = Property::Value( Rectangle(1,1,1,1) );
284   value.Get(r);
285   DALI_TEST_CHECK( Rectangle(1,1,1,1) == r );
286
287   Quaternion q = Quaternion(0,0,0,0);
288   value = Property::Value( Quaternion(1,1,1,1) );
289   value.Get(q);
290   DALI_TEST_CHECK( Quaternion(1,1,1,1) == q );
291
292   AngleAxis aa = AngleAxis( Degree(0), Vector3(0.f,0.f,0.f) );
293   value = Property::Value( AngleAxis( Radian(Math::PI_2), Vector3::XAXIS  ));
294   value.Get(aa);
295   Quaternion r8(Radian(Degree(aa.angle)), aa.axis);
296   DALI_TEST_EQUALS(r8, Quaternion(Radian(Math::PI_2), Vector3::XAXIS), 0.001, TEST_LOCATION);
297
298   std::string s = "no";
299   value = Property::Value("yes");
300   value.Get(s);
301   DALI_TEST_CHECK( "yes" == s );
302
303   Property::Array array;
304   value = Property::Value(Property::ARRAY);
305   value.AppendItem(10);
306   value.Get(array);
307   int getItem = 0;
308   array[0].Get(getItem);
309   DALI_TEST_CHECK( getItem == 10 );
310
311   Property::Map map;
312   value = Property::Value(Property::MAP);
313   value.SetValue("key", "value");
314   value.Get(map);
315   DALI_TEST_CHECK( map.GetKey(0) == "key" );
316
317   END_TEST;
318 }
319
320
321 int UtcDaliPropertyValueCopyConstructors01(void)
322 {
323   TestApplication application;
324   tet_infoline("Testing Property::Value copy construction using values / references");
325
326   Property::Value value;
327
328   value = Property::Value(true);
329   {
330     Property::Value copy( value );
331     DALI_TEST_CHECK( true == copy.Get<bool>() );
332   }
333
334   value = Property::Value(10.f);
335   {
336     Property::Value copy( value );
337     DALI_TEST_CHECK( Dali::Equals(10.f, copy.Get<float>() ) );
338   }
339
340   value = Property::Value(10);
341   {
342     Property::Value copy( value );
343     DALI_TEST_CHECK( 10 == copy.Get<int>() );
344   }
345
346   value = Property::Value(10U);
347   {
348     Property::Value copy( value );
349     DALI_TEST_CHECK( 10 == copy.Get< unsigned int>() );
350   }
351
352   value = Property::Value( Vector2(1,1) );
353   {
354     Property::Value copy( value );
355     DALI_TEST_CHECK( Vector2(1,1) == copy.Get<Vector2>() );
356   }
357
358   value = Property::Value( Vector3(1.f,1.f,1.f) );
359   {
360     Property::Value copy( value );
361     DALI_TEST_CHECK( Vector3(1.f,1.f,1.f) == copy.Get<Vector3>() );
362   }
363
364   value = Property::Value( Vector4(1,1,1,1) );
365   {
366     Property::Value copy( value );
367     DALI_TEST_CHECK( Vector4(1,1,1,1) == copy.Get<Vector4>() );
368   }
369
370   value = Property::Value( Matrix3::IDENTITY );
371   {
372     Property::Value copy( value );
373     DALI_TEST_CHECK( Matrix3::IDENTITY == copy.Get<Matrix3>() );
374   }
375
376   value = Property::Value( Matrix::IDENTITY );
377   {
378     Property::Value copy( value );
379     DALI_TEST_CHECK( Matrix::IDENTITY == copy.Get<Matrix>()  );
380   }
381
382   typedef Dali::Rect<int> Rectangle;
383
384   value = Property::Value( Rectangle(1,1,1,1) );
385   {
386     Property::Value copy( value );
387     Rectangle copyRect;
388     copy.Get(copyRect);
389     DALI_TEST_CHECK(  Rectangle(1,1,1,1) == copyRect);
390   }
391
392   value = Property::Value( Quaternion(1,1,1,1) );
393   {
394     Property::Value copy( value );
395     DALI_TEST_CHECK( Quaternion(1,1,1,1) == copy.Get<Quaternion>() );
396   }
397
398   value = Property::Value( AngleAxis( Radian(Math::PI_2), Vector3::XAXIS  ));
399   {
400     Property::Value copy( value );
401     DALI_TEST_CHECK( value.Get<AngleAxis>().axis == copy.Get<AngleAxis>().axis );
402     DALI_TEST_CHECK( value.Get<AngleAxis>().angle == copy.Get<AngleAxis>().angle );
403   }
404
405   value = Property::Value("yes");
406   {
407     Property::Value copy( value );
408     DALI_TEST_CHECK( "yes" == copy.Get<std::string>() );
409   }
410
411   Property::Array array;
412   value = Property::Value(Property::ARRAY);
413   value.AppendItem(10);
414   {
415     Property::Value copy( value );
416     copy.Get( array );
417     int getItem = 0;
418     array[0].Get( getItem );
419     DALI_TEST_CHECK( getItem == 10 );
420   }
421   Property::Map map;
422   value = Property::Value(Property::MAP);
423   value.SetValue("key", "value");
424   {
425     Property::Value copy( value );
426     copy.Get( map );
427     DALI_TEST_CHECK( map.GetKey(0) == "key" );
428   }
429
430   END_TEST;
431 }
432
433
434 int UtcDaliPropertyValueAssignmentOperator01(void)
435 {
436   // Testing Property Value assignment
437   TestApplication application;
438   tet_infoline("Testing Property::Value assignment operator");
439
440   Property::Value value;
441
442
443   value = Property::Value(true);
444   {
445     Property::Value copy( false );
446     copy = value;
447     DALI_TEST_CHECK( true == copy.Get<bool>() );
448   }
449
450   value = Property::Value(10.f);
451   {
452     Property::Value copy(false);
453     copy = value;
454     DALI_TEST_CHECK( Dali::Equals(10.f, copy.Get<float>() ) );
455   }
456
457   value = Property::Value(10);
458   {
459     Property::Value copy(false);
460     copy = value;
461     DALI_TEST_CHECK( 10 == copy.Get<int>() );
462   }
463
464   value = Property::Value(10U);
465   {
466     Property::Value copy(false);
467     copy = value;
468     DALI_TEST_CHECK( 10 == copy.Get< unsigned int>() );
469   }
470
471   value = Property::Value( Vector2(1,1) );
472   {
473     Property::Value copy(false);
474     copy = value;
475     DALI_TEST_CHECK( Vector2(1,1) == copy.Get<Vector2>() );
476   }
477
478   value = Property::Value( Vector3(1.f,1.f,1.f) );
479   {
480     Property::Value copy(false);
481     copy = value;
482     DALI_TEST_CHECK( Vector3(1.f,1.f,1.f) == copy.Get<Vector3>() );
483   }
484
485   value = Property::Value( Vector4(1,1,1,1) );
486   {
487     Property::Value copy(false);
488     copy = value;
489     DALI_TEST_CHECK( Vector4(1,1,1,1) == copy.Get<Vector4>() );
490   }
491
492   value = Property::Value( Matrix3::IDENTITY );
493   {
494     Property::Value copy(false);
495     copy = value;
496     DALI_TEST_CHECK( Matrix3::IDENTITY == copy.Get<Matrix3>() );
497   }
498
499   value = Property::Value( Matrix::IDENTITY );
500   {
501     Property::Value copy(false);
502     copy = value;
503     DALI_TEST_CHECK( Matrix::IDENTITY == copy.Get<Matrix>()  );
504   }
505
506   typedef Dali::Rect<int> Rectangle;
507
508   value = Property::Value( Rectangle(1,1,1,1) );
509   {
510     Property::Value copy(false);
511     copy = value;
512     Rectangle copyRect;
513     copy.Get(copyRect);
514     DALI_TEST_CHECK(  Rectangle(1,1,1,1) == copyRect);
515   }
516
517   value = Property::Value( Quaternion(1,1,1,1) );
518   {
519     Property::Value copy(false);
520     copy = value;
521     DALI_TEST_CHECK( Quaternion(1,1,1,1) == copy.Get<Quaternion>() );
522   }
523
524   value = Property::Value( AngleAxis( Radian(Math::PI_2), Vector3::XAXIS  ));
525   {
526     Property::Value copy(false);
527     copy = value;
528     DALI_TEST_CHECK( value.Get<AngleAxis>().axis == copy.Get<AngleAxis>().axis );
529     DALI_TEST_CHECK( value.Get<AngleAxis>().angle == copy.Get<AngleAxis>().angle );
530   }
531
532   value = Property::Value("yes");
533   {
534     Property::Value copy(false);
535     copy = value;
536     DALI_TEST_CHECK( "yes" == copy.Get<std::string>() );
537   }
538
539   Property::Array array;
540   value = Property::Value(Property::ARRAY);
541   value.AppendItem(10);
542   {
543     Property::Value copy(false);
544     copy = value;
545     copy.Get( array );
546     int getItem = 0;
547     array[0].Get( getItem );
548     DALI_TEST_CHECK( getItem == 10 );
549   }
550   Property::Map map;
551   value = Property::Value(Property::MAP);
552   value.SetValue("key", "value");
553   {
554     Property::Value copy(false);
555     copy = value;
556     copy.Get( map );
557     DALI_TEST_CHECK( map.GetKey(0) == "key" );
558   }
559
560   END_TEST;
561 }
562
563
564 int UtcDaliPropertyValueOutputStream(void)
565 {
566   TestApplication application;
567   tet_infoline("Testing Property::Value output stream");
568   typedef Dali::Rect<int> Rectangle;
569
570   Property::Value value(true);
571   {
572     std::ostringstream stream;
573     stream << value;
574     DALI_TEST_CHECK( stream.str() == "1")
575   }
576
577   {
578     value = Property::Value(20.2f);
579     std::ostringstream stream;
580     stream <<  value;
581     DALI_TEST_CHECK( stream.str() == "20.2")
582   }
583
584   {
585     value = Property::Value(-25);
586     std::ostringstream stream;
587     stream <<  value;
588     DALI_TEST_CHECK( stream.str() == "-25")
589   }
590
591   {
592     value = Property::Value(25U);
593     std::ostringstream stream;
594     stream <<  value;
595     DALI_TEST_CHECK( stream.str() == "25")
596   }
597
598   {
599     value = Property::Value( Vector2(1.f,1.f) );
600     std::ostringstream stream;
601     stream <<  value;
602     DALI_TEST_CHECK( stream.str() == "[1, 1]");
603   }
604
605   {
606     value = Property::Value( Vector3(1.f,1.f,1.f) );
607     std::ostringstream stream;
608     stream <<  value;
609     DALI_TEST_CHECK( stream.str() == "[1, 1, 1]");
610   }
611
612   {
613     value = Property::Value( Matrix3::IDENTITY );
614     std::ostringstream stream;
615     stream <<  value;
616     DALI_TEST_CHECK( stream.str() == "[ [1, 0, 0], [0, 1, 0], [0, 0, 1] ]");
617   }
618
619   {
620     value = Property::Value( Matrix::IDENTITY );
621     std::ostringstream stream;
622     stream <<  value;
623     DALI_TEST_CHECK( stream.str() == "[ [1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1] ]");
624   }
625
626   {
627     value = Property::Value( Rectangle(1,2,3,4) );
628     std::ostringstream stream;
629     stream <<  value;
630     DALI_TEST_CHECK( stream.str() == "[1, 2, 3, 4]");
631   }
632
633   {
634     value = Property::Value( AngleAxis( Radian(1.2f), Vector3::XAXIS ));
635     std::ostringstream stream;
636     stream <<  value;
637     tet_printf("angle axis = %s \n", stream.str().c_str() );
638     DALI_TEST_CHECK( stream.str() == "[1.2, -0, 0, 0]");
639   }
640
641   // Maps and arrays currently not supported, we just check a message is output
642   {
643     Property::Map map;
644     value = Property::Value( map );
645     std::ostringstream stream;
646     stream <<  value;
647     DALI_TEST_CHECK( !stream.str().empty() );
648   }
649   {
650     Property::Array array;
651     value = Property::Value( array );
652     std::ostringstream stream;
653     stream <<  value;
654     DALI_TEST_CHECK( !stream.str().empty() );
655   }
656
657
658   END_TEST;
659
660 }