2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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.
17 #include "test-platform-abstraction.h"
18 #include "dali-test-suite-utils.h"
19 #include <dali/integration-api/bitmap.h>
27 TestPlatformAbstraction::TestPlatformAbstraction()
36 TestPlatformAbstraction::~TestPlatformAbstraction()
41 * @copydoc PlatformAbstraction::GetTimeMicroseconds()
43 void TestPlatformAbstraction::GetTimeMicroseconds(unsigned int &seconds, unsigned int µSeconds)
46 microSeconds = mMicroSeconds;
47 mTrace.PushCall("GetTimeMicroseconds", "");
51 * @copydoc PlatformAbstraction::Suspend()
53 void TestPlatformAbstraction::Suspend()
55 mTrace.PushCall("Suspend", "");
59 * @copydoc PlatformAbstraction::Resume()
61 void TestPlatformAbstraction::Resume()
63 mTrace.PushCall("Resume", "");
66 void TestPlatformAbstraction::GetClosestImageSize( const std::string& filename,
67 const ImageAttributes& attributes,
70 closestSize = mClosestSize;
71 mTrace.PushCall("GetClosestImageSize", "");
74 void TestPlatformAbstraction::GetClosestImageSize( Integration::ResourcePointer resourceBuffer,
75 const ImageAttributes& attributes,
78 closestSize = mClosestSize;
79 mTrace.PushCall("GetClosestImageSize", "");
84 * @copydoc PlatformAbstraction::LoadResource()
86 void TestPlatformAbstraction::LoadResource(const Integration::ResourceRequest& request)
88 mTrace.PushCall("LoadResource", "");
92 tet_infoline ("Warning: multiple resource requests not handled by Test Suite. You may see unexpected errors");
94 mRequest = new Integration::ResourceRequest(request);
97 Integration::ResourcePointer TestPlatformAbstraction::LoadResourceSynchronously( const Integration::ResourceType& resourceType, const std::string& resourcePath )
99 mTrace.PushCall("LoadResourceSynchronously", "");
100 return mResources.loadedResource;
104 * @copydoc PlatformAbstraction::SaveResource()
106 void TestPlatformAbstraction::SaveResource(const Integration::ResourceRequest& request)
108 mTrace.PushCall("SaveResource", "");
112 tet_infoline ("Warning: multiple resource requests not handled by Test Suite. You may see unexpected errors");
114 mRequest = new Integration::ResourceRequest(request);
118 * @copydoc PlatformAbstraction::CancelLoad()
120 void TestPlatformAbstraction::CancelLoad(Integration::ResourceId id, Integration::ResourceTypeId typeId)
122 mTrace.PushCall("CancelLoad", "");
126 * @copydoc PlatformAbstraction::GetResources()
128 void TestPlatformAbstraction::GetResources(Integration::ResourceCache& cache)
130 mTrace.PushCall("GetResources", "");
132 if(mResources.loaded)
134 cache.LoadResponse( mResources.loadedId, mResources.loadedType, mResources.loadedResource, Integration::RESOURCE_COMPLETELY_LOADED );
136 if(mResources.loadFailed)
138 cache.LoadFailed( mResources.loadFailedId, mResources.loadFailure );
142 cache.SaveComplete( mResources.savedId, mResources.savedType );
144 if(mResources.saveFailed)
146 cache.SaveFailed( mResources.saveFailedId, mResources.saveFailure );
151 * @copydoc PlatformAbstraction::IsLoading()
153 bool TestPlatformAbstraction::IsLoading()
155 mTrace.PushCall("IsLoading", "");
156 return mIsLoadingResult;
160 * @copydoc PlatformAbstraction::GetDefaultFontFamily()
162 std::string TestPlatformAbstraction::GetDefaultFontFamily() const
164 mTrace.PushCall("GetDefaultFontFamily", "");
165 return mGetDefaultFontFamilyResult;
169 * @copydoc PlatformAbstraction::GetDefaultFontSize()
171 const float TestPlatformAbstraction::GetDefaultFontSize() const
173 mTrace.PushCall("GetDefaultFontSize", "");
174 return mGetDefaultFontSizeResult;
177 const PixelSize TestPlatformAbstraction::GetFontLineHeightFromCapsHeight(const std::string& fontFamily, const std::string& fontStyle, const CapsHeight& capsHeight) const
179 mTrace.PushCall("GetFontLineHeightFromCapsHeight", "");
180 // LineHeight will be bigger than CapsHeight, so return capsHeight + 1
181 return PixelSize(capsHeight + 1);
185 * @copydoc PlatformAbstraction::GetGlyphData()
188 Integration::GlyphSet* TestPlatformAbstraction::GetGlyphData ( const Integration::TextResourceType& textRequest,
189 const std::string& fontFamily,
190 bool getBitmap) const
194 mTrace.PushCall("GetGlyphData", "getBitmap:true");
198 mTrace.PushCall("GetGlyphData", "getBitmap:false");
201 // It creates fake metrics for the received characters.
203 Integration::GlyphSet* set = new Dali::Integration::GlyphSet();
204 Integration::BitmapPtr bitmapData;
206 std::set<uint32_t> characters;
208 for( Integration::TextResourceType::CharacterList::const_iterator it = textRequest.mCharacterList.begin(), endIt = textRequest.mCharacterList.end(); it != endIt; ++it )
210 if( characters.find( it->character ) == characters.end() )
212 characters.insert( it->character );
213 Integration::GlyphMetrics character = {it->character, Integration::GlyphMetrics::LOW_QUALITY, 10.0f, 10.0f, 9.0f, 1.0f, 10.0f, it->xPosition, it->yPosition };
217 bitmapData = Integration::Bitmap::New(Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, true);
218 bitmapData->GetPackedPixelsProfile()->ReserveBuffer(Pixel::A8, 64, 64);
219 PixelBuffer* pixelBuffer = bitmapData->GetBuffer();
220 memset( pixelBuffer, it->character, 64*64 );
223 set->AddCharacter(bitmapData, character);
227 set->mLineHeight = 10.0f;
228 set->mAscender = 9.0f;
229 set->mUnitsPerEM = 2048.0f/64.0f;
230 set->SetAtlasResourceId( textRequest.mTextureAtlasId );
231 set->mFontHash = textRequest.mFontHash;
237 * @copydoc PlatformAbstraction::GetCachedGlyphData()
240 Integration::GlyphSet* TestPlatformAbstraction::GetCachedGlyphData( const Integration::TextResourceType& textRequest,
241 const std::string& fontFamily ) const
243 mTrace.PushCall("GetCachedGlyphData", "");
245 // It creates fake metrics and bitmap for received numeric characters '0' through '9'.
246 Integration::GlyphSet* set = new Dali::Integration::GlyphSet();
247 Integration::BitmapPtr bitmapData;
249 std::set<uint32_t> characters;
251 for( Integration::TextResourceType::CharacterList::const_iterator it = textRequest.mCharacterList.begin(), endIt = textRequest.mCharacterList.end(); it != endIt; ++it )
253 if( it->character >= '0' && it->character <= '9' && characters.find( it->character ) == characters.end() )
255 characters.insert( it->character );
256 Integration::GlyphMetrics character = {it->character, Integration::GlyphMetrics::HIGH_QUALITY, 10.0f, 10.0f, 9.0f, 1.0f, 10.0f, it->xPosition, it->yPosition };
258 bitmapData = Integration::Bitmap::New(Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, true);
259 bitmapData->GetPackedPixelsProfile()->ReserveBuffer(Pixel::A8, 64, 64);
260 PixelBuffer* pixelBuffer = bitmapData->GetBuffer();
261 memset( pixelBuffer, it->character, 64*64 );
262 set->AddCharacter(bitmapData, character);
266 set->mLineHeight = 10.0f;
267 set->mAscender = 9.0f;
268 set->mUnitsPerEM = 2048.0f/64.0f;
269 set->SetAtlasResourceId( textRequest.mTextureAtlasId );
270 set->mFontHash = textRequest.mFontHash;
277 * @copydoc PlatformAbstraction::GetGlobalMetrics()
279 void TestPlatformAbstraction::GetGlobalMetrics( const std::string& fontFamily,
280 const std::string& fontStyle,
281 Integration::GlobalMetrics& globalMetrics ) const
283 globalMetrics.lineHeight = 10.0f;
284 globalMetrics.ascender = 9.0f;
285 globalMetrics.unitsPerEM = 2048.0f/64.0f;
286 globalMetrics.underlineThickness = 2.f;
287 globalMetrics.underlinePosition = 1.f;
291 * @copydoc PlatformAbstraction::GetFontPath()
293 std::string TestPlatformAbstraction::GetFontPath(const std::string& family, bool bold, bool italic) const
295 mTrace.PushCall("GetFontPath", "");
296 return mGetFontPathResult;
298 // Do nothing with arguments
302 * @copydoc PlatformAbstraction::SetDpi()
304 void TestPlatformAbstraction::SetDpi (unsigned int dpiHorizontal, unsigned int dpiVertical)
306 mTrace.PushCall("SetDpi", "");
310 * @copydoc PlatformAbstraction::GetFontFamilyForChars()
312 std::string TestPlatformAbstraction::GetFontFamilyForChars(const TextArray& charsRequested) const
314 mTrace.PushCall("GetFontFamilyForChars", "");
315 return mGetDefaultFontFamilyResult;
319 * @copydoc PlatformAbstraction::AllGlyphsSupported()
321 bool TestPlatformAbstraction::AllGlyphsSupported(const std::string& name, const std::string& fontStyle, const TextArray& text) const
323 mTrace.PushCall("AllGlyphsSupported", "");
328 * @copydoc PlatformAbstraction::ValidateFontFamilyName()
330 bool TestPlatformAbstraction::ValidateFontFamilyName(const std::string& fontFamily, const std::string& fontStyle, bool& isDefaultSystemFont, std::string& closestMatch, std::string& closestStyleMatch) const
332 mTrace.PushCall("ValidateFontFamilyName", "");
337 * @copydoc PlatformAbstraction::GetFontList()
339 std::vector<std::string> TestPlatformAbstraction::GetFontList( PlatformAbstraction::FontListMode mode ) const
341 mFontListMode = mode;
342 mTrace.PushCall("ValidateGetFontList", "");
343 std::vector<std::string> fontList;
348 * @copydoc PlatformAbstraction::LoadFile()
350 bool TestPlatformAbstraction::LoadFile( const std::string& filename, std::vector< unsigned char >& buffer ) const
352 mTrace.PushCall("LoadFile", "");
353 if( mLoadFileResult.loadResult )
355 buffer = mLoadFileResult.buffer;
358 return mLoadFileResult.loadResult;
362 * @copydoc PlatformAbstraction::SaveFile()
364 bool TestPlatformAbstraction::SaveFile(const std::string& filename, std::vector< unsigned char >& buffer) const
366 mTrace.PushCall("SaveFile", "");
370 void TestPlatformAbstraction::JoinLoaderThreads()
372 mTrace.PushCall("JoinLoaderThreads", "");
375 void TestPlatformAbstraction::UpdateDefaultsFromDevice()
377 mTrace.PushCall("UpdateDefaultsFromDevice", "");
378 mGetDefaultFontFamilyResult+=1.0f;
381 Integration::DynamicsFactory* TestPlatformAbstraction::GetDynamicsFactory()
383 mTrace.PushCall("GetDynamicsFactory", "");
387 bool TestPlatformAbstraction::ReadGlobalMetricsFromCache( const std::string& fontFamily,
388 const std::string& fontStyle,
389 Integration::GlobalMetrics& globalMetrics )
391 mTrace.PushCall("ReadGlobalMetricsFromCacheFile", "");
392 globalMetrics = mReadGlobalMetrics; // Want to copy contents...
393 return mReadGlobalMetricsResult; // Default false (will be set to true on subsequent write)
396 void TestPlatformAbstraction::WriteGlobalMetricsToCache( const std::string& fontFamily,
397 const std::string& fontStyle,
398 const Integration::GlobalMetrics& globalMetrics )
400 // Copy so next read uses written values. TODO: Could add method
401 // to turn this behaviour off for more extensive testing.
402 mReadGlobalMetrics = globalMetrics;
403 mReadGlobalMetricsResult = true;
405 mTrace.PushCall("WriteGlobalMetricsToCacheFile", "");
408 bool TestPlatformAbstraction::ReadMetricsFromCache( const std::string& fontFamily,
409 const std::string& fontStyle,
410 std::vector<Integration::GlyphMetrics>& glyphMetricsContainer )
412 mTrace.PushCall("ReadMetricsFromCacheFile", "");
413 glyphMetricsContainer = mReadMetrics;
414 return mReadMetricsResult; // Default false (will be set to true on subsequent write)
417 void TestPlatformAbstraction::WriteMetricsToCache( const std::string& fontFamily,
418 const std::string& fontStyle,
419 const Integration::GlyphSet& glyphSet )
421 // Copy so next read uses written values. TODO: Could add method
422 // to turn this behaviour off for more extensive testing.
423 const Integration::GlyphSet::CharacterList& charList = glyphSet.GetCharacterList();
424 mReadMetrics.clear();
425 for(std::size_t i=0, end=charList.size(); i<end; ++i)
427 mReadMetrics.push_back(charList[i].second);
429 mReadMetricsResult = true;
431 mTrace.PushCall("WriteMetricsToCacheFile", "");
435 void TestPlatformAbstraction::GetFileNamesFromDirectory( const std::string& directoryName,
436 std::vector<std::string>& fileNames )
438 fileNames.push_back( std::string( "u1f004.png" ) );
439 fileNames.push_back( std::string( "u1f0cf.png" ) );
440 fileNames.push_back( std::string( "u1f170.png" ) );
441 fileNames.push_back( std::string( "u1f601.png" ) );
445 Integration::BitmapPtr TestPlatformAbstraction::GetGlyphImage( const std::string& fontFamily, const std::string& fontStyle, float fontSize, uint32_t character ) const
447 Integration::BitmapPtr image = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, true );
448 image->GetPackedPixelsProfile()->ReserveBuffer( Pixel::RGBA8888, 1, 1 );
450 mTrace.PushCall("GetGlyphImage", "");
456 /** Call this every test */
457 void TestPlatformAbstraction::Initialize()
461 memset(&mResources, 0, sizeof(Resources));
462 memset(&mReadGlobalMetrics, 0, sizeof(Integration::GlobalMetrics));
465 mIsLoadingResult=false;
466 mGetDefaultFontFamilyResult = "HelveticaNeue";
467 mGetDefaultFontSizeResult=12.0f;
468 mGetFontPathResult="helvetica-12";
469 mReadMetricsResult=false;
470 mReadGlobalMetricsResult=false;
480 bool TestPlatformAbstraction::WasCalled(TestFuncEnum func)
484 case GetTimeMicrosecondsFunc: return mTrace.FindMethod("GetTimeMicroseconds");
485 case SuspendFunc: return mTrace.FindMethod("Suspend");
486 case ResumeFunc: return mTrace.FindMethod("Resume");
487 case LoadResourceFunc: return mTrace.FindMethod("LoadResource");
488 case SaveResourceFunc: return mTrace.FindMethod("SaveResource");
489 case LoadFileFunc: return mTrace.FindMethod("LoadFile");
490 case SaveFileFunc: return mTrace.FindMethod("SaveFile");
491 case CancelLoadFunc: return mTrace.FindMethod("CancelLoad");
492 case GetResourcesFunc: return mTrace.FindMethod("GetResources");
493 case IsLoadingFunc: return mTrace.FindMethod("IsLoading");
494 case GetDefaultFontFamilyFunc: return mTrace.FindMethod("GetDefaultFontFamily");
495 case GetDefaultFontSizeFunc: return mTrace.FindMethod("GetDefaultFontSize");
496 case GetFontLineHeightFromCapsHeightFunc: return mTrace.FindMethod("GetFontLineHeightFromCapsHeight");
497 case GetGlyphDataFunc: return mTrace.FindMethod("GetGlyphData");
498 case GetCachedGlyphDataFunc: return mTrace.FindMethod("GetCachedGlyphData");
499 case GetFontPathFunc: return mTrace.FindMethod("GetFontPath");
500 case SetDpiFunc: return mTrace.FindMethod("SetDpi");
501 case JoinLoaderThreadsFunc: return mTrace.FindMethod("JoinLoaderThreads");
502 case GetFontFamilyForCharsFunc: return mTrace.FindMethod("GetFontFamilyForChars");
503 case AllGlyphsSupportedFunc: return mTrace.FindMethod("AllGlyphsSupported");
504 case ValidateFontFamilyNameFunc: return mTrace.FindMethod("ValidateFontFamilyName");
505 case UpdateDefaultsFromDeviceFunc: return mTrace.FindMethod("UpdateDefaultsFromDevice");
506 case GetDynamicsFactoryFunc: return mTrace.FindMethod("GetDynamicsFactory");
507 case ValidateGetFontListFunc: return mTrace.FindMethod("ValidateGetFontList");
508 case ReadGlobalMetricsFromCacheFileFunc: return mTrace.FindMethod("ReadGlobalMetricsFromCacheFile");
509 case WriteGlobalMetricsToCacheFileFunc: return mTrace.FindMethod("WriteGlobalMetricsToCacheFile");
510 case ReadMetricsFromCacheFileFunc: return mTrace.FindMethod("ReadMetricsFromCacheFile");
511 case WriteMetricsToCacheFileFunc: return mTrace.FindMethod("WriteMetricsToCacheFile");
516 void TestPlatformAbstraction::SetGetTimeMicrosecondsResult(size_t sec, size_t usec)
519 mMicroSeconds = usec;
522 void TestPlatformAbstraction::IncrementGetTimeResult(size_t milliseconds)
524 mMicroSeconds += milliseconds * 1000u;
525 unsigned int additionalSeconds = mMicroSeconds / 1000000u;
527 mSeconds += additionalSeconds;
528 mMicroSeconds -= additionalSeconds * 1000000u;
531 void TestPlatformAbstraction::SetIsLoadingResult(bool result)
533 mIsLoadingResult = result;
536 void TestPlatformAbstraction::SetGetDefaultFontFamilyResult(std::string result)
538 mGetDefaultFontFamilyResult = result;
541 void TestPlatformAbstraction::SetGetDefaultFontSizeResult(float result)
543 mGetDefaultFontSizeResult = result;
546 void TestPlatformAbstraction::SetGetFontPathResult(std::string& result)
548 mGetFontPathResult = result;
551 void TestPlatformAbstraction::ClearReadyResources()
553 memset(&mResources, 0, sizeof(Resources));
556 void TestPlatformAbstraction::SetResourceLoaded(Integration::ResourceId loadedId,
557 Integration::ResourceTypeId loadedType,
558 Integration::ResourcePointer loadedResource)
560 mResources.loaded = true;
561 mResources.loadedId = loadedId;
562 mResources.loadedType = loadedType;
563 mResources.loadedResource = loadedResource;
566 void TestPlatformAbstraction::SetResourceLoadFailed(Integration::ResourceId id,
567 Integration::ResourceFailure failure)
569 mResources.loadFailed = true;
570 mResources.loadFailedId = id;
571 mResources.loadFailure = failure;
574 void TestPlatformAbstraction::SetResourceSaved(Integration::ResourceId savedId,
575 Integration::ResourceTypeId savedType)
577 mResources.saved = true;
578 mResources.savedId = savedId;
579 mResources.savedType = savedType;
582 void TestPlatformAbstraction::SetResourceSaveFailed(Integration::ResourceId id,
583 Integration::ResourceFailure failure)
585 mResources.saveFailed = true;
586 mResources.saveFailedId = id;
587 mResources.saveFailure = failure;
590 Integration::ResourceRequest* TestPlatformAbstraction::GetRequest()
595 void TestPlatformAbstraction::DiscardRequest()
601 void TestPlatformAbstraction::SetClosestImageSize(const Vector2& size)
606 void TestPlatformAbstraction::SetLoadFileResult( bool result, std::vector< unsigned char >& buffer )
608 mLoadFileResult.loadResult = result;
611 mLoadFileResult.buffer = buffer;
615 void TestPlatformAbstraction::SetSaveFileResult( bool result )
617 mSaveFileResult = result;
620 Integration::PlatformAbstraction::FontListMode TestPlatformAbstraction::GetLastFontListMode( )
622 return mFontListMode;
625 void TestPlatformAbstraction::SetReadGlobalMetricsResult( bool success, Integration::GlobalMetrics& globalMetrics )
627 mReadGlobalMetricsResult = success;
628 mReadGlobalMetrics = globalMetrics;
631 void TestPlatformAbstraction::SetReadMetricsResult( bool success, std::vector<Integration::GlyphMetrics>& glyphMetricsContainer )
633 mReadMetricsResult = success;
634 mReadMetrics = glyphMetricsContainer; // copy