1 /*
2 * Copyright (c) 2021-2022 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 <unordered_set>
17
18 #include "gtest/gtest.h"
19
20 #define private public
21 #include "base/log/log.h"
22 #include "base/utils/system_properties.h"
23 #include "core/components/button/button_theme.h"
24 #include "core/components/checkable/checkable_theme.h"
25 #include "core/components/dialog/dialog_theme.h"
26 #include "core/components/marquee/marquee_theme.h"
27 #include "core/components/popup/popup_theme.h"
28 #include "core/components/progress/progress_theme.h"
29 #include "core/components/select/select_theme.h"
30 #include "core/components/slider/slider_theme.h"
31 #include "core/components/stepper/stepper_theme.h"
32 #include "core/components/swiper/swiper_indicator_theme.h"
33 #include "core/components/test/unittest/theme/theme_mock.h"
34 #include "core/components/text/text_theme.h"
35 #include "core/components/theme/app_theme.h"
36 #include "core/components/theme/theme_attributes.h"
37 #include "core/components/theme/theme_constants.h"
38 #include "core/components/theme/theme_constants_defines.h"
39 #include "core/components/theme/theme_manager.h"
40 #include "core/components/toast/toast_theme.h"
41 #include "core/components/video/video_theme.h"
42
43 using namespace testing;
44 using namespace testing::ext;
45
46 namespace OHOS::Ace {
47 namespace {
48
49 RefPtr<ThemeConstants> g_themeConstants = nullptr;
50 // Same with value defined at ThemeConstants
51 const Color COLOR_CONTROL_ACTIVATED_PHONE = Color(0xff0a59f7);
52 const Color COLOR_ERROR = Color(0xff000000);
53 const Dimension BUTTON_TEXT_FONTSIZE_PHONE = Dimension(16.0, DimensionUnit::FP);
54 const Dimension DIMENSION_ERROR = Dimension(0.0, DimensionUnit::VP);
55 const Color CUSTOM_COLOR_FG = Color(0xffffff00);
56 constexpr double CUSTOM_ALPHA = 0.5;
57 constexpr double CUSTOM_LINE_HEIGHT = 1.5;
58 constexpr int32_t CUSTOM_ANIM_DURATION = 500;
59 const Dimension CUSTOM_FONT_SIZE = Dimension(10.0, DimensionUnit::PX);
60 const Dimension CUSTOM_MARGIN = Dimension(10.0, DimensionUnit::PX);
61 constexpr uint32_t STYLE_ID_INVALID = 9999;
62 const Dimension PARSE_DIMENSION_ERROR = Dimension(0.0, DimensionUnit::PX);
63 // test value same with ResourceAdapterMock
64 constexpr uint32_t THEME_ID_VALID = 0;
65 constexpr uint32_t THEME_ID_INVALID = 1;
66 const std::string TEXT_SIZE_VALUE = "?theme:textSizeButton1";
67 constexpr double SYS_RES_BUTTON_WIDTH = 36.0;
68 const Dimension SYS_RES_BUTTON_HEIGHT = Dimension(36.0, DimensionUnit::VP);
69 const Color STATE_NORMAL_COLOR = Color(0x0c000000);
70 const Color STATE_PRESSED_COLOR = Color(0x19000000);
71 const Color THEME_BG_COLOR_TRANSPARENT = Color(0x33000000);
72 constexpr uint32_t SYS_RES_ID_COLOR = 117440512;
73 constexpr uint32_t SYS_RES_ID_DIMENSION = 117440513;
74 constexpr uint32_t SYS_RES_ID_DOUBLE = 117440514;
75 constexpr uint32_t SYS_RES_ID_INT = 117440515;
76 constexpr uint32_t SYS_RES_ID_STRING = 117440516;
77 const std::string SYS_RES_NAME_COLOR = "sys.color.test_color";
78 const std::string SYS_RES_NAME_DIMENSION = "sys.float.test_dimension";
79 const std::string SYS_RES_NAME_DOUBLE = "sys.double.test_double";
80 const std::string SYS_RES_NAME_INT = "sys.integer.test_int";
81 const std::string SYS_RES_NAME_STRING = "sys.string.test_string";
82 const std::string SYS_RES_NAME_MEDIA_PATH = "sys.string.test_media_path";
83 const std::string SYS_RES_NAME_ERROR_PATH = "sys.error.test_error";
84 const Color SYS_VALUE_COLOR = Color(0xffffffff);
85 const Dimension SYS_VALUE_DIMENSION = Dimension(10.0, DimensionUnit::VP);
86 constexpr double SYS_VALUE_DOUBLE = 10.0;
87 constexpr int32_t SYS_VALUE_INT = -10;
88 const std::string SYS_VALUE_STRING = "sans-serif";
89 const std::string SYS_VALUE_MEDIA_PATH = "./user/a.png";
90
91 } // namespace
92
93 class ThemeConstantsTest : public testing::Test {
94 public:
95 static void SetUpTestCase();
96 static void TearDownTestCase();
97 void SetUp();
98 void TearDown();
99 };
100
SetUpTestCase()101 void ThemeConstantsTest::SetUpTestCase()
102 {
103 auto resAdapter = AceType::MakeRefPtr<ResourceAdapterMock>();
104 g_themeConstants = AceType::MakeRefPtr<ThemeConstants>(resAdapter);
105 }
106
TearDownTestCase()107 void ThemeConstantsTest::TearDownTestCase() {}
SetUp()108 void ThemeConstantsTest::SetUp() {}
TearDown()109 void ThemeConstantsTest::TearDown() {}
110
111 /**
112 * @tc.name: PlatformConstants001
113 * @tc.desc: Test get default theme at phone.
114 * @tc.type: FUNC
115 * @tc.require: issueI5IY7K
116 */
117 HWTEST_F(ThemeConstantsTest, PlatformConstants001, TestSize.Level1)
118 {
119 /**
120 * @tc.steps: step1. Mock device type is phone.
121 */
122 SystemProperties::SetDeviceType(DeviceType::PHONE);
123 ThemeConstants::InitDeviceType();
124
125 /**
126 * @tc.steps: step2. Get color THEME_OHOS_COLOR_CONTROL_ACTIVATED, check THEME_COLOR_FOREGROUND is 0xff0a59f7.
127 * @tc.expected: step2. Color is 0xff0a59f7.
128 */
129 auto color = g_themeConstants->GetColor(THEME_OHOS_COLOR_CONTROL_ACTIVATED);
130 EXPECT_EQ(color.GetValue(), COLOR_CONTROL_ACTIVATED_PHONE.GetValue());
131 }
132
133 #ifndef WEARABLE_PRODUCT
134 /**
135 * @tc.name: PlatformConstants002
136 * @tc.desc: Test get default theme at tv.
137 * @tc.type: FUNC
138 * @tc.require: issueI5IY7K
139 */
140 HWTEST_F(ThemeConstantsTest, PlatformConstants002, TestSize.Level1)
141 {
142 /**
143 * @tc.steps: step1. Mock device type is tv.
144 */
145 SystemProperties::SetDeviceType(DeviceType::TV);
146 ThemeConstants::InitDeviceType();
147
148 /**
149 * @tc.steps: step2. Get color THEME_OHOS_COLOR_CONTROL_ACTIVATED, check THEME_COLOR_FOREGROUND is 0xff266efb.
150 * @tc.expected: step2. Color is 0xff266efb.
151 */
152 auto color = g_themeConstants->GetColor(THEME_OHOS_COLOR_CONTROL_ACTIVATED);
153 EXPECT_EQ(color.GetValue(), COLOR_CONTROL_ACTIVATED_PHONE.GetValue());
154 }
155 #endif
156
157 /**
158 * @tc.name: PlatformConstants003
159 * @tc.desc: Test get default theme at other platform.
160 * @tc.type: FUNC
161 * @tc.require: issueI5IY7K
162 */
163 HWTEST_F(ThemeConstantsTest, PlatformConstants003, TestSize.Level1)
164 {
165 /**
166 * @tc.steps: step1. Mock device type is unknown.
167 */
168 SystemProperties::SetDeviceType(DeviceType::UNKNOWN);
169 ThemeConstants::InitDeviceType();
170
171 /**
172 * @tc.steps: step2. Get color THEME_OHOS_COLOR_CONTROL_ACTIVATED, check THEME_COLOR_FOREGROUND is 0xff0a59f7.
173 * @tc.expected: step2. Color is 0xff0a59f7.
174 */
175 auto color = g_themeConstants->GetColor(THEME_OHOS_COLOR_CONTROL_ACTIVATED);
176 EXPECT_EQ(color.GetValue(), COLOR_CONTROL_ACTIVATED_PHONE.GetValue());
177 }
178
179 /**
180 * @tc.name: ConstantsDefine001
181 * @tc.desc: Test get value with key corresponding with correct value type.
182 * @tc.type: FUNC
183 * @tc.require: issueI5IY7K
184 */
185 HWTEST_F(ThemeConstantsTest, ConstantsDefine001, TestSize.Level1)
186 {
187 /**
188 * @tc.steps: step1. Mock device type is phone.
189 */
190 SystemProperties::SetDeviceType(DeviceType::PHONE);
191 ThemeConstants::InitDeviceType();
192
193 /**
194 * @tc.steps: step2. Get color, dimension with correct key.
195 * @tc.expected: step2. Returned value is correct.
196 */
197 auto color = g_themeConstants->GetColor(THEME_OHOS_COLOR_CONTROL_ACTIVATED);
198 EXPECT_EQ(color.GetValue(), COLOR_CONTROL_ACTIVATED_PHONE.GetValue());
199 auto dimension = g_themeConstants->GetDimension(THEME_BUTTON_TEXT_FONTSIZE);
200 EXPECT_TRUE(NearEqual(dimension.Value(), BUTTON_TEXT_FONTSIZE_PHONE.Value()));
201 EXPECT_EQ(dimension.Unit(), BUTTON_TEXT_FONTSIZE_PHONE.Unit());
202 }
203
204 /**
205 * @tc.name: ConstantsDefine002
206 * @tc.desc: Test get value with key corresponding with incorrect value type.
207 * @tc.type: FUNC
208 * @tc.require: issueI5IY7K
209 */
210 HWTEST_F(ThemeConstantsTest, ConstantsDefine002, TestSize.Level1)
211 {
212 /**
213 * @tc.steps: step1. Get color, dimension with incorrect key.
214 * @tc.expected: step1. Returned value is correct.
215 */
216 auto color = g_themeConstants->GetColor(THEME_BUTTON_TEXT_FONTSIZE);
217 EXPECT_EQ(color.GetValue(), COLOR_ERROR.GetValue());
218 auto dimension = g_themeConstants->GetDimension(THEME_OHOS_COLOR_CONTROL_ACTIVATED);
219 EXPECT_TRUE(NearEqual(dimension.Value(), DIMENSION_ERROR.Value()));
220 EXPECT_EQ(dimension.Unit(), DIMENSION_ERROR.Unit());
221 }
222
223 /**
224 * @tc.name: ParseStyle001
225 * @tc.desc: Parse user input custom style config.
226 * @tc.type: FUNC
227 * @tc.require: issueI5IY7K
228 */
229 HWTEST_F(ThemeConstantsTest, ParseStyle001, TestSize.Level1)
230 {
231 /**
232 * @tc.steps: step1. Construct custom style json input.
233 */
234 auto resAdapter = AceType::MakeRefPtr<ResourceAdapterMock>();
235 auto themeConstants = AceType::MakeRefPtr<ThemeConstants>(resAdapter);
236 const std::string jsonStr = "{ "
237 " \"style\": { "
238 " \"000\": \"#ffff00\", "
239 " \"400\": \"0.5\", "
240 " \"500\": \"10px\", "
241 " \"550\": \"HwChinese-bold\", "
242 " \"700\": \"10px\", "
243 " \"780\": \"1.5\", "
244 " \"2020\": \"500\" "
245 " }"
246 "}";
247
248 /**
249 * @tc.steps: step2. Parse input json string, check style map.
250 * @tc.expected: step2. Value in ThemeConstants is same with input custom style.
251 */
252 themeConstants->ParseCustomStyle(jsonStr);
253 EXPECT_EQ(themeConstants->GetColor(THEME_OHOS_COLOR_FG).GetValue(), CUSTOM_COLOR_FG.GetValue());
254 EXPECT_TRUE(NearEqual(themeConstants->GetDouble(THEME_OHOS_PRIMARY_CONTENT_ALPHA), CUSTOM_ALPHA));
255 EXPECT_EQ(themeConstants->GetDimension(THEME_OHOS_TEXT_SIZE_HEADLINE1), CUSTOM_FONT_SIZE);
256 EXPECT_EQ(themeConstants->GetDimension(THEME_OHOS_DIMENS_DEFAULT_START), CUSTOM_MARGIN);
257 EXPECT_TRUE(NearEqual(themeConstants->GetDouble(THEME_OHOS_TEXT_SIZE_SPACE_SHORT), CUSTOM_LINE_HEIGHT));
258 EXPECT_EQ(themeConstants->GetInt(THEME_DIALOG_ANIMATION_DURATION_IN), CUSTOM_ANIM_DURATION);
259 }
260
261 /**
262 * @tc.name: ParseStyle002
263 * @tc.desc: Parse user input custom style config, value format error.
264 * @tc.type: FUNC
265 */
266 HWTEST_F(ThemeConstantsTest, ParseStyle002, TestSize.Level1)
267 {
268 /**
269 * @tc.steps: step1. Construct custom style json input.
270 */
271 auto resAdapter = AceType::MakeRefPtr<ResourceAdapterMock>();
272 auto themeConstants = AceType::MakeRefPtr<ThemeConstants>(resAdapter);
273 const std::string jsonStr = "{ "
274 " \"style\": { "
275 " \"000\": \"0.5\", "
276 " \"400\": \"#ff0000\", "
277 " \"500\": \"#ff0000\", "
278 " \"550\": \"400\", "
279 " \"700\": \"#ff0000\", "
280 " \"780\": \"#ff0000\", "
281 " \"2020\": \"#ff0000\" "
282 " }"
283 "}";
284
285 /**
286 * @tc.steps: step2. Parse input json string, check style map.
287 * @tc.expected: step2. Value in ThemeConstants is not changed, except font family.
288 */
289 themeConstants->ParseCustomStyle(jsonStr);
290 EXPECT_EQ(themeConstants->GetColor(THEME_OHOS_COLOR_FG).GetValue(), COLOR_ERROR.GetValue());
291 EXPECT_TRUE(NearEqual(themeConstants->GetDouble(THEME_OHOS_PRIMARY_CONTENT_ALPHA), 0.0));
292 EXPECT_EQ(themeConstants->GetDimension(THEME_OHOS_TEXT_SIZE_HEADLINE1), PARSE_DIMENSION_ERROR);
293 EXPECT_EQ(themeConstants->GetDimension(THEME_OHOS_DIMENS_DEFAULT_START), PARSE_DIMENSION_ERROR);
294 EXPECT_TRUE(NearEqual(themeConstants->GetDouble(THEME_OHOS_TEXT_SIZE_SPACE_SHORT), 0.0));
295 EXPECT_EQ(themeConstants->GetInt(THEME_DIALOG_ANIMATION_DURATION_IN), 0);
296 }
297
298 /**
299 * @tc.name: ParseStyle003
300 * @tc.desc: Parse user input custom style config, id not found.
301 * @tc.type: FUNC
302 */
303 HWTEST_F(ThemeConstantsTest, ParseStyle003, TestSize.Level1)
304 {
305 /**
306 * @tc.steps: step1. Construct custom style json input.
307 */
308 const std::string jsonStr = "{ "
309 " \"style\": { "
310 " \"9999\": \"0.5\", "
311 " }"
312 "}";
313
314 /**
315 * @tc.steps: step2. Parse input json string, check style map.
316 * @tc.expected: step2. Key 9999 in ThemeConstants is not exist.
317 */
318 g_themeConstants->ParseCustomStyle(jsonStr);
319 EXPECT_EQ(g_themeConstants->GetValue(STYLE_ID_INVALID).type, ThemeConstantsType::ERROR);
320 }
321
322 /**
323 * @tc.name: ParseStyle004
324 * @tc.desc: Parse user input custom style config, id not public.
325 * @tc.type: FUNC
326 */
327 HWTEST_F(ThemeConstantsTest, ParseStyle004, TestSize.Level1)
328 {
329 /**
330 * @tc.steps: step1. Construct custom style json input. Cache current style value.
331 */
332 const std::string jsonStr = "{ "
333 " \"style\": { "
334 " \"136\": \"#ff0000\", "
335 " }"
336 "}";
337 const auto cacheColor = g_themeConstants->GetColor(THEME_OHOS_COLOR_TIPS_BG);
338
339 /**
340 * @tc.steps: step2. Parse input json string, check style map.
341 * @tc.expected: step2. Style in ThemeConstants is not changed.
342 */
343 g_themeConstants->ParseCustomStyle(jsonStr);
344 EXPECT_EQ(g_themeConstants->GetColor(THEME_OHOS_COLOR_TIPS_BG).GetValue(), cacheColor.GetValue());
345 }
346
347 /**
348 * @tc.name: ThemeStyleRead001
349 * @tc.desc: Initialize theme style correctly.
350 * @tc.type: FUNC
351 */
352 HWTEST_F(ThemeConstantsTest, ThemeStyleRead001, TestSize.Level1)
353 {
354 /**
355 * @tc.steps: step1. Construct theme constants with mock adapter, load theme resource.
356 */
357 auto resAdapter = AceType::MakeRefPtr<ResourceAdapterMock>();
358 auto themeConstants = AceType::MakeRefPtr<ThemeConstants>(resAdapter);
359 themeConstants->LoadTheme(THEME_ID_VALID);
360
361 /**
362 * @tc.steps: step2. Check currentThemeStyle_ of themeConstants.
363 * @tc.expected: step2. Style content in currentThemeStyle_ is same with mock data.
364 */
365 auto themeStyle = themeConstants->GetThemeStyle();
366 ASSERT_NE(themeStyle, nullptr);
367 // app background color
368 EXPECT_EQ(themeStyle->GetAttr<Color>(THEME_ATTR_BG_COLOR, Color()).GetValue(), CUSTOM_COLOR_FG.GetValue());
369 // button style
370 auto buttonStyle = themeStyle->GetAttr<RefPtr<ThemeStyle>>(THEME_PATTERN_BUTTON, RefPtr<ThemeStyle>());
371 ASSERT_NE(buttonStyle, nullptr);
372 // button background color
373 auto buttonBgStates = buttonStyle->GetAttr<RefPtr<StateResource>>(PATTERN_BG_COLOR, RefPtr<StateResource>());
374 ASSERT_NE(buttonBgStates, nullptr);
375 EXPECT_EQ(
376 buttonBgStates->GetState<Color>(STATE_NORMAL, Color()).GetValue(), STATE_NORMAL_COLOR.GetValue());
377 EXPECT_EQ(
378 buttonBgStates->GetState<Color>(STATE_PRESSED, Color()).GetValue(), STATE_PRESSED_COLOR.GetValue());
379 // button text size
380 EXPECT_EQ(buttonStyle->GetAttr<std::string>(PATTERN_TEXT_SIZE, ""), TEXT_SIZE_VALUE);
381 // button width
382 EXPECT_TRUE(NearEqual(buttonStyle->GetAttr<double>(PATTERN_WIDTH, 0.0), SYS_RES_BUTTON_WIDTH));
383 // button height
384 EXPECT_EQ(buttonStyle->GetAttr<Dimension>(PATTERN_HEIGHT, 0.0_vp), SYS_RES_BUTTON_HEIGHT);
385 }
386
387 /**
388 * @tc.name: ThemeStyleRead002
389 * @tc.desc: Initialize theme style, get attributes with wrong type.
390 * @tc.type: FUNC
391 */
392 HWTEST_F(ThemeConstantsTest, ThemeStyleRead002, TestSize.Level1)
393 {
394 /**
395 * @tc.steps: step1. Construct theme constants with mock adapter, load theme resource.
396 */
397 auto resAdapter = AceType::MakeRefPtr<ResourceAdapterMock>();
398 auto themeConstants = AceType::MakeRefPtr<ThemeConstants>(resAdapter);
399 themeConstants->LoadTheme(THEME_ID_VALID);
400
401 /**
402 * @tc.steps: step2. Check currentThemeStyle_ of themeConstants.
403 * @tc.expected: step2. Style content in currentThemeStyle_ is same with mock data.
404 */
405 auto themeStyle = themeConstants->GetThemeStyle();
406 ASSERT_NE(themeStyle, nullptr);
407 // app background color
408 auto bgColor = themeStyle->GetAttr<Dimension>(THEME_ATTR_BG_COLOR, Dimension());
409 EXPECT_EQ(bgColor.Value(), 0.0);
410 EXPECT_EQ(bgColor.Unit(), DimensionUnit::PX);
411 // button style
412 auto buttonStyle = themeStyle->GetAttr<RefPtr<ThemeStyle>>(THEME_PATTERN_BUTTON, RefPtr<ThemeStyle>());
413 ASSERT_NE(buttonStyle, nullptr);
414 // button background color
415 auto buttonBgStyle = buttonStyle->GetAttr<double>(PATTERN_BG_COLOR, 0.0);
416 EXPECT_TRUE(NearEqual(buttonBgStyle, 0.0));
417 // button text size
418 EXPECT_EQ(buttonStyle->GetAttr<RefPtr<ThemeStyle>>(PATTERN_TEXT_SIZE, RefPtr<ThemeStyle>()), nullptr);
419 // button width
420 EXPECT_EQ(buttonStyle->GetAttr<Color>(PATTERN_WIDTH, Color()).GetValue(), Color().GetValue());
421 // button height
422 EXPECT_EQ(buttonStyle->GetAttr<std::string>(PATTERN_HEIGHT, ""), "");
423 }
424
425 /**
426 * @tc.name: ThemeStyleRead003
427 * @tc.desc: Initialize theme style with incorrect theme id.
428 * @tc.type: FUNC
429 */
430 HWTEST_F(ThemeConstantsTest, ThemeStyleRead003, TestSize.Level1)
431 {
432 /**
433 * @tc.steps: step1. Construct theme constants with mock adapter, load theme resource with invalid theme id.
434 */
435 auto resAdapter = AceType::MakeRefPtr<ResourceAdapterMock>();
436 auto themeConstants = AceType::MakeRefPtr<ThemeConstants>(resAdapter);
437 themeConstants->LoadTheme(THEME_ID_INVALID);
438
439 /**
440 * @tc.steps: step2. Check currentThemeStyle_ of themeConstants.
441 * @tc.expected: step2. currentThemeStyle_ is nullptr.
442 */
443 EXPECT_EQ(themeConstants->currentThemeStyle_, nullptr);
444 }
445
446 /**
447 * @tc.name: ThemeStyleRead004
448 * @tc.desc: Initialize theme style correctly, override app background color.
449 * @tc.type: FUNC
450 */
451 HWTEST_F(ThemeConstantsTest, ThemeStyleRead004, TestSize.Level1)
452 {
453 /**
454 * @tc.steps: step1. Construct theme constants with mock adapter, load theme resource, set ColorScheme::TRANSPARENT.
455 */
456 auto resAdapter = AceType::MakeRefPtr<ResourceAdapterMock>();
457 auto themeConstants = AceType::MakeRefPtr<ThemeConstants>(resAdapter);
458 themeConstants->LoadTheme(THEME_ID_VALID);
459 themeConstants->SetColorScheme(ColorScheme::SCHEME_TRANSPARENT);
460
461 /**
462 * @tc.steps: step2. Check app background color of currentThemeStyle_.
463 * @tc.expected: step2. App background color is .
464 */
465 auto themeStyle = themeConstants->GetThemeStyle();
466 EXPECT_EQ(
467 themeStyle->GetAttr<Color>(THEME_ATTR_BG_COLOR, Color()).GetValue(), THEME_BG_COLOR_TRANSPARENT.GetValue());
468 }
469
470 /**
471 * @tc.name: ThemeResourceRead001
472 * @tc.desc: Read theme resource correctly.
473 * @tc.type: FUNC
474 */
475 HWTEST_F(ThemeConstantsTest, ThemeResourceRead001, TestSize.Level1)
476 {
477 /**
478 * @tc.steps: step1. Construct theme constants with mock adapter, load theme resource.
479 */
480 auto resAdapter = AceType::MakeRefPtr<ResourceAdapterMock>();
481 auto themeConstants = AceType::MakeRefPtr<ThemeConstants>(resAdapter);
482
483 /**
484 * @tc.steps: step2. Get different type of resources from themeConstants.
485 * @tc.expected: step2. Resource value is same with ResourceAdapterMock.
486 */
487 auto color = themeConstants->GetColor(SYS_RES_ID_COLOR);
488 EXPECT_EQ(color.GetValue(), SYS_VALUE_COLOR.GetValue());
489 auto dimension = themeConstants->GetDimension(SYS_RES_ID_DIMENSION);
490 EXPECT_TRUE(NearEqual(dimension.Value(), SYS_VALUE_DIMENSION.Value()));
491 EXPECT_EQ(dimension.Unit(), SYS_VALUE_DIMENSION.Unit());
492 auto doubleValue = themeConstants->GetDouble(SYS_RES_ID_DOUBLE);
493 EXPECT_TRUE(NearEqual(doubleValue, SYS_VALUE_DOUBLE));
494 auto intValue = themeConstants->GetInt(SYS_RES_ID_INT);
495 EXPECT_EQ(intValue, SYS_VALUE_INT);
496 auto str = themeConstants->GetString(SYS_RES_ID_STRING);
497 EXPECT_EQ(str, SYS_VALUE_STRING);
498 }
499
500 /**
501 * @tc.name: ThemeResourceRead002
502 * @tc.desc: Read theme resource with wrong type.
503 * @tc.type: FUNC
504 */
505 HWTEST_F(ThemeConstantsTest, ThemeResourceRead002, TestSize.Level1)
506 {
507 /**
508 * @tc.steps: step1. Construct theme constants with mock adapter, load theme resource.
509 */
510 auto resAdapter = AceType::MakeRefPtr<ResourceAdapterMock>();
511 auto themeConstants = AceType::MakeRefPtr<ThemeConstants>(resAdapter);
512
513 /**
514 * @tc.steps: step2. Get value from themeConstants use wrong interface.
515 * @tc.expected: step2. Resource value is same with error value.
516 */
517 auto dimension = themeConstants->GetDimension(SYS_RES_ID_COLOR);
518 EXPECT_TRUE(NearEqual(dimension.Value(), 0.0));
519 EXPECT_EQ(dimension.Unit(), DimensionUnit::PX);
520 auto doubleValue = themeConstants->GetDouble(SYS_RES_ID_DIMENSION);
521 EXPECT_TRUE(NearEqual(doubleValue, 0.0));
522 auto intValue = themeConstants->GetInt(SYS_RES_ID_DOUBLE);
523 EXPECT_EQ(intValue, 0);
524 auto str = themeConstants->GetString(SYS_RES_ID_INT);
525 EXPECT_EQ(str, "");
526 auto color = themeConstants->GetColor(SYS_RES_ID_STRING);
527 EXPECT_EQ(color.GetValue(), Color().GetValue());
528 }
529
530 /**
531 * @tc.name: MultiInstance001
532 * @tc.desc: Parse custom style with multi theme constants.
533 * @tc.type: FUNC
534 */
535 HWTEST_F(ThemeConstantsTest, MultiInstance001, TestSize.Level1)
536 {
537 /**
538 * @tc.steps: step1. Construct custom style json input.
539 */
540 auto resAdapter = AceType::MakeRefPtr<ResourceAdapterMock>();
541 auto themeConstantsA = AceType::MakeRefPtr<ThemeConstants>(resAdapter);
542 auto themeConstantsB = AceType::MakeRefPtr<ThemeConstants>(resAdapter);
543 const std::string jsonStrA = "{ "
544 " \"style\": { "
545 " \"000\": \"#ff0000\" "
546 " }"
547 "}";
548 const std::string jsonStrB = "{ "
549 " \"style\": { "
550 " \"000\": \"#0000ff\" "
551 " }"
552 "}";
553
554 /**
555 * @tc.steps: step2. Parse input json string, check style map.
556 * @tc.expected: step2. Value in ThemeConstants is not changed, except font family.
557 */
558 themeConstantsA->ParseCustomStyle(jsonStrA);
559 themeConstantsB->ParseCustomStyle(jsonStrB);
560 EXPECT_EQ(themeConstantsA->GetColor(THEME_OHOS_COLOR_FG).GetValue(), Color::RED.GetValue());
561 EXPECT_EQ(themeConstantsB->GetColor(THEME_OHOS_COLOR_FG).GetValue(), Color::BLUE.GetValue());
562 }
563
564 /**
565 * @tc.name: ParseIdStyle001
566 * @tc.desc: Parse user input id style config.
567 * @tc.type: FUNC
568 */
569 HWTEST_F(ThemeConstantsTest, ParseIdStyle001, TestSize.Level1)
570 {
571 /**
572 * @tc.steps: step1. Construct custom style json input.
573 */
574 auto resAdapter = AceType::MakeRefPtr<ResourceAdapterMock>();
575 auto themeConstants = AceType::MakeRefPtr<ThemeConstants>(resAdapter);
576 const std::string jsonStr = "@ohos_id_500";
577 auto parseResult = ThemeUtils::ParseThemeIdReference(jsonStr);
578 auto parseColor = themeConstants->GetColor(parseResult.id);
579
580 /**
581 * @tc.steps: step2. Check style map.
582 * @tc.expected: step2. Style in ThemeConstants is not changed.
583 */
584 Color correctColor = Color(0xff5434ff);
585 EXPECT_EQ(correctColor.GetValue(), parseColor.GetValue());
586 }
587
588 /**
589 * @tc.name: ThemeResourceReadByName001
590 * @tc.desc: Read theme resource by name correctly.
591 * @tc.type: FUNC
592 */
593 HWTEST_F(ThemeConstantsTest, ThemeResourceReadByName001, TestSize.Level1)
594 {
595 /**
596 * @tc.steps: step1. Construct theme constants with mock adapter, load theme resource.
597 */
598 auto resAdapter = AceType::MakeRefPtr<ResourceAdapterMock>();
599 auto themeConstants = AceType::MakeRefPtr<ThemeConstants>(resAdapter);
600
601 /**
602 * @tc.steps: step2. Get different type of resources from themeConstants.
603 * @tc.expected: step2. Resource value is same with ResourceAdapterMock.
604 */
605 auto color = themeConstants->GetColorByName(SYS_RES_NAME_COLOR);
606 EXPECT_EQ(color.GetValue(), SYS_VALUE_COLOR.GetValue());
607 auto dimension = themeConstants->GetDimensionByName(SYS_RES_NAME_DIMENSION);
608 EXPECT_TRUE(NearEqual(dimension.Value(), SYS_VALUE_DIMENSION.Value()));
609 EXPECT_EQ(dimension.Unit(), SYS_VALUE_DIMENSION.Unit());
610 auto doubleValue = themeConstants->GetDoubleByName(SYS_RES_NAME_DOUBLE);
611 EXPECT_TRUE(NearEqual(doubleValue, SYS_VALUE_DOUBLE));
612 auto intValue = themeConstants->GetIntByName(SYS_RES_NAME_INT);
613 EXPECT_EQ(intValue, SYS_VALUE_INT);
614 auto str = themeConstants->GetStringByName(SYS_RES_NAME_STRING);
615 EXPECT_EQ(str, SYS_VALUE_STRING);
616 auto mediaPath = themeConstants->GetMediaPathByName(SYS_RES_NAME_MEDIA_PATH);
617 EXPECT_EQ(mediaPath, SYS_VALUE_MEDIA_PATH);
618 }
619
620 /**
621 * @tc.name: ThemeResourceReadByName002
622 * @tc.desc: Read theme resource by name with wrong type.
623 * @tc.type: FUNC
624 */
625 HWTEST_F(ThemeConstantsTest, ThemeResourceReadByName002, TestSize.Level1)
626 {
627 /**
628 * @tc.steps: step1. Construct theme constants with mock adapter, load theme resource.
629 */
630 auto resAdapter = AceType::MakeRefPtr<ResourceAdapterMock>();
631 auto themeConstants = AceType::MakeRefPtr<ThemeConstants>(resAdapter);
632
633 /**
634 * @tc.steps: step2. Get value from themeConstants use wrong interface.
635 * @tc.expected: step2. Resource value is same with error value.
636 */
637 auto dimension = themeConstants->GetDimensionByName(SYS_RES_NAME_ERROR_PATH);
638 EXPECT_TRUE(NearEqual(dimension.Value(), 0.0));
639 EXPECT_EQ(dimension.Unit(), DimensionUnit::PX);
640 auto doubleValue = themeConstants->GetDoubleByName(SYS_RES_NAME_ERROR_PATH);
641 EXPECT_TRUE(NearEqual(doubleValue, 0.0));
642 auto intValue = themeConstants->GetIntByName(SYS_RES_NAME_ERROR_PATH);
643 EXPECT_EQ(intValue, 0);
644 auto str = themeConstants->GetStringByName(SYS_RES_NAME_ERROR_PATH);
645 EXPECT_EQ(str, "");
646 auto color = themeConstants->GetColorByName(SYS_RES_NAME_ERROR_PATH);
647 EXPECT_EQ(color.GetValue(), Color().GetValue());
648 auto mediaPath = themeConstants->GetMediaPathByName(SYS_RES_NAME_ERROR_PATH);
649 EXPECT_EQ(mediaPath, "");
650 }
651
652 } // namespace OHOS::Ace
653