Merge "DALi Version 1.0.35" into tizen
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / utc-Dali-Path.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 using namespace Dali::Internal;
26
27 namespace
28 {
29 // Knots fed into Allegro, which generates control points
30 static void SetupPath( Dali::Path& path)
31 {
32   path.AddPoint(Vector3( 30.0,  80.0, 0.0));
33   path.AddPoint(Vector3( 70.0, 120.0, 0.0));
34   path.AddPoint(Vector3(100.0, 100.0, 0.0));
35
36   //Control points for first segment
37   path.AddControlPoint( Vector3( 39.0,  90.0, 0.0) );
38   path.AddControlPoint(Vector3( 56.0, 119.0, 0.0) );
39
40   //Control points for second segment
41   path.AddControlPoint(Vector3( 78.0, 120.0, 0.0) );
42   path.AddControlPoint(Vector3( 93.0, 104.0, 0.0) );
43 }
44
45 } // anonymous namespace
46
47 int utcDaliPathGetPoint(void)
48 {
49   TestApplication application;
50
51   Dali::Path path = Dali::Path::New();
52   path.AddPoint(Vector3( 50.0,  50.0, 0.0));
53   path.AddPoint(Vector3(120.0,  70.0, 0.0));
54   path.AddPoint(Vector3(190.0, 250.0, 0.0));
55   path.AddPoint(Vector3(260.0, 260.0, 0.0));
56   path.AddPoint(Vector3(330.0, 220.0, 0.0));
57   path.AddPoint(Vector3(400.0,  50.0, 0.0));
58
59   DALI_TEST_EQUALS(path.GetPoint(0), Vector3( 50.0,  50.0, 0.0), TEST_LOCATION);
60   DALI_TEST_EQUALS(path.GetPoint(1), Vector3(120.0,  70.0, 0.0), TEST_LOCATION);
61   DALI_TEST_EQUALS(path.GetPoint(2), Vector3(190.0, 250.0, 0.0), TEST_LOCATION);
62   DALI_TEST_EQUALS(path.GetPoint(3), Vector3(260.0, 260.0, 0.0), TEST_LOCATION);
63   DALI_TEST_EQUALS(path.GetPoint(4), Vector3(330.0, 220.0, 0.0), TEST_LOCATION);
64   DALI_TEST_EQUALS(path.GetPoint(5), Vector3(400.0,  50.0, 0.0), TEST_LOCATION);
65   END_TEST;
66 }
67
68 int utcDaliPathGetPoint02(void)
69 {
70   TestApplication application;
71
72   Dali::Path path = Dali::Path::New();
73   path.AddPoint(Vector3( 50.0,  50.0, 0.0f));
74
75   try
76   {
77     path.GetPoint(1);
78     tet_result(TET_FAIL);
79   }
80   catch (Dali::DaliException& e)
81   {
82     DALI_TEST_PRINT_ASSERT( e );
83     DALI_TEST_ASSERT(e, "index < mPoint.Size()", TEST_LOCATION);
84   }
85   END_TEST;
86 }
87
88 int utcDaliPathGetPoint03(void)
89 {
90   TestApplication application;
91
92   Dali::Path path = Dali::Path::New();
93
94   try
95   {
96     path.GetPoint(0);
97     tet_result(TET_FAIL);
98   }
99   catch (Dali::DaliException& e)
100   {
101     DALI_TEST_PRINT_ASSERT( e );
102     DALI_TEST_ASSERT(e, "index < mPoint.Size()", TEST_LOCATION);
103   }
104   END_TEST;
105 }
106
107 int utcDaliPathGetControlPoints(void)
108 {
109   TestApplication application;
110
111   Dali::Path path = Dali::Path::New();
112   path.AddControlPoint( Vector3(0.0f, 0.0f, 0.0) );
113   path.AddControlPoint( Vector3(108.0f, 57.0f, 0.0) );
114
115   DALI_TEST_EQUALS(path.GetControlPoint(0), Vector3(0.0f, 0.0f, 0.0f), TEST_LOCATION);
116   DALI_TEST_EQUALS(path.GetControlPoint(1), Vector3(108.0f, 57.0f, 0.0f), TEST_LOCATION);
117   END_TEST;
118 }
119
120 int utcDaliPathGetControlPoints01(void)
121 {
122   TestApplication application;
123
124   Dali::Path path = Dali::Path::New();
125   path.AddControlPoint(Vector3(0.0f, 0.0f, 0.0) );
126   path.AddControlPoint(Vector3(108.0f, 57.0f, 0.0) );
127
128   try
129   {
130     path.GetControlPoint(5);
131     tet_result(TET_FAIL);
132   }
133   catch (Dali::DaliException& e)
134   {
135     DALI_TEST_PRINT_ASSERT( e );
136     DALI_TEST_ASSERT(e, "index < mControlPoint.Size()", TEST_LOCATION);
137   }
138   END_TEST;
139 }
140
141 int utcDaliPathGetControlPoints02(void)
142 {
143   TestApplication application;
144
145   Dali::Path path = Dali::Path::New();
146   try
147   {
148     path.GetControlPoint(0);
149     tet_result(TET_FAIL);
150   }
151   catch (Dali::DaliException& e)
152   {
153     DALI_TEST_PRINT_ASSERT( e );
154     DALI_TEST_ASSERT(e, "index < mControlPoint.Size()", TEST_LOCATION);
155   }
156   END_TEST;
157 }
158
159 int utcDaliPathGenerateControlPoints01(void)
160 {
161   TestApplication application;
162
163   Dali::Path path = Dali::Path::New();
164
165   path.AddPoint(Vector3( 50.0,  50.0, 0.0));
166   path.AddPoint(Vector3(120.0,  70.0, 0.0));
167   path.AddPoint(Vector3(190.0, 250.0, 0.0));
168   path.AddPoint(Vector3(260.0, 260.0, 0.0));
169   path.AddPoint(Vector3(330.0, 220.0, 0.0));
170   path.AddPoint(Vector3(400.0,  50.0, 0.0));
171
172   path.GenerateControlPoints(0.25);
173
174   DALI_TEST_EQUALS(path.GetControlPoint(0), Vector3( 68.0,  55.0, 0.0), 1.0, TEST_LOCATION);
175   DALI_TEST_EQUALS(path.GetControlPoint(1), Vector3(107.0,  58.0, 0.0), 1.0, TEST_LOCATION);
176
177   DALI_TEST_EQUALS(path.GetControlPoint(2), Vector3(156.0, 102.0, 0.0), 1.0, TEST_LOCATION);
178   DALI_TEST_EQUALS(path.GetControlPoint(3), Vector3(152.0, 220.0, 0.0), 1.0, TEST_LOCATION);
179
180   DALI_TEST_EQUALS(path.GetControlPoint(4), Vector3(204.0, 261.0, 0.0), 1.0, TEST_LOCATION);
181   DALI_TEST_EQUALS(path.GetControlPoint(5), Vector3(243.0, 263.0, 0.0), 1.0, TEST_LOCATION);
182
183   DALI_TEST_EQUALS(path.GetControlPoint(6), Vector3(280.0, 256.0, 0.0), 1.0, TEST_LOCATION);
184   DALI_TEST_EQUALS(path.GetControlPoint(7), Vector3(317.0, 235.0, 0.0), 1.0, TEST_LOCATION);
185
186   DALI_TEST_EQUALS(path.GetControlPoint(8), Vector3(360.0, 185.0, 0.0), 1.0, TEST_LOCATION);
187   DALI_TEST_EQUALS(path.GetControlPoint(9), Vector3(383.0,  93.0, 0.0), 1.0, TEST_LOCATION);
188
189   END_TEST;
190 }
191
192 int utcDaliPathGenerateControlPoints02(void)
193 {
194   TestApplication application;
195
196   Dali::Path path = Dali::Path::New();
197   try
198   {
199     path.GenerateControlPoints(0.25);
200     tet_result(TET_FAIL);
201   }
202   catch (Dali::DaliException& e)
203   {
204     DALI_TEST_PRINT_ASSERT( e );
205     DALI_TEST_ASSERT(e, "numSegments > 0", TEST_LOCATION);
206   }
207   END_TEST;
208 }
209
210 int utcDaliPathGenerateControlPoints03(void)
211 {
212   TestApplication application;
213
214   Dali::Path path = Dali::Path::New();
215   path.AddPoint(Vector3(400.0,  50.0, 0.0f));
216   try
217   {
218     path.GenerateControlPoints(0.25);
219     tet_result(TET_FAIL);
220   }
221   catch (Dali::DaliException& e)
222   {
223     DALI_TEST_PRINT_ASSERT( e );
224     DALI_TEST_ASSERT(e, "numSegments > 0", TEST_LOCATION);
225   }
226   END_TEST;
227 }
228
229 int UtcDaliPathSample01(void)
230 {
231   TestApplication application;
232   Dali::Path path = Dali::Path::New();
233   SetupPath(path);
234
235   //t = 0
236   Vector3 position, tangent;
237   path.Sample(0.0f, position, tangent );
238   DALI_TEST_EQUALS(position.x, 30.0f, TEST_LOCATION);
239   DALI_TEST_EQUALS(position.y, 80.0f, TEST_LOCATION);
240   DALI_TEST_EQUALS(tangent.x,  0.6f, 0.1f, TEST_LOCATION);
241   DALI_TEST_EQUALS(tangent.y,  0.7f, 0.1f, TEST_LOCATION);
242
243   //t = 0.25
244   path.Sample(0.25f, position, tangent );
245   DALI_TEST_EQUALS(position.x,  48.0f, 2.0f, TEST_LOCATION);
246   DALI_TEST_EQUALS(position.y, 102.0f, 2.0f, TEST_LOCATION);
247   DALI_TEST_EQUALS(tangent.x,  0.6f, 0.1f, TEST_LOCATION);
248   DALI_TEST_EQUALS(tangent.y,  0.7f, 0.1f, TEST_LOCATION);
249
250   // t = 0.5
251   path.Sample(0.5f, position, tangent );
252   DALI_TEST_EQUALS(position.x,  70.0f, TEST_LOCATION);
253   DALI_TEST_EQUALS(position.y, 120.0f, TEST_LOCATION);
254   DALI_TEST_EQUALS(tangent.x,  1.0f, 0.1f, TEST_LOCATION);
255   DALI_TEST_EQUALS(tangent.y,  0.0f, 0.1f, TEST_LOCATION);
256
257
258   //t = 0.75
259   path.Sample(0.75f, position, tangent );
260   DALI_TEST_EQUALS(position.x,  85.0f, 2.0f, TEST_LOCATION);
261   DALI_TEST_EQUALS(position.y, 112.0f, 2.0f, TEST_LOCATION);
262   DALI_TEST_EQUALS(tangent.x,  0.7f, 0.1f, TEST_LOCATION);
263   DALI_TEST_EQUALS(tangent.y,  -0.6f, 0.1f, TEST_LOCATION);
264
265   // t = 1
266   path.Sample(1.0f, position, tangent );
267   DALI_TEST_EQUALS(position.x, 100.0f, TEST_LOCATION);
268   DALI_TEST_EQUALS(position.y, 100.0f, TEST_LOCATION);
269   DALI_TEST_EQUALS(tangent.x,  0.8f, 0.1f, TEST_LOCATION);
270   DALI_TEST_EQUALS(tangent.y,  -0.4f, 0.1f, TEST_LOCATION);
271
272   END_TEST;
273 }
274
275 //PathConstraint test cases
276 int UtcPathConstraintApply(void)
277 {
278   TestApplication application;
279
280   Dali::Actor actor = Dali::Actor::New();
281
282   // Register a float property
283   Property::Index index = actor.RegisterProperty( "t", 0.0f );
284
285   Dali::Stage::GetCurrent().Add(actor);
286
287
288   Dali::Path path = Dali::Path::New();
289   SetupPath(path);
290
291   //Create a PathConstraint
292   Dali::PathConstraint pathConstraint = Dali::PathConstraint::New( path, Vector2(0.0f,1.0f) );
293
294   //Apply the path constraint to the actor position. The source property for the constraint will be the custom property "t"
295   pathConstraint.Apply( Property(actor, index), Property(actor,Dali::Actor::Property::POSITION) );
296
297   //Create an animation to animate the custom property
298   float durationSeconds(1.0f);
299   Dali::Animation animation = Dali::Animation::New(durationSeconds);
300   animation.AnimateTo(Dali::Property(actor,index),1.0f);
301   animation.Play();
302
303   application.SendNotification();
304   application.Render(static_cast<unsigned int>(durationSeconds*200.0f)/* 20% progress */);
305
306   Vector3 position, tangent;
307   path.Sample(0.2f, position, tangent );
308   DALI_TEST_EQUALS( actor.GetCurrentPosition(), position, TEST_LOCATION );
309
310   application.SendNotification();
311   application.Render(static_cast<unsigned int>(durationSeconds*200.0f)/* 40% progress */);
312   path.Sample(0.4f, position, tangent );
313   DALI_TEST_EQUALS( actor.GetCurrentPosition(), position, TEST_LOCATION );
314
315   application.SendNotification();
316   application.Render(static_cast<unsigned int>(durationSeconds*200.0f)/* 60% progress */);
317   path.Sample(0.6f, position, tangent );
318   DALI_TEST_EQUALS( actor.GetCurrentPosition(), position, TEST_LOCATION );
319
320   application.SendNotification();
321   application.Render(static_cast<unsigned int>(durationSeconds*200.0f)/* 80% progress */);
322   path.Sample(0.8f, position, tangent );
323   DALI_TEST_EQUALS( actor.GetCurrentPosition(), position, TEST_LOCATION );
324
325   application.SendNotification();
326   application.Render(static_cast<unsigned int>(durationSeconds*200.0f)/* 100% progress */);
327   path.Sample(1.0f, position, tangent );
328   DALI_TEST_EQUALS( actor.GetCurrentPosition(), position, TEST_LOCATION );
329
330   application.SendNotification();
331   application.Render(static_cast<unsigned int>(durationSeconds*200.0f)/* beyond the animation duration*/);
332   DALI_TEST_EQUALS( actor.GetCurrentPosition(), position, TEST_LOCATION );
333
334   END_TEST;
335 }
336
337 int UtcPathConstraintApplyRange(void)
338 {
339   TestApplication application;
340
341   Dali::Actor actor = Dali::Actor::New();
342
343   // Register a float property
344   Property::Index index = actor.RegisterProperty( "t", 0.0f );
345   Dali::Stage::GetCurrent().Add(actor);
346
347
348   Dali::Path path = Dali::Path::New();
349   SetupPath(path);
350
351   //Create a PathConstraint
352   Vector2 range( 100.0f, 300.0f );
353   Dali::PathConstraint pathConstraint = Dali::PathConstraint::New( path, range );
354
355   //Apply the path constraint to the actor position. The source property for the constraint will be the custom property "t"
356   pathConstraint.Apply( Property(actor,index), Property(actor,Dali::Actor::Property::POSITION) );
357
358
359   //Create an animation to animate the custom property
360   float durationSeconds(1.0f);
361   Dali::Animation animation = Dali::Animation::New(durationSeconds);
362   animation.AnimateTo(Dali::Property(actor,index),400.0f);
363   animation.Play();
364
365   application.SendNotification();
366   application.Render(static_cast<unsigned int>(durationSeconds*250.0f)/* 25% progress */);
367
368
369   Vector3 position, tangent;
370   float tValue;
371   actor.GetProperty(index).Get(tValue);
372   float currentCursor =  ( tValue - range.x ) / (range.y-range.x);
373   path.Sample(currentCursor, position, tangent );
374   DALI_TEST_EQUALS( actor.GetCurrentPosition(), position, TEST_LOCATION );
375
376   application.SendNotification();
377   application.Render(static_cast<unsigned int>(durationSeconds*250.0f)/* 50% progress */);
378   actor.GetProperty(index).Get(tValue);
379   currentCursor =  ( tValue - range.x ) / (range.y-range.x);
380   path.Sample(currentCursor, position, tangent );
381   path.Sample(0.5, position, tangent );
382   DALI_TEST_EQUALS( actor.GetCurrentPosition(), position, TEST_LOCATION );
383
384   application.SendNotification();
385   application.Render(static_cast<unsigned int>(durationSeconds*250.0f)/* 75% progress */);
386   actor.GetProperty(index).Get(tValue);
387   currentCursor =  ( tValue - range.x ) / (range.y-range.x);
388   path.Sample(currentCursor, position, tangent );
389   DALI_TEST_EQUALS( actor.GetCurrentPosition(), position, TEST_LOCATION );
390
391   application.SendNotification();
392   application.Render(static_cast<unsigned int>(durationSeconds*250.0f)/* 100% progress */);
393   actor.GetProperty(index).Get(tValue);
394   currentCursor =  ( tValue - range.x ) / (range.y-range.x);
395   path.Sample(currentCursor, position, tangent );
396   DALI_TEST_EQUALS( actor.GetCurrentPosition(), position, TEST_LOCATION );
397
398   application.SendNotification();
399   application.Render(static_cast<unsigned int>(durationSeconds*250.0f)/* beyond the animation duration*/);
400   actor.GetProperty(index).Get(tValue);
401   currentCursor =  ( tValue - range.x ) / (range.y-range.x);
402   path.Sample(currentCursor, position, tangent );
403   DALI_TEST_EQUALS( actor.GetCurrentPosition(), position, TEST_LOCATION );
404
405   END_TEST;
406 }
407
408 int UtcPathConstraintDestroy(void)
409 {
410   TestApplication application;
411
412   Dali::Actor actor = Dali::Actor::New();
413
414   // Register a float property
415   Property::Index index = actor.RegisterProperty( "t", 0.0f );
416   Dali::Stage::GetCurrent().Add(actor);
417
418
419   Dali::Path path = Dali::Path::New();
420   SetupPath(path);
421
422   {
423     //Create a PathConstraint
424     Vector2 range( 0.0f, 1.0f );
425     Dali::PathConstraint pathConstraint = Dali::PathConstraint::New( path, range );
426
427     //Apply the path constraint to the actor position. The source property for the constraint will be the custom property "t"
428     pathConstraint.Apply( Property(actor,index), Property(actor,Dali::Actor::Property::POSITION) );
429
430     //Test that the constraint is correctly applied
431     actor.SetProperty(index,0.5f);
432     application.SendNotification();
433     application.Render(static_cast<unsigned int>(1.0f));
434
435     Vector3 position, tangent;
436     path.Sample(0.5f, position, tangent );
437     DALI_TEST_EQUALS( actor.GetCurrentPosition(), position, TEST_LOCATION );
438
439   }
440
441   //PathConstraint has been destroyed. Constraint in the actor should have been removed
442   actor.SetProperty(index,0.75f);
443   application.SendNotification();
444   application.Render(static_cast<unsigned int>(1.0f));
445
446   DALI_TEST_EQUALS( actor.GetCurrentPosition(), Vector3::ZERO, TEST_LOCATION );
447
448   END_TEST;
449 }
450
451 int UtcPathConstraintRemove(void)
452 {
453   TestApplication application;
454
455   Dali::Actor actor = Dali::Actor::New();
456
457   // Register a float property
458   Property::Index index = actor.RegisterProperty( "t", 0.0f );
459   Dali::Stage::GetCurrent().Add(actor);
460
461   Dali::Path path = Dali::Path::New();
462   SetupPath(path);
463
464   //Create a PathConstraint
465   Vector2 range( 0.0f, 1.0f );
466   Dali::PathConstraint pathConstraint = Dali::PathConstraint::New( path, range );
467
468   //Apply the path constraint to the actor position. The source property for the constraint will be the custom property "t"
469   pathConstraint.Apply( Property(actor,index), Property(actor,Dali::Actor::Property::POSITION) );
470
471   //Test that the constraint is correctly applied
472   actor.SetProperty(index,0.5f);
473   application.SendNotification();
474   application.Render(static_cast<unsigned int>(1.0f));
475
476   Vector3 position, tangent;
477   path.Sample(0.5f, position, tangent );
478   DALI_TEST_EQUALS( actor.GetCurrentPosition(), position, TEST_LOCATION );
479
480   //Remove constraint
481   pathConstraint.Remove( actor );
482   actor.SetProperty(index,0.75f);
483   application.SendNotification();
484   application.Render(static_cast<unsigned int>(1.0f));
485
486   DALI_TEST_EQUALS( actor.GetCurrentPosition(), Vector3::ZERO, TEST_LOCATION );
487
488   END_TEST;
489 }