• 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 "base/json/json_util.h"
19 #include "base/utils/utils.h"
20 #include "core/components/common/layout/constants.h"
21 #include "core/components/text/text_theme.h"
22 #include "core/components/theme/theme_manager_impl.h"
23 #include "frameworks/bridge/common/dom/dom_div.h"
24 #include "frameworks/bridge/common/dom/dom_document.h"
25 #include "frameworks/bridge/common/dom/dom_label.h"
26 #include "frameworks/bridge/test/unittest/jsfrontend/dom_node_factory.h"
27 
28 using namespace testing;
29 using namespace testing::ext;
30 
31 namespace OHOS::Ace::Framework {
32 namespace {
33 
34 const std::string TEXT_DATA = "test1234@?!";
35 constexpr int32_t TEST_OVER_FLOW = 1;
36 constexpr double TEST_FONT_SIZE = 50.0;
37 constexpr int32_t TEST_FONT_WEIGHT = 1;
38 const std::string TEST_COLOR_VALUE = "#0x0000ff";
39 constexpr double TEST_LINE_HEIGHT = 11.0;
40 constexpr int32_t TEST_FONT_STYLE = 1;
41 constexpr int32_t TEST_TEXT_DECORATION = 2;
42 constexpr uint32_t TEST_MAX_LINE = 10;
43 constexpr Dimension TEST_LETTER_SPACING = 11.0_px;
44 constexpr int32_t TEST_TEXT_ALIGN = 1;
45 const std::string TEST_FONT_FAMILY = "serif";
46 constexpr int32_t DEFAULT_OVER_FLOW = 0;
47 constexpr int32_t DEFAULT_TEXT_ALIGN = 4;
48 
49 } // namespace
50 
51 class DomLabelTest : public testing::Test {
52 public:
53     static void SetUpTestCase();
54     static void TearDownTestCase();
55     void SetUp();
56     void TearDown();
57 };
58 
SetUpTestCase()59 void DomLabelTest::SetUpTestCase() {}
TearDownTestCase()60 void DomLabelTest::TearDownTestCase() {}
SetUp()61 void DomLabelTest::SetUp() {}
TearDown()62 void DomLabelTest::TearDown() {}
63 
64 /**
65  * @tc.name: DomLabelTest001
66  * @tc.desc: Verify that DomLabel can be created.
67  * @tc.type: FUNC
68  */
69 HWTEST_F(DomLabelTest, DomLabelTest001, TestSize.Level1)
70 {
71     /**
72      * @tc.steps: step1. construct the json string of DomLabel without attributes and style.
73      */
74     const std::string jsonTextStr = ""
75                                     "{                                 "
76                                     "  \"tag\": \"text\"               "
77                                     "}";
78     /**
79      * @tc.steps: step2. call JsonUtil interface, create DomLabel and set its style.
80      */
81     auto domNodeRoot = DOMNodeFactory::GetInstance().CreateDOMNodeFromDsl(jsonTextStr);
82     auto boxChild = DOMNodeFactory::GetInstance().GetBoxChildComponent(domNodeRoot);
83     RefPtr<TextComponent> textChild = AceType::DynamicCast<TextComponent>(boxChild->GetChild());
84     const auto textStyle = textChild->GetTextStyle();
85 
86     /**
87      * @tc.steps: step3. Check all the attributes matched with the default values.
88      * @tc.expected: step3. All the attributes are matched.
89      */
90     auto themeManager = AceType::MakeRefPtr<ThemeManagerImpl>();
91     RefPtr<TextTheme> theme = themeManager->GetTheme<TextTheme>();
92     ASSERT_TRUE(theme);
93     EXPECT_TRUE(static_cast<int32_t>(textChild->GetTextStyle().GetTextOverflow()) == DEFAULT_OVER_FLOW);
94     EXPECT_EQ(textStyle.GetFontSize().Unit(), theme->GetTextStyle().GetFontSize().Unit());
95     EXPECT_TRUE(NearEqual(textStyle.GetFontSize().Value(), theme->GetTextStyle().GetFontSize().Value()));
96     EXPECT_EQ(textStyle.GetFontWeight(), theme->GetTextStyle().GetFontWeight());
97     EXPECT_EQ(textStyle.GetTextColor().GetValue(), theme->GetTextStyle().GetTextColor().GetValue());
98     EXPECT_EQ(textStyle.GetLineHeight(), theme->GetTextStyle().GetLineHeight());
99     EXPECT_EQ(textStyle.GetFontStyle(), theme->GetTextStyle().GetFontStyle());
100     EXPECT_EQ(textStyle.GetTextDecoration(), theme->GetTextStyle().GetTextDecoration());
101     EXPECT_EQ(textStyle.GetLetterSpacing().Unit(), theme->GetTextStyle().GetLetterSpacing().Unit());
102     EXPECT_TRUE(NearEqual(textStyle.GetLetterSpacing().Value(), theme->GetTextStyle().GetLetterSpacing().Value()));
103     EXPECT_EQ(static_cast<int32_t>(textChild->GetTextStyle().GetTextAlign()), DEFAULT_TEXT_ALIGN);
104 }
105 
106 /**
107  * @tc.name: DomLabelTest002
108  * @tc.desc: Verify that DomLabel can be set styles.
109  * @tc.type: FUNC
110  */
111 HWTEST_F(DomLabelTest, DomLabelTest002, TestSize.Level1)
112 {
113     /**
114      * @tc.steps: step1. construct the json string of DomLabel with all attributes.
115      */
116     const std::string jsonTextStr = ""
117                                     "{                                         "
118                                     "  \"tag\": \"text\",                      "
119                                     "  \"attr\" : [{                           "
120                                     "           \"value\" : \"test1234@?!\"  "
121                                     "            }],                           "
122                                     "  \"style\": [{                           "
123                                     "           \"textOverflow\":\"ellipsis\"  "
124                                     "          },"
125                                     "          { "
126                                     "           \"fontSize\":\"50.0\"          "
127                                     "          },                              "
128                                     "          {"
129                                     "           \"fontWeight\":\"200\"         "
130                                     "           },"
131                                     "          {"
132                                     "           \"color\":\"#0x0000ff\"        "
133                                     "           },"
134                                     "          {"
135                                     "           \"lineHeight\":\"11.0\"        "
136                                     "           },"
137                                     "          {"
138                                     "           \"fontStyle\":\"italic\"       "
139                                     "           },"
140                                     "          {"
141                                     "           \"textDecoration\":\"overline\""
142                                     "           },"
143                                     "           { "
144                                     "           \"maxLines\":\"10\"            "
145                                     "           },"
146                                     "          {"
147                                     "           \"letterSpacing\":\"11.0\"     "
148                                     "           },"
149                                     "           { "
150                                     "           \"textAlign\":\"right\"        "
151                                     "            },"
152                                     "           { "
153                                     "           \"fontFamily\":\"serif\"       "
154                                     "            }]"
155                                     "}";
156 
157     /**
158      * @tc.steps: step2. call JsonUtil interface, create DomLabel and set its style.
159      */
160     auto domNodeRoot = DOMNodeFactory::GetInstance().CreateDOMNodeFromDsl(jsonTextStr);
161     auto boxChild = DOMNodeFactory::GetInstance().GetBoxChildComponent(domNodeRoot);
162     RefPtr<TextComponent> textChild = AceType::DynamicCast<TextComponent>(boxChild->GetChild());
163     const auto textStyle = textChild->GetTextStyle();
164 
165     /**
166      * @tc.steps: step3. Check all the attributes matched.
167      * @tc.expected: step3. All the attributes are matched.
168      */
169     EXPECT_EQ(textChild->GetData(), TEXT_DATA);
170     EXPECT_TRUE(static_cast<int32_t>(textChild->GetTextStyle().GetTextOverflow()) == TEST_OVER_FLOW);
171     EXPECT_TRUE(NearEqual(textStyle.GetFontSize().Value(), TEST_FONT_SIZE));
172     EXPECT_EQ(static_cast<int32_t>(textStyle.GetFontWeight()), TEST_FONT_WEIGHT);
173     EXPECT_EQ(textStyle.GetTextColor(), Color::FromString(TEST_COLOR_VALUE));
174     EXPECT_TRUE(NearEqual(textStyle.GetLineHeight().Value(), TEST_LINE_HEIGHT));
175     EXPECT_TRUE(NearEqual(static_cast<int32_t>(textStyle.GetFontStyle()), TEST_FONT_STYLE));
176     EXPECT_TRUE(NearEqual(static_cast<int32_t>(textStyle.GetTextDecoration()), TEST_TEXT_DECORATION));
177     EXPECT_TRUE(NearEqual(textChild->GetTextStyle().GetMaxLines(), TEST_MAX_LINE));
178     EXPECT_EQ(textStyle.GetLetterSpacing().Unit(), TEST_LETTER_SPACING.Unit());
179     EXPECT_TRUE(NearEqual(textStyle.GetLetterSpacing().Value(), TEST_LETTER_SPACING.Value()));
180     EXPECT_EQ(static_cast<int32_t>(textChild->GetTextStyle().GetTextAlign()), TEST_TEXT_ALIGN);
181     EXPECT_EQ(textStyle.GetFontFamilies()[0], TEST_FONT_FAMILY);
182 }
183 
184 } // namespace OHOS::Ace::Framework
185