Merge "Flexbox UI control implementation" into devel/master
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / dali-toolkit-test-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 TestPlatformAbstraction::TestPlatformAbstraction()
26 : mTrace(),
27   mIsLoadingResult( false ),
28   mGetDefaultFontSizeResult( 0 ),
29   mResources(),
30   mRequest( NULL ),
31   mSize(),
32   mClosestSize(),
33   mLoadFileResult(),
34   mSaveFileResult( false )
35 {
36   Initialize();
37 }
38
39 TestPlatformAbstraction::~TestPlatformAbstraction()
40 {
41 }
42
43 void TestPlatformAbstraction::Suspend()
44 {
45   mTrace.PushCall("Suspend", "");
46 }
47
48 void TestPlatformAbstraction::Resume()
49 {
50   mTrace.PushCall("Resume", "");
51 }
52
53 ImageDimensions TestPlatformAbstraction::GetClosestImageSize( const std::string& filename,
54                                                               ImageDimensions size,
55                                                               FittingMode::Type fittingMode,
56                                                               SamplingMode::Type samplingMode,
57                                                               bool orientationCorrection )
58 {
59   ImageDimensions closestSize = ImageDimensions( mClosestSize.x, mClosestSize.y );
60   mTrace.PushCall("GetClosestImageSize", "");
61   return closestSize;
62 }
63
64 ImageDimensions TestPlatformAbstraction::GetClosestImageSize( Integration::ResourcePointer resourceBuffer,
65                                                    ImageDimensions size,
66                                                    FittingMode::Type fittingMode,
67                                                    SamplingMode::Type samplingMode,
68                                                    bool orientationCorrection )
69 {
70   ImageDimensions closestSize = ImageDimensions( mClosestSize.x, mClosestSize.y );
71   mTrace.PushCall("GetClosestImageSize", "");
72   return closestSize;
73 }
74
75 void TestPlatformAbstraction::LoadResource(const Integration::ResourceRequest& request)
76 {
77   std::ostringstream out;
78   out << "Type:" << request.GetType()->id << ", Path: " << request.GetPath() << std::endl ;
79
80   mTrace.PushCall("LoadResource", out.str());
81   if(mRequest != NULL)
82   {
83     delete mRequest;
84     tet_infoline ("Warning: multiple resource requests not handled by Test Suite. You may see unexpected errors");
85   }
86   mRequest = new Integration::ResourceRequest(request);
87 }
88
89 Integration::ResourcePointer TestPlatformAbstraction::LoadResourceSynchronously( const Integration::ResourceType& resourceType, const std::string& resourcePath )
90 {
91   mTrace.PushCall("LoadResourceSynchronously", "");
92   return mResources.loadedResource;
93 }
94
95 Integration::BitmapPtr TestPlatformAbstraction::DecodeBuffer( const Integration::ResourceType& resourceType, uint8_t * buffer, size_t size )
96 {
97   mTrace.PushCall("DecodeBuffer", "");
98   return Integration::BitmapPtr();
99 }
100
101 void TestPlatformAbstraction::CancelLoad(Integration::ResourceId id, Integration::ResourceTypeId typeId)
102 {
103   mTrace.PushCall("CancelLoad", "");
104 }
105
106 void TestPlatformAbstraction::GetResources(Integration::ResourceCache& cache)
107 {
108   mTrace.PushCall("GetResources", "");
109
110   if(mResources.loaded)
111   {
112     cache.LoadResponse( mResources.loadedId, mResources.loadedType, mResources.loadedResource, Integration::RESOURCE_COMPLETELY_LOADED );
113   }
114   if(mResources.loadFailed)
115   {
116     cache.LoadFailed( mResources.loadFailedId, mResources.loadFailure );
117   }
118 }
119
120 bool TestPlatformAbstraction::IsLoading()
121 {
122   mTrace.PushCall("IsLoading", "");
123   return mIsLoadingResult;
124 }
125
126 int TestPlatformAbstraction::GetDefaultFontSize() const
127 {
128   mTrace.PushCall("GetDefaultFontSize", "");
129   return mGetDefaultFontSizeResult;
130 }
131
132 void TestPlatformAbstraction::SetDpi (unsigned int dpiHorizontal, unsigned int dpiVertical)
133 {
134   mTrace.PushCall("SetDpi", "");
135 }
136
137 bool TestPlatformAbstraction::LoadFile( const std::string& filename, Dali::Vector< unsigned char >& buffer ) const
138 {
139   mTrace.PushCall("LoadFile", "");
140   if( mLoadFileResult.loadResult )
141   {
142     buffer = mLoadFileResult.buffer;
143   }
144
145   return mLoadFileResult.loadResult;
146 }
147
148 bool TestPlatformAbstraction::LoadShaderBinaryFile( const std::string& filename, Dali::Vector< unsigned char >& buffer ) const
149 {
150   mTrace.PushCall("LoadShaderBinaryFile", "");
151   if( mLoadFileResult.loadResult )
152   {
153     buffer = mLoadFileResult.buffer;
154   }
155
156   return mLoadFileResult.loadResult;
157 }
158
159 bool TestPlatformAbstraction::SaveFile(const std::string& filename, const unsigned char * buffer, unsigned int numBytes ) const
160 {
161   mTrace.PushCall("SaveFile", "");
162   return false;
163 }
164
165 void TestPlatformAbstraction::JoinLoaderThreads()
166 {
167   mTrace.PushCall("JoinLoaderThreads", "");
168 }
169
170 /** Call this every test */
171 void TestPlatformAbstraction::Initialize()
172 {
173   mTrace.Reset();
174   mTrace.Enable(true);
175   memset(&mResources, 0, sizeof(Resources));
176   mIsLoadingResult=false;
177
178   if(mRequest)
179   {
180     delete mRequest;
181     mRequest = 0;
182   }
183 }
184
185 bool TestPlatformAbstraction::WasCalled(TestFuncEnum func)
186 {
187   switch(func)
188   {
189     case SuspendFunc:                         return mTrace.FindMethod("Suspend");
190     case ResumeFunc:                          return mTrace.FindMethod("Resume");
191     case LoadResourceFunc:                    return mTrace.FindMethod("LoadResource");
192     case LoadResourceSynchronouslyFunc:       return mTrace.FindMethod("LoadResourceSynchronously");
193     case LoadFileFunc:                        return mTrace.FindMethod("LoadFile");
194     case LoadShaderBinaryFileFunc:            return mTrace.FindMethod("LoadShaderBinaryFile");
195     case SaveShaderBinaryFileFunc:            return mTrace.FindMethod("SaveShaderBinaryFile");
196     case SaveFileFunc:                        return mTrace.FindMethod("SaveFile");
197     case CancelLoadFunc:                      return mTrace.FindMethod("CancelLoad");
198     case GetResourcesFunc:                    return mTrace.FindMethod("GetResources");
199     case IsLoadingFunc:                       return mTrace.FindMethod("IsLoading");
200     case SetDpiFunc:                          return mTrace.FindMethod("SetDpi");
201     case JoinLoaderThreadsFunc:               return mTrace.FindMethod("JoinLoaderThreads");
202   }
203   return false;
204 }
205
206 void TestPlatformAbstraction::SetIsLoadingResult(bool result)
207 {
208   mIsLoadingResult = result;
209 }
210
211 void TestPlatformAbstraction::ClearReadyResources()
212 {
213   memset(&mResources, 0, sizeof(Resources));
214 }
215
216 void TestPlatformAbstraction::SetResourceLoaded(Integration::ResourceId  loadedId,
217                                                 Integration::ResourceTypeId  loadedType,
218                                                 Integration::ResourcePointer loadedResource)
219 {
220   mResources.loaded = true;
221   mResources.loadedId = loadedId;
222   mResources.loadedType = loadedType;
223   mResources.loadedResource = loadedResource;
224 }
225
226 void TestPlatformAbstraction::SetResourceLoadFailed(Integration::ResourceId  id,
227                                                     Integration::ResourceFailure failure)
228 {
229   mResources.loadFailed = true;
230   mResources.loadFailedId = id;
231   mResources.loadFailure = failure;
232 }
233
234 Integration::ResourceRequest* TestPlatformAbstraction::GetRequest()
235 {
236   return mRequest;
237 }
238
239 void TestPlatformAbstraction::DiscardRequest()
240 {
241   delete mRequest;
242   mRequest = NULL;
243 }
244
245 void TestPlatformAbstraction::SetClosestImageSize(const Vector2& size)
246 {
247   mClosestSize = size;
248 }
249
250 void TestPlatformAbstraction::SetLoadFileResult( bool result, Dali::Vector< unsigned char >& buffer )
251 {
252   mLoadFileResult.loadResult = result;
253   if( result )
254   {
255     mLoadFileResult.buffer = buffer;
256   }
257 }
258
259 void TestPlatformAbstraction::SetSaveFileResult( bool result )
260 {
261   mSaveFileResult = result;
262 }
263
264 } // namespace Dali