Merge "Added scroll-bar to text selection toolbar" into 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 <iostream>
24
25 // INTERNAL INCLUDES
26 #include <dali/public-api/dali-core.h>
27
28 void tet_infoline(const char*str);
29 void tet_printf(const char *format, ...);
30
31 #include "test-application.h"
32 #include "test-actor-utils.h"
33
34 using namespace Dali;
35
36 #define STRINGIZE_I(text) #text
37 #define STRINGIZE(text) STRINGIZE_I(text)
38
39 // the following is the other compilers way of token pasting, gcc seems to just concatenate strings automatically
40 //#define TOKENPASTE(x,y) x ## y
41 #define TOKENPASTE(x,y) x y
42 #define TOKENPASTE2(x,y) TOKENPASTE( x, y )
43 #define TEST_LOCATION TOKENPASTE2( "Test failed in ", TOKENPASTE2( __FILE__, TOKENPASTE2( ", line ", STRINGIZE(__LINE__) ) ) )
44
45 #define TET_UNDEF 2
46 #define TET_FAIL 1
47 #define TET_PASS 0
48
49 extern int test_return_value;
50
51 void tet_result(int value);
52
53 #define END_TEST \
54   return ((test_return_value>0)?1:0)
55
56 void tet_infoline(const char* str);
57 void tet_printf(const char *format, ...);
58
59 /**
60  * DALI_TEST_CHECK is a wrapper for tet_result.
61  * If the condition evaluates to false, the test is stopped.
62  * @param[in] The boolean expression to check
63  */
64 #define DALI_TEST_CHECK(condition)                                                        \
65 if ( (condition) )                                                                        \
66 {                                                                                         \
67   tet_result(TET_PASS);                                                                   \
68 }                                                                                         \
69 else                                                                                      \
70 {                                                                                         \
71   fprintf(stderr, "%s Failed in %s at line %d\n", __PRETTY_FUNCTION__, __FILE__, __LINE__);    \
72   tet_result(TET_FAIL);                                                                   \
73   throw("TET_FAIL");                                                                      \
74 }
75
76 template <typename Type>
77 inline bool CompareType(Type value1, Type value2, float epsilon);
78
79 /**
80  * A helper for fuzzy-comparing Vector2 objects
81  * @param[in] vector1 the first object
82  * @param[in] vector2 the second object
83  * @param[in] epsilon difference threshold
84  * @returns true if difference is smaller than epsilon threshold, false otherwise
85  */
86 template <>
87 inline bool CompareType<float>(float value1, float value2, float epsilon)
88 {
89   return fabsf(value1 - value2) < epsilon;
90 }
91
92 /**
93  * A helper for fuzzy-comparing Vector2 objects
94  * @param[in] vector1 the first object
95  * @param[in] vector2 the second object
96  * @param[in] epsilon difference threshold
97  * @returns true if difference is smaller than epsilon threshold, false otherwise
98  */
99 template <>
100 inline bool CompareType<Vector2>(Vector2 vector1, Vector2 vector2, float epsilon)
101 {
102   return fabsf(vector1.x - vector2.x)<epsilon && fabsf(vector1.y - vector2.y)<epsilon;
103 }
104
105 /**
106  * A helper for fuzzy-comparing Vector3 objects
107  * @param[in] vector1 the first object
108  * @param[in] vector2 the second object
109  * @param[in] epsilon difference threshold
110  * @returns true if difference is smaller than epsilon threshold, false otherwise
111  */
112 template <>
113 inline bool CompareType<Vector3>(Vector3 vector1, Vector3 vector2, float epsilon)
114 {
115   return fabsf(vector1.x - vector2.x)<epsilon &&
116          fabsf(vector1.y - vector2.y)<epsilon &&
117          fabsf(vector1.z - vector2.z)<epsilon;
118 }
119
120
121 /**
122  * A helper for fuzzy-comparing Vector4 objects
123  * @param[in] vector1 the first object
124  * @param[in] vector2 the second object
125  * @param[in] epsilon difference threshold
126  * @returns true if difference is smaller than epsilon threshold, false otherwise
127  */
128 template <>
129 inline bool CompareType<Vector4>(Vector4 vector1, Vector4 vector2, float epsilon)
130 {
131   return fabsf(vector1.x - vector2.x)<epsilon &&
132          fabsf(vector1.y - vector2.y)<epsilon &&
133          fabsf(vector1.z - vector2.z)<epsilon &&
134          fabsf(vector1.w - vector2.w)<epsilon;
135 }
136
137 template <>
138 inline bool CompareType<Quaternion>(Quaternion q1, Quaternion q2, float epsilon)
139 {
140   Quaternion q2N = -q2; // These quaternions represent the same rotation
141   return CompareType<Vector4>(q1.mVector, q2.mVector, epsilon) || CompareType<Vector4>(q1.mVector, q2N.mVector, epsilon);
142 }
143
144 template <>
145 inline bool CompareType<Radian>(Radian q1, Radian q2, float epsilon)
146 {
147   return CompareType<float>(q1.radian, q2.radian, epsilon);
148 }
149
150 template <>
151 inline bool CompareType<Degree>(Degree q1, Degree q2, float epsilon)
152 {
153   return CompareType<float>(q1.degree, q2.degree, epsilon);
154 }
155
156 template <>
157 inline bool CompareType<Property::Value>(Property::Value q1, Property::Value q2, float epsilon)
158 {
159   Property::Type type = q1.GetType();
160   if( type != q2.GetType() )
161   {
162     return false;
163   }
164
165   bool result = false;
166   switch(type)
167   {
168     case Property::BOOLEAN:
169     {
170       bool a, b;
171       q1.Get(a);
172       q2.Get(b);
173       result =  a == b;
174       break;
175     }
176     case Property::INTEGER:
177     {
178       int a, b;
179       q1.Get(a);
180       q2.Get(b);
181       result =  a == b;
182       break;
183     }
184     case Property::FLOAT:
185     {
186       float a, b;
187       q1.Get(a);
188       q2.Get(b);
189       result =  CompareType<float>(a, b, epsilon);
190       break;
191     }
192     case Property::VECTOR2:
193     {
194       Vector2 a, b;
195       q1.Get(a);
196       q2.Get(b);
197       result = CompareType<Vector2>(a, b, epsilon);
198       break;
199     }
200     case Property::VECTOR3:
201     {
202       Vector3 a, b;
203       q1.Get(a);
204       q2.Get(b);
205       result = CompareType<Vector3>(a, b, epsilon);
206       break;
207     }
208     case Property::RECTANGLE:
209     case Property::VECTOR4:
210     {
211       Vector4 a, b;
212       q1.Get(a);
213       q2.Get(b);
214       result = CompareType<Vector4>(a, b, epsilon);
215       break;
216     }
217     case Property::ROTATION:
218     {
219       Quaternion a, b;
220       q1.Get(a);
221       q2.Get(b);
222       result = CompareType<Quaternion>(a, b, epsilon);
223       break;
224     }
225     case Property::MATRIX:
226     case Property::MATRIX3:
227     case Property::STRING:
228     case Property::ARRAY:
229     case Property::MAP:
230     {
231       //TODO: Implement this?
232       DALI_ASSERT_ALWAYS( 0 && "Not implemented");
233       result = false;
234       break;
235     }
236     case Property::NONE:
237     {
238       result = false;
239       break;
240     }
241   }
242
243   return result;
244 }
245
246
247 bool operator==(TimePeriod a, TimePeriod b);
248 std::ostream& operator<<( std::ostream& ostream, TimePeriod value );
249 std::ostream& operator<<( std::ostream& ostream, Radian angle );
250 std::ostream& operator<<( std::ostream& ostream, Degree angle );
251
252 /**
253  * Test whether two values are equal.
254  * @param[in] value1 The first value
255  * @param[in] value2 The second value
256  * @param[in] location The TEST_LOCATION macro should be used here
257  */
258 template<typename Type>
259 inline void DALI_TEST_EQUALS(Type value1, Type value2, const char* location)
260 {
261   if (!(value1 == value2))
262   {
263     std::ostringstream o;
264     o << value1 << " == " << value2 << std::endl;
265     fprintf(stderr, "%s, checking %s", location, o.str().c_str());
266     tet_result(TET_FAIL);
267   }
268   else
269   {
270     tet_result(TET_PASS);
271   }
272 }
273
274 template<typename Type>
275 inline void DALI_TEST_EQUALS(Type value1, Type value2, float epsilon, const char* location)
276 {
277   if( !CompareType<Type>(value1, value2, epsilon) )
278   {
279     std::ostringstream o;
280     o << value1 << " == " << value2 << std::endl;
281     fprintf(stderr, "%s, checking %s", location, o.str().c_str());
282     tet_result(TET_FAIL);
283   }
284   else
285   {
286     tet_result(TET_PASS);
287   }
288 }
289
290 template<typename Type>
291 inline void DALI_TEST_NOT_EQUALS(Type value1, Type value2, float epsilon, const char* location)
292 {
293   if( CompareType<Type>(value1, value2, epsilon) )
294   {
295     std::ostringstream o;
296     o << value1 << " !=  " << value2 << std::endl;
297     fprintf(stderr, "%s, checking %s", location, o.str().c_str());
298     tet_result(TET_FAIL);
299   }
300   else
301   {
302     tet_result(TET_PASS);
303   }
304 }
305
306
307 /**
308  * Test whether two TimePeriods are within a certain distance of each other.
309  * @param[in] value1 The first value
310  * @param[in] value2 The second value
311  * @param[in] epsilon The values must be within this distance of each other
312  * @param[in] location The TEST_LOCATION macro should be used here
313  */
314 template<>
315 inline void DALI_TEST_EQUALS<TimePeriod>( TimePeriod value1, TimePeriod value2, float epsilon, const char* location)
316 {
317   if ((fabs(value1.durationSeconds - value2.durationSeconds) > epsilon))
318   {
319     fprintf(stderr, "%s, checking durations %f == %f, epsilon %f\n", location, value1.durationSeconds, value2.durationSeconds, epsilon);
320     tet_result(TET_FAIL);
321   }
322   else if ((fabs(value1.delaySeconds - value2.delaySeconds) > epsilon))
323   {
324     fprintf(stderr, "%s, checking delays %f == %f, epsilon %f\n", location, value1.delaySeconds, value2.delaySeconds, epsilon);
325     tet_result(TET_FAIL);
326   }
327   else
328   {
329     tet_result(TET_PASS);
330   }
331 }
332
333 /**
334  * Test whether two base handles are equal.
335  * @param[in] baseHandle1 The first value
336  * @param[in] baseHandle2 The second value
337  * @param[in] location The TEST_LOCATION macro should be used here
338  */
339 void DALI_TEST_EQUALS( const BaseHandle& baseHandle1, const BaseHandle& baseHandle2, const char* location );
340
341 /**
342  * Test whether a size_t value and an unsigned int are equal.
343  * @param[in] value1 The first value
344  * @param[in] value2 The second value
345  * @param[in] location The TEST_LOCATION macro should be used here
346  */
347 void DALI_TEST_EQUALS( const size_t value1, const unsigned int value2, const char* location );
348
349 /**
350  * Test whether an unsigned int and a size_t value and are equal.
351  * @param[in] value1 The first value
352  * @param[in] value2 The second value
353  * @param[in] location The TEST_LOCATION macro should be used here
354  */
355 void DALI_TEST_EQUALS( const unsigned int value1, const size_t value2, const char* location );
356
357 /**
358  * Test whether two Matrix3 objects are equal.
359  * @param[in] matrix1 The first object
360  * @param[in] matrix2 The second object
361  * @param[in] location The TEST_LOCATION macro should be used here
362  */
363 void DALI_TEST_EQUALS( const Matrix3& matrix1, const Matrix3& matrix2, const char* location);
364
365 /** Test whether two Matrix3 objects are equal (fuzzy compare).
366  * @param[in] matrix1 The first object
367  * @param[in] matrix2 The second object
368  * @param[in] epsilon The epsilon to use for comparison
369  * @param[in] location The TEST_LOCATION macro should be used here
370  */
371 void DALI_TEST_EQUALS( const Matrix3& matrix1, const Matrix3& matrix2, float epsilon, const char* location);
372
373 /**
374  * Test whether two Matrix objects are equal.
375  * @param[in] matrix1 The first object
376  * @param[in] matrix2 The second object
377  * @param[in] location The TEST_LOCATION macro should be used here
378  */
379 void DALI_TEST_EQUALS( const Matrix& matrix1, const Matrix& matrix2, const char* location);
380
381 /**
382  * Test whether two Matrix objects are equal (fuzzy-compare).
383  * @param[in] matrix1 The first object
384  * @param[in] matrix2 The second object
385  * @param[in] location The TEST_LOCATION macro should be used here
386  */
387 void DALI_TEST_EQUALS( const Matrix& matrix1, const Matrix& matrix2, float epsilon, const char* location);
388
389 /**
390  * Test whether two strings are equal.
391  * @param[in] str1 The first string
392  * @param[in] str2 The second string
393  * @param[in] location The TEST_LOCATION macro should be used here
394  */
395 template<>
396 inline void DALI_TEST_EQUALS<const char*>( const char* str1, const char* str2, const char* location)
397 {
398   if (strcmp(str1, str2))
399   {
400     fprintf(stderr, "%s, checking '%s' == '%s'\n", location, str1, str2);
401     tet_result(TET_FAIL);
402   }
403   else
404   {
405     tet_result(TET_PASS);
406   }
407 }
408
409 /**
410  * Test whether two strings are equal.
411  * @param[in] str1 The first string
412  * @param[in] str2 The second string
413  * @param[in] location The TEST_LOCATION macro should be used here
414  */
415 template<>
416 inline void DALI_TEST_EQUALS<const std::string&>( const std::string &str1, const std::string &str2, const char* location)
417 {
418   DALI_TEST_EQUALS(str1.c_str(), str2.c_str(), location);
419 }
420
421 /**
422  * Test whether two strings are equal.
423  * @param[in] str1 The first string
424  * @param[in] str2 The second string
425  * @param[in] location The TEST_LOCATION macro should be used here
426  */
427 void DALI_TEST_EQUALS( Property::Value& str1, const char* str2, const char* location);
428
429 /**
430  * Test whether two strings are equal.
431  * @param[in] str1 The first string
432  * @param[in] str2 The second string
433  * @param[in] location The TEST_LOCATION macro should be used here
434  */
435 void DALI_TEST_EQUALS( const std::string &str1, const char* str2, const char* location);
436
437 /**
438  * Test whether two strings are equal.
439  * @param[in] str1 The first string
440  * @param[in] str2 The second string
441  * @param[in] location The TEST_LOCATION macro should be used here
442  */
443 void DALI_TEST_EQUALS( const char* str1, const std::string &str2, const char* location);
444
445 /**
446  * Test whether one unsigned integer value is greater than another.
447  * Test succeeds if value1 > value2
448  * @param[in] value1 The first value
449  * @param[in] value2 The second value
450  * @param[in] location The TEST_LOCATION macro should be used here
451  */
452 template< typename T >
453 void DALI_TEST_GREATER( T value1, T value2, const char* location)
454 {
455   if (!(value1 > value2))
456   {
457     std::cerr << location << ", checking " << value1 <<" > " << value2 << "\n";
458     tet_result(TET_FAIL);
459   }
460   else
461   {
462     tet_result(TET_PASS);
463   }
464 }
465
466 /**
467  * Test whether the assertion condition that failed and thus triggered the
468  * exception \b e contained a given substring.
469  * @param[in] e The exception that we expect was fired by a runtime assertion failure.
470  * @param[in] conditionSubString The text that we expect to be present in an
471  *                               assertion which triggered the exception.
472  * @param[in] location The TEST_LOCATION macro should be used here.
473  */
474 void DALI_TEST_ASSERT( DaliException& e, std::string conditionSubString, const char* location );
475
476 /**
477  * Print the assert
478  * @param[in] e The exception that we expect was fired by a runtime assertion failure.
479  */
480 inline void DALI_TEST_PRINT_ASSERT( DaliException& e )
481 {
482   tet_printf("Assertion %s failed at %s\n", e.condition, e.location );
483 }
484
485 // Functor to test whether an Applied signal is emitted
486 struct ConstraintAppliedCheck
487 {
488   ConstraintAppliedCheck( bool& signalReceived );
489   void operator()( Constraint& constraint );
490   void Reset();
491   void CheckSignalReceived();
492   void CheckSignalNotReceived();
493   bool& mSignalReceived; // owned by individual tests
494 };
495
496 /**
497  * A Helper to test default functions
498  */
499 template <typename T>
500 struct DefaultFunctionCoverage
501 {
502   DefaultFunctionCoverage()
503   {
504     T a;
505     T *b = new T(a);
506     DALI_TEST_CHECK(b);
507     a = *b;
508     delete b;
509   }
510 };
511
512
513 // Helper to Create buffer image
514 BufferImage CreateBufferImage();
515 BufferImage CreateBufferImage(int width, int height, const Vector4& color);
516
517 // Prepare a resource image to be loaded. Should be called before creating the ResourceImage
518 void PrepareResourceImage( TestApplication& application, unsigned int imageWidth, unsigned int imageHeight, Pixel::Format pixelFormat );
519
520 // Test namespace to prevent pollution of Dali namespace, add Test helper functions here
521 namespace Test
522 {
523 /**
524  *  @brief
525  *
526  *  Helper to check object destruction occurred
527  *  1) In main part of code create an ObjectDestructionTracker
528  *  2) Within sub section of main create object Actor test and call Start with Actor to test for destruction
529  *  3) Perform code which is expected to destroy Actor
530  *  4) Back in main part of code use IsDestroyed() to test if Actor was destroyed
531  */
532 class ObjectDestructionTracker : public ConnectionTracker
533 {
534 public:
535
536   /**
537    * @brief Call in main part of code
538    */
539   ObjectDestructionTracker();
540
541   /**
542    * @brief Call in sub bock of code where the Actor being checked is still alive.
543    *
544    * @param[in] actor Actor to be checked for destruction
545    */
546   void Start( Actor actor );
547
548   /**
549    * @brief Call to check if Actor alive or destroyed.
550    *
551    * @return bool true if Actor was destroyed
552    */
553   bool IsDestroyed();
554
555 private:
556   bool mRefObjectDestroyed;
557 };
558
559 } // namespace Test
560
561 #endif // __DALI_TEST_SUITE_UTILS_H__