9be034b320ca602b828aa4e29a4e137db201fdfe
[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) 2020 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
24 #include <cstring>
25 #include <string>
26 #include <vector>
27
28 // INTERNAL INCLUDES
29 #include <dali/integration-api/platform-abstraction.h>
30 #include <dali/public-api/math/vector2.h>
31
32 #include "test-trace-call-stack.h"
33
34 namespace Dali
35 {
36 /**
37  * Concrete implementation of the platform abstraction class.
38  */
39 class DALI_CORE_API TestPlatformAbstraction : public Dali::Integration::PlatformAbstraction
40 {
41 public:
42   /**
43    * Constructor
44    */
45   TestPlatformAbstraction();
46
47   /**
48    * Destructor
49    */
50   ~TestPlatformAbstraction() override;
51
52   /**
53    * @copydoc PlatformAbstraction::GetClosestImageSize()
54    */
55   ImageDimensions GetClosestImageSize(const std::string& filename,
56                                       ImageDimensions    size,
57                                       FittingMode::Type  fittingMode,
58                                       SamplingMode::Type samplingMode,
59                                       bool               orientationCorrection) override;
60
61   /**
62    * @copydoc PlatformAbstraction::GetClosestImageSize()
63    */
64   ImageDimensions GetClosestImageSize(Integration::ResourcePointer resourceBuffer,
65                                       ImageDimensions              size,
66                                       FittingMode::Type            fittingMode,
67                                       SamplingMode::Type           samplingMode,
68                                       bool                         orientationCorrection) override;
69
70   /**
71    * @copydoc PlatformAbstraction::LoadResourceSynchronously()
72    */
73   Integration::ResourcePointer LoadImageSynchronously(const Integration::BitmapResourceType& resourceType, const std::string& resourcePath) override;
74
75   /**
76    * @copydoc PlatformAbstraction::DecodeBuffer()
77    */
78   Integration::BitmapPtr DecodeBuffer(const Dali::Integration::BitmapResourceType& resourceType, uint8_t* buffer, size_t size) override;
79
80   /**
81    * @copydoc PlatformAbstraction::LoadShaderBinaryFile()
82    */
83   bool LoadShaderBinaryFile(const std::string& filename, Dali::Vector<unsigned char>& buffer) const override;
84
85   /**
86    * @copydoc PlatformAbstraction::SaveShaderBinaryFile()
87    */
88   virtual bool SaveShaderBinaryFile(const std::string& filename, const unsigned char* buffer, unsigned int numBytes) const override
89   {
90     return true;
91   }
92
93   /**
94    * @copydoc PlatformAbstraction::StartTimer()
95    */
96   uint32_t StartTimer(uint32_t milliseconds, CallbackBase* callback) override;
97
98   /*
99    * @copydoc PlatformAbstraction::CancelTimer()
100    */
101   void CancelTimer(uint32_t timerId) override;
102
103 public: // TEST FUNCTIONS
104   // Enumeration of Platform Abstraction methods
105   typedef enum
106   {
107     LoadResourceSynchronouslyFunc,
108     LoadShaderBinaryFileFunc,
109     SaveShaderBinaryFileFunc
110   } TestFuncEnum;
111
112   /** Call this every test */
113   void Initialize();
114
115   inline void EnableTrace(bool enable)
116   {
117     mTrace.Enable(enable);
118   }
119   inline void ResetTrace()
120   {
121     mTrace.Reset();
122   }
123   inline TraceCallStack& GetTrace()
124   {
125     return mTrace;
126   }
127
128   /**
129    * @brief Checks if a platform function was called
130    * @param[in] func The function to check
131    * @return true if the function was called
132    */
133   bool WasCalled(TestFuncEnum func);
134
135   /**
136    * @brief Sets the result to return when IsLoading is called by Core.
137    * @param[in] result The result to set.
138    */
139   void SetIsLoadingResult(bool result);
140
141   /**
142    * @brief Clears all resource queues
143    */
144   void ClearReadyResources();
145
146   /**
147    * @brief Sets the value returned by GetClosestImageSize.
148    * @param[in] size The size that should be returned.
149    */
150   void SetClosestImageSize(const Vector2& size);
151
152   /**
153    * @brief Sets the result return by LoadFile.
154    * @param[in] result The value that LoadFile should return.
155    * @param[in] buffer The buffer of the loaded file.
156    */
157   void SetLoadFileResult(bool result, Dali::Vector<unsigned char>& buffer);
158
159   /**
160    * @brief Sets the SaveFile result
161    * @param[in] result The value that SaveFile should return
162    */
163   void SetSaveFileResult(bool result);
164
165   /**
166    * @brief Sets the resource loaded by LoadResourceSynchronously
167    * @param[in] resource The loaded resource
168    */
169   void SetSynchronouslyLoadedResource(Integration::ResourcePointer resource);
170
171   /**
172    * @brief Sets the bitmap returned by DecodeBuffer()
173    * @param[in] bitmap The decoded bitmap
174    */
175   void SetDecodedBitmap(Integration::BitmapPtr bitmap);
176
177   /**
178    * @brief Triggers the previously stored callback function
179    */
180   void TriggerTimer();
181
182 private:
183   TestPlatformAbstraction(const TestPlatformAbstraction&);            ///< Undefined
184   TestPlatformAbstraction& operator=(const TestPlatformAbstraction&); ///< Undefined
185
186 private:
187   struct LoadFileResult
188   {
189     inline LoadFileResult()
190     : loadResult(false)
191     {
192     }
193
194     bool                        loadResult;
195     Dali::Vector<unsigned char> buffer;
196   };
197
198   mutable TraceCallStack mTrace;
199   bool                   mIsLoadingResult;
200   ImageDimensions        mClosestSize;
201
202   LoadFileResult mLoadFileResult;
203   bool           mSaveFileResult;
204
205   Integration::ResourcePointer mSynchronouslyLoadedResource;
206   Integration::BitmapPtr       mDecodedBitmap;
207
208   uint32_t      mTimerId;
209   CallbackBase* mCallbackFunction;
210 };
211
212 } // namespace Dali
213
214 #endif /* DALI_TEST_PLATFORM_ABSTRACTION_H */