From 22c8d1e471bf2c06ec23aef514abc675724be263 Mon Sep 17 00:00:00 2001 From: guojin31 Date: Wed, 9 Jul 2025 12:18:05 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=85=8B=E9=9A=86=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: guojin31 --- .../src/input_method_ability.cpp | 5 ++- services/src/input_method_system_ability.cpp | 11 ++++--- .../src/input_method_ability_test.cpp | 3 +- .../src/input_method_private_member_test.cpp | 33 +++++++++++++++++++ .../unittest/resource/ohos_test/ohos_test.xml | 5 +++ 5 files changed, 50 insertions(+), 7 deletions(-) diff --git a/frameworks/native/inputmethod_ability/src/input_method_ability.cpp b/frameworks/native/inputmethod_ability/src/input_method_ability.cpp index a4656c99d..e4df50101 100644 --- a/frameworks/native/inputmethod_ability/src/input_method_ability.cpp +++ b/frameworks/native/inputmethod_ability/src/input_method_ability.cpp @@ -1443,7 +1443,10 @@ void InputMethodAbility::OnClientInactive(const sptr &channel) } return false; }); - ClearBindInfo(channel); + // cannot clear inputAttribute,otherwise it will affect hicar + ClearDataChannel(channel); + ClearAttachOptions(); + ClearBindClientInfo(); } void InputMethodAbility::NotifyKeyboardHeight(uint32_t panelHeight, PanelFlag panelFlag) diff --git a/services/src/input_method_system_ability.cpp b/services/src/input_method_system_ability.cpp index 980e98391..5ed4fa1bb 100644 --- a/services/src/input_method_system_ability.cpp +++ b/services/src/input_method_system_ability.cpp @@ -273,10 +273,13 @@ int32_t InputMethodSystemAbility::RestoreInputmethod(std::string &bundleName) } int32_t userId = GetCallingUserId(); - auto result = EnableIme(userId, bundleName); - if (result != ErrorCode::NO_ERROR) { - IMSA_HILOGE("EnableIme failed"); - return ErrorCode::ERROR_ENABLE_IME; + auto defaultIme = ImeInfoInquirer::GetInstance().GetDefaultIme(); + if (defaultIme.bundleName != bundleName) { + auto result = EnableIme(userId, bundleName); + if (result != ErrorCode::NO_ERROR) { + IMSA_HILOGE("EnableIme failed"); + return ErrorCode::ERROR_ENABLE_IME; + } } auto session = UserSessionManager::GetInstance().GetUserSession(userId); diff --git a/test/unittest/cpp_test/src/input_method_ability_test.cpp b/test/unittest/cpp_test/src/input_method_ability_test.cpp index cd15f0fce..626084f5b 100644 --- a/test/unittest/cpp_test/src/input_method_ability_test.cpp +++ b/test/unittest/cpp_test/src/input_method_ability_test.cpp @@ -2134,8 +2134,7 @@ HWTEST_F(InputMethodAbilityTest, testClearBindInfo, TestSize.Level0) options.isSimpleKeyboardEnabled = true; inputMethodAbility_.SetAttachOptions(options); inputMethodAbility_.OnClientInactive(inputMethodAbility_.dataChannelObject_); - InputAttribute nullAttribute = {}; - EXPECT_TRUE(inputMethodAbility_.GetInputAttribute() == nullAttribute); + EXPECT_TRUE(inputMethodAbility_.GetBindClientInfo().name.empty()); EXPECT_TRUE(inputMethodAbility_.dataChannelObject_ == nullptr); InputMethodAbilityTest::GetIMCDetachIMA(); } diff --git a/test/unittest/cpp_test/src/input_method_private_member_test.cpp b/test/unittest/cpp_test/src/input_method_private_member_test.cpp index b198e434c..d7c61ab35 100644 --- a/test/unittest/cpp_test/src/input_method_private_member_test.cpp +++ b/test/unittest/cpp_test/src/input_method_private_member_test.cpp @@ -625,6 +625,39 @@ HWTEST_F(InputMethodPrivateMemberTest, SA_testReleaseInput_001, TestSize.Level0) EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NULL_POINTER); } +/** + * @tc.name: III_TestRestoreInputMethod_001 + * @tc.desc: + * @tc.type: FUNC + * @tc.require: + * @tc.author: chenyu + */ +HWTEST_F(InputMethodPrivateMemberTest, III_TestRestoreInputMethod_001, TestSize.Level0) +{ + IMSA_HILOGI("InputMethodPrivateMemberTest III_TestRestoreInputMethod_001 TEST START"); + std::string bundleName = ""; + auto ret = service_->RestoreInputmethod(bundleName); + EXPECT_EQ(ret, ErrorCode::ERROR_ENABLE_IME); + + auto currentProp = InputMethodController::GetInstance()->GetCurrentInputMethod(); + ret = service_->RestoreInputmethod(currentProp->name); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + + auto defaultIme = ImeInfoInquirer::GetInstance().GetDefaultIme(); + ret = service_->RestoreInputmethod(defaultIme.bundleName); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + + bundleName = "com.example.newTestIme"; + ret = service_->RestoreInputmethod(bundleName); + EXPECT_EQ(ret, ErrorCode::ERROR_IMSA_REBOOT_OLD_IME_NOT_STOP); + + auto userId = service_->GetCallingUserId(); + UserSessionManager::GetInstance().RemoveUserSession(userId); + ret = service_->RestoreInputmethod(defaultIme.bundleName); + EXPECT_EQ(ret, ErrorCode::ERROR_NULL_POINTER); + +} + /** * @tc.name: III_TestGetCurrentInputMethodSubtype_001 * @tc.desc: diff --git a/test/unittest/resource/ohos_test/ohos_test.xml b/test/unittest/resource/ohos_test/ohos_test.xml index ef089ce73..3d572b906 100644 --- a/test/unittest/resource/ohos_test/ohos_test.xml +++ b/test/unittest/resource/ohos_test/ohos_test.xml @@ -176,6 +176,11 @@