1 /* 2 * Copyright (c) 2025 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 <gmock/gmock.h> 19 #include <gtest/gtest.h> 20 21 #include <memory> 22 #include <string> 23 #include <vector> 24 25 #include "input_control_channel.h" 26 #include "input_method_ability_test_channel.h" 27 #include "input_method_ability_test_listener.h" 28 #include "input_method_ability_test_private_command.h" 29 #include "input_method_ability_test_proxy.h" 30 #include "input_method_ability_test_system_channel.h" 31 #include "input_method_agent_stub.h" 32 #include "input_method_core_stub.h" 33 #include "input_method_system_ability_proxy.h" 34 #include "inputmethod_sysevent.h" 35 #include "inputmethod_trace.h" 36 #include "iservice_registry.h" 37 #include "itypes_util.h" 38 #include "message_parcel.h" 39 #include "mock_ime_listener.h" 40 #include "mock_input_control_channel_proxy.h" 41 #include "mock_input_method_ability.h" 42 #include "mock_input_method_agent_stub.h" 43 #include "mock_input_method_panel.h" 44 #include "mock_system_cmd_channel_proxy.h" 45 #include "msg_handler_callback_interface.h" 46 #include "on_demand_start_stop_sa.h" 47 #include "soft_keyboard_panel.h" 48 #include "string_ex.h" 49 50 using namespace MiscServices; 51 using namespace OHOS; 52 53 class MockInputDataChannelProxy : public InputDataChannelProxy { 54 public: 55 MOCK_METHOD2(SetPreviewText, int32_t(const std::string &, const Range &)); 56 MOCK_METHOD1(FinishTextPreview, int32_t(bool)); 57 MOCK_METHOD1(SendMessage, int32_t(const ArrayBuffer &)); 58 }; 59 60 class MockSoftKeyboardPanel : public SoftKeyboardPanel { 61 public: 62 MOCK_METHOD1(SetCallingWindow, int32_t(int32_t)); 63 MOCK_METHOD1(GetCallingWindowInfo, int32_t(CallingWindowInfo &)); 64 }; 65 66 class MockInputControlChannel : public InputControlChannel { 67 public: 68 MOCK_METHOD0(HideKeyboardSelf, void()); 69 }; 70 71 class MockMsgHandlerCallback : public MsgHandlerCallbackInterface { 72 public: 73 MOCK_METHOD1(OnMessage, int32_t(const ArrayBuffer &)); 74 MOCK_METHOD0(OnTerminated, void()); 75 }; 76 77 class InputMethodAbilityTest : public testing::Test { 78 protected: SetUp()79 void SetUp() override 80 { 81 inputMethodAbility_ = std::make_shared<InputMethodAbility>(); 82 inputDataChannelProxy_ = std::make_shared<MockInputDataChannelProxy>(); 83 softKeyboardPanel_ = std::make_shared<MockSoftKeyboardPanel>(); 84 inputControlChannel_ = std::make_shared<MockInputControlChannel>(); 85 msgHandlerCallback_ = std::make_shared<MockMsgHandlerCallback>(); 86 } 87 TearDown()88 void TearDown() override 89 { 90 inputMethodAbility_ = nullptr; 91 inputDataChannelProxy_ = nullptr; 92 softKeyboardPanel_ = nullptr; 93 inputControlChannel_ = nullptr; 94 msgHandlerCallback_ = nullptr; 95 } 96 97 std::shared_ptr<InputMethodAbility> inputMethodAbility_; 98 std::shared_ptr<MockInputDataChannelProxy> inputDataChannelProxy_; 99 std::shared_ptr<MockSoftKeyboardPanel> softKeyboardPanel_; 100 std::shared_ptr<MockInputControlChannel> inputControlChannel_; 101 std::shared_ptr<MockMsgHandlerCallback> msgHandlerCallback_; 102 }; 103 104 class MockInputMethodAbility : public InputMethodAbility { 105 public: 106 MOCK_METHOD(sptr<SoftKeyboardPanel>, GetSoftKeyboardPanel, (), (override)); 107 MOCK_METHOD(int32_t, InvokeStartInputCallback, (const InputClientConfig &, bool), (override)); 108 MOCK_METHOD(int32_t, InvokeStartInputCallback, (bool), (override)); 109 MOCK_METHOD0(GetInputDataChannelProxy, InputDataChannelProxy *()); 110 MOCK_METHOD0(IsImeTerminating, bool()); 111 MOCK_METHOD0(PostTaskToEventHandler, void(std::function<void()>, const std::string &)); 112 MOCK_METHOD0(ForEach, void(std::function<bool(const PanelType &, const std::shared_ptr<InputMethodPanel> &)>)); 113 }; 114 115 class MockSoftKeyboardPanel : public SoftKeyboardPanel { 116 public: 117 MOCK_METHOD(void, HidePanel, (), (override)); 118 }; 119 120 class MockTaskManager : public TaskManager { 121 public: 122 MOCK_METHOD(void, WaitExec, (uint64_t, uint32_t, std::function<void()>), (override)); 123 }; 124 125 class MockInputMethodEngineListener : public InputMethodEngineListener { 126 public: 127 MOCK_METHOD(void, PostTaskToEventHandler, (std::function<void()>, const std::string &), (override)); 128 }; 129 130 class InputMethodAbilityTest : public Test { 131 protected: SetUp()132 void SetUp() override 133 { 134 inputMethodAbility = new MockInputMethodAbility(); 135 softKeyboardPanel = new MockSoftKeyboardPanel(); 136 taskManager = new MockTaskManager(); 137 imeListener = new MockInputMethodEngineListener(); 138 139 ON_CALL(*inputMethodAbility, GetSoftKeyboardPanel()).WillByDefault(Return(softKeyboardPanel)); 140 ON_CALL(*inputMethodAbility, InvokeStartInputCallback(_, _)).WillByDefault(Return(ErrorCode::NO_ERROR)); 141 ON_CALL(*inputMethodAbility, InvokeStartInputCallback(_)).WillByDefault(Return(ErrorCode::NO_ERROR)); 142 ON_CALL(*taskManager, WaitExec(_, _, _)).WillByDefault(Invoke([](uint64_t, uint32_t, std::function<void()>) {})); 143 ON_CALL(*imeListener, PostTaskToEventHandler(_, _)) 144 .WillByDefault(Invoke([](std::function<void()>, const std::string &) {})); 145 } 146 TearDown()147 void TearDown() override 148 { 149 delete inputMethodAbility; 150 delete softKeyboardPanel; 151 delete taskManager; 152 delete imeListener; 153 } 154 155 MockInputMethodAbility *inputMethodAbility; 156 MockSoftKeyboardPanel *softKeyboardPanel; 157 MockTaskManager *taskManager; 158 MockInputMethodEngineListener *imeListener; 159 }; 160 161 class MockInputDataChannelProxy : public InputDataChannelProxy { 162 public: 163 MOCK_METHOD1(InsertText, int32_t(const std::u16string &)); 164 MOCK_METHOD1(DeleteForward, int32_t(int32_t)); 165 MOCK_METHOD1(DeleteBackward, int32_t(int32_t)); 166 MOCK_METHOD1(SendFunctionKey, int32_t(int32_t)); 167 MOCK_METHOD1(HandleExtendAction, int32_t(int32_t)); 168 MOCK_METHOD2(GetTextBeforeCursor, int32_t(int32_t, std::u16string &)); 169 MOCK_METHOD2(GetTextAfterCursor, int32_t(int32_t, std::u16string &)); 170 MOCK_METHOD1(MoveCursor, int32_t(int32_t)); 171 MOCK_METHOD2(SelectByRange, int32_t(int32_t, int32_t)); 172 MOCK_METHOD2(SelectByMovement, int32_t(int32_t, int32_t)); 173 MOCK_METHOD1(GetEnterKeyType, int32_t(int32_t &)); 174 MOCK_METHOD1(GetInputPattern, int32_t(int32_t &)); 175 MOCK_METHOD1(GetTextIndexAtCursor, int32_t(int32_t &)); 176 }; 177 178 HWTEST_F(InputMethodAbilityTest, dispatchKeyEvent_001, TestSize.Level0) 179 { 180 InputClientInfo clientInfo; 181 clientInfo.channel = new RemoteObject(); 182 clientInfo.config.inputAttribute.bundleName = "testBundle"; 183 184 EXPECT_CALL(*inputMethodAbility, InvokeStartInputCallback(_)); 185 186 int32_t result = inputMethodAbility->StartInput(clientInfo, false); 187 188 EXPECT_EQ(result, ErrorCode::NO_ERROR); 189 } 190 191 HWTEST_F(InputMethodAbilityTest, dispatchKeyEvent_002, TestSize.Level0) 192 { 193 InputClientInfo clientInfo; 194 clientInfo.channel = new RemoteObject(); 195 clientInfo.isShowKeyboard = true; 196 197 inputMethodAbility->SetImeListener(nullptr); 198 199 EXPECT_CALL(*softKeyboardPanel, HidePanel()); 200 201 int32_t result = inputMethodAbility->StartInput(clientInfo, false); 202 203 EXPECT_EQ(result, ErrorCode::NO_ERROR); 204 } 205 206 HWTEST_F(InputMethodAbilityTest, dispatchKeyEvent_003, TestSize.Level0) 207 { 208 InputClientInfo clientInfo; 209 clientInfo.channel = new RemoteObject(); 210 clientInfo.isShowKeyboard = true; 211 212 inputMethodAbility->SetImeListener(std::shared_ptr<InputMethodEngineListener>(imeListener)); 213 214 EXPECT_CALL(*taskManager, WaitExec(_, _, _)); 215 216 int32_t result = inputMethodAbility->StartInput(clientInfo, false); 217 218 EXPECT_EQ(result, ErrorCode::NO_ERROR); 219 } 220 221 HWTEST_F(InputMethodAbilityTest, dispatchKeyEvent_004, TestSize.Level0) 222 { 223 InputClientInfo clientInfo; 224 clientInfo.channel = nullptr; 225 226 int32_t result = inputMethodAbility->StartInput(clientInfo, false); 227 228 EXPECT_EQ(result, ErrorCode::ERROR_CLIENT_NULL_POINTER); 229 } 230 231 HWTEST_F(InputMethodAbilityTest, dispatchKeyEvent_005, TestSize.Level0) 232 { 233 InputClientInfo clientInfo; 234 clientInfo.channel = new RemoteObject(); 235 clientInfo.needHide = true; 236 237 EXPECT_CALL(*softKeyboardPanel, HidePanel()); 238 239 int32_t result = inputMethodAbility->StartInput(clientInfo, false); 240 241 EXPECT_EQ(result, ErrorCode::NO_ERROR); 242 } 243 244 HWTEST_F(InputMethodAbilityTest, dispatchKeyEvent_006, TestSize.Level0) 245 { 246 InputClientInfo clientInfo; 247 clientInfo.channel = new RemoteObject(); 248 clientInfo.config.inputAttribute.bundleName = "testBundle"; 249 250 EXPECT_CALL(*inputMethodAbility, InvokeStartInputCallback(_, _)); 251 252 int32_t result = inputMethodAbility->StartInput(clientInfo, true); 253 254 EXPECT_EQ(result, ErrorCode::NO_ERROR); 255 } 256 257 HWTEST_F(InputMethodAbilityTest, dispatchKeyEvent_007, TestSize.Level0) 258 { 259 InputClientInfo clientInfo; 260 clientInfo.channel = new RemoteObject(); 261 clientInfo.config.inputAttribute.bundleName = "testBundle"; 262 263 EXPECT_CALL(*inputMethodAbility, InvokeStartInputCallback(_)); 264 265 int32_t result = inputMethodAbility->StartInput(clientInfo, false); 266 267 EXPECT_EQ(result, ErrorCode::NO_ERROR); 268 } 269 270 HWTEST_F(InputMethodAbilityTest, dispatchKeyEvent_008, TestSize.Level0) 271 { 272 InputClientInfo clientInfo; 273 clientInfo.channel = new RemoteObject(); 274 clientInfo.isShowKeyboard = true; 275 276 inputMethodAbility->SetImeListener(nullptr); 277 278 EXPECT_CALL(*softKeyboardPanel, HidePanel()); 279 280 int32_t result = inputMethodAbility->StartInput(clientInfo, false); 281 282 EXPECT_EQ(result, ErrorCode::NO_ERROR); 283 } 284 285 HWTEST_F(InputMethodAbilityTest, dispatchKeyEvent_009, TestSize.Level0) 286 { 287 InputClientInfo clientInfo; 288 clientInfo.channel = new RemoteObject(); 289 clientInfo.isShowKeyboard = true; 290 291 inputMethodAbility->SetImeListener(std::shared_ptr<InputMethodEngineListener>(imeListener)); 292 293 EXPECT_CALL(*taskManager, WaitExec(_, _, _)); 294 295 int32_t result = inputMethodAbility->StartInput(clientInfo, false); 296 297 EXPECT_EQ(result, ErrorCode::NO_ERROR); 298 } 299 HWTEST_F(InputMethodAbilityTest, dispatchKeyEvent_010, TestSize.Level0) 300 { 301 EXPECT_CALL(*imeListener_, OnInputFinish()).Times(1); 302 int32_t result = inputMethodAbility_->StopInput(channelObject_); 303 EXPECT_EQ(result, ErrorCode::NO_ERROR); 304 } 305 306 HWTEST_F(InputMethodAbilityTest, dispatchKeyEvent_011, TestSize.Level0) 307 { 308 inputMethodAbility_->imeListener_ = nullptr; 309 int32_t result = inputMethodAbility_->StopInput(channelObject_); 310 EXPECT_EQ(result, ErrorCode::NO_ERROR); 311 } 312 313 HWTEST_F(InputMethodAbilityTest, dispatchKeyEvent_012, TestSize.Level0) 314 { 315 int32_t result = inputMethodAbility_->DispatchKeyEvent(nullptr, consumer_); 316 EXPECT_EQ(result, ErrorCode::ERROR_CLIENT_NULL_POINTER); 317 } 318 319 HWTEST_F(InputMethodAbilityTest, dispatchKeyEvent_013, TestSize.Level0) 320 { 321 inputMethodAbility_->kdListener_ = nullptr; 322 int32_t result = inputMethodAbility_->DispatchKeyEvent(keyEvent_, consumer_); 323 EXPECT_EQ(result, ErrorCode::ERROR_CLIENT_NULL_POINTER); 324 } 325 326 HWTEST_F(InputMethodAbilityTest, onAttributeChange_001, TestSize.Level0) 327 { 328 EXPECT_CALL(*kdListener_, OnDealKeyEvent(keyEvent_, consumer_)).WillOnce(Return(false)); 329 int32_t result = inputMethodAbility_->DispatchKeyEvent(keyEvent_, consumer_); 330 EXPECT_EQ(result, ErrorCode::ERROR_DISPATCH_KEY_EVENT); 331 } 332 333 HWTEST_F(InputMethodAbilityTest, onAttributeChange_002, TestSize.Level0) 334 { 335 EXPECT_CALL(*kdListener_, OnDealKeyEvent(keyEvent_, consumer_)).WillOnce(Return(true)); 336 int32_t result = inputMethodAbility_->DispatchKeyEvent(keyEvent_, consumer_); 337 EXPECT_EQ(result, ErrorCode::NO_ERROR); 338 } 339 340 HWTEST_F(InputMethodAbilityTest, onAttributeChange_003, TestSize.Level0) 341 { 342 EXPECT_CALL(*imeListener_, OnSetCallingWindow(123)).Times(1); 343 inputMethodAbility_->SetCallingWindow(123); 344 } 345 346 HWTEST_F(InputMethodAbilityTest, onAttributeChange_004, TestSize.Level0) 347 { 348 inputMethodAbility_->imeListener_ = nullptr; 349 inputMethodAbility_->SetCallingWindow(123); 350 } 351 352 HWTEST_F(InputMethodAbilityTest, onAttributeChange_005, TestSize.Level0) 353 { 354 EXPECT_CALL(*kdListener_, OnCursorUpdate(10, 20, 30)).Times(1); 355 inputMethodAbility_->OnCursorUpdate(10, 20, 30); 356 } 357 358 HWTEST_F(InputMethodAbilityTest, onAttributeChange_006, TestSize.Level0) 359 { 360 inputMethodAbility_->kdListener_ = nullptr; 361 inputMethodAbility_->OnCursorUpdate(10, 20, 30); 362 } 363 364 HWTEST_F(InputMethodAbilityTest, onAttributeChange_007, TestSize.Level0) 365 { 366 EXPECT_CALL(*kdListener_, OnTextChange(Str16ToStr8(u"test"))).Times(1); 367 EXPECT_CALL(*kdListener_, OnSelectionChange(0, 1, 2, 3)).Times(1); 368 inputMethodAbility_->OnSelectionChange(u"test", 0, 1, 2, 3); 369 } 370 371 HWTEST_F(InputMethodAbilityTest, onAttributeChange_008, TestSize.Level0) 372 { 373 inputMethodAbility_->kdListener_ = nullptr; 374 inputMethodAbility_->OnSelectionChange(u"test", 0, 1, 2, 3); 375 } 376 377 HWTEST_F(InputMethodAbilityTest, onAttributeChange_009, TestSize.Level0) 378 { 379 InputAttribute attribute = { 1, 2 }; 380 EXPECT_CALL(*kdListener_, OnEditorAttributeChange(attribute)).Times(1); 381 inputMethodAbility_->OnAttributeChange(attribute); 382 } 383 384 HWTEST_F(InputMethodAbilityTest, insertText_001, TestSize.Level0) 385 { 386 inputMethodAbility_->kdListener_ = nullptr; 387 InputAttribute attribute = { 1, 2 }; 388 inputMethodAbility_->OnAttributeChange(attribute); 389 } 390 391 HWTEST_F(InputMethodAbilityTest, insertText_002, TestSize.Level0) 392 { 393 EXPECT_CALL(*imeListener_, OnInputStop()).WillOnce(Return(ErrorCode::NO_ERROR)); 394 int32_t result = inputMethodAbility_->OnStopInputService(true); 395 EXPECT_EQ(result, ErrorCode::NO_ERROR); 396 } 397 398 HWTEST_F(InputMethodAbilityTest, insertText_003, TestSize.Level0) 399 { 400 inputMethodAbility_->imeListener_ = nullptr; 401 int32_t result = inputMethodAbility_->OnStopInputService(true); 402 EXPECT_EQ(result, ErrorCode::ERROR_IME_NOT_STARTED); 403 } 404 405 HWTEST_F(InputMethodAbilityTest, insertText_004, TestSize.Level0) 406 { 407 int32_t result = inputMethodAbility_->HideKeyboard(); 408 EXPECT_EQ(result, ErrorCode::NO_ERROR); 409 } 410 411 HWTEST_F(InputMethodAbilityTest, insertText_005, TestSize.Level0) 412 { 413 EXPECT_CALL(*imeListener_, OnKeyboardStatus(true)).Times(1); 414 int32_t result = inputMethodAbility_->ShowKeyboard(); 415 EXPECT_EQ(result, ErrorCode::NO_ERROR); 416 } 417 418 HWTEST_F(InputMethodAbilityTest, insertText_006, TestSize.Level0) 419 { 420 inputMethodAbility_->imeListener_ = nullptr; 421 int32_t result = inputMethodAbility_->ShowKeyboard(); 422 EXPECT_EQ(result, ErrorCode::ERROR_IME); 423 } 424 425 HWTEST_F(InputMethodAbilityTest, insertText_007, TestSize.Level0) 426 { 427 EXPECT_CALL(*imeListener_, OnInputStart()).Times(1); 428 int32_t result = inputMethodAbility_->InvokeStartInputCallback(true); 429 EXPECT_EQ(result, ErrorCode::NO_ERROR); 430 } 431 432 HWTEST_F(InputMethodAbilityTest, insertText_008, TestSize.Level0) 433 { 434 inputMethodAbility_->imeListener_ = nullptr; 435 int32_t result = inputMethodAbility_->InvokeStartInputCallback(true); 436 EXPECT_EQ(result, ErrorCode::ERROR_IME); 437 } 438 HWTEST_F(InputMethodAbilityTest, insertText_009, TestSize.Level0) 439 { 440 // Arrange 441 EXPECT_CALL(*mockAbility, GetInputDataChannelProxy()).WillRepeatedly(Return(mockChannel)); 442 // ģ��panel�ı��� 443 444 // Act 445 InputMethodConfig textConfig; 446 textConfig.windowId = 1; 447 mockAbility->SetCallingWindow(textConfig); 448 449 // Assert 450 // ��֤����ID������ 451 } 452 453 HWTEST_F(InputMethodAbilityTest, insertText_010, TestSize.Level0) 454 { 455 // Arrange 456 EXPECT_CALL(*mockAbility, GetInputDataChannelProxy()).WillOnce(Return(nullptr)); 457 458 // Act 459 int32_t result = mockAbility->InsertText("test"); 460 461 // Assert 462 EXPECT_EQ(result, ErrorCode::ERROR_CLIENT_NULL_POINTER); 463 } 464 465 HWTEST_F(InputMethodAbilityTest, insertText_011, TestSize.Level0) 466 { 467 // Arrange 468 EXPECT_CALL(*mockAbility, GetInputDataChannelProxy()).WillOnce(Return(mockChannel)); 469 EXPECT_CALL(*mockChannel, DeleteForward(1)).WillOnce(Return(ErrorCode::NO_ERROR)); 470 471 // Act 472 int32_t result = mockAbility->DeleteForward(1); 473 474 // Assert 475 EXPECT_EQ(result, ErrorCode::NO_ERROR); 476 } 477 478 HWTEST_F(InputMethodAbilityTest, clearSystemCmdChannel_001, TestSize.Level0) 479 { 480 // Arrange 481 EXPECT_CALL(*mockAbility, IsImeTerminating()).WillOnce(Return(true)); 482 483 // Act 484 int32_t result = mockAbility->HideKeyboardSelf(); 485 486 // Assert 487 EXPECT_EQ(result, ErrorCode::NO_ERROR); 488 } 489 490 HWTEST_F(InputMethodAbilityTest, clearSystemCmdChannel_002, TestSize.Level0) 491 { 492 // Arrange 493 EXPECT_CALL(*mockAbility, GetInputDataChannelProxy()).WillOnce(Return(mockChannel)); 494 495 // Act 496 int32_t result = mockAbility->SelectByRange(-1, 10); 497 498 // Assert 499 EXPECT_EQ(result, ErrorCode::ERROR_PARAMETER_CHECK_FAILED); 500 } 501 502 HWTEST_F(InputMethodAbilityTest, clearSystemCmdChannel_003, TestSize.Level0) 503 { 504 EXPECT_CALL(*inputMethodAbility_, GetInputDataChannelProxy()).WillOnce(Return(nullptr)); 505 506 TextTotalConfig textConfig; 507 int32_t result = inputMethodAbility_->GetTextConfig(textConfig); 508 509 EXPECT_EQ(result, ErrorCode::ERROR_CLIENT_NULL_POINTER); 510 } 511 512 HWTEST_F(InputMethodAbilityTest, clearSystemCmdChannel_004, TestSize.Level0) 513 { 514 auto mockChannel = std::make_shared<MockInputDataChannelProxy>(); 515 EXPECT_CALL(*inputMethodAbility_, GetInputDataChannelProxy()).WillOnce(Return(mockChannel)); 516 EXPECT_CALL(*mockChannel, GetTextConfig(_)).WillOnce(Return(ErrorCode::NO_ERROR)); 517 EXPECT_CALL(*inputMethodAbility_, GetInputAttribute()).WillOnce(Return(InputAttribute{ .bundleName = "testBundle" })); 518 519 TextTotalConfig textConfig; 520 int32_t result = inputMethodAbility_->GetTextConfig(textConfig); 521 522 EXPECT_EQ(result, ErrorCode::NO_ERROR); 523 EXPECT_EQ(textConfig.inputAttribute.bundleName, "testBundle"); 524 } 525 526 HWTEST_F(InputMethodAbilityTest, clearSystemCmdChannel_005, TestSize.Level0) 527 { 528 auto mockChannel = std::make_shared<MockInputDataChannelProxy>(); 529 EXPECT_CALL(*inputMethodAbility_, GetInputDataChannelProxy()).WillOnce(Return(mockChannel)); 530 EXPECT_CALL(*mockChannel, GetTextConfig(_)).WillOnce(Return(ErrorCode::ERROR_CLIENT_NULL_POINTER)); 531 532 TextTotalConfig textConfig; 533 int32_t result = inputMethodAbility_->GetTextConfig(textConfig); 534 535 EXPECT_EQ(result, ErrorCode::ERROR_CLIENT_NULL_POINTER); 536 EXPECT_TRUE(textConfig.inputAttribute.bundleName.empty()); 537 } 538 539 HWTEST_F(InputMethodAbilityTest, clearSystemCmdChannel_006, TestSize.Level0) 540 { 541 int32_t security = 0; 542 ability_->securityMode_.store(1); 543 int32_t result = ability_->GetSecurityMode(security); 544 EXPECT_EQ(result, ErrorCode::NO_ERROR); 545 EXPECT_EQ(security, 1); 546 } 547 548 HWTEST_F(InputMethodAbilityTest, clearSystemCmdChannel_007, TestSize.Level0) 549 { 550 int32_t security = 0; 551 ability_->securityMode_.store(INVALID_SECURITY_MODE); 552 EXPECT_CALL(*mockImsaProxy_, GetSecurityMode(_)).WillOnce(DoAll(SetArgReferee<0>(1), Return(ErrorCode::NO_ERROR))); 553 EXPECT_CALL(*ability_, GetImsaProxy()).WillOnce(Return(mockImsaProxy_)); 554 int32_t result = ability_->GetSecurityMode(security); 555 EXPECT_EQ(result, ErrorCode::NO_ERROR); 556 EXPECT_EQ(security, 1); 557 } 558 559 HWTEST_F(InputMethodAbilityTest, clearSystemCmdChannel_008, TestSize.Level0) 560 { 561 int32_t security = 0; 562 ability_->securityMode_.store(INVALID_SECURITY_MODE); 563 EXPECT_CALL(*ability_, GetImsaProxy()).WillOnce(Return(nullptr)); 564 int32_t result = ability_->GetSecurityMode(security); 565 EXPECT_EQ(result, ErrorCode::ERROR_NULL_POINTER); 566 } 567 568 HWTEST_F(InputMethodAbilityTest, clearSystemCmdChannel_009, TestSize.Level0) 569 { 570 ability_->systemCmdChannelProxy_ = nullptr; 571 EXPECT_CALL(*ability_, GetSystemCmdChannelProxy()).WillOnce(Return(nullptr)); 572 ability_->ClearSystemCmdChannel(); 573 } 574 575 HWTEST_F(InputMethodAbilityTest, clearInputAttribute_001, TestSize.Level0) 576 { 577 ability_->systemCmdChannelProxy_ = mockSystemCmdChannelProxy_; 578 EXPECT_CALL(*ability_, GetSystemCmdChannelProxy()).WillOnce(Return(mockSystemCmdChannelProxy_)); 579 ability_->ClearSystemCmdChannel(); 580 EXPECT_EQ(ability_->systemCmdChannelProxy_, nullptr); 581 } 582 583 HWTEST_F(InputMethodAbilityTest, clearInputAttribute_002, TestSize.Level0) 584 { 585 EXPECT_CALL(*ability_, GetSystemCmdChannelProxy()).WillOnce(Return(nullptr)); 586 EXPECT_CALL(*ability_, GetInputMethodAgentStub()).WillOnce(Return(mockInputMethodAgentStub_)); 587 int32_t result = ability_->OnConnectSystemCmd(mockSystemCmdChannelProxy_, mockInputMethodAgentStub_); 588 EXPECT_EQ(result, ErrorCode::NO_ERROR); 589 } 590 591 HWTEST_F(InputMethodAbilityTest, clearInputAttribute_003, TestSize.Level0) 592 { 593 ability_->imeListener_ = nullptr; 594 int32_t result = ability_->OnSecurityChange(1); 595 EXPECT_EQ(result, ErrorCode::ERROR_BAD_PARAMETERS); 596 } 597 598 HWTEST_F(InputMethodAbilityTest, clearInputAttribute_004, TestSize.Level0) 599 { 600 ability_->imeListener_ = mockImeListener_; 601 EXPECT_CALL(*mockImeListener_, OnSecurityChange(1)).Times(1); 602 int32_t result = ability_->OnSecurityChange(1); 603 EXPECT_EQ(result, ErrorCode::NO_ERROR); 604 } 605 606 HWTEST_F(InputMethodAbilityTest, clearInputAttribute_005, TestSize.Level0) 607 { 608 PanelInfo panelInfo; 609 panelInfo.panelType = PanelType::SOFT_KEYBOARD; 610 EXPECT_CALL(*mockInputMethodPanel_, CreatePanel(_, panelInfo)).WillOnce(Return(ErrorCode::NO_ERROR)); 611 int32_t result = ability_->CreatePanel(nullptr, panelInfo, mockInputMethodPanel_); 612 EXPECT_EQ(result, ErrorCode::NO_ERROR); 613 } 614 615 HWTEST_F(InputMethodAbilityTest, clearInputAttribute_006, TestSize.Level0) 616 { 617 int32_t result = ability_->DestroyPanel(nullptr); 618 EXPECT_EQ(result, ErrorCode::ERROR_BAD_PARAMETERS); 619 } 620 621 HWTEST_F(InputMethodAbilityTest, clearInputAttribute_007, TestSize.Level0) 622 { 623 int32_t result = ability_->ShowPanel(nullptr); 624 EXPECT_EQ(result, ErrorCode::ERROR_BAD_PARAMETERS); 625 } 626 627 HWTEST_F(InputMethodAbilityTest, clearInputAttribute_008, TestSize.Level0) 628 { 629 int32_t result = ability_->HidePanel(nullptr); 630 EXPECT_EQ(result, ErrorCode::ERROR_BAD_PARAMETERS); 631 } 632 633 HWTEST_F(InputMethodAbilityTest, clearInputAttribute_009, TestSize.Level0) 634 { 635 SysPanelStatus sysPanelStatus; 636 EXPECT_CALL(*ability_, GetSystemCmdChannelProxy()).WillOnce(Return(nullptr)); 637 int32_t result = ability_->NotifyPanelStatus(PanelType::SOFT_KEYBOARD, sysPanelStatus); 638 EXPECT_EQ(result, ErrorCode::NO_ERROR); 639 } 640 641 HWTEST_F(InputMethodAbilityTest, clearInputAttribute_010, TestSize.Level0) 642 { 643 InputAttribute inputAttribute; 644 ability_->SetInputAttribute(inputAttribute); 645 // ��֤�����Ƿ����� 646 } 647 648 HWTEST_F(InputMethodAbilityTest, clearInputAttribute_011, TestSize.Level0) 649 { 650 // ���� 651 InputAttribute inputAttribute = { true, false, true }; 652 inputMethodAbility_->inputAttribute_ = inputAttribute; 653 654 // ���� 655 inputMethodAbility_->ClearInputAttribute(); 656 657 // ��֤ 658 EXPECT_EQ(inputMethodAbility_->GetInputAttribute(), InputAttribute{}); 659 } 660 661 HWTEST_F(InputMethodAbilityTest, finishTextPreview_001, TestSize.Level0) 662 { 663 // ���� 664 EXPECT_CALL(*mockInputMethodAbility_, GetImsaProxy()).WillOnce(Return(nullptr)); 665 666 // ���� 667 int32_t result = mockInputMethodAbility_->HideKeyboard(Trigger::IME_APP); 668 669 // ��֤ 670 EXPECT_EQ(result, ErrorCode::ERROR_IME); 671 } 672 673 HWTEST_F(InputMethodAbilityTest, finishTextPreview_002, TestSize.Level0) 674 { 675 // ���� 676 EXPECT_CALL(*mockInputMethodAbility_, GetImsaProxy()).WillOnce(Return(std::make_shared<MockImsaProxy>())); 677 EXPECT_CALL(*mockInputMethodAbility_, GetSoftKeyboardPanel()).WillOnce(Return(nullptr)); 678 679 // ���� 680 int32_t result = mockInputMethodAbility_->HideKeyboard(Trigger::IME_APP); 681 682 // ��֤ 683 EXPECT_EQ(result, ErrorCode::ERROR_IME); 684 } 685 686 HWTEST_F(InputMethodAbilityTest, finishTextPreview_003, TestSize.Level0) 687 { 688 // ���� 689 auto mockPanel = std::make_shared<MockInputMethodPanel>(); 690 EXPECT_CALL(*mockInputMethodAbility_, GetSoftKeyboardPanel()).WillOnce(Return(mockPanel)); 691 EXPECT_CALL(*mockPanel, GetPanelFlag()).WillOnce(Return(FLG_CANDIDATE_COLUMN)); 692 693 // ���� 694 int32_t result = mockInputMethodAbility_->HideKeyboard(Trigger::IME_APP); 695 696 // ��֤ 697 EXPECT_EQ(result, ErrorCode::NO_ERROR); 698 } 699 700 HWTEST_F(InputMethodAbilityTest, finishTextPreview_004, TestSize.Level0) 701 { 702 // ���� 703 mockInputMethodAbility_->isCurrentIme_ = true; 704 705 // ���� 706 bool result = mockInputMethodAbility_->IsCurrentIme(); 707 708 // ��֤ 709 EXPECT_TRUE(result); 710 } 711 712 HWTEST_F(InputMethodAbilityTest, finishTextPreview_005, TestSize.Level0) 713 { 714 // ���� 715 mockInputMethodAbility_->isDefaultIme_ = true; 716 717 // ���� 718 bool result = mockInputMethodAbility_->IsDefaultIme(); 719 720 // ��֤ 721 EXPECT_TRUE(result); 722 } 723 724 HWTEST_F(InputMethodAbilityTest, finishTextPreview_006, TestSize.Level0) 725 { 726 // ���� 727 mockInputMethodAbility_->imeListener_ = nullptr; 728 729 // ���� 730 bool result = mockInputMethodAbility_->IsEnable(); 731 732 // ��֤ 733 EXPECT_FALSE(result); 734 } 735 736 HWTEST_F(InputMethodAbilityTest, finishTextPreview_007, TestSize.Level0) 737 { 738 // ���� 739 auto mockPanel = std::make_shared<MockInputMethodPanel>(); 740 EXPECT_CALL(*mockInputMethodAbility_, GetSoftKeyboardPanel()).WillOnce(Return(mockPanel)); 741 742 // ���� 743 int32_t result = mockInputMethodAbility_->ExitCurrentInputType(); 744 745 // ��֤ 746 EXPECT_EQ(result, ErrorCode::NO_ERROR); 747 } 748 749 HWTEST_F(InputMethodAbilityTest, finishTextPreview_008, TestSize.Level0) 750 { 751 // ���� 752 PanelInfo panelInfo = { PanelType::SOFT_KEYBOARD, FLG_CANDIDATE_COLUMN }; 753 bool isShown = false; 754 755 // ���� 756 int32_t result = mockInputMethodAbility_->IsPanelShown(panelInfo, isShown); 757 758 // ��֤ 759 EXPECT_EQ(result, ErrorCode::NO_ERROR); 760 EXPECT_FALSE(isShown); 761 } 762 763 HWTEST_F(InputMethodAbilityTest, finishTextPreview_009, TestSize.Level0) 764 { 765 // ���� 766 auto mockChannel = std::make_shared<MockIRemoteObject>(); 767 EXPECT_CALL(*mockInputMethodAbility_, GetSoftKeyboardPanel()).WillOnce(Return(nullptr)); 768 769 // ���� 770 mockInputMethodAbility_->OnClientInactive(mockChannel); 771 772 // ��֤ 773 // ��Ҫ��֤��־��״̬�仯����Ŀǰû��ֱ�ӵķ���ֵ��״̬�仯 774 } 775 776 HWTEST_F(InputMethodAbilityTest, finishTextPreview_010, TestSize.Level0) 777 { 778 // ���� 779 EXPECT_CALL(*mockInputMethodAbility_, GetInputDataChannelProxy()) 780 .WillOnce(Return(std::make_shared<MockInputDataChannelProxy>())); 781 782 // ���� 783 mockInputMethodAbility_->NotifyKeyboardHeight(100, FLG_FIXED); 784 785 // ��֤ 786 // ��Ҫ��֤��־��״̬�仯����Ŀǰû��ֱ�ӵķ���ֵ��״̬�仯 787 } 788 789 HWTEST_F(InputMethodAbilityTest, finishTextPreview_011, TestSize.Level0) 790 { 791 // ���� 792 std::unordered_map<std::string, PrivateDataValue> privateCommand = { 793 { "key", PrivateDataValue{} }; // �����˽������ 794 EXPECT_CALL(*mockInputMethodAbility_, IsDefaultIme()).WillOnce(Return(true)); 795 EXPECT_CALL(*mockInputMethodAbility_, GetInputDataChannelProxy()) 796 .WillOnce(Return(std::make_shared<MockInputDataChannelProxy>())); 797 798 // ���� 799 int32_t result = mockInputMethodAbility_->SendPrivateCommand(privateCommand); 800 801 // ��֤ 802 EXPECT_EQ(result, ErrorCode::NO_ERROR); 803 } 804 805 HWTEST_F(InputMethodAbilityTest, finishTextPreview_012, TestSize.Level0) 806 { 807 // ���� 808 std::unordered_map<std::string, PrivateDataValue> privateCommand = { 809 { "key", PrivateDataValue{} }; // �����˽������ 810 EXPECT_CALL(*mockInputMethodAbility_, GetImsaProxy()).WillOnce(Return(std::make_shared<MockImsaProxy>())); 811 812 // ���� 813 int32_t result = mockInputMethodAbility_->ReceivePrivateCommand(privateCommand); 814 815 // ��֤ 816 EXPECT_EQ(result, ErrorCode::NO_ERROR); 817 } 818 819 HWTEST_F(InputMethodAbilityTest, finishTextPreview_013, TestSize.Level0) 820 { 821 EXPECT_CALL(*inputMethodAbility_, GetInputDataChannelProxy()).WillOnce(testing::Return(nullptr)); 822 int32_t result = inputMethodAbility_->SetPreviewText("test", Range{ 0, 4 }); 823 EXPECT_EQ(result, ErrorCode::ERROR_CLIENT_NULL_POINTER); 824 } 825 826 HWTEST_F(InputMethodAbilityTest, finishTextPreview_014, TestSize.Level0) 827 { 828 EXPECT_CALL(*inputMethodAbility_, GetInputDataChannelProxy()).WillOnce(testing::Return(inputDataChannelProxy_)); 829 EXPECT_CALL(*inputDataChannelProxy_, SetPreviewText("test", Range{ 0, 4 })) 830 .WillOnce(testing::Return(ErrorCode::NO_ERROR)); 831 int32_t result = inputMethodAbility_->SetPreviewText("test", Range{ 0, 4 }); 832 EXPECT_EQ(result, ErrorCode::NO_ERROR); 833 } 834 835 HWTEST_F(InputMethodAbilityTest, finishTextPreview_015, TestSize.Level0) 836 { 837 EXPECT_CALL(*inputMethodAbility_, GetInputDataChannelProxy()).WillOnce(testing::Return(nullptr)); 838 int32_t result = inputMethodAbility_->FinishTextPreview(true); 839 EXPECT_EQ(result, ErrorCode::ERROR_CLIENT_NULL_POINTER); 840 } 841 842 HWTEST_F(InputMethodAbilityTest, getMsgHandlerCallback_001, TestSize.Level0) 843 { 844 EXPECT_CALL(*inputMethodAbility_, GetInputDataChannelProxy()).WillOnce(testing::Return(inputDataChannelProxy_)); 845 EXPECT_CALL(*inputDataChannelProxy_, FinishTextPreview(true)).WillOnce(testing::Return(ErrorCode::NO_ERROR)); 846 int32_t result = inputMethodAbility_->FinishTextPreview(true); 847 EXPECT_EQ(result, ErrorCode::NO_ERROR); 848 } 849 850 HWTEST_F(InputMethodAbilityTest, getMsgHandlerCallback_002, TestSize.Level0) 851 { 852 EXPECT_CALL(*inputMethodAbility_, GetInputDataChannelProxy()).WillOnce(testing::Return(nullptr)); 853 CallingWindowInfo windowInfo; 854 int32_t result = inputMethodAbility_->GetCallingWindowInfo(windowInfo); 855 EXPECT_EQ(result, ErrorCode::ERROR_CLIENT_NOT_FOUND); 856 } 857 858 HWTEST_F(InputMethodAbilityTest, getMsgHandlerCallback_003, TestSize.Level0) 859 { 860 EXPECT_CALL(*inputMethodAbility_, GetInputDataChannelProxy()).WillOnce(testing::Return(inputDataChannelProxy_)); 861 EXPECT_CALL(*inputMethodAbility_, GetSoftKeyboardPanel()).WillOnce(testing::Return(nullptr)); 862 CallingWindowInfo windowInfo; 863 int32_t result = inputMethodAbility_->GetCallingWindowInfo(windowInfo); 864 EXPECT_EQ(result, ErrorCode::ERROR_PANEL_NOT_FOUND); 865 } 866 867 HWTEST_F(InputMethodAbilityTest, getMsgHandlerCallback_004, TestSize.Level0) 868 { 869 EXPECT_CALL(*inputMethodAbility_, GetInputDataChannelProxy()).WillOnce(testing::Return(inputDataChannelProxy_)); 870 EXPECT_CALL(*inputMethodAbility_, GetSoftKeyboardPanel()).WillOnce(testing::Return(softKeyboardPanel_)); 871 EXPECT_CALL(*inputMethodAbility_, GetTextConfig(testing::_)).WillOnce(testing::Return(ErrorCode::NO_ERROR)); 872 EXPECT_CALL(*softKeyboardPanel_, SetCallingWindow(testing::_)).WillOnce(testing::Return(ErrorCode::NO_ERROR)); 873 EXPECT_CALL(*softKeyboardPanel_, GetCallingWindowInfo(testing::_)).WillOnce(testing::Return(ErrorCode::NO_ERROR)); 874 CallingWindowInfo windowInfo; 875 int32_t result = inputMethodAbility_->GetCallingWindowInfo(windowInfo); 876 EXPECT_EQ(result, ErrorCode::NO_ERROR); 877 } 878 879 HWTEST_F(InputMethodAbilityTest, getMsgHandlerCallback_005, TestSize.Level0) 880 { 881 PanelStatusInfo info; 882 info.panelInfo.panelFlag = PanelFlag::FLG_CANDIDATE_COLUMN; 883 inputMethodAbility_->NotifyPanelStatusInfo(info, inputDataChannelProxy_); 884 // Ԥ��û�н��� 885 } 886 887 HWTEST_F(InputMethodAbilityTest, getMsgHandlerCallback_006, TestSize.Level0) 888 { 889 PanelStatusInfo info; 890 info.panelInfo.panelFlag = PanelFlag::FLG_NORMAL; 891 EXPECT_CALL(*inputDataChannelProxy_, NotifyPanelStatusInfo(info)); 892 inputMethodAbility_->NotifyPanelStatusInfo(info, inputDataChannelProxy_); 893 } 894 895 HWTEST_F(InputMethodAbilityTest, getMsgHandlerCallback_007, TestSize.Level0) 896 { 897 EXPECT_CALL(*inputMethodAbility_, GetSecurityMode(testing::_)) 898 .WillOnce(testing::DoAll(testing::SetArgReferee<0>(1), testing::Return(ErrorCode::NO_ERROR))); 899 ArrayBuffer arrayBuffer; 900 int32_t result = inputMethodAbility_->SendMessage(arrayBuffer); 901 EXPECT_EQ(result, ErrorCode::ERROR_SECURITY_MODE_OFF); 902 } 903 904 HWTEST_F(InputMethodAbilityTest, getMsgHandlerCallback_008, TestSize.Level0) 905 { 906 EXPECT_CALL(*inputMethodAbility_, GetSecurityMode(testing::_)) 907 .WillOnce(testing::DoAll(testing::SetArgReferee<0>(static_cast<int32_t>(SecurityMode::FULL)), 908 testing::Return(ErrorCode::NO_ERROR))); 909 EXPECT_CALL(*inputMethodAbility_, GetInputDataChannelProxy()).WillOnce(testing::Return(nullptr)); 910 ArrayBuffer arrayBuffer; 911 int32_t result = inputMethodAbility_->SendMessage(arrayBuffer); 912 EXPECT_EQ(result, ErrorCode::ERROR_CLIENT_NULL_POINTER); 913 } 914 915 HWTEST_F(InputMethodAbilityTest, getMsgHandlerCallback_009, TestSize.Level0) 916 { 917 EXPECT_CALL(*inputMethodAbility_, GetSecurityMode(testing::_)) 918 .WillOnce(testing::DoAll(testing::SetArgReferee<0>(1), testing::Return(ErrorCode::NO_ERROR))); 919 ArrayBuffer arrayBuffer; 920 int32_t result = inputMethodAbility_->RecvMessage(arrayBuffer); 921 EXPECT_EQ(result, ErrorCode::ERROR_SECURITY_MODE_OFF); 922 } 923 924 HWTEST_F(InputMethodAbilityTest, getMsgHandlerCallback_010, TestSize.Level0) 925 { 926 EXPECT_CALL(*inputMethodAbility_, GetSecurityMode(testing::_)) 927 .WillOnce(testing::DoAll(testing::SetArgReferee<0>(static_cast<int32_t>(SecurityMode::FULL)), 928 testing::Return(ErrorCode::NO_ERROR))); 929 EXPECT_CALL(*inputMethodAbility_, GetMsgHandlerCallback()).WillOnce(testing::Return(nullptr)); 930 ArrayBuffer arrayBuffer; 931 int32_t result = inputMethodAbility_->RecvMessage(arrayBuffer); 932 EXPECT_EQ(result, ErrorCode::ERROR_MSG_HANDLER_NOT_REGIST); 933 } 934 935 HWTEST_F(InputMethodAbilityTest, getMsgHandlerCallback_011, TestSize.Level0) 936 { 937 EXPECT_CALL(*msgHandlerCallback_, OnTerminated()); 938 inputMethodAbility_->RegisterMsgHandler(msgHandlerCallback_); 939 inputMethodAbility_->RegisterMsgHandler(nullptr); 940 } 941 942 HWTEST_F(InputMethodAbilityTest, getMsgHandlerCallback_012, TestSize.Level0) 943 { 944 inputMethodAbility_->RegisterMsgHandler(msgHandlerCallback_); 945 std::shared_ptr<MsgHandlerCallbackInterface> handler = inputMethodAbility_->GetMsgHandlerCallback(); 946 EXPECT_EQ(handler, msgHandlerCallback_); 947 }