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 #include "test/mock/base/mock_task_executor.h"
16 #include "test/mock/core/common/mock_clipboard.h"
17 #include "test/unittest/core/pattern/rich_editor/rich_editor_common_test_ng.h"
18
19 using namespace testing;
20 using namespace testing::ext;
21
22 namespace OHOS::Ace::NG {
23 namespace {
24 constexpr uint32_t RECORD_MAX_LENGTH = 20;
25 const std::string TEST_INSERT_LINE_SPACE = " ";
26 } // namespace
27
28 class RichEditorPatternTestThreeNg : public RichEditorCommonTestNg {
29 public:
30 void SetUp() override;
31 void TearDown() override;
32 static void TearDownTestSuite();
33 RefPtr<RichEditorPattern> GetRichEditorPattern();
34 };
35
SetUp()36 void RichEditorPatternTestThreeNg::SetUp()
37 {
38 MockPipelineContext::SetUp();
39 MockContainer::SetUp();
40 MockContainer::Current()->taskExecutor_ = AceType::MakeRefPtr<MockTaskExecutor>();
41 auto* stack = ViewStackProcessor::GetInstance();
42 auto nodeId = stack->ClaimNodeId();
43 richEditorNode_ = FrameNode::GetOrCreateFrameNode(
44 V2::RICH_EDITOR_ETS_TAG, nodeId, []() { return AceType::MakeRefPtr<RichEditorPattern>(); });
45 ASSERT_NE(richEditorNode_, nullptr);
46 auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
47 richEditorPattern->SetRichEditorController(AceType::MakeRefPtr<RichEditorController>());
48 richEditorPattern->GetRichEditorController()->SetPattern(AceType::WeakClaim(AceType::RawPtr(richEditorPattern)));
49 richEditorPattern->CreateNodePaintMethod();
50 richEditorNode_->GetGeometryNode()->SetContentSize({});
51 }
52
TearDown()53 void RichEditorPatternTestThreeNg::TearDown()
54 {
55 richEditorNode_ = nullptr;
56 MockParagraph::TearDown();
57 }
58
TearDownTestSuite()59 void RichEditorPatternTestThreeNg::TearDownTestSuite()
60 {
61 TestNG::TearDownTestSuite();
62 }
63
GetRichEditorPattern()64 RefPtr<RichEditorPattern> RichEditorPatternTestThreeNg::GetRichEditorPattern()
65 {
66 CHECK_NULL_RETURN(richEditorNode_, nullptr);
67 return richEditorNode_->GetPattern<RichEditorPattern>();
68 }
69
70 /**
71 * @tc.name: SetInputMethodStatus001
72 * @tc.desc: test SetInputMethodStatus
73 * @tc.type: FUNC
74 */
75 HWTEST_F(RichEditorPatternTestThreeNg, SetInputMethodStatus001, TestSize.Level1)
76 {
77 auto richEditorPattern = GetRichEditorPattern();
78 ASSERT_NE(richEditorPattern, nullptr);
79 #if defined(OHOS_STANDARD_SYSTEM) && !defined(PREVIEW)
80 richEditorPattern->SetInputMethodStatus(true);
81 EXPECT_TRUE(richEditorPattern->imeShown_);
82 richEditorPattern->SetInputMethodStatus(false);
83 EXPECT_FALSE(richEditorPattern->imeShown_);
84 #endif
85 }
86
87 /**
88 * @tc.name: CursorMoveRight001
89 * @tc.desc: test CursorMoveRight
90 * @tc.type: FUNC
91 */
92 HWTEST_F(RichEditorPatternTestThreeNg, CursorMoveRight001, TestSize.Level1)
93 {
94 auto richEditorPattern = GetRichEditorPattern();
95 ASSERT_NE(richEditorPattern, nullptr);
96 AddSpan(INIT_VALUE_1);
97 richEditorPattern->CursorMoveRight();
98 EXPECT_TRUE(richEditorPattern->caretVisible_);
99 }
100
101 /**
102 * @tc.name: CursorMoveLeftAndRightWord001
103 * @tc.desc: test CursorMoveLeftWord and CursorMoveRightWord
104 * @tc.type: FUNC
105 */
106 HWTEST_F(RichEditorPatternTestThreeNg, CursorMoveLeftWord001, TestSize.Level2)
107 {
108 auto richEditorPattern = GetRichEditorPattern();
109 ASSERT_NE(richEditorPattern, nullptr);
110 EXPECT_FALSE(richEditorPattern->CursorMoveLeftWord());
111 EXPECT_FALSE(richEditorPattern->CursorMoveRightWord());
112 }
113
114 /**
115 * @tc.name: CursorMoveToParagraphBegin001
116 * @tc.desc: test CursorMoveToParagraphBegin
117 * @tc.type: FUNC
118 */
119 HWTEST_F(RichEditorPatternTestThreeNg, CursorMoveToParagraphBegin001, TestSize.Level2)
120 {
121 auto richEditorPattern = GetRichEditorPattern();
122 ASSERT_NE(richEditorPattern, nullptr);
123 EXPECT_FALSE(richEditorPattern->CursorMoveToParagraphBegin());
124 AddSpan(TEST_INSERT_LINE_SEP);
125 richEditorPattern->caretPosition_ = 1;
126 EXPECT_TRUE(richEditorPattern->CursorMoveToParagraphBegin());
127 }
128
129 /**
130 * @tc.name: CursorMoveEnd001
131 * @tc.desc: test CursorMoveToParagraphBegin
132 * @tc.type: FUNC
133 */
134 HWTEST_F(RichEditorPatternTestThreeNg, CursorMoveEnd001, TestSize.Level2)
135 {
136 auto richEditorPattern = GetRichEditorPattern();
137 ASSERT_NE(richEditorPattern, nullptr);
138 EXPECT_FALSE(richEditorPattern->CursorMoveToParagraphBegin());
139 }
140
141 /**
142 * @tc.name: GetLeftWordPosition001
143 * @tc.desc: test GetLeftWordPosition
144 * @tc.type: FUNC
145 */
146 HWTEST_F(RichEditorPatternTestThreeNg, GetLeftWordPosition001, TestSize.Level2)
147 {
148 auto richEditorPattern = GetRichEditorPattern();
149 ASSERT_NE(richEditorPattern, nullptr);
150 EXPECT_EQ(richEditorPattern->GetLeftWordPosition(richEditorPattern->caretPosition_), 0);
151 AddSpan(INIT_VALUE_1 + TEST_INSERT_LINE_SPACE);
152 EXPECT_EQ(richEditorPattern->GetLeftWordPosition(richEditorPattern->GetTextContentLength()), 0);
153 }
154
155 /**
156 * @tc.name: GetRightWordPosition001
157 * @tc.desc: test GetRightWordPosition
158 * @tc.type: FUNC
159 */
160 HWTEST_F(RichEditorPatternTestThreeNg, GetRightWordPosition001, TestSize.Level2)
161 {
162 auto richEditorPattern = GetRichEditorPattern();
163 ASSERT_NE(richEditorPattern, nullptr);
164 EXPECT_EQ(richEditorPattern->GetLeftWordPosition(richEditorPattern->caretPosition_), 0);
165 AddSpan(INIT_VALUE_1 + TEST_INSERT_LINE_SPACE);
166 EXPECT_EQ(richEditorPattern->GetRightWordPosition(richEditorPattern->GetTextContentLength()),
167 richEditorPattern->GetTextContentLength());
168 }
169
170 /**
171 * @tc.name: HandleSelect001
172 * @tc.desc: test HandleSelect
173 * @tc.type: FUNC
174 */
175 HWTEST_F(RichEditorPatternTestThreeNg, HandleSelect001, TestSize.Level2)
176 {
177 auto richEditorPattern = GetRichEditorPattern();
178 ASSERT_NE(richEditorPattern, nullptr);
179 EXPECT_EQ(richEditorPattern->GetLeftWordPosition(richEditorPattern->caretPosition_), 0);
180 AddSpan(INIT_VALUE_1 + TEST_INSERT_LINE_SPACE);
181 richEditorPattern->HandleSelect(CaretMoveIntent::Home);
182 EXPECT_EQ(richEditorPattern->textSelector_.GetTextStart(), richEditorPattern->textSelector_.GetStart());
183 }
184
185 /**
186 * @tc.name: HandleOnEscape001
187 * @tc.desc: test HandleOnEscape
188 * @tc.type: FUNC
189 */
190 HWTEST_F(RichEditorPatternTestThreeNg, HandleOnEscape001, TestSize.Level1)
191 {
192 auto richEditorPattern = GetRichEditorPattern();
193 ASSERT_NE(richEditorPattern, nullptr);
194 EXPECT_FALSE(richEditorPattern->HandleOnEscape());
195 }
196
197 /**
198 * @tc.name: HandleOnUndoAction001
199 * @tc.desc: test HandleOnUndoAction
200 * @tc.type: FUNC
201 */
202 HWTEST_F(RichEditorPatternTestThreeNg, HandleOnUndoAction001, TestSize.Level2)
203 {
204 auto richEditorPattern = GetRichEditorPattern();
205 ASSERT_NE(richEditorPattern, nullptr);
206 RichEditorPattern::OperationRecord firstRecord;
207 firstRecord.addText = "first Record helloWorld";
208 firstRecord.deleteText = "helloWorld";
209 richEditorPattern->operationRecords_.emplace_back(firstRecord);
210 richEditorPattern->redoOperationRecords_.clear();
211 for (uint32_t count = 0; count < RECORD_MAX_LENGTH; ++count) {
212 RichEditorPattern::OperationRecord emptyRecord;
213 richEditorPattern->redoOperationRecords_.emplace_back(emptyRecord);
214 }
215 richEditorPattern->HandleOnUndoAction();
216 EXPECT_TRUE(richEditorPattern->operationRecords_.empty());
217
218 RichEditorPattern::OperationRecord secondRecord;
219 secondRecord.addText = "second Record helloWorld";
220 secondRecord.deleteCaretPostion = 3;
221 richEditorPattern->operationRecords_.clear();
222 richEditorPattern->operationRecords_.emplace_back(secondRecord);
223 richEditorPattern->HandleOnUndoAction();
224 EXPECT_TRUE(richEditorPattern->operationRecords_.empty());
225 }
226
227 /**
228 * @tc.name: HandleOnRedoAction001
229 * @tc.desc: test HandleOnRedoAction
230 * @tc.type: FUNC
231 */
232 HWTEST_F(RichEditorPatternTestThreeNg, HandleOnRedoAction001, TestSize.Level2)
233 {
234 auto richEditorPattern = GetRichEditorPattern();
235 ASSERT_NE(richEditorPattern, nullptr);
236 richEditorPattern->HandleOnRedoAction();
237 RichEditorPattern::OperationRecord firstRecord;
238 firstRecord.addText = "first Record helloWorld";
239 firstRecord.deleteCaretPostion = 3;
240 richEditorPattern->redoOperationRecords_.emplace_back(firstRecord);
241 richEditorPattern->HandleOnRedoAction();
242 EXPECT_TRUE(richEditorPattern->redoOperationRecords_.empty());
243
244 RichEditorPattern::OperationRecord secondRecord;
245 secondRecord.addText = "second Record helloWorld";
246 secondRecord.deleteText = "helloWorld";
247 richEditorPattern->redoOperationRecords_.clear();
248 richEditorPattern->redoOperationRecords_.emplace_back(secondRecord);
249 richEditorPattern->HandleOnRedoAction();
250 EXPECT_TRUE(richEditorPattern->redoOperationRecords_.empty());
251
252 RichEditorPattern::OperationRecord thridRecord;
253 thridRecord.deleteText = "helloWorld";
254 thridRecord.beforeCaretPosition = 10;
255 thridRecord.afterCaretPosition = 15;
256 richEditorPattern->redoOperationRecords_.clear();
257 richEditorPattern->redoOperationRecords_.emplace_back(thridRecord);
258 richEditorPattern->HandleOnRedoAction();
259 EXPECT_TRUE(richEditorPattern->redoOperationRecords_.empty());
260 }
261
262 /**
263 * @tc.name: CalcDeleteValueObj001
264 * @tc.desc: test CalcDeleteValueObj delete unicode
265 * @tc.type: FUNC
266 */
267 HWTEST_F(RichEditorPatternTestThreeNg, CalcDeleteValueObj001, TestSize.Level1)
268 {
269 auto richEditorPattern = GetRichEditorPattern();
270 ASSERT_NE(richEditorPattern, nullptr);
271 AddSpan(INIT_VALUE_1);
272 auto spans = richEditorPattern->GetSpanItemChildren();
273 ASSERT_FALSE(spans.empty());
274 auto spanItem = spans.back();
275 ASSERT_NE(spanItem, nullptr);
276 spanItem->unicode = 1;
277
278 RichEditorDeleteValue info;
279 int32_t currentPosition = INIT_VALUE_1.size() - 2;
280 int32_t length = 2;
281 richEditorPattern->CalcDeleteValueObj(currentPosition, length, info);
282 EXPECT_EQ(info.GetLength(), 0);
283 }
284
285 /**
286 * @tc.name: CalcDeleteValueObj001
287 * @tc.desc: test CalcDeleteValueObj delete builder span
288 * @tc.type: FUNC
289 */
290 HWTEST_F(RichEditorPatternTestThreeNg, CalcDeleteValueObj002, TestSize.Level1)
291 {
292 auto richEditorPattern = GetRichEditorPattern();
293 ASSERT_NE(richEditorPattern, nullptr);
294 AddSpan(INIT_VALUE_1);
295 auto spans = richEditorPattern->GetSpanItemChildren();
296 ASSERT_FALSE(spans.empty());
297 auto spanItem = spans.back();
298 ASSERT_NE(spanItem, nullptr);
299 int32_t currentPosition = INIT_VALUE_1.size() - 2;
300 spanItem->placeholderIndex = currentPosition;
301
302 RichEditorDeleteValue info;
303 int32_t length = 2;
304 richEditorPattern->CalcDeleteValueObj(currentPosition, length, info);
305 EXPECT_EQ(info.GetRichEditorDeleteSpans().size(), 1);
306 }
307
308 /**
309 * @tc.name: GetSpanNodeBySpanItem001
310 * @tc.desc: test GetSpanNodeBySpanItem
311 * @tc.type: FUNC
312 */
313 HWTEST_F(RichEditorPatternTestThreeNg, GetSpanNodeBySpanItem001, TestSize.Level2)
314 {
315 auto richEditorPattern = GetRichEditorPattern();
316 ASSERT_NE(richEditorPattern, nullptr);
317 AddSpan(INIT_VALUE_1);
318 ASSERT_EQ(richEditorPattern->GetSpanNodeBySpanItem(nullptr), nullptr);
319 }
320
321 /**
322 * @tc.name: DeleteValueSetImageSpan001
323 * @tc.desc: test DeleteValueSetImageSpan
324 * @tc.type: FUNC
325 */
326 HWTEST_F(RichEditorPatternTestThreeNg, DeleteValueSetImageSpan001, TestSize.Level1)
327 {
328 AddImageSpan();
329 ASSERT_NE(richEditorNode_, nullptr);
330 auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
331 ASSERT_NE(richEditorPattern, nullptr);
332
333 auto imageNode = AceType::DynamicCast<FrameNode>(richEditorNode_->GetLastChild());
334 ASSERT_NE(imageNode, nullptr);
335 auto imageLayoutProperty = imageNode->GetLayoutProperty<ImageLayoutProperty>();
336 ASSERT_NE(imageLayoutProperty, nullptr);
337 imageLayoutProperty->UpdateImageFit(ImageFit::FILL);
338 imageLayoutProperty->UpdateVerticalAlign(VerticalAlign::CENTER);
339
340 RichEditorAbstractSpanResult spanResult;
341 spanResult.SetSpanIndex(richEditorNode_->GetChildIndexById(imageNode->GetId()));
342
343 auto spans = richEditorPattern->GetSpanItemChildren();
344 ASSERT_FALSE(spans.empty());
345 auto imageSpanItem = AceType::DynamicCast<ImageSpanItem>(spans.back());
346 ASSERT_NE(imageSpanItem, nullptr);
347 richEditorPattern->DeleteValueSetImageSpan(imageSpanItem, spanResult);
348 EXPECT_EQ(spanResult.GetObjectFit(), ImageFit::FILL);
349 EXPECT_EQ(spanResult.GetVerticalAlign(), VerticalAlign::CENTER);
350 }
351
352 /**
353 * @tc.name: HandleOnCopyStyledString001
354 * @tc.desc: test HandleOnCopyStyledString
355 * @tc.type: FUNC
356 */
357 HWTEST_F(RichEditorPatternTestThreeNg, HandleOnCopyStyledString001, TestSize.Level1)
358 {
359 auto richEditorPattern = GetRichEditorPattern();
360 ASSERT_NE(richEditorPattern, nullptr);
361 AddSpan(INIT_VALUE_1);
362
363 richEditorPattern->OnModifyDone();
364 richEditorPattern->SetSpanStringMode(true);
365 richEditorPattern->OnCopyOperation();
366 ASSERT_NE(richEditorPattern->GetClipboard(), nullptr);
367 }
368
369 /**
370 * @tc.name: OnCopyOperationExt001
371 * @tc.desc: test OnCopyOperationExt
372 * @tc.type: FUNC
373 */
374 HWTEST_F(RichEditorPatternTestThreeNg, OnCopyOperationExt001, TestSize.Level1)
375 {
376 auto richEditorPattern = GetRichEditorPattern();
377 ASSERT_NE(richEditorPattern, nullptr);
378 AddSpan(INIT_VALUE_1);
379
380 richEditorPattern->OnModifyDone();
381 auto clipboard = richEditorPattern->GetClipboard();
382 ASSERT_NE(clipboard, nullptr);
383 auto pasteDataMix = clipboard->CreatePasteDataMix();
384 richEditorPattern->OnCopyOperationExt(pasteDataMix);
385 }
386
387 /**
388 * @tc.name: ClearContent001
389 * @tc.desc: test ClearContent
390 * @tc.type: FUNC
391 */
392 HWTEST_F(RichEditorPatternTestThreeNg, ClearContent001, TestSize.Level1)
393 {
394 auto richEditorPattern = GetRichEditorPattern();
395 ASSERT_NE(richEditorPattern, nullptr);
396 auto placeholderSpanNode = PlaceholderSpanNode::GetOrCreateSpanNode(V2::PLACEHOLDER_SPAN_ETS_TAG,
__anon5311f5020302() 397 ElementRegister::GetInstance()->MakeUniqueId(), []() { return AceType::MakeRefPtr<PlaceholderSpanPattern>(); });
398 auto placeholderSpanItem = placeholderSpanNode->GetSpanItem();
399 ASSERT_NE(placeholderSpanItem, nullptr);
400 placeholderSpanItem->content = " ";
401 placeholderSpanNode->MountToParent(richEditorNode_);
402 richEditorPattern->ClearContent(placeholderSpanNode);
403 EXPECT_TRUE(placeholderSpanItem->content.empty());
404 }
405
406 /**
407 * @tc.name: UpdateTextFieldManager001
408 * @tc.desc: test UpdateTextFieldManager
409 * @tc.type: FUNC
410 */
411 HWTEST_F(RichEditorPatternTestThreeNg, UpdateTextFieldManager001, TestSize.Level1)
412 {
413 auto richEditorPattern = GetRichEditorPattern();
414 ASSERT_NE(richEditorPattern, nullptr);
415 auto context = PipelineContext::GetCurrentContext();
416 ASSERT_NE(context, nullptr);
417 context->taskExecutor_ = AceType::MakeRefPtr<MockTaskExecutor>();
418 auto taskExecutor = context->GetTaskExecutor();
419 ASSERT_NE(taskExecutor, nullptr);
420 auto textFieldManager = AceType::MakeRefPtr<TextFieldManagerNG>();
421 textFieldManager->SetHeight(20);
422 context->SetTextFieldManager(textFieldManager);
423
424 auto theme = AceType::MakeRefPtr<MockThemeManager>();
425 context->SetThemeManager(theme);
426 EXPECT_CALL(*theme, GetTheme(_)).WillRepeatedly(Return(AceType::MakeRefPtr<RichEditorTheme>()));
427 auto focusHub = richEditorPattern->GetFocusHub();
428 ASSERT_NE(focusHub, nullptr);
429 focusHub->currentFocus_ = true;
430 Offset Offset = { 1, 4 };
431 richEditorPattern->isTextChange_ = true;
432 richEditorPattern->UpdateTextFieldManager(Offset, 1.0f);
433 EXPECT_NE(textFieldManager->GetOnFocusTextField().Upgrade(), nullptr);
434 }
435
436 /**
437 * @tc.name: OnDirtyLayoutWrapperSwap001
438 * @tc.desc: test OnDirtyLayoutWrapperSwap
439 * @tc.type: FUNC
440 */
441 HWTEST_F(RichEditorPatternTestThreeNg, OnDirtyLayoutWrapperSwap001, TestSize.Level1)
442 {
443 auto richEditorPattern = GetRichEditorPattern();
444 ASSERT_NE(richEditorPattern, nullptr);
445 auto rendenContext = richEditorNode_->GetRenderContext();
446 ASSERT_NE(rendenContext, nullptr);
447 rendenContext->UpdateClipEdge(false);
448 auto geometryNode = richEditorNode_->GetGeometryNode();
449 ASSERT_NE(geometryNode, nullptr);
450 auto globalOffset = OffsetF(15.0f, 3.0f);
451 geometryNode->SetFrameSize(SizeF(20.0f, 5.0f));
452 geometryNode->SetFrameOffset(globalOffset);
453
454 auto layoutWrapper = AceType::MakeRefPtr<LayoutWrapperNode>(
455 richEditorNode_, AceType::MakeRefPtr<GeometryNode>(), richEditorNode_->GetLayoutProperty());
456 ASSERT_NE(layoutWrapper, nullptr);
457 auto layoutAlgorithm = AceType::DynamicCast<RichEditorLayoutAlgorithm>(richEditorPattern->CreateLayoutAlgorithm());
458 ASSERT_NE(layoutAlgorithm, nullptr);
459 layoutAlgorithm->parentGlobalOffset_ = globalOffset;
460 layoutWrapper->SetLayoutAlgorithm(AceType::MakeRefPtr<LayoutAlgorithmWrapper>(layoutAlgorithm));
461 DirtySwapConfig config;
462 richEditorPattern->baselineOffset_ = 6.0f;
463 richEditorPattern->CreateNodePaintMethod();
464 auto ret = richEditorPattern->OnDirtyLayoutWrapperSwap(layoutWrapper, config);
465 EXPECT_FALSE(ret);
466 }
467
468 /**
469 * @tc.name: HandleUserGestureEvent001
470 * @tc.desc: test HandleUserGestureEvent
471 * @tc.type: FUNC
472 */
473 HWTEST_F(RichEditorPatternTestThreeNg, HandleUserGestureEvent001, TestSize.Level1)
474 {
475 auto richEditorPattern = GetRichEditorPattern();
476 ASSERT_NE(richEditorPattern, nullptr);
477 AddSpan(EXCEPT_VALUE);
478 ASSERT_FALSE(richEditorPattern->spans_.empty());
479 auto firstSpanItem = richEditorPattern->spans_.front();
480 ASSERT_NE(firstSpanItem, nullptr);
481 firstSpanItem->leadingMargin = std::make_optional<NG::LeadingMargin>();
482 auto paragraph = MockParagraph::GetOrCreateMockParagraph();
483 ASSERT_NE(paragraph, nullptr);
484 richEditorPattern->paragraphs_.AddParagraph({ .paragraph = paragraph, .start = 0, .end = 10 });
485
486 std::vector<RectF> rects { RectF(0, 0, 5, 5) };
487 EXPECT_CALL(*paragraph, GetRectsForRange(_, _, _)).WillRepeatedly(SetArgReferee<THIRD_PARAM>(rects));
488 EXPECT_CALL(*paragraph, GetHeight).WillRepeatedly(Return(50));
489 GestureEvent info;
490 info.SetLocalLocation(Offset(3, 3));
491 richEditorPattern->contentRect_ = RectF(0, 0, 20.0, 20.0);
__anon5311f5020402(RefPtr<SpanItem> item, GestureEvent& info) 492 auto gestureFunc = [](RefPtr<SpanItem> item, GestureEvent& info) -> bool { return true; };
493 EXPECT_TRUE(richEditorPattern->HandleUserGestureEvent(info, std::move(gestureFunc)));
494 }
495
496 /**
497 * @tc.name: HandleTouchEvent001
498 * @tc.desc: test HandleTouchEvent
499 * @tc.type: FUNC
500 */
501 HWTEST_F(RichEditorPatternTestThreeNg, HandleTouchEvent001, TestSize.Level1)
502 {
503 auto richEditorPattern = GetRichEditorPattern();
504 ASSERT_NE(richEditorPattern, nullptr);
505 TouchEventInfo touchEventInfo("");
506 TouchLocationInfo touchLocationInfo(0);
507 touchLocationInfo.touchType_ = TouchType::DOWN;
508 touchLocationInfo.localLocation_ = Offset(0.0f, 0.0f);
509 touchEventInfo.AddTouchLocationInfo(std::move(touchLocationInfo));
510 richEditorPattern->HandleTouchEvent(touchEventInfo);
511 auto touchInfo = touchEventInfo.GetTouches().front();
512 auto touchType = touchInfo.GetTouchType();
513 EXPECT_EQ(touchType, TouchType::DOWN);
514 }
515
516 /**
517 * @tc.name: HandleTouchEvent002
518 * @tc.desc: test HandleTouchEvent
519 * @tc.type: FUNC
520 */
521 HWTEST_F(RichEditorPatternTestThreeNg, HandleTouchEvent002, TestSize.Level1)
522 {
523 auto richEditorPattern = GetRichEditorPattern();
524 ASSERT_NE(richEditorPattern, nullptr);
525 TouchEventInfo touchEventInfo("");
526 TouchLocationInfo touchLocationInfo(0);
527 touchLocationInfo.touchType_ = TouchType::UP;
528 touchLocationInfo.localLocation_ = Offset(0.0f, 0.0f);
529 touchEventInfo.AddTouchLocationInfo(std::move(touchLocationInfo));
530 richEditorPattern->HandleTouchEvent(touchEventInfo);
531 auto touchInfo = touchEventInfo.GetTouches().front();
532 auto touchType = touchInfo.GetTouchType();
533 EXPECT_EQ(touchType, TouchType::UP);
534 }
535
536 /**
537 * @tc.name: HandleTouchEvent003
538 * @tc.desc: test HandleTouchEvent
539 * @tc.type: FUNC
540 */
541 HWTEST_F(RichEditorPatternTestThreeNg, HandleTouchEvent003, TestSize.Level1)
542 {
543 auto richEditorPattern = GetRichEditorPattern();
544 ASSERT_NE(richEditorPattern, nullptr);
545 TouchEventInfo touchEventInfo("");
546 TouchLocationInfo touchLocationInfo(0);
547 touchLocationInfo.touchType_ = TouchType::MOVE;
548 touchLocationInfo.localLocation_ = Offset(0.0f, 0.0f);
549 touchEventInfo.AddTouchLocationInfo(std::move(touchLocationInfo));
550 richEditorPattern->HandleTouchEvent(touchEventInfo);
551 auto touchInfo = touchEventInfo.GetTouches().front();
552 auto touchType = touchInfo.GetTouchType();
553 EXPECT_EQ(touchType, TouchType::MOVE);
554 }
555
556 /**
557 * @tc.name: HandleTouchEvent004
558 * @tc.desc: test HandleTouchEvent
559 * @tc.type: FUNC
560 */
561 HWTEST_F(RichEditorPatternTestThreeNg, HandleTouchEvent004, TestSize.Level1)
562 {
563 auto richEditorPattern = GetRichEditorPattern();
564 ASSERT_NE(richEditorPattern, nullptr);
565 TouchEventInfo touchEventInfo("");
566 TouchLocationInfo touchLocationInfo(0);
567 touchLocationInfo.touchType_ = TouchType::UNKNOWN;
568 touchLocationInfo.localLocation_ = Offset(0.0f, 0.0f);
569 touchEventInfo.AddTouchLocationInfo(std::move(touchLocationInfo));
570 richEditorPattern->HandleTouchEvent(touchEventInfo);
571 auto touchInfo = touchEventInfo.GetTouches().front();
572 auto touchType = touchInfo.GetTouchType();
573 EXPECT_EQ(touchType, TouchType::UNKNOWN);
574 }
575
576 /**
577 * @tc.name: GetRightWordPosition002
578 * @tc.desc: test GetRightWordPosition
579 * @tc.type: FUNC
580 */
581 HWTEST_F(RichEditorPatternTestThreeNg, GetRightWordPosition002, TestSize.Level1)
582 {
583 auto richEditorPattern = GetRichEditorPattern();
584 ASSERT_NE(richEditorPattern, nullptr);
585 std::string firstText = "text";
586 AddSpan(firstText);
587 std::string space = " ";
588 std::string secondText = "content";
589 AddSpan(space + secondText);
590 auto initCaretPosition = firstText.size();
591 EXPECT_EQ(richEditorPattern->GetRightWordPosition(initCaretPosition), initCaretPosition + space.size());
592 }
593
594 /**
595 * @tc.name: GetRightWordPosition003
596 * @tc.desc: test GetRightWordPosition
597 * @tc.type: FUNC
598 */
599 HWTEST_F(RichEditorPatternTestThreeNg, GetRightWordPosition003, TestSize.Level1)
600 {
601 auto richEditorPattern = GetRichEditorPattern();
602 ASSERT_NE(richEditorPattern, nullptr);
603 std::string firstText = "Text ";
604 AddSpan(firstText);
605 std::string secondText = "Content\n";
606 AddSpan(secondText);
607 AddSpan(INIT_VALUE_3);
608
609 auto initCaretPosition = firstText.size() + secondText.size() - 1;
610 EXPECT_EQ(richEditorPattern->GetRightWordPosition(initCaretPosition), initCaretPosition + 1);
611 }
612
613 /**
614 * @tc.name: InitScrollablePattern001
615 * @tc.desc: test InitScrollablePattern when update padding property
616 * @tc.type: FUNC
617 */
618 HWTEST_F(RichEditorPatternTestThreeNg, InitScrollablePattern001, TestSize.Level1)
619 {
620 auto richEditorPattern = GetRichEditorPattern();
621 ASSERT_NE(richEditorPattern, nullptr);
622 auto layoutProperty = richEditorPattern->GetLayoutProperty<RichEditorLayoutProperty>();
623 ASSERT_NE(layoutProperty, nullptr);
624 auto theme = AceType::MakeRefPtr<MockThemeManager>();
625 auto context = PipelineContext::GetCurrentContext();
626 ASSERT_NE(context, nullptr);
627 context->SetThemeManager(theme);
628 EXPECT_CALL(*theme, GetTheme(_)).WillRepeatedly(Return(AceType::MakeRefPtr<RichEditorTheme>()));
629 PaddingProperty padding;
630 padding.top = CalcLength(10);
631 padding.left = CalcLength(10);
632 padding.right = CalcLength(10);
633 padding.bottom = CalcLength(10);
634 layoutProperty->UpdatePadding(padding);
635 richEditorPattern->InitScrollablePattern();
636 EXPECT_TRUE(IsEqual(richEditorPattern->richTextRect_.GetOffset(), OffsetF(10.0f, 10.0f)));
637 }
638 } // namespace OHOS::Ace::NG