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