0c765bd932a829287bf236bfc36d07775dda37ff
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / utc-Dali-ActiveConstraint.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
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
18 #include <iostream>
19 #include <stdlib.h>
20 #include <dali/public-api/dali-core.h>
21 #include <dali-test-suite-utils.h>
22
23 using namespace Dali;
24
25 void utc_dali_active_constraint_startup(void)
26 {
27   test_return_value = TET_UNDEF;
28 }
29
30 void utc_dali_active_constraint_cleanup(void)
31 {
32   test_return_value = TET_PASS;
33 }
34
35 namespace
36 {
37
38 static const Vector3 TEST_CONSTRAINT_TARGET = Vector3( 10.0f, 10.0f, 10.0f );
39
40 struct TestConstraintVector3
41 {
42   Vector3 operator()( const Vector3& current )
43   {
44     return TEST_CONSTRAINT_TARGET;
45   }
46 };
47
48 static bool constraintSignalled=false;
49 static void ConstraintCallback( ActiveConstraint& constraint )
50 {
51   constraintSignalled = true;
52 }
53
54 } // anon namespace
55
56 int UtcDaliConstraintGetTargetObject(void)
57 {
58   TestApplication application;
59
60   // Apply a constraint to an actor
61
62   Constraint constraint = Constraint::New<Vector3>( Actor::Property::SIZE, TestConstraintVector3() );
63
64   Actor actor = Actor::New();
65
66   ActiveConstraint active = actor.ApplyConstraint( constraint );
67
68   // Retrieve the actor back from the active-constraint
69
70   Handle object = active.GetTargetObject();
71
72   DALI_TEST_CHECK( object );
73
74   DALI_TEST_CHECK( object.GetObjectPtr() == actor.GetObjectPtr() );
75
76   // Throw-away the actor, and check GetTargetObject returns NULL
77
78   object.Reset();
79   actor.Reset();
80
81   object = active.GetTargetObject();
82
83   DALI_TEST_CHECK( !object );
84   END_TEST;
85 }
86
87 int UtcDaliConstraintGetTargetProperty(void)
88 {
89   TestApplication application;
90
91   // Apply a constraint to an actor
92
93   Constraint constraint = Constraint::New<Vector3>( Actor::Property::SIZE, TestConstraintVector3() );
94
95   Actor actor = Actor::New();
96
97   ActiveConstraint active = actor.ApplyConstraint( constraint );
98
99   // Check the property index
100
101   Property::Index index = active.GetTargetProperty();
102
103   DALI_TEST_CHECK( Actor::Property::SIZE == index );
104   END_TEST;
105 }
106
107 int UtcDaliConstraintSetWeight(void)
108 {
109   TestApplication application;
110
111   // Apply a constraint to an actor
112
113   Constraint constraint = Constraint::New<Vector3>( Actor::Property::SIZE, TestConstraintVector3() );
114
115   Actor actor = Actor::New();
116   Stage::GetCurrent().Add( actor );
117
118   ActiveConstraint active = actor.ApplyConstraint( constraint );
119
120   // Apply the constraint manually
121
122   active.SetWeight( 0.0f ); // start at zero
123
124   application.SendNotification();
125   application.Render(static_cast<unsigned int>(1000.0f)); // 1 elapsed second
126
127   DALI_TEST_EQUALS( actor.GetCurrentSize(), Vector3::ZERO, TEST_LOCATION );
128
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 );
134
135   weight = 0.5f;
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 );
140
141   weight = 0.75f;
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 );
146
147   weight = 1.0f;
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 );
152   END_TEST;
153 }
154
155 int UtcDaliConstraintGetCurrentWeight(void)
156 {
157   TestApplication application;
158
159   // Apply a constraint to an actor
160
161   Constraint constraint = Constraint::New<Vector3>( Actor::Property::SIZE, TestConstraintVector3() );
162
163   Actor actor = Actor::New();
164
165   ActiveConstraint active = actor.ApplyConstraint( constraint );
166
167   // Check default weight
168
169   DALI_TEST_CHECK( ActiveConstraint::DEFAULT_WEIGHT == active.GetCurrentWeight() );
170   END_TEST;
171 }
172
173 int UtcDaliConstraintSignalApplied(void)
174 {
175   TestApplication application;
176
177   // Apply a constraint to an actor
178
179   Constraint constraint = Constraint::New<Vector3>( Actor::Property::SIZE, TestConstraintVector3() );
180
181   float duration( 10.0f );
182   constraint.SetApplyTime( duration );
183
184   Actor actor = Actor::New();
185   Stage::GetCurrent().Add( actor );
186
187   ActiveConstraint active = actor.ApplyConstraint( constraint );
188
189   // Check signal is received after duration
190
191   bool constraintCheck( false );
192   ConstraintAppliedCheck appliedCheck( constraintCheck );
193
194   active.AppliedSignal().Connect( &application, appliedCheck );
195
196   application.SendNotification();
197   application.Render(static_cast<unsigned int>(1000.0f)); // 1 elapsed second
198
199   // Check signal has not fired
200   application.SendNotification();
201   appliedCheck.CheckSignalNotReceived();
202
203   application.Render(static_cast<unsigned int>(4000.0f)); // 5 elapsed seconds
204
205   // Check signal has not fired
206   application.SendNotification();
207   appliedCheck.CheckSignalNotReceived();
208
209   application.Render(static_cast<unsigned int>(5000.0f - 1.0f)); // <10 elapsed seconds
210
211   // Check signal has not fired
212   application.SendNotification();
213   appliedCheck.CheckSignalNotReceived();
214
215   application.Render(static_cast<unsigned int>(2.0f)); // >10 elapsed seconds
216
217   // Signal should have fired
218   application.SendNotification();
219   appliedCheck.CheckSignalReceived();
220   END_TEST;
221 }
222
223 int UtcDaliConstraintRemove(void)
224 {
225   TestApplication application;
226
227   // Apply a constraint to an actor
228
229   Constraint constraint = Constraint::New<Vector3>( Actor::Property::SIZE, TestConstraintVector3() );
230
231   float duration( 1.0f );
232   constraint.SetApplyTime( duration );
233
234   Actor actor = Actor::New();
235   const Vector3 startSize( 1, 2, 3 );
236   actor.SetSize( startSize );
237   Stage::GetCurrent().Add( actor );
238
239   ActiveConstraint active = actor.ApplyConstraint( constraint );
240
241   application.SendNotification();
242   application.Render(static_cast<unsigned int>(0.0f)); // 0 elapsed seconds
243
244   DALI_TEST_CHECK( 0 == active.GetCurrentWeight() );
245   DALI_TEST_CHECK( startSize == actor.GetCurrentSize() );
246
247   bool constraintCheck( false );
248   ConstraintAppliedCheck appliedCheck( constraintCheck );
249
250   active.AppliedSignal().Connect( &application, appliedCheck );
251
252   application.SendNotification();
253   application.Render(static_cast<unsigned int>(2000.0f)); // 2 elapsed seconds
254
255   application.SendNotification();
256   appliedCheck.CheckSignalReceived();
257
258   DALI_TEST_CHECK( ActiveConstraint::DEFAULT_WEIGHT == active.GetCurrentWeight() );
259   DALI_TEST_CHECK( TEST_CONSTRAINT_TARGET == actor.GetCurrentSize() );
260
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() );
267
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() );
275
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() );
281   END_TEST;
282 }
283
284 int UtcDaliConstraintCallback(void)
285 {
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();
299
300   DALI_TEST_CHECK( constraintSignalled );
301   END_TEST;
302 }
303
304 int UtcDaliConstraintProperties(void)
305 {
306   TestApplication application;
307
308   Constraint constraint = Constraint::New<Vector3>( Actor::Property::SIZE, TestConstraintVector3() );
309   Actor actor = Actor::New();
310   ActiveConstraint active = actor.ApplyConstraint( constraint );
311
312   Property::IndexContainer indices;
313   active.GetPropertyIndices( indices );
314   DALI_TEST_CHECK( ! indices.empty() );
315   DALI_TEST_EQUALS( indices.size(), active.GetPropertyCount(), TEST_LOCATION );
316
317   // Valid property
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 );
327
328   END_TEST;
329 }