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