Upload package dali_0.9.11.
[platform/core/uifw/dali-core.git] / automated-tests / TET / dali-test-suite / animation / utc-Dali-ActiveConstraint.cpp
1 //
2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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 #include <iostream>
18
19 #include <stdlib.h>
20 #include <tet_api.h>
21
22 #include <dali/public-api/dali-core.h>
23
24 #include <dali-test-suite-utils.h>
25
26 using namespace Dali;
27
28 static void Startup();
29 static void Cleanup();
30
31 extern "C" {
32   void (*tet_startup)() = Startup;
33   void (*tet_cleanup)() = Cleanup;
34 }
35
36 static void UtcDaliConstraintGetTargetObject();
37 static void UtcDaliConstraintGetTargetProperty();
38 static void UtcDaliConstraintSetWeight();
39 static void UtcDaliConstraintGetCurrentWeight();
40 static void UtcDaliConstraintSignalApplied();
41
42 enum {
43   POSITIVE_TC_IDX = 0x01,
44   NEGATIVE_TC_IDX,
45 };
46
47 #define MAX_NUMBER_OF_TESTS 10000
48 extern "C" {
49   struct tet_testlist tet_testlist[MAX_NUMBER_OF_TESTS];
50 }
51
52 // Add test functionality for all APIs in the class (Positive and Negative)
53 TEST_FUNCTION( UtcDaliConstraintGetTargetObject,            POSITIVE_TC_IDX );
54 TEST_FUNCTION( UtcDaliConstraintGetTargetProperty,          POSITIVE_TC_IDX );
55 TEST_FUNCTION( UtcDaliConstraintSetWeight,                  POSITIVE_TC_IDX );
56 TEST_FUNCTION( UtcDaliConstraintGetCurrentWeight,           POSITIVE_TC_IDX );
57 TEST_FUNCTION( UtcDaliConstraintSignalApplied,              POSITIVE_TC_IDX );
58 TEST_FUNCTION( UtcDaliConstraintRemove,                     POSITIVE_TC_IDX );
59 TEST_FUNCTION( UtcDaliConstraintCallback,                   POSITIVE_TC_IDX );
60 TEST_FUNCTION( UtcDaliConstraintProperties,                 POSITIVE_TC_IDX );
61
62 // Called only once before first test is run.
63 static void Startup()
64 {
65 }
66
67 // Called only once after last test is run
68 static void Cleanup()
69 {
70 }
71
72 static const Vector3 TEST_CONSTRAINT_TARGET = Vector3( 10.0f, 10.0f, 10.0f );
73
74 struct TestConstraintVector3
75 {
76   Vector3 operator()( const Vector3& current )
77   {
78     return TEST_CONSTRAINT_TARGET;
79   }
80 };
81
82 static void UtcDaliConstraintGetTargetObject()
83 {
84   TestApplication application;
85
86   // Apply a constraint to an actor
87
88   Constraint constraint = Constraint::New<Vector3>( Actor::SIZE, TestConstraintVector3() );
89
90   Actor actor = Actor::New();
91
92   ActiveConstraint active = actor.ApplyConstraint( constraint );
93
94   // Retrieve the actor back from the active-constraint
95
96   Handle object = active.GetTargetObject();
97
98   DALI_TEST_CHECK( object );
99
100   DALI_TEST_CHECK( object.GetObjectPtr() == actor.GetObjectPtr() );
101
102   // Throw-away the actor, and check GetTargetObject returns NULL
103
104   object.Reset();
105   actor.Reset();
106
107   object = active.GetTargetObject();
108
109   DALI_TEST_CHECK( !object );
110 }
111
112 static void UtcDaliConstraintGetTargetProperty()
113 {
114   TestApplication application;
115
116   // Apply a constraint to an actor
117
118   Constraint constraint = Constraint::New<Vector3>( Actor::SIZE, TestConstraintVector3() );
119
120   Actor actor = Actor::New();
121
122   ActiveConstraint active = actor.ApplyConstraint( constraint );
123
124   // Check the property index
125
126   Property::Index index = active.GetTargetProperty();
127
128   DALI_TEST_CHECK( Actor::SIZE == index );
129 }
130
131 static void UtcDaliConstraintSetWeight()
132 {
133   TestApplication application;
134
135   // Apply a constraint to an actor
136
137   Constraint constraint = Constraint::New<Vector3>( Actor::SIZE, TestConstraintVector3() );
138
139   Actor actor = Actor::New();
140   Stage::GetCurrent().Add( actor );
141
142   ActiveConstraint active = actor.ApplyConstraint( constraint );
143
144   // Apply the constraint manually
145
146   active.SetWeight( 0.0f ); // start at zero
147
148   application.SendNotification();
149   application.Render(static_cast<unsigned int>(1000.0f)); // 1 elapsed second
150
151   DALI_TEST_EQUALS( actor.GetCurrentSize(), Vector3::ZERO, TEST_LOCATION );
152
153   float weight( 0.25f );
154   active.SetWeight( weight );
155   application.SendNotification();
156   application.Render(static_cast<unsigned int>(1000.0f)); // 1 elapsed second
157   DALI_TEST_EQUALS( actor.GetCurrentSize(), TEST_CONSTRAINT_TARGET * weight, TEST_LOCATION );
158
159   weight = 0.5f;
160   active.SetWeight( weight );
161   application.SendNotification();
162   application.Render(static_cast<unsigned int>(1000.0f)); // 2 elapsed seconds
163   DALI_TEST_EQUALS( actor.GetCurrentSize(), TEST_CONSTRAINT_TARGET * weight, TEST_LOCATION );
164
165   weight = 0.75f;
166   active.SetWeight( weight );
167   application.SendNotification();
168   application.Render(static_cast<unsigned int>(1000.0f)); // 2 elapsed seconds
169   DALI_TEST_EQUALS( actor.GetCurrentSize(), TEST_CONSTRAINT_TARGET * weight, TEST_LOCATION );
170
171   weight = 1.0f;
172   active.SetWeight( weight );
173   application.SendNotification();
174   application.Render(static_cast<unsigned int>(1000.0f)); // 2 elapsed seconds
175   DALI_TEST_EQUALS( actor.GetCurrentSize(), TEST_CONSTRAINT_TARGET, TEST_LOCATION );
176 }
177
178 static void UtcDaliConstraintGetCurrentWeight()
179 {
180   TestApplication application;
181
182   // Apply a constraint to an actor
183
184   Constraint constraint = Constraint::New<Vector3>( Actor::SIZE, TestConstraintVector3() );
185
186   Actor actor = Actor::New();
187
188   ActiveConstraint active = actor.ApplyConstraint( constraint );
189
190   // Check default weight
191
192   DALI_TEST_CHECK( ActiveConstraint::DEFAULT_WEIGHT == active.GetCurrentWeight() );
193 }
194
195 static void UtcDaliConstraintSignalApplied()
196 {
197   TestApplication application;
198
199   // Apply a constraint to an actor
200
201   Constraint constraint = Constraint::New<Vector3>( Actor::SIZE, TestConstraintVector3() );
202
203   float duration( 10.0f );
204   constraint.SetApplyTime( duration );
205
206   Actor actor = Actor::New();
207   Stage::GetCurrent().Add( actor );
208
209   ActiveConstraint active = actor.ApplyConstraint( constraint );
210
211   // Check signal is received after duration
212
213   bool constraintCheck( false );
214   ConstraintAppliedCheck appliedCheck( constraintCheck );
215
216   active.AppliedSignal().Connect( &application, appliedCheck );
217
218   application.SendNotification();
219   application.Render(static_cast<unsigned int>(1000.0f)); // 1 elapsed second
220
221   // Check signal has not fired
222   application.SendNotification();
223   appliedCheck.CheckSignalNotReceived();
224
225   application.Render(static_cast<unsigned int>(4000.0f)); // 5 elapsed seconds
226
227   // Check signal has not fired
228   application.SendNotification();
229   appliedCheck.CheckSignalNotReceived();
230
231   application.Render(static_cast<unsigned int>(5000.0f - 1.0f)); // <10 elapsed seconds
232
233   // Check signal has not fired
234   application.SendNotification();
235   appliedCheck.CheckSignalNotReceived();
236
237   application.Render(static_cast<unsigned int>(2.0f)); // >10 elapsed seconds
238
239   // Signal should have fired
240   application.SendNotification();
241   appliedCheck.CheckSignalReceived();
242 }
243
244 static void UtcDaliConstraintRemove()
245 {
246   TestApplication application;
247
248   // Apply a constraint to an actor
249
250   Constraint constraint = Constraint::New<Vector3>( Actor::SIZE, TestConstraintVector3() );
251
252   float duration( 1.0f );
253   constraint.SetApplyTime( duration );
254
255   Actor actor = Actor::New();
256   const Vector3 startSize( 1, 2, 3 );
257   actor.SetSize( startSize );
258   Stage::GetCurrent().Add( actor );
259
260   ActiveConstraint active = actor.ApplyConstraint( constraint );
261
262   application.SendNotification();
263   application.Render(static_cast<unsigned int>(0.0f)); // 0 elapsed seconds
264
265   DALI_TEST_CHECK( 0 == active.GetCurrentWeight() );
266   DALI_TEST_CHECK( startSize == actor.GetCurrentSize() );
267
268   bool constraintCheck( false );
269   ConstraintAppliedCheck appliedCheck( constraintCheck );
270
271   active.AppliedSignal().Connect( &application, appliedCheck );
272
273   application.SendNotification();
274   application.Render(static_cast<unsigned int>(2000.0f)); // 2 elapsed seconds
275
276   application.SendNotification();
277   appliedCheck.CheckSignalReceived();
278
279   DALI_TEST_CHECK( ActiveConstraint::DEFAULT_WEIGHT == active.GetCurrentWeight() );
280   DALI_TEST_CHECK( TEST_CONSTRAINT_TARGET == actor.GetCurrentSize() );
281
282   // This should be NOOP while constraint is applied
283   actor.SetSize( startSize );
284   application.SendNotification();
285   application.Render(static_cast<unsigned int>(1000.0f));
286   DALI_TEST_CHECK( ActiveConstraint::DEFAULT_WEIGHT == active.GetCurrentWeight() );
287   DALI_TEST_CHECK( TEST_CONSTRAINT_TARGET == actor.GetCurrentSize() );
288
289   // Remove constraint & try again
290   actor.RemoveConstraint( active );
291   actor.SetSize( startSize );
292   application.SendNotification();
293   application.Render(static_cast<unsigned int>(1000.0f));
294   DALI_TEST_CHECK( ActiveConstraint::DEFAULT_WEIGHT == active.GetCurrentWeight() );
295   DALI_TEST_CHECK( startSize == actor.GetCurrentSize() );
296
297   // Try setting the weight after removal
298   active.SetProperty( ActiveConstraint::WEIGHT, 0.5f );
299   application.SendNotification();
300   application.Render(static_cast<unsigned int>(1000.0f));
301   DALI_TEST_CHECK( 0.5f == active.GetCurrentWeight() );
302
303   // Quick check for operator = override on constrainables (needs rhs as a Handle!!) and destructor from heap
304   Constrainable* constrainable = new Constrainable;
305   Constrainable constrainable2;
306   Handle& handle = dynamic_cast< Handle& > ( *constrainable );
307   constrainable2 = handle;
308   delete constrainable;
309 }
310
311 static bool constraintSignalled=false;
312 static void ConstraintCallback( ActiveConstraint& constraint )
313 {
314   constraintSignalled = true;
315 }
316
317 static void UtcDaliConstraintCallback()
318 {
319   TestApplication application;
320   Constraint constraint = Constraint::New<Vector3>( Actor::SIZE, TestConstraintVector3() );
321   constraint.SetApplyTime(2.0f);
322   Actor actor = Actor::New();
323   ActiveConstraint active = actor.ApplyConstraint( constraint );
324   active.AppliedSignal().Connect( ConstraintCallback );
325   application.SendNotification();
326   application.Render(0);
327   application.Render(1000);
328   application.SendNotification();
329   DALI_TEST_CHECK( ! constraintSignalled );
330   application.Render(1016);
331   application.SendNotification();
332
333   DALI_TEST_CHECK( constraintSignalled );
334 }
335
336 void UtcDaliConstraintProperties()
337 {
338   TestApplication application;
339
340   Constraint constraint = Constraint::New<Vector3>( Actor::SIZE, TestConstraintVector3() );
341   Actor actor = Actor::New();
342   ActiveConstraint active = actor.ApplyConstraint( constraint );
343
344   Property::IndexContainer indices;
345   active.GetPropertyIndices( indices );
346   DALI_TEST_CHECK( ! indices.empty() );
347   DALI_TEST_EQUALS( indices.size(), active.GetPropertyCount(), TEST_LOCATION );
348
349   // Valid property
350   DALI_TEST_EQUALS( active.GetPropertyName( 0 ), "weight", TEST_LOCATION );
351   DALI_TEST_EQUALS( active.GetPropertyIndex( "weight" ), 0, TEST_LOCATION );
352   DALI_TEST_CHECK( active.IsPropertyWritable( 0 ) );
353   DALI_TEST_CHECK( active.IsPropertyAnimatable( 0 ) );
354   DALI_TEST_EQUALS( active.GetPropertyType( 0 ), Property::FLOAT, TEST_LOCATION );
355   DALI_TEST_CHECK( active.GetCurrentWeight() != 21312.0f );
356   active.SetProperty( 0, 21312.0f );
357   DALI_TEST_EQUALS( active.GetCurrentWeight(), 21312.0f, TEST_LOCATION );
358   DALI_TEST_EQUALS( active.GetProperty< float >( 0 ), 21312.0f, TEST_LOCATION );
359
360   // Invalid Property
361   try
362   {
363     active.GetPropertyName( PROPERTY_REGISTRATION_START_INDEX );
364     tet_result( TET_FAIL );
365   }
366   catch ( DaliException& e )
367   {
368     DALI_TEST_ASSERT_CONDITION_STARTS_WITH_SUBSTRING( e, "! \"Cannot find property index", TEST_LOCATION );
369   }
370   DALI_TEST_EQUALS( active.GetPropertyIndex( "invalid-property-name"), Property::INVALID_INDEX, TEST_LOCATION );
371   try
372   {
373     active.IsPropertyWritable( PROPERTY_REGISTRATION_START_INDEX );
374     tet_result( TET_FAIL );
375   }
376   catch ( DaliException& e )
377   {
378     DALI_TEST_ASSERT_CONDITION_STARTS_WITH_SUBSTRING( e, "! \"Cannot find property index", TEST_LOCATION );
379   }
380   DALI_TEST_CHECK( ! active.IsPropertyAnimatable( PROPERTY_REGISTRATION_START_INDEX ) );
381   try
382   {
383     active.GetPropertyType( PROPERTY_REGISTRATION_START_INDEX );
384     tet_result( TET_FAIL );
385   }
386   catch ( DaliException& e )
387   {
388     DALI_TEST_ASSERT_CONDITION_STARTS_WITH_SUBSTRING( e, "! \"Cannot find property index", TEST_LOCATION );
389   }
390   try
391   {
392     active.SetProperty( PROPERTY_REGISTRATION_START_INDEX, true );
393     tet_result( TET_FAIL );
394   }
395   catch ( DaliException& e )
396   {
397     DALI_TEST_ASSERT_CONDITION_STARTS_WITH_SUBSTRING( e, "! \"Cannot find property index", TEST_LOCATION );
398   }
399   try
400   {
401     active.GetProperty< bool >( PROPERTY_REGISTRATION_START_INDEX );
402     tet_result( TET_FAIL );
403   }
404   catch ( DaliException& e )
405   {
406     DALI_TEST_ASSERT_CONDITION_STARTS_WITH_SUBSTRING( e, "! \"Cannot find property index", TEST_LOCATION );
407   }
408 }