2 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "dali-test-suite-utils.h"
25 #include <dali/public-api/dali-core.h>
29 int test_return_value = TET_UNDEF;
31 void tet_result(int value)
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;
41 return ((test_return_value>0)?1:0)
44 void tet_infoline(const char* str)
46 fprintf(stderr, "%s\n", str);
49 void tet_printf(const char *format, ...)
52 va_start(arg, format);
53 vfprintf(stderr, format, arg);
57 bool operator==(TimePeriod a, TimePeriod b)
59 return Equals(a.durationSeconds, b.durationSeconds) && Equals(a.delaySeconds, b.delaySeconds) ;
62 std::ostream& operator<<( std::ostream& ostream, TimePeriod value )
64 return ostream << "( Duration:" << value.durationSeconds << " Delay:" << value.delaySeconds << ")";
67 std::ostream& operator<<( std::ostream& ostream, Radian angle )
69 ostream << angle.radian;
73 std::ostream& operator<<( std::ostream& ostream, Degree angle )
75 ostream << angle.degree;
79 void DALI_TEST_EQUALS( const BaseHandle& baseHandle1, const BaseHandle& baseHandle2, const char* location )
81 DALI_TEST_EQUALS< const BaseHandle& >( baseHandle1, baseHandle2, location );
84 void DALI_TEST_EQUALS( const size_t value1, const unsigned int value2, const char* location )
86 DALI_TEST_EQUALS< unsigned int>( ( unsigned int )( value1 ), value2, location );
89 void DALI_TEST_EQUALS( const unsigned int value1, const size_t value2, const char* location )
91 DALI_TEST_EQUALS< unsigned int >( value1, ( unsigned int )( value2 ), location );
94 void DALI_TEST_EQUALS( const Matrix3& matrix1, const Matrix3& matrix2, const char* location)
96 const float* m1 = matrix1.AsFloat();
97 const float* m2 = matrix2.AsFloat();
98 bool equivalent = true;
100 for (int i=0;i<9;++i)
102 if( ! (fabsf(m1[i] - m2[i])< GetRangedEpsilon(m1[i], m2[i])) )
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",
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]);
119 tet_result(TET_FAIL);
123 tet_result(TET_PASS);
127 void DALI_TEST_EQUALS( const Matrix3& matrix1, const Matrix3& matrix2, float epsilon, const char* location)
129 const float* m1 = matrix1.AsFloat();
130 const float* m2 = matrix2.AsFloat();
131 bool equivalent = true;
133 for (int i=0;i<9;++i)
135 equivalent &= (fabsf(m1[i] - m2[i])<epsilon);
140 fprintf(stderr, "%s, checking\n"
141 "(%f, %f, %f) (%f, %f, %f)\n"
142 "(%f, %f, %f) == (%f, %f, %f)\n"
143 "(%f, %f, %f) (%f, %f, %f)\n",
145 m1[0], m1[1], m1[2], m2[0], m2[1], m2[2],
146 m1[3], m1[4], m1[5], m2[3], m2[4], m2[5],
147 m1[6], m1[7], m1[8], m2[6], m2[7], m2[8]);
149 tet_result(TET_FAIL);
153 tet_result(TET_PASS);
157 void DALI_TEST_EQUALS( const Matrix& matrix1, const Matrix& matrix2, const char* location)
159 const float* m1 = matrix1.AsFloat();
160 const float* m2 = matrix2.AsFloat();
161 bool identical = true;
175 fprintf(stderr, "%s, checking\n"
176 "(%f, %f, %f, %f) (%f, %f, %f, %f)\n"
177 "(%f, %f, %f, %f) == (%f, %f, %f, %f)\n"
178 "(%f, %f, %f, %f) (%f, %f, %f, %f)\n"
179 "(%f, %f, %f, %f) (%f, %f, %f, %f)\n", location,
180 m1[0], m1[1], m1[2], m1[3], m2[0], m2[1], m2[2], m2[3],
181 m1[4], m1[5], m1[6], m1[7], m2[4], m2[5], m2[6], m2[7],
182 m1[8], m1[9], m1[10], m1[11], m2[8], m2[9], m2[10], m2[11],
183 m1[12], m1[13], m1[14], m1[15], m2[12], m2[13], m2[14], m2[15]);
185 tet_result(TET_FAIL);
189 tet_result(TET_PASS);
193 void DALI_TEST_EQUALS( const Matrix& matrix1, const Matrix& matrix2, float epsilon, const char* location)
195 const float* m1 = matrix1.AsFloat();
196 const float* m2 = matrix2.AsFloat();
197 bool equivalent = true;
199 for (int i=0;i<16;++i)
201 equivalent &= (fabsf(m1[i] - m2[i])<epsilon);
206 fprintf(stderr, "%s, checking\n"
207 "(%f, %f, %f, %f) (%f, %f, %f, %f)\n"
208 "(%f, %f, %f, %f) == (%f, %f, %f, %f)\n"
209 "(%f, %f, %f, %f) (%f, %f, %f, %f)\n"
210 "(%f, %f, %f, %f) (%f, %f, %f, %f)\n", location,
211 m1[0], m1[1], m1[2], m1[3], m2[0], m2[1], m2[2], m2[3],
212 m1[4], m1[5], m1[6], m1[7], m2[4], m2[5], m2[6], m2[7],
213 m1[8], m1[9], m1[10], m1[11], m2[8], m2[9], m2[10], m2[11],
214 m1[12], m1[13], m1[14], m1[15], m2[12], m2[13], m2[14], m2[15]);
216 tet_result(TET_FAIL);
220 tet_result(TET_PASS);
226 * Test whether two strings are equal.
227 * @param[in] str1 The first string
228 * @param[in] str2 The second string
229 * @param[in] location The TEST_LOCATION macro should be used here
231 void DALI_TEST_EQUALS( const std::string &str1, const char* str2, const char* location)
233 DALI_TEST_EQUALS(str1.c_str(), str2, location);
236 void DALI_TEST_EQUALS( Property::Value& str1, const char* str2, const char* location)
240 if( str1.GetType() == Property::STRING )
244 result = !string.compare(str2);
249 tet_result(TET_PASS);
253 tet_result(TET_FAIL);
257 void DALI_TEST_EQUALS( const char* str1, const std::string &str2, const char* location)
259 DALI_TEST_EQUALS(str1, str2.c_str(), location);
262 void DALI_TEST_ASSERT( DaliException& e, std::string conditionSubString, const char* location )
264 if( NULL == strstr( e.condition, conditionSubString.c_str() ) )
266 fprintf(stderr, "Expected substring '%s' : actual exception string '%s' : location %s\n", conditionSubString.c_str(), e.condition, location );
267 tet_result(TET_FAIL);
271 tet_result(TET_PASS);
275 // Functor to test whether an Applied signal is emitted
276 ConstraintAppliedCheck::ConstraintAppliedCheck( bool& signalReceived )
277 : mSignalReceived( signalReceived )
281 void ConstraintAppliedCheck::operator()( Constraint& constraint )
283 mSignalReceived = true;
286 void ConstraintAppliedCheck::Reset()
288 mSignalReceived = false;
291 void ConstraintAppliedCheck::CheckSignalReceived()
293 if ( !mSignalReceived )
295 fprintf(stderr, "Expected Applied signal was not received\n" );
296 tet_result( TET_FAIL );
300 tet_result( TET_PASS );
304 void ConstraintAppliedCheck::CheckSignalNotReceived()
306 if ( mSignalReceived )
308 fprintf(stderr, "Unexpected Applied signal was received\n" );
309 tet_result( TET_FAIL );
313 tet_result( TET_PASS );
317 BufferImage CreateBufferImage(int width, int height, const Vector4& color)
319 BufferImage image = BufferImage::New(width, height, Pixel::RGBA8888);
321 PixelBuffer* pixbuf = image.GetBuffer();
323 // Using a 4x4 image gives a better blend with the GL implementation
325 for(size_t i=0; i<16; i++)
327 pixbuf[i*4+0] = color.r*255;
328 pixbuf[i*4+1] = color.g*255;
329 pixbuf[i*4+2] = color.b*255;
330 pixbuf[i*4+3] = color.a*255;
336 BufferImage CreateBufferImage()
338 return CreateBufferImage(4, 4, Color::WHITE);
341 void PrepareResourceImage( TestApplication& application, unsigned int imageWidth, unsigned int imageHeight, Pixel::Format pixelFormat )
343 TestPlatformAbstraction& platform = application.GetPlatform();
344 platform.SetClosestImageSize(Vector2( imageWidth, imageHeight));
346 Integration::Bitmap* bitmap = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, ResourcePolicy::OWNED_RETAIN );
347 Integration::PixelBuffer* pixbuffer = bitmap->GetPackedPixelsProfile()->ReserveBuffer( pixelFormat, imageWidth, imageHeight, imageWidth, imageHeight );
348 unsigned int bytesPerPixel = GetBytesPerPixel( pixelFormat );
349 unsigned int initialColor = 0xFF;
350 memset( pixbuffer, initialColor, imageHeight*imageWidth*bytesPerPixel);
352 Integration::ResourcePointer resourcePtr(bitmap);
353 platform.SetSynchronouslyLoadedResource( resourcePtr );
359 struct ObjectDestructionFunctor
361 // Create a ObjectDestructionFunctor passing in a Dali::RefObject* to be monitored and a bool variable.
362 // Create ObjectRegistry instance and connect to the ObjectDestroyedSignal passing in the above functor for the callback.
363 // Get the ObjectPointer (Actor::GetObjectPtr) of the Actor to be checked for destruction and assign it to the Dali::RefObject*
364 // Check the bool variable which would be true when object destroyed.
365 ObjectDestructionFunctor( Dali::RefObject* objectPtr, bool& refObjectDestroyed )
366 : refObjectPointerToCheck( objectPtr ),
367 refObjectDestroyedBoolean( refObjectDestroyed )
369 refObjectDestroyed = false;
372 void operator()( const Dali::RefObject* objectPointer )
374 if ( refObjectPointerToCheck == objectPointer )
376 refObjectDestroyedBoolean = true;
380 Dali::RefObject* refObjectPointerToCheck;
381 bool& refObjectDestroyedBoolean;
384 ObjectDestructionTracker::ObjectDestructionTracker()
385 :mRefObjectDestroyed( false)
389 void ObjectDestructionTracker::Start( Actor actor )
391 ObjectDestructionFunctor destructionFunctor( actor.GetObjectPtr(), mRefObjectDestroyed );
393 ObjectRegistry objectRegistry = Stage::GetCurrent().GetObjectRegistry();
394 objectRegistry.ObjectDestroyedSignal().Connect( this, destructionFunctor );
397 bool ObjectDestructionTracker::IsDestroyed()
399 return mRefObjectDestroyed;