2 * Copyright (c) 2014 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.
18 #include "test-platform-abstraction.h"
19 #include "dali-test-suite-utils.h"
20 #include <dali/integration-api/bitmap.h>
28 TestPlatformAbstraction::TestPlatformAbstraction()
37 TestPlatformAbstraction::~TestPlatformAbstraction()
42 * @copydoc PlatformAbstraction::GetTimeMicroseconds()
44 void TestPlatformAbstraction::GetTimeMicroseconds(unsigned int &seconds, unsigned int µSeconds)
47 microSeconds = mMicroSeconds;
48 mTrace.PushCall("GetTimeMicroseconds", "");
52 * @copydoc PlatformAbstraction::Suspend()
54 void TestPlatformAbstraction::Suspend()
56 mTrace.PushCall("Suspend", "");
60 * @copydoc PlatformAbstraction::Resume()
62 void TestPlatformAbstraction::Resume()
64 mTrace.PushCall("Resume", "");
67 void TestPlatformAbstraction::GetClosestImageSize( const std::string& filename,
68 const ImageAttributes& attributes,
71 closestSize = mClosestSize;
72 mTrace.PushCall("GetClosestImageSize", "");
75 void TestPlatformAbstraction::GetClosestImageSize( Integration::ResourcePointer resourceBuffer,
76 const ImageAttributes& attributes,
79 closestSize = mClosestSize;
80 mTrace.PushCall("GetClosestImageSize", "");
85 * @copydoc PlatformAbstraction::LoadResource()
87 void TestPlatformAbstraction::LoadResource(const Integration::ResourceRequest& request)
89 std::ostringstream out;
91 if( request.GetType()->id == Integration::ResourceText )
97 out << request.GetType()->id;
99 out << ", Path: " << request.GetPath() << std::endl ;
101 mTrace.PushCall("LoadResource", out.str());
105 tet_infoline ("Warning: multiple resource requests not handled by Test Suite. You may see unexpected errors");
107 mRequest = new Integration::ResourceRequest(request);
110 Integration::ResourcePointer TestPlatformAbstraction::LoadResourceSynchronously( const Integration::ResourceType& resourceType, const std::string& resourcePath )
112 mTrace.PushCall("LoadResourceSynchronously", "");
113 return mResources.loadedResource;
117 * @copydoc PlatformAbstraction::SaveResource()
119 void TestPlatformAbstraction::SaveResource(const Integration::ResourceRequest& request)
121 mTrace.PushCall("SaveResource", "");
125 tet_infoline ("Warning: multiple resource requests not handled by Test Suite. You may see unexpected errors");
127 mRequest = new Integration::ResourceRequest(request);
131 * @copydoc PlatformAbstraction::CancelLoad()
133 void TestPlatformAbstraction::CancelLoad(Integration::ResourceId id, Integration::ResourceTypeId typeId)
135 mTrace.PushCall("CancelLoad", "");
139 * @copydoc PlatformAbstraction::GetResources()
141 void TestPlatformAbstraction::GetResources(Integration::ResourceCache& cache)
143 mTrace.PushCall("GetResources", "");
145 if(mResources.loaded)
147 cache.LoadResponse( mResources.loadedId, mResources.loadedType, mResources.loadedResource, Integration::RESOURCE_COMPLETELY_LOADED );
149 if(mResources.loadFailed)
151 cache.LoadFailed( mResources.loadFailedId, mResources.loadFailure );
155 cache.SaveComplete( mResources.savedId, mResources.savedType );
157 if(mResources.saveFailed)
159 cache.SaveFailed( mResources.saveFailedId, mResources.saveFailure );
164 * @copydoc PlatformAbstraction::IsLoading()
166 bool TestPlatformAbstraction::IsLoading()
168 mTrace.PushCall("IsLoading", "");
169 return mIsLoadingResult;
173 * @copydoc PlatformAbstraction::GetDefaultFontFamily()
175 const std::string& TestPlatformAbstraction::GetDefaultFontFamily() const
177 mTrace.PushCall("GetDefaultFontFamily", "");
178 return mGetDefaultFontFamilyResult;
182 * @copydoc PlatformAbstraction::GetDefaultFontSize()
184 float TestPlatformAbstraction::GetDefaultFontSize() const
186 mTrace.PushCall("GetDefaultFontSize", "");
187 return mGetDefaultFontSizeResult;
190 PixelSize TestPlatformAbstraction::GetFontLineHeightFromCapsHeight(const std::string& fontFamily, const std::string& fontStyle, CapsHeight capsHeight) const
192 mTrace.PushCall("GetFontLineHeightFromCapsHeight", "");
193 // LineHeight will be bigger than CapsHeight, so return capsHeight + 1
194 return PixelSize(capsHeight + 1);
198 * @copydoc PlatformAbstraction::GetGlyphData()
201 Integration::GlyphSet* TestPlatformAbstraction::GetGlyphData ( const Integration::TextResourceType& textRequest,
202 const std::string& fontFamily,
203 bool getBitmap) const
207 mTrace.PushCall("GetGlyphData", "getBitmap:true");
211 mTrace.PushCall("GetGlyphData", "getBitmap:false");
214 // It creates fake metrics for the received characters.
216 Integration::GlyphSet* set = new Dali::Integration::GlyphSet();
217 Integration::BitmapPtr bitmapData;
219 std::set<uint32_t> characters;
221 for( Integration::TextResourceType::CharacterList::const_iterator it = textRequest.mCharacterList.begin(), endIt = textRequest.mCharacterList.end(); it != endIt; ++it )
223 if( characters.find( it->character ) == characters.end() )
225 characters.insert( it->character );
226 Integration::GlyphMetrics character = {it->character, Integration::GlyphMetrics::LOW_QUALITY, 10.0f, 10.0f, 9.0f, 1.0f, 10.0f, it->xPosition, it->yPosition };
230 bitmapData = Integration::Bitmap::New(Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, ResourcePolicy::DISCARD);
231 bitmapData->GetPackedPixelsProfile()->ReserveBuffer(Pixel::A8, 64, 64);
232 PixelBuffer* pixelBuffer = bitmapData->GetBuffer();
233 memset( pixelBuffer, it->character, 64*64 );
236 set->AddCharacter(bitmapData, character);
240 set->mLineHeight = 10.0f;
241 set->mAscender = 9.0f;
242 set->mUnitsPerEM = 2048.0f/64.0f;
243 set->SetAtlasResourceId( textRequest.mTextureAtlasId );
244 set->mFontHash = textRequest.mFontHash;
250 * @copydoc PlatformAbstraction::GetCachedGlyphData()
253 Integration::GlyphSet* TestPlatformAbstraction::GetCachedGlyphData( const Integration::TextResourceType& textRequest,
254 const std::string& fontFamily ) const
256 mTrace.PushCall("GetCachedGlyphData", "");
258 // It creates fake metrics and bitmap for received numeric characters '0' through '9'.
259 Integration::GlyphSet* set = new Dali::Integration::GlyphSet();
260 Integration::BitmapPtr bitmapData;
262 std::set<uint32_t> characters;
264 for( Integration::TextResourceType::CharacterList::const_iterator it = textRequest.mCharacterList.begin(), endIt = textRequest.mCharacterList.end(); it != endIt; ++it )
266 if( it->character >= '0' && it->character <= '9' && characters.find( it->character ) == characters.end() )
268 characters.insert( it->character );
269 Integration::GlyphMetrics character = {it->character, Integration::GlyphMetrics::HIGH_QUALITY, 10.0f, 10.0f, 9.0f, 1.0f, 10.0f, it->xPosition, it->yPosition };
271 bitmapData = Integration::Bitmap::New(Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, ResourcePolicy::DISCARD);
272 bitmapData->GetPackedPixelsProfile()->ReserveBuffer(Pixel::A8, 64, 64);
273 PixelBuffer* pixelBuffer = bitmapData->GetBuffer();
274 memset( pixelBuffer, it->character, 64*64 );
275 set->AddCharacter(bitmapData, character);
279 set->mLineHeight = 10.0f;
280 set->mAscender = 9.0f;
281 set->mUnitsPerEM = 2048.0f/64.0f;
282 set->SetAtlasResourceId( textRequest.mTextureAtlasId );
283 set->mFontHash = textRequest.mFontHash;
290 * @copydoc PlatformAbstraction::GetGlobalMetrics()
292 void TestPlatformAbstraction::GetGlobalMetrics( const std::string& fontFamily,
293 const std::string& fontStyle,
294 Integration::GlobalMetrics& globalMetrics ) const
296 globalMetrics.lineHeight = 10.0f;
297 globalMetrics.ascender = 9.0f;
298 globalMetrics.unitsPerEM = 2048.0f/64.0f;
299 globalMetrics.underlineThickness = 2.f;
300 globalMetrics.underlinePosition = 1.f;
304 * @copydoc PlatformAbstraction::GetFontPath()
306 std::string TestPlatformAbstraction::GetFontPath(const std::string& family, bool bold, bool italic) const
308 mTrace.PushCall("GetFontPath", "");
309 return mGetFontPathResult;
311 // Do nothing with arguments
315 * @copydoc PlatformAbstraction::SetDpi()
317 void TestPlatformAbstraction::SetDpi (unsigned int dpiHorizontal, unsigned int dpiVertical)
319 mTrace.PushCall("SetDpi", "");
323 * @copydoc PlatformAbstraction::GetFontFamilyForChars()
325 const std::string& TestPlatformAbstraction::GetFontFamilyForChars(const Integration::TextArray& charsRequested) const
327 mTrace.PushCall("GetFontFamilyForChars", "");
328 return mGetDefaultFontFamilyResult;
332 * @copydoc PlatformAbstraction::AllGlyphsSupported()
334 bool TestPlatformAbstraction::AllGlyphsSupported(const std::string& name, const std::string& fontStyle, const Integration::TextArray& text) const
336 mTrace.PushCall("AllGlyphsSupported", "");
341 * @copydoc PlatformAbstraction::ValidateFontFamilyName()
343 bool TestPlatformAbstraction::ValidateFontFamilyName(const std::string& fontFamily, const std::string& fontStyle, bool& isDefaultSystemFont, std::string& closestMatch, std::string& closestStyleMatch) const
345 mTrace.PushCall("ValidateFontFamilyName", "");
350 * @copydoc PlatformAbstraction::GetFontList()
352 void TestPlatformAbstraction::GetFontList( PlatformAbstraction::FontListMode mode, std::vector<std::string>& fonstList ) const
354 mFontListMode = mode;
355 mTrace.PushCall("ValidateGetFontList", "");
359 * @copydoc PlatformAbstraction::LoadFile()
361 bool TestPlatformAbstraction::LoadFile( const std::string& filename, std::vector< unsigned char >& buffer ) const
363 mTrace.PushCall("LoadFile", "");
364 if( mLoadFileResult.loadResult )
366 buffer = mLoadFileResult.buffer;
369 return mLoadFileResult.loadResult;
373 * @copydoc PlatformAbstraction::LoadShaderBinFile()
375 bool TestPlatformAbstraction::LoadShaderBinFile( const std::string& filename, std::vector< unsigned char >& buffer ) const
377 mTrace.PushCall("LoadShaderBinFile", "");
378 if( mLoadFileResult.loadResult )
380 buffer = mLoadFileResult.buffer;
383 return mLoadFileResult.loadResult;
387 * @copydoc PlatformAbstraction::SaveFile()
389 bool TestPlatformAbstraction::SaveFile(const std::string& filename, std::vector< unsigned char >& buffer) const
391 mTrace.PushCall("SaveFile", "");
395 void TestPlatformAbstraction::JoinLoaderThreads()
397 mTrace.PushCall("JoinLoaderThreads", "");
400 void TestPlatformAbstraction::UpdateDefaultsFromDevice()
402 mTrace.PushCall("UpdateDefaultsFromDevice", "");
403 mGetDefaultFontFamilyResult+=1.0f;
406 Integration::DynamicsFactory* TestPlatformAbstraction::GetDynamicsFactory()
408 mTrace.PushCall("GetDynamicsFactory", "");
412 bool TestPlatformAbstraction::ReadGlobalMetricsFromCache( const std::string& fontFamily,
413 const std::string& fontStyle,
414 Integration::GlobalMetrics& globalMetrics )
416 mTrace.PushCall("ReadGlobalMetricsFromCacheFile", "");
417 globalMetrics = mReadGlobalMetrics; // Want to copy contents...
418 return mReadGlobalMetricsResult; // Default false (will be set to true on subsequent write)
421 void TestPlatformAbstraction::WriteGlobalMetricsToCache( const std::string& fontFamily,
422 const std::string& fontStyle,
423 const Integration::GlobalMetrics& globalMetrics )
425 // Copy so next read uses written values. TODO: Could add method
426 // to turn this behaviour off for more extensive testing.
427 mReadGlobalMetrics = globalMetrics;
428 mReadGlobalMetricsResult = true;
430 mTrace.PushCall("WriteGlobalMetricsToCacheFile", "");
433 bool TestPlatformAbstraction::ReadMetricsFromCache( const std::string& fontFamily,
434 const std::string& fontStyle,
435 std::vector<Integration::GlyphMetrics>& glyphMetricsContainer )
437 mTrace.PushCall("ReadMetricsFromCacheFile", "");
438 glyphMetricsContainer = mReadMetrics;
439 return mReadMetricsResult; // Default false (will be set to true on subsequent write)
442 void TestPlatformAbstraction::WriteMetricsToCache( const std::string& fontFamily,
443 const std::string& fontStyle,
444 const Integration::GlyphSet& glyphSet )
446 // Copy so next read uses written values. TODO: Could add method
447 // to turn this behaviour off for more extensive testing.
448 const Integration::GlyphSet::CharacterList& charList = glyphSet.GetCharacterList();
449 mReadMetrics.clear();
450 for(std::size_t i=0, end=charList.size(); i<end; ++i)
452 mReadMetrics.push_back(charList[i].second);
454 mReadMetricsResult = true;
456 mTrace.PushCall("WriteMetricsToCacheFile", "");
460 void TestPlatformAbstraction::GetFileNamesFromDirectory( const std::string& directoryName,
461 std::vector<std::string>& fileNames )
463 fileNames.push_back( std::string( "u1f004.png" ) );
464 fileNames.push_back( std::string( "u1f0cf.png" ) );
465 fileNames.push_back( std::string( "u1f170.png" ) );
466 fileNames.push_back( std::string( "u1f601.png" ) );
470 Integration::BitmapPtr TestPlatformAbstraction::GetGlyphImage( const std::string& fontFamily, const std::string& fontStyle, float fontSize, uint32_t character ) const
472 Integration::BitmapPtr image = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, ResourcePolicy::DISCARD );
473 image->GetPackedPixelsProfile()->ReserveBuffer( Pixel::RGBA8888, 1, 1 );
475 mTrace.PushCall("GetGlyphImage", "");
481 /** Call this every test */
482 void TestPlatformAbstraction::Initialize()
486 memset(&mResources, 0, sizeof(Resources));
487 memset(&mReadGlobalMetrics, 0, sizeof(Integration::GlobalMetrics));
490 mIsLoadingResult=false;
491 mGetDefaultFontFamilyResult = "HelveticaNeue";
492 mGetDefaultFontSizeResult=12.0f;
493 mGetFontPathResult="helvetica-12";
494 mReadMetricsResult=false;
495 mReadGlobalMetricsResult=false;
505 bool TestPlatformAbstraction::WasCalled(TestFuncEnum func)
509 case GetTimeMicrosecondsFunc: return mTrace.FindMethod("GetTimeMicroseconds");
510 case SuspendFunc: return mTrace.FindMethod("Suspend");
511 case ResumeFunc: return mTrace.FindMethod("Resume");
512 case LoadResourceFunc: return mTrace.FindMethod("LoadResource");
513 case SaveResourceFunc: return mTrace.FindMethod("SaveResource");
514 case LoadFileFunc: return mTrace.FindMethod("LoadFile");
515 case LoadShaderBinFileFunc: return mTrace.FindMethod("LoadShaderBinFile");
516 case SaveFileFunc: return mTrace.FindMethod("SaveFile");
517 case CancelLoadFunc: return mTrace.FindMethod("CancelLoad");
518 case GetResourcesFunc: return mTrace.FindMethod("GetResources");
519 case IsLoadingFunc: return mTrace.FindMethod("IsLoading");
520 case GetDefaultFontFamilyFunc: return mTrace.FindMethod("GetDefaultFontFamily");
521 case GetDefaultFontSizeFunc: return mTrace.FindMethod("GetDefaultFontSize");
522 case GetFontLineHeightFromCapsHeightFunc: return mTrace.FindMethod("GetFontLineHeightFromCapsHeight");
523 case GetGlyphDataFunc: return mTrace.FindMethod("GetGlyphData");
524 case GetCachedGlyphDataFunc: return mTrace.FindMethod("GetCachedGlyphData");
525 case GetFontPathFunc: return mTrace.FindMethod("GetFontPath");
526 case SetDpiFunc: return mTrace.FindMethod("SetDpi");
527 case JoinLoaderThreadsFunc: return mTrace.FindMethod("JoinLoaderThreads");
528 case GetFontFamilyForCharsFunc: return mTrace.FindMethod("GetFontFamilyForChars");
529 case AllGlyphsSupportedFunc: return mTrace.FindMethod("AllGlyphsSupported");
530 case ValidateFontFamilyNameFunc: return mTrace.FindMethod("ValidateFontFamilyName");
531 case UpdateDefaultsFromDeviceFunc: return mTrace.FindMethod("UpdateDefaultsFromDevice");
532 case GetDynamicsFactoryFunc: return mTrace.FindMethod("GetDynamicsFactory");
533 case ValidateGetFontListFunc: return mTrace.FindMethod("ValidateGetFontList");
534 case ReadGlobalMetricsFromCacheFileFunc: return mTrace.FindMethod("ReadGlobalMetricsFromCacheFile");
535 case WriteGlobalMetricsToCacheFileFunc: return mTrace.FindMethod("WriteGlobalMetricsToCacheFile");
536 case ReadMetricsFromCacheFileFunc: return mTrace.FindMethod("ReadMetricsFromCacheFile");
537 case WriteMetricsToCacheFileFunc: return mTrace.FindMethod("WriteMetricsToCacheFile");
542 void TestPlatformAbstraction::SetGetTimeMicrosecondsResult(size_t sec, size_t usec)
545 mMicroSeconds = usec;
548 void TestPlatformAbstraction::IncrementGetTimeResult(size_t milliseconds)
550 mMicroSeconds += milliseconds * 1000u;
551 unsigned int additionalSeconds = mMicroSeconds / 1000000u;
553 mSeconds += additionalSeconds;
554 mMicroSeconds -= additionalSeconds * 1000000u;
557 void TestPlatformAbstraction::SetIsLoadingResult(bool result)
559 mIsLoadingResult = result;
562 void TestPlatformAbstraction::SetGetDefaultFontFamilyResult(std::string result)
564 mGetDefaultFontFamilyResult = result;
567 void TestPlatformAbstraction::SetGetDefaultFontSizeResult(float result)
569 mGetDefaultFontSizeResult = result;
572 void TestPlatformAbstraction::SetGetFontPathResult(std::string& result)
574 mGetFontPathResult = result;
577 void TestPlatformAbstraction::ClearReadyResources()
579 memset(&mResources, 0, sizeof(Resources));
582 void TestPlatformAbstraction::SetResourceLoaded(Integration::ResourceId loadedId,
583 Integration::ResourceTypeId loadedType,
584 Integration::ResourcePointer loadedResource)
586 mResources.loaded = true;
587 mResources.loadedId = loadedId;
588 mResources.loadedType = loadedType;
589 mResources.loadedResource = loadedResource;
592 void TestPlatformAbstraction::SetResourceLoadFailed(Integration::ResourceId id,
593 Integration::ResourceFailure failure)
595 mResources.loadFailed = true;
596 mResources.loadFailedId = id;
597 mResources.loadFailure = failure;
600 void TestPlatformAbstraction::SetResourceSaved(Integration::ResourceId savedId,
601 Integration::ResourceTypeId savedType)
603 mResources.saved = true;
604 mResources.savedId = savedId;
605 mResources.savedType = savedType;
608 void TestPlatformAbstraction::SetResourceSaveFailed(Integration::ResourceId id,
609 Integration::ResourceFailure failure)
611 mResources.saveFailed = true;
612 mResources.saveFailedId = id;
613 mResources.saveFailure = failure;
616 Integration::ResourceRequest* TestPlatformAbstraction::GetRequest()
621 void TestPlatformAbstraction::DiscardRequest()
627 void TestPlatformAbstraction::SetClosestImageSize(const Vector2& size)
632 void TestPlatformAbstraction::SetLoadFileResult( bool result, std::vector< unsigned char >& buffer )
634 mLoadFileResult.loadResult = result;
637 mLoadFileResult.buffer = buffer;
641 void TestPlatformAbstraction::SetSaveFileResult( bool result )
643 mSaveFileResult = result;
646 Integration::PlatformAbstraction::FontListMode TestPlatformAbstraction::GetLastFontListMode( )
648 return mFontListMode;
651 void TestPlatformAbstraction::SetReadGlobalMetricsResult( bool success, Integration::GlobalMetrics& globalMetrics )
653 mReadGlobalMetricsResult = success;
654 mReadGlobalMetrics = globalMetrics;
657 void TestPlatformAbstraction::SetReadMetricsResult( bool success, std::vector<Integration::GlyphMetrics>& glyphMetricsContainer )
659 mReadMetricsResult = success;
660 mReadMetrics = glyphMetricsContainer; // copy