Move more public-api headers to devel-api. PART 2
[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 UtcLinearConstrainerApply(void)
316 {
317   TestApplication application;
318
319   Dali::Actor actor = Dali::Actor::New();
320
321   // Register a float property
322   Property::Index index = actor.RegisterProperty( "t", 0.0f );
323
324   Dali::Stage::GetCurrent().Add(actor);
325
326
327   //Create a LinearConstrainer without specifying progress for values
328   Dali::LinearConstrainer linearConstrainer = Dali::LinearConstrainer::New();
329   SetupLinearConstrainerUniformProgress( linearConstrainer );
330
331   //Apply the linear constraint to the actor's position. The source property for the constraint will be the custom property "t"
332   Vector2 range( 0.0f, 1.0f );
333   linearConstrainer.Apply( Property(actor,Dali::Actor::Property::POSITION_X), Property(actor,index), range );
334
335   //Create an animation to animate the custom property
336   float durationSeconds(1.0f);
337   Dali::Animation animation = Dali::Animation::New(durationSeconds);
338   animation.AnimateTo(Dali::Property(actor,index),1.0f);
339   animation.Play();
340
341   application.SendNotification();
342   application.Render(static_cast<unsigned int>(durationSeconds*250.0f)/* 25% progress */);
343
344   DALI_TEST_EQUALS( actor.GetCurrentPosition().x, 0.5f, TEST_LOCATION );
345
346   application.SendNotification();
347   application.Render(static_cast<unsigned int>(durationSeconds*250.0f)/* 50% progress */);
348   DALI_TEST_EQUALS( actor.GetCurrentPosition().x, 1.0f, TEST_LOCATION );
349
350   application.SendNotification();
351   application.Render(static_cast<unsigned int>(durationSeconds*250.0f)/* 75% progress */);
352   DALI_TEST_EQUALS( actor.GetCurrentPosition().x, 0.5f, TEST_LOCATION );
353
354   application.SendNotification();
355   application.Render(static_cast<unsigned int>(durationSeconds*250.0f)/* 100% progress */);
356   DALI_TEST_EQUALS( actor.GetCurrentPosition().x, 0.0f, TEST_LOCATION );
357
358   application.SendNotification();
359   application.Render(static_cast<unsigned int>(durationSeconds*250.0f)/* beyond the animation duration*/);
360   DALI_TEST_EQUALS( actor.GetCurrentPosition().x, 0.0f, TEST_LOCATION );
361
362   //Setup a LinearConstrainer specifying the progress for each value
363   linearConstrainer.Remove(actor);
364   SetupLinearConstrainerNonUniformProgress( linearConstrainer );
365   linearConstrainer.Apply( Property(actor,Dali::Actor::Property::POSITION_X), Property(actor,index), range );
366
367   actor.SetProperty(index,0.0f);
368   animation.Play();
369   application.SendNotification();
370   application.Render(static_cast<unsigned int>(durationSeconds*250.0f)/* 25% progress */);
371
372   DALI_TEST_EQUALS( actor.GetCurrentPosition().x, 1.0f, TEST_LOCATION );
373
374   application.SendNotification();
375   application.Render(static_cast<unsigned int>(durationSeconds*250.0f)/* 50% progress */);
376   DALI_TEST_EQUALS( actor.GetCurrentPosition().x, 2.0f/3.0f, Math::MACHINE_EPSILON_1, TEST_LOCATION );
377
378   application.SendNotification();
379   application.Render(static_cast<unsigned int>(durationSeconds*250.0f)/* 75% progress */);
380   DALI_TEST_EQUALS( actor.GetCurrentPosition().x, 1.0f/3.0f, Math::MACHINE_EPSILON_1, TEST_LOCATION );
381
382   application.SendNotification();
383   application.Render(static_cast<unsigned int>(durationSeconds*250.0f)/* 100% progress */);
384   DALI_TEST_EQUALS( actor.GetCurrentPosition().x, 0.0f, TEST_LOCATION );
385
386   application.SendNotification();
387   application.Render(static_cast<unsigned int>(durationSeconds*250.0f)/* beyond the animation duration*/);
388   DALI_TEST_EQUALS( actor.GetCurrentPosition().x, 0.0f, TEST_LOCATION );
389
390   END_TEST;
391 }
392
393 int UtcLinearConstrainerApplyRange(void)
394 {
395   TestApplication application;
396
397   Dali::Actor actor = Dali::Actor::New();
398
399   // Register a float property
400   Property::Index index = actor.RegisterProperty( "t", 100.0f );
401   Dali::Stage::GetCurrent().Add(actor);
402
403   //Create a LinearConstrainer
404   Dali::LinearConstrainer linearConstrainer = Dali::LinearConstrainer::New();
405   SetupLinearConstrainerUniformProgress( linearConstrainer );
406
407   //Apply the linear constraint to the actor's position. The source property for the constraint will be the custom property "t"
408   Vector2 range( 100.0f, 300.0f );
409   linearConstrainer.Apply( Property(actor,Dali::Actor::Property::POSITION_X), Property(actor,index), range );
410
411
412   //Create an animation to animate the custom property
413   float durationSeconds(1.0f);
414   Dali::Animation animation = Dali::Animation::New(durationSeconds);
415   animation.AnimateTo(Dali::Property(actor,index),300.0f);
416   animation.Play();
417
418   application.SendNotification();
419   application.Render(static_cast<unsigned int>(durationSeconds*250.0f)/* 25% progress */);
420
421   DALI_TEST_EQUALS( actor.GetCurrentPosition().x, 0.5f, TEST_LOCATION );
422
423   application.SendNotification();
424   application.Render(static_cast<unsigned int>(durationSeconds*250.0f)/* 50% progress */);
425   DALI_TEST_EQUALS( actor.GetCurrentPosition().x, 1.0f, TEST_LOCATION );
426
427   application.SendNotification();
428   application.Render(static_cast<unsigned int>(durationSeconds*250.0f)/* 75% progress */);
429   DALI_TEST_EQUALS( actor.GetCurrentPosition().x, 0.5f, TEST_LOCATION );
430
431   application.SendNotification();
432   application.Render(static_cast<unsigned int>(durationSeconds*250.0f)/* 100% progress */);
433   DALI_TEST_EQUALS( actor.GetCurrentPosition().x, 0.0f, TEST_LOCATION );
434
435   application.SendNotification();
436   application.Render(static_cast<unsigned int>(durationSeconds*250.0f)/* beyond the animation duration*/);
437   DALI_TEST_EQUALS( actor.GetCurrentPosition().x, 0.0f, TEST_LOCATION );
438
439   END_TEST;
440 }
441
442 int UtcLinearConstrainerDestroy(void)
443 {
444   TestApplication application;
445
446   Dali::Actor actor = Dali::Actor::New();
447
448   // Register a float property
449   Property::Index index = actor.RegisterProperty( "t", 0.0f );
450   Dali::Stage::GetCurrent().Add(actor);
451
452   {
453     //Create a LinearConstrainer
454     Dali::LinearConstrainer linearConstrainer = Dali::LinearConstrainer::New();
455     SetupLinearConstrainerUniformProgress( linearConstrainer );
456
457     //Apply the linear constraint to the actor's position. The source property for the constraint will be the custom property "t"
458     Vector2 range( 0.0f, 1.0f );
459     linearConstrainer.Apply( Property(actor,Dali::Actor::Property::POSITION_X), Property(actor,index), range );
460
461     //Test that the constraint is correctly applied
462     actor.SetProperty(index,0.5f);
463     application.SendNotification();
464     application.Render(static_cast<unsigned int>(1.0f));
465
466     DALI_TEST_EQUALS( actor.GetCurrentPosition().x, 1.0f, TEST_LOCATION );
467
468   }
469
470   //LinearConstrainer has been destroyed. Constraint in the actor should have been removed
471   actor.SetProperty(index,0.75f);
472   application.SendNotification();
473   application.Render(static_cast<unsigned int>(1.0f));
474
475   DALI_TEST_EQUALS( actor.GetCurrentPosition().x, 0.0f, TEST_LOCATION );
476
477   END_TEST;
478 }
479
480 int UtcLinearConstrainerRemove(void)
481 {
482   TestApplication application;
483
484   Dali::Actor actor = Dali::Actor::New();
485
486   // Register a float property
487   Property::Index index = actor.RegisterProperty( "t", 0.0f );
488   Dali::Stage::GetCurrent().Add(actor);
489
490   //Create a LinearConstrainer
491   Dali::LinearConstrainer linearConstrainer = Dali::LinearConstrainer::New();
492   SetupLinearConstrainerUniformProgress( linearConstrainer );
493
494   //Apply the path constraint to the actor's position. The source property for the constraint will be the custom property "t"
495   Vector2 range( 0.0f, 1.0f );
496   linearConstrainer.Apply( Property(actor,Dali::Actor::Property::POSITION_X), Property(actor,index), range );
497
498   //Test that the constraint is correctly applied
499   actor.SetProperty(index,0.5f);
500   application.SendNotification();
501   application.Render(static_cast<unsigned int>(1.0f));
502
503   DALI_TEST_EQUALS( actor.GetCurrentPosition().x, 1.0f, TEST_LOCATION );
504
505   //Remove constraint
506   linearConstrainer.Remove( actor );
507   actor.SetProperty(index,0.75f);
508   application.SendNotification();
509   application.Render(static_cast<unsigned int>(1.0f));
510
511   DALI_TEST_EQUALS( actor.GetCurrentPosition().x, 0.0f, TEST_LOCATION );
512
513   END_TEST;
514 }