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()
30 mDynamicsFactory(NULL)
38 TestPlatformAbstraction::~TestPlatformAbstraction()
40 delete mDynamicsFactory;
44 * @copydoc PlatformAbstraction::GetTimeMicroseconds()
46 void TestPlatformAbstraction::GetTimeMicroseconds(unsigned int &seconds, unsigned int µSeconds)
49 microSeconds = mMicroSeconds;
50 mTrace.PushCall("GetTimeMicroseconds", "");
54 * @copydoc PlatformAbstraction::Suspend()
56 void TestPlatformAbstraction::Suspend()
58 mTrace.PushCall("Suspend", "");
62 * @copydoc PlatformAbstraction::Resume()
64 void TestPlatformAbstraction::Resume()
66 mTrace.PushCall("Resume", "");
69 void TestPlatformAbstraction::GetClosestImageSize( const std::string& filename,
70 const ImageAttributes& attributes,
73 closestSize = mClosestSize;
74 mTrace.PushCall("GetClosestImageSize", "");
77 void TestPlatformAbstraction::GetClosestImageSize( Integration::ResourcePointer resourceBuffer,
78 const ImageAttributes& attributes,
81 closestSize = mClosestSize;
82 mTrace.PushCall("GetClosestImageSize", "");
87 * @copydoc PlatformAbstraction::LoadResource()
89 void TestPlatformAbstraction::LoadResource(const Integration::ResourceRequest& request)
91 std::ostringstream out;
93 if( request.GetType()->id == Integration::ResourceText )
99 out << request.GetType()->id;
101 out << ", Path: " << request.GetPath() << std::endl ;
103 mTrace.PushCall("LoadResource", out.str());
107 tet_infoline ("Warning: multiple resource requests not handled by Test Suite. You may see unexpected errors");
109 mRequest = new Integration::ResourceRequest(request);
112 Integration::ResourcePointer TestPlatformAbstraction::LoadResourceSynchronously( const Integration::ResourceType& resourceType, const std::string& resourcePath )
114 mTrace.PushCall("LoadResourceSynchronously", "");
115 return mResources.loadedResource;
119 * @copydoc PlatformAbstraction::SaveResource()
121 void TestPlatformAbstraction::SaveResource(const Integration::ResourceRequest& request)
123 mTrace.PushCall("SaveResource", "");
127 tet_infoline ("Warning: multiple resource requests not handled by Test Suite. You may see unexpected errors");
129 mRequest = new Integration::ResourceRequest(request);
133 * @copydoc PlatformAbstraction::CancelLoad()
135 void TestPlatformAbstraction::CancelLoad(Integration::ResourceId id, Integration::ResourceTypeId typeId)
137 mTrace.PushCall("CancelLoad", "");
141 * @copydoc PlatformAbstraction::GetResources()
143 void TestPlatformAbstraction::GetResources(Integration::ResourceCache& cache)
145 mTrace.PushCall("GetResources", "");
147 if(mResources.loaded)
149 cache.LoadResponse( mResources.loadedId, mResources.loadedType, mResources.loadedResource, Integration::RESOURCE_COMPLETELY_LOADED );
151 if(mResources.loadFailed)
153 cache.LoadFailed( mResources.loadFailedId, mResources.loadFailure );
157 cache.SaveComplete( mResources.savedId, mResources.savedType );
159 if(mResources.saveFailed)
161 cache.SaveFailed( mResources.saveFailedId, mResources.saveFailure );
166 * @copydoc PlatformAbstraction::IsLoading()
168 bool TestPlatformAbstraction::IsLoading()
170 mTrace.PushCall("IsLoading", "");
171 return mIsLoadingResult;
175 * @copydoc PlatformAbstraction::GetDefaultFontFamily()
177 const std::string& TestPlatformAbstraction::GetDefaultFontFamily() const
179 mTrace.PushCall("GetDefaultFontFamily", "");
180 return mGetDefaultFontFamilyResult;
184 * @copydoc PlatformAbstraction::GetDefaultFontSize()
186 float TestPlatformAbstraction::GetDefaultFontSize() const
188 mTrace.PushCall("GetDefaultFontSize", "");
189 return mGetDefaultFontSizeResult;
192 PixelSize TestPlatformAbstraction::GetFontLineHeightFromCapsHeight(const std::string& fontFamily, const std::string& fontStyle, CapsHeight capsHeight) const
194 mTrace.PushCall("GetFontLineHeightFromCapsHeight", "");
195 // LineHeight will be bigger than CapsHeight, so return capsHeight + 1
196 return PixelSize(capsHeight + 1);
200 * @copydoc PlatformAbstraction::GetGlyphData()
203 Integration::GlyphSet* TestPlatformAbstraction::GetGlyphData ( const Integration::TextResourceType& textRequest,
204 const std::string& fontFamily,
205 bool getBitmap) const
209 mTrace.PushCall("GetGlyphData", "getBitmap:true");
213 mTrace.PushCall("GetGlyphData", "getBitmap:false");
216 // It creates fake metrics for the received characters.
218 Integration::GlyphSet* set = new Dali::Integration::GlyphSet();
219 Integration::BitmapPtr bitmapData;
221 std::set<uint32_t> characters;
223 for( Integration::TextResourceType::CharacterList::const_iterator it = textRequest.mCharacterList.begin(), endIt = textRequest.mCharacterList.end(); it != endIt; ++it )
225 if( characters.find( it->character ) == characters.end() )
227 characters.insert( it->character );
228 Integration::GlyphMetrics character = {it->character, Integration::GlyphMetrics::LOW_QUALITY, 10.0f, 10.0f, 9.0f, 1.0f, 10.0f, it->xPosition, it->yPosition };
232 bitmapData = Integration::Bitmap::New(Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, ResourcePolicy::DISCARD);
233 bitmapData->GetPackedPixelsProfile()->ReserveBuffer(Pixel::A8, 64, 64);
234 PixelBuffer* pixelBuffer = bitmapData->GetBuffer();
235 memset( pixelBuffer, it->character, 64*64 );
238 set->AddCharacter(bitmapData, character);
242 set->mLineHeight = 10.0f;
243 set->mAscender = 9.0f;
244 set->mUnitsPerEM = 2048.0f/64.0f;
245 set->SetAtlasResourceId( textRequest.mTextureAtlasId );
246 set->mFontHash = textRequest.mFontHash;
252 * @copydoc PlatformAbstraction::GetCachedGlyphData()
255 Integration::GlyphSet* TestPlatformAbstraction::GetCachedGlyphData( const Integration::TextResourceType& textRequest,
256 const std::string& fontFamily ) const
258 mTrace.PushCall("GetCachedGlyphData", "");
260 // It creates fake metrics and bitmap for received numeric characters '0' through '9'.
261 Integration::GlyphSet* set = new Dali::Integration::GlyphSet();
262 Integration::BitmapPtr bitmapData;
264 std::set<uint32_t> characters;
266 for( Integration::TextResourceType::CharacterList::const_iterator it = textRequest.mCharacterList.begin(), endIt = textRequest.mCharacterList.end(); it != endIt; ++it )
268 if( it->character >= '0' && it->character <= '9' && characters.find( it->character ) == characters.end() )
270 characters.insert( it->character );
271 Integration::GlyphMetrics character = {it->character, Integration::GlyphMetrics::HIGH_QUALITY, 10.0f, 10.0f, 9.0f, 1.0f, 10.0f, it->xPosition, it->yPosition };
273 bitmapData = Integration::Bitmap::New(Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, ResourcePolicy::DISCARD);
274 bitmapData->GetPackedPixelsProfile()->ReserveBuffer(Pixel::A8, 64, 64);
275 PixelBuffer* pixelBuffer = bitmapData->GetBuffer();
276 memset( pixelBuffer, it->character, 64*64 );
277 set->AddCharacter(bitmapData, character);
281 set->mLineHeight = 10.0f;
282 set->mAscender = 9.0f;
283 set->mUnitsPerEM = 2048.0f/64.0f;
284 set->SetAtlasResourceId( textRequest.mTextureAtlasId );
285 set->mFontHash = textRequest.mFontHash;
292 * @copydoc PlatformAbstraction::GetGlobalMetrics()
294 void TestPlatformAbstraction::GetGlobalMetrics( const std::string& fontFamily,
295 const std::string& fontStyle,
296 Integration::GlobalMetrics& globalMetrics ) const
298 globalMetrics.lineHeight = 10.0f;
299 globalMetrics.ascender = 9.0f;
300 globalMetrics.unitsPerEM = 2048.0f/64.0f;
301 globalMetrics.underlineThickness = 2.f;
302 globalMetrics.underlinePosition = 1.f;
306 * @copydoc PlatformAbstraction::GetFontPath()
308 std::string TestPlatformAbstraction::GetFontPath(const std::string& family, bool bold, bool italic) const
310 mTrace.PushCall("GetFontPath", "");
311 return mGetFontPathResult;
313 // Do nothing with arguments
317 * @copydoc PlatformAbstraction::SetDpi()
319 void TestPlatformAbstraction::SetDpi (unsigned int dpiHorizontal, unsigned int dpiVertical)
321 mTrace.PushCall("SetDpi", "");
325 * @copydoc PlatformAbstraction::GetFontFamilyForChars()
327 const std::string& TestPlatformAbstraction::GetFontFamilyForChars(const Integration::TextArray& charsRequested) const
329 mTrace.PushCall("GetFontFamilyForChars", "");
330 return mGetDefaultFontFamilyResult;
334 * @copydoc PlatformAbstraction::AllGlyphsSupported()
336 bool TestPlatformAbstraction::AllGlyphsSupported(const std::string& name, const std::string& fontStyle, const Integration::TextArray& text) const
338 mTrace.PushCall("AllGlyphsSupported", "");
343 * @copydoc PlatformAbstraction::ValidateFontFamilyName()
345 bool TestPlatformAbstraction::ValidateFontFamilyName(const std::string& fontFamily, const std::string& fontStyle, bool& isDefaultSystemFont, std::string& closestMatch, std::string& closestStyleMatch) const
347 mTrace.PushCall("ValidateFontFamilyName", "");
352 * @copydoc PlatformAbstraction::GetFontList()
354 void TestPlatformAbstraction::GetFontList( PlatformAbstraction::FontListMode mode, std::vector<std::string>& fontList ) const
356 mFontListMode = mode;
357 mTrace.PushCall("ValidateGetFontList", "");
361 * @copydoc PlatformAbstraction::LoadFile()
363 bool TestPlatformAbstraction::LoadFile( const std::string& filename, std::vector< unsigned char >& buffer ) const
365 mTrace.PushCall("LoadFile", "");
366 if( mLoadFileResult.loadResult )
368 buffer = mLoadFileResult.buffer;
371 return mLoadFileResult.loadResult;
375 * @copydoc PlatformAbstraction::LoadShaderBinFile()
377 bool TestPlatformAbstraction::LoadShaderBinFile( const std::string& filename, std::vector< unsigned char >& buffer ) const
379 mTrace.PushCall("LoadShaderBinFile", "");
380 if( mLoadFileResult.loadResult )
382 buffer = mLoadFileResult.buffer;
385 return mLoadFileResult.loadResult;
389 * @copydoc PlatformAbstraction::SaveFile()
391 bool TestPlatformAbstraction::SaveFile(const std::string& filename, std::vector< unsigned char >& buffer) const
393 mTrace.PushCall("SaveFile", "");
397 void TestPlatformAbstraction::JoinLoaderThreads()
399 mTrace.PushCall("JoinLoaderThreads", "");
402 void TestPlatformAbstraction::UpdateDefaultsFromDevice()
404 mTrace.PushCall("UpdateDefaultsFromDevice", "");
405 mGetDefaultFontFamilyResult+=1.0f;
408 Integration::DynamicsFactory* TestPlatformAbstraction::GetDynamicsFactory()
410 mTrace.PushCall("GetDynamicsFactory", "");
411 if( mDynamicsFactory == NULL )
413 mDynamicsFactory = new TestDynamicsFactory( mTrace );
415 return mDynamicsFactory;
418 bool TestPlatformAbstraction::ReadGlobalMetricsFromCache( const std::string& fontFamily,
419 const std::string& fontStyle,
420 Integration::GlobalMetrics& globalMetrics )
422 mTrace.PushCall("ReadGlobalMetricsFromCacheFile", "");
423 globalMetrics = mReadGlobalMetrics; // Want to copy contents...
424 return mReadGlobalMetricsResult; // Default false (will be set to true on subsequent write)
427 void TestPlatformAbstraction::WriteGlobalMetricsToCache( const std::string& fontFamily,
428 const std::string& fontStyle,
429 const Integration::GlobalMetrics& globalMetrics )
431 // Copy so next read uses written values. TODO: Could add method
432 // to turn this behaviour off for more extensive testing.
433 mReadGlobalMetrics = globalMetrics;
434 mReadGlobalMetricsResult = true;
436 mTrace.PushCall("WriteGlobalMetricsToCacheFile", "");
439 bool TestPlatformAbstraction::ReadMetricsFromCache( const std::string& fontFamily,
440 const std::string& fontStyle,
441 std::vector<Integration::GlyphMetrics>& glyphMetricsContainer )
443 mTrace.PushCall("ReadMetricsFromCacheFile", "");
444 glyphMetricsContainer = mReadMetrics;
445 return mReadMetricsResult; // Default false (will be set to true on subsequent write)
448 void TestPlatformAbstraction::WriteMetricsToCache( const std::string& fontFamily,
449 const std::string& fontStyle,
450 const Integration::GlyphSet& glyphSet )
452 // Copy so next read uses written values. TODO: Could add method
453 // to turn this behaviour off for more extensive testing.
454 const Integration::GlyphSet::CharacterList& charList = glyphSet.GetCharacterList();
455 mReadMetrics.clear();
456 for(std::size_t i=0, end=charList.size(); i<end; ++i)
458 mReadMetrics.push_back(charList[i].second);
460 mReadMetricsResult = true;
462 mTrace.PushCall("WriteMetricsToCacheFile", "");
465 void TestPlatformAbstraction::GetFileNamesFromDirectory( const std::string& directoryName,
466 std::vector<std::string>& fileNames )
468 fileNames.push_back( std::string( "u1f004.png" ) );
469 fileNames.push_back( std::string( "u1f0cf.png" ) );
470 fileNames.push_back( std::string( "u1f170.png" ) );
471 fileNames.push_back( std::string( "u1f601.png" ) );
474 Integration::BitmapPtr TestPlatformAbstraction::GetGlyphImage( const std::string& fontFamily, const std::string& fontStyle, float fontSize, uint32_t character ) const
476 Integration::BitmapPtr image = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, ResourcePolicy::DISCARD );
477 image->GetPackedPixelsProfile()->ReserveBuffer( Pixel::RGBA8888, 1, 1 );
479 mTrace.PushCall("GetGlyphImage", "");
484 /** Call this every test */
485 void TestPlatformAbstraction::Initialize()
489 memset(&mResources, 0, sizeof(Resources));
490 memset(&mReadGlobalMetrics, 0, sizeof(Integration::GlobalMetrics));
493 mIsLoadingResult=false;
494 mGetDefaultFontFamilyResult = "HelveticaNeue";
495 mGetDefaultFontSizeResult=12.0f;
496 mGetFontPathResult="helvetica-12";
497 mReadMetricsResult=false;
498 mReadGlobalMetricsResult=false;
508 bool TestPlatformAbstraction::WasCalled(TestFuncEnum func)
512 case GetTimeMicrosecondsFunc: return mTrace.FindMethod("GetTimeMicroseconds");
513 case SuspendFunc: return mTrace.FindMethod("Suspend");
514 case ResumeFunc: return mTrace.FindMethod("Resume");
515 case LoadResourceFunc: return mTrace.FindMethod("LoadResource");
516 case SaveResourceFunc: return mTrace.FindMethod("SaveResource");
517 case LoadFileFunc: return mTrace.FindMethod("LoadFile");
518 case SaveFileFunc: return mTrace.FindMethod("SaveFile");
519 case CancelLoadFunc: return mTrace.FindMethod("CancelLoad");
520 case GetResourcesFunc: return mTrace.FindMethod("GetResources");
521 case IsLoadingFunc: return mTrace.FindMethod("IsLoading");
522 case GetDefaultFontFamilyFunc: return mTrace.FindMethod("GetDefaultFontFamily");
523 case GetDefaultFontSizeFunc: return mTrace.FindMethod("GetDefaultFontSize");
524 case GetFontLineHeightFromCapsHeightFunc: return mTrace.FindMethod("GetFontLineHeightFromCapsHeight");
525 case GetGlyphDataFunc: return mTrace.FindMethod("GetGlyphData");
526 case GetCachedGlyphDataFunc: return mTrace.FindMethod("GetCachedGlyphData");
527 case GetFontPathFunc: return mTrace.FindMethod("GetFontPath");
528 case SetDpiFunc: return mTrace.FindMethod("SetDpi");
529 case JoinLoaderThreadsFunc: return mTrace.FindMethod("JoinLoaderThreads");
530 case GetFontFamilyForCharsFunc: return mTrace.FindMethod("GetFontFamilyForChars");
531 case AllGlyphsSupportedFunc: return mTrace.FindMethod("AllGlyphsSupported");
532 case ValidateFontFamilyNameFunc: return mTrace.FindMethod("ValidateFontFamilyName");
533 case UpdateDefaultsFromDeviceFunc: return mTrace.FindMethod("UpdateDefaultsFromDevice");
534 case GetDynamicsFactoryFunc: return mTrace.FindMethod("GetDynamicsFactory");
535 case ValidateGetFontListFunc: return mTrace.FindMethod("ValidateGetFontList");
536 case ReadGlobalMetricsFromCacheFileFunc: return mTrace.FindMethod("ReadGlobalMetricsFromCacheFile");
537 case WriteGlobalMetricsToCacheFileFunc: return mTrace.FindMethod("WriteGlobalMetricsToCacheFile");
538 case ReadMetricsFromCacheFileFunc: return mTrace.FindMethod("ReadMetricsFromCacheFile");
539 case WriteMetricsToCacheFileFunc: return mTrace.FindMethod("WriteMetricsToCacheFile");
544 void TestPlatformAbstraction::SetGetTimeMicrosecondsResult(size_t sec, size_t usec)
547 mMicroSeconds = usec;
550 void TestPlatformAbstraction::IncrementGetTimeResult(size_t milliseconds)
552 mMicroSeconds += milliseconds * 1000u;
553 unsigned int additionalSeconds = mMicroSeconds / 1000000u;
555 mSeconds += additionalSeconds;
556 mMicroSeconds -= additionalSeconds * 1000000u;
559 void TestPlatformAbstraction::SetIsLoadingResult(bool result)
561 mIsLoadingResult = result;
564 void TestPlatformAbstraction::SetGetDefaultFontFamilyResult(std::string result)
566 mGetDefaultFontFamilyResult = result;
569 void TestPlatformAbstraction::SetGetDefaultFontSizeResult(float result)
571 mGetDefaultFontSizeResult = result;
574 void TestPlatformAbstraction::SetGetFontPathResult(std::string& result)
576 mGetFontPathResult = result;
579 void TestPlatformAbstraction::ClearReadyResources()
581 memset(&mResources, 0, sizeof(Resources));
584 void TestPlatformAbstraction::SetResourceLoaded(Integration::ResourceId loadedId,
585 Integration::ResourceTypeId loadedType,
586 Integration::ResourcePointer loadedResource)
588 mResources.loaded = true;
589 mResources.loadedId = loadedId;
590 mResources.loadedType = loadedType;
591 mResources.loadedResource = loadedResource;
594 void TestPlatformAbstraction::SetResourceLoadFailed(Integration::ResourceId id,
595 Integration::ResourceFailure failure)
597 mResources.loadFailed = true;
598 mResources.loadFailedId = id;
599 mResources.loadFailure = failure;
602 void TestPlatformAbstraction::SetResourceSaved(Integration::ResourceId savedId,
603 Integration::ResourceTypeId savedType)
605 mResources.saved = true;
606 mResources.savedId = savedId;
607 mResources.savedType = savedType;
610 void TestPlatformAbstraction::SetResourceSaveFailed(Integration::ResourceId id,
611 Integration::ResourceFailure failure)
613 mResources.saveFailed = true;
614 mResources.saveFailedId = id;
615 mResources.saveFailure = failure;
618 Integration::ResourceRequest* TestPlatformAbstraction::GetRequest()
623 void TestPlatformAbstraction::DiscardRequest()
629 void TestPlatformAbstraction::SetClosestImageSize(const Vector2& size)
634 void TestPlatformAbstraction::SetLoadFileResult( bool result, std::vector< unsigned char >& buffer )
636 mLoadFileResult.loadResult = result;
639 mLoadFileResult.buffer = buffer;
643 void TestPlatformAbstraction::SetSaveFileResult( bool result )
645 mSaveFileResult = result;
648 Integration::PlatformAbstraction::FontListMode TestPlatformAbstraction::GetLastFontListMode( )
650 return mFontListMode;
653 void TestPlatformAbstraction::SetReadGlobalMetricsResult( bool success, Integration::GlobalMetrics& globalMetrics )
655 mReadGlobalMetricsResult = success;
656 mReadGlobalMetrics = globalMetrics;
659 void TestPlatformAbstraction::SetReadMetricsResult( bool success, std::vector<Integration::GlyphMetrics>& glyphMetricsContainer )
661 mReadMetricsResult = success;
662 mReadMetrics = glyphMetricsContainer; // copy