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