• 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 "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  * @tc.require: AR000DD66C
62  * @tc.author: wangchun
63  */
64 HWTEST_F(DomSpanTest, DomSpanTest001, TestSize.Level1)
65 {
66     /**
67      * @tc.steps: step1. call JsonUtil interface and create DomSpan.
68      */
69     const std::string jsonSpanStr = ""
70                                     "{                                 "
71                                     "  \"tag\": \"span\"               "
72                                     "}";
73 
74     /**
75      * @tc.steps: step2. call JsonUtil interface and create DomSpan.
76      */
77     auto domNodeSpan = DOMNodeFactory::GetInstance().CreateDOMNodeFromDsl(jsonSpanStr);
78 
79     /**
80      * @tc.steps: step3. Verify whether the DomSpan.
81      * @tc.expected: step3. DomSpan is not null.
82      */
83     ASSERT_TRUE(domNodeSpan != nullptr);
84 }
85 
86 /**
87  * @tc.name: DomSpanTest002
88  * @tc.desc: Verify that DomSpan can be set styles.
89  * @tc.type: FUNC
90  * @tc.require: AR000DD66C AR000FRTPP
91  * @tc.author: wangchun
92  */
93 HWTEST_F(DomSpanTest, DomSpanTest002, TestSize.Level1)
94 {
95     /**
96      * @tc.steps: step1. construct the json string of DomSpan with all attributes.
97      */
98     const std::string jsonSpanStr = ""
99                                     "{                                             "
100                                     "  \"tag\": \"span\",                          "
101                                     "  \"attr\" : [{                               "
102                                     "           \"value\" : \"test1234@?!\"      "
103                                     "            }],                               "
104                                     "  \"style\": [{                               "
105                                     "           \"fontSize\":\"50.0\"              "
106                                     "          },                                  "
107                                     "          {                                   "
108                                     "           \"fontWeight\":\"200\"             "
109                                     "           },                                 "
110                                     "          {                                   "
111                                     "           \"color\":\"#000000ff\"            "
112                                     "           },                                 "
113                                     "          {                                   "
114                                     "           \"fontStyle\":\"italic\"           "
115                                     "           },                                 "
116                                     "          {                                   "
117                                     "           \"textDecoration\":\"overline\"    "
118                                     "           },                                 "
119                                     "           {                                                                  "
120                                     "           \"fontVariant\":\"small-caps slashed-zero common-ligatures ruby    "
121                                     "stylistic(2) \"                                                               "
122                                     "           },                                 "
123                                     "           {                                  "
124                                     "           \"fontFamily\":\"serif\"           "
125                                     "            }]                                "
126                                     "}";
127 
128     /**
129      * @tc.steps: step2. call JsonUtil interface, create DomSpan and set its style.
130      */
131     auto domNodeRoot = DOMNodeFactory::GetInstance().CreateDOMNodeFromDsl(jsonSpanStr);
132     RefPtr<TextSpanComponent> spanChild =
133         AceType::DynamicCast<TextSpanComponent>(domNodeRoot->GetSpecializedComponent());
134     const auto& spanStyle = spanChild->GetTextStyle();
135 
136     /**
137      * @tc.steps: step3. Check all the attributes matched.
138      * @tc.expected: step3. All the attributes are matched.
139      */
140     EXPECT_EQ(spanChild->GetSpanData(), SPAN_DATA);
141     EXPECT_TRUE(NearEqual(spanStyle.GetFontSize().Value(), TEST_FONT_SIZE));
142     EXPECT_EQ(static_cast<int32_t>(spanStyle.GetFontWeight()), TEST_FONT_WEIGHT);
143     EXPECT_EQ(spanStyle.GetTextColor(), Color::FromString("#000000ff"));
144     EXPECT_TRUE(NearEqual(static_cast<int32_t>(spanStyle.GetFontStyle()), TEST_FONT_STYLE));
145     EXPECT_TRUE(NearEqual(static_cast<int32_t>(spanStyle.GetTextDecoration()), TEST_TEXT_DECORATION));
146     EXPECT_EQ(spanStyle.GetFontFamilies()[0], TEST_FONT_FAMILY);
147     EXPECT_EQ(spanStyle.GetFontFeatures().size(), TEST_FONT_FEATURES_SIZE);
148 }
149 
150 /**
151  * @tc.name: DomSpanTest003
152  * @tc.desc: Verify that DomSpan can be set styles to the default values when styles' attributes are invalid.
153  * @tc.type: FUNC
154  * @tc.require: AR000DD66C
155  * @tc.author: wangchun
156  */
157 HWTEST_F(DomSpanTest, DomSpanTest003, TestSize.Level1)
158 {
159     /**
160      * @tc.steps: step1. construct the json string of DomSpan with invalid attributes.
161      */
162     const std::string jsonSpanStr = ""
163                                     "{                                                 "
164                                     "  \"tag\": \"span\",                              "
165                                     "  \"style\": [{                                   "
166                                     "           \"fontWeight\":\"invalidValue\"        "
167                                     "           },                                     "
168                                     "          {                                       "
169                                     "           \"fontStyle\":\"invalidValue\"         "
170                                     "           },                                     "
171                                     "          {                                       "
172                                     "           \"fontVariant\":\"invalidValue\"       "
173                                     "           },                                     "
174                                     "          {                                       "
175                                     "           \"textDecoration\":\"invalidValue\"    "
176                                     "           }]                                     "
177                                     "}";
178 
179     /**
180      * @tc.steps: step2. call JsonUtil interface, create DomSpan and set attributes' value.
181      */
182     auto domNodeRoot = DOMNodeFactory::GetInstance().CreateDOMNodeFromDsl(jsonSpanStr);
183     RefPtr<TextSpanComponent> spanChild =
184         AceType::DynamicCast<TextSpanComponent>(domNodeRoot->GetSpecializedComponent());
185     const auto& spanStyle = spanChild->GetTextStyle();
186 
187     /**
188      * @tc.steps: step3. Check all the attributes matched with default values.
189      * @tc.expected: step3. All the attributes are matched.
190      */
191     EXPECT_EQ(static_cast<int32_t>(spanStyle.GetFontWeight()), DEFAULT_FONT_WEIGHT);
192     EXPECT_TRUE(NearEqual(static_cast<int32_t>(spanStyle.GetFontStyle()), DEFAULT_FONT_STYLE));
193     EXPECT_TRUE(NearEqual(static_cast<int32_t>(spanStyle.GetTextDecoration()), DEFAULT_TEXT_DECORATION));
194     EXPECT_TRUE(spanStyle.GetFontFeatures().empty());
195 }
196 
197 /**
198  * @tc.name: DomSpanTest004
199  * @tc.desc: Verify that DomSpan can be created.
200  * @tc.type: FUNC
201  * @tc.require: AR000FRTPP
202  * @tc.author: caocan
203  */
204 HWTEST_F(DomSpanTest, DomSpanTest004, TestSize.Level1)
205 {
206     const std::string jsonSpanStr = ""
207                                     "{                                          "
208                                     "  \"tag\": \"span\",                       "
209                                     "  \"style\": [{                            "
210                                     "               \"fontVariant\":\"normal\"  "
211                                     "            }]                             "
212                                     "}";
213 
214     /**
215      * @tc.steps: step1. call JsonUtil interface and create DomText.
216      */
217     auto domNodeRoot = DOMNodeFactory::GetInstance().CreateDOMNodeFromDsl(jsonSpanStr);
218     RefPtr<TextSpanComponent> spanChild =
219         AceType::DynamicCast<TextSpanComponent>(domNodeRoot->GetSpecializedComponent());
220     const auto textStyle = spanChild->GetTextStyle();
221 
222     /**
223      * @tc.steps: step3. Check font features is correct.
224      * @tc.expected: step3. Font featrues is empty.
225      */
226     EXPECT_TRUE(textStyle.GetFontFeatures().empty());
227 }
228 
229 /**
230  * @tc.name: DomSpanTest005
231  * @tc.desc: Verify that DomSpan can be created.
232  * @tc.type: FUNC
233  * @tc.require: AR000FRTPP
234  * @tc.author: caocan
235  */
236 HWTEST_F(DomSpanTest, DomSpanTest005, TestSize.Level1)
237 {
238     const std::string jsonSpanStr = ""
239                                     "{                                     "
240                                     "  \"tag\": \"span\",                  "
241                                     "  \"style\": [{                       "
242                                     "           \"fontVariant\":\"none\"   "
243                                     "            }]                        "
244                                     "}";
245 
246     /**
247      * @tc.steps: step1. call JsonUtil interface and create DomText.
248      */
249     auto domNodeRoot = DOMNodeFactory::GetInstance().CreateDOMNodeFromDsl(jsonSpanStr);
250     RefPtr<TextSpanComponent> spanChild =
251         AceType::DynamicCast<TextSpanComponent>(domNodeRoot->GetSpecializedComponent());
252     const auto textStyle = spanChild->GetTextStyle();
253 
254     /**
255      * @tc.steps: step3. Check font features is correct.
256      * @tc.expected: step3. Font feature is correct.
257      */
258     EXPECT_EQ(textStyle.GetFontFeatures().size(), TEST_FONT_FEATURES_NONE_SIZE);
259 }
260 
261 } // namespace OHOS::Ace::Framework
262