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