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 UtcLinearConstrainerDownCast(void)
317 TestApplication application;
318 Dali::LinearConstrainer linearConstrainer = Dali::LinearConstrainer::New();
320 BaseHandle handle( linearConstrainer );
321 Dali::LinearConstrainer linearConstrainer2 = Dali::LinearConstrainer::DownCast( handle );
322 DALI_TEST_EQUALS( (bool)linearConstrainer2, true, TEST_LOCATION );
325 Dali:: LinearConstrainer linearConstrainer3 = Dali::LinearConstrainer::DownCast( handle2 );
326 DALI_TEST_EQUALS( (bool)linearConstrainer3, false, TEST_LOCATION );
331 int UtcLinearConstrainerCopyConstructor(void)
333 TestApplication application;
334 Dali::LinearConstrainer linearConstrainer;
335 DALI_TEST_EQUALS( (bool)linearConstrainer, false, TEST_LOCATION );
337 linearConstrainer = Dali::LinearConstrainer::New();
338 DALI_TEST_EQUALS( (bool)linearConstrainer, true, TEST_LOCATION );
340 // call the copy constructor
341 Dali::LinearConstrainer linearConstrainer2( linearConstrainer );
342 DALI_TEST_EQUALS( (bool)linearConstrainer2, true, TEST_LOCATION );
347 int UtcLinearConstrainerApply(void)
349 TestApplication application;
351 Dali::Actor actor = Dali::Actor::New();
353 // Register a float property
354 Property::Index index = actor.RegisterProperty( "t", 0.0f );
356 Dali::Stage::GetCurrent().Add(actor);
359 //Create a LinearConstrainer without specifying progress for values
360 Dali::LinearConstrainer linearConstrainer = Dali::LinearConstrainer::New();
361 SetupLinearConstrainerUniformProgress( linearConstrainer );
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 );
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);
373 application.SendNotification();
374 application.Render(static_cast<unsigned int>(durationSeconds*250.0f)/* 25% progress */);
376 DALI_TEST_EQUALS( actor.GetCurrentPosition().x, 0.5f, TEST_LOCATION );
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 );
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 );
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 );
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 );
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 );
399 actor.SetProperty(index,0.0f);
401 application.SendNotification();
402 application.Render(static_cast<unsigned int>(durationSeconds*250.0f)/* 25% progress */);
404 DALI_TEST_EQUALS( actor.GetCurrentPosition().x, 1.0f, TEST_LOCATION );
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 );
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 );
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 );
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 );
425 int UtcLinearConstrainerApplyRange(void)
427 TestApplication application;
429 Dali::Actor actor = Dali::Actor::New();
431 // Register a float property
432 Property::Index index = actor.RegisterProperty( "t", 100.0f );
433 Dali::Stage::GetCurrent().Add(actor);
435 //Create a LinearConstrainer
436 Dali::LinearConstrainer linearConstrainer = Dali::LinearConstrainer::New();
437 SetupLinearConstrainerUniformProgress( linearConstrainer );
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 );
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);
450 application.SendNotification();
451 application.Render(static_cast<unsigned int>(durationSeconds*250.0f)/* 25% progress */);
453 DALI_TEST_EQUALS( actor.GetCurrentPosition().x, 0.5f, TEST_LOCATION );
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 );
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 );
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 );
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 );
474 int UtcLinearConstrainerDestroy(void)
476 TestApplication application;
478 Dali::Actor actor = Dali::Actor::New();
480 // Register a float property
481 Property::Index index = actor.RegisterProperty( "t", 0.0f );
482 Dali::Stage::GetCurrent().Add(actor);
485 //Create a LinearConstrainer
486 Dali::LinearConstrainer linearConstrainer = Dali::LinearConstrainer::New();
487 SetupLinearConstrainerUniformProgress( linearConstrainer );
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 );
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));
498 DALI_TEST_EQUALS( actor.GetCurrentPosition().x, 1.0f, TEST_LOCATION );
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));
507 DALI_TEST_EQUALS( actor.GetCurrentPosition().x, 0.0f, TEST_LOCATION );
512 int UtcLinearConstrainerRemove(void)
514 TestApplication application;
516 Dali::Actor actor = Dali::Actor::New();
518 // Register a float property
519 Property::Index index = actor.RegisterProperty( "t", 0.0f );
520 Dali::Stage::GetCurrent().Add(actor);
522 //Create a LinearConstrainer
523 Dali::LinearConstrainer linearConstrainer = Dali::LinearConstrainer::New();
524 SetupLinearConstrainerUniformProgress( linearConstrainer );
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 );
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));
535 DALI_TEST_EQUALS( actor.GetCurrentPosition().x, 1.0f, TEST_LOCATION );
538 linearConstrainer.Remove( actor );
539 actor.SetProperty(index,0.75f);
540 application.SendNotification();
541 application.Render(static_cast<unsigned int>(1.0f));
543 DALI_TEST_EQUALS( actor.GetCurrentPosition().x, 0.0f, TEST_LOCATION );