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