1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "core/components/test/unittest/theme/theme_mock.h"
17
18 #include "core/components/common/layout/constants.h"
19 #include "core/components/theme/theme_attributes.h"
20 #include "core/pipeline/pipeline_context.h"
21
22 namespace OHOS::Ace {
23 namespace {
24
25 std::unordered_map<int32_t, std::unordered_map<std::string, ResValueWrapper>> g_themes = {
26 { 0,
27 {
28 { THEME_ATTR_BG_COLOR, { .type = ThemeConstantsType::COLOR, .value = Color(0xffffff00) } },
29 { THEME_PATTERN_BUTTON, { .type = ThemeConstantsType::PATTERN, .value = 0U } },
30 } },
31 };
32
33 std::unordered_map<uint32_t, std::unordered_map<std::string, ResValueWrapper>> g_componentStyles = {
34 // buttonStyle
35 { 0,
36 {
37 { PATTERN_BG_COLOR, { .type = ThemeConstantsType::STATE_RESOURCE, .value = 0U } },
38 { PATTERN_TEXT_SIZE, { .type = ThemeConstantsType::STRING, .value = "?theme:textSizeButton1" } },
39 { PATTERN_WIDTH, { .type = ThemeConstantsType::DOUBLE, .value = 36.0 } },
40 { PATTERN_HEIGHT, { .type = ThemeConstantsType::DIMENSION, .value = 36.0_vp } },
41 } },
42 };
43
44 const std::unordered_map<uint32_t, std::unordered_map<uint32_t, ResValueWrapper>> STATE_ELEMENTS = {
45 // buttonBackgroundColor
46 { 0,
47 {
48 { STATE_NORMAL, { .type = ThemeConstantsType::COLOR, .value = Color(0x0c000000) } },
49 { STATE_PRESSED, { .type = ThemeConstantsType::COLOR, .value = Color(0x19000000) } },
50 } },
51 };
52
53 const std::unordered_map<uint32_t, ResValueWrapper> RESOURCES = {
54 { 117440511, { .type = ThemeConstantsType::COLOR, .value = Color(0xffffffff) } },
55 { 117440512, { .type = ThemeConstantsType::COLOR, .value = Color(0xffffffff) } },
56 { 117440513, { .type = ThemeConstantsType::DIMENSION, .value = 10.0_vp } },
57 { 117440514, { .type = ThemeConstantsType::DOUBLE, .value = 10.0 } },
58 { 117440515, { .type = ThemeConstantsType::INT, .value = -10 } },
59 { 117440516, { .type = ThemeConstantsType::STRING, .value = "sans-serif" } },
60 { 117441012, { .type = ThemeConstantsType::COLOR, .value = Color(0xff5434ff) } },
61 };
62
63 const std::unordered_map<std::string, ResValueWrapper> RESOURCES_WITH_NAME = {
64 { "sys.color.test_color", { .type = ThemeConstantsType::COLOR, .value = Color(0xffffffff) } },
65 { "sys.float.test_dimension", { .type = ThemeConstantsType::DIMENSION, .value = 10.0_vp } },
66 { "sys.double.test_double", { .type = ThemeConstantsType::DOUBLE, .value = 10.0 } },
67 { "sys.integer.test_int", { .type = ThemeConstantsType::INT, .value = -10 } },
68 { "sys.string.test_string", { .type = ThemeConstantsType::STRING, .value = "sans-serif" } },
69 { "sys.string.test_media_path", { .type = ThemeConstantsType::STRING, .value = "./user/a.png" } },
70 };
71
72 const Color ERROR_VALUE_COLOR = Color(0xff000000);
73 constexpr Dimension ERROR_VALUE_DIMENSION = 0.0_px;
74 constexpr double ERROR_VALUE_DOUBLE = 0.0;
75 constexpr int32_t ERROR_VALUE_INT = 0;
76
77 } // namespace
78
ResourceAdapterMock()79 ResourceAdapterMock::ResourceAdapterMock()
80 {
81 Initialize();
82 }
83
Initialize()84 void ResourceAdapterMock::Initialize()
85 {
86 // Replace state resource id with real content in g_componentStyles.
87 for (auto& iter : g_componentStyles) {
88 for (auto& [attr, valueWrapper] : iter.second) {
89 if (valueWrapper.type != ThemeConstantsType::STATE_RESOURCE) {
90 continue;
91 }
92 auto uintPtr = std::get_if<uint32_t>(&(valueWrapper.value));
93 if (!uintPtr) {
94 continue;
95 }
96 auto elementItr = STATE_ELEMENTS.find(*uintPtr);
97 if (elementItr == STATE_ELEMENTS.end()) {
98 continue;
99 }
100 auto states = AceType::MakeRefPtr<StateResource>();
101 for (const auto& [state, value] : elementItr->second) {
102 states->SetStateValue(state, value);
103 }
104 valueWrapper.type = ThemeConstantsType::STATE_RESOURCE;
105 valueWrapper.value = states;
106 }
107 }
108 for (auto& iter : g_themes) {
109 for (auto& [attr, valueWrapper] : iter.second) {
110 if (valueWrapper.type != ThemeConstantsType::PATTERN) {
111 continue;
112 }
113 auto uintPtr = std::get_if<uint32_t>(&(valueWrapper.value));
114 if (!uintPtr) {
115 continue;
116 }
117 auto styleItr = g_componentStyles.find(*uintPtr);
118 if (styleItr == g_componentStyles.end()) {
119 continue;
120 }
121 RefPtr<ThemeStyle> componentStyle = AceType::MakeRefPtr<ThemeStyle>();
122 for (const auto& [attr, value] : styleItr->second) {
123 componentStyle->SetAttr(attr, value);
124 }
125 valueWrapper.type = ThemeConstantsType::PATTERN;
126 valueWrapper.value = componentStyle;
127 }
128 }
129 }
130
GetTheme(int32_t themeId)131 RefPtr<ThemeStyle> ResourceAdapterMock::GetTheme(int32_t themeId)
132 {
133 auto findIter = g_themes.find(themeId);
134 if (findIter == g_themes.end()) {
135 return nullptr;
136 }
137 RefPtr<ThemeStyle> theme = AceType::MakeRefPtr<ThemeStyle>();
138 theme->SetAttributes(findIter->second);
139 return theme;
140 }
141
GetColor(uint32_t resId)142 Color ResourceAdapterMock::GetColor(uint32_t resId)
143 {
144 auto findIter = RESOURCES.find(resId);
145 if (findIter == RESOURCES.end()) {
146 return ERROR_VALUE_COLOR;
147 }
148 return findIter->second.GetValue<Color>(ERROR_VALUE_COLOR).second;
149 }
150
GetDimension(uint32_t resId)151 Dimension ResourceAdapterMock::GetDimension(uint32_t resId)
152 {
153 auto findIter = RESOURCES.find(resId);
154 if (findIter == RESOURCES.end()) {
155 return ERROR_VALUE_DIMENSION;
156 }
157 return findIter->second.GetValue<Dimension>(ERROR_VALUE_DIMENSION).second;
158 }
159
GetString(uint32_t resId)160 std::string ResourceAdapterMock::GetString(uint32_t resId)
161 {
162 auto findIter = RESOURCES.find(resId);
163 if (findIter == RESOURCES.end()) {
164 return "";
165 }
166 return findIter->second.GetValue<std::string>("").second;
167 }
168
169
GetStringArray(uint32_t resId) const170 std::vector<std::string> ResourceAdapterMock::GetStringArray(uint32_t resId) const
171 {
172 return {};
173 }
174
GetDouble(uint32_t resId)175 double ResourceAdapterMock::GetDouble(uint32_t resId)
176 {
177 auto findIter = RESOURCES.find(resId);
178 if (findIter == RESOURCES.end()) {
179 return ERROR_VALUE_DOUBLE;
180 }
181 return findIter->second.GetValue<double>(ERROR_VALUE_DOUBLE).second;
182 }
183
GetInt(uint32_t resId)184 int32_t ResourceAdapterMock::GetInt(uint32_t resId)
185 {
186 auto findIter = RESOURCES.find(resId);
187 if (findIter == RESOURCES.end()) {
188 return ERROR_VALUE_INT;
189 }
190 return findIter->second.GetValue<int32_t>(ERROR_VALUE_INT).second;
191 }
192
GetColorByName(const std::string & resName)193 Color ResourceAdapterMock::GetColorByName(const std::string& resName)
194 {
195 auto findIter = RESOURCES_WITH_NAME.find(resName);
196 if (findIter == RESOURCES_WITH_NAME.end()) {
197 return ERROR_VALUE_COLOR;
198 }
199 return findIter->second.GetValue<Color>(ERROR_VALUE_COLOR).second;
200 }
201
GetDimensionByName(const std::string & resName)202 Dimension ResourceAdapterMock::GetDimensionByName(const std::string& resName)
203 {
204 auto findIter = RESOURCES_WITH_NAME.find(resName);
205 if (findIter == RESOURCES_WITH_NAME.end()) {
206 return ERROR_VALUE_DIMENSION;
207 }
208 return findIter->second.GetValue<Dimension>(ERROR_VALUE_DIMENSION).second;
209 }
210
GetStringByName(const std::string & resName)211 std::string ResourceAdapterMock::GetStringByName(const std::string& resName)
212 {
213 auto findIter = RESOURCES_WITH_NAME.find(resName);
214 if (findIter == RESOURCES_WITH_NAME.end()) {
215 return "";
216 }
217 return findIter->second.GetValue<std::string>("").second;
218 }
219
GetStringArrayByName(const std::string & resName) const220 std::vector<std::string> ResourceAdapterMock::GetStringArrayByName(const std::string& resName) const
221 {
222 return {};
223 }
224
GetDoubleByName(const std::string & resName)225 double ResourceAdapterMock::GetDoubleByName(const std::string& resName)
226 {
227 auto findIter = RESOURCES_WITH_NAME.find(resName);
228 if (findIter == RESOURCES_WITH_NAME.end()) {
229 return ERROR_VALUE_DOUBLE;
230 }
231 return findIter->second.GetValue<double>(ERROR_VALUE_DOUBLE).second;
232 }
233
GetIntByName(const std::string & resName)234 int32_t ResourceAdapterMock::GetIntByName(const std::string& resName)
235 {
236 auto findIter = RESOURCES_WITH_NAME.find(resName);
237 if (findIter == RESOURCES_WITH_NAME.end()) {
238 return ERROR_VALUE_INT;
239 }
240 return findIter->second.GetValue<int32_t>(ERROR_VALUE_INT).second;
241 }
242
GetMediaPathByName(const std::string & resName)243 std::string ResourceAdapterMock::GetMediaPathByName(const std::string& resName)
244 {
245 auto findIter = RESOURCES_WITH_NAME.find(resName);
246 if (findIter == RESOURCES_WITH_NAME.end()) {
247 return "";
248 }
249 return findIter->second.GetValue<std::string>("").second;
250 }
251
GetIntArrayByName(const std::string & resName) const252 std::vector<uint32_t> ResourceAdapterMock::GetIntArrayByName(const std::string& resName) const
253 {
254 return {};
255 }
256
GetExplicitAnimationOption() const257 AnimationOption PipelineContext::GetExplicitAnimationOption() const
258 {
259 return {};
260 }
261
GetTimeFromExternalTimer()262 uint64_t PipelineContext::GetTimeFromExternalTimer()
263 {
264 return 0;
265 }
266
AddScheduleTask(const RefPtr<ScheduleTask> & task)267 uint32_t PipelineContext::AddScheduleTask(const RefPtr<ScheduleTask>& task)
268 {
269 return 0;
270 }
271
RemoveScheduleTask(uint32_t id)272 void PipelineContext::RemoveScheduleTask(uint32_t id) {}
273
Animate(const AnimationOption & option,const RefPtr<Curve> & curve,const std::function<void ()> & propertyCallback,const std::function<void ()> & finishCallBack)274 bool PipelineBase::Animate(const AnimationOption& option, const RefPtr<Curve>& curve,
275 const std::function<void()>& propertyCallback, const std::function<void()>& finishCallBack)
276 {
277 return true;
278 }
279
OpenImplicitAnimation(const AnimationOption & option,const RefPtr<Curve> & curve,const std::function<void ()> & finishCallBack)280 void PipelineBase::OpenImplicitAnimation(
281 const AnimationOption& option, const RefPtr<Curve>& curve, const std::function<void()>& finishCallBack)
282 {}
283
CloseImplicitAnimation()284 bool PipelineBase::CloseImplicitAnimation()
285 {
286 return true;
287 }
288
PrepareOpenImplicitAnimation()289 void PipelineBase::PrepareOpenImplicitAnimation() {}
290
PrepareCloseImplicitAnimation()291 void PipelineBase::PrepareCloseImplicitAnimation() {}
292
GetIsDeclarative() const293 bool PipelineContext::GetIsDeclarative() const
294 {
295 return true;
296 }
297
AddGeometryChangedNode(const RefPtr<RenderNode> & renderNode)298 void PipelineContext::AddGeometryChangedNode(const RefPtr<RenderNode>& renderNode) {}
299
PostAsyncEvent(const std::function<void ()> & task,TaskExecutor::TaskType type)300 void PipelineBase::PostAsyncEvent(const std::function<void()>& task, TaskExecutor::TaskType type) {}
301
PostAsyncEvent(std::function<void ()> && task,TaskExecutor::TaskType type)302 void PipelineBase::PostAsyncEvent(std::function<void()>&& task, TaskExecutor::TaskType type) {}
303
304 } // namespace OHOS::Ace
305