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 namespace OHOS::Ace::NG {
19
20 namespace {} // namespace
21
22 class TextAdjustObject : public TextInputBases {
23 protected:
24 static void InitAdjustObject(MockDataDetectorMgr& mockDataDetectorMgr);
25 };
26 class TextFieldResponseAreaTest : public TextInputBases {
27 public:
28 };
29 class TextFieldControllerTest : public TextInputBases {
30 public:
31 };
32
InitAdjustObject(MockDataDetectorMgr & mockDataDetectorMgr)33 void TextAdjustObject::InitAdjustObject(MockDataDetectorMgr& mockDataDetectorMgr)
34 {
35 EXPECT_CALL(mockDataDetectorMgr, GetCursorPosition(_, _))
36 .WillRepeatedly([](const std::string& text, int8_t /* offset */) -> int8_t {
37 if (text.empty()) {
38 return DEFAULT_RETURN_VALUE;
39 }
40 if (text.length() <= WORD_LIMIT_LEN) {
41 return WORD_LIMIT_RETURN;
42 }
43 return BEYOND_LIMIT_RETURN;
44 });
45
46 EXPECT_CALL(mockDataDetectorMgr, GetWordSelection(_, _))
47 .WillRepeatedly([](const std::string& text, int8_t /* offset */) -> std::vector<int8_t> {
48 if (text.empty()) {
49 return std::vector<int8_t> { -1, -1 };
50 }
51
52 if (text.length() <= WORD_LIMIT_LEN) {
53 return std::vector<int8_t> { 2, 3 };
54 }
55 return std::vector<int8_t> { 0, 2 };
56 });
57 }
58
59 /*
60 * @tc.name: AdjustWordCursorAndSelect01
61 * @tc.desc: Test .adjust word cursor and select(true)
62 * @tc.type: FUNC
63 */
64 HWTEST_F(TextAdjustObject, AdjustWordCursorAndSelect01, TestSize.Level1)
65 {
66 using namespace std::chrono;
67 /**
68 * @tc.steps: step1. Initialize text input "hello"
69 */
70 CreateTextField(HELLO_TEXT);
71 pattern_->selectController_->lastAiPosTimeStamp_ = high_resolution_clock::now();
72 pattern_->lastClickTimeStamp_ = pattern_->selectController_->lastAiPosTimeStamp_ + seconds(2);
73
74 MockDataDetectorMgr mockDataDetectorMgr;
75 InitAdjustObject(mockDataDetectorMgr);
76
77 std::string content = pattern_->contentController_->GetTextValue();
78 int32_t pos = 3;
79 mockDataDetectorMgr.AdjustCursorPosition(
80 pos, content, pattern_->selectController_->lastAiPosTimeStamp_, pattern_->lastClickTimeStamp_);
81 EXPECT_EQ(pos, 2);
82
83 int32_t start = 1;
84 int32_t end = 3;
85 mockDataDetectorMgr.AdjustWordSelection(pos, content, start, end);
86 EXPECT_EQ(start, 2);
87 EXPECT_EQ(end, 3);
88 /**
89 * @tc.steps: step2. assign text as default text
90 */
91 pos = 1;
92 pattern_->contentController_->SetTextValue(DEFAULT_TEXT);
93 content = pattern_->contentController_->GetTextValue();
94 mockDataDetectorMgr.AdjustCursorPosition(
95 pos, content, pattern_->selectController_->lastAiPosTimeStamp_, pattern_->lastClickTimeStamp_);
96 EXPECT_EQ(pos, 4);
97
98 start = 1;
99 end = 3;
100 mockDataDetectorMgr.AdjustWordSelection(pos, content, start, end);
101 EXPECT_EQ(start, 0);
102 EXPECT_EQ(end, 2);
103
104 /**
105 * @tc.steps: step3. assign text as empty
106 */
107 pos = 2;
108 pattern_->contentController_->Reset();
109 content = pattern_->contentController_->GetTextValue();
110 mockDataDetectorMgr.AdjustCursorPosition(
111 pos, content, pattern_->selectController_->lastAiPosTimeStamp_, pattern_->lastClickTimeStamp_);
112 EXPECT_EQ(pos, -1);
113
114 start = 1;
115 end = 3;
116 mockDataDetectorMgr.AdjustWordSelection(pos, content, start, end);
117 EXPECT_EQ(start, -1);
118 EXPECT_EQ(end, -1);
119 }
120
121 /**
122 * @tc.name: TextFieldResponseArea001
123 * @tc.desc: test password response area show/hide.
124 * @tc.type: FUNC
125 */
126 HWTEST_F(TextFieldResponseAreaTest, TextFieldResponseArea001, TestSize.Level1)
127 {
128 PasswordIcon myIcon = {
129 .showResult = "1",
130 .hideResult = "2",
131 .showBundleName = "1",
132 .hideBundleName = "2",
133 .showModuleName = "1",
134 .hideModuleName = "2",
135 };
__anon3e9d21400402(TextFieldModelNG model) 136 CreateTextField(DEFAULT_TEXT, "", [myIcon](TextFieldModelNG model) {
137 model.SetType(TextInputType::VISIBLE_PASSWORD);
138 model.SetShowPasswordIcon(true);
139 model.SetPasswordIcon(myIcon);
140 model.SetCleanNodeStyle(CleanNodeStyle::CONSTANT);
141 model.SetIsShowCancelButton(true);
142 });
143 auto passwordArea = AceType::MakeRefPtr<PasswordResponseArea>(pattern_, false);
144 passwordArea->InitResponseArea();
145 EXPECT_EQ(passwordArea->GetCurrentSourceInfo()->GetSrc(), "1");
146 passwordArea->SetObscured(true);
147 EXPECT_EQ(passwordArea->GetCurrentSourceInfo()->GetSrc(), "2");
148 }
149
150 /**
151 * @tc.name: TextFieldResponseArea002
152 * @tc.desc: test change response area not effect response state.
153 * @tc.type: FUNC
154 */
155 HWTEST_F(TextFieldResponseAreaTest, TextFieldResponseArea002, TestSize.Level1)
156 {
157 PasswordIcon myIcon = {
158 .showResult = "1",
159 .hideResult = "2",
160 .showBundleName = "1",
161 .hideBundleName = "2",
162 .showModuleName = "1",
163 .hideModuleName = "2",
164 };
__anon3e9d21400502(TextFieldModelNG model) 165 CreateTextField(DEFAULT_TEXT, "", [myIcon](TextFieldModelNG model) {
166 model.SetType(TextInputType::VISIBLE_PASSWORD);
167 model.SetShowPasswordIcon(true);
168 model.SetPasswordIcon(myIcon);
169 model.SetCleanNodeStyle(CleanNodeStyle::CONSTANT);
170 model.SetIsShowCancelButton(true);
171 });
172 RefPtr<TextInputResponseArea> responseArea = AceType::MakeRefPtr<CleanNodeResponseArea>(pattern_);
173 auto convertedArea = AceType::DynamicCast<CleanNodeResponseArea>(responseArea);
174 convertedArea->UpdateCleanNode(true);
175 EXPECT_EQ(convertedArea->IsShow(), true);
176 responseArea = AceType::MakeRefPtr<PasswordResponseArea>(pattern_, true);
177 auto passwordArea = AceType::DynamicCast<PasswordResponseArea>(responseArea);
178 passwordArea->InitResponseArea();
179 EXPECT_EQ(passwordArea->GetCurrentSourceInfo()->GetSrc(), "2");
180 }
181
182
183 /**
184 * @tc.name: LayoutProperty001
185 * @tc.desc: Test attrs on TextInput
186 * @tc.type: FUNC
187 */
188 HWTEST_F(TextFieldControllerTest, LayoutProperty001, TestSize.Level1)
189 {
190 /**
191 * @tc.steps: Create Text filed node with default attrs
192 */
__anon3e9d21400602(TextFieldModelNG model) 193 CreateTextField(DEFAULT_TEXT, "", [](TextFieldModelNG model) {
194 model.SetWidthAuto(true);
195 model.SetType(TextInputType::TEXT);
196 model.SetPlaceholderColor(DEFAULT_PLACE_HODER_COLOR);
197 model.SetTextColor(DEFAULT_TEXT_COLOR);
198 model.SetEnterKeyType(DEFAULT_ENTER_KEY_TYPE);
199 model.SetTextAlign(DEFAULT_TEXT_ALIGN);
200 model.SetCaretColor(DEFAULT_CARET_COLOR);
201 model.SetCaretStyle(DEFAULT_CARET_STYLE);
202 model.SetSelectedBackgroundColor(DEFAULT_SELECTED_BACKFROUND_COLOR);
203 model.SetMaxLength(DEFAULT_MAX_LENGTH);
204 model.SetMaxLines(DEFAULT_MAX_LINES);
205 model.SetFontSize(DEFAULT_FONT_SIZE);
206 model.SetFontWeight(DEFAULT_FONT_WEIGHT);
207 model.SetTextColor(DEFAULT_TEXT_COLOR);
208 model.SetInputFilter(DEFAULT_INPUT_FILTER, nullptr);
209 model.SetCopyOption(DEFAULT_COPY_OPTIONS);
210 model.SetBarState(DEFAULT_DISPLAY_MODE);
211 model.SetInputStyle(DEFAULT_INPUT_STYLE);
212 model.SetShowUnderline(true);
213 model.SetSelectAllValue(true);
214 model.SetShowCounterBorder(true);
215 model.SetWordBreak(WordBreak::BREAK_ALL);
216 model.SetTextOverflow(TextOverflow::CLIP);
217 model.SetTextIndent(DEFAULT_INDENT_SIZE);
218 });
219
220 /**
221 * @tc.expected: Check if all set properties are displayed in the corresponding JSON
222 */
223 auto json = JsonUtil::Create(true);
224 pattern_->ToJsonValue(json, filter);
225 EXPECT_EQ(json->GetString("text"), DEFAULT_TEXT.c_str());
226 EXPECT_EQ(json->GetString("type"), "InputType.Normal");
227 EXPECT_EQ(json->GetString("caretColor"), "#FF000000");
228 EXPECT_EQ(json->GetString("placeholderColor"), "#FFFF0000");
229 EXPECT_EQ(json->GetString("textAlign"), "TextAlign.Left");
230 EXPECT_EQ(json->GetString("enterKeyType"), "EnterKeyType.Done");
231 EXPECT_EQ(json->GetString("maxLength"), "30");
232 EXPECT_EQ(json->GetString("inputFilter"), "[a-z]");
233 EXPECT_EQ(json->GetString("copyOption"), "CopyOptions.InApp");
234 EXPECT_EQ(json->GetString("style"), "TextInputStyle.Inline");
235 EXPECT_EQ(json->GetString("maxLines"), "3");
236 EXPECT_EQ(json->GetString("barState"), "BarState.AUTO");
237 json = JsonUtil::Create(true);
238 layoutProperty_->ToJsonValue(json, filter);
239 EXPECT_EQ(json->GetString("caretPosition"), "");
240 EXPECT_TRUE(json->GetBool("showUnderline"));
241 EXPECT_TRUE(json->GetBool("selectAll"));
242 EXPECT_EQ(json->GetString("wordBreak"), "break-all");
243 EXPECT_EQ(json->GetString("textOverflow"), "TextOverflow.Clip");
244 EXPECT_EQ(json->GetString("textIndent"), "5.00vp");
245 }
246
247 /**
248 * @tc.name: TextFieldFilter001
249 * @tc.desc: Test textfield filter.
250 * @tc.type: FUNC
251 */
252 HWTEST_F(TextFieldControllerTest, TextFieldFilter001, TestSize.Level1)
253 {
254 /**
255 * @tc.steps: step1. Initialize text input.
256 */
257 CreateTextField(DEFAULT_TEXT, "");
258
259 /**
260 * @tc.steps: step2. assign filter as lowercase filter
261 */
262 layoutProperty_->UpdateInputFilter(LOWERCASE_FILTER);
263 pattern_->InsertValue("X");
264 EXPECT_EQ(pattern_->GetInputFilter(), LOWERCASE_FILTER);
265 }
266
267 /**
268 * @tc.name: TextFieldFilter002
269 * @tc.desc: Test textfield filter.
270 * @tc.type: FUNC
271 */
272 HWTEST_F(TextFieldControllerTest, TextFieldFilter002, TestSize.Level1)
273 {
274 /**
275 * @tc.steps: step1. Initialize text input.
276 */
277 CreateTextField(DEFAULT_TEXT, "");
278
279 /**
280 * @tc.steps: step2. assign filter as number filter
281 */
282 layoutProperty_->UpdateInputFilter(NUMBER_FILTER);
283 auto numStr = "1";
284 pattern_->InsertValue(numStr);
285 EXPECT_EQ(pattern_->GetInputFilter(), NUMBER_FILTER);
286 }
287
288 /**
289 * @tc.name: TextFieldPatternOnTextInputScroll001
290 * @tc.desc: Verify that the AddScrollEvent interface calls normally and exits without exception.
291 * @tc.type: FUNC
292 */
293 HWTEST_F(TextFieldControllerTest, TextFieldPatternOnTextInputScroll001, TestSize.Level1)
294 {
295 /**
296 * @tc.steps: step1. Initialize text input.
297 */
298 CreateTextField(DEFAULT_TEXT);
299
300 /**
301 * @tc.steps: step2. Call the OnTextInputScroll.
302 * @tc.expected: step2. Check the value set in OnTextInputScroll.
303 */
304 layoutProperty_->UpdateMaxLines(2);
305 pattern_->OnTextInputScroll(0.0f);
306 layoutProperty_->UpdateMaxLines(1);
307 pattern_->textRect_.x_ = 10.0f;
308 pattern_->textRect_.width_ = 200.0f;
309 pattern_->contentRect_.x_ = 20.0f;
310 pattern_->contentRect_.width_ = 100.0f;
311 pattern_->OnTextInputScroll(-1000.0f);
312 pattern_->SetIsSingleHandle(false);
313 pattern_->OnTextInputScroll(0.0f);
314 pattern_->SetIsSingleHandle(true);
315 pattern_->OnTextInputScroll(0.0f);
316 EXPECT_EQ(pattern_->selectController_->GetCaretRect().GetX(), -90.0f);
317 EXPECT_EQ(pattern_->textRect_.GetOffset(), OffsetF(pattern_->currentOffset_, pattern_->textRect_.GetY()));
318 }
319
320 /**
321 * @tc.name: CreateNodePaintMethod001
322 * @tc.desc: Test textfield to create paint.
323 * @tc.type: FUNC
324 */
325 HWTEST_F(TextFieldControllerTest, CreateNodePaintMethod001, TestSize.Level1)
326 {
327 /**
328 * @tc.steps: step1. Initialize text input.
329 */
330 CreateTextField(DEFAULT_TEXT);
331
332 /**
333 * @tc.steps: step2. call CreateNodePaintMethod
334 * tc.expected: step2. Check if the value is created.
335 */
336 auto paint = pattern_->CreateNodePaintMethod();
337 EXPECT_NE(pattern_->GetContentModifier(), nullptr);
338 }
339
340 /**
341 * @tc.name: CreateNodePaintMethod002
342 * @tc.desc: Test textfield to create paint.
343 * @tc.type: FUNC
344 */
345 HWTEST_F(TextFieldControllerTest, CreateNodePaintMethod002, TestSize.Level1)
346 {
347 /**
348 * @tc.steps: step1. Initialize text input.
349 */
350 CreateTextField(DEFAULT_TEXT);
351 GetFocus();
352
353 /**
354 * @tc.steps: step2. call CreateNodePaintMethod
355 * tc.expected: step2. Check if the value is created.
356 */
357 pattern_->SetIsCustomFont(true);
358 auto paint = pattern_->CreateNodePaintMethod();
359 EXPECT_TRUE(pattern_->textFieldContentModifier_->GetIsCustomFont());
360 }
361
362 /**
363 * @tc.name: CreateNodePaintMethod003
364 * @tc.desc: Test textfield to create paint.
365 * @tc.type: FUNC
366 */
367 HWTEST_F(TextFieldControllerTest, CreateNodePaintMethod003, TestSize.Level1)
368 {
369 /**
370 * @tc.steps: step1. Initialize text input.
371 */
372 CreateTextField(DEFAULT_TEXT);
373 GetFocus();
374
375 /**
376 * @tc.steps: step2. call CreateNodePaintMethod
377 * tc.expected: step2. Check if the value is created.
378 */
379 auto paintProperty = frameNode_->GetPaintProperty<TextFieldPaintProperty>();
380 paintProperty->UpdateInputStyle(InputStyle::INLINE);
381 frameNode_->MarkModifyDone();
382 pattern_->OnModifyDone();
383 EXPECT_TRUE(pattern_->IsNormalInlineState());
384 pattern_->UpdateScrollBarOffset();
385
386 auto paint = AceType::DynamicCast<TextFieldPaintMethod>(pattern_->CreateNodePaintMethod());
387 auto inlineScrollRect = pattern_->GetScrollBar()->GetActiveRect();
388 EXPECT_EQ(inlineScrollRect, Rect(720, 0, 0, 50));
389 EXPECT_NE(pattern_->textFieldContentModifier_, nullptr);
390 }
391
392 /**
393 * @tc.name: CursorInContentRegion001
394 * @tc.desc: Test textfield if the cursor in content.
395 * @tc.type: FUNC
396 */
397 HWTEST_F(TextFieldControllerTest, CursorInContentRegion001, TestSize.Level1)
398 {
399 /**
400 * @tc.steps: step1. Initialize text input.
401 */
402 CreateTextField(DEFAULT_TEXT);
403
404 /**
405 * @tc.steps: step2. call CursorInContentRegion
406 * tc.expected: step2. Check the cursor position.
407 */
408 GetFocus();
409 EXPECT_TRUE(pattern_->CursorInContentRegion());
410 }
411
412 /**
413 * @tc.name: CreateDisplayText001
414 * @tc.desc: Test textInput display of context.
415 * @tc.type: FUNC
416 */
417 HWTEST_F(TextFieldControllerTest, CreateDisplayText001, TestSize.Level1)
418 {
419 /**
420 * @tc.steps: step1. Initialize text input.
421 */
422 CreateTextField(DEFAULT_TEXT);
423
424 /**
425 * @tc.steps: step2. call CreateDisplayText with showPasswordDirectly is true
426 * tc.expected: step2. Check the CreateDisplayText return.
427 */
428 GetFocus();
429 PipelineBase::GetCurrentContext()->SetMinPlatformVersion((int32_t)PlatformVersion::VERSION_TWELVE);
430 std::string inputPartOne = "tes";
431 std::string inputPartTwo = "t";
432 std::string input = inputPartOne + inputPartTwo;
433 auto outputOne = pattern_->CreateObscuredText(static_cast<int32_t>(StringUtils::ToWstring(input).length()));
434 auto res = pattern_->CreateDisplayText(input, 3, true, true);
435 EXPECT_EQ(outputOne, res);
436
437 /**
438 * @tc.steps: step3. call CreateDisplayText with showPasswordDirectly is false
439 * tc.expected: step3. Check the CreateDisplayText return.
440 */
441 auto outputTwo = pattern_->CreateObscuredText(static_cast<int32_t>(StringUtils::ToWstring(inputPartOne).length()));
442 outputTwo += StringUtils::Str8ToStr16(inputPartTwo);
443 res = pattern_->CreateDisplayText(input, 3, true, false);
444 EXPECT_EQ(outputTwo, res);
445 }
446
447 /**
448 * @tc.name: OffsetInContentRegion
449 * @tc.desc: Test textfield if the cursor in content.
450 * @tc.type: FUNC
451 */
452 HWTEST_F(TextFieldControllerTest, OffsetInContentRegion, TestSize.Level1)
453 {
454 /**
455 * @tc.steps: step1. Initialize text input.
456 */
457 CreateTextField(DEFAULT_TEXT);
458
459 /**
460 * @tc.steps: step2. call CursorInContentRegion
461 * tc.expected: step2. Check the offset position.
462 */
463 EXPECT_TRUE(pattern_->OffsetInContentRegion(Offset(1.0f, 1.0f)));
464 }
465
466 /**
467 * @tc.name: OnModifyDone001
468 * @tc.desc: Test the OnModifyDone.
469 * @tc.type: FUNC
470 */
471 HWTEST_F(TextFieldControllerTest, OnModifyDone001, TestSize.Level1)
472 {
473 /**
474 * @tc.steps: step1. Initialize text input.
475 */
476 CreateTextField(DEFAULT_TEXT);
477
478 /**
479 * @tc.steps: step2. Set showUnderLine. Call function OnModifyDone.
480 * @tc.expected: Check the showUnderLine set successfully.
481 */
482 layoutProperty_->UpdateShowUnderline(true);
483 pattern_->OnModifyDone();
484 GetFocus();
485 EXPECT_TRUE(layoutProperty_->GetShowUnderlineValue(false));
486 layoutProperty_->UpdateShowUnderline(false);
487 pattern_->OnModifyDone();
488 pattern_->HandleBlurEvent();
489 GetFocus();
490 EXPECT_FALSE(layoutProperty_->GetShowUnderlineValue(false));
491 auto focusHub = pattern_->GetFocusHub();
492 EXPECT_NE(focusHub->onFocusInternal_, nullptr);
493 EXPECT_NE(focusHub->onBlurInternal_, nullptr);
494 }
495
496 /**
497 * @tc.name: ContentController001
498 * @tc.desc: Test ContentController in different input type
499 * @tc.type: FUNC
500 */
501 HWTEST_F(TextFieldControllerTest, ContentController001, TestSize.Level1)
502 {
503 /**
504 * @tc.steps: Initialize insert text and expected values
505 */
506 std::vector<std::string> insertValues = {
507 "openharmony123_ *+%$",
508 "openharmony123456*+&@huawei.com",
509 "openharmony (new)#15612932075*.com",
510 "open_harmony@@harmony.com*+$helloworld",
511 "open_harmony123 password*+#",
512 "openharmony123456*+&@huawei.com",
513 "o达瓦大屋顶pen_harmony456顶顶顶 password*+#得到",
514 };
515 std::vector<TestItem<TextInputType, std::string>> testItems;
516 testItems.emplace_back(TextInputType::TEXT, "openharmony123_ *+%$", "TextInputType::TEXT");
517 testItems.emplace_back(TextInputType::NUMBER, "123456", "TextInputType::NUMBER");
518 testItems.emplace_back(TextInputType::PHONE, " ()#15612932075*", "TextInputType::PHONE");
519 testItems.emplace_back(
520 TextInputType::EMAIL_ADDRESS, "open_harmony@harmony.com*+$helloworld", "TextInputType::EMAIL_ADDRESS");
521 testItems.emplace_back(
522 TextInputType::VISIBLE_PASSWORD, "open_harmony123 password*+#", "TextInputType::VISIBLE_PASSWORD");
523 testItems.emplace_back(TextInputType::NUMBER_PASSWORD, "123456", "TextInputType::NUMBER_PASSWORD");
524 testItems.emplace_back(
525 TextInputType::SCREEN_LOCK_PASSWORD, "open_harmony456 password*+#", "TextInputType::SCREEN_LOCK_PASSWORD");
526
527 /**
528 * @tc.expected: Check if text filtering meets expectations
529 */
530 int index = 0;
531 for (const auto& testItem : testItems) {
__anon3e9d21400702(TextFieldModelNG& model) 532 CreateTextField("", "", [testItem](TextFieldModelNG& model) { model.SetType(testItem.item); });
533
534 pattern_->contentController_->InsertValue(0, insertValues[index]);
535 index++;
536 auto errorMessage = "InputType is " + testItem.error + ", text is " + pattern_->GetTextValue();
537 EXPECT_EQ(pattern_->GetTextValue().compare(testItem.expected), 0) << errorMessage;
538 }
539 }
540
541 /**
542 * @tc.name: ContentController002
543 * @tc.desc: Test ContentController in different input filter
544 * @tc.type: FUNC
545 */
546 HWTEST_F(TextFieldControllerTest, ContentController002, TestSize.Level1)
547 {
548 /**
549 * @tc.steps: Initialize text and filter patterns
550 */
551 std::string text = "CabcdefgABhCDEFG0123a456A789";
552 std::vector<TestItem<std::string, std::string>> testItems;
553 testItems.emplace_back("", "CabcdefgABhCDEFG0123a456A789", "None");
554 testItems.emplace_back("[0-9]", "0123456789", "Input filter [0-9]");
555 testItems.emplace_back("[A-Z]", "CABCDEFGA", "Input filter [A-Z]");
556 testItems.emplace_back("[a-z]", "abcdefgha", "Input filter [a-z]");
557
558 /**
559 * @tc.expected: Check if the text filter patterns for the input box are compliant
560 */
561 for (const auto& testItem : testItems) {
__anon3e9d21400802(TextFieldModelNG& model) 562 CreateTextField("", "", [testItem](TextFieldModelNG& model) { model.SetInputFilter(testItem.item, nullptr); });
563
564 pattern_->contentController_->InsertValue(0, text);
565 auto errorMessage = testItem.error + ", text is " + pattern_->GetTextValue();
566 EXPECT_EQ(pattern_->GetTextValue().compare(testItem.expected), 0) << errorMessage;
567 }
568 }
569
570 /**
571 * @tc.name: ContentController003
572 * @tc.desc: Test ContentController in different input filter
573 * @tc.type: FUNC
574 */
575 HWTEST_F(TextFieldControllerTest, ContentController003, TestSize.Level1)
576 {
577 /**
578 * @tc.steps: Initialize text filed node
579 */
580 CreateTextField(DEFAULT_TEXT);
581
582 /**
583 * @tc.expected: Check if text is selected based on corresponding left and right coordinates
584 */
585 auto selectedValue = pattern_->contentController_->GetSelectedValue(1, 4);
586 EXPECT_EQ(selectedValue.compare("bcd"), 0) << "Text is " + selectedValue;
587
588 /**
589 * @tc.expected: Check if text is selected based on preceding coordinates
590 */
591 auto beforeSelectedValue = pattern_->contentController_->GetValueBeforeIndex(3);
592 EXPECT_EQ(beforeSelectedValue.compare("abc"), 0) << "Text is " + beforeSelectedValue;
593
594 /**
595 * @tc.expected: Check if text is selected based on trailing coordinates
596 */
597 auto afterSelectedValue = pattern_->contentController_->GetValueAfterIndex(3);
598 EXPECT_EQ(afterSelectedValue.compare("defghijklmnopqrstuvwxyz"), 0) << "Text is " + afterSelectedValue;
599 }
600
601 /**
602 * @tc.name: ContentController004
603 * @tc.desc: Test ContentController in different input filter
604 * @tc.type: FUNC
605 */
606 HWTEST_F(TextFieldControllerTest, ContentController004, TestSize.Level1)
607 {
608 /**
609 * @tc.steps: Initialize text field node
610 */
611 CreateTextField();
612 GetFocus();
613
614 /**
615 * @tc.expected: Set text type is NUMBER_DECIMAL
616 */
617 layoutProperty_->UpdateTextInputType(TextInputType::NUMBER_DECIMAL);
618
619 /**
620 * @tc.expected: when the text point more then one, FilterWithDecimal return true,
621 * @tc.expected: and the value update only one point
622 */
623 std::string text = "3.1.4.";
624 pattern_->contentController_->InsertValue(0, text);
625 EXPECT_TRUE(pattern_->contentController_->FilterWithDecimal(text));
626 EXPECT_EQ(pattern_->GetTextValue(), "3.14");
627 }
628
629 /**
630 * @tc.name: ContentController005
631 * @tc.desc: Test ContentController in different input filter
632 * @tc.type: FUNC
633 */
634 HWTEST_F(TextFieldControllerTest, ContentController005, TestSize.Level1)
635 {
636 /**
637 * @tc.steps: Initialize text field node
638 */
639 CreateTextField();
640 GetFocus();
641
642 /**
643 * @tc.expected: Set text type is NUMBER_DECIMAL
644 */
645 layoutProperty_->UpdateTextInputType(TextInputType::NUMBER_DECIMAL);
646
647 /**
648 * @tc.expected: when the text have one point, FilterWithDecimal return false
649 */
650 std::string text = "3.14";
651 pattern_->contentController_->InsertValue(0, text);
652 EXPECT_FALSE(pattern_->contentController_->FilterWithDecimal(text));
653 EXPECT_EQ(pattern_->GetTextValue(), "3.14");
654 }
655
656 /**
657 * @tc.name: TextFiledControllerTest001
658 * @tc.desc: Test TextFieldController GetTextContentLinesNum
659 * @tc.type: FUNC
660 */
661 HWTEST_F(TextFieldControllerTest, TextFieldControllerTest001, TestSize.Level1)
662 {
663 /**
664 * @tc.steps: Initialize text filed node
665 */
666 CreateTextField();
667
668 /**
669 * @tc.expected: Check if the number of lines meets the expectation
670 */
671 auto controller = pattern_->GetTextFieldController();
672 auto line = controller->GetTextContentLinesNum();
673 EXPECT_EQ(line, 0);
674 }
675
676 /**
677 * @tc.name: ControllerTest002
678 * @tc.desc: Test TextFieldController GetTextContentLinesNum
679 * @tc.type: FUNC
680 */
681 HWTEST_F(TextFieldControllerTest, TextFieldControllerTest002, TestSize.Level1)
682 {
683 /**
684 * @tc.steps: Initialize text filed node
685 */
686 CreateTextField(DEFAULT_TEXT);
687
688 /**
689 * @tc.expected: Check if the number of lines meets the expectation
690 */
691 auto controller = pattern_->GetTextFieldController();
692 auto line = controller->GetTextContentLinesNum();
693 EXPECT_EQ(line, 1);
694 }
695
696 /**
697 * @tc.name: TextFieldControllerTest003
698 * @tc.desc: Test TextFieldModelNG controller.
699 * @tc.type: FUNC
700 */
701 HWTEST_F(TextFieldControllerTest, TextFieldControllerTest003, TestSize.Level1)
702 {
703 /**
704 * @tc.steps: Initialize textarea node.
705 */
706 auto frameNode = TextFieldModelNG::CreateFrameNode(-1, "", "", true);
707 ASSERT_NE(frameNode, nullptr);
708 auto node = AceType::RawPtr(frameNode);
709
710 /**
711 * @tc.expected: Check jsController value.
712 */
713 auto jsController = AceType::MakeRefPtr<Referenced>();
714 TextFieldModelNG::SetJSTextEditableController(node, jsController);
715 auto getController = TextFieldModelNG::GetJSTextEditableController(node);
716 EXPECT_NE(getController, nullptr);
717 }
718
719 /**
720 * @tc.name: TextFieldFontFeatureTest
721 * @tc.desc: Test the caret move right
722 * @tc.type: FUNC
723 */
724 HWTEST_F(TextFieldControllerTest, FontFeature003, TestSize.Level1)
725 {
726 /**
727 * @tc.steps: step1. Initialize text input.
728 */
729 TextFieldModelNG textFieldModelNG;
730 textFieldModelNG.CreateTextInput(DEFAULT_TEXT, "");
731
732 auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
733 ASSERT_NE(frameNode, nullptr);
734 auto layoutProperty = frameNode->GetLayoutProperty<TextFieldLayoutProperty>();
735 textFieldModelNG.SetFontFeature(FONT_FEATURE_VALUE_0);
736 EXPECT_EQ(layoutProperty->GetFontFeature(), FONT_FEATURE_VALUE_0);
737
738 layoutProperty->UpdateFontFeature(ParseFontFeatureSettings("\"ss01\" 1"));
739 TextFieldModelNG::SetFontFeature(frameNode, FONT_FEATURE_VALUE_0);
740 EXPECT_EQ(layoutProperty->GetFontFeature(), FONT_FEATURE_VALUE_0);
741 }
742
743 /**
744 * @tc.name: TextFieldFontFeatureTest
745 * @tc.desc: Test the caret move right
746 * @tc.type: FUNC
747 */
748 HWTEST_F(TextFieldControllerTest, FontFeature004, TestSize.Level1)
749 {
750 /**
751 * @tc.steps: step1. Initialize text input.
752 */
753 TextFieldModelNG textFieldModelNG;
754 textFieldModelNG.CreateTextInput(DEFAULT_TEXT, "");
755
756 auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
757 ASSERT_NE(frameNode, nullptr);
758 auto layoutProperty = frameNode->GetLayoutProperty<TextFieldLayoutProperty>();
759 textFieldModelNG.SetFontFeature(FONT_FEATURE_VALUE_1);
760 EXPECT_EQ(layoutProperty->GetFontFeature(), FONT_FEATURE_VALUE_1);
761
762 layoutProperty->UpdateFontFeature(ParseFontFeatureSettings("\"ss01\" 0"));
763 TextFieldModelNG::SetFontFeature(frameNode, FONT_FEATURE_VALUE_1);
764 EXPECT_EQ(layoutProperty->GetFontFeature(), FONT_FEATURE_VALUE_1);
765 }
766
767 /**
768 * @tc.name: HandleOnDeleteAction001
769 * @tc.desc: test testInput deleteAction for normal emoji.
770 * @tc.type: FUNC
771 */
772 HWTEST_F(TextFieldControllerTest, HandleOnDeleteAction001, TestSize.Level1)
773 {
774 #if defined(__HuaweiLite__) || defined(__OHOS__)
775 // use system icudt .dat file
776 std::string dataPath = "/system/usr/ohos_icu";
777 #else
778 // use project icudt .dat file
779 #ifdef WINDOWS_PLATFORM
780 char buffer[MAX_PATH];
781 GetCurrentDirectoryA(MAX_PATH, buffer);
782 std::string currentPath = std::string(buffer);
783 #else
784 char buffer[PATH_MAX];
785 getcwd(buffer, sizeof(buffer));
786 std::string currentPath = std::string(buffer);
787 #endif
788 std::string dataPath = currentPath + "/../../../../../../third_party/icu/ohos_icu4j/data";
789 #endif
790 u_setDataDirectory(dataPath.c_str());
791 UErrorCode code;
792 u_init(&code);
793 /**
794 * @tc.steps: Create Text field node
795 */
796 std::string txt = "";
__anon3e9d21400902(TextFieldModelNG model) 797 CreateTextField(txt, "", [](TextFieldModelNG model) {
798 model.SetType(TextInputType::TEXT);
799 });
800 GetFocus();
801 /**
802 * @tc.step: step2. Set caretPosition and call Delete
803 */
804 pattern_->SetCaretPosition(24);
805 pattern_->DeleteBackward(2);
806 FlushLayoutTask(frameNode_);
807 std::string result = "";
808 EXPECT_EQ(pattern_->GetTextValue().compare(result), 0) << "Text is: " << pattern_->GetTextValue();
809
810 pattern_->SetCaretPosition(10);
811 pattern_->DeleteBackward(2);
812 FlushLayoutTask(frameNode_);
813 result = "";
814 EXPECT_EQ(pattern_->GetTextValue().compare(result), 0) << "Text is: " << pattern_->GetTextValue();
815
816 pattern_->SetCaretPosition(0);
817 pattern_->DeleteForward(2);
818 FlushLayoutTask(frameNode_);
819 result = "";
820 EXPECT_EQ(pattern_->GetTextValue().compare(result), 0) << "Text is: " + pattern_->GetTextValue();
821
822 pattern_->SetCaretPosition(6);
823 pattern_->DeleteForward(2);
824 FlushLayoutTask(frameNode_);
825 result = "";
826 EXPECT_EQ(pattern_->GetTextValue().compare(result), 0) << "Text is: " + pattern_->GetTextValue();
827 }
828
829 /**
830 * @tc.name: HandleOnDeleteAction002
831 * @tc.desc: test testInput deleteAction for ZWJ emoji.
832 * @tc.type: FUNC
833 */
834 HWTEST_F(TextFieldControllerTest, HandleOnDeleteAction002, TestSize.Level1)
835 {
836 /**
837 * @tc.steps: Create Text field node
838 */
839 // change line to aviod the line length exceed 120
840 const std::string txt = std::string("")
841 .append("");
__anon3e9d21400a02(TextFieldModelNG model) 842 CreateTextField(txt, "", [](TextFieldModelNG model) {
843 model.SetType(TextInputType::TEXT);
844 });
845 GetFocus();
846
847 /**
848 * @tc.step: step2. Set caretPosition and call Delete
849 */
850
851 pattern_->SetCaretPosition(132);
852 pattern_->DeleteBackward(2);
853 FlushLayoutTask(frameNode_);
854 // change line to aviod the line length exceed 120
855 std::string result = std::string("")
856 .append("");
857
858 pattern_->SetCaretPosition(88);
859 pattern_->DeleteBackward(2);
860 FlushLayoutTask(frameNode_);
861 result = "";
862
863 pattern_->SetCaretPosition(0);
864 pattern_->DeleteForward(2);
865 FlushLayoutTask(frameNode_);
866 result = "";
867
868 pattern_->SetCaretPosition(44);
869 pattern_->DeleteForward(2);
870 FlushLayoutTask(frameNode_);
871 result = "";
872 }
873
874 /**
875 * @tc.name: HandleOnDeleteAction003
876 * @tc.desc: test testInput deleteAction for VS emoji.
877 * @tc.type: FUNC
878 */
879 HWTEST_F(TextFieldControllerTest, HandleOnDeleteAction003, TestSize.Level1)
880 {
881 /**
882 * @tc.steps: Create Text field node
883 */
884 const std::string txt = "️️️️️️️️️️️️";
__anon3e9d21400b02(TextFieldModelNG model) 885 CreateTextField(txt, "", [](TextFieldModelNG model) {
886 model.SetType(TextInputType::TEXT);
887 });
888 GetFocus();
889
890 /**
891 * @tc.step: step2. Set caretPosition and call Delete
892 */
893 pattern_->SetCaretPosition(36);
894 pattern_->DeleteBackward(2);
895 FlushLayoutTask(frameNode_);
896 std::string result = "️️️️️️️️️️";
897 EXPECT_EQ(pattern_->GetTextValue().compare(result), 0) << "Text is: " + pattern_->GetTextValue();
898
899 pattern_->SetCaretPosition(30);
900 pattern_->DeleteBackward(2);
901 FlushLayoutTask(frameNode_);
902 result = "️️️️️️️️";
903 EXPECT_EQ(pattern_->GetTextValue().compare(result), 0) << "Text is: " + pattern_->GetTextValue();
904
905 pattern_->SetCaretPosition(0);
906 pattern_->DeleteForward(2);
907 FlushLayoutTask(frameNode_);
908 result = "️️️️️️";
909 EXPECT_EQ(pattern_->GetTextValue().compare(result), 0) << "Text is: " + pattern_->GetTextValue();
910
911 pattern_->SetCaretPosition(12);
912 pattern_->DeleteForward(2);
913 FlushLayoutTask(frameNode_);
914 result = "️️️️";
915 EXPECT_EQ(pattern_->GetTextValue().compare(result), 0) << "Text is: " + pattern_->GetTextValue();
916 }
917
918 /**
919 * @tc.name: HandleOnDeleteAction004
920 * @tc.desc: test testInput deleteAction for RIS.
921 * @tc.type: FUNC
922 */
923 HWTEST_F(TextFieldControllerTest, HandleOnDeleteAction004, TestSize.Level1)
924 {
925 /**
926 * @tc.steps: Create Text field node
927 */
928 const std::string txt = "";
__anon3e9d21400c02(TextFieldModelNG model) 929 CreateTextField(txt, "", [](TextFieldModelNG model) {
930 model.SetType(TextInputType::TEXT);
931 });
932 GetFocus();
933
934 /**
935 * @tc.step: step2. Set caretPosition and call Delete
936 */
937 pattern_->SetCaretPosition(48);
938 pattern_->DeleteBackward(1);
939 FlushLayoutTask(frameNode_);
940 std::string result = "";
941 EXPECT_EQ(pattern_->GetTextValue().compare(result), 0) << "Text is: " + pattern_->GetTextValue();
942
943 pattern_->SetCaretPosition(28);
944 pattern_->DeleteBackward(3);
945 FlushLayoutTask(frameNode_);
946 result = "";
947 EXPECT_EQ(pattern_->GetTextValue().compare(result), 0) << "Text is: " + pattern_->GetTextValue();
948
949 pattern_->SetCaretPosition(0);
950 pattern_->DeleteForward(3);
951 FlushLayoutTask(frameNode_);
952 result = "";
953 EXPECT_EQ(pattern_->GetTextValue().compare(result), 0) << "Text is: " + pattern_->GetTextValue();
954
955 pattern_->SetCaretPosition(12);
956 pattern_->DeleteForward(1);
957 FlushLayoutTask(frameNode_);
958 result = "";
959 EXPECT_EQ(pattern_->GetTextValue().compare(result), 0) << "Text is: " + pattern_->GetTextValue();
960 }
961
962 /**
963 * @tc.name: HandleOnDeleteAction005
964 * @tc.desc: test testInput deleteAction for keycap.
965 * @tc.type: FUNC
966 */
967 HWTEST_F(TextFieldControllerTest, HandleOnDeleteAction005, TestSize.Level1)
968 {
969 /**
970 * @tc.steps: Create Text field node
971 */
972 const std::string txt = "3️⃣3️⃣3️⃣3️⃣3️⃣3️⃣3️⃣3️⃣3️⃣3️⃣3️⃣3️⃣";
__anon3e9d21400d02(TextFieldModelNG model) 973 CreateTextField(txt, "", [](TextFieldModelNG model) {
974 model.SetType(TextInputType::TEXT);
975 });
976 GetFocus();
977
978 /**
979 * @tc.step: step2. Set caretPosition and call Delete
980 */
981 pattern_->SetCaretPosition(36);
982 pattern_->DeleteBackward(2);
983 FlushLayoutTask(frameNode_);
984 std::string result = "3️⃣3️⃣3️⃣3️⃣3️⃣3️⃣3️⃣3️⃣3️⃣3️⃣";
985 EXPECT_EQ(pattern_->GetTextValue().compare(result), 0) << "Text is: " + pattern_->GetTextValue();
986
987 pattern_->SetCaretPosition(27);
988 pattern_->DeleteBackward(2);
989 FlushLayoutTask(frameNode_);
990 result = "3️⃣3️⃣3️⃣3️⃣3️⃣3️⃣3️⃣3️⃣";
991 EXPECT_EQ(pattern_->GetTextValue().compare(result), 0) << "Text is: " + pattern_->GetTextValue();
992
993 pattern_->SetCaretPosition(0);
994 pattern_->DeleteForward(2);
995 FlushLayoutTask(frameNode_);
996 result = "3️⃣3️⃣3️⃣3️⃣3️⃣3️⃣";
997 EXPECT_EQ(pattern_->GetTextValue().compare(result), 0) << "Text is: " + pattern_->GetTextValue();
998
999 pattern_->SetCaretPosition(12);
1000 pattern_->DeleteForward(2);
1001 FlushLayoutTask(frameNode_);
1002 result = "3️⃣3️⃣3️⃣3️⃣";
1003 EXPECT_EQ(pattern_->GetTextValue().compare(result), 0) << "Text is: " + pattern_->GetTextValue();
1004 }
1005
1006 /**
1007 * @tc.name: HandleOnDeleteAction006
1008 * @tc.desc: test testInput deleteAction for ZWJ&VS emoji.
1009 * @tc.type: FUNC
1010 */
1011 HWTEST_F(TextFieldControllerTest, HandleOnDeleteAction006, TestSize.Level1)
1012 {
1013 /**
1014 * @tc.steps: Create Text field node
1015 */
1016 const std::string txt = "️️️️️️️️️️️️️️️️️️️️️️️️";
__anon3e9d21400e02(TextFieldModelNG model) 1017 CreateTextField(txt, "", [](TextFieldModelNG model) {
1018 model.SetType(TextInputType::TEXT);
1019 });
1020 GetFocus();
1021
1022 /**
1023 * @tc.step: step2. Set caretPosition and call Delete
1024 */
1025 pattern_->SetCaretPosition(84);
1026 pattern_->DeleteBackward(2);
1027 FlushLayoutTask(frameNode_);
1028 std::string result = "️️️️️️️️️️️️️️️️️️️️";
1029
1030 pattern_->SetCaretPosition(77);
1031 pattern_->DeleteBackward(2);
1032 FlushLayoutTask(frameNode_);
1033 result = "️️️️️️️️️️️️️️️️";
1034
1035 pattern_->SetCaretPosition(0);
1036 pattern_->DeleteForward(2);
1037 FlushLayoutTask(frameNode_);
1038 result = "️️️️️️️️️️️️";
1039 }
1040
1041 /**
1042 * @tc.name: HandleOnDeleteAction007
1043 * @tc.desc: test testInput deleteAction for line break.
1044 * @tc.type: FUNC
1045 */
1046 HWTEST_F(TextFieldControllerTest, HandleOnDeleteAction007, TestSize.Level1)
1047 {
1048 /**
1049 * @tc.steps: Create Text field node
1050 */
1051 const std::string txt = "1\n23\n45\r\n6\n78\n9";
__anon3e9d21400f02(TextFieldModelNG model) 1052 CreateTextField(txt, "", [](TextFieldModelNG model) {
1053 model.SetType(TextInputType::TEXT);
1054 });
1055 GetFocus();
1056
1057 /**
1058 * @tc.step: step2. Set caretPosition and call Delete
1059 */
1060 pattern_->SetCaretPosition(15);
1061 pattern_->DeleteBackward(4);
1062 FlushLayoutTask(frameNode_);
1063 std::string result = "1\n23\n45\r\n6\n";
1064 EXPECT_EQ(pattern_->GetTextValue().compare(result), 0) << "Text is: " + pattern_->GetTextValue();
1065
1066 pattern_->SetCaretPosition(9);
1067 pattern_->DeleteBackward(3);
1068 FlushLayoutTask(frameNode_);
1069 result = "1\n23\n6\n";
1070 EXPECT_EQ(pattern_->GetTextValue().compare(result), 0) << "Text is: " + pattern_->GetTextValue();
1071
1072 pattern_->SetCaretPosition(0);
1073 pattern_->DeleteForward(3);
1074 FlushLayoutTask(frameNode_);
1075 result = "3\n6\n";
1076 EXPECT_EQ(pattern_->GetTextValue().compare(result), 0) << "Text is: " + pattern_->GetTextValue();
1077
1078 pattern_->SetCaretPosition(2);
1079 pattern_->DeleteForward(2);
1080 FlushLayoutTask(frameNode_);
1081 result = "3\n";
1082 EXPECT_EQ(pattern_->GetTextValue().compare(result), 0) << "Text is: " + pattern_->GetTextValue();
1083 }
1084
1085 /**
1086 * @tc.name: HandleOnDeleteAction008
1087 * @tc.desc: test testInput deleteAction for all case.
1088 * @tc.type: FUNC
1089 */
1090 HWTEST_F(TextFieldControllerTest, HandleOnDeleteAction008, TestSize.Level1)
1091 {
1092 /**
1093 * @tc.steps: Create Text field node
1094 */
1095 const std::string txt = "12345️\n3️⃣️️\n67890";
__anon3e9d21401002(TextFieldModelNG model) 1096 CreateTextField(txt, "", [](TextFieldModelNG model) {
1097 model.SetType(TextInputType::TEXT);
1098 });
1099 GetFocus();
1100
1101 /**
1102 * @tc.step: step2. Set caretPosition and call Delete
1103 */
1104 pattern_->SetCaretPosition(42);
1105 pattern_->DeleteBackward(6);
1106 FlushLayoutTask(frameNode_);
1107 std::string result = "12345️\n3️⃣️️";
1108 EXPECT_EQ(pattern_->GetTextValue().compare(result), 0) << "Text is: " + pattern_->GetTextValue();
1109
1110 pattern_->SetCaretPosition(29);
1111 pattern_->DeleteBackward(2);
1112 FlushLayoutTask(frameNode_);
1113 result = "12345️\n️️";
1114 EXPECT_EQ(pattern_->GetTextValue().compare(result), 0) << "Text is: " + pattern_->GetTextValue();
1115
1116 pattern_->SetCaretPosition(0);
1117 pattern_->DeleteForward(6);
1118 FlushLayoutTask(frameNode_);
1119 result = "️\n️️";
1120 EXPECT_EQ(pattern_->GetTextValue().compare(result), 0) << "Text is: " + pattern_->GetTextValue();
1121
1122 pattern_->SetCaretPosition(14);
1123 pattern_->DeleteForward(1);
1124 FlushLayoutTask(frameNode_);
1125 result = "️️️";
1126 EXPECT_EQ(pattern_->GetTextValue().compare(result), 0) << "Text is: " + pattern_->GetTextValue();
1127 }
1128
1129 /**
1130 * @tc.name: GetGlobalPointsWithTransform
1131 * @tc.desc: test GetGlobalPointsWithTransform.
1132 * @tc.type: FUNC
1133 */
1134 HWTEST_F(TextFieldControllerTest, GetGlobalPointsWithTransform, TestSize.Level1)
1135 {
1136 /**
1137 * @tc.steps: Create Text field node
1138 */
1139 const std::string txt = "1234567890";
__anon3e9d21401102(TextFieldModelNG model) 1140 CreateTextField(txt, "", [](TextFieldModelNG model) { model.SetType(TextInputType::TEXT); });
1141 GetFocus();
1142
1143 /**
1144 * @tc.step: step2. Call GetGlobalPointsWithTransform.
1145 */
1146 std::vector<OffsetF> localPoints = { OffsetF(5.0f, 5.0f) };
1147 pattern_->selectOverlay_->hasTransform_ = true;
1148 auto renderContext = frameNode_->GetRenderContext();
1149 ASSERT_NE(renderContext, nullptr);
1150 auto mockRenderContext = AceType::MakeRefPtr<MockRenderContext>();
__anon3e9d21401202(PointF& point) 1151 EXPECT_CALL(*mockRenderContext, GetPointTransform(_)).WillRepeatedly([](PointF& point) {
1152 point.SetX(-5.0f);
1153 point.SetY(5.0f);
1154 });
1155 pattern_->frameNode_.Upgrade()->renderContext_ = mockRenderContext;
1156 pattern_->selectOverlay_->GetGlobalPointsWithTransform(localPoints);
1157 EXPECT_EQ(localPoints[0].GetX(), -5.0f);
1158 EXPECT_EQ(localPoints[0].GetY(), 5.0f);
1159 pattern_->frameNode_.Upgrade()->renderContext_ = renderContext;
1160 }
1161
1162 /**
1163 * @tc.name: GetGlobalRectWithTransform
1164 * @tc.desc: test GetGlobalRectWithTransform.
1165 * @tc.type: FUNC
1166 */
1167 HWTEST_F(TextFieldControllerTest, GetGlobalRectWithTransform, TestSize.Level1)
1168 {
1169 /**
1170 * @tc.steps: Create Text field node
1171 */
1172 const std::string txt = "1234567890";
__anon3e9d21401302(TextFieldModelNG model) 1173 CreateTextField(txt, "", [](TextFieldModelNG model) { model.SetType(TextInputType::TEXT); });
1174 GetFocus();
1175
1176 /**
1177 * @tc.step: step2. Call GetGlobalRectWithTransform.
1178 */
1179 pattern_->selectOverlay_->hasTransform_ = true;
1180 auto renderContext = frameNode_->GetRenderContext();
1181 ASSERT_NE(renderContext, nullptr);
1182 auto mockRenderContext = AceType::MakeRefPtr<MockRenderContext>();
__anon3e9d21401402(PointF& point) 1183 EXPECT_CALL(*mockRenderContext, GetPointTransform(_)).WillRepeatedly([](PointF& point) {
1184 point.SetX(point.GetX());
1185 point.SetY(point.GetY());
1186 });
1187 RectF rect = RectF(OffsetF(5.0f, 5.0f), SizeF(5.0f, 5.0f));
1188 pattern_->selectOverlay_->GetGlobalRectWithTransform(rect);
1189 EXPECT_EQ(rect.GetOffset(), OffsetF(5.0f, 5.0f));
1190 EXPECT_EQ(rect.GetSize(), SizeF(5.0f, 5.0f));
1191 }
1192
1193 /**
1194 * @tc.name: RevertLocalPointWithTransform
1195 * @tc.desc: test RevertLocalPointWithTransform.
1196 * @tc.type: FUNC
1197 */
1198 HWTEST_F(TextFieldControllerTest, RevertLocalPointWithTransform, TestSize.Level1)
1199 {
1200 /**
1201 * @tc.steps: Create Text field node
1202 */
1203 const std::string txt = "1234567890";
__anon3e9d21401502(TextFieldModelNG model) 1204 CreateTextField(txt, "", [](TextFieldModelNG model) { model.SetType(TextInputType::TEXT); });
1205 GetFocus();
1206
1207 /**
1208 * @tc.step: step2. Call RevertLocalPointWithTransform
1209 */
1210 pattern_->selectOverlay_->hasTransform_ = true;
1211 auto renderContext = frameNode_->GetRenderContext();
1212 ASSERT_NE(renderContext, nullptr);
1213 auto mockRenderContext = AceType::MakeRefPtr<MockRenderContext>();
__anon3e9d21401602(PointF& point) 1214 EXPECT_CALL(*mockRenderContext, GetPointWithRevert(_)).WillRepeatedly([](PointF& point) {
1215 point.SetX(5.0f);
1216 point.SetY(5.0f);
1217 });
1218 OffsetF offset(-5.0f, 5.0f);
1219 pattern_->frameNode_.Upgrade()->renderContext_ = mockRenderContext;
1220 pattern_->selectOverlay_->RevertLocalPointWithTransform(offset);
1221 EXPECT_EQ(offset.GetX(), 5.0f);
1222 EXPECT_EQ(offset.GetY(), 5.0f);
1223 pattern_->frameNode_.Upgrade()->renderContext_ = renderContext;
1224 }
1225
1226 /**
1227 * @tc.name: GetGlobalRectVertexWithTransform
1228 * @tc.desc: test GetGlobalRectVertexWithTransform.
1229 * @tc.type: FUNC
1230 */
1231 HWTEST_F(TextFieldControllerTest, GetGlobalRectVertexWithTransform, TestSize.Level1)
1232 {
1233 /**
1234 * @tc.steps: Create Text field node
1235 */
1236 const std::string txt = "1234567890";
__anon3e9d21401702(TextFieldModelNG model) 1237 CreateTextField(txt, "", [](TextFieldModelNG model) { model.SetType(TextInputType::TEXT); });
1238 GetFocus();
1239
1240 /**
1241 * @tc.step: step2. Call GetGlobalRectVertexWithTransform
1242 */
1243 pattern_->selectOverlay_->hasTransform_ = true;
1244 auto renderContext = frameNode_->GetRenderContext();
1245 ASSERT_NE(renderContext, nullptr);
1246 auto mockRenderContext = AceType::MakeRefPtr<MockRenderContext>();
__anon3e9d21401802(PointF& point) 1247 EXPECT_CALL(*mockRenderContext, GetPointWithRevert(_)).WillRepeatedly([](PointF& point) {
1248 point.SetX(point.GetX());
1249 point.SetY(point.GetY());
1250 });
1251 RectF rect = RectF(OffsetF(5.0f, 5.0f), SizeF(5.0f, 5.0f));
1252 auto vertex = pattern_->selectOverlay_->GetGlobalRectVertexWithTransform(rect);
1253 EXPECT_EQ(vertex[0], OffsetF(5.0f, 5.0f));
1254 EXPECT_EQ(vertex[1], OffsetF(10.0f, 5.0f));
1255 EXPECT_EQ(vertex[2], OffsetF(5.0f, 10.0f));
1256 EXPECT_EQ(vertex[3], OffsetF(10.0f, 10.0f));
1257 }
1258
1259 /**
1260 * @tc.name: GetLocalPointsWithTransform
1261 * @tc.desc: test GetLocalPointsWithTransform.
1262 * @tc.type: FUNC
1263 */
1264 HWTEST_F(TextFieldControllerTest, GetLocalPointsWithTransform, TestSize.Level1)
1265 {
1266 /**
1267 * @tc.steps: Create Text field node
1268 */
1269 const std::string txt = "1234567890";
__anon3e9d21401902(TextFieldModelNG model) 1270 CreateTextField(txt, "", [](TextFieldModelNG model) { model.SetType(TextInputType::TEXT); });
1271 GetFocus();
1272
1273 /**
1274 * @tc.step: step2. Call GetLocalPointsWithTransform
1275 */
1276 pattern_->selectOverlay_->hasTransform_ = true;
1277 auto renderContext = frameNode_->GetRenderContext();
1278 ASSERT_NE(renderContext, nullptr);
1279 auto mockRenderContext = AceType::MakeRefPtr<MockRenderContext>();
__anon3e9d21401a02(PointF& point) 1280 EXPECT_CALL(*mockRenderContext, GetPointWithRevert(_)).WillRepeatedly([](PointF& point) {
1281 point.SetX(point.GetX());
1282 point.SetY(point.GetY());
1283 });
1284 std::vector<OffsetF> localPoints = { OffsetF(5.0f, 5.0f) };
1285 pattern_->selectOverlay_->GetLocalPointsWithTransform(localPoints);
1286 EXPECT_EQ(localPoints[0].GetX(), 5.0f);
1287 EXPECT_EQ(localPoints[0].GetY(), 5.0f);
1288 }
1289
1290 /**
1291 * @tc.name: GetLocalRectWithTransform
1292 * @tc.desc: test GetLocalRectWithTransform.
1293 * @tc.type: FUNC
1294 */
1295 HWTEST_F(TextFieldControllerTest, GetLocalRectWithTransform, TestSize.Level1)
1296 {
1297 /**
1298 * @tc.steps: Create Text field node
1299 */
1300 const std::string txt = "1234567890";
__anon3e9d21401b02(TextFieldModelNG model) 1301 CreateTextField(txt, "", [](TextFieldModelNG model) { model.SetType(TextInputType::TEXT); });
1302 GetFocus();
1303
1304 /**
1305 * @tc.step: step2. Call GetLocalRectWithTransform
1306 */
1307 pattern_->selectOverlay_->hasTransform_ = true;
1308 auto renderContext = frameNode_->GetRenderContext();
1309 ASSERT_NE(renderContext, nullptr);
1310 auto mockRenderContext = AceType::MakeRefPtr<MockRenderContext>();
__anon3e9d21401c02(PointF& point) 1311 EXPECT_CALL(*mockRenderContext, GetPointWithRevert(_)).WillRepeatedly([](PointF& point) {
1312 point.SetX(point.GetX());
1313 point.SetY(point.GetY());
1314 });
1315 RectF rect(OffsetF(0.0f, 0.0f), SizeF(5.0f, 5.0f));
1316 pattern_->selectOverlay_->GetLocalRectWithTransform(rect);
1317 EXPECT_EQ(rect.GetOffset().GetX(), 0.0f);
1318 EXPECT_EQ(rect.GetOffset().GetY(), 0.0f);
1319 EXPECT_EQ(rect.GetSize().Width(), 5.0f);
1320 EXPECT_EQ(rect.GetSize().Height(), 5.0f);
1321 }
1322
1323 /**
1324 * @tc.name: IsPointInRect
1325 * @tc.desc: test IsPointInRect.
1326 * @tc.type: FUNC
1327 */
1328 HWTEST_F(TextFieldControllerTest, IsPointInRect, TestSize.Level1)
1329 {
1330 /**
1331 * @tc.steps: Create Text field node
1332 */
1333 const std::string txt = "1234567890";
__anon3e9d21401d02(TextFieldModelNG model) 1334 CreateTextField(txt, "", [](TextFieldModelNG model) { model.SetType(TextInputType::TEXT); });
1335 GetFocus();
1336
1337 /**
1338 * @tc.step: step2. Call IsPointInRect
1339 */
1340 OffsetF leftTop(0.0f, 0.0f);
1341 OffsetF leftBottom(0.0f, 5.0f);
1342 OffsetF rightTop(5.0f, 0.0f);
1343 OffsetF rightBottom(5.0f, 5.0f);
1344 OffsetF point(1.0f, 1.0f);
1345 auto ret = pattern_->selectOverlay_->IsPointInRect(point, leftBottom, rightBottom, rightTop, leftTop);
1346 EXPECT_TRUE(ret);
1347 point = OffsetF(-1.0f, -1.0f);
1348 ret = pattern_->selectOverlay_->IsPointInRect(point, leftBottom, rightBottom, rightTop, leftTop);
1349 EXPECT_FALSE(ret);
1350 }
1351 }