41ad1e5d1e526013ce2cb5ee2bc93b089fb9f206
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / dali-toolkit-test-utils / test-platform-abstraction.cpp
1 /*
2  * Copyright (c) 2020 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
20 #include <dali/integration-api/bitmap.h>
21
22 #include "dali-test-suite-utils.h"
23
24 namespace Dali
25 {
26 TestPlatformAbstraction::TestPlatformAbstraction()
27 : mTrace(),
28   mIsLoadingResult(false),
29   mClosestSize(),
30   mLoadFileResult(),
31   mSaveFileResult(false),
32   mSynchronouslyLoadedResource(),
33   mTimerId(0),
34   mCallbackFunction(nullptr)
35 {
36   Initialize();
37 }
38
39 TestPlatformAbstraction::~TestPlatformAbstraction()
40 {
41 }
42
43 ImageDimensions TestPlatformAbstraction::GetClosestImageSize(const std::string& filename,
44                                                              ImageDimensions    size,
45                                                              FittingMode::Type  fittingMode,
46                                                              SamplingMode::Type samplingMode,
47                                                              bool               orientationCorrection)
48 {
49   ImageDimensions closestSize = ImageDimensions(mClosestSize);
50   mTrace.PushCall("GetClosestImageSize", "");
51   return closestSize;
52 }
53
54 ImageDimensions TestPlatformAbstraction::GetClosestImageSize(Integration::ResourcePointer resourceBuffer,
55                                                              ImageDimensions              size,
56                                                              FittingMode::Type            fittingMode,
57                                                              SamplingMode::Type           samplingMode,
58                                                              bool                         orientationCorrection)
59 {
60   ImageDimensions closestSize = ImageDimensions(mClosestSize);
61   mTrace.PushCall("GetClosestImageSize", "");
62   return closestSize;
63 }
64
65 Integration::ResourcePointer TestPlatformAbstraction::LoadImageSynchronously(const Integration::BitmapResourceType& resourceType, const std::string& resourcePath)
66 {
67   mTrace.PushCall("LoadResourceSynchronously", "");
68   return mSynchronouslyLoadedResource;
69 }
70
71 Integration::BitmapPtr TestPlatformAbstraction::DecodeBuffer(const Integration::BitmapResourceType& resourceType, uint8_t* buffer, size_t size)
72 {
73   mTrace.PushCall("DecodeBuffer", "");
74   return mDecodedBitmap;
75 }
76
77 bool TestPlatformAbstraction::LoadShaderBinaryFile(const std::string& filename, Dali::Vector<unsigned char>& buffer) const
78 {
79   mTrace.PushCall("LoadShaderBinaryFile", "");
80   if(mLoadFileResult.loadResult)
81   {
82     buffer = mLoadFileResult.buffer;
83   }
84
85   return mLoadFileResult.loadResult;
86 }
87
88 /** Call this every test */
89 void TestPlatformAbstraction::Initialize()
90 {
91   mTrace.Reset();
92   mTrace.Enable(true);
93   mIsLoadingResult = false;
94   mSynchronouslyLoadedResource.Reset();
95   mDecodedBitmap.Reset();
96 }
97
98 bool TestPlatformAbstraction::WasCalled(TestFuncEnum func)
99 {
100   switch(func)
101   {
102     case LoadResourceSynchronouslyFunc:
103       return mTrace.FindMethod("LoadResourceSynchronously");
104     case LoadShaderBinaryFileFunc:
105       return mTrace.FindMethod("LoadShaderBinaryFile");
106     case SaveShaderBinaryFileFunc:
107       return mTrace.FindMethod("SaveShaderBinaryFile");
108   }
109   return false;
110 }
111
112 void TestPlatformAbstraction::SetIsLoadingResult(bool result)
113 {
114   mIsLoadingResult = result;
115 }
116
117 void TestPlatformAbstraction::ClearReadyResources()
118 {
119   mSynchronouslyLoadedResource.Reset();
120   mDecodedBitmap.Reset();
121 }
122
123 void TestPlatformAbstraction::SetClosestImageSize(const Vector2& size)
124 {
125   mClosestSize = ImageDimensions(static_cast<uint32_t>(size.x), static_cast<uint32_t>(size.y));
126 }
127
128 void TestPlatformAbstraction::SetLoadFileResult(bool result, Dali::Vector<unsigned char>& buffer)
129 {
130   mLoadFileResult.loadResult = result;
131   if(result)
132   {
133     mLoadFileResult.buffer = buffer;
134   }
135 }
136
137 void TestPlatformAbstraction::SetSaveFileResult(bool result)
138 {
139   mSaveFileResult = result;
140 }
141
142 void TestPlatformAbstraction::SetSynchronouslyLoadedResource(Integration::ResourcePointer resource)
143 {
144   mSynchronouslyLoadedResource = resource;
145 }
146
147 void TestPlatformAbstraction::SetDecodedBitmap(Integration::BitmapPtr bitmap)
148 {
149   mDecodedBitmap = bitmap;
150 }
151
152 uint32_t TestPlatformAbstraction::StartTimer(uint32_t milliseconds, CallbackBase* callback)
153 {
154   mCallbackFunction = callback;
155   mTimerId++;
156   return mTimerId;
157 }
158
159 void TestPlatformAbstraction::TriggerTimer()
160 {
161   if(mCallbackFunction != nullptr)
162   {
163     CallbackBase::Execute(*mCallbackFunction);
164   }
165 }
166
167 void TestPlatformAbstraction::CancelTimer(uint32_t timerId)
168 {
169   mCallbackFunction = nullptr;
170 }
171
172 } // namespace Dali