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