Merge remote-tracking branch 'origin/tizen' into devel/new_mesh
[platform/core/uifw/dali-adaptor.git] / automated-tests / src / dali-adaptor / dali-test-suite-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
18 // CLASS HEADER
19 #include "dali-test-suite-utils.h"
20
21 // EXTERNAL INCLUDES
22 #include <ostream>
23 #include <cstdio>
24 #include <cstdarg>
25
26 // INTERNAL INCLUDES
27 #include <dali/public-api/dali-core.h>
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& ostream, TimePeriod value )
81 {
82   return ostream << "( Duration:" << value.durationSeconds << " Delay:" << value.delaySeconds << ")";
83 }
84
85 std::ostream& operator<<( std::ostream& ostream, Radian angle )
86 {
87   ostream << angle.radian;
88   return ostream;
89 }
90
91 std::ostream& operator<<( std::ostream& ostream, Degree angle )
92 {
93   ostream << angle.degree;
94   return ostream;
95 }
96
97 void DALI_TEST_EQUALS( const Matrix3& matrix1, const Matrix3& matrix2, const char* location)
98 {
99   const float* m1 = matrix1.AsFloat();
100   const float* m2 = matrix2.AsFloat();
101   bool equivalent = true;
102
103   for (int i=0;i<9;++i)
104   {
105     equivalent &= (m1[i] != m2[i]);
106   }
107
108   if (!equivalent)
109   {
110     fprintf(stderr, "%s, checking\n"
111                "(%f, %f, %f)    (%f, %f, %f)\n"
112                "(%f, %f, %f) == (%f, %f, %f)\n"
113                "(%f, %f, %f)    (%f, %f, %f)\n",
114                location,
115                m1[0],  m1[1], m1[2],   m2[0],  m2[1], m2[2],
116                m1[3],  m1[4], m1[5],   m2[3],  m2[4], m2[5],
117                m1[6],  m1[7], m1[8],   m2[6],  m2[7], m2[8]);
118
119     tet_result(TET_FAIL);
120   }
121   else
122   {
123     tet_result(TET_PASS);
124   }
125 }
126
127 void DALI_TEST_EQUALS( const Matrix3& matrix1, const Matrix3& matrix2, float epsilon, const char* location)
128 {
129   const float* m1 = matrix1.AsFloat();
130   const float* m2 = matrix2.AsFloat();
131   bool equivalent = true;
132
133   for (int i=0;i<9;++i)
134   {
135     equivalent &= (fabsf(m1[i] - m2[i])<epsilon);
136   }
137
138   if (!equivalent)
139   {
140     fprintf(stderr, "%s, checking\n"
141                "(%f, %f, %f)    (%f, %f, %f)\n"
142                "(%f, %f, %f) == (%f, %f, %f)\n"
143                "(%f, %f, %f)    (%f, %f, %f)\n",
144                location,
145                m1[0],  m1[1], m1[2],   m2[0],  m2[1], m2[2],
146                m1[3],  m1[4], m1[5],   m2[3],  m2[4], m2[5],
147                m1[6],  m1[7], m1[8],   m2[6],  m2[7], m2[8]);
148
149     tet_result(TET_FAIL);
150   }
151   else
152   {
153     tet_result(TET_PASS);
154   }
155 }
156
157 void DALI_TEST_EQUALS( const Matrix& matrix1, const Matrix& matrix2, const char* location)
158 {
159   const float* m1 = matrix1.AsFloat();
160   const float* m2 = matrix2.AsFloat();
161   bool identical = true;
162
163   int i;
164   for (i=0;i<16;++i)
165   {
166     if(m1[i] != m2[i])
167     {
168       identical = false;
169       break;
170     }
171   }
172
173   if (!identical)
174   {
175     fprintf(stderr, "%s, checking\n"
176                "(%f, %f, %f, %f) == (%f, %f, %f, %f)\n"
177                "(%f, %f, %f, %f) == (%f, %f, %f, %f)\n"
178                "(%f, %f, %f, %f) == (%f, %f, %f, %f)\n"
179                "(%f, %f, %f, %f) == (%f, %f, %f, %f)\n", location,
180                m1[0],  m1[1],  m1[2],  m1[3],   m2[0],  m2[1],  m2[2],  m2[3],
181                m1[4],  m1[5],  m1[6],  m1[7],   m2[4],  m2[5],  m2[6],  m2[7],
182                m1[8],  m1[9], m1[10], m1[11],   m2[8],  m2[9], m2[10], m2[11],
183               m1[12], m1[13], m1[14], m1[15],  m2[12], m2[13], m2[14], m2[15]);
184
185     tet_result(TET_FAIL);
186   }
187   else
188   {
189     tet_result(TET_PASS);
190   }
191 }
192
193 void DALI_TEST_EQUALS( const Matrix& matrix1, const Matrix& matrix2, float epsilon, const char* location)
194 {
195   const float* m1 = matrix1.AsFloat();
196   const float* m2 = matrix2.AsFloat();
197   bool equivalent = true;
198
199   for (int i=0;i<16;++i)
200   {
201     equivalent &= (fabsf(m1[i] - m2[i])<epsilon);
202   }
203
204   if (!equivalent)
205   {
206     fprintf(stderr, "%s, checking\n"
207                "(%f, %f, %f, %f) == (%f, %f, %f, %f)\n"
208                "(%f, %f, %f, %f) == (%f, %f, %f, %f)\n"
209                "(%f, %f, %f, %f) == (%f, %f, %f, %f)\n"
210                "(%f, %f, %f, %f) == (%f, %f, %f, %f)\n", location,
211                m1[0],  m1[1],  m1[2],  m1[3],   m2[0],  m2[1],  m2[2],  m2[3],
212                m1[4],  m1[5],  m1[6],  m1[7],   m2[4],  m2[5],  m2[6],  m2[7],
213                m1[8],  m1[9], m1[10], m1[11],   m2[8],  m2[9], m2[10], m2[11],
214               m1[12], m1[13], m1[14], m1[15],  m2[12], m2[13], m2[14], m2[15]);
215
216     tet_result(TET_FAIL);
217   }
218   else
219   {
220     tet_result(TET_PASS);
221   }
222 }
223
224
225 /**
226  * Test whether two strings are equal.
227  * @param[in] str1 The first string
228  * @param[in] str2 The second string
229  * @param[in] location The TEST_LOCATION macro should be used here
230  */
231 void DALI_TEST_EQUALS( const std::string &str1, const char* str2, const char* location)
232 {
233   DALI_TEST_EQUALS(str1.c_str(), str2, location);
234 }
235
236 /**
237  * Test whether two strings are equal.
238  * @param[in] str1 The first string
239  * @param[in] str2 The second string
240  * @param[in] location The TEST_LOCATION macro should be used here
241  */
242 void DALI_TEST_EQUALS( const char* str1, const std::string &str2, const char* location)
243 {
244   DALI_TEST_EQUALS(str1, str2.c_str(), location);
245 }
246
247
248 /**
249  * Test whether one unsigned integer value is greater than another.
250  * Test succeeds if value1 > value2
251  * @param[in] value1 The first value
252  * @param[in] value2 The second value
253  * @param[in] location The TEST_LOCATION macro should be used here
254  */
255 void DALI_TEST_GREATER(unsigned int value1, unsigned int value2, const char* location)
256 {
257   if (!(value1 > value2))
258   {
259     fprintf(stderr, "%s, checking %d > %d\n", location, value1, value2);
260     tet_result(TET_FAIL);
261   }
262   else
263   {
264     tet_result(TET_PASS);
265   }
266 }
267
268 /**
269  * Test whether one float value is greater than another.
270  * Test succeeds if value1 > value2
271  * @param[in] value1 The first value
272  * @param[in] value2 The second value
273  * @param[in] location The TEST_LOCATION macro should be used here
274  */
275 void DALI_TEST_GREATER( float value1, float value2, const char* location)
276 {
277   if (!(value1 > value2))
278   {
279     fprintf(stderr, "%s, checking %f > %f\n", location, value1, value2);
280     tet_result(TET_FAIL);
281   }
282   else
283   {
284     tet_result(TET_PASS);
285   }
286 }
287
288 void DALI_TEST_ASSERT( DaliException& e, std::string conditionSubString, const char* location )
289 {
290   if( NULL == strstr( e.condition, conditionSubString.c_str() ) )
291   {
292     fprintf(stderr, "Expected substring '%s' : actual exception string '%s' : location %s\n", conditionSubString.c_str(), e.condition, location );
293     tet_result(TET_FAIL);
294   }
295   else
296   {
297     tet_result(TET_PASS);
298   }
299 }
300
301 // Functor to test whether an Applied signal is emitted
302 ConstraintAppliedCheck::ConstraintAppliedCheck( bool& signalReceived )
303 : mSignalReceived( signalReceived )
304 {
305 }
306
307 void ConstraintAppliedCheck::operator()( Constraint& constraint )
308 {
309   mSignalReceived = true;
310 }
311
312 void ConstraintAppliedCheck::Reset()
313 {
314   mSignalReceived = false;
315 }
316
317 void ConstraintAppliedCheck::CheckSignalReceived()
318 {
319   if ( !mSignalReceived )
320   {
321     fprintf(stderr,  "Expected Applied signal was not received\n" );
322     tet_result( TET_FAIL );
323   }
324   else
325   {
326     tet_result( TET_PASS );
327   }
328 }
329
330 void ConstraintAppliedCheck::CheckSignalNotReceived()
331 {
332   if ( mSignalReceived )
333   {
334     fprintf(stderr,  "Unexpected Applied signal was received\n" );
335     tet_result( TET_FAIL );
336   }
337   else
338   {
339     tet_result( TET_PASS );
340   }
341 }
342
343 BufferImage CreateBufferImage()
344 {
345   BufferImage image = BufferImage::New(4,4,Pixel::RGBA8888);
346
347   PixelBuffer* pixbuf = image.GetBuffer();
348
349   // Using a 4x4 image gives a better blend with the GL implementation
350   // than a 3x3 image
351   for(size_t i=0; i<16; i++)
352   {
353     pixbuf[i*4+0] = 0xFF;
354     pixbuf[i*4+1] = 0xFF;
355     pixbuf[i*4+2] = 0xFF;
356     pixbuf[i*4+3] = 0xFF;
357   }
358
359   return image;
360 }