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