1 /* 2 * Copyright (C) 2023 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 #define private public 17 #define protected public 18 #include "input_method_ability.h" 19 #include "task_manager.h" 20 #include "ime_info_inquirer.h" 21 #undef private 22 23 #include <gtest/gtest.h> 24 25 #include "ability_manager_client.h" 26 #include "global.h" 27 #include "ime_event_monitor_manager_impl.h" 28 #include "ime_setting_listener_test_impl.h" 29 #include "input_method_ability_interface.h" 30 #include "input_method_controller.h" 31 #include "input_method_engine_listener_impl.h" 32 #include "input_method_types.h" 33 #include "keyboard_listener_test_impl.h" 34 #include "tdd_util.h" 35 #include "text_listener.h" 36 #include "scope_utils.h" 37 using namespace testing::ext; 38 namespace OHOS { 39 namespace MiscServices { 40 constexpr int32_t RETRY_INTERVAL = 100; 41 constexpr int32_t RETRY_TIME = 30; 42 constexpr int32_t WAIT_APP_START_COMPLETE = 1; 43 constexpr int32_t WAIT_BIND_COMPLETE = 1; 44 constexpr int32_t WAIT_CLICK_COMPLETE = 100; 45 constexpr const char *BUNDLENAME = "com.example.editorbox"; 46 class ImeProxyTest : public testing::Test { 47 public: 48 static sptr<InputMethodController> imc_; 49 static int32_t uid_; SetUpTestCase(void)50 static void SetUpTestCase(void) 51 { 52 TddUtil::StorageSelfTokenID(); 53 TddUtil::InitWindow(false); 54 imc_ = InputMethodController::GetInstance(); 55 RegisterImeSettingListener(); 56 SwitchToTestIme(); 57 // native sa permission 58 SystemConfig systemConfig; 59 SysCfgParser::ParseSystemConfig(systemConfig); 60 for (auto id : systemConfig.proxyImeUidList) { 61 uid_ = id; 62 } 63 TddUtil::GrantNativePermission(); 64 } TearDownTestCase(void)65 static void TearDownTestCase(void) 66 { 67 TddUtil::DestroyWindow(); 68 TddUtil::RestoreSelfTokenID(); 69 if (ImeProxyTest::uid_ == -1) { 70 return; 71 } 72 TddUtil::KillImsaProcess(); 73 } SetUp()74 void SetUp() 75 { 76 if (ImeProxyTest::uid_ == -1) { 77 GTEST_SKIP() << "proxy ime is not enabled"; 78 } 79 IMSA_HILOGI("InputMethodAbilityTest::SetUp"); 80 InputMethodAbilityInterface::GetInstance().SetImeListener(std::make_shared<InputMethodEngineListenerImpl>()); 81 InputMethodAbilityInterface::GetInstance().SetKdListener(std::make_shared<KeyboardListenerTestImpl>()); 82 TaskManager::GetInstance().SetInited(true); 83 } TearDown()84 void TearDown() 85 { 86 IMSA_HILOGI("InputMethodAbilityTest::TearDown"); 87 std::this_thread::sleep_for(std::chrono::seconds(1)); 88 TaskManager::GetInstance().Reset(); 89 } 90 Attach(bool isPc)91 static int32_t Attach(bool isPc) 92 { 93 isPc ? InputMethodEngineListenerImpl::isEnable_ = true : InputMethodEngineListenerImpl::isEnable_ = false; 94 TextConfig config; 95 config.cursorInfo = { .left = 0, .top = 1, .width = 0.5, .height = 1.2 }; 96 sptr<OnTextChangedListener> testListener = new TextListener(); 97 auto ret = imc_->Attach(testListener, true, config); 98 return ret; 99 } Close(bool isPc)100 static void Close(bool isPc) 101 { 102 isPc ? InputMethodEngineListenerImpl::isEnable_ = true : InputMethodEngineListenerImpl::isEnable_ = false; 103 imc_->Close(); 104 } 105 StartApp()106 static void StartApp() // bind client default, not show keyboard 107 { 108 static std::string cmd = "aa start ability -a EntryAbility -b com.example.editorbox"; 109 std::string result; 110 auto ret = TddUtil::ExecuteCmd(cmd, result); 111 EXPECT_TRUE(ret); 112 BlockRetry(RETRY_INTERVAL, RETRY_TIME, []() { 113 return AAFwk::AbilityManagerClient::GetInstance()->GetTopAbility().GetBundleName() == BUNDLENAME; 114 }); 115 IMSA_HILOGI("start app success"); 116 sleep(WAIT_APP_START_COMPLETE); // ensure app start complete 117 } 118 ClickEditor(bool isPc)119 static void ClickEditor(bool isPc) 120 { 121 isPc ? InputMethodEngineListenerImpl::isEnable_ = true : InputMethodEngineListenerImpl::isEnable_ = false; 122 static std::string cmd = "uinput -T -d 200 200 -u 200 200"; 123 std::string result; 124 auto ret = TddUtil::ExecuteCmd(cmd, result); 125 EXPECT_TRUE(ret); 126 usleep(WAIT_CLICK_COMPLETE); // ensure click complete 127 } 128 StopApp()129 static void StopApp() 130 { 131 static std::string cmd = "aa force-stop com.example.editorbox"; 132 std::string result; 133 auto ret = TddUtil::ExecuteCmd(cmd, result); 134 EXPECT_TRUE(ret); 135 BlockRetry(RETRY_INTERVAL, RETRY_TIME, []() { 136 return AAFwk::AbilityManagerClient::GetInstance()->GetTopAbility().GetBundleName() != BUNDLENAME; 137 }); 138 IMSA_HILOGI("stop app success"); 139 } 140 EnsureBindComplete()141 static void EnsureBindComplete() 142 { 143 sleep(WAIT_BIND_COMPLETE); 144 } 145 SwitchToTestIme()146 static void SwitchToTestIme() 147 { 148 ImeSettingListenerTestImpl::ResetParam(); 149 TddUtil::SetTestTokenID( 150 TddUtil::AllocTestTokenID(true, "ImeProxyTest", { "ohos.permission.CONNECT_IME_ABILITY" })); 151 TddUtil::EnabledAllIme(); 152 SubProperty subProp; 153 subProp.name = "com.example.testIme"; 154 subProp.id = "InputMethodExtAbility"; 155 auto ret = imc_->SwitchInputMethod(SwitchTrigger::CURRENT_IME, subProp.name, subProp.id); 156 EXPECT_EQ(ret, ErrorCode::NO_ERROR); 157 EXPECT_TRUE(ImeSettingListenerTestImpl::WaitImeChange(subProp)); 158 TddUtil::RestoreSelfTokenID(); 159 } 160 161 private: RegisterImeSettingListener()162 static void RegisterImeSettingListener() 163 { 164 TddUtil::StorageSelfTokenID(); 165 TddUtil::SetTestTokenID(TddUtil::AllocTestTokenID(true, "ImeProxyTest")); 166 auto listener = std::make_shared<ImeSettingListenerTestImpl>(); 167 ImeEventMonitorManagerImpl::GetInstance().RegisterImeEventListener( 168 EVENT_IME_HIDE_MASK | EVENT_IME_SHOW_MASK | EVENT_IME_CHANGE_MASK, listener); 169 TddUtil::RestoreSelfTokenID(); 170 } 171 }; 172 sptr<InputMethodController> ImeProxyTest::imc_; 173 int32_t ImeProxyTest::uid_ { -1 }; 174 175 /** 176 * @tc.name: RegisteredProxyNotPermission 177 * @tc.desc: not in permission 178 * @tc.type: FUNC 179 */ 180 HWTEST_F(ImeProxyTest, RegisteredProxyNotPermission, TestSize.Level1) 181 { 182 IMSA_HILOGI("ImeProxyTest::RegisteredProxyNotPermission"); 183 // RegisteredProxy not in ima bind 184 InputMethodEngineListenerImpl::ResetParam(); 185 InputMethodEngineListenerImpl::isEnable_ = true; 186 auto ret = InputMethodAbilityInterface::GetInstance().RegisteredProxy(); 187 EXPECT_NE(ret, ErrorCode::NO_ERROR); 188 } 189 190 /** 191 * @tc.name: RegisteredProxyNotInEditor_001 192 * @tc.desc: not in editor 193 * @tc.type: FUNC 194 */ 195 HWTEST_F(ImeProxyTest, RegisteredProxyNotInEditor_001, TestSize.Level1) 196 { 197 IMSA_HILOGI("ImeProxyTest::RegisteredProxyNotInEditor_001"); 198 // RegisteredProxy not in ima bind 199 InputMethodEngineListenerImpl::ResetParam(); 200 InputMethodEngineListenerImpl::isEnable_ = true; 201 { 202 UidScope uidScope(ImeProxyTest::uid_); 203 auto ret = InputMethodAbilityInterface::GetInstance().RegisteredProxy(); 204 EXPECT_EQ(ret, ErrorCode::NO_ERROR); 205 EXPECT_FALSE(InputMethodEngineListenerImpl::WaitInputStart()); 206 207 InputMethodAbilityInterface::GetInstance().UnRegisteredProxy(UnRegisteredType::REMOVE_PROXY_IME); 208 } 209 } 210 211 /** 212 * @tc.name: AttachInPcAfterRegisteredProxyNotInEditor_002 213 * @tc.desc: not in editor 214 * @tc.type: FUNC 215 */ 216 HWTEST_F(ImeProxyTest, AttachInPcAfterRegisteredProxyNotInEditor_002, TestSize.Level1) 217 { 218 IMSA_HILOGI("ImeProxyTest::AttachInPcAfterRegisteredProxyNotInEditor_002"); 219 TddUtil::GetFocused(); 220 // RegisteredProxy not in ima bind 221 InputMethodEngineListenerImpl::isEnable_ = true; 222 { 223 UidScope uidScope(ImeProxyTest::uid_); 224 auto ret = InputMethodAbilityInterface::GetInstance().RegisteredProxy(); 225 EXPECT_EQ(ret, ErrorCode::NO_ERROR); 226 227 // mock click the edit box in pc, bind proxy 228 ImeSettingListenerTestImpl::ResetParam(); 229 InputMethodEngineListenerImpl::ResetParam(); 230 ret = Attach(true); 231 EXPECT_EQ(ret, ErrorCode::NO_ERROR); 232 EXPECT_FALSE(ImeSettingListenerTestImpl::WaitPanelShow()); 233 EXPECT_TRUE(InputMethodEngineListenerImpl::WaitInputStart()); 234 Close(false); 235 InputMethodAbilityInterface::GetInstance().UnRegisteredProxy(UnRegisteredType::REMOVE_PROXY_IME); 236 TddUtil::GetUnfocused(); 237 } 238 } 239 240 /** 241 * @tc.name: AttachInPeAfterRegisteredProxyNotInEditor_003 242 * @tc.desc: not in editor 243 * @tc.type: FUNC 244 */ 245 HWTEST_F(ImeProxyTest, AttachInPeAfterRegisteredProxyNotInEditor_003, TestSize.Level1) 246 { 247 IMSA_HILOGI("ImeProxyTest::AttachInPeAfterRegisteredProxyNotInEditor_003"); 248 TddUtil::GetFocused(); 249 // RegisteredProxy not in ima bind 250 InputMethodEngineListenerImpl::isEnable_ = true; 251 { 252 UidScope uidScope(ImeProxyTest::uid_); 253 auto ret = InputMethodAbilityInterface::GetInstance().RegisteredProxy(); 254 EXPECT_EQ(ret, ErrorCode::NO_ERROR); 255 256 // mock click the edit box in pe, bind ima 257 ImeSettingListenerTestImpl::ResetParam(); 258 InputMethodEngineListenerImpl::ResetParam(); 259 ret = Attach(false); 260 EXPECT_EQ(ret, ErrorCode::NO_ERROR); 261 EXPECT_TRUE(ImeSettingListenerTestImpl::WaitPanelShow()); 262 EXPECT_FALSE(InputMethodEngineListenerImpl::WaitInputStart()); 263 Close(false); 264 InputMethodAbilityInterface::GetInstance().UnRegisteredProxy(UnRegisteredType::REMOVE_PROXY_IME); 265 TddUtil::GetUnfocused(); 266 } 267 } 268 269 /** 270 * @tc.name: RegisteredProxyInImaEditor_004 271 * @tc.desc: 272 * @tc.type: FUNC 273 */ 274 HWTEST_F(ImeProxyTest, RegisteredProxyInImaEditor_004, TestSize.Level1) 275 { 276 IMSA_HILOGI("ImeProxyTest::RegisteredProxyInImaEditor_004"); 277 TddUtil::GetFocused(); 278 // mock click the edit box in pe, bind ima 279 ImeSettingListenerTestImpl::ResetParam(); 280 auto ret = Attach(false); 281 EXPECT_EQ(ret, ErrorCode::NO_ERROR); 282 EXPECT_TRUE(ImeSettingListenerTestImpl::WaitPanelShow()); 283 284 // RegisteredProxy in ima bind, unbind ima, bind proxy 285 InputMethodEngineListenerImpl::ResetParam(); 286 ImeSettingListenerTestImpl::ResetParam(); 287 KeyboardListenerTestImpl::ResetParam(); 288 { 289 UidScope uidScope(ImeProxyTest::uid_); 290 InputMethodEngineListenerImpl::isEnable_ = true; 291 ret = InputMethodAbilityInterface::GetInstance().RegisteredProxy(); 292 EXPECT_EQ(ret, ErrorCode::NO_ERROR); 293 EXPECT_TRUE(ImeSettingListenerTestImpl::WaitPanelHide()); 294 EXPECT_TRUE(InputMethodEngineListenerImpl::WaitInputStart()); 295 EXPECT_TRUE(KeyboardListenerTestImpl::WaitCursorUpdate()); 296 Close(false); 297 InputMethodAbilityInterface::GetInstance().UnRegisteredProxy(UnRegisteredType::REMOVE_PROXY_IME); 298 TddUtil::GetUnfocused(); 299 } 300 } 301 302 /** 303 * @tc.name: UnRegisteredAndRegisteredProxyInProxyBind_005 304 * @tc.desc: 305 * @tc.type: FUNC 306 */ 307 HWTEST_F(ImeProxyTest, UnRegisteredAndRegisteredProxyInProxyBind_005, TestSize.Level1) 308 { 309 IMSA_HILOGI("ImeProxyTest::UnRegisteredAndRegisteredProxyInProxyBind_005"); 310 TddUtil::GetFocused(); 311 { 312 UidScope uidScope(ImeProxyTest::uid_); 313 auto ret = InputMethodAbilityInterface::GetInstance().RegisteredProxy(); 314 EXPECT_EQ(ret, ErrorCode::NO_ERROR); 315 316 // mock click the edit box in pc, bind proxy 317 InputMethodEngineListenerImpl::ResetParam(); 318 ret = Attach(true); 319 EXPECT_EQ(ret, ErrorCode::NO_ERROR); 320 EXPECT_TRUE(InputMethodEngineListenerImpl::WaitInputStart()); 321 322 // UnRegisteredProxy in proxy bind 323 InputMethodEngineListenerImpl::ResetParam(); 324 ret = InputMethodAbilityInterface::GetInstance().UnRegisteredProxy(UnRegisteredType::REMOVE_PROXY_IME); 325 EXPECT_EQ(ret, ErrorCode::NO_ERROR); 326 EXPECT_TRUE(InputMethodEngineListenerImpl::WaitInputFinish()); 327 ret = InputMethodAbilityInterface::GetInstance().InsertText("b"); 328 EXPECT_EQ(ret, ErrorCode::ERROR_IMA_CHANNEL_NULLPTR); 329 330 // RegisteredProxy proxy, rebind proxy 331 InputMethodEngineListenerImpl::ResetParam(); 332 InputMethodEngineListenerImpl::isEnable_ = true; 333 ret = InputMethodAbilityInterface::GetInstance().RegisteredProxy(); 334 EXPECT_EQ(ret, ErrorCode::NO_ERROR); 335 EXPECT_TRUE(InputMethodEngineListenerImpl::WaitInputStart()); 336 Close(false); 337 InputMethodAbilityInterface::GetInstance().UnRegisteredProxy(UnRegisteredType::REMOVE_PROXY_IME); 338 TddUtil::GetUnfocused(); 339 } 340 } 341 342 /** 343 * @tc.name: UnRegisteredProxyNotInBind_stop_006 344 * @tc.desc: 345 * @tc.type: FUNC 346 */ 347 HWTEST_F(ImeProxyTest, UnRegisteredProxyNotInBind_stop_006, TestSize.Level1) 348 { 349 IMSA_HILOGI("ImeProxyTest::UnRegisteredProxyNotInBind_stop_006"); 350 InputMethodEngineListenerImpl::ResetParam(); 351 { 352 UidScope uidScope(ImeProxyTest::uid_); 353 InputMethodEngineListenerImpl::isEnable_ = true; 354 auto ret = InputMethodAbilityInterface::GetInstance().RegisteredProxy(); 355 EXPECT_EQ(ret, ErrorCode::NO_ERROR); 356 ret = InputMethodAbilityInterface::GetInstance().UnRegisteredProxy(UnRegisteredType::REMOVE_PROXY_IME); 357 EXPECT_EQ(ret, ErrorCode::NO_ERROR); 358 EXPECT_FALSE(InputMethodEngineListenerImpl::WaitInputFinish()); 359 } 360 } 361 362 /** 363 * @tc.name: UnRegisteredProxyInProxyBind_stop_007 364 * @tc.desc: 365 * @tc.type: FUNC 366 */ 367 HWTEST_F(ImeProxyTest, UnRegisteredProxyInProxyBind_stop_007, TestSize.Level1) 368 { 369 IMSA_HILOGI("ImeProxyTest::UnRegisteredProxyInProxyBind_stop_007"); 370 TddUtil::GetFocused(); 371 { 372 UidScope uidScope(ImeProxyTest::uid_); 373 InputMethodEngineListenerImpl::isEnable_ = true; 374 auto ret = InputMethodAbilityInterface::GetInstance().RegisteredProxy(); 375 EXPECT_EQ(ret, ErrorCode::NO_ERROR); 376 std::this_thread::sleep_for(std::chrono::seconds(2)); 377 378 // mock click the edit box in pc, bind proxy 379 InputMethodEngineListenerImpl::ResetParam(); 380 ret = Attach(true); 381 EXPECT_EQ(ret, ErrorCode::NO_ERROR); 382 EXPECT_TRUE(InputMethodEngineListenerImpl::WaitInputStart()); 383 384 InputMethodEngineListenerImpl::ResetParam(); 385 ImeSettingListenerTestImpl::ResetParam(); 386 ret = InputMethodAbilityInterface::GetInstance().UnRegisteredProxy(UnRegisteredType::REMOVE_PROXY_IME); 387 EXPECT_EQ(ret, ErrorCode::NO_ERROR); 388 EXPECT_TRUE(InputMethodEngineListenerImpl::WaitInputFinish()); 389 EXPECT_FALSE(ImeSettingListenerTestImpl::WaitPanelShow()); 390 Close(false); 391 TddUtil::GetUnfocused(); 392 } 393 } 394 395 /** 396 * @tc.name: UnRegisteredProxyInImaBind_stop_008 397 * @tc.desc: 398 * @tc.type: FUNC 399 */ 400 HWTEST_F(ImeProxyTest, UnRegisteredProxyInImaBind_stop_008, TestSize.Level1) 401 { 402 IMSA_HILOGI("ImeProxyTest::UnRegisteredProxyInImaBind_stop_008"); 403 TddUtil::GetFocused(); 404 { 405 UidScope uidScope(ImeProxyTest::uid_); 406 InputMethodEngineListenerImpl::isEnable_ = true; 407 auto ret = InputMethodAbilityInterface::GetInstance().RegisteredProxy(); 408 EXPECT_EQ(ret, ErrorCode::NO_ERROR); 409 410 // mock click the edit box in pe, bind ima 411 ImeSettingListenerTestImpl::ResetParam(); 412 ret = Attach(false); 413 EXPECT_EQ(ret, ErrorCode::NO_ERROR); 414 EXPECT_TRUE(ImeSettingListenerTestImpl::WaitPanelShow()); 415 416 InputMethodEngineListenerImpl::ResetParam(); 417 ImeSettingListenerTestImpl::ResetParam(); 418 ret = InputMethodAbilityInterface::GetInstance().UnRegisteredProxy(UnRegisteredType::REMOVE_PROXY_IME); 419 EXPECT_EQ(ret, ErrorCode::NO_ERROR); 420 EXPECT_FALSE(ImeSettingListenerTestImpl::WaitPanelHide()); 421 EXPECT_FALSE(InputMethodEngineListenerImpl::WaitInputFinish()); 422 Close(false); 423 TddUtil::GetUnfocused(); 424 } 425 } 426 427 /** 428 * @tc.name: UnRegisteredProxyNotInBind_switch_009 429 * @tc.desc: 430 * @tc.type: FUNC 431 */ 432 HWTEST_F(ImeProxyTest, UnRegisteredProxyNotInBind_switch_009, TestSize.Level1) 433 { 434 IMSA_HILOGI("ImeProxyTest::UnRegisteredProxyNotInBind_switch_009"); 435 { 436 UidScope uidScope(ImeProxyTest::uid_); 437 InputMethodEngineListenerImpl::isEnable_ = true; 438 auto ret = InputMethodAbilityInterface::GetInstance().RegisteredProxy(); 439 EXPECT_EQ(ret, ErrorCode::NO_ERROR); 440 } 441 } 442 443 /** 444 * @tc.name: UnRegisteredProxyInProxyBind_switch_010 445 * @tc.desc: 446 * @tc.type: FUNC 447 */ 448 HWTEST_F(ImeProxyTest, UnRegisteredProxyInProxyBind_switch_010, TestSize.Level1) 449 { 450 IMSA_HILOGI("ImeProxyTest::UnRegisteredProxyInProxyBind_switch_010"); 451 TddUtil::GetFocused(); 452 { 453 UidScope uidScope(ImeProxyTest::uid_); 454 InputMethodEngineListenerImpl::isEnable_ = true; 455 auto ret = InputMethodAbilityInterface::GetInstance().RegisteredProxy(); 456 EXPECT_EQ(ret, ErrorCode::NO_ERROR); 457 458 // mock click the edit box in pc, bind proxy 459 InputMethodEngineListenerImpl::ResetParam(); 460 ret = Attach(true); 461 EXPECT_EQ(ret, ErrorCode::NO_ERROR); 462 EXPECT_TRUE(InputMethodEngineListenerImpl::WaitInputStart()); 463 464 ImeSettingListenerTestImpl::ResetParam(); 465 InputMethodEngineListenerImpl::ResetParam(); 466 Close(false); 467 TddUtil::GetUnfocused(); 468 } 469 } 470 471 /** 472 * @tc.name: UnRegisteredProxyWithErrorType_011 473 * @tc.desc: 474 * @tc.type: FUNC 475 */ 476 HWTEST_F(ImeProxyTest, UnRegisteredProxyWithErrorType_011, TestSize.Level1) 477 { 478 IMSA_HILOGI("ImeProxyTest::UnRegisteredProxyWithErrorType_011"); 479 InputMethodEngineListenerImpl::isEnable_ = true; 480 { 481 UidScope uidScope(ImeProxyTest::uid_); 482 auto ret = InputMethodAbilityInterface::GetInstance().RegisteredProxy(); 483 EXPECT_EQ(ret, ErrorCode::NO_ERROR); 484 ret = InputMethodAbilityInterface::GetInstance().UnRegisteredProxy(static_cast<UnRegisteredType>(3)); 485 EXPECT_EQ(ret, ErrorCode::ERROR_BAD_PARAMETERS); 486 } 487 } 488 489 /** 490 * @tc.name: AppUnFocusInProxyBindInPe_012 491 * @tc.desc: 492 * @tc.type: FUNC 493 */ 494 HWTEST_F(ImeProxyTest, AppUnFocusInProxyBindInPe_012, TestSize.Level1) 495 { 496 IMSA_HILOGI("ImeProxyTest::AppUnFocusInProxyBindInPe_012"); 497 TddUtil::GetFocused(); 498 InputMethodEngineListenerImpl::isEnable_ = true; 499 { 500 UidScope uidScope(ImeProxyTest::uid_); 501 auto ret = InputMethodAbilityInterface::GetInstance().RegisteredProxy(); 502 EXPECT_EQ(ret, ErrorCode::NO_ERROR); 503 504 // mock click the edit box in pc, bind proxy 505 InputMethodEngineListenerImpl::ResetParam(); 506 ret = Attach(true); 507 EXPECT_EQ(ret, ErrorCode::NO_ERROR); 508 EXPECT_TRUE(InputMethodEngineListenerImpl::WaitInputStart()); 509 510 // mock app unFocus in proxy bind in pe, unbind proxy 511 Close(false); 512 EXPECT_TRUE(InputMethodEngineListenerImpl::WaitInputFinish()); 513 ret = InputMethodAbilityInterface::GetInstance().InsertText("d"); 514 EXPECT_EQ(ret, ErrorCode::ERROR_IMA_CHANNEL_NULLPTR); 515 516 ret = InputMethodAbilityInterface::GetInstance().UnRegisteredProxy(UnRegisteredType::REMOVE_PROXY_IME); 517 TddUtil::GetUnfocused(); 518 } 519 } 520 521 /** 522 * @tc.name: AppUnFocusInProxyBindInPc_013 523 * @tc.desc: 524 * @tc.type: FUNC 525 */ 526 HWTEST_F(ImeProxyTest, AppUnFocusInProxyBindInPc_013, TestSize.Level1) 527 { 528 IMSA_HILOGI("ImeProxyTest::AppUnFocusInProxyBindInPc_013"); 529 { 530 UidScope uidScope(ImeProxyTest::uid_); 531 TddUtil::GetFocused(); 532 auto ret = InputMethodAbilityInterface::GetInstance().RegisteredProxy(); 533 EXPECT_EQ(ret, ErrorCode::NO_ERROR); 534 535 // open the app, click the edit box in pc, bind proxy 536 InputMethodEngineListenerImpl::ResetParam(); 537 ret = Attach(true); 538 EXPECT_EQ(ret, ErrorCode::NO_ERROR); 539 EXPECT_TRUE(InputMethodEngineListenerImpl::WaitInputStart()); 540 541 // mock app unFocus in proxy bind in pc, unbind proxy 542 Close(true); 543 EXPECT_TRUE(InputMethodEngineListenerImpl::WaitInputFinish()); 544 ret = InputMethodAbilityInterface::GetInstance().InsertText("d"); 545 EXPECT_EQ(ret, ErrorCode::ERROR_IMA_CHANNEL_NULLPTR); 546 547 InputMethodAbilityInterface::GetInstance().UnRegisteredProxy(UnRegisteredType::REMOVE_PROXY_IME); 548 TddUtil::GetUnfocused(); 549 } 550 } 551 552 /** 553 * @tc.name: ProxyAndImaSwitchTest_014 554 * @tc.desc: 555 * @tc.type: FUNC 556 */ 557 HWTEST_F(ImeProxyTest, ProxyAndImaSwitchTest_014, TestSize.Level1) 558 { 559 IMSA_HILOGI("ImeProxyTest::ProxyAndImaSwitchTest_014"); 560 TddUtil::GetFocused(); 561 { 562 UidScope uidScope(ImeProxyTest::uid_); 563 auto ret = InputMethodAbilityInterface::GetInstance().RegisteredProxy(); 564 EXPECT_EQ(ret, ErrorCode::NO_ERROR); 565 566 // mock click the edit box in pe, bind ima 567 ImeSettingListenerTestImpl::ResetParam(); 568 ret = Attach(false); 569 EXPECT_EQ(ret, ErrorCode::NO_ERROR); 570 EXPECT_TRUE(ImeSettingListenerTestImpl::WaitPanelShow()); 571 572 // mock click the edit box in pc, unbind ima, bind proxy 573 ImeSettingListenerTestImpl::ResetParam(); 574 InputMethodEngineListenerImpl::ResetParam(); 575 ret = Attach(true); 576 EXPECT_EQ(ret, ErrorCode::NO_ERROR); 577 EXPECT_TRUE(InputMethodEngineListenerImpl::WaitInputStart()); 578 EXPECT_TRUE(ImeSettingListenerTestImpl::WaitPanelHide()); 579 580 // mock click the edit box in pe, unbind proxy, bind ima 581 ImeSettingListenerTestImpl::ResetParam(); 582 InputMethodEngineListenerImpl::ResetParam(); 583 ret = Attach(false); 584 EXPECT_EQ(ret, ErrorCode::NO_ERROR); 585 EXPECT_TRUE(ImeSettingListenerTestImpl::WaitPanelShow()); 586 EXPECT_TRUE(InputMethodEngineListenerImpl::WaitInputFinish()); 587 588 InputMethodAbilityInterface::GetInstance().UnRegisteredProxy(UnRegisteredType::REMOVE_PROXY_IME); 589 Close(false); 590 TddUtil::GetUnfocused(); 591 } 592 } 593 594 /** 595 * @tc.name: KeyboardListenerTest_015 596 * @tc.desc: 597 * @tc.type: FUNC 598 */ 599 HWTEST_F(ImeProxyTest, KeyboardListenerTest_015, TestSize.Level1) 600 { 601 IMSA_HILOGI("ImeProxyTest::KeyboardListenerTest_015"); 602 // 1:positionX, 2: positionY, 5: height 603 InputMethodAbility::GetInstance().OnCursorUpdate(1, 2, 5); 604 EXPECT_TRUE(KeyboardListenerTestImpl::WaitCursorUpdate()); 605 606 InputMethodAbility::GetInstance().OnSelectionChange(std::u16string(u"text"), 1, 2, 4, 6); 607 EXPECT_TRUE(KeyboardListenerTestImpl::WaitSelectionChange(4)); 608 EXPECT_TRUE(KeyboardListenerTestImpl::WaitTextChange("text")); 609 } 610 611 /** 612 * @tc.name: TextEditTest 613 * @tc.desc: 614 * @tc.type: FUNC 615 */ 616 HWTEST_F(ImeProxyTest, TextEditTest, TestSize.Level1) 617 { 618 IMSA_HILOGI("ImeProxyTest::TextEditTest"); 619 TddUtil::GetFocused(); 620 { 621 UidScope uidScope(ImeProxyTest::uid_); 622 auto ret = InputMethodAbilityInterface::GetInstance().RegisteredProxy(); 623 EXPECT_EQ(ret, ErrorCode::NO_ERROR); 624 InputMethodEngineListenerImpl::ResetParam(); 625 ret = Attach(true); 626 EXPECT_EQ(ret, ErrorCode::NO_ERROR); 627 EXPECT_TRUE(InputMethodEngineListenerImpl::WaitInputStart()); 628 629 TextListener::ResetParam(); 630 ret = InputMethodAbilityInterface::GetInstance().InsertText("b"); 631 EXPECT_EQ(ret, ErrorCode::NO_ERROR); 632 EXPECT_TRUE(TextListener::WaitInsertText(u"b")); 633 ret = InputMethodAbilityInterface::GetInstance().MoveCursor(1); 634 EXPECT_EQ(ret, ErrorCode::NO_ERROR); 635 EXPECT_TRUE(TextListener::WaitMoveCursor(1)); 636 ret = InputMethodAbilityInterface::GetInstance().DeleteBackward(1); 637 EXPECT_EQ(ret, ErrorCode::NO_ERROR); 638 EXPECT_TRUE(TextListener::WaitDeleteForward(1)); 639 ret = InputMethodAbilityInterface::GetInstance().DeleteForward(2); 640 EXPECT_EQ(ret, ErrorCode::NO_ERROR); 641 EXPECT_TRUE(TextListener::WaitDeleteBackward(2)); 642 643 Close(true); 644 InputMethodAbilityInterface::GetInstance().UnRegisteredProxy(UnRegisteredType::REMOVE_PROXY_IME); 645 TddUtil::GetUnfocused(); 646 } 647 } 648 649 /** 650 * @tc.name: ClientDiedInImaBind_016 651 * @tc.desc: 652 * @tc.type: FUNC 653 */ 654 HWTEST_F(ImeProxyTest, ClientDiedInImaBind_016, TestSize.Level1) 655 { 656 IMSA_HILOGI("ImeProxyTest::ClientDiedInImaBind_016"); 657 // open the app, click the edit box in pe, bind ima 658 { 659 UidScope uidScope(ImeProxyTest::uid_); 660 StartApp(); 661 ImeSettingListenerTestImpl::ResetParam(); 662 ClickEditor(false); 663 EXPECT_TRUE(ImeSettingListenerTestImpl::WaitPanelShow()); 664 EnsureBindComplete(); 665 666 ImeSettingListenerTestImpl::ResetParam(); 667 StopApp(); 668 EXPECT_TRUE(ImeSettingListenerTestImpl::WaitPanelHide()); 669 } 670 } 671 672 /** 673 * @tc.name: ClientDiedInProxyBind_017 674 * @tc.desc: 675 * @tc.type: FUNC 676 */ 677 HWTEST_F(ImeProxyTest, ClientDiedInProxyBind_017, TestSize.Level1) 678 { 679 IMSA_HILOGI("ImeProxyTest::ClientDiedInProxyBind_017"); 680 // RegisteredProxy not in ima bind 681 { 682 UidScope uidScope(ImeProxyTest::uid_); 683 InputMethodEngineListenerImpl::isEnable_ = true; 684 auto ret = InputMethodAbilityInterface::GetInstance().RegisteredProxy(); 685 EXPECT_EQ(ret, ErrorCode::NO_ERROR); 686 687 StartApp(); 688 InputMethodEngineListenerImpl::ResetParam(); 689 ClickEditor(true); 690 EXPECT_TRUE(InputMethodEngineListenerImpl::WaitInputStart()); 691 EnsureBindComplete(); 692 693 InputMethodEngineListenerImpl::ResetParam(); 694 StopApp(); 695 EXPECT_TRUE(InputMethodEngineListenerImpl::WaitInputFinish()); 696 InputMethodAbilityInterface::GetInstance().UnRegisteredProxy(UnRegisteredType::REMOVE_PROXY_IME); 697 } 698 } 699 700 /** 701 * @tc.name: onInputFinishTest_StopInput 702 * @tc.desc: close 703 * @tc.type: FUNC 704 */ 705 HWTEST_F(ImeProxyTest, onInputFinishTest_StopInput, TestSize.Level1) 706 { 707 IMSA_HILOGI("ImeProxyTest::onInputFinishTest_StopInput"); 708 InputMethodAbility::GetInstance().StopInput(nullptr, 0); 709 EXPECT_TRUE(InputMethodEngineListenerImpl::WaitInputFinish()); 710 } 711 /** 712 * @tc.name: onInputFinishTest_OnClientInactive 713 * @tc.desc: OnClientInactive 714 * @tc.type: FUNC 715 */ 716 HWTEST_F(ImeProxyTest, onInputFinishTest_OnClientInactive, TestSize.Level1) 717 { 718 IMSA_HILOGI("ImeProxyTest::onInputFinishTest_OnClientInactive"); 719 InputMethodAbility::GetInstance().OnClientInactive(nullptr); 720 EXPECT_TRUE(InputMethodEngineListenerImpl::WaitInputFinish()); 721 } 722 723 /** 724 * @tc.name: DiscardTypingTextTest 725 * @tc.desc: DiscardTypingText 726 * @tc.type: FUNC 727 */ 728 HWTEST_F(ImeProxyTest, DiscardTypingTextTest, TestSize.Level0) 729 { 730 IMSA_HILOGI("ImeProxyTest::DiscardTypingTextTest"); 731 auto ret = InputMethodAbility::GetInstance().OnDiscardTypingText(); 732 EXPECT_EQ(ret, ErrorCode::NO_ERROR); 733 } 734 735 /** 736 * @tc.name: TestRegisterProxyIme 737 * @tc.desc: Test RegisterProxyIme 738 * @tc.type: FUNC 739 */ 740 HWTEST_F(ImeProxyTest, RegisterProxyImeTest001, TestSize.Level0) 741 { 742 IMSA_HILOGI("ImeProxyTest::TestRegisterProxyImeTest001 start"); 743 uint64_t displayId = -1; 744 auto ret = InputMethodAbilityInterface::GetInstance().RegisterProxyIme(displayId); 745 EXPECT_NE(ret, ErrorCode::NO_ERROR); 746 ret = InputMethodAbilityInterface::GetInstance().UnregisterProxyIme(displayId); 747 EXPECT_NE(ret, ErrorCode::NO_ERROR); 748 } 749 750 /** 751 * @tc.name: TestRegisterProxyIme 752 * @tc.desc: Test RegisterProxyIme failed 753 * @tc.type: FUNC 754 */ 755 HWTEST_F(ImeProxyTest, RegisterProxyImeTest002, TestSize.Level0) 756 { 757 IMSA_HILOGI("ImeProxyTest::RegisterProxyImeTest002 start"); 758 { 759 UidScope uidScope(ImeProxyTest::uid_); 760 uint64_t displayId = 0; 761 auto ret = InputMethodAbilityInterface::GetInstance().RegisterProxyIme(displayId); 762 EXPECT_EQ(ret, ErrorCode::NO_ERROR); 763 std::shared_ptr<Property> property = imc_->GetCurrentInputMethod(); 764 ASSERT_TRUE(property != nullptr); 765 EXPECT_EQ(property->name, "com.example.testIme"); 766 } 767 } 768 } // namespace MiscServices 769 } // namespace OHOS 770