2 * Copyright (c) 2018 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-test-suite-utils.h>
26 ///////////////////////////////////////////////////////////////////////////////
27 void utc_dali_constraint_startup(void)
29 test_return_value = TET_UNDEF;
32 void utc_dali_constraint_cleanup(void)
34 test_return_value = TET_PASS;
36 ///////////////////////////////////////////////////////////////////////////////
38 ///////////////////////////////////////////////////////////////////////////////
43 * A function to use for a constraint, no data collected.
45 template< typename T >
46 void BasicFunction( T& /* current */, const PropertyInputContainer& /* inputs */ )
51 * A functor which sets a given boolean when the functor is called.
53 template< typename T >
54 struct BasicCalledFunctor
56 BasicCalledFunctor( bool& functorCalled ) : mCalled( functorCalled ) { }
58 void operator()( T& /* current */, const PropertyInputContainer& /* inputs */ )
67 * A functor which increments a given integer when the functor is called.
69 template< typename T >
70 struct CalledCountFunctor
72 CalledCountFunctor( int& callCount ) : mCallCount( callCount ) { }
74 void operator()( T& /* current */, const PropertyInputContainer& /* inputs */ )
83 * A functor which sets the given value as the value required when the functor is called.
85 template< typename T >
86 struct SetValueFunctor
88 SetValueFunctor( const T& value ) : mValue( value ) { }
90 void operator()( T& current, const PropertyInputContainer& /* inputs */ )
98 } // unnamed namespace
99 ///////////////////////////////////////////////////////////////////////////////
101 ///////////////////////////////////////////////////////////////////////////////
105 // void( *function )( T&, const PropertyInputContainer& ) )
106 ///////////////////////////////////////////////////////////////////////////////
107 namespace UtcDaliConstraintNewFunction
109 bool gConstraintFunctionCalled = false;
110 void ConstraintFunction( Vector3& /* current */, const PropertyInputContainer& /* inputs */ )
112 gConstraintFunctionCalled = true;
114 } // namespace UtcDaliConstraintNewFunction
116 int UtcDaliConstraintNewFunctionP(void)
118 // Ensure that we can create a constraint using a C function and that it is called.
120 TestApplication application;
121 UtcDaliConstraintNewFunction::gConstraintFunctionCalled = false;
123 Actor actor = Actor::New();
124 Stage::GetCurrent().Add( actor );
126 application.SendNotification();
127 application.Render();
129 DALI_TEST_EQUALS( UtcDaliConstraintNewFunction::gConstraintFunctionCalled, false, TEST_LOCATION );
132 Constraint constraint = Constraint::New< Vector3 >( actor, Actor::Property::POSITION, &UtcDaliConstraintNewFunction::ConstraintFunction );
133 DALI_TEST_CHECK( constraint );
136 application.SendNotification();
137 application.Render();
139 DALI_TEST_EQUALS( UtcDaliConstraintNewFunction::gConstraintFunctionCalled, true, TEST_LOCATION );
144 int UtcDaliConstraintNewFunctionN(void)
146 // Create a constraint with an uninitialised handle
148 TestApplication application;
150 // Add a constraint with an uninitialised handle
153 Constraint constraint = Constraint::New< Vector3 >( Actor(), Actor::Property::POSITION, &UtcDaliConstraintNewFunction::ConstraintFunction );
154 DALI_TEST_CHECK( false ); // Should not reach here
158 DALI_TEST_CHECK( true ); // Should assert!
164 // helper for next test
165 void StringConstraintFunction( std::string& /* current */, const PropertyInputContainer& /* inputs */ )
169 int UtcDaliConstraintNewFunctionNonConstrainableTypeN(void)
171 // Ensure that we can create a constraint using a C function and that it is called.
173 TestApplication application;
174 UtcDaliConstraintNewFunction::gConstraintFunctionCalled = false;
176 Actor actor = Actor::New();
177 Stage::GetCurrent().Add( actor );
179 application.SendNotification();
180 application.Render();
185 Constraint constraint = Constraint::New< std::string >( actor, Actor::Property::COLOR_MODE, &StringConstraintFunction );
186 DALI_TEST_CHECK( constraint );
188 tet_result(TET_FAIL);
190 catch ( Dali::DaliException& e )
192 DALI_TEST_ASSERT( e, "Property not constrainable", TEST_LOCATION );
198 ///////////////////////////////////////////////////////////////////////////////
200 ///////////////////////////////////////////////////////////////////////////////
205 ///////////////////////////////////////////////////////////////////////////////
206 int UtcDaliConstraintNewFunctorP(void)
208 // Ensure that we can create a constraint using a functor and that it is called.
210 TestApplication application;
211 bool functorCalled = false;
213 Actor actor = Actor::New();
214 Stage::GetCurrent().Add( actor );
216 application.SendNotification();
217 application.Render();
219 DALI_TEST_EQUALS( functorCalled, false, TEST_LOCATION );
222 Constraint constraint = Constraint::New< Vector3 >( actor, Actor::Property::POSITION, BasicCalledFunctor< Vector3 >( functorCalled ) );
223 DALI_TEST_CHECK( constraint );
226 application.SendNotification();
227 application.Render();
229 DALI_TEST_EQUALS( functorCalled, true, TEST_LOCATION );
234 int UtcDaliConstraintNewFunctorN(void)
236 // Create a constraint with an uninitialised handle
238 TestApplication application;
239 bool functorCalled = false;
241 // Add a constraint with an uninitialised handle
244 Constraint constraint = Constraint::New< Vector3 >( Actor(), Actor::Property::POSITION, BasicCalledFunctor< Vector3 >( functorCalled ) );
245 DALI_TEST_CHECK( false ); // Should not reach here
249 DALI_TEST_CHECK( true ); // Should assert!
254 ///////////////////////////////////////////////////////////////////////////////
256 ///////////////////////////////////////////////////////////////////////////////
261 // void ( T::*memberFunction ) ( P&, const PropertyInputContainer& ) )
262 ///////////////////////////////////////////////////////////////////////////////
263 namespace UtcDaliConstraintNewFunctorMember
267 Functor( bool& positionCalled, bool& scaleCalled )
268 : mPositionCalled( positionCalled ),
269 mScaleCalled( scaleCalled )
273 void Position( Vector3& /* current */, const PropertyInputContainer& /* inputs */ )
275 mPositionCalled = true;
278 void Scale( Vector3& /* current */, const PropertyInputContainer& /* inputs */ )
283 bool& mPositionCalled;
286 } // namespace UtcDaliConstraintNewFunctorMember
288 int UtcDaliConstraintNewFunctorMemberP(void)
290 // Ensure that we can create a constraint using a functor and that it is called.
292 TestApplication application;
293 bool positionFunctorCalled = false;
294 bool sizeFunctorCalled = false;
296 Actor actor = Actor::New();
297 Stage::GetCurrent().Add( actor );
299 application.SendNotification();
300 application.Render();
302 DALI_TEST_EQUALS( positionFunctorCalled, false, TEST_LOCATION );
303 DALI_TEST_EQUALS( sizeFunctorCalled, false, TEST_LOCATION );
305 // Add a constraint that calls Functor::Position
306 Constraint constraint = Constraint::New< Vector3 >(
308 Actor::Property::POSITION,
309 UtcDaliConstraintNewFunctorMember::Functor( positionFunctorCalled, sizeFunctorCalled ),
310 &UtcDaliConstraintNewFunctorMember::Functor::Position );
311 DALI_TEST_CHECK( constraint );
314 application.SendNotification();
315 application.Render();
317 DALI_TEST_EQUALS( positionFunctorCalled, true, TEST_LOCATION );
318 DALI_TEST_EQUALS( sizeFunctorCalled, false, TEST_LOCATION );
320 // Add another constraint that calls Functor::Size
321 Constraint constraint2 = Constraint::New< Vector3 >(
323 Actor::Property::SCALE,
324 UtcDaliConstraintNewFunctorMember::Functor( positionFunctorCalled, sizeFunctorCalled ),
325 &UtcDaliConstraintNewFunctorMember::Functor::Scale );
326 DALI_TEST_CHECK( constraint2 );
329 application.SendNotification();
330 application.Render();
332 DALI_TEST_EQUALS( positionFunctorCalled, true, TEST_LOCATION );
333 DALI_TEST_EQUALS( sizeFunctorCalled, true, TEST_LOCATION );
338 int UtcDaliConstraintNewFunctorMemberN(void)
340 // Create a constraint with an uninitialised handle
342 TestApplication application;
343 bool positionFunctorCalled = false;
344 bool sizeFunctorCalled = false;
346 // Add a constraint with an uninitialised handle
349 Constraint constraint = Constraint::New< Vector3 >(
351 Actor::Property::POSITION,
352 UtcDaliConstraintNewFunctorMember::Functor( positionFunctorCalled, sizeFunctorCalled ),
353 &UtcDaliConstraintNewFunctorMember::Functor::Position );
354 DALI_TEST_CHECK( false ); // Should not reach here
356 catch ( Dali::DaliException& e )
358 DALI_TEST_CHECK( true ); // Should assert!
363 ///////////////////////////////////////////////////////////////////////////////
365 ///////////////////////////////////////////////////////////////////////////////
367 ///////////////////////////////////////////////////////////////////////////////
368 int UtcDaliConstraintCloneP(void)
370 // Ensure we can clone for another actor and it's called appropriately
372 TestApplication application;
375 Actor actor = Actor::New();
376 Actor clone = Actor::New();
378 Stage stage = Stage::GetCurrent();
382 application.SendNotification();
383 application.Render();
385 DALI_TEST_EQUALS( calledCount, 0, TEST_LOCATION );
387 // Add a constraint to actor
388 Constraint constraint = Constraint::New< Vector3 >( actor, Actor::Property::POSITION, CalledCountFunctor< Vector3 >( calledCount ) );
389 DALI_TEST_CHECK( constraint );
392 // Create a clone but don't apply
393 Constraint constraintClone = constraint.Clone( clone );
395 application.SendNotification();
396 application.Render();
398 DALI_TEST_EQUALS( calledCount, 1, TEST_LOCATION );
403 application.SendNotification();
404 application.Render();
406 DALI_TEST_EQUALS( calledCount, 1, TEST_LOCATION );
408 // Apply the clone constraint
409 constraintClone.Apply();
411 application.SendNotification();
412 application.Render();
414 // Should be called once for the new constraint clone and once for the original constraint
415 DALI_TEST_EQUALS( calledCount, 3, TEST_LOCATION );
420 // Change the position of both actors
421 actor.SetPosition( 100.0f, 100.0f );
422 clone.SetPosition( 100.0f, 100.0f );
424 application.SendNotification();
425 application.Render();
427 // Functor should have been called twice
428 DALI_TEST_EQUALS( calledCount, 2, TEST_LOCATION );
433 int UtcDaliConstraintCloneN(void)
435 // Attempt to clone an uninitialised constraint should cause an assert
437 TestApplication application;
439 Constraint constraint;
443 Actor actor = Actor::New();
444 Constraint clone = constraint.Clone( actor );
445 DALI_TEST_CHECK( false );
449 DALI_TEST_CHECK( true );
455 namespace UtcDaliConstraintClone
457 void Function( Vector3& /* current */, const PropertyInputContainer& inputs )
459 DALI_TEST_EQUALS( inputs[0]->GetType(), Property::VECTOR3, TEST_LOCATION );
460 DALI_TEST_EQUALS( inputs[1]->GetType(), Property::ROTATION, TEST_LOCATION );
461 DALI_TEST_EQUALS( inputs[2]->GetType(), Property::VECTOR4, TEST_LOCATION );
462 DALI_TEST_EQUALS( inputs[3]->GetType(), Property::BOOLEAN, TEST_LOCATION );
464 } // namespace UtcDaliConstraintClone
466 int UtcDaliConstraintCloneCheckSourcesAndSetters(void)
468 // Ensure all sources, the tag and remove-action are cloned appropriately
470 TestApplication application;
472 Actor actor = Actor::New();
473 Actor clone = Actor::New();
475 Stage stage = Stage::GetCurrent();
479 application.SendNotification();
480 application.Render();
482 // Create a constraint, DON'T Apply it though
483 Constraint constraint = Constraint::New< Vector3 >( actor, Actor::Property::POSITION, &UtcDaliConstraintClone::Function );
484 constraint.AddSource( LocalSource( Actor::Property::SIZE ) );
485 constraint.AddSource( LocalSource( Actor::Property::ORIENTATION ) );
486 constraint.AddSource( LocalSource( Actor::Property::COLOR ) );
487 constraint.AddSource( LocalSource( Actor::Property::VISIBLE ) );
488 constraint.SetRemoveAction( Constraint::Discard );
489 constraint.SetTag( 123 );
491 // Clone the constraint & apply the clone
492 Constraint constraintClone = constraint.Clone( clone );
493 constraintClone.Apply();
495 application.SendNotification();
496 application.Render();
498 DALI_TEST_EQUALS( constraint.GetRemoveAction(), constraintClone.GetRemoveAction(), TEST_LOCATION );
499 DALI_TEST_EQUALS( constraint.GetTag(), constraintClone.GetTag(), TEST_LOCATION );
503 ///////////////////////////////////////////////////////////////////////////////
505 ///////////////////////////////////////////////////////////////////////////////
506 // Constraint::Constraint( const Constraint& )
507 // Constraint::operator=
508 ///////////////////////////////////////////////////////////////////////////////
509 int UtcDaliConstraintCopyAndAssignment(void)
511 // Ensure copy constructor & assignment operators work
513 TestApplication application;
515 Actor actor = Actor::New();
516 Stage::GetCurrent().Add( actor );
518 Constraint constraint = Constraint::New< Vector3 >( actor, Actor::Property::POSITION, &BasicFunction< Vector3 > );
519 Constraint copied( constraint );
521 DALI_TEST_CHECK( constraint == copied );
522 DALI_TEST_CHECK( copied != assigned );
524 assigned = constraint;
525 DALI_TEST_CHECK( constraint == assigned );
529 ///////////////////////////////////////////////////////////////////////////////
531 ///////////////////////////////////////////////////////////////////////////////
532 // Constraint::DownCast
533 ///////////////////////////////////////////////////////////////////////////////
534 int UtcDaliConstraintDownCast(void)
536 // Ensure DownCast works as expected
538 TestApplication application;
540 Actor actor = Actor::New();
541 Constraint constraint = Constraint::New< Vector3 >( actor, Actor::Property::POSITION, &BasicFunction< Vector3 > );
543 // Another BaseHandle type
544 Constraint downCast = Constraint::DownCast( actor );
545 DALI_TEST_CHECK( ! downCast );
548 downCast = Constraint::DownCast( constraint );
549 DALI_TEST_CHECK( downCast );
551 // An empty constraint
552 downCast = Constraint::DownCast( Constraint() );
553 DALI_TEST_CHECK( ! downCast );
557 ///////////////////////////////////////////////////////////////////////////////
559 ///////////////////////////////////////////////////////////////////////////////
560 // Constraint::GetTargetObject
561 ///////////////////////////////////////////////////////////////////////////////
562 int UtcDaliConstraintGetTargetObjectP(void)
564 TestApplication application;
566 Actor actor = Actor::New();
567 Constraint constraint = Constraint::New< Vector3 >( actor, Actor::Property::POSITION, &BasicFunction< Vector3 > );
568 DALI_TEST_CHECK( constraint.GetTargetObject() == actor );
570 Actor actor2 = Actor::New();
571 DALI_TEST_CHECK( constraint.GetTargetObject() != actor2 );
576 int UtcDaliConstraintGetTargetObjectN(void)
578 // Attempt to retrieve from uninitialised constraint
580 TestApplication application;
582 Constraint constraint;
585 Handle handle = constraint.GetTargetObject();
586 DALI_TEST_CHECK( false ); // Should not reach here!
590 DALI_TEST_CHECK( true );
595 ///////////////////////////////////////////////////////////////////////////////
597 ///////////////////////////////////////////////////////////////////////////////
598 // Constraint::GetTargetProperty
599 ///////////////////////////////////////////////////////////////////////////////
600 int UtcDaliConstraintGetTargetPropertyP(void)
602 TestApplication application;
604 Actor actor = Actor::New();
605 Constraint constraint = Constraint::New< Vector3 >( actor, Actor::Property::POSITION, &BasicFunction< Vector3 > );
606 DALI_TEST_EQUALS( constraint.GetTargetProperty(), (Property::Index)Actor::Property::POSITION, TEST_LOCATION );
611 int UtcDaliConstraintGetTargetPropertyN(void)
613 // Attempt to retrieve from uninitialised constraint
615 TestApplication application;
617 Constraint constraint;
620 Property::Index propertyIndex = constraint.GetTargetProperty();
621 ( void )propertyIndex;
622 DALI_TEST_CHECK( false ); // Should not reach here!
626 DALI_TEST_CHECK( true );
631 ///////////////////////////////////////////////////////////////////////////////
633 ///////////////////////////////////////////////////////////////////////////////
634 // Constraint::SetTag
635 // Constraint::GetTag
636 ///////////////////////////////////////////////////////////////////////////////
637 int UtcDaliConstraintTagP(void)
639 TestApplication application;
641 Actor actor = Actor::New();
642 Constraint constraint = Constraint::New< Vector3 >( actor, Actor::Property::POSITION, &BasicFunction< Vector3 > );
643 DALI_TEST_EQUALS( constraint.GetTag(), 0u, TEST_LOCATION );
645 const unsigned int tag = 123;
646 constraint.SetTag( tag );
647 DALI_TEST_EQUALS( constraint.GetTag(), tag, TEST_LOCATION );
652 int UtcDaliConstraintSetTagN(void)
654 // Attempt to set from uninitialised constraint
656 TestApplication application;
658 Constraint constraint;
661 constraint.SetTag( 123 );
662 DALI_TEST_CHECK( false ); // Should not reach here!
666 DALI_TEST_CHECK( true );
672 int UtcDaliConstraintGetTagN(void)
674 // Attempt to retrieve from uninitialised constraint
676 TestApplication application;
678 Constraint constraint;
681 int tag = constraint.GetTag();
683 DALI_TEST_CHECK( false ); // Should not reach here!
687 DALI_TEST_CHECK( true );
693 ///////////////////////////////////////////////////////////////////////////////
695 ///////////////////////////////////////////////////////////////////////////////
696 // Constraint::SetRemoveAction
697 // Constraint::GetRemoveAction
698 ///////////////////////////////////////////////////////////////////////////////
699 int UtcDaliConstraintRemoveActionP(void)
701 TestApplication application;
703 Actor actor = Actor::New();
704 Constraint constraint = Constraint::New< Vector3 >( actor, Actor::Property::POSITION, &BasicFunction< Vector3 > );
705 DALI_TEST_EQUALS( constraint.GetRemoveAction(), Constraint::DEFAULT_REMOVE_ACTION, TEST_LOCATION );
707 constraint.SetRemoveAction( Constraint::Discard );
708 DALI_TEST_EQUALS( constraint.GetRemoveAction(), Constraint::Discard, TEST_LOCATION );
710 constraint.SetRemoveAction( Constraint::Bake );
711 DALI_TEST_EQUALS( constraint.GetRemoveAction(), Constraint::Bake, TEST_LOCATION );
716 int UtcDaliConstraintSetRemoveActionN(void)
718 // Attempt to set from uninitialised constraint
720 TestApplication application;
722 Constraint constraint;
725 constraint.SetRemoveAction( Constraint::Discard );
726 DALI_TEST_CHECK( false ); // Should not reach here!
730 DALI_TEST_CHECK( true );
736 int UtcDaliConstraintGetRemoveActionN(void)
738 // Attempt to retrieve from uninitialised constraint
740 TestApplication application;
742 Constraint constraint;
745 Constraint::RemoveAction removeAction = constraint.GetRemoveAction();
746 ( void )removeAction;
747 DALI_TEST_CHECK( false ); // Should not reach here!
751 DALI_TEST_CHECK( true );
757 int UtcDaliConstraintBakeRemoveAction(void)
759 // Ensure value is baked when constraint is removed
761 TestApplication application;
763 Actor actor = Actor::New();
764 Stage::GetCurrent().Add( actor );
766 application.SendNotification();
767 application.Render();
769 // Should not equal position by default
770 Vector3 position( 10.0f, 20.0f, 30.0f );
771 DALI_TEST_CHECK( actor.GetCurrentPosition() != position );
773 // Create a constraint that constrains to position
774 Constraint constraint = Constraint::New< Vector3 >( actor, Actor::Property::POSITION, SetValueFunctor< Vector3 >( position ) );
775 constraint.SetRemoveAction( Constraint::Bake );
778 application.SendNotification();
779 application.Render();
781 DALI_TEST_EQUALS( actor.GetCurrentPosition(), position, TEST_LOCATION );
783 // Remove the constraint, it should still be at position
786 application.SendNotification();
787 application.Render();
789 DALI_TEST_EQUALS( actor.GetCurrentPosition(), position, TEST_LOCATION );
794 int UtcDaliConstraintDiscardRemoveAction(void)
796 // Ensure value is baked when constraint is removed
798 TestApplication application;
800 Actor actor = Actor::New();
801 Stage::GetCurrent().Add( actor );
803 application.SendNotification();
804 application.Render();
806 // Get and store current position
807 Vector3 originalPosition = actor.GetCurrentPosition();
809 // Should not equal position by default
810 Vector3 position( 10.0f, 20.0f, 30.0f );
811 DALI_TEST_CHECK( actor.GetCurrentPosition() != position );
813 // Create a constraint that constrains to position
814 Constraint constraint = Constraint::New< Vector3 >( actor, Actor::Property::POSITION, SetValueFunctor< Vector3 >( position ) );
815 constraint.SetRemoveAction( Constraint::Discard );
818 application.SendNotification();
819 application.Render();
821 DALI_TEST_EQUALS( actor.GetCurrentPosition(), position, TEST_LOCATION );
823 // Remove the constraint, it should still be at position
826 application.SendNotification();
827 application.Render();
829 DALI_TEST_EQUALS( actor.GetCurrentPosition(), originalPosition, TEST_LOCATION );
830 DALI_TEST_CHECK( actor.GetCurrentPosition() != position );
835 ///////////////////////////////////////////////////////////////////////////////
837 ///////////////////////////////////////////////////////////////////////////////
839 // Constraint::Remove
840 ///////////////////////////////////////////////////////////////////////////////
841 int UtcDaliConstraintApplyRemove(void)
843 // Ensure constraint functors are called appropriately
845 TestApplication application;
846 bool functorCalled = false;
848 Actor actor = Actor::New();
849 Stage::GetCurrent().Add( actor );
851 application.SendNotification();
852 application.Render();
854 DALI_TEST_EQUALS( functorCalled, false, TEST_LOCATION );
856 // Create a constraint and apply, functor should be called
857 Constraint constraint = Constraint::New< Vector3 >( actor, Actor::Property::POSITION, BasicCalledFunctor< Vector3 >( functorCalled ) );
860 application.SendNotification();
861 application.Render();
863 DALI_TEST_EQUALS( functorCalled, true, TEST_LOCATION );
866 functorCalled = false;
868 // Remove the constraint, functor should not be called
871 application.SendNotification();
872 application.Render();
874 DALI_TEST_EQUALS( functorCalled, false, TEST_LOCATION );
876 // Re-apply the constraint, functor should be called again
879 application.SendNotification();
880 application.Render();
882 DALI_TEST_EQUALS( functorCalled, true, TEST_LOCATION );
887 int UtcDaliConstraintApplyBeforeAddedToStage(void)
889 // Constraint gets applied to an off-stage actor.
890 // Constraint should be automatically applied when the actor is added to the stage and not before
892 TestApplication application;
893 bool functorCalled = false;
895 // Create an actor and a constraint and apply, DON'T add to stage just yet
896 Actor actor = Actor::New();
897 Constraint constraint = Constraint::New< Vector3 >( actor, Actor::Property::POSITION, BasicCalledFunctor< Vector3 >( functorCalled ) );
900 application.SendNotification();
901 application.Render();
903 // Should NOT be called
904 DALI_TEST_EQUALS( functorCalled, false, TEST_LOCATION );
906 // Add actor to stage
907 Stage::GetCurrent().Add( actor );
909 application.SendNotification();
910 application.Render();
912 // Should now be called
913 DALI_TEST_EQUALS( functorCalled, true, TEST_LOCATION );
918 int UtcDaliConstraintApplyAndRemoveBeforeAddedToStage(void)
920 // Constraint gets applied to an off-stage actor, then gets removed before it's added to the stage
921 // Constraint should NOT be called at all
923 TestApplication application;
924 bool functorCalled = false;
926 // Create an actor and a constraint and apply, DON'T add to stage just yet
927 Actor actor = Actor::New();
928 Constraint constraint = Constraint::New< Vector3 >( actor, Actor::Property::POSITION, BasicCalledFunctor< Vector3 >( functorCalled ) );
931 application.SendNotification();
932 application.Render();
934 // Should NOT be called
935 DALI_TEST_EQUALS( functorCalled, false, TEST_LOCATION );
937 // Remove the constraint
940 // Add actor to stage
941 Stage::GetCurrent().Add( actor );
943 application.SendNotification();
944 application.Render();
946 // Still should NOT be called
947 DALI_TEST_EQUALS( functorCalled, false, TEST_LOCATION );
952 int UtcDaliConstraintApplyActorStagedUnstaged(void)
954 // Apply a constraint to an actor which is staged and unstaged.
955 // Functor should only be called while the actor is staged.
957 TestApplication application;
958 bool functorCalled = false;
960 // Create an actor and add to stage
961 Actor actor = Actor::New();
962 Stage stage = Stage::GetCurrent();
965 // Create a constraint and apply
966 Constraint constraint = Constraint::New< Vector3 >( actor, Actor::Property::POSITION, BasicCalledFunctor< Vector3 >( functorCalled ) );
969 application.SendNotification();
970 application.Render();
972 // Constraint should be called
973 DALI_TEST_EQUALS( functorCalled, true, TEST_LOCATION );
976 functorCalled = false;
978 // Remove actor from stage
979 stage.Remove( actor );
981 application.SendNotification();
982 application.Render();
984 // Constraint should NOT be called
985 DALI_TEST_EQUALS( functorCalled, false, TEST_LOCATION );
990 application.SendNotification();
991 application.Render();
993 // Constraint should be called
994 DALI_TEST_EQUALS( functorCalled, true, TEST_LOCATION );
999 int UtcDaliConstraintApplySeveralTimes(void)
1001 // Apply the same constraint several times.
1002 // Should not cause any problems (subsequent attempts should be no-ops)
1004 TestApplication application;
1007 // Create an actor and add to stage
1008 Actor actor = Actor::New();
1009 Stage stage = Stage::GetCurrent();
1012 // Create a constraint and apply
1013 Constraint constraint = Constraint::New< Vector3 >( actor, Actor::Property::POSITION, CalledCountFunctor< Vector3 >( count ) );
1017 constraint.Apply(); // no-op
1019 application.SendNotification();
1020 application.Render();
1022 // Should only have been called once
1023 DALI_TEST_EQUALS( count, 1, TEST_LOCATION );
1029 constraint.Apply(); // no-op
1031 application.SendNotification();
1032 application.Render();
1034 DALI_TEST_EQUALS( count, 1, TEST_LOCATION );
1039 // Change the position property, apply again
1040 actor.SetPosition( 10.0f, 10.0f );
1043 application.SendNotification();
1044 application.Render();
1046 // Constraint should have been called once
1047 DALI_TEST_EQUALS( count, 1, TEST_LOCATION );
1052 ///////////////////////////////////////////////////////////////////////////////
1054 ///////////////////////////////////////////////////////////////////////////////
1055 // Constraint::AddSource
1056 ///////////////////////////////////////////////////////////////////////////////
1057 namespace UtcDaliConstraintAddSource
1059 void Function( Vector3& /* current */, const PropertyInputContainer& inputs )
1061 DALI_TEST_EQUALS( inputs.Size(), 4u, TEST_LOCATION );
1062 DALI_TEST_EQUALS( inputs[0]->GetType(), Property::VECTOR3, TEST_LOCATION );
1063 DALI_TEST_EQUALS( inputs[1]->GetType(), Property::ROTATION, TEST_LOCATION );
1064 DALI_TEST_EQUALS( inputs[2]->GetType(), Property::VECTOR4, TEST_LOCATION );
1065 DALI_TEST_EQUALS( inputs[3]->GetType(), Property::BOOLEAN, TEST_LOCATION );
1067 } // namespace UtcDaliConstraintAddSource
1069 int UtcDaliConstraintAddSourceP(void)
1071 // Ensure all sources are in the correct order in the functor
1073 TestApplication application;
1075 Actor actor = Actor::New();
1076 Stage::GetCurrent().Add( actor );
1078 // Create a constraint, add sources
1079 Constraint constraint = Constraint::New< Vector3 >( actor, Actor::Property::POSITION, &UtcDaliConstraintAddSource::Function );
1080 constraint.AddSource( LocalSource( Actor::Property::SIZE ) );
1081 constraint.AddSource( LocalSource( Actor::Property::ORIENTATION ) );
1082 constraint.AddSource( LocalSource( Actor::Property::COLOR ) );
1083 constraint.AddSource( LocalSource( Actor::Property::VISIBLE ) );
1086 application.SendNotification();
1087 application.Render();
1092 int UtcDaliConstraintAddSourceN(void)
1094 // Attempt to set from uninitialised constraint
1096 TestApplication application;
1098 Constraint constraint;
1101 constraint.AddSource( LocalSource( Actor::Property::POSITION ) );
1102 DALI_TEST_CHECK( false ); // Should not reach here!
1106 DALI_TEST_CHECK( true );
1111 ///////////////////////////////////////////////////////////////////////////////
1113 ///////////////////////////////////////////////////////////////////////////////
1114 namespace TestChaining
1117 const Vector3 gFunction1Output( Vector3::ONE );
1118 void Function1( Vector3& current, const PropertyInputContainer& /* inputs */ )
1120 // current is original position
1121 DALI_TEST_EQUALS( current, Vector3::ZERO, TEST_LOCATION );
1122 current = gFunction1Output;
1125 const Vector3 gFunction2Output( 10.0f, 20.0f, 30.0f );
1126 void Function2( Vector3& current, const PropertyInputContainer& /* inputs */ )
1128 // current is output from Function1
1129 DALI_TEST_EQUALS( current, gFunction1Output, TEST_LOCATION );
1131 current = gFunction2Output;
1134 const Vector3 gFunction3Output( 10.0f, 20.0f, 30.0f );
1135 void Function3( Vector3& current, const PropertyInputContainer& /* inputs */ )
1137 // current is output from Function2
1138 DALI_TEST_EQUALS( current, gFunction2Output, TEST_LOCATION );
1140 current = gFunction3Output;
1143 const Vector3 gFunction4Output( 10.0f, 20.0f, 30.0f );
1144 void Function4( Vector3& current, const PropertyInputContainer& /* inputs */ )
1146 // current is output from Function3
1147 DALI_TEST_EQUALS( current, gFunction3Output, TEST_LOCATION );
1149 current = gFunction4Output;
1152 void Function5( Vector3& current, const PropertyInputContainer& /* inputs */ )
1154 // current is output from Function4
1155 DALI_TEST_EQUALS( current, gFunction4Output, TEST_LOCATION );
1157 current = Vector3::ZERO;
1160 } // namespace TestChaining
1162 int UtcDaliConstraintChaining(void)
1164 // Apply several constraints to the same property and ensure the functors are called in the correct order.
1166 TestApplication application;
1168 Actor actor = Actor::New();
1169 Stage::GetCurrent().Add( actor );
1171 Constraint constraint1 = Constraint::New< Vector3 >( actor, Actor::Property::POSITION, &TestChaining::Function1 );
1172 Constraint constraint2 = Constraint::New< Vector3 >( actor, Actor::Property::POSITION, &TestChaining::Function2 );
1173 Constraint constraint3 = Constraint::New< Vector3 >( actor, Actor::Property::POSITION, &TestChaining::Function3 );
1174 Constraint constraint4 = Constraint::New< Vector3 >( actor, Actor::Property::POSITION, &TestChaining::Function4 );
1175 Constraint constraint5 = Constraint::New< Vector3 >( actor, Actor::Property::POSITION, &TestChaining::Function5 );
1177 constraint1.Apply();
1178 constraint2.Apply();
1179 constraint3.Apply();
1180 constraint4.Apply();
1181 constraint5.Apply();
1183 application.SendNotification();
1184 application.Render();
1188 ///////////////////////////////////////////////////////////////////////////////
1190 ///////////////////////////////////////////////////////////////////////////////
1191 namespace TestPropertyTypes
1193 template< typename T >
1194 void Execute( T value )
1196 TestApplication application;
1197 bool functorCalled = false;
1199 Actor actor = Actor::New();
1200 Property::Index index = actor.RegisterProperty( "TEMP_PROPERTY_NAME", value );
1202 Stage::GetCurrent().Add( actor );
1204 application.SendNotification();
1205 application.Render();
1207 DALI_TEST_EQUALS( functorCalled, false, TEST_LOCATION );
1210 Constraint constraint = Constraint::New< T >( actor, index, BasicCalledFunctor< T >( functorCalled ) );
1211 DALI_TEST_CHECK( constraint );
1214 application.SendNotification();
1215 application.Render();
1217 DALI_TEST_EQUALS( functorCalled, true, TEST_LOCATION );
1219 } // namespace UtcDaliConstraintNewFunctor
1221 int UtcDaliConstraintTestPropertyTypesP(void)
1223 // Ensure we can use a constraint functor with all supported property types
1225 TestPropertyTypes::Execute< bool >( false );
1226 TestPropertyTypes::Execute< int >( 0 );
1227 TestPropertyTypes::Execute< float >( 0.0f );
1228 TestPropertyTypes::Execute< Vector2 >( Vector2::ZERO );
1229 TestPropertyTypes::Execute< Vector3 >( Vector3::ZERO );
1230 TestPropertyTypes::Execute< Vector4 >( Vector4::ZERO );
1231 TestPropertyTypes::Execute< Quaternion >( Quaternion::IDENTITY );
1232 TestPropertyTypes::Execute< Matrix >( Matrix::IDENTITY );
1233 TestPropertyTypes::Execute< Matrix3 >( Matrix3::IDENTITY );
1238 ///////////////////////////////////////////////////////////////////////////////
1240 ///////////////////////////////////////////////////////////////////////////////
1243 void SetHalfOpacity( Vector4& current, const PropertyInputContainer& inputs )
1247 } // unnamed namespace
1249 int UtcDaliConstraintEnsureResetterAppliedOnStageRemoval(void)
1251 // Ensure BOTH double-buffered values of our color property is reset when a constraint is applied to it.
1253 TestApplication application;
1255 Actor actor = Actor::New();
1256 Stage::GetCurrent().Add( actor );
1258 // Check initial value is fully opaque
1259 application.SendNotification();
1260 application.Render();
1261 DALI_TEST_EQUALS( actor.GetCurrentColor().a, 1.0f, TEST_LOCATION );
1263 // Create a constraint whose value is discarded when it is removed
1264 Constraint constraint = Constraint::New< Vector4 >( actor, Actor::Property::COLOR, SetHalfOpacity );
1265 constraint.SetRemoveAction( Constraint::RemoveAction::Discard );
1268 // Check value after one render, it should be constrained
1269 application.SendNotification();
1270 application.Render();
1271 DALI_TEST_EQUALS( actor.GetCurrentColor().a, 0.5f, TEST_LOCATION );
1273 // Render another frame, ensure the other value has also been updated
1274 application.SendNotification();
1275 application.Render();
1276 DALI_TEST_EQUALS( actor.GetCurrentColor().a, 0.5f, TEST_LOCATION );
1278 // Remove the actor from the stage and delete the constraint
1280 constraint.Remove();
1283 // Check value while off-stage, it should be fully opaque
1284 application.SendNotification();
1285 application.Render();
1286 DALI_TEST_EQUALS( actor.GetCurrentColor().a, 1.0f, TEST_LOCATION );
1288 // Add the actor back to the stage and check the value, it should be fully opaque again
1289 Stage::GetCurrent().Add( actor );
1291 // Check value when back on-stage, it should be fully opaque as the constraint is no longer applied to it.
1292 application.SendNotification();
1293 application.Render();
1294 DALI_TEST_EQUALS( actor.GetCurrentColor().a, 1.0f, TEST_LOCATION );
1296 // Render for another frame to ensure both buffers have the correct value
1297 application.SendNotification();
1298 application.Render();
1299 DALI_TEST_EQUALS( actor.GetCurrentColor().a, 1.0f, TEST_LOCATION );
1304 int UtcDaliConstraintOnActorAddedAndRemoved(void)
1306 // Ensure adding and removing an actor from stage with a constraint still has it applied when it is re-added back to the stage
1308 TestApplication application;
1310 Actor actor = Actor::New();
1311 Stage::GetCurrent().Add( actor );
1313 // Check initial value is fully opaque
1314 application.SendNotification();
1315 application.Render();
1316 DALI_TEST_EQUALS( actor.GetCurrentColor().a, 1.0f, TEST_LOCATION );
1318 // Create a constraint whose value is discarded when it is removed
1319 Constraint constraint = Constraint::New< Vector4 >( actor, Actor::Property::COLOR, SetHalfOpacity );
1320 constraint.SetRemoveAction( Constraint::RemoveAction::Discard );
1323 // Check value after one render, it should be constrained
1324 application.SendNotification();
1325 application.Render();
1326 DALI_TEST_EQUALS( actor.GetCurrentColor().a, 0.5f, TEST_LOCATION );
1328 // Render another frame, ensure the other value has also been updated
1329 application.SendNotification();
1330 application.Render();
1331 DALI_TEST_EQUALS( actor.GetCurrentColor().a, 0.5f, TEST_LOCATION );
1333 // Remove the actor from the stage
1336 // Check value while off-stage, the constraint is no longer being applied as it's off-stage
1337 application.SendNotification();
1338 application.Render();
1339 DALI_TEST_EQUALS( actor.GetCurrentColor().a, 1.0f, TEST_LOCATION );
1341 // Check the other buffer, the constraint should not be applied to this either.
1342 application.SendNotification();
1343 application.Render();
1344 DALI_TEST_EQUALS( actor.GetCurrentColor().a, 1.0f, TEST_LOCATION );
1346 // Add the actor back to the stage and check the value, the constraint should have been re-applied
1347 Stage::GetCurrent().Add( actor );
1348 application.SendNotification();
1349 application.Render();
1350 DALI_TEST_EQUALS( actor.GetCurrentColor().a, 0.5f, TEST_LOCATION );
1352 // Render for another frame to ensure both buffers have the correct value
1353 application.SendNotification();
1354 application.Render();
1355 DALI_TEST_EQUALS( actor.GetCurrentColor().a, 0.5f, TEST_LOCATION );
1360 ///////////////////////////////////////////////////////////////////////////////