From 511faf6dd71074932bf8e3996787cdb504e1652b Mon Sep 17 00:00:00 2001 From: cy7717 Date: Fri, 22 Aug 2025 10:34:29 +0800 Subject: [PATCH] mod for tmp ime switch_part2 and tdd failed Signed-off-by: cy7717 --- services/src/input_method_system_ability.cpp | 6 +- test/unittest/cpp_test/BUILD.gn | 61 +++++++++ .../cpp_test/common/include/tdd_util.h | 2 +- .../unittest/cpp_test/common/src/tdd_util.cpp | 27 +++- .../cpp_test/src/ima_text_edit_test.cpp | 3 +- test/unittest/cpp_test/src/ime_proxy_test.cpp | 7 +- .../cpp_test/src/input_method_panel_test.cpp | 2 +- .../src/input_status_changed_cb_demo.cpp | 114 ++++++++++++++++ .../cpp_test/src/proxy_ime_test_demo.cpp | 124 ++++++++++++++++++ 9 files changed, 333 insertions(+), 13 deletions(-) create mode 100644 test/unittest/cpp_test/src/input_status_changed_cb_demo.cpp create mode 100644 test/unittest/cpp_test/src/proxy_ime_test_demo.cpp diff --git a/services/src/input_method_system_ability.cpp b/services/src/input_method_system_ability.cpp index af92b18f9..c6c5d79f9 100644 --- a/services/src/input_method_system_ability.cpp +++ b/services/src/input_method_system_ability.cpp @@ -2302,8 +2302,10 @@ int32_t InputMethodSystemAbility::CheckSwitchPermission(int32_t userId, const Sw return ErrorCode::NO_ERROR; } IMSA_HILOGE("have not PERMISSION_CONNECT_IME_ABILITY!"); - // switchInfo.subName.empty() check temporarily reserved for application adaptation, will be deleted soon - if (IsCurrentIme(userId, tokenId)) { + auto currentBundleName = ImeCfgManager::GetInstance().GetCurrentImeCfg(userId)->bundleName; + if (identityChecker_->IsBundleNameValid(IPCSkeleton::GetCallingTokenID(), currentBundleName) + || IsTmpIme(userId, tokenId)) { + IMSA_HILOGD("current ime!"); return ErrorCode::NO_ERROR; } IMSA_HILOGE("not current ime!"); diff --git a/test/unittest/cpp_test/BUILD.gn b/test/unittest/cpp_test/BUILD.gn index ec109c267..1b9fd495a 100644 --- a/test/unittest/cpp_test/BUILD.gn +++ b/test/unittest/cpp_test/BUILD.gn @@ -1820,3 +1820,64 @@ ohos_executable("ImeMirrorDemo") { subsystem_name = "inputmethod" part_name = "imf" } + +ohos_executable("InputStatusChangedCbDemo") { + branch_protector_ret = "pac_ret" + sanitize = { + cfi = true + cfi_cross_dso = true + debug = false + } + + sources = [ "input_status_changed_cb_demo.cpp" ] + include_dirs = [ + "${inputmethod_path}/common/include", + "${inputmethod_path}/interfaces/inner_api/inputmethod_controller/include" + ] + + deps = [ + "${inputmethod_path}/interfaces/inner_api/inputmethod_controller:inputmethod_client", + "${inputmethod_path}/test/unittest/cpp_test/common:inputmethod_tdd_util", + ] + + configs = [ ":module_private_config" ] + + external_deps = [ + "access_token:libaccesstoken_sdk", + "c_utils:utils", + ] + + install_enable = false + subsystem_name = "inputmethod" + part_name = "imf" +} + +ohos_executable("ProxyImeTestDemo") { + branch_protector_ret = "pac_ret" + sanitize = { + cfi = true + cfi_cross_dso = true + debug = false + } + + sources = [ "proxy_ime_test_demo.cpp" ] + include_dirs = [ + "${inputmethod_path}/common/include", + "${inputmethod_path}/interfaces/inner_api/inputmethod_ability/include" + ] + + deps = [ + "${inputmethod_path}/interfaces/inner_api/inputmethod_ability:inputmethod_ability", + ] + + configs = [ ":module_private_config" ] + + external_deps = [ + "access_token:libaccesstoken_sdk", + "c_utils:utils", + ] + + install_enable = false + subsystem_name = "inputmethod" + part_name = "imf" +} diff --git a/test/unittest/cpp_test/common/include/tdd_util.h b/test/unittest/cpp_test/common/include/tdd_util.h index 8dd63691b..c13afbe69 100644 --- a/test/unittest/cpp_test/common/include/tdd_util.h +++ b/test/unittest/cpp_test/common/include/tdd_util.h @@ -74,7 +74,7 @@ public: static void DisabledAllIme(); static void StartApp(const std::string &bundleName); static void StopApp(const std::string &bundleName); - static void ClickApp(const std::string &cmd); + static void ClickApp(); static bool WaitTaskEmpty(); static std::string currentBundleNameMock_; class WindowManager { diff --git a/test/unittest/cpp_test/common/src/tdd_util.cpp b/test/unittest/cpp_test/common/src/tdd_util.cpp index a25ff1dda..1fa2a75ac 100644 --- a/test/unittest/cpp_test/common/src/tdd_util.cpp +++ b/test/unittest/cpp_test/common/src/tdd_util.cpp @@ -30,6 +30,8 @@ #include "accesstoken_kit.h" #include "datashare_helper.h" +#include "display_info.h" +#include "display_manager_lite.h" #include "global.h" #include "if_system_ability_manager.h" #include "input_method_controller.h" @@ -58,6 +60,7 @@ constexpr int32_t FOURTH_PARAM_INDEX = 3; constexpr int32_t FIFTH_PARAM_INDEX = 4; constexpr int32_t SIXTH_PARAM_INDEX = 5; static constexpr int32_t MAX_TIMEOUT_WAIT_FOCUS = 2000; +constexpr int32_t WAIT_CLICK_COMPLETE = 100; uint64_t TddUtil::selfTokenID_ = 0; int32_t TddUtil::userID_ = INVALID_USER_ID; std::string TddUtil::currentBundleNameMock_; @@ -504,11 +507,33 @@ void TddUtil::StopApp(const std::string &bundleName) IMSA_HILOGI("ExecuteCmd ret = %{public}d", ret); } -void TddUtil::ClickApp(const std::string &cmd) +void TddUtil::ClickApp() { + auto displayInfo = Rosen::DisplayManagerLite::GetInstance().GetDefaultDisplay(); + if (displayInfo == nullptr) { + return; + } + int32_t Height = displayInfo->GetHeight() / 2; + int32_t Width = displayInfo->GetWidth() / 2; + std::string cmd = "uinput"; + cmd.append(" ") + .append("-T") + .append(" ") + .append("-d") + .append(" ") + .append(std::to_string(Width)) + .append(" ") + .append(std::to_string(Height)) + .append(" ") + .append("-u") + .append(" ") + .append(std::to_string(Width)) + .append(" ") + .append(std::to_string(Height)); std::string result; auto ret = TddUtil::ExecuteCmd(cmd, result); IMSA_HILOGI("ExecuteCmd ret = %{public}d", ret); + usleep(WAIT_CLICK_COMPLETE); // ensure click complete } bool TddUtil::WaitTaskEmpty() diff --git a/test/unittest/cpp_test/src/ima_text_edit_test.cpp b/test/unittest/cpp_test/src/ima_text_edit_test.cpp index 8dc8eca6a..090879404 100644 --- a/test/unittest/cpp_test/src/ima_text_edit_test.cpp +++ b/test/unittest/cpp_test/src/ima_text_edit_test.cpp @@ -40,7 +40,6 @@ namespace MiscServices { class ImaTextEditTest : public testing::Test { public: static constexpr const char *NORMAL_EDITOR_BOX_BUNDLE_NAME = "com.example.editorbox"; - static constexpr const char *CLICK_CMD = "uinput -T -d 200 200 -u 200 200"; static const std::string INSERT_TEXT; static constexpr int32_t GET_LENGTH = 2; static constexpr int32_t DEL_LENGTH = 1; @@ -60,7 +59,7 @@ public: InputMethodAbility::GetInstance().SetImeListener(std::make_shared()); InputMethodAbility::GetInstance().SetKdListener(std::make_shared()); TddUtil::StartApp(NORMAL_EDITOR_BOX_BUNDLE_NAME); - TddUtil::ClickApp(CLICK_CMD); + TddUtil::ClickApp(); EXPECT_TRUE(InputMethodEngineListenerImpl::WaitInputStart()); EXPECT_TRUE(TddUtil::WaitTaskEmpty()); } diff --git a/test/unittest/cpp_test/src/ime_proxy_test.cpp b/test/unittest/cpp_test/src/ime_proxy_test.cpp index e55831229..7d83fedf3 100644 --- a/test/unittest/cpp_test/src/ime_proxy_test.cpp +++ b/test/unittest/cpp_test/src/ime_proxy_test.cpp @@ -41,7 +41,6 @@ constexpr int32_t RETRY_INTERVAL = 100; constexpr int32_t RETRY_TIME = 30; constexpr int32_t WAIT_APP_START_COMPLETE = 1; constexpr int32_t WAIT_BIND_COMPLETE = 1; -constexpr int32_t WAIT_CLICK_COMPLETE = 100; constexpr const char *BUNDLENAME = "com.example.editorbox"; class ImeProxyTest : public testing::Test { public: @@ -119,11 +118,7 @@ public: static void ClickEditor(bool isPc) { isPc ? InputMethodEngineListenerImpl::isEnable_ = true : InputMethodEngineListenerImpl::isEnable_ = false; - static std::string cmd = "uinput -T -d 200 200 -u 200 200"; - std::string result; - auto ret = TddUtil::ExecuteCmd(cmd, result); - EXPECT_TRUE(ret); - usleep(WAIT_CLICK_COMPLETE); // ensure click complete + TddUtil::ClickApp(); } static void StopApp() diff --git a/test/unittest/cpp_test/src/input_method_panel_test.cpp b/test/unittest/cpp_test/src/input_method_panel_test.cpp index 5ec49b7a5..b3f828e42 100644 --- a/test/unittest/cpp_test/src/input_method_panel_test.cpp +++ b/test/unittest/cpp_test/src/input_method_panel_test.cpp @@ -370,7 +370,7 @@ void InputMethodPanelTest::ImcPanelShowNumCheck(uint32_t num) } bool ret = imcPanelStatusListenerCv_.wait_for(lock, std::chrono::milliseconds(IMC_WAIT_PANEL_STATUS_LISTEN_TIME), [&num] { - return num == imeShowCallbackNum_; + return imeShowCallbackNum_ >= num; }); EXPECT_TRUE(ret); } diff --git a/test/unittest/cpp_test/src/input_status_changed_cb_demo.cpp b/test/unittest/cpp_test/src/input_status_changed_cb_demo.cpp new file mode 100644 index 000000000..a6a42cce4 --- /dev/null +++ b/test/unittest/cpp_test/src/input_status_changed_cb_demo.cpp @@ -0,0 +1,114 @@ +/* + * 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 "accesstoken_kit.h" +#include "global.h" +#include "ime_event_listener.h" +#include "ime_event_monitor_manager.h" +#include "nativetoken_kits.h" +#include "tdd_util.h" +#include "token_setproc.h" + +using namespace std; +using namespace OHOS::Security::AccessToken; +using namespace OHOS::MiscServices; +std::vector> listeners_; +class InputStatusChangedListener : public ImeEventListener { +public: + void OnInputStart(uint32_t callingWndId, int32_t requestKeyboardReason) override + { + IMSA_HILOGI("callingWndId:%{public}d.", callingWndId); + } + void OnInputStop() override + { + IMSA_HILOGI("run in."); + } +}; + +void RegisterInputStatusChangedListener() +{ + auto listener = std::make_shared(); + auto ret = + ImeEventMonitorManager::GetInstance().RegisterImeEventListener(EVENT_INPUT_STATUS_CHANGED_MASK, listener); + if (ret != ErrorCode::NO_ERROR) { + IMSA_HILOGI("Register input status changed listener failed:%{public}d.", ret); + return; + } + listeners_.push_back(listener); + IMSA_HILOGI( + "Register input status changed listener succeed, current listener nums:%{public}zu.", listeners_.size()); +} + +void RegisterSameInputStatusChangedListener() +{ + if (listeners_.empty()) { + IMSA_HILOGW("has no listener ."); + return; + } + auto listener = listeners_.back(); + auto ret = + ImeEventMonitorManager::GetInstance().RegisterImeEventListener(EVENT_INPUT_STATUS_CHANGED_MASK, listener); + if (ret != ErrorCode::NO_ERROR) { + IMSA_HILOGI("Register same input status changed listener failed:%{public}d.", ret); + return; + } + IMSA_HILOGI("Register same input status changed succeed, current listener nums:%{public}zu.", listeners_.size()); +} + +void UnRegisterInputStatusChangedListener() +{ + if (listeners_.empty()) { + IMSA_HILOGW("has no listener ."); + return; + } + auto listener = listeners_.back(); + auto ret = + ImeEventMonitorManager::GetInstance().UnRegisterImeEventListener(EVENT_INPUT_STATUS_CHANGED_MASK, listener); + if (ret != ErrorCode::NO_ERROR) { + IMSA_HILOGI("UnRegister input status changed listener failed:%{public}d.", ret); + return; + } + listeners_.pop_back(); + IMSA_HILOGI("UnRegister input status changed succeed, current listener nums:%{public}zu.", listeners_.size()); +} + +int main() +{ + TddUtil::GrantNativePermission(); + int32_t input = 1; + while (true) { + printf("=====1:RegisterListener 2:RegisterSameListener 3:UnRegisterListener=====\n"); + printf("input: "); + fflush(stdout); + scanf("%d", &input); + getchar(); + printf("input:%d \n", input); + switch (input) { + case 1: + RegisterInputStatusChangedListener(); + break; + case 2: + RegisterSameInputStatusChangedListener(); + break; + case 3: + UnRegisterInputStatusChangedListener(); + break; + default: + printf("input error!\n"); + } + printf("=======================END========================="); + } +} \ No newline at end of file diff --git a/test/unittest/cpp_test/src/proxy_ime_test_demo.cpp b/test/unittest/cpp_test/src/proxy_ime_test_demo.cpp new file mode 100644 index 000000000..632ffe926 --- /dev/null +++ b/test/unittest/cpp_test/src/proxy_ime_test_demo.cpp @@ -0,0 +1,124 @@ +/* + * 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 "accesstoken_kit.h" +#include "global.h" +#include "input_method_ability_interface.h" +#include "input_method_engine_listener.h" +#include "nativetoken_kits.h" +#include "sys_cfg_parser.h" +#include "token_setproc.h" + +using namespace std; +using namespace OHOS::Security::AccessToken; +using namespace OHOS::MiscServices; +const uint32_t INSERT_TEXT_MAX_NUM = 1000; +void InsertText() +{ + for (int32_t i = 0; i < INSERT_TEXT_MAX_NUM; i++) { + InputMethodAbilityInterface::GetInstance().InsertText("A"); + } +} +class InputMethodEngineListenerImpl : public InputMethodEngineListener { +public: + void OnKeyboardStatus(bool isShow) override + { + } + void OnInputStart() override + { + std::thread t(InsertText); + t.detach(); + } + int32_t OnInputStop() override + { + return 0; + } + void OnSetCallingWindow(uint32_t windowId) override + { + } + void OnSetSubtype(const SubProperty &property) override + { + } + void ReceivePrivateCommand(const std::unordered_map &privateCommand) override + { + } + void OnInputFinish() override + { + } + bool IsEnable() override + { + return true; + } + void NotifyPreemption() override + { + } +}; + +void RegisterProxy() +{ + InputMethodAbilityInterface::GetInstance().SetImeListener(std::make_shared()); + auto ret = InputMethodAbilityInterface::GetInstance().RegisteredProxy(); + if (ret != ErrorCode::NO_ERROR) { + IMSA_HILOGI("Register proxy ime failed:%{public}d.", ret); + return; + } + IMSA_HILOGI("Register proxy ime succeed."); +} + +void UnRegisterProxy() +{ + auto ret = InputMethodAbilityInterface::GetInstance().UnRegisteredProxy(UnRegisteredType::REMOVE_PROXY_IME); + if (ret != ErrorCode::NO_ERROR) { + IMSA_HILOGI("UnRegister proxy ime failed:%{public}d.", ret); + return; + } + IMSA_HILOGI("UnRegister proxy ime succeed."); +} + +int32_t GetProxyImeUid() +{ + SystemConfig systemConfig; + SysCfgParser::ParseSystemConfig(systemConfig); + if (systemConfig.proxyImeUidList.empty()) { + return -1; + } + return *systemConfig.proxyImeUidList.begin(); +} + +int main() +{ + setuid(GetProxyImeUid()); + int32_t input = 1; + while (true) { + printf("=====1:RegisterProxy 2:UnRegisterProxy=====\n"); + printf("input: "); + fflush(stdout); + scanf("%d", &input); + getchar(); + printf("input:%d \n", input); + switch (input) { + case 1: + RegisterProxy(); + break; + case 2: + UnRegisterProxy(); + break; + default: + printf("input error!\n"); + } + printf("=======================END========================="); + } +} \ No newline at end of file -- Gitee