Fix for slider labels
[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 <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, then the function & line number is printed.
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 }
70
71 template <typename Type>
72 inline bool CompareType(Type value1, Type value2, float epsilon);
73
74 /**
75  * A helper for fuzzy-comparing Vector2 objects
76  * @param[in] vector1 the first object
77  * @param[in] vector2 the second object
78  * @param[in] epsilon difference threshold
79  * @returns true if difference is smaller than epsilon threshold, false otherwise
80  */
81 template <>
82 inline bool CompareType<float>(float value1, float value2, float epsilon)
83 {
84   return fabsf(value1 - value2) < epsilon;
85 }
86
87 /**
88  * A helper for fuzzy-comparing Vector2 objects
89  * @param[in] vector1 the first object
90  * @param[in] vector2 the second object
91  * @param[in] epsilon difference threshold
92  * @returns true if difference is smaller than epsilon threshold, false otherwise
93  */
94 template <>
95 inline bool CompareType<Vector2>(Vector2 vector1, Vector2 vector2, float epsilon)
96 {
97   return fabsf(vector1.x - vector2.x)<epsilon && fabsf(vector1.y - vector2.y)<epsilon;
98 }
99
100 /**
101  * A helper for fuzzy-comparing Vector3 objects
102  * @param[in] vector1 the first object
103  * @param[in] vector2 the second object
104  * @param[in] epsilon difference threshold
105  * @returns true if difference is smaller than epsilon threshold, false otherwise
106  */
107 template <>
108 inline bool CompareType<Vector3>(Vector3 vector1, Vector3 vector2, float epsilon)
109 {
110   return fabsf(vector1.x - vector2.x)<epsilon &&
111          fabsf(vector1.y - vector2.y)<epsilon &&
112          fabsf(vector1.z - vector2.z)<epsilon;
113 }
114
115
116 /**
117  * A helper for fuzzy-comparing Vector4 objects
118  * @param[in] vector1 the first object
119  * @param[in] vector2 the second object
120  * @param[in] epsilon difference threshold
121  * @returns true if difference is smaller than epsilon threshold, false otherwise
122  */
123 template <>
124 inline bool CompareType<Vector4>(Vector4 vector1, Vector4 vector2, float epsilon)
125 {
126   return fabsf(vector1.x - vector2.x)<epsilon &&
127          fabsf(vector1.y - vector2.y)<epsilon &&
128          fabsf(vector1.z - vector2.z)<epsilon &&
129          fabsf(vector1.w - vector2.w)<epsilon;
130 }
131
132 template <>
133 inline bool CompareType<Quaternion>(Quaternion q1, Quaternion q2, float epsilon)
134 {
135   Quaternion q2N = -q2; // These quaternions represent the same rotation
136   return CompareType<Vector4>(q1.mVector, q2.mVector, epsilon) || CompareType<Vector4>(q1.mVector, q2N.mVector, epsilon);
137 }
138
139 template <>
140 inline bool CompareType<Radian>(Radian q1, Radian q2, float epsilon)
141 {
142   return CompareType<float>(float(q1), float(q2), epsilon);
143 }
144
145 template <>
146 inline bool CompareType<Degree>(Degree q1, Degree q2, float epsilon)
147 {
148   return CompareType<float>(float(q1), float(q2), epsilon);
149 }
150
151 bool operator==(TimePeriod a, TimePeriod b);
152 std::ostream& operator<< (std::ostream& o, const TimePeriod value);
153
154 /**
155  * Test whether two values are equal.
156  * @param[in] value1 The first value
157  * @param[in] value2 The second value
158  * @param[in] location The TEST_LOCATION macro should be used here
159  */
160 template<typename TypeA, typename TypeB>
161 inline void DALI_TEST_EQUALS(TypeA value1, TypeB value2, const char* location)
162 {
163   if (!(value1 == value2))
164   {
165     std::ostringstream o;
166     o << value1 << " == " << value2 << std::endl;
167     fprintf(stderr, "%s, checking %s", location, o.str().c_str());
168     tet_result(TET_FAIL);
169   }
170   else
171   {
172     tet_result(TET_PASS);
173   }
174 }
175
176 template<typename Type>
177 inline void DALI_TEST_EQUALS(Type value1, Type value2, float epsilon, const char* location)
178 {
179   if( !CompareType<Type>(value1, value2, epsilon) )
180   {
181     std::ostringstream o;
182     o << value1 << " == " << value2 << std::endl;
183     fprintf(stderr, "%s, checking %s", location, o.str().c_str());
184     tet_result(TET_FAIL);
185   }
186   else
187   {
188     tet_result(TET_PASS);
189   }
190 }
191
192 /**
193  * Test whether two TimePeriods are within a certain distance of each other.
194  * @param[in] value1 The first value
195  * @param[in] value2 The second value
196  * @param[in] epsilon The values must be within this distance of each other
197  * @param[in] location The TEST_LOCATION macro should be used here
198  */
199 template<>
200 inline void DALI_TEST_EQUALS<TimePeriod>( TimePeriod value1, TimePeriod value2, float epsilon, const char* location)
201 {
202   if ((fabs(value1.durationSeconds - value2.durationSeconds) > epsilon))
203   {
204     fprintf(stderr, "%s, checking durations %f == %f, epsilon %f\n", location, value1.durationSeconds, value2.durationSeconds, epsilon);
205     tet_result(TET_FAIL);
206   }
207   else if ((fabs(value1.delaySeconds - value2.delaySeconds) > epsilon))
208   {
209     fprintf(stderr, "%s, checking delays %f == %f, epsilon %f\n", location, value1.delaySeconds, value2.delaySeconds, epsilon);
210     tet_result(TET_FAIL);
211   }
212   else
213   {
214     tet_result(TET_PASS);
215   }
216 }
217
218 /**
219  * Test whether two Matrix3 objects are equal.
220  * @param[in] matrix1 The first object
221  * @param[in] matrix2 The second object
222  * @param[in] location The TEST_LOCATION macro should be used here
223  */
224 void DALI_TEST_EQUALS( const Matrix3& matrix1, const Matrix3& matrix2, const char* location);
225
226 /** Test whether two Matrix3 objects are equal (fuzzy compare).
227  * @param[in] matrix1 The first object
228  * @param[in] matrix2 The second object
229  * @param[in] epsilon The epsilon to use for comparison
230  * @param[in] location The TEST_LOCATION macro should be used here
231  */
232 void DALI_TEST_EQUALS( const Matrix3& matrix1, const Matrix3& matrix2, float epsilon, const char* location);
233
234 /**
235  * Test whether two Matrix objects are equal.
236  * @param[in] matrix1 The first object
237  * @param[in] matrix2 The second object
238  * @param[in] location The TEST_LOCATION macro should be used here
239  */
240 void DALI_TEST_EQUALS( const Matrix& matrix1, const Matrix& matrix2, const char* location);
241
242 /**
243  * Test whether two Matrix objects are equal (fuzzy-compare).
244  * @param[in] matrix1 The first object
245  * @param[in] matrix2 The second object
246  * @param[in] location The TEST_LOCATION macro should be used here
247  */
248 void DALI_TEST_EQUALS( const Matrix& matrix1, const Matrix& matrix2, float epsilon, const char* location);
249
250 /**
251  * Test whether two strings are equal.
252  * @param[in] str1 The first string
253  * @param[in] str2 The second string
254  * @param[in] location The TEST_LOCATION macro should be used here
255  */
256 template<>
257 inline void DALI_TEST_EQUALS<const char*>( const char* str1, const char* str2, const char* location)
258 {
259   if (strcmp(str1, str2))
260   {
261     fprintf(stderr, "%s, checking '%s' == '%s'\n", location, str1, str2);
262     tet_result(TET_FAIL);
263   }
264   else
265   {
266     tet_result(TET_PASS);
267   }
268 }
269
270 /**
271  * Test whether two strings are equal.
272  * @param[in] str1 The first string
273  * @param[in] str2 The second string
274  * @param[in] location The TEST_LOCATION macro should be used here
275  */
276 template<>
277 inline void DALI_TEST_EQUALS<const std::string&>( const std::string &str1, const std::string &str2, const char* location)
278 {
279   DALI_TEST_EQUALS(str1.c_str(), str2.c_str(), location);
280 }
281
282 /**
283  * Test whether two strings are equal.
284  * @param[in] str1 The first string
285  * @param[in] str2 The second string
286  * @param[in] location The TEST_LOCATION macro should be used here
287  */
288 void DALI_TEST_EQUALS( const std::string &str1, const char* str2, const char* location);
289
290 /**
291  * Test whether two strings are equal.
292  * @param[in] str1 The first string
293  * @param[in] str2 The second string
294  * @param[in] location The TEST_LOCATION macro should be used here
295  */
296 void DALI_TEST_EQUALS( const char* str1, const std::string &str2, const char* location);
297
298 /**
299  * Test whether one unsigned integer value is greater than another.
300  * Test succeeds if value1 > value2
301  * @param[in] value1 The first value
302  * @param[in] value2 The second value
303  * @param[in] location The TEST_LOCATION macro should be used here
304  */
305 void DALI_TEST_GREATER(unsigned int value1, unsigned int value2, const char* location);
306
307 /**
308  * Test whether one float value is greater than another.
309  * Test succeeds if value1 > value2
310  * @param[in] value1 The first value
311  * @param[in] value2 The second value
312  * @param[in] location The TEST_LOCATION macro should be used here
313  */
314 void DALI_TEST_GREATER( float value1, float value2, const char* location);
315
316 /**
317  * Test whether the assertion condition that failed and thus triggered the
318  * exception \b e contained a given substring.
319  * @param[in] e The exception that we expect was fired by a runtime assertion failure.
320  * @param[in] conditionSubString The text that we expect to be present in an
321  *                               assertion which triggered the exception.
322  * @param[in] location The TEST_LOCATION macro should be used here.
323  */
324 void DALI_TEST_ASSERT( DaliException& e, std::string conditionSubString, const char* location );
325
326 /**
327  * Print the assert
328  * @param[in] e The exception that we expect was fired by a runtime assertion failure.
329  */
330 inline void DALI_TEST_PRINT_ASSERT( DaliException& e )
331 {
332   tet_printf("Assertion %s failed at %s\n", e.condition, e.location );
333 }
334
335 // Functor to test whether an Applied signal is emitted
336 struct ConstraintAppliedCheck
337 {
338   ConstraintAppliedCheck( bool& signalReceived );
339   void operator()( ActiveConstraint& constraint );
340   void Reset();
341   void CheckSignalReceived();
342   void CheckSignalNotReceived();
343   bool& mSignalReceived; // owned by individual tests
344 };
345
346 /**
347  * A Helper to test default functions
348  */
349 template <typename T>
350 struct DefaultFunctionCoverage
351 {
352   DefaultFunctionCoverage()
353   {
354     T a;
355     T *b = new T(a);
356     DALI_TEST_CHECK(b);
357     a = *b;
358     delete b;
359   }
360 };
361
362
363 // Helper to Create bitmap image
364 BufferImage CreateBufferImage();
365 BufferImage CreateBufferImage(int width, int height, const Vector4& color);
366
367
368 #endif // __DALI_TEST_SUITE_UTILS_H__