• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "gtest/gtest.h"
17 
18 #include "core/components/theme/theme_constants_defines.h"
19 #include "core/components/theme/theme_manager_impl.h"
20 #include "core/components/theme/theme_utils.h"
21 #include "frameworks/bridge/common/utils/utils.h"
22 
23 using namespace testing;
24 using namespace testing::ext;
25 
26 namespace OHOS::Ace::Framework {
27 namespace {
28 
29 const std::string DIMENSION_ID_NORMAL = "\"@id500\"";
30 const std::string DIMENSION_ID_NOT_FOUND = "\"@id9999\"";
31 const std::string DIMENSION_ID_ERROR[] = { "", "\"@id\"", "\"@idabc\"", "\"@idid003\"", "\"@003\"",
32     "\"@id001@id003\"" };
33 const std::string OHOS_THEME_ID_NORMAL = "@ohos_id_500";
34 
35 } // namespace
36 
37 class JsUtilsTest : public testing::Test {
38 public:
39     static void SetUpTestCase();
40     static void TearDownTestCase();
41     void SetUp() override;
42     void TearDown() override;
43 };
44 
SetUpTestCase()45 void JsUtilsTest::SetUpTestCase() {}
TearDownTestCase()46 void JsUtilsTest::TearDownTestCase() {}
SetUp()47 void JsUtilsTest::SetUp() {}
TearDown()48 void JsUtilsTest::TearDown() {}
49 
50 /**
51  * @tc.name: ParseDimensionId001
52  * @tc.desc: Test parse string contains platform style id reference.
53  * @tc.type: FUNC
54  */
55 HWTEST_F(JsUtilsTest, ParseDimensionId001, TestSize.Level1)
56 {
57     /**
58      * @tc.steps: step1. Parse dimension string contains platform id reference.
59      * @tc.expected: step1. Dimension parse correctly, equal with value in ThemeConstants.
60      */
61     auto themeManager = AceType::MakeRefPtr<ThemeManagerImpl>();
62     auto themeConstants = themeManager->GetThemeConstants();
63     ASSERT_NE(themeConstants, nullptr);
64     const auto& parseResult = ThemeUtils::ParseThemeIdReference(DIMENSION_ID_NORMAL);
65     Dimension dimension = themeConstants->GetDimension(parseResult.id);
66     ASSERT_EQ(dimension, themeConstants->GetDimension(THEME_OHOS_TEXT_SIZE_HEADLINE1));
67 }
68 
69 /**
70  * @tc.name: ParseDimensionId002
71  * @tc.desc: Test parse string contains platform style id reference, id not found.
72  * @tc.type: FUNC
73  */
74 HWTEST_F(JsUtilsTest, ParseDimensionId002, TestSize.Level1)
75 {
76     /**
77      * @tc.steps: step1. Parse dimension string contains platform id reference, id not found.
78      * @tc.expected: step1. Dimension parse get dimension 0dp.
79      */
80     auto themeManager = AceType::MakeRefPtr<ThemeManagerImpl>();
81     auto themeConstants = themeManager->GetThemeConstants();
82     ASSERT_NE(themeConstants, nullptr);
83     const auto& parseResult = ThemeUtils::ParseThemeIdReference(DIMENSION_ID_NOT_FOUND);
84     Dimension dimension = themeConstants->GetDimension(parseResult.id);
85     ASSERT_EQ(dimension, Dimension(0.0, DimensionUnit::VP));
86 }
87 
88 /**
89  * @tc.name: ParseDimensionId003
90  * @tc.desc: Test parse string contains platform style id which format is error.
91  * @tc.type: FUNC
92  */
93 HWTEST_F(JsUtilsTest, ParseDimensionId003, TestSize.Level1)
94 {
95     /**
96      * @tc.steps: step1. Parse dimension string contains id which format is error.
97      * @tc.expected: step1. Dimension parse result equal with 0px.
98      */
99     for (size_t i = 0; i < std::size(DIMENSION_ID_ERROR); i++) {
100         Dimension dimension = StringToDimension(DIMENSION_ID_ERROR[i]);
101         ASSERT_EQ(dimension, Dimension(0.0, DimensionUnit::PX));
102     }
103 }
104 
105 /**
106  * @tc.name: ParseDimensionId004
107  * @tc.desc: Test parse string contains platform style id reference, id not found.
108  * @tc.type: FUNC
109  */
110 HWTEST_F(JsUtilsTest, ParseDimensionId004, TestSize.Level1)
111 {
112     /**
113      * @tc.steps: step1. Parse dimension string contains platform id reference, id not found.
114      * @tc.expected: step1. Dimension parse get dimension 0dp.
115      */
116     auto themeManager = AceType::MakeRefPtr<ThemeManagerImpl>();
117     auto themeConstants = themeManager->GetThemeConstants();
118     ASSERT_NE(themeConstants, nullptr);
119     const auto& parseResult = ThemeUtils::ParseThemeIdReference(OHOS_THEME_ID_NORMAL);
120     uint32_t correctId = 117441012;
121     ASSERT_EQ(parseResult.id, correctId);
122 }
123 
124 } // namespace OHOS::Ace::Framework