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 std::string TestPlatformAbstraction::GetDefaultFontFamily() const
165 mTrace.PushCall("GetDefaultFontFamily", "");
166 return mGetDefaultFontFamilyResult;
170 * @copydoc PlatformAbstraction::GetDefaultFontSize()
172 const float TestPlatformAbstraction::GetDefaultFontSize() const
174 mTrace.PushCall("GetDefaultFontSize", "");
175 return mGetDefaultFontSizeResult;
178 const PixelSize TestPlatformAbstraction::GetFontLineHeightFromCapsHeight(const std::string& fontFamily, const std::string& fontStyle, const 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 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 std::vector<std::string> TestPlatformAbstraction::GetFontList( PlatformAbstraction::FontListMode mode ) const
342 mFontListMode = mode;
343 mTrace.PushCall("ValidateGetFontList", "");
344 std::vector<std::string> fontList;
349 * @copydoc PlatformAbstraction::LoadFile()
351 bool TestPlatformAbstraction::LoadFile( const std::string& filename, std::vector< unsigned char >& buffer ) const
353 mTrace.PushCall("LoadFile", "");
354 if( mLoadFileResult.loadResult )
356 buffer = mLoadFileResult.buffer;
359 return mLoadFileResult.loadResult;
363 * @copydoc PlatformAbstraction::SaveFile()
365 bool TestPlatformAbstraction::SaveFile(const std::string& filename, std::vector< unsigned char >& buffer) const
367 mTrace.PushCall("SaveFile", "");
371 void TestPlatformAbstraction::JoinLoaderThreads()
373 mTrace.PushCall("JoinLoaderThreads", "");
376 void TestPlatformAbstraction::UpdateDefaultsFromDevice()
378 mTrace.PushCall("UpdateDefaultsFromDevice", "");
379 mGetDefaultFontFamilyResult+=1.0f;
382 Integration::DynamicsFactory* TestPlatformAbstraction::GetDynamicsFactory()
384 mTrace.PushCall("GetDynamicsFactory", "");
388 bool TestPlatformAbstraction::ReadGlobalMetricsFromCache( const std::string& fontFamily,
389 const std::string& fontStyle,
390 Integration::GlobalMetrics& globalMetrics )
392 mTrace.PushCall("ReadGlobalMetricsFromCacheFile", "");
393 globalMetrics = mReadGlobalMetrics; // Want to copy contents...
394 return mReadGlobalMetricsResult; // Default false (will be set to true on subsequent write)
397 void TestPlatformAbstraction::WriteGlobalMetricsToCache( const std::string& fontFamily,
398 const std::string& fontStyle,
399 const Integration::GlobalMetrics& globalMetrics )
401 // Copy so next read uses written values. TODO: Could add method
402 // to turn this behaviour off for more extensive testing.
403 mReadGlobalMetrics = globalMetrics;
404 mReadGlobalMetricsResult = true;
406 mTrace.PushCall("WriteGlobalMetricsToCacheFile", "");
409 bool TestPlatformAbstraction::ReadMetricsFromCache( const std::string& fontFamily,
410 const std::string& fontStyle,
411 std::vector<Integration::GlyphMetrics>& glyphMetricsContainer )
413 mTrace.PushCall("ReadMetricsFromCacheFile", "");
414 glyphMetricsContainer = mReadMetrics;
415 return mReadMetricsResult; // Default false (will be set to true on subsequent write)
418 void TestPlatformAbstraction::WriteMetricsToCache( const std::string& fontFamily,
419 const std::string& fontStyle,
420 const Integration::GlyphSet& glyphSet )
422 // Copy so next read uses written values. TODO: Could add method
423 // to turn this behaviour off for more extensive testing.
424 const Integration::GlyphSet::CharacterList& charList = glyphSet.GetCharacterList();
425 mReadMetrics.clear();
426 for(std::size_t i=0, end=charList.size(); i<end; ++i)
428 mReadMetrics.push_back(charList[i].second);
430 mReadMetricsResult = true;
432 mTrace.PushCall("WriteMetricsToCacheFile", "");
436 void TestPlatformAbstraction::GetFileNamesFromDirectory( const std::string& directoryName,
437 std::vector<std::string>& fileNames )
439 fileNames.push_back( std::string( "u1f004.png" ) );
440 fileNames.push_back( std::string( "u1f0cf.png" ) );
441 fileNames.push_back( std::string( "u1f170.png" ) );
442 fileNames.push_back( std::string( "u1f601.png" ) );
446 Integration::BitmapPtr TestPlatformAbstraction::GetGlyphImage( const std::string& fontFamily, const std::string& fontStyle, float fontSize, uint32_t character ) const
448 Integration::BitmapPtr image = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, true );
449 image->GetPackedPixelsProfile()->ReserveBuffer( Pixel::RGBA8888, 1, 1 );
451 mTrace.PushCall("GetGlyphImage", "");
457 /** Call this every test */
458 void TestPlatformAbstraction::Initialize()
462 memset(&mResources, 0, sizeof(Resources));
463 memset(&mReadGlobalMetrics, 0, sizeof(Integration::GlobalMetrics));
466 mIsLoadingResult=false;
467 mGetDefaultFontFamilyResult = "HelveticaNeue";
468 mGetDefaultFontSizeResult=12.0f;
469 mGetFontPathResult="helvetica-12";
470 mReadMetricsResult=false;
471 mReadGlobalMetricsResult=false;
481 bool TestPlatformAbstraction::WasCalled(TestFuncEnum func)
485 case GetTimeMicrosecondsFunc: return mTrace.FindMethod("GetTimeMicroseconds");
486 case SuspendFunc: return mTrace.FindMethod("Suspend");
487 case ResumeFunc: return mTrace.FindMethod("Resume");
488 case LoadResourceFunc: return mTrace.FindMethod("LoadResource");
489 case SaveResourceFunc: return mTrace.FindMethod("SaveResource");
490 case LoadFileFunc: return mTrace.FindMethod("LoadFile");
491 case SaveFileFunc: return mTrace.FindMethod("SaveFile");
492 case CancelLoadFunc: return mTrace.FindMethod("CancelLoad");
493 case GetResourcesFunc: return mTrace.FindMethod("GetResources");
494 case IsLoadingFunc: return mTrace.FindMethod("IsLoading");
495 case GetDefaultFontFamilyFunc: return mTrace.FindMethod("GetDefaultFontFamily");
496 case GetDefaultFontSizeFunc: return mTrace.FindMethod("GetDefaultFontSize");
497 case GetFontLineHeightFromCapsHeightFunc: return mTrace.FindMethod("GetFontLineHeightFromCapsHeight");
498 case GetGlyphDataFunc: return mTrace.FindMethod("GetGlyphData");
499 case GetCachedGlyphDataFunc: return mTrace.FindMethod("GetCachedGlyphData");
500 case GetFontPathFunc: return mTrace.FindMethod("GetFontPath");
501 case SetDpiFunc: return mTrace.FindMethod("SetDpi");
502 case JoinLoaderThreadsFunc: return mTrace.FindMethod("JoinLoaderThreads");
503 case GetFontFamilyForCharsFunc: return mTrace.FindMethod("GetFontFamilyForChars");
504 case AllGlyphsSupportedFunc: return mTrace.FindMethod("AllGlyphsSupported");
505 case ValidateFontFamilyNameFunc: return mTrace.FindMethod("ValidateFontFamilyName");
506 case UpdateDefaultsFromDeviceFunc: return mTrace.FindMethod("UpdateDefaultsFromDevice");
507 case GetDynamicsFactoryFunc: return mTrace.FindMethod("GetDynamicsFactory");
508 case ValidateGetFontListFunc: return mTrace.FindMethod("ValidateGetFontList");
509 case ReadGlobalMetricsFromCacheFileFunc: return mTrace.FindMethod("ReadGlobalMetricsFromCacheFile");
510 case WriteGlobalMetricsToCacheFileFunc: return mTrace.FindMethod("WriteGlobalMetricsToCacheFile");
511 case ReadMetricsFromCacheFileFunc: return mTrace.FindMethod("ReadMetricsFromCacheFile");
512 case WriteMetricsToCacheFileFunc: return mTrace.FindMethod("WriteMetricsToCacheFile");
517 void TestPlatformAbstraction::SetGetTimeMicrosecondsResult(size_t sec, size_t usec)
520 mMicroSeconds = usec;
523 void TestPlatformAbstraction::IncrementGetTimeResult(size_t milliseconds)
525 mMicroSeconds += milliseconds * 1000u;
526 unsigned int additionalSeconds = mMicroSeconds / 1000000u;
528 mSeconds += additionalSeconds;
529 mMicroSeconds -= additionalSeconds * 1000000u;
532 void TestPlatformAbstraction::SetIsLoadingResult(bool result)
534 mIsLoadingResult = result;
537 void TestPlatformAbstraction::SetGetDefaultFontFamilyResult(std::string result)
539 mGetDefaultFontFamilyResult = result;
542 void TestPlatformAbstraction::SetGetDefaultFontSizeResult(float result)
544 mGetDefaultFontSizeResult = result;
547 void TestPlatformAbstraction::SetGetFontPathResult(std::string& result)
549 mGetFontPathResult = result;
552 void TestPlatformAbstraction::ClearReadyResources()
554 memset(&mResources, 0, sizeof(Resources));
557 void TestPlatformAbstraction::SetResourceLoaded(Integration::ResourceId loadedId,
558 Integration::ResourceTypeId loadedType,
559 Integration::ResourcePointer loadedResource)
561 mResources.loaded = true;
562 mResources.loadedId = loadedId;
563 mResources.loadedType = loadedType;
564 mResources.loadedResource = loadedResource;
567 void TestPlatformAbstraction::SetResourceLoadFailed(Integration::ResourceId id,
568 Integration::ResourceFailure failure)
570 mResources.loadFailed = true;
571 mResources.loadFailedId = id;
572 mResources.loadFailure = failure;
575 void TestPlatformAbstraction::SetResourceSaved(Integration::ResourceId savedId,
576 Integration::ResourceTypeId savedType)
578 mResources.saved = true;
579 mResources.savedId = savedId;
580 mResources.savedType = savedType;
583 void TestPlatformAbstraction::SetResourceSaveFailed(Integration::ResourceId id,
584 Integration::ResourceFailure failure)
586 mResources.saveFailed = true;
587 mResources.saveFailedId = id;
588 mResources.saveFailure = failure;
591 Integration::ResourceRequest* TestPlatformAbstraction::GetRequest()
596 void TestPlatformAbstraction::DiscardRequest()
602 void TestPlatformAbstraction::SetClosestImageSize(const Vector2& size)
607 void TestPlatformAbstraction::SetLoadFileResult( bool result, std::vector< unsigned char >& buffer )
609 mLoadFileResult.loadResult = result;
612 mLoadFileResult.buffer = buffer;
616 void TestPlatformAbstraction::SetSaveFileResult( bool result )
618 mSaveFileResult = result;
621 Integration::PlatformAbstraction::FontListMode TestPlatformAbstraction::GetLastFontListMode( )
623 return mFontListMode;
626 void TestPlatformAbstraction::SetReadGlobalMetricsResult( bool success, Integration::GlobalMetrics& globalMetrics )
628 mReadGlobalMetricsResult = success;
629 mReadGlobalMetrics = globalMetrics;
632 void TestPlatformAbstraction::SetReadMetricsResult( bool success, std::vector<Integration::GlyphMetrics>& glyphMetricsContainer )
634 mReadMetricsResult = success;
635 mReadMetrics = glyphMetricsContainer; // copy