Merge "Size negotiation patch 4: Remove SetRelayoutEnabled" into tizen
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / dali-test-suite-utils / test-platform-abstraction.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 #include "test-platform-abstraction.h"
19 #include "dali-test-suite-utils.h"
20 #include <dali/integration-api/bitmap.h>
21
22 namespace Dali
23 {
24
25 /**
26  * Constructor
27  */
28 TestPlatformAbstraction::TestPlatformAbstraction()
29 : mRequest(0),
30   mDynamicsFactory(NULL)
31 {
32   Initialize();
33 }
34
35 /**
36  * Destructor
37  */
38 TestPlatformAbstraction::~TestPlatformAbstraction()
39 {
40   delete mDynamicsFactory;
41 }
42
43 /**
44  * @copydoc PlatformAbstraction::GetTimeMicroseconds()
45  */
46 void TestPlatformAbstraction::GetTimeMicroseconds(unsigned int &seconds, unsigned int &microSeconds)
47 {
48   seconds = mSeconds;
49   microSeconds = mMicroSeconds;
50   mTrace.PushCall("GetTimeMicroseconds", "");
51 }
52
53 /**
54  * @copydoc PlatformAbstraction::Suspend()
55  */
56 void TestPlatformAbstraction::Suspend()
57 {
58   mTrace.PushCall("Suspend", "");
59 }
60
61 /**
62  * @copydoc PlatformAbstraction::Resume()
63  */
64 void TestPlatformAbstraction::Resume()
65 {
66   mTrace.PushCall("Resume", "");
67 }
68
69 ImageDimensions TestPlatformAbstraction::GetClosestImageSize( const std::string& filename,
70                                                               ImageDimensions size,
71                                                               FittingMode::Type fittingMode,
72                                                               SamplingMode::Type samplingMode,
73                                                               bool orientationCorrection )
74 {
75   ImageDimensions closestSize = ImageDimensions( mClosestSize.x, mClosestSize.y );
76   mTrace.PushCall("GetClosestImageSize", "");
77   return closestSize;
78 }
79
80 ImageDimensions TestPlatformAbstraction::GetClosestImageSize( Integration::ResourcePointer resourceBuffer,
81                                                    ImageDimensions size,
82                                                    FittingMode::Type fittingMode,
83                                                    SamplingMode::Type samplingMode,
84                                                    bool orientationCorrection )
85 {
86   ImageDimensions closestSize = ImageDimensions( mClosestSize.x, mClosestSize.y );
87   mTrace.PushCall("GetClosestImageSize", "");
88   return closestSize;
89 }
90
91
92 /**
93  * @copydoc PlatformAbstraction::LoadResource()
94  */
95 void TestPlatformAbstraction::LoadResource(const Integration::ResourceRequest& request)
96 {
97   std::ostringstream out;
98   out << "Type:" << request.GetType()->id << ", Path: " << request.GetPath() << std::endl ;
99
100   mTrace.PushCall("LoadResource", out.str());
101   if(mRequest != NULL)
102   {
103     delete mRequest;
104     tet_infoline ("Warning: multiple resource requests not handled by Test Suite. You may see unexpected errors");
105   }
106   mRequest = new Integration::ResourceRequest(request);
107 }
108
109 Integration::ResourcePointer TestPlatformAbstraction::LoadResourceSynchronously( const Integration::ResourceType& resourceType, const std::string& resourcePath )
110 {
111   mTrace.PushCall("LoadResourceSynchronously", "");
112   return mResources.loadedResource;
113 }
114
115 /**
116  * @copydoc PlatformAbstraction::SaveResource()
117  */
118 void TestPlatformAbstraction::SaveResource(const Integration::ResourceRequest& request)
119 {
120   mTrace.PushCall("SaveResource", "");
121   if(mRequest != NULL)
122   {
123     delete mRequest;
124     tet_infoline ("Warning: multiple resource requests not handled by Test Suite. You may see unexpected errors");
125   }
126   mRequest = new Integration::ResourceRequest(request);
127 }
128
129 /**
130  * @copydoc PlatformAbstraction::CancelLoad()
131  */
132 void TestPlatformAbstraction::CancelLoad(Integration::ResourceId id, Integration::ResourceTypeId typeId)
133 {
134   mTrace.PushCall("CancelLoad", "");
135 }
136
137 /**
138  * @copydoc PlatformAbstraction::GetResources()
139  */
140 void TestPlatformAbstraction::GetResources(Integration::ResourceCache& cache)
141 {
142   mTrace.PushCall("GetResources", "");
143
144   if(mResources.loaded)
145   {
146     cache.LoadResponse( mResources.loadedId, mResources.loadedType, mResources.loadedResource, Integration::RESOURCE_COMPLETELY_LOADED );
147   }
148   if(mResources.loadFailed)
149   {
150     cache.LoadFailed( mResources.loadFailedId, mResources.loadFailure );
151   }
152   if(mResources.saved)
153   {
154     cache.SaveComplete( mResources.savedId, mResources.savedType );
155   }
156   if(mResources.saveFailed)
157   {
158     cache.SaveFailed( mResources.saveFailedId, mResources.saveFailure );
159   }
160 }
161
162 /**
163  * @copydoc PlatformAbstraction::IsLoading()
164  */
165 bool TestPlatformAbstraction::IsLoading()
166 {
167   mTrace.PushCall("IsLoading", "");
168   return mIsLoadingResult;
169 }
170
171 /**
172  * @copydoc PlatformAbstraction::GetDefaultFontDescription()
173  */
174 void TestPlatformAbstraction::GetDefaultFontDescription( std::string& family, std::string& style ) const
175 {
176   mTrace.PushCall("GetDefaultFontFamily", "");
177   family = mGetDefaultFontFamilyResult;
178   style = mGetDefaultFontStyleResult;
179 }
180
181 /**
182  * @copydoc PlatformAbstraction::GetDefaultFontSize()
183  */
184 int TestPlatformAbstraction::GetDefaultFontSize() const
185 {
186   mTrace.PushCall("GetDefaultFontSize", "");
187   return mGetDefaultFontSizeResult;
188 }
189
190 /**
191  * @copydoc PlatformAbstraction::SetDpi()
192  */
193 void TestPlatformAbstraction::SetDpi (unsigned int dpiHorizontal, unsigned int dpiVertical)
194 {
195   mTrace.PushCall("SetDpi", "");
196 }
197
198 /**
199  * @copydoc PlatformAbstraction::LoadFile()
200  */
201 bool TestPlatformAbstraction::LoadFile( const std::string& filename, std::vector< unsigned char >& buffer ) const
202 {
203   mTrace.PushCall("LoadFile", "");
204   if( mLoadFileResult.loadResult )
205   {
206     buffer = mLoadFileResult.buffer;
207   }
208
209   return mLoadFileResult.loadResult;
210 }
211
212 /**
213  * @copydoc PlatformAbstraction::LoadShaderBinFile()
214  */
215 bool TestPlatformAbstraction::LoadShaderBinFile( const std::string& filename, std::vector< unsigned char >& buffer ) const
216 {
217   mTrace.PushCall("LoadShaderBinFile", "");
218   if( mLoadFileResult.loadResult )
219   {
220     buffer = mLoadFileResult.buffer;
221   }
222
223   return mLoadFileResult.loadResult;
224 }
225
226 /**
227  * @copydoc PlatformAbstraction::SaveFile()
228  */
229 bool TestPlatformAbstraction::SaveFile(const std::string& filename, std::vector< unsigned char >& buffer) const
230 {
231   mTrace.PushCall("SaveFile", "");
232   return false;
233 }
234
235 void TestPlatformAbstraction::JoinLoaderThreads()
236 {
237   mTrace.PushCall("JoinLoaderThreads", "");
238 }
239
240 Integration::DynamicsFactory* TestPlatformAbstraction::GetDynamicsFactory()
241 {
242   mTrace.PushCall("GetDynamicsFactory", "");
243   if( mDynamicsFactory == NULL )
244   {
245     mDynamicsFactory = new TestDynamicsFactory( mTrace );
246   }
247   return mDynamicsFactory;
248 }
249
250 /** Call this every test */
251 void TestPlatformAbstraction::Initialize()
252 {
253   mTrace.Reset();
254   mTrace.Enable(true);
255   memset(&mResources, 0, sizeof(Resources));
256   mSeconds=0;
257   mMicroSeconds=0;
258   mIsLoadingResult=false;
259
260   if(mRequest)
261   {
262     delete mRequest;
263     mRequest = 0;
264   }
265 }
266
267 bool TestPlatformAbstraction::WasCalled(TestFuncEnum func)
268 {
269   switch(func)
270   {
271     case GetTimeMicrosecondsFunc:             return mTrace.FindMethod("GetTimeMicroseconds");
272     case SuspendFunc:                         return mTrace.FindMethod("Suspend");
273     case ResumeFunc:                          return mTrace.FindMethod("Resume");
274     case LoadResourceFunc:                    return mTrace.FindMethod("LoadResource");
275     case SaveResourceFunc:                    return mTrace.FindMethod("SaveResource");
276     case LoadFileFunc:                        return mTrace.FindMethod("LoadFile");
277     case SaveFileFunc:                        return mTrace.FindMethod("SaveFile");
278     case CancelLoadFunc:                      return mTrace.FindMethod("CancelLoad");
279     case GetResourcesFunc:                    return mTrace.FindMethod("GetResources");
280     case IsLoadingFunc:                       return mTrace.FindMethod("IsLoading");
281     case SetDpiFunc:                          return mTrace.FindMethod("SetDpi");
282     case JoinLoaderThreadsFunc:               return mTrace.FindMethod("JoinLoaderThreads");
283     case GetDynamicsFactoryFunc:              return mTrace.FindMethod("GetDynamicsFactory");
284   }
285   return false;
286 }
287
288 void TestPlatformAbstraction::SetGetTimeMicrosecondsResult(size_t sec, size_t usec)
289 {
290   mSeconds = sec;
291   mMicroSeconds = usec;
292 }
293
294 void TestPlatformAbstraction::IncrementGetTimeResult(size_t milliseconds)
295 {
296   mMicroSeconds += milliseconds * 1000u;
297   unsigned int additionalSeconds = mMicroSeconds / 1000000u;
298
299   mSeconds += additionalSeconds;
300   mMicroSeconds -= additionalSeconds * 1000000u;
301 }
302
303 void TestPlatformAbstraction::SetIsLoadingResult(bool result)
304 {
305   mIsLoadingResult = result;
306 }
307
308 void TestPlatformAbstraction::ClearReadyResources()
309 {
310   memset(&mResources, 0, sizeof(Resources));
311 }
312
313 void TestPlatformAbstraction::SetResourceLoaded(Integration::ResourceId  loadedId,
314                                                 Integration::ResourceTypeId  loadedType,
315                                                 Integration::ResourcePointer loadedResource)
316 {
317   mResources.loaded = true;
318   mResources.loadedId = loadedId;
319   mResources.loadedType = loadedType;
320   mResources.loadedResource = loadedResource;
321 }
322
323 void TestPlatformAbstraction::SetResourceLoadFailed(Integration::ResourceId  id,
324                                                     Integration::ResourceFailure failure)
325 {
326   mResources.loadFailed = true;
327   mResources.loadFailedId = id;
328   mResources.loadFailure = failure;
329 }
330
331 void TestPlatformAbstraction::SetResourceSaved(Integration::ResourceId      savedId,
332                                                Integration::ResourceTypeId  savedType)
333 {
334   mResources.saved = true;
335   mResources.savedId = savedId;
336   mResources.savedType = savedType;
337 }
338
339 void TestPlatformAbstraction::SetResourceSaveFailed(Integration::ResourceId  id,
340                                                     Integration::ResourceFailure failure)
341 {
342   mResources.saveFailed = true;
343   mResources.saveFailedId = id;
344   mResources.saveFailure = failure;
345 }
346
347 Integration::ResourceRequest* TestPlatformAbstraction::GetRequest()
348 {
349   return mRequest;
350 }
351
352 void TestPlatformAbstraction::DiscardRequest()
353 {
354   delete mRequest;
355   mRequest = NULL;
356 }
357
358 void TestPlatformAbstraction::SetClosestImageSize(const Vector2& size)
359 {
360   mClosestSize = size;
361 }
362
363 void TestPlatformAbstraction::SetLoadFileResult( bool result, std::vector< unsigned char >& buffer )
364 {
365   mLoadFileResult.loadResult = result;
366   if( result )
367   {
368     mLoadFileResult.buffer = buffer;
369   }
370 }
371
372 void TestPlatformAbstraction::SetSaveFileResult( bool result )
373 {
374   mSaveFileResult = result;
375 }
376
377 } // namespace Dali