1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "text_input_base.h"
17 
18 #include "core/components_ng/pattern/indexer/indexer_layout_property.h"
19 
20 namespace OHOS::Ace::NG {
21 
22 namespace {} // namespace
23 
24 class TextInputCursorTest : public TextInputBases {
25 public:
26 };
27 
28 /**
29  * @tc.name: CaretPosition001
30  * @tc.desc: Test caret position on TextFieldModelNG::CreateNode.
31  * @tc.type: FUNC
32  */
33 HWTEST_F(TextInputCursorTest, CaretPosition001, TestSize.Level1)
34 {
35     /**
36      * @tc.steps: Create Text filed node with default text and placeholder
37      */
38     CreateTextField(DEFAULT_TEXT);
39     GetFocus();
40 
41     /**
42      * @tc.expected: Current caret position is end of text
43      */
44     EXPECT_EQ(pattern_->GetCaretIndex(), static_cast<int>(DEFAULT_TEXT.size()));
45 
46     /**
47      * @tc.steps: Changed new text and remeasure and layout
48      */
49     pattern_->InsertValue("new");
50     FlushLayoutTask(frameNode_);
51 
52     /**
53      * @tc.expected: Current caret position is end of text
54      */
55     EXPECT_EQ(pattern_->GetCaretIndex(), static_cast<int>(DEFAULT_TEXT.size() + 3));
56 }
57 
58 /**
59  * @tc.name: CaretPosition002
60  * @tc.desc: Test caret position on SetType.
61  * @tc.type: FUNC
62  */
63 HWTEST_F(TextInputCursorTest, CaretPosition002, TestSize.Level1)
64 {
65     /**
66      * @tc.steps: Create Text filed node with default text and placeholder and set input type
67      */
68     std::string text = "openharmony@harmony.com+* ()0123456789";
69     std::vector<TestItem<TextInputType, int32_t>> testItems;
70     testItems.emplace_back(TextInputType::TEXT, text.length(), "TextInputType::TEXT");
71     testItems.emplace_back(TextInputType::NUMBER, 10, "TextInputType::NUMBER");
72     testItems.emplace_back(TextInputType::PHONE, 15, "TextInputType::PHONE");
73     testItems.emplace_back(TextInputType::EMAIL_ADDRESS, text.length() - 3, "TextInputType::EMAIL_ADDRESS");
74     testItems.emplace_back(TextInputType::VISIBLE_PASSWORD, text.length(), "TextInputType::VISIBLE_PASSWORD");
75     testItems.emplace_back(TextInputType::NUMBER_PASSWORD, 10, "TextInputType::NUMBER_PASSWORD");
76     testItems.emplace_back(TextInputType::SCREEN_LOCK_PASSWORD, text.length(), "TextInputType::SCREEN_LOCK_PASSWORD");
77 
78     /**
79      * @tc.expected: Check if the text filter rules for the input box are compliant
80      */
81     for (const auto& testItem : testItems) {
__anon916893860202(TextFieldModelNG& model) 82         CreateTextField(text, "", [testItem](TextFieldModelNG& model) { model.SetType(testItem.item); });
83         auto errorMessage = "InputType is " + testItem.error + ", text is " + pattern_->GetTextValue();
84         EXPECT_EQ(pattern_->GetCaretIndex(), testItem.expected) << errorMessage;
85         TearDown();
86     }
87 }
88 
89 /**
90  * @tc.name: CaretPosition003
91  * @tc.desc: Test caret position on SetCaretPosition
92  * @tc.type: FUNC
93  */
94 HWTEST_F(TextInputCursorTest, CaretPosition003, TestSize.Level1)
95 {
96     /**
97      * @tc.steps: Create Text filed node with default text
98      * @tc.expected: Cursor movement position matches the actual position
99      */
100     CreateTextField(DEFAULT_TEXT);
101     auto controller = pattern_->GetTextFieldController();
102     controller->CaretPosition(static_cast<int>(DEFAULT_TEXT.size() - 2));
103     EXPECT_EQ(pattern_->GetCaretIndex(), static_cast<int>(DEFAULT_TEXT.size() - 2));
104 }
105 
106 /**
107  * @tc.name: CaretPosition004
108  * @tc.desc: Test caret position on SetMaxLength
109  * @tc.type: FUNC
110  */
111 HWTEST_F(TextInputCursorTest, CaretPosition004, TestSize.Level1)
112 {
113     /**
114      * @tc.steps: Create Text filed node with default text and placeholder
115      * @tc.expected: Cursor movement position matches the actual position
116      */
__anon916893860302(TextFieldModelNG& model) 117     CreateTextField(DEFAULT_TEXT, "", [](TextFieldModelNG& model) { model.SetMaxLength(DEFAULT_TEXT.size() - 2); });
118     auto controller = pattern_->GetTextFieldController();
119     controller->CaretPosition(static_cast<int>(DEFAULT_TEXT.size() - 2));
120     EXPECT_EQ(pattern_->GetCaretIndex(), static_cast<int>(DEFAULT_TEXT.size() - 2));
121 }
122 
123 /**
124  * @tc.name: CaretPosition005
125  * @tc.desc: Test caret position on SetInputFilter.
126  * @tc.type: FUNC
127  */
128 HWTEST_F(TextInputCursorTest, CaretPosition005, TestSize.Level1)
129 {
130     /**
131      * @tc.steps: Initialize text and filter patterns
132      */
133     std::string text = "abcdefghABCDEFG0123456789";
134     std::vector<TestItem<std::string, int32_t>> testItems;
135     testItems.emplace_back("", StringUtils::ToWstring(text).length());
136     testItems.emplace_back("[0-9]", 10);
137     testItems.emplace_back("[A-Z]", 7);
138     testItems.emplace_back("[a-z]", 8);
139 
140     /**
141      * @tc.expected: Check if the text filter patterns for the input box are compliant
142      */
143     for (const auto& testItem : testItems) {
144         CreateTextField(
__anon916893860402(TextFieldModelNG& model) 145             text, "", [testItem](TextFieldModelNG& model) { model.SetInputFilter(testItem.item, nullptr); });
146         auto errorMessage = "InputType is " + testItem.item + ", text is " + pattern_->GetTextValue();
147         EXPECT_EQ(pattern_->GetCaretIndex(), testItem.expected) << errorMessage;
148         TearDown();
149     }
150 }
151 
152 /**
153  * @tc.name: CaretPosition005
154  * @tc.desc: Test input string at the cursor position
155  * @tc.type: FUNC
156  */
157 HWTEST_F(TextInputCursorTest, CaretPosition006, TestSize.Level1)
158 {
159     /**
160      * @tc.steps: Initialize text input and get select controller, update caret position and insert value
161      */
162     CreateTextField(DEFAULT_TEXT);
163     GetFocus();
164 
165     auto controller = pattern_->GetTextSelectController();
166     controller->UpdateCaretIndex(2);
167     pattern_->InsertValue("new");
168     FlushLayoutTask(frameNode_);
169 
170     /**
171      * @tc.expected: Check if the text and cursor position are correct
172      */
173     EXPECT_EQ(pattern_->GetTextValue(), "abnewcdefghijklmnopqrstuvwxyz");
174     EXPECT_EQ(controller->GetCaretIndex(), 5);
175 }
176 
177 /**
178  * @tc.name: CaretPosition006
179  * @tc.desc: Test stop edting input mode
180  * @tc.type: FUNC
181  */
182 HWTEST_F(TextInputCursorTest, CaretPosition007, TestSize.Level1)
183 {
184     /**
185      * @tc.steps: Initialize text input node
186      */
187     CreateTextField(DEFAULT_TEXT);
188 
189     /**
190      * @tc.expected: The cursor is neither blinking nor visible when unfocused
191      */
192     EXPECT_FALSE(pattern_->GetCursorVisible());
193 
194     /**
195      * @tc.steps: Manually trigger focus and perform measure and layout again
196      * @tc.expected: Check if the cursor is twinking
197      */
198     GetFocus();
199     EXPECT_TRUE(pattern_->GetCursorVisible());
200 
201     /**
202      * @tc.steps: Get text filed controller and stop editing
203      */
204     auto controller = pattern_->GetTextFieldController();
205     controller->StopEditing();
206     FlushLayoutTask(frameNode_);
207 
208     /**
209      * @tc.expected: Check if the cursor stop twinking
210      */
211     EXPECT_FALSE(pattern_->GetCursorVisible());
212 }
213 
214 /**
215  * @tc.name: OnTextChangedListenerCaretPosition001
216  * @tc.desc: Test the soft keyboard interface
217  * @tc.type: FUNC
218  */
219 HWTEST_F(TextInputCursorTest, OnTextChangedListenerCaretPosition001, TestSize.Level1)
220 {
221     /**
222      * @tc.steps: Initialize text input node and call text changed listener update edting value
223      */
224     CreateTextField(DEFAULT_TEXT);
225 
226     GetFocus();
227     TextEditingValue value;
228     TextSelection selection;
229     value.text = "new text";
230     selection.baseOffset = value.text.length();
231     value.selection = selection;
232     pattern_->UpdateEditingValue(std::make_shared<TextEditingValue>(value));
233     FlushLayoutTask(frameNode_);
234 
235     /**
236      * @tc.expected: Check if the new text and cursor position are correct
237      */
238     EXPECT_EQ(pattern_->GetTextValue().compare("new text"), 0);
239     EXPECT_EQ(pattern_->GetCaretIndex(), static_cast<int>(value.text.length()));
240 }
241 
242 /**
243  * @tc.name: OnTextChangedListenerCaretPosition002
244  * @tc.desc: Test the soft keyboard interface
245  * @tc.type: FUNC
246  */
247 HWTEST_F(TextInputCursorTest, OnTextChangedListenerCaretPosition002, TestSize.Level1)
248 {
249     /**
250      * @tc.steps: Initialize text input node and call delete backward
251      */
252     CreateTextField(DEFAULT_TEXT);
253 
254     GetFocus();
255     pattern_->DeleteBackward(5);
256     FlushLayoutTask(frameNode_);
257 
258     /**
259      * @tc.expected: Check if the new text and cursor position are correct
260      */
261     EXPECT_EQ(pattern_->GetTextValue().compare("abcdefghijklmnopqrstu"), 0) << "Text is " + pattern_->GetTextValue();
262     EXPECT_EQ(pattern_->GetCaretIndex(), static_cast<int>(DEFAULT_TEXT.length() - 5));
263 
264     /**
265      * @tc.steps: Move the cursor and then delete text
266      */
267     auto textFiledController = pattern_->GetTextFieldController();
268     textFiledController->CaretPosition(5);
269     pattern_->DeleteBackward(5);
270     FlushLayoutTask(frameNode_);
271 
272     /**
273      * @tc.expected: Check if the new text and cursor position are correct
274      */
275     EXPECT_EQ(pattern_->GetTextValue().compare("fghijklmnopqrstu"), 0) << "Text is " + pattern_->GetTextValue();
276     EXPECT_EQ(pattern_->GetCaretIndex(), 0);
277 
278     /**
279      * @tc.steps: Trigger a backspace key press that exceeds the length of the text
280      */
281     pattern_->DeleteBackward(MAX_BACKWARD_NUMBER);
282     FlushLayoutTask(frameNode_);
283 
284     /**
285      * @tc.expected: Check if the new text and cursor position are correct
286      */
287     EXPECT_EQ(pattern_->GetTextValue().compare("fghijklmnopqrstu"), 0) << "Text is " + pattern_->GetTextValue();
288     EXPECT_EQ(pattern_->GetCaretIndex(), 0);
289 }
290 
291 /**
292  * @tc.name: OnTextChangedListenerCaretPosition003
293  * @tc.desc: Test the soft keyboard interface
294  * @tc.type: FUNC
295  */
296 HWTEST_F(TextInputCursorTest, OnTextChangedListenerCaretPosition003, TestSize.Level1)
297 {
298     /**
299      * @tc.steps: Initialize insert text and expected values
300      */
301     CreateTextField(DEFAULT_TEXT);
302 
303     GetFocus();
304     pattern_->DeleteForward(5);
305     FlushLayoutTask(frameNode_);
306 
307     /**
308      * @tc.expected: Check if the new text and cursor position are correct
309      */
310     EXPECT_EQ(pattern_->GetTextValue().compare(DEFAULT_TEXT), 0);
311     EXPECT_EQ(pattern_->GetCaretIndex(), static_cast<int>(DEFAULT_TEXT.length()));
312 
313     /**
314      * @tc.steps: Move the cursor and then delete text forward.
315      */
316     auto textFiledController = pattern_->GetTextFieldController();
317     textFiledController->CaretPosition(5);
318     pattern_->DeleteForward(MAX_FORWARD_NUMBER);
319     FlushLayoutTask(frameNode_);
320 
321     /**
322      * @tc.expected: Check if the new text and cursor position are correct
323      */
324     EXPECT_EQ(pattern_->GetTextValue().compare("abcde"), 0) << "Text is " + pattern_->GetTextValue();
325     EXPECT_EQ(pattern_->GetCaretIndex(), 5) << "Caret position is " + std::to_string(pattern_->GetCaretIndex());
326 }
327 
328 /**
329  * @tc.name: OnTextChangedListenerCaretPosition004
330  * @tc.desc: Test the soft keyboard interface
331  * @tc.type: FUNC
332  */
333 HWTEST_F(TextInputCursorTest, OnTextChangedListenerCaretPosition004, TestSize.Level1)
334 {
335     /**
336      * @tc.steps: Initialize insert text and expected values when 'IsSelected() = false'
337      */
338     CreateTextField(DEFAULT_TEXT, DEFAULT_PLACE_HOLDER);
339     GetFocus();
340     pattern_->InsertValue("abc");
341     FlushLayoutTask(frameNode_);
342 
343     /**
344      * @tc.expected: Check if the new text and cursor position are correct
345      */
346     EXPECT_EQ(pattern_->GetTextValue().compare(DEFAULT_TEXT + "abc"), 0);
347     EXPECT_EQ(pattern_->GetCaretIndex(), DEFAULT_TEXT.length() + 3);
348 
349     /**
350      * @tc.steps: Move the cursor and then insert text forward.
351      */
352     auto textFiledController = pattern_->GetTextFieldController();
353     textFiledController->CaretPosition(0);
354     pattern_->InsertValue("abcde");
355     FlushLayoutTask(frameNode_);
356 
357     /**
358      * @tc.expected: Check if the new text and cursor position are correct
359      */
360     EXPECT_EQ(pattern_->GetTextValue().compare("abcde" + DEFAULT_TEXT + "abc"), 0);
361     EXPECT_EQ(pattern_->GetCaretIndex(), 5);
362 }
363 
364 /**
365  * @tc.name: OnTextChangedListenerCaretPosition005
366  * @tc.desc: Test the soft keyboard interface
367  * @tc.type: FUNC
368  */
369 HWTEST_F(TextInputCursorTest, OnTextChangedListenerCaretPosition005, TestSize.Level1)
370 {
371     /**
372      * @tc.steps: Initialize insert text and expected values
373      */
374     CreateTextField(DEFAULT_TEXT, DEFAULT_PLACE_HOLDER);
375     int32_t start = 5;
376     int32_t end = 10;
377     pattern_->HandleSetSelection(start, end, false);
378     FlushLayoutTask(frameNode_);
379 
380     /**
381      * @tc.expected: Check if the new handle positions are correct
382      */
383     EXPECT_EQ(pattern_->selectController_->GetFirstHandleInfo().index, start);
384     EXPECT_EQ(pattern_->selectController_->GetSecondHandleInfo().index, end)
385         << "Second index is " + std::to_string(pattern_->selectController_->GetSecondHandleInfo().index);
386 }
387 
388 /**
389  * @tc.name: OnTextChangedListenerCaretPosition006
390  * @tc.desc: Test the soft keyboard interface
391  * @tc.type: FUNC
392  */
393 HWTEST_F(TextInputCursorTest, OnTextChangedListenerCaretPosition006, TestSize.Level1)
394 {
395     /**
396      * @tc.steps: Initialize insert text and expected values
397      */
398     CreateTextField(DEFAULT_TEXT, DEFAULT_PLACE_HOLDER);
399     std::vector<std::int32_t> action = {
400         ACTION_SELECT_ALL,
401         ACTION_CUT,
402         ACTION_COPY,
403         ACTION_PASTE,
404     };
405     pattern_->HandleExtendAction(action[0]);
406     FlushLayoutTask(frameNode_);
407 
408     /**
409      * @tc.expected: Check if the new handle positions are correct
410      */
411     EXPECT_EQ(pattern_->selectController_->GetFirstHandleInfo().index, 0);
412     EXPECT_EQ(pattern_->selectController_->GetCaretRect().GetX(), 0);
413     EXPECT_EQ(pattern_->selectController_->GetSecondHandleInfo().index, 0)
414         << "Second index is " + std::to_string(pattern_->selectController_->GetSecondHandleInfo().index);
415 
416     /**
417      * @tc.steps: Move the handles and then cut text snippet.
418      */
419     int32_t start = 5;
420     int32_t end = 10;
421     pattern_->HandleSetSelection(start, end, false);
422     pattern_->HandleExtendAction(action[1]);
423     FlushLayoutTask(frameNode_);
424 
425     /**
426      * @tc.expected: Check if the new handle positions are correct
427      * Cut data hasn't simulated
428      */
429     EXPECT_EQ(pattern_->selectController_->GetFirstHandleInfo().index, 5);
430     EXPECT_EQ(pattern_->selectController_->GetSecondHandleInfo().index, 5)
431         << "Second index is " + std::to_string(pattern_->selectController_->GetSecondHandleInfo().index);
432     EXPECT_EQ(pattern_->GetTextSelectController()->GetCaretIndex(), 5);
433 }
434 
435 /**
436  * @tc.name: OnTextChangedListenerCaretPosition007
437  * @tc.desc: Test the soft keyboard interface
438  * @tc.type: FUNC
439  */
440 HWTEST_F(TextInputCursorTest, OnTextChangedListenerCaretPosition007, TestSize.Level1)
441 {
442     /**
443      * @tc.steps: steps1. Initialize text input and Move the handles and then cut text snippet.
444      */
445     int32_t start = 5;
446     int32_t end = 10;
447     std::string expectStr = "fghij";
448     std::vector<std::int32_t> action = {
449         ACTION_SELECT_ALL,
450         ACTION_CUT,
451         ACTION_COPY,
452         ACTION_PASTE,
453     };
__anon916893860502(const std::string& str) 454     auto callback = [expectStr](const std::string& str) { EXPECT_EQ(expectStr, str); };
__anon916893860602(TextFieldModel& model) 455     CreateTextField(DEFAULT_TEXT, DEFAULT_PLACE_HOLDER, [&](TextFieldModel& model) { model.SetOnCut(callback); });
456     pattern_->HandleSetSelection(start, end, false);
457     pattern_->HandleExtendAction(action[1]);
458     FlushLayoutTask(frameNode_);
459 
460     /**
461      * @tc.expected: Check if the new handle positions are correct
462      *               Verify the cut data
463      */
464     EXPECT_EQ(pattern_->selectController_->GetFirstHandleInfo().index, start);
465     EXPECT_EQ(pattern_->selectController_->GetSecondHandleInfo().index, start)
466         << "Second index is " + std::to_string(pattern_->selectController_->GetSecondHandleInfo().index);
467     EXPECT_EQ(pattern_->GetTextSelectController()->GetCaretIndex(), start);
468     EXPECT_EQ(pattern_->contentController_->GetTextValue().compare("abcdeklmnopqrstuvwxyz"), 0);
469 }
470 
471 /**
472  * @tc.name: OnTextChangedListenerCaretPosition008
473  * @tc.desc: Test the soft keyboard interface
474  * @tc.type: FUNC
475  */
476 HWTEST_F(TextInputCursorTest, OnTextChangedListenerCaretPosition008, TestSize.Level1)
477 {
478     /**
479      * @tc.steps: steps1. Initialize text input and Move the handles and then cut text snippet.
480      */
481     int32_t start = 5;
482     int32_t end = 10;
483     std::string expectStr = "fghij";
484     std::vector<std::int32_t> action = {
485         ACTION_SELECT_ALL,
486         ACTION_CUT,
487         ACTION_COPY,
488         ACTION_PASTE,
489     };
__anon916893860702(const std::string& str) 490     auto onCopy = [expectStr](const std::string& str) { EXPECT_EQ(expectStr, str); };
__anon916893860802(const std::string& str) 491     auto onPaste = [expectStr](const std::string& str) { EXPECT_EQ(expectStr, str); };
__anon916893860902(const std::string& str, NG::TextCommonEvent& event) 492     auto onPasteWithEvent = [expectStr](const std::string& str, NG::TextCommonEvent& event) {
493         EXPECT_EQ(expectStr, str); };
__anon916893860a02(TextFieldModel& model) 494     CreateTextField(DEFAULT_TEXT, DEFAULT_PLACE_HOLDER, [&](TextFieldModel& model) -> void {
495         model.SetOnCopy(onCopy);
496         model.SetOnPaste(onPaste);
497         model.SetOnPasteWithEvent(onPasteWithEvent);
498     });
499 
500     /**
501      * @tc.steps: Move the handles and then cut text snippet.
502      *            Verify the copy and paste data.
503      */
504     pattern_->HandleSetSelection(start, end, false);
505     pattern_->HandleExtendAction(action[2]);
506     pattern_->HandleExtendAction(action[3]);
507     FlushLayoutTask(frameNode_);
508     EXPECT_EQ(pattern_->GetTextValue().compare("abcdefghijklmnopqrstuvwxyz"), 0)
509         << "Text is " + pattern_->GetTextValue();
510 }
511 
512 /**
513  * @tc.name: OnHandleMove001
514  * @tc.desc: Test the clip board interface
515  * @tc.type: FUNC
516  */
517 HWTEST_F(TextInputCursorTest, OnHandleMove001, TestSize.Level1)
518 {
519     /**
520      * @tc.steps: steps1. Initialize text input and Move the handles and then do handle selection.
521      */
522     int32_t start = 5;
523     int32_t end = 10;
524     std::vector<CaretMoveIntent> select = { CaretMoveIntent::Left, CaretMoveIntent::Right, CaretMoveIntent::Up,
525         CaretMoveIntent::Down };
526     CreateTextField(DEFAULT_TEXT, DEFAULT_PLACE_HOLDER);
527 
528     /**
529      * @tc.steps: Move the handles and selection left.
530      *            Verify the selection data.
531      */
532     pattern_->HandleSetSelection(start, end, false);
533     pattern_->HandleSelect(select[0]);
534     FlushLayoutTask(frameNode_);
535     EXPECT_EQ(pattern_->selectController_->GetFirstHandleInfo().index, start);
536     EXPECT_EQ(pattern_->selectController_->GetSecondHandleInfo().index, end - 1);
537 
538     /**
539      * @tc.steps: Move the handles and selection right.
540      *            Verify the selection data.
541      */
542     pattern_->HandleSetSelection(start, end, false);
543     pattern_->HandleSelect(select[1]);
544     FlushLayoutTask(frameNode_);
545     EXPECT_EQ(pattern_->selectController_->GetFirstHandleInfo().index, start);
546     EXPECT_EQ(pattern_->selectController_->GetSecondHandleInfo().index, end + 1);
547 }
548 
549 /**
550  * @tc.name: OnHandleMove002
551  * @tc.desc: Test the clip board interface
552  * @tc.type: FUNC
553  */
554 HWTEST_F(TextInputCursorTest, OnHandleMove002, TestSize.Level1)
555 {
556     /**
557      * @tc.steps: steps1. Initialize text input and Move the handles and then do handle selection.
558      */
559     int32_t start = 5;
560     int32_t end = 10;
561     std::vector<CaretMoveIntent> select = { CaretMoveIntent::Left, CaretMoveIntent::Right, CaretMoveIntent::Up,
562         CaretMoveIntent::Down };
563     CreateTextField(DEFAULT_TEXT, DEFAULT_PLACE_HOLDER);
564 
565     /**
566      * @tc.steps: Move the handles and selection up.
567      *            Verify the selection data.
568      */
569     EXPECT_FALSE(pattern_->IsTextArea());
570     pattern_->HandleSetSelection(start, end, false);
571     pattern_->HandleSelect(select[2]);
572     FlushLayoutTask(frameNode_);
573     EXPECT_EQ(pattern_->selectController_->GetFirstHandleInfo().index, start);
574     EXPECT_EQ(pattern_->selectController_->GetSecondHandleInfo().index, end);
575 
576     /**
577      * @tc.steps: Move the handles and selection down.
578      *            Verify the selection data.
579      */
580     EXPECT_FALSE(pattern_->IsTextArea());
581     pattern_->HandleSetSelection(start, end, false);
582     pattern_->HandleSelect(select[3]);
583     FlushLayoutTask(frameNode_);
584     EXPECT_EQ(pattern_->selectController_->GetFirstHandleInfo().index, start);
585     EXPECT_EQ(pattern_->selectController_->GetSecondHandleInfo().index, end);
586 }
587 
588 /**
589  * @tc.name: OnHandleMove003
590  * @tc.desc: Test the clip board interface
591  * @tc.type: FUNC
592  */
593 HWTEST_F(TextInputCursorTest, OnHandleMove003, TestSize.Level1)
594 {
595     /**
596      * @tc.steps: steps1. Initialize text input and Move the handles and then do handle selection.
597      */
598     CreateTextField(DEFAULT_TEXT, DEFAULT_PLACE_HOLDER);
599 
600     /**
601      * @tc.steps: Move the handles and selection left word.
602      *            Verify the selection data.
603      */
604     auto textFiledController = pattern_->GetTextFieldController();
605     textFiledController->CaretPosition(5);
606     pattern_->HandleSelectionLeftWord();
607     FlushLayoutTask(frameNode_);
608     EXPECT_EQ(pattern_->selectController_->GetFirstHandleInfo().index, 5);
609     EXPECT_EQ(pattern_->selectController_->GetSecondHandleInfo().index, 0);
610 
611     /**
612      * @tc.steps: Move the handles and selection right word.
613      *            Verify the selection data.
614      */
615     pattern_->HandleSelectionRightWord();
616     FlushLayoutTask(frameNode_);
617     EXPECT_EQ(pattern_->selectController_->GetFirstHandleInfo().index, 5);
618     EXPECT_EQ(pattern_->selectController_->GetSecondHandleInfo().index, 26);
619 }
620 
621 /**
622  * @tc.name: OnHandleMove004
623  * @tc.desc: Test the clip board interface
624  * @tc.type: FUNC
625  */
626 HWTEST_F(TextInputCursorTest, OnHandleMove004, TestSize.Level1)
627 {
628     /**
629      * @tc.steps: steps1. Initialize text input and Move the handles and then do handle selection.
630      */
631     std::vector<CaretMoveIntent> select = { CaretMoveIntent::LeftWord, CaretMoveIntent::RightWord,
632         CaretMoveIntent::Home, CaretMoveIntent::End };
633     CreateTextField(DEFAULT_PLACE_HOLDER, DEFAULT_PLACE_HOLDER);
634 
635     /**
636      * @tc.steps: Move the handles and selection leftword "please".
637      *            Verify the selection data.
638      */
639     pattern_->HandleSetSelection(6, 6, false);
640     pattern_->HandleSelect(select[0]);
641     FlushLayoutTask(frameNode_);
642     EXPECT_EQ(pattern_->selectController_->GetFirstHandleInfo().index, 6);
643     EXPECT_EQ(pattern_->selectController_->GetSecondHandleInfo().index, 0);
644 
645     /**
646      * @tc.steps: Move the handles and selection rightword "input".
647      *            Verify the selection data.
648      */
649     pattern_->HandleSetSelection(7, 7, false);
650     pattern_->HandleSelect(select[1]);
651     FlushLayoutTask(frameNode_);
652     EXPECT_EQ(pattern_->selectController_->GetFirstHandleInfo().index, 7);
653     EXPECT_EQ(pattern_->selectController_->GetSecondHandleInfo().index, 13);
654 }
655 
656 /**
657  * @tc.name: OnHandleMove005
658  * @tc.desc: Test the clip board interface
659  * @tc.type: FUNC
660  */
661 HWTEST_F(TextInputCursorTest, OnHandleMove005, TestSize.Level1)
662 {
663     /**
664      * @tc.steps: steps1. Initialize text input and Move the handles and then do handle selection.
665      */
666     int32_t start = 6;
667     int32_t end = 6;
668     std::vector<CaretMoveIntent> select = { CaretMoveIntent::LeftWord, CaretMoveIntent::RightWord,
669         CaretMoveIntent::Home, CaretMoveIntent::End };
670     CreateTextField(DEFAULT_PLACE_HOLDER, DEFAULT_PLACE_HOLDER);
671 
672     /**
673      * @tc.steps: Move the handles and selection home.
674      *            Verify the selection data.
675      */
676     pattern_->HandleSetSelection(start, end, false);
677     pattern_->HandleSelect(select[2]);
678     FlushLayoutTask(frameNode_);
679     EXPECT_EQ(pattern_->selectController_->GetFirstHandleInfo().index, start);
680     EXPECT_EQ(pattern_->selectController_->GetSecondHandleInfo().index, 0);
681 
682     /**
683      * @tc.steps: Move the handles and selection end.
684      *            Verify the selection data.
685      */
686     pattern_->HandleSetSelection(start, end, false);
687     pattern_->HandleSelect(select[3]);
688     FlushLayoutTask(frameNode_);
689     EXPECT_EQ(pattern_->selectController_->GetFirstHandleInfo().index, start);
690     EXPECT_EQ(pattern_->selectController_->GetSecondHandleInfo().index, 22);
691 }
692 
693 /**
694  * @tc.name: CursonMoveLeftTest001
695  * @tc.desc: Test the curson move left
696  * @tc.type: FUNC
697  */
698 HWTEST_F(TextInputCursorTest, CursonMoveLeftTest001, TestSize.Level1)
699 {
700     /**
701      * @tc.steps: Initialize insert text and expected values
702      */
703     CreateTextField(DEFAULT_TEXT, DEFAULT_PLACE_HOLDER);
704     GetFocus();
705     auto ret = pattern_->CursorMoveLeft();
706     FlushLayoutTask(frameNode_);
707 
708     /**
709      * @tc.expected: In a situation where no text is selected, the movement is successfull
710      */
711     EXPECT_TRUE(ret);
712     EXPECT_EQ(pattern_->GetSelectMode(), SelectionMode::NONE);
713     EXPECT_EQ(pattern_->GetCaretIndex(), DEFAULT_TEXT.length() - 1);
714 
715     /**
716      * @tc.steps: In a situation where text is selected, the movement is successful
717      */
718     pattern_->HandleSetSelection(5, 5, false);
719     FlushLayoutTask(frameNode_);
720     ret = pattern_->CursorMoveLeft();
721 
722     /**
723      * @tc.expected: The cursor moves to the position after the selected text is deleted
724      */
725     EXPECT_TRUE(ret);
726     EXPECT_EQ(pattern_->GetSelectMode(), SelectionMode::NONE);
727     EXPECT_EQ(pattern_->GetCaretIndex(), 4)
728         << "Text is " + pattern_->GetTextValue() + ", CaretIndex is " + std::to_string(pattern_->GetCaretIndex());
729 }
730 
731 /**
732  * @tc.name: CursonMoveLeftWordTest001
733  * @tc.desc: Test the curson move left word
734  * @tc.type: FUNC
735  */
736 HWTEST_F(TextInputCursorTest, CursonMoveLeftWordTest001, TestSize.Level1)
737 {
738     /**
739      * @tc.steps: Initialize text input node and default text
740      */
741     CreateTextField(DEFAULT_TEXT, DEFAULT_PLACE_HOLDER);
742     GetFocus();
743 
744     auto ret = pattern_->CursorMoveLeftWord();
745 
746     /**
747      * @tc.expected: In a situation where no text is selected, the movement is successfull
748      */
749     EXPECT_TRUE(ret);
750     EXPECT_EQ(pattern_->GetSelectMode(), SelectionMode::NONE);
751     EXPECT_EQ(pattern_->GetCaretIndex(), 0)
752         << "Text is " + pattern_->GetTextValue() + ", CaretIndex is " + std::to_string(pattern_->GetCaretIndex());
753 
754     /**
755      * @tc.steps: In a situation where text is selected, the movement is successful
756      */
757     pattern_->HandleSetSelection(3, 5, false);
758     FlushLayoutTask(frameNode_);
759     ret = pattern_->CursorMoveLeftWord();
760 
761     /**
762      * @tc.expected: The cursor moves to the position after the selected text is deleted
763      */
764     EXPECT_TRUE(ret);
765     EXPECT_EQ(pattern_->GetSelectMode(), SelectionMode::NONE);
766     EXPECT_EQ(pattern_->GetCaretIndex(), 0)
767         << "Text is " + pattern_->GetTextValue() + ", CaretIndex is " + std::to_string(pattern_->GetCaretIndex());
768 }
769 
770 /**
771  * @tc.name: CursorMoveLineBeginTest001
772  * @tc.desc: Test the cursor move line begin
773  * @tc.type: FUNC
774  */
775 HWTEST_F(TextInputCursorTest, CursorMoveLineBeginTest001, TestSize.Level1)
776 {
777     /**
778      * @tc.steps: step1. Initialize empty text and CursorMoveLineBegin
779      */
780     CreateTextField();
781     GetFocus();
782     auto ret = pattern_->CursorMoveLineBegin();
783     EXPECT_TRUE(ret);
784 
785     /**
786      * @tc.steps: step2. Insert text and move line begin
787      */
788     pattern_->InsertValue("hello world");
789     FlushLayoutTask(frameNode_);
790     ret = pattern_->CursorMoveLineBegin();
791 
792     /**
793      * @tc.expected: Cursor move to the line head
794      */
795     EXPECT_TRUE(ret);
796     EXPECT_EQ(pattern_->GetSelectMode(), SelectionMode::NONE);
797     EXPECT_EQ(pattern_->GetCaretIndex(), 0)
798         << "Text is " + pattern_->GetTextValue() + ", CaretIndex is " + std::to_string(pattern_->GetCaretIndex());
799 
800     /**
801      * @tc.steps: In a situation where text is all selected, the movement is successful
802      */
803     pattern_->HandleSetSelection(0, 11, false);
804     FlushLayoutTask(frameNode_);
805     ret = pattern_->CursorMoveLineBegin();
806 
807     /**
808      * @tc.expected: Cursor move to the line head
809      */
810     EXPECT_TRUE(ret);
811     EXPECT_EQ(pattern_->GetSelectMode(), SelectionMode::NONE);
812     EXPECT_EQ(pattern_->GetCaretIndex(), 0)
813         << "Text is " + pattern_->GetTextValue() + ", CaretIndex is " + std::to_string(pattern_->GetCaretIndex());
814 }
815 
816 /**
817  * @tc.name: CursorMoveToParagraphBeginTest001
818  * @tc.desc: Test the cursor move paragraph begin
819  * @tc.type: FUNC
820  */
821 HWTEST_F(TextInputCursorTest, CursorMoveToParagraphBeginTest001, TestSize.Level1)
822 {
823     /**
824      * @tc.steps: step1. Initialize empty text and cursor move to paragraph begin
825      */
826     CreateTextField();
827     GetFocus();
828     auto ret = pattern_->CursorMoveToParagraphBegin();
829     EXPECT_TRUE(ret);
830 
831     /**
832      * @tc.steps: step2. Insert text
833      */
834     pattern_->InsertValue("hello world");
835     FlushLayoutTask(frameNode_);
836     ret = pattern_->CursorMoveToParagraphBegin();
837 
838     /**
839      * @tc.expected: Cursor move to the line head
840      */
841     EXPECT_TRUE(ret);
842     EXPECT_EQ(pattern_->GetSelectMode(), SelectionMode::NONE);
843     EXPECT_EQ(pattern_->GetCaretIndex(), 0)
844         << "Text is " + pattern_->GetTextValue() + ", CaretIndex is " + std::to_string(pattern_->GetCaretIndex());
845 }
846 
847 /**
848  * @tc.name: CursorMoveHomeTest001
849  * @tc.desc: Test the cursor move home
850  * @tc.type: FUNC
851  */
852 HWTEST_F(TextInputCursorTest, CursorMoveHomeTest001, TestSize.Level1)
853 {
854     /**
855      * @tc.steps: step1. Initialize empty text and curson move home
856      */
857     CreateTextField();
858     GetFocus();
859     auto ret = pattern_->CursorMoveHome();
860     EXPECT_TRUE(ret);
861 
862     /**
863      * @tc.steps: step2. Insert text
864      */
865     pattern_->InsertValue("hello world");
866     FlushLayoutTask(frameNode_);
867     ret = pattern_->CursorMoveHome();
868 
869     /**
870      * @tc.expected: Cursor move to the line head
871      */
872     EXPECT_TRUE(ret);
873     EXPECT_EQ(pattern_->GetSelectMode(), SelectionMode::NONE);
874     EXPECT_EQ(pattern_->GetCaretIndex(), 0)
875         << "Text is " + pattern_->GetTextValue() + ", CaretIndex is " + std::to_string(pattern_->GetCaretIndex());
876 }
877 
878 /**
879  * @tc.name: CursorMoveRightTest001
880  * @tc.desc: Test the cursor move right
881  * @tc.type: FUNC
882  */
883 HWTEST_F(TextInputCursorTest, CursorMoveRightTest001, TestSize.Level1)
884 {
885     /**
886      * @tc.steps: step1. Initialize default text and curson move right
887      */
888     CreateTextField(DEFAULT_TEXT);
889     GetFocus();
890     auto ret = pattern_->CursorMoveRight();
891 
892     /**
893      * @tc.expected: Unable to move
894      */
895     EXPECT_FALSE(ret);
896     EXPECT_EQ(pattern_->GetSelectMode(), SelectionMode::NONE);
897     EXPECT_EQ(pattern_->GetCaretIndex(), 26)
898         << "Text is " + pattern_->GetTextValue() + ", CaretIndex is " + std::to_string(pattern_->GetCaretIndex());
899 
900     /**
901      * @tc.steps: step2. Move cursor back to the line head and move right
902      */
903     ret = pattern_->CursorMoveLineBegin();
904     ret = pattern_->CursorMoveRight();
905 
906     /**
907      * @tc.expected: In a situation where no text is selected, the movement is successfull
908      */
909     EXPECT_TRUE(ret);
910     EXPECT_EQ(pattern_->GetSelectMode(), SelectionMode::NONE);
911     EXPECT_EQ(pattern_->GetCaretIndex(), 1)
912         << "Text is " + pattern_->GetTextValue() + ", CaretIndex is " + std::to_string(pattern_->GetCaretIndex());
913 
914     /**
915      * @tc.steps: step3. Select the text within coordinates 3 to 5 and move cursor right
916      */
917     pattern_->HandleSetSelection(5, 5, false);
918     FlushLayoutTask(frameNode_);
919     ret = pattern_->CursorMoveRight();
920 
921     /**
922      * @tc.expected: Select from 3 to 5, move the cursor to 6.
923      */
924     EXPECT_TRUE(ret);
925     EXPECT_EQ(pattern_->GetSelectMode(), SelectionMode::NONE);
926     EXPECT_EQ(pattern_->GetCaretIndex(), 6)
927         << "Text is " + pattern_->GetTextValue() + ", CaretIndex is " + std::to_string(pattern_->GetCaretIndex());
928 }
929 
930 /**
931  * @tc.name: CursorMoveRightWordTest001
932  * @tc.desc: Test the cursor move right word
933  * @tc.type: FUNC
934  */
935 HWTEST_F(TextInputCursorTest, CursorMoveRightWordTest001, TestSize.Level1)
936 {
937     /**
938      * @tc.steps: step1. Initialize default text and curson move right
939      */
940     CreateTextField(DEFAULT_TEXT);
941     GetFocus();
942     auto ret = pattern_->CursorMoveRightWord();
943 
944     /**
945      * @tc.expected: Moving to the right character when there is initial text
946      */
947     EXPECT_TRUE(ret);
948     EXPECT_EQ(pattern_->GetSelectMode(), SelectionMode::NONE);
949     EXPECT_EQ(pattern_->GetCaretIndex(), DEFAULT_TEXT.length())
950         << "Text is " + pattern_->GetTextValue() + ", CaretIndex is " + std::to_string(pattern_->GetCaretIndex());
951 
952     /**
953      * @tc.steps: steps2. Move to the left 2 times first.
954      */
955     ret = pattern_->CursorMoveLeft();
956     ret = pattern_->CursorMoveLeft();
957     FlushLayoutTask(frameNode_);
958 
959     /**
960      * @tc.expected:  the current text length - 2
961      */
962     EXPECT_EQ(pattern_->GetCaretIndex(), DEFAULT_TEXT.length() - 2)
963         << "Text is " + pattern_->GetTextValue() + ", CaretIndex is " + std::to_string(pattern_->GetCaretIndex());
964 
965     /**
966      * @tc.steps: steps3. Continue moving to the right word.
967      */
968     ret = pattern_->CursorMoveRightWord();
969 
970     /**
971      * @tc.expected: Moving to the right character when there is initial text
972      */
973     EXPECT_TRUE(ret);
974     EXPECT_EQ(pattern_->GetSelectMode(), SelectionMode::NONE);
975     EXPECT_EQ(pattern_->GetCaretIndex(), DEFAULT_TEXT.length())
976         << "Text is " + pattern_->GetTextValue() + ", CaretIndex is " + std::to_string(pattern_->GetCaretIndex());
977 
978     /**
979      * @tc.steps: step4. Move to the beginning of the line and select all text.
980      */
981     ret = pattern_->CursorMoveLineBegin();
982     pattern_->HandleSetSelection(0, DEFAULT_TEXT.length(), false);
983     FlushLayoutTask(frameNode_);
984     ret = pattern_->CursorMoveRightWord();
985 
986     /**
987      * @tc.expected: Moving to the right character when there is initial text
988      */
989     EXPECT_TRUE(ret);
990     EXPECT_EQ(pattern_->GetSelectMode(), SelectionMode::NONE);
991     EXPECT_EQ(pattern_->GetCaretIndex(), DEFAULT_TEXT.length())
992         << "Text is " + pattern_->GetTextValue() + ", CaretIndex is " + std::to_string(pattern_->GetCaretIndex());
993 }
994 
995 /**
996  * @tc.name: CursorMoveLineEndTest001
997  * @tc.desc: Test the cursor move line end
998  * @tc.type: FUNC
999  */
1000 HWTEST_F(TextInputCursorTest, CursorMoveLineEndTest001, TestSize.Level1)
1001 {
1002     /**
1003      * @tc.steps: step1. Initialize  text and move to the end of the line
1004      */
1005     CreateTextField(DEFAULT_TEXT);
1006     GetFocus();
1007     auto ret = pattern_->CursorMoveLineEnd();
1008 
1009     /**
1010      * @tc.expected: Moving to the right character when there is initial text
1011      */
1012     EXPECT_TRUE(ret);
1013     EXPECT_EQ(pattern_->GetSelectMode(), SelectionMode::NONE);
1014     EXPECT_EQ(pattern_->GetCaretIndex(), DEFAULT_TEXT.length())
1015         << "Text is " + pattern_->GetTextValue() + ", CaretIndex is " + std::to_string(pattern_->GetCaretIndex());
1016 
1017     /**
1018      * @tc.steps: step2. Move to the beginning of the line and select all text
1019      */
1020     pattern_->HandleSetSelection(0, DEFAULT_TEXT.length(), false);
1021     FlushLayoutTask(frameNode_);
1022     ret = pattern_->CursorMoveLineEnd();
1023 
1024     /**
1025      * @tc.expected: Moving to the right character when there is initial text
1026      */
1027     EXPECT_TRUE(ret);
1028     EXPECT_EQ(pattern_->GetSelectMode(), SelectionMode::NONE);
1029     EXPECT_EQ(pattern_->GetCaretIndex(), DEFAULT_TEXT.length())
1030         << "Text is " + pattern_->GetTextValue() + ", CaretIndex is " + std::to_string(pattern_->GetCaretIndex());
1031 }
1032 
1033 /**
1034  * @tc.name: CursorMoveToParagraphEndTest001
1035  * @tc.desc: Test the cursor move to pragraph to the end
1036  * @tc.type: FUNC
1037  */
1038 HWTEST_F(TextInputCursorTest, CursorMoveToParagraphEndTest001, TestSize.Level1)
1039 {
1040     /**
1041      * @tc.steps: Initialize text and move to the pargraph of the line
1042      */
1043     CreateTextField(DEFAULT_TEXT);
1044     GetFocus();
1045     auto ret = pattern_->CursorMoveToParagraphEnd();
1046     EXPECT_TRUE(ret);
1047 
1048     /**
1049      * @tc.expected: Moving to the paragraph end and check if cursor is on pargraph end
1050      */
1051     ret = pattern_->CursorMoveLeft();
1052     ret = pattern_->CursorMoveToParagraphEnd();
1053     EXPECT_TRUE(ret);
1054     EXPECT_EQ(pattern_->GetCaretIndex(), DEFAULT_TEXT.length())
1055         << "Text is " + pattern_->GetTextValue() + ", CaretIndex is " + std::to_string(pattern_->GetCaretIndex());
1056 }
1057 
1058 /**
1059  * @tc.name: CursorMoveEndTest001
1060  * @tc.desc: Test the cursor move end
1061  * @tc.type: FUNC
1062  */
1063 HWTEST_F(TextInputCursorTest, CursorMoveEndTest001, TestSize.Level1)
1064 {
1065     /**
1066      * @tc.steps: Initialize text and move to the pargraph of the line
1067      */
1068     CreateTextField(DEFAULT_TEXT);
1069     GetFocus();
1070     auto ret = pattern_->CursorMoveToParagraphEnd();
1071     EXPECT_TRUE(ret);
1072 
1073     /**
1074      * @tc.expected: Move left once first, and then move to the end
1075      *               Check if the cursor is at the end of the text.
1076      */
1077     ret = pattern_->CursorMoveLeft();
1078     ret = pattern_->CursorMoveEnd();
1079     EXPECT_TRUE(ret);
1080     EXPECT_EQ(pattern_->GetCaretIndex(), DEFAULT_TEXT.length());
1081 }
1082 
1083 /**
1084  * @tc.name: CursorMoveUpTest001
1085  * @tc.desc: Test the cursor move up
1086  * @tc.type: FUNC
1087  */
1088 HWTEST_F(TextInputCursorTest, CursorMoveUpTest001, TestSize.Level1)
1089 {
1090     /**
1091      * @tc.steps: Move up and down in a single-line text
1092      * @tc.expected: In single-line text, there is no up and down movement
1093      */
1094     CreateTextField(DEFAULT_TEXT);
1095     GetFocus();
1096     auto ret = pattern_->CursorMoveUp();
1097     EXPECT_TRUE(ret);
1098     ret = pattern_->CursorMoveDown();
1099     EXPECT_TRUE(ret);
1100 }
1101 
1102 /**
1103  * @tc.name: GetLeftTextOfCursor001
1104  * @tc.desc: Test get text of left cursor
1105  * @tc.type: FUNC
1106  */
1107 HWTEST_F(TextInputCursorTest, GetLeftTextOfCursor001, TestSize.Level1)
1108 {
1109     /**
1110      * @tc.steps: steps1. Create default text and retrieve the left 5 characters before the cursor
1111      */
1112     CreateTextField(DEFAULT_TEXT);
1113     GetFocus();
1114 
1115     /**
1116      * @tc.expected: Check if it equals "vwxyz"
1117      */
1118     EXPECT_EQ(StringUtils::Str16ToStr8(pattern_->GetLeftTextOfCursor(5)), "vwxyz");
1119 
1120     /**
1121      * @tc.steps: step2. Select the text from position 3 to 5
1122      */
1123     pattern_->HandleSetSelection(3, 5, false);
1124     FlushLayoutTask(frameNode_);
1125 
1126     /**
1127      * @tc.expected: Check if it equals "vwxyz"
1128      */
1129     EXPECT_EQ(StringUtils::Str16ToStr8(pattern_->GetLeftTextOfCursor(5)), "abc");
1130 }
1131 
1132 /**
1133  * @tc.name: GetRightTextOfCursor001
1134  * @tc.desc: Test get text of left cursor
1135  * @tc.type: FUNC
1136  */
1137 HWTEST_F(TextInputCursorTest, GetRightTextOfCursor001, TestSize.Level1)
1138 {
1139     /**
1140      * @tc.steps: steps1. Create default text and retrieve the left 5 characters before the cursor
1141      */
1142     CreateTextField(DEFAULT_TEXT);
1143     GetFocus();
1144 
1145     /**
1146      * @tc.expected: Check if it equals "vwxyz"
1147      */
1148     EXPECT_EQ(StringUtils::Str16ToStr8(pattern_->GetRightTextOfCursor(5)), "");
1149 
1150     /**
1151      * @tc.steps: step2. Select the text from position 3 to 5
1152      */
1153     pattern_->HandleSetSelection(3, 5, false);
1154     FlushLayoutTask(frameNode_);
1155 
1156     /**
1157      * @tc.expected: Check if it equals "vwxyz"
1158      */
1159     EXPECT_EQ(StringUtils::Str16ToStr8(pattern_->GetRightTextOfCursor(5)), "fghij");
1160 }
1161 
1162 /**
1163  * @tc.name: SetPreviewTextOperation001
1164  * @tc.desc: Test set preview text.
1165  * @tc.type: FUNC
1166  */
1167 HWTEST_F(TextInputCursorTest, SetPreviewTextOperation001, TestSize.Level1)
1168 {
1169     /**
1170      * @tc.steps: Create Text filed node with default text and placeholder
1171      */
1172     CreateTextField(DEFAULT_TEXT);
1173     GetFocus();
1174 
1175     /**
1176      * @tc.expected: Current caret position is end of text
1177      */
1178     EXPECT_EQ(pattern_->GetCaretIndex(), static_cast<int>(DEFAULT_TEXT.size()));
1179 
1180     /**
1181      * @tc.steps: Set caretPosition and call SetPreviewTextOperation
1182      */
1183     auto controller = pattern_->GetTextSelectController();
1184     controller->UpdateCaretIndex(0);
1185     pattern_->SetPreviewTextOperation(PREVIEW_ONE);
1186 
1187     /**
1188      * @tc.expected: Check if the text and cursor position are correct
1189      */
1190     EXPECT_EQ(pattern_->GetTextValue(), "niabcdefghijklmnopqrstuvwxyz");
1191     EXPECT_EQ(controller->GetCaretIndex(), 2);
1192 
1193     /**
1194      * @tc.steps:step2 continue call SetPreviewTextOperation
1195      */
1196     pattern_->SetPreviewTextOperation(PREVIEW_TWO);
1197     FlushLayoutTask(frameNode_);
1198 
1199     /**
1200      * @tc.expected: Check if the text and cursor position are correct
1201      */
1202     EXPECT_EQ(pattern_->GetTextValue(), "你abcdefghijklmnopqrstuvwxyz");
1203     EXPECT_EQ(controller->GetCaretIndex(), 1);
1204     pattern_->FinishTextPreview();
1205 }
1206 /**
1207  * @tc.name: SetPreviewTextOperation002
1208  * @tc.desc: Test set preview text.
1209  * @tc.type: FUNC
1210  */
1211 HWTEST_F(TextInputCursorTest, SetPreviewTextOperation002, TestSize.Level1)
1212 {
1213         /**
1214      * @tc.steps: Create Text filed node with default text and placeholder
1215      */
1216     CreateTextField(DEFAULT_TEXT);
1217     GetFocus();
1218 
1219     /**
1220      * @tc.expected: Current caret position is end of text
1221      */
1222     EXPECT_EQ(pattern_->GetCaretIndex(), static_cast<int>(DEFAULT_TEXT.size()));
1223 
1224     /**
1225      * @tc.steps: Set caretPosition and call SetPreviewTextOperation
1226      */
1227     auto controller = pattern_->GetTextSelectController();
1228     controller->UpdateCaretIndex(0);
1229     pattern_->SetPreviewTextOperation(PREVIEW_THR);
1230     FlushLayoutTask(frameNode_);
1231 
1232     /**
1233      * @tc.expected: Check if the text and cursor position are correct
1234      */
1235     EXPECT_EQ(pattern_->GetTextValue(), "hellofghijklmnopqrstuvwxyz");
1236     EXPECT_EQ(controller->GetCaretIndex(), 5);
1237 
1238     /**
1239      * @tc.steps:step2 continue call SetPreviewTextOperation
1240      */
1241     pattern_->SetPreviewTextOperation(PREVIEW_FOR);
1242     FlushLayoutTask(frameNode_);
1243 
1244     /**
1245      * @tc.expected: Check if the text and cursor position are correct
1246      */
1247     EXPECT_EQ(pattern_->GetTextValue(), "abllofghijklmnopqrstuvwxyz");
1248     EXPECT_EQ(controller->GetCaretIndex(), 2);
1249 }
1250 /**
1251  * @tc.name: CheckPreviewTextValidate001
1252  * @tc.desc: Test CheckPreviewTextValidate before set preview text.
1253  * @tc.type: FUNC
1254  */
1255 HWTEST_F(TextInputCursorTest, CheckPreviewTextValidate001, TestSize.Level1)
1256 {
1257     /**
1258      * @tc.steps: Create Text filed node with default text and placeholder
1259      */
1260     CreateTextField(DEFAULT_TEXT);
1261     GetFocus();
1262 
1263     /**
1264      * @tc.expected: Current caret position is end of text
1265      */
1266     EXPECT_EQ(pattern_->GetCaretIndex(), static_cast<int>(DEFAULT_TEXT.size()));
1267 
1268     /**
1269      * @tc.expected: call CheckPreviewTextValidate and check return true
1270      */
1271     EXPECT_EQ(pattern_->CheckPreviewTextValidate(PREVIEW_THR.text, PREVIEW_THR.range), 0);
1272 
1273     /**
1274      * @tc.steps:call invalid CheckPreviewTextValidate
1275      * @tc.expected: check return false
1276      */
1277     EXPECT_EQ(pattern_->CheckPreviewTextValidate(PREVIEW_BAD_DATA.text, PREVIEW_BAD_DATA.range), -1);
1278 
1279     /**
1280      * @tc.steps:Set select and call CheckPreviewTextValidate
1281      * @tc.expected: check return false
1282      */
1283     auto controller = pattern_->GetTextSelectController();
1284     controller->UpdateCaretIndex(5);
1285     pattern_->HandleSelectionLeftWord();
1286     FlushLayoutTask(frameNode_);
1287     EXPECT_EQ(pattern_->CheckPreviewTextValidate(PREVIEW_THR.text, PREVIEW_THR.range), -1);
1288 }
1289 
1290 /**
1291  * @tc.name: NeedDrawPreviewText001
1292  * @tc.desc: Test NeedDrawPreviewText before set preview text.
1293  * @tc.type: FUNC
1294  */
1295 HWTEST_F(TextInputCursorTest, NeedDrawPreviewText001, TestSize.Level1)
1296 {
1297     /**
1298      * @tc.steps: Create Text filed node with default text and placeholder
1299      */
1300     CreateTextField(DEFAULT_TEXT);
1301     GetFocus();
1302 
1303     /**
1304      * @tc.expected: Current caret position is end of text
1305      */
1306     EXPECT_EQ(pattern_->GetCaretIndex(), static_cast<int>(DEFAULT_TEXT.size()));
1307 
1308     /**
1309      * @tc.expected: call NeedDrawPreviewText and check return false
1310      */
1311     EXPECT_FALSE(pattern_->NeedDrawPreviewText());
1312 
1313     /**
1314      * @tc.steps:Set caretPosition and call NeedDrawPreviewText001
1315      * @tc.expected: check return true
1316      */
1317     auto controller = pattern_->GetTextSelectController();
1318     controller->UpdateCaretIndex(5);
1319     pattern_->SetPreviewTextOperation(PREVIEW_ONE);
1320     EXPECT_TRUE(pattern_->NeedDrawPreviewText());
1321     FlushLayoutTask(frameNode_);
1322 }
1323 
1324 /**
1325  * @tc.name: FinishTextPreview001
1326  * @tc.desc: Test FinishTextPreview after set preview text.
1327  * @tc.type: FUNC
1328  */
1329 HWTEST_F(TextInputCursorTest, FinishTextPreview001, TestSize.Level1)
1330 {
1331     /**
1332      * @tc.steps: Create Text filed node with default text and placeholder
1333      */
1334     CreateTextField(DEFAULT_TEXT);
1335     GetFocus();
1336 
1337     /**
1338      * @tc.expected: Current caret position is end of text
1339      */
1340     EXPECT_EQ(pattern_->GetCaretIndex(), static_cast<int>(DEFAULT_TEXT.size()));
1341 
1342     /**
1343      * @tc.steps:Set caretPosition and call SetPreviewTextOperation
1344      * @tc.expected: check GetIsPreviewText return true
1345      */
1346     auto controller = pattern_->GetTextSelectController();
1347     controller->UpdateCaretIndex(5);
1348     pattern_->SetPreviewTextOperation(PREVIEW_ONE);
1349     EXPECT_TRUE(pattern_->GetIsPreviewText());
1350     FlushLayoutTask(frameNode_);
1351 
1352     /**
1353      * @tc.steps: call InitEditingValueText value is ""
1354      * @tc.expected: check GetIsPreviewText return false
1355      */
1356     pattern_->InitEditingValueText("");
1357     EXPECT_FALSE(pattern_->GetIsPreviewText());
1358     FlushLayoutTask(frameNode_);
1359 }
1360 
1361 /**
1362  * @tc.name: FinishTextPreview002
1363  * @tc.desc: Test FinishTextPreview after set preview text.
1364  * @tc.type: FUNC
1365  */
1366 HWTEST_F(TextInputCursorTest, FinishTextPreview002, TestSize.Level1)
1367 {
1368     /**
1369      * @tc.steps: Create Text filed node with default text and placeholder
1370      */
1371     CreateTextField(DEFAULT_TEXT);
1372     GetFocus();
1373 
1374     /**
1375      * @tc.expected: Current caret position is end of text
1376      */
1377     EXPECT_EQ(pattern_->GetCaretIndex(), static_cast<int>(DEFAULT_TEXT.size()));
1378 
1379     /**
1380      * @tc.steps:Set caretPosition and call SetPreviewTextOperation
1381      * @tc.expected: check GetIsPreviewText return true
1382      */
1383     auto controller = pattern_->GetTextSelectController();
1384     controller->UpdateCaretIndex(5);
1385     pattern_->SetPreviewTextOperation(PREVIEW_ONE);
1386     EXPECT_TRUE(pattern_->GetIsPreviewText());
1387     FlushLayoutTask(frameNode_);
1388 
1389     /**
1390      * @tc.steps: call HandleBlurEvent, none operation for previewText
1391      * @tc.expected: check GetIsPreviewText return change to true from false
1392      */
1393     pattern_->HandleBlurEvent();
1394     EXPECT_TRUE(pattern_->GetIsPreviewText());
1395     FlushLayoutTask(frameNode_);
1396 }
1397 
1398 /**
1399  * @tc.name: FinishTextPreview003
1400  * @tc.desc: Test FinishTextPreview after set preview text.
1401  * @tc.type: FUNC
1402  */
1403 HWTEST_F(TextInputCursorTest, FinishTextPreview003, TestSize.Level1)
1404 {
1405     /**
1406      * @tc.steps: Create Text filed node with default text and placeholder
1407      */
1408     CreateTextField(DEFAULT_TEXT);
1409     GetFocus();
1410 
1411     /**
1412      * @tc.expected: Current caret position is end of text
1413      */
1414     EXPECT_EQ(pattern_->GetCaretIndex(), static_cast<int>(DEFAULT_TEXT.size()));
1415 
1416     /**
1417      * @tc.steps:Set caretPosition and call SetPreviewTextOperation
1418      * @tc.expected: check GetIsPreviewText return true
1419      */
1420     auto controller = pattern_->GetTextSelectController();
1421     controller->UpdateCaretIndex(5);
1422     pattern_->SetPreviewTextOperation(PREVIEW_ONE);
1423     EXPECT_TRUE(pattern_->GetIsPreviewText());
1424     FlushLayoutTask(frameNode_);
1425 
1426     /**
1427      * @tc.steps: call InsertValueOperation
1428      * @tc.expected: check GetIsPreviewText return false
1429      */
1430     SourceAndValueInfo info;
1431     info.insertValue = HELLO_TEXT;
1432     pattern_->InsertValue(info.insertValue, info.isIME);
1433     EXPECT_TRUE(pattern_->inputOperations_.front() == InputOperation::SET_PREVIEW_TEXT);
1434     FlushLayoutTask(frameNode_);
1435 }
1436 
1437 /**
1438  * @tc.name: FinishTextPreviewOperation001
1439  * @tc.desc: Test FinishTextPreview after set preview text.
1440  * @tc.type: FUNC
1441  */
1442 HWTEST_F(TextInputCursorTest, FinishTextPreviewOperation001, TestSize.Level1)
1443 {
1444     /**
1445      * @tc.steps: Create Text filed node with default text and placeholder
1446      */
1447     CreateTextField(DEFAULT_TEXT);
1448     GetFocus();
1449 
1450     /**
1451      * @tc.expected: Current caret position is end of text
1452      */
1453     EXPECT_EQ(pattern_->GetCaretIndex(), static_cast<int>(DEFAULT_TEXT.size()));
1454 
1455     /**
1456      * @tc.steps:Set caretPosition and call SetPreviewTextOperation
1457      * @tc.expected: check GetIsPreviewText return true
1458      */
1459     auto controller = pattern_->GetTextSelectController();
1460     controller->UpdateCaretIndex(5);
1461     pattern_->SetPreviewTextOperation(PREVIEW_ONE);
1462     EXPECT_TRUE(pattern_->GetIsPreviewText());
1463     FlushLayoutTask(frameNode_);
1464 
1465     /**
1466      * @tc.steps: call FinishTextPreviewOperation
1467      * @tc.expected: check GetIsPreviewText return false
1468      */
1469     pattern_->FinishTextPreviewOperation();
1470     EXPECT_FALSE(pattern_->GetIsPreviewText());
1471     FlushLayoutTask(frameNode_);
1472 }
1473 
1474 /**
1475  * @tc.name: TextInputLineBreakStrategy001
1476  * @tc.desc: test testInput text lineBreakStrategy
1477  * @tc.type: FUNC
1478  */
1479 HWTEST_F(TextInputCursorTest, TextInputLineBreakStrategy001, TestSize.Level1)
1480 {
1481     /**
1482      * @tc.step1: Create Text filed node
1483      * @tc.expected: style is Inline
1484      */
__anon916893860b02(TextFieldModelNG model) 1485     CreateTextField(DEFAULT_TEXT, "", [](TextFieldModelNG model) {
1486         model.SetInputStyle(DEFAULT_INPUT_STYLE);
1487     });
1488 
1489     /**
1490      * @tc.step: step2. Set lineBreakStrategy GREEDY
1491      */
1492     layoutProperty_->UpdateLineBreakStrategy(LineBreakStrategy::GREEDY);
1493     frameNode_->MarkModifyDone();
1494     EXPECT_EQ(layoutProperty_->GetLineBreakStrategy(), LineBreakStrategy::GREEDY);
1495 
1496     /**
1497      * @tc.step: step3. Set lineBreakStrategy HIGH_QUALITY
1498      */
1499     layoutProperty_->UpdateLineBreakStrategy(LineBreakStrategy::HIGH_QUALITY);
1500     frameNode_->MarkModifyDone();
1501     EXPECT_EQ(layoutProperty_->GetLineBreakStrategy(), LineBreakStrategy::HIGH_QUALITY);
1502 
1503     /**
1504      * @tc.step: step4. Set lineBreakStrategy BALANCED
1505      */
1506     layoutProperty_->UpdateLineBreakStrategy(LineBreakStrategy::BALANCED);
1507     frameNode_->MarkModifyDone();
1508     EXPECT_EQ(layoutProperty_->GetLineBreakStrategy(), LineBreakStrategy::BALANCED);
1509 }
1510 
1511 HWTEST_F(TextInputCursorTest, OnFocusNodeChange_001, TestSize.Level0)
1512 {
1513     /**
1514      * @tc.steps: step1. create target node.
1515      */
1516     CreateTextField();
1517     auto textFieldNode_1 = FrameNode::GetOrCreateFrameNode(V2::TEXTINPUT_ETS_TAG,
1518         ElementRegister::GetInstance()->MakeUniqueId(),
__anon916893860c02() 1519         []() { return AceType::MakeRefPtr<TextFieldPattern>(); });
1520     auto textFieldNode_2 = FrameNode::GetOrCreateFrameNode(V2::TEXTINPUT_ETS_TAG,
1521         ElementRegister::GetInstance()->MakeUniqueId(),
__anon916893860d02() 1522         []() { return AceType::MakeRefPtr<TextFieldPattern>(); });
1523     textFieldNode_1->SetParent(frameNode_);
1524     textFieldNode_2->SetParent(frameNode_);
1525 
1526     auto focusHub_1 = textFieldNode_1->GetFocusHub();
1527     ASSERT_NE(focusHub_1, nullptr);
1528     RefPtr<TextFieldPattern> pattern_1 = textFieldNode_1->GetPattern<TextFieldPattern>();
1529     auto focusHub_2 = textFieldNode_2->GetFocusHub();
1530     RefPtr<TextFieldPattern> pattern_2 = textFieldNode_2->GetPattern<TextFieldPattern>();
1531 
1532     focusHub_1->currentFocus_ = true;
1533     pattern_1->HandleFocusEvent();
1534 
1535     FlushLayoutTask(frameNode_);
1536     FlushLayoutTask(textFieldNode_1);
1537     FlushLayoutTask(textFieldNode_2);
1538     EXPECT_TRUE(pattern_1->needToRequestKeyboardInner_);
1539     EXPECT_FALSE(pattern_2->needToRequestKeyboardInner_);
1540 
1541     focusHub_1->currentFocus_ = false;
1542     focusHub_2->currentFocus_ = true;
1543     pattern_1->HandleBlurEvent();
1544     pattern_2->HandleFocusEvent();
1545     FlushLayoutTask(frameNode_);
1546     EXPECT_FALSE(pattern_1->needToRequestKeyboardInner_);
1547     EXPECT_TRUE(pattern_2->needToRequestKeyboardInner_);
1548 }
1549 } // namespace OHOS::Ace::NG