1 /* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #include "gtest/gtest.h" 17 18 #include "core/common/ime/text_editing_value.h" 19 #include "core/common/ime/text_input_action.h" 20 #include "core/common/ime/text_input_configuration.h" 21 #include "core/common/ime/text_input_formatter.h" 22 #include "core/common/ime/text_input_proxy.h" 23 #include "core/common/ime/text_input_type.h" 24 #include "frameworks/bridge/common/utils/utils.h" 25 26 using namespace testing; 27 using namespace testing::ext; 28 29 namespace OHOS::Ace { 30 namespace { 31 const std::string TYPE_TEST = "type"; 32 const std::string OBSCURE_TEXT_TEST = "obscureText"; 33 const std::string ACTION_TEST = "action"; 34 const std::string AUTO_CORRECT_TEST = "autoCorrect"; 35 const std::string ACTION_LABEL_TEST = "actionLabel"; 36 const std::string CAPITALIZATION_TEST = "capitalization"; 37 const std::string KEYBOARD_APPEARANCE_TEST = "keyboardAppearance"; 38 const std::string STR_ACTION_LABEL_TEST = "test_actionLabel"; 39 const std::string STR_CAPITALIZATION_TEST = "test_capitalization"; 40 const std::string STR_KEYBOARD_APPEARANCE_TEST = "test_keyboardAppearance"; 41 const std::string TEXT_TEST_VALUE = "text_test_value"; 42 const std::string HINT_TEST_VALUE = "hint_test_value"; 43 const std::string WIDE_TEXT_TEST = "text_ttext_test_valueest_value"; 44 constexpr int32_t LESS_THAN_TEXT_LENGTH = 6; 45 constexpr int32_t GREATER_THAN_TEXT_LENGTH = 20; 46 constexpr int32_t WIDE_TEXT_TEST_LENGTH = 30; 47 constexpr int32_t BEFOR_SELECTION_TEST_LENGTH = 12; 48 } // namespace 49 50 class ImeTest : public testing::Test {}; 51 52 /** 53 * @tc.name: CastToTextInputTypeTest001 54 * @tc.desc: Test cast to text input type. 55 * @tc.type: FUNC 56 */ 57 HWTEST_F(ImeTest, CastToTextInputTypeTest001, TestSize.Level1) 58 { 59 /** 60 * @tc.steps: step1. Value is greater than BEGIN and less than END. 61 * @tc.expected: step1. The return value is the set value. 62 */ 63 EXPECT_EQ(CastToTextInputType(1), TextInputType::MULTILINE); 64 /** 65 * @tc.steps: step2. Value is less than BEGIN. 66 * @tc.expected: step2. The return value is set to TEXT. 67 */ 68 EXPECT_EQ(CastToTextInputType(-2), TextInputType::TEXT); 69 /** 70 * @tc.steps: step3. Value is greater than END. 71 * @tc.expected: step3. The return value is set to TEXT. 72 */ 73 EXPECT_EQ(CastToTextInputType(static_cast<int32_t>(TextInputType::END) + 1), TextInputType::TEXT); 74 /** 75 * @tc.steps: step4. Value is set to zero. 76 * @tc.expected: step4. The return value is set to TEXT. 77 */ 78 EXPECT_EQ(CastToTextInputType(0), TextInputType::TEXT); 79 } 80 81 /** 82 * @tc.name: CastToTextInputActionTest001 83 * @tc.desc: Test convert to Text Input Operation. 84 * @tc.type: FUNC 85 */ 86 HWTEST_F(ImeTest, CastToTextInputActionTest001, TestSize.Level1) 87 { 88 /** 89 * @tc.steps: step1. Value is greater than BEGIN and less than END. 90 * @tc.expected: step1. The return value is the set value. 91 */ 92 EXPECT_EQ(CastToTextInputAction(1), TextInputAction::NONE); 93 /** 94 * @tc.steps: step2. Value is less than BEGIN. 95 * @tc.expected: step2. The return value is set to UNSPECIFIED. 96 */ 97 EXPECT_EQ(CastToTextInputAction(-2), TextInputAction::UNSPECIFIED); 98 /** 99 * @tc.steps: step3. Value is greater than END. 100 * @tc.expected: step3. The return value is set to UNSPECIFIED. 101 */ 102 EXPECT_EQ(CastToTextInputAction(10), TextInputAction::UNSPECIFIED); 103 /** 104 * @tc.steps: step4. Value is set to zero. 105 * @tc.expected: step4. The return value is set to BEGIN. 106 */ 107 EXPECT_EQ(CastToTextInputAction(0), TextInputAction::BEGIN); 108 } 109 110 /** 111 * @tc.name: ToJsonStringTest001 112 * @tc.desc: Test to json string. 113 * @tc.type: FUNC 114 */ 115 HWTEST_F(ImeTest, ToJsonStringTest001, TestSize.Level1) 116 { 117 /** 118 * @tc.steps: step1. Initialize object, use default data. 119 * @tc.expected: step1. Json data is set as default. 120 */ 121 TextInputConfiguration textInputConfiguration; 122 auto jsonString = textInputConfiguration.ToJsonString(); 123 auto json = JsonUtil::ParseJsonString(jsonString); 124 EXPECT_EQ(json->GetValue(TYPE_TEST)->GetInt(), static_cast<int32_t>(TextInputType::TEXT)); 125 EXPECT_EQ(json->GetValue(OBSCURE_TEXT_TEST)->GetBool(), false); 126 EXPECT_EQ(json->GetValue(ACTION_TEST)->GetInt(), static_cast<int32_t>(TextInputAction::UNSPECIFIED)); 127 EXPECT_EQ(json->GetValue(AUTO_CORRECT_TEST)->GetBool(), false); 128 } 129 130 /** 131 * @tc.name: ToJsonStringTest002 132 * @tc.desc: Test actionLabel,capitalization and keyboardAppearance not empty. 133 * @tc.type: FUNC 134 */ 135 HWTEST_F(ImeTest, ToJsonStringTest002, TestSize.Level1) 136 { 137 /** 138 * @tc.steps: step1. The initialization object, actionLabel, capitalization and keyboardAppearance, 139 * is set to a non empty string. 140 * @tc.expected: step1. Json is written to the set value. 141 */ 142 TextInputConfiguration textInputConfiguration; 143 textInputConfiguration.actionLabel = STR_ACTION_LABEL_TEST; 144 textInputConfiguration.capitalization = STR_CAPITALIZATION_TEST; 145 textInputConfiguration.keyboardAppearance = STR_KEYBOARD_APPEARANCE_TEST; 146 147 auto jsonString = textInputConfiguration.ToJsonString(); 148 auto json = JsonUtil::ParseJsonString(jsonString); 149 EXPECT_EQ(json->GetValue(ACTION_LABEL_TEST)->GetString(), STR_ACTION_LABEL_TEST); 150 EXPECT_EQ(json->GetValue(CAPITALIZATION_TEST)->GetString(), STR_CAPITALIZATION_TEST); 151 EXPECT_EQ(json->GetValue(KEYBOARD_APPEARANCE_TEST)->GetString(), STR_KEYBOARD_APPEARANCE_TEST); 152 } 153 154 /** 155 * @tc.name: TextEditingValueTest001 156 * @tc.desc: Test whether two textEditingValues are equal of operator. 157 * @tc.type: FUNC 158 */ 159 HWTEST_F(ImeTest, TextEditingValueTest001, TestSize.Level1) 160 { 161 /** 162 * @tc.steps: step1. initialize variable. 163 */ 164 TextEditingValue textEditingValue; 165 TextEditingValue textEditingValueOther; 166 textEditingValue.selection.baseOffset = 10; 167 168 /** 169 * @tc.steps: step2. Initialize the property of textEditingValue. 170 * @tc.expected: step2. TextEditingValue is not equal to textEditingValueOther. 171 */ 172 EXPECT_FALSE(textEditingValue == textEditingValueOther); 173 EXPECT_TRUE(textEditingValue != textEditingValueOther); 174 175 /** 176 * @tc.steps: step3. Initialize the property of textEditingValue to make baseOffset 177 * equal to the baseOffset of textEditingValue. 178 * @tc.expected: step3. TextEditingValue is equal to textEditingValueOther. 179 */ 180 textEditingValueOther.selection.baseOffset = 10; 181 EXPECT_TRUE(textEditingValue == textEditingValueOther); 182 EXPECT_FALSE(textEditingValue != textEditingValueOther); 183 } 184 185 /** 186 * @tc.name: MoveLeftTest001 187 * @tc.desc: Test an invalid extentOffset of MoveLeft. 188 * @tc.type: FUNC 189 */ 190 HWTEST_F(ImeTest, MoveLeftTest001, TestSize.Level1) 191 { 192 /** 193 * @tc.steps: step1. Use the default extentOffset value. 194 * @tc.expected: step1. ExtentOffset and baseOffset are both set to zero. 195 */ 196 TextEditingValue textEditingValue; 197 textEditingValue.MoveLeft(); 198 EXPECT_EQ(textEditingValue.selection.baseOffset, 0); 199 EXPECT_EQ(textEditingValue.selection.extentOffset, 0); 200 } 201 202 /** 203 * @tc.name: MoveLeftTest002 204 * @tc.desc: Test extentOffset is longer than the text of MoveLeft. 205 * @tc.type: FUNC 206 */ 207 HWTEST_F(ImeTest, MoveLeftTest002, TestSize.Level1) 208 { 209 /** 210 * @tc.steps: step1. Set the value of extentOffset, and the extentOffset is greater than the length of text. 211 * @tc.expected: step1. ExtentOffset and baseOffset are both set to the length of text. 212 */ 213 TextEditingValue textEditingValue; 214 textEditingValue.selection.extentOffset = GREATER_THAN_TEXT_LENGTH; 215 textEditingValue.text = "text_test"; 216 textEditingValue.MoveLeft(); 217 EXPECT_EQ(textEditingValue.selection.baseOffset, textEditingValue.text.length()); 218 EXPECT_EQ(textEditingValue.selection.extentOffset, textEditingValue.text.length()); 219 } 220 221 /** 222 * @tc.name: MoveLeftTest003 223 * @tc.desc: Test that the length of extentOffset is less than the length of text of MoveLeft. 224 * @tc.type: FUNC 225 */ 226 HWTEST_F(ImeTest, MoveLeftTest003, TestSize.Level1) 227 { 228 /** 229 * @tc.steps: step1. Initialize the value of extentOffset so that it is less than the length of text. 230 * @tc.expected: step1. ExtentOffset and baseOffset are set to ExtentOffset minus one. 231 */ 232 TextEditingValue textEditingValue; 233 textEditingValue.selection.extentOffset = LESS_THAN_TEXT_LENGTH; 234 textEditingValue.text = TEXT_TEST_VALUE; 235 textEditingValue.MoveLeft(); 236 EXPECT_EQ(textEditingValue.selection.baseOffset, LESS_THAN_TEXT_LENGTH - 1); 237 EXPECT_EQ(textEditingValue.selection.extentOffset, LESS_THAN_TEXT_LENGTH - 1); 238 } 239 240 /** 241 * @tc.name: MoveRightTest001 242 * @tc.desc: Test an invalid extentOffset of MoveRight. 243 * @tc.type: FUNC 244 */ 245 HWTEST_F(ImeTest, MoveRightTest001, TestSize.Level1) 246 { 247 /** 248 * @tc.steps: step1. When the member text of the selection is empty. 249 * @tc.expected: step1. Calling the MoveRight method, both ExtentOffset and baseOffset are negative ones. 250 */ 251 TextEditingValue textEditingValue; 252 textEditingValue.MoveRight(); 253 EXPECT_EQ(textEditingValue.selection.baseOffset, -1); 254 EXPECT_EQ(textEditingValue.selection.extentOffset, -1); 255 256 /** 257 * @tc.steps: step2. When the length of the member text variable of TextEditingValue is greater than 0. 258 * @tc.expected: step2. ExtentOffset and baseOffset are both is textEditingValue.text.length(). 259 */ 260 textEditingValue.text = "textEditingValue"; 261 textEditingValue.MoveRight(); 262 EXPECT_EQ(textEditingValue.selection.baseOffset, textEditingValue.text.length()); 263 EXPECT_EQ(textEditingValue.selection.extentOffset, textEditingValue.text.length()); 264 265 /** 266 * @tc.steps: step3. When the length of the member text variable of TextEditingValue is greater than 0. 267 * and textEditingValue.selection.baseOffset greater than length of text. 268 * @tc.expected: step3. ExtentOffset and baseOffset are both is textEditingValue.text.length(). 269 */ 270 textEditingValue.text = "textEdit"; 271 textEditingValue.MoveRight(); 272 EXPECT_EQ(textEditingValue.selection.baseOffset, textEditingValue.text.length()); 273 EXPECT_EQ(textEditingValue.selection.extentOffset, textEditingValue.text.length()); 274 } 275 276 /** 277 * @tc.name: MoveRightTest002 278 * @tc.desc: Test extentOffset is longer than the text of MoveRight. 279 * @tc.type: FUNC 280 */ 281 HWTEST_F(ImeTest, MoveRightTest002, TestSize.Level1) 282 { 283 /** 284 * @tc.steps: step1. Set the value of extentOffset, and the extentOffset is greater than the length of text. 285 * @tc.expected: step1. ExtentOffset and baseOffset are both set to the length of text. 286 */ 287 TextEditingValue textEditingValue; 288 textEditingValue.selection.extentOffset = GREATER_THAN_TEXT_LENGTH; 289 textEditingValue.text = "text_test"; 290 textEditingValue.MoveRight(); 291 EXPECT_EQ(textEditingValue.selection.baseOffset, textEditingValue.text.length()); 292 EXPECT_EQ(textEditingValue.selection.extentOffset, textEditingValue.text.length()); 293 } 294 295 /** 296 * @tc.name: MoveRightTest003 297 * @tc.desc: Test that the length of extentOffset is less than the length of text. 298 * @tc.type: FUNC 299 */ 300 HWTEST_F(ImeTest, MoveRightTest003, TestSize.Level1) 301 { 302 /** 303 * @tc.steps: step1. Initialize the value of extentOffset so that it is less than the length of text. 304 * @tc.expected: step1. ExtentOffset and baseOffset are set to LESS_ THAN_ TEXT_ LENGTH length plus one. 305 */ 306 TextEditingValue textEditingValue; 307 textEditingValue.selection.extentOffset = LESS_THAN_TEXT_LENGTH; 308 textEditingValue.text = TEXT_TEST_VALUE; 309 textEditingValue.MoveRight(); 310 EXPECT_EQ(textEditingValue.selection.baseOffset, LESS_THAN_TEXT_LENGTH + 1); 311 EXPECT_EQ(textEditingValue.selection.extentOffset, LESS_THAN_TEXT_LENGTH + 1); 312 } 313 314 /** 315 * @tc.name: MoveToPositionTest001 316 * @tc.desc: Test invalid position of MoveToPosition. 317 * @tc.type: FUNC 318 */ 319 HWTEST_F(ImeTest, MoveToPositionTest001, TestSize.Level1) 320 { 321 /** 322 * @tc.steps: step1. Set the value of position as invalid. 323 * @tc.expected: step1. ExtentOffset and baseOffset are both set to zero. 324 */ 325 TextEditingValue textEditingValue; 326 textEditingValue.MoveToPosition(-1); 327 EXPECT_EQ(textEditingValue.selection.baseOffset, 0); 328 EXPECT_EQ(textEditingValue.selection.extentOffset, 0); 329 } 330 331 /** 332 * @tc.name: MoveToPositionTest002 333 * @tc.desc: Test a valid Position of MoveToPosition. 334 * @tc.type: FUNC 335 */ 336 HWTEST_F(ImeTest, MoveToPositionTest002, TestSize.Level1) 337 { 338 /** 339 * @tc.steps: step1. Initialize the value of extentOffset so that it is less than the length of text. 340 * @tc.expected: step1. ExtentOffset and baseOffset are set to the length of text. 341 */ 342 TextEditingValue textEditingValue; 343 textEditingValue.text = TEXT_TEST_VALUE; 344 textEditingValue.MoveToPosition(LESS_THAN_TEXT_LENGTH); 345 EXPECT_EQ(textEditingValue.selection.baseOffset, LESS_THAN_TEXT_LENGTH); 346 EXPECT_EQ(textEditingValue.selection.extentOffset, LESS_THAN_TEXT_LENGTH); 347 348 textEditingValue.MoveToPosition(GREATER_THAN_TEXT_LENGTH); 349 EXPECT_EQ(textEditingValue.selection.baseOffset, textEditingValue.text.length()); 350 EXPECT_EQ(textEditingValue.selection.extentOffset, textEditingValue.text.length()); 351 } 352 353 /** 354 * @tc.name: UpdateSelectionTest001 355 * @tc.desc: Test an invalid start of UpdateSelection. 356 * @tc.type: FUNC 357 */ 358 HWTEST_F(ImeTest, UpdateSelectionTest001, TestSize.Level1) 359 { 360 /** 361 * @tc.steps: step1. Set start as invalid value and end as zero. 362 * @tc.expected: step1. ExtentOffset and baseOffset are both set to zero. 363 */ 364 TextEditingValue textEditingValue; 365 textEditingValue.UpdateSelection(-1, 0); 366 EXPECT_EQ(textEditingValue.selection.baseOffset, 0); 367 EXPECT_EQ(textEditingValue.selection.extentOffset, 0); 368 } 369 370 /** 371 * @tc.name: UpdateSelectionTest002 372 * @tc.desc: Test end length is greater than text length of UpdateSelection. 373 * @tc.type: FUNC 374 */ 375 HWTEST_F(ImeTest, UpdateSelectionTest002, TestSize.Level1) 376 { 377 /** 378 * @tc.steps: step1. Set start as a valid value, and the end value is greater than the length of text. 379 * @tc.expected: step1. ExtentOffset is set to the length of text. 380 */ 381 TextEditingValue textEditingValue; 382 textEditingValue.text = TEXT_TEST_VALUE; 383 textEditingValue.UpdateSelection(LESS_THAN_TEXT_LENGTH, GREATER_THAN_TEXT_LENGTH); 384 EXPECT_EQ(textEditingValue.selection.baseOffset, LESS_THAN_TEXT_LENGTH); 385 EXPECT_EQ(textEditingValue.selection.extentOffset, textEditingValue.text.length()); 386 } 387 388 /** 389 * @tc.name: SelectionAwareTextManipulationTest001 390 * @tc.desc: Test the basic functions of Selection Aware Text Manipulation. 391 * @tc.type: FUNC 392 */ 393 HWTEST_F(ImeTest, SelectionAwareTextManipulationTest001, TestSize.Level1) 394 { 395 /** 396 * @tc.steps: step1. Initialize the baseOffset and extOffset values, and the baseOffset is less than extOffset. 397 * @tc.expected: step1. Consistent with expected results. 398 */ 399 TextEditingValue textEditingValue; 400 textEditingValue.text = TEXT_TEST_VALUE; 401 textEditingValue.UpdateSelection(LESS_THAN_TEXT_LENGTH, GREATER_THAN_TEXT_LENGTH); 402 textEditingValue.SelectionAwareTextManipulation( __anona02eb94a0202(std::wstring& manipulateText) 403 [](std::wstring& manipulateText) { manipulateText += manipulateText; }); 404 EXPECT_EQ(textEditingValue.text, WIDE_TEXT_TEST); 405 EXPECT_EQ(textEditingValue.selection.baseOffset, BEFOR_SELECTION_TEST_LENGTH); 406 EXPECT_EQ(textEditingValue.selection.extentOffset, WIDE_TEXT_TEST_LENGTH); 407 } 408 409 /** 410 * @tc.name: SelectionAwareTextManipulationTest002 411 * @tc.desc: Test manipulation is null of SelectionAwareTextManipulation. 412 * @tc.type: FUNC 413 */ 414 HWTEST_F(ImeTest, SelectionAwareTextManipulationTest002, TestSize.Level1) 415 { 416 /** 417 * @tc.steps: step1. Initialization parameter, manipulateText is set to zero. 418 * @tc.expected: step1. ManipulateText is null, the function exits, and the text remains unchanged. 419 */ 420 TextEditingValue textEditingValue; 421 textEditingValue.text = TEXT_TEST_VALUE; 422 textEditingValue.UpdateSelection(LESS_THAN_TEXT_LENGTH, GREATER_THAN_TEXT_LENGTH); 423 textEditingValue.SelectionAwareTextManipulation(0); 424 EXPECT_EQ(textEditingValue.text, TEXT_TEST_VALUE); 425 } 426 427 /** 428 * @tc.name: SelectionAwareTextManipulationTest003 429 * @tc.desc: Test start is greater than end of SelectionAwareTextManipulation. 430 * @tc.type: FUNC 431 */ 432 HWTEST_F(ImeTest, SelectionAwareTextManipulationTest003, TestSize.Level1) 433 { 434 /** 435 * @tc.steps: step1. Initialization parameter, and the value of baseOffset is greater than 436 * the value of externtOffset. 437 * @tc.expected: step1. The parameter is invalid, the function exits, and the parameter remains unchanged. 438 */ 439 TextEditingValue textEditingValue; 440 textEditingValue.text = TEXT_TEST_VALUE; 441 textEditingValue.selection.baseOffset = GREATER_THAN_TEXT_LENGTH; 442 textEditingValue.selection.extentOffset = LESS_THAN_TEXT_LENGTH; 443 textEditingValue.SelectionAwareTextManipulation( __anona02eb94a0302(std::wstring& manipulateText) 444 [](std::wstring& manipulateText) { manipulateText += manipulateText; }); 445 EXPECT_EQ(textEditingValue.text, TEXT_TEST_VALUE); 446 EXPECT_EQ(textEditingValue.selection.baseOffset, GREATER_THAN_TEXT_LENGTH); 447 EXPECT_EQ(textEditingValue.selection.extentOffset, LESS_THAN_TEXT_LENGTH); 448 } 449 450 /** 451 * @tc.name: SelectionAwareTextManipulationTest005 452 * @tc.desc: Test that start and end are equal of SelectionAwareTextManipulation. 453 * @tc.type: FUNC 454 */ 455 HWTEST_F(ImeTest, SelectionAwareTextManipulationTest005, TestSize.Level1) 456 { 457 /** 458 * @tc.steps: step1. Set the value of baseOffset equal to that of externtOffset. 459 * @tc.expected: step1. Equal to expected results. 460 */ 461 TextEditingValue textEditingValue; 462 textEditingValue.text = TEXT_TEST_VALUE; 463 textEditingValue.selection.baseOffset = LESS_THAN_TEXT_LENGTH; 464 textEditingValue.selection.extentOffset = LESS_THAN_TEXT_LENGTH; 465 textEditingValue.SelectionAwareTextManipulation( __anona02eb94a0402(std::wstring& manipulateText) 466 [](std::wstring& manipulateText) { manipulateText += manipulateText; }); 467 EXPECT_EQ(textEditingValue.text, WIDE_TEXT_TEST); 468 EXPECT_EQ(textEditingValue.selection.baseOffset, BEFOR_SELECTION_TEST_LENGTH); 469 EXPECT_EQ(textEditingValue.selection.extentOffset, BEFOR_SELECTION_TEST_LENGTH); 470 } 471 472 /** 473 * @tc.name: GetBeforeSelectionTest001 474 * @tc.desc: Test the basic functions of GetBeforeSelection. 475 * @tc.type: FUNC 476 */ 477 HWTEST_F(ImeTest, GetBeforeSelectionTest001, TestSize.Level1) 478 { 479 /** 480 * @tc.steps: step1. Initialize baseOffset so that it is greater than the length of text. 481 * @tc.expected: step1. Function exit, return empty string. 482 */ 483 TextEditingValue textEditingValue; 484 textEditingValue.text = TEXT_TEST_VALUE; 485 textEditingValue.selection.baseOffset = 20; 486 auto beforeText = textEditingValue.GetBeforeSelection(); 487 EXPECT_EQ(beforeText, ""); 488 489 /** 490 * @tc.steps: step2. Initialization parameter. The length of baseOffset is greater than zero and less than text. 491 * @tc.expected: step2. Return expected results. 492 */ 493 textEditingValue.selection.baseOffset = LESS_THAN_TEXT_LENGTH; 494 textEditingValue.selection.extentOffset = GREATER_THAN_TEXT_LENGTH; 495 beforeText = textEditingValue.GetBeforeSelection(); 496 EXPECT_EQ(beforeText, "text_t"); 497 498 /** 499 * @tc.steps: step3. Initialization parameter, baseOffset is less than zero. 500 * @tc.expected: step3. Function exit, return empty string. 501 */ 502 textEditingValue.selection.baseOffset = -1; 503 beforeText = textEditingValue.GetBeforeSelection(); 504 EXPECT_EQ(beforeText, ""); 505 } 506 507 /** 508 * @tc.name: GetSelectedTextTest001 509 * @tc.desc: Test the basic functions of GetSelectedText without parameters. 510 * @tc.type: FUNC 511 */ 512 HWTEST_F(ImeTest, GetSelectedTextTest001, TestSize.Level1) 513 { 514 /** 515 * @tc.steps: step1. Initialization parameter, extentOffset is greater than the length of text. 516 * @tc.expected: step1. Function exit, return empty string. 517 */ 518 TextEditingValue textEditingValue; 519 textEditingValue.text = TEXT_TEST_VALUE; 520 textEditingValue.selection.baseOffset = LESS_THAN_TEXT_LENGTH; 521 textEditingValue.selection.extentOffset = GREATER_THAN_TEXT_LENGTH; 522 auto beforeText = textEditingValue.GetSelectedText(); 523 EXPECT_EQ(beforeText, ""); 524 525 /** 526 * @tc.steps: step2. Initialization parameter, baseOffset is less than zero. 527 * @tc.expected: step2. Output expected results. 528 */ 529 textEditingValue.UpdateSelection(-1, GREATER_THAN_TEXT_LENGTH); 530 beforeText = textEditingValue.GetSelectedText(); 531 EXPECT_EQ(beforeText, TEXT_TEST_VALUE); 532 533 /** 534 * @tc.steps: step3. Initialization parameter, baseOffset is greater than zero and less than extentOffset. 535 * @tc.expected: step3. Output expected results. 536 */ 537 textEditingValue.UpdateSelection(LESS_THAN_TEXT_LENGTH, GREATER_THAN_TEXT_LENGTH); 538 beforeText = textEditingValue.GetSelectedText(); 539 EXPECT_EQ(beforeText, "est_value"); 540 541 /** 542 * @tc.steps: step4. Initialization parameter, baseOffset is equal to extentOffset. 543 * @tc.expected: step4. Function exit, return empty string. 544 */ 545 textEditingValue.selection.baseOffset = LESS_THAN_TEXT_LENGTH; 546 textEditingValue.selection.extentOffset = LESS_THAN_TEXT_LENGTH; 547 beforeText = textEditingValue.GetSelectedText(); 548 EXPECT_EQ(beforeText, ""); 549 } 550 551 /** 552 * @tc.name: GetSelectedTextTest002 553 * @tc.desc: Test the basic functions of GetSelectedText with parameters. 554 * @tc.type: FUNC 555 */ 556 HWTEST_F(ImeTest, GetSelectedTextTest002, TestSize.Level1) 557 { 558 /** 559 * @tc.steps: step1. Initialization parameter, extentOffset is greater than the length of text. 560 * @tc.expected: step1. Output expected results. 561 */ 562 TextEditingValue textEditingValue; 563 TextSelection textSelection; 564 textEditingValue.text = TEXT_TEST_VALUE; 565 textSelection.baseOffset = LESS_THAN_TEXT_LENGTH; 566 textSelection.extentOffset = GREATER_THAN_TEXT_LENGTH; 567 auto beforeText = textEditingValue.GetSelectedText(textSelection); 568 EXPECT_EQ(beforeText, "est_value"); 569 570 /** 571 * @tc.steps: step2. Initialization parameter, baseOffset is less than zero. 572 * @tc.expected: step2. Output expected results. 573 */ 574 textEditingValue.UpdateSelection(-1, GREATER_THAN_TEXT_LENGTH); 575 textSelection.baseOffset = -1; 576 textSelection.extentOffset = 15; 577 beforeText = textEditingValue.GetSelectedText(textSelection); 578 EXPECT_EQ(beforeText, "text_test_value"); 579 580 /** 581 * @tc.steps: step3. Initialization parameter, baseOffset is greater than zero, 582 * and extentOffset is equal to the text length. 583 * @tc.expected: step3. Output expected results. 584 */ 585 textSelection.baseOffset = LESS_THAN_TEXT_LENGTH; 586 textSelection.extentOffset = 15; 587 beforeText = textEditingValue.GetSelectedText(textSelection); 588 EXPECT_EQ(beforeText, "est_value"); 589 590 /** 591 * @tc.steps: step4. Initialization parameter, baseOffset is equal to extentOffset. 592 * @tc.expected: step4. Function exit, return empty string. 593 */ 594 textSelection.baseOffset = LESS_THAN_TEXT_LENGTH; 595 textSelection.extentOffset = LESS_THAN_TEXT_LENGTH; 596 beforeText = textEditingValue.GetSelectedText(textSelection); 597 EXPECT_EQ(beforeText, ""); 598 } 599 600 /** 601 * @tc.name: GetAfterSelectionTest001 602 * @tc.desc: Test the basic functions of GetAfterSelection. 603 * @tc.type: FUNC 604 */ 605 HWTEST_F(ImeTest, GetAfterSelectionTest001, TestSize.Level1) 606 { 607 /** 608 * @tc.steps: step1. Initialization parameter, the value of extentOffset is greater than the length of text. 609 * @tc.expected: step1. Function exit, return empty string. 610 */ 611 TextEditingValue textEditingValue; 612 textEditingValue.text = TEXT_TEST_VALUE; 613 textEditingValue.selection.baseOffset = LESS_THAN_TEXT_LENGTH; 614 textEditingValue.selection.extentOffset = GREATER_THAN_TEXT_LENGTH; 615 auto beforeText = textEditingValue.GetAfterSelection(); 616 EXPECT_EQ(beforeText, ""); 617 618 /** 619 * @tc.steps: step2. Initialization parameter, the value of extentOffset is less than zero. 620 * @tc.expected: step2. Output expected results. 621 */ 622 textEditingValue.selection.extentOffset = -1; 623 beforeText = textEditingValue.GetAfterSelection(); 624 EXPECT_EQ(beforeText, "est_value"); 625 626 /** 627 * @tc.steps: step3. Initialization parameter, the value of externtOffset is greater than zero 628 * and less than the length of text. 629 * @tc.expected: step3. Output expected results. 630 */ 631 textEditingValue.selection.baseOffset = 5; 632 textEditingValue.selection.extentOffset = 12; 633 beforeText = textEditingValue.GetAfterSelection(); 634 EXPECT_EQ(beforeText, "lue"); 635 } 636 637 /** 638 * @tc.name: DeleteTest001 639 * @tc.desc: Test the basic functions of Delete. 640 * @tc.type: FUNC 641 */ 642 HWTEST_F(ImeTest, DeleteTest001, TestSize.Level1) 643 { 644 /** 645 * @tc.steps: step1. Initialization parameter, baseOffset is less than externtOffset. 646 * @tc.expected: step1. Output expected results. 647 */ 648 TextEditingValue textEditingValue; 649 textEditingValue.text = TEXT_TEST_VALUE; 650 textEditingValue.Delete(LESS_THAN_TEXT_LENGTH, GREATER_THAN_TEXT_LENGTH); 651 EXPECT_EQ(textEditingValue.text, "text_t"); 652 EXPECT_EQ(textEditingValue.selection.baseOffset, 6); 653 EXPECT_EQ(textEditingValue.selection.extentOffset, 6); 654 655 /** 656 * @tc.steps: step2. Initialization parameter, baseOffset is greater than extentOffset. 657 * @tc.expected: step2. Output expected results. 658 */ 659 textEditingValue.selection.Update(-1); 660 textEditingValue.Delete(20, 25); 661 EXPECT_EQ(textEditingValue.selection.baseOffset, -1); 662 EXPECT_EQ(textEditingValue.selection.extentOffset, -1); 663 } 664 665 /** 666 * @tc.name: FormatTest001 667 * @tc.desc: Test invalid parameters of Format. 668 * @tc.type: FUNC 669 */ 670 HWTEST_F(ImeTest, FormatTest001, TestSize.Level1) 671 { 672 /** 673 * @tc.steps: step1. Initialize the parameter so that the length of the text is less than the input parameter. 674 * @tc.expected: step1. Function exit, selection is the default value. 675 */ 676 TextEditingValue newValue; 677 TextEditingValue oldValue; 678 LengthLimitingFormatter lengthLimitingFormatter(20); 679 newValue.text = TEXT_TEST_VALUE; 680 681 lengthLimitingFormatter.Format(oldValue, newValue); 682 EXPECT_EQ(newValue.text, TEXT_TEST_VALUE); 683 EXPECT_EQ(newValue.selection.baseOffset, -1); 684 EXPECT_EQ(newValue.selection.extentOffset, -1); 685 } 686 687 /** 688 * @tc.name: FormatTest002 689 * @tc.desc: Test invalid parameters of text length. 690 * @tc.type: FUNC 691 */ 692 HWTEST_F(ImeTest, FormatTest002, TestSize.Level1) 693 { 694 /** 695 * @tc.steps: step1. Initialization parameter, start is less than end, and limit is equal to start. 696 * @tc.expected: step1. Output expected results. 697 */ 698 TextEditingValue newValue; 699 TextEditingValue oldValue; 700 LengthLimitingFormatter lengthLimitingFormatter(LESS_THAN_TEXT_LENGTH); 701 newValue.text = TEXT_TEST_VALUE; 702 newValue.selection.baseOffset = LESS_THAN_TEXT_LENGTH; 703 newValue.selection.extentOffset = GREATER_THAN_TEXT_LENGTH; 704 705 lengthLimitingFormatter.Format(oldValue, newValue); 706 EXPECT_EQ(newValue.text, TEXT_TEST_VALUE); 707 EXPECT_EQ(newValue.selection.baseOffset, 6); 708 EXPECT_EQ(newValue.selection.extentOffset, 20); 709 } 710 711 /** 712 * @tc.name: FormatTest003 713 * @tc.desc: Test basic functions of Format. 714 * @tc.type: FUNC 715 */ 716 HWTEST_F(ImeTest, FormatTest003, TestSize.Level1) 717 { 718 /** 719 * @tc.steps: step1. Initialize parameters to make removeBeforeExtent and removeAfterExtent greater than zero. 720 * @tc.expected: step1. Output expected results. 721 */ 722 TextEditingValue newValue; 723 TextEditingValue oldValue; 724 LengthLimitingFormatter lengthLimitingFormatter(1); 725 newValue.text = TEXT_TEST_VALUE; 726 newValue.selection.baseOffset = LESS_THAN_TEXT_LENGTH; 727 newValue.selection.extentOffset = 10; 728 729 lengthLimitingFormatter.Format(oldValue, newValue); 730 EXPECT_EQ(newValue.text, "v"); 731 EXPECT_EQ(newValue.selection.baseOffset, 0); 732 EXPECT_EQ(newValue.selection.extentOffset, 0); 733 } 734 735 /** 736 * @tc.name: FormatTest004 737 * @tc.desc: Test remove after extent of format. 738 * @tc.type: FUNC 739 */ 740 HWTEST_F(ImeTest, FormatTest004, TestSize.Level1) 741 { 742 /** 743 * @tc.steps: step1. Initialize the parameter to make removeAfterExtent less than or equal to zero. 744 * @tc.expected: step1. Output expected results. 745 */ 746 TextEditingValue newValue; 747 TextEditingValue oldValue; 748 LengthLimitingFormatter lengthLimitingFormatter(14); 749 newValue.text = TEXT_TEST_VALUE; 750 newValue.selection.extentOffset = 10; 751 752 lengthLimitingFormatter.Format(oldValue, newValue); 753 EXPECT_EQ(newValue.text, "text_testvalue"); 754 EXPECT_EQ(newValue.selection.baseOffset, 9); 755 EXPECT_EQ(newValue.selection.extentOffset, 9); 756 } 757 758 /** 759 * @tc.name: FormatTest005 760 * @tc.desc: Test invalid parameters of removeBeforeExtent. 761 * @tc.type: FUNC 762 */ 763 HWTEST_F(ImeTest, FormatTest005, TestSize.Level1) 764 { 765 /** 766 * @tc.steps: step1. Initialize parameters to make removeBeforeExtent less than or equal to zero. 767 * @tc.expected: step1. Output expected results. 768 */ 769 TextEditingValue newValue; 770 TextEditingValue oldValue; 771 LengthLimitingFormatter lengthLimitingFormatter(14); 772 newValue.text = TEXT_TEST_VALUE; 773 newValue.selection.extentOffset = 0; 774 775 lengthLimitingFormatter.Format(oldValue, newValue); 776 EXPECT_EQ(newValue.text, "text_test_valu"); 777 EXPECT_EQ(newValue.selection.baseOffset, -1); 778 } 779 780 /** 781 * @tc.name: FormatTest006 782 * @tc.desc: Test blackListCharsFormatter. 783 * @tc.type: FUNC 784 */ 785 HWTEST_F(ImeTest, FormatTest006, TestSize.Level1) 786 { 787 TextEditingValue newValue; 788 TextEditingValue oldValue; 789 std::wregex regex(L"[^\\d]"); 790 BlackListCharsFormatter blackListCharsFormatter(std::move(regex)); 791 792 newValue.text = "abc123"; 793 newValue.selection.extentOffset = 0; 794 795 blackListCharsFormatter.Format(oldValue, newValue); 796 797 EXPECT_EQ(newValue.text, "123"); 798 EXPECT_EQ(newValue.selection.baseOffset, -1); 799 } 800 801 /** 802 * @tc.name: FormatTest007 803 * @tc.desc: Test numberFormatter. 804 * @tc.type: FUNC 805 */ 806 HWTEST_F(ImeTest, FormatTest007, TestSize.Level1) 807 { 808 TextEditingValue newValue; 809 TextEditingValue oldValue; 810 NumberFormatter numberFormatter; 811 812 newValue.text = "123abc456def"; 813 newValue.selection.extentOffset = 0; 814 815 numberFormatter.Format(oldValue, newValue); 816 817 EXPECT_EQ(newValue.text, "123456e"); 818 EXPECT_EQ(newValue.selection.baseOffset, -1); 819 } 820 821 /** 822 * @tc.name: FormatTest008 823 * @tc.desc: Test phoneNumberFormatter. 824 * @tc.type: FUNC 825 */ 826 HWTEST_F(ImeTest, FormatTest008, TestSize.Level1) 827 { 828 TextEditingValue newValue; 829 TextEditingValue oldValue; 830 PhoneNumberFormatter phoneNumberFormatter; 831 832 newValue.text = "123-456-7890"; 833 newValue.selection.extentOffset = 0; 834 835 phoneNumberFormatter.Format(oldValue, newValue); 836 837 EXPECT_EQ(newValue.text, "123-456-7890"); 838 EXPECT_EQ(newValue.selection.baseOffset, -1); 839 840 newValue.text = "abc123"; 841 phoneNumberFormatter.Format(oldValue, newValue); 842 843 EXPECT_EQ(newValue.text, "123"); 844 EXPECT_EQ(newValue.selection.baseOffset, -1); 845 } 846 847 /** 848 * @tc.name: FormatTest009 849 * @tc.desc: Test emailFormatter. 850 * @tc.type: FUNC 851 */ 852 HWTEST_F(ImeTest, FormatTest009, TestSize.Level1) 853 { 854 TextEditingValue newValue; 855 TextEditingValue oldValue; 856 EmailFormatter emailFormatter; 857 858 newValue.text = "test@example"; 859 emailFormatter.Format(oldValue, newValue); 860 861 EXPECT_EQ(newValue.text, "test@example"); 862 EXPECT_EQ(newValue.selection.baseOffset, -1); 863 } 864 } // namespace OHOS::Ace 865