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/unittest/core/pattern/rich_editor/rich_editor_common_test_ng.h"
16 
17 using namespace testing;
18 using namespace testing::ext;
19 
20 namespace OHOS::Ace::NG {
21 namespace {
22 int32_t testOnReadyEvent = 0;
23 int32_t testAboutToIMEInput = 0;
24 int32_t testOnIMEInputComplete = 0;
25 int32_t testAboutToDelete = 0;
26 int32_t testOnDeleteComplete = 0;
27 int32_t testSpanNodeId = 1;
28 int32_t testFrameNodeId = 1;
29 int32_t testNumber0 = 0;
30 int32_t testNumber1 = 1;
31 int32_t testNumber2 = 2;
32 int32_t testNumber3 = 3;
33 int32_t testNumber4 = 4;
34 int32_t testNumber5 = 5;
35 } // namespace
36 
37 class RichEditorPatternTestNg : public RichEditorCommonTestNg {
38 public:
39     void SetUp() override;
40     void TearDown() override;
41     static void TearDownTestSuite();
42 };
43 
SetUp()44 void RichEditorPatternTestNg::SetUp()
45 {
46     MockPipelineContext::SetUp();
47     MockContainer::SetUp();
48     MockContainer::Current()->taskExecutor_ = AceType::MakeRefPtr<MockTaskExecutor>();
49     auto* stack = ViewStackProcessor::GetInstance();
50     auto nodeId = stack->ClaimNodeId();
51     richEditorNode_ = FrameNode::GetOrCreateFrameNode(
52         V2::RICH_EDITOR_ETS_TAG, nodeId, []() { return AceType::MakeRefPtr<RichEditorPattern>(); });
53     ASSERT_NE(richEditorNode_, nullptr);
54     auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
55     richEditorPattern->InitScrollablePattern();
56     richEditorPattern->SetRichEditorController(AceType::MakeRefPtr<RichEditorController>());
57     richEditorPattern->GetRichEditorController()->SetPattern(AceType::WeakClaim(AceType::RawPtr(richEditorPattern)));
58     richEditorPattern->CreateNodePaintMethod();
59     richEditorNode_->GetGeometryNode()->SetContentSize({});
60 }
61 
TearDown()62 void RichEditorPatternTestNg::TearDown()
63 {
64     richEditorNode_ = nullptr;
65     testOnReadyEvent = 0;
66     testAboutToIMEInput = 0;
67     testOnIMEInputComplete = 0;
68     testAboutToDelete = 0;
69     testOnDeleteComplete = 0;
70     MockParagraph::TearDown();
71 }
72 
TearDownTestSuite()73 void RichEditorPatternTestNg::TearDownTestSuite()
74 {
75     TestNG::TearDownTestSuite();
76 }
77 
78 /**
79  * @tc.name: RichEditorPatternTestClearDragDropEvent001
80  * @tc.desc: test RichEditorPattern
81  * @tc.type: FUNC
82  */
83 HWTEST_F(RichEditorPatternTestNg, RichEditorPatternTestClearDragDropEvent001, TestSize.Level1)
84 {
85     ASSERT_NE(richEditorNode_, nullptr);
86     auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
87     ASSERT_NE(richEditorPattern, nullptr);
88 
89     richEditorPattern->ClearDragDropEvent();
90 
91     auto host = richEditorPattern->GetHost();
92     ASSERT_NE(host, nullptr);
93     auto eventHub = host->GetEventHub<EventHub>();
94     ASSERT_NE(eventHub, nullptr);
95     ASSERT_EQ(eventHub->onDragStart_, nullptr);
96 }
97 
98 /**
99  * @tc.name: RichEditorPatternTestOnDragMove001
100  * @tc.desc: test OnDragMove
101  * @tc.type: FUNC
102  */
103 HWTEST_F(RichEditorPatternTestNg, RichEditorPatternTestOnDragMove001, TestSize.Level1)
104 {
105     ASSERT_NE(richEditorNode_, nullptr);
106     auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
107     ASSERT_NE(richEditorPattern, nullptr);
108     auto themeManager = AceType::MakeRefPtr<MockThemeManager>();
109     ASSERT_NE(themeManager, nullptr);
110     EXPECT_CALL(*themeManager, GetTheme(_)).WillRepeatedly(Return(AceType::MakeRefPtr<RichEditorTheme>()));
111     auto event = AceType::MakeRefPtr<OHOS::Ace::DragEvent>();
112 
113     auto oldThemeManager = PipelineBase::GetCurrentContext()->themeManager_;
114     PipelineBase::GetCurrentContext()->themeManager_ = themeManager;
115 
116     auto overlayMod = richEditorPattern->overlayMod_;
117     richEditorPattern->overlayMod_ = nullptr;
118     richEditorPattern->OnDragMove(event);
119     richEditorPattern->overlayMod_ = overlayMod;
120 
121     auto isShowPlaceholder = richEditorPattern->isShowPlaceholder_;
122     richEditorPattern->isShowPlaceholder_ = !isShowPlaceholder;
123     richEditorPattern->OnDragMove(event);
124     richEditorPattern->isShowPlaceholder_ = isShowPlaceholder;
125 
126     richEditorPattern->prevAutoScrollOffset_.SetX(testNumber1);
127     richEditorPattern->prevAutoScrollOffset_.SetY(testNumber1);
128     richEditorPattern->richTextRect_.SetRect(testNumber0, testNumber0, testNumber5, testNumber5);
129 
130     event->SetX(testNumber3);
131     event->SetY(testNumber3);
132     richEditorPattern->OnDragMove(event);
133     EXPECT_EQ(richEditorPattern->prevAutoScrollOffset_.GetX(), testNumber3);
134 
135     event->SetX(testNumber4);
136     event->SetY(testNumber4);
137     richEditorPattern->OnDragMove(event);
138     EXPECT_EQ(richEditorPattern->prevAutoScrollOffset_.GetX(), testNumber4);
139 
140     PipelineBase::GetCurrentContext()->themeManager_ = oldThemeManager;
141 }
142 
143 /**
144  * @tc.name: RichEditorPatternTestOnDragEnd001
145  * @tc.desc: test OnDragEnd
146  * @tc.type: FUNC
147  */
148 HWTEST_F(RichEditorPatternTestNg, RichEditorPatternTestOnDragEnd001, TestSize.Level1)
149 {
150     ASSERT_NE(richEditorNode_, nullptr);
151     auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
152     ASSERT_NE(richEditorPattern, nullptr);
153 
154     bool isTestAddObject = false;
155     ResultObject resultObject;
156     if (richEditorPattern->recoverDragResultObjects_.empty()) {
157         isTestAddObject = true;
158         richEditorPattern->recoverDragResultObjects_.emplace_back(resultObject);
159     }
160 
161     RefPtr<Ace::DragEvent> event = nullptr;
162     richEditorPattern->showSelect_ = false;
163     richEditorPattern->OnDragEnd(event);
164     ASSERT_EQ(richEditorPattern->showSelect_, false);
165 
166     event = AceType::MakeRefPtr<Ace::DragEvent>();
167     event->SetResult(DragRet::DRAG_SUCCESS);
168     richEditorPattern->showSelect_ = false;
169     richEditorPattern->OnDragEnd(event);
170     ASSERT_EQ(richEditorPattern->showSelect_, false);
171 
172     if (isTestAddObject) {
173         richEditorPattern->recoverDragResultObjects_.clear();
174     }
175 }
176 
177 /**
178  * @tc.name: RichEditorPatternTestResetDragSpanItems001
179  * @tc.desc: test ResetDragSpanItems
180  * @tc.type: FUNC
181  */
182 HWTEST_F(RichEditorPatternTestNg, RichEditorPatternTestResetDragSpanItems001, TestSize.Level1)
183 {
184     ASSERT_NE(richEditorNode_, nullptr);
185     auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
186     ASSERT_NE(richEditorPattern, nullptr);
187 
188     richEditorPattern->dragSpanItems_.clear();
189     richEditorPattern->ResetDragSpanItems();
190 
191     auto firstItem = AceType::MakeRefPtr<ImageSpanItem>();
192     firstItem->imageNodeId = testFrameNodeId;
193     richEditorPattern->dragSpanItems_.emplace_back(firstItem);
194     richEditorPattern->ResetDragSpanItems();
195     ASSERT_EQ(richEditorPattern->dragSpanItems_.size(), 0);
196 
197     auto secondItem = AceType::MakeRefPtr<PlaceholderSpanItem>();
198     secondItem->placeholderSpanNodeId = testFrameNodeId;
199     richEditorPattern->dragSpanItems_.emplace_back(secondItem);
200     richEditorPattern->ResetDragSpanItems();
201     ASSERT_EQ(richEditorPattern->dragSpanItems_.size(), 0);
202 
203     auto host = richEditorPattern->GetHost();
204     ASSERT_NE(host, nullptr);
205     auto childFrameNode = FrameNode::CreateFrameNode(V2::BLANK_ETS_TAG, testFrameNodeId, richEditorPattern);
206     ASSERT_NE(childFrameNode, nullptr);
207     host->children_.emplace_back(childFrameNode);
208     auto thirdItem = AceType::MakeRefPtr<PlaceholderSpanItem>();
209     thirdItem->placeholderSpanNodeId = testFrameNodeId;
210     richEditorPattern->dragSpanItems_.clear();
211     richEditorPattern->dragSpanItems_.emplace_back(thirdItem);
212     richEditorPattern->ResetDragSpanItems();
213     ASSERT_EQ(richEditorPattern->dragSpanItems_.size(), 0);
214 }
215 
216 /**
217  * @tc.name: RichEditorPatternTestRequestKeyboard001
218  * @tc.desc: test RequestKeyboard
219  * @tc.type: FUNC
220  */
221 HWTEST_F(RichEditorPatternTestNg, RichEditorPatternTestRequestKeyboard001, TestSize.Level1)
222 {
223     ASSERT_NE(richEditorNode_, nullptr);
224     auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
225     ASSERT_NE(richEditorPattern, nullptr);
226 
__anonfca374aa0302null227     auto func = [] {};
228 
229     auto customKeyboardBuilder = richEditorPattern->customKeyboardBuilder_;
230     richEditorPattern->customKeyboardBuilder_ = func;
231     ASSERT_EQ(richEditorPattern->RequestKeyboard(true, true, true), true);
232     richEditorPattern->customKeyboardBuilder_ = customKeyboardBuilder;
233 }
234 
235 /**
236  * @tc.name: RichEditorPatternTestInitMouseEvent001
237  * @tc.desc: test InitMouseEvent
238  * @tc.type: FUNC
239  */
240 HWTEST_F(RichEditorPatternTestNg, RichEditorPatternTestInitMouseEvent001, TestSize.Level1)
241 {
242     ASSERT_NE(richEditorNode_, nullptr);
243     auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
244     ASSERT_NE(richEditorPattern, nullptr);
245 
246     richEditorPattern->InitMouseEvent();
247 
248     auto host = richEditorPattern->GetHost();
249     ASSERT_NE(host, nullptr);
250     auto eventHub = host->GetEventHub<EventHub>();
251     ASSERT_NE(eventHub, nullptr);
252     auto inputHub = eventHub->GetOrCreateInputEventHub();
253     ASSERT_NE(inputHub, nullptr);
254 
255     MouseInfo mouseInfo;
256     auto mouseEventActuator = inputHub->mouseEventActuator_;
257     ASSERT_NE(mouseEventActuator, nullptr);
258     auto mouseInputEvents = mouseEventActuator->inputEvents_;
259     for (auto input : mouseInputEvents) {
260         input->GetOnMouseEventFunc()(mouseInfo);
261     }
262 
263     auto hoverEventActuator = inputHub->hoverEventActuator_;
264     ASSERT_NE(hoverEventActuator, nullptr);
265     auto hoverInputEvents = hoverEventActuator->inputEvents_;
266     for (auto input : hoverInputEvents) {
267         input->GetOnHoverEventFunc()(true);
268     }
269 
270     ASSERT_EQ(richEditorPattern->mouseEventInitialized_, true);
271 }
272 
273 /**
274  * @tc.name: RichEditorPatternTestCloseCustomKeyboard001
275  * @tc.desc: test CloseCustomKeyboard
276  * @tc.type: FUNC
277  */
278 HWTEST_F(RichEditorPatternTestNg, RichEditorPatternTestCloseCustomKeyboard001, TestSize.Level1)
279 {
280     ASSERT_NE(richEditorNode_, nullptr);
281     auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
282     ASSERT_NE(richEditorPattern, nullptr);
283     richEditorPattern->CloseCustomKeyboard();
284 
__anonfca374aa0402() 285     auto func = []() {};
286     auto oldFunc = richEditorPattern->customKeyboardBuilder_;
287 
288     richEditorPattern->customKeyboardBuilder_ = func;
289 
290     EXPECT_EQ(richEditorPattern->isCustomKeyboardAttached_, false);
291     EXPECT_EQ(richEditorPattern->RequestCustomKeyboard(), true);
292     EXPECT_EQ(richEditorPattern->RequestCustomKeyboard(), true);
293     EXPECT_EQ(richEditorPattern->isCustomKeyboardAttached_, true);
294     EXPECT_NE(richEditorPattern->keyboardOverlay_, nullptr);
295     ASSERT_EQ(richEditorPattern->CloseCustomKeyboard(), true);
296 
297     richEditorPattern->customKeyboardBuilder_ = oldFunc;
298 }
299 
300 /**
301  * @tc.name: RichEditorPatternTestUpdatePreviewText001
302  * @tc.desc: test UpdatePreviewText
303  * @tc.type: FUNC
304  */
305 HWTEST_F(RichEditorPatternTestNg, RichEditorPatternTestUpdatePreviewText001, TestSize.Level1)
306 {
307     ASSERT_NE(richEditorNode_, nullptr);
308     auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
309     ASSERT_NE(richEditorPattern, nullptr);
310 
311     std::string previewTextValue = INIT_VALUE_1;
312     PreviewRange previewRange;
313     previewRange.start = -1;
314     previewRange.end = -1;
315     richEditorPattern->SetPreviewText(PREVIEW_TEXT_VALUE1, previewRange);
316 
317     previewRange.start = -1;
318     previewRange.end = -1;
319     EXPECT_EQ(richEditorPattern->UpdatePreviewText(previewTextValue, previewRange), true);
320 
321     previewRange.start = 0;
322     previewRange.end = -1;
323     EXPECT_EQ(richEditorPattern->UpdatePreviewText(previewTextValue, previewRange), false);
324 
325     previewRange.start = -1;
326     previewRange.end = 0;
327     EXPECT_EQ(richEditorPattern->UpdatePreviewText(previewTextValue, previewRange), false);
328 
329     previewRange.start = 0;
330     previewRange.end = 0;
331     EXPECT_EQ(richEditorPattern->UpdatePreviewText(previewTextValue, previewRange), true);
332 
333     previewRange.start = richEditorPattern->previewTextRecord_.startOffset;
334     previewRange.end = richEditorPattern->previewTextRecord_.endOffset;
335     EXPECT_EQ(richEditorPattern->UpdatePreviewText(previewTextValue, previewRange), true);
336 }
337 
338 /**
339  * @tc.name: RichEditorPatternTestInsertDiffStyleValueInSpan001
340  * @tc.desc: test InsertDiffStyleValueInSpan
341  * @tc.type: FUNC
342  */
343 HWTEST_F(RichEditorPatternTestNg, RichEditorPatternTestInsertDiffStyleValueInSpan001, TestSize.Level1)
344 {
345     ASSERT_NE(richEditorNode_, nullptr);
346     auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
347     ASSERT_NE(richEditorPattern, nullptr);
348     auto spanNode = AceType::MakeRefPtr<SpanNode>(testSpanNodeId);
349     ASSERT_NE(spanNode, nullptr);
350 
351     TextInsertValueInfo info;
352     std::string insertValue;
353     struct UpdateSpanStyle typingStyle;
354     TextStyle textStyle(5);
355     richEditorPattern->SetTypingStyle(typingStyle, textStyle);
356 
357     richEditorPattern->InsertDiffStyleValueInSpan(spanNode, info, insertValue, false);
358     ASSERT_EQ(richEditorPattern->moveLength_, 0);
359 }
360 
361 /**
362  * @tc.name: RichEditorPatternTestCreateTextSpanNode001
363  * @tc.desc: test CreateTextSpanNode
364  * @tc.type: FUNC
365  */
366 HWTEST_F(RichEditorPatternTestNg, RichEditorPatternTestCreateTextSpanNode001, TestSize.Level1)
367 {
368     ASSERT_NE(richEditorNode_, nullptr);
369     auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
370     ASSERT_NE(richEditorPattern, nullptr);
371     RefPtr<SpanNode> spanNode;
372 
373     TextInsertValueInfo info;
374     std::string insertValue;
375     UpdateSpanStyle updateSpanStyle;
376     TextStyle textStyle;
377 
378     updateSpanStyle.useThemeFontColor = false;
379 
380     auto typingStyle = richEditorPattern->typingStyle_;
381     auto typingTextStyle = richEditorPattern->typingTextStyle_;
382 
383     richEditorPattern->typingStyle_ = std::nullopt;
384     richEditorPattern->typingTextStyle_ = std::nullopt;
385     richEditorPattern->CreateTextSpanNode(spanNode, info, insertValue, false);
386     EXPECT_EQ(spanNode->GetSpanItem()->useThemeDecorationColor, true);
387 
388     richEditorPattern->typingStyle_ = updateSpanStyle;
389     richEditorPattern->CreateTextSpanNode(spanNode, info, insertValue, false);
390     EXPECT_EQ(spanNode->GetSpanItem()->useThemeDecorationColor, true);
391 
392     richEditorPattern->typingStyle_ = std::nullopt;
393     richEditorPattern->typingTextStyle_ = textStyle;
394     richEditorPattern->CreateTextSpanNode(spanNode, info, insertValue, false);
395     EXPECT_EQ(spanNode->GetSpanItem()->useThemeDecorationColor, true);
396 
397     richEditorPattern->typingStyle_ = updateSpanStyle;
398     richEditorPattern->CreateTextSpanNode(spanNode, info, insertValue, false);
399     EXPECT_EQ(spanNode->GetSpanItem()->useThemeDecorationColor, true);
400 
401     richEditorPattern->typingStyle_ = typingStyle;
402     richEditorPattern->typingTextStyle_ = typingTextStyle;
403 }
404 
405 /**
406  * @tc.name: RichEditorPatternTestInitPreviewText001
407  * @tc.desc: test InitPreviewText
408  * @tc.type: FUNC
409  */
410 HWTEST_F(RichEditorPatternTestNg, RichEditorPatternTestInitPreviewText001, TestSize.Level1)
411 {
412     ASSERT_NE(richEditorNode_, nullptr);
413     auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
414     ASSERT_NE(richEditorPattern, nullptr);
415 
416     std::string previewTextValue;
417     PreviewRange range;
418 
419     range.start = -1;
420     range.end = 0;
421     ASSERT_EQ(richEditorPattern->InitPreviewText(previewTextValue, range), false);
422 
423     range.start = 0;
424     range.end = -1;
425     ASSERT_EQ(richEditorPattern->InitPreviewText(previewTextValue, range), false);
426 
427     range.start = -1;
428     range.end = -1;
429     ASSERT_EQ(richEditorPattern->InitPreviewText(previewTextValue, range), true);
430 
431     richEditorPattern->textSelector_.baseOffset = 0;
432     richEditorPattern->textSelector_.destinationOffset = 1;
433     ASSERT_EQ(richEditorPattern->InitPreviewText(previewTextValue, range), true);
434 }
435 
436 /**
437  * @tc.name: ResetDragOption001
438  * @tc.desc: test ResetDragOption
439  * @tc.type: FUNC
440  */
441 HWTEST_F(RichEditorPatternTestNg, ResetDragOption001, TestSize.Level1)
442 {
443     /**
444      * @tc.steps: step1. init and call function.
445      */
446     ASSERT_NE(richEditorNode_, nullptr);
447     auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
448     ASSERT_NE(richEditorPattern, nullptr);
449     richEditorPattern->CreateNodePaintMethod();
450     EXPECT_NE(richEditorPattern->contentMod_, nullptr);
451     EXPECT_NE(richEditorPattern->overlayMod_, nullptr);
452     auto gestureEventHub = richEditorPattern->GetGestureEventHub();
453     EXPECT_NE(gestureEventHub, nullptr);
454     /**
455      * @tc.steps: step2. init and call function.
456      */
457     gestureEventHub->isTextDraggable_ = true;
458     richEditorPattern->ResetDragOption();
459     EXPECT_EQ(richEditorPattern->textSelector_.baseOffset, -1);
460     EXPECT_EQ(richEditorPattern->textSelector_.destinationOffset, -1);
461 }
462 
463 /**
464  * @tc.name: VirtualKeyboardAreaChanged001
465  * @tc.desc: test OnVirtualKeyboardAreaChanged
466  * @tc.type: FUNC
467  */
468 HWTEST_F(RichEditorPatternTestNg, VirtualKeyboardAreaChanged001, TestSize.Level1)
469 {
470     /**
471      * @tc.steps: step1. init and call function.
472      */
473     ASSERT_NE(richEditorNode_, nullptr);
474     auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
475     ASSERT_NE(richEditorPattern, nullptr);
476     richEditorPattern->CreateNodePaintMethod();
477     EXPECT_NE(richEditorPattern->contentMod_, nullptr);
478     EXPECT_NE(richEditorPattern->overlayMod_, nullptr);
479     float height = 0.0f;
480     auto x = richEditorPattern->CalcCursorOffsetByPosition(richEditorPattern->textSelector_.GetStart(), height).GetX();
481     richEditorPattern->OnVirtualKeyboardAreaChanged();
482     EXPECT_EQ(richEditorPattern->textSelector_.selectionBaseOffset.GetX(), x);
483 }
484 
485 /**
486  * @tc.name: GetCaretMetrics001
487  * @tc.desc: test GetCaretMetrics
488  * @tc.type: FUNC
489  */
490 HWTEST_F(RichEditorPatternTestNg, GetCaretMetrics001, TestSize.Level1)
491 {
492     /**
493      * @tc.steps: step1. init and call function.
494      */
495     ASSERT_NE(richEditorNode_, nullptr);
496     auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
497     ASSERT_NE(richEditorPattern, nullptr);
498     richEditorPattern->CreateNodePaintMethod();
499     EXPECT_NE(richEditorPattern->contentMod_, nullptr);
500     EXPECT_NE(richEditorPattern->overlayMod_, nullptr);
501 
502     CaretMetricsF caretCaretMetric;
503     caretCaretMetric.height = 10.0f;
504     richEditorPattern->GetCaretMetrics(caretCaretMetric);
505     EXPECT_EQ(caretCaretMetric.height, 0.0f);
506 }
507 
508 /**
509  * @tc.name: RichEditorToJsonValue001
510  * @tc.desc: test ToJsonValue
511  * @tc.type: FUNC
512  */
513 HWTEST_F(RichEditorPatternTestNg, RichEditorToJsonValue001, TestSize.Level1)
514 {
515     /**
516      * @tc.steps: step1. init and call function.
517      */
518     ASSERT_NE(richEditorNode_, nullptr);
519     auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
520     ASSERT_NE(richEditorPattern, nullptr);
521     richEditorPattern->CreateNodePaintMethod();
522     EXPECT_NE(richEditorPattern->contentMod_, nullptr);
523     EXPECT_NE(richEditorPattern->overlayMod_, nullptr);
524 
525     auto jsonObject = JsonUtil::Create(true);
526     InspectorFilter filter;
527     filter.filterFixed = 0;
528     filter.filterExt.clear();
529     auto ret1 = filter.IsFastFilter();
530     richEditorPattern->ToJsonValue(jsonObject, filter);
531     auto ret2 = filter.IsFastFilter();
532     EXPECT_EQ(ret1, ret2);
533 }
534 
535 /**
536  * @tc.name: HandleOnDragStatusCallback001
537  * @tc.desc: test HandleOnDragStatusCallback
538  * @tc.type: FUNC
539  */
540 HWTEST_F(RichEditorPatternTestNg, HandleOnDragStatusCallback001, TestSize.Level1)
541 {
542     /**
543      * @tc.steps: step1. init and call function.
544      */
545     ASSERT_NE(richEditorNode_, nullptr);
546     auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
547     ASSERT_NE(richEditorPattern, nullptr);
548     richEditorPattern->CreateNodePaintMethod();
549     EXPECT_NE(richEditorPattern->contentMod_, nullptr);
550     EXPECT_NE(richEditorPattern->overlayMod_, nullptr);
551     RefPtr<NotifyDragEvent> notifyDragEvent = AceType::MakeRefPtr<NotifyDragEvent>();
552     /**
553      * @tc.steps: step2. change parameter and call function.
554      */
555     DragEventType dragEventType1 = DragEventType::MOVE;
556     notifyDragEvent->SetX(10.0f);
557     notifyDragEvent->SetY(20.0f);
558     richEditorPattern->HandleOnDragStatusCallback(dragEventType1, notifyDragEvent);
559     EXPECT_TRUE(richEditorPattern->isOnlyRequestFocus_);
560 }
561 
562 /**
563  * @tc.name: HandleCursorOnDragEnded001
564  * @tc.desc: test HandleCursorOnDragEnded
565  * @tc.type: FUNC
566  */
567 HWTEST_F(RichEditorPatternTestNg, HandleCursorOnDragEnded001, TestSize.Level1)
568 {
569     /**
570      * @tc.steps: step1. init and call function.
571      */
572     ASSERT_NE(richEditorNode_, nullptr);
573     auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
574     ASSERT_NE(richEditorPattern, nullptr);
575     richEditorPattern->CreateNodePaintMethod();
576     EXPECT_NE(richEditorPattern->contentMod_, nullptr);
577     EXPECT_NE(richEditorPattern->overlayMod_, nullptr);
578     auto focusHub = richEditorPattern->GetFocusHub();
579     EXPECT_NE(focusHub, nullptr);
580     RefPtr<NotifyDragEvent> notifyDragEvent = AceType::MakeRefPtr<NotifyDragEvent>();
581     EXPECT_NE(notifyDragEvent, nullptr);
582     /**
583      * @tc.steps: step2. change parameter and call function.
584      */
585     richEditorPattern->isCursorAlwaysDisplayed_ = false;
586     richEditorPattern->HandleCursorOnDragEnded(notifyDragEvent);
587     EXPECT_EQ(richEditorPattern->caretTwinkling_, false);
588 
589     /**
590      * @tc.steps: step3. change parameter and call function.
591      */
592     richEditorPattern->isCursorAlwaysDisplayed_ = true;
593 
594     focusHub->currentFocus_ = false;
595     richEditorPattern->HandleCursorOnDragEnded(notifyDragEvent);
596     EXPECT_EQ(richEditorPattern->isCursorAlwaysDisplayed_, false);
597 
598     /**
599      * @tc.steps: step4. change parameter and call function.
600      */
601     richEditorPattern->isCursorAlwaysDisplayed_ = true;
602     focusHub->currentFocus_ = true;
603     richEditorPattern->HandleCursorOnDragEnded(notifyDragEvent);
604     EXPECT_EQ(richEditorPattern->isCursorAlwaysDisplayed_, false);
605 }
606 
607 /**
608  * @tc.name: HandleCursorOnDragLeaved001
609  * @tc.desc: test HandleCursorOnDragLeaved
610  * @tc.type: FUNC
611  */
612 HWTEST_F(RichEditorPatternTestNg, HandleCursorOnDragLeaved001, TestSize.Level1)
613 {
614     /**
615      * @tc.steps: step1. init and call function.
616      */
617     ASSERT_NE(richEditorNode_, nullptr);
618     auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
619     ASSERT_NE(richEditorPattern, nullptr);
620     richEditorPattern->CreateNodePaintMethod();
621     EXPECT_NE(richEditorPattern->contentMod_, nullptr);
622     EXPECT_NE(richEditorPattern->overlayMod_, nullptr);
623     RefPtr<NotifyDragEvent> notifyDragEvent = AceType::MakeRefPtr<NotifyDragEvent>();
624     EXPECT_NE(notifyDragEvent, nullptr);
625     /**
626      * @tc.steps: step2. change parameter and call function.
627      */
628     richEditorPattern->isCursorAlwaysDisplayed_ = false;
629     richEditorPattern->HandleCursorOnDragLeaved(notifyDragEvent);
630     EXPECT_EQ(richEditorPattern->isCursorAlwaysDisplayed_, false);
631 }
632 
633 /**
634  * @tc.name: HandleCursorOnDragMoved001
635  * @tc.desc: test HandleCursorOnDragMoved
636  * @tc.type: FUNC
637  */
638 HWTEST_F(RichEditorPatternTestNg, HandleCursorOnDragMoved001, TestSize.Level1)
639 {
640     /**
641      * @tc.steps: step1. init and call function.
642      */
643     ASSERT_NE(richEditorNode_, nullptr);
644     auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
645     ASSERT_NE(richEditorPattern, nullptr);
646     richEditorPattern->CreateNodePaintMethod();
647     EXPECT_NE(richEditorPattern->contentMod_, nullptr);
648     EXPECT_NE(richEditorPattern->overlayMod_, nullptr);
649     RefPtr<NotifyDragEvent> notifyDragEvent = AceType::MakeRefPtr<NotifyDragEvent>();
650     EXPECT_NE(notifyDragEvent, nullptr);
651     /**
652      * @tc.steps: step2. change parameter and call function.
653      */
654     richEditorPattern->isCursorAlwaysDisplayed_ = true;
655     richEditorPattern->HandleCursorOnDragMoved(notifyDragEvent);
656     EXPECT_EQ(richEditorPattern->isCursorAlwaysDisplayed_, true);
657     /**
658      * @tc.steps: step2. change parameter and call function.
659      */
660     richEditorPattern->isCursorAlwaysDisplayed_ = false;
661     richEditorPattern->HandleCursorOnDragMoved(notifyDragEvent);
662     EXPECT_EQ(richEditorPattern->isCursorAlwaysDisplayed_, true);
663 }
664 
665 /**
666  * @tc.name: IsClickBoundary001
667  * @tc.desc: test IsClickBoundary
668  * @tc.type: FUNC
669  */
670 HWTEST_F(RichEditorPatternTestNg, IsClickBoundary001, TestSize.Level1)
671 {
672     /**
673      * @tc.steps: step1. init and call function.
674      */
675     ASSERT_NE(richEditorNode_, nullptr);
676     auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
677     ASSERT_NE(richEditorPattern, nullptr);
678     richEditorPattern->CreateNodePaintMethod();
679     EXPECT_NE(richEditorPattern->contentMod_, nullptr);
680     EXPECT_NE(richEditorPattern->overlayMod_, nullptr);
681     /**
682      * @tc.steps: step2. change parameter and call function.
683      */
684     auto length = richEditorPattern->GetTextContentLength();
685     auto caretPosition = length + 1;
686     auto ret = richEditorPattern->IsClickBoundary(caretPosition);
687     EXPECT_EQ(ret, true);
688 }
689 
690 /**
691  * @tc.name: AdjustPlaceholderSelection001
692  * @tc.desc: test AdjustPlaceholderSelection
693  * @tc.type: FUNC
694  */
695 HWTEST_F(RichEditorPatternTestNg, AdjustPlaceholderSelection001, TestSize.Level1)
696 {
697     /**
698      * @tc.steps: step1. init and call function.
699      */
700     ASSERT_NE(richEditorNode_, nullptr);
701     auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
702     ASSERT_NE(richEditorPattern, nullptr);
703     richEditorPattern->CreateNodePaintMethod();
704     EXPECT_NE(richEditorPattern->contentMod_, nullptr);
705     EXPECT_NE(richEditorPattern->overlayMod_, nullptr);
706     AddSpan(INIT_VALUE_1);
707     OHOS::Ace::RefPtr<OHOS::Ace::NG::SpanItem> spanItem1 = AceType::MakeRefPtr<ImageSpanItem>();
708     richEditorPattern->spans_.emplace_back(spanItem1);
709     OHOS::Ace::RefPtr<OHOS::Ace::NG::SpanItem> spanItem2 = AceType::MakeRefPtr<PlaceholderSpanItem>();
710     richEditorPattern->spans_.emplace_back(spanItem2);
711     /**
712      * @tc.steps: step2. change parameter and call function.
713      */
714     int32_t start = 10;
715     int32_t end = 20;
716     Offset touchPos(11.0f, 11.0f);
717     richEditorPattern->AdjustPlaceholderSelection(start, end, touchPos);
718     EXPECT_NE(start, end);
719 }
720 
721 /**
722  * @tc.name: OnAutoScroll001
723  * @tc.desc: test OnAutoScroll
724  * @tc.type: FUNC
725  */
726 HWTEST_F(RichEditorPatternTestNg, OnAutoScroll001, TestSize.Level1)
727 {
728     /**
729      * @tc.steps: step1. init and call function.
730      */
731     ASSERT_NE(richEditorNode_, nullptr);
732     auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
733     ASSERT_NE(richEditorPattern, nullptr);
734     richEditorPattern->CreateNodePaintMethod();
735     EXPECT_NE(richEditorPattern->contentMod_, nullptr);
736     EXPECT_NE(richEditorPattern->overlayMod_, nullptr);
737     AutoScrollParam param;
738     param.showScrollbar = true;
739     param.offset = 0.0f;
740     richEditorPattern->contentChange_ = false;
741     richEditorPattern->contentRect_.SetRect(0, 0, 1, 1);
742     richEditorPattern->richTextRect_.SetRect(0, 0, 1, 1);
743     /**
744      * @tc.steps: step2. change parameter and call function.
745      */
746     param.isFirstHandle = true;
747     param.autoScrollEvent = AutoScrollEvent::HANDLE;
748     richEditorPattern->OnAutoScroll(param);
749     EXPECT_TRUE(param.showScrollbar);
750     /**
751      * @tc.steps: step3. change parameter and call function.
752      */
753     param.autoScrollEvent = AutoScrollEvent::DRAG;
754     richEditorPattern->OnAutoScroll(param);
755     EXPECT_TRUE(param.showScrollbar);
756     /**
757      * @tc.steps: step4. change parameter and call function.
758      */
759     param.autoScrollEvent = AutoScrollEvent::MOUSE;
760     richEditorPattern->OnAutoScroll(param);
761     EXPECT_TRUE(param.showScrollbar);
762 }
763 
764 /**
765  * @tc.name: ScheduleAutoScroll001
766  * @tc.desc: test ScheduleAutoScroll
767  * @tc.type: FUNC
768  */
769 HWTEST_F(RichEditorPatternTestNg, ScheduleAutoScroll001, TestSize.Level1)
770 {
771     /**
772      * @tc.steps: step1. init and call function.
773      */
774     ASSERT_NE(richEditorNode_, nullptr);
775     auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
776     ASSERT_NE(richEditorPattern, nullptr);
777     richEditorPattern->CreateNodePaintMethod();
778     EXPECT_NE(richEditorPattern->contentMod_, nullptr);
779     EXPECT_NE(richEditorPattern->overlayMod_, nullptr);
780     AutoScrollParam param;
781     param.isFirstRun_ = true;
782     param.offset = 0.0f;
783     richEditorPattern->contentRect_.SetRect(0, 1, 1, 1);
784     richEditorPattern->richTextRect_.SetRect(0, 2, 1, 1);
785     /**
786      * @tc.steps: step2. change parameter and call function.
787      */
788     richEditorPattern->isAutoScrollRunning_ = true;
789     richEditorPattern->ScheduleAutoScroll(param);
790     EXPECT_TRUE(param.isFirstRun_);
791 }
792 
793 /**
794  * @tc.name: UpdateChildrenOffset001
795  * @tc.desc: test UpdateChildrenOffset
796  * @tc.type: FUNC
797  */
798 HWTEST_F(RichEditorPatternTestNg, UpdateChildrenOffset001, TestSize.Level1)
799 {
800     /**
801      * @tc.steps: step1. init and call function.
802      */
803     ASSERT_NE(richEditorNode_, nullptr);
804     auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
805     ASSERT_NE(richEditorPattern, nullptr);
806     richEditorPattern->CreateNodePaintMethod();
807     EXPECT_NE(richEditorPattern->contentMod_, nullptr);
808     EXPECT_NE(richEditorPattern->overlayMod_, nullptr);
809     auto host = richEditorPattern->GetHost();
810     ASSERT_NE(host, nullptr);
811     /**
812      * @tc.steps: step2. change parameter and call function.
813      */
814     AddSpan(INIT_VALUE_1);
815     OHOS::Ace::RefPtr<OHOS::Ace::NG::SpanItem> spanItem1 = AceType::MakeRefPtr<ImageSpanItem>();
816     richEditorPattern->spans_.emplace_back(spanItem1);
817     OHOS::Ace::RefPtr<OHOS::Ace::NG::SpanItem> spanItem2 = AceType::MakeRefPtr<PlaceholderSpanItem>();
818     richEditorPattern->spans_.emplace_back(spanItem2);
819     TestParagraphRect paragraphRect = { .start = 0, .end = 6, .rects = { { -400.0, -400.0, 200.0, 200.0 } } };
820     TestParagraphItem paragraphItem = { .start = 0, .end = 6, .testParagraphRects = { paragraphRect } };
821     AddParagraph(paragraphItem);
822     richEditorPattern->textSelector_.baseOffset = 0;
823     richEditorPattern->textSelector_.destinationOffset = 6;
824     richEditorPattern->contentRect_ = { -500.0, -500.0, 500.0, 500.0 };
825     richEditorPattern->isSpanStringMode_ = true;
826     std::list<RefPtr<UINode>>& childrens = host->ModifyChildren();
827     auto childNode2 = FrameNode::CreateFrameNode("childNode", 2, AceType::MakeRefPtr<ImagePattern>());
828     auto childNode3 = FrameNode::CreateFrameNode("childNode", 3, AceType::MakeRefPtr<PlaceholderSpanPattern>());
829     auto childNode4 = FrameNode::CreateFrameNode("childNode", 4, AceType::MakeRefPtr<ImagePattern>());
830     auto childNode5 = FrameNode::CreateFrameNode("childNode", 5, AceType::MakeRefPtr<PlaceholderSpanPattern>());
831     auto childNode6 = FrameNode::CreateFrameNode("childNode", 5, AceType::MakeRefPtr<Pattern>());
832     childrens.emplace_back(childNode2);
833     childrens.emplace_back(childNode3);
834     childrens.emplace_back(childNode4);
835     childrens.emplace_back(childNode5);
836     richEditorPattern->UpdateChildrenOffset();
837     EXPECT_NE(richEditorPattern->spans_.size(), 0);
838 }
839 
840 /**
841  * @tc.name: NeedAiAnalysis001
842  * @tc.desc: test NeedAiAnalysis
843  * @tc.type: FUNC
844  */
845 HWTEST_F(RichEditorPatternTestNg, NeedAiAnalysis001, TestSize.Level1)
846 {
847     /**
848      * @tc.steps: step1. init and call function.
849      */
850     ASSERT_NE(richEditorNode_, nullptr);
851     auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
852     ASSERT_NE(richEditorPattern, nullptr);
853     richEditorPattern->CreateNodePaintMethod();
854     EXPECT_NE(richEditorPattern->contentMod_, nullptr);
855     EXPECT_NE(richEditorPattern->overlayMod_, nullptr);
856     /**
857      * @tc.steps: step2. change parameter and call function.
858      */
859     std::string content = "";
860     richEditorPattern->isSpanStringMode_ = true;
861     richEditorPattern->styledString_ = AceType::MakeRefPtr<MutableSpanString>(content);
862     CaretUpdateType targeType1 = CaretUpdateType::PRESSED;
863     int32_t pos = 0;
864     int32_t spanStart = 10;
865     auto ret = richEditorPattern->NeedAiAnalysis(targeType1, pos, spanStart, content);
866     EXPECT_FALSE(ret);
867     /**
868      * @tc.steps: step3. change parameter and call function.
869      */
870     CaretUpdateType targeType2 = CaretUpdateType::DOUBLE_CLICK;
871     ret = richEditorPattern->NeedAiAnalysis(targeType2, pos, spanStart, content);
872     EXPECT_FALSE(ret);
873 }
874 
875 /**
876  * @tc.name: AdjustCursorPosition001
877  * @tc.desc: test AdjustCursorPosition
878  * @tc.type: FUNC
879  */
880 HWTEST_F(RichEditorPatternTestNg, AdjustCursorPosition001, TestSize.Level1)
881 {
882     /**
883      * @tc.steps: step1. init and call function.
884      */
885     ASSERT_NE(richEditorNode_, nullptr);
886     auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
887     ASSERT_NE(richEditorPattern, nullptr);
888     richEditorPattern->CreateNodePaintMethod();
889     EXPECT_NE(richEditorPattern->contentMod_, nullptr);
890     EXPECT_NE(richEditorPattern->overlayMod_, nullptr);
891     /**
892      * @tc.steps: step2. change parameter and call function.
893      */
894     std::string content = "TEST123";
895     richEditorPattern->isSpanStringMode_ = true;
896     richEditorPattern->styledString_ = AceType::MakeRefPtr<MutableSpanString>(content);
897     int32_t pos = 0;
898     richEditorPattern->AdjustCursorPosition(pos);
899     EXPECT_EQ(pos, 0);
900 }
901 
902 /**
903  * @tc.name: OnScrollEndCallback001
904  * @tc.desc: test OnScrollEndCallback
905  * @tc.type: FUNC
906  */
907 HWTEST_F(RichEditorPatternTestNg, OnScrollEndCallback001, TestSize.Level1)
908 {
909     /**
910      * @tc.steps: step1. init and call function.
911      */
912     ASSERT_NE(richEditorNode_, nullptr);
913     auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
914     ASSERT_NE(richEditorPattern, nullptr);
915     richEditorPattern->CreateNodePaintMethod();
916     EXPECT_NE(richEditorPattern->contentMod_, nullptr);
917     EXPECT_NE(richEditorPattern->overlayMod_, nullptr);
918     TestParagraphRect paragraphRect = { .start = 0, .end = 6, .rects = { { -400.0, -400.0, 200.0, 200.0 } } };
919     TestParagraphItem paragraphItem = { .start = 0, .end = 6, .testParagraphRects = { paragraphRect } };
920     AddParagraph(paragraphItem);
921     richEditorPattern->textSelector_.baseOffset = 0;
922     richEditorPattern->textSelector_.destinationOffset = 6;
923     richEditorPattern->contentRect_ = { -500.0, -500.0, 500.0, 500.0 };
924     /**
925      * @tc.steps: step2. change parameter and call function.
926      */
927     richEditorPattern->OnScrollEndCallback();
928     auto res = richEditorPattern->IsSelectAreaVisible();
929     EXPECT_TRUE(res);
930 }
931 
932 /**
933  * @tc.name: IsCaretInContentArea001
934  * @tc.desc: test IsCaretInContentArea
935  * @tc.type: FUNC
936  */
937 HWTEST_F(RichEditorPatternTestNg, IsCaretInContentArea001, TestSize.Level1)
938 {
939     /**
940      * @tc.steps: step1. init and call function.
941      */
942     ASSERT_NE(richEditorNode_, nullptr);
943     auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
944     ASSERT_NE(richEditorPattern, nullptr);
945     richEditorPattern->CreateNodePaintMethod();
946     EXPECT_NE(richEditorPattern->contentMod_, nullptr);
947     EXPECT_NE(richEditorPattern->overlayMod_, nullptr);
948     /**
949      * @tc.steps: step2. change parameter and call function.
950      */
951     auto ret = richEditorPattern->IsCaretInContentArea();
952     EXPECT_EQ(ret, false);
953 }
954 
955 /**
956  * @tc.name: UpdateScrollBarOffset001
957  * @tc.desc: test UpdateScrollBarOffset
958  * @tc.type: FUNC
959  */
960 HWTEST_F(RichEditorPatternTestNg, UpdateScrollBarOffset001, TestSize.Level1)
961 {
962     /**
963      * @tc.steps: step1. init and call function.
964      */
965     ASSERT_NE(richEditorNode_, nullptr);
966     auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
967     ASSERT_NE(richEditorPattern, nullptr);
968     richEditorPattern->CreateNodePaintMethod();
969     EXPECT_NE(richEditorPattern->contentMod_, nullptr);
970     EXPECT_NE(richEditorPattern->overlayMod_, nullptr);
971     auto tmpHost = richEditorPattern->GetHost();
972     ASSERT_NE(tmpHost, nullptr);
973     /**
974      * @tc.steps: step2. change parameter and call function.
975      */
976     richEditorPattern->scrollBar_ = nullptr;
977     richEditorPattern->scrollBarProxy_ = nullptr;
978     richEditorPattern->UpdateScrollBarOffset();
979     EXPECT_FALSE(tmpHost->isPropertyDiffMarked_);
980 }
981 
982 /**
983  * @tc.name: GetChangeSpanStyle002
984  * @tc.desc: test GetChangeSpanStyle
985  * @tc.type: FUNC
986  */
987 HWTEST_F(RichEditorPatternTestNg, GetChangeSpanStyle002, TestSize.Level1)
988 {
989     /**
990      * @tc.steps: step1. init and call function.
991      */
992     ASSERT_NE(richEditorNode_, nullptr);
993     auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
994     ASSERT_NE(richEditorPattern, nullptr);
995 
996     struct UpdateParagraphStyle paragraphStyle;
997     paragraphStyle.textAlign = TextAlign::END;
998     paragraphStyle.leadingMargin = std::make_optional<NG::LeadingMargin>();
999     paragraphStyle.leadingMargin->size = LeadingMarginSize(Dimension(5.0), Dimension(10.0));
1000     richEditorPattern->UpdateParagraphStyle(0, 6, paragraphStyle);
1001     std::optional<struct UpdateParagraphStyle> spanParaStyle = paragraphStyle;
1002 
1003     RichEditorChangeValue changeValue;
1004     RichEditorAbstractSpanResult span1;
1005     changeValue.originalSpans_.emplace_back(span1);
1006     RichEditorAbstractSpanResult span2;
1007     changeValue.originalSpans_.emplace_back(span2);
1008 
1009     RichEditorAbstractSpanResult& firstInfo = changeValue.originalSpans_.front();
1010     int32_t firstLength = static_cast<int32_t>(StringUtils::ToWstring(firstInfo.GetValue()).length());
1011     firstInfo.SetEraseLength(firstLength);
1012     RichEditorAbstractSpanResult& lastInfo = changeValue.originalSpans_.back();
1013     int32_t lastLength = static_cast<int32_t>(StringUtils::ToWstring(lastInfo.GetValue()).length());
1014     lastInfo.SetEraseLength(lastLength);
1015 
1016     std::optional<TextStyle> spanTextStyle;
1017     RefPtr<SpanNode> spanNode = OHOS::Ace::NG::SpanNode::CreateSpanNode(1);
1018     int32_t spanIndex = 0;
1019     richEditorPattern->spans_.clear();
1020     OHOS::Ace::RefPtr<OHOS::Ace::NG::SpanItem> spanItem1 = AceType::MakeRefPtr<ImageSpanItem>();
1021     richEditorPattern->spans_.emplace_back(spanItem1);
1022     OHOS::Ace::RefPtr<OHOS::Ace::NG::SpanItem> spanItem2 = AceType::MakeRefPtr<PlaceholderSpanItem>();
1023     richEditorPattern->spans_.emplace_back(spanItem2);
1024     OHOS::Ace::RefPtr<OHOS::Ace::NG::SpanItem> spanItem3 = AceType::MakeRefPtr<ImageSpanItem>();
1025     richEditorPattern->spans_.emplace_back(spanItem3);
1026     /**
1027      * @tc.steps: step2. change parameter and call function.
1028      */
1029     lastInfo.SetSpanIndex(richEditorPattern->spans_.size() - 2);
1030     richEditorPattern->GetChangeSpanStyle(changeValue, spanTextStyle, spanParaStyle, spanNode, spanIndex);
1031     EXPECT_FALSE(spanTextStyle.has_value());
1032 }
1033 
1034 /**
1035  * @tc.name: GetReplacedSpan001
1036  * @tc.desc: test GetReplacedSpan
1037  * @tc.type: FUNC
1038  */
1039 HWTEST_F(RichEditorPatternTestNg, GetReplacedSpan001, TestSize.Level1)
1040 {
1041     /**
1042      * @tc.steps: step1. init and call function.
1043      */
1044     ASSERT_NE(richEditorNode_, nullptr);
1045     auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
1046     ASSERT_NE(richEditorPattern, nullptr);
1047     auto host = richEditorPattern->GetHost();
1048     EXPECT_NE(host, nullptr);
1049 
1050     richEditorPattern->spans_.clear();
1051     host->children_.clear();
1052     auto textframeNode = FrameNode::CreateFrameNode(
1053         V2::TEXT_ETS_TAG, ElementRegister::GetInstance()->MakeUniqueId(), AceType::MakeRefPtr<TextPattern>());
1054     ASSERT_NE(textframeNode, nullptr);
1055     host->AddChild(textframeNode);
1056     textframeNode->SetParent(host);
1057     auto imageSpanNode = ImageSpanNode::GetOrCreateSpanNode(V2::IMAGE_ETS_TAG,
__anonfca374aa0502() 1058         ElementRegister::GetInstance()->MakeUniqueId(), []() { return AceType::MakeRefPtr<ImagePattern>(); });
1059     EXPECT_NE(imageSpanNode, nullptr);
1060     host->AddChild(imageSpanNode);
1061     imageSpanNode->SetParent(host);
1062 
1063     RichEditorChangeValue changeValue;
1064     struct UpdateParagraphStyle paragraphStyle;
1065     paragraphStyle.textAlign = TextAlign::END;
1066     paragraphStyle.leadingMargin = std::make_optional<NG::LeadingMargin>();
1067     paragraphStyle.leadingMargin->size = LeadingMarginSize(Dimension(5.0), Dimension(10.0));
1068     richEditorPattern->UpdateParagraphStyle(0, 6, paragraphStyle);
1069     std::optional<struct UpdateParagraphStyle> spanParaStyle = paragraphStyle;
1070     int32_t innerPosition = 0;
1071     std::string insertValue = "test123\n123";
1072     int32_t textIndex = 1;
1073     std::optional<TextStyle> textStyle = std::optional<TextStyle>(TextStyle());
1074     richEditorPattern->GetReplacedSpan(
1075         changeValue, innerPosition, insertValue, textIndex, textStyle, spanParaStyle, false, false);
1076     auto uiNode = host->GetChildAtIndex(0);
1077     EXPECT_NE(uiNode, nullptr);
1078     EXPECT_EQ(uiNode->GetTag(), V2::TEXT_ETS_TAG);
1079 }
1080 
1081 /**
1082  * @tc.name: GetReplacedSpanFission001
1083  * @tc.desc: test GetReplacedSpanFission
1084  * @tc.type: FUNC
1085  */
1086 HWTEST_F(RichEditorPatternTestNg, GetReplacedSpanFission001, TestSize.Level1)
1087 {
1088     /**
1089      * @tc.steps: step1. init and call function.
1090      */
1091     ASSERT_NE(richEditorNode_, nullptr);
1092     auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
1093     ASSERT_NE(richEditorPattern, nullptr);
1094     RichEditorChangeValue changeValue;
1095     int32_t innerPosition = 0;
1096     std::string content = "test123\n";
1097     int32_t startSpanIndex = 0;
1098     int32_t offsetInSpan = 0;
1099     std::optional<TextStyle> textStyle = std::optional<TextStyle>(TextStyle());
1100     struct UpdateParagraphStyle paragraphStyle;
1101     paragraphStyle.textAlign = TextAlign::END;
1102     paragraphStyle.leadingMargin = std::make_optional<NG::LeadingMargin>();
1103     paragraphStyle.leadingMargin->size = LeadingMarginSize(Dimension(5.0), Dimension(10.0));
1104     richEditorPattern->UpdateParagraphStyle(0, 6, paragraphStyle);
1105     std::optional<struct UpdateParagraphStyle> spanParaStyle = paragraphStyle;
1106     richEditorPattern->GetReplacedSpanFission(
1107         changeValue, innerPosition, content, startSpanIndex, offsetInSpan, textStyle, spanParaStyle);
1108     EXPECT_NE(innerPosition, 0);
1109 }
1110 /**
1111  * @tc.name: SetTextStyleToRet001
1112  * @tc.desc: test SetTextStyleToRet
1113  * @tc.type: FUNC
1114  */
1115 HWTEST_F(RichEditorPatternTestNg, SetTextStyleToRet001, TestSize.Level1)
1116 {
1117     /**
1118      * @tc.steps: step1. init and call function.
1119      */
1120     ASSERT_NE(richEditorNode_, nullptr);
1121     auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
1122     ASSERT_NE(richEditorPattern, nullptr);
1123     RichEditorAbstractSpanResult retInfo;
1124     TextStyle textStyle;
1125     textStyle.fontFamilies_.emplace_back(INIT_VALUE_1);
1126     textStyle.fontFamilies_.emplace_back(INIT_VALUE_2);
1127     textStyle.fontFamilies_.emplace_back(INIT_VALUE_3);
1128     richEditorPattern->SetTextStyleToRet(retInfo, textStyle);
1129     EXPECT_EQ(retInfo.GetFontSize(), textStyle.GetFontSize().ConvertToVp());
1130 }
1131 
1132 /**
1133  * @tc.name: CalcInsertValueObj003
1134  * @tc.desc: test CalcInsertValueObj
1135  * @tc.type: FUNC
1136  */
1137 HWTEST_F(RichEditorPatternTestNg, CalcInsertValueObj003, TestSize.Level1)
1138 {
1139     /**
1140      * @tc.steps: step1. init and call function.
1141      */
1142     ASSERT_NE(richEditorNode_, nullptr);
1143     auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
1144     ASSERT_NE(richEditorPattern, nullptr);
1145 
1146     richEditorPattern->spans_.clear();
1147     AddSpan("");
1148     richEditorPattern->spans_.push_front(AceType::MakeRefPtr<SpanItem>());
1149     auto it = richEditorPattern->spans_.front();
1150     TextInsertValueInfo info;
1151     it->content = "";
1152     it->position = 0;
1153     int textIndex = 1;
1154     richEditorPattern->CalcInsertValueObj(info, textIndex, false);
1155     EXPECT_NE(info.GetSpanIndex(), richEditorPattern->spans_.size());
1156 
1157     /**
1158      * @tc.steps: step2. change parameter and call function.
1159      */
1160     it->content = "test123\n";
1161     it->position = 18;
1162     textIndex = 18;
1163     richEditorPattern->CalcInsertValueObj(info, textIndex, false);
1164     EXPECT_NE(info.GetSpanIndex(), richEditorPattern->spans_.size());
1165 }
1166 
1167 /**
1168  * @tc.name: GetDelPartiallySpanItem001
1169  * @tc.desc: test GetDelPartiallySpanItem
1170  * @tc.type: FUNC
1171  */
1172 HWTEST_F(RichEditorPatternTestNg, GetDelPartiallySpanItem001, TestSize.Level1)
1173 {
1174     /**
1175      * @tc.steps: step1. init and call function.
1176      */
1177     ASSERT_NE(richEditorNode_, nullptr);
1178     auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
1179     ASSERT_NE(richEditorPattern, nullptr);
1180     richEditorPattern->spans_.clear();
1181     OHOS::Ace::RefPtr<OHOS::Ace::NG::SpanItem> spanItem1 = AceType::MakeRefPtr<ImageSpanItem>();
1182     richEditorPattern->spans_.emplace_back(spanItem1);
1183     OHOS::Ace::RefPtr<OHOS::Ace::NG::SpanItem> spanItem2 = AceType::MakeRefPtr<PlaceholderSpanItem>();
1184     richEditorPattern->spans_.emplace_back(spanItem2);
1185     OHOS::Ace::RefPtr<OHOS::Ace::NG::SpanItem> spanItem3 = AceType::MakeRefPtr<ImageSpanItem>();
1186     richEditorPattern->spans_.emplace_back(spanItem3);
1187     RichEditorChangeValue changeValue;
1188     RichEditorAbstractSpanResult span1;
1189     changeValue.originalSpans_.emplace_back(span1);
1190     RichEditorAbstractSpanResult span2;
1191     changeValue.originalSpans_.emplace_back(span2);
1192     RichEditorAbstractSpanResult& firstInfo = changeValue.originalSpans_.front();
1193     firstInfo.SetValue("");
1194     RichEditorAbstractSpanResult& lastInfo = changeValue.originalSpans_.back();
1195     lastInfo.SetValue("test123\n");
1196     int32_t lastLength = static_cast<int32_t>(StringUtils::ToWstring(lastInfo.GetValue()).length());
1197     lastInfo.SetEraseLength(lastLength - 1);
1198     std::string originalStr;
1199     int32_t originalPos = 0;
1200     auto ret = richEditorPattern->GetDelPartiallySpanItem(changeValue, originalStr, originalPos);
1201     auto it = richEditorPattern->spans_.front();
1202     EXPECT_NE(ret, nullptr);
1203     /**
1204      * @tc.steps: step2. change parameter and call function.
1205      */
1206     int32_t firstLength = static_cast<int32_t>(StringUtils::ToWstring(firstInfo.GetValue()).length());
1207     firstInfo.SetEraseLength(firstLength);
1208     lastLength = static_cast<int32_t>(StringUtils::ToWstring(lastInfo.GetValue()).length());
1209     lastInfo.SetEraseLength(lastLength);
1210     ret = richEditorPattern->GetDelPartiallySpanItem(changeValue, originalStr, originalPos);
1211     EXPECT_NE(ret, nullptr);
1212 }
1213 
1214 /**
1215  * @tc.name: TestRichEditorBeforeChangeText002
1216  * @tc.desc: test BeforeChangeText
1217  * @tc.type: FUNC
1218  */
1219 HWTEST_F(RichEditorPatternTestNg, TestRichEditorBeforeChangeText002, TestSize.Level1)
1220 {
1221     /**
1222      * @tc.steps: step1. declare and init variables and call function.
1223      */
1224     ASSERT_NE(richEditorNode_, nullptr);
1225     auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
1226     ASSERT_NE(richEditorPattern, nullptr);
1227     richEditorPattern->CreateNodePaintMethod();
1228     EXPECT_NE(richEditorPattern->contentMod_, nullptr);
1229     EXPECT_NE(richEditorPattern->overlayMod_, nullptr);
1230     RichEditorChangeValue changeValue;
1231     TextSpanOptions options;
1232     options.style = std::optional<TextStyle>(TextStyle(5));
1233     richEditorPattern->spans_.clear();
1234     auto ret = richEditorPattern->BeforeChangeText(changeValue, options);
1235     EXPECT_EQ(ret, true);
1236     /**
1237      * @tc.steps: step2. change parameters and call function.
1238      */
1239     options.offset = 200;
1240     ret = richEditorPattern->BeforeChangeText(changeValue, options);
1241     EXPECT_EQ(ret, true);
1242 }
1243 
1244 /**
1245  * @tc.name: FixMoveDownChange001
1246  * @tc.desc: test FixMoveDownChange
1247  * @tc.type: FUNC
1248  */
1249 HWTEST_F(RichEditorPatternTestNg, FixMoveDownChange001, TestSize.Level1)
1250 {
1251     /**
1252      * @tc.steps: step1. declare and init variables and call function.
1253      */
1254     ASSERT_NE(richEditorNode_, nullptr);
1255     auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
1256     ASSERT_NE(richEditorPattern, nullptr);
1257     richEditorPattern->CreateNodePaintMethod();
1258     EXPECT_NE(richEditorPattern->contentMod_, nullptr);
1259     EXPECT_NE(richEditorPattern->overlayMod_, nullptr);
1260     RichEditorChangeValue changeValue;
1261     RichEditorAbstractSpanResult span1;
1262     span1.SetValue("test123");
1263     changeValue.originalSpans_.emplace_back(span1);
1264     RichEditorAbstractSpanResult span2;
1265     span2.SetValue("test");
1266     changeValue.replacedSpans_.emplace_back(span2);
1267     RichEditorAbstractSpanResult& firstInfo = changeValue.originalSpans_.front();
1268     int32_t firstLength = static_cast<int32_t>(StringUtils::ToWstring(firstInfo.GetValue()).length());
1269     firstInfo.SetEraseLength(firstLength);
1270     auto spanIndex = firstInfo.GetSpanIndex();
1271     richEditorPattern->FixMoveDownChange(changeValue, 0);
1272     EXPECT_EQ(spanIndex, firstInfo.GetSpanIndex());
1273 }
1274 
1275 /**
1276  * @tc.name: BeforeUndo001
1277  * @tc.desc: test BeforeUndo
1278  * @tc.type: FUNC
1279  */
1280 HWTEST_F(RichEditorPatternTestNg, BeforeUndo001, TestSize.Level1)
1281 {
1282     /**
1283      * @tc.steps: step1. declare and init variables and call function.
1284      */
1285     ASSERT_NE(richEditorNode_, nullptr);
1286     auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
1287     ASSERT_NE(richEditorPattern, nullptr);
1288     richEditorPattern->CreateNodePaintMethod();
1289     EXPECT_NE(richEditorPattern->contentMod_, nullptr);
1290     EXPECT_NE(richEditorPattern->overlayMod_, nullptr);
1291     RichEditorChangeValue changeValue;
1292     int32_t innerPosition = 0;
1293     RichEditorPattern::OperationRecord record;
1294     record.addText = "test123\n";
1295     record.afterCaretPosition = 1;
1296     /**
1297      * @tc.steps: step2. change parameters and call function.
1298      */
1299     record.deleteCaretPostion = 0;
1300     richEditorPattern->BeforeUndo(changeValue, innerPosition, record);
1301     EXPECT_NE(innerPosition, 0);
1302     /**
1303      * @tc.steps: step3. change parameters and call function.
1304      */
1305     record.deleteCaretPostion = -1;
1306     record.deleteText = "test\n";
1307     richEditorPattern->BeforeUndo(changeValue, innerPosition, record);
1308     EXPECT_NE(innerPosition, 0);
1309 }
1310 
1311 /**
1312  * @tc.name: BeforeRedo001
1313  * @tc.desc: test BeforeRedo
1314  * @tc.type: FUNC
1315  */
1316 HWTEST_F(RichEditorPatternTestNg, BeforeRedo001, TestSize.Level1)
1317 {
1318     /**
1319      * @tc.steps: step1. declare and init variables and call function.
1320      */
1321     ASSERT_NE(richEditorNode_, nullptr);
1322     auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
1323     ASSERT_NE(richEditorPattern, nullptr);
1324     richEditorPattern->CreateNodePaintMethod();
1325     EXPECT_NE(richEditorPattern->contentMod_, nullptr);
1326     EXPECT_NE(richEditorPattern->overlayMod_, nullptr);
1327     RichEditorChangeValue changeValue;
1328     int32_t innerPosition = 0;
1329     RichEditorPattern::OperationRecord record;
1330     record.addText = "test123\n";
1331     record.beforeCaretPosition = 20;
1332     /**
1333      * @tc.steps: step2. change parameters and call function.
1334      */
1335     record.deleteCaretPostion = 0;
1336     richEditorPattern->BeforeRedo(changeValue, innerPosition, record);
1337     EXPECT_NE(innerPosition, 0);
1338     /**
1339      * @tc.steps: step3. change parameters and call function.
1340      */
1341     record.deleteCaretPostion = -1;
1342     record.deleteText = "test\n";
1343     richEditorPattern->BeforeRedo(changeValue, innerPosition, record);
1344     EXPECT_NE(innerPosition, 0);
1345 }
1346 
1347 /**
1348  * @tc.name: BeforeDrag001
1349  * @tc.desc: test BeforeDrag
1350  * @tc.type: FUNC
1351  */
1352 HWTEST_F(RichEditorPatternTestNg, BeforeDrag001, TestSize.Level1)
1353 {
1354     /**
1355      * @tc.steps: step1. declare and init variables and call function.
1356      */
1357     ASSERT_NE(richEditorNode_, nullptr);
1358     auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
1359     ASSERT_NE(richEditorPattern, nullptr);
1360     richEditorPattern->CreateNodePaintMethod();
1361     EXPECT_NE(richEditorPattern->contentMod_, nullptr);
1362     EXPECT_NE(richEditorPattern->overlayMod_, nullptr);
1363     struct UpdateSpanStyle typingStyle;
1364     TextStyle textStyle(5);
1365     richEditorPattern->SetTypingStyle(typingStyle, textStyle);
1366     RichEditorChangeValue changeValue;
1367     int32_t innerPosition = 0;
1368     RichEditorPattern::OperationRecord record;
1369     record.addText = "test123\n";
1370     record.beforeCaretPosition = 20;
1371     /**
1372      * @tc.steps: step2. change parameters and call function.
1373      */
1374     richEditorPattern->isDragSponsor_ = false;
1375     richEditorPattern->BeforeDrag(changeValue, innerPosition, record);
1376     EXPECT_NE(innerPosition, 0);
1377     /**
1378      * @tc.steps: step3. change parameters and call function.
1379      */
1380     richEditorPattern->isDragSponsor_ = true;
1381     richEditorPattern->BeforeDrag(changeValue, innerPosition, record);
1382     EXPECT_NE(innerPosition, 0);
1383 }
1384 
1385 /**
1386  * @tc.name: GetThumbnailCallback003
1387  * @tc.desc: test GetThumbnailCallback
1388  * @tc.type: FUNC
1389  */
1390 HWTEST_F(RichEditorPatternTestNg, GetThumbnailCallback003, TestSize.Level1)
1391 {
1392     ASSERT_NE(richEditorNode_, nullptr);
1393     auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
1394     ASSERT_NE(richEditorPattern, nullptr);
1395     richEditorPattern->GetThumbnailCallback()(Offset(1.0f, 10.0f));
1396     EXPECT_EQ(richEditorPattern->dragNode_, nullptr);
1397 }
1398 
1399 /**
1400  * @tc.name: InitScrollablePattern002
1401  * @tc.desc: test InitScrollablePattern and more.
1402  * @tc.type: FUNC
1403  */
1404 HWTEST_F(RichEditorPatternTestNg, InitScrollablePattern002, TestSize.Level1)
1405 {
1406     ASSERT_NE(richEditorNode_, nullptr);
1407     auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
1408     ASSERT_NE(richEditorPattern, nullptr);
1409 
1410     auto theme = AceType::MakeRefPtr<MockThemeManager>();
1411     PipelineBase::GetCurrentContext()->SetThemeManager(theme);
1412     EXPECT_CALL(*theme, GetTheme(_)).WillRepeatedly(Return(AceType::MakeRefPtr<RichEditorTheme>()));
1413 
1414     richEditorPattern->overlayMod_ = AceType::MakeRefPtr<TextOverlayModifier>();
1415     richEditorPattern->InitScrollablePattern();
1416     EXPECT_EQ(richEditorPattern->GetScrollBar(), true);
1417 
1418     Offset Offset = { 1, 4 };
1419     richEditorPattern->isTextChange_ = true;
1420     richEditorPattern->UpdateTextFieldManager(Offset, 1.0f);
1421     EXPECT_EQ(richEditorPattern->HasFocus(), false);
1422 
1423     richEditorPattern->isTextChange_ = false;
1424     richEditorPattern->UpdateTextFieldManager(Offset, 1.0f);
1425     EXPECT_EQ(richEditorPattern->HasFocus(), false);
1426 
1427     int32_t scroll_from_update = 1;
1428     richEditorPattern->scrollBar_ = nullptr;
1429     richEditorPattern->richTextRect_ = RectF(0, 4, 100, 61);
1430     richEditorPattern->contentRect_ = RectF(0, 7, 100, 60);
1431     richEditorPattern->UpdateScrollStateAfterLayout(true);
1432     EXPECT_TRUE(richEditorPattern->OnScrollCallback(10, scroll_from_update));
1433 
1434     auto offsetF = OffsetF(5.0f, 30.0f);
1435     richEditorPattern->isShowPlaceholder_ = false;
1436     richEditorPattern->MoveCaretToContentRect(offsetF, 40.0f);
1437     EXPECT_EQ(richEditorPattern->GetTextRect(), richEditorPattern->richTextRect_);
1438 
1439     auto offsetFtemp = OffsetF(5.0f, 6.0f);
1440     richEditorPattern->MoveCaretToContentRect(offsetFtemp, 40.0f);
1441     EXPECT_EQ(richEditorPattern->GetTextRect(), richEditorPattern->richTextRect_);
1442 }
1443 
1444 /**
1445  * @tc.name: GetPreviewTextDecorationColor001
1446  * @tc.desc: test GetPreviewTextDecorationColor
1447  * @tc.type: FUNC
1448  */
1449 HWTEST_F(RichEditorPatternTestNg, GetPreviewTextDecorationColor001, TestSize.Level1)
1450 {
1451     ASSERT_NE(richEditorNode_, nullptr);
1452     auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
1453     ASSERT_NE(richEditorPattern, nullptr);
1454     auto themeManager = AceType::MakeRefPtr<MockThemeManager>();
1455     ASSERT_NE(themeManager, nullptr);
1456     EXPECT_CALL(*themeManager, GetTheme(_)).WillRepeatedly(Return(AceType::MakeRefPtr<RichEditorTheme>()));
1457 
1458     auto oldThemeManager = PipelineBase::GetCurrentContext()->themeManager_;
1459     PipelineBase::GetCurrentContext()->themeManager_ = themeManager;
1460 
1461     RichEditorTheme richEditorTheme;
1462     EXPECT_EQ(richEditorPattern->GetPreviewTextDecorationColor(), richEditorTheme.GetPreviewUnderLineColor());
1463 
1464     auto property = richEditorPattern->GetLayoutProperty<RichEditorLayoutProperty>();
1465     ASSERT_NE(property, nullptr);
1466 
1467     property->UpdatePreviewTextStyle("normal");
1468     EXPECT_EQ(richEditorPattern->GetPreviewTextDecorationColor(), Color::TRANSPARENT);
1469 
1470     PipelineBase::GetCurrentContext()->themeManager_ = oldThemeManager;
1471 }
1472 
1473 /**
1474  * @tc.name: GetPreviewTextUnderlineWidth001
1475  * @tc.desc: test GetPreviewTextUnderlineWidth
1476  * @tc.type: FUNC
1477  */
1478 HWTEST_F(RichEditorPatternTestNg, GetPreviewTextUnderlineWidth001, TestSize.Level1)
1479 {
1480     ASSERT_NE(richEditorNode_, nullptr);
1481     auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
1482     ASSERT_NE(richEditorPattern, nullptr);
1483     auto themeManager = AceType::MakeRefPtr<MockThemeManager>();
1484     ASSERT_NE(themeManager, nullptr);
1485     EXPECT_CALL(*themeManager, GetTheme(_)).WillRepeatedly(Return(AceType::MakeRefPtr<RichEditorTheme>()));
1486 
1487     auto oldThemeManager = PipelineBase::GetCurrentContext()->themeManager_;
1488     PipelineBase::GetCurrentContext()->themeManager_ = themeManager;
1489 
1490     RichEditorTheme richEditorTheme;
1491     EXPECT_EQ(
1492         richEditorPattern->GetPreviewTextUnderlineWidth(), richEditorTheme.GetPreviewUnderlineWidth().ConvertToPx()
1493     );
1494 
1495     PipelineBase::GetCurrentContext()->themeManager_ = oldThemeManager;
1496 }
1497 
1498 /**
1499  * @tc.name: FromStyledString001
1500  * @tc.desc: test FromStyledString
1501  * @tc.type: FUNC
1502  */
1503 HWTEST_F(RichEditorPatternTestNg, FromStyledString001, TestSize.Level1)
1504 {
1505     ASSERT_NE(richEditorNode_, nullptr);
1506     auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
1507     ASSERT_NE(richEditorPattern, nullptr);
1508 
1509     Selection selection;
1510     RefPtr<SpanString> spanString;
1511 
1512     selection = richEditorPattern->FromStyledString(spanString).GetSelection();
1513     EXPECT_EQ(selection.selection[0], 0);
1514     EXPECT_EQ(selection.selection[1], 0);
1515 
1516     spanString = AceType::MakeRefPtr<SpanString>(INIT_VALUE_1);
1517     ASSERT_NE(spanString, nullptr);
1518     selection = richEditorPattern->FromStyledString(spanString).GetSelection();
1519     EXPECT_EQ(selection.selection[0], 0);
1520     EXPECT_EQ(selection.selection[1], INIT_VALUE_1.size());
1521 
1522     auto imageSpanItem = AceType::MakeRefPtr<NG::ImageSpanItem>();
1523     spanString->AppendSpanItem(imageSpanItem);
1524     selection = richEditorPattern->FromStyledString(spanString).GetSelection();
1525     EXPECT_EQ(selection.selection[0], 0);
1526     EXPECT_EQ(selection.selection[1], 0);
1527 }
1528 
1529 /**
1530  * @tc.name: ToGestureSpan001
1531  * @tc.desc: test ToGestureSpan
1532  * @tc.type: FUNC
1533  */
1534 HWTEST_F(RichEditorPatternTestNg, ToGestureSpan001, TestSize.Level1)
1535 {
1536     ASSERT_NE(richEditorNode_, nullptr);
1537     auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
1538     ASSERT_NE(richEditorPattern, nullptr);
1539 
1540     auto spanItem = AceType::MakeRefPtr<SpanItem>();
1541 
__anonfca374aa0602(GestureEvent& info) 1542     spanItem->onClick = [](GestureEvent& info) {};
__anonfca374aa0702(GestureEvent& info) 1543     spanItem->onLongPress = [](GestureEvent& info) {};
1544 
1545     auto spanString = AceType::MakeRefPtr<SpanString>(INIT_VALUE_1);
1546     ASSERT_NE(spanString, nullptr);
1547     auto start = spanItem->position - StringUtils::ToWstring(spanItem->content).length();
1548     auto end = spanItem->position;
1549     EXPECT_NE(spanString->ToGestureSpan(spanItem, start, end), nullptr);
1550 }
1551 
1552 /**
1553  * @tc.name: AddSpanByPasteData001
1554  * @tc.desc: test AddSpanByPasteData
1555  * @tc.type: FUNC
1556  */
1557 HWTEST_F(RichEditorPatternTestNg, AddSpanByPasteData001, TestSize.Level1)
1558 {
1559     ASSERT_NE(richEditorNode_, nullptr);
1560     auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
1561     ASSERT_NE(richEditorPattern, nullptr);
1562 
1563     auto spanString = AceType::MakeRefPtr<SpanString>(INIT_VALUE_1);
1564     ASSERT_NE(spanString, nullptr);
1565 
1566     richEditorPattern->SetSpanStringMode(true);
1567     richEditorPattern->AddSpanByPasteData(spanString);
1568     richEditorPattern->SetSpanStringMode(false);
1569 
1570     auto start = richEditorPattern->operationRecords_.size();
1571     richEditorPattern->AddSpanByPasteData(spanString);
1572     EXPECT_EQ(richEditorPattern->operationRecords_.size(), start + testNumber1);
1573 
1574     auto imageSpanItem = AceType::MakeRefPtr<NG::ImageSpanItem>();
1575     spanString->AppendSpanItem(imageSpanItem);
1576     start = richEditorPattern->operationRecords_.size();
1577     richEditorPattern->AddSpanByPasteData(spanString);
1578     EXPECT_EQ(richEditorPattern->operationRecords_.size(), start + testNumber2);
1579 }
1580 
1581 /**
1582  * @tc.name: FinishTextPreview001
1583  * @tc.desc: test FinishTextPreview
1584  * @tc.type: FUNC
1585  */
1586 HWTEST_F(RichEditorPatternTestNg, FinishTextPreview001, TestSize.Level1)
1587 {
1588     ASSERT_NE(richEditorNode_, nullptr);
1589     auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
1590     ASSERT_NE(richEditorPattern, nullptr);
1591 
1592     PreviewRange previewRange;
1593     previewRange.start = -1;
1594     previewRange.end = -1;
1595 
1596     richEditorPattern->InitPreviewText(PREVIEW_TEXT_VALUE1, previewRange);
1597     richEditorPattern->InitPreviewText(PREVIEW_TEXT_VALUE2, previewRange);
1598     richEditorPattern->FinishTextPreview();
1599 
1600     auto childFrameNode = FrameNode::CreateFrameNode(V2::BLANK_ETS_TAG, testFrameNodeId, richEditorPattern);
1601     ASSERT_NE(childFrameNode, nullptr);
1602     auto childSpanNode = AceType::MakeRefPtr<SpanNode>(testSpanNodeId);
1603     ASSERT_NE(childSpanNode, nullptr);
1604 
1605     do {
1606         auto newHost1 = richEditorPattern->GetHost();
1607         auto newHost2 = richEditorPattern->GetHost();
1608         ASSERT_EQ(newHost1, newHost2);
1609 
1610         newHost1->children_.emplace_back(childFrameNode);
1611         newHost1->children_.emplace_back(childSpanNode);
1612         ASSERT_EQ(newHost1, newHost2);
1613     } while (0);
1614 
1615     richEditorPattern->InitPreviewText(PREVIEW_TEXT_VALUE1, previewRange);
1616     richEditorPattern->InitPreviewText(PREVIEW_TEXT_VALUE2, previewRange);
1617     richEditorPattern->FinishTextPreview();
1618     EXPECT_EQ(richEditorPattern->previewTextRecord_.previewContent, "");
1619 }
1620 
1621 /**
1622  * @tc.name: OnBackPressed001
1623  * @tc.desc: test OnBackPressed
1624  * @tc.type: FUNC
1625  */
1626 HWTEST_F(RichEditorPatternTestNg, OnBackPressed001, TestSize.Level1)
1627 {
1628     ASSERT_NE(richEditorNode_, nullptr);
1629     auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
1630     ASSERT_NE(richEditorPattern, nullptr);
1631 
1632     EXPECT_EQ(richEditorPattern->OnBackPressed(), false);
1633 
1634     RectF rect(testNumber0, testNumber0, testNumber5, testNumber5);
1635     richEditorPattern->CreateHandles();
1636     richEditorPattern->textSelector_.Update(0, testNumber5);
1637     richEditorPattern->textSelector_.Update(0, testNumber5);
1638     richEditorPattern->selectOverlay_->OnHandleMoveDone(rect, true);
1639     EXPECT_EQ(richEditorPattern->OnBackPressed(), true);
1640 
__anonfca374aa0802() 1641     auto func = []() {};
1642     richEditorPattern->SetCustomKeyboard(func);
1643     richEditorPattern->RequestCustomKeyboard();
1644     EXPECT_EQ(richEditorPattern->OnBackPressed(), true);
1645 
1646     richEditorPattern->imeShown_ = true;
1647     EXPECT_EQ(richEditorPattern->OnBackPressed(), true);
1648 }
1649 
1650 /**
1651  * @tc.name: AddUdmfData001
1652  * @tc.desc: test AddUdmfData
1653  * @tc.type: FUNC
1654  */
1655 HWTEST_F(RichEditorPatternTestNg, AddUdmfData001, TestSize.Level1)
1656 {
1657     ASSERT_NE(richEditorNode_, nullptr);
1658     auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
1659     ASSERT_NE(richEditorPattern, nullptr);
1660 
1661     ResultObject resultObject;
1662     richEditorPattern->dragResultObjects_.emplace_back(resultObject);
1663 
1664     resultObject.type = SelectSpanType::TYPESYMBOLSPAN;
1665     richEditorPattern->dragResultObjects_.emplace_back(resultObject);
1666 
1667     resultObject.type = SelectSpanType::TYPEIMAGE;
1668     resultObject.valueString = INIT_VALUE_1;
1669     richEditorPattern->dragResultObjects_.emplace_back(resultObject);
1670 
1671     resultObject.type = SelectSpanType::TYPEIMAGE;
1672     resultObject.valueString.clear();
1673     richEditorPattern->dragResultObjects_.emplace_back(resultObject);
1674 
1675     resultObject.type = SelectSpanType::TYPEIMAGE;
1676     resultObject.valuePixelMap = PixelMap::CreatePixelMap(nullptr);
1677     ASSERT_NE(resultObject.valuePixelMap, nullptr);
1678     richEditorPattern->dragResultObjects_.emplace_back(resultObject);
1679 
1680     richEditorPattern->AddImageSpan(IMAGE_SPAN_OPTIONS_1);
1681 
1682     auto event = AceType::MakeRefPtr<Ace::DragEvent>();
1683     richEditorPattern->AddUdmfData(event);
1684     if (UdmfClient::GetInstance()->CreateUnifiedData()) {
1685         EXPECT_NE(event->GetData(), 0);
1686     } else {
1687         EXPECT_EQ(event->GetData(), 0);
1688     }
1689 }
1690 
1691 /**
1692  * @tc.name: ToBaselineOffsetSpan001
1693  * @tc.desc: test ToBaselineOffsetSpan
1694  * @tc.type: FUNC
1695  */
1696 HWTEST_F(RichEditorPatternTestNg, ToBaselineOffsetSpan001, TestSize.Level1)
1697 {
1698     ASSERT_NE(richEditorNode_, nullptr);
1699     auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
1700     ASSERT_NE(richEditorPattern, nullptr);
1701 
1702     auto spanItem = AceType::MakeRefPtr<SpanItem>();
1703     ASSERT_NE(spanItem, nullptr);
1704     spanItem->textLineStyle->UpdateBaselineOffset(Dimension(testNumber5, DimensionUnit::PX));
1705     auto spanString = AceType::MakeRefPtr<SpanString>(INIT_VALUE_1);
1706     ASSERT_NE(spanString, nullptr);
1707     auto start = spanItem->position - StringUtils::ToWstring(spanItem->content).length();
1708     auto end = spanItem->position;
1709     EXPECT_NE(spanString->ToBaselineOffsetSpan(spanItem, start, end), nullptr);
1710 }
1711 
1712 /**
1713  * @tc.name: ToTextShadowSpan001
1714  * @tc.desc: test ToTextShadowSpan
1715  * @tc.type: FUNC
1716  */
1717 HWTEST_F(RichEditorPatternTestNg, ToTextShadowSpan001, TestSize.Level1)
1718 {
1719     ASSERT_NE(richEditorNode_, nullptr);
1720     auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
1721     ASSERT_NE(richEditorPattern, nullptr);
1722 
1723     auto spanItem = AceType::MakeRefPtr<SpanItem>();
1724     ASSERT_NE(spanItem, nullptr);
1725 
1726     Shadow textShadow1 = Shadow();
1727     Shadow textShadow2 = Shadow();
1728     textShadow1.SetColor(Color::RED);
1729     textShadow2.SetColor(Color::WHITE);
1730     std::vector<Shadow> shadows { textShadow1, textShadow2 };
1731     spanItem->fontStyle->UpdateTextShadow(shadows);
1732     auto spanString = AceType::MakeRefPtr<SpanString>(INIT_VALUE_1);
1733     ASSERT_NE(spanString, nullptr);
1734     auto start = spanItem->position - StringUtils::ToWstring(spanItem->content).length();
1735     auto end = spanItem->position;
1736     EXPECT_NE(spanString->ToTextShadowSpan(spanItem, start, end), nullptr);
1737 }
1738 
1739 /**
1740  * @tc.name: GetPreviewTextRects001
1741  * @tc.desc: test GetPreviewTextRects
1742  * @tc.type: FUNC
1743  */
1744 HWTEST_F(RichEditorPatternTestNg, GetPreviewTextRects001, TestSize.Level1)
1745 {
1746     ASSERT_NE(richEditorNode_, nullptr);
1747     auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
1748     ASSERT_NE(richEditorPattern, nullptr);
1749 
1750     PreviewRange previewRange;
1751     previewRange.start = -1;
1752     previewRange.end = -1;
1753     richEditorPattern->InitPreviewText(PREVIEW_TEXT_VALUE1, previewRange);
1754 
1755     auto paragraph = MockParagraph::GetOrCreateMockParagraph();
1756     std::vector<RectF> firstRects { RectF(testNumber0, testNumber0, testNumber5, testNumber5) };
1757     EXPECT_CALL(*paragraph, GetTightRectsForRange(_, _, _)).WillRepeatedly(SetArgReferee<2>(firstRects));
1758     richEditorPattern->paragraphs_.AddParagraph(
1759         { .paragraph = paragraph, .start = testNumber0, .end = testNumber2 });
1760     richEditorPattern->paragraphs_.AddParagraph(
1761         { .paragraph = paragraph, .start = testNumber2, .end = testNumber4 });
1762 
1763     EXPECT_NE(richEditorPattern->GetPreviewTextRects().size(), 0);
1764 }
1765 
1766 /**
1767  * @tc.name: DeleteSelectOperation001
1768  * @tc.desc: test DeleteSelectOperation
1769  * @tc.type: FUNC
1770  */
1771 HWTEST_F(RichEditorPatternTestNg, DeleteSelectOperation001, TestSize.Level1)
1772 {
1773     ASSERT_NE(richEditorNode_, nullptr);
1774     auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
1775     ASSERT_NE(richEditorPattern, nullptr);
1776 
1777     RichEditorPattern::OperationRecord record;
1778     richEditorPattern->DeleteSelectOperation(&record);
1779 
1780     RectF rect(testNumber0, testNumber0, testNumber5, testNumber5);
1781     richEditorPattern->CreateHandles();
1782     richEditorPattern->textSelector_.Update(0, testNumber5);
1783     richEditorPattern->selectOverlay_->OnHandleMoveDone(rect, true);
1784 
1785     EXPECT_TRUE(richEditorPattern->selectOverlay_->SelectOverlayIsOn());
1786     richEditorPattern->DeleteSelectOperation(nullptr);
1787     EXPECT_FALSE(richEditorPattern->selectOverlay_->SelectOverlayIsOn());
1788 }
1789 
1790 /**
1791  * @tc.name: IsLineSeparatorInLast001
1792  * @tc.desc: test IsLineSeparatorInLast
1793  * @tc.type: FUNC
1794  */
1795 HWTEST_F(RichEditorPatternTestNg, IsLineSeparatorInLast001, TestSize.Level1)
1796 {
1797     ASSERT_NE(richEditorNode_, nullptr);
1798     auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
1799     ASSERT_NE(richEditorPattern, nullptr);
1800 
1801     auto spanNode = AceType::MakeRefPtr<SpanNode>(testSpanNodeId);
1802     ASSERT_NE(spanNode, nullptr);
1803     auto spanItem = spanNode->GetSpanItem();
1804     ASSERT_NE(spanNode, nullptr);
1805 
1806     spanItem->content = "ni\nhao";
1807     EXPECT_FALSE(richEditorPattern->IsLineSeparatorInLast(spanNode));
1808 
1809     spanItem->content = "nihao\n";
1810     EXPECT_TRUE(richEditorPattern->IsLineSeparatorInLast(spanNode));
1811 }
1812 
1813 /**
1814  * @tc.name: GetRichEditorController001
1815  * @tc.desc: test GetRichEditorController
1816  * @tc.type: FUNC
1817  */
1818 HWTEST_F(RichEditorPatternTestNg, GetRichEditorController001, TestSize.Level1)
1819 {
1820     RichEditorModelNG richEditorModel;
1821     richEditorModel.Create();
1822     auto controller = richEditorModel.GetRichEditorController();
1823     ASSERT_NE(controller, nullptr);
1824 }
1825 
1826 /**
1827  * @tc.name: BindSelectionMenu001
1828  * @tc.desc: test BindSelectionMenu
1829  * @tc.type: FUNC
1830  */
1831 HWTEST_F(RichEditorPatternTestNg, BindSelectionMenu001, TestSize.Level1)
1832 {
1833     RichEditorModelNG richEditorModel;
1834     richEditorModel.Create();
__anonfca374aa0902() 1835     std::function<void()> buildFunc = []() {
1836         callBack1 = 1;
1837         return;
1838     };
1839     TextSpanType textSpanType = TextSpanType::TEXT;
1840     TextResponseType textResponseType = TextResponseType::LONG_PRESS;
__anonfca374aa0a02() 1841     SelectMenuParam menuParam { .onAppear = [](int32_t, int32_t) {}, .onDisappear = []() {} };
1842     richEditorModel.BindSelectionMenu(textSpanType, textResponseType, buildFunc, menuParam);
1843 }
1844 
1845 /**
1846  * @tc.name: OnHandleMove001
1847  * @tc.desc: test OnHandleMove
1848  * @tc.type: FUNC
1849  */
1850 HWTEST_F(RichEditorPatternTestNg, OnHandleMove001, TestSize.Level1)
1851 {
1852     ASSERT_NE(richEditorNode_, nullptr);
1853     auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
1854     ASSERT_NE(richEditorPattern, nullptr);
1855     EXPECT_EQ(richEditorPattern->OnBackPressed(), false);
1856     RectF rect(testNumber0, testNumber0, testNumber5, testNumber5);
1857     richEditorPattern->CreateHandles();
1858     richEditorPattern->textSelector_.Update(0, testNumber5);
1859     richEditorPattern->selectOverlay_->OnHandleMove(rect, true);
1860 }
1861 
1862 /**
1863  * @tc.name: UpdateSelectorOnHandleMove001
1864  * @tc.desc: test UpdateSelectorOnHandleMove
1865  * @tc.type: FUNC
1866  */
1867 HWTEST_F(RichEditorPatternTestNg, UpdateSelectorOnHandleMove001, TestSize.Level1)
1868 {
1869     ASSERT_NE(richEditorNode_, nullptr);
1870     auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
1871     ASSERT_NE(richEditorPattern, nullptr);
1872     EXPECT_EQ(richEditorPattern->OnBackPressed(), false);
1873     auto offsetF = OffsetF(5.0f, 30.0f);
1874     RectF rect(testNumber0, testNumber0, testNumber5, testNumber5);
1875     richEditorPattern->CreateHandles();
1876     richEditorPattern->textSelector_.Update(0, testNumber5);
1877     richEditorPattern->selectOverlay_->UpdateSelectorOnHandleMove(offsetF, true);
1878 }
1879 } // namespace