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 "drawing_painter_impl.h"
18 #include "recording_canvas.h"
19 #include "text_blob_builder.h"
20
21 using namespace testing;
22 using namespace testing::ext;
23 using namespace OHOS::Rosen::Drawing;
24 using namespace OHOS::Rosen::SPText;
25 using namespace OHOS::Rosen::TextEngine;
26 using namespace skia::textlayout;
27
28 namespace txt {
29 class RSCanvasParagraphPainterTest : public testing::Test {
30 public:
31 static void SetUpTestCase();
32 static void TearDownTestCase();
33 static bool AnimationFunc(const std::shared_ptr<SymbolAnimationConfig>& symbolAnimationConfig);
34 static inline std::vector<PaintRecord> paintRecords_;
35 static inline std::vector<PaintRecord> paintRecordsWithoutPen_;
36 static inline std::vector<PaintRecord> paintRecordsWithoutBrush_;
37 static inline std::vector<PaintRecord> paintRecordsWithoutPenBrush_;
38 static inline PaintRecord paintRecord_;
39 static inline PaintRecord paintRecordWithoutPen_;
40 static inline PaintRecord paintRecordWithoutBrush_;
41 static inline PaintRecord paintRecordWithoutPenBrush_;
42 static inline std::shared_ptr<RSCanvasParagraphPainter> canvasParagraphPainter_ = nullptr;
43 static inline std::shared_ptr<RSCanvasParagraphPainter> canvasParagraphPainterWithoutPen_ = nullptr;
44 static inline std::shared_ptr<RSCanvasParagraphPainter> canvasParagraphPainterWithoutBrush_ = nullptr;
45 static inline std::shared_ptr<RSCanvasParagraphPainter> canvasParagraphPainterWithoutPenBrush_ = nullptr;
46 static inline std::shared_ptr<RecordingCanvas> recordingCanvas_ = nullptr;
47 };
48
SetUpTestCase()49 void RSCanvasParagraphPainterTest::SetUpTestCase()
50 {
51 Brush rsBrush;
52 Pen rsPen;
53 PaintRecord::RSColor color(0, 0, 0, 255); // 255 just fot test
54 rsBrush.SetColor(color);
55 rsPen.SetColor(color);
56 std::optional<RSBrush> brush(rsBrush);
57 std::optional<RSPen> pen(rsPen);
58 paintRecord_ = PaintRecord(brush, pen);
59 paintRecords_.push_back(paintRecord_);
60
61 int32_t width = 200; // 200 just for test
62 int32_t height = 100; // 100 just for test
63 recordingCanvas_ = std::make_shared<RecordingCanvas>(width, height);
64 if (!recordingCanvas_) {
65 std::cout << "RSCanvasParagraphPainterTest::SetUpTestCase error recordingCanvas_ is nullptr"
66 << std::endl;
67 return;
68 }
69 canvasParagraphPainter_ = std::make_shared<RSCanvasParagraphPainter>(recordingCanvas_.get(), paintRecords_);
70 if (!canvasParagraphPainter_) {
71 std::cout << "RSCanvasParagraphPainterTest::SetUpTestCase error canvasParagraphPainter_ is nullptr"
72 << std::endl;
73 }
74
75 // without pen
76 pen.reset();
77 paintRecordWithoutPen_ = PaintRecord(brush, pen);
78 paintRecordsWithoutPen_.push_back(paintRecordWithoutPen_);
79 canvasParagraphPainterWithoutPen_ =
80 std::make_shared<RSCanvasParagraphPainter>(recordingCanvas_.get(), paintRecordsWithoutPen_);
81
82 // without brush
83 brush.reset();
84 pen = std::optional<RSPen>(rsPen);
85 paintRecordWithoutBrush_ = PaintRecord(brush, pen);
86 paintRecordsWithoutBrush_.push_back(paintRecordWithoutBrush_);
87 canvasParagraphPainterWithoutBrush_ =
88 std::make_shared<RSCanvasParagraphPainter>(recordingCanvas_.get(), paintRecordsWithoutBrush_);
89
90 // without pen and brush
91 pen.reset();
92 paintRecordWithoutPenBrush_ = PaintRecord(brush, pen);
93 paintRecordsWithoutPenBrush_.push_back(paintRecordWithoutPenBrush_);
94 canvasParagraphPainterWithoutPenBrush_ =
95 std::make_shared<RSCanvasParagraphPainter>(recordingCanvas_.get(), paintRecordsWithoutPenBrush_);
96 }
97
TearDownTestCase()98 void RSCanvasParagraphPainterTest::TearDownTestCase()
99 {
100 }
101
AnimationFunc(const std::shared_ptr<SymbolAnimationConfig> & symbolAnimationConfig)102 bool RSCanvasParagraphPainterTest::AnimationFunc(const std::shared_ptr<SymbolAnimationConfig>& symbolAnimationConfig)
103 {
104 return true;
105 }
106
107 /*
108 * @tc.name: RSCanvasParagraphPainterTest001
109 * @tc.desc: test for drawTextBlob
110 * @tc.type: FUNC
111 */
112 HWTEST_F(RSCanvasParagraphPainterTest, RSCanvasParagraphPainterTest001, TestSize.Level0)
113 {
114 ASSERT_NE(canvasParagraphPainter_, nullptr);
115 ASSERT_NE(recordingCanvas_, nullptr);
116 std::string str("TextBlob");
117 Font font;
118 std::shared_ptr<TextBlob> textBlob = TextBlob::MakeFromText(str.c_str(), str.length(), font);
119 ASSERT_NE(textBlob, nullptr);
120 ParagraphPainter::PaintID paintID = static_cast<int>(paintRecords_.size()) - 1;
121 ParagraphPainter::SkPaintOrID paint;
122 paint.emplace<ParagraphPainter::PaintID>(paintID);
123 canvasParagraphPainter_->drawTextBlob(textBlob, 0.0, 0.0, paint);
124 }
125
126 /*
127 * @tc.name: RSCanvasParagraphPainterTest002
128 * @tc.desc: test for drawTextShadow
129 * @tc.type: FUNC
130 */
131 HWTEST_F(RSCanvasParagraphPainterTest, RSCanvasParagraphPainterTest002, TestSize.Level0)
132 {
133 ASSERT_NE(canvasParagraphPainter_, nullptr);
134 ASSERT_NE(recordingCanvas_, nullptr);
135 std::string str("drawTextShadow");
136 Font font;
137 std::shared_ptr<TextBlob> textBlob = TextBlob::MakeFromText(str.c_str(), str.length(), font);
138 ASSERT_NE(textBlob, nullptr);
139 SkColor color = 255; // 255 just fot test
140 canvasParagraphPainter_->drawTextShadow(textBlob, 0.0, 0.0, color, 0.5); // 0.5 just fot test
141 }
142
143 /*
144 * @tc.name: RSCanvasParagraphPainterTest003
145 * @tc.desc: test for drawRect
146 * @tc.type: FUNC
147 */
148 HWTEST_F(RSCanvasParagraphPainterTest, RSCanvasParagraphPainterTest003, TestSize.Level0)
149 {
150 ASSERT_NE(canvasParagraphPainter_, nullptr);
151 ASSERT_NE(recordingCanvas_, nullptr);
152 ParagraphPainter::PaintID paintID = static_cast<int>(paintRecords_.size()) - 1;
153 ParagraphPainter::SkPaintOrID paint;
154 paint.emplace<ParagraphPainter::PaintID>(paintID);
155 SkRect rect = SkRect::MakeEmpty();
156 canvasParagraphPainter_->drawRect(rect, paint);
157 }
158
159 /*
160 * @tc.name: RSCanvasParagraphPainterTest004
161 * @tc.desc: test for drawRRect
162 * @tc.type: FUNC
163 */
164 HWTEST_F(RSCanvasParagraphPainterTest, RSCanvasParagraphPainterTest004, TestSize.Level0)
165 {
166 ASSERT_NE(canvasParagraphPainter_, nullptr);
167 ASSERT_NE(recordingCanvas_, nullptr);
168 SkRRect rect = SkRRect::MakeEmpty();
169 SkColor color = 255; // 255 just fot test
170 canvasParagraphPainter_->drawRRect(rect, color);
171 }
172
173 /*
174 * @tc.name: RSCanvasParagraphPainterTest005
175 * @tc.desc: test for drawFilledRect
176 * @tc.type: FUNC
177 */
178 HWTEST_F(RSCanvasParagraphPainterTest, RSCanvasParagraphPainterTest005, TestSize.Level0)
179 {
180 ASSERT_NE(canvasParagraphPainter_, nullptr);
181 ASSERT_NE(recordingCanvas_, nullptr);
182 SkRect rect = SkRect::MakeEmpty();
183 ParagraphPainter::DecorationStyle decorStyle;
184 canvasParagraphPainter_->drawFilledRect(rect, decorStyle);
185 }
186
187 /*
188 * @tc.name: RSCanvasParagraphPainterTest006
189 * @tc.desc: test for drawPath
190 * @tc.type: FUNC
191 */
192 HWTEST_F(RSCanvasParagraphPainterTest, RSCanvasParagraphPainterTest006, TestSize.Level0)
193 {
194 ASSERT_NE(canvasParagraphPainter_, nullptr);
195 ASSERT_NE(recordingCanvas_, nullptr);
196 ParagraphPainter::DecorationStyle decorStyle;
197 Path path;
198 canvasParagraphPainter_->drawPath(path, decorStyle);
199 }
200
201 /*
202 * @tc.name: RSCanvasParagraphPainterTest007
203 * @tc.desc: test for drawLine
204 * @tc.type: FUNC
205 */
206 HWTEST_F(RSCanvasParagraphPainterTest, RSCanvasParagraphPainterTest007, TestSize.Level0)
207 {
208 ASSERT_NE(canvasParagraphPainter_, nullptr);
209 ASSERT_NE(recordingCanvas_, nullptr);
210 ParagraphPainter::DecorationStyle decorStyle;
211 canvasParagraphPainter_->drawLine(0.0, 0.0, 0.0, 0.0, decorStyle);
212 }
213
214 /*
215 * @tc.name: RSCanvasParagraphPainterTest008
216 * @tc.desc: test for SymbolAnimation
217 * @tc.type: FUNC
218 */
219 HWTEST_F(RSCanvasParagraphPainterTest, RSCanvasParagraphPainterTest008, TestSize.Level0)
220 {
221 ASSERT_NE(canvasParagraphPainter_, nullptr);
222 ASSERT_NE(recordingCanvas_, nullptr);
223 PaintRecord paintRecord;
224 canvasParagraphPainter_->SymbolAnimation(paintRecord);
225 }
226
227 /*
228 * @tc.name: RSCanvasParagraphPainterTest009
229 * @tc.desc: test for DrawSymbolSkiaTxt
230 * @tc.type: FUNC
231 */
232 HWTEST_F(RSCanvasParagraphPainterTest, RSCanvasParagraphPainterTest009, TestSize.Level0)
233 {
234 ASSERT_NE(canvasParagraphPainter_, nullptr);
235 ASSERT_NE(recordingCanvas_, nullptr);
236
237 PaintRecord paintRecord;
238 Point offset;
239 std::string str("DrawSymbolSkiaTxt");
240 Font font;
241 std::shared_ptr<TextBlob> textBlob = TextBlob::MakeFromText(str.c_str(), str.length(), font);
242 ASSERT_NE(textBlob, nullptr);
243 canvasParagraphPainter_->DrawSymbolSkiaTxt(textBlob, offset, paintRecord);
244 }
245
246 /*
247 * @tc.name: RSCanvasParagraphPainterTest010
248 * @tc.desc: test for clipRect
249 * @tc.type: FUNC
250 */
251 HWTEST_F(RSCanvasParagraphPainterTest, RSCanvasParagraphPainterTest010, TestSize.Level0)
252 {
253 ASSERT_NE(canvasParagraphPainter_, nullptr);
254 ASSERT_NE(recordingCanvas_, nullptr);
255 SkRect rect = SkRect::MakeEmpty();
256 canvasParagraphPainter_->clipRect(rect);
257 }
258
259 /*
260 * @tc.name: RSCanvasParagraphPainterTest011
261 * @tc.desc: test for translate
262 * @tc.type: FUNC
263 */
264 HWTEST_F(RSCanvasParagraphPainterTest, RSCanvasParagraphPainterTest011, TestSize.Level0)
265 {
266 ASSERT_NE(canvasParagraphPainter_, nullptr);
267 ASSERT_NE(recordingCanvas_, nullptr);
268 canvasParagraphPainter_->translate(0.0, 0.0);
269 }
270
271 /*
272 * @tc.name: RSCanvasParagraphPainterTest012
273 * @tc.desc: test for save
274 * @tc.type: FUNC
275 */
276 HWTEST_F(RSCanvasParagraphPainterTest, RSCanvasParagraphPainterTest012, TestSize.Level0)
277 {
278 ASSERT_NE(canvasParagraphPainter_, nullptr);
279 ASSERT_NE(recordingCanvas_, nullptr);
280 canvasParagraphPainter_->save();
281 }
282
283 /*
284 * @tc.name: RSCanvasParagraphPainterTest013
285 * @tc.desc: test for restore
286 * @tc.type: FUNC
287 */
288 HWTEST_F(RSCanvasParagraphPainterTest, RSCanvasParagraphPainterTest013, TestSize.Level0)
289 {
290 ASSERT_NE(canvasParagraphPainter_, nullptr);
291 ASSERT_NE(recordingCanvas_, nullptr);
292 canvasParagraphPainter_->restore();
293 }
294
295 /*
296 * @tc.name: RSCanvasParagraphPainterTest014
297 * @tc.desc: test for SetAnimation
298 * @tc.type: FUNC
299 */
300 HWTEST_F(RSCanvasParagraphPainterTest, RSCanvasParagraphPainterTest014, TestSize.Level0)
301 {
302 ASSERT_NE(canvasParagraphPainter_, nullptr);
303 ASSERT_NE(recordingCanvas_, nullptr);
304 std::function<bool(const std::shared_ptr<SymbolAnimationConfig>&)> animationFunc =
305 RSCanvasParagraphPainterTest::AnimationFunc;
306 canvasParagraphPainter_->SetAnimation(animationFunc);
307 ASSERT_NE(canvasParagraphPainter_->animationFunc_, nullptr);
308 }
309
310 /*
311 * @tc.name: RSCanvasParagraphPainterTest016
312 * @tc.desc: test for drawTextBlob
313 * @tc.type: FUNC
314 */
315 HWTEST_F(RSCanvasParagraphPainterTest, RSCanvasParagraphPainterTest016, TestSize.Level0)
316 {
317 std::string str("TextBlob");
318 Font font;
319 std::shared_ptr<TextBlob> textBlob = TextBlob::MakeFromText(str.c_str(), str.length(), font);
320 ASSERT_NE(textBlob, nullptr);
321 ParagraphPainter::PaintID paintID = static_cast<int>(paintRecordsWithoutPen_.size()) - 1;
322 ParagraphPainter::SkPaintOrID paint;
323 paint.emplace<ParagraphPainter::PaintID>(paintID);
324 canvasParagraphPainterWithoutPen_->drawTextBlob(textBlob, 0.0, 0.0, paint);
325 }
326
327 /*
328 * @tc.name: RSCanvasParagraphPainterTest017
329 * @tc.desc: test for drawTextBlob
330 * @tc.type: FUNC
331 */
332 HWTEST_F(RSCanvasParagraphPainterTest, RSCanvasParagraphPainterTest017, TestSize.Level0)
333 {
334 std::string str("TextBlob");
335 Font font;
336 std::shared_ptr<TextBlob> textBlob = TextBlob::MakeFromText(str.c_str(), str.length(), font);
337 ASSERT_NE(textBlob, nullptr);
338 ParagraphPainter::PaintID paintID = static_cast<int>(paintRecordsWithoutBrush_.size()) - 1;
339 ParagraphPainter::SkPaintOrID paint;
340 paint.emplace<ParagraphPainter::PaintID>(paintID);
341 canvasParagraphPainterWithoutBrush_->drawTextBlob(textBlob, 0.0, 0.0, paint);
342 }
343
344 /*
345 * @tc.name: RSCanvasParagraphPainterTest018
346 * @tc.desc: test for drawTextBlob
347 * @tc.type: FUNC
348 */
349 HWTEST_F(RSCanvasParagraphPainterTest, RSCanvasParagraphPainterTest018, TestSize.Level0)
350 {
351 std::string str("TextBlob");
352 Font font;
353 std::shared_ptr<TextBlob> textBlob = TextBlob::MakeFromText(str.c_str(), str.length(), font);
354 ASSERT_NE(textBlob, nullptr);
355 ParagraphPainter::PaintID paintID = static_cast<int>(paintRecordsWithoutPenBrush_.size()) - 1;
356 ParagraphPainter::SkPaintOrID paint;
357 paint.emplace<ParagraphPainter::PaintID>(paintID);
358 canvasParagraphPainterWithoutPenBrush_->drawTextBlob(textBlob, 0.0, 0.0, paint);
359 }
360
361 /*
362 * @tc.name: RSCanvasParagraphPainterTest019
363 * @tc.desc: test for drawTextBlob
364 * @tc.type: FUNC
365 */
366 HWTEST_F(RSCanvasParagraphPainterTest, RSCanvasParagraphPainterTest019, TestSize.Level0)
367 {
368 std::string str(u8"\U0001F60A");
369 Font font;
370 std::shared_ptr<TextBlob> textBlob = TextBlob::MakeFromText(str.c_str(), str.length(), font);
371 ASSERT_NE(textBlob, nullptr);
372 ParagraphPainter::PaintID paintID = static_cast<int>(paintRecordsWithoutPenBrush_.size()) - 1;
373 ParagraphPainter::SkPaintOrID paint;
374 paint.emplace<ParagraphPainter::PaintID>(paintID);
375 canvasParagraphPainterWithoutPenBrush_->drawTextBlob(textBlob, 0.0, 0.0, paint);
376 }
377
378 /*
379 * @tc.name: RSCanvasParagraphPainterTest020
380 * @tc.desc: test for DrawSymbolSkiaTxt
381 * @tc.type: FUNC
382 */
383 HWTEST_F(RSCanvasParagraphPainterTest, RSCanvasParagraphPainterTest020, TestSize.Level0)
384 {
385 Point offset;
386 std::string str("DrawSymbolSkiaTxt");
387 Font font;
388 std::shared_ptr<TextBlob> textBlob = TextBlob::MakeFromText(str.c_str(), str.length(), font);
389 ASSERT_NE(textBlob, nullptr);
390 canvasParagraphPainter_->DrawSymbolSkiaTxt(textBlob, offset, paintRecord_);
391 }
392
393 /*
394 * @tc.name: RSCanvasParagraphPainterTest021
395 * @tc.desc: test for DrawSymbolSkiaTxt
396 * @tc.type: FUNC
397 */
398 HWTEST_F(RSCanvasParagraphPainterTest, RSCanvasParagraphPainterTest021, TestSize.Level0)
399 {
400 Point offset;
401 std::string str("DrawSymbolSkiaTxt");
402 Font font;
403 std::shared_ptr<TextBlob> textBlob = TextBlob::MakeFromText(str.c_str(), str.length(), font);
404 ASSERT_NE(textBlob, nullptr);
405 canvasParagraphPainterWithoutPen_->DrawSymbolSkiaTxt(textBlob, offset, paintRecordWithoutPen_);
406 }
407
408 /*
409 * @tc.name: RSCanvasParagraphPainterTest022
410 * @tc.desc: test for DrawSymbolSkiaTxt
411 * @tc.type: FUNC
412 */
413 HWTEST_F(RSCanvasParagraphPainterTest, RSCanvasParagraphPainterTest022, TestSize.Level0)
414 {
415 Point offset;
416 std::string str("DrawSymbolSkiaTxt");
417 Font font;
418 std::shared_ptr<TextBlob> textBlob = TextBlob::MakeFromText(str.c_str(), str.length(), font);
419 ASSERT_NE(textBlob, nullptr);
420 canvasParagraphPainterWithoutBrush_->DrawSymbolSkiaTxt(textBlob, offset, paintRecordWithoutBrush_);
421 }
422
423 /*
424 * @tc.name: RSCanvasParagraphPainterTest023
425 * @tc.desc: test for drawRect
426 * @tc.type: FUNC
427 */
428 HWTEST_F(RSCanvasParagraphPainterTest, RSCanvasParagraphPainterTest023, TestSize.Level0)
429 {
430 ParagraphPainter::PaintID paintID = static_cast<int>(paintRecordsWithoutPen_.size()) - 1;
431 ASSERT_GE(paintID, 0);
432 ParagraphPainter::SkPaintOrID paint;
433 paint.emplace<ParagraphPainter::PaintID>(paintID);
434 SkRect rect = SkRect::MakeEmpty();
435 canvasParagraphPainterWithoutPen_->drawRect(rect, paint);
436 }
437
438 /*
439 * @tc.name: RSCanvasParagraphPainterTest024
440 * @tc.desc: test for drawRect
441 * @tc.type: FUNC
442 */
443 HWTEST_F(RSCanvasParagraphPainterTest, RSCanvasParagraphPainterTest024, TestSize.Level0)
444 {
445 ParagraphPainter::PaintID paintID = static_cast<int>(paintRecordsWithoutBrush_.size()) - 1;
446 ASSERT_GE(paintID, 0);
447 ParagraphPainter::SkPaintOrID paint;
448 paint.emplace<ParagraphPainter::PaintID>(paintID);
449 SkRect rect = SkRect::MakeEmpty();
450 canvasParagraphPainterWithoutBrush_->drawRect(rect, paint);
451 }
452
453 /*
454 * @tc.name: RSCanvasParagraphPainterTest025
455 * @tc.desc: test for drawRect
456 * @tc.type: FUNC
457 */
458 HWTEST_F(RSCanvasParagraphPainterTest, RSCanvasParagraphPainterTest025, TestSize.Level0)
459 {
460 ParagraphPainter::PaintID paintID = static_cast<int>(paintRecordsWithoutPenBrush_.size()) - 1;
461 ASSERT_GE(paintID, 0);
462 ParagraphPainter::SkPaintOrID paint;
463 paint.emplace<ParagraphPainter::PaintID>(paintID);
464 SkRect rect = SkRect::MakeEmpty();
465 canvasParagraphPainterWithoutPenBrush_->drawRect(rect, paint);
466 }
467
468 } // namespace txt