diff --git a/test/unittest/functional_test/input_method_ability_test.cpp b/test/unittest/functional_test/input_method_ability_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..864f0c86f46b4977318e2949edc1ce71425a77f8 --- /dev/null +++ b/test/unittest/functional_test/input_method_ability_test.cpp @@ -0,0 +1,947 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "input_method_ability.h" + +#include +#include + +#include +#include +#include + +#include "input_control_channel.h" +#include "input_method_ability_test_channel.h" +#include "input_method_ability_test_listener.h" +#include "input_method_ability_test_private_command.h" +#include "input_method_ability_test_proxy.h" +#include "input_method_ability_test_system_channel.h" +#include "input_method_agent_stub.h" +#include "input_method_core_stub.h" +#include "input_method_system_ability_proxy.h" +#include "inputmethod_sysevent.h" +#include "inputmethod_trace.h" +#include "iservice_registry.h" +#include "itypes_util.h" +#include "message_parcel.h" +#include "mock_ime_listener.h" +#include "mock_input_control_channel_proxy.h" +#include "mock_input_method_ability.h" +#include "mock_input_method_agent_stub.h" +#include "mock_input_method_panel.h" +#include "mock_system_cmd_channel_proxy.h" +#include "msg_handler_callback_interface.h" +#include "on_demand_start_stop_sa.h" +#include "soft_keyboard_panel.h" +#include "string_ex.h" + +using namespace MiscServices; +using namespace OHOS; + +class MockInputDataChannelProxy : public InputDataChannelProxy { +public: + MOCK_METHOD2(SetPreviewText, int32_t(const std::string &, const Range &)); + MOCK_METHOD1(FinishTextPreview, int32_t(bool)); + MOCK_METHOD1(SendMessage, int32_t(const ArrayBuffer &)); +}; + +class MockSoftKeyboardPanel : public SoftKeyboardPanel { +public: + MOCK_METHOD1(SetCallingWindow, int32_t(int32_t)); + MOCK_METHOD1(GetCallingWindowInfo, int32_t(CallingWindowInfo &)); +}; + +class MockInputControlChannel : public InputControlChannel { +public: + MOCK_METHOD0(HideKeyboardSelf, void()); +}; + +class MockMsgHandlerCallback : public MsgHandlerCallbackInterface { +public: + MOCK_METHOD1(OnMessage, int32_t(const ArrayBuffer &)); + MOCK_METHOD0(OnTerminated, void()); +}; + +class InputMethodAbilityTest : public testing::Test { +protected: + void SetUp() override + { + inputMethodAbility_ = std::make_shared(); + inputDataChannelProxy_ = std::make_shared(); + softKeyboardPanel_ = std::make_shared(); + inputControlChannel_ = std::make_shared(); + msgHandlerCallback_ = std::make_shared(); + } + + void TearDown() override + { + inputMethodAbility_ = nullptr; + inputDataChannelProxy_ = nullptr; + softKeyboardPanel_ = nullptr; + inputControlChannel_ = nullptr; + msgHandlerCallback_ = nullptr; + } + + std::shared_ptr inputMethodAbility_; + std::shared_ptr inputDataChannelProxy_; + std::shared_ptr softKeyboardPanel_; + std::shared_ptr inputControlChannel_; + std::shared_ptr msgHandlerCallback_; +}; + +class MockInputMethodAbility : public InputMethodAbility { +public: + MOCK_METHOD(sptr, GetSoftKeyboardPanel, (), (override)); + MOCK_METHOD(int32_t, InvokeStartInputCallback, (const InputClientConfig &, bool), (override)); + MOCK_METHOD(int32_t, InvokeStartInputCallback, (bool), (override)); + MOCK_METHOD0(GetInputDataChannelProxy, InputDataChannelProxy *()); + MOCK_METHOD0(IsImeTerminating, bool()); + MOCK_METHOD0(PostTaskToEventHandler, void(std::function, const std::string &)); + MOCK_METHOD0(ForEach, void(std::function &)>)); +}; + +class MockSoftKeyboardPanel : public SoftKeyboardPanel { +public: + MOCK_METHOD(void, HidePanel, (), (override)); +}; + +class MockTaskManager : public TaskManager { +public: + MOCK_METHOD(void, WaitExec, (uint64_t, uint32_t, std::function), (override)); +}; + +class MockInputMethodEngineListener : public InputMethodEngineListener { +public: + MOCK_METHOD(void, PostTaskToEventHandler, (std::function, const std::string &), (override)); +}; + +class InputMethodAbilityTest : public Test { +protected: + void SetUp() override + { + inputMethodAbility = new MockInputMethodAbility(); + softKeyboardPanel = new MockSoftKeyboardPanel(); + taskManager = new MockTaskManager(); + imeListener = new MockInputMethodEngineListener(); + + ON_CALL(*inputMethodAbility, GetSoftKeyboardPanel()).WillByDefault(Return(softKeyboardPanel)); + ON_CALL(*inputMethodAbility, InvokeStartInputCallback(_, _)).WillByDefault(Return(ErrorCode::NO_ERROR)); + ON_CALL(*inputMethodAbility, InvokeStartInputCallback(_)).WillByDefault(Return(ErrorCode::NO_ERROR)); + ON_CALL(*taskManager, WaitExec(_, _, _)).WillByDefault(Invoke([](uint64_t, uint32_t, std::function) {})); + ON_CALL(*imeListener, PostTaskToEventHandler(_, _)) + .WillByDefault(Invoke([](std::function, const std::string &) {})); + } + + void TearDown() override + { + delete inputMethodAbility; + delete softKeyboardPanel; + delete taskManager; + delete imeListener; + } + + MockInputMethodAbility *inputMethodAbility; + MockSoftKeyboardPanel *softKeyboardPanel; + MockTaskManager *taskManager; + MockInputMethodEngineListener *imeListener; +}; + +class MockInputDataChannelProxy : public InputDataChannelProxy { +public: + MOCK_METHOD1(InsertText, int32_t(const std::u16string &)); + MOCK_METHOD1(DeleteForward, int32_t(int32_t)); + MOCK_METHOD1(DeleteBackward, int32_t(int32_t)); + MOCK_METHOD1(SendFunctionKey, int32_t(int32_t)); + MOCK_METHOD1(HandleExtendAction, int32_t(int32_t)); + MOCK_METHOD2(GetTextBeforeCursor, int32_t(int32_t, std::u16string &)); + MOCK_METHOD2(GetTextAfterCursor, int32_t(int32_t, std::u16string &)); + MOCK_METHOD1(MoveCursor, int32_t(int32_t)); + MOCK_METHOD2(SelectByRange, int32_t(int32_t, int32_t)); + MOCK_METHOD2(SelectByMovement, int32_t(int32_t, int32_t)); + MOCK_METHOD1(GetEnterKeyType, int32_t(int32_t &)); + MOCK_METHOD1(GetInputPattern, int32_t(int32_t &)); + MOCK_METHOD1(GetTextIndexAtCursor, int32_t(int32_t &)); +}; + +HWTEST_F(InputMethodAbilityTest, dispatchKeyEvent_001, TestSize.Level0) +{ + InputClientInfo clientInfo; + clientInfo.channel = new RemoteObject(); + clientInfo.config.inputAttribute.bundleName = "testBundle"; + + EXPECT_CALL(*inputMethodAbility, InvokeStartInputCallback(_)); + + int32_t result = inputMethodAbility->StartInput(clientInfo, false); + + EXPECT_EQ(result, ErrorCode::NO_ERROR); +} + +HWTEST_F(InputMethodAbilityTest, dispatchKeyEvent_002, TestSize.Level0) +{ + InputClientInfo clientInfo; + clientInfo.channel = new RemoteObject(); + clientInfo.isShowKeyboard = true; + + inputMethodAbility->SetImeListener(nullptr); + + EXPECT_CALL(*softKeyboardPanel, HidePanel()); + + int32_t result = inputMethodAbility->StartInput(clientInfo, false); + + EXPECT_EQ(result, ErrorCode::NO_ERROR); +} + +HWTEST_F(InputMethodAbilityTest, dispatchKeyEvent_003, TestSize.Level0) +{ + InputClientInfo clientInfo; + clientInfo.channel = new RemoteObject(); + clientInfo.isShowKeyboard = true; + + inputMethodAbility->SetImeListener(std::shared_ptr(imeListener)); + + EXPECT_CALL(*taskManager, WaitExec(_, _, _)); + + int32_t result = inputMethodAbility->StartInput(clientInfo, false); + + EXPECT_EQ(result, ErrorCode::NO_ERROR); +} + +HWTEST_F(InputMethodAbilityTest, dispatchKeyEvent_004, TestSize.Level0) +{ + InputClientInfo clientInfo; + clientInfo.channel = nullptr; + + int32_t result = inputMethodAbility->StartInput(clientInfo, false); + + EXPECT_EQ(result, ErrorCode::ERROR_CLIENT_NULL_POINTER); +} + +HWTEST_F(InputMethodAbilityTest, dispatchKeyEvent_005, TestSize.Level0) +{ + InputClientInfo clientInfo; + clientInfo.channel = new RemoteObject(); + clientInfo.needHide = true; + + EXPECT_CALL(*softKeyboardPanel, HidePanel()); + + int32_t result = inputMethodAbility->StartInput(clientInfo, false); + + EXPECT_EQ(result, ErrorCode::NO_ERROR); +} + +HWTEST_F(InputMethodAbilityTest, dispatchKeyEvent_006, TestSize.Level0) +{ + InputClientInfo clientInfo; + clientInfo.channel = new RemoteObject(); + clientInfo.config.inputAttribute.bundleName = "testBundle"; + + EXPECT_CALL(*inputMethodAbility, InvokeStartInputCallback(_, _)); + + int32_t result = inputMethodAbility->StartInput(clientInfo, true); + + EXPECT_EQ(result, ErrorCode::NO_ERROR); +} + +HWTEST_F(InputMethodAbilityTest, dispatchKeyEvent_007, TestSize.Level0) +{ + InputClientInfo clientInfo; + clientInfo.channel = new RemoteObject(); + clientInfo.config.inputAttribute.bundleName = "testBundle"; + + EXPECT_CALL(*inputMethodAbility, InvokeStartInputCallback(_)); + + int32_t result = inputMethodAbility->StartInput(clientInfo, false); + + EXPECT_EQ(result, ErrorCode::NO_ERROR); +} + +HWTEST_F(InputMethodAbilityTest, dispatchKeyEvent_008, TestSize.Level0) +{ + InputClientInfo clientInfo; + clientInfo.channel = new RemoteObject(); + clientInfo.isShowKeyboard = true; + + inputMethodAbility->SetImeListener(nullptr); + + EXPECT_CALL(*softKeyboardPanel, HidePanel()); + + int32_t result = inputMethodAbility->StartInput(clientInfo, false); + + EXPECT_EQ(result, ErrorCode::NO_ERROR); +} + +HWTEST_F(InputMethodAbilityTest, dispatchKeyEvent_009, TestSize.Level0) +{ + InputClientInfo clientInfo; + clientInfo.channel = new RemoteObject(); + clientInfo.isShowKeyboard = true; + + inputMethodAbility->SetImeListener(std::shared_ptr(imeListener)); + + EXPECT_CALL(*taskManager, WaitExec(_, _, _)); + + int32_t result = inputMethodAbility->StartInput(clientInfo, false); + + EXPECT_EQ(result, ErrorCode::NO_ERROR); +} +HWTEST_F(InputMethodAbilityTest, dispatchKeyEvent_010, TestSize.Level0) +{ + EXPECT_CALL(*imeListener_, OnInputFinish()).Times(1); + int32_t result = inputMethodAbility_->StopInput(channelObject_); + EXPECT_EQ(result, ErrorCode::NO_ERROR); +} + +HWTEST_F(InputMethodAbilityTest, dispatchKeyEvent_011, TestSize.Level0) +{ + inputMethodAbility_->imeListener_ = nullptr; + int32_t result = inputMethodAbility_->StopInput(channelObject_); + EXPECT_EQ(result, ErrorCode::NO_ERROR); +} + +HWTEST_F(InputMethodAbilityTest, dispatchKeyEvent_012, TestSize.Level0) +{ + int32_t result = inputMethodAbility_->DispatchKeyEvent(nullptr, consumer_); + EXPECT_EQ(result, ErrorCode::ERROR_CLIENT_NULL_POINTER); +} + +HWTEST_F(InputMethodAbilityTest, dispatchKeyEvent_013, TestSize.Level0) +{ + inputMethodAbility_->kdListener_ = nullptr; + int32_t result = inputMethodAbility_->DispatchKeyEvent(keyEvent_, consumer_); + EXPECT_EQ(result, ErrorCode::ERROR_CLIENT_NULL_POINTER); +} + +HWTEST_F(InputMethodAbilityTest, onAttributeChange_001, TestSize.Level0) +{ + EXPECT_CALL(*kdListener_, OnDealKeyEvent(keyEvent_, consumer_)).WillOnce(Return(false)); + int32_t result = inputMethodAbility_->DispatchKeyEvent(keyEvent_, consumer_); + EXPECT_EQ(result, ErrorCode::ERROR_DISPATCH_KEY_EVENT); +} + +HWTEST_F(InputMethodAbilityTest, onAttributeChange_002, TestSize.Level0) +{ + EXPECT_CALL(*kdListener_, OnDealKeyEvent(keyEvent_, consumer_)).WillOnce(Return(true)); + int32_t result = inputMethodAbility_->DispatchKeyEvent(keyEvent_, consumer_); + EXPECT_EQ(result, ErrorCode::NO_ERROR); +} + +HWTEST_F(InputMethodAbilityTest, onAttributeChange_003, TestSize.Level0) +{ + EXPECT_CALL(*imeListener_, OnSetCallingWindow(123)).Times(1); + inputMethodAbility_->SetCallingWindow(123); +} + +HWTEST_F(InputMethodAbilityTest, onAttributeChange_004, TestSize.Level0) +{ + inputMethodAbility_->imeListener_ = nullptr; + inputMethodAbility_->SetCallingWindow(123); +} + +HWTEST_F(InputMethodAbilityTest, onAttributeChange_005, TestSize.Level0) +{ + EXPECT_CALL(*kdListener_, OnCursorUpdate(10, 20, 30)).Times(1); + inputMethodAbility_->OnCursorUpdate(10, 20, 30); +} + +HWTEST_F(InputMethodAbilityTest, onAttributeChange_006, TestSize.Level0) +{ + inputMethodAbility_->kdListener_ = nullptr; + inputMethodAbility_->OnCursorUpdate(10, 20, 30); +} + +HWTEST_F(InputMethodAbilityTest, onAttributeChange_007, TestSize.Level0) +{ + EXPECT_CALL(*kdListener_, OnTextChange(Str16ToStr8(u"test"))).Times(1); + EXPECT_CALL(*kdListener_, OnSelectionChange(0, 1, 2, 3)).Times(1); + inputMethodAbility_->OnSelectionChange(u"test", 0, 1, 2, 3); +} + +HWTEST_F(InputMethodAbilityTest, onAttributeChange_008, TestSize.Level0) +{ + inputMethodAbility_->kdListener_ = nullptr; + inputMethodAbility_->OnSelectionChange(u"test", 0, 1, 2, 3); +} + +HWTEST_F(InputMethodAbilityTest, onAttributeChange_009, TestSize.Level0) +{ + InputAttribute attribute = { 1, 2 }; + EXPECT_CALL(*kdListener_, OnEditorAttributeChange(attribute)).Times(1); + inputMethodAbility_->OnAttributeChange(attribute); +} + +HWTEST_F(InputMethodAbilityTest, insertText_001, TestSize.Level0) +{ + inputMethodAbility_->kdListener_ = nullptr; + InputAttribute attribute = { 1, 2 }; + inputMethodAbility_->OnAttributeChange(attribute); +} + +HWTEST_F(InputMethodAbilityTest, insertText_002, TestSize.Level0) +{ + EXPECT_CALL(*imeListener_, OnInputStop()).WillOnce(Return(ErrorCode::NO_ERROR)); + int32_t result = inputMethodAbility_->OnStopInputService(true); + EXPECT_EQ(result, ErrorCode::NO_ERROR); +} + +HWTEST_F(InputMethodAbilityTest, insertText_003, TestSize.Level0) +{ + inputMethodAbility_->imeListener_ = nullptr; + int32_t result = inputMethodAbility_->OnStopInputService(true); + EXPECT_EQ(result, ErrorCode::ERROR_IME_NOT_STARTED); +} + +HWTEST_F(InputMethodAbilityTest, insertText_004, TestSize.Level0) +{ + int32_t result = inputMethodAbility_->HideKeyboard(); + EXPECT_EQ(result, ErrorCode::NO_ERROR); +} + +HWTEST_F(InputMethodAbilityTest, insertText_005, TestSize.Level0) +{ + EXPECT_CALL(*imeListener_, OnKeyboardStatus(true)).Times(1); + int32_t result = inputMethodAbility_->ShowKeyboard(); + EXPECT_EQ(result, ErrorCode::NO_ERROR); +} + +HWTEST_F(InputMethodAbilityTest, insertText_006, TestSize.Level0) +{ + inputMethodAbility_->imeListener_ = nullptr; + int32_t result = inputMethodAbility_->ShowKeyboard(); + EXPECT_EQ(result, ErrorCode::ERROR_IME); +} + +HWTEST_F(InputMethodAbilityTest, insertText_007, TestSize.Level0) +{ + EXPECT_CALL(*imeListener_, OnInputStart()).Times(1); + int32_t result = inputMethodAbility_->InvokeStartInputCallback(true); + EXPECT_EQ(result, ErrorCode::NO_ERROR); +} + +HWTEST_F(InputMethodAbilityTest, insertText_008, TestSize.Level0) +{ + inputMethodAbility_->imeListener_ = nullptr; + int32_t result = inputMethodAbility_->InvokeStartInputCallback(true); + EXPECT_EQ(result, ErrorCode::ERROR_IME); +} +HWTEST_F(InputMethodAbilityTest, insertText_009, TestSize.Level0) +{ + // Arrange + EXPECT_CALL(*mockAbility, GetInputDataChannelProxy()).WillRepeatedly(Return(mockChannel)); + // 模拟panel的遍历 + + // Act + InputMethodConfig textConfig; + textConfig.windowId = 1; + mockAbility->SetCallingWindow(textConfig); + + // Assert + // 验证窗口ID已设置 +} + +HWTEST_F(InputMethodAbilityTest, insertText_010, TestSize.Level0) +{ + // Arrange + EXPECT_CALL(*mockAbility, GetInputDataChannelProxy()).WillOnce(Return(nullptr)); + + // Act + int32_t result = mockAbility->InsertText("test"); + + // Assert + EXPECT_EQ(result, ErrorCode::ERROR_CLIENT_NULL_POINTER); +} + +HWTEST_F(InputMethodAbilityTest, insertText_011, TestSize.Level0) +{ + // Arrange + EXPECT_CALL(*mockAbility, GetInputDataChannelProxy()).WillOnce(Return(mockChannel)); + EXPECT_CALL(*mockChannel, DeleteForward(1)).WillOnce(Return(ErrorCode::NO_ERROR)); + + // Act + int32_t result = mockAbility->DeleteForward(1); + + // Assert + EXPECT_EQ(result, ErrorCode::NO_ERROR); +} + +HWTEST_F(InputMethodAbilityTest, clearSystemCmdChannel_001, TestSize.Level0) +{ + // Arrange + EXPECT_CALL(*mockAbility, IsImeTerminating()).WillOnce(Return(true)); + + // Act + int32_t result = mockAbility->HideKeyboardSelf(); + + // Assert + EXPECT_EQ(result, ErrorCode::NO_ERROR); +} + +HWTEST_F(InputMethodAbilityTest, clearSystemCmdChannel_002, TestSize.Level0) +{ + // Arrange + EXPECT_CALL(*mockAbility, GetInputDataChannelProxy()).WillOnce(Return(mockChannel)); + + // Act + int32_t result = mockAbility->SelectByRange(-1, 10); + + // Assert + EXPECT_EQ(result, ErrorCode::ERROR_PARAMETER_CHECK_FAILED); +} + +HWTEST_F(InputMethodAbilityTest, clearSystemCmdChannel_003, TestSize.Level0) +{ + EXPECT_CALL(*inputMethodAbility_, GetInputDataChannelProxy()).WillOnce(Return(nullptr)); + + TextTotalConfig textConfig; + int32_t result = inputMethodAbility_->GetTextConfig(textConfig); + + EXPECT_EQ(result, ErrorCode::ERROR_CLIENT_NULL_POINTER); +} + +HWTEST_F(InputMethodAbilityTest, clearSystemCmdChannel_004, TestSize.Level0) +{ + auto mockChannel = std::make_shared(); + EXPECT_CALL(*inputMethodAbility_, GetInputDataChannelProxy()).WillOnce(Return(mockChannel)); + EXPECT_CALL(*mockChannel, GetTextConfig(_)).WillOnce(Return(ErrorCode::NO_ERROR)); + EXPECT_CALL(*inputMethodAbility_, GetInputAttribute()).WillOnce(Return(InputAttribute{ .bundleName = "testBundle" })); + + TextTotalConfig textConfig; + int32_t result = inputMethodAbility_->GetTextConfig(textConfig); + + EXPECT_EQ(result, ErrorCode::NO_ERROR); + EXPECT_EQ(textConfig.inputAttribute.bundleName, "testBundle"); +} + +HWTEST_F(InputMethodAbilityTest, clearSystemCmdChannel_005, TestSize.Level0) +{ + auto mockChannel = std::make_shared(); + EXPECT_CALL(*inputMethodAbility_, GetInputDataChannelProxy()).WillOnce(Return(mockChannel)); + EXPECT_CALL(*mockChannel, GetTextConfig(_)).WillOnce(Return(ErrorCode::ERROR_CLIENT_NULL_POINTER)); + + TextTotalConfig textConfig; + int32_t result = inputMethodAbility_->GetTextConfig(textConfig); + + EXPECT_EQ(result, ErrorCode::ERROR_CLIENT_NULL_POINTER); + EXPECT_TRUE(textConfig.inputAttribute.bundleName.empty()); +} + +HWTEST_F(InputMethodAbilityTest, clearSystemCmdChannel_006, TestSize.Level0) +{ + int32_t security = 0; + ability_->securityMode_.store(1); + int32_t result = ability_->GetSecurityMode(security); + EXPECT_EQ(result, ErrorCode::NO_ERROR); + EXPECT_EQ(security, 1); +} + +HWTEST_F(InputMethodAbilityTest, clearSystemCmdChannel_007, TestSize.Level0) +{ + int32_t security = 0; + ability_->securityMode_.store(INVALID_SECURITY_MODE); + EXPECT_CALL(*mockImsaProxy_, GetSecurityMode(_)).WillOnce(DoAll(SetArgReferee<0>(1), Return(ErrorCode::NO_ERROR))); + EXPECT_CALL(*ability_, GetImsaProxy()).WillOnce(Return(mockImsaProxy_)); + int32_t result = ability_->GetSecurityMode(security); + EXPECT_EQ(result, ErrorCode::NO_ERROR); + EXPECT_EQ(security, 1); +} + +HWTEST_F(InputMethodAbilityTest, clearSystemCmdChannel_008, TestSize.Level0) +{ + int32_t security = 0; + ability_->securityMode_.store(INVALID_SECURITY_MODE); + EXPECT_CALL(*ability_, GetImsaProxy()).WillOnce(Return(nullptr)); + int32_t result = ability_->GetSecurityMode(security); + EXPECT_EQ(result, ErrorCode::ERROR_NULL_POINTER); +} + +HWTEST_F(InputMethodAbilityTest, clearSystemCmdChannel_009, TestSize.Level0) +{ + ability_->systemCmdChannelProxy_ = nullptr; + EXPECT_CALL(*ability_, GetSystemCmdChannelProxy()).WillOnce(Return(nullptr)); + ability_->ClearSystemCmdChannel(); +} + +HWTEST_F(InputMethodAbilityTest, clearInputAttribute_001, TestSize.Level0) +{ + ability_->systemCmdChannelProxy_ = mockSystemCmdChannelProxy_; + EXPECT_CALL(*ability_, GetSystemCmdChannelProxy()).WillOnce(Return(mockSystemCmdChannelProxy_)); + ability_->ClearSystemCmdChannel(); + EXPECT_EQ(ability_->systemCmdChannelProxy_, nullptr); +} + +HWTEST_F(InputMethodAbilityTest, clearInputAttribute_002, TestSize.Level0) +{ + EXPECT_CALL(*ability_, GetSystemCmdChannelProxy()).WillOnce(Return(nullptr)); + EXPECT_CALL(*ability_, GetInputMethodAgentStub()).WillOnce(Return(mockInputMethodAgentStub_)); + int32_t result = ability_->OnConnectSystemCmd(mockSystemCmdChannelProxy_, mockInputMethodAgentStub_); + EXPECT_EQ(result, ErrorCode::NO_ERROR); +} + +HWTEST_F(InputMethodAbilityTest, clearInputAttribute_003, TestSize.Level0) +{ + ability_->imeListener_ = nullptr; + int32_t result = ability_->OnSecurityChange(1); + EXPECT_EQ(result, ErrorCode::ERROR_BAD_PARAMETERS); +} + +HWTEST_F(InputMethodAbilityTest, clearInputAttribute_004, TestSize.Level0) +{ + ability_->imeListener_ = mockImeListener_; + EXPECT_CALL(*mockImeListener_, OnSecurityChange(1)).Times(1); + int32_t result = ability_->OnSecurityChange(1); + EXPECT_EQ(result, ErrorCode::NO_ERROR); +} + +HWTEST_F(InputMethodAbilityTest, clearInputAttribute_005, TestSize.Level0) +{ + PanelInfo panelInfo; + panelInfo.panelType = PanelType::SOFT_KEYBOARD; + EXPECT_CALL(*mockInputMethodPanel_, CreatePanel(_, panelInfo)).WillOnce(Return(ErrorCode::NO_ERROR)); + int32_t result = ability_->CreatePanel(nullptr, panelInfo, mockInputMethodPanel_); + EXPECT_EQ(result, ErrorCode::NO_ERROR); +} + +HWTEST_F(InputMethodAbilityTest, clearInputAttribute_006, TestSize.Level0) +{ + int32_t result = ability_->DestroyPanel(nullptr); + EXPECT_EQ(result, ErrorCode::ERROR_BAD_PARAMETERS); +} + +HWTEST_F(InputMethodAbilityTest, clearInputAttribute_007, TestSize.Level0) +{ + int32_t result = ability_->ShowPanel(nullptr); + EXPECT_EQ(result, ErrorCode::ERROR_BAD_PARAMETERS); +} + +HWTEST_F(InputMethodAbilityTest, clearInputAttribute_008, TestSize.Level0) +{ + int32_t result = ability_->HidePanel(nullptr); + EXPECT_EQ(result, ErrorCode::ERROR_BAD_PARAMETERS); +} + +HWTEST_F(InputMethodAbilityTest, clearInputAttribute_009, TestSize.Level0) +{ + SysPanelStatus sysPanelStatus; + EXPECT_CALL(*ability_, GetSystemCmdChannelProxy()).WillOnce(Return(nullptr)); + int32_t result = ability_->NotifyPanelStatus(PanelType::SOFT_KEYBOARD, sysPanelStatus); + EXPECT_EQ(result, ErrorCode::NO_ERROR); +} + +HWTEST_F(InputMethodAbilityTest, clearInputAttribute_010, TestSize.Level0) +{ + InputAttribute inputAttribute; + ability_->SetInputAttribute(inputAttribute); + // 验证属性是否设置 +} + +HWTEST_F(InputMethodAbilityTest, clearInputAttribute_011, TestSize.Level0) +{ + // 设置 + InputAttribute inputAttribute = { true, false, true }; + inputMethodAbility_->inputAttribute_ = inputAttribute; + + // 操作 + inputMethodAbility_->ClearInputAttribute(); + + // 验证 + EXPECT_EQ(inputMethodAbility_->GetInputAttribute(), InputAttribute{}); +} + +HWTEST_F(InputMethodAbilityTest, finishTextPreview_001, TestSize.Level0) +{ + // 设置 + EXPECT_CALL(*mockInputMethodAbility_, GetImsaProxy()).WillOnce(Return(nullptr)); + + // 操作 + int32_t result = mockInputMethodAbility_->HideKeyboard(Trigger::IME_APP); + + // 验证 + EXPECT_EQ(result, ErrorCode::ERROR_IME); +} + +HWTEST_F(InputMethodAbilityTest, finishTextPreview_002, TestSize.Level0) +{ + // 设置 + EXPECT_CALL(*mockInputMethodAbility_, GetImsaProxy()).WillOnce(Return(std::make_shared())); + EXPECT_CALL(*mockInputMethodAbility_, GetSoftKeyboardPanel()).WillOnce(Return(nullptr)); + + // 操作 + int32_t result = mockInputMethodAbility_->HideKeyboard(Trigger::IME_APP); + + // 验证 + EXPECT_EQ(result, ErrorCode::ERROR_IME); +} + +HWTEST_F(InputMethodAbilityTest, finishTextPreview_003, TestSize.Level0) +{ + // 设置 + auto mockPanel = std::make_shared(); + EXPECT_CALL(*mockInputMethodAbility_, GetSoftKeyboardPanel()).WillOnce(Return(mockPanel)); + EXPECT_CALL(*mockPanel, GetPanelFlag()).WillOnce(Return(FLG_CANDIDATE_COLUMN)); + + // 操作 + int32_t result = mockInputMethodAbility_->HideKeyboard(Trigger::IME_APP); + + // 验证 + EXPECT_EQ(result, ErrorCode::NO_ERROR); +} + +HWTEST_F(InputMethodAbilityTest, finishTextPreview_004, TestSize.Level0) +{ + // 设置 + mockInputMethodAbility_->isCurrentIme_ = true; + + // 操作 + bool result = mockInputMethodAbility_->IsCurrentIme(); + + // 验证 + EXPECT_TRUE(result); +} + +HWTEST_F(InputMethodAbilityTest, finishTextPreview_005, TestSize.Level0) +{ + // 设置 + mockInputMethodAbility_->isDefaultIme_ = true; + + // 操作 + bool result = mockInputMethodAbility_->IsDefaultIme(); + + // 验证 + EXPECT_TRUE(result); +} + +HWTEST_F(InputMethodAbilityTest, finishTextPreview_006, TestSize.Level0) +{ + // 设置 + mockInputMethodAbility_->imeListener_ = nullptr; + + // 操作 + bool result = mockInputMethodAbility_->IsEnable(); + + // 验证 + EXPECT_FALSE(result); +} + +HWTEST_F(InputMethodAbilityTest, finishTextPreview_007, TestSize.Level0) +{ + // 设置 + auto mockPanel = std::make_shared(); + EXPECT_CALL(*mockInputMethodAbility_, GetSoftKeyboardPanel()).WillOnce(Return(mockPanel)); + + // 操作 + int32_t result = mockInputMethodAbility_->ExitCurrentInputType(); + + // 验证 + EXPECT_EQ(result, ErrorCode::NO_ERROR); +} + +HWTEST_F(InputMethodAbilityTest, finishTextPreview_008, TestSize.Level0) +{ + // 设置 + PanelInfo panelInfo = { PanelType::SOFT_KEYBOARD, FLG_CANDIDATE_COLUMN }; + bool isShown = false; + + // 操作 + int32_t result = mockInputMethodAbility_->IsPanelShown(panelInfo, isShown); + + // 验证 + EXPECT_EQ(result, ErrorCode::NO_ERROR); + EXPECT_FALSE(isShown); +} + +HWTEST_F(InputMethodAbilityTest, finishTextPreview_009, TestSize.Level0) +{ + // 设置 + auto mockChannel = std::make_shared(); + EXPECT_CALL(*mockInputMethodAbility_, GetSoftKeyboardPanel()).WillOnce(Return(nullptr)); + + // 操作 + mockInputMethodAbility_->OnClientInactive(mockChannel); + + // 验证 + // 需要验证日志或状态变化,但目前没有直接的返回值或状态变化 +} + +HWTEST_F(InputMethodAbilityTest, finishTextPreview_010, TestSize.Level0) +{ + // 设置 + EXPECT_CALL(*mockInputMethodAbility_, GetInputDataChannelProxy()) + .WillOnce(Return(std::make_shared())); + + // 操作 + mockInputMethodAbility_->NotifyKeyboardHeight(100, FLG_FIXED); + + // 验证 + // 需要验证日志或状态变化,但目前没有直接的返回值或状态变化 +} + +HWTEST_F(InputMethodAbilityTest, finishTextPreview_011, TestSize.Level0) +{ + // 设置 + std::unordered_map privateCommand = { + { "key", PrivateDataValue{} }; // 假设的私有命令 + EXPECT_CALL(*mockInputMethodAbility_, IsDefaultIme()).WillOnce(Return(true)); + EXPECT_CALL(*mockInputMethodAbility_, GetInputDataChannelProxy()) + .WillOnce(Return(std::make_shared())); + + // 操作 + int32_t result = mockInputMethodAbility_->SendPrivateCommand(privateCommand); + + // 验证 + EXPECT_EQ(result, ErrorCode::NO_ERROR); +} + +HWTEST_F(InputMethodAbilityTest, finishTextPreview_012, TestSize.Level0) +{ + // 设置 + std::unordered_map privateCommand = { + { "key", PrivateDataValue{} }; // 假设的私有命令 + EXPECT_CALL(*mockInputMethodAbility_, GetImsaProxy()).WillOnce(Return(std::make_shared())); + + // 操作 + int32_t result = mockInputMethodAbility_->ReceivePrivateCommand(privateCommand); + + // 验证 + EXPECT_EQ(result, ErrorCode::NO_ERROR); +} + +HWTEST_F(InputMethodAbilityTest, finishTextPreview_013, TestSize.Level0) +{ + EXPECT_CALL(*inputMethodAbility_, GetInputDataChannelProxy()).WillOnce(testing::Return(nullptr)); + int32_t result = inputMethodAbility_->SetPreviewText("test", Range{ 0, 4 }); + EXPECT_EQ(result, ErrorCode::ERROR_CLIENT_NULL_POINTER); +} + +HWTEST_F(InputMethodAbilityTest, finishTextPreview_014, TestSize.Level0) +{ + EXPECT_CALL(*inputMethodAbility_, GetInputDataChannelProxy()).WillOnce(testing::Return(inputDataChannelProxy_)); + EXPECT_CALL(*inputDataChannelProxy_, SetPreviewText("test", Range{ 0, 4 })) + .WillOnce(testing::Return(ErrorCode::NO_ERROR)); + int32_t result = inputMethodAbility_->SetPreviewText("test", Range{ 0, 4 }); + EXPECT_EQ(result, ErrorCode::NO_ERROR); +} + +HWTEST_F(InputMethodAbilityTest, finishTextPreview_015, TestSize.Level0) +{ + EXPECT_CALL(*inputMethodAbility_, GetInputDataChannelProxy()).WillOnce(testing::Return(nullptr)); + int32_t result = inputMethodAbility_->FinishTextPreview(true); + EXPECT_EQ(result, ErrorCode::ERROR_CLIENT_NULL_POINTER); +} + +HWTEST_F(InputMethodAbilityTest, getMsgHandlerCallback_001, TestSize.Level0) +{ + EXPECT_CALL(*inputMethodAbility_, GetInputDataChannelProxy()).WillOnce(testing::Return(inputDataChannelProxy_)); + EXPECT_CALL(*inputDataChannelProxy_, FinishTextPreview(true)).WillOnce(testing::Return(ErrorCode::NO_ERROR)); + int32_t result = inputMethodAbility_->FinishTextPreview(true); + EXPECT_EQ(result, ErrorCode::NO_ERROR); +} + +HWTEST_F(InputMethodAbilityTest, getMsgHandlerCallback_002, TestSize.Level0) +{ + EXPECT_CALL(*inputMethodAbility_, GetInputDataChannelProxy()).WillOnce(testing::Return(nullptr)); + CallingWindowInfo windowInfo; + int32_t result = inputMethodAbility_->GetCallingWindowInfo(windowInfo); + EXPECT_EQ(result, ErrorCode::ERROR_CLIENT_NOT_FOUND); +} + +HWTEST_F(InputMethodAbilityTest, getMsgHandlerCallback_003, TestSize.Level0) +{ + EXPECT_CALL(*inputMethodAbility_, GetInputDataChannelProxy()).WillOnce(testing::Return(inputDataChannelProxy_)); + EXPECT_CALL(*inputMethodAbility_, GetSoftKeyboardPanel()).WillOnce(testing::Return(nullptr)); + CallingWindowInfo windowInfo; + int32_t result = inputMethodAbility_->GetCallingWindowInfo(windowInfo); + EXPECT_EQ(result, ErrorCode::ERROR_PANEL_NOT_FOUND); +} + +HWTEST_F(InputMethodAbilityTest, getMsgHandlerCallback_004, TestSize.Level0) +{ + EXPECT_CALL(*inputMethodAbility_, GetInputDataChannelProxy()).WillOnce(testing::Return(inputDataChannelProxy_)); + EXPECT_CALL(*inputMethodAbility_, GetSoftKeyboardPanel()).WillOnce(testing::Return(softKeyboardPanel_)); + EXPECT_CALL(*inputMethodAbility_, GetTextConfig(testing::_)).WillOnce(testing::Return(ErrorCode::NO_ERROR)); + EXPECT_CALL(*softKeyboardPanel_, SetCallingWindow(testing::_)).WillOnce(testing::Return(ErrorCode::NO_ERROR)); + EXPECT_CALL(*softKeyboardPanel_, GetCallingWindowInfo(testing::_)).WillOnce(testing::Return(ErrorCode::NO_ERROR)); + CallingWindowInfo windowInfo; + int32_t result = inputMethodAbility_->GetCallingWindowInfo(windowInfo); + EXPECT_EQ(result, ErrorCode::NO_ERROR); +} + +HWTEST_F(InputMethodAbilityTest, getMsgHandlerCallback_005, TestSize.Level0) +{ + PanelStatusInfo info; + info.panelInfo.panelFlag = PanelFlag::FLG_CANDIDATE_COLUMN; + inputMethodAbility_->NotifyPanelStatusInfo(info, inputDataChannelProxy_); + // 预期没有交互 +} + +HWTEST_F(InputMethodAbilityTest, getMsgHandlerCallback_006, TestSize.Level0) +{ + PanelStatusInfo info; + info.panelInfo.panelFlag = PanelFlag::FLG_NORMAL; + EXPECT_CALL(*inputDataChannelProxy_, NotifyPanelStatusInfo(info)); + inputMethodAbility_->NotifyPanelStatusInfo(info, inputDataChannelProxy_); +} + +HWTEST_F(InputMethodAbilityTest, getMsgHandlerCallback_007, TestSize.Level0) +{ + EXPECT_CALL(*inputMethodAbility_, GetSecurityMode(testing::_)) + .WillOnce(testing::DoAll(testing::SetArgReferee<0>(1), testing::Return(ErrorCode::NO_ERROR))); + ArrayBuffer arrayBuffer; + int32_t result = inputMethodAbility_->SendMessage(arrayBuffer); + EXPECT_EQ(result, ErrorCode::ERROR_SECURITY_MODE_OFF); +} + +HWTEST_F(InputMethodAbilityTest, getMsgHandlerCallback_008, TestSize.Level0) +{ + EXPECT_CALL(*inputMethodAbility_, GetSecurityMode(testing::_)) + .WillOnce(testing::DoAll(testing::SetArgReferee<0>(static_cast(SecurityMode::FULL)), + testing::Return(ErrorCode::NO_ERROR))); + EXPECT_CALL(*inputMethodAbility_, GetInputDataChannelProxy()).WillOnce(testing::Return(nullptr)); + ArrayBuffer arrayBuffer; + int32_t result = inputMethodAbility_->SendMessage(arrayBuffer); + EXPECT_EQ(result, ErrorCode::ERROR_CLIENT_NULL_POINTER); +} + +HWTEST_F(InputMethodAbilityTest, getMsgHandlerCallback_009, TestSize.Level0) +{ + EXPECT_CALL(*inputMethodAbility_, GetSecurityMode(testing::_)) + .WillOnce(testing::DoAll(testing::SetArgReferee<0>(1), testing::Return(ErrorCode::NO_ERROR))); + ArrayBuffer arrayBuffer; + int32_t result = inputMethodAbility_->RecvMessage(arrayBuffer); + EXPECT_EQ(result, ErrorCode::ERROR_SECURITY_MODE_OFF); +} + +HWTEST_F(InputMethodAbilityTest, getMsgHandlerCallback_010, TestSize.Level0) +{ + EXPECT_CALL(*inputMethodAbility_, GetSecurityMode(testing::_)) + .WillOnce(testing::DoAll(testing::SetArgReferee<0>(static_cast(SecurityMode::FULL)), + testing::Return(ErrorCode::NO_ERROR))); + EXPECT_CALL(*inputMethodAbility_, GetMsgHandlerCallback()).WillOnce(testing::Return(nullptr)); + ArrayBuffer arrayBuffer; + int32_t result = inputMethodAbility_->RecvMessage(arrayBuffer); + EXPECT_EQ(result, ErrorCode::ERROR_MSG_HANDLER_NOT_REGIST); +} + +HWTEST_F(InputMethodAbilityTest, getMsgHandlerCallback_011, TestSize.Level0) +{ + EXPECT_CALL(*msgHandlerCallback_, OnTerminated()); + inputMethodAbility_->RegisterMsgHandler(msgHandlerCallback_); + inputMethodAbility_->RegisterMsgHandler(nullptr); +} + +HWTEST_F(InputMethodAbilityTest, getMsgHandlerCallback_012, TestSize.Level0) +{ + inputMethodAbility_->RegisterMsgHandler(msgHandlerCallback_); + std::shared_ptr handler = inputMethodAbility_->GetMsgHandlerCallback(); + EXPECT_EQ(handler, msgHandlerCallback_); +} \ No newline at end of file diff --git a/test/unittest/functional_test/inputmethod_private_command_capi_test.cpp b/test/unittest/functional_test/inputmethod_private_command_capi_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d00b1865731d372773ce039d086109db2241c475 --- /dev/null +++ b/test/unittest/functional_test/inputmethod_private_command_capi_test.cpp @@ -0,0 +1,433 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include "global.h" +#include "native_inputmethod_types.h" + +HWTEST_F(InputMethod_PrivateCommandTest, Create_ValidKey_ShouldCreateObject) +{ + char key[] = "testKey"; + InputMethod_PrivateCommand *command = OH_PrivateCommand_Create(key, sizeof(key) - 1); + EXPECT_NE(command, nullptr); + OH_PrivateCommand_Destroy(command); +} + +HWTEST_F(InputMethod_PrivateCommandTest, Create_EmptyKey_ShouldCreateObject) +{ + char key[] = ""; + InputMethod_PrivateCommand *command = OH_PrivateCommand_Create(key, 0); + EXPECT_NE(command, nullptr); + OH_PrivateCommand_Destroy(command); +} + +HWTEST_F(InputMethod_PrivateCommandTest, Destroy_ValidCommand_ShouldNotCrash) +{ + char key[] = "testKey"; + InputMethod_PrivateCommand *command = OH_PrivateCommand_Create(key, sizeof(key) - 1); + EXPECT_NE(command, nullptr); + OH_PrivateCommand_Destroy(command); +} + +HWTEST_F(InputMethod_PrivateCommandTest, Destroy_NullCommand_ShouldLogError) +{ + OH_PrivateCommand_Destroy(nullptr); +} + +HWTEST_F(InputMethod_PrivateCommandTest, SetKey_ValidCommandAndKey_ShouldSetKey) +{ + char key[] = "testKey"; + InputMethod_PrivateCommand *command = OH_PrivateCommand_Create(key, sizeof(key) - 1); + EXPECT_NE(command, nullptr); + + char newKey[] = "newKey"; + InputMethod_ErrorCode result = OH_PrivateCommand_SetKey(command, newKey, sizeof(newKey) - 1); + EXPECT_EQ(result, IME_ERR_OK); + + const char *actualKey; + size_t keyLength; + result = OH_PrivateCommand_GetKey(command, &actualKey, &keyLength); + EXPECT_EQ(result, IME_ERR_OK); + EXPECT_STREQ(actualKey, newKey); + EXPECT_EQ(keyLength, sizeof(newKey) - 1); + + OH_PrivateCommand_Destroy(command); +} + +HWTEST_F(InputMethod_PrivateCommandTest, SetKey_NullCommand_ShouldReturnError) +{ + char key[] = "testKey"; + InputMethod_ErrorCode result = OH_PrivateCommand_SetKey(nullptr, key, sizeof(key) - 1); + EXPECT_EQ(result, IME_ERR_NULL_POINTER); +} + +HWTEST_F(InputMethod_PrivateCommandTest, SetKey_NullKey_ShouldReturnError) +{ + char key[] = "testKey"; + InputMethod_PrivateCommand *command = OH_PrivateCommand_Create(key, sizeof(key) - 1); + EXPECT_NE(command, nullptr); + + InputMethod_ErrorCode result = OH_PrivateCommand_SetKey(command, nullptr, sizeof(key) - 1); + EXPECT_EQ(result, IME_ERR_NULL_POINTER); + + OH_PrivateCommand_Destroy(command); +} + +HWTEST_F(InputMethod_PrivateCommandTest, SetBoolValue_ValidCommand_ShouldSetValue) +{ + char key[] = "testKey"; + InputMethod_PrivateCommand *command = OH_PrivateCommand_Create(key, sizeof(key) - 1); + EXPECT_NE(command, nullptr); + + InputMethod_ErrorCode result = OH_PrivateCommand_SetBoolValue(command, true); + EXPECT_EQ(result, IME_ERR_OK); + + bool value; + result = OH_PrivateCommand_GetBoolValue(command, &value); + EXPECT_EQ(result, IME_ERR_OK); + EXPECT_TRUE(value); + + OH_PrivateCommand_Destroy(command); +} + +HWTEST_F(InputMethod_PrivateCommandTest, SetBoolValue_NullCommand_ShouldReturnError) +{ + InputMethod_ErrorCode result = OH_PrivateCommand_SetBoolValue(nullptr, true); + EXPECT_EQ(result, IME_ERR_NULL_POINTER); +} + +HWTEST_F(InputMethod_PrivateCommandTest, SetIntValue_ValidCommand_ShouldSetValue) +{ + char key[] = "testKey"; + InputMethod_PrivateCommand *command = OH_PrivateCommand_Create(key, sizeof(key) - 1); + EXPECT_NE(command, nullptr); + + InputMethod_ErrorCode result = OH_PrivateCommand_SetIntValue(command, 42); + EXPECT_EQ(result, IME_ERR_OK); + + int32_t value; + result = OH_PrivateCommand_GetIntValue(command, &value); + EXPECT_EQ(result, IME_ERR_OK); + EXPECT_EQ(value, 42); + + OH_PrivateCommand_Destroy(command); +} + +HWTEST_F(InputMethod_PrivateCommandTest, SetIntValue_NullCommand_ShouldReturnError) +{ + InputMethod_ErrorCode result = OH_PrivateCommand_SetIntValue(nullptr, 42); + EXPECT_EQ(result, IME_ERR_NULL_POINTER); +} + +HWTEST_F(InputMethod_PrivateCommandTest, SetStrValue_ValidCommandAndValue_ShouldSetValue) +{ + char key[] = "testKey"; + InputMethod_PrivateCommand *command = OH_PrivateCommand_Create(key, sizeof(key) - 1); + EXPECT_NE(command, nullptr); + + char value[] = "testValue"; + InputMethod_ErrorCode result = OH_PrivateCommand_SetStrValue(command, value, sizeof(value) - 1); + EXPECT_EQ(result, IME_ERR_OK); + + const char *actualValue; + size_t valueLength; + result = OH_PrivateCommand_GetStrValue(command, &actualValue, &valueLength); + EXPECT_EQ(result, IME_ERR_OK); + EXPECT_STREQ(actualValue, value); + EXPECT_EQ(valueLength, sizeof(value) - 1); + + OH_PrivateCommand_Destroy(command); +} + +HWTEST_F(InputMethod_PrivateCommandTest, SetStrValue_NullCommand_ShouldReturnError) +{ + char value[] = "testValue"; + InputMethod_ErrorCode result = OH_PrivateCommand_SetStrValue(nullptr, value, sizeof(value) - 1); + EXPECT_EQ(result, IME_ERR_NULL_POINTER); +} + +HWTEST_F(InputMethod_PrivateCommandTest, SetStrValue_NullValue_ShouldReturnError) +{ + char key[] = "testKey"; + InputMethod_PrivateCommand *command = OH_PrivateCommand_Create(key, sizeof(key) - 1); + EXPECT_NE(command, nullptr); + + InputMethod_ErrorCode result = OH_PrivateCommand_SetStrValue(command, nullptr, 0); + EXPECT_EQ(result, IME_ERR_NULL_POINTER); + + OH_PrivateCommand_Destroy(command); +} + +HWTEST_F(InputMethod_PrivateCommandTest, GetKey_ValidCommand_ShouldReturnKey) +{ + char key[] = "testKey"; + InputMethod_PrivateCommand *command = OH_PrivateCommand_Create(key, sizeof(key) - 1); + EXPECT_NE(command, nullptr); + + const char *actualKey; + size_t keyLength; + InputMethod_ErrorCode result = OH_PrivateCommand_GetKey(command, &actualKey, &keyLength); + EXPECT_EQ(result, IME_ERR_OK); + EXPECT_STREQ(actualKey, key); + EXPECT_EQ(keyLength, sizeof(key) - 1); + + OH_PrivateCommand_Destroy(command); +} + +HWTEST_F(InputMethod_PrivateCommandTest, GetKey_NullCommand_ShouldReturnError) +{ + const char *key; + size_t keyLength; + InputMethod_ErrorCode result = OH_PrivateCommand_GetKey(nullptr, &key, &keyLength); + EXPECT_EQ(result, IME_ERR_NULL_POINTER); +} + +HWTEST_F(InputMethod_PrivateCommandTest, GetKey_NullKey_ShouldReturnError) +{ + char key[] = "testKey"; + InputMethod_PrivateCommand *command = OH_PrivateCommand_Create(key, sizeof(key) - 1); + EXPECT_NE(command, nullptr); + + size_t keyLength; + InputMethod_ErrorCode result = OH_PrivateCommand_GetKey(command, nullptr, &keyLength); + EXPECT_EQ(result, IME_ERR_NULL_POINTER); + + OH_PrivateCommand_Destroy(command); +} + +HWTEST_F(InputMethod_PrivateCommandTest, GetKey_NullKeyLength_ShouldReturnError) +{ + char key[] = "testKey"; + InputMethod_PrivateCommand *command = OH_PrivateCommand_Create(key, sizeof(key) - 1); + EXPECT_NE(command, nullptr); + + const char *actualKey; + InputMethod_ErrorCode result = OH_PrivateCommand_GetKey(command, &actualKey, nullptr); + EXPECT_EQ(result, IME_ERR_NULL_POINTER); + + OH_PrivateCommand_Destroy(command); +} + +HWTEST_F(InputMethod_PrivateCommandTest, GetValueType_ValidCommand_ShouldReturnCorrectType) +{ + char key[] = "testKey"; + InputMethod_PrivateCommand *command = OH_PrivateCommand_Create(key, sizeof(key) - 1); + EXPECT_NE(command, nullptr); + + InputMethod_ErrorCode result = OH_PrivateCommand_SetBoolValue(command, true); + EXPECT_EQ(result, IME_ERR_OK); + + InputMethod_CommandValueType type; + result = OH_PrivateCommand_GetValueType(command, &type); + EXPECT_EQ(result, IME_ERR_OK); + EXPECT_EQ(type, IME_COMMAND_VALUE_TYPE_BOOL); + + OH_PrivateCommand_Destroy(command); +} + +HWTEST_F(InputMethod_PrivateCommandTest, GetValueType_NullCommand_ShouldReturnError) +{ + InputMethod_CommandValueType type; + InputMethod_ErrorCode result = OH_PrivateCommand_GetValueType(nullptr, &type); + EXPECT_EQ(result, IME_ERR_NULL_POINTER); +} + +HWTEST_F(InputMethod_PrivateCommandTest, GetValueType_NullType_ShouldReturnError) +{ + char key[] = "testKey"; + InputMethod_PrivateCommand *command = OH_PrivateCommand_Create(key, sizeof(key) - 1); + EXPECT_NE(command, nullptr); + + InputMethod_ErrorCode result = OH_PrivateCommand_GetValueType(command, nullptr); + EXPECT_EQ(result, IME_ERR_NULL_POINTER); + + OH_PrivateCommand_Destroy(command); +} + +HWTEST_F(InputMethod_PrivateCommandTest, GetBoolValue_ValidCommand_ShouldReturnBool) +{ + char key[] = "testKey"; + InputMethod_PrivateCommand *command = OH_PrivateCommand_Create(key, sizeof(key) - 1); + EXPECT_NE(command, nullptr); + + InputMethod_ErrorCode result = OH_PrivateCommand_SetBoolValue(command, true); + EXPECT_EQ(result, IME_ERR_OK); + + bool value; + result = OH_PrivateCommand_GetBoolValue(command, &value); + EXPECT_EQ(result, IME_ERR_OK); + EXPECT_TRUE(value); + + OH_PrivateCommand_Destroy(command); +} + +HWTEST_F(InputMethod_PrivateCommandTest, GetBoolValue_NullCommand_ShouldReturnError) +{ + bool value; + InputMethod_ErrorCode result = OH_PrivateCommand_GetBoolValue(nullptr, &value); + EXPECT_EQ(result, IME_ERR_NULL_POINTER); +} + +HWTEST_F(InputMethod_PrivateCommandTest, GetBoolValue_NullValue_ShouldReturnError) +{ + char key[] = "testKey"; + InputMethod_PrivateCommand *command = OH_PrivateCommand_Create(key, sizeof(key) - 1); + EXPECT_NE(command, nullptr); + + InputMethod_ErrorCode result = OH_PrivateCommand_GetBoolValue(command, nullptr); + EXPECT_EQ(result, IME_ERR_NULL_POINTER); + + OH_PrivateCommand_Destroy(command); +} + +HWTEST_F(InputMethod_PrivateCommandTest, GetBoolValue_NonBoolValue_ShouldReturnError) +{ + char key[] = "testKey"; + InputMethod_PrivateCommand *command = OH_PrivateCommand_Create(key, sizeof(key) - 1); + EXPECT_NE(command, nullptr); + + InputMethod_ErrorCode result = OH_PrivateCommand_SetIntValue(command, 42); + EXPECT_EQ(result, IME_ERR_OK); + + bool value; + result = OH_PrivateCommand_GetBoolValue(command, &value); + EXPECT_EQ(result, IME_ERR_QUERY_FAILED); + + OH_PrivateCommand_Destroy(command); +} + +HWTEST_F(InputMethod_PrivateCommandTest, GetIntValue_ValidCommand_ShouldReturnInt) +{ + char key[] = "testKey"; + InputMethod_PrivateCommand *command = OH_PrivateCommand_Create(key, sizeof(key) - 1); + EXPECT_NE(command, nullptr); + + InputMethod_ErrorCode result = OH_PrivateCommand_SetIntValue(command, 42); + EXPECT_EQ(result, IME_ERR_OK); + + int32_t value; + result = OH_PrivateCommand_GetIntValue(command, &value); + EXPECT_EQ(result, IME_ERR_OK); + EXPECT_EQ(value, 42); + + OH_PrivateCommand_Destroy(command); +} + +HWTEST_F(InputMethod_PrivateCommandTest, GetIntValue_NullCommand_ShouldReturnError) +{ + int32_t value; + InputMethod_ErrorCode result = OH_PrivateCommand_GetIntValue(nullptr, &value); + EXPECT_EQ(result, IME_ERR_NULL_POINTER); +} + +HWTEST_F(InputMethod_PrivateCommandTest, GetIntValue_NullValue_ShouldReturnError) +{ + char key[] = "testKey"; + InputMethod_PrivateCommand *command = OH_PrivateCommand_Create(key, sizeof(key) - 1); + EXPECT_NE(command, nullptr); + + InputMethod_ErrorCode result = OH_PrivateCommand_GetIntValue(command, nullptr); + EXPECT_EQ(result, IME_ERR_NULL_POINTER); + + OH_PrivateCommand_Destroy(command); +} + +HWTEST_F(InputMethod_PrivateCommandTest, GetIntValue_NonIntValue_ShouldReturnError) +{ + char key[] = "testKey"; + InputMethod_PrivateCommand *command = OH_PrivateCommand_Create(key, sizeof(key) - 1); + EXPECT_NE(command, nullptr); + + InputMethod_ErrorCode result = OH_PrivateCommand_SetBoolValue(command, true); + EXPECT_EQ(result, IME_ERR_OK); + + int32_t value; + result = OH_PrivateCommand_GetIntValue(command, &value); + EXPECT_EQ(result, IME_ERR_QUERY_FAILED); + + OH_PrivateCommand_Destroy(command); +} + +HWTEST_F(InputMethod_PrivateCommandTest, GetStrValue_ValidCommand_ShouldReturnString) +{ + char key[] = "testKey"; + InputMethod_PrivateCommand *command = OH_PrivateCommand_Create(key, sizeof(key) - 1); + EXPECT_NE(command, nullptr); + + char value[] = "testValue"; + InputMethod_ErrorCode result = OH_PrivateCommand_SetStrValue(command, value, sizeof(value) - 1); + EXPECT_EQ(result, IME_ERR_OK); + + const char *actualValue; + size_t valueLength; + result = OH_PrivateCommand_GetStrValue(command, &actualValue, &valueLength); + EXPECT_EQ(result, IME_ERR_OK); + EXPECT_STREQ(actualValue, value); + EXPECT_EQ(valueLength, sizeof(value) - 1); + + OH_PrivateCommand_Destroy(command); +} + +HWTEST_F(InputMethod_PrivateCommandTest, GetStrValue_NullCommand_ShouldReturnError) +{ + const char *value; + size_t valueLength; + InputMethod_ErrorCode result = OH_PrivateCommand_GetStrValue(nullptr, &value, &valueLength); + EXPECT_EQ(result, IME_ERR_NULL_POINTER); +} + +HWTEST_F(InputMethod_PrivateCommandTest, GetStrValue_NullValue_ShouldReturnError) +{ + char key[] = "testKey"; + InputMethod_PrivateCommand *command = OH_PrivateCommand_Create(key, sizeof(key) - 1); + EXPECT_NE(command, nullptr); + + size_t valueLength; + InputMethod_ErrorCode result = OH_PrivateCommand_GetStrValue(command, nullptr, &valueLength); + EXPECT_EQ(result, IME_ERR_NULL_POINTER); + + OH_PrivateCommand_Destroy(command); +} + +HWTEST_F(InputMethod_PrivateCommandTest, GetStrValue_NullValueLength_ShouldReturnError) +{ + char key[] = "testKey"; + InputMethod_PrivateCommand *command = OH_PrivateCommand_Create(key, sizeof(key) - 1); + EXPECT_NE(command, nullptr); + + const char *value; + InputMethod_ErrorCode result = OH_PrivateCommand_GetStrValue(command, &value, nullptr); + EXPECT_EQ(result, IME_ERR_NULL_POINTER); + + OH_PrivateCommand_Destroy(command); +} + +HWTEST_F(InputMethod_PrivateCommandTest, GetStrValue_NonStringValue_ShouldReturnError) +{ + char key[] = "testKey"; + InputMethod_PrivateCommand *command = OH_PrivateCommand_Create(key, sizeof(key) - 1); + EXPECT_NE(command, nullptr); + + InputMethod_ErrorCode result = OH_PrivateCommand_SetIntValue(command, 42); + EXPECT_EQ(result, IME_ERR_OK); + + const char *value; + size_t valueLength; + result = OH_PrivateCommand_GetStrValue(command, &value, &valueLength); + EXPECT_EQ(result, IME_ERR_QUERY_FAILED); + + OH_PrivateCommand_Destroy(command); +} \ No newline at end of file diff --git a/test/unittest/functional_test/peruser_session_test.cpp b/test/unittest/functional_test/peruser_session_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f0e9527b994f4cedc069622eca3d16a97fa790ce --- /dev/null +++ b/test/unittest/functional_test/peruser_session_test.cpp @@ -0,0 +1,459 @@ +/* + * Copyright (C) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "peruser_session.h" + +#include + +#include + +#include "input_client_info.h" +#include "mock_input_client.h" +#include "mock_input_method_core.h" + +using namespace OHOS; +using namespace MiscServices; +using namespace testing; + +class PerUserSessionTest : public Test { +protected: + static void SetUpTestCase() + { + userId = 100; + eventHandler = std::make_shared(AppExecFwk::EventRunner::Create(true)); + perUserSession = std::make_unique(userId, eventHandler); + } + + static void TearDownTestCase() + { + // clear shared resources after all tests + } + + void SetUp() override + { + // prepare resource before each test + } + + void TearDown() override + { + // clear resource after each test + } + int userId; + std::shared_ptr eventHandler; + std::unique_ptr perUserSession; +}; +/** + * @tc.name: AddClientInfoTest + * @tc.desc: Verify that the PerUserSession::AddClientInfo method + * returns ErrorCode::NO_ERROR when adding client information + * @tc.type: FUNC + */ +TEST_F(PerUserSessionTest, AddClientInfoTest) +{ + auto mockClient = std::make_shared(); + InputClientInfo clientInfo; + clientInfo.client = mockClient; + clientInfo.config = { /* initialize configuration */ }; + clientInfo.uiExtensionTokenId = 123; + + EXPECT_EQ(perUserSession->AddClientInfo(mockClient, clientInfo, START_LISTENING), ErrorCode::NO_ERROR); +} + +/** + * @tc.name: RemoveClientInfoTest + * @tc.desc: Verify that the PerUserSession::RemoveClientInfo method works normally when removing client information + * @tc.type: FUNC + */ +TEST_F(PerUserSessionTest, RemoveClientInfoTest) +{ + auto mockClient = std::make_shared(); + InputClientInfo clientInfo; + clientInfo.client = mockClient; + clientInfo.config = { /* initialize configuration */ }; + clientInfo.uiExtensionTokenId = 123; + + perUserSession->AddClientInfo(mockClient, clientInfo, START_LISTENING); + perUserSession->RemoveClientInfo(mockClient, false); +} + +/** + * @tc.name: UpdateClientInfoTest + * @tc.desc: Verify that the PerUserSession::UpdateClientInfo method works normally when updating client information + * @tc.type: FUNC + */ +TEST_F(PerUserSessionTest, UpdateClientInfoTest) +{ + auto mockClient = std::make_shared(); + InputClientInfo clientInfo; + clientInfo.client = mockClient; + clientInfo.config = { /* initialize configuration */ }; + clientInfo.uiExtensionTokenId = 123; + + perUserSession->AddClientInfo(mockClient, clientInfo, START_LISTENING); + + std::unordered_map> updateInfos; + updateInfos[UpdateFlag::ISSHOWKEYBOARD] = true; + + perUserSession->UpdateClientInfo(mockClient, updateInfos); +} + +/** + * @tc.name: HideKeyboardTest + * @tc.desc: Verify that the PerUserSession::HideKeyboard method returns ErrorCode::NO_ERROR when hiding the keyboard + * @tc.type: FUNC + */ +TEST_F(PerUserSessionTest, HideKeyboardTest) +{ + auto mockClient = std::make_shared(); + InputClientInfo clientInfo; + clientInfo.client = mockClient; + clientInfo.config = { /* initialize configuration */ }; + clientInfo.uiExtensionTokenId = 123; + clientInfo.bindImeType = ImeType::IME; + + perUserSession->AddClientInfo(mockClient, clientInfo, START_LISTENING); + + EXPECT_CALL(*mockClient, AsObject()).WillOnce(Return(mockClient->AsObject())); + EXPECT_EQ(perUserSession->HideKeyboard(mockClient), ErrorCode::NO_ERROR); +} + +/** + * @tc.name: ShowKeyboardTest + * @tc.desc: Verify that the PerUserSession::ShowKeyboard method returns ErrorCode::NO_ERROR when showing the keyboard + * @tc.type: FUNC + */ +TEST_F(PerUserSessionTest, ShowKeyboardTest) +{ + auto mockClient = std::make_shared(); + InputClientInfo clientInfo; + clientInfo.client = mockClient; + clientInfo.config = { /* initialize configuration */ }; + clientInfo.uiExtensionTokenId = 123; + clientInfo.bindImeType = ImeType::IME; + + perUserSession->AddClientInfo(mockClient, clientInfo, START_LISTENING); + + EXPECT_CALL(*mockClient, AsObject()).WillOnce(Return(mockClient->AsObject())); + EXPECT_EQ(perUserSession->ShowKeyboard(mockClient), ErrorCode::NO_ERROR); +} + +/** + * @tc.name: OnClientDiedTest + * @tc.desc: Verify that the PerUserSession::OnClientDied method works normally when the client dies + * @tc.type: FUNC + */ +TEST_F(PerUserSessionTest, OnClientDiedTest) +{ + auto mockClient = std::make_shared(); + InputClientInfo clientInfo; + clientInfo.client = mockClient; + clientInfo.config = { /* initialize configuration */ }; + clientInfo.uiExtensionTokenId = 123; + clientInfo.bindImeType = ImeType::IME; + + perUserSession->AddClientInfo(mockClient, clientInfo, START_LISTENING); + perUserSession->OnClientDied(mockClient); +} + +/** + * @tc.name: OnImeDiedTest + * @tc.desc: Verify that the PerUserSession::OnImeDied method works normally when the input method dies + * @tc.type: FUNC + */ +TEST_F(PerUserSessionTest, OnImeDiedTest) +{ + auto mockImeCore = std::make_shared(); + perUserSession->OnImeDied(mockImeCore, ImeType::IME); +} + +/** + * @tc.name: RemoveImeTest + * @tc.desc: Verify that the PerUserSession::RemoveIme method + * returns ErrorCode::ERROR_IME_NOT_STARTED when removing the input method + * @tc.type: FUNC + */ +TEST_F(PerUserSessionTest, RemoveImeTest) +{ + auto mockImeCore = std::make_shared(); + EXPECT_EQ(perUserSession->RemoveIme(mockImeCore, ImeType::IME), ErrorCode::ERROR_IME_NOT_STARTED); +} + +/** + * @tc.name: OnHideCurrentInputTest + * @tc.desc: Verify that the PerUserSession::OnHideCurrentInput method + * returns ErrorCode::NO_ERROR when hiding the current input + * @tc.type: FUNC + */ +TEST_F(PerUserSessionTest, OnHideCurrentInputTest) +{ + auto mockClient = std::make_shared(); + InputClientInfo clientInfo; + clientInfo.client = mockClient; + clientInfo.config = { /* initialize configuration */ }; + clientInfo.uiExtensionTokenId = 123; + clientInfo.bindImeType = ImeType::IME; + + perUserSession->AddClientInfo(mockClient, clientInfo, START_LISTENING); + perUserSession->SetCurrentClient(mockClient); + EXPECT_EQ(perUserSession->OnHideCurrentInput(), ErrorCode::NO_ERROR); +} + +/** + * @tc.name: OnShowCurrentInputTest + * @tc.desc: Verify that the PerUserSession::OnShowCurrentInput method + * returns ErrorCode::NO_ERROR when showing the current input + * @tc.type: FUNC + */ +TEST_F(PerUserSessionTest, OnShowCurrentInputTest) +{ + auto mockClient = std::make_shared(); + InputClientInfo clientInfo; + clientInfo.client = mockClient; + clientInfo.config = { /* initialize configuration */ }; + clientInfo.uiExtensionTokenId = 123; + clientInfo.bindImeType = ImeType::IME; + + perUserSession->AddClientInfo(mockClient, clientInfo, START_LISTENING); + perUserSession->SetCurrentClient(mockClient); + EXPECT_EQ(perUserSession->OnShowCurrentInput(), ErrorCode::NO_ERROR); +} + +/** + * @tc.name: OnHideInputTest + * @tc.desc: Verify that the PerUserSession::OnHideInput method returns ErrorCode::NO_ERROR when hiding the input + * @tc.type: FUNC + */ +TEST_F(PerUserSessionTest, OnHideInputTest) +{ + auto mockClient = std::make_shared(); + InputClientInfo clientInfo; + clientInfo.client = mockClient; + clientInfo.config = { /* initialize configuration */ }; + clientInfo.uiExtensionTokenId = 123; + clientInfo.bindImeType = ImeType::IME; + + perUserSession->AddClientInfo(mockClient, clientInfo, START_LISTENING); + perUserSession->SetCurrentClient(mockClient); + EXPECT_EQ(perUserSession->OnHideInput(mockClient), ErrorCode::NO_ERROR); +} + +/** + * @tc.name: OnShowInputTest + * @tc.desc: Verify that the PerUserSession::OnShowInput method returns ErrorCode::NO_ERROR when showing the input + * @tc.type: FUNC + */ +TEST_F(PerUserSessionTest, OnShowInputTest) +{ + auto mockClient = std::make_shared(); + InputClientInfo clientInfo; + clientInfo.client = mockClient; + clientInfo.config = { /* initialize configuration */ }; + clientInfo.uiExtensionTokenId = 123; + clientInfo.bindImeType = ImeType::IME; + + perUserSession->AddClientInfo(mockClient, clientInfo, START_LISTENING); + perUserSession->SetCurrentClient(mockClient); + EXPECT_EQ(perUserSession->OnShowInput(mockClient), ErrorCode::NO_ERROR); +} + +/** + * @tc.name: OnHideSoftKeyBoardSelfTest + * @tc.desc: Verify that the PerUserSession::OnHideSoftKeyBoardSelf method works normally + * when hiding the soft keyboard + * @tc.type: FUNC + */ +TEST_F(PerUserSessionTest, OnHideSoftKeyBoardSelfTest) +{ + auto mockClient = std::make_shared(); + InputClientInfo clientInfo; + clientInfo.client = mockClient; + clientInfo.config = { /* initialize configuration */ }; + clientInfo.uiExtensionTokenId = 123; + clientInfo.bindImeType = ImeType::IME; + + perUserSession->AddClientInfo(mockClient, clientInfo, START_LISTENING); + perUserSession->SetCurrentClient(mockClient); + perUserSession->OnHideSoftKeyBoardSelf(); +} + +/** + * @tc.name: OnRequestShowInputTest + * @tc.desc: Verify that the PerUserSession::OnRequestShowInput method + * returns ErrorCode::ERROR_IME_NOT_STARTED when requesting to show the input + * @tc.type: FUNC + */ +TEST_F(PerUserSessionTest, OnRequestShowInputTest) +{ + EXPECT_EQ(perUserSession->OnRequestShowInput(), ErrorCode::ERROR_IME_NOT_STARTED); +} + +/** + * @tc.name: OnRequestHideInputTest + * @tc.desc: Verify that the PerUserSession::OnRequestHideInput method + * returns ErrorCode::NO_ERROR when requesting to hide the input + * @tc.type: FUNC + */ +TEST_F(PerUserSessionTest, OnRequestHideInputTest) +{ + EXPECT_EQ(perUserSession->OnRequestHideInput(getpid()), ErrorCode::NO_ERROR); +} + +/** + * @tc.name: OnPrepareInputTest + * @tc.desc: Verify that the PerUserSession::OnPrepareInput method + * returns ErrorCode::NO_ERROR when preparing the input + * @tc.type: FUNC + */ +TEST_F(PerUserSessionTest, OnPrepareInputTest) +{ + auto mockClient = std::make_shared(); + InputClientInfo clientInfo; + clientInfo.client = mockClient; + clientInfo.config = { /* initialize configuration */ }; + clientInfo.uiExtensionTokenId = 123; + + EXPECT_EQ(perUserSession->OnPrepareInput(clientInfo), ErrorCode::NO_ERROR); +} + +/** + * @tc.name: OnReleaseInputTest + * @tc.desc: Verify that the PerUserSession::OnReleaseInput method returns ErrorCode::NO_ERROR when releasing the input + * @tc.type: FUNC + */ +TEST_F(PerUserSessionTest, OnReleaseInputTest) +{ + auto mockClient = std::make_shared(); + InputClientInfo clientInfo; + clientInfo.client = mockClient; + clientInfo.config = { /* initialize configuration */ }; + clientInfo.uiExtensionTokenId = 123; + + perUserSession->AddClientInfo(mockClient, clientInfo, START_LISTENING); + EXPECT_EQ(perUserSession->OnReleaseInput(mockClient), ErrorCode::NO_ERROR); +} + +/** + * @tc.name: RemoveClientTest + * @tc.desc: Verify that the PerUserSession::RemoveClient method returns ErrorCode::NO_ERROR when removing the client + * @tc.type: FUNC + */ +TEST_F(PerUserSessionTest, RemoveClientTest) +{ + auto mockClient = std::make_shared(); + InputClientInfo clientInfo; + clientInfo.client = mockClient; + clientInfo.config = { /* initialize configuration */ }; + clientInfo.uiExtensionTokenId = 123; + + perUserSession->AddClientInfo(mockClient, clientInfo, START_LISTENING); + EXPECT_EQ(perUserSession->RemoveClient(mockClient, true), ErrorCode::NO_ERROR); +} + +/** + * @tc.name: DeactivateClientTest + * @tc.desc: Verify that the PerUserSession::DeactivateClient method works normally when deactivating the client + * @tc.type: FUNC + */ +TEST_F(PerUserSessionTest, DeactivateClientTest) +{ + auto mockClient = std::make_shared(); + InputClientInfo clientInfo; + clientInfo.client = mockClient; + clientInfo.config = { /* initialize configuration */ }; + clientInfo.uiExtensionTokenId = 123; + + perUserSession->AddClientInfo(mockClient, clientInfo, START_LISTENING); + perUserSession->DeactivateClient(mockClient); +} + +/** + * @tc.name: IsProxyImeEnableTest + * @tc.desc: Verify that the PerUserSession::IsProxyImeEnable method + * returns false when checking if the proxy IME is enabled + * @tc.type: FUNC + */ +TEST_F(PerUserSessionTest, IsProxyImeEnableTest) +{ + EXPECT_FALSE(perUserSession->IsProxyImeEnable()); +} + +/** + * @tc.name: OnStartInputTest + * @tc.desc: Verify that the PerUserSession::OnStartInput method + * returns ErrorCode::ERROR_IME_NOT_STARTED when starting the input + * @tc.type: FUNC + */ +TEST_F(PerUserSessionTest, OnStartInputTest) +{ + auto mockClient = std::make_shared(); + InputClientInfo clientInfo; + clientInfo.client = mockClient; + clientInfo.config = { /* initialize configuration */ }; + clientInfo.uiExtensionTokenId = 123; + + sptr agent; + EXPECT_EQ(perUserSession->OnStartInput(clientInfo, agent), ErrorCode::ERROR_IME_NOT_STARTED); +} + +/** + * @tc.name: BindClientWithImeTest + * @tc.desc: Verify that the PerUserSession::BindClientWithIme method + * returns ErrorCode::ERROR_IME_NOT_STARTED when binding the client with the input method + * @tc.type: FUNC + */ +TEST_F(PerUserSessionTest, BindClientWithImeTest) +{ + auto mockClient = std::make_shared(); + InputClientInfo clientInfo; + clientInfo.client = mockClient; + clientInfo.config = { /* initialize configuration */ }; + clientInfo.uiExtensionTokenId = 123; + + EXPECT_EQ(perUserSession->BindClientWithIme(std::make_shared(clientInfo), ImeType::IME, true), + ErrorCode::ERROR_IME_NOT_STARTED); +} + +/** + * @tc.name: UnBindClientWithImeTest + * @tc.desc: Verify that the PerUserSession::UnBindClientWithIme method works normally + * when unbinding the client with the input method + * @tc.type: FUNC + */ +TEST_F(PerUserSessionTest, UnBindClientWithImeTest) +{ + auto mockClient = std::make_shared(); + InputClientInfo clientInfo; + clientInfo.client = mockClient; + clientInfo.config = { /* initialize configuration */ }; + clientInfo.uiExtensionTokenId = 123; + + perUserSession->UnBindClientWithIme(std::make_shared(clientInfo), true); +} + +/** + * @tc.name: StopClientInputTest + * @tc.desc: Verify that the PerUserSession::StopClientInput method works normally when stopping the client input + * @tc.type: FUNC + */ +TEST_F(PerUserSessionTest, StopClientInputTest) +{ + auto mockClient = std::make_shared(); + InputClientInfo clientInfo; + clientInfo.client = mockClient; + clientInfo.config = { /* initialize configuration */ }; + clientInfo.uiExtensionTokenId = 123; + + perUserSession->StopClientInput(std::make_shared(clientInfo)); +} \ No newline at end of file