dee880b6846945345caee27d1b848dbf63627098
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / dali-toolkit-test-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 <dali/public-api/dali-core-capi-internal.h>
24 #include <stdarg.h>
25
26 void tet_infoline(const char*str);
27 void tet_printf(const char *format, ...);
28
29 #include "test-application.h"
30
31 using namespace Dali;
32
33 #define STRINGIZE_I(text) #text
34 #define STRINGIZE(text) STRINGIZE_I(text)
35
36 // the following is the other compilers way of token pasting, gcc seems to just concatenate strings automatically
37 //#define TOKENPASTE(x,y) x ## y
38 #define TOKENPASTE(x,y) x y
39 #define TOKENPASTE2(x,y) TOKENPASTE( x, y )
40 #define TEST_LOCATION TOKENPASTE2( "Test failed in ", TOKENPASTE2( __FILE__, TOKENPASTE2( ", line ", STRINGIZE(__LINE__) ) ) )
41
42 #define TET_UNDEF 2
43 #define TET_FAIL 1
44 #define TET_PASS 0
45
46 extern int test_return_value;
47
48 void tet_result(int value);
49
50 #define END_TEST \
51   return ((test_return_value>0)?1:0)
52
53 void tet_infoline(const char* str);
54 void tet_printf(const char *format, ...);
55
56 /**
57  * DALI_TEST_CHECK is a wrapper for tet_result.
58  * If the condition evaluates to false, then the function & line number is printed.
59  * @param[in] The boolean expression to check
60  */
61 #define DALI_TEST_CHECK(condition)                                                        \
62 if ( (condition) )                                                                        \
63 {                                                                                         \
64   tet_result(TET_PASS);                                                                   \
65 }                                                                                         \
66 else                                                                                      \
67 {                                                                                         \
68   fprintf(stderr, "%s Failed in %s at line %d\n", __PRETTY_FUNCTION__, __FILE__, __LINE__);    \
69   tet_result(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 /**
301  * Test whether two UTF32 strings are equal.
302  * @param[in] str1 The first string
303  * @param[in] str2 The second string
304  * @param[in] location The TEST_LOCATION macro should be used here
305  */
306 template<>
307 inline void DALI_TEST_EQUALS<const TextArray&>( const TextArray& str1, const TextArray& str2, const char* location)
308 {
309   if (!std::equal(str1.begin(), str1.end(), str2.begin()))
310   {
311     fprintf(stderr, "%s, checking '", location);
312
313     for( unsigned int i = 0; i < str1.size(); ++i)
314     {
315       fprintf(stderr, "%c", str1[i]);
316     }
317
318     fprintf(stderr, "' == '");
319
320     for( unsigned int i = 0; i < str2.size(); ++i)
321     {
322       fprintf(stderr, "%c", str2[i]);
323     }
324
325     fprintf(stderr, "'\n");
326
327     tet_result(TET_FAIL);
328   }
329   else
330   {
331     tet_result(TET_PASS);
332   }
333 }
334
335 /**
336  * Test whether one unsigned integer value is greater than another.
337  * Test succeeds if value1 > value2
338  * @param[in] value1 The first value
339  * @param[in] value2 The second value
340  * @param[in] location The TEST_LOCATION macro should be used here
341  */
342 void DALI_TEST_GREATER(unsigned int value1, unsigned int value2, const char* location);
343
344 /**
345  * Test whether one float value is greater than another.
346  * Test succeeds if value1 > value2
347  * @param[in] value1 The first value
348  * @param[in] value2 The second value
349  * @param[in] location The TEST_LOCATION macro should be used here
350  */
351 void DALI_TEST_GREATER( float value1, float value2, const char* location);
352
353 /**
354  * Test whether the assertion condition that failed and thus triggered the
355  * exception \b e contained a given substring at the start of its literal text.
356  * @param[in] e The exception that we expect was fired by a runtime assertion
357  *              failure.
358  * @param[in] conditionSubString The text that we expect to be present in an
359  *                               assertion which triggered the exception.
360  * @param[in] location The TEST_LOCATION macro should be used here.
361  *
362  * @remark **Side-effects:** The result of the tet test is set to TET_PASS if
363  *         the substring is at the start of the exception's condition and
364  *         TET_FAIL if it isn't. Note, if the result of a test is set multiple
365  *         times, a TET_FAIL will override any number of TET_PASSes.
366  */
367 void DALI_TEST_ASSERT( DaliException& e, std::string conditionSubString, const char* location );
368
369 /** Self-documenting wrapper for DALI_TEST_ASSERT.
370  * @copydoc DALI_TEST_ASSERT()
371  */
372 void DALI_TEST_ASSERT_CONDITION_STARTS_WITH_SUBSTRING( DaliException& exceptionFromAssertion, std::string conditionSubString, const char* location );
373
374
375 // Functor to test whether an Applied signal is emitted
376 struct ConstraintAppliedCheck
377 {
378   ConstraintAppliedCheck( bool& signalReceived );
379   void operator()( ActiveConstraint& constraint );
380   void Reset();
381   void CheckSignalReceived();
382   void CheckSignalNotReceived();
383   bool& mSignalReceived; // owned by individual tests
384 };
385
386 /**
387  * A Helper to test default functions
388  */
389 template <typename T>
390 struct DefaultFunctionCoverage
391 {
392   DefaultFunctionCoverage()
393   {
394     T a;
395     T *b = new T(a);
396     DALI_TEST_CHECK(b);
397     a = *b;
398     delete b;
399   }
400 };
401
402
403 // Helper to Create bitmap image
404 BitmapImage CreateBitmapImage();
405 BitmapImage CreateBitmapImage(int width, int height, const Vector4& color);
406
407
408 #endif // __DALI_TEST_SUITE_UTILS_H__