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