Merge "Re-enable Color Emoji on platforms which have FreeType >= 2.0" into tizen
[platform/core/uifw/dali-adaptor.git] / automated-tests / src / dali-adaptor / dali-test-suite-utils / dali-test-suite-utils.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // HEADER
19 #include "dali-test-suite-utils.h"
20
21 // EXTERNAL INCLDUES
22 #include <cstdio>
23 #include <cstdarg>
24
25 // INTERNAL INCLUDES
26 #include <dali/public-api/dali-core.h>
27
28
29 using namespace Dali;
30
31 int test_return_value = TET_UNDEF;
32
33 void tet_result(int value)
34 {
35   // First TET_PASS should set to zero
36   // first TET_FAIL should prevent any further TET_PASS from setting back to zero
37   // Any TET_FAIL should set to fail or leave as fail
38   if( test_return_value != 1 )
39     test_return_value = value;
40 }
41
42 #define END_TEST \
43   return ((test_return_value>0)?1:0)
44
45
46 void tet_infoline(const char* str)
47 {
48   fprintf(stderr, "%s\n", str);
49 }
50
51 void tet_printf(const char *format, ...)
52 {
53   va_list arg;
54   va_start(arg, format);
55   vfprintf(stderr, format, arg);
56   va_end(arg);
57 }
58
59 /**
60  * DALI_TEST_CHECK is a wrapper for tet_result.
61  * If the condition evaluates to false, then the function & line number is printed.
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 }
74
75 bool operator==(TimePeriod a, TimePeriod b)
76 {
77   return Equals(a.durationSeconds, b.durationSeconds) && Equals(a.delaySeconds, b.delaySeconds) ;
78 }
79
80 std::ostream& operator<< (std::ostream& o, const TimePeriod value)
81 {
82   return o << "( Duration:" << value.durationSeconds << " Delay:" << value.delaySeconds << ")";
83 }
84
85 void DALI_TEST_EQUALS( const Matrix3& matrix1, const Matrix3& matrix2, const char* location)
86 {
87   const float* m1 = matrix1.AsFloat();
88   const float* m2 = matrix2.AsFloat();
89   bool equivalent = true;
90
91   for (int i=0;i<9;++i)
92   {
93     equivalent &= (m1[i] != m2[i]);
94   }
95
96   if (!equivalent)
97   {
98     fprintf(stderr, "%s, checking\n"
99                "(%f, %f, %f)    (%f, %f, %f)\n"
100                "(%f, %f, %f) == (%f, %f, %f)\n"
101                "(%f, %f, %f)    (%f, %f, %f)\n",
102                location,
103                m1[0],  m1[1], m1[2],   m2[0],  m2[1], m2[2],
104                m1[3],  m1[4], m1[5],   m2[3],  m2[4], m2[5],
105                m1[6],  m1[7], m1[8],   m2[6],  m2[7], m2[8]);
106
107     tet_result(TET_FAIL);
108   }
109   else
110   {
111     tet_result(TET_PASS);
112   }
113 }
114
115 void DALI_TEST_EQUALS( const Matrix3& matrix1, const Matrix3& matrix2, float epsilon, const char* location)
116 {
117   const float* m1 = matrix1.AsFloat();
118   const float* m2 = matrix2.AsFloat();
119   bool equivalent = true;
120
121   for (int i=0;i<9;++i)
122   {
123     equivalent &= (fabsf(m1[i] - m2[i])<epsilon);
124   }
125
126   if (!equivalent)
127   {
128     fprintf(stderr, "%s, checking\n"
129                "(%f, %f, %f)    (%f, %f, %f)\n"
130                "(%f, %f, %f) == (%f, %f, %f)\n"
131                "(%f, %f, %f)    (%f, %f, %f)\n",
132                location,
133                m1[0],  m1[1], m1[2],   m2[0],  m2[1], m2[2],
134                m1[3],  m1[4], m1[5],   m2[3],  m2[4], m2[5],
135                m1[6],  m1[7], m1[8],   m2[6],  m2[7], m2[8]);
136
137     tet_result(TET_FAIL);
138   }
139   else
140   {
141     tet_result(TET_PASS);
142   }
143 }
144
145 void DALI_TEST_EQUALS( const Matrix& matrix1, const Matrix& matrix2, const char* location)
146 {
147   const float* m1 = matrix1.AsFloat();
148   const float* m2 = matrix2.AsFloat();
149   bool identical = true;
150
151   int i;
152   for (i=0;i<16;++i)
153   {
154     if(m1[i] != m2[i])
155     {
156       identical = false;
157       break;
158     }
159   }
160
161   if (!identical)
162   {
163     fprintf(stderr, "%s, checking\n"
164                "(%f, %f, %f, %f) == (%f, %f, %f, %f)\n"
165                "(%f, %f, %f, %f) == (%f, %f, %f, %f)\n"
166                "(%f, %f, %f, %f) == (%f, %f, %f, %f)\n"
167                "(%f, %f, %f, %f) == (%f, %f, %f, %f)\n", location,
168                m1[0],  m1[1],  m1[2],  m1[3],   m2[0],  m2[1],  m2[2],  m2[3],
169                m1[4],  m1[5],  m1[6],  m1[7],   m2[4],  m2[5],  m2[6],  m2[7],
170                m1[8],  m1[9], m1[10], m1[11],   m2[8],  m2[9], m2[10], m2[11],
171               m1[12], m1[13], m1[14], m1[15],  m2[12], m2[13], m2[14], m2[15]);
172
173     tet_result(TET_FAIL);
174   }
175   else
176   {
177     tet_result(TET_PASS);
178   }
179 }
180
181 void DALI_TEST_EQUALS( const Matrix& matrix1, const Matrix& matrix2, float epsilon, const char* location)
182 {
183   const float* m1 = matrix1.AsFloat();
184   const float* m2 = matrix2.AsFloat();
185   bool equivalent = true;
186
187   for (int i=0;i<16;++i)
188   {
189     equivalent &= (fabsf(m1[i] - m2[i])<epsilon);
190   }
191
192   if (!equivalent)
193   {
194     fprintf(stderr, "%s, checking\n"
195                "(%f, %f, %f, %f) == (%f, %f, %f, %f)\n"
196                "(%f, %f, %f, %f) == (%f, %f, %f, %f)\n"
197                "(%f, %f, %f, %f) == (%f, %f, %f, %f)\n"
198                "(%f, %f, %f, %f) == (%f, %f, %f, %f)\n", location,
199                m1[0],  m1[1],  m1[2],  m1[3],   m2[0],  m2[1],  m2[2],  m2[3],
200                m1[4],  m1[5],  m1[6],  m1[7],   m2[4],  m2[5],  m2[6],  m2[7],
201                m1[8],  m1[9], m1[10], m1[11],   m2[8],  m2[9], m2[10], m2[11],
202               m1[12], m1[13], m1[14], m1[15],  m2[12], m2[13], m2[14], m2[15]);
203
204     tet_result(TET_FAIL);
205   }
206   else
207   {
208     tet_result(TET_PASS);
209   }
210 }
211
212
213 /**
214  * Test whether two strings are equal.
215  * @param[in] str1 The first string
216  * @param[in] str2 The second string
217  * @param[in] location The TEST_LOCATION macro should be used here
218  */
219 void DALI_TEST_EQUALS( const std::string &str1, const char* str2, const char* location)
220 {
221   DALI_TEST_EQUALS(str1.c_str(), str2, location);
222 }
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 void DALI_TEST_EQUALS( const char* str1, const std::string &str2, const char* location)
231 {
232   DALI_TEST_EQUALS(str1, str2.c_str(), location);
233 }
234
235
236 /**
237  * Test whether one unsigned integer value is greater than another.
238  * Test succeeds if value1 > value2
239  * @param[in] value1 The first value
240  * @param[in] value2 The second value
241  * @param[in] location The TEST_LOCATION macro should be used here
242  */
243 void DALI_TEST_GREATER(unsigned int value1, unsigned int value2, const char* location)
244 {
245   if (!(value1 > value2))
246   {
247     fprintf(stderr, "%s, checking %d > %d\n", location, value1, value2);
248     tet_result(TET_FAIL);
249   }
250   else
251   {
252     tet_result(TET_PASS);
253   }
254 }
255
256 /**
257  * Test whether one float value is greater than another.
258  * Test succeeds if value1 > value2
259  * @param[in] value1 The first value
260  * @param[in] value2 The second value
261  * @param[in] location The TEST_LOCATION macro should be used here
262  */
263 void DALI_TEST_GREATER( float value1, float value2, const char* location)
264 {
265   if (!(value1 > value2))
266   {
267     fprintf(stderr, "%s, checking %f > %f\n", location, value1, value2);
268     tet_result(TET_FAIL);
269   }
270   else
271   {
272     tet_result(TET_PASS);
273   }
274 }
275
276 void DALI_TEST_ASSERT( DaliException& e, std::string conditionSubString, const char* location )
277 {
278   if( NULL == strstr( e.condition, conditionSubString.c_str() ) )
279   {
280     fprintf(stderr, "Expected substring '%s' : actual exception string '%s' : location %s\n", conditionSubString.c_str(), e.condition, location );
281     tet_result(TET_FAIL);
282   }
283   else
284   {
285     tet_result(TET_PASS);
286   }
287 }
288
289 // Functor to test whether an Applied signal is emitted
290 ConstraintAppliedCheck::ConstraintAppliedCheck( bool& signalReceived )
291 : mSignalReceived( signalReceived )
292 {
293 }
294
295 void ConstraintAppliedCheck::operator()( Constraint& constraint )
296 {
297   mSignalReceived = true;
298 }
299
300 void ConstraintAppliedCheck::Reset()
301 {
302   mSignalReceived = false;
303 }
304
305 void ConstraintAppliedCheck::CheckSignalReceived()
306 {
307   if ( !mSignalReceived )
308   {
309     fprintf(stderr,  "Expected Applied signal was not received\n" );
310     tet_result( TET_FAIL );
311   }
312   else
313   {
314     tet_result( TET_PASS );
315   }
316 }
317
318 void ConstraintAppliedCheck::CheckSignalNotReceived()
319 {
320   if ( mSignalReceived )
321   {
322     fprintf(stderr,  "Unexpected Applied signal was received\n" );
323     tet_result( TET_FAIL );
324   }
325   else
326   {
327     tet_result( TET_PASS );
328   }
329 }
330
331 BufferImage CreateBufferImage()
332 {
333   BufferImage image = BufferImage::New(4,4,Pixel::RGBA8888);
334
335   PixelBuffer* pixbuf = image.GetBuffer();
336
337   // Using a 4x4 image gives a better blend with the GL implementation
338   // than a 3x3 image
339   for(size_t i=0; i<16; i++)
340   {
341     pixbuf[i*4+0] = 0xFF;
342     pixbuf[i*4+1] = 0xFF;
343     pixbuf[i*4+2] = 0xFF;
344     pixbuf[i*4+3] = 0xFF;
345   }
346
347   return image;
348 }