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/image-data.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 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 = Dali::Integration::NewBitmapImageData( 64, 64, Pixel::A8 );
218 PixelBuffer* pixelBuffer = bitmapData->GetBuffer();
219 memset( pixelBuffer, it->character, 64*64 );
222 set->AddCharacter(bitmapData, character);
226 set->mLineHeight = 10.0f;
227 set->mAscender = 9.0f;
228 set->mUnitsPerEM = 2048.0f/64.0f;
229 set->SetAtlasResourceId( textRequest.mTextureAtlasId );
230 set->mFontHash = textRequest.mFontHash;
236 * @copydoc PlatformAbstraction::GetCachedGlyphData()
239 Integration::GlyphSet* TestPlatformAbstraction::GetCachedGlyphData( const Integration::TextResourceType& textRequest,
240 const std::string& fontFamily ) const
242 mTrace.PushCall("GetCachedGlyphData", "");
244 // It creates fake metrics and bitmap for received numeric characters '0' through '9'.
245 Integration::GlyphSet* set = new Dali::Integration::GlyphSet();
246 Integration::ImageDataPtr bitmapData;
248 std::set<uint32_t> characters;
250 for( Integration::TextResourceType::CharacterList::const_iterator it = textRequest.mCharacterList.begin(), endIt = textRequest.mCharacterList.end(); it != endIt; ++it )
252 if( it->character >= '0' && it->character <= '9' && characters.find( it->character ) == characters.end() )
254 characters.insert( it->character );
255 Integration::GlyphMetrics character = {it->character, Integration::GlyphMetrics::HIGH_QUALITY, 10.0f, 10.0f, 9.0f, 1.0f, 10.0f, it->xPosition, it->yPosition };
257 bitmapData = Dali::Integration::NewBitmapImageData( 64, 64, Pixel::A8 );
258 PixelBuffer* pixelBuffer = bitmapData->GetBuffer();
259 memset( pixelBuffer, it->character, 64*64 );
260 set->AddCharacter(bitmapData, character);
264 set->mLineHeight = 10.0f;
265 set->mAscender = 9.0f;
266 set->mUnitsPerEM = 2048.0f/64.0f;
267 set->SetAtlasResourceId( textRequest.mTextureAtlasId );
268 set->mFontHash = textRequest.mFontHash;
275 * @copydoc PlatformAbstraction::GetGlobalMetrics()
277 void TestPlatformAbstraction::GetGlobalMetrics( const std::string& fontFamily,
278 const std::string& fontStyle,
279 Integration::GlobalMetrics& globalMetrics ) const
281 globalMetrics.lineHeight = 10.0f;
282 globalMetrics.ascender = 9.0f;
283 globalMetrics.unitsPerEM = 2048.0f/64.0f;
284 globalMetrics.underlineThickness = 2.f;
285 globalMetrics.underlinePosition = 1.f;
289 * @copydoc PlatformAbstraction::GetFontPath()
291 std::string TestPlatformAbstraction::GetFontPath(const std::string& family, bool bold, bool italic) const
293 mTrace.PushCall("GetFontPath", "");
294 return mGetFontPathResult;
296 // Do nothing with arguments
300 * @copydoc PlatformAbstraction::SetDpi()
302 void TestPlatformAbstraction::SetDpi (unsigned int dpiHorizontal, unsigned int dpiVertical)
304 mTrace.PushCall("SetDpi", "");
308 * @copydoc PlatformAbstraction::GetFontFamilyForChars()
310 std::string TestPlatformAbstraction::GetFontFamilyForChars(const TextArray& charsRequested) const
312 mTrace.PushCall("GetFontFamilyForChars", "");
313 return mGetDefaultFontFamilyResult;
317 * @copydoc PlatformAbstraction::AllGlyphsSupported()
319 bool TestPlatformAbstraction::AllGlyphsSupported(const std::string& name, const std::string& fontStyle, const TextArray& text) const
321 mTrace.PushCall("AllGlyphsSupported", "");
326 * @copydoc PlatformAbstraction::ValidateFontFamilyName()
328 bool TestPlatformAbstraction::ValidateFontFamilyName(const std::string& fontFamily, const std::string& fontStyle, bool& isDefaultSystemFont, std::string& closestMatch, std::string& closestStyleMatch) const
330 mTrace.PushCall("ValidateFontFamilyName", "");
335 * @copydoc PlatformAbstraction::GetFontList()
337 std::vector<std::string> TestPlatformAbstraction::GetFontList( PlatformAbstraction::FontListMode mode ) const
339 mFontListMode = mode;
340 mTrace.PushCall("ValidateGetFontList", "");
341 std::vector<std::string> fontList;
346 * @copydoc PlatformAbstraction::LoadFile()
348 bool TestPlatformAbstraction::LoadFile( const std::string& filename, std::vector< unsigned char >& buffer ) const
350 mTrace.PushCall("LoadFile", "");
351 if( mLoadFileResult.loadResult )
353 buffer = mLoadFileResult.buffer;
356 return mLoadFileResult.loadResult;
360 * @copydoc PlatformAbstraction::SaveFile()
362 bool TestPlatformAbstraction::SaveFile(const std::string& filename, std::vector< unsigned char >& buffer) const
364 mTrace.PushCall("SaveFile", "");
368 void TestPlatformAbstraction::JoinLoaderThreads()
370 mTrace.PushCall("JoinLoaderThreads", "");
373 void TestPlatformAbstraction::UpdateDefaultsFromDevice()
375 mTrace.PushCall("UpdateDefaultsFromDevice", "");
376 mGetDefaultFontFamilyResult+=1.0f;
379 Integration::DynamicsFactory* TestPlatformAbstraction::GetDynamicsFactory()
381 mTrace.PushCall("GetDynamicsFactory", "");
385 bool TestPlatformAbstraction::ReadGlobalMetricsFromCache( const std::string& fontFamily,
386 const std::string& fontStyle,
387 Integration::GlobalMetrics& globalMetrics )
389 mTrace.PushCall("ReadGlobalMetricsFromCacheFile", "");
390 globalMetrics = mReadGlobalMetrics; // Want to copy contents...
391 return mReadGlobalMetricsResult; // Default false (will be set to true on subsequent write)
394 void TestPlatformAbstraction::WriteGlobalMetricsToCache( const std::string& fontFamily,
395 const std::string& fontStyle,
396 const Integration::GlobalMetrics& globalMetrics )
398 // Copy so next read uses written values. TODO: Could add method
399 // to turn this behaviour off for more extensive testing.
400 mReadGlobalMetrics = globalMetrics;
401 mReadGlobalMetricsResult = true;
403 mTrace.PushCall("WriteGlobalMetricsToCacheFile", "");
406 bool TestPlatformAbstraction::ReadMetricsFromCache( const std::string& fontFamily,
407 const std::string& fontStyle,
408 std::vector<Integration::GlyphMetrics>& glyphMetricsContainer )
410 mTrace.PushCall("ReadMetricsFromCacheFile", "");
411 glyphMetricsContainer = mReadMetrics;
412 return mReadMetricsResult; // Default false (will be set to true on subsequent write)
415 void TestPlatformAbstraction::WriteMetricsToCache( const std::string& fontFamily,
416 const std::string& fontStyle,
417 const Integration::GlyphSet& glyphSet )
419 // Copy so next read uses written values. TODO: Could add method
420 // to turn this behaviour off for more extensive testing.
421 const Integration::GlyphSet::CharacterList& charList = glyphSet.GetCharacterList();
422 mReadMetrics.clear();
423 for(std::size_t i=0, end=charList.size(); i<end; ++i)
425 mReadMetrics.push_back(charList[i].second);
427 mReadMetricsResult = true;
429 mTrace.PushCall("WriteMetricsToCacheFile", "");
433 void TestPlatformAbstraction::GetFileNamesFromDirectory( const std::string& directoryName,
434 std::vector<std::string>& fileNames )
436 fileNames.push_back( std::string( "u1f004.png" ) );
437 fileNames.push_back( std::string( "u1f0cf.png" ) );
438 fileNames.push_back( std::string( "u1f170.png" ) );
439 fileNames.push_back( std::string( "u1f601.png" ) );
443 Integration::ImageDataPtr TestPlatformAbstraction::GetGlyphImage( const std::string& fontFamily, const std::string& fontStyle, float fontSize, uint32_t character ) const
445 Integration::ImageDataPtr image = Dali::Integration::NewBitmapImageData( 1, 1, Pixel::RGBA8888 );
446 mTrace.PushCall("GetGlyphImage", "");
451 /** Call this every test */
452 void TestPlatformAbstraction::Initialize()
456 memset(&mResources, 0, sizeof(Resources));
457 memset(&mReadGlobalMetrics, 0, sizeof(Integration::GlobalMetrics));
460 mIsLoadingResult=false;
461 mGetDefaultFontFamilyResult = "HelveticaNeue";
462 mGetDefaultFontSizeResult=12.0f;
463 mGetFontPathResult="helvetica-12";
464 mReadMetricsResult=false;
465 mReadGlobalMetricsResult=false;
475 bool TestPlatformAbstraction::WasCalled(TestFuncEnum func)
479 case GetTimeMicrosecondsFunc: return mTrace.FindMethod("GetTimeMicroseconds");
480 case SuspendFunc: return mTrace.FindMethod("Suspend");
481 case ResumeFunc: return mTrace.FindMethod("Resume");
482 case LoadResourceFunc: return mTrace.FindMethod("LoadResource");
483 case SaveResourceFunc: return mTrace.FindMethod("SaveResource");
484 case LoadFileFunc: return mTrace.FindMethod("LoadFile");
485 case SaveFileFunc: return mTrace.FindMethod("SaveFile");
486 case CancelLoadFunc: return mTrace.FindMethod("CancelLoad");
487 case GetResourcesFunc: return mTrace.FindMethod("GetResources");
488 case IsLoadingFunc: return mTrace.FindMethod("IsLoading");
489 case GetDefaultFontFamilyFunc: return mTrace.FindMethod("GetDefaultFontFamily");
490 case GetDefaultFontSizeFunc: return mTrace.FindMethod("GetDefaultFontSize");
491 case GetFontLineHeightFromCapsHeightFunc: return mTrace.FindMethod("GetFontLineHeightFromCapsHeight");
492 case GetGlyphDataFunc: return mTrace.FindMethod("GetGlyphData");
493 case GetCachedGlyphDataFunc: return mTrace.FindMethod("GetCachedGlyphData");
494 case GetFontPathFunc: return mTrace.FindMethod("GetFontPath");
495 case SetDpiFunc: return mTrace.FindMethod("SetDpi");
496 case JoinLoaderThreadsFunc: return mTrace.FindMethod("JoinLoaderThreads");
497 case GetFontFamilyForCharsFunc: return mTrace.FindMethod("GetFontFamilyForChars");
498 case AllGlyphsSupportedFunc: return mTrace.FindMethod("AllGlyphsSupported");
499 case ValidateFontFamilyNameFunc: return mTrace.FindMethod("ValidateFontFamilyName");
500 case UpdateDefaultsFromDeviceFunc: return mTrace.FindMethod("UpdateDefaultsFromDevice");
501 case GetDynamicsFactoryFunc: return mTrace.FindMethod("GetDynamicsFactory");
502 case ValidateGetFontListFunc: return mTrace.FindMethod("ValidateGetFontList");
503 case ReadGlobalMetricsFromCacheFileFunc: return mTrace.FindMethod("ReadGlobalMetricsFromCacheFile");
504 case WriteGlobalMetricsToCacheFileFunc: return mTrace.FindMethod("WriteGlobalMetricsToCacheFile");
505 case ReadMetricsFromCacheFileFunc: return mTrace.FindMethod("ReadMetricsFromCacheFile");
506 case WriteMetricsToCacheFileFunc: return mTrace.FindMethod("WriteMetricsToCacheFile");
511 void TestPlatformAbstraction::SetGetTimeMicrosecondsResult(size_t sec, size_t usec)
514 mMicroSeconds = usec;
517 void TestPlatformAbstraction::IncrementGetTimeResult(size_t milliseconds)
519 mMicroSeconds += milliseconds * 1000u;
520 unsigned int additionalSeconds = mMicroSeconds / 1000000u;
522 mSeconds += additionalSeconds;
523 mMicroSeconds -= additionalSeconds * 1000000u;
526 void TestPlatformAbstraction::SetIsLoadingResult(bool result)
528 mIsLoadingResult = result;
531 void TestPlatformAbstraction::SetGetDefaultFontFamilyResult(std::string result)
533 mGetDefaultFontFamilyResult = result;
536 void TestPlatformAbstraction::SetGetDefaultFontSizeResult(float result)
538 mGetDefaultFontSizeResult = result;
541 void TestPlatformAbstraction::SetGetFontPathResult(std::string& result)
543 mGetFontPathResult = result;
546 void TestPlatformAbstraction::ClearReadyResources()
548 memset(&mResources, 0, sizeof(Resources));
551 void TestPlatformAbstraction::SetResourceLoaded(Integration::ResourceId loadedId,
552 Integration::ResourceTypeId loadedType,
553 Integration::ResourcePointer loadedResource)
555 mResources.loaded = true;
556 mResources.loadedId = loadedId;
557 mResources.loadedType = loadedType;
558 mResources.loadedResource = loadedResource;
561 void TestPlatformAbstraction::SetResourceLoadFailed(Integration::ResourceId id,
562 Integration::ResourceFailure failure)
564 mResources.loadFailed = true;
565 mResources.loadFailedId = id;
566 mResources.loadFailure = failure;
569 void TestPlatformAbstraction::SetResourceSaved(Integration::ResourceId savedId,
570 Integration::ResourceTypeId savedType)
572 mResources.saved = true;
573 mResources.savedId = savedId;
574 mResources.savedType = savedType;
577 void TestPlatformAbstraction::SetResourceSaveFailed(Integration::ResourceId id,
578 Integration::ResourceFailure failure)
580 mResources.saveFailed = true;
581 mResources.saveFailedId = id;
582 mResources.saveFailure = failure;
585 Integration::ResourceRequest* TestPlatformAbstraction::GetRequest()
590 void TestPlatformAbstraction::DiscardRequest()
596 void TestPlatformAbstraction::SetClosestImageSize(const Vector2& size)
601 void TestPlatformAbstraction::SetLoadFileResult( bool result, std::vector< unsigned char >& buffer )
603 mLoadFileResult.loadResult = result;
606 mLoadFileResult.buffer = buffer;
610 void TestPlatformAbstraction::SetSaveFileResult( bool result )
612 mSaveFileResult = result;
615 Integration::PlatformAbstraction::FontListMode TestPlatformAbstraction::GetLastFontListMode( )
617 return mFontListMode;
620 void TestPlatformAbstraction::SetReadGlobalMetricsResult( bool success, Integration::GlobalMetrics& globalMetrics )
622 mReadGlobalMetricsResult = success;
623 mReadGlobalMetrics = globalMetrics;
626 void TestPlatformAbstraction::SetReadMetricsResult( bool success, std::vector<Integration::GlyphMetrics>& glyphMetricsContainer )
628 mReadMetricsResult = success;
629 mReadMetrics = glyphMetricsContainer; // copy