Merge "UTC coverage for Path,KeyFrames,LinearConstrainer,Layer,ImageActor" into tizen
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / utc-Dali-Constrainer.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/devel-api/animation/path-constrainer.h>
23 #include <dali-test-suite-utils.h>
24
25 using namespace Dali;
26 using namespace Dali::Internal;
27
28 namespace
29 {
30
31 static void SetupPath( Dali::Path& path)
32 {
33   path.AddPoint(Vector3( 30.0,  80.0, 0.0));
34   path.AddPoint(Vector3( 70.0, 120.0, 0.0));
35   path.AddPoint(Vector3(100.0, 100.0, 0.0));
36
37   //Control points for first segment
38   path.AddControlPoint( Vector3( 39.0,  90.0, 0.0) );
39   path.AddControlPoint(Vector3( 56.0, 119.0, 0.0) );
40
41   //Control points for second segment
42   path.AddControlPoint(Vector3( 78.0, 120.0, 0.0) );
43   path.AddControlPoint(Vector3( 93.0, 104.0, 0.0) );
44 }
45
46 static void SetupPathConstrainer( Dali::PathConstrainer& PathConstrainer)
47 {
48   PathConstrainer.SetProperty( Dali::PathConstrainer::Property::FORWARD, Vector3(1.0f,0.0f,0.0f) );
49
50   Dali::Property::Array points;
51   points.Resize(3);
52   points[0] = Vector3( 30.0,  80.0, 0.0);
53   points[1] = Vector3( 70.0, 120.0, 0.0);
54   points[2] = Vector3(100.0, 100.0, 0.0);
55   PathConstrainer.SetProperty( Dali::PathConstrainer::Property::POINTS, points );
56
57   points.Resize(4);
58   points[0] = Vector3( 39.0,  90.0, 0.0);
59   points[1] = Vector3( 56.0, 119.0, 0.0);
60   points[2] = Vector3( 78.0, 120.0, 0.0);
61   points[3] = Vector3( 93.0, 104.0, 0.0);
62   PathConstrainer.SetProperty( Dali::PathConstrainer::Property::CONTROL_POINTS, points );
63 }
64
65 static void SetupLinearConstrainerUniformProgress( Dali::LinearConstrainer& linearConstrainer)
66 {
67   Dali::Property::Array points;
68   points.Resize(3);
69   points[0] = 0.0f;
70   points[1] = 1.0f;
71   points[2] = 0.0f;
72   linearConstrainer.SetProperty( Dali::LinearConstrainer::Property::VALUE, points );
73 }
74
75 static void SetupLinearConstrainerNonUniformProgress( Dali::LinearConstrainer& linearConstrainer)
76 {
77   Dali::Property::Array points;
78   points.Resize(3);
79   points[0] = 0.0f;
80   points[1] = 1.0f;
81   points[2] = 0.0f;
82   linearConstrainer.SetProperty( Dali::LinearConstrainer::Property::VALUE, points );
83
84   points[0] = 0.0f;
85   points[1] = 0.25f;
86   points[2] = 1.0f;
87   linearConstrainer.SetProperty( Dali::LinearConstrainer::Property::PROGRESS, points );
88 }
89
90 } // anonymous namespace
91
92 //PathConstrainer test cases
93 int UtcPathConstrainerApply(void)
94 {
95   TestApplication application;
96
97   Dali::Actor actor = Dali::Actor::New();
98
99   // Register a float property
100   Property::Index index = actor.RegisterProperty( "t", 0.0f );
101
102   Dali::Stage::GetCurrent().Add(actor);
103
104   //Create a Path
105   Dali::Path path = Dali::Path::New();
106   SetupPath(path);
107
108   //Create a PathConstrainer
109   Dali::PathConstrainer pathConstrainer = Dali::PathConstrainer::New();
110   SetupPathConstrainer( pathConstrainer );
111
112   //Apply the path constraint to the actor's position. The source property for the constraint will be the custom property "t"
113   Vector2 range( 0.0f, 1.0f );
114   pathConstrainer.Apply( Property(actor,Dali::Actor::Property::POSITION), Property(actor,index), range );
115
116   //Create an animation to animate the custom property
117   float durationSeconds(1.0f);
118   Dali::Animation animation = Dali::Animation::New(durationSeconds);
119   animation.AnimateTo(Dali::Property(actor,index),1.0f);
120   animation.Play();
121
122   application.SendNotification();
123   application.Render(static_cast<unsigned int>(durationSeconds*200.0f)/* 20% progress */);
124
125   Vector3 position, tangent;
126   path.Sample(0.2f, position, tangent );
127   DALI_TEST_EQUALS( actor.GetCurrentPosition(), position, TEST_LOCATION );
128
129   application.SendNotification();
130   application.Render(static_cast<unsigned int>(durationSeconds*200.0f)/* 40% progress */);
131   path.Sample(0.4f, position, tangent );
132   DALI_TEST_EQUALS( actor.GetCurrentPosition(), position, TEST_LOCATION );
133
134   application.SendNotification();
135   application.Render(static_cast<unsigned int>(durationSeconds*200.0f)/* 60% progress */);
136   path.Sample(0.6f, position, tangent );
137   DALI_TEST_EQUALS( actor.GetCurrentPosition(), position, TEST_LOCATION );
138
139   application.SendNotification();
140   application.Render(static_cast<unsigned int>(durationSeconds*200.0f)/* 80% progress */);
141   path.Sample(0.8f, position, tangent );
142   DALI_TEST_EQUALS( actor.GetCurrentPosition(), position, TEST_LOCATION );
143
144   application.SendNotification();
145   application.Render(static_cast<unsigned int>(durationSeconds*200.0f)/* 100% progress */);
146   path.Sample(1.0f, position, tangent );
147   DALI_TEST_EQUALS( actor.GetCurrentPosition(), position, TEST_LOCATION );
148
149   application.SendNotification();
150   application.Render(static_cast<unsigned int>(durationSeconds*200.0f)/* beyond the animation duration*/);
151   DALI_TEST_EQUALS( actor.GetCurrentPosition(), position, TEST_LOCATION );
152
153   END_TEST;
154 }
155
156 int UtcPathConstrainerApplyRange(void)
157 {
158   TestApplication application;
159
160   Dali::Actor actor = Dali::Actor::New();
161
162   // Register a float property
163   Property::Index index = actor.RegisterProperty( "t", 0.0f );
164   Dali::Stage::GetCurrent().Add(actor);
165
166   //Create a Path
167   Dali::Path path = Dali::Path::New();
168   SetupPath(path);
169
170   //Create a PathConstrainer
171   Dali::PathConstrainer pathConstrainer = Dali::PathConstrainer::New();
172   SetupPathConstrainer( pathConstrainer );
173
174   //Apply the path constraint to the actor's position. The source property for the constraint will be the custom property "t"
175   Vector2 range( 100.0f, 300.0f );
176   pathConstrainer.Apply( Property(actor,Dali::Actor::Property::POSITION), Property(actor,index), range );
177
178
179   //Create an animation to animate the custom property
180   float durationSeconds(1.0f);
181   Dali::Animation animation = Dali::Animation::New(durationSeconds);
182   animation.AnimateTo(Dali::Property(actor,index),400.0f);
183   animation.Play();
184
185   application.SendNotification();
186   application.Render(static_cast<unsigned int>(durationSeconds*250.0f)/* 25% progress */);
187
188
189   Vector3 position, tangent;
190   float tValue;
191   actor.GetProperty(index).Get(tValue);
192   float currentCursor =  ( tValue - range.x ) / (range.y-range.x);
193   path.Sample(currentCursor, position, tangent );
194   DALI_TEST_EQUALS( actor.GetCurrentPosition(), position, TEST_LOCATION );
195
196   application.SendNotification();
197   application.Render(static_cast<unsigned int>(durationSeconds*250.0f)/* 50% progress */);
198   actor.GetProperty(index).Get(tValue);
199   currentCursor =  ( tValue - range.x ) / (range.y-range.x);
200   path.Sample(currentCursor, position, tangent );
201   path.Sample(0.5, position, tangent );
202   DALI_TEST_EQUALS( actor.GetCurrentPosition(), position, TEST_LOCATION );
203
204   application.SendNotification();
205   application.Render(static_cast<unsigned int>(durationSeconds*250.0f)/* 75% progress */);
206   actor.GetProperty(index).Get(tValue);
207   currentCursor =  ( tValue - range.x ) / (range.y-range.x);
208   path.Sample(currentCursor, position, tangent );
209   DALI_TEST_EQUALS( actor.GetCurrentPosition(), position, TEST_LOCATION );
210
211   application.SendNotification();
212   application.Render(static_cast<unsigned int>(durationSeconds*250.0f)/* 100% progress */);
213   actor.GetProperty(index).Get(tValue);
214   currentCursor =  ( tValue - range.x ) / (range.y-range.x);
215   path.Sample(currentCursor, position, tangent );
216   DALI_TEST_EQUALS( actor.GetCurrentPosition(), position, TEST_LOCATION );
217
218   application.SendNotification();
219   application.Render(static_cast<unsigned int>(durationSeconds*250.0f)/* beyond the animation duration*/);
220   actor.GetProperty(index).Get(tValue);
221   currentCursor =  ( tValue - range.x ) / (range.y-range.x);
222   path.Sample(currentCursor, position, tangent );
223   DALI_TEST_EQUALS( actor.GetCurrentPosition(), position, TEST_LOCATION );
224
225   END_TEST;
226 }
227
228 int UtcPathConstrainerDestroy(void)
229 {
230   TestApplication application;
231
232   Dali::Actor actor = Dali::Actor::New();
233
234   // Register a float property
235   Property::Index index = actor.RegisterProperty( "t", 0.0f );
236   Dali::Stage::GetCurrent().Add(actor);
237
238   {
239     //Create a Path
240     Dali::Path path = Dali::Path::New();
241     SetupPath(path);
242
243     //Create a PathConstrainer
244     Dali::PathConstrainer pathConstrainer = Dali::PathConstrainer::New();
245     SetupPathConstrainer( pathConstrainer );
246
247     //Apply the path constraint to the actor's position. The source property for the constraint will be the custom property "t"
248     Vector2 range( 0.0f, 1.0f );
249     pathConstrainer.Apply( Property(actor,Dali::Actor::Property::POSITION), Property(actor,index), range );
250
251     //Test that the constraint is correctly applied
252     actor.SetProperty(index,0.5f);
253     application.SendNotification();
254     application.Render(static_cast<unsigned int>(1.0f));
255
256     Vector3 position, tangent;
257     path.Sample(0.5f, position, tangent );
258     DALI_TEST_EQUALS( actor.GetCurrentPosition(), position, TEST_LOCATION );
259
260   }
261
262   //PathConstrainer has been destroyed. Constraint in the actor should have been removed
263   actor.SetProperty(index,0.75f);
264   application.SendNotification();
265   application.Render(static_cast<unsigned int>(1.0f));
266
267   DALI_TEST_EQUALS( actor.GetCurrentPosition(), Vector3::ZERO, TEST_LOCATION );
268
269   END_TEST;
270 }
271
272 int UtcPathConstrainerRemove(void)
273 {
274   TestApplication application;
275
276   Dali::Actor actor = Dali::Actor::New();
277
278   // Register a float property
279   Property::Index index = actor.RegisterProperty( "t", 0.0f );
280   Dali::Stage::GetCurrent().Add(actor);
281
282   //Create a Path
283   Dali::Path path = Dali::Path::New();
284   SetupPath(path);
285
286   //Create a PathConstrainer
287   Dali::PathConstrainer pathConstrainer = Dali::PathConstrainer::New();
288   SetupPathConstrainer( pathConstrainer );
289
290   //Apply the path constraint to the actor's position. The source property for the constraint will be the custom property "t"
291   Vector2 range( 0.0f, 1.0f );
292   pathConstrainer.Apply( Property(actor,Dali::Actor::Property::POSITION), Property(actor,index), range );
293
294   //Test that the constraint is correctly applied
295   actor.SetProperty(index,0.5f);
296   application.SendNotification();
297   application.Render(static_cast<unsigned int>(1.0f));
298
299   Vector3 position, tangent;
300   path.Sample(0.5f, position, tangent );
301   DALI_TEST_EQUALS( actor.GetCurrentPosition(), position, TEST_LOCATION );
302
303   //Remove constraint
304   pathConstrainer.Remove( actor );
305   actor.SetProperty(index,0.75f);
306   application.SendNotification();
307   application.Render(static_cast<unsigned int>(1.0f));
308
309   DALI_TEST_EQUALS( actor.GetCurrentPosition(), Vector3::ZERO, TEST_LOCATION );
310
311   END_TEST;
312 }
313
314 //LinearConstrainer test cases
315 int UtcLinearConstrainerDownCast(void)
316 {
317   TestApplication application;
318   Dali::LinearConstrainer linearConstrainer = Dali::LinearConstrainer::New();
319
320   BaseHandle handle( linearConstrainer );
321   Dali::LinearConstrainer linearConstrainer2 = Dali::LinearConstrainer::DownCast( handle );
322   DALI_TEST_EQUALS( (bool)linearConstrainer2, true, TEST_LOCATION );
323
324   BaseHandle handle2;
325   Dali:: LinearConstrainer linearConstrainer3 = Dali::LinearConstrainer::DownCast( handle2 );
326   DALI_TEST_EQUALS( (bool)linearConstrainer3, false, TEST_LOCATION );
327
328   END_TEST;
329 }
330
331 int UtcLinearConstrainerCopyConstructor(void)
332 {
333   TestApplication application;
334   Dali::LinearConstrainer linearConstrainer;
335   DALI_TEST_EQUALS( (bool)linearConstrainer, false, TEST_LOCATION );
336
337   linearConstrainer = Dali::LinearConstrainer::New();
338   DALI_TEST_EQUALS( (bool)linearConstrainer, true, TEST_LOCATION );
339
340   // call the copy constructor
341   Dali::LinearConstrainer linearConstrainer2( linearConstrainer );
342   DALI_TEST_EQUALS( (bool)linearConstrainer2, true, TEST_LOCATION );
343
344   END_TEST;
345 }
346
347 int UtcLinearConstrainerApply(void)
348 {
349   TestApplication application;
350
351   Dali::Actor actor = Dali::Actor::New();
352
353   // Register a float property
354   Property::Index index = actor.RegisterProperty( "t", 0.0f );
355
356   Dali::Stage::GetCurrent().Add(actor);
357
358
359   //Create a LinearConstrainer without specifying progress for values
360   Dali::LinearConstrainer linearConstrainer = Dali::LinearConstrainer::New();
361   SetupLinearConstrainerUniformProgress( linearConstrainer );
362
363   //Apply the linear constraint to the actor's position. The source property for the constraint will be the custom property "t"
364   Vector2 range( 0.0f, 1.0f );
365   linearConstrainer.Apply( Property(actor,Dali::Actor::Property::POSITION_X), Property(actor,index), range );
366
367   //Create an animation to animate the custom property
368   float durationSeconds(1.0f);
369   Dali::Animation animation = Dali::Animation::New(durationSeconds);
370   animation.AnimateTo(Dali::Property(actor,index),1.0f);
371   animation.Play();
372
373   application.SendNotification();
374   application.Render(static_cast<unsigned int>(durationSeconds*250.0f)/* 25% progress */);
375
376   DALI_TEST_EQUALS( actor.GetCurrentPosition().x, 0.5f, TEST_LOCATION );
377
378   application.SendNotification();
379   application.Render(static_cast<unsigned int>(durationSeconds*250.0f)/* 50% progress */);
380   DALI_TEST_EQUALS( actor.GetCurrentPosition().x, 1.0f, TEST_LOCATION );
381
382   application.SendNotification();
383   application.Render(static_cast<unsigned int>(durationSeconds*250.0f)/* 75% progress */);
384   DALI_TEST_EQUALS( actor.GetCurrentPosition().x, 0.5f, TEST_LOCATION );
385
386   application.SendNotification();
387   application.Render(static_cast<unsigned int>(durationSeconds*250.0f)/* 100% progress */);
388   DALI_TEST_EQUALS( actor.GetCurrentPosition().x, 0.0f, TEST_LOCATION );
389
390   application.SendNotification();
391   application.Render(static_cast<unsigned int>(durationSeconds*250.0f)/* beyond the animation duration*/);
392   DALI_TEST_EQUALS( actor.GetCurrentPosition().x, 0.0f, TEST_LOCATION );
393
394   //Setup a LinearConstrainer specifying the progress for each value
395   linearConstrainer.Remove(actor);
396   SetupLinearConstrainerNonUniformProgress( linearConstrainer );
397   linearConstrainer.Apply( Property(actor,Dali::Actor::Property::POSITION_X), Property(actor,index), range );
398
399   actor.SetProperty(index,0.0f);
400   animation.Play();
401   application.SendNotification();
402   application.Render(static_cast<unsigned int>(durationSeconds*250.0f)/* 25% progress */);
403
404   DALI_TEST_EQUALS( actor.GetCurrentPosition().x, 1.0f, TEST_LOCATION );
405
406   application.SendNotification();
407   application.Render(static_cast<unsigned int>(durationSeconds*250.0f)/* 50% progress */);
408   DALI_TEST_EQUALS( actor.GetCurrentPosition().x, 2.0f/3.0f, Math::MACHINE_EPSILON_1, TEST_LOCATION );
409
410   application.SendNotification();
411   application.Render(static_cast<unsigned int>(durationSeconds*250.0f)/* 75% progress */);
412   DALI_TEST_EQUALS( actor.GetCurrentPosition().x, 1.0f/3.0f, Math::MACHINE_EPSILON_1, TEST_LOCATION );
413
414   application.SendNotification();
415   application.Render(static_cast<unsigned int>(durationSeconds*250.0f)/* 100% progress */);
416   DALI_TEST_EQUALS( actor.GetCurrentPosition().x, 0.0f, TEST_LOCATION );
417
418   application.SendNotification();
419   application.Render(static_cast<unsigned int>(durationSeconds*250.0f)/* beyond the animation duration*/);
420   DALI_TEST_EQUALS( actor.GetCurrentPosition().x, 0.0f, TEST_LOCATION );
421
422   END_TEST;
423 }
424
425 int UtcLinearConstrainerApplyRange(void)
426 {
427   TestApplication application;
428
429   Dali::Actor actor = Dali::Actor::New();
430
431   // Register a float property
432   Property::Index index = actor.RegisterProperty( "t", 100.0f );
433   Dali::Stage::GetCurrent().Add(actor);
434
435   //Create a LinearConstrainer
436   Dali::LinearConstrainer linearConstrainer = Dali::LinearConstrainer::New();
437   SetupLinearConstrainerUniformProgress( linearConstrainer );
438
439   //Apply the linear constraint to the actor's position. The source property for the constraint will be the custom property "t"
440   Vector2 range( 100.0f, 300.0f );
441   linearConstrainer.Apply( Property(actor,Dali::Actor::Property::POSITION_X), Property(actor,index), range );
442
443
444   //Create an animation to animate the custom property
445   float durationSeconds(1.0f);
446   Dali::Animation animation = Dali::Animation::New(durationSeconds);
447   animation.AnimateTo(Dali::Property(actor,index),300.0f);
448   animation.Play();
449
450   application.SendNotification();
451   application.Render(static_cast<unsigned int>(durationSeconds*250.0f)/* 25% progress */);
452
453   DALI_TEST_EQUALS( actor.GetCurrentPosition().x, 0.5f, TEST_LOCATION );
454
455   application.SendNotification();
456   application.Render(static_cast<unsigned int>(durationSeconds*250.0f)/* 50% progress */);
457   DALI_TEST_EQUALS( actor.GetCurrentPosition().x, 1.0f, TEST_LOCATION );
458
459   application.SendNotification();
460   application.Render(static_cast<unsigned int>(durationSeconds*250.0f)/* 75% progress */);
461   DALI_TEST_EQUALS( actor.GetCurrentPosition().x, 0.5f, TEST_LOCATION );
462
463   application.SendNotification();
464   application.Render(static_cast<unsigned int>(durationSeconds*250.0f)/* 100% progress */);
465   DALI_TEST_EQUALS( actor.GetCurrentPosition().x, 0.0f, TEST_LOCATION );
466
467   application.SendNotification();
468   application.Render(static_cast<unsigned int>(durationSeconds*250.0f)/* beyond the animation duration*/);
469   DALI_TEST_EQUALS( actor.GetCurrentPosition().x, 0.0f, TEST_LOCATION );
470
471   END_TEST;
472 }
473
474 int UtcLinearConstrainerDestroy(void)
475 {
476   TestApplication application;
477
478   Dali::Actor actor = Dali::Actor::New();
479
480   // Register a float property
481   Property::Index index = actor.RegisterProperty( "t", 0.0f );
482   Dali::Stage::GetCurrent().Add(actor);
483
484   {
485     //Create a LinearConstrainer
486     Dali::LinearConstrainer linearConstrainer = Dali::LinearConstrainer::New();
487     SetupLinearConstrainerUniformProgress( linearConstrainer );
488
489     //Apply the linear constraint to the actor's position. The source property for the constraint will be the custom property "t"
490     Vector2 range( 0.0f, 1.0f );
491     linearConstrainer.Apply( Property(actor,Dali::Actor::Property::POSITION_X), Property(actor,index), range );
492
493     //Test that the constraint is correctly applied
494     actor.SetProperty(index,0.5f);
495     application.SendNotification();
496     application.Render(static_cast<unsigned int>(1.0f));
497
498     DALI_TEST_EQUALS( actor.GetCurrentPosition().x, 1.0f, TEST_LOCATION );
499
500   }
501
502   //LinearConstrainer has been destroyed. Constraint in the actor should have been removed
503   actor.SetProperty(index,0.75f);
504   application.SendNotification();
505   application.Render(static_cast<unsigned int>(1.0f));
506
507   DALI_TEST_EQUALS( actor.GetCurrentPosition().x, 0.0f, TEST_LOCATION );
508
509   END_TEST;
510 }
511
512 int UtcLinearConstrainerRemove(void)
513 {
514   TestApplication application;
515
516   Dali::Actor actor = Dali::Actor::New();
517
518   // Register a float property
519   Property::Index index = actor.RegisterProperty( "t", 0.0f );
520   Dali::Stage::GetCurrent().Add(actor);
521
522   //Create a LinearConstrainer
523   Dali::LinearConstrainer linearConstrainer = Dali::LinearConstrainer::New();
524   SetupLinearConstrainerUniformProgress( linearConstrainer );
525
526   //Apply the path constraint to the actor's position. The source property for the constraint will be the custom property "t"
527   Vector2 range( 0.0f, 1.0f );
528   linearConstrainer.Apply( Property(actor,Dali::Actor::Property::POSITION_X), Property(actor,index), range );
529
530   //Test that the constraint is correctly applied
531   actor.SetProperty(index,0.5f);
532   application.SendNotification();
533   application.Render(static_cast<unsigned int>(1.0f));
534
535   DALI_TEST_EQUALS( actor.GetCurrentPosition().x, 1.0f, TEST_LOCATION );
536
537   //Remove constraint
538   linearConstrainer.Remove( actor );
539   actor.SetProperty(index,0.75f);
540   application.SendNotification();
541   application.Render(static_cast<unsigned int>(1.0f));
542
543   DALI_TEST_EQUALS( actor.GetCurrentPosition().x, 0.0f, TEST_LOCATION );
544
545   END_TEST;
546 }