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