Synchronise Automated Tests
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / dali-toolkit-test-utils / dali-test-suite-utils.cpp
1 /*
2  * Copyright (c) 2018 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 BaseHandle& baseHandle1, const BaseHandle& baseHandle2, const char* location )
80 {
81   DALI_TEST_EQUALS< const BaseHandle& >( baseHandle1, baseHandle2, location );
82 }
83
84 void DALI_TEST_EQUALS( const size_t value1, const unsigned int value2, const char* location )
85 {
86   DALI_TEST_EQUALS< unsigned int>( ( unsigned int )( value1 ), value2, location );
87 }
88
89 void DALI_TEST_EQUALS( const unsigned int value1, const size_t value2, const char* location )
90 {
91   DALI_TEST_EQUALS< unsigned int >( value1, ( unsigned int )( value2 ), location );
92 }
93
94 void DALI_TEST_EQUALS( const Matrix3& matrix1, const Matrix3& matrix2, const char* location)
95 {
96   const float* m1 = matrix1.AsFloat();
97   const float* m2 = matrix2.AsFloat();
98   bool equivalent = true;
99
100   for (int i=0;i<9;++i)
101   {
102     if( ! (fabsf(m1[i] - m2[i])< GetRangedEpsilon(m1[i], m2[i])) )
103     {
104       equivalent = false;
105     }
106   }
107
108   if( !equivalent )
109   {
110     // Align each float to 1234.67, i.e. 3.6 will be "   3.60"
111     fprintf( stderr, "%s, checking\n"
112                "%7.2f %7.2f %7.2f    %7.2f %7.2f %7.2f\n"
113                "%7.2f %7.2f %7.2f == %7.2f %7.2f %7.2f\n"
114                "%7.2f %7.2f %7.2f    %7.2f %7.2f %7.2f\n",
115                location,
116                m1[0], m1[3], m1[6],    m2[0], m2[3], m2[6],
117                m1[1], m1[4], m1[7],    m2[1], m2[4], m2[7],
118                m1[2], m1[5], m1[8],    m2[2], m2[5], m2[8] );
119
120     tet_result(TET_FAIL);
121     throw("TET_FAIL");
122   }
123   else
124   {
125     tet_result(TET_PASS);
126   }
127 }
128
129 void DALI_TEST_EQUALS( const Matrix3& matrix1, const Matrix3& matrix2, float epsilon, const char* location)
130 {
131   const float* m1 = matrix1.AsFloat();
132   const float* m2 = matrix2.AsFloat();
133   bool equivalent = true;
134
135   for (int i=0;i<9;++i)
136   {
137     equivalent &= (fabsf(m1[i] - m2[i])<epsilon);
138   }
139
140   if (!equivalent)
141   {
142     // Align each float to 1234.67, i.e. 3.6 will be "   3.60"
143     fprintf( stderr, "%s, checking\n"
144                "%7.2f %7.2f %7.2f    %7.2f %7.2f %7.2f\n"
145                "%7.2f %7.2f %7.2f == %7.2f %7.2f %7.2f\n"
146                "%7.2f %7.2f %7.2f    %7.2f %7.2f %7.2f\n",
147                location,
148                m1[0], m1[3], m1[6],    m2[0], m2[3], m2[6],
149                m1[1], m1[4], m1[7],    m2[1], m2[4], m2[7],
150                m1[2], m1[5], m1[8],    m2[2], m2[5], m2[8] );
151
152     tet_result(TET_FAIL);
153     throw("TET_FAIL");
154   }
155   else
156   {
157     tet_result(TET_PASS);
158   }
159 }
160
161 void DALI_TEST_EQUALS( const Matrix& matrix1, const Matrix& matrix2, const char* location)
162 {
163   const float* m1 = matrix1.AsFloat();
164   const float* m2 = matrix2.AsFloat();
165   bool identical = true;
166
167   int i;
168   for (i=0;i<16;++i)
169   {
170     if(m1[i] != m2[i])
171     {
172       identical = false;
173       break;
174     }
175   }
176
177   if (!identical)
178   {
179     // Align each float to 1234.67, i.e. 3.6 will be "   3.60"
180     fprintf( stderr, "%s, checking\n"
181              "%7.2f %7.2f %7.2f %7.2f    %7.2f %7.2f %7.2f %7.2f\n"
182              "%7.2f %7.2f %7.2f %7.2f == %7.2f %7.2f %7.2f %7.2f\n"
183              "%7.2f %7.2f %7.2f %7.2f    %7.2f %7.2f %7.2f %7.2f\n"
184              "%7.2f %7.2f %7.2f %7.2f    %7.2f %7.2f %7.2f %7.2f\n",
185              location,
186              m1[0], m1[4], m1[8],  m1[12],    m2[0], m2[4], m2[8],  m2[12],
187              m1[1], m1[5], m1[9],  m1[13],    m2[1], m2[5], m2[9],  m2[13],
188              m1[2], m1[6], m1[10], m1[14],    m2[2], m2[6], m2[10], m2[14],
189              m1[3], m1[7], m1[11], m1[15],    m2[3], m2[7], m2[11], m2[15] );
190
191     tet_result(TET_FAIL);
192     throw("TET_FAIL");
193   }
194   else
195   {
196     tet_result(TET_PASS);
197   }
198 }
199
200 void DALI_TEST_EQUALS( const Matrix& matrix1, const Matrix& matrix2, float epsilon, const char* location)
201 {
202   const float* m1 = matrix1.AsFloat();
203   const float* m2 = matrix2.AsFloat();
204   bool equivalent = true;
205
206   for (int i=0;i<16;++i)
207   {
208     equivalent &= (fabsf(m1[i] - m2[i])<epsilon);
209   }
210
211   if (!equivalent)
212   {
213     // Align each float to 1234.67, i.e. 3.6 will be "   3.60"
214     fprintf( stderr, "%s, checking\n"
215              "%7.2f %7.2f %7.2f %7.2f    %7.2f %7.2f %7.2f %7.2f\n"
216              "%7.2f %7.2f %7.2f %7.2f == %7.2f %7.2f %7.2f %7.2f\n"
217              "%7.2f %7.2f %7.2f %7.2f    %7.2f %7.2f %7.2f %7.2f\n"
218              "%7.2f %7.2f %7.2f %7.2f    %7.2f %7.2f %7.2f %7.2f\n",
219              location,
220              m1[0], m1[4], m1[8],  m1[12],    m2[0], m2[4], m2[8],  m2[12],
221              m1[1], m1[5], m1[9],  m1[13],    m2[1], m2[5], m2[9],  m2[13],
222              m1[2], m1[6], m1[10], m1[14],    m2[2], m2[6], m2[10], m2[14],
223              m1[3], m1[7], m1[11], m1[15],    m2[3], m2[7], m2[11], m2[15] );
224
225     tet_result(TET_FAIL);
226     throw("TET_FAIL");
227   }
228   else
229   {
230     tet_result(TET_PASS);
231   }
232 }
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 std::string &str1, const char* str2, const char* location)
242 {
243   DALI_TEST_EQUALS(str1.c_str(), str2, location);
244 }
245
246 void DALI_TEST_EQUALS( Property::Value& str1, const char* str2, const char* location)
247 {
248   bool result = false;
249
250   if( str1.GetType() == Property::STRING )
251   {
252     std::string string;
253     str1.Get(string);
254     result = !string.compare(str2);
255   }
256
257   if( result )
258   {
259     tet_result(TET_PASS);
260   }
261   else
262   {
263     tet_result(TET_FAIL);
264     throw("TET_FAIL");
265   }
266 }
267
268 void DALI_TEST_EQUALS( const char* str1, const std::string &str2, const char* location)
269 {
270   DALI_TEST_EQUALS(str1, str2.c_str(), location);
271 }
272
273 void DALI_TEST_ASSERT( DaliException& e, std::string conditionSubString, const char* location )
274 {
275   if( NULL == strstr( e.condition, conditionSubString.c_str() ) )
276   {
277     fprintf(stderr, "Expected substring '%s' : actual exception string '%s' : location %s\n", conditionSubString.c_str(), e.condition, location );
278     tet_result(TET_FAIL);
279     throw("TET_FAIL");
280   }
281   else
282   {
283     tet_result(TET_PASS);
284   }
285 }
286
287 // Functor to test whether an Applied signal is emitted
288 ConstraintAppliedCheck::ConstraintAppliedCheck( bool& signalReceived )
289 : mSignalReceived( signalReceived )
290 {
291 }
292
293 void ConstraintAppliedCheck::operator()( Constraint& constraint )
294 {
295   mSignalReceived = true;
296 }
297
298 void ConstraintAppliedCheck::Reset()
299 {
300   mSignalReceived = false;
301 }
302
303 void ConstraintAppliedCheck::CheckSignalReceived()
304 {
305   if ( !mSignalReceived )
306   {
307     fprintf(stderr,  "Expected Applied signal was not received\n" );
308     tet_result( TET_FAIL );
309     throw("TET_FAIL");
310   }
311   else
312   {
313     tet_result( TET_PASS );
314   }
315 }
316
317 void ConstraintAppliedCheck::CheckSignalNotReceived()
318 {
319   if ( mSignalReceived )
320   {
321     fprintf(stderr,  "Unexpected Applied signal was received\n" );
322     tet_result( TET_FAIL );
323     throw("TET_FAIL");
324   }
325   else
326   {
327     tet_result( TET_PASS );
328   }
329 }
330
331 BufferImage CreateBufferImage(int width, int height, const Vector4& color)
332 {
333   BufferImage image = BufferImage::New(width, height, Pixel::RGBA8888);
334
335   PixelBuffer* pixbuf = image.GetBuffer();
336
337   // Using a 4x4 image gives a better blend with the GL implementation
338   // than a 3x3 image
339   for(size_t i=0; i<16; i++)
340   {
341     pixbuf[i*4+0] = color.r*255;
342     pixbuf[i*4+1] = color.g*255;
343     pixbuf[i*4+2] = color.b*255;
344     pixbuf[i*4+3] = color.a*255;
345   }
346
347   return image;
348 }
349
350 BufferImage CreateBufferImage()
351 {
352   return CreateBufferImage(4, 4, Color::WHITE);
353 }
354
355 void PrepareResourceImage( TestApplication& application, unsigned int imageWidth, unsigned int imageHeight, Pixel::Format pixelFormat )
356 {
357   TestPlatformAbstraction& platform = application.GetPlatform();
358   platform.SetClosestImageSize(Vector2( imageWidth, imageHeight));
359
360   Integration::Bitmap* bitmap = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, ResourcePolicy::OWNED_RETAIN );
361   Integration::PixelBuffer* pixbuffer = bitmap->GetPackedPixelsProfile()->ReserveBuffer( pixelFormat, imageWidth, imageHeight, imageWidth, imageHeight );
362   unsigned int bytesPerPixel = GetBytesPerPixel(  pixelFormat );
363   unsigned int initialColor = 0xFF;
364   memset( pixbuffer, initialColor, imageHeight*imageWidth*bytesPerPixel);
365
366   Integration::ResourcePointer resourcePtr(bitmap);
367   platform.SetSynchronouslyLoadedResource( resourcePtr );
368 }
369
370 namespace Test
371 {
372
373 struct ObjectDestructionFunctor
374 {
375   // Create a ObjectDestructionFunctor passing in a Dali::RefObject* to be monitored and a bool variable.
376   // Create ObjectRegistry instance and connect to the ObjectDestroyedSignal passing in the above functor for the callback.
377   // Get the ObjectPointer (Actor::GetObjectPtr) of the Actor to be checked for destruction and assign it to the Dali::RefObject*
378   // Check the bool variable which would be true when object destroyed.
379   ObjectDestructionFunctor( Dali::RefObject* objectPtr, bool& refObjectDestroyed )
380   : refObjectPointerToCheck( objectPtr ),
381     refObjectDestroyedBoolean( refObjectDestroyed )
382   {
383     refObjectDestroyed = false;
384   }
385
386   void operator()( const Dali::RefObject* objectPointer )
387   {
388     if ( refObjectPointerToCheck == objectPointer )
389     {
390       refObjectDestroyedBoolean = true;
391     }
392   }
393
394   Dali::RefObject* refObjectPointerToCheck;
395   bool& refObjectDestroyedBoolean;
396 };
397
398 ObjectDestructionTracker::ObjectDestructionTracker()
399   :mRefObjectDestroyed( false)
400 {
401 }
402
403 void ObjectDestructionTracker::Start( Actor actor )
404 {
405   ObjectDestructionFunctor destructionFunctor( actor.GetObjectPtr(), mRefObjectDestroyed );
406
407   ObjectRegistry objectRegistry = Stage::GetCurrent().GetObjectRegistry();
408   objectRegistry.ObjectDestroyedSignal().Connect( this, destructionFunctor );
409 }
410
411 bool ObjectDestructionTracker::IsDestroyed()
412 {
413    return mRefObjectDestroyed;
414 }
415
416 } // namespace Test