Merge "When matchSystemLanguageDirection is set, it should follow the direction setti...
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / dali-toolkit-test-utils / dali-test-suite-utils.cpp
1 /*
2  * Copyright (c) 2020 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 int32_t test_return_value = TET_UNDEF;
30
31 void tet_result(int32_t 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 extern "C"
44 {
45
46 void tet_infoline(const char* str)
47 {
48   fprintf(stderr, "%s\n", str);
49 }
50
51 void tet_printf(const char *format, ...)
52 {
53   va_list arg;
54   va_start(arg, format);
55   vfprintf(stderr, format, arg);
56   va_end(arg);
57 }
58 }
59
60
61 bool operator==(TimePeriod a, TimePeriod b)
62 {
63   return Equals(a.durationSeconds, b.durationSeconds) && Equals(a.delaySeconds, b.delaySeconds) ;
64 }
65
66 std::ostream& operator<<( std::ostream& ostream, TimePeriod value )
67 {
68   return ostream << "( Duration:" << value.durationSeconds << " Delay:" << value.delaySeconds << ")";
69 }
70
71 std::ostream& operator<<( std::ostream& ostream, Radian angle )
72 {
73   ostream << angle.radian;
74   return ostream;
75 }
76
77 std::ostream& operator<<( std::ostream& ostream, Degree angle )
78 {
79   ostream << angle.degree;
80   return ostream;
81 }
82
83 void DALI_TEST_EQUALS( const BaseHandle& baseHandle1, const BaseHandle& baseHandle2, const char* location )
84 {
85   DALI_TEST_EQUALS< const BaseHandle& >( baseHandle1, baseHandle2, location );
86 }
87
88 void DALI_TEST_EQUALS( const size_t value1, const uint32_t value2, const char* location )
89 {
90   DALI_TEST_EQUALS< uint32_t >( ( uint32_t )( value1 ), value2, location );
91 }
92
93 void DALI_TEST_EQUALS( const uint32_t value1, const size_t value2, const char* location )
94 {
95   DALI_TEST_EQUALS< uint32_t >( value1, ( uint32_t )( value2 ), location );
96 }
97
98 void DALI_TEST_EQUALS( const Matrix3& matrix1, const Matrix3& matrix2, const char* location)
99 {
100   const float* m1 = matrix1.AsFloat();
101   const float* m2 = matrix2.AsFloat();
102   bool equivalent = true;
103
104   for (int32_t i=0;i<9;++i)
105   {
106     if( ! (fabsf(m1[i] - m2[i])< GetRangedEpsilon(m1[i], m2[i])) )
107     {
108       equivalent = false;
109     }
110   }
111
112   if( !equivalent )
113   {
114     // Align each float to 1234.67, i.e. 3.6 will be "   3.60"
115     fprintf( stderr, "%s, checking\n"
116                "%7.2f %7.2f %7.2f    %7.2f %7.2f %7.2f\n"
117                "%7.2f %7.2f %7.2f == %7.2f %7.2f %7.2f\n"
118                "%7.2f %7.2f %7.2f    %7.2f %7.2f %7.2f\n",
119                location,
120                m1[0], m1[3], m1[6],    m2[0], m2[3], m2[6],
121                m1[1], m1[4], m1[7],    m2[1], m2[4], m2[7],
122                m1[2], m1[5], m1[8],    m2[2], m2[5], m2[8] );
123
124     tet_result(TET_FAIL);
125     throw("TET_FAIL");
126   }
127   else
128   {
129     tet_result(TET_PASS);
130   }
131 }
132
133 void DALI_TEST_EQUALS( const Matrix3& matrix1, const Matrix3& matrix2, float epsilon, const char* location)
134 {
135   const float* m1 = matrix1.AsFloat();
136   const float* m2 = matrix2.AsFloat();
137   bool equivalent = true;
138
139   for (int32_t i=0;i<9;++i)
140   {
141     equivalent &= (fabsf(m1[i] - m2[i])<epsilon);
142   }
143
144   if (!equivalent)
145   {
146     // Align each float to 1234.67, i.e. 3.6 will be "   3.60"
147     fprintf( stderr, "%s, checking\n"
148                "%7.2f %7.2f %7.2f    %7.2f %7.2f %7.2f\n"
149                "%7.2f %7.2f %7.2f == %7.2f %7.2f %7.2f\n"
150                "%7.2f %7.2f %7.2f    %7.2f %7.2f %7.2f\n",
151                location,
152                m1[0], m1[3], m1[6],    m2[0], m2[3], m2[6],
153                m1[1], m1[4], m1[7],    m2[1], m2[4], m2[7],
154                m1[2], m1[5], m1[8],    m2[2], m2[5], m2[8] );
155
156     tet_result(TET_FAIL);
157     throw("TET_FAIL");
158   }
159   else
160   {
161     tet_result(TET_PASS);
162   }
163 }
164
165 void DALI_TEST_EQUALS( const Matrix& matrix1, const Matrix& matrix2, const char* location)
166 {
167   const float* m1 = matrix1.AsFloat();
168   const float* m2 = matrix2.AsFloat();
169   bool identical = true;
170
171   int32_t i;
172   for (i=0;i<16;++i)
173   {
174     if(m1[i] != m2[i])
175     {
176       identical = false;
177       break;
178     }
179   }
180
181   if (!identical)
182   {
183     // Align each float to 1234.67, i.e. 3.6 will be "   3.60"
184     fprintf( stderr, "%s, checking\n"
185              "%7.2f %7.2f %7.2f %7.2f    %7.2f %7.2f %7.2f %7.2f\n"
186              "%7.2f %7.2f %7.2f %7.2f == %7.2f %7.2f %7.2f %7.2f\n"
187              "%7.2f %7.2f %7.2f %7.2f    %7.2f %7.2f %7.2f %7.2f\n"
188              "%7.2f %7.2f %7.2f %7.2f    %7.2f %7.2f %7.2f %7.2f\n",
189              location,
190              m1[0], m1[4], m1[8],  m1[12],    m2[0], m2[4], m2[8],  m2[12],
191              m1[1], m1[5], m1[9],  m1[13],    m2[1], m2[5], m2[9],  m2[13],
192              m1[2], m1[6], m1[10], m1[14],    m2[2], m2[6], m2[10], m2[14],
193              m1[3], m1[7], m1[11], m1[15],    m2[3], m2[7], m2[11], m2[15] );
194
195     tet_result(TET_FAIL);
196     throw("TET_FAIL");
197   }
198   else
199   {
200     tet_result(TET_PASS);
201   }
202 }
203
204 void DALI_TEST_EQUALS( const Matrix& matrix1, const Matrix& matrix2, float epsilon, const char* location)
205 {
206   const float* m1 = matrix1.AsFloat();
207   const float* m2 = matrix2.AsFloat();
208   bool equivalent = true;
209
210   for (int32_t i=0;i<16;++i)
211   {
212     equivalent &= (fabsf(m1[i] - m2[i])<epsilon);
213   }
214
215   if (!equivalent)
216   {
217     // Align each float to 1234.67, i.e. 3.6 will be "   3.60"
218     fprintf( stderr, "%s, checking\n"
219              "%7.2f %7.2f %7.2f %7.2f    %7.2f %7.2f %7.2f %7.2f\n"
220              "%7.2f %7.2f %7.2f %7.2f == %7.2f %7.2f %7.2f %7.2f\n"
221              "%7.2f %7.2f %7.2f %7.2f    %7.2f %7.2f %7.2f %7.2f\n"
222              "%7.2f %7.2f %7.2f %7.2f    %7.2f %7.2f %7.2f %7.2f\n",
223              location,
224              m1[0], m1[4], m1[8],  m1[12],    m2[0], m2[4], m2[8],  m2[12],
225              m1[1], m1[5], m1[9],  m1[13],    m2[1], m2[5], m2[9],  m2[13],
226              m1[2], m1[6], m1[10], m1[14],    m2[2], m2[6], m2[10], m2[14],
227              m1[3], m1[7], m1[11], m1[15],    m2[3], m2[7], m2[11], m2[15] );
228
229     tet_result(TET_FAIL);
230     throw("TET_FAIL");
231   }
232   else
233   {
234     tet_result(TET_PASS);
235   }
236 }
237
238
239 /**
240  * Test whether two strings are equal.
241  * @param[in] str1 The first string
242  * @param[in] str2 The second string
243  * @param[in] location The TEST_LOCATION macro should be used here
244  */
245 void DALI_TEST_EQUALS( const std::string &str1, const char* str2, const char* location)
246 {
247   DALI_TEST_EQUALS(str1.c_str(), str2, location);
248 }
249
250 void DALI_TEST_EQUALS( Property::Value& str1, const char* str2, const char* location)
251 {
252   bool result = false;
253
254   if( str1.GetType() == Property::STRING )
255   {
256     std::string string;
257     str1.Get(string);
258     result = !string.compare(str2);
259   }
260
261   if( result )
262   {
263     tet_result(TET_PASS);
264   }
265   else
266   {
267     tet_result(TET_FAIL);
268     throw("TET_FAIL");
269   }
270 }
271
272 void DALI_TEST_EQUALS( const char* str1, const std::string &str2, const char* location)
273 {
274   DALI_TEST_EQUALS(str1, str2.c_str(), location);
275 }
276
277 void DALI_TEST_ASSERT( DaliException& e, std::string conditionSubString, const char* location )
278 {
279   if( NULL == strstr( e.condition, conditionSubString.c_str() ) )
280   {
281     fprintf(stderr, "Expected substring '%s' : actual exception string '%s' : location %s\n", conditionSubString.c_str(), e.condition, location );
282     tet_result(TET_FAIL);
283     throw("TET_FAIL");
284   }
285   else
286   {
287     tet_result(TET_PASS);
288   }
289 }
290
291 // Functor to test whether an Applied signal is emitted
292 ConstraintAppliedCheck::ConstraintAppliedCheck( bool& signalReceived )
293 : mSignalReceived( signalReceived )
294 {
295 }
296
297 void ConstraintAppliedCheck::operator()( Constraint& constraint )
298 {
299   mSignalReceived = true;
300 }
301
302 void ConstraintAppliedCheck::Reset()
303 {
304   mSignalReceived = false;
305 }
306
307 void ConstraintAppliedCheck::CheckSignalReceived()
308 {
309   if ( !mSignalReceived )
310   {
311     fprintf(stderr,  "Expected Applied signal was not received\n" );
312     tet_result( TET_FAIL );
313     throw("TET_FAIL");
314   }
315   else
316   {
317     tet_result( TET_PASS );
318   }
319 }
320
321 void ConstraintAppliedCheck::CheckSignalNotReceived()
322 {
323   if ( mSignalReceived )
324   {
325     fprintf(stderr,  "Unexpected Applied signal was received\n" );
326     tet_result( TET_FAIL );
327     throw("TET_FAIL");
328   }
329   else
330   {
331     tet_result( TET_PASS );
332   }
333 }
334
335 namespace Test
336 {
337
338 struct ObjectDestructionFunctor
339 {
340   // Create a ObjectDestructionFunctor passing in a Dali::RefObject* to be monitored and a bool variable.
341   // Create ObjectRegistry instance and connect to the ObjectDestroyedSignal passing in the above functor for the callback.
342   // Get the ObjectPointer (Actor::GetObjectPtr) of the Actor to be checked for destruction and assign it to the Dali::RefObject*
343   // Check the bool variable which would be true when object destroyed.
344   ObjectDestructionFunctor( Dali::RefObject* objectPtr, bool& refObjectDestroyed )
345   : refObjectPointerToCheck( objectPtr ),
346     refObjectDestroyedBoolean( refObjectDestroyed )
347   {
348     refObjectDestroyed = false;
349   }
350
351   void operator()( const Dali::RefObject* objectPointer )
352   {
353     if ( refObjectPointerToCheck == objectPointer )
354     {
355       refObjectDestroyedBoolean = true;
356     }
357   }
358
359   Dali::RefObject* refObjectPointerToCheck;
360   bool& refObjectDestroyedBoolean;
361 };
362
363 ObjectDestructionTracker::ObjectDestructionTracker( ObjectRegistry objectRegistry )
364 : mObjectRegistry( objectRegistry ),
365   mRefObjectDestroyed( false)
366 {
367 }
368
369 void ObjectDestructionTracker::Start( Actor actor )
370 {
371   ObjectDestructionFunctor destructionFunctor( actor.GetObjectPtr(), mRefObjectDestroyed );
372   mObjectRegistry.ObjectDestroyedSignal().Connect( this, destructionFunctor );
373 }
374
375 bool ObjectDestructionTracker::IsDestroyed()
376 {
377    return mRefObjectDestroyed;
378 }
379
380 } // namespace Test