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