2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 // http://floralicense.org/license/
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.
22 #include <dali/public-api/dali-core.h>
24 #include <dali-test-suite-utils.h>
28 static void Startup();
29 static void Cleanup();
32 void (*tet_startup)() = Startup;
33 void (*tet_cleanup)() = Cleanup;
36 static void UtcDaliConstraintGetTargetObject();
37 static void UtcDaliConstraintGetTargetProperty();
38 static void UtcDaliConstraintSetWeight();
39 static void UtcDaliConstraintGetCurrentWeight();
40 static void UtcDaliConstraintSignalApplied();
43 POSITIVE_TC_IDX = 0x01,
47 #define MAX_NUMBER_OF_TESTS 10000
49 struct tet_testlist tet_testlist[MAX_NUMBER_OF_TESTS];
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 );
62 // Called only once before first test is run.
67 // Called only once after last test is run
72 static const Vector3 TEST_CONSTRAINT_TARGET = Vector3( 10.0f, 10.0f, 10.0f );
74 struct TestConstraintVector3
76 Vector3 operator()( const Vector3& current )
78 return TEST_CONSTRAINT_TARGET;
82 static void UtcDaliConstraintGetTargetObject()
84 TestApplication application;
86 // Apply a constraint to an actor
88 Constraint constraint = Constraint::New<Vector3>( Actor::SIZE, TestConstraintVector3() );
90 Actor actor = Actor::New();
92 ActiveConstraint active = actor.ApplyConstraint( constraint );
94 // Retrieve the actor back from the active-constraint
96 Handle object = active.GetTargetObject();
98 DALI_TEST_CHECK( object );
100 DALI_TEST_CHECK( object.GetObjectPtr() == actor.GetObjectPtr() );
102 // Throw-away the actor, and check GetTargetObject returns NULL
107 object = active.GetTargetObject();
109 DALI_TEST_CHECK( !object );
112 static void UtcDaliConstraintGetTargetProperty()
114 TestApplication application;
116 // Apply a constraint to an actor
118 Constraint constraint = Constraint::New<Vector3>( Actor::SIZE, TestConstraintVector3() );
120 Actor actor = Actor::New();
122 ActiveConstraint active = actor.ApplyConstraint( constraint );
124 // Check the property index
126 Property::Index index = active.GetTargetProperty();
128 DALI_TEST_CHECK( Actor::SIZE == index );
131 static void UtcDaliConstraintSetWeight()
133 TestApplication application;
135 // Apply a constraint to an actor
137 Constraint constraint = Constraint::New<Vector3>( Actor::SIZE, TestConstraintVector3() );
139 Actor actor = Actor::New();
140 Stage::GetCurrent().Add( actor );
142 ActiveConstraint active = actor.ApplyConstraint( constraint );
144 // Apply the constraint manually
146 active.SetWeight( 0.0f ); // start at zero
148 application.SendNotification();
149 application.Render(static_cast<unsigned int>(1000.0f)); // 1 elapsed second
151 DALI_TEST_EQUALS( actor.GetCurrentSize(), Vector3::ZERO, TEST_LOCATION );
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 );
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 );
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 );
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 );
178 static void UtcDaliConstraintGetCurrentWeight()
180 TestApplication application;
182 // Apply a constraint to an actor
184 Constraint constraint = Constraint::New<Vector3>( Actor::SIZE, TestConstraintVector3() );
186 Actor actor = Actor::New();
188 ActiveConstraint active = actor.ApplyConstraint( constraint );
190 // Check default weight
192 DALI_TEST_CHECK( ActiveConstraint::DEFAULT_WEIGHT == active.GetCurrentWeight() );
195 static void UtcDaliConstraintSignalApplied()
197 TestApplication application;
199 // Apply a constraint to an actor
201 Constraint constraint = Constraint::New<Vector3>( Actor::SIZE, TestConstraintVector3() );
203 float duration( 10.0f );
204 constraint.SetApplyTime( duration );
206 Actor actor = Actor::New();
207 Stage::GetCurrent().Add( actor );
209 ActiveConstraint active = actor.ApplyConstraint( constraint );
211 // Check signal is received after duration
213 bool constraintCheck( false );
214 ConstraintAppliedCheck appliedCheck( constraintCheck );
216 active.AppliedSignal().Connect( &application, appliedCheck );
218 application.SendNotification();
219 application.Render(static_cast<unsigned int>(1000.0f)); // 1 elapsed second
221 // Check signal has not fired
222 application.SendNotification();
223 appliedCheck.CheckSignalNotReceived();
225 application.Render(static_cast<unsigned int>(4000.0f)); // 5 elapsed seconds
227 // Check signal has not fired
228 application.SendNotification();
229 appliedCheck.CheckSignalNotReceived();
231 application.Render(static_cast<unsigned int>(5000.0f - 1.0f)); // <10 elapsed seconds
233 // Check signal has not fired
234 application.SendNotification();
235 appliedCheck.CheckSignalNotReceived();
237 application.Render(static_cast<unsigned int>(2.0f)); // >10 elapsed seconds
239 // Signal should have fired
240 application.SendNotification();
241 appliedCheck.CheckSignalReceived();
244 static void UtcDaliConstraintRemove()
246 TestApplication application;
248 // Apply a constraint to an actor
250 Constraint constraint = Constraint::New<Vector3>( Actor::SIZE, TestConstraintVector3() );
252 float duration( 1.0f );
253 constraint.SetApplyTime( duration );
255 Actor actor = Actor::New();
256 const Vector3 startSize( 1, 2, 3 );
257 actor.SetSize( startSize );
258 Stage::GetCurrent().Add( actor );
260 ActiveConstraint active = actor.ApplyConstraint( constraint );
262 application.SendNotification();
263 application.Render(static_cast<unsigned int>(0.0f)); // 0 elapsed seconds
265 DALI_TEST_CHECK( 0 == active.GetCurrentWeight() );
266 DALI_TEST_CHECK( startSize == actor.GetCurrentSize() );
268 bool constraintCheck( false );
269 ConstraintAppliedCheck appliedCheck( constraintCheck );
271 active.AppliedSignal().Connect( &application, appliedCheck );
273 application.SendNotification();
274 application.Render(static_cast<unsigned int>(2000.0f)); // 2 elapsed seconds
276 application.SendNotification();
277 appliedCheck.CheckSignalReceived();
279 DALI_TEST_CHECK( ActiveConstraint::DEFAULT_WEIGHT == active.GetCurrentWeight() );
280 DALI_TEST_CHECK( TEST_CONSTRAINT_TARGET == actor.GetCurrentSize() );
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() );
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() );
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() );
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;
311 static bool constraintSignalled=false;
312 static void ConstraintCallback( ActiveConstraint& constraint )
314 constraintSignalled = true;
317 static void UtcDaliConstraintCallback()
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();
333 DALI_TEST_CHECK( constraintSignalled );
336 void UtcDaliConstraintProperties()
338 TestApplication application;
340 Constraint constraint = Constraint::New<Vector3>( Actor::SIZE, TestConstraintVector3() );
341 Actor actor = Actor::New();
342 ActiveConstraint active = actor.ApplyConstraint( constraint );
344 Property::IndexContainer indices;
345 active.GetPropertyIndices( indices );
346 DALI_TEST_CHECK( ! indices.empty() );
347 DALI_TEST_EQUALS( indices.size(), active.GetPropertyCount(), TEST_LOCATION );
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 );
363 active.GetPropertyName( PROPERTY_REGISTRATION_START_INDEX );
364 tet_result( TET_FAIL );
366 catch ( DaliException& e )
368 DALI_TEST_ASSERT_CONDITION_STARTS_WITH_SUBSTRING( e, "! \"Cannot find property index", TEST_LOCATION );
370 DALI_TEST_EQUALS( active.GetPropertyIndex( "invalid-property-name"), Property::INVALID_INDEX, TEST_LOCATION );
373 active.IsPropertyWritable( PROPERTY_REGISTRATION_START_INDEX );
374 tet_result( TET_FAIL );
376 catch ( DaliException& e )
378 DALI_TEST_ASSERT_CONDITION_STARTS_WITH_SUBSTRING( e, "! \"Cannot find property index", TEST_LOCATION );
380 DALI_TEST_CHECK( ! active.IsPropertyAnimatable( PROPERTY_REGISTRATION_START_INDEX ) );
383 active.GetPropertyType( PROPERTY_REGISTRATION_START_INDEX );
384 tet_result( TET_FAIL );
386 catch ( DaliException& e )
388 DALI_TEST_ASSERT_CONDITION_STARTS_WITH_SUBSTRING( e, "! \"Cannot find property index", TEST_LOCATION );
392 active.SetProperty( PROPERTY_REGISTRATION_START_INDEX, true );
393 tet_result( TET_FAIL );
395 catch ( DaliException& e )
397 DALI_TEST_ASSERT_CONDITION_STARTS_WITH_SUBSTRING( e, "! \"Cannot find property index", TEST_LOCATION );
401 active.GetProperty< bool >( PROPERTY_REGISTRATION_START_INDEX );
402 tet_result( TET_FAIL );
404 catch ( DaliException& e )
406 DALI_TEST_ASSERT_CONDITION_STARTS_WITH_SUBSTRING( e, "! \"Cannot find property index", TEST_LOCATION );