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