• 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/common/layout/constants.h"
19 #include "frameworks/bridge/common/dom/dom_document.h"
20 #include "frameworks/bridge/common/dom/dom_span.h"
21 #include "frameworks/bridge/test/unittest/jsfrontend/dom_node_factory.h"
22 
23 using namespace testing;
24 using namespace testing::ext;
25 
26 namespace OHOS::Ace::Framework {
27 namespace {
28 
29 const std::string SPAN_DATA = "test1234@?!";
30 constexpr double TEST_FONT_SIZE = 50.0;
31 constexpr int32_t TEST_FONT_WEIGHT = 1;
32 constexpr int32_t TEST_FONT_STYLE = 1;
33 constexpr int32_t TEST_TEXT_DECORATION = 2;
34 const std::string TEST_FONT_FAMILY = "serif";
35 
36 constexpr int32_t DEFAULT_FONT_WEIGHT = 10;
37 constexpr int32_t DEFAULT_FONT_STYLE = 0;
38 constexpr int32_t DEFAULT_TEXT_DECORATION = 0;
39 constexpr uint32_t TEST_FONT_FEATURES_SIZE = 6;
40 constexpr uint32_t TEST_FONT_FEATURES_NONE_SIZE = 5;
41 
42 } // namespace
43 
44 class DomSpanTest : public testing::Test {
45 public:
46     static void SetUpTestCase();
47     static void TearDownTestCase();
48     void SetUp() override;
49     void TearDown() override;
50 };
51 
SetUpTestCase()52 void DomSpanTest::SetUpTestCase() {}
TearDownTestCase()53 void DomSpanTest::TearDownTestCase() {}
SetUp()54 void DomSpanTest::SetUp() {}
TearDown()55 void DomSpanTest::TearDown() {}
56 
57 /**
58  * @tc.name: DomSpanTest001
59  * @tc.desc: Verify that DomSpan can be created.
60  * @tc.type: FUNC
61  */
62 HWTEST_F(DomSpanTest, DomSpanTest001, TestSize.Level1)
63 {
64     /**
65      * @tc.steps: step1. call JsonUtil interface and create DomSpan.
66      */
67     const std::string jsonSpanStr = ""
68                                     "{                                 "
69                                     "  \"tag\": \"span\"               "
70                                     "}";
71 
72     /**
73      * @tc.steps: step2. call JsonUtil interface and create DomSpan.
74      */
75     auto domNodeSpan = DOMNodeFactory::GetInstance().CreateDOMNodeFromDsl(jsonSpanStr);
76 
77     /**
78      * @tc.steps: step3. Verify whether the DomSpan.
79      * @tc.expected: step3. DomSpan is not null.
80      */
81     ASSERT_TRUE(domNodeSpan != nullptr);
82 }
83 
84 /**
85  * @tc.name: DomSpanTest002
86  * @tc.desc: Verify that DomSpan can be set styles.
87  * @tc.type: FUNC
88  */
89 HWTEST_F(DomSpanTest, DomSpanTest002, TestSize.Level1)
90 {
91     /**
92      * @tc.steps: step1. construct the json string of DomSpan with all attributes.
93      */
94     const std::string jsonSpanStr = ""
95                                     "{                                             "
96                                     "  \"tag\": \"span\",                          "
97                                     "  \"attr\" : [{                               "
98                                     "           \"value\" : \"test1234@?!\"      "
99                                     "            }],                               "
100                                     "  \"style\": [{                               "
101                                     "           \"fontSize\":\"50.0\"              "
102                                     "          },                                  "
103                                     "          {                                   "
104                                     "           \"fontWeight\":\"200\"             "
105                                     "           },                                 "
106                                     "          {                                   "
107                                     "           \"color\":\"#000000ff\"            "
108                                     "           },                                 "
109                                     "          {                                   "
110                                     "           \"fontStyle\":\"italic\"           "
111                                     "           },                                 "
112                                     "          {                                   "
113                                     "           \"textDecoration\":\"overline\"    "
114                                     "           },                                 "
115                                     "           {                                                                  "
116                                     "           \"fontVariant\":\"small-caps slashed-zero common-ligatures ruby    "
117                                     "stylistic(2) \"                                                               "
118                                     "           },                                 "
119                                     "           {                                  "
120                                     "           \"fontFamily\":\"serif\"           "
121                                     "            }]                                "
122                                     "}";
123 
124     /**
125      * @tc.steps: step2. call JsonUtil interface, create DomSpan and set its style.
126      */
127     auto domNodeRoot = DOMNodeFactory::GetInstance().CreateDOMNodeFromDsl(jsonSpanStr);
128     RefPtr<TextSpanComponent> spanChild =
129         AceType::DynamicCast<TextSpanComponent>(domNodeRoot->GetSpecializedComponent());
130     const auto& spanStyle = spanChild->GetTextStyle();
131 
132     /**
133      * @tc.steps: step3. Check all the attributes matched.
134      * @tc.expected: step3. All the attributes are matched.
135      */
136     EXPECT_EQ(spanChild->GetSpanData(), SPAN_DATA);
137     EXPECT_TRUE(NearEqual(spanStyle.GetFontSize().Value(), TEST_FONT_SIZE));
138     EXPECT_EQ(static_cast<int32_t>(spanStyle.GetFontWeight()), TEST_FONT_WEIGHT);
139     EXPECT_EQ(spanStyle.GetTextColor(), Color::FromString("#000000ff"));
140     EXPECT_TRUE(NearEqual(static_cast<int32_t>(spanStyle.GetFontStyle()), TEST_FONT_STYLE));
141     EXPECT_TRUE(NearEqual(static_cast<int32_t>(spanStyle.GetTextDecoration()), TEST_TEXT_DECORATION));
142     EXPECT_EQ(spanStyle.GetFontFamilies()[0], TEST_FONT_FAMILY);
143     EXPECT_EQ(spanStyle.GetFontFeatures().size(), TEST_FONT_FEATURES_SIZE);
144 }
145 
146 /**
147  * @tc.name: DomSpanTest003
148  * @tc.desc: Verify that DomSpan can be set styles to the default values when styles' attributes are invalid.
149  * @tc.type: FUNC
150  */
151 HWTEST_F(DomSpanTest, DomSpanTest003, TestSize.Level1)
152 {
153     /**
154      * @tc.steps: step1. construct the json string of DomSpan with invalid attributes.
155      */
156     const std::string jsonSpanStr = ""
157                                     "{                                                 "
158                                     "  \"tag\": \"span\",                              "
159                                     "  \"style\": [{                                   "
160                                     "           \"fontWeight\":\"invalidValue\"        "
161                                     "           },                                     "
162                                     "          {                                       "
163                                     "           \"fontStyle\":\"invalidValue\"         "
164                                     "           },                                     "
165                                     "          {                                       "
166                                     "           \"fontVariant\":\"invalidValue\"       "
167                                     "           },                                     "
168                                     "          {                                       "
169                                     "           \"textDecoration\":\"invalidValue\"    "
170                                     "           }]                                     "
171                                     "}";
172 
173     /**
174      * @tc.steps: step2. call JsonUtil interface, create DomSpan and set attributes' value.
175      */
176     auto domNodeRoot = DOMNodeFactory::GetInstance().CreateDOMNodeFromDsl(jsonSpanStr);
177     RefPtr<TextSpanComponent> spanChild =
178         AceType::DynamicCast<TextSpanComponent>(domNodeRoot->GetSpecializedComponent());
179     const auto& spanStyle = spanChild->GetTextStyle();
180 
181     /**
182      * @tc.steps: step3. Check all the attributes matched with default values.
183      * @tc.expected: step3. All the attributes are matched.
184      */
185     EXPECT_EQ(static_cast<int32_t>(spanStyle.GetFontWeight()), DEFAULT_FONT_WEIGHT);
186     EXPECT_TRUE(NearEqual(static_cast<int32_t>(spanStyle.GetFontStyle()), DEFAULT_FONT_STYLE));
187     EXPECT_TRUE(NearEqual(static_cast<int32_t>(spanStyle.GetTextDecoration()), DEFAULT_TEXT_DECORATION));
188     EXPECT_TRUE(spanStyle.GetFontFeatures().empty());
189 }
190 
191 /**
192  * @tc.name: DomSpanTest004
193  * @tc.desc: Verify that DomSpan can be created.
194  * @tc.type: FUNC
195  */
196 HWTEST_F(DomSpanTest, DomSpanTest004, TestSize.Level1)
197 {
198     const std::string jsonSpanStr = ""
199                                     "{                                          "
200                                     "  \"tag\": \"span\",                       "
201                                     "  \"style\": [{                            "
202                                     "               \"fontVariant\":\"normal\"  "
203                                     "            }]                             "
204                                     "}";
205 
206     /**
207      * @tc.steps: step1. call JsonUtil interface and create DomText.
208      */
209     auto domNodeRoot = DOMNodeFactory::GetInstance().CreateDOMNodeFromDsl(jsonSpanStr);
210     RefPtr<TextSpanComponent> spanChild =
211         AceType::DynamicCast<TextSpanComponent>(domNodeRoot->GetSpecializedComponent());
212     const auto textStyle = spanChild->GetTextStyle();
213 
214     /**
215      * @tc.steps: step3. Check font features is correct.
216      * @tc.expected: step3. Font featrues is empty.
217      */
218     EXPECT_TRUE(textStyle.GetFontFeatures().empty());
219 }
220 
221 /**
222  * @tc.name: DomSpanTest005
223  * @tc.desc: Verify that DomSpan can be created.
224  * @tc.type: FUNC
225  */
226 HWTEST_F(DomSpanTest, DomSpanTest005, TestSize.Level1)
227 {
228     const std::string jsonSpanStr = ""
229                                     "{                                     "
230                                     "  \"tag\": \"span\",                  "
231                                     "  \"style\": [{                       "
232                                     "           \"fontVariant\":\"none\"   "
233                                     "            }]                        "
234                                     "}";
235 
236     /**
237      * @tc.steps: step1. call JsonUtil interface and create DomText.
238      */
239     auto domNodeRoot = DOMNodeFactory::GetInstance().CreateDOMNodeFromDsl(jsonSpanStr);
240     RefPtr<TextSpanComponent> spanChild =
241         AceType::DynamicCast<TextSpanComponent>(domNodeRoot->GetSpecializedComponent());
242     const auto textStyle = spanChild->GetTextStyle();
243 
244     /**
245      * @tc.steps: step3. Check font features is correct.
246      * @tc.expected: step3. Font feature is correct.
247      */
248     EXPECT_EQ(textStyle.GetFontFeatures().size(), TEST_FONT_FEATURES_NONE_SIZE);
249 }
250 
251 } // namespace OHOS::Ace::Framework
252