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