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.
20 #include <dali/public-api/dali-core.h>
21 #include <dali-test-suite-utils.h>
25 void utc_dali_active_constraint_startup(void)
27 test_return_value = TET_UNDEF;
30 void utc_dali_active_constraint_cleanup(void)
32 test_return_value = TET_PASS;
38 static const Vector3 TEST_CONSTRAINT_TARGET = Vector3( 10.0f, 10.0f, 10.0f );
40 struct TestConstraintVector3
42 Vector3 operator()( const Vector3& current )
44 return TEST_CONSTRAINT_TARGET;
48 static bool constraintSignalled=false;
49 static void ConstraintCallback( ActiveConstraint& constraint )
51 constraintSignalled = true;
56 int UtcDaliConstraintGetTargetObject(void)
58 TestApplication application;
60 // Apply a constraint to an actor
62 Constraint constraint = Constraint::New<Vector3>( Actor::Property::SIZE, TestConstraintVector3() );
64 Actor actor = Actor::New();
66 ActiveConstraint active = actor.ApplyConstraint( constraint );
68 // Retrieve the actor back from the active-constraint
70 Handle object = active.GetTargetObject();
72 DALI_TEST_CHECK( object );
74 DALI_TEST_CHECK( object.GetObjectPtr() == actor.GetObjectPtr() );
76 // Throw-away the actor, and check GetTargetObject returns NULL
81 object = active.GetTargetObject();
83 DALI_TEST_CHECK( !object );
87 int UtcDaliConstraintGetTargetProperty(void)
89 TestApplication application;
91 // Apply a constraint to an actor
93 Constraint constraint = Constraint::New<Vector3>( Actor::Property::SIZE, TestConstraintVector3() );
95 Actor actor = Actor::New();
97 ActiveConstraint active = actor.ApplyConstraint( constraint );
99 // Check the property index
101 Property::Index index = active.GetTargetProperty();
103 DALI_TEST_CHECK( Actor::Property::SIZE == index );
107 int UtcDaliConstraintSetWeight(void)
109 TestApplication application;
111 // Apply a constraint to an actor
113 Constraint constraint = Constraint::New<Vector3>( Actor::Property::SIZE, TestConstraintVector3() );
115 Actor actor = Actor::New();
116 Stage::GetCurrent().Add( actor );
118 ActiveConstraint active = actor.ApplyConstraint( constraint );
120 // Apply the constraint manually
122 active.SetWeight( 0.0f ); // start at zero
124 application.SendNotification();
125 application.Render(static_cast<unsigned int>(1000.0f)); // 1 elapsed second
127 DALI_TEST_EQUALS( actor.GetCurrentSize(), Vector3::ZERO, TEST_LOCATION );
129 float weight( 0.25f );
130 active.SetWeight( weight );
131 application.SendNotification();
132 application.Render(static_cast<unsigned int>(1000.0f)); // 1 elapsed second
133 DALI_TEST_EQUALS( actor.GetCurrentSize(), TEST_CONSTRAINT_TARGET * weight, TEST_LOCATION );
136 active.SetWeight( weight );
137 application.SendNotification();
138 application.Render(static_cast<unsigned int>(1000.0f)); // 2 elapsed seconds
139 DALI_TEST_EQUALS( actor.GetCurrentSize(), TEST_CONSTRAINT_TARGET * weight, TEST_LOCATION );
142 active.SetWeight( weight );
143 application.SendNotification();
144 application.Render(static_cast<unsigned int>(1000.0f)); // 2 elapsed seconds
145 DALI_TEST_EQUALS( actor.GetCurrentSize(), TEST_CONSTRAINT_TARGET * weight, TEST_LOCATION );
148 active.SetWeight( weight );
149 application.SendNotification();
150 application.Render(static_cast<unsigned int>(1000.0f)); // 2 elapsed seconds
151 DALI_TEST_EQUALS( actor.GetCurrentSize(), TEST_CONSTRAINT_TARGET, TEST_LOCATION );
155 int UtcDaliConstraintGetCurrentWeight(void)
157 TestApplication application;
159 // Apply a constraint to an actor
161 Constraint constraint = Constraint::New<Vector3>( Actor::Property::SIZE, TestConstraintVector3() );
163 Actor actor = Actor::New();
165 ActiveConstraint active = actor.ApplyConstraint( constraint );
167 // Check default weight
169 DALI_TEST_CHECK( ActiveConstraint::DEFAULT_WEIGHT == active.GetCurrentWeight() );
173 int UtcDaliConstraintSignalApplied(void)
175 TestApplication application;
177 // Apply a constraint to an actor
179 Constraint constraint = Constraint::New<Vector3>( Actor::Property::SIZE, TestConstraintVector3() );
181 float duration( 10.0f );
182 constraint.SetApplyTime( duration );
184 Actor actor = Actor::New();
185 Stage::GetCurrent().Add( actor );
187 ActiveConstraint active = actor.ApplyConstraint( constraint );
189 // Check signal is received after duration
191 bool constraintCheck( false );
192 ConstraintAppliedCheck appliedCheck( constraintCheck );
194 active.AppliedSignal().Connect( &application, appliedCheck );
196 application.SendNotification();
197 application.Render(static_cast<unsigned int>(1000.0f)); // 1 elapsed second
199 // Check signal has not fired
200 application.SendNotification();
201 appliedCheck.CheckSignalNotReceived();
203 application.Render(static_cast<unsigned int>(4000.0f)); // 5 elapsed seconds
205 // Check signal has not fired
206 application.SendNotification();
207 appliedCheck.CheckSignalNotReceived();
209 application.Render(static_cast<unsigned int>(5000.0f - 1.0f)); // <10 elapsed seconds
211 // Check signal has not fired
212 application.SendNotification();
213 appliedCheck.CheckSignalNotReceived();
215 application.Render(static_cast<unsigned int>(2.0f)); // >10 elapsed seconds
217 // Signal should have fired
218 application.SendNotification();
219 appliedCheck.CheckSignalReceived();
223 int UtcDaliConstraintRemove(void)
225 TestApplication application;
227 // Apply a constraint to an actor
229 Constraint constraint = Constraint::New<Vector3>( Actor::Property::SIZE, TestConstraintVector3() );
231 float duration( 1.0f );
232 constraint.SetApplyTime( duration );
234 Actor actor = Actor::New();
235 const Vector3 startSize( 1, 2, 3 );
236 actor.SetSize( startSize );
237 Stage::GetCurrent().Add( actor );
239 ActiveConstraint active = actor.ApplyConstraint( constraint );
241 application.SendNotification();
242 application.Render(static_cast<unsigned int>(0.0f)); // 0 elapsed seconds
244 DALI_TEST_CHECK( 0 == active.GetCurrentWeight() );
245 DALI_TEST_CHECK( startSize == actor.GetCurrentSize() );
247 bool constraintCheck( false );
248 ConstraintAppliedCheck appliedCheck( constraintCheck );
250 active.AppliedSignal().Connect( &application, appliedCheck );
252 application.SendNotification();
253 application.Render(static_cast<unsigned int>(2000.0f)); // 2 elapsed seconds
255 application.SendNotification();
256 appliedCheck.CheckSignalReceived();
258 DALI_TEST_CHECK( ActiveConstraint::DEFAULT_WEIGHT == active.GetCurrentWeight() );
259 DALI_TEST_CHECK( TEST_CONSTRAINT_TARGET == actor.GetCurrentSize() );
261 // This should be NOOP while constraint is applied
262 actor.SetSize( startSize );
263 application.SendNotification();
264 application.Render(static_cast<unsigned int>(1000.0f));
265 DALI_TEST_CHECK( ActiveConstraint::DEFAULT_WEIGHT == active.GetCurrentWeight() );
266 DALI_TEST_CHECK( TEST_CONSTRAINT_TARGET == actor.GetCurrentSize() );
268 // Remove constraint & try again
269 actor.RemoveConstraint( active );
270 actor.SetSize( startSize );
271 application.SendNotification();
272 application.Render(static_cast<unsigned int>(1000.0f));
273 DALI_TEST_CHECK( ActiveConstraint::DEFAULT_WEIGHT == active.GetCurrentWeight() );
274 DALI_TEST_CHECK( startSize == actor.GetCurrentSize() );
276 // Try setting the weight after removal
277 active.SetProperty( ActiveConstraint::Property::WEIGHT, 0.5f );
278 application.SendNotification();
279 application.Render(static_cast<unsigned int>(1000.0f));
280 DALI_TEST_CHECK( 0.5f == active.GetCurrentWeight() );
284 int UtcDaliConstraintCallback(void)
286 TestApplication application;
287 Constraint constraint = Constraint::New<Vector3>( Actor::Property::SIZE, TestConstraintVector3() );
288 constraint.SetApplyTime(2.0f);
289 Actor actor = Actor::New();
290 ActiveConstraint active = actor.ApplyConstraint( constraint );
291 active.AppliedSignal().Connect( ConstraintCallback );
292 application.SendNotification();
293 application.Render(0);
294 application.Render(1000);
295 application.SendNotification();
296 DALI_TEST_CHECK( ! constraintSignalled );
297 application.Render(1016);
298 application.SendNotification();
300 DALI_TEST_CHECK( constraintSignalled );
304 int UtcDaliConstraintProperties(void)
306 TestApplication application;
308 Constraint constraint = Constraint::New<Vector3>( Actor::Property::SIZE, TestConstraintVector3() );
309 Actor actor = Actor::New();
310 ActiveConstraint active = actor.ApplyConstraint( constraint );
312 Property::IndexContainer indices;
313 active.GetPropertyIndices( indices );
314 DALI_TEST_CHECK( ! indices.empty() );
315 DALI_TEST_EQUALS( indices.size(), active.GetPropertyCount(), TEST_LOCATION );
318 DALI_TEST_EQUALS( active.GetPropertyName( 0 ), "weight", TEST_LOCATION );
319 DALI_TEST_EQUALS( active.GetPropertyIndex( "weight" ), 0, TEST_LOCATION );
320 DALI_TEST_CHECK( active.IsPropertyWritable( 0 ) );
321 DALI_TEST_CHECK( active.IsPropertyAnimatable( 0 ) );
322 DALI_TEST_EQUALS( active.GetPropertyType( 0 ), Property::FLOAT, TEST_LOCATION );
323 DALI_TEST_CHECK( active.GetCurrentWeight() != 21312.0f );
324 active.SetProperty( 0, 21312.0f );
325 DALI_TEST_EQUALS( active.GetCurrentWeight(), 21312.0f, TEST_LOCATION );
326 DALI_TEST_EQUALS( active.GetProperty< float >( 0 ), 21312.0f, TEST_LOCATION );