From 1797ce240c0b0c1af56bff068249c637b5030cab Mon Sep 17 00:00:00 2001 From: shiyu-shiyu Date: Sat, 2 Aug 2025 16:52:06 +0800 Subject: [PATCH] =?UTF-8?q?=E6=89=AB=E6=8F=8F=E6=9E=AA-=E4=BA=8B=E4=BB=B6?= =?UTF-8?q?=E8=BF=94=E5=9B=9E=E4=BF=9D=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: shiyu-shiyu --- .../js_keyboard_delegate_setting.cpp | 41 +++++++++++-------- .../js_keyboard_delegate_setting.h | 4 +- .../inputmethod_ability/IInputMethodAgent.idl | 2 +- .../include/input_method_ability.h | 3 +- .../include/input_method_agent_service_impl.h | 2 +- .../src/input_method_ability.cpp | 4 +- .../src/input_method_agent_service_impl.cpp | 4 +- .../IKeyEventConsumer.idl | 2 +- .../include/keyevent_consumer_service_impl.h | 4 +- .../src/input_method_controller.cpp | 39 ++++++++++++++++-- .../src/keyevent_consumer_service_impl.cpp | 27 ++++++++++-- .../include/keyboard_listener.h | 5 +++ .../include/input_method_controller.h | 25 +++++++++-- .../include/keyboard_listener_test_impl.h | 2 + .../src/keyboard_listener_test_impl.cpp | 7 +++- .../inputmethodability_fuzzer.cpp | 7 +++- .../keyeventconsumer_fuzzer.cpp | 7 +--- .../input_method_ability_exception_test.cpp | 4 +- .../src/input_method_controller_test.cpp | 8 +++- .../cpp_test/src/input_method_editor_test.cpp | 9 +++- 20 files changed, 157 insertions(+), 49 deletions(-) diff --git a/frameworks/js/napi/inputmethodability/js_keyboard_delegate_setting.cpp b/frameworks/js/napi/inputmethodability/js_keyboard_delegate_setting.cpp index df4a5d71c..e832d721c 100644 --- a/frameworks/js/napi/inputmethodability/js_keyboard_delegate_setting.cpp +++ b/frameworks/js/napi/inputmethodability/js_keyboard_delegate_setting.cpp @@ -35,9 +35,9 @@ const std::string JsKeyboardDelegateSetting::KDS_CLASS_NAME = "KeyboardDelegate" thread_local napi_ref JsKeyboardDelegateSetting::KDSRef_ = nullptr; std::mutex JsKeyboardDelegateSetting::keyboardMutex_; -std::shared_ptr JsKeyboardDelegateSetting::keyboardDelegate_{ nullptr }; +std::shared_ptr JsKeyboardDelegateSetting::keyboardDelegate_ { nullptr }; std::mutex JsKeyboardDelegateSetting::eventHandlerMutex_; -std::shared_ptr JsKeyboardDelegateSetting::handler_{ nullptr }; +std::shared_ptr JsKeyboardDelegateSetting::handler_ { nullptr }; napi_value JsKeyboardDelegateSetting::Init(napi_env env, napi_value exports) { @@ -64,16 +64,17 @@ napi_value JsKeyboardDelegateSetting::Init(napi_env env, napi_value exports) DECLARE_NAPI_FUNCTION("createKeyboardDelegate", CreateKeyboardDelegate), DECLARE_NAPI_FUNCTION("getKeyboardDelegate", GetKeyboardDelegate), }; - NAPI_CALL(env, - napi_define_properties(env, exports, sizeof(descriptor) / sizeof(napi_property_descriptor), descriptor)); + NAPI_CALL( + env, napi_define_properties(env, exports, sizeof(descriptor) / sizeof(napi_property_descriptor), descriptor)); napi_property_descriptor properties[] = { DECLARE_NAPI_FUNCTION("on", Subscribe), DECLARE_NAPI_FUNCTION("off", UnSubscribe), }; napi_value cons = nullptr; - NAPI_CALL(env, napi_define_class(env, KDS_CLASS_NAME.c_str(), KDS_CLASS_NAME.size(), JsConstructor, nullptr, - sizeof(properties) / sizeof(napi_property_descriptor), properties, &cons)); + NAPI_CALL(env, + napi_define_class(env, KDS_CLASS_NAME.c_str(), KDS_CLASS_NAME.size(), JsConstructor, nullptr, + sizeof(properties) / sizeof(napi_property_descriptor), properties, &cons)); NAPI_CALL(env, napi_create_reference(env, cons, 1, &KDSRef_)); NAPI_CALL(env, napi_set_named_property(env, exports, KDS_CLASS_NAME.c_str(), cons)); return exports; @@ -167,8 +168,8 @@ napi_value JsKeyboardDelegateSetting::GetKDInstance(napi_env env, napi_callback_ return instance; } -void JsKeyboardDelegateSetting::RegisterListener(napi_value callback, std::string type, - std::shared_ptr callbackObj) +void JsKeyboardDelegateSetting::RegisterListener( + napi_value callback, std::string type, std::shared_ptr callbackObj) { IMSA_HILOGD("RegisterListener %{public}s", type.c_str()); std::lock_guard lock(mutex_); @@ -238,9 +239,8 @@ napi_value JsKeyboardDelegateSetting::Subscribe(napi_env env, napi_callback_info if (engine == nullptr) { return nullptr; } - std::shared_ptr callback = - std::make_shared(env, argv[1], std::this_thread::get_id(), - AppExecFwk::EventHandler::Current()); + std::shared_ptr callback = std::make_shared( + env, argv[1], std::this_thread::get_id(), AppExecFwk::EventHandler::Current()); engine->RegisterListener(argv[ARGC_ONE], type, callback); napi_value result = nullptr; @@ -298,8 +298,13 @@ napi_value JsKeyboardDelegateSetting::GetResultOnKeyEvent(napi_env env, int32_t return KeyboardDelegate; } +bool JsKeyboardDelegateSetting::OnDealKeyEvent( + const std::shared_ptr &keyEvent, sptr &consumer) +{ + return true; +} bool JsKeyboardDelegateSetting::OnDealKeyEvent(const std::shared_ptr &keyEvent, - sptr &consumer) + sptr &consumer, uint64_t msgId) { if (keyEvent == nullptr) { IMSA_HILOGE("keyEvent is nullptr"); @@ -321,8 +326,8 @@ bool JsKeyboardDelegateSetting::OnDealKeyEvent(const std::shared_ptrPostTask(task, "OnDealKeyEvent", 0, AppExecFwk::EventQueue::Priority::VIP); return true; @@ -330,7 +335,7 @@ bool JsKeyboardDelegateSetting::OnDealKeyEvent(const std::shared_ptr &keyEvent, const std::shared_ptr &keyEventEntry, const std::shared_ptr &keyCodeEntry, - const sptr &consumer) + const sptr &consumer, uint64_t msgId) { bool isKeyEventConsumed = false; bool isKeyCodeConsumed = false; @@ -379,7 +384,7 @@ void JsKeyboardDelegateSetting::DealKeyEvent(const std::shared_ptrOnKeyEventResult(consumeResult); + consumer->OnKeyEventResult(consumeResult, msgId); } } @@ -486,7 +491,7 @@ void JsKeyboardDelegateSetting::OnKeyEventConsumeResult(bool isConsumed, sptrOnKeyEventResult(keyCodeResult_ || keyEventResult_); + consumer->OnKeyEventResult(keyCodeResult_ || keyEventResult_, 0); keyEventConsume_ = false; keyEventResult_ = false; } @@ -498,7 +503,7 @@ void JsKeyboardDelegateSetting::OnKeyCodeConsumeResult(bool isConsumed, sptrOnKeyEventResult(keyCodeResult_ || keyEventResult_); + consumer->OnKeyEventResult(keyCodeResult_ || keyEventResult_, 0); keyCodeConsume_ = false; keyCodeResult_ = false; } diff --git a/frameworks/js/napi/inputmethodability/js_keyboard_delegate_setting.h b/frameworks/js/napi/inputmethodability/js_keyboard_delegate_setting.h index 315ecdadf..d8a93e218 100644 --- a/frameworks/js/napi/inputmethodability/js_keyboard_delegate_setting.h +++ b/frameworks/js/napi/inputmethodability/js_keyboard_delegate_setting.h @@ -47,6 +47,8 @@ public: void OnTextChange(const std::string &text) override; void OnEditorAttributeChange(const InputAttribute &inputAttribute) override; bool OnDealKeyEvent(const std::shared_ptr &keyEvent, sptr &consumer) override; + bool OnDealKeyEvent(const std::shared_ptr &keyEvent, sptr &consumer, + uint64_t msgId) override; void OnKeyEventConsumeResult(bool isConsumed, sptr consumer); void OnKeyCodeConsumeResult(bool isConsumed, sptr consumer); @@ -99,7 +101,7 @@ private: uv_work_t *GetUVwork(const std::string &type, EntrySetter entrySetter = nullptr); static void DealKeyEvent(const std::shared_ptr &keyEvent, const std::shared_ptr &keyEventEntry, const std::shared_ptr &keyCodeEntry, - const sptr &consumer); + const sptr &consumer, uint64_t msgId); uv_loop_s *loop_ = nullptr; std::recursive_mutex mutex_; std::map>> jsCbMap_; diff --git a/frameworks/native/inputmethod_ability/IInputMethodAgent.idl b/frameworks/native/inputmethod_ability/IInputMethodAgent.idl index a001072e1..24f6cde06 100644 --- a/frameworks/native/inputmethod_ability/IInputMethodAgent.idl +++ b/frameworks/native/inputmethod_ability/IInputMethodAgent.idl @@ -20,7 +20,7 @@ sequenceable input_method_utils..OHOS.MiscServices.ArrayBuffer; sequenceable input_method_utils..OHOS.MiscServices.ResponseDataInner; interface OHOS.MiscServices.IKeyEventConsumer; interface OHOS.MiscServices.IInputMethodAgent { - [ipccode 0] void DispatchKeyEvent([in] KeyEventValue keyEvent, [in] IKeyEventConsumer consumer); + [ipccode 0] void DispatchKeyEvent([in] KeyEventValue keyEvent, [in] IKeyEventConsumer consumer, [in] unsigned long msgId); void OnCursorUpdate([in] int positionX, [in] int positionY, [in] int height); void OnSelectionChange([in] String text, [in] int oldBegin, [in] int oldEnd, [in] int newBegin, [in] int newEnd); void SetCallingWindow([in] unsigned int windowId); diff --git a/frameworks/native/inputmethod_ability/include/input_method_ability.h b/frameworks/native/inputmethod_ability/include/input_method_ability.h index a94f600ae..c7d5b101a 100644 --- a/frameworks/native/inputmethod_ability/include/input_method_ability.h +++ b/frameworks/native/inputmethod_ability/include/input_method_ability.h @@ -69,7 +69,8 @@ public: int32_t MoveCursor(int32_t keyCode, const AsyncIpcCallBack &callback = nullptr); int32_t SelectByRange(int32_t start, int32_t end, const AsyncIpcCallBack &callback = nullptr); int32_t SelectByMovement(int32_t direction, const AsyncIpcCallBack &callback = nullptr); - int32_t DispatchKeyEvent(const std::shared_ptr &keyEvent, sptr &consumer); + int32_t DispatchKeyEvent(const std::shared_ptr &keyEvent, sptr &consumer, + uint64_t msgId); void SetCallingWindow(uint32_t windowId); int32_t GetEnterKeyType(int32_t &keyType); int32_t GetInputPattern(int32_t &inputPattern); diff --git a/frameworks/native/inputmethod_ability/include/input_method_agent_service_impl.h b/frameworks/native/inputmethod_ability/include/input_method_agent_service_impl.h index 78ae62a44..7cf447fac 100644 --- a/frameworks/native/inputmethod_ability/include/input_method_agent_service_impl.h +++ b/frameworks/native/inputmethod_ability/include/input_method_agent_service_impl.h @@ -30,7 +30,7 @@ public: InputMethodAgentServiceImpl(); ~InputMethodAgentServiceImpl(); ErrCode DispatchKeyEvent( - const MiscServices::KeyEventValue &keyEvent, const sptr &consumer) override; + const MiscServices::KeyEventValue &keyEvent, const sptr &consumer, uint64_t msgId) override; ErrCode OnCursorUpdate(int32_t positionX, int32_t positionY, int height) override; ErrCode OnSelectionChange( const std::string& text, int32_t oldBegin, int32_t oldEnd, int32_t newBegin, int32_t newEnd) override; diff --git a/frameworks/native/inputmethod_ability/src/input_method_ability.cpp b/frameworks/native/inputmethod_ability/src/input_method_ability.cpp index 04a611eaf..ab972db47 100644 --- a/frameworks/native/inputmethod_ability/src/input_method_ability.cpp +++ b/frameworks/native/inputmethod_ability/src/input_method_ability.cpp @@ -379,7 +379,7 @@ void InputMethodAbility::ClearBindInfo(const sptr &channel) } int32_t InputMethodAbility::DispatchKeyEvent( - const std::shared_ptr &keyEvent, sptr &consumer) + const std::shared_ptr &keyEvent, sptr &consumer, uint64_t magId) { if (keyEvent == nullptr) { IMSA_HILOGE("keyEvent is nullptr!"); @@ -391,7 +391,7 @@ int32_t InputMethodAbility::DispatchKeyEvent( } IMSA_HILOGD("InputMethodAbility, start."); - if (!kdListener_->OnDealKeyEvent(keyEvent, consumer)) { + if (!kdListener_->OnDealKeyEvent(keyEvent, consumer, magId)) { IMSA_HILOGE("keyEvent not deal!"); return ErrorCode::ERROR_DISPATCH_KEY_EVENT; } diff --git a/frameworks/native/inputmethod_ability/src/input_method_agent_service_impl.cpp b/frameworks/native/inputmethod_ability/src/input_method_agent_service_impl.cpp index 2410b4b96..7327baf9e 100644 --- a/frameworks/native/inputmethod_ability/src/input_method_agent_service_impl.cpp +++ b/frameworks/native/inputmethod_ability/src/input_method_agent_service_impl.cpp @@ -31,10 +31,10 @@ InputMethodAgentServiceImpl::InputMethodAgentServiceImpl() {} InputMethodAgentServiceImpl::~InputMethodAgentServiceImpl() {} ErrCode InputMethodAgentServiceImpl::DispatchKeyEvent( - const MiscServices::KeyEventValue &keyEvent, const sptr &consumer) + const MiscServices::KeyEventValue &keyEvent, const sptr &consumer, uint64_t msgId) { sptr proxyConsumer = new (std::nothrow) KeyEventConsumerProxy(consumer->AsObject()); - return InputMethodAbility::GetInstance().DispatchKeyEvent(keyEvent.event, proxyConsumer); + return InputMethodAbility::GetInstance().DispatchKeyEvent(keyEvent.event, proxyConsumer, msgId); } ErrCode InputMethodAgentServiceImpl::SetCallingWindow(uint32_t windowId) diff --git a/frameworks/native/inputmethod_controller/IKeyEventConsumer.idl b/frameworks/native/inputmethod_controller/IKeyEventConsumer.idl index f96ffbc3c..aa995da5e 100644 --- a/frameworks/native/inputmethod_controller/IKeyEventConsumer.idl +++ b/frameworks/native/inputmethod_controller/IKeyEventConsumer.idl @@ -14,5 +14,5 @@ */ interface OHOS.MiscServices.IKeyEventConsumer { - [ipccode 0, oneway] void OnKeyEventResult([in] boolean isConsumed); + [ipccode 0] void OnKeyEventResult([in] boolean isConsumed, [in] unsigned long msgId); } \ No newline at end of file diff --git a/frameworks/native/inputmethod_controller/include/keyevent_consumer_service_impl.h b/frameworks/native/inputmethod_controller/include/keyevent_consumer_service_impl.h index 651dfc730..8147c06d8 100644 --- a/frameworks/native/inputmethod_controller/include/keyevent_consumer_service_impl.h +++ b/frameworks/native/inputmethod_controller/include/keyevent_consumer_service_impl.h @@ -28,9 +28,9 @@ class KeyEventConsumerServiceImpl final : public KeyEventConsumerStub, public: using KeyEventCallback = std::function &keyEvent, bool isConsumed)>; - KeyEventConsumerServiceImpl(KeyEventCallback callback, std::shared_ptr keyEvent); + KeyEventConsumerServiceImpl(); ~KeyEventConsumerServiceImpl(); - ErrCode OnKeyEventResult(bool isConsumed) override; + ErrCode OnKeyEventResult(bool isConsumed, uint64_t msgId = 0) override; private: KeyEventCallback callback_; diff --git a/frameworks/native/inputmethod_controller/src/input_method_controller.cpp b/frameworks/native/inputmethod_controller/src/input_method_controller.cpp index f7d5e5362..cbccac5a9 100644 --- a/frameworks/native/inputmethod_controller/src/input_method_controller.cpp +++ b/frameworks/native/inputmethod_controller/src/input_method_controller.cpp @@ -137,6 +137,11 @@ int32_t InputMethodController::Initialize() IMSA_HILOGE("failed to new channel!"); return ErrorCode::ERROR_NULL_POINTER; } + consumer_ = new (std::nothrow) KeyEventConsumerServiceImpl(); + if (consumer_ == nullptr) { + IMSA_HILOGE("failed to get consumer_!"); + return ErrorCode::ERROR_NULL_POINTER; + } InputAttribute attribute; attribute.inputPattern = InputAttribute::PATTERN_TEXT; clientInfo_.attribute = attribute; @@ -977,15 +982,22 @@ int32_t InputMethodController::DispatchKeyEvent(std::shared_ptr k return ErrorCode::ERROR_IME_NOT_STARTED; } IMSA_HILOGD("start."); - sptr consumer = new (std::nothrow) KeyEventConsumerServiceImpl(callback, keyEvent); - if (consumer == nullptr) { + + if (consumer_ == nullptr) { IMSA_HILOGE("consumer is nullptr!"); keyEventQueue_.Pop(); return ErrorCode::ERROR_EX_NULL_POINTER; } KeyEventValue keyEventValue; keyEventValue.event = keyEvent; - auto ret = agent->DispatchKeyEvent(keyEventValue, consumer); + + auto handler = std::make_shared(keyEvent, callback); + auto msgId = GenerateMsgId(); + { + std::lock_guard lock(keyEventHandlerMutex_); + KeyEventHandler_insert({msgId, handler}); + } + auto ret = agent->DispatchKeyEvent(keyEventValue, consumer_, msgId); if (ret != ErrorCode::NO_ERROR) { IMSA_HILOGE("failed to DispatchKeyEvent: %{public}d", ret); } @@ -993,6 +1005,27 @@ int32_t InputMethodController::DispatchKeyEvent(std::shared_ptr k return ret; } +uint64_t InputMethodController::GenerateMsgId() +{ + return msgId_.fetch_add(1, std::memory_order_relaxed); +} + +std::shared_ptr InputMethodController::GetMsgHandler(uint64_t msgId) +{ + std::lock_guard lock(keyEventHandlerMutex_); + auto it = keyEventHandler_.find(msgId); + if (it != keyEventHandler_.end()) { + return it->second; + } + return nullptr; +} + +void InputMethodController::RemoveMsgHandler(uint64_t msgId) +{ + std::lock_guard lock(keyEventHandlerMutex_); + keyEventHandler_.erase(msgId); +} + int32_t InputMethodController::GetEnterKeyType(int32_t &keyType) { IMSA_HILOGD("InputMethodController::GetEnterKeyType start."); diff --git a/frameworks/native/inputmethod_controller/src/keyevent_consumer_service_impl.cpp b/frameworks/native/inputmethod_controller/src/keyevent_consumer_service_impl.cpp index 7d88d9e6b..1950528bb 100644 --- a/frameworks/native/inputmethod_controller/src/keyevent_consumer_service_impl.cpp +++ b/frameworks/native/inputmethod_controller/src/keyevent_consumer_service_impl.cpp @@ -14,23 +14,44 @@ */ #include "keyevent_consumer_service_impl.h" +#include "input_method_controller.h" #include "global.h" namespace OHOS { namespace MiscServices { -KeyEventConsumerServiceImpl::KeyEventConsumerServiceImpl(KeyEventCallback callback, - std::shared_ptr keyEvent) : callback_(callback), keyEvent_(keyEvent) {} +KeyEventConsumerServiceImpl::KeyEventConsumerServiceImpl() {} KeyEventConsumerServiceImpl::~KeyEventConsumerServiceImpl() {} -ErrCode KeyEventConsumerServiceImpl::OnKeyEventResult(bool isConsumed) +ErrCode KeyEventConsumerServiceImpl::OnKeyEventResult(bool isConsumed, uint64_t msgId) { + if (msgId == 0) { + if (callback_ != nullptr) { + callback_(keyEvent_, isConsumed); + } else { + IMSA_HILOGE("callback is nullptr, isConsumed: %{public}d!", isConsumed); + } + return ERR_OK; + } + auto instance = InputMethodController::GetInstance(); + if (instance == nullptr) { + IMSA_HILOGE("failed to get InputMethodController instance!"); + return ErrorCode::ERROR_EX_NULL_POINTER; + } + auto handlers = instance->GetMsgHandler(msgId); + if (handlers == nullptr) { + IMSA_HILOGE("failed to get InputMethodController instance!"); + return ERR_OK; + } + callback_ = handlers->callback; + keyEvent_ = handlers->keyEvent; if (callback_ != nullptr) { callback_(keyEvent_, isConsumed); } else { IMSA_HILOGE("callback is nullptr, isConsumed: %{public}d!", isConsumed); } + instance->RemoveMsgHandler(msgId); return ERR_OK; } } // namespace MiscServices diff --git a/interfaces/inner_api/inputmethod_ability/include/keyboard_listener.h b/interfaces/inner_api/inputmethod_ability/include/keyboard_listener.h index a5d0e713e..89af5ed0b 100644 --- a/interfaces/inner_api/inputmethod_ability/include/keyboard_listener.h +++ b/interfaces/inner_api/inputmethod_ability/include/keyboard_listener.h @@ -26,6 +26,11 @@ public: virtual ~KeyboardListener() = default; virtual bool OnDealKeyEvent(const std::shared_ptr &keyEvent, sptr &consumer) = 0; + virtual bool OnDealKeyEvent(const std::shared_ptr &keyEvent, + sptr &consumer, uint64_t msgId) + { + return true; + }; virtual bool OnKeyEvent(int32_t keyCode, int32_t keyStatus, sptr &consumer) = 0; virtual bool OnKeyEvent(const std::shared_ptr &keyEvent, sptr &consumer) = 0; virtual void OnCursorUpdate(int32_t positionX, int32_t positionY, int32_t height) = 0; diff --git a/interfaces/inner_api/inputmethod_controller/include/input_method_controller.h b/interfaces/inner_api/inputmethod_controller/include/input_method_controller.h index cfa43f55e..7be3e80d8 100644 --- a/interfaces/inner_api/inputmethod_controller/include/input_method_controller.h +++ b/interfaces/inner_api/inputmethod_controller/include/input_method_controller.h @@ -47,6 +47,18 @@ namespace OHOS { namespace MiscServices { + +using PrivateDataValue = std::variant; +using KeyEventCallback = std::function &keyEvent, bool isConsumed)>; +using WindowScaleCallback = std::function; + +struct HandKeyEvent { + std::shared_ptr keyEvent; + KeyEventCallback callback; + HandKeyEvent(std::shared_ptr keyEvent, KeyEventCallback callback) + : keyEvent(keyEvent), callback(callback) {} +}; + class OnTextChangedListener : public virtual RefBase { public: virtual ~OnTextChangedListener() {} @@ -143,9 +155,7 @@ private: void FinishTextPreviewV2(); void OnDetachV2(); }; -using PrivateDataValue = std::variant; -using KeyEventCallback = std::function &keyEvent, bool isConsumed)>; -using WindowScaleCallback = std::function; + class InputMethodController : public RefBase, public PrivateCommandInterface { public: /** @@ -1020,8 +1030,12 @@ private: int32_t ResponseDataChannel( const sptr &agentObject, uint64_t msgId, int32_t code, const ResponseData &data); void CalibrateImmersiveParam(InputAttribute &inputAttribute); + uint64_t GenerateMsgId(); + std::shared_ptr GetMsgHandler(uint64_t msgId); + void RemoveMsgHandler(uint64_t msgId); friend class InputDataChannelServiceImpl; + friend class KeyEventConsumerServiceImpl; std::shared_ptr controllerListener_; std::mutex abilityLock_; sptr abilityManager_ = nullptr; @@ -1084,6 +1098,11 @@ private: std::mutex windowScaleCallbackMutex_; WindowScaleCallback windowScaleCallback_ = nullptr; + + std::atomic msgId_ { 1 }; + std::map> keyEventHandler_; + sptr consumer_ { nullptr }; + static std::mutex keyEventHandlerMutex_; }; } // namespace MiscServices } // namespace OHOS diff --git a/test/common/include/keyboard_listener_test_impl.h b/test/common/include/keyboard_listener_test_impl.h index 5538aa352..565387fb2 100644 --- a/test/common/include/keyboard_listener_test_impl.h +++ b/test/common/include/keyboard_listener_test_impl.h @@ -33,6 +33,8 @@ public: return false; } bool OnDealKeyEvent(const std::shared_ptr &keyEvent, sptr &consumer) override; + bool OnDealKeyEvent(const std::shared_ptr &keyEvent, sptr &consumer, + uint64_t msgId) override; void OnCursorUpdate(int32_t positionX, int32_t positionY, int32_t height) override; void OnSelectionChange(int32_t oldBegin, int32_t oldEnd, int32_t newBegin, int32_t newEnd) override; void OnTextChange(const std::string &text) override; diff --git a/test/common/src/keyboard_listener_test_impl.cpp b/test/common/src/keyboard_listener_test_impl.cpp index cfcff1c60..8fced91fb 100644 --- a/test/common/src/keyboard_listener_test_impl.cpp +++ b/test/common/src/keyboard_listener_test_impl.cpp @@ -34,11 +34,16 @@ bool KeyboardListenerTestImpl::OnKeyEvent(int32_t keyCode, int32_t keyStatus, sp bool KeyboardListenerTestImpl::OnDealKeyEvent( const std::shared_ptr &keyEvent, sptr &consumer) +{ + return true; +} +bool KeyboardListenerTestImpl::OnDealKeyEvent( + const std::shared_ptr &keyEvent, sptr &consumer, uint64_t msgId) { bool isKeyCodeConsume = OnKeyEvent(keyEvent->GetKeyCode(), keyEvent->GetKeyAction(), consumer); bool isKeyEventConsume = OnKeyEvent(keyEvent, consumer); if (consumer != nullptr) { - consumer->OnKeyEventResult(isKeyEventConsume || isKeyCodeConsume); + consumer->OnKeyEventResult(isKeyEventConsume || isKeyCodeConsume, 0); } return true; } diff --git a/test/fuzztest/inputmethodability_fuzzer/inputmethodability_fuzzer.cpp b/test/fuzztest/inputmethodability_fuzzer/inputmethodability_fuzzer.cpp index 11886674c..97e697d99 100644 --- a/test/fuzztest/inputmethodability_fuzzer/inputmethodability_fuzzer.cpp +++ b/test/fuzztest/inputmethodability_fuzzer/inputmethodability_fuzzer.cpp @@ -42,6 +42,11 @@ class KeyboardListenerImpl : public KeyboardListener { { return true; } + bool OnDealKeyEvent(const std::shared_ptr &keyEvent, sptr &consumer, + uint64_t msgId) + { + return true; + } void OnCursorUpdate(int32_t positionX, int32_t positionY, int32_t height) { } void OnSelectionChange(int32_t oldBegin, int32_t oldEnd, int32_t newBegin, int32_t newEnd) { } void OnTextChange(const std::string &text) { } @@ -128,7 +133,7 @@ void TestDispatchKeyEvent(int32_t fuzzedInt32) keyEvent->SetKeyCode(fuzzedInt32); keyEvent->SetKeyAction(fuzzedInt32); sptr consumer = new (std::nothrow) KeyEventConsumerProxy(nullptr); - InputMethodAbility::GetInstance().DispatchKeyEvent(keyEvent, consumer); + InputMethodAbility::GetInstance().DispatchKeyEvent(keyEvent, consumer, 0); } void TestSetCallingWindow(int32_t fuzzedInt32) diff --git a/test/fuzztest/keyeventconsumer_fuzzer/keyeventconsumer_fuzzer.cpp b/test/fuzztest/keyeventconsumer_fuzzer/keyeventconsumer_fuzzer.cpp index 2b9131370..57804e41d 100644 --- a/test/fuzztest/keyeventconsumer_fuzzer/keyeventconsumer_fuzzer.cpp +++ b/test/fuzztest/keyeventconsumer_fuzzer/keyeventconsumer_fuzzer.cpp @@ -49,11 +49,8 @@ bool FuzzKeyEventConsumerStub(const uint8_t *rawData, size_t size) MessageParcel reply; MessageOption option; - std::shared_ptr keyEvent = MMI::KeyEvent::Create(); - - sptr stub = new KeyEventConsumerServiceImpl( - [](std::shared_ptr &keyEvent, bool isConsumed) {}, keyEvent); - stub->OnKeyEventResult(isConsumed); + sptr stub = new KeyEventConsumerServiceImpl(); + stub->OnKeyEventResult(isConsumed, 0); stub->OnRemoteRequest(code, data, reply, option); return true; diff --git a/test/unittest/cpp_test/src/input_method_ability_exception_test.cpp b/test/unittest/cpp_test/src/input_method_ability_exception_test.cpp index 8e69bdf4c..bf15fd7bd 100644 --- a/test/unittest/cpp_test/src/input_method_ability_exception_test.cpp +++ b/test/unittest/cpp_test/src/input_method_ability_exception_test.cpp @@ -230,12 +230,12 @@ HWTEST_F(InputMethodAbilityExceptionTest, testDispatchKeyEventException, TestSiz // keyEvent == nullptr; std::shared_ptr keyEvent = nullptr; sptr consumer = new (std::nothrow) KeyEventConsumerProxy(nullptr); - auto ret = inputMethodAbility_.DispatchKeyEvent(keyEvent, consumer); + auto ret = inputMethodAbility_.DispatchKeyEvent(keyEvent, consumer, 0); EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NULL_POINTER); // kdListener_ == nullptr keyEvent = KeyEventUtil::CreateKeyEvent(MMI::KeyEvent::KEYCODE_A, MMI::KeyEvent::KEY_ACTION_DOWN); - ret = inputMethodAbility_.DispatchKeyEvent(keyEvent, consumer); + ret = inputMethodAbility_.DispatchKeyEvent(keyEvent, consumer, 0); EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NULL_POINTER); } diff --git a/test/unittest/cpp_test/src/input_method_controller_test.cpp b/test/unittest/cpp_test/src/input_method_controller_test.cpp index ec1ed4cfc..dea555aca 100644 --- a/test/unittest/cpp_test/src/input_method_controller_test.cpp +++ b/test/unittest/cpp_test/src/input_method_controller_test.cpp @@ -203,12 +203,18 @@ public: } bool OnDealKeyEvent( const std::shared_ptr &keyEvent, sptr &consumer) override + { + return true; + } + bool OnDealKeyEvent( + const std::shared_ptr &keyEvent, sptr &consumer, + uint64_t msgId) override { IMSA_HILOGI("KeyboardListenerImpl run in"); bool isKeyCodeConsume = OnKeyEvent(keyEvent->GetKeyCode(), keyEvent->GetKeyAction(), consumer); bool isKeyEventConsume = OnKeyEvent(keyEvent, consumer); if (consumer != nullptr) { - consumer->OnKeyEventResult(isKeyEventConsume | isKeyCodeConsume); + consumer->OnKeyEventResult(isKeyEventConsume | isKeyCodeConsume, 0); } return true; } diff --git a/test/unittest/cpp_test/src/input_method_editor_test.cpp b/test/unittest/cpp_test/src/input_method_editor_test.cpp index ac118722c..df762c7c3 100644 --- a/test/unittest/cpp_test/src/input_method_editor_test.cpp +++ b/test/unittest/cpp_test/src/input_method_editor_test.cpp @@ -59,6 +59,8 @@ public: static int32_t keyStatus_; static CursorInfo cursorInfo_; bool OnDealKeyEvent(const std::shared_ptr &keyEvent, sptr &consumer) override; + bool OnDealKeyEvent(const std::shared_ptr &keyEvent, sptr &consumer, + uint64_t msgId) override; bool OnKeyEvent(int32_t keyCode, int32_t keyStatus, sptr &consumer) override; bool OnKeyEvent(const std::shared_ptr &keyEvent, sptr &consumer) override; void OnCursorUpdate(int32_t positionX, int32_t positionY, int32_t height) override; @@ -83,11 +85,16 @@ bool KeyboardListenerImpl::OnKeyEvent( } bool KeyboardListenerImpl::OnDealKeyEvent( const std::shared_ptr &keyEvent, sptr &consumer) +{ + return true; +} +bool KeyboardListenerImpl::OnDealKeyEvent( + const std::shared_ptr &keyEvent, sptr &consumer, uint64_t msgId) { bool isKeyCodeConsume = OnKeyEvent(keyEvent->GetKeyCode(), keyEvent->GetKeyAction(), consumer); bool isKeyEventConsume = OnKeyEvent(keyEvent, consumer); if (consumer != nullptr) { - consumer->OnKeyEventResult(isKeyEventConsume | isKeyCodeConsume); + consumer->OnKeyEventResult(isKeyEventConsume | isKeyCodeConsume, 0); } return true; } -- Gitee