4f719201183a4700afef0d6eabfa7d5ca4807c8a
[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) 2017 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 // EXTERNAL INCLUDES
22 #include <cstdarg>
23 #include <cstdio>
24 #include <iostream>
25 #include <cstring>
26 #include <string>
27
28 // INTERNAL INCLUDES
29 #include <dali/public-api/dali-core.h>
30 #include <test-compare-types.h>
31
32 void tet_infoline(const char*str);
33 void tet_printf(const char *format, ...);
34
35 #include "test-application.h"
36 #include "test-actor-utils.h"
37
38 using namespace Dali;
39
40 #define STRINGIZE_I(text) #text
41 #define STRINGIZE(text) STRINGIZE_I(text)
42
43 /**
44  * Inspired by https://stackoverflow.com/questions/1706346/file-macro-manipulation-handling-at-compile-time
45  * answer by Chetan Reddy
46  */
47 constexpr int32_t basenameIndex( const char * const path, const int32_t index = 0, const int32_t slashIndex = -1 )
48 {
49    return path[ index ]
50        ? ( path[ index ] == '/'
51            ? basenameIndex( path, index + 1, index )
52            : basenameIndex( path, index + 1, slashIndex ) )
53        : ( slashIndex + 1 );
54 }
55
56 #define __FILELINE__ ( { static const int32_t basenameIdx = basenameIndex( __FILE__ ); \
57                          static_assert (basenameIdx >= 0, "compile-time basename" );   \
58                          __FILE__ ":" STRINGIZE(__LINE__) + basenameIdx ; } )
59
60 #define TEST_LOCATION __FILELINE__
61 #define TEST_INNER_LOCATION(x) ( std::string(x) + " (" + STRINGIZE(__LINE__) + ")" ).c_str()
62
63 #define TET_UNDEF 2
64 #define TET_FAIL 1
65 #define TET_PASS 0
66
67 extern int test_return_value;
68
69 void tet_result(int value);
70
71 #define END_TEST \
72   return ((test_return_value>0)?1:0)
73
74 void tet_infoline(const char* str);
75 void tet_printf(const char *format, ...);
76
77 /**
78  * DALI_TEST_CHECK is a wrapper for tet_result.
79  * If the condition evaluates to false, the test is stopped.
80  * @param[in] The boolean expression to check
81  */
82 #define DALI_TEST_CHECK(condition)                                                        \
83 if ( (condition) )                                                                        \
84 {                                                                                         \
85   tet_result(TET_PASS);                                                                   \
86 }                                                                                         \
87 else                                                                                      \
88 {                                                                                         \
89   fprintf(stderr, "Test failed in %s, condition: %s\n", __FILELINE__, #condition );       \
90   tet_result(TET_FAIL);                                                                   \
91   throw("TET_FAIL");                                                                      \
92 }
93
94
95 bool operator==(TimePeriod a, TimePeriod b);
96 std::ostream& operator<<( std::ostream& ostream, TimePeriod value );
97 std::ostream& operator<<( std::ostream& ostream, Radian angle );
98 std::ostream& operator<<( std::ostream& ostream, Degree angle );
99
100 /**
101  * Test whether two values are equal.
102  * @param[in] value1 The first value
103  * @param[in] value2 The second value
104  * @param[in] location The TEST_LOCATION macro should be used here
105  */
106 template<typename Type>
107 inline void DALI_TEST_EQUALS(Type value1, Type value2, const char* location)
108 {
109   if( !CompareType<Type>(value1, value2, 0.01f) )
110   {
111     std::ostringstream o;
112     o << value1 << " == " << value2 << std::endl;
113     fprintf(stderr, "Test failed in %s, checking %s", location, o.str().c_str());
114     tet_result(TET_FAIL);
115     throw("TET_FAIL");                                                                      \
116   }
117   else
118   {
119     tet_result(TET_PASS);
120   }
121 }
122
123 /**
124  * Test whether two values are equal.
125  * @param[in] value1 The first value
126  * @param[in] value2 The second value
127  */
128 #define DALI_TEST_EQUAL( v1, v2 ) DALI_TEST_EQUALS( v1, v2, __FILELINE__ )
129
130 template<typename Type>
131 inline void DALI_TEST_EQUALS(Type value1, Type value2, float epsilon, const char* location)
132 {
133   if( !CompareType<Type>(value1, value2, epsilon) )
134   {
135     std::ostringstream o;
136     o << value1 << " == " << value2 << std::endl;
137     fprintf(stderr, "Test failed in %s, checking %s", location, o.str().c_str());
138     tet_result(TET_FAIL);
139     throw("TET_FAIL");                                                                      \
140   }
141   else
142   {
143     tet_result(TET_PASS);
144   }
145 }
146
147 template<typename Type>
148 inline void DALI_TEST_NOT_EQUALS(Type value1, Type value2, float epsilon, const char* location)
149 {
150   if( CompareType<Type>(value1, value2, epsilon) )
151   {
152     std::ostringstream o;
153     o << value1 << " !=  " << value2 << std::endl;
154     fprintf(stderr, "Test failed in %s, checking %s", location, o.str().c_str());
155     tet_result(TET_FAIL);
156     throw("TET_FAIL");                                                                      \
157   }
158   else
159   {
160     tet_result(TET_PASS);
161   }
162 }
163
164
165 /**
166  * Test whether two TimePeriods are within a certain distance of each other.
167  * @param[in] value1 The first value
168  * @param[in] value2 The second value
169  * @param[in] epsilon The values must be within this distance of each other
170  * @param[in] location The TEST_LOCATION macro should be used here
171  */
172 template<>
173 inline void DALI_TEST_EQUALS<TimePeriod>( TimePeriod value1, TimePeriod value2, float epsilon, const char* location)
174 {
175   if ((fabs(value1.durationSeconds - value2.durationSeconds) > epsilon))
176   {
177     fprintf(stderr, "Test failed in %s, checking durations %f == %f, epsilon %f\n", location, value1.durationSeconds, value2.durationSeconds, epsilon);
178     tet_result(TET_FAIL);
179     throw("TET_FAIL");                                                                      \
180   }
181   else if ((fabs(value1.delaySeconds - value2.delaySeconds) > epsilon))
182   {
183     fprintf(stderr, "Test failed in %s, checking delays %f == %f, epsilon %f\n", location, value1.delaySeconds, value2.delaySeconds, epsilon);
184     tet_result(TET_FAIL);
185     throw("TET_FAIL");                                                                      \
186   }
187   else
188   {
189     tet_result(TET_PASS);
190   }
191 }
192
193 /**
194  * Test whether two base handles are equal.
195  * @param[in] baseHandle1 The first value
196  * @param[in] baseHandle2 The second value
197  * @param[in] location The TEST_LOCATION macro should be used here
198  */
199 void DALI_TEST_EQUALS( const BaseHandle& baseHandle1, const BaseHandle& baseHandle2, const char* location );
200
201 /**
202  * Test whether a size_t value and an unsigned int are equal.
203  * @param[in] value1 The first value
204  * @param[in] value2 The second value
205  * @param[in] location The TEST_LOCATION macro should be used here
206  */
207 void DALI_TEST_EQUALS( const size_t value1, const unsigned int value2, const char* location );
208
209 /**
210  * Test whether an unsigned int and a size_t value and are equal.
211  * @param[in] value1 The first value
212  * @param[in] value2 The second value
213  * @param[in] location The TEST_LOCATION macro should be used here
214  */
215 void DALI_TEST_EQUALS( const unsigned int value1, const size_t value2, const char* location );
216
217 /**
218  * Test whether two Matrix3 objects are equal.
219  * @param[in] matrix1 The first object
220  * @param[in] matrix2 The second object
221  * @param[in] location The TEST_LOCATION macro should be used here
222  */
223 void DALI_TEST_EQUALS( const Matrix3& matrix1, const Matrix3& matrix2, const char* location);
224
225 /** Test whether two Matrix3 objects are equal (fuzzy compare).
226  * @param[in] matrix1 The first object
227  * @param[in] matrix2 The second object
228  * @param[in] epsilon The epsilon to use for comparison
229  * @param[in] location The TEST_LOCATION macro should be used here
230  */
231 void DALI_TEST_EQUALS( const Matrix3& matrix1, const Matrix3& matrix2, float epsilon, const char* location);
232
233 /**
234  * Test whether two Matrix objects are equal.
235  * @param[in] matrix1 The first object
236  * @param[in] matrix2 The second object
237  * @param[in] location The TEST_LOCATION macro should be used here
238  */
239 void DALI_TEST_EQUALS( const Matrix& matrix1, const Matrix& matrix2, const char* location);
240
241 /**
242  * Test whether two Matrix objects are equal (fuzzy-compare).
243  * @param[in] matrix1 The first object
244  * @param[in] matrix2 The second object
245  * @param[in] location The TEST_LOCATION macro should be used here
246  */
247 void DALI_TEST_EQUALS( const Matrix& matrix1, const Matrix& matrix2, float epsilon, const char* location);
248
249 /**
250  * Test whether two strings are equal.
251  * @param[in] str1 The first string
252  * @param[in] str2 The second string
253  * @param[in] location The TEST_LOCATION macro should be used here
254  */
255 template<>
256 inline void DALI_TEST_EQUALS<const char*>( const char* str1, const char* str2, const char* location)
257 {
258   if (strcmp(str1, str2))
259   {
260     fprintf(stderr, "Test failed in %s, checking '%s' == '%s'\n", location, str1, str2);
261     tet_result(TET_FAIL);
262     throw("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( Property::Value& 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 std::string &str1, const char* str2, const char* location);
297
298 /**
299  * Test whether two strings are equal.
300  * @param[in] str1 The first string
301  * @param[in] str2 The second string
302  * @param[in] location The TEST_LOCATION macro should be used here
303  */
304 void DALI_TEST_EQUALS( const char* str1, const std::string &str2, const char* location);
305
306 /**
307  * Test whether one unsigned integer value is greater than another.
308  * Test succeeds if value1 > value2
309  * @param[in] value1 The first value
310  * @param[in] value2 The second value
311  * @param[in] location The TEST_LOCATION macro should be used here
312  */
313 template< typename T >
314 void DALI_TEST_GREATER( T value1, T value2, const char* location)
315 {
316   if (!(value1 > value2))
317   {
318     std::cerr << "Test failed in " << location << ", checking " << value1 <<" > " << value2 << "\n";
319     tet_result(TET_FAIL);
320     throw("TET_FAIL");                                                                      \
321   }
322   else
323   {
324     tet_result(TET_PASS);
325   }
326 }
327
328 /**
329  * Test whether the assertion condition that failed and thus triggered the
330  * exception \b e contained a given substring.
331  * @param[in] e The exception that we expect was fired by a runtime assertion failure.
332  * @param[in] conditionSubString The text that we expect to be present in an
333  *                               assertion which triggered the exception.
334  * @param[in] location The TEST_LOCATION macro should be used here.
335  */
336 void DALI_TEST_ASSERT( DaliException& e, std::string conditionSubString, const char* location );
337
338 /**
339  * Print the assert
340  * @param[in] e The exception that we expect was fired by a runtime assertion failure.
341  */
342 inline void DALI_TEST_PRINT_ASSERT( DaliException& e )
343 {
344   tet_printf("Assertion %s failed at %s\n", e.condition, e.location );
345 }
346
347 // Functor to test whether an Applied signal is emitted
348 struct ConstraintAppliedCheck
349 {
350   ConstraintAppliedCheck( bool& signalReceived );
351   void operator()( Constraint& constraint );
352   void Reset();
353   void CheckSignalReceived();
354   void CheckSignalNotReceived();
355   bool& mSignalReceived; // owned by individual tests
356 };
357
358 /**
359  * A Helper to test default functions
360  */
361 template <typename T>
362 struct DefaultFunctionCoverage
363 {
364   DefaultFunctionCoverage()
365   {
366     T a;
367     T *b = new T(a);
368     DALI_TEST_CHECK(b);
369     a = *b;
370     delete b;
371   }
372 };
373
374
375 // Helper to Create buffer image
376 BufferImage CreateBufferImage();
377 BufferImage CreateBufferImage(int width, int height, const Vector4& color);
378
379
380 // Prepare a resource image to be loaded. Should be called before creating the ResourceImage
381 void PrepareResourceImage( TestApplication& application, unsigned int imageWidth, unsigned int imageHeight, Pixel::Format pixelFormat );
382
383 // Test namespace to prevent pollution of Dali namespace, add Test helper functions here
384 namespace Test
385 {
386 /**
387  *  @brief
388  *
389  *  Helper to check object destruction occurred
390  *  1) In main part of code create an ObjectDestructionTracker
391  *  2) Within sub section of main create object Actor test and call Start with Actor to test for destruction
392  *  3) Perform code which is expected to destroy Actor
393  *  4) Back in main part of code use IsDestroyed() to test if Actor was destroyed
394  */
395 class ObjectDestructionTracker : public ConnectionTracker
396 {
397 public:
398
399   /**
400    * @brief Call in main part of code
401    */
402   ObjectDestructionTracker();
403
404   /**
405    * @brief Call in sub bock of code where the Actor being checked is still alive.
406    *
407    * @param[in] actor Actor to be checked for destruction
408    */
409   void Start( Actor actor );
410
411   /**
412    * @brief Call to check if Actor alive or destroyed.
413    *
414    * @return bool true if Actor was destroyed
415    */
416   bool IsDestroyed();
417
418 private:
419   bool mRefObjectDestroyed;
420 };
421
422 } // namespace Test
423
424 #endif // __DALI_TEST_SUITE_UTILS_H__