From cbc0924486a1929795ee24bc97c0166c2b196764 Mon Sep 17 00:00:00 2001 From: Hollokin Date: Fri, 25 Nov 2022 20:26:12 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90TDD=E3=80=91ability=E7=9B=B8=E5=85=B3T?= =?UTF-8?q?DD=E7=94=A8=E4=BE=8B=E8=A1=A5=E5=85=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Hollokin --- .../include/input_method_ability.h | 1 - .../include/input_method_setting_listener.h | 6 +- .../src/input_method_controller.cpp | 2 +- .../js/napi/inputmethodclient/js_utils.cpp | 165 +++--- services/include/global.h | 93 ++-- services/src/global.cpp | 22 +- unitest/BUILD.gn | 3 + unitest/src/input_method_ability_test.cpp | 511 +++++++++++++----- 8 files changed, 504 insertions(+), 299 deletions(-) diff --git a/frameworks/inputmethod_ability/include/input_method_ability.h b/frameworks/inputmethod_ability/include/input_method_ability.h index 76dcbb02..8afb464e 100644 --- a/frameworks/inputmethod_ability/include/input_method_ability.h +++ b/frameworks/inputmethod_ability/include/input_method_ability.h @@ -43,7 +43,6 @@ public: InputMethodAbility(); ~InputMethodAbility(); static sptr GetInstance(); - sptr OnConnect(); int32_t InsertText(const std::string text); void setImeListener(std::shared_ptr imeListener); void setKdListener(std::shared_ptr kdListener); diff --git a/frameworks/inputmethod_controller/include/input_method_setting_listener.h b/frameworks/inputmethod_controller/include/input_method_setting_listener.h index 308d5a11..1203fca7 100644 --- a/frameworks/inputmethod_controller/include/input_method_setting_listener.h +++ b/frameworks/inputmethod_controller/include/input_method_setting_listener.h @@ -13,8 +13,8 @@ * limitations under the License. */ -#ifndef INPUTMETHOD_IMF_INPUT_METHOD_ENGINE_LISTENER_H -#define INPUTMETHOD_IMF_INPUT_METHOD_ENGINE_LISTENER_H +#ifndef INPUTMETHOD_IMF_INPUT_METHOD_SETTING_LISTENER_H +#define INPUTMETHOD_IMF_INPUT_METHOD_SETTING_LISTENER_H #include "input_method_property.h" @@ -28,4 +28,4 @@ public: } // namespace MiscServices } // namespace OHOS -#endif // INPUTMETHOD_IMF_INPUT_METHOD_ENGINE_LISTENER_H +#endif // INPUTMETHOD_IMF_INPUT_METHOD_SETTING_LISTENER_H diff --git a/frameworks/inputmethod_controller/src/input_method_controller.cpp b/frameworks/inputmethod_controller/src/input_method_controller.cpp index 57f216a9..39b6f359 100644 --- a/frameworks/inputmethod_controller/src/input_method_controller.cpp +++ b/frameworks/inputmethod_controller/src/input_method_controller.cpp @@ -502,7 +502,7 @@ using namespace MessageID; int32_t InputMethodController::GetTextAfterCursor(int32_t number, std::u16string &text) { - IMSA_HILOGI("InputMethodController::GetTextBeforeCursor"); + IMSA_HILOGI("InputMethodController::GetTextAfterCursor"); if (!mTextString.empty() && mTextString.size() <= INT_MAX) { int32_t endPos = (mSelectNewEnd + number < static_cast(mTextString.size())) ? (mSelectNewEnd + number) diff --git a/interfaces/kits/js/napi/inputmethodclient/js_utils.cpp b/interfaces/kits/js/napi/inputmethodclient/js_utils.cpp index 2a3b644e..d9388adb 100644 --- a/interfaces/kits/js/napi/inputmethodclient/js_utils.cpp +++ b/interfaces/kits/js/napi/inputmethodclient/js_utils.cpp @@ -20,9 +20,7 @@ namespace MiscServices { const std::map JsUtils::ERROR_CODE_MAP = { { ErrorCode::ERROR_STATUS_PERMISSION_DENIED, EXCEPTION_PERMISSION }, { ErrorCode::ERROR_REMOTE_IME_DIED, EXCEPTION_IMENGINE }, - { ErrorCode::ERROR_RESTART_IME_FAILED, EXCEPTION_IMENGINE }, { ErrorCode::ERROR_REMOTE_CLIENT_DIED, EXCEPTION_IMCLIENT }, - { ErrorCode::ERROR_CLIENT_DUPLICATED, EXCEPTION_IMCLIENT }, { ErrorCode::ERROR_CLIENT_NOT_FOUND, EXCEPTION_IMCLIENT }, { ErrorCode::ERROR_CLIENT_NULL_POINTER, EXCEPTION_IMCLIENT }, { ErrorCode::ERROR_NOT_IME_PACKAGE, EXCEPTION_SETTINGS }, @@ -42,19 +40,11 @@ const std::map JsUtils::ERROR_CODE_MAP = { { ErrorCode::ERROR_IME_BIND_FAILED, EXCEPTION_IMMS }, { ErrorCode::ERROR_IME_UNBIND_FAILED, EXCEPTION_IMMS }, { ErrorCode::ERROR_IME_START_FAILED, EXCEPTION_IMMS }, - { ErrorCode::ERROR_IME_STOP_FAILED, EXCEPTION_IMMS }, { ErrorCode::ERROR_KBD_SHOW_FAILED, EXCEPTION_IMMS }, { ErrorCode::ERROR_KBD_HIDE_FAILED, EXCEPTION_IMMS }, { ErrorCode::ERROR_IME_NOT_STARTED, EXCEPTION_IMMS }, { ErrorCode::ERROR_KBD_IS_OCCUPIED, EXCEPTION_IMMS }, { ErrorCode::ERROR_KBD_IS_NOT_SHOWING, EXCEPTION_IMMS }, - { ErrorCode::ERROR_IME_ALREADY_STARTED, EXCEPTION_IMMS }, - { ErrorCode::ERROR_NO_NEXT_IME, EXCEPTION_IMMS }, - { ErrorCode::ERROR_CLIENTWINDOW_NOT_FOCUSED, EXCEPTION_IMMS }, - { ErrorCode::ERROR_CLIENT_NOT_WINDOW, EXCEPTION_IMMS }, - { ErrorCode::ERROR_IME_PROPERTY_MARSHALL, EXCEPTION_IMMS }, - { ErrorCode::ERROR_GETTING_CURRENT_IME, EXCEPTION_IMMS }, - { ErrorCode::ERROR_LIST_IME, EXCEPTION_IMMS }, { ErrorCode::ERROR_EX_NULL_POINTER, EXCEPTION_IMMS }, { ErrorCode::ERROR_PERSIST_CONFIG, EXCEPTION_CONFPERSIST }, { ErrorCode::ERROR_PACKAGE_MANAGER, EXCEPTION_PACKAGEMANAGER }, @@ -83,86 +73,85 @@ const std::map JsUtils::ERROR_CODE_MAP = { }; const std::map JsUtils::ERROR_CODE_CONVERT_MESSAGE_MAP = { - { EXCEPTION_PERMISSION, "the permissions check fails." }, - { EXCEPTION_PARAMCHECK, "the parameters check fails." }, - { EXCEPTION_UNSUPPORTED, "call unsupported api." }, - { EXCEPTION_PACKAGEMANAGER, "package manager error." }, - { EXCEPTION_IMENGINE, "input method engine error." }, - { EXCEPTION_IMCLIENT, "input method client error." }, - { EXCEPTION_KEYEVENT, "key event processing error." }, - { EXCEPTION_CONFPERSIST, "configuration persisting error." }, - { EXCEPTION_CONTROLLER, "input method controller error." }, - { EXCEPTION_SETTINGS, "input method settings extension error." }, - { EXCEPTION_IMMS, "input method manager service error." }, - { EXCEPTION_OTHERS, "others error." }, - }; + { EXCEPTION_PERMISSION, "the permissions check fails." }, + { EXCEPTION_PARAMCHECK, "the parameters check fails." }, + { EXCEPTION_UNSUPPORTED, "call unsupported api." }, + { EXCEPTION_PACKAGEMANAGER, "package manager error." }, + { EXCEPTION_IMENGINE, "input method engine error." }, + { EXCEPTION_IMCLIENT, "input method client error." }, + { EXCEPTION_KEYEVENT, "key event processing error." }, + { EXCEPTION_CONFPERSIST, "configuration persisting error." }, + { EXCEPTION_CONTROLLER, "input method controller error." }, + { EXCEPTION_SETTINGS, "input method settings extension error." }, + { EXCEPTION_IMMS, "input method manager service error." }, + { EXCEPTION_OTHERS, "others error." }, +}; - const std::map JsUtils::PARAMETER_TYPE = { - { TYPE_UNDEFINED, "napi_undefine." }, - { TYPE_NULL, "napi_null." }, - { TYPE_BOOLEAN, "napi_boolean." }, - { TYPE_NUMBER, "napi_number." }, - { TYPE_STRING, "napi_string." }, - { TYPE_SYMBOL, "napi_symbol." }, - { TYPE_OBJECT, "napi_object." }, - { TYPE_FUNCTION, "napi_function." }, - { TYPE_EXTERNAL, "napi_external." }, - { TYPE_BIGINT, "napi_bigint." }, - }; +const std::map JsUtils::PARAMETER_TYPE = { + { TYPE_UNDEFINED, "napi_undefine." }, + { TYPE_NULL, "napi_null." }, + { TYPE_BOOLEAN, "napi_boolean." }, + { TYPE_NUMBER, "napi_number." }, + { TYPE_STRING, "napi_string." }, + { TYPE_SYMBOL, "napi_symbol." }, + { TYPE_OBJECT, "napi_object." }, + { TYPE_FUNCTION, "napi_function." }, + { TYPE_EXTERNAL, "napi_external." }, + { TYPE_BIGINT, "napi_bigint." }, +}; - void JsUtils::ThrowException(napi_env env, int32_t err, const std::string &msg, TypeCode type) - { - std::string errMsg = ToMessage(err); - if (type == TypeCode::TYPE_NONE) { - errMsg = errMsg + msg; - IMSA_HILOGE("THROW_PARAMTER_ERROR message: %{public}s", errMsg.c_str()); - } else { - auto iter = PARAMETER_TYPE.find(type); - if (iter != PARAMETER_TYPE.end()) { - errMsg = errMsg + "The type of " + msg + " must be " + iter->second; - IMSA_HILOGE("THROW_PARAMTER_TYPE_ERROR message: %{public}s", errMsg.c_str()); - } +void JsUtils::ThrowException(napi_env env, int32_t err, const std::string &msg, TypeCode type) +{ + std::string errMsg = ToMessage(err); + if (type == TypeCode::TYPE_NONE) { + errMsg = errMsg + msg; + IMSA_HILOGE("THROW_PARAMTER_ERROR message: %{public}s", errMsg.c_str()); + } else { + auto iter = PARAMETER_TYPE.find(type); + if (iter != PARAMETER_TYPE.end()) { + errMsg = errMsg + "The type of " + msg + " must be " + iter->second; + IMSA_HILOGE("THROW_PARAMTER_TYPE_ERROR message: %{public}s", errMsg.c_str()); } - napi_throw_error(env, std::to_string(err).c_str(), errMsg.c_str()); - } - - napi_value JsUtils::ToError(napi_env env, int32_t code) - { - IMSA_HILOGE("ToError start"); - napi_value errorObj; - NAPI_CALL(env, napi_create_object(env, &errorObj)); - napi_value errorCode = nullptr; - NAPI_CALL( - env, napi_create_string_utf8(env, std::to_string(Convert(code)).c_str(), NAPI_AUTO_LENGTH, &errorCode)); - napi_value errorMessage = nullptr; - NAPI_CALL(env, napi_create_string_utf8(env, ToMessage(Convert(code)).c_str(), NAPI_AUTO_LENGTH, &errorMessage)); - NAPI_CALL(env, napi_set_named_property(env, errorObj, "code", errorCode)); - NAPI_CALL(env, napi_set_named_property(env, errorObj, "message", errorMessage)); - IMSA_HILOGE("ToError end"); - return errorObj; - } - - int32_t JsUtils::Convert(int32_t code) - { - IMSA_HILOGI("Convert start"); - auto iter = ERROR_CODE_MAP.find(code); - if (iter != ERROR_CODE_MAP.end()) { - IMSA_HILOGE("ErrorCode: %{public}d", iter->second); - return iter->second; - } - IMSA_HILOGI("Convert end"); - return ERROR_CODE_QUERY_FAILED; - } - - const std::string JsUtils::ToMessage(int32_t code) - { - IMSA_HILOGI("ToMessage start"); - auto iter = ERROR_CODE_CONVERT_MESSAGE_MAP.find(code); - if (iter != ERROR_CODE_CONVERT_MESSAGE_MAP.end()) { - IMSA_HILOGI("ErrorMessage: %{public}s", (iter->second).c_str()); - return iter->second; - } - return "error is out of definition."; } + napi_throw_error(env, std::to_string(err).c_str(), errMsg.c_str()); } -} \ No newline at end of file + +napi_value JsUtils::ToError(napi_env env, int32_t code) +{ + IMSA_HILOGE("ToError start"); + napi_value errorObj; + NAPI_CALL(env, napi_create_object(env, &errorObj)); + napi_value errorCode = nullptr; + NAPI_CALL(env, napi_create_string_utf8(env, std::to_string(Convert(code)).c_str(), NAPI_AUTO_LENGTH, &errorCode)); + napi_value errorMessage = nullptr; + NAPI_CALL(env, napi_create_string_utf8(env, ToMessage(Convert(code)).c_str(), NAPI_AUTO_LENGTH, &errorMessage)); + NAPI_CALL(env, napi_set_named_property(env, errorObj, "code", errorCode)); + NAPI_CALL(env, napi_set_named_property(env, errorObj, "message", errorMessage)); + IMSA_HILOGE("ToError end"); + return errorObj; +} + +int32_t JsUtils::Convert(int32_t code) +{ + IMSA_HILOGI("Convert start"); + auto iter = ERROR_CODE_MAP.find(code); + if (iter != ERROR_CODE_MAP.end()) { + IMSA_HILOGE("ErrorCode: %{public}d", iter->second); + return iter->second; + } + IMSA_HILOGI("Convert end"); + return ERROR_CODE_QUERY_FAILED; +} + +const std::string JsUtils::ToMessage(int32_t code) +{ + IMSA_HILOGI("ToMessage start"); + auto iter = ERROR_CODE_CONVERT_MESSAGE_MAP.find(code); + if (iter != ERROR_CODE_CONVERT_MESSAGE_MAP.end()) { + IMSA_HILOGI("ErrorMessage: %{public}s", (iter->second).c_str()); + return iter->second; + } + return "error is out of definition."; +} +} // namespace MiscServices +} // namespace OHOS \ No newline at end of file diff --git a/services/include/global.h b/services/include/global.h index 18a9445d..3be7aaa1 100644 --- a/services/include/global.h +++ b/services/include/global.h @@ -101,64 +101,53 @@ enum { ERROR_STATUS_UNEXPECTED_NULL = ERROR_STATUS_UNKNOWN_ERROR + 8, // unexpected null, // binder exception error code from Status.h - ERROR_EX_ILLEGAL_ARGUMENT = -3, // illegal argument exception - ERROR_EX_NULL_POINTER = -4, // null pointer exception - ERROR_EX_ILLEGAL_STATE = -5, // illegal state exception - ERROR_EX_NETWORK_MAIN_THREAD = -6, // network main thread exception - ERROR_EX_UNSUPPORTED_OPERATION = -7, // unsupported operation exception - ERROR_EX_SERVICE_SPECIFIC = -8, // service specific exception - ERROR_EX_PARCELABLE = -9, // parcelable exception + ERROR_EX_ILLEGAL_ARGUMENT = -3, // illegal argument exception + ERROR_EX_NULL_POINTER = -4, // null pointer exception + ERROR_EX_ILLEGAL_STATE = -5, // illegal state exception + ERROR_EX_NETWORK_MAIN_THREAD = -6, // network main thread exception + ERROR_EX_UNSUPPORTED_OPERATION = -7, // unsupported operation exception + ERROR_EX_SERVICE_SPECIFIC = -8, // service specific exception + ERROR_EX_PARCELABLE = -9, // parcelable exception // no error - NO_ERROR = 0, // no error + NO_ERROR = 0, // no error // system service error - ERROR_NULL_POINTER, // null pointer - ERROR_BAD_PARAMETERS, // bad parameters - ERROR_SERVICE_START_FAILED, // failed to start service - ERROR_USER_NOT_STARTED, // user is not started - ERROR_USER_ALREADY_STARTED, // user has already started - ERROR_USER_NOT_UNLOCKED, // user is not unlocked - ERROR_USER_ALREADY_UNLOCKED, // user has already unlocked - ERROR_USER_NOT_LOCKED, // user is not locked + ERROR_NULL_POINTER = 1, // null pointer + ERROR_BAD_PARAMETERS = 2, // bad parameters + ERROR_USER_NOT_STARTED = 3, // user is not started + ERROR_USER_ALREADY_STARTED = 4, // user has already started + ERROR_USER_NOT_UNLOCKED = 5, // user is not unlocked + ERROR_USER_ALREADY_UNLOCKED = 6, // user has already unlocked + ERROR_USER_NOT_LOCKED = 7, // user is not locked - ERROR_IME_NOT_AVAILABLE, // input method engine is not available - ERROR_SECURITY_IME_NOT_AVAILABLE, // security input method engine is not available - ERROR_TOKEN_CREATE_FAILED, // failed to create window token - ERROR_TOKEN_DESTROY_FAILED, // failed to destroy window token - ERROR_IME_BIND_FAILED, // failed to bind IME service - ERROR_IME_UNBIND_FAILED, // failed to unbind IME service - ERROR_IME_START_FAILED, // failed to start IME service - ERROR_IME_STOP_FAILED, // failed to stop IME service - ERROR_KBD_SHOW_FAILED, // failed to show keyboard - ERROR_KBD_HIDE_FAILED, // failed to hide keyboard - ERROR_IME_NOT_STARTED, // input method service is not started - ERROR_KBD_IS_OCCUPIED, // keyboard is showing by other client - ERROR_KBD_IS_NOT_SHOWING, // keyboard is not showing - ERROR_IME_ALREADY_STARTED, // input method service has already started - ERROR_NOT_IME_PACKAGE, // not an IME package - ERROR_IME_PACKAGE_DUPLICATED, // duplicated IME package - ERROR_SETTING_SAME_VALUE, // same setting value - ERROR_NO_NEXT_IME, // no next ime is available - ERROR_CLIENTWINDOW_NOT_FOCUSED, // the input client window is not focused - ERROR_CLIENT_NOT_WINDOW, // the input client is not from a valid window - ERROR_ADD_CLIENT_FAILED, - ERROR_IME_PROPERTY_MARSHALL, // failed to marshall the ime property - ERROR_GETTING_CURRENT_IME, - ERROR_LIST_IME, - ERROR_SWITCH_IME, + ERROR_IME_NOT_AVAILABLE = 8, // input method engine is not available + ERROR_SECURITY_IME_NOT_AVAILABLE = 9, // security input method engine is not available + ERROR_TOKEN_CREATE_FAILED = 10, // failed to create window token + ERROR_TOKEN_DESTROY_FAILED = 11, // failed to destroy window token + ERROR_IME_BIND_FAILED = 12, // failed to bind IME service + ERROR_IME_UNBIND_FAILED = 13, // failed to unbind IME service + ERROR_IME_START_FAILED = 14, // failed to start IME service + ERROR_KBD_SHOW_FAILED = 15, // failed to show keyboard + ERROR_KBD_HIDE_FAILED = 16, // failed to hide keyboard + ERROR_IME_NOT_STARTED = 17, // input method service is not started + ERROR_KBD_IS_OCCUPIED = 18, // keyboard is showing by other client + ERROR_KBD_IS_NOT_SHOWING = 19, // keyboard is not showing + ERROR_NOT_IME_PACKAGE = 20, // not an IME package + ERROR_IME_PACKAGE_DUPLICATED = 21, // duplicated IME package + ERROR_SETTING_SAME_VALUE = 22, // same setting value + ERROR_ADD_CLIENT_FAILED = 23, // add client failed + ERROR_SWITCH_IME = 24, // error from ime - ERROR_REMOTE_IME_DIED, // remote input method service died abnormally - ERROR_RESTART_IME_FAILED, // failed to restart input method service - ERROR_IME_NULL_POINTER, // null pointer + ERROR_REMOTE_IME_DIED = 25, // remote input method service died abnormally // error from client - ERROR_REMOTE_CLIENT_DIED, // remote client died abnormally - ERROR_CLIENT_DUPLICATED, // duplicated client - ERROR_CLIENT_NOT_FOUND, // client is not found - ERROR_CLIENT_NULL_POINTER, // null pointer - ERROR_SUBSCRIBE_KEYBOARD_EVENT, - ERROR_CONTROLLER_INVOKING_FAILED, - ERROR_PERSIST_CONFIG, - ERROR_PACKAGE_MANAGER, + ERROR_REMOTE_CLIENT_DIED = 26, // remote client died abnormally + ERROR_CLIENT_NOT_FOUND = 27, // client is not found + ERROR_CLIENT_NULL_POINTER = 28, // null pointer + ERROR_SUBSCRIBE_KEYBOARD_EVENT = 29, + ERROR_CONTROLLER_INVOKING_FAILED = 30, + ERROR_PERSIST_CONFIG = 31, + ERROR_PACKAGE_MANAGER = 32, + ERROR_SERVICE_START_FAILED = 33, }; const char *ToString(int errorCode); }; // namespace ErrorCode diff --git a/services/src/global.cpp b/services/src/global.cpp index 2d123132..7cbe8ec2 100644 --- a/services/src/global.cpp +++ b/services/src/global.cpp @@ -82,8 +82,8 @@ const char *ToString(int errorCode) case ERROR_IME_START_FAILED: { return "failed to start input"; } - case ERROR_IME_STOP_FAILED: { - return "failed to stop input"; + case ERROR_ADD_CLIENT_FAILED: { + return "failed fo add client"; } case ERROR_KBD_SHOW_FAILED: { return "failed to show keyboard"; @@ -100,9 +100,6 @@ const char *ToString(int errorCode) case ERROR_KBD_IS_NOT_SHOWING: { return "keyboard is not showing"; } - case ERROR_IME_ALREADY_STARTED: { - return "input method service has already started"; - } case ERROR_NOT_IME_PACKAGE: { return "not an input method engine package"; } @@ -112,27 +109,12 @@ const char *ToString(int errorCode) case ERROR_SETTING_SAME_VALUE: { return "same setting value"; } - case ERROR_NO_NEXT_IME: { - return "next input method engine is not available"; - } - case ERROR_CLIENTWINDOW_NOT_FOCUSED: { - return "input client window is not focused"; - } - case ERROR_CLIENT_NOT_WINDOW: { - return "input client is not from a valid window"; - } case ERROR_REMOTE_IME_DIED: { return "input method service died"; } - case ERROR_RESTART_IME_FAILED: { - return "failed to restart input method service"; - } case ERROR_REMOTE_CLIENT_DIED: { return "input client died"; } - case ERROR_CLIENT_DUPLICATED: { - return "duplicated client"; - } case ERROR_CLIENT_NOT_FOUND: { return "client is not found"; } diff --git a/unitest/BUILD.gn b/unitest/BUILD.gn index 0b2b0ed3..1f6c9110 100644 --- a/unitest/BUILD.gn +++ b/unitest/BUILD.gn @@ -76,6 +76,9 @@ ohos_unittest("InputMethodAbilityTest") { external_deps = [ "ability_base:want", + "access_token:libaccesstoken_sdk", + "access_token:libnativetoken", + "access_token:libtoken_setproc", "c_utils:utils", "hiviewdfx_hilog_native:libhilog", ] diff --git a/unitest/src/input_method_ability_test.cpp b/unitest/src/input_method_ability_test.cpp index df4e49ca..24b60791 100644 --- a/unitest/src/input_method_ability_test.cpp +++ b/unitest/src/input_method_ability_test.cpp @@ -13,15 +13,17 @@ * limitations under the License. */ -#include -#include +#include "input_method_ability.h" #include #include +#include #include #include +#include #include +#include "accesstoken_kit.h" #include "global.h" #include "i_input_data_channel.h" #include "input_attribute.h" @@ -29,164 +31,405 @@ #include "input_data_channel_proxy.h" #include "input_data_channel_stub.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 "message_handler.h" +#include "nativetoken_kit.h" +#include "token_setproc.h" using namespace testing::ext; +using namespace OHOS::Security::AccessToken; namespace OHOS { namespace MiscServices { - class InputMethodAbilityTest : public testing::Test { - public: - static void SetUpTestCase(void); - static void TearDownTestCase(void); - void SetUp(); - void TearDown(); +class InputMethodAbilityTest : public testing::Test { +public: + static std::string imeIdStopped_; + static bool showKeyboard_; + static int direction_; + static int deleteForwardLength_; + static int deleteBackwardLength_; + static std::u16string insertText_; + static int key_; + static int keyboardStatus_; + static bool status_; + static sptr imc_; + static sptr inputMethodAbility_; + + class KeyboardListenerTestImpl : public KeyboardListener { + bool OnKeyEvent(int32_t keyCode, int32_t keyStatus) + { + return true; + } + void OnCursorUpdate(int32_t positionX, int32_t positionY, int32_t height) + { + } + void OnSelectionChange(int32_t oldBegin, int32_t oldEnd, int32_t newBegin, int32_t newEnd) + { + } + void OnTextChange(std::string text) + { + } }; + class InputMethodEngineListenerImpl : public InputMethodEngineListener { + public: + InputMethodEngineListenerImpl() = default; + ~InputMethodEngineListenerImpl() = default; - void InputMethodAbilityTest::SetUpTestCase(void) + void OnKeyboardStatus(bool isShow) + { + showKeyboard_ = isShow; + IMSA_HILOGI("InputMethodEngineListenerImpl OnKeyboardStatus"); + } + + void OnInputStart() + { + IMSA_HILOGI("InputMethodEngineListenerImpl OnInputStart"); + } + + void OnInputStop(std::string imeId) + { + imeIdStopped_ = imeId; + IMSA_HILOGI("InputMethodEngineListenerImpl OnInputStop"); + } + + void OnSetCallingWindow(uint32_t windowId) + { + IMSA_HILOGI("InputMethodEngineListenerImpl OnSetCallingWindow"); + } + + void OnSetSubtype(const SubProperty &property) + { + IMSA_HILOGI("InputMethodEngineListenerImpl OnSetSubtype"); + } + }; + class TextChangeListener : public OnTextChangedListener { + public: + void InsertText(const std::u16string &text) override + { + insertText_ = text; + } + + void DeleteForward(int32_t length) override + { + deleteForwardLength_ = length; + IMSA_HILOGI("TextChangeListener: DeleteForward, length is: %{public}d", length); + } + + void DeleteBackward(int32_t length) override + { + deleteBackwardLength_ = length; + IMSA_HILOGI("TextChangeListener: DeleteBackward, direction is: %{public}d", length); + } + + void SendKeyEventFromInputMethod(const KeyEvent &event) override + { + } + + void SendKeyboardInfo(const KeyboardInfo &info) override + { + FunctionKey functionKey = info.GetFunctionKey(); + KeyboardStatus keyboardStatus = info.GetKeyboardStatus(); + key_ = (int)functionKey; + keyboardStatus_ = (int)keyboardStatus; + } + + void SetKeyboardStatus(bool status) override + { + status_ = status; + } + + void MoveCursor(const Direction direction) override + { + direction_ = (int)direction; + IMSA_HILOGI("TextChangeListener: MoveCursor, direction is: %{public}d", direction); + } + }; + void GrantPermission() { - IMSA_HILOGI("InputMethodAbilityTest::SetUpTestCase"); + const char **perms = new const char *[1]; + perms[0] = "ohos.permission.CONNECT_IME_ABILITY"; + TokenInfoParams infoInstance = { + .dcapsNum = 0, + .permsNum = 1, + .aclsNum = 0, + .dcaps = nullptr, + .perms = perms, + .acls = nullptr, + .processName = "inputmethod_imf", + .aplStr = "system_core", + }; + uint64_t tokenId = GetAccessTokenId(&infoInstance); + int res = SetSelfTokenID(tokenId); + if (res == 0) { + IMSA_HILOGI("SetSelfTokenID success!"); + } else { + IMSA_HILOGE("SetSelfTokenID fail!"); + } + AccessTokenKit::ReloadNativeTokenInfo(); + delete[] perms; } - - void InputMethodAbilityTest::TearDownTestCase(void) + static void SetUpTestCase(void) { - IMSA_HILOGI("InputMethodAbilityTest::TearDownTestCase"); + imc_ = InputMethodController::GetInstance(); + ASSERT_TRUE(imc_ != nullptr); } - - void InputMethodAbilityTest::SetUp(void) + static void TearDownTestCase(void) { + } + void SetUp() + { + inputMethodAbility_ = InputMethodAbility::GetInstance(); + GrantPermission(); IMSA_HILOGI("InputMethodAbilityTest::SetUp"); + sptr textListener = new TextChangeListener(); + imc_->Attach(textListener); } - - void InputMethodAbilityTest::TearDown(void) + void TearDown() { - IMSA_HILOGI("InputMethodAbilityTest::TearDown"); } +}; - /** - * @tc.name: testReadWriteIInputMethodAgent - * @tc.desc: Checkout IInputMethodAgent. - * @tc.type: FUNC - * @tc.require: issueI5JBR6 - */ - HWTEST_F(InputMethodAbilityTest, testReadWriteIInputMethodAgent, TestSize.Level0) - { - sptr mInputMethodAgentStub = new InputMethodAgentStub(); - MessageParcel data; - auto ret = data.WriteRemoteObject(mInputMethodAgentStub->AsObject()); - EXPECT_TRUE(ret); - auto remoteObject = data.ReadRemoteObject(); - sptr iface = iface_cast(remoteObject); - EXPECT_TRUE(iface != nullptr); - } +std::string InputMethodAbilityTest::imeIdStopped_; +bool InputMethodAbilityTest::showKeyboard_; +int InputMethodAbilityTest::direction_; +int InputMethodAbilityTest::deleteForwardLength_; +int InputMethodAbilityTest::deleteBackwardLength_; +std::u16string InputMethodAbilityTest::insertText_; +int InputMethodAbilityTest::key_; +int InputMethodAbilityTest::keyboardStatus_; +bool InputMethodAbilityTest::status_; +sptr InputMethodAbilityTest::imc_; +sptr InputMethodAbilityTest::inputMethodAbility_; - /** - * @tc.name: testReadWriteIInputMethodCore - * @tc.desc: Checkout IInputMethodCore. - * @tc.type: FUNC - */ - HWTEST_F(InputMethodAbilityTest, testReadWriteIInputMethodCore, TestSize.Level0) - { - sptr mInputMethodCoreStub = new InputMethodCoreStub(0); - MessageParcel data; - auto ret = data.WriteRemoteObject(mInputMethodCoreStub->AsObject()); - EXPECT_TRUE(ret); - auto remoteObject = data.ReadRemoteObject(); - sptr iface = iface_cast(remoteObject); - EXPECT_TRUE(iface != nullptr); - } +/** +* @tc.name: testShowKeyboardInputMethodCoreProxy +* @tc.desc: Test InputMethodCoreProxy ShowKeyboard +* @tc.type: FUNC +* @tc.require: issueI5NXHK +*/ +HWTEST_F(InputMethodAbilityTest, testShowKeyboardInputMethodCoreProxy, TestSize.Level0) +{ + sptr coreStub = new InputMethodCoreStub(0); + sptr core = coreStub; + auto msgHandler = new (std::nothrow) MessageHandler(); + coreStub->SetMessageHandler(msgHandler); + sptr channelStub = new InputDataChannelStub(); - /** - * @tc.name: testShowKeyboardInputMethodCoreProxy - * @tc.desc: Test InputMethodCoreProxy ShowKeyboard - * @tc.type: FUNC - * @tc.require: issueI5NXHK - */ - HWTEST_F(InputMethodAbilityTest, testShowKeyboardInputMethodCoreProxy, TestSize.Level0) - { - sptr coreStub = new InputMethodCoreStub(0); - sptr core = coreStub; - auto msgHandler = new (std::nothrow) MessageHandler(); - coreStub->SetMessageHandler(msgHandler); - sptr channelStub = new InputDataChannelStub(); + MessageParcel data; + data.WriteRemoteObject(core->AsObject()); + data.WriteRemoteObject(channelStub->AsObject()); + sptr coreObject = data.ReadRemoteObject(); + sptr channelObject = data.ReadRemoteObject(); - MessageParcel data; - data.WriteRemoteObject(core->AsObject()); - data.WriteRemoteObject(channelStub->AsObject()); - sptr coreObject = data.ReadRemoteObject(); - sptr channelObject = data.ReadRemoteObject(); + sptr coreProxy = new InputMethodCoreProxy(coreObject); + sptr channelProxy = new InputDataChannelProxy(channelObject); + SubProperty subProperty; + auto ret = coreProxy->showKeyboard(channelProxy, true, subProperty); + delete msgHandler; + EXPECT_EQ(ret, ErrorCode::NO_ERROR); +} - sptr coreProxy = new InputMethodCoreProxy(coreObject); - sptr channelProxy = new InputDataChannelProxy(channelObject); - SubProperty subProperty; - auto ret = coreProxy->showKeyboard(channelProxy, true, subProperty); - delete msgHandler; - EXPECT_EQ(ret, 0); - } +/** +* @tc.name: testShowKeyboardInputMethodCoreStub +* @tc.desc: Test InputMethodCoreStub ShowKeyboard +* @tc.type: FUNC +* @tc.require: issueI5NXHK +*/ +HWTEST_F(InputMethodAbilityTest, testShowKeyboardInputMethodCoreStub, TestSize.Level0) +{ + sptr coreStub = new InputMethodCoreStub(0); + SubProperty subProperty; + auto ret = coreStub->showKeyboard(nullptr, true, subProperty); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); +} - /** - * @tc.name: testShowKeyboardInputMethodCoreStub - * @tc.desc: Test InputMethodCoreStub ShowKeyboard - * @tc.type: FUNC - * @tc.require: issueI5NXHK - */ - HWTEST_F(InputMethodAbilityTest, testShowKeyboardInputMethodCoreStub, TestSize.Level0) - { - sptr coreStub = new InputMethodCoreStub(0); - SubProperty subProperty; - auto ret = coreStub->showKeyboard(nullptr, true, subProperty); - EXPECT_EQ(ret, 0); - } +/** +* @tc.name: testSerializedInputAttribute +* @tc.desc: Checkout the serialization of InputAttribute. +* @tc.type: FUNC +*/ +HWTEST_F(InputMethodAbilityTest, testSerializedInputAttribute, TestSize.Level0) +{ + InputAttribute inAttribute; + inAttribute.inputPattern = InputAttribute::PATTERN_PASSWORD; + MessageParcel data; + EXPECT_TRUE(InputAttribute::Marshalling(inAttribute, data)); + InputAttribute outAttribute; + EXPECT_TRUE(InputAttribute::Unmarshalling(outAttribute, data)); + EXPECT_TRUE(outAttribute.GetSecurityFlag()); +} - /** - * @tc.name: testReadWriteIInputControlChannel - * @tc.desc: Checkout IInputControlChannel. - * @tc.type: FUNC - */ - HWTEST_F(InputMethodAbilityTest, testReadWriteIInputControlChannel, TestSize.Level0) - { - sptr mInputControlChannelStub = new InputControlChannelStub(0); - MessageParcel data; - auto ret = data.WriteRemoteObject(mInputControlChannelStub->AsObject()); - EXPECT_TRUE(ret); - auto remoteObject = data.ReadRemoteObject(); - sptr iface = iface_cast(remoteObject); - EXPECT_TRUE(iface != nullptr); - } +/** +* @tc.name: testSerializedKeyboardType +* @tc.desc: Checkout the serialization of KeyboardType. +* @tc.type: FUNC +*/ +HWTEST_F(InputMethodAbilityTest, testSerializedKeyboardType, TestSize.Level0) +{ + int32_t def_value = 2021; + sptr mKeyboardType = new KeyboardType(); + mKeyboardType->setId(def_value); + MessageParcel data; + auto ret = data.WriteParcelable(mKeyboardType); + EXPECT_TRUE(ret); + sptr deserialization = data.ReadParcelable(); + ASSERT_TRUE(deserialization != nullptr); + EXPECT_TRUE(deserialization->getId() == def_value); +} - /** - * @tc.name: testSerializedInputAttribute - * @tc.desc: Checkout the serialization of InputAttribute. - * @tc.type: FUNC - */ - HWTEST_F(InputMethodAbilityTest, testSerializedInputAttribute, TestSize.Level0) - { - InputAttribute inAttribute; - inAttribute.inputPattern = InputAttribute::PATTERN_PASSWORD; - MessageParcel data; - EXPECT_TRUE(InputAttribute::Marshalling(inAttribute, data)); - InputAttribute outAttribute; - EXPECT_TRUE(InputAttribute::Unmarshalling(outAttribute, data)); - EXPECT_TRUE(outAttribute.GetSecurityFlag()); - } +/** +* @tc.name: testMoveCursor +* @tc.desc: InputMethodAbility MoveCursor +* @tc.type: FUNC +* @tc.require: +* @tc.author: Hollokin +*/ +HWTEST_F(InputMethodAbilityTest, testMoveCursor, TestSize.Level0) +{ + IMSA_HILOGI("InputMethodAbility MoveCursor Test START"); + auto ret = inputMethodAbility_->MoveCursor(4); // move cursor right + usleep(500); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + EXPECT_EQ(direction_, 4); +} - /** - * @tc.name: testSerializedKeyboardType - * @tc.desc: Checkout the serialization of KeyboardType. - * @tc.type: FUNC - */ - HWTEST_F(InputMethodAbilityTest, testSerializedKeyboardType, TestSize.Level0) - { - int32_t def_value = 2021; - sptr mKeyboardType = new KeyboardType(); - mKeyboardType->setId(def_value); - MessageParcel data; - auto ret = data.WriteParcelable(mKeyboardType); - EXPECT_TRUE(ret); - sptr deserialization = data.ReadParcelable(); - EXPECT_TRUE(deserialization != nullptr); - EXPECT_TRUE(deserialization->getId() == def_value); - } +/** +* @tc.name: testInsertText +* @tc.desc: InputMethodAbility InsertText +* @tc.type: FUNC +* @tc.require: +* @tc.author: Hollokin +*/ +HWTEST_F(InputMethodAbilityTest, testInsertText, TestSize.Level0) +{ + IMSA_HILOGI("InputMethodAbility InsertText Test START"); + auto ret = inputMethodAbility_->InsertText("text"); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + usleep(500); + EXPECT_EQ(insertText_, u"text"); +} + +/** +* @tc.name: testSetImeListener +* @tc.desc: InputMethodAbility SetImeListener +* @tc.type: FUNC +* @tc.require: +* @tc.author: Hollokin +*/ +HWTEST_F(InputMethodAbilityTest, testSetImeListener, TestSize.Level0) +{ + IMSA_HILOGI("InputMethodAbility SetImeListener Test START"); + auto listener = std::make_shared(); + inputMethodAbility_->setImeListener(listener); + auto ret = imc_->StopInputSession(); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + EXPECT_EQ(imeIdStopped_, ""); + + ret = imc_->ShowSoftKeyboard(); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + usleep(500); + EXPECT_EQ(showKeyboard_, true); + + ret = imc_->HideSoftKeyboard(); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + EXPECT_EQ(showKeyboard_, false); +} + +/** +* @tc.name: testSetKdListener +* @tc.desc: InputMethodAbility SetKdListener +* @tc.type: FUNC +* @tc.require: +* @tc.author: Hollokin +*/ +HWTEST_F(InputMethodAbilityTest, testSetKdListener, TestSize.Level0) +{ + IMSA_HILOGI("InputMethodAbility SetKdListener Test START"); + auto keyBoardListener = std::make_shared(); + inputMethodAbility_->setKdListener(keyBoardListener); +} + +/** +* @tc.name: testSendFunctionKey +* @tc.desc: InputMethodAbility SendFunctionKey +* @tc.type: FUNC +* @tc.require: +* @tc.author: Hollokin +*/ +HWTEST_F(InputMethodAbilityTest, testSendFunctionKey, TestSize.Level0) +{ + IMSA_HILOGI("InputMethodAbility SendFunctionKey Test START"); + auto ret = inputMethodAbility_->SendFunctionKey(0); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); +} + +/** +* @tc.name: testDeleteText +* @tc.desc: InputMethodAbility DeleteForward & DeleteBackward +* @tc.type: FUNC +* @tc.require: +* @tc.author: Hollokin +*/ +HWTEST_F(InputMethodAbilityTest, testDeleteText, TestSize.Level0) +{ + IMSA_HILOGI("InputMethodAbility testDelete Test START"); + int32_t deleteForwardLenth = 1; + auto ret = inputMethodAbility_->DeleteForward(deleteForwardLenth); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + usleep(500); + EXPECT_EQ(deleteForwardLength_, deleteForwardLenth); + int32_t deleteBackwardLenth = 2; + ret = inputMethodAbility_->DeleteBackward(deleteBackwardLenth); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + usleep(500); + EXPECT_EQ(deleteBackwardLength_, deleteBackwardLenth); +} + +/** +* @tc.name: testGetText001 +* @tc.desc: InputMethodAbility GetTextBeforeCursor & GetTextAfterCursor +* @tc.type: FUNC +* @tc.require: +* @tc.author: Hollokin +*/ +HWTEST_F(InputMethodAbilityTest, testGetText001, TestSize.Level0) +{ + IMSA_HILOGI("InputMethodAbility testGetText001 START"); + imc_->OnSelectionChange(u"onselectionchange", 0, 3); + std::u16string text; + auto ret = inputMethodAbility_->GetTextAfterCursor(8, text); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + EXPECT_EQ(text, u"electioncha"); + ret = inputMethodAbility_->GetTextBeforeCursor(3, text); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + EXPECT_EQ(text, u""); +} + +/** +* @tc.name: testGetEnterKeyType +* @tc.desc: InputMethodAbility GetEnterKeyType & GetInputPattern +* @tc.type: FUNC +* @tc.require: +* @tc.author: Hollokin +*/ +HWTEST_F(InputMethodAbilityTest, testGetEnterKeyType, TestSize.Level0) +{ + IMSA_HILOGI("InputMethodAbility testGetEnterKeyType START"); + Configuration config; + EnterKeyType keyType = EnterKeyType::NEXT; + config.SetEnterKeyType(keyType); + TextInputType textInputType = TextInputType::DATETIME; + config.SetTextInputType(textInputType); + imc_->OnConfigurationChange(config); + int32_t keyType2; + auto 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); +} } // namespace MiscServices } // namespace OHOS