d9d3247f85d3032764a7ae43e0ab44bbcd5458c0
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / dali-toolkit-test-utils / test-platform-abstraction.h
1 #ifndef __DALI_TEST_PLATFORM_ABSTRACTION_H__
2 #define __DALI_TEST_PLATFORM_ABSTRACTION_H__
3
4 /*
5  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <stdint.h>
23 #include <cstring>
24 #include <string>
25 #include <vector>
26
27 // INTERNAL INCLUDES
28 #include <dali/integration-api/platform-abstraction.h>
29
30 #include "test-trace-call-stack.h"
31
32 namespace Dali
33 {
34
35 /**
36  * Concrete implementation of the platform abstraction class.
37  */
38 class DALI_IMPORT_API TestPlatformAbstraction : public Dali::Integration::PlatformAbstraction
39 {
40
41 public:
42
43   typedef Vector< Integration::ResourceRequest* > ResourceRequestContainer;
44
45   /**
46    * Constructor
47    */
48   TestPlatformAbstraction();
49
50   /**
51    * Destructor
52    */
53   virtual ~TestPlatformAbstraction();
54
55   /**
56    * @copydoc PlatformAbstraction::Suspend()
57    */
58   virtual void Suspend();
59
60   /**
61    * @copydoc PlatformAbstraction::Resume()
62    */
63   virtual void Resume();
64
65   /**
66    * @copydoc PlatformAbstraction::GetClosestImageSize()
67    */
68   virtual ImageDimensions GetClosestImageSize( const std::string& filename,
69                                                  ImageDimensions size,
70                                                  FittingMode::Type fittingMode,
71                                                  SamplingMode::Type samplingMode,
72                                                  bool orientationCorrection );
73
74   /**
75    * @copydoc PlatformAbstraction::GetClosestImageSize()
76    */
77   virtual ImageDimensions GetClosestImageSize( Integration::ResourcePointer resourceBuffer,
78                                                ImageDimensions size,
79                                                FittingMode::Type fittingMode,
80                                                SamplingMode::Type samplingMode,
81                                                bool orientationCorrection );
82
83   /**
84    * @copydoc PlatformAbstraction::LoadResource()
85    */
86   virtual void LoadResource(const Integration::ResourceRequest& request);
87
88   /**
89    * @copydoc PlatformAbstraction::LoadResourceSynchronously()
90    */
91   virtual Integration::ResourcePointer LoadResourceSynchronously( const Integration::ResourceType& resourceType, const std::string& resourcePath );
92
93   /**
94    * @copydoc PlatformAbstraction::DecodeBuffer()
95    */
96   virtual Integration::BitmapPtr DecodeBuffer( const Dali::Integration::ResourceType& resourceType, uint8_t * buffer, size_t size );
97
98   /**
99    * @copydoc PlatformAbstraction::CancelLoad()
100    */
101   virtual void CancelLoad(Integration::ResourceId id, Integration::ResourceTypeId typeId);
102
103   /**
104    * @copydoc PlatformAbstraction::GetResources()
105    */
106   virtual void GetResources(Integration::ResourceCache& cache);
107
108   /**
109    * @copydoc PlatformAbstraction::IsLoading()
110    */
111   virtual bool IsLoading();
112
113   /**
114    * @copydoc PlatformAbstraction::GetDefaultFontSize()
115    */
116   virtual int GetDefaultFontSize() const;
117
118   /**
119    * @copydoc PlatformAbstraction::SetDpi()
120    */
121   virtual void SetDpi (unsigned int dpiHorizontal, unsigned int dpiVertical);
122
123   /**
124    * @copydoc PlatformAbstraction::LoadFile()
125    */
126   virtual bool LoadFile( const std::string& filename, Dali::Vector< unsigned char >& buffer ) const;
127
128   /**
129    * @copydoc PlatformAbstraction::LoadShaderBinaryFile()
130    */
131   virtual bool LoadShaderBinaryFile( const std::string& filename, Dali::Vector< unsigned char >& buffer ) const;
132
133   /**
134    * @copydoc PlatformAbstraction::SaveFile()
135    */
136   virtual bool SaveFile(const std::string& filename, const unsigned char * buffer, unsigned int numBytes ) const;
137
138  /**
139   * @copydoc PlatformAbstraction::SaveShaderBinaryFile()
140   */
141   virtual bool SaveShaderBinaryFile( const std::string& filename, const unsigned char * buffer, unsigned int numBytes ) const { return true; }
142
143   virtual void JoinLoaderThreads();
144
145 public: // TEST FUNCTIONS
146
147   // Enumeration of Platform Abstraction methods
148   typedef enum
149   {
150     SuspendFunc,
151     ResumeFunc,
152     LoadResourceFunc,
153     LoadResourceSynchronouslyFunc,
154     SaveFileFunc,
155     LoadFileFunc,
156     LoadShaderBinaryFileFunc,
157     SaveShaderBinaryFileFunc,
158     CancelLoadFunc,
159     GetResourcesFunc,
160     IsLoadingFunc,
161     SetDpiFunc,
162     JoinLoaderThreadsFunc,
163   } TestFuncEnum;
164
165   /** Call this every test */
166   void Initialize();
167
168   inline void EnableTrace(bool enable) { mTrace.Enable(enable); }
169   inline void ResetTrace() { mTrace.Reset(); }
170   inline TraceCallStack& GetTrace() { return mTrace; }
171
172   /**
173    * @brief Checks if a platform function was called
174    * @param[in] func The function to check
175    * @return true if the function was called
176    */
177   bool WasCalled(TestFuncEnum func);
178
179   /**
180    * @brief Sets the result to return when IsLoading is called by Core.
181    * @param[in] result The result to set.
182    */
183   void SetIsLoadingResult(bool result);
184
185   /**
186    * @brief Sets the value returned by GetDefaultFontSize
187    * @param[in] result The value to return
188    */
189   void SetGetDefaultFontSizeResult(float result);
190
191   /**
192    * @brief Clears all resource queues
193    */
194   void ClearReadyResources();
195
196   /**
197    * @brief Sets a particular resource request as loaded.
198    * @param[in] loadedId The ResourceID of the resource that has been loaded.
199    * @param[in] loadedType The type of resource that has been loaded.
200    * @param[in] loadedResource A pointer to the resource that has been loaded.
201    */
202   void SetResourceLoaded(Integration::ResourceId  loadedId,
203                          Integration::ResourceTypeId  loadedType,
204                          Integration::ResourcePointer loadedResource);
205
206   /**
207    * @brief Sets a particular resource request as load failure.
208    * @param[in] id The ID of the failed resource request.
209    * @param[in] failure The type of failure.
210    */
211   void SetResourceLoadFailed(Integration::ResourceId  id,
212                              Integration::ResourceFailure failure);
213
214   /**
215    * @brief Retrieves the latest resource request
216    * @return A pointer to the latest resource request.
217    */
218   Integration::ResourceRequest* GetRequest();
219
220   /**
221    * @brief Retrieves a reference to a container of all the resource requests.
222    * @return A reference to a container of all the resource requests.
223    */
224   const ResourceRequestContainer& GetAllRequests() const;
225
226   /**
227    * @brief Sets all resource requests as loaded.
228    */
229   void SetAllResourceRequestsAsLoaded();
230
231   /**
232    * @brief Sets all resource requests as loaded.
233    * @param[in] failure The failure type
234    */
235   void SetAllResourceRequestsAsFailed( Integration::ResourceFailure failure );
236
237   /**
238    * @brief Discards all current resource requests.
239    */
240   void DiscardRequest();
241
242   /**
243    * @brief Sets the value returned by GetClosestImageSize.
244    * @param[in] size The size that should be returned.
245    */
246   void SetClosestImageSize(const Vector2& size);
247
248   /**
249    * @brief Sets the result return by LoadFile.
250    * @param[in] result The value that LoadFile should return.
251    * @param[in] buffer The buffer of the loaded file.
252    */
253   void SetLoadFileResult( bool result, Dali::Vector< unsigned char >& buffer );
254
255   /**
256    * @brief Sets the SaveFile result
257    * @param[in] result The value that SaveFile should return
258    */
259   void SetSaveFileResult( bool result );
260
261   /**
262    * @brief Sets the resource loaded by LoadResourceSynchronously
263    * @param[in] resource The loaded resource
264    */
265   void SetSynchronouslyLoadedResource( Integration::ResourcePointer resource );
266
267 private:
268
269   struct LoadedResource
270   {
271     Integration::ResourceId      id;
272     Integration::ResourceTypeId  type;
273     Integration::ResourcePointer resource;
274   };
275
276   struct FailedLoad
277   {
278     Integration::ResourceId      id;
279     Integration::ResourceFailure failure;
280   };
281
282   struct LoadFileResult
283   {
284     inline LoadFileResult()
285     : loadResult(false)
286     {
287
288     }
289
290     bool loadResult;
291     Dali::Vector< unsigned char> buffer;
292   };
293
294   typedef std::vector< LoadedResource > LoadedResourceContainer;
295   typedef std::vector< FailedLoad > FailedLoadContainer;
296
297   mutable TraceCallStack        mTrace;
298   bool                          mIsLoadingResult;
299   int                           mGetDefaultFontSizeResult;
300   LoadedResourceContainer       mLoadedResourcesQueue;
301   FailedLoadContainer           mFailedLoadQueue;
302   ResourceRequestContainer      mResourceRequests;
303   Vector2                       mSize;
304   Vector2                       mClosestSize;
305
306   LoadFileResult                mLoadFileResult;
307   bool                          mSaveFileResult;
308
309   Integration::ResourcePointer  mSynchronouslyLoadedResource;
310 };
311
312 } // Dali
313
314 #endif /* __DALI_TET_PLATFORM_ABSTRACTION_H__ */