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