1 /*
2 * Copyright (c) 2023 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 "font_collection.h"
19 #include "measurer.h"
20 #include "mock/mock_any_span.h"
21 #include "mock/mock_measurer.h"
22 #include "param_test_macros.h"
23 #include "texgine_exception.h"
24 #include "texgine/font_providers.h"
25 #include "texgine_text_blob.h"
26 #include "texgine_text_blob_builder.h"
27 #include "texgine/typography_types.h"
28 #include "text_shaper.h"
29 #include "typeface.h"
30
31 using namespace testing;
32 using namespace testing::ext;
33
34 namespace OHOS {
35 namespace Rosen {
36 namespace TextEngine {
37 struct MockVars {
38 std::vector<uint16_t> catchedBufferGlyphs;
39 std::vector<float> catchedBufferPos;
40 std::vector<std::string> catchedGenerateFontCollectionFamilies;
41
42 std::shared_ptr<TexgineTextBlob> retvalTextBlobBuilderMake = nullptr;
43 std::shared_ptr<FontCollection> retvalGenerateFontCollection =
44 std::make_shared<FontCollection>(std::vector<std::shared_ptr<VariantFontStyleSet>>{});
45 std::unique_ptr<MockMeasurer> retvalMeasurerCreate = std::make_unique<MockMeasurer>();
46 } g_tsMockvars;
47
InitTsMockVars(struct MockVars && vars)48 void InitTsMockVars(struct MockVars &&vars)
49 {
50 g_tsMockvars = std::move(vars);
51 }
52
53 #ifndef USE_ROSEN_DRAWING
Make()54 std::shared_ptr<TexgineTextBlob> TexgineTextBlobBuilder::Make()
55 {
56 return g_tsMockvars.retvalTextBlobBuilderMake;
57 }
58 #endif
59
GenerateFontCollection(const std::vector<std::string> & families) const60 std::shared_ptr<FontCollection> FontProviders::GenerateFontCollection(
61 const std::vector<std::string> &families) const noexcept(true)
62 {
63 g_tsMockvars.catchedGenerateFontCollectionFamilies = families;
64 return g_tsMockvars.retvalGenerateFontCollection;
65 }
66
Create(const std::vector<uint16_t> & text,const FontCollection & fontCollection)67 std::unique_ptr<Measurer> Measurer::Create(const std::vector<uint16_t> &text,
68 const FontCollection &fontCollection)
69 {
70 return std::move(g_tsMockvars.retvalMeasurerCreate);
71 }
72
73 struct TextSpanInfo {
74 CharGroups cgs_ = CharGroups::CreateEmpty();
75 };
76
77 class ControllerForTest {
78 public:
GenerateTextSpan(TextSpanInfo info)79 static std::shared_ptr<TextSpan> GenerateTextSpan(TextSpanInfo info)
80 {
81 auto ts = std::make_shared<TextSpan>();
82 ts->cgs_ = info.cgs_;
83 return ts;
84 }
85 };
86
GenerateTextSpan(TextSpanInfo info)87 auto GenerateTextSpan(TextSpanInfo info)
88 {
89 return ControllerForTest::GenerateTextSpan(info);
90 }
91
92 class TextShaperTest : public testing::TestWithParam<std::shared_ptr<TextSpan>> {
93 public:
SetUpTestCase()94 static void SetUpTestCase()
95 {
96 // {1, 1, 0, 0.1, 0.1} is {code point, advanceX, advanceY, offsetX, offsetY}
97 cgs1_.PushBack({ .glyphs = { {1, 1, 0, 0.1, 0.1}, {2, 1, 0, 0.2, 0.2} }, .visibleWidth = 2 });
98 cgs2_.PushBack({ .glyphs = { {1, 1, 0, 0.1, 0.1} }, .visibleWidth = 1 });
99 cgs2_.PushBack({ .glyphs = { {2, 1, 0, 0.2, 0.2} }, .visibleWidth = 1 });
100 }
101
102 static inline CharGroups cgs1_ = CharGroups::CreateEmpty();
103 static inline CharGroups cgs2_ = CharGroups::CreateEmpty();
104 static inline std::shared_ptr<TypographyStyle> ysNormal_ = std::make_shared<TypographyStyle>();
105 static inline std::shared_ptr<TypographyStyle> ysNoProvider_ = std::make_shared<TypographyStyle>();
106 };
107
108 /**
109 * @tc.name: DoShape1
110 * @tc.desc: Verify the DoShape
111 * @tc.type:FUNC
112 */
113 HWTEST_F(TextShaperTest, DoShape1, TestSize.Level1)
114 {
115 TextShaper shaper;
116 auto fp = FontProviders::Create();
117 std::shared_ptr<TextSpan> tsNullptr;
118 auto tsNormal = TextSpan::MakeFromText("normal");
119 ASSERT_EQ(1, shaper.DoShape(tsNullptr, {}, {}, fp));
120 ASSERT_EQ(1, shaper.DoShape(tsNormal, {}, {}, nullptr));
121 }
122
123 /**
124 * @tc.name: DoShape2
125 * @tc.desc: Verify the DoShape
126 * @tc.type:FUNC
127 */
128 HWTEST_F(TextShaperTest, DoShape2, TestSize.Level1)
129 {
130 InitTsMockVars({});
131 EXPECT_CALL(*g_tsMockvars.retvalMeasurerCreate, Measure).Times(1).WillOnce(testing::Return(0));
132 TypographyStyle ys;
133 ys.fontFamilies = {"Roboto"};
134 auto span = GenerateTextSpan({.cgs_ = cgs1_});
135
136 EXPECT_NO_THROW({
137 TextShaper shaper;
138 shaper.DoShape(span, {}, ys, FontProviders::Create());
139 ASSERT_EQ(ys.fontFamilies, g_tsMockvars.catchedGenerateFontCollectionFamilies);
140 });
141 }
142
143 /**
144 * @tc.name: DoShape3
145 * @tc.desc: Verify the DoShape
146 * @tc.type:FUNC
147 */
148 HWTEST_F(TextShaperTest, DoShape3, TestSize.Level1)
149 {
150 InitTsMockVars({});
151 EXPECT_CALL(*g_tsMockvars.retvalMeasurerCreate, Measure).Times(1).WillOnce(testing::Return(0));
152 TextStyle style = {.fontFamilies = {"Sans"}};
153 auto span = GenerateTextSpan({.cgs_ = cgs1_});
154
155 EXPECT_NO_THROW({
156 TextShaper shaper;
157 shaper.DoShape(span, style, {}, FontProviders::Create());
158 ASSERT_EQ(style.fontFamilies, g_tsMockvars.catchedGenerateFontCollectionFamilies);
159 });
160 }
161
162 /**
163 * @tc.name: DoShape4
164 * @tc.desc: Verify the DoShape
165 * @tc.type:FUNC
166 */
167 HWTEST_F(TextShaperTest, DoShape4, TestSize.Level1)
168 {
169 InitTsMockVars({.retvalGenerateFontCollection = nullptr});
170 auto span = GenerateTextSpan({});
171
172 EXPECT_NO_THROW({
173 TextShaper shaper;
174 auto ret = shaper.DoShape(span, {}, {}, FontProviders::Create());
175 ASSERT_EQ(ret, 1);
176 });
177 }
178
179 /**
180 * @tc.name: DoShape5
181 * @tc.desc: Verify the DoShape
182 * @tc.type:FUNC
183 */
184 HWTEST_F(TextShaperTest, DoShape5, TestSize.Level1)
185 {
186 InitTsMockVars({});
187 EXPECT_CALL(*g_tsMockvars.retvalMeasurerCreate, Measure).Times(1).WillOnce(testing::Return(1));
188 auto span = GenerateTextSpan({.cgs_ = cgs1_});
189
190 EXPECT_NO_THROW({
191 TextShaper shaper;
192 auto ret = shaper.DoShape(span, {}, {}, FontProviders::Create());
193 ASSERT_EQ(ret, 1);
194 });
195 }
196
197 /**
198 * @tc.name: DoShape6
199 * @tc.desc: Verify the DoShape
200 * @tc.type:FUNC
201 */
202 HWTEST_F(TextShaperTest, DoShape6, TestSize.Level1)
203 {
204 InitTsMockVars({});
205 EXPECT_CALL(*g_tsMockvars.retvalMeasurerCreate, Measure).Times(1).WillOnce(testing::Return(0));
206 auto span = GenerateTextSpan({.cgs_ = cgs1_});
207
208 EXPECT_NO_THROW({
209 TextShaper shaper;
210 auto ret = shaper.DoShape(span, {}, {}, FontProviders::Create());
211 ASSERT_EQ(ret, 0);
212 });
213 }
214
215 /**
216 * @tc.name: GenerateTextBlob1
217 * @tc.desc: Verify the GenerateTextBlob
218 * @tc.type:FUNC
219 */
220 HWTEST_F(TextShaperTest, GenerateTextBlob1, TestSize.Level1)
221 {
222 double spanWidth = 0.0;
223 std::vector<double> glyphWidths;
224 TextShaper shaper;
225 TexgineFont font;
226 ASSERT_EQ(nullptr, shaper.GenerateTextBlob(font, CharGroups::CreateEmpty(), spanWidth, glyphWidths));
227 }
228
229 /**
230 * @tc.name: Shape1
231 * @tc.desc: Verify the Shape
232 * @tc.type:FUNC
233 */
234 HWTEST_F(TextShaperTest, Shape1, TestSize.Level1)
235 {
236 std::shared_ptr<TextSpan> tsNullptr = nullptr;
237 std::shared_ptr<AnySpan> asNullptr = nullptr;
238 TextShaper shaper;
239 ASSERT_EQ(1, shaper.Shape(tsNullptr, {}, FontProviders::Create()));
240 ASSERT_EQ(1, shaper.Shape(asNullptr, {}, FontProviders::Create()));
241 }
242
243 /**
244 * @tc.name: Shape2
245 * @tc.desc: Verify the Shape
246 * @tc.type:FUNC
247 */
248 HWTEST_F(TextShaperTest, Shape2, TestSize.Level1)
249 {
250 InitTsMockVars({.retvalGenerateFontCollection = nullptr});
251
252 EXPECT_NO_THROW({
253 std::shared_ptr<MockAnySpan> mas = std::make_shared<MockAnySpan>();
254 TextShaper shaper;
255 auto ret = shaper.Shape(mas, {}, FontProviders::Create());
256 ASSERT_EQ(ret, 0);
257 });
258 }
259
260 /**
261 * @tc.name: Shape3
262 * @tc.desc: Verify the Shape
263 * @tc.type:FUNC
264 */
265 HWTEST_F(TextShaperTest, Shape3, TestSize.Level1)
266 {
267 InitTsMockVars({.retvalGenerateFontCollection = nullptr});
268
269 EXPECT_NO_THROW({
270 TextShaper shaper;
271 auto ret = shaper.Shape(GenerateTextSpan({}), {}, FontProviders::Create());
272 ASSERT_EQ(ret, 1);
273 });
274 }
275
276 /**
277 * @tc.name: Shape4
278 * @tc.desc: Verify the Shape
279 * @tc.type:FUNC
280 */
281 HWTEST_F(TextShaperTest, Shape4, TestSize.Level1)
282 {
283 InitTsMockVars({});
284 TextShaper shaper;
285 ASSERT_EXCEPTION(ExceptionType::INVALID_CHAR_GROUPS,
286 shaper.Shape(GenerateTextSpan({.cgs_ = {}}), {}, FontProviders::Create()));
287
288 InitTsMockVars({});
289 EXPECT_CALL(*g_tsMockvars.retvalMeasurerCreate, Measure).Times(1).WillOnce(testing::Return(0));
290 cgs1_.Get(0).typeface = nullptr;
291
292 ASSERT_EQ(1, shaper.Shape(GenerateTextSpan({.cgs_ = cgs1_}), {}, FontProviders::Create()));
293 }
294 } // namespace TextEngine
295 } // namespace Rosen
296 } // namespace OHOS
297