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 getImageData) const
194 mTrace.PushCall("GetGlyphData", "getImageData:true");
198 mTrace.PushCall("GetGlyphData", "getImageData:false");
201 // It creates fake metrics for the received characters.
203 Integration::GlyphSet* set = new Dali::Integration::GlyphSet();
204 Integration::ImageDataPtr imageData;
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 // Allocate an ImageData object to hold some pixels:
218 const unsigned dimension = 64;
219 imageData = Integration::NewBitmapImageData( dimension, dimension, Pixel::A8 );
220 uint8_t* const pixelBuffer = imageData->GetBuffer();
221 memset( pixelBuffer, it->character, dimension * dimension );
223 set->AddCharacter(imageData, 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::ImageDataPtr imageData;
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 imageData = Integration::NewBitmapImageData( 64, 64, Pixel::A8 );
259 PixelBuffer* pixelBuffer = imageData->GetBuffer();
260 memset( pixelBuffer, it->character, 64*64 );
261 set->AddCharacter(imageData, character);
265 set->mLineHeight = 10.0f;
266 set->mAscender = 9.0f;
267 set->mUnitsPerEM = 2048.0f/64.0f;
268 set->SetAtlasResourceId( textRequest.mTextureAtlasId );
269 set->mFontHash = textRequest.mFontHash;
276 * @copydoc PlatformAbstraction::GetGlobalMetrics()
278 void TestPlatformAbstraction::GetGlobalMetrics( const std::string& fontFamily,
279 const std::string& fontStyle,
280 Integration::GlobalMetrics& globalMetrics ) const
282 globalMetrics.lineHeight = 10.0f;
283 globalMetrics.ascender = 9.0f;
284 globalMetrics.unitsPerEM = 2048.0f/64.0f;
285 globalMetrics.underlineThickness = 2.f;
286 globalMetrics.underlinePosition = 1.f;
290 * @copydoc PlatformAbstraction::GetFontPath()
292 std::string TestPlatformAbstraction::GetFontPath(const std::string& family, bool bold, bool italic) const
294 mTrace.PushCall("GetFontPath", "");
295 return mGetFontPathResult;
297 // Do nothing with arguments
301 * @copydoc PlatformAbstraction::SetDpi()
303 void TestPlatformAbstraction::SetDpi (unsigned int dpiHorizontal, unsigned int dpiVertical)
305 mTrace.PushCall("SetDpi", "");
309 * @copydoc PlatformAbstraction::GetFontFamilyForChars()
311 std::string TestPlatformAbstraction::GetFontFamilyForChars(const TextArray& charsRequested) const
313 mTrace.PushCall("GetFontFamilyForChars", "");
314 return mGetDefaultFontFamilyResult;
318 * @copydoc PlatformAbstraction::AllGlyphsSupported()
320 bool TestPlatformAbstraction::AllGlyphsSupported(const std::string& name, const std::string& fontStyle, const TextArray& text) const
322 mTrace.PushCall("AllGlyphsSupported", "");
327 * @copydoc PlatformAbstraction::ValidateFontFamilyName()
329 bool TestPlatformAbstraction::ValidateFontFamilyName(const std::string& fontFamily, const std::string& fontStyle, bool& isDefaultSystemFont, std::string& closestMatch, std::string& closestStyleMatch) const
331 mTrace.PushCall("ValidateFontFamilyName", "");
336 * @copydoc PlatformAbstraction::GetFontList()
338 std::vector<std::string> TestPlatformAbstraction::GetFontList( PlatformAbstraction::FontListMode mode ) const
340 mFontListMode = mode;
341 mTrace.PushCall("ValidateGetFontList", "");
342 std::vector<std::string> fontList;
347 * @copydoc PlatformAbstraction::LoadFile()
349 bool TestPlatformAbstraction::LoadFile( const std::string& filename, std::vector< unsigned char >& buffer ) const
351 mTrace.PushCall("LoadFile", "");
352 if( mLoadFileResult.loadResult )
354 buffer = mLoadFileResult.buffer;
357 return mLoadFileResult.loadResult;
361 * @copydoc PlatformAbstraction::SaveFile()
363 bool TestPlatformAbstraction::SaveFile(const std::string& filename, std::vector< unsigned char >& buffer) const
365 mTrace.PushCall("SaveFile", "");
369 void TestPlatformAbstraction::JoinLoaderThreads()
371 mTrace.PushCall("JoinLoaderThreads", "");
374 void TestPlatformAbstraction::UpdateDefaultsFromDevice()
376 mTrace.PushCall("UpdateDefaultsFromDevice", "");
377 mGetDefaultFontFamilyResult+=1.0f;
380 Integration::DynamicsFactory* TestPlatformAbstraction::GetDynamicsFactory()
382 mTrace.PushCall("GetDynamicsFactory", "");
386 bool TestPlatformAbstraction::ReadGlobalMetricsFromCache( const std::string& fontFamily,
387 const std::string& fontStyle,
388 Integration::GlobalMetrics& globalMetrics )
390 mTrace.PushCall("ReadGlobalMetricsFromCacheFile", "");
391 globalMetrics = mReadGlobalMetrics; // Want to copy contents...
392 return mReadGlobalMetricsResult; // Default false (will be set to true on subsequent write)
395 void TestPlatformAbstraction::WriteGlobalMetricsToCache( const std::string& fontFamily,
396 const std::string& fontStyle,
397 const Integration::GlobalMetrics& globalMetrics )
399 // Copy so next read uses written values. TODO: Could add method
400 // to turn this behaviour off for more extensive testing.
401 mReadGlobalMetrics = globalMetrics;
402 mReadGlobalMetricsResult = true;
404 mTrace.PushCall("WriteGlobalMetricsToCacheFile", "");
407 bool TestPlatformAbstraction::ReadMetricsFromCache( const std::string& fontFamily,
408 const std::string& fontStyle,
409 std::vector<Integration::GlyphMetrics>& glyphMetricsContainer )
411 mTrace.PushCall("ReadMetricsFromCacheFile", "");
412 glyphMetricsContainer = mReadMetrics;
413 return mReadMetricsResult; // Default false (will be set to true on subsequent write)
416 void TestPlatformAbstraction::WriteMetricsToCache( const std::string& fontFamily,
417 const std::string& fontStyle,
418 const Integration::GlyphSet& glyphSet )
420 // Copy so next read uses written values. TODO: Could add method
421 // to turn this behaviour off for more extensive testing.
422 const Integration::GlyphSet::CharacterList& charList = glyphSet.GetCharacterList();
423 mReadMetrics.clear();
424 for(std::size_t i=0, end=charList.size(); i<end; ++i)
426 mReadMetrics.push_back(charList[i].second);
428 mReadMetricsResult = true;
430 mTrace.PushCall("WriteMetricsToCacheFile", "");
434 void TestPlatformAbstraction::GetFileNamesFromDirectory( const std::string& directoryName,
435 std::vector<std::string>& fileNames )
437 fileNames.push_back( std::string( "u1f004.png" ) );
438 fileNames.push_back( std::string( "u1f0cf.png" ) );
439 fileNames.push_back( std::string( "u1f170.png" ) );
440 fileNames.push_back( std::string( "u1f601.png" ) );
444 Integration::ImageDataPtr TestPlatformAbstraction::GetGlyphImage( const std::string& fontFamily, const std::string& fontStyle, float fontSize, uint32_t character ) const
446 Integration::ImageDataPtr image = Integration::NewBitmapImageData( 1, 1, Pixel::RGBA8888 );
448 mTrace.PushCall("GetGlyphImage", "");
454 /** Call this every test */
455 void TestPlatformAbstraction::Initialize()
459 memset(&mResources, 0, sizeof(Resources));
460 memset(&mReadGlobalMetrics, 0, sizeof(Integration::GlobalMetrics));
463 mIsLoadingResult=false;
464 mGetDefaultFontFamilyResult = "HelveticaNeue";
465 mGetDefaultFontSizeResult=12.0f;
466 mGetFontPathResult="helvetica-12";
467 mReadMetricsResult=false;
468 mReadGlobalMetricsResult=false;
478 bool TestPlatformAbstraction::WasCalled(TestFuncEnum func)
482 case GetTimeMicrosecondsFunc: return mTrace.FindMethod("GetTimeMicroseconds");
483 case SuspendFunc: return mTrace.FindMethod("Suspend");
484 case ResumeFunc: return mTrace.FindMethod("Resume");
485 case LoadResourceFunc: return mTrace.FindMethod("LoadResource");
486 case SaveResourceFunc: return mTrace.FindMethod("SaveResource");
487 case LoadFileFunc: return mTrace.FindMethod("LoadFile");
488 case SaveFileFunc: return mTrace.FindMethod("SaveFile");
489 case CancelLoadFunc: return mTrace.FindMethod("CancelLoad");
490 case GetResourcesFunc: return mTrace.FindMethod("GetResources");
491 case IsLoadingFunc: return mTrace.FindMethod("IsLoading");
492 case GetDefaultFontFamilyFunc: return mTrace.FindMethod("GetDefaultFontFamily");
493 case GetDefaultFontSizeFunc: return mTrace.FindMethod("GetDefaultFontSize");
494 case GetFontLineHeightFromCapsHeightFunc: return mTrace.FindMethod("GetFontLineHeightFromCapsHeight");
495 case GetGlyphDataFunc: return mTrace.FindMethod("GetGlyphData");
496 case GetCachedGlyphDataFunc: return mTrace.FindMethod("GetCachedGlyphData");
497 case GetFontPathFunc: return mTrace.FindMethod("GetFontPath");
498 case SetDpiFunc: return mTrace.FindMethod("SetDpi");
499 case JoinLoaderThreadsFunc: return mTrace.FindMethod("JoinLoaderThreads");
500 case GetFontFamilyForCharsFunc: return mTrace.FindMethod("GetFontFamilyForChars");
501 case AllGlyphsSupportedFunc: return mTrace.FindMethod("AllGlyphsSupported");
502 case ValidateFontFamilyNameFunc: return mTrace.FindMethod("ValidateFontFamilyName");
503 case UpdateDefaultsFromDeviceFunc: return mTrace.FindMethod("UpdateDefaultsFromDevice");
504 case GetDynamicsFactoryFunc: return mTrace.FindMethod("GetDynamicsFactory");
505 case ValidateGetFontListFunc: return mTrace.FindMethod("ValidateGetFontList");
506 case ReadGlobalMetricsFromCacheFileFunc: return mTrace.FindMethod("ReadGlobalMetricsFromCacheFile");
507 case WriteGlobalMetricsToCacheFileFunc: return mTrace.FindMethod("WriteGlobalMetricsToCacheFile");
508 case ReadMetricsFromCacheFileFunc: return mTrace.FindMethod("ReadMetricsFromCacheFile");
509 case WriteMetricsToCacheFileFunc: return mTrace.FindMethod("WriteMetricsToCacheFile");
514 void TestPlatformAbstraction::SetGetTimeMicrosecondsResult(size_t sec, size_t usec)
517 mMicroSeconds = usec;
520 void TestPlatformAbstraction::IncrementGetTimeResult(size_t milliseconds)
522 mMicroSeconds += milliseconds * 1000u;
523 unsigned int additionalSeconds = mMicroSeconds / 1000000u;
525 mSeconds += additionalSeconds;
526 mMicroSeconds -= additionalSeconds * 1000000u;
529 void TestPlatformAbstraction::SetIsLoadingResult(bool result)
531 mIsLoadingResult = result;
534 void TestPlatformAbstraction::SetGetDefaultFontFamilyResult(std::string result)
536 mGetDefaultFontFamilyResult = result;
539 void TestPlatformAbstraction::SetGetDefaultFontSizeResult(float result)
541 mGetDefaultFontSizeResult = result;
544 void TestPlatformAbstraction::SetGetFontPathResult(std::string& result)
546 mGetFontPathResult = result;
549 void TestPlatformAbstraction::ClearReadyResources()
551 memset(&mResources, 0, sizeof(Resources));
554 void TestPlatformAbstraction::SetResourceLoaded(Integration::ResourceId loadedId,
555 Integration::ResourceTypeId loadedType,
556 Integration::ResourcePointer loadedResource)
558 mResources.loaded = true;
559 mResources.loadedId = loadedId;
560 mResources.loadedType = loadedType;
561 mResources.loadedResource = loadedResource;
564 void TestPlatformAbstraction::SetResourceLoadFailed(Integration::ResourceId id,
565 Integration::ResourceFailure failure)
567 mResources.loadFailed = true;
568 mResources.loadFailedId = id;
569 mResources.loadFailure = failure;
572 void TestPlatformAbstraction::SetResourceSaved(Integration::ResourceId savedId,
573 Integration::ResourceTypeId savedType)
575 mResources.saved = true;
576 mResources.savedId = savedId;
577 mResources.savedType = savedType;
580 void TestPlatformAbstraction::SetResourceSaveFailed(Integration::ResourceId id,
581 Integration::ResourceFailure failure)
583 mResources.saveFailed = true;
584 mResources.saveFailedId = id;
585 mResources.saveFailure = failure;
588 Integration::ResourceRequest* TestPlatformAbstraction::GetRequest()
593 void TestPlatformAbstraction::DiscardRequest()
599 void TestPlatformAbstraction::SetClosestImageSize(const Vector2& size)
604 void TestPlatformAbstraction::SetLoadFileResult( bool result, std::vector< unsigned char >& buffer )
606 mLoadFileResult.loadResult = result;
609 mLoadFileResult.buffer = buffer;
613 void TestPlatformAbstraction::SetSaveFileResult( bool result )
615 mSaveFileResult = result;
618 Integration::PlatformAbstraction::FontListMode TestPlatformAbstraction::GetLastFontListMode( )
620 return mFontListMode;
623 void TestPlatformAbstraction::SetReadGlobalMetricsResult( bool success, Integration::GlobalMetrics& globalMetrics )
625 mReadGlobalMetricsResult = success;
626 mReadGlobalMetrics = globalMetrics;
629 void TestPlatformAbstraction::SetReadMetricsResult( bool success, std::vector<Integration::GlyphMetrics>& glyphMetricsContainer )
631 mReadMetricsResult = success;
632 mReadMetrics = glyphMetricsContainer; // copy