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