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