• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "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.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  * @tc.require: AR000DAR18
69  * @tc.author: hushilong
70  */
71 HWTEST_F(DomLabelTest, DomLabelTest001, TestSize.Level1)
72 {
73     /**
74      * @tc.steps: step1. construct the json string of DomLabel without attributes and style.
75      */
76     const std::string jsonTextStr = ""
77                                     "{                                 "
78                                     "  \"tag\": \"text\"               "
79                                     "}";
80     /**
81      * @tc.steps: step2. call JsonUtil interface, create DomLabel and set its style.
82      */
83     auto domNodeRoot = DOMNodeFactory::GetInstance().CreateDOMNodeFromDsl(jsonTextStr);
84     auto boxChild = DOMNodeFactory::GetInstance().GetBoxChildComponent(domNodeRoot);
85     RefPtr<TextComponent> textChild = AceType::DynamicCast<TextComponent>(boxChild->GetChild());
86     const auto textStyle = textChild->GetTextStyle();
87 
88     /**
89      * @tc.steps: step3. Check all the attributes matched with the default values.
90      * @tc.expected: step3. All the attributes are matched.
91      */
92     auto themeManager = AceType::MakeRefPtr<ThemeManager>();
93     RefPtr<TextTheme> theme = themeManager->GetTheme<TextTheme>();
94     ASSERT_TRUE(theme);
95     EXPECT_TRUE(static_cast<int32_t>(textChild->GetTextStyle().GetTextOverflow()) == DEFAULT_OVER_FLOW);
96     EXPECT_EQ(textStyle.GetFontSize().Unit(), theme->GetTextStyle().GetFontSize().Unit());
97     EXPECT_TRUE(NearEqual(textStyle.GetFontSize().Value(), theme->GetTextStyle().GetFontSize().Value()));
98     EXPECT_EQ(textStyle.GetFontWeight(), theme->GetTextStyle().GetFontWeight());
99     EXPECT_EQ(textStyle.GetTextColor().GetValue(), theme->GetTextStyle().GetTextColor().GetValue());
100     EXPECT_EQ(textStyle.GetLineHeight(), theme->GetTextStyle().GetLineHeight());
101     EXPECT_EQ(textStyle.GetFontStyle(), theme->GetTextStyle().GetFontStyle());
102     EXPECT_EQ(textStyle.GetTextDecoration(), theme->GetTextStyle().GetTextDecoration());
103     EXPECT_EQ(textStyle.GetLetterSpacing().Unit(), theme->GetTextStyle().GetLetterSpacing().Unit());
104     EXPECT_TRUE(NearEqual(textStyle.GetLetterSpacing().Value(), theme->GetTextStyle().GetLetterSpacing().Value()));
105     EXPECT_EQ(static_cast<int32_t>(textChild->GetTextStyle().GetTextAlign()), DEFAULT_TEXT_ALIGN);
106 }
107 
108 /**
109  * @tc.name: DomLabelTest002
110  * @tc.desc: Verify that DomLabel can be set styles.
111  * @tc.type: FUNC
112  * @tc.require: AR000DAR18
113  * @tc.author: hushilong
114  */
115 HWTEST_F(DomLabelTest, DomLabelTest002, TestSize.Level1)
116 {
117     /**
118      * @tc.steps: step1. construct the json string of DomLabel with all attributes.
119      */
120     const std::string jsonTextStr = ""
121                                     "{                                         "
122                                     "  \"tag\": \"text\",                      "
123                                     "  \"attr\" : [{                           "
124                                     "           \"value\" : \"test1234@?!\"  "
125                                     "            }],                           "
126                                     "  \"style\": [{                           "
127                                     "           \"textOverflow\":\"ellipsis\"  "
128                                     "          },"
129                                     "          { "
130                                     "           \"fontSize\":\"50.0\"          "
131                                     "          },                              "
132                                     "          {"
133                                     "           \"fontWeight\":\"200\"         "
134                                     "           },"
135                                     "          {"
136                                     "           \"color\":\"#0x0000ff\"        "
137                                     "           },"
138                                     "          {"
139                                     "           \"lineHeight\":\"11.0\"        "
140                                     "           },"
141                                     "          {"
142                                     "           \"fontStyle\":\"italic\"       "
143                                     "           },"
144                                     "          {"
145                                     "           \"textDecoration\":\"overline\""
146                                     "           },"
147                                     "           { "
148                                     "           \"maxLines\":\"10\"            "
149                                     "           },"
150                                     "          {"
151                                     "           \"letterSpacing\":\"11.0\"     "
152                                     "           },"
153                                     "           { "
154                                     "           \"textAlign\":\"right\"        "
155                                     "            },"
156                                     "           { "
157                                     "           \"fontFamily\":\"serif\"       "
158                                     "            }]"
159                                     "}";
160 
161     /**
162      * @tc.steps: step2. call JsonUtil interface, create DomLabel and set its style.
163      */
164     auto domNodeRoot = DOMNodeFactory::GetInstance().CreateDOMNodeFromDsl(jsonTextStr);
165     auto boxChild = DOMNodeFactory::GetInstance().GetBoxChildComponent(domNodeRoot);
166     RefPtr<TextComponent> textChild = AceType::DynamicCast<TextComponent>(boxChild->GetChild());
167     const auto textStyle = textChild->GetTextStyle();
168 
169     /**
170      * @tc.steps: step3. Check all the attributes matched.
171      * @tc.expected: step3. All the attributes are matched.
172      */
173     EXPECT_EQ(textChild->GetData(), TEXT_DATA);
174     EXPECT_TRUE(static_cast<int32_t>(textChild->GetTextStyle().GetTextOverflow()) == TEST_OVER_FLOW);
175     EXPECT_TRUE(NearEqual(textStyle.GetFontSize().Value(), TEST_FONT_SIZE));
176     EXPECT_EQ(static_cast<int32_t>(textStyle.GetFontWeight()), TEST_FONT_WEIGHT);
177     EXPECT_EQ(textStyle.GetTextColor(), Color::FromString(TEST_COLOR_VALUE));
178     EXPECT_TRUE(NearEqual(textStyle.GetLineHeight().Value(), TEST_LINE_HEIGHT));
179     EXPECT_TRUE(NearEqual(static_cast<int32_t>(textStyle.GetFontStyle()), TEST_FONT_STYLE));
180     EXPECT_TRUE(NearEqual(static_cast<int32_t>(textStyle.GetTextDecoration()), TEST_TEXT_DECORATION));
181     EXPECT_TRUE(NearEqual(textChild->GetTextStyle().GetMaxLines(), TEST_MAX_LINE));
182     EXPECT_EQ(textStyle.GetLetterSpacing().Unit(), TEST_LETTER_SPACING.Unit());
183     EXPECT_TRUE(NearEqual(textStyle.GetLetterSpacing().Value(), TEST_LETTER_SPACING.Value()));
184     EXPECT_EQ(static_cast<int32_t>(textChild->GetTextStyle().GetTextAlign()), TEST_TEXT_ALIGN);
185     EXPECT_EQ(textStyle.GetFontFamilies()[0], TEST_FONT_FAMILY);
186 }
187 
188 } // namespace OHOS::Ace::Framework
189