2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include <dali/public-api/dali-core.h>
22 #include <dali/devel-api/animation/path-constrainer.h>
23 #include <dali-test-suite-utils.h>
26 using namespace Dali::Internal;
31 static void SetupPath( Dali::Path& path)
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));
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) );
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) );
46 static void SetupPathConstrainer( Dali::PathConstrainer& PathConstrainer)
48 PathConstrainer.SetProperty( Dali::PathConstrainer::Property::FORWARD, Vector3(1.0f,0.0f,0.0f) );
50 Dali::Property::Array points;
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 );
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 );
65 static void SetupLinearConstrainerUniformProgress( Dali::LinearConstrainer& linearConstrainer)
67 Dali::Property::Array points;
72 linearConstrainer.SetProperty( Dali::LinearConstrainer::Property::VALUE, points );
75 static void SetupLinearConstrainerNonUniformProgress( Dali::LinearConstrainer& linearConstrainer)
77 Dali::Property::Array points;
82 linearConstrainer.SetProperty( Dali::LinearConstrainer::Property::VALUE, points );
87 linearConstrainer.SetProperty( Dali::LinearConstrainer::Property::PROGRESS, points );
90 } // anonymous namespace
92 //PathConstrainer test cases
93 int UtcPathConstrainerApply(void)
95 TestApplication application;
97 Dali::Actor actor = Dali::Actor::New();
99 // Register a float property
100 Property::Index index = actor.RegisterProperty( "t", 0.0f );
102 Dali::Stage::GetCurrent().Add(actor);
105 Dali::Path path = Dali::Path::New();
108 //Create a PathConstrainer
109 Dali::PathConstrainer pathConstrainer = Dali::PathConstrainer::New();
110 SetupPathConstrainer( pathConstrainer );
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 );
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);
122 application.SendNotification();
123 application.Render(static_cast<unsigned int>(durationSeconds*200.0f)/* 20% progress */);
125 Vector3 position, tangent;
126 path.Sample(0.2f, position, tangent );
127 DALI_TEST_EQUALS( actor.GetCurrentPosition(), position, TEST_LOCATION );
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 );
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 );
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 );
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 );
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 );
156 int UtcPathConstrainerApplyRange(void)
158 TestApplication application;
160 Dali::Actor actor = Dali::Actor::New();
162 // Register a float property
163 Property::Index index = actor.RegisterProperty( "t", 0.0f );
164 Dali::Stage::GetCurrent().Add(actor);
167 Dali::Path path = Dali::Path::New();
170 //Create a PathConstrainer
171 Dali::PathConstrainer pathConstrainer = Dali::PathConstrainer::New();
172 SetupPathConstrainer( pathConstrainer );
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 );
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);
185 application.SendNotification();
186 application.Render(static_cast<unsigned int>(durationSeconds*250.0f)/* 25% progress */);
189 Vector3 position, tangent;
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 );
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 );
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 );
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 );
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 );
228 int UtcPathConstrainerDestroy(void)
230 TestApplication application;
232 Dali::Actor actor = Dali::Actor::New();
234 // Register a float property
235 Property::Index index = actor.RegisterProperty( "t", 0.0f );
236 Dali::Stage::GetCurrent().Add(actor);
240 Dali::Path path = Dali::Path::New();
243 //Create a PathConstrainer
244 Dali::PathConstrainer pathConstrainer = Dali::PathConstrainer::New();
245 SetupPathConstrainer( pathConstrainer );
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 );
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));
256 Vector3 position, tangent;
257 path.Sample(0.5f, position, tangent );
258 DALI_TEST_EQUALS( actor.GetCurrentPosition(), position, TEST_LOCATION );
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));
267 DALI_TEST_EQUALS( actor.GetCurrentPosition(), Vector3::ZERO, TEST_LOCATION );
272 int UtcPathConstrainerRemove(void)
274 TestApplication application;
276 Dali::Actor actor = Dali::Actor::New();
278 // Register a float property
279 Property::Index index = actor.RegisterProperty( "t", 0.0f );
280 Dali::Stage::GetCurrent().Add(actor);
283 Dali::Path path = Dali::Path::New();
286 //Create a PathConstrainer
287 Dali::PathConstrainer pathConstrainer = Dali::PathConstrainer::New();
288 SetupPathConstrainer( pathConstrainer );
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 );
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));
299 Vector3 position, tangent;
300 path.Sample(0.5f, position, tangent );
301 DALI_TEST_EQUALS( actor.GetCurrentPosition(), position, TEST_LOCATION );
304 pathConstrainer.Remove( actor );
305 actor.SetProperty(index,0.75f);
306 application.SendNotification();
307 application.Render(static_cast<unsigned int>(1.0f));
309 DALI_TEST_EQUALS( actor.GetCurrentPosition(), Vector3::ZERO, TEST_LOCATION );
314 //LinearConstrainer test cases
315 int UtcLinearConstrainerApply(void)
317 TestApplication application;
319 Dali::Actor actor = Dali::Actor::New();
321 // Register a float property
322 Property::Index index = actor.RegisterProperty( "t", 0.0f );
324 Dali::Stage::GetCurrent().Add(actor);
327 //Create a LinearConstrainer without specifying progress for values
328 Dali::LinearConstrainer linearConstrainer = Dali::LinearConstrainer::New();
329 SetupLinearConstrainerUniformProgress( linearConstrainer );
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 );
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);
341 application.SendNotification();
342 application.Render(static_cast<unsigned int>(durationSeconds*250.0f)/* 25% progress */);
344 DALI_TEST_EQUALS( actor.GetCurrentPosition().x, 0.5f, TEST_LOCATION );
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 );
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 );
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 );
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 );
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 );
367 actor.SetProperty(index,0.0f);
369 application.SendNotification();
370 application.Render(static_cast<unsigned int>(durationSeconds*250.0f)/* 25% progress */);
372 DALI_TEST_EQUALS( actor.GetCurrentPosition().x, 1.0f, TEST_LOCATION );
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 );
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 );
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 );
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 );
393 int UtcLinearConstrainerApplyRange(void)
395 TestApplication application;
397 Dali::Actor actor = Dali::Actor::New();
399 // Register a float property
400 Property::Index index = actor.RegisterProperty( "t", 100.0f );
401 Dali::Stage::GetCurrent().Add(actor);
403 //Create a LinearConstrainer
404 Dali::LinearConstrainer linearConstrainer = Dali::LinearConstrainer::New();
405 SetupLinearConstrainerUniformProgress( linearConstrainer );
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 );
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);
418 application.SendNotification();
419 application.Render(static_cast<unsigned int>(durationSeconds*250.0f)/* 25% progress */);
421 DALI_TEST_EQUALS( actor.GetCurrentPosition().x, 0.5f, TEST_LOCATION );
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 );
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 );
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 );
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 );
442 int UtcLinearConstrainerDestroy(void)
444 TestApplication application;
446 Dali::Actor actor = Dali::Actor::New();
448 // Register a float property
449 Property::Index index = actor.RegisterProperty( "t", 0.0f );
450 Dali::Stage::GetCurrent().Add(actor);
453 //Create a LinearConstrainer
454 Dali::LinearConstrainer linearConstrainer = Dali::LinearConstrainer::New();
455 SetupLinearConstrainerUniformProgress( linearConstrainer );
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 );
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));
466 DALI_TEST_EQUALS( actor.GetCurrentPosition().x, 1.0f, TEST_LOCATION );
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));
475 DALI_TEST_EQUALS( actor.GetCurrentPosition().x, 0.0f, TEST_LOCATION );
480 int UtcLinearConstrainerRemove(void)
482 TestApplication application;
484 Dali::Actor actor = Dali::Actor::New();
486 // Register a float property
487 Property::Index index = actor.RegisterProperty( "t", 0.0f );
488 Dali::Stage::GetCurrent().Add(actor);
490 //Create a LinearConstrainer
491 Dali::LinearConstrainer linearConstrainer = Dali::LinearConstrainer::New();
492 SetupLinearConstrainerUniformProgress( linearConstrainer );
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 );
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));
503 DALI_TEST_EQUALS( actor.GetCurrentPosition().x, 1.0f, TEST_LOCATION );
506 linearConstrainer.Remove( actor );
507 actor.SetProperty(index,0.75f);
508 application.SendNotification();
509 application.Render(static_cast<unsigned int>(1.0f));
511 DALI_TEST_EQUALS( actor.GetCurrentPosition().x, 0.0f, TEST_LOCATION );