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 mTrace.PushCall("LoadResource", "");
93 tet_infoline ("Warning: multiple resource requests not handled by Test Suite. You may see unexpected errors");
95 mRequest = new Integration::ResourceRequest(request);
98 Integration::ResourcePointer TestPlatformAbstraction::LoadResourceSynchronously( const Integration::ResourceType& resourceType, const std::string& resourcePath )
100 mTrace.PushCall("LoadResourceSynchronously", "");
101 return mResources.loadedResource;
105 * @copydoc PlatformAbstraction::SaveResource()
107 void TestPlatformAbstraction::SaveResource(const Integration::ResourceRequest& request)
109 mTrace.PushCall("SaveResource", "");
113 tet_infoline ("Warning: multiple resource requests not handled by Test Suite. You may see unexpected errors");
115 mRequest = new Integration::ResourceRequest(request);
119 * @copydoc PlatformAbstraction::CancelLoad()
121 void TestPlatformAbstraction::CancelLoad(Integration::ResourceId id, Integration::ResourceTypeId typeId)
123 mTrace.PushCall("CancelLoad", "");
127 * @copydoc PlatformAbstraction::GetResources()
129 void TestPlatformAbstraction::GetResources(Integration::ResourceCache& cache)
131 mTrace.PushCall("GetResources", "");
133 if(mResources.loaded)
135 cache.LoadResponse( mResources.loadedId, mResources.loadedType, mResources.loadedResource, Integration::RESOURCE_COMPLETELY_LOADED );
137 if(mResources.loadFailed)
139 cache.LoadFailed( mResources.loadFailedId, mResources.loadFailure );
143 cache.SaveComplete( mResources.savedId, mResources.savedType );
145 if(mResources.saveFailed)
147 cache.SaveFailed( mResources.saveFailedId, mResources.saveFailure );
152 * @copydoc PlatformAbstraction::IsLoading()
154 bool TestPlatformAbstraction::IsLoading()
156 mTrace.PushCall("IsLoading", "");
157 return mIsLoadingResult;
161 * @copydoc PlatformAbstraction::GetDefaultFontFamily()
163 const std::string& TestPlatformAbstraction::GetDefaultFontFamily() const
165 mTrace.PushCall("GetDefaultFontFamily", "");
166 return mGetDefaultFontFamilyResult;
170 * @copydoc PlatformAbstraction::GetDefaultFontSize()
172 float TestPlatformAbstraction::GetDefaultFontSize() const
174 mTrace.PushCall("GetDefaultFontSize", "");
175 return mGetDefaultFontSizeResult;
178 PixelSize TestPlatformAbstraction::GetFontLineHeightFromCapsHeight(const std::string& fontFamily, const std::string& fontStyle, CapsHeight capsHeight) const
180 mTrace.PushCall("GetFontLineHeightFromCapsHeight", "");
181 // LineHeight will be bigger than CapsHeight, so return capsHeight + 1
182 return PixelSize(capsHeight + 1);
186 * @copydoc PlatformAbstraction::GetGlyphData()
189 Integration::GlyphSet* TestPlatformAbstraction::GetGlyphData ( const Integration::TextResourceType& textRequest,
190 const std::string& fontFamily,
191 bool getBitmap) const
195 mTrace.PushCall("GetGlyphData", "getBitmap:true");
199 mTrace.PushCall("GetGlyphData", "getBitmap:false");
202 // It creates fake metrics for the received characters.
204 Integration::GlyphSet* set = new Dali::Integration::GlyphSet();
205 Integration::BitmapPtr bitmapData;
207 std::set<uint32_t> characters;
209 for( Integration::TextResourceType::CharacterList::const_iterator it = textRequest.mCharacterList.begin(), endIt = textRequest.mCharacterList.end(); it != endIt; ++it )
211 if( characters.find( it->character ) == characters.end() )
213 characters.insert( it->character );
214 Integration::GlyphMetrics character = {it->character, Integration::GlyphMetrics::LOW_QUALITY, 10.0f, 10.0f, 9.0f, 1.0f, 10.0f, it->xPosition, it->yPosition };
218 bitmapData = Integration::Bitmap::New(Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, true);
219 bitmapData->GetPackedPixelsProfile()->ReserveBuffer(Pixel::A8, 64, 64);
220 PixelBuffer* pixelBuffer = bitmapData->GetBuffer();
221 memset( pixelBuffer, it->character, 64*64 );
224 set->AddCharacter(bitmapData, character);
228 set->mLineHeight = 10.0f;
229 set->mAscender = 9.0f;
230 set->mUnitsPerEM = 2048.0f/64.0f;
231 set->SetAtlasResourceId( textRequest.mTextureAtlasId );
232 set->mFontHash = textRequest.mFontHash;
238 * @copydoc PlatformAbstraction::GetCachedGlyphData()
241 Integration::GlyphSet* TestPlatformAbstraction::GetCachedGlyphData( const Integration::TextResourceType& textRequest,
242 const std::string& fontFamily ) const
244 mTrace.PushCall("GetCachedGlyphData", "");
246 // It creates fake metrics and bitmap for received numeric characters '0' through '9'.
247 Integration::GlyphSet* set = new Dali::Integration::GlyphSet();
248 Integration::BitmapPtr bitmapData;
250 std::set<uint32_t> characters;
252 for( Integration::TextResourceType::CharacterList::const_iterator it = textRequest.mCharacterList.begin(), endIt = textRequest.mCharacterList.end(); it != endIt; ++it )
254 if( it->character >= '0' && it->character <= '9' && characters.find( it->character ) == characters.end() )
256 characters.insert( it->character );
257 Integration::GlyphMetrics character = {it->character, Integration::GlyphMetrics::HIGH_QUALITY, 10.0f, 10.0f, 9.0f, 1.0f, 10.0f, it->xPosition, it->yPosition };
259 bitmapData = Integration::Bitmap::New(Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, true);
260 bitmapData->GetPackedPixelsProfile()->ReserveBuffer(Pixel::A8, 64, 64);
261 PixelBuffer* pixelBuffer = bitmapData->GetBuffer();
262 memset( pixelBuffer, it->character, 64*64 );
263 set->AddCharacter(bitmapData, character);
267 set->mLineHeight = 10.0f;
268 set->mAscender = 9.0f;
269 set->mUnitsPerEM = 2048.0f/64.0f;
270 set->SetAtlasResourceId( textRequest.mTextureAtlasId );
271 set->mFontHash = textRequest.mFontHash;
278 * @copydoc PlatformAbstraction::GetGlobalMetrics()
280 void TestPlatformAbstraction::GetGlobalMetrics( const std::string& fontFamily,
281 const std::string& fontStyle,
282 Integration::GlobalMetrics& globalMetrics ) const
284 globalMetrics.lineHeight = 10.0f;
285 globalMetrics.ascender = 9.0f;
286 globalMetrics.unitsPerEM = 2048.0f/64.0f;
287 globalMetrics.underlineThickness = 2.f;
288 globalMetrics.underlinePosition = 1.f;
292 * @copydoc PlatformAbstraction::GetFontPath()
294 std::string TestPlatformAbstraction::GetFontPath(const std::string& family, bool bold, bool italic) const
296 mTrace.PushCall("GetFontPath", "");
297 return mGetFontPathResult;
299 // Do nothing with arguments
303 * @copydoc PlatformAbstraction::SetDpi()
305 void TestPlatformAbstraction::SetDpi (unsigned int dpiHorizontal, unsigned int dpiVertical)
307 mTrace.PushCall("SetDpi", "");
311 * @copydoc PlatformAbstraction::GetFontFamilyForChars()
313 const std::string& TestPlatformAbstraction::GetFontFamilyForChars(const TextArray& charsRequested) const
315 mTrace.PushCall("GetFontFamilyForChars", "");
316 return mGetDefaultFontFamilyResult;
320 * @copydoc PlatformAbstraction::AllGlyphsSupported()
322 bool TestPlatformAbstraction::AllGlyphsSupported(const std::string& name, const std::string& fontStyle, const TextArray& text) const
324 mTrace.PushCall("AllGlyphsSupported", "");
329 * @copydoc PlatformAbstraction::ValidateFontFamilyName()
331 bool TestPlatformAbstraction::ValidateFontFamilyName(const std::string& fontFamily, const std::string& fontStyle, bool& isDefaultSystemFont, std::string& closestMatch, std::string& closestStyleMatch) const
333 mTrace.PushCall("ValidateFontFamilyName", "");
338 * @copydoc PlatformAbstraction::GetFontList()
340 void TestPlatformAbstraction::GetFontList( PlatformAbstraction::FontListMode mode, std::vector<std::string>& fonstList ) const
342 mFontListMode = mode;
343 mTrace.PushCall("ValidateGetFontList", "");
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::BitmapPtr TestPlatformAbstraction::GetGlyphImage( const std::string& fontFamily, const std::string& fontStyle, float fontSize, uint32_t character ) const
446 Integration::BitmapPtr image = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, true );
447 image->GetPackedPixelsProfile()->ReserveBuffer( Pixel::RGBA8888, 1, 1 );
449 mTrace.PushCall("GetGlyphImage", "");
455 /** Call this every test */
456 void TestPlatformAbstraction::Initialize()
460 memset(&mResources, 0, sizeof(Resources));
461 memset(&mReadGlobalMetrics, 0, sizeof(Integration::GlobalMetrics));
464 mIsLoadingResult=false;
465 mGetDefaultFontFamilyResult = "HelveticaNeue";
466 mGetDefaultFontSizeResult=12.0f;
467 mGetFontPathResult="helvetica-12";
468 mReadMetricsResult=false;
469 mReadGlobalMetricsResult=false;
479 bool TestPlatformAbstraction::WasCalled(TestFuncEnum func)
483 case GetTimeMicrosecondsFunc: return mTrace.FindMethod("GetTimeMicroseconds");
484 case SuspendFunc: return mTrace.FindMethod("Suspend");
485 case ResumeFunc: return mTrace.FindMethod("Resume");
486 case LoadResourceFunc: return mTrace.FindMethod("LoadResource");
487 case SaveResourceFunc: return mTrace.FindMethod("SaveResource");
488 case LoadFileFunc: return mTrace.FindMethod("LoadFile");
489 case SaveFileFunc: return mTrace.FindMethod("SaveFile");
490 case CancelLoadFunc: return mTrace.FindMethod("CancelLoad");
491 case GetResourcesFunc: return mTrace.FindMethod("GetResources");
492 case IsLoadingFunc: return mTrace.FindMethod("IsLoading");
493 case GetDefaultFontFamilyFunc: return mTrace.FindMethod("GetDefaultFontFamily");
494 case GetDefaultFontSizeFunc: return mTrace.FindMethod("GetDefaultFontSize");
495 case GetFontLineHeightFromCapsHeightFunc: return mTrace.FindMethod("GetFontLineHeightFromCapsHeight");
496 case GetGlyphDataFunc: return mTrace.FindMethod("GetGlyphData");
497 case GetCachedGlyphDataFunc: return mTrace.FindMethod("GetCachedGlyphData");
498 case GetFontPathFunc: return mTrace.FindMethod("GetFontPath");
499 case SetDpiFunc: return mTrace.FindMethod("SetDpi");
500 case JoinLoaderThreadsFunc: return mTrace.FindMethod("JoinLoaderThreads");
501 case GetFontFamilyForCharsFunc: return mTrace.FindMethod("GetFontFamilyForChars");
502 case AllGlyphsSupportedFunc: return mTrace.FindMethod("AllGlyphsSupported");
503 case ValidateFontFamilyNameFunc: return mTrace.FindMethod("ValidateFontFamilyName");
504 case UpdateDefaultsFromDeviceFunc: return mTrace.FindMethod("UpdateDefaultsFromDevice");
505 case GetDynamicsFactoryFunc: return mTrace.FindMethod("GetDynamicsFactory");
506 case ValidateGetFontListFunc: return mTrace.FindMethod("ValidateGetFontList");
507 case ReadGlobalMetricsFromCacheFileFunc: return mTrace.FindMethod("ReadGlobalMetricsFromCacheFile");
508 case WriteGlobalMetricsToCacheFileFunc: return mTrace.FindMethod("WriteGlobalMetricsToCacheFile");
509 case ReadMetricsFromCacheFileFunc: return mTrace.FindMethod("ReadMetricsFromCacheFile");
510 case WriteMetricsToCacheFileFunc: return mTrace.FindMethod("WriteMetricsToCacheFile");
515 void TestPlatformAbstraction::SetGetTimeMicrosecondsResult(size_t sec, size_t usec)
518 mMicroSeconds = usec;
521 void TestPlatformAbstraction::IncrementGetTimeResult(size_t milliseconds)
523 mMicroSeconds += milliseconds * 1000u;
524 unsigned int additionalSeconds = mMicroSeconds / 1000000u;
526 mSeconds += additionalSeconds;
527 mMicroSeconds -= additionalSeconds * 1000000u;
530 void TestPlatformAbstraction::SetIsLoadingResult(bool result)
532 mIsLoadingResult = result;
535 void TestPlatformAbstraction::SetGetDefaultFontFamilyResult(std::string result)
537 mGetDefaultFontFamilyResult = result;
540 void TestPlatformAbstraction::SetGetDefaultFontSizeResult(float result)
542 mGetDefaultFontSizeResult = result;
545 void TestPlatformAbstraction::SetGetFontPathResult(std::string& result)
547 mGetFontPathResult = result;
550 void TestPlatformAbstraction::ClearReadyResources()
552 memset(&mResources, 0, sizeof(Resources));
555 void TestPlatformAbstraction::SetResourceLoaded(Integration::ResourceId loadedId,
556 Integration::ResourceTypeId loadedType,
557 Integration::ResourcePointer loadedResource)
559 mResources.loaded = true;
560 mResources.loadedId = loadedId;
561 mResources.loadedType = loadedType;
562 mResources.loadedResource = loadedResource;
565 void TestPlatformAbstraction::SetResourceLoadFailed(Integration::ResourceId id,
566 Integration::ResourceFailure failure)
568 mResources.loadFailed = true;
569 mResources.loadFailedId = id;
570 mResources.loadFailure = failure;
573 void TestPlatformAbstraction::SetResourceSaved(Integration::ResourceId savedId,
574 Integration::ResourceTypeId savedType)
576 mResources.saved = true;
577 mResources.savedId = savedId;
578 mResources.savedType = savedType;
581 void TestPlatformAbstraction::SetResourceSaveFailed(Integration::ResourceId id,
582 Integration::ResourceFailure failure)
584 mResources.saveFailed = true;
585 mResources.saveFailedId = id;
586 mResources.saveFailure = failure;
589 Integration::ResourceRequest* TestPlatformAbstraction::GetRequest()
594 void TestPlatformAbstraction::DiscardRequest()
600 void TestPlatformAbstraction::SetClosestImageSize(const Vector2& size)
605 void TestPlatformAbstraction::SetLoadFileResult( bool result, std::vector< unsigned char >& buffer )
607 mLoadFileResult.loadResult = result;
610 mLoadFileResult.buffer = buffer;
614 void TestPlatformAbstraction::SetSaveFileResult( bool result )
616 mSaveFileResult = result;
619 Integration::PlatformAbstraction::FontListMode TestPlatformAbstraction::GetLastFontListMode( )
621 return mFontListMode;
624 void TestPlatformAbstraction::SetReadGlobalMetricsResult( bool success, Integration::GlobalMetrics& globalMetrics )
626 mReadGlobalMetricsResult = success;
627 mReadGlobalMetrics = globalMetrics;
630 void TestPlatformAbstraction::SetReadMetricsResult( bool success, std::vector<Integration::GlyphMetrics>& glyphMetricsContainer )
632 mReadMetricsResult = success;
633 mReadMetrics = glyphMetricsContainer; // copy