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 "frameworks/bridge/common/dom/dom_svg_text.h"
19 #include "frameworks/bridge/common/dom/dom_svg_text_path.h"
20 #include "frameworks/bridge/common/dom/dom_svg_tspan.h"
21 #include "frameworks/bridge/test/unittest/jsfrontend/dom_node_factory.h"
22 #include "frameworks/core/components/svg/svg_tspan_component.h"
23
24 using namespace testing;
25 using namespace testing::ext;
26
27 namespace OHOS::Ace::Framework {
28 namespace {
29
30 const std::string TEXT_DATA = "test1234@?!";
31 const std::string SVG_LENGTH_ADJUST_VALUE = "spacingAndGlyphs";
32 const std::string SVG_PATH_VALUE = "M40,1160 Q360,1160 360,980 Q360,840 200,840 Q40,"
33 "840 40,960 Q40,1080 180,1080 Q280,1080 300,1000";
34 const Dimension SVG_TEXT_FONT_SIZE = Dimension(50.0);
35 const Dimension SVG_OFFSET_X = Dimension(10.0);
36 const Dimension SVG_OFFSET_Y = Dimension(20.0);
37 const Dimension SVG_OFFSET_DX = Dimension(30.0);
38 const Dimension SVG_OFFSET_DY = Dimension(40.0);
39 const Dimension SVG_START_OFFSET_VALUE = Dimension(50.0);
40 const Dimension SVG_STROKE_WIDTH_VALUE = Dimension(2.0);
41 const Color SVG_FILL_COLOR = Color::FromString("#0x0000ff");
42 const Color SVG_STROKE_COLOR = Color::FromString("#0x0000ee");
43 constexpr double SVG_ROTATE_VALUE = 10.0;
44 constexpr double SVG_FILL_OPACITY_VALUE = 0.5;
45 constexpr double SVG_STROKE_OPACITY_VALUE = 0.6;
46
47 const std::string JSON_SVG_TEXT_TAG = ""
48 "{ "
49 " \"tag\": \"svg-text\", ";
50 const std::string JSON_SVG_TSPAN_TAG = ""
51 "{ "
52 " \"tag\": \"tspan\", ";
53 const std::string JSON_SVG_TEXT_STR = " \"attr\": [{ "
54 " \"value\": \"test1234@?!\" "
55 " },"
56 " { "
57 " \"fontSize\":\"50.0\" "
58 " }, "
59 " {"
60 " \"x\":\"10\" "
61 " },"
62 " {"
63 " \"y\":\"20\" "
64 " },"
65 " {"
66 " \"dx\":\"30\" "
67 " },"
68 " {"
69 " \"dy\":\"40\" "
70 " },"
71 " { "
72 " \"rotate\":\"10.0\" "
73 " }, "
74 " { "
75 " \"TextLength\":\"100.0\" "
76 " },"
77 " { "
78 " \"LengthAdjust\":\"spacingAndGlyphs\" "
79 " },"
80 " {"
81 " \"fill\":\"#0x0000ff\" "
82 " },"
83 " {"
84 " \"fillOpacity\":\"0.5\" "
85 " },"
86 " {"
87 " \"stroke\":\"0x0000ee\" "
88 " },"
89 " {"
90 " \"strokeWidth\":\"2\" "
91 " },"
92 " { "
93 " \"strokeOpacity\":\"0.6\" "
94 " }]"
95 "}";
96
97 const std::string JSON_SVG_TEXT_PATH_STR = ""
98 "{ "
99 " \"tag\": \"textpath\", "
100 " \"attr\": [{ "
101 " \"value\": \"test1234@?!\" "
102 " },"
103 " { "
104 " \"fontSize\":\"50.0\" "
105 " }, "
106 " { "
107 " \"startOffset\":\"50.0\" "
108 " }, "
109 " { "
110 " \"path\":\"M40,1160 Q360,1160 360,980 Q360,"
111 "840 200,840 Q40,840 40,960 Q40,1080 180,1080 Q280,1080 300,1000\""
112 " }, "
113 " { "
114 " \"TextLength\":\"100.0\" "
115 " },"
116 " { "
117 " \"LengthAdjust\":\"spacingAndGlyphs\" "
118 " },"
119 " {"
120 " \"fill\":\"#0x0000ff\" "
121 " },"
122 " {"
123 " \"fillOpacity\":\"0.5\" "
124 " },"
125 " {"
126 " \"stroke\":\"0x0000ee\" "
127 " },"
128 " {"
129 " \"strokeWidth\":\"2\" "
130 " },"
131 " { "
132 " \"strokeOpacity\":\"0.6\" "
133 " }]"
134 "}";
135 } // namespace
136
137 class DomSvgTextTest : public testing::Test {
138 public:
139 static void SetUpTestCase();
140 static void TearDownTestCase();
141 void SetUp();
142 void TearDown();
143 };
144
SetUpTestCase()145 void DomSvgTextTest::SetUpTestCase() {}
TearDownTestCase()146 void DomSvgTextTest::TearDownTestCase() {}
SetUp()147 void DomSvgTextTest::SetUp() {}
TearDown()148 void DomSvgTextTest::TearDown() {}
149
150 /**
151 * @tc.name: DomSvgTextTest001
152 * @tc.desc: Verify that DomSvgText can be created.
153 * @tc.type: FUNC
154 * @tc.require: AR000FL0TO
155 * @tc.author: huye
156 */
157 HWTEST_F(DomSvgTextTest, DomSvgTextTest001, TestSize.Level1)
158 {
159 const std::string jsonTextStr = ""
160 "{ "
161 " \"tag\": \"svg-text\" "
162 "}";
163
164 /**
165 * @tc.steps: step1. call JsonUtil interface and create DomSvgText.
166 */
167 auto domNode = DOMNodeFactory::GetInstance().CreateDOMNodeFromDsl(jsonTextStr);
168 RefPtr<DOMSvgText> svgText = AceType::DynamicCast<DOMSvgText>(domNode);
169
170 /**
171 * @tc.steps: step2. Verify whether the DomSvgText.
172 * @tc.expected: step2. DomSvgText is not null.
173 */
174 ASSERT_TRUE(svgText != nullptr);
175 }
176
177 /**
178 * @tc.name: DomSvgTextTest002
179 * @tc.desc: Verify that DomSvgText can be set styles.
180 * @tc.type: FUNC
181 * @tc.require: AR000FL0TO
182 * @tc.author: huye
183 */
184 HWTEST_F(DomSvgTextTest, DomSvgTextTest002, TestSize.Level1)
185 {
186 /**
187 * @tc.steps: step1. construct the json string of DomSvgText with all attributes.
188 */
189 const std::string& jsonTextStr = JSON_SVG_TEXT_TAG + JSON_SVG_TEXT_STR;
190
191 /**
192 * @tc.steps: step2. call JsonUtil interface, create DomSvgText and set its attr.
193 */
194 auto domNodeRoot = DOMNodeFactory::GetInstance().CreateDOMNodeFromDsl(jsonTextStr);
195 ASSERT_TRUE(domNodeRoot != nullptr);
196 RefPtr<SvgTextComponent> svgText =
197 AceType::DynamicCast<SvgTextComponent>(domNodeRoot->GetSpecializedComponent());
198 ASSERT_TRUE(svgText != nullptr);
199 /**
200 * @tc.steps: step3. Check all the attributes matched.
201 * @tc.expected: step3. All the attributes are matched.
202 */
203 EXPECT_EQ(svgText->GetTextData(), TEXT_DATA);
204 EXPECT_EQ(svgText->GetX().Value(), SVG_OFFSET_X.Value());
205 EXPECT_EQ(svgText->GetY().Value(), SVG_OFFSET_Y.Value());
206 EXPECT_EQ(svgText->GetDx().Value(), SVG_OFFSET_DX.Value());
207 EXPECT_EQ(svgText->GetDy().Value(), SVG_OFFSET_DY.Value());
208 EXPECT_EQ(svgText->GetRotate(), SVG_ROTATE_VALUE);
209 EXPECT_EQ(svgText->GetDeclaration()->GetSvgTextStyle().GetFontSize().Value(), SVG_TEXT_FONT_SIZE.Value());
210 EXPECT_EQ(svgText->GetDeclaration()->GetFillState().GetColor().GetValue(), SVG_FILL_COLOR.GetValue());
211 EXPECT_EQ(svgText->GetDeclaration()->GetFillState().GetOpacity().GetValue(), SVG_FILL_OPACITY_VALUE);
212 EXPECT_EQ(svgText->GetDeclaration()->GetStrokeState().GetColor().GetValue(), SVG_STROKE_COLOR.GetValue());
213 EXPECT_EQ(svgText->GetDeclaration()->GetStrokeState().GetOpacity().GetValue(), SVG_STROKE_OPACITY_VALUE);
214 EXPECT_EQ(svgText->GetDeclaration()->GetStrokeState().GetLineWidth().Value(), SVG_STROKE_WIDTH_VALUE.Value());
215 }
216
217 /**
218 * @tc.name: DomSvgTspanTest001
219 * @tc.desc: Verify that DomSvgTspan can be created.
220 * @tc.type: FUNC
221 * @tc.require: AR000FL0TP
222 * @tc.author: huye
223 */
224 HWTEST_F(DomSvgTextTest, DomSvgTspanTest001, TestSize.Level1)
225 {
226 const std::string jsonTextStr = ""
227 "{ "
228 " \"tag\": \"tspan\" "
229 "}";
230
231 /**
232 * @tc.steps: step1. call JsonUtil interface and create DomSvgTspan.
233 */
234 auto domNode = DOMNodeFactory::GetInstance().CreateDOMNodeFromDsl(jsonTextStr);
235 RefPtr<DOMSvgTspan> svgTspan = AceType::DynamicCast<DOMSvgTspan>(domNode);
236
237 /**
238 * @tc.steps: step2. Verify whether the DomSvgTspan.
239 * @tc.expected: step2. DomSvgTspan is not null.
240 */
241 ASSERT_TRUE(svgTspan != nullptr);
242 }
243
244 /**
245 * @tc.name: DomSvgTspanTest002
246 * @tc.desc: Verify that DomSvgTspan can be set styles.
247 * @tc.type: FUNC
248 * @tc.require: AR000FL0TP
249 * @tc.author: huye
250 */
251 HWTEST_F(DomSvgTextTest, DomSvgTspanTest002, TestSize.Level1)
252 {
253 /**
254 * @tc.steps: step1. construct the json string of DomSvgTspan with all attributes.
255 */
256 const std::string& jsonTextStr = JSON_SVG_TSPAN_TAG + JSON_SVG_TEXT_STR;
257
258 /**
259 * @tc.steps: step2. call JsonUtil interface, create DomSvgTspan and set its attr.
260 */
261 auto domNodeRoot = DOMNodeFactory::GetInstance().CreateDOMNodeFromDsl(jsonTextStr);
262 ASSERT_TRUE(domNodeRoot != nullptr);
263
264 RefPtr<SvgTspanComponent> svgTspan =
265 AceType::DynamicCast<SvgTspanComponent>(domNodeRoot->GetSpecializedComponent());
266 ASSERT_TRUE(svgTspan != nullptr);
267
268 /**
269 * @tc.steps: step3. Check all the attributes matched.
270 * @tc.expected: step3. All the attributes are matched.
271 */
272 EXPECT_EQ(svgTspan->GetTextData(), TEXT_DATA);
273 EXPECT_EQ(svgTspan->GetX().Value(), SVG_OFFSET_X.Value());
274 EXPECT_EQ(svgTspan->GetY().Value(), SVG_OFFSET_Y.Value());
275 EXPECT_EQ(svgTspan->GetDx().Value(), SVG_OFFSET_DX.Value());
276 EXPECT_EQ(svgTspan->GetDy().Value(), SVG_OFFSET_DY.Value());
277 EXPECT_EQ(svgTspan->GetRotate(), SVG_ROTATE_VALUE);
278 EXPECT_EQ(svgTspan->GetDeclaration()->GetSvgTextStyle().GetFontSize().Value(), SVG_TEXT_FONT_SIZE.Value());
279 EXPECT_EQ(svgTspan->GetDeclaration()->GetFillState().GetColor().GetValue(), SVG_FILL_COLOR.GetValue());
280 EXPECT_EQ(svgTspan->GetDeclaration()->GetFillState().GetOpacity().GetValue(), SVG_FILL_OPACITY_VALUE);
281 EXPECT_EQ(svgTspan->GetDeclaration()->GetStrokeState().GetColor().GetValue(), SVG_STROKE_COLOR.GetValue());
282 EXPECT_EQ(svgTspan->GetDeclaration()->GetStrokeState().GetOpacity().GetValue(), SVG_STROKE_OPACITY_VALUE);
283 EXPECT_EQ(svgTspan->GetDeclaration()->GetStrokeState().GetLineWidth().Value(), SVG_STROKE_WIDTH_VALUE.Value());
284 }
285
286 /**
287 * @tc.name: DomSvgTextPathTest001
288 * @tc.desc: Verify that DomSvgTextPath can be created.
289 * @tc.type: FUNC
290 * @tc.require: AR000FL0TQ
291 * @tc.author: huye
292 */
293 HWTEST_F(DomSvgTextTest, DomSvgTextPathTest001, TestSize.Level1)
294 {
295 const std::string jsonTextStr = ""
296 "{ "
297 " \"tag\": \"textpath\" "
298 "}";
299
300 /**
301 * @tc.steps: step1. call JsonUtil interface and create DomSvgTextPath.
302 */
303 auto domNode = DOMNodeFactory::GetInstance().CreateDOMNodeFromDsl(jsonTextStr);
304 RefPtr<DOMSvgTextPath> svgTextPath = AceType::DynamicCast<DOMSvgTextPath>(domNode);
305
306 /**
307 * @tc.steps: step2. Verify whether the DOMSvgTextPath.
308 * @tc.expected: step2. DOMSvgTextPath is not null.
309 */
310 ASSERT_TRUE(svgTextPath != nullptr);
311 }
312
313 /**
314 * @tc.name: DomSvgTextPathTest002
315 * @tc.desc: Verify that DomSvgTextPath can be set styles.
316 * @tc.type: FUNC
317 * @tc.require: AR000FL0TQ
318 * @tc.author: huye
319 */
320 HWTEST_F(DomSvgTextTest, DomSvgTextPathTest002, TestSize.Level1)
321 {
322 /**
323 * @tc.steps: step1. construct the json string of DomSvgTextPath with all attributes.
324 */
325 const std::string& jsonTextStr = JSON_SVG_TEXT_PATH_STR;
326
327 /**
328 * @tc.steps: step2. call JsonUtil interface, create DomSvgTextPath and set its attr.
329 */
330 auto domNode = DOMNodeFactory::GetInstance().CreateDOMNodeFromDsl(jsonTextStr);
331 ASSERT_TRUE(domNode != nullptr);
332 RefPtr<SvgTextPathComponent> svgTextPath =
333 AceType::DynamicCast<SvgTextPathComponent>(domNode->GetSpecializedComponent());
334 ASSERT_TRUE(svgTextPath != nullptr);
335
336 /**
337 * @tc.steps: step3. Check all the attributes matched.
338 * @tc.expected: step3. All the attributes are matched.
339 */
340 EXPECT_EQ(svgTextPath->GetTextData(), TEXT_DATA);
341 EXPECT_EQ(svgTextPath->GetPath(), SVG_PATH_VALUE);
342 EXPECT_EQ(svgTextPath->GetStartOffset().Value(), SVG_START_OFFSET_VALUE.Value());
343 EXPECT_EQ(svgTextPath->GetDeclaration()->GetSvgTextStyle().GetFontSize().Value(), SVG_TEXT_FONT_SIZE.Value());
344 EXPECT_EQ(svgTextPath->GetDeclaration()->GetFillState().GetColor().GetValue(), SVG_FILL_COLOR.GetValue());
345 EXPECT_EQ(svgTextPath->GetDeclaration()->GetFillState().GetOpacity().GetValue(), SVG_FILL_OPACITY_VALUE);
346 EXPECT_EQ(svgTextPath->GetDeclaration()->GetStrokeState().GetColor().GetValue(), SVG_STROKE_COLOR.GetValue());
347 EXPECT_EQ(svgTextPath->GetDeclaration()->GetStrokeState().GetOpacity().GetValue(), SVG_STROKE_OPACITY_VALUE);
348 EXPECT_EQ(svgTextPath->GetDeclaration()->GetStrokeState().GetLineWidth().Value(), SVG_STROKE_WIDTH_VALUE.Value());
349 }
350
351 } // namespace OHOS::Ace::Framework
352