Merge "DALi Version 1.0.31" into tizen
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / 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 // INTERNAL INCLUDES
19 #include <dali/public-api/dali-core.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 bool operator==(TimePeriod a, TimePeriod b)
55 {
56   return Equals(a.durationSeconds, b.durationSeconds) && Equals(a.delaySeconds, b.delaySeconds) ;
57 }
58
59 std::ostream& operator<< (std::ostream& o, const TimePeriod value)
60 {
61   return o << "( Duration:" << value.durationSeconds << " Delay:" << value.delaySeconds << ")";
62 }
63
64 void DALI_TEST_EQUALS( const Matrix3& matrix1, const Matrix3& matrix2, const char* location)
65 {
66   const float* m1 = matrix1.AsFloat();
67   const float* m2 = matrix2.AsFloat();
68   bool equivalent = true;
69
70   for (int i=0;i<9;++i)
71   {
72     equivalent &= (m1[i] != m2[i]);
73   }
74
75   if (!equivalent)
76   {
77     fprintf(stderr, "%s, checking\n"
78                "(%f, %f, %f)    (%f, %f, %f)\n"
79                "(%f, %f, %f) == (%f, %f, %f)\n"
80                "(%f, %f, %f)    (%f, %f, %f)\n",
81                location,
82                m1[0],  m1[1], m1[2],   m2[0],  m2[1], m2[2],
83                m1[3],  m1[4], m1[5],   m2[3],  m2[4], m2[5],
84                m1[6],  m1[7], m1[8],   m2[6],  m2[7], m2[8]);
85
86     tet_result(TET_FAIL);
87   }
88   else
89   {
90     tet_result(TET_PASS);
91   }
92 }
93
94 void DALI_TEST_EQUALS( const Matrix3& matrix1, const Matrix3& matrix2, float epsilon, 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 &= (fabsf(m1[i] - m2[i])<epsilon);
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 Matrix& matrix1, const Matrix& matrix2, const char* location)
125 {
126   const float* m1 = matrix1.AsFloat();
127   const float* m2 = matrix2.AsFloat();
128   bool identical = true;
129
130   int i;
131   for (i=0;i<16;++i)
132   {
133     if(m1[i] != m2[i])
134     {
135       identical = false;
136       break;
137     }
138   }
139
140   if (!identical)
141   {
142     fprintf(stderr, "%s, checking\n"
143                "(%f, %f, %f, %f)    (%f, %f, %f, %f)\n"
144                "(%f, %f, %f, %f) == (%f, %f, %f, %f)\n"
145                "(%f, %f, %f, %f)    (%f, %f, %f, %f)\n"
146                "(%f, %f, %f, %f)    (%f, %f, %f, %f)\n", location,
147                m1[0],  m1[1],  m1[2],  m1[3],   m2[0],  m2[1],  m2[2],  m2[3],
148                m1[4],  m1[5],  m1[6],  m1[7],   m2[4],  m2[5],  m2[6],  m2[7],
149                m1[8],  m1[9], m1[10], m1[11],   m2[8],  m2[9], m2[10], m2[11],
150               m1[12], m1[13], m1[14], m1[15],  m2[12], m2[13], m2[14], m2[15]);
151
152     tet_result(TET_FAIL);
153   }
154   else
155   {
156     tet_result(TET_PASS);
157   }
158 }
159
160 void DALI_TEST_EQUALS( const Matrix& matrix1, const Matrix& matrix2, float epsilon, const char* location)
161 {
162   const float* m1 = matrix1.AsFloat();
163   const float* m2 = matrix2.AsFloat();
164   bool equivalent = true;
165
166   for (int i=0;i<16;++i)
167   {
168     equivalent &= (fabsf(m1[i] - m2[i])<epsilon);
169   }
170
171   if (!equivalent)
172   {
173     fprintf(stderr, "%s, checking\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"
177                "(%f, %f, %f, %f)    (%f, %f, %f, %f)\n", location,
178                m1[0],  m1[1],  m1[2],  m1[3],   m2[0],  m2[1],  m2[2],  m2[3],
179                m1[4],  m1[5],  m1[6],  m1[7],   m2[4],  m2[5],  m2[6],  m2[7],
180                m1[8],  m1[9], m1[10], m1[11],   m2[8],  m2[9], m2[10], m2[11],
181               m1[12], m1[13], m1[14], m1[15],  m2[12], m2[13], m2[14], m2[15]);
182
183     tet_result(TET_FAIL);
184   }
185   else
186   {
187     tet_result(TET_PASS);
188   }
189 }
190
191
192 /**
193  * Test whether two strings are equal.
194  * @param[in] str1 The first string
195  * @param[in] str2 The second string
196  * @param[in] location The TEST_LOCATION macro should be used here
197  */
198 void DALI_TEST_EQUALS( const std::string &str1, const char* str2, const char* location)
199 {
200   DALI_TEST_EQUALS(str1.c_str(), str2, location);
201 }
202
203 /**
204  * Test whether two strings are equal.
205  * @param[in] str1 The first string
206  * @param[in] str2 The second string
207  * @param[in] location The TEST_LOCATION macro should be used here
208  */
209 void DALI_TEST_EQUALS( const char* str1, const std::string &str2, const char* location)
210 {
211   DALI_TEST_EQUALS(str1, str2.c_str(), location);
212 }
213
214
215 /**
216  * Test whether one unsigned integer value is greater than another.
217  * Test succeeds if value1 > value2
218  * @param[in] value1 The first value
219  * @param[in] value2 The second value
220  * @param[in] location The TEST_LOCATION macro should be used here
221  */
222 void DALI_TEST_GREATER(unsigned int value1, unsigned int value2, const char* location)
223 {
224   if (!(value1 > value2))
225   {
226     fprintf(stderr, "%s, checking %d > %d\n", location, value1, value2);
227     tet_result(TET_FAIL);
228   }
229   else
230   {
231     tet_result(TET_PASS);
232   }
233 }
234
235 /**
236  * Test whether one float value is greater than another.
237  * Test succeeds if value1 > value2
238  * @param[in] value1 The first value
239  * @param[in] value2 The second value
240  * @param[in] location The TEST_LOCATION macro should be used here
241  */
242 void DALI_TEST_GREATER( float value1, float value2, const char* location)
243 {
244   if (!(value1 > value2))
245   {
246     fprintf(stderr, "%s, checking %f > %f\n", location, value1, value2);
247     tet_result(TET_FAIL);
248   }
249   else
250   {
251     tet_result(TET_PASS);
252   }
253 }
254
255 void DALI_TEST_ASSERT( DaliException& e, std::string conditionSubString, const char* location )
256 {
257   if( NULL == strstr( e.condition, conditionSubString.c_str() ) )
258   {
259     fprintf(stderr, "Expected substring '%s' : actual exception string '%s' : location %s\n", conditionSubString.c_str(), e.condition, location );
260     tet_result(TET_FAIL);
261   }
262   else
263   {
264     tet_result(TET_PASS);
265   }
266 }
267
268 // Functor to test whether an Applied signal is emitted
269 ConstraintAppliedCheck::ConstraintAppliedCheck( bool& signalReceived )
270 : mSignalReceived( signalReceived )
271 {
272 }
273
274 void ConstraintAppliedCheck::operator()( ActiveConstraint& constraint )
275 {
276   mSignalReceived = true;
277 }
278
279 void ConstraintAppliedCheck::Reset()
280 {
281   mSignalReceived = false;
282 }
283
284 void ConstraintAppliedCheck::CheckSignalReceived()
285 {
286   if ( !mSignalReceived )
287   {
288     fprintf(stderr,  "Expected Applied signal was not received\n" );
289     tet_result( TET_FAIL );
290   }
291   else
292   {
293     tet_result( TET_PASS );
294   }
295 }
296
297 void ConstraintAppliedCheck::CheckSignalNotReceived()
298 {
299   if ( mSignalReceived )
300   {
301     fprintf(stderr,  "Unexpected Applied signal was received\n" );
302     tet_result( TET_FAIL );
303   }
304   else
305   {
306     tet_result( TET_PASS );
307   }
308 }
309
310 BitmapImage CreateBitmapImage()
311 {
312   BitmapImage image = BitmapImage::New(4,4,Pixel::RGBA8888);
313
314   PixelBuffer* pixbuf = image.GetBuffer();
315
316   // Using a 4x4 image gives a better blend with the GL implementation
317   // than a 3x3 image
318   for(size_t i=0; i<16; i++)
319   {
320     pixbuf[i*4+0] = 0xFF;
321     pixbuf[i*4+1] = 0xFF;
322     pixbuf[i*4+2] = 0xFF;
323     pixbuf[i*4+3] = 0xFF;
324   }
325
326   return image;
327 }