1 /* 2 * Copyright (C) 2021 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 "input_method_ability.h" 17 18 #include <cstdint> 19 #include <functional> 20 #include <gtest/gtest.h> 21 #include <string> 22 #include <string_ex.h> 23 #include <thread> 24 #include <unistd.h> 25 #include <vector> 26 27 #include "accesstoken_kit.h" 28 #include "global.h" 29 #include "i_input_data_channel.h" 30 #include "input_attribute.h" 31 #include "input_control_channel_stub.h" 32 #include "input_data_channel_proxy.h" 33 #include "input_data_channel_stub.h" 34 #include "input_method_agent_stub.h" 35 #include "input_method_controller.h" 36 #include "input_method_core_proxy.h" 37 #include "input_method_core_stub.h" 38 #include "message_handler.h" 39 #include "nativetoken_kit.h" 40 #include "token_setproc.h" 41 42 using namespace testing::ext; 43 using namespace OHOS::Security::AccessToken; 44 namespace OHOS { 45 namespace MiscServices { 46 constexpr uint32_t DEALY_TIME = 1; 47 std::u16string g_textTemp = u"我們我們ddddd"; 48 class InputMethodAbilityTest : public testing::Test { 49 public: 50 static std::string imeIdStopped_; 51 static std::mutex imeListenerCallbackLock_; 52 static std::condition_variable imeListenerCv_; 53 static bool showKeyboard_; 54 static std::mutex textListenerCallbackLock_; 55 static std::condition_variable textListenerCv_; 56 static int direction_; 57 static int deleteForwardLength_; 58 static int deleteBackwardLength_; 59 static std::u16string insertText_; 60 static int key_; 61 static int keyboardStatus_; 62 static bool status_; 63 static sptr<InputMethodController> imc_; 64 static sptr<InputMethodAbility> inputMethodAbility_; 65 66 class InputMethodEngineListenerImpl : public InputMethodEngineListener { 67 public: 68 InputMethodEngineListenerImpl() = default; 69 ~InputMethodEngineListenerImpl() = default; 70 OnKeyboardStatus(bool isShow)71 void OnKeyboardStatus(bool isShow) 72 { 73 showKeyboard_ = isShow; 74 InputMethodAbilityTest::imeListenerCv_.notify_one(); 75 IMSA_HILOGI("InputMethodEngineListenerImpl OnKeyboardStatus"); 76 } 77 OnInputStart()78 void OnInputStart() 79 { 80 IMSA_HILOGI("InputMethodEngineListenerImpl OnInputStart"); 81 } 82 OnInputStop(const std::string & imeId)83 void OnInputStop(const std::string &imeId) 84 { 85 imeIdStopped_ = imeId; 86 IMSA_HILOGI("InputMethodEngineListenerImpl OnInputStop"); 87 } 88 OnSetCallingWindow(uint32_t windowId)89 void OnSetCallingWindow(uint32_t windowId) 90 { 91 IMSA_HILOGI("InputMethodEngineListenerImpl OnSetCallingWindow"); 92 } 93 OnSetSubtype(const SubProperty & property)94 void OnSetSubtype(const SubProperty &property) 95 { 96 IMSA_HILOGI("InputMethodEngineListenerImpl OnSetSubtype"); 97 } 98 }; 99 class TextChangeListener : public OnTextChangedListener { 100 public: InsertText(const std::u16string & text)101 void InsertText(const std::u16string &text) override 102 { 103 insertText_ = text; 104 InputMethodAbilityTest::textListenerCv_.notify_one(); 105 } 106 DeleteForward(int32_t length)107 void DeleteForward(int32_t length) override 108 { 109 deleteForwardLength_ = length; 110 InputMethodAbilityTest::textListenerCv_.notify_one(); 111 IMSA_HILOGI("TextChangeListener: DeleteForward, length is: %{public}d", length); 112 } 113 DeleteBackward(int32_t length)114 void DeleteBackward(int32_t length) override 115 { 116 deleteBackwardLength_ = length; 117 InputMethodAbilityTest::textListenerCv_.notify_one(); 118 IMSA_HILOGI("TextChangeListener: DeleteBackward, direction is: %{public}d", length); 119 } 120 SendKeyEventFromInputMethod(const KeyEvent & event)121 void SendKeyEventFromInputMethod(const KeyEvent &event) override 122 { 123 } 124 SendKeyboardInfo(const KeyboardInfo & info)125 void SendKeyboardInfo(const KeyboardInfo &info) override 126 { 127 FunctionKey functionKey = info.GetFunctionKey(); 128 KeyboardStatus keyboardStatus = info.GetKeyboardStatus(); 129 key_ = (int)functionKey; 130 keyboardStatus_ = (int)keyboardStatus; 131 InputMethodAbilityTest::textListenerCv_.notify_one(); 132 } 133 SetKeyboardStatus(bool status)134 void SetKeyboardStatus(bool status) override 135 { 136 status_ = status; 137 } 138 MoveCursor(const Direction direction)139 void MoveCursor(const Direction direction) override 140 { 141 direction_ = (int)direction; 142 InputMethodAbilityTest::textListenerCv_.notify_one(); 143 IMSA_HILOGI("TextChangeListener: MoveCursor, direction is: %{public}d", direction); 144 } HandleSetSelection(int32_t start,int32_t end)145 void HandleSetSelection(int32_t start, int32_t end) override 146 { 147 } HandleExtendAction(int32_t action)148 void HandleExtendAction(int32_t action) override 149 { 150 } HandleSelect(int32_t keyCode,int32_t cursorMoveSkip)151 void HandleSelect(int32_t keyCode, int32_t cursorMoveSkip) override 152 { 153 } 154 }; GrantPermission()155 static void GrantPermission() 156 { 157 const char **perms = new const char *[1]; 158 perms[0] = "ohos.permission.CONNECT_IME_ABILITY"; 159 TokenInfoParams infoInstance = { 160 .dcapsNum = 0, 161 .permsNum = 1, 162 .aclsNum = 0, 163 .dcaps = nullptr, 164 .perms = perms, 165 .acls = nullptr, 166 .processName = "inputmethod_imf", 167 .aplStr = "system_core", 168 }; 169 uint64_t tokenId = GetAccessTokenId(&infoInstance); 170 int res = SetSelfTokenID(tokenId); 171 if (res == 0) { 172 IMSA_HILOGI("SetSelfTokenID success!"); 173 } else { 174 IMSA_HILOGE("SetSelfTokenID fail!"); 175 } 176 AccessTokenKit::ReloadNativeTokenInfo(); 177 delete[] perms; 178 } SetUpTestCase(void)179 static void SetUpTestCase(void) 180 { 181 IMSA_HILOGI("InputMethodAbilityTest::SetUpTestCase"); 182 GrantPermission(); 183 inputMethodAbility_ = InputMethodAbility::GetInstance(); 184 inputMethodAbility_->OnImeReady(); 185 sptr<OnTextChangedListener> textListener = new TextChangeListener(); 186 imc_ = InputMethodController::GetInstance(); 187 imc_->Attach(textListener); 188 } TearDownTestCase(void)189 static void TearDownTestCase(void) 190 { 191 IMSA_HILOGI("InputMethodAbilityTest::TearDownTestCase"); 192 imc_->Close(); 193 } SetUp()194 void SetUp() 195 { 196 IMSA_HILOGI("InputMethodAbilityTest::SetUp"); 197 } TearDown()198 void TearDown() 199 { 200 IMSA_HILOGI("InputMethodAbilityTest::TearDown"); 201 } 202 }; 203 204 std::string InputMethodAbilityTest::imeIdStopped_; 205 std::mutex InputMethodAbilityTest::imeListenerCallbackLock_; 206 std::condition_variable InputMethodAbilityTest::imeListenerCv_; 207 bool InputMethodAbilityTest::showKeyboard_ = true; 208 std::mutex InputMethodAbilityTest::textListenerCallbackLock_; 209 std::condition_variable InputMethodAbilityTest::textListenerCv_; 210 int InputMethodAbilityTest::direction_; 211 int InputMethodAbilityTest::deleteForwardLength_ = 0; 212 int InputMethodAbilityTest::deleteBackwardLength_ = 0; 213 std::u16string InputMethodAbilityTest::insertText_; 214 int InputMethodAbilityTest::key_ = 0; 215 int InputMethodAbilityTest::keyboardStatus_; 216 bool InputMethodAbilityTest::status_; 217 sptr<InputMethodController> InputMethodAbilityTest::imc_; 218 sptr<InputMethodAbility> InputMethodAbilityTest::inputMethodAbility_; 219 220 /** 221 * @tc.name: testSerializedInputAttribute 222 * @tc.desc: Checkout the serialization of InputAttribute. 223 * @tc.type: FUNC 224 */ 225 HWTEST_F(InputMethodAbilityTest, testSerializedInputAttribute, TestSize.Level0) 226 { 227 InputAttribute inAttribute; 228 inAttribute.inputPattern = InputAttribute::PATTERN_PASSWORD; 229 MessageParcel data; 230 EXPECT_TRUE(InputAttribute::Marshalling(inAttribute, data)); 231 InputAttribute outAttribute; 232 EXPECT_TRUE(InputAttribute::Unmarshalling(outAttribute, data)); 233 EXPECT_TRUE(outAttribute.GetSecurityFlag()); 234 } 235 236 /** 237 * @tc.name: testShowKeyboardInputMethodCoreProxy 238 * @tc.desc: Test InputMethodCoreProxy ShowKeyboard 239 * @tc.type: FUNC 240 * @tc.require: issueI5NXHK 241 */ 242 HWTEST_F(InputMethodAbilityTest, testShowKeyboardInputMethodCoreProxy, TestSize.Level0) 243 { 244 sptr<InputMethodCoreStub> coreStub = new InputMethodCoreStub(0); 245 sptr<IInputMethodCore> core = coreStub; 246 auto msgHandler = new (std::nothrow) MessageHandler(); 247 coreStub->SetMessageHandler(msgHandler); 248 sptr<InputDataChannelStub> channelStub = new InputDataChannelStub(); 249 250 MessageParcel data; 251 data.WriteRemoteObject(core->AsObject()); 252 data.WriteRemoteObject(channelStub->AsObject()); 253 sptr<IRemoteObject> coreObject = data.ReadRemoteObject(); 254 sptr<IRemoteObject> channelObject = data.ReadRemoteObject(); 255 256 sptr<InputMethodCoreProxy> coreProxy = new InputMethodCoreProxy(coreObject); 257 sptr<InputDataChannelProxy> channelProxy = new InputDataChannelProxy(channelObject); 258 SubProperty subProperty; 259 auto ret = coreProxy->showKeyboard(channelProxy, false, subProperty); 260 std::unique_lock<std::mutex> lock(InputMethodAbilityTest::imeListenerCallbackLock_); 261 auto cvStatus = InputMethodAbilityTest::imeListenerCv_.wait_for(lock, std::chrono::seconds(DEALY_TIME)); 262 EXPECT_EQ(ret, ErrorCode::NO_ERROR); 263 EXPECT_EQ(cvStatus, std::cv_status::timeout); 264 EXPECT_TRUE(InputMethodAbilityTest::showKeyboard_); 265 delete msgHandler; 266 } 267 268 /** 269 * @tc.name: testHideKeyboardSelfWithoutImeListener 270 * @tc.desc: InputMethodAbility HideKeyboardSelf Without ImeListener 271 * @tc.type: FUNC 272 * @tc.require: 273 * @tc.author: Hollokin 274 */ 275 HWTEST_F(InputMethodAbilityTest, testHideKeyboardSelfWithoutImeListener, TestSize.Level0) 276 { 277 IMSA_HILOGI("InputMethodAbility testHideKeyboardSelfWithoutImeListener START"); 278 std::unique_lock<std::mutex> lock(InputMethodAbilityTest::imeListenerCallbackLock_); 279 InputMethodAbilityTest::showKeyboard_ = true; 280 auto ret = inputMethodAbility_->HideKeyboardSelf(); 281 auto cvStatus = imeListenerCv_.wait_for(lock, std::chrono::seconds(DEALY_TIME)); 282 EXPECT_EQ(cvStatus, std::cv_status::timeout); 283 EXPECT_EQ(ret, ErrorCode::NO_ERROR); 284 EXPECT_TRUE(InputMethodAbilityTest::showKeyboard_); 285 } 286 287 /** 288 * @tc.name: testHideKeyboardSelf 289 * @tc.desc: InputMethodAbility HideKeyboardSelf 290 * @tc.type: FUNC 291 * @tc.require: 292 * @tc.author: Hollokin 293 */ 294 HWTEST_F(InputMethodAbilityTest, testHideKeyboardSelf, TestSize.Level0) 295 { 296 IMSA_HILOGI("InputMethodAbility testHideKeyboardSelf START"); 297 std::unique_lock<std::mutex> lock(InputMethodAbilityTest::imeListenerCallbackLock_); 298 InputMethodAbilityTest::showKeyboard_ = true; 299 inputMethodAbility_->setImeListener(std::make_shared<InputMethodEngineListenerImpl>()); 300 auto ret = inputMethodAbility_->HideKeyboardSelf(); 301 InputMethodAbilityTest::imeListenerCv_.wait_for( __anona0d5e97e0102null302 lock, std::chrono::seconds(DEALY_TIME), [] { return InputMethodAbilityTest::showKeyboard_ == false; }); 303 EXPECT_EQ(ret, ErrorCode::NO_ERROR); 304 EXPECT_FALSE(InputMethodAbilityTest::showKeyboard_); 305 } 306 307 /** 308 * @tc.name: testMoveCursor 309 * @tc.desc: InputMethodAbility MoveCursor 310 * @tc.type: FUNC 311 * @tc.require: 312 * @tc.author: Hollokin 313 */ 314 HWTEST_F(InputMethodAbilityTest, testMoveCursor, TestSize.Level0) 315 { 316 IMSA_HILOGI("InputMethodAbility MoveCursor Test START"); 317 constexpr int32_t keyCode = 4; 318 auto ret = inputMethodAbility_->MoveCursor(keyCode); // move cursor right 319 std::unique_lock<std::mutex> lock(InputMethodAbilityTest::textListenerCallbackLock_); 320 InputMethodAbilityTest::textListenerCv_.wait_for( __anona0d5e97e0202null321 lock, std::chrono::seconds(DEALY_TIME), [] { return InputMethodAbilityTest::direction_ == keyCode; }); 322 EXPECT_EQ(ret, ErrorCode::NO_ERROR); 323 EXPECT_EQ(InputMethodAbilityTest::direction_, keyCode); 324 } 325 326 /** 327 * @tc.name: testInsertText 328 * @tc.desc: InputMethodAbility InsertText 329 * @tc.type: FUNC 330 * @tc.require: 331 * @tc.author: Hollokin 332 */ 333 HWTEST_F(InputMethodAbilityTest, testInsertText, TestSize.Level0) 334 { 335 IMSA_HILOGI("InputMethodAbility InsertText Test START"); 336 std::string text = "text"; 337 std::u16string u16Text = Str8ToStr16(text); 338 auto ret = inputMethodAbility_->InsertText(text); 339 std::unique_lock<std::mutex> lock(InputMethodAbilityTest::textListenerCallbackLock_); 340 InputMethodAbilityTest::textListenerCv_.wait_for( __anona0d5e97e0302null341 lock, std::chrono::seconds(DEALY_TIME), [u16Text] { return InputMethodAbilityTest::insertText_ == u16Text; }); 342 EXPECT_EQ(ret, ErrorCode::NO_ERROR); 343 EXPECT_EQ(InputMethodAbilityTest::insertText_, u16Text); 344 } 345 346 /** 347 * @tc.name: testSendFunctionKey 348 * @tc.desc: InputMethodAbility SendFunctionKey 349 * @tc.type: FUNC 350 * @tc.require: 351 * @tc.author: Hollokin 352 */ 353 HWTEST_F(InputMethodAbilityTest, testSendFunctionKey, TestSize.Level0) 354 { 355 IMSA_HILOGI("InputMethodAbility SendFunctionKey Test START"); 356 constexpr int32_t funcKey = 1; 357 auto ret = inputMethodAbility_->SendFunctionKey(funcKey); 358 std::unique_lock<std::mutex> lock(InputMethodAbilityTest::textListenerCallbackLock_); 359 InputMethodAbilityTest::textListenerCv_.wait_for( __anona0d5e97e0402null360 lock, std::chrono::seconds(DEALY_TIME), [] { return InputMethodAbilityTest::key_ == funcKey; }); 361 EXPECT_EQ(ret, ErrorCode::NO_ERROR); 362 EXPECT_EQ(InputMethodAbilityTest::key_, funcKey); 363 } 364 365 /** 366 * @tc.name: testDeleteText 367 * @tc.desc: InputMethodAbility DeleteForward & DeleteBackward 368 * @tc.type: FUNC 369 * @tc.require: 370 * @tc.author: Hollokin 371 */ 372 HWTEST_F(InputMethodAbilityTest, testDeleteText, TestSize.Level0) 373 { 374 IMSA_HILOGI("InputMethodAbility testDelete Test START"); 375 int32_t deleteForwardLenth = 1; 376 auto ret = inputMethodAbility_->DeleteForward(deleteForwardLenth); 377 std::unique_lock<std::mutex> lock(InputMethodAbilityTest::textListenerCallbackLock_); 378 InputMethodAbilityTest::textListenerCv_.wait_for(lock, std::chrono::seconds(DEALY_TIME), __anona0d5e97e0502null379 [deleteForwardLenth] { return InputMethodAbilityTest::deleteForwardLength_ == deleteForwardLenth; }); 380 EXPECT_EQ(ret, ErrorCode::NO_ERROR); 381 EXPECT_EQ(InputMethodAbilityTest::deleteForwardLength_, deleteForwardLenth); 382 383 int32_t deleteBackwardLenth = 2; 384 ret = inputMethodAbility_->DeleteBackward(deleteBackwardLenth); 385 InputMethodAbilityTest::textListenerCv_.wait_for(lock, std::chrono::seconds(DEALY_TIME), __anona0d5e97e0602null386 [deleteBackwardLenth] { return InputMethodAbilityTest::deleteBackwardLength_ == deleteBackwardLenth; }); 387 EXPECT_EQ(ret, ErrorCode::NO_ERROR); 388 EXPECT_EQ(InputMethodAbilityTest::deleteBackwardLength_, deleteBackwardLenth); 389 } 390 391 /** 392 * @tc.name: testGetEnterKeyType 393 * @tc.desc: InputMethodAbility GetEnterKeyType & GetInputPattern 394 * @tc.type: FUNC 395 * @tc.require: 396 * @tc.author: Hollokin 397 */ 398 HWTEST_F(InputMethodAbilityTest, testGetEnterKeyType, TestSize.Level0) 399 { 400 IMSA_HILOGI("InputMethodAbility testGetEnterKeyType START"); 401 Configuration config; 402 EnterKeyType keyType = EnterKeyType::NEXT; 403 config.SetEnterKeyType(keyType); 404 TextInputType textInputType = TextInputType::DATETIME; 405 config.SetTextInputType(textInputType); 406 imc_->OnConfigurationChange(config); 407 int32_t keyType2; 408 auto ret = inputMethodAbility_->GetEnterKeyType(keyType2); 409 EXPECT_EQ(ret, ErrorCode::NO_ERROR); 410 EXPECT_EQ(keyType2, (int)keyType); 411 int32_t inputPattern; 412 ret = inputMethodAbility_->GetInputPattern(inputPattern); 413 EXPECT_EQ(ret, ErrorCode::NO_ERROR); 414 EXPECT_EQ(inputPattern, (int)textInputType); 415 } 416 417 /** 418 * @tc.name: testGetTextParamExceptionValidation_001 419 * @tc.desc: mSelectNewBegin = mSelectNewEnd> size() 420 * @tc.type: FUNC 421 * @tc.require: issuesI6PPJM 422 */ 423 HWTEST_F(InputMethodAbilityTest, testGetTextParamExceptionValidation_001, TestSize.Level0) 424 { 425 IMSA_HILOGI("InputMethodAbility testGetTextParamExceptionValidation_001 START"); 426 int32_t start = 10; 427 int32_t end = 10; 428 imc_->OnSelectionChange(g_textTemp, start, end); 429 int32_t number = 3; 430 std::u16string text; 431 auto ret = inputMethodAbility_->GetTextAfterCursor(number, text); 432 EXPECT_EQ(ret, ErrorCode::ERROR_CONTROLLER_INVOKING_FAILED); 433 EXPECT_EQ(text, u""); 434 ret = inputMethodAbility_->GetTextBeforeCursor(number, text); 435 EXPECT_EQ(ret, ErrorCode::ERROR_CONTROLLER_INVOKING_FAILED); 436 EXPECT_EQ(text, u""); 437 } 438 439 /** 440 * @tc.name: testGetTextParamExceptionValidation_002 441 * @tc.desc: mSelectNewBegin > mSelectNewEnd, mSelectNewBegin > size() 442 * @tc.type: FUNC 443 * @tc.require: issuesI6PPJM 444 */ 445 HWTEST_F(InputMethodAbilityTest, testGetTextParamExceptionValidation_002, TestSize.Level0) 446 { 447 IMSA_HILOGI("InputMethodAbility testGetTextParamExceptionValidation_002 START"); 448 int32_t start = 11; 449 int32_t end = 9; 450 imc_->OnSelectionChange(g_textTemp, start, end); 451 int32_t number = 3; 452 std::u16string text; 453 auto ret = inputMethodAbility_->GetTextAfterCursor(number, text); 454 EXPECT_EQ(ret, ErrorCode::ERROR_CONTROLLER_INVOKING_FAILED); 455 EXPECT_EQ(text, u""); 456 ret = inputMethodAbility_->GetTextBeforeCursor(number, text); 457 EXPECT_EQ(ret, ErrorCode::ERROR_CONTROLLER_INVOKING_FAILED); 458 EXPECT_EQ(text, u""); 459 } 460 461 /** 462 * @tc.name: testGetTextParamExceptionValidation_003 463 * @tc.desc: mSelectNewBegin < mSelectNewEnd, mSelectNewEnd > size() 464 * @tc.type: FUNC 465 * @tc.require: issuesI6PPJM 466 */ 467 HWTEST_F(InputMethodAbilityTest, testGetTextParamExceptionValidation_003, TestSize.Level0) 468 { 469 IMSA_HILOGI("InputMethodAbility testGetTextParamExceptionValidation_003 START"); 470 int32_t start = 9; 471 int32_t end = 11; 472 imc_->OnSelectionChange(g_textTemp, start, end); 473 int32_t number = 3; 474 std::u16string text; 475 auto ret = inputMethodAbility_->GetTextAfterCursor(number, text); 476 EXPECT_EQ(ret, ErrorCode::ERROR_CONTROLLER_INVOKING_FAILED); 477 EXPECT_EQ(text, u""); 478 ret = inputMethodAbility_->GetTextBeforeCursor(number, text); 479 EXPECT_EQ(ret, ErrorCode::ERROR_CONTROLLER_INVOKING_FAILED); 480 EXPECT_EQ(text, u""); 481 } 482 483 /** 484 * @tc.name: testGetTextAfterCursor_001 485 * @tc.desc: click textfield, mSelectNewEnd + number > size() 486 * @tc.type: FUNC 487 * @tc.require: issuesI6PPJM 488 */ 489 HWTEST_F(InputMethodAbilityTest, testGetTextAfterCursor_001, TestSize.Level0) 490 { 491 IMSA_HILOGI("InputMethodAbility testGetTextAfterCursor_001 START"); 492 int32_t start = 8; 493 int32_t end = 8; 494 imc_->OnSelectionChange(g_textTemp, start, end); 495 int32_t number = 3; 496 std::u16string text; 497 auto ret = inputMethodAbility_->GetTextAfterCursor(number, text); 498 EXPECT_EQ(ret, ErrorCode::NO_ERROR); 499 EXPECT_EQ(text, u"d"); 500 } 501 502 /** 503 * @tc.name: testGetTextAfterCursor_002 504 * @tc.desc: Select forward, mSelectNewBegin/mSelectNewEnd + number == size() 505 * @tc.type: FUNC 506 * @tc.require: issuesI6PPJM 507 */ 508 HWTEST_F(InputMethodAbilityTest, testGetTextAfterCursor_002, TestSize.Level0) 509 { 510 IMSA_HILOGI("InputMethodAbility testGetTextAfterCursor_002 START"); 511 // implemented correctly: mSelectNewBegin > mSelectNewEnd 512 int32_t start = 7; 513 int32_t end = 4; 514 imc_->OnSelectionChange(g_textTemp, start, end); 515 int32_t number = 2; 516 std::u16string text; 517 auto ret = inputMethodAbility_->GetTextAfterCursor(number, text); 518 EXPECT_EQ(ret, ErrorCode::NO_ERROR); 519 EXPECT_EQ(text, u"dd"); 520 521 // implemented Currently: mSelectNewBegin < mSelectNewEnd 522 start = 4; 523 end = 7; 524 imc_->OnSelectionChange(g_textTemp, start, end); 525 number = 2; 526 ret = inputMethodAbility_->GetTextAfterCursor(number, text); 527 EXPECT_EQ(ret, ErrorCode::NO_ERROR); 528 EXPECT_EQ(text, u"dd"); 529 } 530 531 /** 532 * @tc.name: testGetTextAfterCursor_003 533 * @tc.desc: Select backward, mSelectNewEnd + number < size() 534 * @tc.type: FUNC 535 * @tc.require: issuesI6PPJM 536 */ 537 HWTEST_F(InputMethodAbilityTest, testGetTextAfterCursor_003, TestSize.Level0) 538 { 539 IMSA_HILOGI("InputMethodAbility testGetTextAfterCursor_003 START"); 540 int32_t start = 4; 541 int32_t end = 5; 542 imc_->OnSelectionChange(g_textTemp, start, end); 543 int32_t number = 3; 544 std::u16string text; 545 auto ret = inputMethodAbility_->GetTextAfterCursor(number, text); 546 EXPECT_EQ(ret, ErrorCode::NO_ERROR); 547 EXPECT_EQ(text, u"ddd"); 548 } 549 550 /** 551 * @tc.name: testGetTextBeforeCursor_001 552 * @tc.desc: click textfield, mSelectNewBegin > number 553 * @tc.type: FUNC 554 * @tc.require: issuesI6PPJM 555 */ 556 HWTEST_F(InputMethodAbilityTest, testGetTextBeforeCursor_001, TestSize.Level0) 557 { 558 IMSA_HILOGI("InputMethodAbility testGetTextBeforeCursor_001 START"); 559 int32_t start = 6; 560 int32_t end = 6; 561 imc_->OnSelectionChange(g_textTemp, start, end); 562 int32_t number = 3; 563 std::u16string text; 564 auto ret = inputMethodAbility_->GetTextBeforeCursor(number, text); 565 EXPECT_EQ(ret, ErrorCode::NO_ERROR); 566 EXPECT_EQ(text, u"們dd"); 567 } 568 569 /** 570 * @tc.name: testGetTextBeforeCursor_002 571 * @tc.desc: Select forward, mSelectNewBegin/mSelectNewEnd == number 572 * @tc.type: FUNC 573 * @tc.require: issuesI6PPJM 574 */ 575 HWTEST_F(InputMethodAbilityTest, testGetTextBeforeCursor_002, TestSize.Level0) 576 { 577 IMSA_HILOGI("InputMethodAbility testGetTextBeforeCursor_002 START"); 578 // implemented correctly: mSelectNewBegin > mSelectNewEnd 579 int32_t start = 7; 580 int32_t end = 4; 581 imc_->OnSelectionChange(g_textTemp, start, end); 582 int32_t number = 4; 583 std::u16string text; 584 auto ret = inputMethodAbility_->GetTextBeforeCursor(number, text); 585 EXPECT_EQ(ret, ErrorCode::NO_ERROR); 586 EXPECT_EQ(text, u"我們我們"); 587 588 // implemented Currently: mSelectNewBegin < mSelectNewEnd 589 start = 4; 590 end = 7; 591 imc_->OnSelectionChange(g_textTemp, start, end); 592 number = 4; 593 ret = inputMethodAbility_->GetTextBeforeCursor(number, text); 594 EXPECT_EQ(ret, ErrorCode::NO_ERROR); 595 EXPECT_EQ(text, u"我們我們"); 596 } 597 598 /** 599 * @tc.name: testGetTextBeforeCursor_003 600 * @tc.desc: Select backward, mSelectNewBegin < number 601 * @tc.type: FUNC 602 * @tc.require: issuesI6PPJM 603 */ 604 HWTEST_F(InputMethodAbilityTest, testGetTextBeforeCursor_003, TestSize.Level0) 605 { 606 IMSA_HILOGI("InputMethodAbility testGetTextBeforeCursor_003 START"); 607 int32_t start = 4; 608 int32_t end = 5; 609 imc_->OnSelectionChange(g_textTemp, start, end); 610 int32_t number = 5; 611 std::u16string text; 612 auto ret = inputMethodAbility_->GetTextBeforeCursor(number, text); 613 EXPECT_EQ(ret, ErrorCode::NO_ERROR); 614 EXPECT_EQ(text, u"我們我們"); 615 } 616 } // namespace MiscServices 617 } // namespace OHOS 618