Added Test cases for Constraints
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / utc-Dali-ConstraintFunction.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
20 #include <stdlib.h>
21 #include <dali/public-api/dali-core.h>
22 #include <dali-test-suite-utils.h>
23
24 using namespace Dali;
25
26 ///////////////////////////////////////////////////////////////////////////////
27 void utc_dali_constraint_function_startup(void)
28 {
29   test_return_value = TET_UNDEF;
30 }
31
32 void utc_dali_constraint_function_cleanup(void)
33 {
34   test_return_value = TET_PASS;
35 }
36 ///////////////////////////////////////////////////////////////////////////////
37
38 ///////////////////////////////////////////////////////////////////////////////
39 namespace
40 {
41 bool gFunctionCalled = false;
42
43 template< typename T >
44 void TestCallbackFunction( T& /* current*/ , const PropertyInputContainer& /* inputs */ )
45 {
46   gFunctionCalled = true;
47 }
48
49 template< typename T >
50 struct TestCallbackFunctor
51 {
52   TestCallbackFunctor( bool& functorCalled ) : mFunctorCalled( functorCalled ) { }
53
54   void operator()( T& /* current*/ , const PropertyInputContainer& /* inputs */ )
55   {
56     mFunctorCalled = true;
57   }
58
59   bool& mFunctorCalled;
60 };
61
62 template< typename T >
63 struct TestFunctorMethod
64 {
65   TestFunctorMethod( bool& functorCalled ) : mFunctorCalled( functorCalled ) { }
66
67   void Method( T& /* current*/ , const PropertyInputContainer& /* inputs */ )
68   {
69     mFunctorCalled = true;
70   }
71
72   bool& mFunctorCalled;
73 };
74
75 } // unnamed namespace
76 ///////////////////////////////////////////////////////////////////////////////
77
78 ///////////////////////////////////////////////////////////////////////////////
79 // Constraint::Function::Function( void( *function )( P&, const PropertyInputContainer& ) )
80 ///////////////////////////////////////////////////////////////////////////////
81 namespace
82 {
83 template< typename T >
84 void TestFunctionConstructor()
85 {
86   gFunctionCalled = false;
87   Constraint::Function< T > function( &TestCallbackFunction< T > );
88   T current;
89   PropertyInputContainer inputs;
90
91   DALI_TEST_EQUALS( gFunctionCalled, false, TEST_LOCATION );
92   CallbackBase::Execute< T&, const PropertyInputContainer& >( function, current, inputs );
93   DALI_TEST_EQUALS( gFunctionCalled, true, TEST_LOCATION );
94 }
95 } // unnamed namespace
96
97 int UtcDaliConstraintFunctionWithFunction(void)
98 {
99   TestFunctionConstructor< bool >();
100   TestFunctionConstructor< int >();
101   TestFunctionConstructor< unsigned int >();
102   TestFunctionConstructor< float >();
103   TestFunctionConstructor< Vector2 >();
104   TestFunctionConstructor< Vector3 >();
105   TestFunctionConstructor< Vector4 >();
106   TestFunctionConstructor< Quaternion >();
107   TestFunctionConstructor< Matrix >();
108   TestFunctionConstructor< Matrix3 >();
109   END_TEST;
110 }
111 ///////////////////////////////////////////////////////////////////////////////
112
113 ///////////////////////////////////////////////////////////////////////////////
114 // Constraint::Function::Function( const T& object )
115 ///////////////////////////////////////////////////////////////////////////////
116 namespace
117 {
118 template< typename T >
119 void TestFunctorConstructor()
120 {
121   bool called = false;
122   TestCallbackFunctor< T > functor( called );
123   Constraint::Function< T > callback( functor );
124   T current;
125   PropertyInputContainer inputs;
126
127   DALI_TEST_EQUALS( called, false, TEST_LOCATION );
128   CallbackBase::Execute< T&, const PropertyInputContainer& >( callback, current, inputs );
129   DALI_TEST_EQUALS( called, true, TEST_LOCATION );
130 }
131 } // unnamed namespace
132
133 int UtcDaliConstraintFunctionWithFunctor(void)
134 {
135   TestFunctorConstructor< bool >();
136   TestFunctorConstructor< int >();
137   TestFunctorConstructor< unsigned int >();
138   TestFunctorConstructor< float >();
139   TestFunctorConstructor< Vector2 >();
140   TestFunctorConstructor< Vector3 >();
141   TestFunctorConstructor< Vector4 >();
142   TestFunctorConstructor< Quaternion >();
143   TestFunctorConstructor< Matrix >();
144   TestFunctorConstructor< Matrix3 >();
145   END_TEST;
146 }
147 ///////////////////////////////////////////////////////////////////////////////
148
149 ///////////////////////////////////////////////////////////////////////////////
150 // Constraint::Function::Function( const T& object, void ( T::*memberFunction ) ( P&, const PropertyInputContainer& ) )
151 ///////////////////////////////////////////////////////////////////////////////
152 namespace
153 {
154 template< typename T >
155 void TestFunctorMethodConstructor()
156 {
157   bool called = false;
158   TestFunctorMethod< T > functor( called );
159   Constraint::Function< T > callback( functor, &TestFunctorMethod< T >::Method );
160   T current;
161   PropertyInputContainer inputs;
162
163   DALI_TEST_EQUALS( called, false, TEST_LOCATION );
164   CallbackBase::Execute< T&, const PropertyInputContainer& >( callback, current, inputs );
165   DALI_TEST_EQUALS( called, true, TEST_LOCATION );
166 }
167 } // unnamed namespace
168
169 int UtcDaliConstraintFunctionWithMethodFunctor(void)
170 {
171   TestFunctorMethodConstructor< bool >();
172   TestFunctorMethodConstructor< int >();
173   TestFunctorMethodConstructor< unsigned int >();
174   TestFunctorMethodConstructor< float >();
175   TestFunctorMethodConstructor< Vector2 >();
176   TestFunctorMethodConstructor< Vector3 >();
177   TestFunctorMethodConstructor< Vector4 >();
178   TestFunctorMethodConstructor< Quaternion >();
179   TestFunctorMethodConstructor< Matrix >();
180   TestFunctorMethodConstructor< Matrix3 >();
181   END_TEST;
182 }
183 ///////////////////////////////////////////////////////////////////////////////
184
185 ///////////////////////////////////////////////////////////////////////////////
186 // Constraint::Function::Clone
187 ///////////////////////////////////////////////////////////////////////////////
188 namespace
189 {
190 template< typename T >
191 void TestFunctionClone()
192 {
193   gFunctionCalled = false;
194   Constraint::Function< T > callback( &TestCallbackFunction< T > );
195   CallbackBase* clone = callback.Clone();
196
197   T current;
198   PropertyInputContainer inputs;
199
200   DALI_TEST_EQUALS( gFunctionCalled, false, TEST_LOCATION );
201   CallbackBase::Execute< T&, const PropertyInputContainer& >( *clone, current, inputs );
202   DALI_TEST_EQUALS( gFunctionCalled, true, TEST_LOCATION );
203   delete clone;
204 }
205
206 template< typename T >
207 void TestFunctorClone()
208 {
209   bool called = false;
210   TestCallbackFunctor< T > functor( called );
211   Constraint::Function< T > callback( functor );
212   CallbackBase* clone = callback.Clone();
213
214   T current;
215   PropertyInputContainer inputs;
216
217   DALI_TEST_EQUALS( called, false, TEST_LOCATION );
218   CallbackBase::Execute< T&, const PropertyInputContainer& >( *clone, current, inputs );
219   DALI_TEST_EQUALS( called, true, TEST_LOCATION );
220   delete clone;
221 }
222
223 template< typename T >
224 void TestMethodFunctorClone()
225 {
226   bool called = false;
227   TestFunctorMethod< T > functor( called );
228   Constraint::Function< T > callback( functor, &TestFunctorMethod< T >::Method );
229   CallbackBase* clone = callback.Clone();
230
231   T current;
232   PropertyInputContainer inputs;
233
234   DALI_TEST_EQUALS( called, false, TEST_LOCATION );
235   CallbackBase::Execute< T&, const PropertyInputContainer& >( *clone, current, inputs );
236   DALI_TEST_EQUALS( called, true, TEST_LOCATION );
237   delete clone;
238 }
239
240 } // unnamed namespace
241
242 int UtcDaliConstraintFunctionFunctionClone(void)
243 {
244   TestFunctionClone< bool >();
245   TestFunctionClone< int >();
246   TestFunctionClone< unsigned int >();
247   TestFunctionClone< float >();
248   TestFunctionClone< Vector2 >();
249   TestFunctionClone< Vector3 >();
250   TestFunctionClone< Vector4 >();
251   TestFunctionClone< Quaternion >();
252   TestFunctionClone< Matrix >();
253   TestFunctionClone< Matrix3 >();
254   END_TEST;
255 }
256
257 int UtcDaliConstraintFunctionFunctorClone(void)
258 {
259   TestFunctorClone< bool >();
260   TestFunctorClone< int >();
261   TestFunctorClone< unsigned int >();
262   TestFunctorClone< float >();
263   TestFunctorClone< Vector2 >();
264   TestFunctorClone< Vector3 >();
265   TestFunctorClone< Vector4 >();
266   TestFunctorClone< Quaternion >();
267   TestFunctorClone< Matrix >();
268   TestFunctorClone< Matrix3 >();
269   END_TEST;
270 }
271
272 int UtcDaliConstraintFunctionMethodFunctorClone(void)
273 {
274   TestMethodFunctorClone< bool >();
275   TestMethodFunctorClone< int >();
276   TestMethodFunctorClone< unsigned int >();
277   TestMethodFunctorClone< float >();
278   TestMethodFunctorClone< Vector2 >();
279   TestMethodFunctorClone< Vector3 >();
280   TestMethodFunctorClone< Vector4 >();
281   TestMethodFunctorClone< Quaternion >();
282   TestMethodFunctorClone< Matrix >();
283   TestMethodFunctorClone< Matrix3 >();
284   END_TEST;
285 }
286 ///////////////////////////////////////////////////////////////////////////////
287
288 ///////////////////////////////////////////////////////////////////////////////
289 namespace
290 {
291 struct CountFunctor
292 {
293   CountFunctor( int& count )
294   : mCount( count )
295   {
296     ++mCount;
297   }
298
299   CountFunctor( const CountFunctor& other )
300   : mCount( other.mCount )
301   {
302     ++mCount;
303   }
304
305   CountFunctor& operator=( const CountFunctor& other )
306   {
307     return *this;
308   }
309
310   ~CountFunctor()
311   {
312     --mCount;
313   }
314
315   void operator()( bool& /* current*/ , const PropertyInputContainer& /* inputs */ )
316   {
317   }
318
319   int& mCount;
320 };
321 } // unnamed namespace
322
323 int UtcDaliConstraintFunctionEnsureMemoryCleanup(void)
324 {
325   // Functors are new'd in Constraint::Function, so check that all memory is released at the end
326
327   int count = 0;
328
329   {
330     CountFunctor functor( count );
331     Constraint::Function< bool > callback1( functor );
332     Constraint::Function< bool > callback2( functor );
333     Constraint::Function< bool > callback3( functor );
334     Constraint::Function< bool > callback4( functor );
335     Constraint::Function< bool > callback5( functor );
336     Constraint::Function< bool > callback6( functor );
337     Constraint::Function< bool > callback7( functor );
338     Constraint::Function< bool > callback8( functor );
339     Constraint::Function< bool > callback9( functor );
340     DALI_TEST_EQUALS( count, 10, TEST_LOCATION );
341   }
342
343   DALI_TEST_EQUALS( count, 0, TEST_LOCATION );
344
345   END_TEST;
346 }
347 ///////////////////////////////////////////////////////////////////////////////