From f19c12fa5f132286058867803845da7e06e8b63f Mon Sep 17 00:00:00 2001 From: Hollokin Date: Wed, 5 Jul 2023 22:35:12 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E3=80=90=E9=9C=80=E6=B1=82=E3=80=91?= =?UTF-8?q?=E5=8E=9F=E5=AD=90=E5=8C=96=E7=BB=91=E5=AE=9A=E7=BC=96=E8=BE=91?= =?UTF-8?q?=E6=A1=86=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Hollokin --- .../js_get_input_method_controller.cpp | 75 +- .../js_get_input_method_controller.h | 3 + .../js/napi/inputmethodclient/js_utils.cpp | 6 - .../js/napi/inputmethodclient/js_utils.h | 1 - .../include/input_method_ability.h | 2 + .../src/input_method_ability.cpp | 52 +- .../include/i_input_data_channel.h | 2 + .../include/input_data_channel_proxy.h | 1 + .../include/input_data_channel_stub.h | 1 + .../include/input_method_utils.h | 29 +- .../include/itypes_util.h | 6 + .../src/input_data_channel_proxy.cpp | 8 + .../src/input_data_channel_stub.cpp | 14 + .../src/input_method_controller.cpp | 54 +- .../src/itypes_util.cpp | 59 +- .../include/input_method_controller.h | 31 + test/unittest/BUILD.gn | 1 + test/unittest/cpp_test/BUILD.gn | 31 + .../unittest/cpp_test/common/src/tdd_util.cpp | 19 +- .../src/input_method_ability_test.cpp | 21 + .../cpp_test/src/input_method_attach_test.cpp | 869 ++++++++++++++++++ 21 files changed, 1235 insertions(+), 50 deletions(-) create mode 100644 test/unittest/cpp_test/src/input_method_attach_test.cpp diff --git a/frameworks/js/napi/inputmethodclient/js_get_input_method_controller.cpp b/frameworks/js/napi/inputmethodclient/js_get_input_method_controller.cpp index dfbcca1a..e0db965a 100644 --- a/frameworks/js/napi/inputmethodclient/js_get_input_method_controller.cpp +++ b/frameworks/js/napi/inputmethodclient/js_get_input_method_controller.cpp @@ -399,6 +399,58 @@ napi_value JsGetInputMethodController::CreateSelectMovement(napi_env env, int32_ return movement; } +bool JsGetInputMethodController::GetValue(napi_env env, napi_value in, SelectionRange &out) +{ + auto ret = JsUtil::Object::ReadProperty(env, in, "start", out.start); + return ret && JsUtil::Object::ReadProperty(env, in, "end", out.end); +} + +/** + * let textConfig: TextConfig = { + * inputAttribute: InputAttribute = { + * textInputType: TextInputType = TextInputType.TEXT, + * enterKeyType: EnterKeyType = EnterKeyType.NONE + * }, + * cursorInfo?: CursorInfo = { + * left: number, + * top: number, + * width: number, + * height: number, + * }, + * selection?: Range = { + * start: number, + * end: number + * }, + * windowId?: number + * } + */ +napi_status JsGetInputMethodController::GetValue(napi_env env, napi_value in, TextConfig &out) +{ + napi_value attributeResult = nullptr; + napi_status status = JsUtils::GetValue(env, in, "inputAttribute", attributeResult); + CHECK_RETURN(status == napi_ok, "get inputAttribute", status); + bool ret = JsGetInputMethodController::GetValue(env, attributeResult, out.inputAttribute); + CHECK_RETURN(ret, "get inputAttribute of TextConfig", napi_generic_failure); + + napi_value cursorInfoResult = nullptr; + status = JsUtils::GetValue(env, in, "cursorInfo", cursorInfoResult); + if (status == napi_ok) { + ret = JsGetInputMethodController::GetValue(env, cursorInfoResult, out.cursorInfo); + CHECK_RETURN(ret, "get cursorInfo of TextConfig", napi_generic_failure); + } + + napi_value rangeResult = nullptr; + status = JsUtils::GetValue(env, in, "selection", rangeResult); + if (status == napi_ok) { + ret = JsGetInputMethodController::GetValue(env, rangeResult, out.range); + CHECK_RETURN(ret, "get cursorInfo of TextConfig", napi_generic_failure); + } + + ret = JsUtil::Object::ReadProperty(env, in, "windowId", out.windowId); + CHECK_RETURN(ret, "get windowId of TextConfig", napi_generic_failure); + return napi_ok; +} + napi_value JsGetInputMethodController::HandleSoftKeyboard( napi_env env, napi_callback_info info, std::function callback, bool isOutput, bool needThrowException) { @@ -443,26 +495,7 @@ napi_status JsGetInputMethodController::ParseAttachInput( } // 1 means the second parameter: textConfig - napi_value attributeResult = nullptr; - status = JsUtils::GetValue(env, argv[1], "inputAttribute", attributeResult); - if (status != napi_ok) { - return status; - } - napi_value textResult = nullptr; - status = JsUtils::GetValue(env, attributeResult, "textInputType", textResult); - if (status != napi_ok) { - return status; - } - status = JsUtils::GetValue(env, textResult, ctxt->attribute.inputPattern); - if (status != napi_ok) { - return status; - } - napi_value enterResult = nullptr; - status = JsUtils::GetValue(env, attributeResult, "enterKeyType", enterResult); - if (status != napi_ok) { - return status; - } - return JsUtils::GetValue(env, enterResult, ctxt->attribute.enterKeyType); + return JsGetInputMethodController::GetValue(env, argv[1], ctxt->textConfig); } napi_value JsGetInputMethodController::Attach(napi_env env, napi_callback_info info) @@ -477,7 +510,7 @@ napi_value JsGetInputMethodController::Attach(napi_env env, napi_callback_info i auto exec = [ctxt, env](AsyncCall::Context *ctx) { ctxt->textListener = JsGetInputMethodTextChangedListener::GetInstance(); auto status = - InputMethodController::GetInstance()->Attach(ctxt->textListener, ctxt->showKeyboard, ctxt->attribute); + InputMethodController::GetInstance()->Attach(ctxt->textListener, ctxt->showKeyboard, ctxt->textConfig); ctxt->SetErrorCode(status); CHECK_RETURN_VOID(status == ErrorCode::NO_ERROR, "attach return error!"); ctxt->SetState(napi_ok); diff --git a/frameworks/js/napi/inputmethodclient/js_get_input_method_controller.h b/frameworks/js/napi/inputmethodclient/js_get_input_method_controller.h index 0048436e..b48fbe44 100644 --- a/frameworks/js/napi/inputmethodclient/js_get_input_method_controller.h +++ b/frameworks/js/napi/inputmethodclient/js_get_input_method_controller.h @@ -48,6 +48,7 @@ struct AttachContext : public AsyncCall::Context { sptr textListener; InputAttribute attribute; bool showKeyboard = false; + TextConfig textConfig; AttachContext() : Context(nullptr, nullptr) {}; AttachContext(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)) {}; @@ -195,6 +196,8 @@ private: napi_env env, size_t argc, napi_value *argv, const std::shared_ptr &ctxt); static bool GetValue(napi_env env, napi_value in, CursorInfo &out); static bool GetValue(napi_env env, napi_value in, InputAttribute &out); + static napi_status GetValue(napi_env env, napi_value in, TextConfig &out); + static bool GetValue(napi_env env, napi_value in, SelectionRange &out); static napi_value GetJsKeyboardStatusProperty(napi_env env); static napi_value GetJsEnterKeyTypeProperty(napi_env env); static napi_value GetJsTextInputTypeProperty(napi_env env); diff --git a/frameworks/js/napi/inputmethodclient/js_utils.cpp b/frameworks/js/napi/inputmethodclient/js_utils.cpp index 43de9b76..6db0ac3e 100644 --- a/frameworks/js/napi/inputmethodclient/js_utils.cpp +++ b/frameworks/js/napi/inputmethodclient/js_utils.cpp @@ -318,12 +318,6 @@ napi_status JsUtils::GetValue(napi_env env, napi_value in, PanelInfo &out) return status; } -bool JsUtils::GetValue(napi_env env, napi_value in, SelectionRange &out) -{ - auto ret = JsUtil::Object::ReadProperty(env, in, "start", out.start); - return ret && JsUtil::Object::ReadProperty(env, in, "end", out.end); -} - napi_value JsUtils::GetValue(napi_env env, const std::vector &in) { napi_value array = nullptr; diff --git a/frameworks/js/napi/inputmethodclient/js_utils.h b/frameworks/js/napi/inputmethodclient/js_utils.h index a5764dbb..91006aa7 100644 --- a/frameworks/js/napi/inputmethodclient/js_utils.h +++ b/frameworks/js/napi/inputmethodclient/js_utils.h @@ -110,7 +110,6 @@ public: static napi_status GetValue(napi_env env, napi_value in, std::string &out); static napi_status GetValue(napi_env env, napi_value in, const std::string &type, napi_value &out); static napi_status GetValue(napi_env env, napi_value in, PanelInfo &out); - static bool GetValue(napi_env env, napi_value in, SelectionRange &out); static napi_value GetValue(napi_env env, const std::vector &in); static napi_value GetValue(napi_env env, const InputWindowInfo &in); static napi_value GetValue(napi_env env, const InputAttribute &attribute); diff --git a/frameworks/native/inputmethod_ability/include/input_method_ability.h b/frameworks/native/inputmethod_ability/include/input_method_ability.h index 833cdcce..bf64d933 100644 --- a/frameworks/native/inputmethod_ability/include/input_method_ability.h +++ b/frameworks/native/inputmethod_ability/include/input_method_ability.h @@ -68,6 +68,7 @@ public: int32_t GetEnterKeyType(int32_t &keyType); int32_t GetInputPattern(int32_t &inputPattern); int32_t GetTextIndexAtCursor(int32_t &index); + int32_t GetTextConfig(TextTotalConfig &textConfig); void OnImeReady(); int32_t CreatePanel(const std::shared_ptr &context, const PanelInfo &panelInfo, std::shared_ptr &inputMethodPanel); @@ -122,6 +123,7 @@ private: void OnConfigurationChange(Message *msg); void ShowInputWindow(bool isShowKeyboard); void DismissInputWindow(); + void OnTextConfigChange(const TextTotalConfig &textConfig); bool isImeReady_{ false }; InputStartNotifier notifier_; ConcurrentMap> panels_{}; diff --git a/frameworks/native/inputmethod_ability/src/input_method_ability.cpp b/frameworks/native/inputmethod_ability/src/input_method_ability.cpp index f3e73a61..669973ac 100644 --- a/frameworks/native/inputmethod_ability/src/input_method_ability.cpp +++ b/frameworks/native/inputmethod_ability/src/input_method_ability.cpp @@ -243,10 +243,18 @@ void InputMethodAbility::OnShowKeyboard(Message *msg) return; } if (channelObject == nullptr) { - IMSA_HILOGI("InputMethodAbility::OnShowKeyboard channelObject is nullptr"); + IMSA_HILOGE("InputMethodAbility::OnShowKeyboard channelObject is nullptr"); return; } SetInputDataChannel(channelObject); + TextTotalConfig textConfig = {}; + int32_t ret = GetTextConfig(textConfig); + if (ret != ErrorCode::NO_ERROR) { + IMSA_HILOGE("InputMethodAbility, get text config failed, ret is %{public}d", ret); + return; + } + // todo textConfigÐèÒªÅпÕÂ𣿣¿ + OnTextConfigChange(textConfig); ShowInputWindow(isShowKeyboard); } @@ -407,6 +415,37 @@ void InputMethodAbility::ShowInputWindow(bool isShowKeyboard) } } +void InputMethodAbility::OnTextConfigChange(const TextTotalConfig &textConfig) +{ + IMSA_HILOGI("InputMethodAbility run in."); + // todo ²»ÂÛÊDz»ÊDZ仯£¬¶¼»á֪ͨ¹ýÀ´£¬ÕâÀïÐè²»ÐèÒª×öÀ¹½Ø£¿£¿ + if (kdListener_ != nullptr) { + kdListener_->OnEditorAttributeChange(textConfig.inputAttribute); + if (textConfig.cursorInfo.left != -1.0) { + kdListener_->OnCursorUpdate( + textConfig.cursorInfo.left, textConfig.cursorInfo.top, textConfig.cursorInfo.height); + } + // todo ¹â±êµÄ¸üÐÂÐèÒª´Ócontrolelr¸üйýÀ´£¬´«µÝµ½ability²à£¬´ýʵÏÖ¡£ + if (textConfig.textSelection.newBegin != -1) { + kdListener_->OnSelectionChange(textConfig.textSelection.oldBegin, textConfig.textSelection.oldEnd, + textConfig.textSelection.newBegin, textConfig.textSelection.newEnd); + } + } + if (textConfig.windowId == INVALID_WINDOW_ID) { + return; + } + panels_.ForEach([&textConfig](const PanelType &panelType, const std::shared_ptr &panel) { + panel->SetCallingWindow(textConfig.windowId); + return false; + }); + if (imeListener_ == nullptr) { + IMSA_HILOGE("imeListener_ is nullptr, do not need to send callback of setCallingWindow."); + return; + } + imeListener_->OnSetCallingWindow(textConfig.windowId); + IMSA_HILOGD("setCallingWindow end."); +} + void InputMethodAbility::DismissInputWindow() { IMSA_HILOGI("InputMethodAbility::DismissInputWindow"); @@ -585,6 +624,17 @@ int32_t InputMethodAbility::GetTextIndexAtCursor(int32_t &index) return channel->GetTextIndexAtCursor(index); } +int32_t InputMethodAbility::GetTextConfig(TextTotalConfig &textConfig) +{ + IMSA_HILOGD("InputMethodAbility, run in."); + auto channel = GetInputDataChannelProxy(); + if (channel == nullptr) { + IMSA_HILOGE("InputMethodAbility::channel is nullptr"); + return ErrorCode::ERROR_CLIENT_NULL_POINTER; + } + return channel->GetTextConfig(textConfig); +} + void InputMethodAbility::SetInputDataChannel(sptr &object) { IMSA_HILOGD("run in SetInputDataChannel"); diff --git a/frameworks/native/inputmethod_controller/include/i_input_data_channel.h b/frameworks/native/inputmethod_controller/include/i_input_data_channel.h index 193c3e6c..d7eff6e9 100644 --- a/frameworks/native/inputmethod_controller/include/i_input_data_channel.h +++ b/frameworks/native/inputmethod_controller/include/i_input_data_channel.h @@ -44,6 +44,7 @@ public: SELECT_BY_MOVEMENT, HANDLE_EXTEND_ACTION, GET_TEXT_INDEX_AT_CURSOR, + GET_TEXT_CONFIG, }; DECLARE_INTERFACE_DESCRIPTOR(u"ohos.miscservices.inputmethod.IInputDataChannel"); @@ -53,6 +54,7 @@ public: virtual int32_t DeleteBackward(int32_t length) = 0; virtual int32_t GetTextBeforeCursor(int32_t number, std::u16string &text) = 0; virtual int32_t GetTextAfterCursor(int32_t number, std::u16string &text) = 0; + virtual int32_t GetTextConfig(TextTotalConfig &textConfig) = 0; virtual void SendKeyboardStatus(int32_t status) = 0; virtual int32_t SendFunctionKey(int32_t funcKey) = 0; virtual int32_t MoveCursor(int32_t keyCode) = 0; diff --git a/frameworks/native/inputmethod_controller/include/input_data_channel_proxy.h b/frameworks/native/inputmethod_controller/include/input_data_channel_proxy.h index 2e634d98..457646f2 100644 --- a/frameworks/native/inputmethod_controller/include/input_data_channel_proxy.h +++ b/frameworks/native/inputmethod_controller/include/input_data_channel_proxy.h @@ -49,6 +49,7 @@ public: int32_t SelectByMovement(int32_t direction, int32_t cursorMoveSkip) override; int32_t HandleExtendAction(int32_t action) override; int32_t GetTextIndexAtCursor(int32_t &index) override; + int32_t GetTextConfig(TextTotalConfig &textConfig) override; void NotifyGetOperationCompletion() override; private: diff --git a/frameworks/native/inputmethod_controller/include/input_data_channel_stub.h b/frameworks/native/inputmethod_controller/include/input_data_channel_stub.h index 6addfe2f..5a18ac2c 100644 --- a/frameworks/native/inputmethod_controller/include/input_data_channel_stub.h +++ b/frameworks/native/inputmethod_controller/include/input_data_channel_stub.h @@ -53,6 +53,7 @@ public: int32_t SelectByRange(int32_t start, int32_t end) override; int32_t SelectByMovement(int32_t direction, int32_t cursorMoveSkip) override; int32_t HandleExtendAction(int32_t action) override; + int32_t GetTextConfig(TextTotalConfig &textConfig) override; void NotifyGetOperationCompletion() override; int32_t HandleGetOperation(int32_t number, std::u16string &text, int32_t &index, int32_t msgType); diff --git a/frameworks/native/inputmethod_controller/include/input_method_utils.h b/frameworks/native/inputmethod_controller/include/input_method_utils.h index 61b17e18..7ed58ef9 100644 --- a/frameworks/native/inputmethod_controller/include/input_method_utils.h +++ b/frameworks/native/inputmethod_controller/include/input_method_utils.h @@ -22,6 +22,8 @@ namespace OHOS { namespace MiscServices { +constexpr uint32_t INIT_WINDOW_ID = 0; +constexpr uint32_t INVALID_WINDOW_ID = INIT_WINDOW_ID - 1; enum class EnterKeyType { UNSPECIFIED = 0, NONE, GO, SEARCH, SEND, NEXT, DONE, PREVIOUS }; enum class TextInputType { @@ -79,10 +81,10 @@ private: }; struct CursorInfo { - double left = 0.0; - double top = 0.0; - double width = 0.0; - double height = 0.0; + double left = -1.0; + double top = -1.0; + double width = -1.0; + double height = -1.0; bool operator==(const CursorInfo &info) const { return (left == info.left && top == info.top && width == info.width && height == info.height); @@ -121,6 +123,25 @@ struct TextSelection { int32_t newBegin = -1; int32_t newEnd = -1; }; + +class TextTotalConfig { +public: + InputAttribute inputAttribute = {}; + CursorInfo cursorInfo = {}; + TextSelection textSelection = {}; + uint32_t windowId = INVALID_WINDOW_ID; +}; + +struct TextConfig { + // todo + InputAttribute inputAttribute = {}; + + // + CursorInfo cursorInfo = {}; + SelectionRange range = {}; + // todo + uint32_t windowId = INVALID_WINDOW_ID; +}; } // namespace MiscServices } // namespace OHOS #endif // FRAMEWORKS_INPUTMETHOD_CONTROLLER_INCLUDE_INPUT_METHOD_UTILS_H diff --git a/frameworks/native/inputmethod_controller/include/itypes_util.h b/frameworks/native/inputmethod_controller/include/itypes_util.h index 710105ce..16c0cc41 100644 --- a/frameworks/native/inputmethod_controller/include/itypes_util.h +++ b/frameworks/native/inputmethod_controller/include/itypes_util.h @@ -45,6 +45,9 @@ public: static bool Marshalling(uint64_t input, MessageParcel &data); static bool Unmarshalling(uint64_t &output, MessageParcel &data); + static bool Marshalling(double input, MessageParcel &data); + static bool Unmarshalling(double &output, MessageParcel &data); + static bool Marshalling(const std::u16string &input, MessageParcel &data); static bool Unmarshalling(std::u16string &output, MessageParcel &data); @@ -72,6 +75,9 @@ public: static bool Marshalling(const InputWindowInfo &input, MessageParcel &data); static bool Unmarshalling(InputWindowInfo &output, MessageParcel &data); + static bool Marshalling(const TextTotalConfig &input, MessageParcel &data); + static bool Unmarshalling(TextTotalConfig &output, MessageParcel &data); + static bool Marshalling(EventType input, MessageParcel &data); static bool Unmarshalling(EventType &output, MessageParcel &data); diff --git a/frameworks/native/inputmethod_controller/src/input_data_channel_proxy.cpp b/frameworks/native/inputmethod_controller/src/input_data_channel_proxy.cpp index 7749a8e8..d7bb0cfe 100644 --- a/frameworks/native/inputmethod_controller/src/input_data_channel_proxy.cpp +++ b/frameworks/native/inputmethod_controller/src/input_data_channel_proxy.cpp @@ -111,6 +111,14 @@ int32_t InputDataChannelProxy::GetTextIndexAtCursor(int32_t &index) [&index](MessageParcel &parcel) { return ITypesUtil::Unmarshal(parcel, index);}); } +int32_t InputDataChannelProxy::GetTextConfig(TextTotalConfig &textConfig) +{ + IMSA_HILOGD("InputDataChannelProxy run in"); + return SendRequest(GET_TEXT_CONFIG, nullptr, [&textConfig](MessageParcel &parcel) { + return ITypesUtil::Unmarshal(parcel, textConfig); + }); +} + int32_t InputDataChannelProxy::SelectByRange(int32_t start, int32_t end) { IMSA_HILOGD("InputDataChannelProxy run in"); diff --git a/frameworks/native/inputmethod_controller/src/input_data_channel_stub.cpp b/frameworks/native/inputmethod_controller/src/input_data_channel_stub.cpp index 6d0bf4fe..41fe1331 100644 --- a/frameworks/native/inputmethod_controller/src/input_data_channel_stub.cpp +++ b/frameworks/native/inputmethod_controller/src/input_data_channel_stub.cpp @@ -112,6 +112,14 @@ int32_t InputDataChannelStub::OnRemoteRequest( SelectByMovementOnRemote(data, reply); break; } + case GET_TEXT_CONFIG: { + IMSA_HILOGD("tyx::GetTextConfig in."); + TextTotalConfig textConfig = {}; + reply.WriteInt32(GetTextConfig(textConfig)); + ITypesUtil::Marshal(reply, textConfig); + // todo ÐòÁл¯µÄ˳ÐòºÍ·´ÐòÁл¯µÄ˳ÐòÒªÒ»Ö¡£ + break; + } default: return IPCObjectStub::OnRemoteRequest(code, data, reply, option); } @@ -234,6 +242,12 @@ int32_t InputDataChannelStub::GetInputPattern(int32_t &inputPattern) return InputMethodController::GetInstance()->GetInputPattern(inputPattern); } +int32_t InputDataChannelStub::GetTextConfig(TextTotalConfig &textConfig) +{ + IMSA_HILOGI("InputDataChannelStub run in."); + return InputMethodController::GetInstance()->GetTextConfig(textConfig); +} + int32_t InputDataChannelStub::HandleGetOperation(int32_t number, std::u16string &text, int32_t &index, int32_t msgType) { IMSA_HILOGI("InputDataChannelStub::start, msgId: %{public}d, number: %{public}d", msgType, number); diff --git a/frameworks/native/inputmethod_controller/src/input_method_controller.cpp b/frameworks/native/inputmethod_controller/src/input_method_controller.cpp index 1ee2b3c7..35f938d8 100644 --- a/frameworks/native/inputmethod_controller/src/input_method_controller.cpp +++ b/frameworks/native/inputmethod_controller/src/input_method_controller.cpp @@ -381,6 +381,24 @@ void InputMethodController::OnPanelStatusChange( settingListener_->OnPanelStatusChange(status, windowInfo); } +void InputMethodController::SaveTextConfig(const TextConfig &textConfig) +{ + // todo ²»ÄÜÖ±½Ó¸²¸Ç£¬¿ÉÑ¡²ÎÊýÍòÒ»È˼ÒÖ»ÊÇûÓд«È룬²»Äܸ²¸ÇΪ¿ÕµÄ¡£ + std::lock_guard lock(editorContentLock_); + textConfig_ = textConfig; + +// textConfig_.inputAttribute = textConfig.inputAttribute; +// +// // todo ÆäËûµÄΪ¿ÉÑ¡²ÎÊý£¬ÔõôÇø·Ö£¿ +// textConfig_.textSelection.oldBegin = textConfig_.textSelection.newBegin; +// textConfig_.textSelection.oldEnd = textConfig_.textSelection.newEnd; +// textConfig_.textSelection.newBegin = textConfig.range.start; +// textConfig_.textSelection.newEnd = textConfig.range.end; +// +// textConfig_.cursorInfo = textConfig.cursorInfo; +// textConfig_.windowId = textConfig.windowId; +} + int32_t InputMethodController::Attach(sptr &listener) { return Attach(listener, true); @@ -398,6 +416,15 @@ int32_t InputMethodController::Attach( { IMSA_HILOGI("InputMethodController::Attach isShowKeyboard %{public}s", isShowKeyboard ? "true" : "false"); InputmethodTrace tracer("InputMethodController Attach trace."); + textConfig_.inputAttribute = attribute; + return Attach(listener, isShowKeyboard, textConfig_); +} + +int32_t InputMethodController::Attach( + sptr &listener, bool isShowKeyboard, const TextConfig &textConfig) +{ + IMSA_HILOGI("isShowKeyboard %{public}s", isShowKeyboard ? "true" : "false"); + InputmethodTrace tracer("InputMethodController Attach with textConfig trace."); { std::unique_lock numLock(textFieldReplyCountLock_); textFieldReplyCount_ = 0; @@ -407,7 +434,7 @@ int32_t InputMethodController::Attach( textListener_ = listener; } clientInfo_.isShowKeyboard = isShowKeyboard; - clientInfo_.attribute = attribute; + SaveTextConfig(textConfig); int32_t ret = PrepareInput(clientInfo_); if (ret != ErrorCode::NO_ERROR) { @@ -774,13 +801,17 @@ int32_t InputMethodController::OnSelectionChange(std::u16string text, int start, int32_t InputMethodController::OnConfigurationChange(Configuration info) { IMSA_HILOGI("InputMethodController::OnConfigurationChange"); + if (!isBound_.load()) { + IMSA_HILOGE("not bound yet"); + return ErrorCode::ERROR_CLIENT_NOT_BOUND; + } std::lock_guard lock(configurationMutex_); enterKeyType_ = static_cast(info.GetEnterKeyType()); inputPattern_ = static_cast(info.GetTextInputType()); std::lock_guard agentLock(agentLock_); if (agent_ == nullptr) { IMSA_HILOGE("agent is nullptr"); - return ErrorCode::NO_ERROR; + return ErrorCode::ERROR_SERVICE_START_FAILED; } agent_->OnConfigurationChange(info); return ErrorCode::NO_ERROR; @@ -915,6 +946,25 @@ int32_t InputMethodController::GetInputPattern(int32_t &inputpattern) return ErrorCode::NO_ERROR; } +int32_t InputMethodController::GetTextConfig(TextTotalConfig &config) +{ + IMSA_HILOGI("InputMethodController run in."); + config.inputAttribute = textConfig_.inputAttribute; + config.cursorInfo = textConfig_.cursorInfo; + config.windowId = textConfig_.windowId; + + if (textConfig_.range.start == -1) { + IMSA_HILOGI("no valid SelectionRange param."); + return ErrorCode::NO_ERROR; + } + config.textSelection.oldBegin = selectNewBegin_; + config.textSelection.oldEnd = selectNewEnd_; + config.textSelection.newBegin = textConfig_.range.start; + config.textSelection.newEnd = textConfig_.range.end; + + return ErrorCode::NO_ERROR; +} + int32_t InputMethodController::SetCallingWindow(uint32_t windowId) { if (!isBound_.load()) { diff --git a/frameworks/native/inputmethod_controller/src/itypes_util.cpp b/frameworks/native/inputmethod_controller/src/itypes_util.cpp index 2fb8e0cb..6e329033 100644 --- a/frameworks/native/inputmethod_controller/src/itypes_util.cpp +++ b/frameworks/native/inputmethod_controller/src/itypes_util.cpp @@ -72,6 +72,16 @@ bool ITypesUtil::Unmarshalling(uint64_t &output, MessageParcel &data) return data.ReadUint64(output); } +bool ITypesUtil::Marshalling(double input, MessageParcel &data) +{ + return data.WriteDouble(input); +} + +bool ITypesUtil::Unmarshalling(double &output, MessageParcel &data) +{ + return data.ReadDouble(output); +} + bool ITypesUtil::Marshalling(const std::string &input, MessageParcel &data) { return data.WriteString(input); @@ -143,7 +153,7 @@ bool ITypesUtil::Marshalling(const SubProperty &input, MessageParcel &data) bool ITypesUtil::Unmarshalling(SubProperty &output, MessageParcel &data) { if (!Unmarshal(data, output.label, output.labelId, output.name, output.id, output.mode, output.locale, - output.language, output.icon, output.iconId)) { + output.language, output.icon, output.iconId)) { IMSA_HILOGE("ITypesUtil::read SubProperty from message parcel failed"); return false; } @@ -168,6 +178,53 @@ bool ITypesUtil::Unmarshalling(InputAttribute &output, MessageParcel &data) return true; } +bool ITypesUtil::Marshalling(const TextTotalConfig &input, MessageParcel &data) +{ + IMSA_HILOGI("tyx::start to Marshalling TextTotalConfig."); + if (!Marshal(data, input.inputAttribute.inputPattern, input.inputAttribute.enterKeyType, + input.inputAttribute.inputOption)) { + IMSA_HILOGE("write InputAttribute to message parcel failed"); + return false; + } + if (!Marshal(data, input.cursorInfo.left, input.cursorInfo.top, input.cursorInfo.height, input.cursorInfo.width)) { + IMSA_HILOGE("write CursorInfo to message parcel failed"); + return false; + } + if (!Marshal(data, input.textSelection.oldBegin, input.textSelection.oldEnd, input.textSelection.newBegin, + input.textSelection.newEnd)) { + IMSA_HILOGE("write TextSelection to message parcel failed"); + return false; + } + if (!Marshal(data, input.windowId)) { + IMSA_HILOGE("write windowId to message parcel failed"); + return false; + } + return true; +} + +bool ITypesUtil::Unmarshalling(TextTotalConfig &output, MessageParcel &data) +{ + if (!Unmarshalling(output.inputAttribute, data)) { + IMSA_HILOGE("read InputAttribute from message parcel failed"); + return false; + } + if (!Unmarshal( + data, output.cursorInfo.left, output.cursorInfo.top, output.cursorInfo.height, output.cursorInfo.width)) { + IMSA_HILOGE("read CursorInfo from message parcel failed"); + return false; + } + if (!Unmarshal(data, output.textSelection.oldBegin, output.textSelection.oldEnd, output.textSelection.newBegin, + output.textSelection.newEnd)) { + IMSA_HILOGE("read TextSelection from message parcel failed"); + return false; + } + if (!Unmarshal(data, output.windowId)) { + IMSA_HILOGE("read windowId from message parcel failed"); + return false; + } + return true; +} + bool ITypesUtil::Marshalling(const InputClientInfo &input, MessageParcel &data) { if (!Marshal(data, input.pid, input.uid, input.userID, input.displayID, input.attribute, input.isShowKeyboard, 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 35508359..5a955711 100644 --- a/interfaces/inner_api/inputmethod_controller/include/input_method_controller.h +++ b/interfaces/inner_api/inputmethod_controller/include/input_method_controller.h @@ -108,6 +108,21 @@ public: */ IMF_API int32_t Attach(sptr &listener, bool isShowKeyboard, const InputAttribute &attribute); + /** + * @brief Set listener and bind IMSA with given states and textConfig. + * + * This function is used to set listener and bind IMSA. + * Show soft keyboard when state is true, and customized attribute. + * + * @param listener Indicates the listener in order to manipulate text. + * @param isShowKeyboard Indicates the state, if you want to show soft keyboard, please pass in true. + * @param textConfig Indicates the textConfig, such as input attribute, cursorInfo, range of text selection, + * windowId. + * @return Returns 0 for success, others for failure. + * @since 10 + */ + IMF_API int32_t Attach(sptr &listener, bool isShowKeyboard, const TextConfig &textConfig); + /** * @brief Get text before cursor. * @@ -290,6 +305,17 @@ public: */ IMF_API int32_t GetInputPattern(int32_t &inputPattern); + /** + * @brief Get text config. + * + * This function is used to get text config of current client. + * + * @param textConfig Indicates the text config of current client that will be obtained. + * @return Returns 0 for success, others for failure. + * @since 10 + */ + IMF_API int32_t GetTextConfig(TextTotalConfig &config); + /** * @brief Get current input method property. * @@ -453,6 +479,7 @@ private: void RestoreAttachInfoInSaDied(); int32_t RestoreListenEventFlag(); void UpdateNativeEventFlag(EventType eventType, bool isOn); + void SaveTextConfig(const TextConfig &textConfig); std::shared_ptr settingListener_; std::shared_ptr controllerListener_; @@ -498,6 +525,10 @@ private: std::condition_variable textFieldReplyCountCv_; std::atomic_bool isDiedRestoreListen_{ false }; + + // todo ��Ҫ������ + TextConfig textConfig_; +// TextTotalConfig textConfig_; }; } // namespace MiscServices } // namespace OHOS diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index 8784b10a..b1aa1b59 100644 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -20,6 +20,7 @@ group("unittest") { deps += [ "cpp_test:InputMethodAbilityTest", + "cpp_test:InputMethodAttachTest", "cpp_test:InputMethodControllerTest", "cpp_test:InputMethodDfxTest", "cpp_test:InputMethodEditorTest", diff --git a/test/unittest/cpp_test/BUILD.gn b/test/unittest/cpp_test/BUILD.gn index 2bd599ba..69181061 100644 --- a/test/unittest/cpp_test/BUILD.gn +++ b/test/unittest/cpp_test/BUILD.gn @@ -54,6 +54,37 @@ ohos_unittest("InputMethodControllerTest") { ] } +ohos_unittest("InputMethodAttachTest") { + module_out_path = module_output_path + + sources = [ "src/input_method_controller_test.cpp" ] + + configs = [ ":module_private_config" ] + + deps = [ + "${inputmethod_path}/frameworks/native/inputmethod_ability:inputmethod_ability", + "${inputmethod_path}/interfaces/inner_api/inputmethod_controller:inputmethod_client_static", + "${inputmethod_path}/services:inputmethod_service", + "${inputmethod_path}/test/unittest/cpp_test/common:inputmethod_tdd_util", + "//third_party/googletest:gmock", + "//third_party/googletest:gtest_main", + ] + + external_deps = [ + "ability_base:want", + "ability_runtime:ability_context_native", + "ability_runtime:ability_manager", + "access_token:libaccesstoken_sdk", + "c_utils:utils", + "eventhandler:libeventhandler", + "hilog:libhilog", + "input:libmmi-client", + "ipc:ipc_single", + "napi:ace_napi", + "safwk:system_ability_fwk", + "samgr:samgr_proxy", + ] +} ohos_unittest("InputMethodAbilityTest") { module_out_path = module_output_path diff --git a/test/unittest/cpp_test/common/src/tdd_util.cpp b/test/unittest/cpp_test/common/src/tdd_util.cpp index b9ffad97..04486de9 100644 --- a/test/unittest/cpp_test/common/src/tdd_util.cpp +++ b/test/unittest/cpp_test/common/src/tdd_util.cpp @@ -44,7 +44,6 @@ constexpr const uint16_t EACH_LINE_LENGTH = 500; constexpr int32_t BUFF_LENGTH = 10; constexpr const char *CMD_PIDOF_IMS = "pidof inputmethod_ser"; uint64_t TddUtil::selfTokenID_ = 0; -uint64_t TddUtil::testTokenID_ = 0; int64_t TddUtil::selfUid_ = -1; int32_t TddUtil::userID_ = INVALID_USER_ID; int32_t TddUtil::GetCurrentUserId() @@ -71,27 +70,19 @@ uint64_t TddUtil::AllocTestTokenID(bool isSystemApp, bool needPermission, const HapInfoParams infoParams = { .userID = GetCurrentUserId(), .bundleName = bundleName, .instIndex = 0, - .appIDDesc = "ohos.inputmethod_test.demo", - .isSystemApp = true }; + .appIDDesc = bundleName, + .isSystemApp = isSystemApp }; PermissionStateFull permissionState = { .permissionName = "ohos.permission.CONNECT_IME_ABILITY", .isGeneral = true, .resDeviceID = { "local" }, .grantStatus = { PermissionState::PERMISSION_GRANTED }, .grantFlags = { 1 } }; HapPolicyParams policyParams = { .apl = APL_NORMAL, - .domain = "test.domain.inputmethod", + .domain = bundleName, .permList = {}, .permStateList = { permissionState } }; if (!needPermission) { - policyParams = { .apl = APL_NORMAL, .domain = "test.domain.inputmethod", .permList = {}, .permStateList = {} }; - } - if (!isSystemApp) { - infoParams = { - .userID = GetCurrentUserId(), - .bundleName = bundleName, - .instIndex = 0, - .appIDDesc = "ohos.inputmethod_test.demo" - }; + policyParams = { .apl = APL_NORMAL, .domain = bundleName, .permList = {}, .permStateList = {} }; } auto tokenInfo = AccessTokenKit::AllocHapToken(infoParams, policyParams); return tokenInfo.tokenIDEx; @@ -185,7 +176,7 @@ void TddUtil::KillImsaProcess() IMSA_HILOGE("Kill failed, ret: %{public}d", ret); return; } - IMSA_HILOGE("Kill success."); + IMSA_HILOGI("Kill success."); } sptr TddUtil::GetBundleMgr() 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 f1500794..bf3adb73 100644 --- a/test/unittest/cpp_test/src/input_method_ability_test.cpp +++ b/test/unittest/cpp_test/src/input_method_ability_test.cpp @@ -444,6 +444,27 @@ HWTEST_F(InputMethodAbilityTest, testGetEnterKeyType, TestSize.Level0) EXPECT_EQ(inputPattern, (int)textInputType); } +/** +* @tc.name: testGetTextConfig +* @tc.desc: InputMethodAbility GetTextConfig +* @tc.type: FUNC +* @tc.require: +* @tc.author: Hollokin +*/ +HWTEST_F(InputMethodAbilityTest, testGetTextConfig, TestSize.Level0) +{ + IMSA_HILOGI("InputMethodAbility testGetTextConfig START"); + sptr textChangeListener = new TextChangeListener(); + TextConfig textConfig; + textConfig.inputAttribute = { .inputPattern = 0, .enterKeyType = 1 }; + auto ret = imc_->Attach(textChangeListener, false, textConfig); + TextTotalConfig textTotalConfig; + ret = inputMethodAbility_->GetTextConfig(textTotalConfig); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + EXPECT_EQ(textTotalConfig.inputAttribute.enterKeyType, textConfig.inputAttribute.inputPattern); + textChangeListener = nullptr; +} + /** * @tc.name: testSelectByRange_001 * @tc.desc: InputMethodAbility SelectByRange diff --git a/test/unittest/cpp_test/src/input_method_attach_test.cpp b/test/unittest/cpp_test/src/input_method_attach_test.cpp new file mode 100644 index 00000000..1d4a994b --- /dev/null +++ b/test/unittest/cpp_test/src/input_method_attach_test.cpp @@ -0,0 +1,869 @@ +/* + * Copyright (C) 2023 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 +#include +#include +#include +#include +#include +#include + +#include "ability_manager_client.h" +#include "accesstoken_kit.h" +#include "global.h" +#include "i_input_data_channel.h" +#include "input_attribute.h" +#include "input_control_channel_stub.h" +#include "input_data_channel_proxy.h" +#include "input_data_channel_stub.h" +#include "input_method_ability.h" +#include "input_method_agent_stub.h" +#include "input_method_controller.h" +#include "input_method_core_proxy.h" +#include "input_method_core_stub.h" +#include "input_method_panel.h" +#include "message_handler.h" +#include "os_account_manager.h" +#include "token_setproc.h" + +using namespace testing::ext; +using namespace OHOS::Security::AccessToken; +using namespace OHOS::AccountSA; +namespace OHOS { +namespace MiscServices { +constexpr uint32_t DEALY_TIME = 1; +std::u16string g_textTemp = u"我們我們ddddd"; +constexpr int32_t MAIN_USER_ID = 100; +class InputMethodAttachTest : public testing::Test { +public: + static std::string imeIdStopped_; + static std::mutex imeListenerCallbackLock_; + static std::condition_variable imeListenerCv_; + static bool showKeyboard_; + static std::mutex textListenerCallbackLock_; + static std::condition_variable textListenerCv_; + static int direction_; + static int deleteForwardLength_; + static int deleteBackwardLength_; + static std::u16string insertText_; + static int key_; + static int keyboardStatus_; + static bool status_; + static int selectionStart_; + static int selectionEnd_; + static int selectionDirection_; + static int32_t action_; + static constexpr int CURSOR_DIRECTION_BASE_VALUE = 2011; + static sptr inputMethodController_; + static sptr inputMethodAbility_; + static uint64_t selfTokenID_; + static AccessTokenID testTokenID_; + + class EngineListenerImpl : public InputMethodEngineListener { + public: + EngineListenerImpl() = default; + ~EngineListenerImpl() = default; + + void OnKeyboardStatus(bool isShow) + { + showKeyboard_ = isShow; + InputMethodAttachTest::imeListenerCv_.notify_one(); + IMSA_HILOGI("EngineListenerImpl OnKeyboardStatus"); + } + + void OnInputStart() + { + IMSA_HILOGI("EngineListenerImpl OnInputStart"); + } + + void OnInputStop(const std::string &imeId) + { + imeIdStopped_ = imeId; + IMSA_HILOGI("EngineListenerImpl OnInputStop"); + } + + void OnSetCallingWindow(uint32_t windowId) + { + IMSA_HILOGI("EngineListenerImpl OnSetCallingWindow"); + } + + void OnSetSubtype(const SubProperty &property) + { + IMSA_HILOGI("EngineListenerImpl OnSetSubtype"); + } + }; + class TextChangeListenerImpl : public OnTextChangedListener { + public: + void InsertText(const std::u16string &text) override + { + insertText_ = text; + InputMethodAttachTest::textListenerCv_.notify_one(); + } + + void DeleteForward(int32_t length) override + { + deleteForwardLength_ = length; + InputMethodAttachTest::textListenerCv_.notify_one(); + IMSA_HILOGI("TextChangeListenerImpl: DeleteForward, length is: %{public}d", length); + } + + void DeleteBackward(int32_t length) override + { + deleteBackwardLength_ = length; + InputMethodAttachTest::textListenerCv_.notify_one(); + IMSA_HILOGI("TextChangeListenerImpl: DeleteBackward, direction is: %{public}d", length); + } + + void SendKeyEventFromInputMethod(const KeyEvent &event) override {} + + void SendKeyboardStatus(const KeyboardStatus &keyboardStatus) override {} + + void SendFunctionKey(const FunctionKey &functionKey) override + { + EnterKeyType enterKeyType = functionKey.GetEnterKeyType(); + key_ = static_cast(enterKeyType); + InputMethodAttachTest::textListenerCv_.notify_one(); + } + + void SetKeyboardStatus(bool status) override + { + status_ = status; + } + + void MoveCursor(const Direction direction) override + { + direction_ = (int)direction; + InputMethodAttachTest::textListenerCv_.notify_one(); + IMSA_HILOGI("TextChangeListenerImpl: MoveCursor, direction is: %{public}d", direction); + } + + void HandleSetSelection(int32_t start, int32_t end) override + { + selectionStart_ = start; + selectionEnd_ = end; + InputMethodAttachTest::textListenerCv_.notify_one(); + IMSA_HILOGI("TextChangeListenerImpl, selectionStart_: %{public}d, selectionEnd_: %{public}d", + selectionStart_, selectionEnd_); + } + + void HandleExtendAction(int32_t action) override + { + action_ = action; + InputMethodAttachTest::textListenerCv_.notify_one(); + IMSA_HILOGI("HandleExtendAction, action_: %{public}d", action_); + } + + void HandleSelect(int32_t keyCode, int32_t cursorMoveSkip) override + { + selectionDirection_ = keyCode; + InputMethodAttachTest::textListenerCv_.notify_one(); + IMSA_HILOGI("TextChangeListenerImpl, selectionDirection_: %{public}d", selectionDirection_); + } + }; + static void AllocTestTokenID(const std::string &bundleName) + { + IMSA_HILOGI("bundleName: %{public}s", bundleName.c_str()); + std::vector userIds; + auto ret = OsAccountManager::QueryActiveOsAccountIds(userIds); + if (ret != ErrorCode::NO_ERROR || userIds.empty()) { + IMSA_HILOGE("query active os account id failed"); + userIds[0] = MAIN_USER_ID; + } + HapInfoParams infoParams = { .userID = userIds[0], + .bundleName = bundleName, + .instIndex = 0, + .appIDDesc = "ohos.inputmethod_test.demo" }; + PermissionStateFull permissionState = { .permissionName = "ohos.permission.CONNECT_IME_ABILITY", + .isGeneral = true, + .resDeviceID = { "local" }, + .grantStatus = { PermissionState::PERMISSION_GRANTED }, + .grantFlags = { 1 } }; + HapPolicyParams policyParams = { .apl = APL_NORMAL, + .domain = "test.domain.inputmethod", + .permList = {}, + .permStateList = { permissionState } }; + + AccessTokenKit::AllocHapToken(infoParams, policyParams); + testTokenID_ = AccessTokenKit::GetHapTokenID(infoParams.userID, infoParams.bundleName, infoParams.instIndex); + } + static void DeleteTestTokenID() + { + AccessTokenKit::DeleteToken(testTokenID_); + } + static void SetTestTokenID() + { + auto ret = SetSelfTokenID(testTokenID_); + IMSA_HILOGI("SetSelfTokenID ret: %{public}d", ret); + } + static void RestoreSelfTokenID() + { + auto ret = SetSelfTokenID(selfTokenID_); + IMSA_HILOGI("SetSelfTokenID ret = %{public}d", ret); + } + static void SetUpTestCase(void) + { + IMSA_HILOGI("InputMethodAttachTest::SetUpTestCase"); + inputMethodController_ = InputMethodController::GetInstance(); + inputMethodAbility_ = InputMethodAbility::GetInstance(); + inputMethodAbility_->OnImeReady(); + inputMethodAbility_->SetCoreAndAgent(); + inputMethodAbility_->SetImeListener(std::make_shared()); + } + static void TearDownTestCase(void) + { + IMSA_HILOGI("InputMethodAttachTest::TearDownTestCase"); + } + void SetUp() + { + IMSA_HILOGI("InputMethodAttachTest::SetUp"); + } + void TearDown() + { + IMSA_HILOGI("InputMethodAttachTest::TearDown"); + } +}; + +std::string InputMethodAttachTest::imeIdStopped_; +std::mutex InputMethodAttachTest::imeListenerCallbackLock_; +std::condition_variable InputMethodAttachTest::imeListenerCv_; +bool InputMethodAttachTest::showKeyboard_ = false; +std::mutex InputMethodAttachTest::textListenerCallbackLock_; +std::condition_variable InputMethodAttachTest::textListenerCv_; +int InputMethodAttachTest::direction_; +int InputMethodAttachTest::deleteForwardLength_ = 0; +int InputMethodAttachTest::deleteBackwardLength_ = 0; +std::u16string InputMethodAttachTest::insertText_; +int InputMethodAttachTest::key_ = 0; +int InputMethodAttachTest::keyboardStatus_; +bool InputMethodAttachTest::status_; +int InputMethodAttachTest::selectionStart_ = -1; +int InputMethodAttachTest::selectionEnd_ = -1; +int InputMethodAttachTest::selectionDirection_ = 0; +int32_t InputMethodAttachTest::action_ = 0; +sptr InputMethodAttachTest::inputMethodController_; +sptr InputMethodAttachTest::inputMethodAbility_; +uint64_t InputMethodAttachTest::selfTokenID_ = 0; +AccessTokenID InputMethodAttachTest::testTokenID_ = 0; + +/** + * @tc.name: testAttach001 + * @tc.desc: test Attach + * @tc.type: FUNC + */ +HWTEST_F(InputMethodAttachTest, testAttach001, TestSize.Level0) +{ + IMSA_HILOGI("test testAttach001 after attach."); + sptr TextChangeListenerImpl = new TextChangeListenerImpl(); + auto ret = inputMethodController_->Attach(TextChangeListenerImpl); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + + int32_t keyType = -1; + ret = inputMethodAbility_->GetEnterKeyType(keyType); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + EXPECT_EQ(keyType, 0); + int32_t inputPattern = -1; + ret = inputMethodAbility_->GetInputPattern(inputPattern); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + EXPECT_EQ(inputPattern, InputAttribute::PATTERN_TEXT); + EXPECT_EQ(InputMethodAttachTest::showKeyboard_, true); + + ret = inputMethodController_->Close(); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); +} + +/** + * @tc.name: testAttach002 + * @tc.desc: test Attach + * @tc.type: FUNC + */ +HWTEST_F(InputMethodAttachTest, testAttach002, TestSize.Level0) +{ + IMSA_HILOGI("test testAttach002 after attach."); + sptr TextChangeListenerImpl = new TextChangeListenerImpl(); + auto ret = inputMethodController_->Attach(TextChangeListenerImpl, false); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + + int32_t keyType = -1; + ret = inputMethodAbility_->GetEnterKeyType(keyType); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + EXPECT_EQ(keyType, 0); + int32_t inputPattern = -1; + ret = inputMethodAbility_->GetInputPattern(inputPattern); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + EXPECT_EQ(inputPattern, InputAttribute::PATTERN_TEXT); + + EXPECT_EQ(InputMethodAttachTest::showKeyboard_, false); + ret = inputMethodController_->Close(); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); +} + +/** + * @tc.name: testAttach003 + * @tc.desc: test Attach + * @tc.type: FUNC + */ +HWTEST_F(InputMethodAttachTest, testAttach003, TestSize.Level0) +{ + IMSA_HILOGI("test testAttach003 after attach."); + sptr TextChangeListenerImpl = new TextChangeListenerImpl(); + InputAttribute attribute; + attribute.inputPattern = 2; + attribute.enterKeyType = 1; + auto ret = inputMethodController_->Attach(TextChangeListenerImpl, true, attribute); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + + int32_t keyType = -1; + ret = inputMethodAbility_->GetEnterKeyType(keyType); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + EXPECT_EQ(keyType, attribute.enterKeyType); + int32_t inputPattern = -1; + ret = inputMethodAbility_->GetInputPattern(inputPattern); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + EXPECT_EQ(inputPattern, attribute.inputPattern); + + EXPECT_EQ(InputMethodAttachTest::showKeyboard_, true); + ret = inputMethodController_->Close(); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); +} + +/** + * @tc.name: testAttach004 + * @tc.desc: test Attach + * @tc.type: FUNC + */ +HWTEST_F(InputMethodAttachTest, testAttach004, TestSize.Level0) +{ + IMSA_HILOGI("test testAttach004 after attach."); + sptr TextChangeListenerImpl = new TextChangeListenerImpl(); + InputAttribute attribute; + attribute.inputPattern = 3; + attribute.enterKeyType = 2; + TextConfig config; + config.inputAttribute = attribute; + auto ret = inputMethodController_->Attach(TextChangeListenerImpl, false, config); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + + int32_t keyType = -1; + ret = inputMethodAbility_->GetEnterKeyType(keyType); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + EXPECT_EQ(keyType, config.inputAttribute.enterKeyType); + int32_t inputPattern = -1; + ret = inputMethodAbility_->GetInputPattern(inputPattern); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + EXPECT_EQ(inputPattern, config.inputAttribute.inputPattern); + + EXPECT_EQ(InputMethodAttachTest::showKeyboard_, false); + ret = inputMethodController_->Close(); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); +} + +/** + * @tc.name: testAttach005 + * @tc.desc: test Attach, test optional param in TextConfig + * @tc.type: FUNC + */ +HWTEST_F(InputMethodAttachTest, testAttach005, TestSize.Level0) +{ + IMSA_HILOGI("test testAttach005 after attach."); + sptr TextChangeListenerImpl = new TextChangeListenerImpl(); + InputAttribute attribute; + attribute.inputPattern = 3; + attribute.enterKeyType = 2; + TextConfig config; + config.inputAttribute = attribute; + CursorInfo cursorInfo; + cursorInfo.left = 0; + cursorInfo.top = 1; + cursorInfo.width = 0.5; + cursorInfo.height = 1.2; + config.cursorInfo = cursorInfo; + SelectionRange selectionRange; + selectionRange.start = 0; + selectionRange.end = 2; + config.range = selectionRange; + config.windowId = 10; + auto ret = inputMethodController_->Attach(TextChangeListenerImpl, true, config); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + + int32_t keyType = -1; + ret = inputMethodAbility_->GetEnterKeyType(keyType); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + EXPECT_EQ(keyType, config.inputAttribute.enterKeyType); + int32_t inputPattern = -1; + ret = inputMethodAbility_->GetInputPattern(inputPattern); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + EXPECT_EQ(inputPattern, config.inputAttribute.inputPattern); + + EXPECT_EQ(InputMethodAttachTest::showKeyboard_, true); + TextTotalConfig textConfig; + ret = inputMethodAbility_->GetTextConfig(textConfig); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + EXPECT_EQ(textConfig.inputAttribute, config.inputAttribute); + EXPECT_EQ(textConfig.windowId, config.windowId); + EXPECT_EQ(textConfig.cursorInfo, config.cursorInfo); + EXPECT_EQ(textConfig.textSelection.newBegin, config.range.start); + EXPECT_EQ(textConfig.textSelection.newEnd, config.range.end); + EXPECT_EQ(textConfig.textSelection.oldBegin, 0); + EXPECT_EQ(textConfig.textSelection.oldEnd, 0); + ret = inputMethodController_->Close(); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); +} + +/** + * @tc.name: testOnConfigurationChangeWithOutAttach + * @tc.desc: test OnConfigurationChange without attach + * @tc.type: FUNC + */ +HWTEST_F(InputMethodAttachTest, testOnConfigurationChangeWithOutAttach, TestSize.Level0) +{ + IMSA_HILOGI("InputMethodAttachTest testOnConfigurationChangeWithOutAttach in."); + Configuration config; + EnterKeyType keyType = EnterKeyType::NEXT; + config.SetEnterKeyType(keyType); + TextInputType textInputType = TextInputType::DATETIME; + config.SetTextInputType(textInputType); + auto ret = inputMethodController_->OnConfigurationChange(config); + EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_BOUND); +} + +/** + * @tc.name: testOnConfigurationChange + * @tc.desc: test OnConfigurationChange after attach + * @tc.type: FUNC + */ +HWTEST_F(InputMethodAttachTest, testOnConfigurationChange, TestSize.Level0) +{ + IMSA_HILOGI("test OnConfigurationChange after attach."); + sptr TextChangeListenerImpl = new TextChangeListenerImpl(); + auto ret = inputMethodController_->Attach(TextChangeListenerImpl); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + + Configuration config; + EnterKeyType keyType = EnterKeyType::NEXT; + config.SetEnterKeyType(keyType); + TextInputType textInputType = TextInputType::DATETIME; + config.SetTextInputType(textInputType); + ret = inputMethodController_->OnConfigurationChange(config); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + int32_t keyType2; + ret = inputMethodAbility_->GetEnterKeyType(keyType2); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + EXPECT_EQ(keyType2, (int)keyType); + int32_t inputPattern; + ret = inputMethodAbility_->GetInputPattern(inputPattern); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + EXPECT_EQ(inputPattern, (int)textInputType); + + ret = inputMethodController_->Close(); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); +} + +/** + * @tc.name: testGetTextConfig + * @tc.desc: test GetTextConfig of InputMethodAbility + * @tc.type: FUNC + */ +HWTEST_F(InputMethodAttachTest, testGetTextConfig, TestSize.Level0) +{ + IMSA_HILOGI("test OnConfigurationChange001 after attach."); + sptr TextChangeListenerImpl = new TextChangeListenerImpl(); + InputAttribute attribute; + attribute.inputPattern = 3; + attribute.enterKeyType = 2; + TextConfig config; + config.inputAttribute = attribute; + CursorInfo cursorInfo; + cursorInfo.left = 0; + cursorInfo.top = 1; + cursorInfo.width = 0.5; + cursorInfo.height = 1.2; + config.cursorInfo = cursorInfo; + SelectionRange selectionRange; + selectionRange.start = 0; + selectionRange.end = 2; + config.range = selectionRange; + config.windowId = 10; + auto ret = inputMethodController_->Attach(TextChangeListenerImpl, false, config); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + TextTotalConfig totalConfig; + ret = inputMethodAbility_->GetTextConfig(totalConfig); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + + EXPECT_EQ(totalConfig.inputAttribute.inputPattern, attribute.inputPattern); + EXPECT_EQ(totalConfig.inputAttribute.enterKeyType, attribute.enterKeyType); + EXPECT_EQ(totalConfig.cursorInfo.height, cursorInfo.height); + EXPECT_EQ(totalConfig.cursorInfo.width, cursorInfo.width); + EXPECT_EQ(totalConfig.cursorInfo.left, cursorInfo.left); + EXPECT_EQ(totalConfig.cursorInfo.top, cursorInfo.top); + EXPECT_EQ(totalConfig.textSelection.newBegin, selectionRange.start); + EXPECT_EQ(totalConfig.textSelection.newEnd, selectionRange.end); + EXPECT_EQ(totalConfig.textSelection.oldBegin, 0); + EXPECT_EQ(totalConfig.textSelection.oldEnd, 0); + EXPECT_EQ(totalConfig.windowId, config.windowId); +} + +/** + * @tc.name: testOnCursorUpdateAfterAttach001 + * @tc.desc: test OnCursorUpdate after attach + * @tc.type: FUNC + */ +HWTEST_F(InputMethodAttachTest, testOnCursorUpdateAfterAttach001, TestSize.Level0) +{ + IMSA_HILOGI("test testOnCursorUpdateAfterAttach001."); + sptr TextChangeListenerImpl = new TextChangeListenerImpl(); + auto ret = inputMethodController_->Attach(TextChangeListenerImpl); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + CursorInfo cursorInfo = { .top = 5, .left = 5, .height = 5, .width = 0.8 }; + ret = inputMethodController_->OnCursorUpdate(cursorInfo); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + TextTotalConfig totalConfig; + ret = inputMethodAbility_->GetTextConfig(totalConfig); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + EXPECT_EQ(totalConfig.cursorInfo.height, cursorInfo.height); + EXPECT_EQ(totalConfig.cursorInfo.width, cursorInfo.width); + EXPECT_EQ(totalConfig.cursorInfo.left, cursorInfo.left); + EXPECT_EQ(totalConfig.cursorInfo.top, cursorInfo.top); +} + +/** + * @tc.name: testOnCursorUpdateAfterAttach002 + * @tc.desc: test OnCursorUpdate after attach + * @tc.type: FUNC + */ +HWTEST_F(InputMethodAttachTest, testOnCursorUpdateAfterAttach002, TestSize.Level0) +{ + IMSA_HILOGI("test testOnCursorUpdateAfterAttach002."); + sptr TextChangeListenerImpl = new TextChangeListenerImpl(); + InputAttribute attribute; + attribute.inputPattern = 3; + attribute.enterKeyType = 2; + TextConfig config; + config.inputAttribute = attribute; + config.cursorInfo = { .top = 1, .left = 1, .height = 1, .width = 0.4 }; + auto ret = inputMethodController_->Attach(TextChangeListenerImpl, true, config); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + CursorInfo cursorInfo = { .top = 5, .left = 5, .height = 5, .width = 0.8 }; + ret = inputMethodController_->OnCursorUpdate(cursorInfo); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + TextTotalConfig totalConfig; + ret = inputMethodAbility_->GetTextConfig(totalConfig); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + EXPECT_EQ(totalConfig.cursorInfo.height, cursorInfo.height); + EXPECT_EQ(totalConfig.cursorInfo.width, cursorInfo.width); + EXPECT_EQ(totalConfig.cursorInfo.left, cursorInfo.left); + EXPECT_EQ(totalConfig.cursorInfo.top, cursorInfo.top); +} + +/** + * @tc.name: testOnSelectionChangeAfterAttach001 + * @tc.desc: test OnSelectionChange after attach + * @tc.type: FUNC + */ +HWTEST_F(InputMethodAttachTest, testOnSelectionChangeAfterAttach001, TestSize.Level0) +{ + IMSA_HILOGI("test testOnSelectionChangeAfterAttach001."); + sptr TextChangeListenerImpl = new TextChangeListenerImpl(); + auto ret = inputMethodController_->Attach(TextChangeListenerImpl); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + int start = 0; + int end = 1; + ret = inputMethodController_->OnSelectionChange(Str8ToStr16("aaa"), start, end); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + + TextTotalConfig totalConfig; + ret = inputMethodAbility_->GetTextConfig(totalConfig); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + EXPECT_EQ(totalConfig.textSelection.newBegin, start); + EXPECT_EQ(totalConfig.textSelection.newEnd, end); + EXPECT_EQ(totalConfig.textSelection.oldBegin, 0); + EXPECT_EQ(totalConfig.textSelection.oldEnd, 0); +} + +/** + * @tc.name: testOnSelectionChangeAfterAttach002 + * @tc.desc: test OnSelectionChange after attach + * @tc.type: FUNC + */ +HWTEST_F(InputMethodAttachTest, testOnSelectionChangeAfterAttach002, TestSize.Level0) +{ + IMSA_HILOGI("test testOnSelectionChangeAfterAttach002."); + sptr TextChangeListenerImpl = new TextChangeListenerImpl(); + InputAttribute attribute; + attribute.inputPattern = 3; + attribute.enterKeyType = 2; + TextConfig config; + config.inputAttribute = attribute; + config.range = { .start = 1, .end = 2 }; + auto ret = inputMethodController_->Attach(TextChangeListenerImpl, false, config); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + int start = 0; + int end = 1; + ret = inputMethodController_->OnSelectionChange(Str8ToStr16("aaa"), start, end); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + + TextTotalConfig totalConfig; + ret = inputMethodAbility_->GetTextConfig(totalConfig); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + EXPECT_EQ(totalConfig.textSelection.newBegin, start); + EXPECT_EQ(totalConfig.textSelection.newEnd, end); + EXPECT_EQ(totalConfig.textSelection.oldBegin, config.range.start); + EXPECT_EQ(totalConfig.textSelection.oldEnd, config.range.end); +} + +/** + * @tc.name: testOnConfigurationChangeAfterAttach001 + * @tc.desc: test OnConfigurationChange after attach + * @tc.type: FUNC + */ +HWTEST_F(InputMethodAttachTest, testOnConfigurationChangeAfterAttach001, TestSize.Level0) +{ + IMSA_HILOGI("test testOnConfigurationChangeAfterAttach001."); + sptr TextChangeListenerImpl = new TextChangeListenerImpl(); + auto ret = inputMethodController_->Attach(TextChangeListenerImpl); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + + Configuration config; + config.SetTextInputType(TextInputType::DATETIME); + config.SetEnterKeyType(EnterKeyType::NEXT); + ret = inputMethodController_->OnConfigurationChange(config); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + + TextTotalConfig totalConfig; + ret = inputMethodAbility_->GetTextConfig(totalConfig); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + EXPECT_EQ(totalConfig.inputAttribute.inputPattern, config.GetTextInputType()); + EXPECT_EQ(totalConfig.inputAttribute.enterKeyType, config.GetEnterKeyType()); +} + +/** + * @tc.name: testOnConfigurationChangeAfterAttach002 + * @tc.desc: test OnConfigurationChange after attach + * @tc.type: FUNC + */ +HWTEST_F(InputMethodAttachTest, testOnConfigurationChangeAfterAttach002, TestSize.Level0) +{ + IMSA_HILOGI("test testOnConfigurationChangeAfterAttach002."); + sptr TextChangeListenerImpl = new TextChangeListenerImpl(); + InputAttribute attribute; + attribute.inputPattern = 3; + attribute.enterKeyType = 2; + TextConfig config; + config.inputAttribute = attribute; + auto ret = inputMethodController_->Attach(TextChangeListenerImpl, false, config); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + + Configuration configuration; + configuration.SetTextInputType(TextInputType::DATETIME); + configuration.SetEnterKeyType(EnterKeyType::NEXT); + ret = inputMethodController_->OnConfigurationChange(configuration); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + + TextTotalConfig totalConfig; + ret = inputMethodAbility_->GetTextConfig(totalConfig); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + EXPECT_EQ(totalConfig.inputAttribute.inputPattern, configuration.GetTextInputType()); + EXPECT_EQ(totalConfig.inputAttribute.enterKeyType, configuration.GetEnterKeyType()); +} + +/** + * @tc.name: testSetCallingWindowAfterAttach001 + * @tc.desc: test SetCallingWindow after attach + * @tc.type: FUNC + */ +HWTEST_F(InputMethodAttachTest, testSetCallingWindowAfterAttach001, TestSize.Level0) +{ + IMSA_HILOGI("test testSetCallingWindowAfterAttach001."); + sptr TextChangeListenerImpl = new TextChangeListenerImpl(); + auto ret = inputMethodController_->Attach(TextChangeListenerImpl); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + + uint32_t windowId = 99; + ret = inputMethodController_->SetCallingWindow(windowId); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + + TextTotalConfig totalConfig; + ret = inputMethodAbility_->GetTextConfig(totalConfig); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + EXPECT_EQ(totalConfig.windowId, windowId); +} + +/** + * @tc.name: testSetCallingWindowAfterAttach002 + * @tc.desc: test SetCallingWindow after attach + * @tc.type: FUNC + */ +HWTEST_F(InputMethodAttachTest, testSetCallingWindowAfterAttach002, TestSize.Level0) +{ + IMSA_HILOGI("test testSetCallingWindowAfterAttach002."); + sptr TextChangeListenerImpl = new TextChangeListenerImpl(); + InputAttribute attribute; + attribute.inputPattern = 3; + attribute.enterKeyType = 2; + TextConfig config; + config.inputAttribute = attribute; + config.windowId = 88; + auto ret = inputMethodController_->Attach(TextChangeListenerImpl, false, config); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + + uint32_t windowId = 99; + ret = inputMethodController_->SetCallingWindow(windowId); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + + TextTotalConfig totalConfig; + ret = inputMethodAbility_->GetTextConfig(totalConfig); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + EXPECT_EQ(totalConfig.windowId, windowId); +} + +/** + * @tc.name: testOnCursorUpdate001 + * @tc.desc: test OnCursorUpdate after attach + * @tc.type: FUNC + */ +HWTEST_F(InputMethodAttachTest, testOnCursorUpdate001, TestSize.Level0) +{ + IMSA_HILOGI("test testOnCursorUpdate001."); + sptr TextChangeListenerImpl = new TextChangeListenerImpl(); + auto ret = inputMethodController_->Attach(TextChangeListenerImpl); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + CursorInfo cursorInfo = { .top = 5, .left = 5, .height = 5, .width = 0.8 }; + ret = inputMethodController_->OnCursorUpdate(cursorInfo); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + + InputAttribute attribute; + attribute.inputPattern = 3; + attribute.enterKeyType = 2; + TextConfig config; + config.inputAttribute = attribute; + CursorInfo cursorInfo2 = { .top = 10, .left = 9, .width = 8, .height = 7 }; + config.cursorInfo = cursorInfo2; + ret = inputMethodController_->Attach(TextChangeListenerImpl, false, config); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + + TextTotalConfig totalConfig; + ret = inputMethodAbility_->GetTextConfig(totalConfig); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + EXPECT_EQ(totalConfig.cursorInfo.height, cursorInfo2.height); + EXPECT_EQ(totalConfig.cursorInfo.width, cursorInfo2.width); + EXPECT_EQ(totalConfig.cursorInfo.left, cursorInfo2.left); + EXPECT_EQ(totalConfig.cursorInfo.top, cursorInfo2.top); +} + +/** + * @tc.name: testOnSelectionChange + * @tc.desc: test OnSelectionChange after attach + * @tc.type: FUNC + */ +HWTEST_F(InputMethodAttachTest, testOnSelectionChange, TestSize.Level0) +{ + IMSA_HILOGI("test testOnSelectionChange."); + sptr TextChangeListenerImpl = new TextChangeListenerImpl(); + auto ret = inputMethodController_->Attach(TextChangeListenerImpl); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + int start = 0; + int end = 1; + ret = inputMethodController_->OnSelectionChange(Str8ToStr16("bbb"), start, end); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + + InputAttribute attribute; + attribute.inputPattern = 3; + attribute.enterKeyType = 2; + TextConfig config; + config.inputAttribute = attribute; + config.range.start = 10; + config.range.end = 20; + ret = inputMethodController_->Attach(TextChangeListenerImpl, false, config); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + + TextTotalConfig totalConfig; + ret = inputMethodAbility_->GetTextConfig(totalConfig); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + EXPECT_EQ(totalConfig.textSelection.newBegin, config.range.start); + EXPECT_EQ(totalConfig.textSelection.newEnd, config.range.start); + // todo + EXPECT_EQ(totalConfig.textSelection.oldBegin, 0); + EXPECT_EQ(totalConfig.textSelection.oldEnd, 0); +} + +/** + * @tc.name: testOnConfigurationChange + * @tc.desc: test OnConfigurationChange after attach + * @tc.type: FUNC + */ +HWTEST_F(InputMethodAttachTest, testOnConfigurationChange, TestSize.Level0) +{ + IMSA_HILOGI("test testOnConfigurationChange."); + sptr TextChangeListenerImpl = new TextChangeListenerImpl(); + auto ret = inputMethodController_->Attach(TextChangeListenerImpl); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + + Configuration configuration; + configuration.SetTextInputType(TextInputType::DATETIME); + configuration.SetEnterKeyType(EnterKeyType::NEXT); + ret = inputMethodController_->OnConfigurationChange(configuration); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + + InputAttribute attribute; + attribute.inputPattern = 3; + attribute.enterKeyType = 2; + TextConfig config; + config.inputAttribute = attribute; + config.inputAttribute.enterKeyType = 5; + config.inputAttribute.inputPattern = 5; + ret = inputMethodController_->Attach(TextChangeListenerImpl, false, config); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + + TextTotalConfig totalConfig; + ret = inputMethodAbility_->GetTextConfig(totalConfig); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + + EXPECT_EQ(totalConfig.inputAttribute.inputPattern, config.inputAttribute.inputPattern); + EXPECT_EQ(totalConfig.inputAttribute.enterKeyType, config.inputAttribute.enterKeyType); +} + +/** + * @tc.name: testSetCallingWindow + * @tc.desc: test SetCallingWindow after attach + * @tc.type: FUNC + */ +HWTEST_F(InputMethodAttachTest, testSetCallingWindow, TestSize.Level0) +{ + IMSA_HILOGI("test testSetCallingWindow."); + sptr TextChangeListenerImpl = new TextChangeListenerImpl(); + auto ret = inputMethodController_->Attach(TextChangeListenerImpl); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + + uint32_t windowId = 88; + ret = inputMethodController_->SetCallingWindow(windowId); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + + InputAttribute attribute; + attribute.inputPattern = 3; + attribute.enterKeyType = 2; + TextConfig config; + config.windowId = 77; + ret = inputMethodController_->Attach(TextChangeListenerImpl, false, config); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + + TextTotalConfig totalConfig; + ret = inputMethodAbility_->GetTextConfig(totalConfig); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + + EXPECT_EQ(totalConfig.windowId, config.windowId); +} +} // namespace MiscServices +} // namespace OHOS -- Gitee From a9c24a2a7a1da6c210f7a923c689fd1c4f7e276a Mon Sep 17 00:00:00 2001 From: Hollokin Date: Fri, 7 Jul 2023 10:18:42 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=E4=BF=AE=E6=94=B9TDD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Hollokin --- .../js_get_input_method_controller.cpp | 6 +- .../src/input_method_ability.cpp | 37 +- .../src/input_method_panel.cpp | 3 + .../src/input_data_channel_stub.cpp | 1 - .../src/input_method_controller.cpp | 30 +- test/unittest/cpp_test/BUILD.gn | 8 +- .../src/input_method_ability_test.cpp | 3 +- .../cpp_test/src/input_method_attach_test.cpp | 382 ++++-------------- .../src/input_method_controller_test.cpp | 41 +- 9 files changed, 139 insertions(+), 372 deletions(-) diff --git a/frameworks/js/napi/inputmethodclient/js_get_input_method_controller.cpp b/frameworks/js/napi/inputmethodclient/js_get_input_method_controller.cpp index e0db965a..e2c37cee 100644 --- a/frameworks/js/napi/inputmethodclient/js_get_input_method_controller.cpp +++ b/frameworks/js/napi/inputmethodclient/js_get_input_method_controller.cpp @@ -436,18 +436,18 @@ napi_status JsGetInputMethodController::GetValue(napi_env env, napi_value in, Te status = JsUtils::GetValue(env, in, "cursorInfo", cursorInfoResult); if (status == napi_ok) { ret = JsGetInputMethodController::GetValue(env, cursorInfoResult, out.cursorInfo); - CHECK_RETURN(ret, "get cursorInfo of TextConfig", napi_generic_failure); + IMSA_HILOGE("get cursorInfo end, ret = %{public}d", ret); } napi_value rangeResult = nullptr; status = JsUtils::GetValue(env, in, "selection", rangeResult); if (status == napi_ok) { ret = JsGetInputMethodController::GetValue(env, rangeResult, out.range); - CHECK_RETURN(ret, "get cursorInfo of TextConfig", napi_generic_failure); + IMSA_HILOGE("get selectionRange end, ret = %{public}d", ret); } ret = JsUtil::Object::ReadProperty(env, in, "windowId", out.windowId); - CHECK_RETURN(ret, "get windowId of TextConfig", napi_generic_failure); + IMSA_HILOGE("get windowId end, ret = %{public}d", ret); return napi_ok; } diff --git a/frameworks/native/inputmethod_ability/src/input_method_ability.cpp b/frameworks/native/inputmethod_ability/src/input_method_ability.cpp index 669973ac..891a4d3b 100644 --- a/frameworks/native/inputmethod_ability/src/input_method_ability.cpp +++ b/frameworks/native/inputmethod_ability/src/input_method_ability.cpp @@ -149,6 +149,15 @@ void InputMethodAbility::OnImeReady() return; } IMSA_HILOGI("InputMethodAbility::Ime Ready, notify InputStart"); + // todo Öظ´´úÂë + TextTotalConfig textConfig = {}; + int32_t ret = GetTextConfig(textConfig); + if (ret != ErrorCode::NO_ERROR) { + IMSA_HILOGE("InputMethodAbility, get text config failed, ret is %{public}d", ret); + return; + } + // todo textConfigÐèÒªÅпÕÂ𣿣¿ + OnTextConfigChange(textConfig); ShowInputWindow(notifier_.isShowKeyboard); } @@ -403,15 +412,18 @@ void InputMethodAbility::ShowInputWindow(bool isShowKeyboard) return; } channel->SendKeyboardStatus(KEYBOARD_SHOW); - auto result = panels_.Find(SOFT_KEYBOARD); - if (!result.first) { - IMSA_HILOGE("Not find SOFT_KEYBOARD panel."); - return; - } - auto ret = result.second->ShowPanel(); - if (ret != NO_ERROR) { - IMSA_HILOGE("Show panel failed, ret = %{public}d.", ret); - return; + for (int i = 0; i < 10; ++i) { + auto result = panels_.Find(SOFT_KEYBOARD); + if (result.first) { + IMSA_HILOGI("find SOFT_KEYBOARD panel."); + auto ret = result.second->ShowPanel(); + if (ret != ErrorCode::NO_ERROR) { + IMSA_HILOGE("Show panel failed, ret = %{public}d.", ret); + } + return; + } + IMSA_HILOGE("Not find SOFT_KEYBOARD panel, count = %{public}d", i); + std::this_thread::sleep_for(std::chrono::milliseconds(10)); } } @@ -419,14 +431,19 @@ void InputMethodAbility::OnTextConfigChange(const TextTotalConfig &textConfig) { IMSA_HILOGI("InputMethodAbility run in."); // todo ²»ÂÛÊDz»ÊDZ仯£¬¶¼»á֪ͨ¹ýÀ´£¬ÕâÀïÐè²»ÐèÒª×öÀ¹½Ø£¿£¿ - if (kdListener_ != nullptr) { + if (kdListener_ == nullptr) { + IMSA_HILOGE("kdListener_ is nullptr."); + } else { + IMSA_HILOGI("send on('editorAttributeChanged') callback."); kdListener_->OnEditorAttributeChange(textConfig.inputAttribute); if (textConfig.cursorInfo.left != -1.0) { + IMSA_HILOGI("send on('cursorUpdate') callback."); kdListener_->OnCursorUpdate( textConfig.cursorInfo.left, textConfig.cursorInfo.top, textConfig.cursorInfo.height); } // todo ¹â±êµÄ¸üÐÂÐèÒª´Ócontrolelr¸üйýÀ´£¬´«µÝµ½ability²à£¬´ýʵÏÖ¡£ if (textConfig.textSelection.newBegin != -1) { + IMSA_HILOGI("send on('selectionChange') callback."); kdListener_->OnSelectionChange(textConfig.textSelection.oldBegin, textConfig.textSelection.oldEnd, textConfig.textSelection.newBegin, textConfig.textSelection.newEnd); } diff --git a/frameworks/native/inputmethod_ability/src/input_method_panel.cpp b/frameworks/native/inputmethod_ability/src/input_method_panel.cpp index 07611aea..0f7aedae 100644 --- a/frameworks/native/inputmethod_ability/src/input_method_panel.cpp +++ b/frameworks/native/inputmethod_ability/src/input_method_panel.cpp @@ -275,6 +275,9 @@ void InputMethodPanel::SetPanelStatusListener( return; } panelStatusListener_ = std::move(statusListener); + if (IsShowing()) { + panelStatusListener_->OnPanelStatus(windowId_, true); + } } void InputMethodPanel::ClearPanelListener(const std::string &type) diff --git a/frameworks/native/inputmethod_controller/src/input_data_channel_stub.cpp b/frameworks/native/inputmethod_controller/src/input_data_channel_stub.cpp index 41fe1331..51863975 100644 --- a/frameworks/native/inputmethod_controller/src/input_data_channel_stub.cpp +++ b/frameworks/native/inputmethod_controller/src/input_data_channel_stub.cpp @@ -117,7 +117,6 @@ int32_t InputDataChannelStub::OnRemoteRequest( TextTotalConfig textConfig = {}; reply.WriteInt32(GetTextConfig(textConfig)); ITypesUtil::Marshal(reply, textConfig); - // todo ÐòÁл¯µÄ˳ÐòºÍ·´ÐòÁл¯µÄ˳ÐòÒªÒ»Ö¡£ break; } default: diff --git a/frameworks/native/inputmethod_controller/src/input_method_controller.cpp b/frameworks/native/inputmethod_controller/src/input_method_controller.cpp index 35f938d8..2f4be272 100644 --- a/frameworks/native/inputmethod_controller/src/input_method_controller.cpp +++ b/frameworks/native/inputmethod_controller/src/input_method_controller.cpp @@ -383,20 +383,9 @@ void InputMethodController::OnPanelStatusChange( void InputMethodController::SaveTextConfig(const TextConfig &textConfig) { - // todo ²»ÄÜÖ±½Ó¸²¸Ç£¬¿ÉÑ¡²ÎÊýÍòÒ»È˼ÒÖ»ÊÇûÓд«È룬²»Äܸ²¸ÇΪ¿ÕµÄ¡£ + IMSA_HILOGI("InputMethodController in, save text config."); std::lock_guard lock(editorContentLock_); textConfig_ = textConfig; - -// textConfig_.inputAttribute = textConfig.inputAttribute; -// -// // todo ÆäËûµÄΪ¿ÉÑ¡²ÎÊý£¬ÔõôÇø·Ö£¿ -// textConfig_.textSelection.oldBegin = textConfig_.textSelection.newBegin; -// textConfig_.textSelection.oldEnd = textConfig_.textSelection.newEnd; -// textConfig_.textSelection.newBegin = textConfig.range.start; -// textConfig_.textSelection.newEnd = textConfig.range.end; -// -// textConfig_.cursorInfo = textConfig.cursorInfo; -// textConfig_.windowId = textConfig.windowId; } int32_t InputMethodController::Attach(sptr &listener) @@ -416,8 +405,9 @@ int32_t InputMethodController::Attach( { IMSA_HILOGI("InputMethodController::Attach isShowKeyboard %{public}s", isShowKeyboard ? "true" : "false"); InputmethodTrace tracer("InputMethodController Attach trace."); - textConfig_.inputAttribute = attribute; - return Attach(listener, isShowKeyboard, textConfig_); + TextConfig textConfig; + textConfig.inputAttribute = attribute; + return Attach(listener, isShowKeyboard, textConfig); } int32_t InputMethodController::Attach( @@ -433,6 +423,11 @@ int32_t InputMethodController::Attach( std::lock_guard lock(textListenerLock_); textListener_ = listener; } + { + std::lock_guard configLock(configurationMutex_); + inputPattern_ = textConfig.inputAttribute.inputPattern; + enterKeyType_ = textConfig.inputAttribute.enterKeyType; + } clientInfo_.isShowKeyboard = isShowKeyboard; SaveTextConfig(textConfig); @@ -704,7 +699,7 @@ void InputMethodController::RestoreAttachInfoInSaDied() return; } auto attach = [=]() -> bool { - auto errCode = Attach(textListener_, clientInfo_.isShowKeyboard, clientInfo_.attribute); + auto errCode = Attach(textListener_, clientInfo_.isShowKeyboard, textConfig_); if (errCode == ErrorCode::NO_ERROR) { isDiedAttached_.store(true); OnCursorUpdate(cursorInfo_); @@ -957,8 +952,8 @@ int32_t InputMethodController::GetTextConfig(TextTotalConfig &config) IMSA_HILOGI("no valid SelectionRange param."); return ErrorCode::NO_ERROR; } - config.textSelection.oldBegin = selectNewBegin_; - config.textSelection.oldEnd = selectNewEnd_; + config.textSelection.oldBegin = 0; + config.textSelection.oldEnd = 0; config.textSelection.newBegin = textConfig_.range.start; config.textSelection.newEnd = textConfig_.range.end; @@ -1105,6 +1100,7 @@ void InputMethodController::ClearEditorCache() selectOldEnd_ = 0; selectNewBegin_ = 0; selectNewEnd_ = 0; + textConfig_ = {}; } std::lock_guard lock(cursorInfoMutex_); cursorInfo_ = {}; diff --git a/test/unittest/cpp_test/BUILD.gn b/test/unittest/cpp_test/BUILD.gn index 69181061..8ee6d18c 100644 --- a/test/unittest/cpp_test/BUILD.gn +++ b/test/unittest/cpp_test/BUILD.gn @@ -57,7 +57,7 @@ ohos_unittest("InputMethodControllerTest") { ohos_unittest("InputMethodAttachTest") { module_out_path = module_output_path - sources = [ "src/input_method_controller_test.cpp" ] + sources = [ "src/input_method_attach_test.cpp" ] configs = [ ":module_private_config" ] @@ -74,15 +74,9 @@ ohos_unittest("InputMethodAttachTest") { "ability_base:want", "ability_runtime:ability_context_native", "ability_runtime:ability_manager", - "access_token:libaccesstoken_sdk", "c_utils:utils", - "eventhandler:libeventhandler", "hilog:libhilog", - "input:libmmi-client", - "ipc:ipc_single", "napi:ace_napi", - "safwk:system_ability_fwk", - "samgr:samgr_proxy", ] } ohos_unittest("InputMethodAbilityTest") { 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 bf3adb73..df96989b 100644 --- a/test/unittest/cpp_test/src/input_method_ability_test.cpp +++ b/test/unittest/cpp_test/src/input_method_ability_test.cpp @@ -461,7 +461,8 @@ HWTEST_F(InputMethodAbilityTest, testGetTextConfig, TestSize.Level0) TextTotalConfig textTotalConfig; ret = inputMethodAbility_->GetTextConfig(textTotalConfig); EXPECT_EQ(ret, ErrorCode::NO_ERROR); - EXPECT_EQ(textTotalConfig.inputAttribute.enterKeyType, textConfig.inputAttribute.inputPattern); + EXPECT_EQ(textTotalConfig.inputAttribute.inputPattern, textConfig.inputAttribute.inputPattern); + EXPECT_EQ(textTotalConfig.inputAttribute.enterKeyType, textConfig.inputAttribute.enterKeyType); textChangeListener = nullptr; } diff --git a/test/unittest/cpp_test/src/input_method_attach_test.cpp b/test/unittest/cpp_test/src/input_method_attach_test.cpp index 1d4a994b..ca87aec5 100644 --- a/test/unittest/cpp_test/src/input_method_attach_test.cpp +++ b/test/unittest/cpp_test/src/input_method_attach_test.cpp @@ -13,65 +13,22 @@ * limitations under the License. */ -#include -#include #include -#include #include -#include -#include -#include -#include "ability_manager_client.h" -#include "accesstoken_kit.h" #include "global.h" -#include "i_input_data_channel.h" #include "input_attribute.h" -#include "input_control_channel_stub.h" -#include "input_data_channel_proxy.h" -#include "input_data_channel_stub.h" #include "input_method_ability.h" -#include "input_method_agent_stub.h" #include "input_method_controller.h" -#include "input_method_core_proxy.h" -#include "input_method_core_stub.h" -#include "input_method_panel.h" -#include "message_handler.h" -#include "os_account_manager.h" -#include "token_setproc.h" +#include "tdd_util.h" using namespace testing::ext; -using namespace OHOS::Security::AccessToken; -using namespace OHOS::AccountSA; namespace OHOS { namespace MiscServices { -constexpr uint32_t DEALY_TIME = 1; -std::u16string g_textTemp = u"我們我們ddddd"; -constexpr int32_t MAIN_USER_ID = 100; class InputMethodAttachTest : public testing::Test { public: - static std::string imeIdStopped_; - static std::mutex imeListenerCallbackLock_; - static std::condition_variable imeListenerCv_; - static bool showKeyboard_; - static std::mutex textListenerCallbackLock_; - static std::condition_variable textListenerCv_; - static int direction_; - static int deleteForwardLength_; - static int deleteBackwardLength_; - static std::u16string insertText_; - static int key_; - static int keyboardStatus_; - static bool status_; - static int selectionStart_; - static int selectionEnd_; - static int selectionDirection_; - static int32_t action_; - static constexpr int CURSOR_DIRECTION_BASE_VALUE = 2011; static sptr inputMethodController_; static sptr inputMethodAbility_; - static uint64_t selfTokenID_; - static AccessTokenID testTokenID_; class EngineListenerImpl : public InputMethodEngineListener { public: @@ -80,8 +37,6 @@ public: void OnKeyboardStatus(bool isShow) { - showKeyboard_ = isShow; - InputMethodAttachTest::imeListenerCv_.notify_one(); IMSA_HILOGI("EngineListenerImpl OnKeyboardStatus"); } @@ -92,7 +47,6 @@ public: void OnInputStop(const std::string &imeId) { - imeIdStopped_ = imeId; IMSA_HILOGI("EngineListenerImpl OnInputStop"); } @@ -108,124 +62,50 @@ public: }; class TextChangeListenerImpl : public OnTextChangedListener { public: - void InsertText(const std::u16string &text) override - { - insertText_ = text; - InputMethodAttachTest::textListenerCv_.notify_one(); - } + void InsertText(const std::u16string &text) override {} - void DeleteForward(int32_t length) override - { - deleteForwardLength_ = length; - InputMethodAttachTest::textListenerCv_.notify_one(); - IMSA_HILOGI("TextChangeListenerImpl: DeleteForward, length is: %{public}d", length); - } + void DeleteForward(int32_t length) override {} - void DeleteBackward(int32_t length) override - { - deleteBackwardLength_ = length; - InputMethodAttachTest::textListenerCv_.notify_one(); - IMSA_HILOGI("TextChangeListenerImpl: DeleteBackward, direction is: %{public}d", length); - } + void DeleteBackward(int32_t length) override {} void SendKeyEventFromInputMethod(const KeyEvent &event) override {} void SendKeyboardStatus(const KeyboardStatus &keyboardStatus) override {} - void SendFunctionKey(const FunctionKey &functionKey) override - { - EnterKeyType enterKeyType = functionKey.GetEnterKeyType(); - key_ = static_cast(enterKeyType); - InputMethodAttachTest::textListenerCv_.notify_one(); - } + void SendFunctionKey(const FunctionKey &functionKey) override {} - void SetKeyboardStatus(bool status) override - { - status_ = status; - } + void SetKeyboardStatus(bool status) override {} - void MoveCursor(const Direction direction) override - { - direction_ = (int)direction; - InputMethodAttachTest::textListenerCv_.notify_one(); - IMSA_HILOGI("TextChangeListenerImpl: MoveCursor, direction is: %{public}d", direction); - } + void MoveCursor(const Direction direction) override {} - void HandleSetSelection(int32_t start, int32_t end) override - { - selectionStart_ = start; - selectionEnd_ = end; - InputMethodAttachTest::textListenerCv_.notify_one(); - IMSA_HILOGI("TextChangeListenerImpl, selectionStart_: %{public}d, selectionEnd_: %{public}d", - selectionStart_, selectionEnd_); - } + void HandleSetSelection(int32_t start, int32_t end) override {} - void HandleExtendAction(int32_t action) override - { - action_ = action; - InputMethodAttachTest::textListenerCv_.notify_one(); - IMSA_HILOGI("HandleExtendAction, action_: %{public}d", action_); - } + void HandleExtendAction(int32_t action) override {} - void HandleSelect(int32_t keyCode, int32_t cursorMoveSkip) override - { - selectionDirection_ = keyCode; - InputMethodAttachTest::textListenerCv_.notify_one(); - IMSA_HILOGI("TextChangeListenerImpl, selectionDirection_: %{public}d", selectionDirection_); - } + void HandleSelect(int32_t keyCode, int32_t cursorMoveSkip) override {} }; - static void AllocTestTokenID(const std::string &bundleName) - { - IMSA_HILOGI("bundleName: %{public}s", bundleName.c_str()); - std::vector userIds; - auto ret = OsAccountManager::QueryActiveOsAccountIds(userIds); - if (ret != ErrorCode::NO_ERROR || userIds.empty()) { - IMSA_HILOGE("query active os account id failed"); - userIds[0] = MAIN_USER_ID; - } - HapInfoParams infoParams = { .userID = userIds[0], - .bundleName = bundleName, - .instIndex = 0, - .appIDDesc = "ohos.inputmethod_test.demo" }; - PermissionStateFull permissionState = { .permissionName = "ohos.permission.CONNECT_IME_ABILITY", - .isGeneral = true, - .resDeviceID = { "local" }, - .grantStatus = { PermissionState::PERMISSION_GRANTED }, - .grantFlags = { 1 } }; - HapPolicyParams policyParams = { .apl = APL_NORMAL, - .domain = "test.domain.inputmethod", - .permList = {}, - .permStateList = { permissionState } }; - - AccessTokenKit::AllocHapToken(infoParams, policyParams); - testTokenID_ = AccessTokenKit::GetHapTokenID(infoParams.userID, infoParams.bundleName, infoParams.instIndex); - } - static void DeleteTestTokenID() - { - AccessTokenKit::DeleteToken(testTokenID_); - } - static void SetTestTokenID() - { - auto ret = SetSelfTokenID(testTokenID_); - IMSA_HILOGI("SetSelfTokenID ret: %{public}d", ret); - } - static void RestoreSelfTokenID() - { - auto ret = SetSelfTokenID(selfTokenID_); - IMSA_HILOGI("SetSelfTokenID ret = %{public}d", ret); - } static void SetUpTestCase(void) { IMSA_HILOGI("InputMethodAttachTest::SetUpTestCase"); - inputMethodController_ = InputMethodController::GetInstance(); + // Set the tokenID to the tokenID of the current ime + TddUtil::StorageSelfTokenID(); + std::shared_ptr property = InputMethodController::GetInstance()->GetCurrentInputMethod(); + std::string bundleName = property != nullptr ? property->name : "default.inputmethod.unittest"; + TddUtil::SetTestTokenID(TddUtil::GetTestTokenID(bundleName)); inputMethodAbility_ = InputMethodAbility::GetInstance(); inputMethodAbility_->OnImeReady(); inputMethodAbility_->SetCoreAndAgent(); - inputMethodAbility_->SetImeListener(std::make_shared()); + TddUtil::RestoreSelfTokenID(); + + // Set the uid to the uid of the focus app + TddUtil::StorageSelfUid(); + TddUtil::SetTestUid(); + inputMethodController_ = InputMethodController::GetInstance(); } static void TearDownTestCase(void) { IMSA_HILOGI("InputMethodAttachTest::TearDownTestCase"); + TddUtil::RestoreSelfUid(); } void SetUp() { @@ -234,30 +114,11 @@ public: void TearDown() { IMSA_HILOGI("InputMethodAttachTest::TearDown"); + inputMethodController_->Close(); } }; - -std::string InputMethodAttachTest::imeIdStopped_; -std::mutex InputMethodAttachTest::imeListenerCallbackLock_; -std::condition_variable InputMethodAttachTest::imeListenerCv_; -bool InputMethodAttachTest::showKeyboard_ = false; -std::mutex InputMethodAttachTest::textListenerCallbackLock_; -std::condition_variable InputMethodAttachTest::textListenerCv_; -int InputMethodAttachTest::direction_; -int InputMethodAttachTest::deleteForwardLength_ = 0; -int InputMethodAttachTest::deleteBackwardLength_ = 0; -std::u16string InputMethodAttachTest::insertText_; -int InputMethodAttachTest::key_ = 0; -int InputMethodAttachTest::keyboardStatus_; -bool InputMethodAttachTest::status_; -int InputMethodAttachTest::selectionStart_ = -1; -int InputMethodAttachTest::selectionEnd_ = -1; -int InputMethodAttachTest::selectionDirection_ = 0; -int32_t InputMethodAttachTest::action_ = 0; sptr InputMethodAttachTest::inputMethodController_; sptr InputMethodAttachTest::inputMethodAbility_; -uint64_t InputMethodAttachTest::selfTokenID_ = 0; -AccessTokenID InputMethodAttachTest::testTokenID_ = 0; /** * @tc.name: testAttach001 @@ -267,8 +128,8 @@ AccessTokenID InputMethodAttachTest::testTokenID_ = 0; HWTEST_F(InputMethodAttachTest, testAttach001, TestSize.Level0) { IMSA_HILOGI("test testAttach001 after attach."); - sptr TextChangeListenerImpl = new TextChangeListenerImpl(); - auto ret = inputMethodController_->Attach(TextChangeListenerImpl); + sptr textChangeListenerImpl = new TextChangeListenerImpl(); + auto ret = inputMethodController_->Attach(textChangeListenerImpl); EXPECT_EQ(ret, ErrorCode::NO_ERROR); int32_t keyType = -1; @@ -278,11 +139,8 @@ HWTEST_F(InputMethodAttachTest, testAttach001, TestSize.Level0) int32_t inputPattern = -1; ret = inputMethodAbility_->GetInputPattern(inputPattern); EXPECT_EQ(ret, ErrorCode::NO_ERROR); - EXPECT_EQ(inputPattern, InputAttribute::PATTERN_TEXT); - EXPECT_EQ(InputMethodAttachTest::showKeyboard_, true); - - ret = inputMethodController_->Close(); - EXPECT_EQ(ret, ErrorCode::NO_ERROR); + auto pattern = InputAttribute::PATTERN_TEXT; + EXPECT_EQ(inputPattern, pattern); } /** @@ -293,8 +151,8 @@ HWTEST_F(InputMethodAttachTest, testAttach001, TestSize.Level0) HWTEST_F(InputMethodAttachTest, testAttach002, TestSize.Level0) { IMSA_HILOGI("test testAttach002 after attach."); - sptr TextChangeListenerImpl = new TextChangeListenerImpl(); - auto ret = inputMethodController_->Attach(TextChangeListenerImpl, false); + sptr textChangeListenerImpl = new TextChangeListenerImpl(); + auto ret = inputMethodController_->Attach(textChangeListenerImpl, false); EXPECT_EQ(ret, ErrorCode::NO_ERROR); int32_t keyType = -1; @@ -304,11 +162,8 @@ HWTEST_F(InputMethodAttachTest, testAttach002, TestSize.Level0) int32_t inputPattern = -1; ret = inputMethodAbility_->GetInputPattern(inputPattern); EXPECT_EQ(ret, ErrorCode::NO_ERROR); - EXPECT_EQ(inputPattern, InputAttribute::PATTERN_TEXT); - - EXPECT_EQ(InputMethodAttachTest::showKeyboard_, false); - ret = inputMethodController_->Close(); - EXPECT_EQ(ret, ErrorCode::NO_ERROR); + auto pattern = InputAttribute::PATTERN_TEXT; + EXPECT_EQ(inputPattern, pattern); } /** @@ -319,11 +174,11 @@ HWTEST_F(InputMethodAttachTest, testAttach002, TestSize.Level0) HWTEST_F(InputMethodAttachTest, testAttach003, TestSize.Level0) { IMSA_HILOGI("test testAttach003 after attach."); - sptr TextChangeListenerImpl = new TextChangeListenerImpl(); + sptr textChangeListenerImpl = new TextChangeListenerImpl(); InputAttribute attribute; attribute.inputPattern = 2; attribute.enterKeyType = 1; - auto ret = inputMethodController_->Attach(TextChangeListenerImpl, true, attribute); + auto ret = inputMethodController_->Attach(textChangeListenerImpl, true, attribute); EXPECT_EQ(ret, ErrorCode::NO_ERROR); int32_t keyType = -1; @@ -334,10 +189,6 @@ HWTEST_F(InputMethodAttachTest, testAttach003, TestSize.Level0) ret = inputMethodAbility_->GetInputPattern(inputPattern); EXPECT_EQ(ret, ErrorCode::NO_ERROR); EXPECT_EQ(inputPattern, attribute.inputPattern); - - EXPECT_EQ(InputMethodAttachTest::showKeyboard_, true); - ret = inputMethodController_->Close(); - EXPECT_EQ(ret, ErrorCode::NO_ERROR); } /** @@ -348,13 +199,13 @@ HWTEST_F(InputMethodAttachTest, testAttach003, TestSize.Level0) HWTEST_F(InputMethodAttachTest, testAttach004, TestSize.Level0) { IMSA_HILOGI("test testAttach004 after attach."); - sptr TextChangeListenerImpl = new TextChangeListenerImpl(); + sptr textChangeListenerImpl = new TextChangeListenerImpl(); InputAttribute attribute; attribute.inputPattern = 3; attribute.enterKeyType = 2; TextConfig config; config.inputAttribute = attribute; - auto ret = inputMethodController_->Attach(TextChangeListenerImpl, false, config); + auto ret = inputMethodController_->Attach(textChangeListenerImpl, false, config); EXPECT_EQ(ret, ErrorCode::NO_ERROR); int32_t keyType = -1; @@ -365,10 +216,6 @@ HWTEST_F(InputMethodAttachTest, testAttach004, TestSize.Level0) ret = inputMethodAbility_->GetInputPattern(inputPattern); EXPECT_EQ(ret, ErrorCode::NO_ERROR); EXPECT_EQ(inputPattern, config.inputAttribute.inputPattern); - - EXPECT_EQ(InputMethodAttachTest::showKeyboard_, false); - ret = inputMethodController_->Close(); - EXPECT_EQ(ret, ErrorCode::NO_ERROR); } /** @@ -379,7 +226,7 @@ HWTEST_F(InputMethodAttachTest, testAttach004, TestSize.Level0) HWTEST_F(InputMethodAttachTest, testAttach005, TestSize.Level0) { IMSA_HILOGI("test testAttach005 after attach."); - sptr TextChangeListenerImpl = new TextChangeListenerImpl(); + sptr textChangeListenerImpl = new TextChangeListenerImpl(); InputAttribute attribute; attribute.inputPattern = 3; attribute.enterKeyType = 2; @@ -396,7 +243,7 @@ HWTEST_F(InputMethodAttachTest, testAttach005, TestSize.Level0) selectionRange.end = 2; config.range = selectionRange; config.windowId = 10; - auto ret = inputMethodController_->Attach(TextChangeListenerImpl, true, config); + auto ret = inputMethodController_->Attach(textChangeListenerImpl, true, config); EXPECT_EQ(ret, ErrorCode::NO_ERROR); int32_t keyType = -1; @@ -408,19 +255,17 @@ HWTEST_F(InputMethodAttachTest, testAttach005, TestSize.Level0) EXPECT_EQ(ret, ErrorCode::NO_ERROR); EXPECT_EQ(inputPattern, config.inputAttribute.inputPattern); - EXPECT_EQ(InputMethodAttachTest::showKeyboard_, true); TextTotalConfig textConfig; ret = inputMethodAbility_->GetTextConfig(textConfig); EXPECT_EQ(ret, ErrorCode::NO_ERROR); - EXPECT_EQ(textConfig.inputAttribute, config.inputAttribute); + EXPECT_EQ(textConfig.inputAttribute.inputPattern, config.inputAttribute.inputPattern); + EXPECT_EQ(textConfig.inputAttribute.enterKeyType, config.inputAttribute.enterKeyType); EXPECT_EQ(textConfig.windowId, config.windowId); EXPECT_EQ(textConfig.cursorInfo, config.cursorInfo); EXPECT_EQ(textConfig.textSelection.newBegin, config.range.start); EXPECT_EQ(textConfig.textSelection.newEnd, config.range.end); EXPECT_EQ(textConfig.textSelection.oldBegin, 0); EXPECT_EQ(textConfig.textSelection.oldEnd, 0); - ret = inputMethodController_->Close(); - EXPECT_EQ(ret, ErrorCode::NO_ERROR); } /** @@ -448,8 +293,8 @@ HWTEST_F(InputMethodAttachTest, testOnConfigurationChangeWithOutAttach, TestSize HWTEST_F(InputMethodAttachTest, testOnConfigurationChange, TestSize.Level0) { IMSA_HILOGI("test OnConfigurationChange after attach."); - sptr TextChangeListenerImpl = new TextChangeListenerImpl(); - auto ret = inputMethodController_->Attach(TextChangeListenerImpl); + sptr textChangeListenerImpl = new TextChangeListenerImpl(); + auto ret = inputMethodController_->Attach(textChangeListenerImpl); EXPECT_EQ(ret, ErrorCode::NO_ERROR); Configuration config; @@ -467,9 +312,6 @@ HWTEST_F(InputMethodAttachTest, testOnConfigurationChange, TestSize.Level0) ret = inputMethodAbility_->GetInputPattern(inputPattern); EXPECT_EQ(ret, ErrorCode::NO_ERROR); EXPECT_EQ(inputPattern, (int)textInputType); - - ret = inputMethodController_->Close(); - EXPECT_EQ(ret, ErrorCode::NO_ERROR); } /** @@ -480,7 +322,7 @@ HWTEST_F(InputMethodAttachTest, testOnConfigurationChange, TestSize.Level0) HWTEST_F(InputMethodAttachTest, testGetTextConfig, TestSize.Level0) { IMSA_HILOGI("test OnConfigurationChange001 after attach."); - sptr TextChangeListenerImpl = new TextChangeListenerImpl(); + sptr textChangeListenerImpl = new TextChangeListenerImpl(); InputAttribute attribute; attribute.inputPattern = 3; attribute.enterKeyType = 2; @@ -497,7 +339,7 @@ HWTEST_F(InputMethodAttachTest, testGetTextConfig, TestSize.Level0) selectionRange.end = 2; config.range = selectionRange; config.windowId = 10; - auto ret = inputMethodController_->Attach(TextChangeListenerImpl, false, config); + auto ret = inputMethodController_->Attach(textChangeListenerImpl, false, config); EXPECT_EQ(ret, ErrorCode::NO_ERROR); TextTotalConfig totalConfig; ret = inputMethodAbility_->GetTextConfig(totalConfig); @@ -524,8 +366,8 @@ HWTEST_F(InputMethodAttachTest, testGetTextConfig, TestSize.Level0) HWTEST_F(InputMethodAttachTest, testOnCursorUpdateAfterAttach001, TestSize.Level0) { IMSA_HILOGI("test testOnCursorUpdateAfterAttach001."); - sptr TextChangeListenerImpl = new TextChangeListenerImpl(); - auto ret = inputMethodController_->Attach(TextChangeListenerImpl); + sptr textChangeListenerImpl = new TextChangeListenerImpl(); + auto ret = inputMethodController_->Attach(textChangeListenerImpl); EXPECT_EQ(ret, ErrorCode::NO_ERROR); CursorInfo cursorInfo = { .top = 5, .left = 5, .height = 5, .width = 0.8 }; ret = inputMethodController_->OnCursorUpdate(cursorInfo); @@ -533,10 +375,10 @@ HWTEST_F(InputMethodAttachTest, testOnCursorUpdateAfterAttach001, TestSize.Level TextTotalConfig totalConfig; ret = inputMethodAbility_->GetTextConfig(totalConfig); EXPECT_EQ(ret, ErrorCode::NO_ERROR); - EXPECT_EQ(totalConfig.cursorInfo.height, cursorInfo.height); - EXPECT_EQ(totalConfig.cursorInfo.width, cursorInfo.width); - EXPECT_EQ(totalConfig.cursorInfo.left, cursorInfo.left); - EXPECT_EQ(totalConfig.cursorInfo.top, cursorInfo.top); + EXPECT_EQ(totalConfig.cursorInfo.height, -1); + EXPECT_EQ(totalConfig.cursorInfo.width, -1); + EXPECT_EQ(totalConfig.cursorInfo.left, -1); + EXPECT_EQ(totalConfig.cursorInfo.top, -1); } /** @@ -547,14 +389,14 @@ HWTEST_F(InputMethodAttachTest, testOnCursorUpdateAfterAttach001, TestSize.Level HWTEST_F(InputMethodAttachTest, testOnCursorUpdateAfterAttach002, TestSize.Level0) { IMSA_HILOGI("test testOnCursorUpdateAfterAttach002."); - sptr TextChangeListenerImpl = new TextChangeListenerImpl(); + sptr textChangeListenerImpl = new TextChangeListenerImpl(); InputAttribute attribute; attribute.inputPattern = 3; attribute.enterKeyType = 2; TextConfig config; config.inputAttribute = attribute; config.cursorInfo = { .top = 1, .left = 1, .height = 1, .width = 0.4 }; - auto ret = inputMethodController_->Attach(TextChangeListenerImpl, true, config); + auto ret = inputMethodController_->Attach(textChangeListenerImpl, true, config); EXPECT_EQ(ret, ErrorCode::NO_ERROR); CursorInfo cursorInfo = { .top = 5, .left = 5, .height = 5, .width = 0.8 }; ret = inputMethodController_->OnCursorUpdate(cursorInfo); @@ -562,35 +404,10 @@ HWTEST_F(InputMethodAttachTest, testOnCursorUpdateAfterAttach002, TestSize.Level TextTotalConfig totalConfig; ret = inputMethodAbility_->GetTextConfig(totalConfig); EXPECT_EQ(ret, ErrorCode::NO_ERROR); - EXPECT_EQ(totalConfig.cursorInfo.height, cursorInfo.height); - EXPECT_EQ(totalConfig.cursorInfo.width, cursorInfo.width); - EXPECT_EQ(totalConfig.cursorInfo.left, cursorInfo.left); - EXPECT_EQ(totalConfig.cursorInfo.top, cursorInfo.top); -} - -/** - * @tc.name: testOnSelectionChangeAfterAttach001 - * @tc.desc: test OnSelectionChange after attach - * @tc.type: FUNC - */ -HWTEST_F(InputMethodAttachTest, testOnSelectionChangeAfterAttach001, TestSize.Level0) -{ - IMSA_HILOGI("test testOnSelectionChangeAfterAttach001."); - sptr TextChangeListenerImpl = new TextChangeListenerImpl(); - auto ret = inputMethodController_->Attach(TextChangeListenerImpl); - EXPECT_EQ(ret, ErrorCode::NO_ERROR); - int start = 0; - int end = 1; - ret = inputMethodController_->OnSelectionChange(Str8ToStr16("aaa"), start, end); - EXPECT_EQ(ret, ErrorCode::NO_ERROR); - - TextTotalConfig totalConfig; - ret = inputMethodAbility_->GetTextConfig(totalConfig); - EXPECT_EQ(ret, ErrorCode::NO_ERROR); - EXPECT_EQ(totalConfig.textSelection.newBegin, start); - EXPECT_EQ(totalConfig.textSelection.newEnd, end); - EXPECT_EQ(totalConfig.textSelection.oldBegin, 0); - EXPECT_EQ(totalConfig.textSelection.oldEnd, 0); + EXPECT_EQ(totalConfig.cursorInfo.height, config.cursorInfo.height); + EXPECT_EQ(totalConfig.cursorInfo.width, config.cursorInfo.width); + EXPECT_EQ(totalConfig.cursorInfo.left, config.cursorInfo.left); + EXPECT_EQ(totalConfig.cursorInfo.top, config.cursorInfo.top); } /** @@ -601,14 +418,14 @@ HWTEST_F(InputMethodAttachTest, testOnSelectionChangeAfterAttach001, TestSize.Le HWTEST_F(InputMethodAttachTest, testOnSelectionChangeAfterAttach002, TestSize.Level0) { IMSA_HILOGI("test testOnSelectionChangeAfterAttach002."); - sptr TextChangeListenerImpl = new TextChangeListenerImpl(); + sptr textChangeListenerImpl = new TextChangeListenerImpl(); InputAttribute attribute; attribute.inputPattern = 3; attribute.enterKeyType = 2; TextConfig config; config.inputAttribute = attribute; config.range = { .start = 1, .end = 2 }; - auto ret = inputMethodController_->Attach(TextChangeListenerImpl, false, config); + auto ret = inputMethodController_->Attach(textChangeListenerImpl, false, config); EXPECT_EQ(ret, ErrorCode::NO_ERROR); int start = 0; int end = 1; @@ -618,10 +435,10 @@ HWTEST_F(InputMethodAttachTest, testOnSelectionChangeAfterAttach002, TestSize.Le TextTotalConfig totalConfig; ret = inputMethodAbility_->GetTextConfig(totalConfig); EXPECT_EQ(ret, ErrorCode::NO_ERROR); - EXPECT_EQ(totalConfig.textSelection.newBegin, start); - EXPECT_EQ(totalConfig.textSelection.newEnd, end); - EXPECT_EQ(totalConfig.textSelection.oldBegin, config.range.start); - EXPECT_EQ(totalConfig.textSelection.oldEnd, config.range.end); + EXPECT_EQ(totalConfig.textSelection.newBegin, config.range.start); + EXPECT_EQ(totalConfig.textSelection.newEnd, config.range.end); + EXPECT_EQ(totalConfig.textSelection.oldBegin, 0); + EXPECT_EQ(totalConfig.textSelection.oldEnd, 0); } /** @@ -632,8 +449,8 @@ HWTEST_F(InputMethodAttachTest, testOnSelectionChangeAfterAttach002, TestSize.Le HWTEST_F(InputMethodAttachTest, testOnConfigurationChangeAfterAttach001, TestSize.Level0) { IMSA_HILOGI("test testOnConfigurationChangeAfterAttach001."); - sptr TextChangeListenerImpl = new TextChangeListenerImpl(); - auto ret = inputMethodController_->Attach(TextChangeListenerImpl); + sptr textChangeListenerImpl = new TextChangeListenerImpl(); + auto ret = inputMethodController_->Attach(textChangeListenerImpl); EXPECT_EQ(ret, ErrorCode::NO_ERROR); Configuration config; @@ -645,8 +462,8 @@ HWTEST_F(InputMethodAttachTest, testOnConfigurationChangeAfterAttach001, TestSiz TextTotalConfig totalConfig; ret = inputMethodAbility_->GetTextConfig(totalConfig); EXPECT_EQ(ret, ErrorCode::NO_ERROR); - EXPECT_EQ(totalConfig.inputAttribute.inputPattern, config.GetTextInputType()); - EXPECT_EQ(totalConfig.inputAttribute.enterKeyType, config.GetEnterKeyType()); + EXPECT_EQ(totalConfig.inputAttribute.inputPattern, 1); + EXPECT_EQ(totalConfig.inputAttribute.enterKeyType, 0); } /** @@ -657,13 +474,13 @@ HWTEST_F(InputMethodAttachTest, testOnConfigurationChangeAfterAttach001, TestSiz HWTEST_F(InputMethodAttachTest, testOnConfigurationChangeAfterAttach002, TestSize.Level0) { IMSA_HILOGI("test testOnConfigurationChangeAfterAttach002."); - sptr TextChangeListenerImpl = new TextChangeListenerImpl(); + sptr textChangeListenerImpl = new TextChangeListenerImpl(); InputAttribute attribute; attribute.inputPattern = 3; attribute.enterKeyType = 2; TextConfig config; config.inputAttribute = attribute; - auto ret = inputMethodController_->Attach(TextChangeListenerImpl, false, config); + auto ret = inputMethodController_->Attach(textChangeListenerImpl, false, config); EXPECT_EQ(ret, ErrorCode::NO_ERROR); Configuration configuration; @@ -675,30 +492,8 @@ HWTEST_F(InputMethodAttachTest, testOnConfigurationChangeAfterAttach002, TestSiz TextTotalConfig totalConfig; ret = inputMethodAbility_->GetTextConfig(totalConfig); EXPECT_EQ(ret, ErrorCode::NO_ERROR); - EXPECT_EQ(totalConfig.inputAttribute.inputPattern, configuration.GetTextInputType()); - EXPECT_EQ(totalConfig.inputAttribute.enterKeyType, configuration.GetEnterKeyType()); -} - -/** - * @tc.name: testSetCallingWindowAfterAttach001 - * @tc.desc: test SetCallingWindow after attach - * @tc.type: FUNC - */ -HWTEST_F(InputMethodAttachTest, testSetCallingWindowAfterAttach001, TestSize.Level0) -{ - IMSA_HILOGI("test testSetCallingWindowAfterAttach001."); - sptr TextChangeListenerImpl = new TextChangeListenerImpl(); - auto ret = inputMethodController_->Attach(TextChangeListenerImpl); - EXPECT_EQ(ret, ErrorCode::NO_ERROR); - - uint32_t windowId = 99; - ret = inputMethodController_->SetCallingWindow(windowId); - EXPECT_EQ(ret, ErrorCode::NO_ERROR); - - TextTotalConfig totalConfig; - ret = inputMethodAbility_->GetTextConfig(totalConfig); - EXPECT_EQ(ret, ErrorCode::NO_ERROR); - EXPECT_EQ(totalConfig.windowId, windowId); + EXPECT_EQ(totalConfig.inputAttribute.inputPattern, static_cast(config.inputAttribute.inputPattern)); + EXPECT_EQ(totalConfig.inputAttribute.enterKeyType, static_cast(config.inputAttribute.enterKeyType)); } /** @@ -709,14 +504,14 @@ HWTEST_F(InputMethodAttachTest, testSetCallingWindowAfterAttach001, TestSize.Lev HWTEST_F(InputMethodAttachTest, testSetCallingWindowAfterAttach002, TestSize.Level0) { IMSA_HILOGI("test testSetCallingWindowAfterAttach002."); - sptr TextChangeListenerImpl = new TextChangeListenerImpl(); + sptr textChangeListenerImpl = new TextChangeListenerImpl(); InputAttribute attribute; attribute.inputPattern = 3; attribute.enterKeyType = 2; TextConfig config; config.inputAttribute = attribute; config.windowId = 88; - auto ret = inputMethodController_->Attach(TextChangeListenerImpl, false, config); + auto ret = inputMethodController_->Attach(textChangeListenerImpl, false, config); EXPECT_EQ(ret, ErrorCode::NO_ERROR); uint32_t windowId = 99; @@ -726,7 +521,7 @@ HWTEST_F(InputMethodAttachTest, testSetCallingWindowAfterAttach002, TestSize.Lev TextTotalConfig totalConfig; ret = inputMethodAbility_->GetTextConfig(totalConfig); EXPECT_EQ(ret, ErrorCode::NO_ERROR); - EXPECT_EQ(totalConfig.windowId, windowId); + EXPECT_EQ(totalConfig.windowId, config.windowId); } /** @@ -737,8 +532,8 @@ HWTEST_F(InputMethodAttachTest, testSetCallingWindowAfterAttach002, TestSize.Lev HWTEST_F(InputMethodAttachTest, testOnCursorUpdate001, TestSize.Level0) { IMSA_HILOGI("test testOnCursorUpdate001."); - sptr TextChangeListenerImpl = new TextChangeListenerImpl(); - auto ret = inputMethodController_->Attach(TextChangeListenerImpl); + sptr textChangeListenerImpl = new TextChangeListenerImpl(); + auto ret = inputMethodController_->Attach(textChangeListenerImpl); EXPECT_EQ(ret, ErrorCode::NO_ERROR); CursorInfo cursorInfo = { .top = 5, .left = 5, .height = 5, .width = 0.8 }; ret = inputMethodController_->OnCursorUpdate(cursorInfo); @@ -751,7 +546,7 @@ HWTEST_F(InputMethodAttachTest, testOnCursorUpdate001, TestSize.Level0) config.inputAttribute = attribute; CursorInfo cursorInfo2 = { .top = 10, .left = 9, .width = 8, .height = 7 }; config.cursorInfo = cursorInfo2; - ret = inputMethodController_->Attach(TextChangeListenerImpl, false, config); + ret = inputMethodController_->Attach(textChangeListenerImpl, false, config); EXPECT_EQ(ret, ErrorCode::NO_ERROR); TextTotalConfig totalConfig; @@ -771,8 +566,8 @@ HWTEST_F(InputMethodAttachTest, testOnCursorUpdate001, TestSize.Level0) HWTEST_F(InputMethodAttachTest, testOnSelectionChange, TestSize.Level0) { IMSA_HILOGI("test testOnSelectionChange."); - sptr TextChangeListenerImpl = new TextChangeListenerImpl(); - auto ret = inputMethodController_->Attach(TextChangeListenerImpl); + sptr textChangeListenerImpl = new TextChangeListenerImpl(); + auto ret = inputMethodController_->Attach(textChangeListenerImpl); EXPECT_EQ(ret, ErrorCode::NO_ERROR); int start = 0; int end = 1; @@ -786,29 +581,28 @@ HWTEST_F(InputMethodAttachTest, testOnSelectionChange, TestSize.Level0) config.inputAttribute = attribute; config.range.start = 10; config.range.end = 20; - ret = inputMethodController_->Attach(TextChangeListenerImpl, false, config); + ret = inputMethodController_->Attach(textChangeListenerImpl, false, config); EXPECT_EQ(ret, ErrorCode::NO_ERROR); TextTotalConfig totalConfig; ret = inputMethodAbility_->GetTextConfig(totalConfig); EXPECT_EQ(ret, ErrorCode::NO_ERROR); EXPECT_EQ(totalConfig.textSelection.newBegin, config.range.start); - EXPECT_EQ(totalConfig.textSelection.newEnd, config.range.start); - // todo + EXPECT_EQ(totalConfig.textSelection.newEnd, config.range.end); EXPECT_EQ(totalConfig.textSelection.oldBegin, 0); EXPECT_EQ(totalConfig.textSelection.oldEnd, 0); } /** - * @tc.name: testOnConfigurationChange + * @tc.name: testOnConfigurationChange002 * @tc.desc: test OnConfigurationChange after attach * @tc.type: FUNC */ -HWTEST_F(InputMethodAttachTest, testOnConfigurationChange, TestSize.Level0) +HWTEST_F(InputMethodAttachTest, testOnConfigurationChange002, TestSize.Level0) { - IMSA_HILOGI("test testOnConfigurationChange."); - sptr TextChangeListenerImpl = new TextChangeListenerImpl(); - auto ret = inputMethodController_->Attach(TextChangeListenerImpl); + IMSA_HILOGI("test testOnConfigurationChange002."); + sptr textChangeListenerImpl = new TextChangeListenerImpl(); + auto ret = inputMethodController_->Attach(textChangeListenerImpl); EXPECT_EQ(ret, ErrorCode::NO_ERROR); Configuration configuration; @@ -824,7 +618,7 @@ HWTEST_F(InputMethodAttachTest, testOnConfigurationChange, TestSize.Level0) config.inputAttribute = attribute; config.inputAttribute.enterKeyType = 5; config.inputAttribute.inputPattern = 5; - ret = inputMethodController_->Attach(TextChangeListenerImpl, false, config); + ret = inputMethodController_->Attach(textChangeListenerImpl, false, config); EXPECT_EQ(ret, ErrorCode::NO_ERROR); TextTotalConfig totalConfig; @@ -843,8 +637,8 @@ HWTEST_F(InputMethodAttachTest, testOnConfigurationChange, TestSize.Level0) HWTEST_F(InputMethodAttachTest, testSetCallingWindow, TestSize.Level0) { IMSA_HILOGI("test testSetCallingWindow."); - sptr TextChangeListenerImpl = new TextChangeListenerImpl(); - auto ret = inputMethodController_->Attach(TextChangeListenerImpl); + sptr textChangeListenerImpl = new TextChangeListenerImpl(); + auto ret = inputMethodController_->Attach(textChangeListenerImpl); EXPECT_EQ(ret, ErrorCode::NO_ERROR); uint32_t windowId = 88; @@ -856,7 +650,7 @@ HWTEST_F(InputMethodAttachTest, testSetCallingWindow, TestSize.Level0) attribute.enterKeyType = 2; TextConfig config; config.windowId = 77; - ret = inputMethodController_->Attach(TextChangeListenerImpl, false, config); + ret = inputMethodController_->Attach(textChangeListenerImpl, false, config); EXPECT_EQ(ret, ErrorCode::NO_ERROR); TextTotalConfig totalConfig; 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 da4ba966..ad549c63 100644 --- a/test/unittest/cpp_test/src/input_method_controller_test.cpp +++ b/test/unittest/cpp_test/src/input_method_controller_test.cpp @@ -834,45 +834,6 @@ constexpr uint32_t KEY_EVENT_DELAY_TIME = 100; && inputPattern <= static_cast(TextInputType::VISIBLE_PASSWORD)); } - /** - * @tc.name: testIMCOnConfigurationChange - * @tc.desc: IMC testOnConfigurationChange. - * @tc.type: FUNC - * @tc.require: - */ - HWTEST_F(InputMethodControllerTest, testIMCOnConfigurationChange, TestSize.Level0) - { - IMSA_HILOGI("IMC OnConfigurationChange Test START"); - Configuration info; - info.SetEnterKeyType(EnterKeyType::GO); - info.SetTextInputType(TextInputType::NUMBER); - int32_t ret = inputMethodController_->Close(); - EXPECT_EQ(ret, ErrorCode::NO_ERROR); - ret = inputMethodController_->OnConfigurationChange(info); - EXPECT_EQ(ret, ErrorCode::NO_ERROR); - { - std::unique_lock lock(InputMethodControllerTest::keyboardListenerMutex_); - ret = InputMethodControllerTest::keyboardListenerCv_.wait_for( - lock, std::chrono::seconds(DEALY_TIME), [&info] { - return (static_cast( - InputMethodControllerTest::inputAttribute_.inputPattern) == info.GetTextInputType()) && - (static_cast( - InputMethodControllerTest::inputAttribute_.enterKeyType) == info.GetEnterKeyType()); - }); - EXPECT_NE(InputMethodControllerTest::inputAttribute_.inputPattern, - static_cast(info.GetTextInputType())); - EXPECT_NE( - InputMethodControllerTest::inputAttribute_.enterKeyType, static_cast(info.GetEnterKeyType())); - } - - auto keyType = static_cast(EnterKeyType::UNSPECIFIED); - auto inputPattern = static_cast(TextInputType::NONE); - ret = inputMethodController_->GetEnterKeyType(keyType); - EXPECT_EQ(ret, ErrorCode::NO_ERROR); - ret = inputMethodController_->GetInputPattern(inputPattern); - EXPECT_EQ(ret, ErrorCode::NO_ERROR); - } - /** * @tc.name: testOnEditorAttributeChanged * @tc.desc: IMC testOnEditorAttributeChanged. @@ -903,6 +864,8 @@ constexpr uint32_t KEY_EVENT_DELAY_TIME = 100; EXPECT_EQ( InputMethodControllerTest::inputAttribute_.enterKeyType, static_cast(info.GetEnterKeyType())); } + ret = inputMethodController_->Close(); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); } /** -- Gitee From c8241ad08c4ef1fd69f5e85e49ebd63ceb93f3aa Mon Sep 17 00:00:00 2001 From: Hollokin Date: Sat, 8 Jul 2023 15:51:32 +0800 Subject: [PATCH 3/5] =?UTF-8?q?=E5=9C=A8InputMethodAbility::OnShowKeyboard?= =?UTF-8?q?=E4=B8=AD=E8=BF=9B=E8=A1=8C=E7=BC=96=E8=BE=91=E6=A1=86=E4=BF=A1?= =?UTF-8?q?=E6=81=AF=E5=9B=9E=E8=B0=83=E9=80=9A=E7=9F=A5=EF=BC=8C=E9=81=BF?= =?UTF-8?q?=E5=85=8D=E5=9C=A8=E9=9D=9EAttach=E7=9A=84=E6=83=85=E5=86=B5?= =?UTF-8?q?=E4=B8=8B=E4=B9=9F=E8=BF=9B=E8=A1=8C=E9=80=9A=E7=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Hollokin --- .../include/i_input_method_core.h | 3 ++- .../include/input_method_core_proxy.h | 3 ++- .../include/input_method_core_stub.h | 3 ++- .../src/input_method_ability.cpp | 19 ++++++++++--------- .../src/input_method_core_proxy.cpp | 7 ++++--- .../src/input_method_core_stub.cpp | 11 ++++++----- .../input_method_system_ability_proxy.h | 2 +- .../src/input_method_controller.cpp | 8 ++++---- .../src/input_method_system_ability_proxy.cpp | 9 +++++---- .../include/input_method_controller.h | 2 +- .../include/i_input_method_system_ability.h | 2 +- .../include/input_method_system_ability.h | 2 +- services/include/peruser_session.h | 6 +++--- services/src/input_method_system_ability.cpp | 4 ++-- .../src/input_method_system_ability_stub.cpp | 3 ++- services/src/peruser_session.cpp | 16 ++++++++-------- .../perusersession_fuzzer.cpp | 2 +- .../src/input_method_ability_test.cpp | 2 +- .../src/input_method_private_member_test.cpp | 6 +++--- 19 files changed, 59 insertions(+), 51 deletions(-) diff --git a/frameworks/native/inputmethod_ability/include/i_input_method_core.h b/frameworks/native/inputmethod_ability/include/i_input_method_core.h index 0d6ab905..ae301601 100644 --- a/frameworks/native/inputmethod_ability/include/i_input_method_core.h +++ b/frameworks/native/inputmethod_ability/include/i_input_method_core.h @@ -44,7 +44,8 @@ public: DECLARE_INTERFACE_DESCRIPTOR(u"ohos.miscservices.inputmethod.IInputMethodCore"); - virtual int32_t ShowKeyboard(const sptr &inputDataChannel, bool isShowKeyboard) = 0; + virtual int32_t ShowKeyboard( + const sptr &inputDataChannel, bool isShowKeyboard, bool attachFlag) = 0; virtual bool HideKeyboard(int32_t flags) = 0; virtual int32_t InitInputControlChannel( sptr &inputControlChannel, const std::string &imeId) = 0; diff --git a/frameworks/native/inputmethod_ability/include/input_method_core_proxy.h b/frameworks/native/inputmethod_ability/include/input_method_core_proxy.h index 8d0965a4..5408ded1 100644 --- a/frameworks/native/inputmethod_ability/include/input_method_core_proxy.h +++ b/frameworks/native/inputmethod_ability/include/input_method_core_proxy.h @@ -35,7 +35,8 @@ public: DISALLOW_COPY_AND_MOVE(InputMethodCoreProxy); - int32_t ShowKeyboard(const sptr &inputDataChannel, bool isShowKeyboard) override; + int32_t ShowKeyboard( + const sptr &inputDataChannel, bool isShowKeyboard, bool attachFlag) override; bool HideKeyboard(int32_t flags) override; int32_t InitInputControlChannel(sptr &inputControlChannel, const std::string &imeId) override; void StopInputService(std::string imeId) override; diff --git a/frameworks/native/inputmethod_ability/include/input_method_core_stub.h b/frameworks/native/inputmethod_ability/include/input_method_core_stub.h index 6804d377..85a860c8 100644 --- a/frameworks/native/inputmethod_ability/include/input_method_core_stub.h +++ b/frameworks/native/inputmethod_ability/include/input_method_core_stub.h @@ -39,7 +39,8 @@ public: explicit InputMethodCoreStub(int userId); virtual ~InputMethodCoreStub(); int OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override; - int32_t ShowKeyboard(const sptr &inputDataChannel, bool isShowKeyboard) override; + int32_t ShowKeyboard( + const sptr &inputDataChannel, bool isShowKeyboard, bool attachFlag) override; bool HideKeyboard(int32_t flags) override; int32_t InitInputControlChannel(sptr &inputControlChannel, const std::string &imeId) override; void StopInputService(std::string imeId) override; diff --git a/frameworks/native/inputmethod_ability/src/input_method_ability.cpp b/frameworks/native/inputmethod_ability/src/input_method_ability.cpp index 891a4d3b..2d26f046 100644 --- a/frameworks/native/inputmethod_ability/src/input_method_ability.cpp +++ b/frameworks/native/inputmethod_ability/src/input_method_ability.cpp @@ -247,7 +247,8 @@ void InputMethodAbility::OnShowKeyboard(Message *msg) MessageParcel *data = msg->msgContent_; sptr channelObject = nullptr; bool isShowKeyboard = false; - if (!ITypesUtil::Unmarshal(*data, channelObject, isShowKeyboard)) { + bool attachFlag = false; + if (!ITypesUtil::Unmarshal(*data, channelObject, isShowKeyboard, attachFlag)) { IMSA_HILOGE("InputMethodAbility::OnShowKeyboard read message parcel failed"); return; } @@ -256,14 +257,15 @@ void InputMethodAbility::OnShowKeyboard(Message *msg) return; } SetInputDataChannel(channelObject); - TextTotalConfig textConfig = {}; - int32_t ret = GetTextConfig(textConfig); - if (ret != ErrorCode::NO_ERROR) { - IMSA_HILOGE("InputMethodAbility, get text config failed, ret is %{public}d", ret); - return; + if (attachFlag) { + TextTotalConfig textConfig = {}; + int32_t ret = GetTextConfig(textConfig); + if (ret != ErrorCode::NO_ERROR) { + IMSA_HILOGE("InputMethodAbility, get text config failed, ret is %{public}d", ret); + return; + } + OnTextConfigChange(textConfig); } - // todo textConfigÐèÒªÅпÕÂ𣿣¿ - OnTextConfigChange(textConfig); ShowInputWindow(isShowKeyboard); } @@ -430,7 +432,6 @@ void InputMethodAbility::ShowInputWindow(bool isShowKeyboard) void InputMethodAbility::OnTextConfigChange(const TextTotalConfig &textConfig) { IMSA_HILOGI("InputMethodAbility run in."); - // todo ²»ÂÛÊDz»ÊDZ仯£¬¶¼»á֪ͨ¹ýÀ´£¬ÕâÀïÐè²»ÐèÒª×öÀ¹½Ø£¿£¿ if (kdListener_ == nullptr) { IMSA_HILOGE("kdListener_ is nullptr."); } else { diff --git a/frameworks/native/inputmethod_ability/src/input_method_core_proxy.cpp b/frameworks/native/inputmethod_ability/src/input_method_core_proxy.cpp index af3ee924..310e9a9f 100644 --- a/frameworks/native/inputmethod_ability/src/input_method_core_proxy.cpp +++ b/frameworks/native/inputmethod_ability/src/input_method_core_proxy.cpp @@ -39,11 +39,12 @@ int32_t InputMethodCoreProxy::InitInputControlChannel( }); } -int32_t InputMethodCoreProxy::ShowKeyboard(const sptr &inputDataChannel, bool isShowKeyboard) +int32_t InputMethodCoreProxy::ShowKeyboard( + const sptr &inputDataChannel, bool isShowKeyboard, bool attachFlag) { IMSA_HILOGD("InputMethodCoreProxy::showKeyboard"); - return SendRequest(SHOW_KEYBOARD, [&inputDataChannel, &isShowKeyboard](MessageParcel &data) { - return ITypesUtil::Marshal(data, inputDataChannel->AsObject(), isShowKeyboard); + return SendRequest(SHOW_KEYBOARD, [&inputDataChannel, isShowKeyboard, attachFlag](MessageParcel &data) { + return ITypesUtil::Marshal(data, inputDataChannel->AsObject(), isShowKeyboard, attachFlag); }); } diff --git a/frameworks/native/inputmethod_ability/src/input_method_core_stub.cpp b/frameworks/native/inputmethod_ability/src/input_method_core_stub.cpp index 22c42598..5d9b7791 100644 --- a/frameworks/native/inputmethod_ability/src/input_method_core_stub.cpp +++ b/frameworks/native/inputmethod_ability/src/input_method_core_stub.cpp @@ -157,10 +157,11 @@ void InputMethodCoreStub::ShowKeyboardOnRemote(MessageParcel &data, MessageParce IMSA_HILOGD("InputMethodCoreStub::ShowKeyboardOnRemote"); sptr channel; bool isShowKeyboard = false; - int32_t ret = - SendMessage(MessageID::MSG_ID_SHOW_KEYBOARD, [&data, &channel, &isShowKeyboard](MessageParcel &parcel) { - return ITypesUtil::Unmarshal(data, channel, isShowKeyboard) - && ITypesUtil::Marshal(parcel, channel, isShowKeyboard); + bool attachFlag = false; + int32_t ret = SendMessage( + MessageID::MSG_ID_SHOW_KEYBOARD, [&data, &channel, &isShowKeyboard, &attachFlag](MessageParcel &parcel) { + return ITypesUtil::Unmarshal(data, channel, isShowKeyboard, attachFlag) && + ITypesUtil::Marshal(parcel, channel, isShowKeyboard, attachFlag); }); reply.WriteInt32(ret); } @@ -185,7 +186,7 @@ void InputMethodCoreStub::ClearDataChannelOnRemote(MessageParcel &data, MessageP } int32_t InputMethodCoreStub::ShowKeyboard( - const sptr &inputDataChannel, bool isShowKeyboard) + const sptr &inputDataChannel, bool isShowKeyboard, bool attachFlag) { return ErrorCode::NO_ERROR; } diff --git a/frameworks/native/inputmethod_controller/include/input_method_system_ability_proxy.h b/frameworks/native/inputmethod_controller/include/input_method_system_ability_proxy.h index 6e457ede..46059839 100644 --- a/frameworks/native/inputmethod_controller/include/input_method_system_ability_proxy.h +++ b/frameworks/native/inputmethod_controller/include/input_method_system_ability_proxy.h @@ -45,7 +45,7 @@ public: DISALLOW_COPY_AND_MOVE(InputMethodSystemAbilityProxy); int32_t PrepareInput(InputClientInfo &inputClientInfo) override; - int32_t StartInput(sptr client, bool isShowKeyboard) override; + int32_t StartInput(sptr client, bool isShowKeyboard, bool attachFlag) override; int32_t ShowCurrentInput() override; int32_t HideCurrentInput() override; int32_t StopInputSession() override; diff --git a/frameworks/native/inputmethod_controller/src/input_method_controller.cpp b/frameworks/native/inputmethod_controller/src/input_method_controller.cpp index 2f4be272..8bcc58e1 100644 --- a/frameworks/native/inputmethod_controller/src/input_method_controller.cpp +++ b/frameworks/native/inputmethod_controller/src/input_method_controller.cpp @@ -436,7 +436,7 @@ int32_t InputMethodController::Attach( IMSA_HILOGE("failed to prepare, ret: %{public}d", ret); return ret; } - ret = StartInput(clientInfo_.client, isShowKeyboard); + ret = StartInput(clientInfo_.client, isShowKeyboard, true); if (ret != ErrorCode::NO_ERROR) { IMSA_HILOGE("failed to start input, ret:%{public}d", ret); return ret; @@ -460,7 +460,7 @@ int32_t InputMethodController::ShowTextInput() } clientInfo_.isShowKeyboard = true; InputMethodSysEvent::OperateSoftkeyboardBehaviour(IME_SHOW_ENEDITABLE); - int32_t ret = StartInput(clientInfo_.client, true); + int32_t ret = StartInput(clientInfo_.client, true, false); if (ret != ErrorCode::NO_ERROR) { IMSA_HILOGE("failed to start input, ret: %{public}d", ret); return ret; @@ -620,7 +620,7 @@ std::shared_ptr InputMethodController::GetCurrentInputMethodSubtype return property; } -int32_t InputMethodController::StartInput(sptr &client, bool isShowKeyboard) +int32_t InputMethodController::StartInput(sptr &client, bool isShowKeyboard, bool attachFlag) { IMSA_HILOGI("InputMethodController::StartInput"); auto proxy = GetSystemAbilityProxy(); @@ -628,7 +628,7 @@ int32_t InputMethodController::StartInput(sptr &client, bool isSho IMSA_HILOGE("proxy is nullptr"); return ErrorCode::ERROR_SERVICE_START_FAILED; } - return proxy->StartInput(client, isShowKeyboard); + return proxy->StartInput(client, isShowKeyboard, attachFlag); } int32_t InputMethodController::ReleaseInput(sptr &client) diff --git a/frameworks/native/inputmethod_controller/src/input_method_system_ability_proxy.cpp b/frameworks/native/inputmethod_controller/src/input_method_system_ability_proxy.cpp index 877425ef..24557273 100644 --- a/frameworks/native/inputmethod_controller/src/input_method_system_ability_proxy.cpp +++ b/frameworks/native/inputmethod_controller/src/input_method_system_ability_proxy.cpp @@ -37,12 +37,13 @@ int32_t InputMethodSystemAbilityProxy::PrepareInput(InputClientInfo &inputClient }); } -int32_t InputMethodSystemAbilityProxy::StartInput(sptr client, bool isShowKeyboard) +int32_t InputMethodSystemAbilityProxy::StartInput(sptr client, bool isShowKeyboard, bool attachFlag) { IMSA_HILOGD("%{public}s in", __func__); - return SendRequest( - static_cast(InputMethodInterfaceCode::START_INPUT), [isShowKeyboard, client](MessageParcel &data) { - return data.WriteRemoteObject(client->AsObject()) && data.WriteBool(isShowKeyboard); + return SendRequest(static_cast(InputMethodInterfaceCode::START_INPUT), + [isShowKeyboard, client, attachFlag](MessageParcel &data) { + return data.WriteRemoteObject(client->AsObject()) && data.WriteBool(isShowKeyboard) && + data.WriteBool(attachFlag); }); } 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 5a955711..d32cffa9 100644 --- a/interfaces/inner_api/inputmethod_controller/include/input_method_controller.h +++ b/interfaces/inner_api/inputmethod_controller/include/input_method_controller.h @@ -460,7 +460,7 @@ private: bool Initialize(); sptr GetSystemAbilityProxy(); int32_t PrepareInput(InputClientInfo &inputClientInfo); - int32_t StartInput(sptr &client, bool isShowKeyboard); + int32_t StartInput(sptr &client, bool isShowKeyboard, bool attachFlag); int32_t StopInput(sptr &client); int32_t ReleaseInput(sptr &client); void OnSwitchInput(const Property &property, const SubProperty &subProperty); diff --git a/services/include/i_input_method_system_ability.h b/services/include/i_input_method_system_ability.h index 53fc7863..e2edd18e 100644 --- a/services/include/i_input_method_system_ability.h +++ b/services/include/i_input_method_system_ability.h @@ -41,7 +41,7 @@ public: DECLARE_INTERFACE_DESCRIPTOR(u"ohos.miscservices.inputmethod.IInputMethodSystemAbility"); virtual int32_t PrepareInput(InputClientInfo &clientInfo) = 0; - virtual int32_t StartInput(sptr client, bool isShowKeyboard) = 0; + virtual int32_t StartInput(sptr client, bool isShowKeyboard, bool attachFlag) = 0; virtual int32_t ShowCurrentInput() = 0; virtual int32_t HideCurrentInput() = 0; virtual int32_t StopInputSession() = 0; diff --git a/services/include/input_method_system_ability.h b/services/include/input_method_system_ability.h index f56ea0a0..4591a27d 100644 --- a/services/include/input_method_system_ability.h +++ b/services/include/input_method_system_ability.h @@ -58,7 +58,7 @@ public: ~InputMethodSystemAbility(); int32_t PrepareInput(InputClientInfo &clientInfo) override; - int32_t StartInput(sptr client, bool isShowKeyboard) override; + int32_t StartInput(sptr client, bool isShowKeyboard, bool attachFlag) override; int32_t ShowCurrentInput() override; int32_t HideCurrentInput() override; int32_t StopInput(sptr client) override; diff --git a/services/include/peruser_session.h b/services/include/peruser_session.h index 5ffa82e1..8a6a331b 100644 --- a/services/include/peruser_session.h +++ b/services/include/peruser_session.h @@ -75,7 +75,7 @@ public: ~PerUserSession(); int32_t OnPrepareInput(const InputClientInfo &clientInfo); - int32_t OnStartInput(const sptr &client, bool isShowKeyboard); + int32_t OnStartInput(const sptr &client, bool isShowKeyboard, bool attachFlag); int32_t OnStopInput(sptr client); int32_t OnReleaseInput(const sptr &client); int32_t OnSetCoreAndAgent(const sptr &core, const sptr &agent); @@ -122,8 +122,8 @@ private: int AddClient(sptr inputClient, const InputClientInfo &clientInfo, ClientAddEvent event); void UpdateClient(sptr inputClient, bool isShowKeyboard); int32_t RemoveClient(const sptr &client, bool isClientDied); - int32_t ShowKeyboard( - const sptr &channel, const sptr &inputClient, bool isShowKeyboard); + int32_t ShowKeyboard(const sptr &channel, const sptr &inputClient, + bool isShowKeyboard, bool attachFlag); int32_t HideKeyboard(const sptr &inputClient); int32_t ClearDataChannel(const sptr &channel); int32_t SendAgentToSingleClient(const sptr &client); diff --git a/services/src/input_method_system_ability.cpp b/services/src/input_method_system_ability.cpp index 65ea498e..822f1e63 100644 --- a/services/src/input_method_system_ability.cpp +++ b/services/src/input_method_system_ability.cpp @@ -246,7 +246,7 @@ int32_t InputMethodSystemAbility::ReleaseInput(sptr client) return userSession_->OnReleaseInput(client); }; -int32_t InputMethodSystemAbility::StartInput(sptr client, bool isShowKeyboard) +int32_t InputMethodSystemAbility::StartInput(sptr client, bool isShowKeyboard, bool attachFlag) { if (!BundleChecker::IsFocused(IPCSkeleton::GetCallingUid())) { return ErrorCode::ERROR_CLIENT_NOT_FOCUSED; @@ -255,7 +255,7 @@ int32_t InputMethodSystemAbility::StartInput(sptr client, bool isS IMSA_HILOGE("InputMethodSystemAbility::client is nullptr"); return ErrorCode::ERROR_CLIENT_NULL_POINTER; } - return userSession_->OnStartInput(client, isShowKeyboard); + return userSession_->OnStartInput(client, isShowKeyboard, attachFlag); }; int32_t InputMethodSystemAbility::StopInput(sptr client) diff --git a/services/src/input_method_system_ability_stub.cpp b/services/src/input_method_system_ability_stub.cpp index 02066f45..5db90007 100644 --- a/services/src/input_method_system_ability_stub.cpp +++ b/services/src/input_method_system_ability_stub.cpp @@ -63,7 +63,8 @@ int32_t InputMethodSystemAbilityStub::StartInputOnRemote(MessageParcel &data, Me return ErrorCode::ERROR_EX_PARCELABLE; } bool isShowKeyboard = data.ReadBool(); - int32_t ret = StartInput(iface_cast(clientObject), isShowKeyboard); + bool attachFlag = data.ReadBool(); + int32_t ret = StartInput(iface_cast(clientObject), isShowKeyboard, attachFlag); return reply.WriteInt32(ret) ? ErrorCode::NO_ERROR : ErrorCode::ERROR_EX_PARCELABLE; } diff --git a/services/src/peruser_session.cpp b/services/src/peruser_session.cpp index e1a3716e..ab11c40e 100644 --- a/services/src/peruser_session.cpp +++ b/services/src/peruser_session.cpp @@ -141,8 +141,8 @@ int32_t PerUserSession::RemoveClient(const sptr &client, bool isC * @return ErrorCode::ERROR_KBD_SHOW_FAILED failed to show keyboard * @return other errors returned by binder driver */ -int32_t PerUserSession::ShowKeyboard( - const sptr& channel, const sptr &inputClient, bool isShowKeyboard) +int32_t PerUserSession::ShowKeyboard(const sptr &channel, const sptr &inputClient, + bool isShowKeyboard, bool attachFlag) { IMSA_HILOGD("PerUserSession, run in"); if (inputClient == nullptr) { @@ -153,7 +153,7 @@ int32_t PerUserSession::ShowKeyboard( IMSA_HILOGE("Aborted! imsCore[%{public}d] is nullptr", CURRENT_IME); return ErrorCode::ERROR_IME_NOT_STARTED; } - int32_t ret = core->ShowKeyboard(channel, isShowKeyboard); + int32_t ret = core->ShowKeyboard(channel, isShowKeyboard, attachFlag); if (ret != ErrorCode::NO_ERROR) { IMSA_HILOGE("failed to show keyboard, ret: %{public}d", ret); return ErrorCode::ERROR_KBD_SHOW_FAILED; @@ -268,7 +268,7 @@ int PerUserSession::OnShowKeyboardSelf() IMSA_HILOGE("client info not found"); return ErrorCode::ERROR_CLIENT_NOT_FOUND; } - return ShowKeyboard(clientInfo->channel, client, true); + return ShowKeyboard(clientInfo->channel, client, true, false); } /** Get ClientInfo @@ -337,9 +337,9 @@ int32_t PerUserSession::OnReleaseInput(const sptr& client) * @param the parameters from remote client * @return ErrorCode */ -int32_t PerUserSession::OnStartInput(const sptr &client, bool isShowKeyboard) +int32_t PerUserSession::OnStartInput(const sptr &client, bool isShowKeyboard, bool attachFlag) { - IMSA_HILOGD("start input with keyboard[%{public}d]", isShowKeyboard); + IMSA_HILOGD("start input with keyboard[%{public}d], attchFlag[%{public}d]", isShowKeyboard, attachFlag); if (client == nullptr) { IMSA_HILOGE("client is nullptr"); return ErrorCode::ERROR_CLIENT_NULL_POINTER; @@ -363,7 +363,7 @@ int32_t PerUserSession::OnStartInput(const sptr &client, bool isSh return ret; } // build channel from ima to imc - return ShowKeyboard(clientInfo->channel, client, isShowKeyboard); + return ShowKeyboard(clientInfo->channel, client, isShowKeyboard, attachFlag); } int32_t PerUserSession::OnSetCoreAndAgent(const sptr &core, const sptr &agent) @@ -391,7 +391,7 @@ int32_t PerUserSession::OnSetCoreAndAgent(const sptr &core, co if (client != nullptr) { auto clientInfo = GetClientInfo(client->AsObject()); if (clientInfo != nullptr) { - ret = OnStartInput(clientInfo->client, clientInfo->isShowKeyboard); + ret = OnStartInput(clientInfo->client, clientInfo->isShowKeyboard, false); IMSA_HILOGI("start input ret: %{public}d", ret); } } diff --git a/test/fuzztest/perusersession_fuzzer/perusersession_fuzzer.cpp b/test/fuzztest/perusersession_fuzzer/perusersession_fuzzer.cpp index 6fcb73f1..26448c60 100644 --- a/test/fuzztest/perusersession_fuzzer/perusersession_fuzzer.cpp +++ b/test/fuzztest/perusersession_fuzzer/perusersession_fuzzer.cpp @@ -67,7 +67,7 @@ bool FuzzPerUserSession(const uint8_t *rawData, size_t size) userSessions->OnSwitchIme(property, subProperty, true); userSessions->StopInputService(str); userSessions->OnHideKeyboardSelf(); - userSessions->OnStartInput(client, isShowKeyboard); + userSessions->OnStartInput(client, isShowKeyboard, false); userSessions->OnStopInput(client); userSessions->OnReleaseInput(client); userSessions->OnSetCoreAndAgent(core, agent); 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 df96989b..9724fef8 100644 --- a/test/unittest/cpp_test/src/input_method_ability_test.cpp +++ b/test/unittest/cpp_test/src/input_method_ability_test.cpp @@ -267,7 +267,7 @@ HWTEST_F(InputMethodAbilityTest, testShowKeyboardInputMethodCoreProxy, TestSize. sptr coreProxy = new InputMethodCoreProxy(coreObject); sptr channelProxy = new InputDataChannelProxy(channelObject); - auto ret = coreProxy->ShowKeyboard(channelProxy, false); + auto ret = coreProxy->ShowKeyboard(channelProxy, false, false); std::unique_lock lock(InputMethodAbilityTest::imeListenerCallbackLock_); auto cvStatus = InputMethodAbilityTest::imeListenerCv_.wait_for(lock, std::chrono::seconds(DEALY_TIME)); EXPECT_EQ(ret, ErrorCode::NO_ERROR); 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 0948ca80..9f635804 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 @@ -189,7 +189,7 @@ HWTEST_F(InputMethodPrivateMemberTest, PerUserSessionCoreOrAgentNullptr, TestSiz auto userSession = std::make_shared(MAIN_USER_ID); userSession->SetImsCore(CURRENT_IME, nullptr); auto imc = InputMethodController::GetInstance(); - int32_t ret = userSession->ShowKeyboard(imc->clientInfo_.channel, imc->clientInfo_.client, false); + int32_t ret = userSession->ShowKeyboard(imc->clientInfo_.channel, imc->clientInfo_.client, false, false); EXPECT_EQ(ret, ErrorCode::ERROR_IME_NOT_STARTED); ret = userSession->HideKeyboard(imc->clientInfo_.client); EXPECT_EQ(ret, ErrorCode::ERROR_IME_NOT_STARTED); @@ -252,11 +252,11 @@ HWTEST_F(InputMethodPrivateMemberTest, PerUserSessionParameterNullptr001, TestSi { IMSA_HILOGI("InputMethodPrivateMemberTest PerUserSessionParameterNullptr001 TEST START"); auto userSession = std::make_shared(MAIN_USER_ID); - int32_t ret = userSession->OnStartInput(nullptr, true); + int32_t ret = userSession->OnStartInput(nullptr, true, false); EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NULL_POINTER); ret = userSession->OnReleaseInput(nullptr); EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NULL_POINTER); - ret = userSession->ShowKeyboard(nullptr, nullptr, false); + ret = userSession->ShowKeyboard(nullptr, nullptr, false, false); EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NULL_POINTER); ret = userSession->RemoveClient(nullptr, false); EXPECT_EQ(ret, ErrorCode::NO_ERROR); -- Gitee From d12e013ec561ef0542ff1a9fc3a87c01d25c7920 Mon Sep 17 00:00:00 2001 From: Hollokin Date: Sat, 8 Jul 2023 18:02:09 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=A3=80=E8=A7=86?= =?UTF-8?q?=E6=84=8F=E8=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Hollokin --- .../js_get_input_method_controller.cpp | 92 ++++++------ .../js_get_input_method_controller.h | 4 +- .../include/input_method_ability.h | 1 + .../src/input_method_ability.cpp | 46 +++--- .../include/input_method_utils.h | 13 +- .../src/input_data_channel_stub.cpp | 1 - .../src/input_method_controller.cpp | 56 ++++---- .../src/itypes_util.cpp | 15 +- .../include/input_method_controller.h | 4 +- services/include/input_attribute.h | 6 + services/src/peruser_session.cpp | 2 +- .../src/input_method_ability_test.cpp | 6 +- .../cpp_test/src/input_method_attach_test.cpp | 133 +++++++----------- .../src/input_method_controller_test.cpp | 5 +- 14 files changed, 181 insertions(+), 203 deletions(-) diff --git a/frameworks/js/napi/inputmethodclient/js_get_input_method_controller.cpp b/frameworks/js/napi/inputmethodclient/js_get_input_method_controller.cpp index 1d29a2b6..2a6c9585 100644 --- a/frameworks/js/napi/inputmethodclient/js_get_input_method_controller.cpp +++ b/frameworks/js/napi/inputmethodclient/js_get_input_method_controller.cpp @@ -422,6 +422,40 @@ napi_value JsGetInputMethodController::CreateSelectMovement(napi_env env, int32_ return movement; } +napi_value JsGetInputMethodController::HandleSoftKeyboard( + napi_env env, napi_callback_info info, std::function callback, bool isOutput, bool needThrowException) +{ + auto ctxt = std::make_shared(); + auto input = [ctxt]( + napi_env env, size_t argc, napi_value *argv, napi_value self) -> napi_status { return napi_ok; }; + auto output = [ctxt, isOutput](napi_env env, napi_value *result) -> napi_status { + if (!isOutput) { + return napi_ok; + } + napi_status status = napi_get_boolean(env, ctxt->isHandle, result); + IMSA_HILOGE("output napi_get_boolean != nullptr[%{public}d]", result != nullptr); + return status; + }; + auto exec = [ctxt, callback, needThrowException](AsyncCall::Context *ctx) { + int errCode = callback(); + if (errCode == ErrorCode::NO_ERROR) { + IMSA_HILOGI("exec success"); + ctxt->status = napi_ok; + ctxt->isHandle = true; + ctxt->SetState(ctxt->status); + return; + } + IMSA_HILOGI("exec %{public}d", errCode); + if (needThrowException) { + ctxt->SetErrorCode(errCode); + } + }; + ctxt->SetAction(std::move(input), std::move(output)); + // 1 means JsAPI has 1 params at most. + AsyncCall asyncCall(env, info, ctxt, 1); + return asyncCall.Call(env, exec, "handleSoftKeyboard"); +} + bool JsGetInputMethodController::GetValue(napi_env env, napi_value in, SelectionRange &out) { auto ret = JsUtil::Object::ReadProperty(env, in, "start", out.start); @@ -447,13 +481,13 @@ bool JsGetInputMethodController::GetValue(napi_env env, napi_value in, Selection * windowId?: number * } */ -napi_status JsGetInputMethodController::GetValue(napi_env env, napi_value in, TextConfig &out) +bool JsGetInputMethodController::GetValue(napi_env env, napi_value in, TextConfig &out) { napi_value attributeResult = nullptr; napi_status status = JsUtils::GetValue(env, in, "inputAttribute", attributeResult); - CHECK_RETURN(status == napi_ok, "get inputAttribute", status); + CHECK_RETURN(status == napi_ok, "get inputAttribute", false); bool ret = JsGetInputMethodController::GetValue(env, attributeResult, out.inputAttribute); - CHECK_RETURN(ret, "get inputAttribute of TextConfig", napi_generic_failure); + CHECK_RETURN(ret, "get inputAttribute of TextConfig", ret); napi_value cursorInfoResult = nullptr; status = JsUtils::GetValue(env, in, "cursorInfo", cursorInfoResult); @@ -471,54 +505,18 @@ napi_status JsGetInputMethodController::GetValue(napi_env env, napi_value in, Te ret = JsUtil::Object::ReadProperty(env, in, "windowId", out.windowId); IMSA_HILOGE("get windowId end, ret = %{public}d", ret); - return napi_ok; + return ret; } -napi_value JsGetInputMethodController::HandleSoftKeyboard( - napi_env env, napi_callback_info info, std::function callback, bool isOutput, bool needThrowException) -{ - auto ctxt = std::make_shared(); - auto input = [ctxt]( - napi_env env, size_t argc, napi_value *argv, napi_value self) -> napi_status { return napi_ok; }; - auto output = [ctxt, isOutput](napi_env env, napi_value *result) -> napi_status { - if (!isOutput) { - return napi_ok; - } - napi_status status = napi_get_boolean(env, ctxt->isHandle, result); - IMSA_HILOGE("output napi_get_boolean != nullptr[%{public}d]", result != nullptr); - return status; - }; - auto exec = [ctxt, callback, needThrowException](AsyncCall::Context *ctx) { - int errCode = callback(); - if (errCode == ErrorCode::NO_ERROR) { - IMSA_HILOGI("exec success"); - ctxt->status = napi_ok; - ctxt->isHandle = true; - ctxt->SetState(ctxt->status); - return; - } - IMSA_HILOGI("exec %{public}d", errCode); - if (needThrowException) { - ctxt->SetErrorCode(errCode); - } - }; - ctxt->SetAction(std::move(input), std::move(output)); - // 1 means JsAPI has 1 params at most. - AsyncCall asyncCall(env, info, ctxt, 1); - return asyncCall.Call(env, exec, "handleSoftKeyboard"); -} - -napi_status JsGetInputMethodController::ParseAttachInput( +bool JsGetInputMethodController::ParseAttachInput( napi_env env, size_t argc, napi_value *argv, const std::shared_ptr &ctxt) { // 0 means the first parameter: showkeyboard - napi_status status = JsUtils::GetValue(env, argv[0], ctxt->showKeyboard); - if (status != napi_ok) { - return status; - } + bool ret = JsUtil::GetValue(env, argv[0], ctxt->showKeyboard); + IMSA_HILOGE("get showKeyboard end, ret = %{public}d", ret); // 1 means the second parameter: textConfig - return JsGetInputMethodController::GetValue(env, argv[1], ctxt->textConfig); + return ret && JsGetInputMethodController::GetValue(env, argv[1], ctxt->textConfig); } napi_value JsGetInputMethodController::Attach(napi_env env, napi_callback_info info) @@ -526,9 +524,9 @@ napi_value JsGetInputMethodController::Attach(napi_env env, napi_callback_info i auto ctxt = std::make_shared(); auto input = [ctxt](napi_env env, size_t argc, napi_value *argv, napi_value self) -> napi_status { PARAM_CHECK_RETURN(env, argc > 1, "should 2 or 3 parameters!", TYPE_NONE, napi_generic_failure); - napi_status status = ParseAttachInput(env, argc, argv, ctxt); - PARAM_CHECK_RETURN(env, status == napi_ok, "paramters of attach is error. ", TYPE_NONE, status); - return status; + bool ret = ParseAttachInput(env, argc, argv, ctxt); + PARAM_CHECK_RETURN(env, ret, "paramters of attach is error. ", TYPE_NONE, napi_generic_failure); + return napi_ok; }; auto exec = [ctxt, env](AsyncCall::Context *ctx) { ctxt->textListener = JsGetInputMethodTextChangedListener::GetInstance(); diff --git a/frameworks/js/napi/inputmethodclient/js_get_input_method_controller.h b/frameworks/js/napi/inputmethodclient/js_get_input_method_controller.h index 1ecdbe5c..8e4a17f2 100644 --- a/frameworks/js/napi/inputmethodclient/js_get_input_method_controller.h +++ b/frameworks/js/napi/inputmethodclient/js_get_input_method_controller.h @@ -194,11 +194,11 @@ private: static napi_value CreateSendFunctionKey(napi_env env, int32_t functionKey); void RegisterListener(napi_value callback, std::string type, std::shared_ptr callbackObj); void UnRegisterListener(napi_value callback, std::string type); - static napi_status ParseAttachInput( + static bool ParseAttachInput( napi_env env, size_t argc, napi_value *argv, const std::shared_ptr &ctxt); static bool GetValue(napi_env env, napi_value in, CursorInfo &out); static bool GetValue(napi_env env, napi_value in, InputAttribute &out); - static napi_status GetValue(napi_env env, napi_value in, TextConfig &out); + static bool GetValue(napi_env env, napi_value in, TextConfig &out); static bool GetValue(napi_env env, napi_value in, SelectionRange &out); static napi_value GetJsKeyboardStatusProperty(napi_env env); static napi_value GetJsEnterKeyTypeProperty(napi_env env); diff --git a/frameworks/native/inputmethod_ability/include/input_method_ability.h b/frameworks/native/inputmethod_ability/include/input_method_ability.h index bf64d933..2209b615 100644 --- a/frameworks/native/inputmethod_ability/include/input_method_ability.h +++ b/frameworks/native/inputmethod_ability/include/input_method_ability.h @@ -123,6 +123,7 @@ private: void OnConfigurationChange(Message *msg); void ShowInputWindow(bool isShowKeyboard); void DismissInputWindow(); + void EnsureToShowPanel(); void OnTextConfigChange(const TextTotalConfig &textConfig); bool isImeReady_{ false }; InputStartNotifier notifier_; diff --git a/frameworks/native/inputmethod_ability/src/input_method_ability.cpp b/frameworks/native/inputmethod_ability/src/input_method_ability.cpp index 7bdc7a38..18d7cc44 100644 --- a/frameworks/native/inputmethod_ability/src/input_method_ability.cpp +++ b/frameworks/native/inputmethod_ability/src/input_method_ability.cpp @@ -37,6 +37,10 @@ class MessageHandler; using namespace MessageID; sptr InputMethodAbility::instance_; std::mutex InputMethodAbility::instanceLock_; +constexpr int32_t RETRY_COUNT = 10; +constexpr int32_t WAIT_GAP = 10; +constexpr double INVALID_CURSOR_VALUE = -1.0; +constexpr int32_t INVALID_SELECTION_VALUE = -1; InputMethodAbility::InputMethodAbility() : stop_(false) { writeInputChannel = nullptr; @@ -149,12 +153,9 @@ void InputMethodAbility::OnImeReady() return; } IMSA_HILOGI("InputMethodAbility::Ime Ready, notify InputStart"); - TextTotalConfig textConfig = {}; + TextTotalConfig textConfig{}; int32_t ret = GetTextConfig(textConfig); - if (ret != ErrorCode::NO_ERROR) { - IMSA_HILOGE("InputMethodAbility, get text config failed, ret is %{public}d", ret); - return; - } + IMSA_HILOGI("InputMethodAbility, get text config failed, ret is %{public}d", ret); OnTextConfigChange(textConfig); ShowInputWindow(notifier_.isShowKeyboard); } @@ -387,6 +388,23 @@ void InputMethodAbility::OnConfigurationChange(Message *msg) kdListener_->OnEditorAttributeChange(attribute); } +void InputMethodAbility::EnsureToShowPanel() +{ + for (int i = 0; i < RETRY_COUNT; ++i) { + auto result = panels_.Find(SOFT_KEYBOARD); + if (result.first) { + IMSA_HILOGI("find SOFT_KEYBOARD panel."); + auto ret = result.second->ShowPanel(); + if (ret != ErrorCode::NO_ERROR) { + IMSA_HILOGE("Show panel failed, ret = %{public}d.", ret); + } + return; + } + IMSA_HILOGE("Not find SOFT_KEYBOARD panel, count = %{public}d", i); + std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_GAP)); + } +} + void InputMethodAbility::ShowInputWindow(bool isShowKeyboard) { IMSA_HILOGI("InputMethodAbility::ShowInputWindow"); @@ -412,19 +430,7 @@ void InputMethodAbility::ShowInputWindow(bool isShowKeyboard) return; } channel->SendKeyboardStatus(KEYBOARD_SHOW); - for (int i = 0; i < 10; ++i) { - auto result = panels_.Find(SOFT_KEYBOARD); - if (result.first) { - IMSA_HILOGI("find SOFT_KEYBOARD panel."); - auto ret = result.second->ShowPanel(); - if (ret != ErrorCode::NO_ERROR) { - IMSA_HILOGE("Show panel failed, ret = %{public}d.", ret); - } - return; - } - IMSA_HILOGE("Not find SOFT_KEYBOARD panel, count = %{public}d", i); - std::this_thread::sleep_for(std::chrono::milliseconds(10)); - } + EnsureToShowPanel(); } void InputMethodAbility::OnTextConfigChange(const TextTotalConfig &textConfig) @@ -435,12 +441,12 @@ void InputMethodAbility::OnTextConfigChange(const TextTotalConfig &textConfig) } else { IMSA_HILOGI("send on('editorAttributeChanged') callback."); kdListener_->OnEditorAttributeChange(textConfig.inputAttribute); - if (textConfig.cursorInfo.left != -1.0) { + if (textConfig.cursorInfo.left != INVALID_CURSOR_VALUE) { IMSA_HILOGI("send on('cursorUpdate') callback."); kdListener_->OnCursorUpdate( textConfig.cursorInfo.left, textConfig.cursorInfo.top, textConfig.cursorInfo.height); } - if (textConfig.textSelection.newBegin != -1) { + if (textConfig.textSelection.newBegin != INVALID_SELECTION_VALUE) { IMSA_HILOGI("send on('selectionChange') callback."); kdListener_->OnSelectionChange(textConfig.textSelection.oldBegin, textConfig.textSelection.oldEnd, textConfig.textSelection.newBegin, textConfig.textSelection.newEnd); diff --git a/frameworks/native/inputmethod_controller/include/input_method_utils.h b/frameworks/native/inputmethod_controller/include/input_method_utils.h index c7ebae8e..3f158817 100644 --- a/frameworks/native/inputmethod_controller/include/input_method_utils.h +++ b/frameworks/native/inputmethod_controller/include/input_method_utils.h @@ -24,6 +24,7 @@ namespace OHOS { namespace MiscServices { constexpr uint32_t INIT_WINDOW_ID = 0; constexpr uint32_t INVALID_WINDOW_ID = INIT_WINDOW_ID - 1; +constexpr int32_t INVALID_VALUE = -1; enum class EnterKeyType { UNSPECIFIED = 0, NONE, GO, SEARCH, SEND, NEXT, DONE, PREVIOUS }; enum class TextInputType { @@ -113,15 +114,15 @@ private: }; struct SelectionRange { - int32_t start = -1; - int32_t end = -1; + int32_t start = INVALID_VALUE; + int32_t end = INVALID_VALUE; }; struct TextSelection { - int32_t oldBegin = -1; - int32_t oldEnd = -1; - int32_t newBegin = -1; - int32_t newEnd = -1; + int32_t oldBegin = INVALID_VALUE; + int32_t oldEnd = INVALID_VALUE; + int32_t newBegin = INVALID_VALUE; + int32_t newEnd = INVALID_VALUE; }; class TextTotalConfig { diff --git a/frameworks/native/inputmethod_controller/src/input_data_channel_stub.cpp b/frameworks/native/inputmethod_controller/src/input_data_channel_stub.cpp index ec3a53d1..ce372450 100644 --- a/frameworks/native/inputmethod_controller/src/input_data_channel_stub.cpp +++ b/frameworks/native/inputmethod_controller/src/input_data_channel_stub.cpp @@ -104,7 +104,6 @@ int32_t InputDataChannelStub::OnRemoteRequest( break; } case GET_TEXT_CONFIG: { - IMSA_HILOGD("tyx::GetTextConfig in."); TextTotalConfig textConfig = {}; reply.WriteInt32(GetTextConfig(textConfig)); ITypesUtil::Marshal(reply, textConfig); diff --git a/frameworks/native/inputmethod_controller/src/input_method_controller.cpp b/frameworks/native/inputmethod_controller/src/input_method_controller.cpp index 9606e1bb..b167081b 100644 --- a/frameworks/native/inputmethod_controller/src/input_method_controller.cpp +++ b/frameworks/native/inputmethod_controller/src/input_method_controller.cpp @@ -378,7 +378,7 @@ void InputMethodController::OnPanelStatusChange( void InputMethodController::SaveTextConfig(const TextConfig &textConfig) { IMSA_HILOGI("InputMethodController in, save text config."); - std::lock_guard lock(editorContentLock_); + std::lock_guard lock(textConfigLock_); textConfig_ = textConfig; } @@ -407,17 +407,12 @@ int32_t InputMethodController::Attach( int32_t InputMethodController::Attach( sptr &listener, bool isShowKeyboard, const TextConfig &textConfig) { - IMSA_HILOGI("isShowKeyboard %{public}s", isShowKeyboard ? "true" : "false"); + IMSA_HILOGI("isShowKeyboard %{public}d", isShowKeyboard); InputmethodTrace tracer("InputMethodController Attach with textConfig trace."); { std::lock_guard lock(textListenerLock_); textListener_ = listener; } - { - std::lock_guard configLock(configurationMutex_); - inputPattern_ = textConfig.inputAttribute.inputPattern; - enterKeyType_ = textConfig.inputAttribute.enterKeyType; - } clientInfo_.isShowKeyboard = isShowKeyboard; SaveTextConfig(textConfig); @@ -689,15 +684,17 @@ void InputMethodController::RestoreAttachInfoInSaDied() return; } auto attach = [=]() -> bool { - auto errCode = Attach(textListener_, clientInfo_.isShowKeyboard, textConfig_); - if (errCode == ErrorCode::NO_ERROR) { - isDiedAttached_.store(true); - OnCursorUpdate(cursorInfo_); - OnSelectionChange(textString_, selectNewBegin_, selectNewEnd_); - IMSA_HILOGI("attach success."); - return true; + TextConfig tempConfig{}; + { + std::lock_guard lock(textConfigLock_); + tempConfig = textConfig_; + tempConfig.cursorInfo = cursorInfo_; + tempConfig.range.start = selectNewBegin_; + tempConfig.range.end = selectNewEnd_; } - return false; + auto errCode = Attach(textListener_, clientInfo_.isShowKeyboard, tempConfig); + IMSA_HILOGI("attach end, errCode = %{public}d", errCode); + return errCode == ErrorCode::NO_ERROR; }; if (attach()) { return; @@ -778,9 +775,11 @@ int32_t InputMethodController::OnConfigurationChange(Configuration info) IMSA_HILOGE("not bound yet"); return ErrorCode::ERROR_CLIENT_NOT_BOUND; } - std::lock_guard lock(configurationMutex_); - enterKeyType_ = static_cast(info.GetEnterKeyType()); - inputPattern_ = static_cast(info.GetTextInputType()); + { + std::lock_guard lock(textConfigLock_); + textConfig_.inputAttribute.enterKeyType = static_cast(info.GetEnterKeyType()); + textConfig_.inputAttribute.inputPattern = static_cast(info.GetTextInputType()); + } std::lock_guard agentLock(agentLock_); if (agent_ == nullptr) { IMSA_HILOGE("agent is nullptr"); @@ -849,32 +848,36 @@ bool InputMethodController::DispatchKeyEvent(std::shared_ptr keyE int32_t InputMethodController::GetEnterKeyType(int32_t &keyType) { IMSA_HILOGI("InputMethodController::GetEnterKeyType"); - std::lock_guard lock(configurationMutex_); - keyType = enterKeyType_; + std::lock_guard lock(textConfigLock_); + keyType = textConfig_.inputAttribute.enterKeyType; return ErrorCode::NO_ERROR; } int32_t InputMethodController::GetInputPattern(int32_t &inputpattern) { IMSA_HILOGI("InputMethodController::GetInputPattern"); - std::lock_guard lock(configurationMutex_); - inputpattern = inputPattern_; + std::lock_guard lock(textConfigLock_); + inputpattern = textConfig_.inputAttribute.inputPattern; return ErrorCode::NO_ERROR; } int32_t InputMethodController::GetTextConfig(TextTotalConfig &config) { IMSA_HILOGI("InputMethodController run in."); + std::lock_guard lock(textConfigLock_); config.inputAttribute = textConfig_.inputAttribute; config.cursorInfo = textConfig_.cursorInfo; config.windowId = textConfig_.windowId; - if (textConfig_.range.start == -1) { + if (textConfig_.range.start == INVALID_VALUE) { IMSA_HILOGI("no valid SelectionRange param."); return ErrorCode::NO_ERROR; } - config.textSelection.oldBegin = 0; - config.textSelection.oldEnd = 0; + { + std::lock_guard editorLock(editorContentLock_); + config.textSelection.oldBegin = selectOldBegin_; + config.textSelection.oldEnd = selectOldEnd_; + } config.textSelection.newBegin = textConfig_.range.start; config.textSelection.newEnd = textConfig_.range.end; @@ -1021,6 +1024,9 @@ void InputMethodController::ClearEditorCache() selectOldEnd_ = 0; selectNewBegin_ = 0; selectNewEnd_ = 0; + } + { + std::lock_guard lock(textConfigLock_); textConfig_ = {}; } std::lock_guard lock(cursorInfoMutex_); diff --git a/frameworks/native/inputmethod_controller/src/itypes_util.cpp b/frameworks/native/inputmethod_controller/src/itypes_util.cpp index 6e329033..46238430 100644 --- a/frameworks/native/inputmethod_controller/src/itypes_util.cpp +++ b/frameworks/native/inputmethod_controller/src/itypes_util.cpp @@ -153,7 +153,7 @@ bool ITypesUtil::Marshalling(const SubProperty &input, MessageParcel &data) bool ITypesUtil::Unmarshalling(SubProperty &output, MessageParcel &data) { if (!Unmarshal(data, output.label, output.labelId, output.name, output.id, output.mode, output.locale, - output.language, output.icon, output.iconId)) { + output.language, output.icon, output.iconId)) { IMSA_HILOGE("ITypesUtil::read SubProperty from message parcel failed"); return false; } @@ -180,9 +180,8 @@ bool ITypesUtil::Unmarshalling(InputAttribute &output, MessageParcel &data) bool ITypesUtil::Marshalling(const TextTotalConfig &input, MessageParcel &data) { - IMSA_HILOGI("tyx::start to Marshalling TextTotalConfig."); if (!Marshal(data, input.inputAttribute.inputPattern, input.inputAttribute.enterKeyType, - input.inputAttribute.inputOption)) { + input.inputAttribute.inputOption)) { IMSA_HILOGE("write InputAttribute to message parcel failed"); return false; } @@ -191,7 +190,7 @@ bool ITypesUtil::Marshalling(const TextTotalConfig &input, MessageParcel &data) return false; } if (!Marshal(data, input.textSelection.oldBegin, input.textSelection.oldEnd, input.textSelection.newBegin, - input.textSelection.newEnd)) { + input.textSelection.newEnd)) { IMSA_HILOGE("write TextSelection to message parcel failed"); return false; } @@ -208,13 +207,13 @@ bool ITypesUtil::Unmarshalling(TextTotalConfig &output, MessageParcel &data) IMSA_HILOGE("read InputAttribute from message parcel failed"); return false; } - if (!Unmarshal( - data, output.cursorInfo.left, output.cursorInfo.top, output.cursorInfo.height, output.cursorInfo.width)) { + if (!Unmarshal(data, output.cursorInfo.left, output.cursorInfo.top, + output.cursorInfo.height, output.cursorInfo.width)) { IMSA_HILOGE("read CursorInfo from message parcel failed"); return false; } - if (!Unmarshal(data, output.textSelection.oldBegin, output.textSelection.oldEnd, output.textSelection.newBegin, - output.textSelection.newEnd)) { + if (!Unmarshal(data, output.textSelection.oldBegin, output.textSelection.oldEnd, + output.textSelection.newBegin, output.textSelection.newEnd)) { IMSA_HILOGE("read TextSelection from message parcel failed"); return false; } 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 06c6b7fe..fb0727d4 100644 --- a/interfaces/inner_api/inputmethod_controller/include/input_method_controller.h +++ b/interfaces/inner_api/inputmethod_controller/include/input_method_controller.h @@ -485,9 +485,6 @@ private: std::thread workThreadHandler; MessageHandler *msgHandler_; bool stop_; - std::mutex configurationMutex_; - int32_t enterKeyType_ = 0; - int32_t inputPattern_ = 0; std::atomic_bool isEditable_{ false }; std::atomic_bool isBound_{ false }; @@ -498,6 +495,7 @@ private: static constexpr int CURSOR_DIRECTION_BASE_VALUE = 2011; std::atomic_bool isDiedRestoreListen_{ false }; + std::mutex textConfigLock_; TextConfig textConfig_; }; } // namespace MiscServices diff --git a/services/include/input_attribute.h b/services/include/input_attribute.h index 3c728fa7..d598d750 100644 --- a/services/include/input_attribute.h +++ b/services/include/input_attribute.h @@ -43,6 +43,12 @@ struct InputAttribute { { return inputPattern == PATTERN_PASSWORD; } + + bool operator==(const InputAttribute &info) const + { + return inputPattern == info.inputPattern && enterKeyType == info.enterKeyType && + inputOption == info.inputOption; + } }; } // namespace MiscServices } // namespace OHOS diff --git a/services/src/peruser_session.cpp b/services/src/peruser_session.cpp index ab11c40e..ef4debaf 100644 --- a/services/src/peruser_session.cpp +++ b/services/src/peruser_session.cpp @@ -391,7 +391,7 @@ int32_t PerUserSession::OnSetCoreAndAgent(const sptr &core, co if (client != nullptr) { auto clientInfo = GetClientInfo(client->AsObject()); if (clientInfo != nullptr) { - ret = OnStartInput(clientInfo->client, clientInfo->isShowKeyboard, false); + ret = OnStartInput(clientInfo->client, clientInfo->isShowKeyboard, true); IMSA_HILOGI("start input ret: %{public}d", ret); } } 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 5bffb5f1..bd43436c 100644 --- a/test/unittest/cpp_test/src/input_method_ability_test.cpp +++ b/test/unittest/cpp_test/src/input_method_ability_test.cpp @@ -360,16 +360,16 @@ HWTEST_F(InputMethodAbilityTest, testGetEnterKeyType, TestSize.Level0) HWTEST_F(InputMethodAbilityTest, testGetTextConfig, TestSize.Level0) { IMSA_HILOGI("InputMethodAbility testGetTextConfig START"); - sptr textChangeListener = new TextChangeListener(); + sptr textListener = new TextListener(); TextConfig textConfig; textConfig.inputAttribute = { .inputPattern = 0, .enterKeyType = 1 }; - auto ret = imc_->Attach(textChangeListener, false, textConfig); + auto ret = imc_->Attach(textListener, false, textConfig); TextTotalConfig textTotalConfig; ret = inputMethodAbility_->GetTextConfig(textTotalConfig); EXPECT_EQ(ret, ErrorCode::NO_ERROR); EXPECT_EQ(textTotalConfig.inputAttribute.inputPattern, textConfig.inputAttribute.inputPattern); EXPECT_EQ(textTotalConfig.inputAttribute.enterKeyType, textConfig.inputAttribute.enterKeyType); - textChangeListener = nullptr; + textListener = nullptr; } /** diff --git a/test/unittest/cpp_test/src/input_method_attach_test.cpp b/test/unittest/cpp_test/src/input_method_attach_test.cpp index ca87aec5..59cd0f39 100644 --- a/test/unittest/cpp_test/src/input_method_attach_test.cpp +++ b/test/unittest/cpp_test/src/input_method_attach_test.cpp @@ -21,6 +21,7 @@ #include "input_method_ability.h" #include "input_method_controller.h" #include "tdd_util.h" +#include "text_listener.h" using namespace testing::ext; namespace OHOS { @@ -60,30 +61,6 @@ public: IMSA_HILOGI("EngineListenerImpl OnSetSubtype"); } }; - class TextChangeListenerImpl : public OnTextChangedListener { - public: - void InsertText(const std::u16string &text) override {} - - void DeleteForward(int32_t length) override {} - - void DeleteBackward(int32_t length) override {} - - void SendKeyEventFromInputMethod(const KeyEvent &event) override {} - - void SendKeyboardStatus(const KeyboardStatus &keyboardStatus) override {} - - void SendFunctionKey(const FunctionKey &functionKey) override {} - - void SetKeyboardStatus(bool status) override {} - - void MoveCursor(const Direction direction) override {} - - void HandleSetSelection(int32_t start, int32_t end) override {} - - void HandleExtendAction(int32_t action) override {} - - void HandleSelect(int32_t keyCode, int32_t cursorMoveSkip) override {} - }; static void SetUpTestCase(void) { IMSA_HILOGI("InputMethodAttachTest::SetUpTestCase"); @@ -128,8 +105,8 @@ sptr InputMethodAttachTest::inputMethodAbility_; HWTEST_F(InputMethodAttachTest, testAttach001, TestSize.Level0) { IMSA_HILOGI("test testAttach001 after attach."); - sptr textChangeListenerImpl = new TextChangeListenerImpl(); - auto ret = inputMethodController_->Attach(textChangeListenerImpl); + sptr textListener = new TextListener(); + auto ret = inputMethodController_->Attach(textListener); EXPECT_EQ(ret, ErrorCode::NO_ERROR); int32_t keyType = -1; @@ -151,8 +128,8 @@ HWTEST_F(InputMethodAttachTest, testAttach001, TestSize.Level0) HWTEST_F(InputMethodAttachTest, testAttach002, TestSize.Level0) { IMSA_HILOGI("test testAttach002 after attach."); - sptr textChangeListenerImpl = new TextChangeListenerImpl(); - auto ret = inputMethodController_->Attach(textChangeListenerImpl, false); + sptr textListener = new TextListener(); + auto ret = inputMethodController_->Attach(textListener, false); EXPECT_EQ(ret, ErrorCode::NO_ERROR); int32_t keyType = -1; @@ -174,11 +151,11 @@ HWTEST_F(InputMethodAttachTest, testAttach002, TestSize.Level0) HWTEST_F(InputMethodAttachTest, testAttach003, TestSize.Level0) { IMSA_HILOGI("test testAttach003 after attach."); - sptr textChangeListenerImpl = new TextChangeListenerImpl(); + sptr textListener = new TextListener(); InputAttribute attribute; attribute.inputPattern = 2; attribute.enterKeyType = 1; - auto ret = inputMethodController_->Attach(textChangeListenerImpl, true, attribute); + auto ret = inputMethodController_->Attach(textListener, true, attribute); EXPECT_EQ(ret, ErrorCode::NO_ERROR); int32_t keyType = -1; @@ -199,13 +176,13 @@ HWTEST_F(InputMethodAttachTest, testAttach003, TestSize.Level0) HWTEST_F(InputMethodAttachTest, testAttach004, TestSize.Level0) { IMSA_HILOGI("test testAttach004 after attach."); - sptr textChangeListenerImpl = new TextChangeListenerImpl(); + sptr textListener = new TextListener(); InputAttribute attribute; attribute.inputPattern = 3; attribute.enterKeyType = 2; TextConfig config; config.inputAttribute = attribute; - auto ret = inputMethodController_->Attach(textChangeListenerImpl, false, config); + auto ret = inputMethodController_->Attach(textListener, false, config); EXPECT_EQ(ret, ErrorCode::NO_ERROR); int32_t keyType = -1; @@ -226,7 +203,7 @@ HWTEST_F(InputMethodAttachTest, testAttach004, TestSize.Level0) HWTEST_F(InputMethodAttachTest, testAttach005, TestSize.Level0) { IMSA_HILOGI("test testAttach005 after attach."); - sptr textChangeListenerImpl = new TextChangeListenerImpl(); + sptr textListener = new TextListener(); InputAttribute attribute; attribute.inputPattern = 3; attribute.enterKeyType = 2; @@ -243,7 +220,7 @@ HWTEST_F(InputMethodAttachTest, testAttach005, TestSize.Level0) selectionRange.end = 2; config.range = selectionRange; config.windowId = 10; - auto ret = inputMethodController_->Attach(textChangeListenerImpl, true, config); + auto ret = inputMethodController_->Attach(textListener, true, config); EXPECT_EQ(ret, ErrorCode::NO_ERROR); int32_t keyType = -1; @@ -258,8 +235,7 @@ HWTEST_F(InputMethodAttachTest, testAttach005, TestSize.Level0) TextTotalConfig textConfig; ret = inputMethodAbility_->GetTextConfig(textConfig); EXPECT_EQ(ret, ErrorCode::NO_ERROR); - EXPECT_EQ(textConfig.inputAttribute.inputPattern, config.inputAttribute.inputPattern); - EXPECT_EQ(textConfig.inputAttribute.enterKeyType, config.inputAttribute.enterKeyType); + EXPECT_EQ(textConfig.inputAttribute, config.inputAttribute); EXPECT_EQ(textConfig.windowId, config.windowId); EXPECT_EQ(textConfig.cursorInfo, config.cursorInfo); EXPECT_EQ(textConfig.textSelection.newBegin, config.range.start); @@ -293,8 +269,8 @@ HWTEST_F(InputMethodAttachTest, testOnConfigurationChangeWithOutAttach, TestSize HWTEST_F(InputMethodAttachTest, testOnConfigurationChange, TestSize.Level0) { IMSA_HILOGI("test OnConfigurationChange after attach."); - sptr textChangeListenerImpl = new TextChangeListenerImpl(); - auto ret = inputMethodController_->Attach(textChangeListenerImpl); + sptr textListener = new TextListener(); + auto ret = inputMethodController_->Attach(textListener); EXPECT_EQ(ret, ErrorCode::NO_ERROR); Configuration config; @@ -322,7 +298,7 @@ HWTEST_F(InputMethodAttachTest, testOnConfigurationChange, TestSize.Level0) HWTEST_F(InputMethodAttachTest, testGetTextConfig, TestSize.Level0) { IMSA_HILOGI("test OnConfigurationChange001 after attach."); - sptr textChangeListenerImpl = new TextChangeListenerImpl(); + sptr textListener = new TextListener(); InputAttribute attribute; attribute.inputPattern = 3; attribute.enterKeyType = 2; @@ -339,18 +315,14 @@ HWTEST_F(InputMethodAttachTest, testGetTextConfig, TestSize.Level0) selectionRange.end = 2; config.range = selectionRange; config.windowId = 10; - auto ret = inputMethodController_->Attach(textChangeListenerImpl, false, config); + auto ret = inputMethodController_->Attach(textListener, false, config); EXPECT_EQ(ret, ErrorCode::NO_ERROR); TextTotalConfig totalConfig; ret = inputMethodAbility_->GetTextConfig(totalConfig); EXPECT_EQ(ret, ErrorCode::NO_ERROR); - EXPECT_EQ(totalConfig.inputAttribute.inputPattern, attribute.inputPattern); - EXPECT_EQ(totalConfig.inputAttribute.enterKeyType, attribute.enterKeyType); - EXPECT_EQ(totalConfig.cursorInfo.height, cursorInfo.height); - EXPECT_EQ(totalConfig.cursorInfo.width, cursorInfo.width); - EXPECT_EQ(totalConfig.cursorInfo.left, cursorInfo.left); - EXPECT_EQ(totalConfig.cursorInfo.top, cursorInfo.top); + EXPECT_EQ(totalConfig.inputAttribute, attribute); + EXPECT_EQ(totalConfig.cursorInfo, cursorInfo); EXPECT_EQ(totalConfig.textSelection.newBegin, selectionRange.start); EXPECT_EQ(totalConfig.textSelection.newEnd, selectionRange.end); EXPECT_EQ(totalConfig.textSelection.oldBegin, 0); @@ -366,8 +338,8 @@ HWTEST_F(InputMethodAttachTest, testGetTextConfig, TestSize.Level0) HWTEST_F(InputMethodAttachTest, testOnCursorUpdateAfterAttach001, TestSize.Level0) { IMSA_HILOGI("test testOnCursorUpdateAfterAttach001."); - sptr textChangeListenerImpl = new TextChangeListenerImpl(); - auto ret = inputMethodController_->Attach(textChangeListenerImpl); + sptr textListener = new TextListener(); + auto ret = inputMethodController_->Attach(textListener); EXPECT_EQ(ret, ErrorCode::NO_ERROR); CursorInfo cursorInfo = { .top = 5, .left = 5, .height = 5, .width = 0.8 }; ret = inputMethodController_->OnCursorUpdate(cursorInfo); @@ -389,14 +361,14 @@ HWTEST_F(InputMethodAttachTest, testOnCursorUpdateAfterAttach001, TestSize.Level HWTEST_F(InputMethodAttachTest, testOnCursorUpdateAfterAttach002, TestSize.Level0) { IMSA_HILOGI("test testOnCursorUpdateAfterAttach002."); - sptr textChangeListenerImpl = new TextChangeListenerImpl(); + sptr textListener = new TextListener(); InputAttribute attribute; attribute.inputPattern = 3; attribute.enterKeyType = 2; TextConfig config; config.inputAttribute = attribute; config.cursorInfo = { .top = 1, .left = 1, .height = 1, .width = 0.4 }; - auto ret = inputMethodController_->Attach(textChangeListenerImpl, true, config); + auto ret = inputMethodController_->Attach(textListener, true, config); EXPECT_EQ(ret, ErrorCode::NO_ERROR); CursorInfo cursorInfo = { .top = 5, .left = 5, .height = 5, .width = 0.8 }; ret = inputMethodController_->OnCursorUpdate(cursorInfo); @@ -404,10 +376,7 @@ HWTEST_F(InputMethodAttachTest, testOnCursorUpdateAfterAttach002, TestSize.Level TextTotalConfig totalConfig; ret = inputMethodAbility_->GetTextConfig(totalConfig); EXPECT_EQ(ret, ErrorCode::NO_ERROR); - EXPECT_EQ(totalConfig.cursorInfo.height, config.cursorInfo.height); - EXPECT_EQ(totalConfig.cursorInfo.width, config.cursorInfo.width); - EXPECT_EQ(totalConfig.cursorInfo.left, config.cursorInfo.left); - EXPECT_EQ(totalConfig.cursorInfo.top, config.cursorInfo.top); + EXPECT_EQ(totalConfig.cursorInfo, config.cursorInfo); } /** @@ -418,14 +387,14 @@ HWTEST_F(InputMethodAttachTest, testOnCursorUpdateAfterAttach002, TestSize.Level HWTEST_F(InputMethodAttachTest, testOnSelectionChangeAfterAttach002, TestSize.Level0) { IMSA_HILOGI("test testOnSelectionChangeAfterAttach002."); - sptr textChangeListenerImpl = new TextChangeListenerImpl(); + sptr textListener = new TextListener(); InputAttribute attribute; attribute.inputPattern = 3; attribute.enterKeyType = 2; TextConfig config; config.inputAttribute = attribute; config.range = { .start = 1, .end = 2 }; - auto ret = inputMethodController_->Attach(textChangeListenerImpl, false, config); + auto ret = inputMethodController_->Attach(textListener, false, config); EXPECT_EQ(ret, ErrorCode::NO_ERROR); int start = 0; int end = 1; @@ -449,8 +418,8 @@ HWTEST_F(InputMethodAttachTest, testOnSelectionChangeAfterAttach002, TestSize.Le HWTEST_F(InputMethodAttachTest, testOnConfigurationChangeAfterAttach001, TestSize.Level0) { IMSA_HILOGI("test testOnConfigurationChangeAfterAttach001."); - sptr textChangeListenerImpl = new TextChangeListenerImpl(); - auto ret = inputMethodController_->Attach(textChangeListenerImpl); + sptr textListener = new TextListener(); + auto ret = inputMethodController_->Attach(textListener); EXPECT_EQ(ret, ErrorCode::NO_ERROR); Configuration config; @@ -462,8 +431,8 @@ HWTEST_F(InputMethodAttachTest, testOnConfigurationChangeAfterAttach001, TestSiz TextTotalConfig totalConfig; ret = inputMethodAbility_->GetTextConfig(totalConfig); EXPECT_EQ(ret, ErrorCode::NO_ERROR); - EXPECT_EQ(totalConfig.inputAttribute.inputPattern, 1); - EXPECT_EQ(totalConfig.inputAttribute.enterKeyType, 0); + EXPECT_EQ(totalConfig.inputAttribute.inputPattern, static_cast(TextInputType::DATETIME)); + EXPECT_EQ(totalConfig.inputAttribute.enterKeyType, static_cast(EnterKeyType::NEXT)); } /** @@ -474,13 +443,13 @@ HWTEST_F(InputMethodAttachTest, testOnConfigurationChangeAfterAttach001, TestSiz HWTEST_F(InputMethodAttachTest, testOnConfigurationChangeAfterAttach002, TestSize.Level0) { IMSA_HILOGI("test testOnConfigurationChangeAfterAttach002."); - sptr textChangeListenerImpl = new TextChangeListenerImpl(); + sptr textListener = new TextListener(); InputAttribute attribute; attribute.inputPattern = 3; attribute.enterKeyType = 2; TextConfig config; config.inputAttribute = attribute; - auto ret = inputMethodController_->Attach(textChangeListenerImpl, false, config); + auto ret = inputMethodController_->Attach(textListener, false, config); EXPECT_EQ(ret, ErrorCode::NO_ERROR); Configuration configuration; @@ -492,8 +461,8 @@ HWTEST_F(InputMethodAttachTest, testOnConfigurationChangeAfterAttach002, TestSiz TextTotalConfig totalConfig; ret = inputMethodAbility_->GetTextConfig(totalConfig); EXPECT_EQ(ret, ErrorCode::NO_ERROR); - EXPECT_EQ(totalConfig.inputAttribute.inputPattern, static_cast(config.inputAttribute.inputPattern)); - EXPECT_EQ(totalConfig.inputAttribute.enterKeyType, static_cast(config.inputAttribute.enterKeyType)); + EXPECT_EQ(totalConfig.inputAttribute.inputPattern, static_cast(configuration.GetTextInputType())); + EXPECT_EQ(totalConfig.inputAttribute.enterKeyType, static_cast(configuration.GetEnterKeyType())); } /** @@ -504,14 +473,14 @@ HWTEST_F(InputMethodAttachTest, testOnConfigurationChangeAfterAttach002, TestSiz HWTEST_F(InputMethodAttachTest, testSetCallingWindowAfterAttach002, TestSize.Level0) { IMSA_HILOGI("test testSetCallingWindowAfterAttach002."); - sptr textChangeListenerImpl = new TextChangeListenerImpl(); + sptr textListener = new TextListener(); InputAttribute attribute; attribute.inputPattern = 3; attribute.enterKeyType = 2; TextConfig config; config.inputAttribute = attribute; config.windowId = 88; - auto ret = inputMethodController_->Attach(textChangeListenerImpl, false, config); + auto ret = inputMethodController_->Attach(textListener, false, config); EXPECT_EQ(ret, ErrorCode::NO_ERROR); uint32_t windowId = 99; @@ -532,8 +501,8 @@ HWTEST_F(InputMethodAttachTest, testSetCallingWindowAfterAttach002, TestSize.Lev HWTEST_F(InputMethodAttachTest, testOnCursorUpdate001, TestSize.Level0) { IMSA_HILOGI("test testOnCursorUpdate001."); - sptr textChangeListenerImpl = new TextChangeListenerImpl(); - auto ret = inputMethodController_->Attach(textChangeListenerImpl); + sptr textListener = new TextListener(); + auto ret = inputMethodController_->Attach(textListener); EXPECT_EQ(ret, ErrorCode::NO_ERROR); CursorInfo cursorInfo = { .top = 5, .left = 5, .height = 5, .width = 0.8 }; ret = inputMethodController_->OnCursorUpdate(cursorInfo); @@ -546,16 +515,13 @@ HWTEST_F(InputMethodAttachTest, testOnCursorUpdate001, TestSize.Level0) config.inputAttribute = attribute; CursorInfo cursorInfo2 = { .top = 10, .left = 9, .width = 8, .height = 7 }; config.cursorInfo = cursorInfo2; - ret = inputMethodController_->Attach(textChangeListenerImpl, false, config); + ret = inputMethodController_->Attach(textListener, false, config); EXPECT_EQ(ret, ErrorCode::NO_ERROR); TextTotalConfig totalConfig; ret = inputMethodAbility_->GetTextConfig(totalConfig); EXPECT_EQ(ret, ErrorCode::NO_ERROR); - EXPECT_EQ(totalConfig.cursorInfo.height, cursorInfo2.height); - EXPECT_EQ(totalConfig.cursorInfo.width, cursorInfo2.width); - EXPECT_EQ(totalConfig.cursorInfo.left, cursorInfo2.left); - EXPECT_EQ(totalConfig.cursorInfo.top, cursorInfo2.top); + EXPECT_EQ(totalConfig.cursorInfo, cursorInfo2); } /** @@ -566,8 +532,8 @@ HWTEST_F(InputMethodAttachTest, testOnCursorUpdate001, TestSize.Level0) HWTEST_F(InputMethodAttachTest, testOnSelectionChange, TestSize.Level0) { IMSA_HILOGI("test testOnSelectionChange."); - sptr textChangeListenerImpl = new TextChangeListenerImpl(); - auto ret = inputMethodController_->Attach(textChangeListenerImpl); + sptr textListener = new TextListener(); + auto ret = inputMethodController_->Attach(textListener); EXPECT_EQ(ret, ErrorCode::NO_ERROR); int start = 0; int end = 1; @@ -581,7 +547,7 @@ HWTEST_F(InputMethodAttachTest, testOnSelectionChange, TestSize.Level0) config.inputAttribute = attribute; config.range.start = 10; config.range.end = 20; - ret = inputMethodController_->Attach(textChangeListenerImpl, false, config); + ret = inputMethodController_->Attach(textListener, false, config); EXPECT_EQ(ret, ErrorCode::NO_ERROR); TextTotalConfig totalConfig; @@ -601,8 +567,8 @@ HWTEST_F(InputMethodAttachTest, testOnSelectionChange, TestSize.Level0) HWTEST_F(InputMethodAttachTest, testOnConfigurationChange002, TestSize.Level0) { IMSA_HILOGI("test testOnConfigurationChange002."); - sptr textChangeListenerImpl = new TextChangeListenerImpl(); - auto ret = inputMethodController_->Attach(textChangeListenerImpl); + sptr textListener = new TextListener(); + auto ret = inputMethodController_->Attach(textListener); EXPECT_EQ(ret, ErrorCode::NO_ERROR); Configuration configuration; @@ -618,15 +584,14 @@ HWTEST_F(InputMethodAttachTest, testOnConfigurationChange002, TestSize.Level0) config.inputAttribute = attribute; config.inputAttribute.enterKeyType = 5; config.inputAttribute.inputPattern = 5; - ret = inputMethodController_->Attach(textChangeListenerImpl, false, config); + ret = inputMethodController_->Attach(textListener, false, config); EXPECT_EQ(ret, ErrorCode::NO_ERROR); TextTotalConfig totalConfig; ret = inputMethodAbility_->GetTextConfig(totalConfig); EXPECT_EQ(ret, ErrorCode::NO_ERROR); - EXPECT_EQ(totalConfig.inputAttribute.inputPattern, config.inputAttribute.inputPattern); - EXPECT_EQ(totalConfig.inputAttribute.enterKeyType, config.inputAttribute.enterKeyType); + EXPECT_EQ(totalConfig.inputAttribute, config.inputAttribute); } /** @@ -637,8 +602,8 @@ HWTEST_F(InputMethodAttachTest, testOnConfigurationChange002, TestSize.Level0) HWTEST_F(InputMethodAttachTest, testSetCallingWindow, TestSize.Level0) { IMSA_HILOGI("test testSetCallingWindow."); - sptr textChangeListenerImpl = new TextChangeListenerImpl(); - auto ret = inputMethodController_->Attach(textChangeListenerImpl); + sptr textListener = new TextListener(); + auto ret = inputMethodController_->Attach(textListener); EXPECT_EQ(ret, ErrorCode::NO_ERROR); uint32_t windowId = 88; @@ -650,7 +615,7 @@ HWTEST_F(InputMethodAttachTest, testSetCallingWindow, TestSize.Level0) attribute.enterKeyType = 2; TextConfig config; config.windowId = 77; - ret = inputMethodController_->Attach(textChangeListenerImpl, false, config); + ret = inputMethodController_->Attach(textListener, false, config); EXPECT_EQ(ret, ErrorCode::NO_ERROR); TextTotalConfig totalConfig; 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 ff032b90..2a369c7b 100644 --- a/test/unittest/cpp_test/src/input_method_controller_test.cpp +++ b/test/unittest/cpp_test/src/input_method_controller_test.cpp @@ -315,7 +315,8 @@ constexpr uint32_t KEY_EVENT_DELAY_TIME = 100; { IMSA_HILOGI("InputMethodControllerTest::WaitRemoteDiedCallback"); std::unique_lock lock(onRemoteSaDiedMutex_); - return onRemoteSaDiedCv_.wait_for(lock, std::chrono::seconds(1)) != std::cv_status::timeout; + // 2 means wait 2 seconds. + return onRemoteSaDiedCv_.wait_for(lock, std::chrono::seconds(2)) != std::cv_status::timeout; } bool InputMethodControllerTest::CheckKeyEvent(std::shared_ptr keyEvent) @@ -745,8 +746,6 @@ constexpr uint32_t KEY_EVENT_DELAY_TIME = 100; EXPECT_EQ( InputMethodControllerTest::inputAttribute_.enterKeyType, static_cast(info.GetEnterKeyType())); } - ret = inputMethodController_->Close(); - EXPECT_EQ(ret, ErrorCode::NO_ERROR); } /** -- Gitee From bd6fc25da24befe76beea819bd6bd1aee111b41a Mon Sep 17 00:00:00 2001 From: Hollokin Date: Sun, 9 Jul 2023 18:33:43 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=E5=8E=BB=E6=8E=89ShowInputWindow=E4=B8=AD?= =?UTF-8?q?=E5=AF=B9ShowPanel=E7=9A=84=E7=AD=89=E5=BE=85=EF=BC=8C=E5=9B=A0?= =?UTF-8?q?=E4=B8=BA=E5=AE=83=E5=AF=B9=E4=BA=8E=E8=BE=93=E5=85=A5=E6=B3=95?= =?UTF-8?q?=E8=8B=B1=E8=AF=AD=E7=9B=B4=E6=8E=A5=E5=88=9B=E5=BB=BAwindow?= =?UTF-8?q?=E7=9A=84=E6=83=85=E5=86=B5=E4=B8=8B=EF=BC=8C=E6=AF=8F=E6=AC=A1?= =?UTF-8?q?Attach=E9=83=BD=E5=A4=9A100ms=E7=9A=84=E7=AD=89=E5=BE=85?= =?UTF-8?q?=E6=97=B6=E9=97=B4=EF=BC=8C=E5=BE=88=E8=80=97=E6=97=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Hollokin --- .../js_get_input_method_controller.cpp | 13 +++++---- .../include/input_method_ability.h | 1 - .../src/input_method_ability.cpp | 29 ++++++------------- .../src/input_method_panel.cpp | 2 +- 4 files changed, 17 insertions(+), 28 deletions(-) diff --git a/frameworks/js/napi/inputmethodclient/js_get_input_method_controller.cpp b/frameworks/js/napi/inputmethodclient/js_get_input_method_controller.cpp index 2a6c9585..1ed03f4c 100644 --- a/frameworks/js/napi/inputmethodclient/js_get_input_method_controller.cpp +++ b/frameworks/js/napi/inputmethodclient/js_get_input_method_controller.cpp @@ -491,20 +491,21 @@ bool JsGetInputMethodController::GetValue(napi_env env, napi_value in, TextConfi napi_value cursorInfoResult = nullptr; status = JsUtils::GetValue(env, in, "cursorInfo", cursorInfoResult); + bool result = false; if (status == napi_ok) { - ret = JsGetInputMethodController::GetValue(env, cursorInfoResult, out.cursorInfo); - IMSA_HILOGE("get cursorInfo end, ret = %{public}d", ret); + result = JsGetInputMethodController::GetValue(env, cursorInfoResult, out.cursorInfo); + IMSA_HILOGE("get cursorInfo end, ret = %{public}d", result); } napi_value rangeResult = nullptr; status = JsUtils::GetValue(env, in, "selection", rangeResult); if (status == napi_ok) { - ret = JsGetInputMethodController::GetValue(env, rangeResult, out.range); - IMSA_HILOGE("get selectionRange end, ret = %{public}d", ret); + result = JsGetInputMethodController::GetValue(env, rangeResult, out.range); + IMSA_HILOGE("get selectionRange end, ret = %{public}d", result); } - ret = JsUtil::Object::ReadProperty(env, in, "windowId", out.windowId); - IMSA_HILOGE("get windowId end, ret = %{public}d", ret); + result = JsUtil::Object::ReadProperty(env, in, "windowId", out.windowId); + IMSA_HILOGE("get windowId end, ret = %{public}d", result); return ret; } diff --git a/frameworks/native/inputmethod_ability/include/input_method_ability.h b/frameworks/native/inputmethod_ability/include/input_method_ability.h index 2209b615..bf64d933 100644 --- a/frameworks/native/inputmethod_ability/include/input_method_ability.h +++ b/frameworks/native/inputmethod_ability/include/input_method_ability.h @@ -123,7 +123,6 @@ private: void OnConfigurationChange(Message *msg); void ShowInputWindow(bool isShowKeyboard); void DismissInputWindow(); - void EnsureToShowPanel(); void OnTextConfigChange(const TextTotalConfig &textConfig); bool isImeReady_{ false }; InputStartNotifier notifier_; diff --git a/frameworks/native/inputmethod_ability/src/input_method_ability.cpp b/frameworks/native/inputmethod_ability/src/input_method_ability.cpp index 18d7cc44..334cc2f3 100644 --- a/frameworks/native/inputmethod_ability/src/input_method_ability.cpp +++ b/frameworks/native/inputmethod_ability/src/input_method_ability.cpp @@ -37,8 +37,6 @@ class MessageHandler; using namespace MessageID; sptr InputMethodAbility::instance_; std::mutex InputMethodAbility::instanceLock_; -constexpr int32_t RETRY_COUNT = 10; -constexpr int32_t WAIT_GAP = 10; constexpr double INVALID_CURSOR_VALUE = -1.0; constexpr int32_t INVALID_SELECTION_VALUE = -1; InputMethodAbility::InputMethodAbility() : stop_(false) @@ -388,23 +386,6 @@ void InputMethodAbility::OnConfigurationChange(Message *msg) kdListener_->OnEditorAttributeChange(attribute); } -void InputMethodAbility::EnsureToShowPanel() -{ - for (int i = 0; i < RETRY_COUNT; ++i) { - auto result = panels_.Find(SOFT_KEYBOARD); - if (result.first) { - IMSA_HILOGI("find SOFT_KEYBOARD panel."); - auto ret = result.second->ShowPanel(); - if (ret != ErrorCode::NO_ERROR) { - IMSA_HILOGE("Show panel failed, ret = %{public}d.", ret); - } - return; - } - IMSA_HILOGE("Not find SOFT_KEYBOARD panel, count = %{public}d", i); - std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_GAP)); - } -} - void InputMethodAbility::ShowInputWindow(bool isShowKeyboard) { IMSA_HILOGI("InputMethodAbility::ShowInputWindow"); @@ -430,7 +411,15 @@ void InputMethodAbility::ShowInputWindow(bool isShowKeyboard) return; } channel->SendKeyboardStatus(KEYBOARD_SHOW); - EnsureToShowPanel(); + auto result = panels_.Find(SOFT_KEYBOARD); + if (result.first) { + IMSA_HILOGI("find SOFT_KEYBOARD panel."); + auto ret = result.second->ShowPanel(); + if (ret != ErrorCode::NO_ERROR) { + IMSA_HILOGE("Show panel failed, ret = %{public}d.", ret); + } + return; + } } void InputMethodAbility::OnTextConfigChange(const TextTotalConfig &textConfig) diff --git a/frameworks/native/inputmethod_ability/src/input_method_panel.cpp b/frameworks/native/inputmethod_ability/src/input_method_panel.cpp index 0f7aedae..5464690c 100644 --- a/frameworks/native/inputmethod_ability/src/input_method_panel.cpp +++ b/frameworks/native/inputmethod_ability/src/input_method_panel.cpp @@ -275,7 +275,7 @@ void InputMethodPanel::SetPanelStatusListener( return; } panelStatusListener_ = std::move(statusListener); - if (IsShowing()) { + if (window_ != nullptr && IsShowing()) { panelStatusListener_->OnPanelStatus(windowId_, true); } } -- Gitee