RenderTasks would not finish if source actor was invisible.
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / dali-test-suite-utils / dali-test-suite-utils.h
1 #ifndef __DALI_TEST_SUITE_UTILS_H__
2 #define __DALI_TEST_SUITE_UTILS_H__
3
4 /*
5  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // INTERNAL INCLUDES
22 #include <dali/public-api/dali-core.h>
23 #include <stdarg.h>
24
25 void tet_infoline(const char*str);
26 void tet_printf(const char *format, ...);
27
28 #include "test-application.h"
29
30 using namespace Dali;
31
32 #define STRINGIZE_I(text) #text
33 #define STRINGIZE(text) STRINGIZE_I(text)
34
35 // the following is the other compilers way of token pasting, gcc seems to just concatenate strings automatically
36 //#define TOKENPASTE(x,y) x ## y
37 #define TOKENPASTE(x,y) x y
38 #define TOKENPASTE2(x,y) TOKENPASTE( x, y )
39 #define TEST_LOCATION TOKENPASTE2( "Test failed in ", TOKENPASTE2( __FILE__, TOKENPASTE2( ", line ", STRINGIZE(__LINE__) ) ) )
40
41 #define TET_UNDEF 2
42 #define TET_FAIL 1
43 #define TET_PASS 0
44
45 extern int test_return_value;
46
47 void tet_result(int value);
48
49 #define END_TEST \
50   return ((test_return_value>0)?1:0)
51
52 void tet_infoline(const char* str);
53 void tet_printf(const char *format, ...);
54
55 /**
56  * DALI_TEST_CHECK is a wrapper for tet_result.
57  * If the condition evaluates to false, the test is stopped.
58  * @param[in] The boolean expression to check
59  */
60 #define DALI_TEST_CHECK(condition)                                                        \
61 if ( (condition) )                                                                        \
62 {                                                                                         \
63   tet_result(TET_PASS);                                                                   \
64 }                                                                                         \
65 else                                                                                      \
66 {                                                                                         \
67   fprintf(stderr, "%s Failed in %s at line %d\n", __PRETTY_FUNCTION__, __FILE__, __LINE__);    \
68   tet_result(TET_FAIL);                                                                   \
69   throw("TET_FAIL");                                                                      \
70 }
71
72 template <typename Type>
73 inline bool CompareType(Type value1, Type value2, float epsilon);
74
75 /**
76  * A helper for fuzzy-comparing Vector2 objects
77  * @param[in] vector1 the first object
78  * @param[in] vector2 the second object
79  * @param[in] epsilon difference threshold
80  * @returns true if difference is smaller than epsilon threshold, false otherwise
81  */
82 template <>
83 inline bool CompareType<float>(float value1, float value2, float epsilon)
84 {
85   return fabsf(value1 - value2) < epsilon;
86 }
87
88 /**
89  * A helper for fuzzy-comparing Vector2 objects
90  * @param[in] vector1 the first object
91  * @param[in] vector2 the second object
92  * @param[in] epsilon difference threshold
93  * @returns true if difference is smaller than epsilon threshold, false otherwise
94  */
95 template <>
96 inline bool CompareType<Vector2>(Vector2 vector1, Vector2 vector2, float epsilon)
97 {
98   return fabsf(vector1.x - vector2.x)<epsilon && fabsf(vector1.y - vector2.y)<epsilon;
99 }
100
101 /**
102  * A helper for fuzzy-comparing Vector3 objects
103  * @param[in] vector1 the first object
104  * @param[in] vector2 the second object
105  * @param[in] epsilon difference threshold
106  * @returns true if difference is smaller than epsilon threshold, false otherwise
107  */
108 template <>
109 inline bool CompareType<Vector3>(Vector3 vector1, Vector3 vector2, float epsilon)
110 {
111   return fabsf(vector1.x - vector2.x)<epsilon &&
112          fabsf(vector1.y - vector2.y)<epsilon &&
113          fabsf(vector1.z - vector2.z)<epsilon;
114 }
115
116
117 /**
118  * A helper for fuzzy-comparing Vector4 objects
119  * @param[in] vector1 the first object
120  * @param[in] vector2 the second object
121  * @param[in] epsilon difference threshold
122  * @returns true if difference is smaller than epsilon threshold, false otherwise
123  */
124 template <>
125 inline bool CompareType<Vector4>(Vector4 vector1, Vector4 vector2, float epsilon)
126 {
127   return fabsf(vector1.x - vector2.x)<epsilon &&
128          fabsf(vector1.y - vector2.y)<epsilon &&
129          fabsf(vector1.z - vector2.z)<epsilon &&
130          fabsf(vector1.w - vector2.w)<epsilon;
131 }
132
133 template <>
134 inline bool CompareType<Quaternion>(Quaternion q1, Quaternion q2, float epsilon)
135 {
136   Quaternion q2N = -q2; // These quaternions represent the same rotation
137   return CompareType<Vector4>(q1.mVector, q2.mVector, epsilon) || CompareType<Vector4>(q1.mVector, q2N.mVector, epsilon);
138 }
139
140 template <>
141 inline bool CompareType<Radian>(Radian q1, Radian q2, float epsilon)
142 {
143   return CompareType<float>(float(q1), float(q2), epsilon);
144 }
145
146 template <>
147 inline bool CompareType<Degree>(Degree q1, Degree q2, float epsilon)
148 {
149   return CompareType<float>(float(q1), float(q2), epsilon);
150 }
151
152 bool operator==(TimePeriod a, TimePeriod b);
153 std::ostream& operator<< (std::ostream& o, const TimePeriod value);
154
155 /**
156  * Test whether two values are equal.
157  * @param[in] value1 The first value
158  * @param[in] value2 The second value
159  * @param[in] location The TEST_LOCATION macro should be used here
160  */
161 template<typename TypeA, typename TypeB>
162 inline void DALI_TEST_EQUALS(TypeA value1, TypeB value2, const char* location)
163 {
164   if (!(value1 == value2))
165   {
166     std::ostringstream o;
167     o << value1 << " == " << value2 << std::endl;
168     fprintf(stderr, "%s, checking %s", location, o.str().c_str());
169     tet_result(TET_FAIL);
170   }
171   else
172   {
173     tet_result(TET_PASS);
174   }
175 }
176
177 template<typename Type>
178 inline void DALI_TEST_EQUALS(Type value1, Type value2, float epsilon, const char* location)
179 {
180   if( !CompareType<Type>(value1, value2, epsilon) )
181   {
182     std::ostringstream o;
183     o << value1 << " == " << value2 << std::endl;
184     fprintf(stderr, "%s, checking %s", location, o.str().c_str());
185     tet_result(TET_FAIL);
186   }
187   else
188   {
189     tet_result(TET_PASS);
190   }
191 }
192
193 /**
194  * Test whether two TimePeriods are within a certain distance of each other.
195  * @param[in] value1 The first value
196  * @param[in] value2 The second value
197  * @param[in] epsilon The values must be within this distance of each other
198  * @param[in] location The TEST_LOCATION macro should be used here
199  */
200 template<>
201 inline void DALI_TEST_EQUALS<TimePeriod>( TimePeriod value1, TimePeriod value2, float epsilon, const char* location)
202 {
203   if ((fabs(value1.durationSeconds - value2.durationSeconds) > epsilon))
204   {
205     fprintf(stderr, "%s, checking durations %f == %f, epsilon %f\n", location, value1.durationSeconds, value2.durationSeconds, epsilon);
206     tet_result(TET_FAIL);
207   }
208   else if ((fabs(value1.delaySeconds - value2.delaySeconds) > epsilon))
209   {
210     fprintf(stderr, "%s, checking delays %f == %f, epsilon %f\n", location, value1.delaySeconds, value2.delaySeconds, epsilon);
211     tet_result(TET_FAIL);
212   }
213   else
214   {
215     tet_result(TET_PASS);
216   }
217 }
218
219 /**
220  * Test whether two Matrix3 objects are equal.
221  * @param[in] matrix1 The first object
222  * @param[in] matrix2 The second object
223  * @param[in] location The TEST_LOCATION macro should be used here
224  */
225 void DALI_TEST_EQUALS( const Matrix3& matrix1, const Matrix3& matrix2, const char* location);
226
227 /** Test whether two Matrix3 objects are equal (fuzzy compare).
228  * @param[in] matrix1 The first object
229  * @param[in] matrix2 The second object
230  * @param[in] epsilon The epsilon to use for comparison
231  * @param[in] location The TEST_LOCATION macro should be used here
232  */
233 void DALI_TEST_EQUALS( const Matrix3& matrix1, const Matrix3& matrix2, float epsilon, const char* location);
234
235 /**
236  * Test whether two Matrix objects are equal.
237  * @param[in] matrix1 The first object
238  * @param[in] matrix2 The second object
239  * @param[in] location The TEST_LOCATION macro should be used here
240  */
241 void DALI_TEST_EQUALS( const Matrix& matrix1, const Matrix& matrix2, const char* location);
242
243 /**
244  * Test whether two Matrix objects are equal (fuzzy-compare).
245  * @param[in] matrix1 The first object
246  * @param[in] matrix2 The second object
247  * @param[in] location The TEST_LOCATION macro should be used here
248  */
249 void DALI_TEST_EQUALS( const Matrix& matrix1, const Matrix& matrix2, float epsilon, const char* location);
250
251 /**
252  * Test whether two strings are equal.
253  * @param[in] str1 The first string
254  * @param[in] str2 The second string
255  * @param[in] location The TEST_LOCATION macro should be used here
256  */
257 template<>
258 inline void DALI_TEST_EQUALS<const char*>( const char* str1, const char* str2, const char* location)
259 {
260   if (strcmp(str1, str2))
261   {
262     fprintf(stderr, "%s, checking '%s' == '%s'\n", location, str1, str2);
263     tet_result(TET_FAIL);
264   }
265   else
266   {
267     tet_result(TET_PASS);
268   }
269 }
270
271 /**
272  * Test whether two strings are equal.
273  * @param[in] str1 The first string
274  * @param[in] str2 The second string
275  * @param[in] location The TEST_LOCATION macro should be used here
276  */
277 template<>
278 inline void DALI_TEST_EQUALS<const std::string&>( const std::string &str1, const std::string &str2, const char* location)
279 {
280   DALI_TEST_EQUALS(str1.c_str(), str2.c_str(), location);
281 }
282
283 /**
284  * Test whether two strings are equal.
285  * @param[in] str1 The first string
286  * @param[in] str2 The second string
287  * @param[in] location The TEST_LOCATION macro should be used here
288  */
289 void DALI_TEST_EQUALS( const std::string &str1, const char* str2, const char* location);
290
291 /**
292  * Test whether two strings are equal.
293  * @param[in] str1 The first string
294  * @param[in] str2 The second string
295  * @param[in] location The TEST_LOCATION macro should be used here
296  */
297 void DALI_TEST_EQUALS( const char* str1, const std::string &str2, const char* location);
298
299 /**
300  * Test whether two UTF32 strings are equal.
301  * @param[in] str1 The first string
302  * @param[in] str2 The second string
303  * @param[in] location The TEST_LOCATION macro should be used here
304  */
305 template<>
306 inline void DALI_TEST_EQUALS<const Integration::TextArray&>( const Integration::TextArray& str1, const Integration::TextArray& str2, const char* location)
307 {
308   if( !std::equal( str1.Begin(), str1.End(), str2.Begin() ) )
309   {
310     fprintf(stderr, "%s, checking '", location);
311
312     for( unsigned int i = 0; i < str1.Count(); ++i )
313     {
314       fprintf(stderr, "%c", str1[i]);
315     }
316
317     fprintf(stderr, "' == '");
318
319     for( unsigned int i = 0; i < str2.Count(); ++i )
320     {
321       fprintf(stderr, "%c", str2[i]);
322     }
323
324     fprintf(stderr, "'\n");
325
326     tet_result(TET_FAIL);
327   }
328   else
329   {
330     tet_result(TET_PASS);
331   }
332 }
333
334 /**
335  * Test whether one unsigned integer value is greater than another.
336  * Test succeeds if value1 > value2
337  * @param[in] value1 The first value
338  * @param[in] value2 The second value
339  * @param[in] location The TEST_LOCATION macro should be used here
340  */
341 void DALI_TEST_GREATER(unsigned int value1, unsigned int value2, const char* location);
342
343 /**
344  * Test whether one float value is greater than another.
345  * Test succeeds if value1 > value2
346  * @param[in] value1 The first value
347  * @param[in] value2 The second value
348  * @param[in] location The TEST_LOCATION macro should be used here
349  */
350 void DALI_TEST_GREATER( float value1, float value2, const char* location);
351
352 /**
353  * Test whether the assertion condition that failed and thus triggered the
354  * exception \b e contained a given substring.
355  * @param[in] e The exception that we expect was fired by a runtime assertion failure.
356  * @param[in] conditionSubString The text that we expect to be present in an
357  *                               assertion which triggered the exception.
358  * @param[in] location The TEST_LOCATION macro should be used here.
359  */
360 void DALI_TEST_ASSERT( DaliException& e, std::string conditionSubString, const char* location );
361
362 /**
363  * Print the assert
364  * @param[in] e The exception that we expect was fired by a runtime assertion failure.
365  */
366 inline void DALI_TEST_PRINT_ASSERT( DaliException& e )
367 {
368   tet_printf("Assertion %s failed at %s\n", e.condition, e.location );
369 }
370
371 // Functor to test whether an Applied signal is emitted
372 struct ConstraintAppliedCheck
373 {
374   ConstraintAppliedCheck( bool& signalReceived );
375   void operator()( ActiveConstraint& constraint );
376   void Reset();
377   void CheckSignalReceived();
378   void CheckSignalNotReceived();
379   bool& mSignalReceived; // owned by individual tests
380 };
381
382 /**
383  * A Helper to test default functions
384  */
385 template <typename T>
386 struct DefaultFunctionCoverage
387 {
388   DefaultFunctionCoverage()
389   {
390     T a;
391     T *b = new T(a);
392     DALI_TEST_CHECK(b);
393     a = *b;
394     delete b;
395   }
396 };
397
398
399 // Helper to Create bitmap image
400 BitmapImage CreateBitmapImage();
401
402 #endif // __DALI_TEST_SUITE_UTILS_H__