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, true);
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, true);
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::SaveFile()
375 bool TestPlatformAbstraction::SaveFile(const std::string& filename, std::vector< unsigned char >& buffer) const
377 mTrace.PushCall("SaveFile", "");
381 void TestPlatformAbstraction::JoinLoaderThreads()
383 mTrace.PushCall("JoinLoaderThreads", "");
386 void TestPlatformAbstraction::UpdateDefaultsFromDevice()
388 mTrace.PushCall("UpdateDefaultsFromDevice", "");
389 mGetDefaultFontFamilyResult+=1.0f;
392 Integration::DynamicsFactory* TestPlatformAbstraction::GetDynamicsFactory()
394 mTrace.PushCall("GetDynamicsFactory", "");
398 bool TestPlatformAbstraction::ReadGlobalMetricsFromCache( const std::string& fontFamily,
399 const std::string& fontStyle,
400 Integration::GlobalMetrics& globalMetrics )
402 mTrace.PushCall("ReadGlobalMetricsFromCacheFile", "");
403 globalMetrics = mReadGlobalMetrics; // Want to copy contents...
404 return mReadGlobalMetricsResult; // Default false (will be set to true on subsequent write)
407 void TestPlatformAbstraction::WriteGlobalMetricsToCache( const std::string& fontFamily,
408 const std::string& fontStyle,
409 const Integration::GlobalMetrics& globalMetrics )
411 // Copy so next read uses written values. TODO: Could add method
412 // to turn this behaviour off for more extensive testing.
413 mReadGlobalMetrics = globalMetrics;
414 mReadGlobalMetricsResult = true;
416 mTrace.PushCall("WriteGlobalMetricsToCacheFile", "");
419 bool TestPlatformAbstraction::ReadMetricsFromCache( const std::string& fontFamily,
420 const std::string& fontStyle,
421 std::vector<Integration::GlyphMetrics>& glyphMetricsContainer )
423 mTrace.PushCall("ReadMetricsFromCacheFile", "");
424 glyphMetricsContainer = mReadMetrics;
425 return mReadMetricsResult; // Default false (will be set to true on subsequent write)
428 void TestPlatformAbstraction::WriteMetricsToCache( const std::string& fontFamily,
429 const std::string& fontStyle,
430 const Integration::GlyphSet& glyphSet )
432 // Copy so next read uses written values. TODO: Could add method
433 // to turn this behaviour off for more extensive testing.
434 const Integration::GlyphSet::CharacterList& charList = glyphSet.GetCharacterList();
435 mReadMetrics.clear();
436 for(std::size_t i=0, end=charList.size(); i<end; ++i)
438 mReadMetrics.push_back(charList[i].second);
440 mReadMetricsResult = true;
442 mTrace.PushCall("WriteMetricsToCacheFile", "");
446 void TestPlatformAbstraction::GetFileNamesFromDirectory( const std::string& directoryName,
447 std::vector<std::string>& fileNames )
449 fileNames.push_back( std::string( "u1f004.png" ) );
450 fileNames.push_back( std::string( "u1f0cf.png" ) );
451 fileNames.push_back( std::string( "u1f170.png" ) );
452 fileNames.push_back( std::string( "u1f601.png" ) );
456 Integration::BitmapPtr TestPlatformAbstraction::GetGlyphImage( const std::string& fontFamily, const std::string& fontStyle, float fontSize, uint32_t character ) const
458 Integration::BitmapPtr image = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, true );
459 image->GetPackedPixelsProfile()->ReserveBuffer( Pixel::RGBA8888, 1, 1 );
461 mTrace.PushCall("GetGlyphImage", "");
467 /** Call this every test */
468 void TestPlatformAbstraction::Initialize()
472 memset(&mResources, 0, sizeof(Resources));
473 memset(&mReadGlobalMetrics, 0, sizeof(Integration::GlobalMetrics));
476 mIsLoadingResult=false;
477 mGetDefaultFontFamilyResult = "HelveticaNeue";
478 mGetDefaultFontSizeResult=12.0f;
479 mGetFontPathResult="helvetica-12";
480 mReadMetricsResult=false;
481 mReadGlobalMetricsResult=false;
491 bool TestPlatformAbstraction::WasCalled(TestFuncEnum func)
495 case GetTimeMicrosecondsFunc: return mTrace.FindMethod("GetTimeMicroseconds");
496 case SuspendFunc: return mTrace.FindMethod("Suspend");
497 case ResumeFunc: return mTrace.FindMethod("Resume");
498 case LoadResourceFunc: return mTrace.FindMethod("LoadResource");
499 case SaveResourceFunc: return mTrace.FindMethod("SaveResource");
500 case LoadFileFunc: return mTrace.FindMethod("LoadFile");
501 case SaveFileFunc: return mTrace.FindMethod("SaveFile");
502 case CancelLoadFunc: return mTrace.FindMethod("CancelLoad");
503 case GetResourcesFunc: return mTrace.FindMethod("GetResources");
504 case IsLoadingFunc: return mTrace.FindMethod("IsLoading");
505 case GetDefaultFontFamilyFunc: return mTrace.FindMethod("GetDefaultFontFamily");
506 case GetDefaultFontSizeFunc: return mTrace.FindMethod("GetDefaultFontSize");
507 case GetFontLineHeightFromCapsHeightFunc: return mTrace.FindMethod("GetFontLineHeightFromCapsHeight");
508 case GetGlyphDataFunc: return mTrace.FindMethod("GetGlyphData");
509 case GetCachedGlyphDataFunc: return mTrace.FindMethod("GetCachedGlyphData");
510 case GetFontPathFunc: return mTrace.FindMethod("GetFontPath");
511 case SetDpiFunc: return mTrace.FindMethod("SetDpi");
512 case JoinLoaderThreadsFunc: return mTrace.FindMethod("JoinLoaderThreads");
513 case GetFontFamilyForCharsFunc: return mTrace.FindMethod("GetFontFamilyForChars");
514 case AllGlyphsSupportedFunc: return mTrace.FindMethod("AllGlyphsSupported");
515 case ValidateFontFamilyNameFunc: return mTrace.FindMethod("ValidateFontFamilyName");
516 case UpdateDefaultsFromDeviceFunc: return mTrace.FindMethod("UpdateDefaultsFromDevice");
517 case GetDynamicsFactoryFunc: return mTrace.FindMethod("GetDynamicsFactory");
518 case ValidateGetFontListFunc: return mTrace.FindMethod("ValidateGetFontList");
519 case ReadGlobalMetricsFromCacheFileFunc: return mTrace.FindMethod("ReadGlobalMetricsFromCacheFile");
520 case WriteGlobalMetricsToCacheFileFunc: return mTrace.FindMethod("WriteGlobalMetricsToCacheFile");
521 case ReadMetricsFromCacheFileFunc: return mTrace.FindMethod("ReadMetricsFromCacheFile");
522 case WriteMetricsToCacheFileFunc: return mTrace.FindMethod("WriteMetricsToCacheFile");
527 void TestPlatformAbstraction::SetGetTimeMicrosecondsResult(size_t sec, size_t usec)
530 mMicroSeconds = usec;
533 void TestPlatformAbstraction::IncrementGetTimeResult(size_t milliseconds)
535 mMicroSeconds += milliseconds * 1000u;
536 unsigned int additionalSeconds = mMicroSeconds / 1000000u;
538 mSeconds += additionalSeconds;
539 mMicroSeconds -= additionalSeconds * 1000000u;
542 void TestPlatformAbstraction::SetIsLoadingResult(bool result)
544 mIsLoadingResult = result;
547 void TestPlatformAbstraction::SetGetDefaultFontFamilyResult(std::string result)
549 mGetDefaultFontFamilyResult = result;
552 void TestPlatformAbstraction::SetGetDefaultFontSizeResult(float result)
554 mGetDefaultFontSizeResult = result;
557 void TestPlatformAbstraction::SetGetFontPathResult(std::string& result)
559 mGetFontPathResult = result;
562 void TestPlatformAbstraction::ClearReadyResources()
564 memset(&mResources, 0, sizeof(Resources));
567 void TestPlatformAbstraction::SetResourceLoaded(Integration::ResourceId loadedId,
568 Integration::ResourceTypeId loadedType,
569 Integration::ResourcePointer loadedResource)
571 mResources.loaded = true;
572 mResources.loadedId = loadedId;
573 mResources.loadedType = loadedType;
574 mResources.loadedResource = loadedResource;
577 void TestPlatformAbstraction::SetResourceLoadFailed(Integration::ResourceId id,
578 Integration::ResourceFailure failure)
580 mResources.loadFailed = true;
581 mResources.loadFailedId = id;
582 mResources.loadFailure = failure;
585 void TestPlatformAbstraction::SetResourceSaved(Integration::ResourceId savedId,
586 Integration::ResourceTypeId savedType)
588 mResources.saved = true;
589 mResources.savedId = savedId;
590 mResources.savedType = savedType;
593 void TestPlatformAbstraction::SetResourceSaveFailed(Integration::ResourceId id,
594 Integration::ResourceFailure failure)
596 mResources.saveFailed = true;
597 mResources.saveFailedId = id;
598 mResources.saveFailure = failure;
601 Integration::ResourceRequest* TestPlatformAbstraction::GetRequest()
606 void TestPlatformAbstraction::DiscardRequest()
612 void TestPlatformAbstraction::SetClosestImageSize(const Vector2& size)
617 void TestPlatformAbstraction::SetLoadFileResult( bool result, std::vector< unsigned char >& buffer )
619 mLoadFileResult.loadResult = result;
622 mLoadFileResult.buffer = buffer;
626 void TestPlatformAbstraction::SetSaveFileResult( bool result )
628 mSaveFileResult = result;
631 Integration::PlatformAbstraction::FontListMode TestPlatformAbstraction::GetLastFontListMode( )
633 return mFontListMode;
636 void TestPlatformAbstraction::SetReadGlobalMetricsResult( bool success, Integration::GlobalMetrics& globalMetrics )
638 mReadGlobalMetricsResult = success;
639 mReadGlobalMetrics = globalMetrics;
642 void TestPlatformAbstraction::SetReadMetricsResult( bool success, std::vector<Integration::GlyphMetrics>& glyphMetricsContainer )
644 mReadMetricsResult = success;
645 mReadMetrics = glyphMetricsContainer; // copy