(AutomatedTests) Pushed line & function coverage of public API up
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / utc-Dali-Constraints.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
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 ///////////////////////////////////////////////////////////////////////////////
27 void utc_dali_constraints_startup(void)
28 {
29   test_return_value = TET_UNDEF;
30 }
31
32 void utc_dali_constraints_cleanup(void)
33 {
34   test_return_value = TET_PASS;
35 }
36 ///////////////////////////////////////////////////////////////////////////////
37
38 ///////////////////////////////////////////////////////////////////////////////
39 namespace
40 {
41
42 struct PropertyInputImpl : public PropertyInput
43 {
44 public:
45
46   // Constants
47   static const bool BOOLEAN_VALUE;
48   static const float FLOAT_VALUE;
49   static const int INTEGER_VALUE;
50   static const unsigned int UNSIGNED_INTEGER_VALUE;
51   static const Vector2 VECTOR2_VALUE;
52   static const Vector3 VECTOR3_VALUE;
53   static const Vector4 VECTOR4_VALUE;
54   static const Matrix3 MATRIX3_VALUE;
55   static const Matrix MATRIX_VALUE;
56   static const Quaternion QUATERNION_VALUE;
57
58   // Construction & Destruction
59   PropertyInputImpl( Property::Type type ) : mType( type ) { }
60   virtual ~PropertyInputImpl() { }
61
62   // Methods
63   Property::Type GetType() const { return mType; }
64
65   // Virtual Methods
66   virtual const bool& GetBoolean() const                 { return BOOLEAN_VALUE;          }
67   virtual const float& GetFloat() const                  { return FLOAT_VALUE;            }
68   virtual const int& GetInteger() const                  { return INTEGER_VALUE;          }
69   virtual const unsigned int& GetUnsignedInteger() const { return UNSIGNED_INTEGER_VALUE; }
70   virtual const Vector2& GetVector2() const              { return VECTOR2_VALUE;          }
71   virtual const Vector3& GetVector3() const              { return VECTOR3_VALUE;          }
72   virtual const Vector4& GetVector4() const              { return VECTOR4_VALUE;          }
73   virtual const Matrix3& GetMatrix3() const              { return MATRIX3_VALUE;          }
74   virtual const Matrix& GetMatrix() const                { return MATRIX_VALUE;           }
75   virtual const Quaternion& GetQuaternion() const        { return QUATERNION_VALUE;       }
76
77   // Data
78   Property::Type mType;
79 };
80
81 const bool PropertyInputImpl::BOOLEAN_VALUE                  = true;
82 const float PropertyInputImpl::FLOAT_VALUE                   = 123.0f;
83 const int PropertyInputImpl::INTEGER_VALUE                   = 456;
84 const unsigned int PropertyInputImpl::UNSIGNED_INTEGER_VALUE = 789u;
85 const Vector2 PropertyInputImpl::VECTOR2_VALUE               = Vector2( 10.0f, 20.0f );
86 const Vector3 PropertyInputImpl::VECTOR3_VALUE               = Vector3( 30.0f, 40.0f, 50.0f );
87 const Vector4 PropertyInputImpl::VECTOR4_VALUE               = Vector4( 60.0f, 70.0f, 80.0f, 90.0f );
88 const Matrix3 PropertyInputImpl::MATRIX3_VALUE               ( 1.0f, 2.0f, 3.0f,
89                                                                4.0f, 5.0f, 6.0f,
90                                                                7.0f, 8.0f, 9.0f );
91 const Matrix PropertyInputImpl::MATRIX_VALUE                 = Matrix::IDENTITY;
92 const Quaternion PropertyInputImpl::QUATERNION_VALUE         ( 1.0f, 2.0f, 3.0f, 4.0f );
93
94 struct Vector3PropertyInput : public PropertyInputImpl
95 {
96 public:
97
98   // Construction & Destruction
99   Vector3PropertyInput( Vector3& value )
100   : PropertyInputImpl( Property::VECTOR3 ),
101     mValue( value )
102   {
103   }
104
105   ~Vector3PropertyInput()
106   {
107   }
108
109   const Vector3& GetVector3() const
110   {
111     return mValue;
112   }
113
114   // Data
115   Vector3& mValue;
116 };
117
118 struct QuaternionPropertyInput : public PropertyInputImpl
119 {
120 public:
121
122   // Construction & Destruction
123   QuaternionPropertyInput( Quaternion& value )
124   : PropertyInputImpl( Property::ROTATION ),
125     mValue( value )
126   {
127   }
128
129   ~QuaternionPropertyInput()
130   {
131   }
132
133   const Quaternion& GetQuaternion() const
134   {
135     return mValue;
136   }
137
138   // Data
139   Quaternion& mValue;
140 };
141
142 } // unnamed namespace
143 ///////////////////////////////////////////////////////////////////////////////
144
145 ///////////////////////////////////////////////////////////////////////////////
146 // EqualToConstraint
147 ///////////////////////////////////////////////////////////////////////////////
148 int UtcDaliConstraintsEqualToConstraintFloat(void)
149 {
150   PropertyInputContainer inputs;
151   PropertyInputImpl input( Property::FLOAT );
152   inputs.PushBack( &input );
153
154   float value = 0.0f;
155   DALI_TEST_CHECK( value != PropertyInputImpl::FLOAT_VALUE );
156
157   EqualToConstraint constraint;
158   constraint( value, inputs );
159
160   DALI_TEST_EQUALS( value, PropertyInputImpl::FLOAT_VALUE, TEST_LOCATION );
161
162   END_TEST;
163 }
164
165 int UtcDaliConstraintsEqualToConstraintVector2(void)
166 {
167   PropertyInputContainer inputs;
168   PropertyInputImpl input( Property::VECTOR2 );
169   inputs.PushBack( &input );
170
171   Vector2 value;
172   DALI_TEST_CHECK( value != PropertyInputImpl::VECTOR2_VALUE );
173
174   EqualToConstraint constraint;
175   constraint( value, inputs );
176
177   DALI_TEST_EQUALS( value, PropertyInputImpl::VECTOR2_VALUE, TEST_LOCATION );
178
179   END_TEST;
180 }
181
182 int UtcDaliConstraintsEqualToConstraintVector3(void)
183 {
184   PropertyInputContainer inputs;
185   PropertyInputImpl input( Property::VECTOR3 );
186   inputs.PushBack( &input );
187
188   Vector3 value;
189   DALI_TEST_CHECK( value != PropertyInputImpl::VECTOR3_VALUE );
190
191   EqualToConstraint constraint;
192   constraint( value, inputs );
193
194   DALI_TEST_EQUALS( value, PropertyInputImpl::VECTOR3_VALUE, TEST_LOCATION );
195
196   END_TEST;
197 }
198
199 int UtcDaliConstraintsEqualToConstraintVector4(void)
200 {
201   PropertyInputContainer inputs;
202   PropertyInputImpl input( Property::VECTOR4 );
203   inputs.PushBack( &input );
204
205   Vector4 value;
206   DALI_TEST_CHECK( value != PropertyInputImpl::VECTOR4_VALUE );
207
208   EqualToConstraint constraint;
209   constraint( value, inputs );
210
211   DALI_TEST_EQUALS( value, PropertyInputImpl::VECTOR4_VALUE, TEST_LOCATION );
212
213   END_TEST;
214 }
215
216 int UtcDaliConstraintsEqualToConstraintQuaternion(void)
217 {
218   PropertyInputContainer inputs;
219   PropertyInputImpl input( Property::ROTATION );
220   inputs.PushBack( &input );
221
222   Quaternion value;
223   DALI_TEST_CHECK( value != PropertyInputImpl::QUATERNION_VALUE );
224
225   EqualToConstraint constraint;
226   constraint( value, inputs );
227
228   DALI_TEST_EQUALS( value, PropertyInputImpl::QUATERNION_VALUE, TEST_LOCATION );
229
230   END_TEST;
231 }
232
233 int UtcDaliConstraintsEqualToConstraintMatrix3(void)
234 {
235   PropertyInputContainer inputs;
236   PropertyInputImpl input( Property::MATRIX3 );
237   inputs.PushBack( &input );
238
239   Matrix3 value;
240   DALI_TEST_CHECK( value != PropertyInputImpl::MATRIX3_VALUE );
241
242   EqualToConstraint constraint;
243   constraint( value, inputs );
244
245   DALI_TEST_EQUALS( value, PropertyInputImpl::MATRIX3_VALUE, 0.1f, TEST_LOCATION);
246
247   END_TEST;
248 }
249
250 int UtcDaliConstraintsEqualToConstraintMatrix(void)
251 {
252   PropertyInputContainer inputs;
253   PropertyInputImpl input( Property::MATRIX );
254   inputs.PushBack( &input );
255
256   Matrix value;
257   DALI_TEST_CHECK( value != PropertyInputImpl::MATRIX_VALUE );
258
259   EqualToConstraint constraint;
260   constraint( value, inputs );
261
262   DALI_TEST_EQUALS( value, PropertyInputImpl::MATRIX_VALUE, TEST_LOCATION );
263
264   END_TEST;
265 }
266 ///////////////////////////////////////////////////////////////////////////////
267
268 ///////////////////////////////////////////////////////////////////////////////
269 // RelativeToConstraint
270 ///////////////////////////////////////////////////////////////////////////////
271 int UtcDaliConstraintsRelativeToConstraintUsingFloat(void)
272 {
273   PropertyInputContainer inputs;
274   PropertyInputImpl input( Property::VECTOR3 );
275   inputs.PushBack( &input );
276
277   Vector3 value;
278   DALI_TEST_EQUALS( value, Vector3::ZERO, TEST_LOCATION );
279
280   const float scale( 4.0f );
281   RelativeToConstraint constraint( scale );
282   constraint( value, inputs );
283
284   DALI_TEST_EQUALS( value, PropertyInputImpl::VECTOR3_VALUE * scale, TEST_LOCATION );
285
286   END_TEST;
287 }
288
289 int UtcDaliConstraintsRelativeToConstraintUsingVector3(void)
290 {
291   PropertyInputContainer inputs;
292   PropertyInputImpl input( Property::VECTOR3 );
293   inputs.PushBack( &input );
294
295   Vector3 value;
296   DALI_TEST_EQUALS( value, Vector3::ZERO, TEST_LOCATION );
297
298   const Vector3 scale( 4.0f, 5.0f, 6.0f );
299   RelativeToConstraint constraint( scale );
300   constraint( value, inputs );
301
302   DALI_TEST_EQUALS( value, PropertyInputImpl::VECTOR3_VALUE * scale, TEST_LOCATION );
303
304   END_TEST;
305 }
306 ///////////////////////////////////////////////////////////////////////////////
307
308 ///////////////////////////////////////////////////////////////////////////////
309 // RelativeToConstraintFloat
310 ///////////////////////////////////////////////////////////////////////////////
311 int UtcDaliConstraintsRelativeToConstraintFloat(void)
312 {
313   PropertyInputContainer inputs;
314   PropertyInputImpl input( Property::VECTOR3 );
315   inputs.PushBack( &input );
316
317   const float scale( 4.0f );
318
319   float value = 0.0f;
320   DALI_TEST_CHECK( value != PropertyInputImpl::FLOAT_VALUE * scale );
321
322   RelativeToConstraintFloat constraint( scale );
323   constraint( value, inputs );
324
325   DALI_TEST_EQUALS( value, PropertyInputImpl::FLOAT_VALUE * scale, TEST_LOCATION );
326
327   END_TEST;
328 }
329 ///////////////////////////////////////////////////////////////////////////////
330
331 ///////////////////////////////////////////////////////////////////////////////
332 // LookAt
333 ///////////////////////////////////////////////////////////////////////////////
334 int UtcDaliConstraintsLookAt(void)
335 {
336   TestApplication application;
337
338   Actor actor = Actor::New();
339   DALI_TEST_EQUALS( actor.GetCurrentWorldOrientation(), Quaternion::IDENTITY, TEST_LOCATION );
340
341   Vector3 targetPosition;
342   Vector3 cameraPosition;
343   Quaternion targetOrientation;
344
345   Vector3PropertyInput targetPositionProperty( targetPosition );
346   Vector3PropertyInput cameraPositionProperty( cameraPosition );
347   QuaternionPropertyInput targetOrientationProperty( targetOrientation );
348
349   PropertyInputContainer inputs;
350   inputs.PushBack( &targetPositionProperty );
351   inputs.PushBack( &cameraPositionProperty );
352   inputs.PushBack( &targetOrientationProperty );
353
354   Quaternion current;
355
356   // 180 degrees round y
357   targetPosition = Vector3::ZERO;
358   cameraPosition = Vector3( 0.0f, 0.0f, 1.0f );
359   targetOrientation = Quaternion::IDENTITY;
360   Quaternion lookAtOrientation( Quaternion( Radian( Math::PI ), Vector3::YAXIS ) );
361   LookAt( current, inputs );
362   DALI_TEST_EQUALS( current, lookAtOrientation, TEST_LOCATION );
363
364   // 180 degrees round y * -45 degrees round x
365   targetPosition = Vector3::ZERO;
366   cameraPosition = Vector3( 0.0f, -1.0f, 1.0f );
367   targetOrientation = Quaternion::IDENTITY;
368   lookAtOrientation = Quaternion( Radian( Math::PI ), Vector3::YAXIS ) * Quaternion( Radian( Math::PI * 0.25f ), -Vector3::XAXIS );
369   LookAt( current, inputs );
370   DALI_TEST_EQUALS( current, lookAtOrientation, Math::MACHINE_EPSILON_10, TEST_LOCATION );
371
372   // 180 degrees round y * -45 degrees round x at different points
373   targetPosition = Vector3( 0.0f, 1.0f, -1.0f );
374   cameraPosition = Vector3::ZERO;
375   targetOrientation = Quaternion::IDENTITY;
376   lookAtOrientation = Quaternion( Radian( Math::PI ), Vector3::YAXIS ) * Quaternion( Radian( Math::PI * 0.25f ), -Vector3::XAXIS );
377   LookAt( current, inputs );
378   DALI_TEST_EQUALS( current, lookAtOrientation, Math::MACHINE_EPSILON_10, TEST_LOCATION );
379
380   // 225 degrees round y
381   targetPosition = Vector3( -1.0f, 0.0f, 0.0f );
382   cameraPosition = Vector3( 0.0f, 0.0f, 1.0f );
383   targetOrientation = Quaternion::IDENTITY;
384   lookAtOrientation = Quaternion( Radian( Math::PI * 1.25), Vector3::YAXIS );
385   LookAt( current, inputs );
386   DALI_TEST_EQUALS( current, lookAtOrientation, Math::MACHINE_EPSILON_10, TEST_LOCATION );
387
388   // 180 degrees round y * -45 degrees round x, Up Vector: 180 degrees
389   targetPosition = Vector3::ZERO;
390   cameraPosition = Vector3( 0.0f, -1.0f, 1.0f );
391   targetOrientation = Quaternion( Radian( Math::PI ), Vector3::ZAXIS );
392   lookAtOrientation = Quaternion( Radian( Math::PI ), Vector3::YAXIS ) * Quaternion( Radian( Math::PI * 0.25f ), -Vector3::XAXIS ) * Quaternion( Radian( Math::PI ), -Vector3::ZAXIS );
393   LookAt( current, inputs );
394   DALI_TEST_EQUALS( current, lookAtOrientation, Math::MACHINE_EPSILON_10, TEST_LOCATION );
395
396   END_TEST;
397 }
398 ///////////////////////////////////////////////////////////////////////////////
399
400 int UtcDaliPropertyInputGetExtension(void)
401 {
402   PropertyInputImpl input( Property::BOOLEAN );
403   DALI_TEST_CHECK( input.GetExtension() == NULL );
404   END_TEST;
405 }