• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 #include "font_collection.h"
18 #include "modules/skparagraph/src/ParagraphBuilderImpl.h"
19 #include "paragraph_builder.h"
20 #include "paragraph_builder_impl.h"
21 #include "paragraph_style.h"
22 
23 using namespace testing;
24 using namespace testing::ext;
25 using namespace OHOS::Rosen::SPText;
26 
27 namespace txt {
28 class ParagraphBuilderTest : public testing::Test {
29 public:
30     void SetUp() override;
31     void TearDown() override;
32 
33 private:
34     std::shared_ptr<ParagraphBuilderImpl> paragraphBuilder_;
35 };
36 
37 
SetUp()38 void ParagraphBuilderTest::SetUp()
39 {
40     ParagraphStyle paragraphStyle;
41     std::shared_ptr<FontCollection> fontCollection = std::make_shared<FontCollection>();
42     paragraphBuilder_ = std::make_shared<ParagraphBuilderImpl>(paragraphStyle, fontCollection);
43     ASSERT_NE(paragraphBuilder_, nullptr);
44     ASSERT_NE(paragraphBuilder_->builder_, nullptr);
45 }
46 
47 
TearDown()48 void ParagraphBuilderTest::TearDown()
49 {
50     paragraphBuilder_.reset();
51 }
52 
53 /*
54  * @tc.name: ParagraphBuilderTest001
55  * @tc.desc: test for PushStyle
56  * @tc.type: FUNC
57  */
58 HWTEST_F(ParagraphBuilderTest, ParagraphBuilderTest001, TestSize.Level0)
59 {
60     TextStyle style;
61     paragraphBuilder_->PushStyle(style);
62     auto peekStyle = paragraphBuilder_->builder_->peekStyle();
63     EXPECT_EQ(peekStyle.fFontSize, style.fontSize);
64     EXPECT_EQ(peekStyle.fHeight, style.height);
65     EXPECT_EQ(peekStyle.fHeightOverride, style.heightOverride);
66     EXPECT_EQ(peekStyle.fBaselineShift, style.baseLineShift);
67     EXPECT_EQ(peekStyle.fHalfLeading, style.halfLeading);
68     EXPECT_EQ(peekStyle.fBackgroundRect.color, style.backgroundRect.color);
69     EXPECT_EQ(peekStyle.fStyleId, style.styleId);
70     EXPECT_EQ(peekStyle.fLetterSpacing, style.letterSpacing);
71     EXPECT_EQ(peekStyle.fWordSpacing, style.wordSpacing);
72     EXPECT_EQ(static_cast<int>(peekStyle.fTextBaseline), static_cast<int>(style.baseline));
73     EXPECT_EQ(peekStyle.fColor, style.color);
74     EXPECT_EQ(peekStyle.fHasBackground, style.background.has_value());
75     EXPECT_EQ(peekStyle.fIsPlaceholder, style.isPlaceholder);
76 }
77 
78 /*
79  * @tc.name: ParagraphBuilderTest002
80  * @tc.desc: test for Pop
81  * @tc.type: FUNC
82  */
83 HWTEST_F(ParagraphBuilderTest, ParagraphBuilderTest002, TestSize.Level0)
84 {
85     TextStyle style;
86     // 10 is just for test
87     style.styleId = 10;
88     paragraphBuilder_->PushStyle(style);
89     TextStyle style1;
90     paragraphBuilder_->PushStyle(style1);
91     paragraphBuilder_->Pop();
92     EXPECT_EQ(paragraphBuilder_->builder_->peekStyle().fStyleId, style.styleId);
93 }
94 
95 /*
96  * @tc.name: ParagraphBuilderTest004
97  * @tc.desc: test for AddText
98  * @tc.type: FUNC
99  */
100 HWTEST_F(ParagraphBuilderTest, ParagraphBuilderTest004, TestSize.Level0)
101 {
102     std::u16string text = u"text";
103     paragraphBuilder_->AddText(text);
104     auto skText = paragraphBuilder_->builder_->getText();
105     EXPECT_EQ(std::string(skText.data()), std::string(text.begin(), text.end()));
106 }
107 
108 /*
109  * @tc.name: ParagraphBuilderTest005
110  * @tc.desc: test for AddPlaceholder
111  * @tc.type: FUNC
112  */
113 HWTEST_F(ParagraphBuilderTest, ParagraphBuilderTest005, TestSize.Level0)
114 {
115     PlaceholderRun placeholderRunDefault;
116     // 50 just for test
117     PlaceholderRun placeholderRun(50, 50, PlaceholderAlignment::BASELINE, TextBaseline::ALPHABETIC, 0.0);
118     paragraphBuilder_->AddPlaceholder(placeholderRunDefault);
119     paragraphBuilder_->AddPlaceholder(placeholderRun);
120     paragraphBuilder_->AddText(u"text");
121     auto builder = static_cast<skia::textlayout::ParagraphBuilderImpl*>(paragraphBuilder_->builder_.get());
122     // only two placeholders, because we add two placeholders before
123     EXPECT_EQ(builder->fPlaceholders.size(), 2);
124     EXPECT_EQ(builder->fPlaceholders[0].fStyle.fHeight, placeholderRunDefault.height);
125     EXPECT_EQ(builder->fPlaceholders[0].fStyle.fWidth, placeholderRunDefault.width);
126     EXPECT_EQ(builder->fPlaceholders[1].fStyle.fHeight, placeholderRun.height);
127     EXPECT_EQ(builder->fPlaceholders[1].fStyle.fWidth, placeholderRun.width);
128 }
129 
130 /*
131  * @tc.name: ParagraphBuilderTest007
132  * @tc.desc: test for locale
133  * @tc.type: FUNC
134  */
135 HWTEST_F(ParagraphBuilderTest, ParagraphBuilderTest007, TestSize.Level0)
136 {
137     std::string localeEN = "en-US";
138     TextStyle textStyle;
139     textStyle.locale = localeEN;
140 
141     paragraphBuilder_->PushStyle(textStyle);
142     paragraphBuilder_->AddText(u"text");
143     EXPECT_EQ(paragraphBuilder_->builder_->peekStyle().fLocale, SkString(localeEN));
144     EXPECT_EQ(paragraphBuilder_->builder_->peekStyle().getLocale(), SkString(localeEN));
145 }
146 
147 /*
148  * @tc.name: ParagraphBuilderTest008
149  * @tc.desc: test for italic font style
150  * @tc.type: FUNC
151  */
152 HWTEST_F(ParagraphBuilderTest, ParagraphBuilderTest008, TestSize.Level0)
153 {
154     TextStyle textStyle;
155     textStyle.fontStyle = FontStyle::ITALIC;
156     paragraphBuilder_->PushStyle(textStyle);
157     paragraphBuilder_->AddText(u"text");
158     EXPECT_EQ(paragraphBuilder_->builder_->peekStyle().fFontStyle.GetSlant(), RSFontStyle::Slant::ITALIC_SLANT);
159 }
160 
161 /*
162  * @tc.name: ParagraphBuilderTest009
163  * @tc.desc: test for oblique font style
164  * @tc.type: FUNC
165  */
166 HWTEST_F(ParagraphBuilderTest, ParagraphBuilderTest009, TestSize.Level0)
167 {
168     TextStyle textStyle;
169     textStyle.fontStyle = FontStyle::OBLIQUE;
170     paragraphBuilder_->PushStyle(textStyle);
171     paragraphBuilder_->AddText(u"text");
172     EXPECT_EQ(paragraphBuilder_->builder_->peekStyle().fFontStyle.GetSlant(), RSFontStyle::Slant::OBLIQUE_SLANT);
173 }
174 
175 /*
176  * @tc.name: ParagraphBuilderTest010
177  * @tc.desc: test for default font style
178  * @tc.type: FUNC
179  */
180 HWTEST_F(ParagraphBuilderTest, ParagraphBuilderTest010, TestSize.Level0)
181 {
182     TextStyle textStyle;
183     // 999 is not a valid value, so the default value should be used
184     textStyle.fontStyle = static_cast<FontStyle>(999);
185     paragraphBuilder_->PushStyle(textStyle);
186     paragraphBuilder_->AddText(u"text");
187     EXPECT_EQ(paragraphBuilder_->builder_->peekStyle().fFontStyle.GetSlant(), RSFontStyle::Slant::UPRIGHT_SLANT);
188 }
189 
190 /*
191  * @tc.name: ParagraphBuilderTest011
192  * @tc.desc: test for MakeTextShadow
193  * @tc.type: FUNC
194  */
195 HWTEST_F(ParagraphBuilderTest, ParagraphBuilderTest011, TestSize.Level0)
196 {
197     TextStyle textStyle;
198     textStyle.textShadows.emplace_back();
199     EXPECT_EQ(textStyle.textShadows.size(), 1);
200     paragraphBuilder_->PushStyle(textStyle);
201     paragraphBuilder_->AddText(u"text");
202     // builder should have one shadow, because we only push one shadow
203     ASSERT_EQ(textStyle.textShadows.size(), 1);
204     ASSERT_EQ(textStyle.textShadows.size(), paragraphBuilder_->builder_->peekStyle().fTextShadows.size());
205     EXPECT_EQ(paragraphBuilder_->builder_->peekStyle().fTextShadows.at(0).fColor, textStyle.textShadows.at(0).color);
206     EXPECT_EQ(paragraphBuilder_->builder_->peekStyle().fTextShadows.at(0).fOffset, textStyle.textShadows.at(0).offset);
207 }
208 
209 /*
210  * @tc.name: ParagraphBuilderTest012
211  * @tc.desc: test for isPlaceholder is true
212  * @tc.type: FUNC
213  */
214 HWTEST_F(ParagraphBuilderTest, ParagraphBuilderTest012, TestSize.Level0)
215 {
216     TextStyle textStyle;
217     textStyle.isPlaceholder = true;
218     paragraphBuilder_->PushStyle(textStyle);
219     paragraphBuilder_->AddText(u"text");
220     EXPECT_EQ(paragraphBuilder_->builder_->peekStyle().isPlaceholder(), textStyle.isPlaceholder);
221 }
222 
223 /*
224  * @tc.name: ParagraphBuilderTest013
225  * @tc.desc: test for BuildLineFetcher
226  * @tc.type: FUNC
227  */
228 HWTEST_F(ParagraphBuilderTest, ParagraphBuilderTest013, TestSize.Level0)
229 {
230     TextStyle textStyle;
231     textStyle.textShadows.emplace_back();
232     EXPECT_EQ(textStyle.textShadows.size(), 1);
233     paragraphBuilder_->PushStyle(textStyle);
234     paragraphBuilder_->AddText(u"text");
235     EXPECT_EQ(paragraphBuilder_->BuildLineFetcher()->GetUnicodeSize(), 4);
236 }
237 
238 /*
239  * @tc.name: ParagraphBuilderTest014
240  * @tc.desc: test for BuildLineFetcher
241  * @tc.type: FUNC
242  */
243 HWTEST_F(ParagraphBuilderTest, ParagraphBuilderTest014, TestSize.Level0)
244 {
245     EXPECT_EQ(paragraphBuilder_->BuildLineFetcher(), nullptr);
246 }
247 
248 /*
249  * @tc.name: ParagraphBuilderTest015
250  * @tc.desc: test for TextStyleToSkStyle ParagraphSpacing
251  * @tc.type: FUNC
252  */
253  HWTEST_F(ParagraphBuilderTest, ParagraphBuilderTest015, TestSize.Level0)
254  {
255      skia::textlayout::ParagraphStyle skStyle;
256      ParagraphStyle txt;
257      txt.paragraphSpacing = 100.0f;
258      txt.isEndAddParagraphSpacing = true;
259      skStyle.setParagraphSpacing(txt.paragraphSpacing);
260      skStyle.setIsEndAddParagraphSpacing(txt.isEndAddParagraphSpacing);
261      EXPECT_EQ(skStyle.getParagraphSpacing(), txt.paragraphSpacing);
262      EXPECT_EQ(skStyle.getIsEndAddParagraphSpacing(), txt.isEndAddParagraphSpacing);
263 
264      txt.paragraphSpacing = 0.0f;
265      txt.isEndAddParagraphSpacing = true;
266      skStyle.setParagraphSpacing(txt.paragraphSpacing);
267      skStyle.setIsEndAddParagraphSpacing(txt.isEndAddParagraphSpacing);
268      EXPECT_EQ(skStyle.getParagraphSpacing(), txt.paragraphSpacing);
269      EXPECT_EQ(skStyle.getIsEndAddParagraphSpacing(), txt.isEndAddParagraphSpacing);
270  }
271 
272 /*
273  * @tc.name: ParagraphBuilderTest016
274  * @tc.desc: test for TextStyleToSkStyle AutoSpace
275  * @tc.type: FUNC
276  */
277 HWTEST_F(ParagraphBuilderTest, ParagraphBuilderTest016, TestSize.Level0)
278 {
279      skia::textlayout::ParagraphStyle skStyle;
280      ParagraphStyle txt;
281      txt.enableAutoSpace = true;
282      skStyle.setEnableAutoSpace(txt.enableAutoSpace);
283      EXPECT_EQ(skStyle.getEnableAutoSpace(), txt.enableAutoSpace);
284 }
285 } // namespace txt