!862 [代码整改]: 超大函数与超大圈层复杂数整改

Merge pull request !862 from 赵凌岚/master
This commit is contained in:
openharmony_ci 2023-08-29 09:31:56 +00:00 committed by Gitee
commit 2d1a69de5b
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
21 changed files with 1346 additions and 690 deletions

View File

@ -26,18 +26,18 @@
"ram": "1024KB",
"deps": {
"components": [
"init",
"napi",
"samgr",
"common_event_service",
"ipc",
"eventhandler",
"bundle_framework",
"ability_runtime",
"hilog",
"ability_base",
"safwk",
"input",
"init",
"napi",
"samgr",
"common_event_service",
"ipc",
"eventhandler",
"bundle_framework",
"ability_runtime",
"hilog",
"ability_base",
"safwk",
"input",
"c_utils",
"access_token",
"i18n",
@ -45,7 +45,8 @@
"os_account",
"hisysevent",
"hitrace",
"graphic_2d"
"graphic_2d",
"ffrt"
],
"third_party": [
"jsoncpp"

View File

@ -43,11 +43,18 @@ public:
{
std::unique_lock<std::mutex> lock(mutex_);
cv_.wait_for(lock, std::chrono::milliseconds(INTERVAL), [this]() { return isSet_; });
isTimeOut_ = !isSet_;
T data = data_;
return data;
}
bool GetValue(T &data)
{
std::unique_lock<std::mutex> lock(mutex_);
cv_.wait_for(lock, std::chrono::milliseconds(INTERVAL), [this]() { return isSet_; });
data = data_;
return isSet_;
}
void Clear(const T &invalid = T())
{
std::lock_guard<std::mutex> lock(mutex_);
@ -55,16 +62,10 @@ public:
data_ = invalid;
}
bool IsTimeOut()
{
return isTimeOut_;
}
private:
bool isSet_ = false;
const uint32_t INTERVAL;
T data_;
bool isTimeOut_{ false };
std::mutex mutex_;
std::condition_variable cv_;
};

View File

@ -156,15 +156,13 @@ private:
IMSA_HILOGE("Not enough params");
return engine.CreateUndefined();
}
decltype(info.argc) unwrapArgc = 0;
decltype(info.argc) argc = 0;
AAFwk::Want want;
OHOS::AppExecFwk::UnwrapWant(
reinterpret_cast<napi_env>(&engine), reinterpret_cast<napi_value>(info.argv[INDEX_ZERO]), want);
IMSA_HILOGI("%{public}s bundlename:%{public}s abilityname:%{public}s", __func__, want.GetBundle().c_str(),
IMSA_HILOGI("bundleName:%{public}s abilityName:%{public}s", want.GetBundle().c_str(),
want.GetElement().GetAbilityName().c_str());
unwrapArgc++;
argc++;
int32_t accountId = 0;
if (!OHOS::AppExecFwk::UnwrapInt32FromJS2(
reinterpret_cast<napi_env>(&engine), reinterpret_cast<napi_value>(info.argv[INDEX_ONE]), accountId)) {
@ -172,37 +170,30 @@ private:
return engine.CreateUndefined();
}
IMSA_HILOGI("%{public}d accountId:", accountId);
unwrapArgc++;
argc++;
AAFwk::StartOptions startOptions;
if (info.argc > ARGC_TWO && info.argv[INDEX_TWO]->TypeOf() == NATIVE_OBJECT) {
IMSA_HILOGI("OnStartAbilityWithAccount start options is used.");
AppExecFwk::UnwrapStartOptions(
reinterpret_cast<napi_env>(&engine), reinterpret_cast<napi_value>(info.argv[INDEX_TWO]), startOptions);
unwrapArgc++;
argc++;
}
AsyncTask::CompleteCallback complete = [weak = context_, want, accountId, startOptions, unwrapArgc](
AsyncTask::CompleteCallback complete = [weak = context_, want, accountId, startOptions, argc](
NativeEngine &engine, AsyncTask &task, int32_t status) {
IMSA_HILOGI("startAbility begin");
auto context = weak.lock();
if (context == nullptr) {
IMSA_HILOGW("context is released");
task.Reject(engine, CreateJsError(engine, ERROR_CODE_ONE, "Context is released"));
return;
}
ErrCode errcode = ERR_OK;
(unwrapArgc == ARGC_TWO) ? errcode = context->StartAbilityWithAccount(want, accountId)
: errcode = context->StartAbilityWithAccount(want, accountId, startOptions);
ErrCode errcode = (argc == ARGC_TWO) ? context->StartAbilityWithAccount(want, accountId)
: context->StartAbilityWithAccount(want, accountId, startOptions);
if (errcode == 0) {
task.Resolve(engine, engine.CreateUndefined());
} else {
task.Reject(engine, CreateJsError(engine, errcode, "Start Ability failed."));
}
task.Reject(engine, CreateJsError(engine, errcode, "Start Ability failed."));
};
NativeValue *lastParam = (info.argc == unwrapArgc) ? nullptr : info.argv[unwrapArgc];
NativeValue *lastParam = (info.argc == argc) ? nullptr : info.argv[argc];
NativeValue *result = nullptr;
AsyncTask::Schedule("InputMethodExtensionContext::OnStartAbilityWithAccount", engine,
CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));

View File

@ -45,6 +45,7 @@ public:
HANDLE_EXTEND_ACTION,
GET_TEXT_INDEX_AT_CURSOR,
GET_TEXT_CONFIG,
DATA_CHANNEL_CMD_LAST
};
DECLARE_INTERFACE_DESCRIPTOR(u"ohos.miscservices.inputmethod.IInputDataChannel");

View File

@ -35,7 +35,6 @@ public:
int32_t OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override;
InputClientStub();
~InputClientStub();
void SetHandler(MessageHandler *handler);
int32_t OnInputReady(const sptr<IInputMethodAgent> &agent) override;
int32_t OnInputStop() override;
@ -44,13 +43,10 @@ public:
const InputWindowStatus &status, const std::vector<InputWindowInfo> &windowInfo) override;
private:
MessageHandler *msgHandler = nullptr;
using ParcelHandler = std::function<bool(MessageParcel &)>;
int32_t SendMessage(int code, ParcelHandler input = nullptr);
void OnInputReadyOnRemote(MessageParcel &data, MessageParcel &reply);
void OnInputStopOnRemote(MessageParcel &data, MessageParcel &reply);
void OnSwitchInputOnRemote(MessageParcel &data, MessageParcel &reply);
void OnPanelStatusChangeOnRemote(MessageParcel &data, MessageParcel &reply);
int32_t OnInputStopOnRemote(MessageParcel &data, MessageParcel &reply);
int32_t OnSwitchInputOnRemote(MessageParcel &data, MessageParcel &reply);
int32_t OnPanelStatusChangeOnRemote(MessageParcel &data, MessageParcel &reply);
};
} // namespace MiscServices
} // namespace OHOS

View File

@ -37,7 +37,6 @@ public:
int32_t OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override;
InputDataChannelStub();
~InputDataChannelStub();
void SetHandler(MessageHandler *handler);
int32_t InsertText(const std::u16string &text) override;
int32_t DeleteForward(int32_t length) override;
int32_t DeleteBackward(int32_t length) override;
@ -55,14 +54,45 @@ public:
int32_t GetTextConfig(TextTotalConfig &textConfig) override;
private:
MessageHandler *msgHandler;
template<class T>
struct ResultInfo {
T data;
int32_t errCode{0};
};
int32_t InsertTextOnRemote(MessageParcel &data, MessageParcel &reply);
int32_t DeleteForwardOnRemote(MessageParcel &data, MessageParcel &reply);
int32_t DeleteBackwardOnRemote(MessageParcel &data, MessageParcel &reply);
int32_t GetTextBeforeCursorOnRemote(MessageParcel &data, MessageParcel &reply);
int32_t GetTextAfterCursorOnRemote(MessageParcel &data, MessageParcel &reply);
int32_t GetTextConfigOnRemote(MessageParcel &data, MessageParcel &reply);
int32_t SendKeyboardStatusOnRemote(MessageParcel &data, MessageParcel &reply);
int32_t SendFunctionKeyOnRemote(MessageParcel &data, MessageParcel &reply);
int32_t MoveCursorOnRemote(MessageParcel &data, MessageParcel &reply);
int32_t GetEnterKeyTypeOnRemote(MessageParcel &data, MessageParcel &reply);
int32_t GetInputPatternOnRemote(MessageParcel &data, MessageParcel &reply);
int32_t SelectByRangeOnRemote(MessageParcel &data, MessageParcel &reply);
int32_t SelectByMovementOnRemote(MessageParcel &data, MessageParcel &reply);
int32_t HandleExtendActionOnRemote(MessageParcel &data, MessageParcel &reply);
int32_t GetText(int32_t msgId, MessageParcel &data, MessageParcel &reply);
int32_t GetTextIndexAtCursor(int32_t msgId, MessageParcel &data, MessageParcel &reply);
using MsgConstructor = std::function<Message *(MessageParcel &parcel)>;
int32_t SendMessage(const MsgConstructor &msgConstructor);
int32_t GetTextIndexAtCursorOnRemote(MessageParcel &data, MessageParcel &reply);
using RequestHandler = int32_t (InputDataChannelStub::*)(MessageParcel &, MessageParcel &);
static constexpr RequestHandler HANDLERS[static_cast<uint32_t>(DATA_CHANNEL_CMD_LAST)] = {
[static_cast<uint32_t>(INSERT_TEXT)] = &InputDataChannelStub::InsertTextOnRemote,
[static_cast<uint32_t>(DELETE_FORWARD)] = &InputDataChannelStub::DeleteForwardOnRemote,
[static_cast<uint32_t>(DELETE_BACKWARD)] = &InputDataChannelStub::DeleteBackwardOnRemote,
[static_cast<uint32_t>(GET_TEXT_BEFORE_CURSOR)] = &InputDataChannelStub::GetTextBeforeCursorOnRemote,
[static_cast<uint32_t>(GET_TEXT_AFTER_CURSOR)] = &InputDataChannelStub::GetTextAfterCursorOnRemote,
[static_cast<uint32_t>(GET_ENTER_KEY_TYPE)] = &InputDataChannelStub::GetEnterKeyTypeOnRemote,
[static_cast<uint32_t>(GET_INPUT_PATTERN)] = &InputDataChannelStub::GetInputPatternOnRemote,
[static_cast<uint32_t>(SEND_KEYBOARD_STATUS)] = &InputDataChannelStub::SendKeyboardStatusOnRemote,
[static_cast<uint32_t>(SEND_FUNCTION_KEY)] = &InputDataChannelStub::SendFunctionKeyOnRemote,
[static_cast<uint32_t>(MOVE_CURSOR)] = &InputDataChannelStub::MoveCursorOnRemote,
[static_cast<uint32_t>(SELECT_BY_RANGE)] = &InputDataChannelStub::SelectByRangeOnRemote,
[static_cast<uint32_t>(SELECT_BY_MOVEMENT)] = &InputDataChannelStub::SelectByMovementOnRemote,
[static_cast<uint32_t>(HANDLE_EXTEND_ACTION)] = &InputDataChannelStub::HandleExtendActionOnRemote,
[static_cast<uint32_t>(GET_TEXT_INDEX_AT_CURSOR)] = &InputDataChannelStub::GetTextIndexAtCursorOnRemote,
[static_cast<uint32_t>(GET_TEXT_CONFIG)] = &InputDataChannelStub::GetTextConfigOnRemote,
};
};
} // namespace MiscServices
} // namespace OHOS

View File

@ -18,8 +18,8 @@
#include "global.h"
#include "input_method_controller.h"
#include "ipc_object_stub.h"
#include "ipc_types.h"
#include "ipc_skeleton.h"
#include "ipc_types.h"
#include "itypes_util.h"
#include "message.h"
@ -37,7 +37,7 @@ int32_t InputClientStub::OnRemoteRequest(
uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
{
IMSA_HILOGI("code = %{public}u, callingPid:%{public}d, callingUid:%{public}d", code, IPCSkeleton::GetCallingPid(),
IPCSkeleton::GetCallingUid());
IPCSkeleton::GetCallingUid());
auto descriptorToken = data.ReadInterfaceToken();
if (descriptorToken != GetDescriptor()) {
return ErrorCode::ERROR_STATUS_UNKNOWN_TRANSACTION;
@ -52,12 +52,10 @@ int32_t InputClientStub::OnRemoteRequest(
break;
}
case ON_SWITCH_INPUT: {
OnSwitchInputOnRemote(data, reply);
break;
return OnSwitchInputOnRemote(data, reply);
}
case ON_PANEL_STATUS_CHANGE: {
OnPanelStatusChangeOnRemote(data, reply);
break;
return OnPanelStatusChangeOnRemote(data, reply);
}
default:
return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
@ -71,88 +69,36 @@ void InputClientStub::OnInputReadyOnRemote(MessageParcel &data, MessageParcel &r
InputMethodController::GetInstance()->OnInputReady(object);
}
void InputClientStub::OnInputStopOnRemote(MessageParcel &data, MessageParcel &reply)
int32_t InputClientStub::OnInputStopOnRemote(MessageParcel &data, MessageParcel &reply)
{
int32_t ret = SendMessage(MessageID::MSG_ID_ON_INPUT_STOP);
if (!ITypesUtil::Marshal(reply, ret)) {
IMSA_HILOGE("failed to write reply");
}
return reply.WriteInt32(OnInputStop()) ? ErrorCode::NO_ERROR : ErrorCode::ERROR_EX_PARCELABLE;
}
void InputClientStub::OnSwitchInputOnRemote(MessageParcel &data, MessageParcel &reply)
int32_t InputClientStub::OnSwitchInputOnRemote(MessageParcel &data, MessageParcel &reply)
{
IMSA_HILOGI("InputClientStub::OnSwitchInputOnRemote");
if (msgHandler == nullptr) {
IMSA_HILOGE("InputClientStub::msgHandler is nullptr");
return;
}
auto *parcel = new (std::nothrow) MessageParcel();
if (parcel == nullptr) {
IMSA_HILOGE("parcel is nullptr");
reply.WriteInt32(ErrorCode::ERROR_EX_NULL_POINTER);
return;
}
Property property;
SubProperty subProperty;
if (!ITypesUtil::Unmarshal(data, property, subProperty)) {
IMSA_HILOGE("read message parcel failed");
reply.WriteInt32(ErrorCode::ERROR_EX_PARCELABLE);
delete parcel;
return;
return ErrorCode::ERROR_EX_PARCELABLE;
}
if (!ITypesUtil::Marshal(*parcel, property, subProperty)) {
IMSA_HILOGE("write message parcel failed");
reply.WriteInt32(ErrorCode::ERROR_EX_PARCELABLE);
delete parcel;
return;
}
auto *msg = new (std::nothrow) Message(MessageID::MSG_ID_ON_SWITCH_INPUT, parcel);
if (msg == nullptr) {
IMSA_HILOGE("msg is nullptr");
delete parcel;
reply.WriteInt32(ErrorCode::ERROR_EX_NULL_POINTER);
return;
}
msgHandler->SendMessage(msg);
reply.WriteInt32(ErrorCode::NO_ERROR);
return reply.WriteInt32(OnSwitchInput(property, subProperty)) ? ErrorCode::NO_ERROR
: ErrorCode::ERROR_EX_PARCELABLE;
}
void InputClientStub::OnPanelStatusChangeOnRemote(MessageParcel &data, MessageParcel &reply)
int32_t InputClientStub::OnPanelStatusChangeOnRemote(MessageParcel &data, MessageParcel &reply)
{
IMSA_HILOGD("InputClientStub::OnPanelStatusChangeOnRemote");
if (msgHandler == nullptr) {
IMSA_HILOGE("InputClientStub::msgHandler is nullptr");
return;
}
auto *parcel = new (std::nothrow) MessageParcel();
if (parcel == nullptr) {
IMSA_HILOGE("parcel is nullptr");
reply.WriteInt32(ErrorCode::ERROR_EX_NULL_POINTER);
return;
}
uint32_t status;
std::vector<InputWindowInfo> windowInfo;
if (!ITypesUtil::Unmarshal(data, status, windowInfo)) {
IMSA_HILOGE("read message parcel failed");
reply.WriteInt32(ErrorCode::ERROR_EX_PARCELABLE);
delete parcel;
return;
return ErrorCode::ERROR_EX_PARCELABLE;
}
if (!ITypesUtil::Marshal(*parcel, status, windowInfo)) {
IMSA_HILOGE("write message parcel failed");
reply.WriteInt32(ErrorCode::ERROR_EX_PARCELABLE);
delete parcel;
return;
}
auto *msg = new (std::nothrow) Message(MessageID::MSG_ID_ON_PANEL_STATUS_CHANGE, parcel);
if (msg == nullptr) {
IMSA_HILOGE("msg is nullptr");
delete parcel;
reply.WriteInt32(ErrorCode::ERROR_EX_NULL_POINTER);
return;
}
msgHandler->SendMessage(msg);
reply.WriteInt32(ErrorCode::NO_ERROR);
return reply.WriteInt32(OnPanelStatusChange(static_cast<InputWindowStatus>(status), windowInfo))
? ErrorCode::NO_ERROR
: ErrorCode::ERROR_EX_PARCELABLE;
}
int32_t InputClientStub::OnInputReady(const sptr<IInputMethodAgent> &agent)
@ -162,50 +108,19 @@ int32_t InputClientStub::OnInputReady(const sptr<IInputMethodAgent> &agent)
int32_t InputClientStub::OnInputStop()
{
InputMethodController::GetInstance()->OnInputStop();
return ErrorCode::NO_ERROR;
}
void InputClientStub::SetHandler(MessageHandler *handler)
{
msgHandler = handler;
}
int32_t InputClientStub::OnSwitchInput(const Property &property, const SubProperty &subProperty)
{
return ErrorCode::NO_ERROR;
return InputMethodController::GetInstance()->OnSwitchInput(property, subProperty);
}
int32_t InputClientStub::OnPanelStatusChange(
const InputWindowStatus &status, const std::vector<InputWindowInfo> &windowInfo)
{
return ErrorCode::NO_ERROR;
}
int32_t InputClientStub::SendMessage(int code, ParcelHandler input)
{
IMSA_HILOGD("InputClientStub run in");
if (msgHandler == nullptr) {
IMSA_HILOGE("msgHandler_ is nullptr");
return ErrorCode::ERROR_EX_NULL_POINTER;
}
auto *parcel = new (std::nothrow) MessageParcel();
if (parcel == nullptr) {
IMSA_HILOGE("parcel is nullptr");
return ErrorCode::ERROR_EX_NULL_POINTER;
}
if (input != nullptr && (!input(*parcel))) {
IMSA_HILOGE("write data failed");
delete parcel;
return ErrorCode::ERROR_EX_PARCELABLE;
}
auto *msg = new (std::nothrow) Message(code, parcel);
if (msg == nullptr) {
IMSA_HILOGE("msg is nullptr");
delete parcel;
return ErrorCode::ERROR_EX_NULL_POINTER;
}
msgHandler->SendMessage(msg);
return ErrorCode::NO_ERROR;
return InputMethodController::GetInstance()->OnPanelStatusChange(status, windowInfo);
}
} // namespace MiscServices
} // namespace OHOS

View File

@ -15,6 +15,7 @@
#include "input_data_channel_stub.h"
#include "ffrt_inner.h"
#include "global.h"
#include "input_method_controller.h"
#include "ipc_object_stub.h"
@ -24,8 +25,8 @@
#include "message.h"
namespace OHOS {
namespace MiscServices {
constexpr int32_t MAX_TIMEOUT = 2500;
InputDataChannelStub::InputDataChannelStub() : msgHandler(nullptr)
constexpr uint32_t MAX_TIMEOUT = 2500;
InputDataChannelStub::InputDataChannelStub()
{
}
@ -36,325 +37,416 @@ InputDataChannelStub::~InputDataChannelStub()
int32_t InputDataChannelStub::OnRemoteRequest(
uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
{
IMSA_HILOGD("code = %{public}u, callingPid:%{public}d, callingUid:%{public}d", code, IPCSkeleton::GetCallingPid(),
IPCSkeleton::GetCallingUid());
IMSA_HILOGI("InputDataChannelStub, code: %{public}u, callingPid: %{public}d, callingUid: %{public}d", code,
IPCSkeleton::GetCallingPid(), IPCSkeleton::GetCallingUid());
auto descriptorToken = data.ReadInterfaceToken();
if (descriptorToken != GetDescriptor()) {
if (descriptorToken != IInputDataChannel::GetDescriptor()) {
IMSA_HILOGE("InputDataChannelStub descriptor error");
return ErrorCode::ERROR_STATUS_UNKNOWN_TRANSACTION;
}
switch (code) {
case INSERT_TEXT: {
reply.WriteInt32(InsertText(data.ReadString16()));
break;
}
case DELETE_FORWARD: {
reply.WriteInt32(DeleteForward(data.ReadInt32()));
break;
}
case DELETE_BACKWARD: {
reply.WriteInt32(DeleteBackward(data.ReadInt32()));
break;
}
case GET_TEXT_BEFORE_CURSOR: {
GetText(MessageID::MSG_ID_GET_TEXT_BEFORE_CURSOR, data, reply);
break;
}
case GET_TEXT_AFTER_CURSOR: {
GetText(MessageID::MSG_ID_GET_TEXT_AFTER_CURSOR, data, reply);
break;
}
case GET_TEXT_INDEX_AT_CURSOR: {
GetTextIndexAtCursor(MessageID::MSG_ID_GET_TEXT_INDEX_AT_CURSOR, data, reply);
break;
}
case SEND_KEYBOARD_STATUS: {
SendKeyboardStatus(data.ReadInt32());
break;
}
case SEND_FUNCTION_KEY: {
reply.WriteInt32(SendFunctionKey(data.ReadInt32()));
break;
}
case MOVE_CURSOR: {
reply.WriteInt32(MoveCursor(data.ReadInt32()));
break;
}
case GET_ENTER_KEY_TYPE: {
int32_t keyType = 0;
reply.WriteInt32(GetEnterKeyType(keyType));
reply.WriteInt32(keyType);
break;
}
case GET_INPUT_PATTERN: {
int32_t inputPattern = 0;
reply.WriteInt32(GetInputPattern(inputPattern));
reply.WriteInt32(inputPattern);
break;
}
case SELECT_BY_RANGE: {
SelectByRangeOnRemote(data, reply);
break;
}
case HANDLE_EXTEND_ACTION: {
HandleExtendActionOnRemote(data, reply);
break;
}
case SELECT_BY_MOVEMENT: {
SelectByMovementOnRemote(data, reply);
break;
}
case GET_TEXT_CONFIG: {
TextTotalConfig textConfig = {};
reply.WriteInt32(GetTextConfig(textConfig));
ITypesUtil::Marshal(reply, textConfig);
break;
}
default:
return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
if (code >= 0 && code < static_cast<uint32_t>(DATA_CHANNEL_CMD_LAST)) {
return (this->*HANDLERS[code])(data, reply);
} else {
return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
}
return NO_ERROR;
}
int32_t InputDataChannelStub::InsertTextOnRemote(MessageParcel &data, MessageParcel &reply)
{
std::u16string text;
if (!ITypesUtil::Unmarshal(data, text)) {
IMSA_HILOGE("failed to read message parcel");
return ErrorCode::ERROR_EX_PARCELABLE;
}
return reply.WriteInt32(InsertText(text)) ? ErrorCode::NO_ERROR : ErrorCode::ERROR_EX_PARCELABLE;
}
int32_t InputDataChannelStub::DeleteForwardOnRemote(MessageParcel &data, MessageParcel &reply)
{
int32_t length = 0;
if (!ITypesUtil::Unmarshal(data, length)) {
IMSA_HILOGE("failed to read message parcel");
return ErrorCode::ERROR_EX_PARCELABLE;
}
return reply.WriteInt32(DeleteForward(length)) ? ErrorCode::NO_ERROR : ErrorCode::ERROR_EX_PARCELABLE;
}
int32_t InputDataChannelStub::DeleteBackwardOnRemote(MessageParcel &data, MessageParcel &reply)
{
int32_t length = 0;
if (!ITypesUtil::Unmarshal(data, length)) {
IMSA_HILOGE("failed to read message parcel");
return ErrorCode::ERROR_EX_PARCELABLE;
}
return reply.WriteInt32(DeleteBackward(length)) ? ErrorCode::NO_ERROR : ErrorCode::ERROR_EX_PARCELABLE;
}
int32_t InputDataChannelStub::GetTextBeforeCursorOnRemote(MessageParcel &data, MessageParcel &reply)
{
int32_t length = 0;
if (!ITypesUtil::Unmarshal(data, length)) {
IMSA_HILOGE("failed to unmarshal");
return ErrorCode::ERROR_EX_PARCELABLE;
}
std::u16string text;
return ITypesUtil::Marshal(reply, GetTextBeforeCursor(length, text), text) ? ErrorCode::NO_ERROR
: ErrorCode::ERROR_EX_PARCELABLE;
}
int32_t InputDataChannelStub::GetTextAfterCursorOnRemote(MessageParcel &data, MessageParcel &reply)
{
int32_t length = 0;
if (!ITypesUtil::Unmarshal(data, length)) {
IMSA_HILOGE("failed to unmarshal");
return ErrorCode::ERROR_EX_PARCELABLE;
}
std::u16string text;
return ITypesUtil::Marshal(reply, GetTextAfterCursor(length, text), text) ? ErrorCode::NO_ERROR
: ErrorCode::ERROR_EX_PARCELABLE;
}
int32_t InputDataChannelStub::GetTextConfigOnRemote(MessageParcel &data, MessageParcel &reply)
{
TextTotalConfig config;
return ITypesUtil::Marshal(reply, GetTextConfig(config), config) ? ErrorCode::NO_ERROR
: ErrorCode::ERROR_EX_PARCELABLE;
}
int32_t InputDataChannelStub::SendKeyboardStatusOnRemote(MessageParcel &data, MessageParcel &reply)
{
int32_t status = 0;
if (!ITypesUtil::Unmarshal(data, status)) {
IMSA_HILOGE("failed to read message parcel");
return ErrorCode::ERROR_EX_PARCELABLE;
}
SendKeyboardStatus(status);
return reply.WriteInt32(ErrorCode::NO_ERROR) ? ErrorCode::NO_ERROR : ErrorCode::ERROR_EX_PARCELABLE;
}
int32_t InputDataChannelStub::SendFunctionKeyOnRemote(MessageParcel &data, MessageParcel &reply)
{
int32_t functionKey = 0;
if (!ITypesUtil::Unmarshal(data, functionKey)) {
IMSA_HILOGE("failed to read message parcel");
return ErrorCode::ERROR_EX_PARCELABLE;
}
return reply.WriteInt32(SendFunctionKey(functionKey)) ? ErrorCode::NO_ERROR : ErrorCode::ERROR_EX_PARCELABLE;
}
int32_t InputDataChannelStub::MoveCursorOnRemote(MessageParcel &data, MessageParcel &reply)
{
int32_t direction = 0;
if (!ITypesUtil::Unmarshal(data, direction)) {
IMSA_HILOGE("failed to read message parcel");
return ErrorCode::ERROR_EX_PARCELABLE;
}
return reply.WriteInt32(MoveCursor(direction)) ? ErrorCode::NO_ERROR : ErrorCode::ERROR_EX_PARCELABLE;
}
int32_t InputDataChannelStub::GetEnterKeyTypeOnRemote(MessageParcel &data, MessageParcel &reply)
{
int32_t type = 0;
return ITypesUtil::Marshal(reply, GetEnterKeyType(type), type) ? ErrorCode::NO_ERROR
: ErrorCode::ERROR_EX_PARCELABLE;
}
int32_t InputDataChannelStub::GetInputPatternOnRemote(MessageParcel &data, MessageParcel &reply)
{
int32_t pattern = 0;
return ITypesUtil::Marshal(reply, GetInputPattern(pattern), pattern) ? ErrorCode::NO_ERROR
: ErrorCode::ERROR_EX_PARCELABLE;
}
int32_t InputDataChannelStub::SelectByRangeOnRemote(MessageParcel &data, MessageParcel &reply)
{
IMSA_HILOGD("InputDataChannelStub run in");
int32_t start = 0;
int32_t end = 0;
int ret = SendMessage([&data, &start, &end](MessageParcel &parcel) {
return ITypesUtil::Unmarshal(data, start, end) && ITypesUtil::Marshal(parcel, start, end) ?
new (std::nothrow)Message(MessageID::MSG_ID_SELECT_BY_RANGE, &parcel) : nullptr;
});
if (!ITypesUtil::Marshal(reply, ret)) {
IMSA_HILOGE("failed to write reply");
if (!ITypesUtil::Unmarshal(data, start, end)) {
IMSA_HILOGE("failed to read message parcel");
return ErrorCode::ERROR_EX_PARCELABLE;
}
return ErrorCode::NO_ERROR;
return reply.WriteInt32(SelectByRange(start, end)) ? ErrorCode::NO_ERROR : ErrorCode::ERROR_EX_PARCELABLE;
}
int32_t InputDataChannelStub::SelectByMovementOnRemote(MessageParcel &data, MessageParcel &reply)
{
IMSA_HILOGD("InputDataChannelStub run in");
int32_t direction = 0;
int32_t cursorMoveSkip = 0;
auto ret = SendMessage([&data, &direction, &cursorMoveSkip](MessageParcel &parcel) {
return ITypesUtil::Unmarshal(data, direction, cursorMoveSkip)
&& ITypesUtil::Marshal(parcel, direction, cursorMoveSkip)
? new (std::nothrow) Message(MessageID::MSG_ID_SELECT_BY_MOVEMENT, &parcel)
: nullptr;
});
if (!ITypesUtil::Marshal(reply, ret)) {
IMSA_HILOGE("failed to write reply");
int32_t skip = 0;
if (!ITypesUtil::Unmarshal(data, direction, skip)) {
IMSA_HILOGE("failed to read message parcel");
return ErrorCode::ERROR_EX_PARCELABLE;
}
return ErrorCode::NO_ERROR;
return reply.WriteInt32(SelectByMovement(direction, skip)) ? ErrorCode::NO_ERROR : ErrorCode::ERROR_EX_PARCELABLE;
}
int32_t InputDataChannelStub::HandleExtendActionOnRemote(MessageParcel &data, MessageParcel &reply)
{
IMSA_HILOGD("InputDataChannelStub run in");
int32_t action = 0;
auto ret = SendMessage([&data, &action](MessageParcel &parcel) {
return ITypesUtil::Unmarshal(data, action) && ITypesUtil::Marshal(parcel, action) ?
new (std::nothrow)Message(MessageID::MSG_ID_HANDLE_EXTEND_ACTION, &parcel) : nullptr;
});
if (!ITypesUtil::Marshal(reply, ret)) {
IMSA_HILOGE("failed to write reply");
if (!ITypesUtil::Unmarshal(data, action)) {
IMSA_HILOGE("failed to read message parcel");
return ErrorCode::ERROR_EX_PARCELABLE;
}
return ErrorCode::NO_ERROR;
return reply.WriteInt32(HandleExtendAction(action)) ? ErrorCode::NO_ERROR : ErrorCode::ERROR_EX_PARCELABLE;
}
int32_t InputDataChannelStub::GetTextIndexAtCursorOnRemote(MessageParcel &data, MessageParcel &reply)
{
int32_t index = -1;
return ITypesUtil::Marshal(reply, GetTextIndexAtCursor(index), index) ? ErrorCode::NO_ERROR
: ErrorCode::ERROR_EX_PARCELABLE;
}
int32_t InputDataChannelStub::InsertText(const std::u16string &text)
{
IMSA_HILOGI("InputDataChannelStub::InsertText");
if (msgHandler == nullptr) {
return ErrorCode::ERROR_CLIENT_NULL_POINTER;
auto result = std::make_shared<BlockData<int32_t>>(MAX_TIMEOUT);
auto blockTask = [text, result]() {
auto ret = InputMethodController::GetInstance()->InsertText(text);
result->SetValue(ret);
};
ffrt::submit(blockTask);
int32_t ret = 0;
if (!result->GetValue(ret)) {
IMSA_HILOGE("failed due to timeout");
return ErrorCode::ERROR_CONTROLLER_INVOKING_FAILED;
}
MessageParcel *parcel = new MessageParcel;
parcel->WriteString16(text);
Message *msg = new Message(MessageID::MSG_ID_INSERT_CHAR, parcel);
msgHandler->SendMessage(msg);
IMSA_HILOGI("InputDataChannelStub::InsertText return true");
return ErrorCode::NO_ERROR;
return ret;
}
int32_t InputDataChannelStub::DeleteForward(int32_t length)
{
IMSA_HILOGI("InputDataChannelStub::DeleteForward");
if (msgHandler == nullptr) {
return ErrorCode::ERROR_CLIENT_NULL_POINTER;
auto result = std::make_shared<BlockData<int32_t>>(MAX_TIMEOUT);
auto blockTask = [length, result]() {
auto ret = InputMethodController::GetInstance()->DeleteForward(length);
result->SetValue(ret);
};
ffrt::submit(blockTask);
int32_t ret = 0;
if (!result->GetValue(ret)) {
IMSA_HILOGE("failed due to timeout");
return ErrorCode::ERROR_CONTROLLER_INVOKING_FAILED;
}
MessageParcel *parcel = new MessageParcel;
parcel->WriteInt32(length);
Message *msg = new Message(MessageID::MSG_ID_DELETE_FORWARD, parcel);
msgHandler->SendMessage(msg);
return ErrorCode::NO_ERROR;
return ret;
}
int32_t InputDataChannelStub::DeleteBackward(int32_t length)
{
IMSA_HILOGI("InputDataChannelStub::DeleteBackward");
if (msgHandler == nullptr) {
return ErrorCode::ERROR_CLIENT_NULL_POINTER;
auto result = std::make_shared<BlockData<int32_t>>(MAX_TIMEOUT);
auto blockTask = [length, result]() {
auto ret = InputMethodController::GetInstance()->DeleteBackward(length);
result->SetValue(ret);
};
ffrt::submit(blockTask);
int32_t ret = 0;
if (!result->GetValue(ret)) {
IMSA_HILOGE("failed due to timeout");
return ErrorCode::ERROR_CONTROLLER_INVOKING_FAILED;
}
MessageParcel *parcel = new MessageParcel;
parcel->WriteInt32(length);
Message *msg = new Message(MessageID::MSG_ID_DELETE_BACKWARD, parcel);
msgHandler->SendMessage(msg);
return ErrorCode::NO_ERROR;
}
int32_t InputDataChannelStub::GetText(int32_t msgId, MessageParcel &data, MessageParcel &reply)
{
IMSA_HILOGD("InputDataChannelStub::start");
int32_t number = -1;
auto resultHandler = std::make_shared<BlockData<std::u16string>>(MAX_TIMEOUT, u"");
auto ret = SendMessage([&msgId, &data, &number, &resultHandler](MessageParcel &parcel) {
return ITypesUtil::Unmarshal(data, number) && ITypesUtil::Marshal(parcel, number) ?
new (std::nothrow)Message(msgId, &parcel, resultHandler) : nullptr;
});
if (ret != ErrorCode::NO_ERROR) {
return ITypesUtil::Marshal(reply, ret) ? ErrorCode::NO_ERROR : ErrorCode::ERROR_EX_PARCELABLE;
}
auto text = resultHandler->GetValue();
ret = resultHandler->IsTimeOut() ? ErrorCode::ERROR_CONTROLLER_INVOKING_FAILED : ErrorCode::NO_ERROR;
if (!ITypesUtil::Marshal(reply, ret, text)) {
IMSA_HILOGE("failed to write reply");
return ErrorCode::ERROR_EX_PARCELABLE;
}
return ErrorCode::NO_ERROR;
}
int32_t InputDataChannelStub::GetTextIndexAtCursor(int32_t msgId, MessageParcel &data, MessageParcel &reply)
{
IMSA_HILOGD("InputDataChannelStub::start");
auto resultHandler = std::make_shared<BlockData<int32_t>>(MAX_TIMEOUT, -1);
auto ret = SendMessage([&msgId, &resultHandler](MessageParcel &parcel) {
return new (std::nothrow) Message(msgId, &parcel, resultHandler);
});
if (ret != ErrorCode::NO_ERROR) {
return ITypesUtil::Marshal(reply, ret) ? ErrorCode::NO_ERROR : ErrorCode::ERROR_EX_PARCELABLE;
}
auto index = resultHandler->GetValue();
ret = resultHandler->IsTimeOut() ? ErrorCode::ERROR_CONTROLLER_INVOKING_FAILED : ErrorCode::NO_ERROR;
if (!ITypesUtil::Marshal(reply, ret, index)) {
IMSA_HILOGE("failed to write reply");
return ErrorCode::ERROR_EX_PARCELABLE;
}
return ErrorCode::NO_ERROR;
return ret;
}
int32_t InputDataChannelStub::GetTextBeforeCursor(int32_t number, std::u16string &text)
{
return ErrorCode::NO_ERROR;
auto ret = std::make_shared<BlockData<ResultInfo<std::u16string>>>(MAX_TIMEOUT);
auto blockTask = [number, ret]() {
ResultInfo<std::u16string> info;
info.errCode = InputMethodController::GetInstance()->GetLeft(number, info.data);
ret->SetValue(info);
};
ffrt::submit(blockTask);
ResultInfo<std::u16string> result;
if (!ret->GetValue(result)) {
IMSA_HILOGE("failed due to timeout");
return ErrorCode::ERROR_CONTROLLER_INVOKING_FAILED;
}
text = result.data;
return result.errCode;
}
int32_t InputDataChannelStub::GetTextAfterCursor(int32_t number, std::u16string &text)
{
return ErrorCode::NO_ERROR;
auto ret = std::make_shared<BlockData<ResultInfo<std::u16string>>>(MAX_TIMEOUT);
auto blockTask = [number, ret]() {
ResultInfo<std::u16string> info;
info.errCode = InputMethodController::GetInstance()->GetRight(number, info.data);
ret->SetValue(info);
};
ffrt::submit(blockTask);
ResultInfo<std::u16string> result;
if (!ret->GetValue(result)) {
IMSA_HILOGE("failed due to timeout");
return ErrorCode::ERROR_CONTROLLER_INVOKING_FAILED;
}
text = result.data;
return result.errCode;
}
int32_t InputDataChannelStub::GetTextIndexAtCursor(int32_t &index)
{
return ErrorCode::NO_ERROR;
auto ret = std::make_shared<BlockData<ResultInfo<int32_t>>>(MAX_TIMEOUT);
auto blockTask = [ret]() {
ResultInfo<int32_t> info;
info.errCode = InputMethodController::GetInstance()->GetTextIndexAtCursor(info.data);
ret->SetValue(info);
};
ffrt::submit(blockTask);
ResultInfo<int32_t> result;
if (!ret->GetValue(result)) {
IMSA_HILOGE("failed due to timeout");
return ErrorCode::ERROR_CONTROLLER_INVOKING_FAILED;
}
index = result.data;
return result.errCode;
}
int32_t InputDataChannelStub::GetEnterKeyType(int32_t &keyType)
{
IMSA_HILOGI("InputDataChannelStub::GetEnterKeyType");
return InputMethodController::GetInstance()->GetEnterKeyType(keyType);
auto ret = std::make_shared<BlockData<ResultInfo<int32_t>>>(MAX_TIMEOUT);
auto blockTask = [ret]() {
ResultInfo<int32_t> info;
info.errCode = InputMethodController::GetInstance()->GetEnterKeyType(info.data);
ret->SetValue(info);
};
ffrt::submit(blockTask);
ResultInfo<int32_t> result;
if (!ret->GetValue(result)) {
IMSA_HILOGE("failed due to timeout");
return ErrorCode::ERROR_CONTROLLER_INVOKING_FAILED;
}
keyType = result.data;
return result.errCode;
}
int32_t InputDataChannelStub::GetInputPattern(int32_t &inputPattern)
{
IMSA_HILOGI("InputDataChannelStub::GetInputPattern");
return InputMethodController::GetInstance()->GetInputPattern(inputPattern);
auto ret = std::make_shared<BlockData<ResultInfo<int32_t>>>(MAX_TIMEOUT);
auto blockTask = [ret]() {
ResultInfo<int32_t> info;
info.errCode = InputMethodController::GetInstance()->GetInputPattern(info.data);
ret->SetValue(info);
};
ffrt::submit(blockTask);
ResultInfo<int32_t> result;
if (!ret->GetValue(result)) {
IMSA_HILOGE("failed due to timeout");
return ErrorCode::ERROR_CONTROLLER_INVOKING_FAILED;
}
inputPattern = result.data;
return result.errCode;
}
int32_t InputDataChannelStub::GetTextConfig(TextTotalConfig &textConfig)
{
IMSA_HILOGI("InputDataChannelStub run in.");
return InputMethodController::GetInstance()->GetTextConfig(textConfig);
auto ret = std::make_shared<BlockData<ResultInfo<TextTotalConfig>>>(MAX_TIMEOUT);
auto blockTask = [ret]() {
ResultInfo<TextTotalConfig> info;
info.errCode = InputMethodController::GetInstance()->GetTextConfig(info.data);
ret->SetValue(info);
};
ffrt::submit(blockTask);
ResultInfo<TextTotalConfig> result;
if (!ret->GetValue(result)) {
IMSA_HILOGE("failed due to timeout");
return ErrorCode::ERROR_CONTROLLER_INVOKING_FAILED;
}
textConfig = result.data;
return result.errCode;
}
void InputDataChannelStub::SendKeyboardStatus(int32_t status)
{
IMSA_HILOGI("InputDataChannelStub::SendKeyboardStatus");
if (msgHandler != nullptr) {
MessageParcel *parcel = new MessageParcel;
parcel->WriteInt32(status);
Message *msg = new Message(MessageID::MSG_ID_SEND_KEYBOARD_STATUS, parcel);
msgHandler->SendMessage(msg);
auto result = std::make_shared<BlockData<bool>>(MAX_TIMEOUT, false);
auto blockTask = [status, result]() {
InputMethodController::GetInstance()->SendKeyboardStatus(status);
bool ret = true;
result->SetValue(ret);
};
ffrt::submit(blockTask);
if (!result->GetValue()) {
IMSA_HILOGE("failed due to timeout");
}
}
int32_t InputDataChannelStub::SendFunctionKey(int32_t funcKey)
{
IMSA_HILOGI("InputDataChannelStub::SendFunctionKey");
if (msgHandler == nullptr) {
return ErrorCode::ERROR_CLIENT_NULL_POINTER;
auto result = std::make_shared<BlockData<int32_t>>(MAX_TIMEOUT);
auto blockTask = [funcKey, result]() {
auto ret = InputMethodController::GetInstance()->SendFunctionKey(funcKey);
result->SetValue(ret);
};
ffrt::submit(blockTask);
int32_t ret = 0;
if (!result->GetValue(ret)) {
IMSA_HILOGE("failed due to timeout");
return ErrorCode::ERROR_CONTROLLER_INVOKING_FAILED;
}
MessageParcel *parcel = new MessageParcel;
parcel->WriteInt32(funcKey);
Message *msg = new Message(MessageID::MSG_ID_SEND_FUNCTION_KEY, parcel);
msgHandler->SendMessage(msg);
return ErrorCode::NO_ERROR;
return ret;
}
int32_t InputDataChannelStub::MoveCursor(int32_t keyCode)
{
IMSA_HILOGI("InputDataChannelStub::MoveCursor");
if (msgHandler == nullptr) {
return ErrorCode::ERROR_CLIENT_NULL_POINTER;
auto result = std::make_shared<BlockData<int32_t>>(MAX_TIMEOUT);
auto blockTask = [keyCode, result]() {
auto ret = InputMethodController::GetInstance()->MoveCursor(static_cast<Direction>(keyCode));
result->SetValue(ret);
};
ffrt::submit(blockTask);
int32_t ret = 0;
if (!result->GetValue(ret)) {
IMSA_HILOGE("failed due to timeout");
return ErrorCode::ERROR_CONTROLLER_INVOKING_FAILED;
}
MessageParcel *parcel = new MessageParcel;
parcel->WriteInt32(keyCode);
Message *msg = new Message(MessageID::MSG_ID_MOVE_CURSOR, parcel);
msgHandler->SendMessage(msg);
return ErrorCode::NO_ERROR;
return ret;
}
int32_t InputDataChannelStub::SelectByRange(int32_t start, int32_t end)
{
return ErrorCode::NO_ERROR;
auto result = std::make_shared<BlockData<int32_t>>(MAX_TIMEOUT);
auto blockTask = [start, end, result]() {
InputMethodController::GetInstance()->SelectByRange(start, end);
int32_t ret = ErrorCode::NO_ERROR;
result->SetValue(ret);
};
ffrt::submit(blockTask);
int32_t ret = 0;
if (!result->GetValue(ret)) {
IMSA_HILOGE("failed due to timeout");
return ErrorCode::ERROR_CONTROLLER_INVOKING_FAILED;
}
return ret;
}
int32_t InputDataChannelStub::SelectByMovement(int32_t direction, int32_t cursorMoveSkip)
{
return ErrorCode::NO_ERROR;
auto result = std::make_shared<BlockData<int32_t>>(MAX_TIMEOUT);
auto blockTask = [direction, cursorMoveSkip, result]() {
InputMethodController::GetInstance()->SelectByMovement(direction, cursorMoveSkip);
int32_t ret = ErrorCode::NO_ERROR;
result->SetValue(ret);
};
ffrt::submit(blockTask);
int32_t ret = 0;
if (!result->GetValue(ret)) {
IMSA_HILOGE("failed due to timeout");
return ErrorCode::ERROR_CONTROLLER_INVOKING_FAILED;
}
return ret;
}
int32_t InputDataChannelStub::HandleExtendAction(int32_t action)
{
return ErrorCode::NO_ERROR;
}
void InputDataChannelStub::SetHandler(MessageHandler *handler)
{
msgHandler = handler;
}
int32_t InputDataChannelStub::SendMessage(const MsgConstructor &msgConstructor)
{
IMSA_HILOGD("InputMethodCoreStub run in");
if (msgHandler == nullptr) {
IMSA_HILOGE("InputMethodCoreStub msgHandler_ is nullptr");
return ErrorCode::ERROR_EX_NULL_POINTER;
auto result = std::make_shared<BlockData<int32_t>>(MAX_TIMEOUT);
auto blockTask = [action, result]() {
InputMethodController::GetInstance()->HandleExtendAction(action);
int32_t ret = ErrorCode::NO_ERROR;
result->SetValue(ret);
};
ffrt::submit(blockTask);
int32_t ret = 0;
if (!result->GetValue(ret)) {
IMSA_HILOGE("failed due to timeout");
return ErrorCode::ERROR_CONTROLLER_INVOKING_FAILED;
}
auto *parcel = new (std::nothrow) MessageParcel();
if (parcel == nullptr) {
IMSA_HILOGE("parcel is nullptr");
return ErrorCode::ERROR_EX_NULL_POINTER;
}
auto *msg = msgConstructor(*parcel);
if (msg == nullptr) {
IMSA_HILOGE("msg is nullptr");
delete parcel;
return ErrorCode::ERROR_EX_NULL_POINTER;
}
msgHandler->SendMessage(msg);
return ErrorCode::NO_ERROR;
return ret;
}
} // namespace MiscServices
} // namespace OHOS

View File

@ -42,16 +42,13 @@ constexpr int32_t LOOP_COUNT = 5;
constexpr int64_t DELAY_TIME = 100;
const std::unordered_map<std::string, EventType> EVENT_TYPE{ { "imeChange", IME_CHANGE }, { "imeShow", IME_SHOW },
{ "imeHide", IME_HIDE } };
InputMethodController::InputMethodController() : msgHandler_(nullptr), stop_(false)
InputMethodController::InputMethodController()
{
IMSA_HILOGI("InputMethodController structure");
}
InputMethodController::~InputMethodController()
{
QuitWorkThread();
delete msgHandler_;
msgHandler_ = nullptr;
}
sptr<InputMethodController> InputMethodController::GetInstance()
@ -124,27 +121,19 @@ void InputMethodController::SetControllerListener(std::shared_ptr<ControllerList
int32_t InputMethodController::Initialize()
{
auto handler = new (std::nothrow) MessageHandler();
if (handler == nullptr) {
IMSA_HILOGE("failed to new message handler");
return ErrorCode::ERROR_NULL_POINTER;
}
msgHandler_ = handler;
auto client = new (std::nothrow) InputClientStub();
if (client == nullptr) {
IMSA_HILOGE("failed to new client");
return ErrorCode::ERROR_NULL_POINTER;
}
client->SetHandler(msgHandler_);
auto channel = new (std::nothrow) InputDataChannelStub();
if (channel == nullptr) {
delete client;
IMSA_HILOGE("failed to new channel");
return ErrorCode::ERROR_NULL_POINTER;
}
channel->SetHandler(msgHandler_);
InputAttribute attribute = { .inputPattern = InputAttribute::PATTERN_TEXT };
clientInfo_ = { .attribute = attribute, .client = client, .channel = channel };
workThreadHandler = std::thread([this] { WorkThread(); });
// make AppExecFwk::EventHandler handler
handler_ = std::make_shared<AppExecFwk::EventHandler>(AppExecFwk::EventRunner::GetMainEventRunner());
@ -185,210 +174,27 @@ sptr<IInputMethodSystemAbility> InputMethodController::GetSystemAbilityProxy()
return abilityManager_;
}
void InputMethodController::WorkThread()
{
prctl(PR_SET_NAME, "IMCWorkThread");
while (!stop_) {
Message *msg = msgHandler_->GetMessage();
switch (msg->msgId_) {
case MSG_ID_INSERT_CHAR: {
IMSA_HILOGD("insert text");
auto listener = GetTextListener();
if (!isEditable_.load() || listener == nullptr) {
IMSA_HILOGE("not editable or textListener is nullptr");
break;
}
MessageParcel *data = msg->msgContent_;
listener->InsertText(data->ReadString16());
break;
}
case MSG_ID_DELETE_FORWARD: {
IMSA_HILOGD("delete forward");
auto listener = GetTextListener();
if (!isEditable_.load() || listener == nullptr) {
IMSA_HILOGE("not editable or textListener is nullptr");
break;
}
MessageParcel *data = msg->msgContent_;
// reverse for compatibility
listener->DeleteBackward(data->ReadInt32());
break;
}
case MSG_ID_DELETE_BACKWARD: {
IMSA_HILOGD("delete backward");
auto listener = GetTextListener();
if (!isEditable_.load() || listener == nullptr) {
IMSA_HILOGE("not editable or textListener is nullptr");
break;
}
MessageParcel *data = msg->msgContent_;
// reverse for compatibility
listener->DeleteForward(data->ReadInt32());
break;
}
case MSG_ID_ON_INPUT_STOP: {
auto listener = GetTextListener();
if (listener != nullptr) {
IMSA_HILOGE("textListener_ is not nullptr");
listener->SendKeyboardStatus(KeyboardStatus::HIDE);
}
isBound_.store(false);
isEditable_.store(false);
SetTextListener(nullptr);
{
std::lock_guard<std::mutex> autoLock(agentLock_);
agent_ = nullptr;
agentObject_ = nullptr;
}
ClearEditorCache();
break;
}
case MSG_ID_SEND_KEYBOARD_STATUS: {
auto listener = GetTextListener();
if (listener == nullptr) {
IMSA_HILOGE("textListener_ is nullptr");
break;
}
MessageParcel *data = msg->msgContent_;
KeyboardStatus status = static_cast<KeyboardStatus>(data->ReadInt32());
listener->SendKeyboardStatus(status);
if (status == KeyboardStatus::HIDE) {
clientInfo_.isShowKeyboard = false;
}
break;
}
case MSG_ID_SEND_FUNCTION_KEY: {
auto listener = GetTextListener();
if (!isEditable_.load() || listener == nullptr) {
IMSA_HILOGE("not editable or textListener_ is nullptr");
break;
}
MessageParcel *data = msg->msgContent_;
FunctionKey *info = new FunctionKey();
info->SetEnterKeyType(static_cast<EnterKeyType>(data->ReadInt32()));
listener->SendFunctionKey(*info);
delete info;
break;
}
case MSG_ID_MOVE_CURSOR: {
IMSA_HILOGD("move cursor");
auto listener = GetTextListener();
if (!isEditable_.load() || listener == nullptr) {
IMSA_HILOGE("not editable or textListener_ is nullptr");
break;
}
MessageParcel *data = msg->msgContent_;
Direction direction = static_cast<Direction>(data->ReadInt32());
listener->MoveCursor(direction);
break;
}
case MSG_ID_ON_SWITCH_INPUT: {
auto data = msg->msgContent_;
Property property;
SubProperty subProperty;
if (!ITypesUtil::Unmarshal(*data, property, subProperty)) {
IMSA_HILOGE("read property from message parcel failed");
break;
}
OnSwitchInput(property, subProperty);
break;
}
case MSG_ID_ON_PANEL_STATUS_CHANGE: {
auto data = msg->msgContent_;
uint32_t status;
std::vector<InputWindowInfo> windowInfo;
if (!ITypesUtil::Unmarshal(*data, status, windowInfo)) {
IMSA_HILOGE("read property from message parcel failed");
break;
}
OnPanelStatusChange(static_cast<InputWindowStatus>(status), windowInfo);
break;
}
case MSG_ID_SELECT_BY_RANGE: {
IMSA_HILOGD("select by range");
MessageParcel *data = msg->msgContent_;
int32_t start = 0;
int32_t end = 0;
if (!ITypesUtil::Unmarshal(*data, start, end)) {
IMSA_HILOGE("failed to read message parcel");
break;
}
OnSelectByRange(start, end);
break;
}
case MSG_ID_HANDLE_EXTEND_ACTION: {
IMSA_HILOGD("handle extend action");
MessageParcel *data = msg->msgContent_;
int32_t action;
if (!ITypesUtil::Unmarshal(*data, action)) {
IMSA_HILOGE("failed to read message parcel");
break;
}
HandleExtendAction(action);
break;
}
case MSG_ID_SELECT_BY_MOVEMENT: {
IMSA_HILOGD("select by movement");
MessageParcel *data = msg->msgContent_;
int32_t direction = 0;
int32_t cursorMoveSkip = 0;
if (!ITypesUtil::Unmarshal(*data, direction, cursorMoveSkip)) {
IMSA_HILOGE("failed to read message parcel");
break;
}
OnSelectByMovement(direction, cursorMoveSkip);
break;
}
case MSG_ID_GET_TEXT_BEFORE_CURSOR:
case MSG_ID_GET_TEXT_AFTER_CURSOR: {
IMSA_HILOGD("get text, msgId:%{public}d", msg->msgId_);
GetText(msg);
break;
}
case MSG_ID_GET_TEXT_INDEX_AT_CURSOR: {
IMSA_HILOGD("get text index at cursor");
GetTextIndexAtCursor(msg);
break;
}
default: {
IMSA_HILOGD("the message is %{public}d.", msg->msgId_);
break;
}
}
delete msg;
msg = nullptr;
}
}
void InputMethodController::QuitWorkThread()
{
stop_ = true;
Message *msg = new Message(MessageID::MSG_ID_QUIT_WORKER_THREAD, nullptr);
msgHandler_->SendMessage(msg);
if (workThreadHandler.joinable()) {
workThreadHandler.join();
}
}
void InputMethodController::OnSwitchInput(const Property &property, const SubProperty &subProperty)
int32_t InputMethodController::OnSwitchInput(const Property &property, const SubProperty &subProperty)
{
IMSA_HILOGE("InputMethodController::OnSwitchInput");
if (settingListener_ == nullptr) {
IMSA_HILOGE("imeListener_ is nullptr");
return;
return ErrorCode::ERROR_NULL_POINTER;
}
settingListener_->OnImeChange(property, subProperty);
return ErrorCode::NO_ERROR;
}
void InputMethodController::OnPanelStatusChange(
int32_t InputMethodController::OnPanelStatusChange(
const InputWindowStatus &status, const std::vector<InputWindowInfo> &windowInfo)
{
IMSA_HILOGD("InputMethodController::OnPanelStatusChange");
if (settingListener_ == nullptr) {
IMSA_HILOGE("imeListener_ is nullptr");
return;
return ErrorCode::ERROR_NULL_POINTER;
}
settingListener_->OnPanelStatusChange(status, windowInfo);
return ErrorCode::NO_ERROR;
}
void InputMethodController::SaveTextConfig(const TextConfig &textConfig)
@ -800,43 +606,40 @@ int32_t InputMethodController::OnConfigurationChange(Configuration info)
return ErrorCode::NO_ERROR;
}
void InputMethodController::GetText(const Message *msg)
int32_t InputMethodController::GetLeft(int32_t length, std::u16string &text)
{
std::u16string text;
auto resultHandler = msg->textResultHandler_;
IMSA_HILOGD("run in, length: %{public}d", length);
auto listener = GetTextListener();
if (!isEditable_.load() || listener == nullptr) {
IMSA_HILOGE("not editable or textListener_ is nullptr");
resultHandler->SetValue(text);
return;
IMSA_HILOGE("not editable or listener is nullptr");
return ErrorCode::ERROR_CLIENT_NOT_EDITABLE;
}
auto number = msg->msgContent_->ReadInt32();
if (number < 0) {
resultHandler->SetValue(text);
return;
}
if (msg->msgId_ == MSG_ID_GET_TEXT_BEFORE_CURSOR) {
text = listener->GetLeftTextOfCursor(number);
} else {
text = listener->GetRightTextOfCursor(number);
}
IMSA_HILOGI("get text success, msgId:%{public}d", msg->msgId_);
resultHandler->SetValue(text);
text = listener->GetLeftTextOfCursor(length);
return ErrorCode::NO_ERROR;
}
void InputMethodController::GetTextIndexAtCursor(const Message *msg)
int32_t InputMethodController::GetRight(int32_t length, std::u16string &text)
{
int32_t index = -1;
auto resultHandler = msg->indexResultHandler_;
IMSA_HILOGD("run in, length: %{public}d", length);
auto listener = GetTextListener();
if (!isEditable_.load() || listener == nullptr) {
IMSA_HILOGE("not editable or textListener_ is nullptr");
resultHandler->SetValue(index);
return;
return ErrorCode::ERROR_CLIENT_NOT_EDITABLE;
}
text = listener->GetRightTextOfCursor(length);
return ErrorCode::NO_ERROR;
}
int32_t InputMethodController::GetTextIndexAtCursor(int32_t &index)
{
IMSA_HILOGD("run in");
auto listener = GetTextListener();
if (!isEditable_.load() || listener == nullptr) {
IMSA_HILOGE("not editable or textListener_ is nullptr");
return ErrorCode::ERROR_CLIENT_NOT_EDITABLE;
}
index = listener->GetTextIndexAtCursor();
IMSA_HILOGI("get text index success");
resultHandler->SetValue(index);
return ErrorCode::NO_ERROR;
}
bool InputMethodController::DispatchKeyEvent(std::shared_ptr<MMI::KeyEvent> keyEvent)
@ -1028,6 +831,24 @@ void InputMethodController::OnInputReady(sptr<IRemoteObject> agentObject)
agent_ = agent;
}
void InputMethodController::OnInputStop()
{
auto listener = GetTextListener();
if (listener != nullptr) {
IMSA_HILOGD("textListener_ is not nullptr");
listener->SendKeyboardStatus(KeyboardStatus::HIDE);
}
isBound_.store(false);
isEditable_.store(false);
SetTextListener(nullptr);
{
std::lock_guard<std::mutex> autoLock(agentLock_);
agent_ = nullptr;
agentObject_ = nullptr;
}
ClearEditorCache();
}
void InputMethodController::ClearEditorCache()
{
IMSA_HILOGD("clear editor content cache");
@ -1047,7 +868,7 @@ void InputMethodController::ClearEditorCache()
cursorInfo_ = {};
}
void InputMethodController::OnSelectByRange(int32_t start, int32_t end)
void InputMethodController::SelectByRange(int32_t start, int32_t end)
{
IMSA_HILOGI("InputMethodController run in");
auto listener = GetTextListener();
@ -1064,7 +885,7 @@ void InputMethodController::OnSelectByRange(int32_t start, int32_t end)
}
}
void InputMethodController::OnSelectByMovement(int32_t direction, int32_t cursorMoveSkip)
void InputMethodController::SelectByMovement(int32_t direction, int32_t cursorMoveSkip)
{
IMSA_HILOGI("InputMethodController run in");
auto listener = GetTextListener();
@ -1081,15 +902,16 @@ void InputMethodController::OnSelectByMovement(int32_t direction, int32_t cursor
}
}
void InputMethodController::HandleExtendAction(int32_t action)
int32_t InputMethodController::HandleExtendAction(int32_t action)
{
IMSA_HILOGI("InputMethodController run in");
auto listener = GetTextListener();
if (!isEditable_.load() || listener == nullptr) {
IMSA_HILOGE("not editable or textListener_ is nullptr");
return;
IMSA_HILOGE("not editable or textListener is nullptr");
return ErrorCode::ERROR_CLIENT_NOT_EDITABLE;
}
listener->HandleExtendAction(action);
return ErrorCode::NO_ERROR;
}
sptr<OnTextChangedListener> InputMethodController::GetTextListener()
@ -1103,5 +925,83 @@ void InputMethodController::SetTextListener(sptr<OnTextChangedListener> listener
std::lock_guard<std::mutex> lock(textListenerLock_);
textListener_ = listener;
}
int32_t InputMethodController::InsertText(const std::u16string &text)
{
IMSA_HILOGD("in");
auto listener = GetTextListener();
if (!isEditable_.load() || listener == nullptr) {
IMSA_HILOGE("not editable or textListener is nullptr");
return ErrorCode::ERROR_CLIENT_NOT_EDITABLE;
}
listener->InsertText(text);
return ErrorCode::NO_ERROR;
}
int32_t InputMethodController::DeleteForward(int32_t length)
{
IMSA_HILOGD("run in, length: %{public}d", length);
auto listener = GetTextListener();
if (!isEditable_.load() || listener == nullptr) {
IMSA_HILOGE("not editable or textListener is nullptr");
return ErrorCode::ERROR_CLIENT_NOT_EDITABLE;
}
// reverse for compatibility
listener->DeleteBackward(length);
return ErrorCode::NO_ERROR;
}
int32_t InputMethodController::DeleteBackward(int32_t length)
{
IMSA_HILOGD("run in, length: %{public}d", length);
auto listener = GetTextListener();
if (!isEditable_.load() || listener == nullptr) {
IMSA_HILOGE("not editable or textListener is nullptr");
return ErrorCode::ERROR_CLIENT_NOT_EDITABLE;
}
// reverse for compatibility
listener->DeleteForward(length);
return ErrorCode::NO_ERROR;
}
int32_t InputMethodController::MoveCursor(Direction direction)
{
IMSA_HILOGD("run in, direction: %{public}d", static_cast<int32_t>(direction));
auto listener = GetTextListener();
if (!isEditable_.load() || listener == nullptr) {
IMSA_HILOGE("not editable or textListener_ is nullptr");
return ErrorCode::ERROR_CLIENT_NOT_EDITABLE;
}
listener->MoveCursor(direction);
return ErrorCode::NO_ERROR;
}
void InputMethodController::SendKeyboardStatus(int32_t status)
{
IMSA_HILOGD("run in, status: %{public}d", status);
auto listener = GetTextListener();
if (listener == nullptr) {
IMSA_HILOGE("textListener_ is nullptr");
return;
}
auto keyboardStatus = static_cast<KeyboardStatus>(status);
listener->SendKeyboardStatus(keyboardStatus);
if (keyboardStatus == KeyboardStatus::HIDE) {
clientInfo_.isShowKeyboard = false;
}
}
int32_t InputMethodController::SendFunctionKey(int32_t functionKey)
{
auto listener = GetTextListener();
if (!isEditable_.load() || listener == nullptr) {
IMSA_HILOGE("not editable or textListener_ is nullptr");
return ErrorCode::ERROR_CLIENT_NOT_EDITABLE;
}
FunctionKey funcKey;
funcKey.SetEnterKeyType(static_cast<EnterKeyType>(functionKey));
listener->SendFunctionKey(funcKey);
return ErrorCode::NO_ERROR;
}
} // namespace MiscServices
} // namespace OHOS

View File

@ -19,6 +19,8 @@ innerkits_path = "${inputmethod_path}/interfaces/innerkits"
adapter_path = "${inputmethod_path}/adapter"
resourceschedule_ffrt_path = "//foundation/resourceschedule/ffrt"
windowmanager_path = "//foundation/window/window_manager"
bundllemanager_path = "//foundation/bundlemanager/bundle_framework"

View File

@ -22,6 +22,7 @@ config("inputmethod_client_native_config") {
"${inputmethod_path}/frameworks/native/inputmethod_controller/include",
"${inputmethod_path}/services/dfx/include",
"${inputmethod_path}/services/include",
"${resourceschedule_ffrt_path}/interfaces/kits",
]
}
@ -34,6 +35,7 @@ config("inputmethod_client_native_public_config") {
"${inputmethod_path}/frameworks/native/inputmethod_ability/include",
"${inputmethod_path}/services/include",
"${inputmethod_path}/services/dfx/include",
"${resourceschedule_ffrt_path}/interfaces/kits",
]
}
@ -71,6 +73,7 @@ ohos_shared_library("inputmethod_client") {
external_deps = [
"c_utils:utils",
"eventhandler:libeventhandler",
"ffrt:libffrt",
"hilog:libhilog",
"input:libmmi-client",
"ipc:ipc_single",
@ -109,6 +112,7 @@ ohos_static_library("inputmethod_client_static") {
external_deps = [
"c_utils:utils",
"eventhandler:libeventhandler",
"ffrt:libffrt",
"hilog:libhilog",
"input:libmmi-client",
"ipc:ipc_single",

View File

@ -429,6 +429,175 @@ public:
*/
IMF_API void OnInputReady(sptr<IRemoteObject> agentObject);
/**
* @brief Unbind IMC with Service.
*
* This function is unbind imc with service.
*
* @since 10
*/
IMF_API void OnInputStop();
/**
* @brief Insert text.
*
* This function is used to insert text into editor.
*
* @param text Indicates the text which will be inserted.
* @return Returns 0 for success, others for failure.
* @since 10
*/
IMF_API int32_t InsertText(const std::u16string &text);
/**
* @brief Move cursor.
*
* This function is used to move cursor according to the direction.
*
* @param direction Indicates the direction according to which the cursor will be moved.
* @return Returns 0 for success, others for failure.
* @since 10
*/
IMF_API int32_t MoveCursor(Direction direction);
/**
* @brief Delete forward.
*
* This function is used to delete text at the left of cursor.
*
* @param length Indicates the length of deleted text.
* @return Returns 0 for success, others for failure.
* @since 10
*/
IMF_API int32_t DeleteForward(int32_t length);
/**
* @brief Delete backward.
*
* This function is used to delete text at the right of cursor.
*
* @param length Indicates the length of deleted text.
* @return Returns 0 for success, others for failure.
* @since 10
*/
IMF_API int32_t DeleteBackward(int32_t length);
/**
* @brief Get text at the left of cursor.
*
* This function is used to get text at the left of cursor.
*
* @param length Indicates the length of text.
* @param text Indicates the text which will be get.
* @return Returns 0 for success, others for failure.
* @since 10
*/
IMF_API int32_t GetLeft(int32_t length, std::u16string &text);
/**
* @brief Get text at the right of cursor.
*
* This function is used to get text at the right of cursor.
*
* @param length Indicates the length of text.
* @param text Indicates the text which will be get.
* @return Returns 0 for success, others for failure.
* @since 10
*/
IMF_API int32_t GetRight(int32_t length, std::u16string &text);
/**
* @brief Select text in editor by range.
*
* This function is used to select text in editor by range.
*
* @param start Indicates the beginning of the range.
* @param start Indicates the end of the range.
* @return Returns 0 for success, others for failure.
* @since 10
*/
IMF_API void SelectByRange(int32_t start, int32_t end);
/**
* @brief Select text in editor by cursor movement.
*
* This function is used to select text in editor by cursor movement.
*
* @param direction Indicates the direction of cursor movement.
* @param cursorMoveSkip Indicates the skip of cursor movement.
* @return Returns 0 for success, others for failure.
* @since 10
*/
IMF_API void SelectByMovement(int32_t direction, int32_t cursorMoveSkip);
/**
* @brief Handle extend action code.
*
* This function is used to handle extend action code.
*
* @param action Indicates the action code which will be handled.
* @return Returns 0 for success, others for failure.
* @since 10
*/
IMF_API int32_t HandleExtendAction(int32_t action);
/**
* @brief Get the index number of text at cursor.
*
* This function is used to get the index number of text at cursor.
*
* @param index Indicates the index number of text at cursor.
* @return Returns 0 for success, others for failure.
* @since 10
*/
IMF_API int32_t GetTextIndexAtCursor(int32_t &index);
/**
* @brief Send keyboard status.
*
* This function is used to send keyboard status to editor.
*
* @param status Indicates the status of keyboard.
* @since 10
*/
IMF_API void SendKeyboardStatus(int32_t status);
/**
* @brief Send function key.
*
* This function is used to send function key to editor.
*
* @param functionKey Indicates the function key.
* @return Returns 0 for success, others for failure.
* @since 10
*/
IMF_API int32_t SendFunctionKey(int32_t functionKey);
/**
* @brief Inform the change of ime to client.
*
* This function is used to inform the change of ime to client.
*
* @param property Indicates the property of ime.
* @param subProperty Indicates the sub property of ime.
* @return Returns 0 for success, others for failure.
* @since 10
*/
IMF_API int32_t OnSwitchInput(const Property &property, const SubProperty &subProperty);
/**
* @brief Inform the change panel status.
*
* This function is used to inform the change panel status.
*
* @param status Indicates the status of panel.
* @param windowInfo Indicates the detailed info of window.
* @return Returns 0 for success, others for failure.
* @since 10
*/
IMF_API int32_t OnPanelStatusChange(
const InputWindowStatus &status, const std::vector<InputWindowInfo> &windowInfo);
private:
InputMethodController();
~InputMethodController();
@ -439,23 +608,14 @@ private:
int32_t StartInput(sptr<IInputClient> &client, bool isShowKeyboard, bool attachFlag);
int32_t StopInput(sptr<IInputClient> &client);
int32_t ReleaseInput(sptr<IInputClient> &client);
void OnSwitchInput(const Property &property, const SubProperty &subProperty);
void OnPanelStatusChange(const InputWindowStatus &status, const std::vector<InputWindowInfo> &windowInfo);
void WorkThread();
void QuitWorkThread();
int32_t ListInputMethodCommon(InputMethodStatus status, std::vector<Property> &props);
void ClearEditorCache();
void OnSelectByRange(int32_t start, int32_t end);
void OnSelectByMovement(int32_t direction, int32_t cursorMoveSkip);
void HandleExtendAction(int32_t action);
void OnRemoteSaDied(const wptr<IRemoteObject> &object);
void RestoreListenInfoInSaDied();
void RestoreAttachInfoInSaDied();
int32_t RestoreListenEventFlag();
void UpdateNativeEventFlag(EventType eventType, bool isOn);
void SaveTextConfig(const TextConfig &textConfig);
void GetText(const Message *msg);
void GetTextIndexAtCursor(const Message *msg);
sptr<OnTextChangedListener> GetTextListener();
void SetTextListener(sptr<OnTextChangedListener> listener);
@ -484,9 +644,6 @@ private:
static std::mutex instanceLock_;
static sptr<InputMethodController> instance_;
static std::shared_ptr<AppExecFwk::EventHandler> handler_;
std::thread workThreadHandler;
MessageHandler *msgHandler_;
bool stop_;
std::atomic_bool isEditable_{ false };
std::atomic_bool isBound_{ false };

View File

@ -58,6 +58,7 @@ public:
static int32_t selectionStart_;
static int32_t selectionEnd_;
static int32_t selectionDirection_;
static int32_t selectionSkip_;
static int32_t action_;
static KeyboardStatus keyboardStatus_;
static bool isTimeout_;

View File

@ -28,6 +28,7 @@ bool TextListener::status_ = false;
int32_t TextListener::selectionStart_ = -1;
int32_t TextListener::selectionEnd_ = -1;
int32_t TextListener::selectionDirection_ = -1;
int32_t TextListener::selectionSkip_ = -1;
int32_t TextListener::action_ = -1;
KeyboardStatus TextListener::keyboardStatus_ = { KeyboardStatus::NONE };
bool TextListener::isTimeout_ = { false };
@ -117,6 +118,7 @@ void TextListener::HandleExtendAction(int32_t action)
void TextListener::HandleSelect(int32_t keyCode, int32_t cursorMoveSkip)
{
selectionDirection_ = keyCode;
selectionSkip_ = cursorMoveSkip;
textListenerCv_.notify_one();
IMSA_HILOGI("TextChangeListener, selectionDirection_: %{public}d", selectionDirection_);
}
@ -156,6 +158,7 @@ void TextListener::ResetParam()
selectionStart_ = -1;
selectionEnd_ = -1;
selectionDirection_ = -1;
selectionSkip_ = -1;
action_ = -1;
keyboardStatus_ = KeyboardStatus::NONE;
isTimeout_ = false;

View File

@ -52,8 +52,6 @@ void FuzzInputClientStub(const uint8_t *rawData, size_t size)
MessageOption option;
sptr<InputClientStub> mClient = new InputClientStub();
MessageHandler *handler = MessageHandler::Instance();
mClient->SetHandler(handler);
mClient->OnRemoteRequest(code, data, reply, option);
}

View File

@ -31,6 +31,7 @@ group("unittest") {
"cpp_test:InputMethodSwitchTest",
"cpp_test:InputMethodUtilsTest",
"cpp_test:NewImeSwitchTest",
"cpp_test:TextListenerInnerApiTest",
"cpp_test/common:inputmethod_tdd_util",
"napi_test/src:GetInputMethodJsTest",
"resource/bundle_dependencies/extImfBundle:extImf",

View File

@ -391,3 +391,37 @@ ohos_unittest("IdentityCheckerTest") {
"window_manager:libwm",
]
}
ohos_unittest("TextListenerInnerApiTest") {
module_out_path = module_output_path
sources = [ "src/text_listener_inner_api_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/common:inputmethod_test_common",
"${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",
"window_manager:libwm",
]
}

View File

@ -871,32 +871,27 @@ HWTEST_F(InputMethodControllerTest, testWithoutEditableState, TestSize.Level0)
int32_t deleteForwardLength = 1;
ret = inputMethodAbility_->DeleteForward(deleteForwardLength);
EXPECT_EQ(ret, ErrorCode::NO_ERROR);
usleep(100);
EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_EDITABLE);
EXPECT_NE(TextListener::deleteForwardLength_, deleteForwardLength);
int32_t deleteBackwardLength = 2;
ret = inputMethodAbility_->DeleteBackward(deleteBackwardLength);
EXPECT_EQ(ret, ErrorCode::NO_ERROR);
usleep(100);
EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_EDITABLE);
EXPECT_NE(TextListener::deleteBackwardLength_, deleteBackwardLength);
std::string insertText = "t";
ret = inputMethodAbility_->InsertText(insertText);
EXPECT_EQ(ret, ErrorCode::NO_ERROR);
usleep(100);
EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_EDITABLE);
EXPECT_NE(TextListener::insertText_, Str8ToStr16(insertText));
constexpr int32_t funcKey = 1;
ret = inputMethodAbility_->SendFunctionKey(funcKey);
EXPECT_EQ(ret, ErrorCode::NO_ERROR);
usleep(100);
EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_EDITABLE);
EXPECT_NE(TextListener::key_, funcKey);
constexpr int32_t keyCode = 4;
ret = inputMethodAbility_->MoveCursor(keyCode);
EXPECT_EQ(ret, ErrorCode::NO_ERROR);
usleep(100);
EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_EDITABLE);
EXPECT_NE(TextListener::direction_, keyCode);
}

View File

@ -36,8 +36,6 @@ public:
static void CheckCurrentProp(const std::string &extName);
static void CheckCurrentSubProp(const std::string &extName);
static void CheckCurrentSubProps();
static std::mutex imeChangeFlagLock;
static std::condition_variable conditionVar;
static sptr<InputMethodController> imc_;
static bool imeChangeFlag;
static std::string newImeBundleName;
@ -46,8 +44,6 @@ public:
static std::vector<std::string> extName;
static std::vector<std::string> language;
};
std::mutex InputMethodSwitchTest::imeChangeFlagLock;
std::condition_variable InputMethodSwitchTest::conditionVar;
bool InputMethodSwitchTest::imeChangeFlag = false;
sptr<InputMethodController> InputMethodSwitchTest::imc_;
std::string InputMethodSwitchTest::newImeBundleName = "com.example.newTestIme";
@ -59,8 +55,6 @@ constexpr uint32_t IME_EXT_NUM = 2;
constexpr uint32_t NEW_IME_SUBTYPE_NUM = 3;
constexpr uint32_t TOTAL_IME_MIN_NUM = 2;
constexpr uint32_t ENABLE_IME_NUM = 1;
constexpr uint32_t SUBTYPE_SWITCH_DELAY_TIME = 20;
constexpr uint32_t IME_SWITCH_DELAY_TIME = 200;
constexpr uint32_t WAIT_IME_READY_TIME = 1;
class InputMethodSettingListenerImpl : public InputMethodSettingListener {
public:
@ -68,11 +62,7 @@ public:
~InputMethodSettingListenerImpl() = default;
void OnImeChange(const Property &property, const SubProperty &subProperty)
{
{
std::unique_lock<std::mutex> lock(InputMethodSwitchTest::imeChangeFlagLock);
InputMethodSwitchTest::imeChangeFlag = true;
}
InputMethodSwitchTest::conditionVar.notify_one();
InputMethodSwitchTest::imeChangeFlag = true;
IMSA_HILOGI("InputMethodSettingListenerImpl OnImeChange");
}
void OnPanelStatusChange(const InputWindowStatus &status, const std::vector<InputWindowInfo> &windowInfo)
@ -147,12 +137,10 @@ void InputMethodSwitchTest::CheckCurrentSubProps()
HWTEST_F(InputMethodSwitchTest, testImeSwitch, TestSize.Level0)
{
IMSA_HILOGI("oldIme testImeSwitch Test START");
std::unique_lock<std::mutex> lock(imeChangeFlagLock);
imeChangeFlag = false;
// switch to ext testIme
auto ret = imc_->SwitchInputMethod(bundleName);
EXPECT_EQ(ret, ErrorCode::NO_ERROR);
conditionVar.wait_for(lock, std::chrono::milliseconds(IME_SWITCH_DELAY_TIME), [] { return imeChangeFlag == true; });
EXPECT_TRUE(imeChangeFlag);
CheckCurrentProp(extName[0]);
CheckCurrentSubProp(extName[0]);
@ -170,12 +158,9 @@ HWTEST_F(InputMethodSwitchTest, testImeSwitch, TestSize.Level0)
HWTEST_F(InputMethodSwitchTest, testSubTypeSwitch_001, TestSize.Level0)
{
IMSA_HILOGI("oldIme testSubTypeSwitch_001 Test START");
std::unique_lock<std::mutex> lock(imeChangeFlagLock);
imeChangeFlag = false;
int32_t ret = imc_->SwitchInputMethod(bundleName, extName[0]);
EXPECT_EQ(ret, ErrorCode::NO_ERROR);
conditionVar.wait_for(
lock, std::chrono::milliseconds(SUBTYPE_SWITCH_DELAY_TIME), [] { return imeChangeFlag == true; });
EXPECT_FALSE(imeChangeFlag);
CheckCurrentProp(extName[0]);
CheckCurrentSubProp(extName[0]);
@ -192,12 +177,9 @@ HWTEST_F(InputMethodSwitchTest, testSubTypeSwitch_001, TestSize.Level0)
HWTEST_F(InputMethodSwitchTest, testSubTypeSwitch_002, TestSize.Level0)
{
IMSA_HILOGI("oldIme testSubTypeSwitch_002 Test START");
std::unique_lock<std::mutex> lock(imeChangeFlagLock);
imeChangeFlag = false;
int32_t ret = imc_->SwitchInputMethod(bundleName, extName[1]);
EXPECT_EQ(ret, ErrorCode::NO_ERROR);
conditionVar.wait_for(
lock, std::chrono::milliseconds(SUBTYPE_SWITCH_DELAY_TIME), [] { return imeChangeFlag == true; });
EXPECT_TRUE(imeChangeFlag);
CheckCurrentProp(extName[1]);
CheckCurrentSubProp(extName[1]);
@ -232,13 +214,10 @@ HWTEST_F(InputMethodSwitchTest, testSubTypeSwitchWithErrorSubName, TestSize.Leve
HWTEST_F(InputMethodSwitchTest, testSwitchToCurrentImeWithEmptySubName, TestSize.Level0)
{
IMSA_HILOGI("oldIme testSwitchToCurrentImeWithEmptySubName Test START");
std::unique_lock<std::mutex> lock(imeChangeFlagLock);
imeChangeFlag = false;
std::string subName = InputMethodSwitchTest::imc_->GetCurrentInputMethodSubtype()->id;
int32_t ret = imc_->SwitchInputMethod(bundleName);
EXPECT_EQ(ret, ErrorCode::NO_ERROR);
InputMethodSwitchTest::conditionVar.wait_for(
lock, std::chrono::milliseconds(SUBTYPE_SWITCH_DELAY_TIME), [] { return imeChangeFlag == true; });
EXPECT_FALSE(imeChangeFlag);
CheckCurrentProp(subName);
CheckCurrentSubProp(subName);

View File

@ -36,8 +36,6 @@ public:
static void CheckCurrentProp();
static void CheckCurrentSubProp(const std::string &subName);
static void CheckCurrentSubProps();
static std::mutex imeChangeFlagLock;
static std::condition_variable conditionVar;
static bool imeChangeFlag;
static sptr<InputMethodController> imc_;
static std::string bundleName;
@ -46,8 +44,6 @@ public:
static std::vector<std::string> locale;
static std::vector<std::string> language;
};
std::mutex NewImeSwitchTest::imeChangeFlagLock;
std::condition_variable NewImeSwitchTest::conditionVar;
bool NewImeSwitchTest::imeChangeFlag = false;
sptr<InputMethodController> NewImeSwitchTest::imc_;
std::string NewImeSwitchTest::bundleName = "com.example.newTestIme";
@ -56,8 +52,6 @@ std::vector<std::string> NewImeSwitchTest::subName{ "lowerInput", "upperInput",
std::vector<std::string> NewImeSwitchTest::locale{ "en-US", "en-US", "zh-CN" };
std::vector<std::string> NewImeSwitchTest::language{ "english", "english", "chinese" };
constexpr uint32_t IME_SUBTYPE_NUM = 3;
constexpr uint32_t SUBTYPE_SWITCH_DELAY_TIME = 10;
constexpr uint32_t IME_SWITCH_DELAY_TIME = 200;
constexpr uint32_t WAIT_IME_READY_TIME = 1;
class InputMethodSettingListenerImpl : public InputMethodSettingListener {
public:
@ -65,11 +59,7 @@ public:
~InputMethodSettingListenerImpl() = default;
void OnImeChange(const Property &property, const SubProperty &subProperty)
{
{
std::unique_lock<std::mutex> lock(NewImeSwitchTest::imeChangeFlagLock);
NewImeSwitchTest::imeChangeFlag = true;
}
NewImeSwitchTest::conditionVar.notify_one();
NewImeSwitchTest::imeChangeFlag = true;
IMSA_HILOGI("InputMethodSettingListenerImpl OnImeChange");
}
void OnPanelStatusChange(const InputWindowStatus &status, const std::vector<InputWindowInfo> &windowInfo)
@ -143,12 +133,10 @@ void NewImeSwitchTest::CheckCurrentSubProps()
HWTEST_F(NewImeSwitchTest, testNewImeSwitch, TestSize.Level0)
{
IMSA_HILOGI("newIme testNewImeSwitch Test START");
std::unique_lock<std::mutex> lock(imeChangeFlagLock);
imeChangeFlag = false;
// switch to newTestIme
auto ret = imc_->SwitchInputMethod(bundleName);
EXPECT_EQ(ret, ErrorCode::NO_ERROR);
conditionVar.wait_for(lock, std::chrono::milliseconds(IME_SWITCH_DELAY_TIME), [] { return imeChangeFlag == true; });
EXPECT_TRUE(imeChangeFlag);
CheckCurrentProp();
CheckCurrentSubProp(subName[0]);
@ -166,12 +154,9 @@ HWTEST_F(NewImeSwitchTest, testNewImeSwitch, TestSize.Level0)
HWTEST_F(NewImeSwitchTest, testSubTypeSwitch_001, TestSize.Level0)
{
IMSA_HILOGI("newIme testSubTypeSwitch_001 Test START");
std::unique_lock<std::mutex> lock(imeChangeFlagLock);
imeChangeFlag = false;
int32_t ret = imc_->SwitchInputMethod(bundleName, subName[0]);
EXPECT_EQ(ret, ErrorCode::NO_ERROR);
conditionVar.wait_for(
lock, std::chrono::milliseconds(SUBTYPE_SWITCH_DELAY_TIME), [] { return imeChangeFlag == true; });
EXPECT_FALSE(imeChangeFlag);
CheckCurrentProp();
CheckCurrentSubProp(subName[0]);
@ -188,12 +173,9 @@ HWTEST_F(NewImeSwitchTest, testSubTypeSwitch_001, TestSize.Level0)
HWTEST_F(NewImeSwitchTest, testSubTypeSwitch_002, TestSize.Level0)
{
IMSA_HILOGI("newIme testSubTypeSwitch_002 Test START");
std::unique_lock<std::mutex> lock(imeChangeFlagLock);
imeChangeFlag = false;
int32_t ret = imc_->SwitchInputMethod(bundleName, subName[1]);
EXPECT_EQ(ret, ErrorCode::NO_ERROR);
conditionVar.wait_for(
lock, std::chrono::milliseconds(SUBTYPE_SWITCH_DELAY_TIME), [] { return imeChangeFlag == true; });
EXPECT_TRUE(imeChangeFlag);
CheckCurrentProp();
CheckCurrentSubProp(subName[1]);
@ -210,13 +192,9 @@ HWTEST_F(NewImeSwitchTest, testSubTypeSwitch_002, TestSize.Level0)
HWTEST_F(NewImeSwitchTest, testSubTypeSwitch_003, TestSize.Level0)
{
IMSA_HILOGI("newIme testSubTypeSwitch_003 Test START");
std::unique_lock<std::mutex> lock(imeChangeFlagLock);
imeChangeFlag = false;
int32_t ret = imc_->SwitchInputMethod(bundleName, subName[2]);
EXPECT_EQ(ret, ErrorCode::NO_ERROR);
conditionVar.wait_for(
lock, std::chrono::milliseconds(SUBTYPE_SWITCH_DELAY_TIME), [] { return imeChangeFlag == true; });
EXPECT_TRUE(imeChangeFlag);
EXPECT_TRUE(imeChangeFlag);
CheckCurrentProp();
CheckCurrentSubProp(subName[2]);
@ -233,12 +211,9 @@ HWTEST_F(NewImeSwitchTest, testSubTypeSwitch_003, TestSize.Level0)
HWTEST_F(NewImeSwitchTest, testSubTypeSwitch_004, TestSize.Level0)
{
IMSA_HILOGI("newIme testSubTypeSwitch_004 Test START");
std::unique_lock<std::mutex> lock(imeChangeFlagLock);
imeChangeFlag = false;
int32_t ret = imc_->SwitchInputMethod(bundleName, subName[0]);
EXPECT_EQ(ret, ErrorCode::NO_ERROR);
conditionVar.wait_for(
lock, std::chrono::milliseconds(SUBTYPE_SWITCH_DELAY_TIME), [] { return imeChangeFlag == true; });
EXPECT_TRUE(imeChangeFlag);
CheckCurrentProp();
CheckCurrentSubProp(subName[0]);
@ -272,12 +247,9 @@ HWTEST_F(NewImeSwitchTest, testSubTypeSwitchWithErrorSubName, TestSize.Level0)
HWTEST_F(NewImeSwitchTest, testSwitchToCurrentImeWithEmptySubName, TestSize.Level0)
{
IMSA_HILOGI("newIme testSwitchToCurrentImeWithEmptySubName Test START");
std::unique_lock<std::mutex> lock(imeChangeFlagLock);
imeChangeFlag = false;
int32_t ret = imc_->SwitchInputMethod(bundleName);
EXPECT_EQ(ret, ErrorCode::NO_ERROR);
conditionVar.wait_for(
lock, std::chrono::milliseconds(SUBTYPE_SWITCH_DELAY_TIME), [] { return imeChangeFlag == true; });
EXPECT_FALSE(imeChangeFlag);
CheckCurrentProp();
CheckCurrentSubProp(subName[0]);

View File

@ -0,0 +1,583 @@
/*
* 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.
*/
#define private public
#define protected public
#include "input_method_controller.h"
#undef private
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <sys/time.h>
#include <unistd.h>
#include "tdd_util.h"
#include "text_listener.h"
using namespace testing;
using namespace testing::ext;
namespace OHOS {
namespace MiscServices {
using WindowMgr = TddUtil::WindowManager;
constexpr int CURSOR_DIRECTION_BASE_VALUE = 2011;
class TextListenerInnerApiTest : public testing::Test {
public:
static sptr<InputMethodController> imc_;
static sptr<OnTextChangedListener> textListener_;
static void SetUpTestCase(void)
{
WindowMgr::RegisterFocusChangeListener();
WindowMgr::CreateWindow();
WindowMgr::ShowWindow();
bool isFocused = FocusChangedListenerTestImpl::isFocused_->GetValue();
IMSA_HILOGI("getFocus end, isFocused = %{public}d", isFocused);
TextListener::ResetParam();
textListener_ = new TextListener();
imc_ = InputMethodController::GetInstance();
}
static void TearDownTestCase(void)
{
IMSA_HILOGI("InputMethodInnerApiTest::TearDownTestCase");
imc_->Close();
TextListener::ResetParam();
WindowMgr::HideWindow();
WindowMgr::DestroyWindow();
}
void SetUp()
{
IMSA_HILOGI("InputMethodAbilityTest::SetUp");
TextListener::ResetParam();
}
void TearDown()
{
IMSA_HILOGI("InputMethodAbilityTest::TearDown");
TextListener::ResetParam();
}
};
sptr<InputMethodController> TextListenerInnerApiTest::imc_;
sptr<OnTextChangedListener> TextListenerInnerApiTest::textListener_;
/**
* @tc.name: testInsertText01
* @tc.desc: insert text.
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(TextListenerInnerApiTest, testInsertText01, TestSize.Level0)
{
IMSA_HILOGI("TextListenerInnerApiTest testInsertText01 START");
TextListener::ResetParam();
TextListenerInnerApiTest::imc_->Attach(TextListenerInnerApiTest::textListener_);
std::string text = "text";
std::u16string u16Text = Str8ToStr16(text);
int32_t ret = imc_->InsertText(u16Text);
EXPECT_EQ(ret, ErrorCode::NO_ERROR);
EXPECT_EQ(TextListener::insertText_, u16Text);
}
/**
* @tc.name: testInsertText02
* @tc.desc: insert text without Attach.
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(TextListenerInnerApiTest, testInsertText02, TestSize.Level0)
{
IMSA_HILOGI("TextListenerInnerApiTest testInsertText02 START");
TextListener::ResetParam();
std::string text = "text";
std::u16string u16Text = Str8ToStr16(text);
TextListenerInnerApiTest::imc_->Attach(TextListenerInnerApiTest::textListener_);
TextListenerInnerApiTest::imc_->textListener_ = nullptr;
int32_t ret = imc_->InsertText(u16Text);
EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_EDITABLE);
EXPECT_NE(TextListener::insertText_, u16Text);
TextListener::ResetParam();
TextListenerInnerApiTest::imc_->Close();
ret = imc_->InsertText(u16Text);
EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_EDITABLE);
EXPECT_NE(TextListener::insertText_, u16Text);
}
/**
* @tc.name: testDeleteForward01
* @tc.desc: delete forward.
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(TextListenerInnerApiTest, testDeleteForward01, TestSize.Level0)
{
IMSA_HILOGI("TextListenerInnerApiTest testDeleteForward01 START");
TextListener::ResetParam();
imc_->Attach(textListener_);
int32_t length = 5;
int32_t ret = TextListenerInnerApiTest::imc_->DeleteForward(length);
EXPECT_EQ(ret, ErrorCode::NO_ERROR);
EXPECT_EQ(TextListener::deleteBackwardLength_, length);
}
/**
* @tc.name: testDeleteForward02
* @tc.desc: delete forward without Attach.
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(TextListenerInnerApiTest, testDeleteForward02, TestSize.Level0)
{
IMSA_HILOGI("TextListenerInnerApiTest testDeleteForward02 START");
TextListener::ResetParam();
int32_t length = 5;
TextListenerInnerApiTest::imc_->Attach(TextListenerInnerApiTest::textListener_);
TextListenerInnerApiTest::imc_->textListener_ = nullptr;
int32_t ret = TextListenerInnerApiTest::imc_->DeleteForward(length);
EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_EDITABLE);
EXPECT_NE(TextListener::deleteForwardLength_, length);
TextListener::ResetParam();
TextListenerInnerApiTest::imc_->Close();
ret = TextListenerInnerApiTest::imc_->DeleteForward(length);
EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_EDITABLE);
EXPECT_NE(TextListener::deleteBackwardLength_, length);
}
/**
* @tc.name: testDeleteBackward01
* @tc.desc: delete backward.
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(TextListenerInnerApiTest, testDeleteBackward01, TestSize.Level0)
{
IMSA_HILOGI("TextListenerInnerApiTest testDeleteBackward01 START");
TextListener::ResetParam();
imc_->Attach(textListener_);
int32_t length = 5;
int32_t ret = TextListenerInnerApiTest::imc_->DeleteBackward(length);
EXPECT_EQ(ret, ErrorCode::NO_ERROR);
EXPECT_EQ(TextListener::deleteForwardLength_, length);
}
/**
* @tc.name: testDeleteBackward02
* @tc.desc: delete backward without Attach.
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(TextListenerInnerApiTest, testDeleteBackward02, TestSize.Level0)
{
IMSA_HILOGI("TextListenerInnerApiTest testDeleteBackward02 START");
TextListener::ResetParam();
int32_t length = 5;
TextListenerInnerApiTest::imc_->Attach(TextListenerInnerApiTest::textListener_);
TextListenerInnerApiTest::imc_->textListener_ = nullptr;
int32_t ret = TextListenerInnerApiTest::imc_->DeleteBackward(length);
EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_EDITABLE);
EXPECT_NE(TextListener::deleteBackwardLength_, length);
TextListener::ResetParam();
TextListenerInnerApiTest::imc_->Close();
ret = TextListenerInnerApiTest::imc_->DeleteBackward(length);
EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_EDITABLE);
EXPECT_NE(TextListener::deleteForwardLength_, length);
}
/**
* @tc.name: testGetLeft01
* @tc.desc: get left.
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(TextListenerInnerApiTest, testGetLeft01, TestSize.Level0)
{
IMSA_HILOGI("TextListenerInnerApiTest testGetLeft01 START");
TextListener::ResetParam();
imc_->Attach(textListener_);
int32_t number = 5;
std::u16string text;
int32_t ret = TextListenerInnerApiTest::imc_->GetLeft(number, text);
EXPECT_EQ(ret, ErrorCode::NO_ERROR);
EXPECT_EQ(text, Str8ToStr16(TextListener::TEXT_BEFORE_CURSOR));
}
/**
* @tc.name: testGetLeft02
* @tc.desc: get left without Attach.
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(TextListenerInnerApiTest, testGetLeft02, TestSize.Level0)
{
IMSA_HILOGI("TextListenerInnerApiTest testGetLeft02 START");
TextListener::ResetParam();
int32_t number = 5;
std::u16string text = u"";
TextListenerInnerApiTest::imc_->Attach(TextListenerInnerApiTest::textListener_);
TextListenerInnerApiTest::imc_->textListener_ = nullptr;
int32_t ret = TextListenerInnerApiTest::imc_->GetLeft(number, text);
EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_EDITABLE);
EXPECT_NE(text, Str8ToStr16(TextListener::TEXT_BEFORE_CURSOR));
text = u"";
TextListener::ResetParam();
TextListenerInnerApiTest::imc_->Close();
ret = TextListenerInnerApiTest::imc_->GetLeft(number, text);
EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_EDITABLE);
EXPECT_NE(text, Str8ToStr16(TextListener::TEXT_BEFORE_CURSOR));
}
/**
* @tc.name: testGetRight01
* @tc.desc: get left.
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(TextListenerInnerApiTest, testGetRight01, TestSize.Level0)
{
IMSA_HILOGI("TextListenerInnerApiTest testGetRight01 START");
TextListener::ResetParam();
imc_->Attach(textListener_);
int32_t number = 5;
std::u16string text;
int32_t ret = TextListenerInnerApiTest::imc_->GetRight(number, text);
EXPECT_EQ(ret, ErrorCode::NO_ERROR);
EXPECT_EQ(text, Str8ToStr16(TextListener::TEXT_AFTER_CURSOR));
}
/**
* @tc.name: testGetRight02
* @tc.desc: get right without Attach.
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(TextListenerInnerApiTest, testGetRight02, TestSize.Level0)
{
IMSA_HILOGI("TextListenerInnerApiTest testGetRight02 START");
TextListener::ResetParam();
int32_t number = 5;
std::u16string text = u"";
TextListenerInnerApiTest::imc_->Attach(TextListenerInnerApiTest::textListener_);
TextListenerInnerApiTest::imc_->textListener_ = nullptr;
int32_t ret = TextListenerInnerApiTest::imc_->GetRight(number, text);
EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_EDITABLE);
EXPECT_NE(text, Str8ToStr16(TextListener::TEXT_AFTER_CURSOR));
TextListener::ResetParam();
TextListenerInnerApiTest::imc_->Close();
ret = TextListenerInnerApiTest::imc_->GetRight(number, text);
EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_EDITABLE);
EXPECT_NE(text, Str8ToStr16(TextListener::TEXT_AFTER_CURSOR));
}
/**
* @tc.name: testSendKeyboardStatus01
* @tc.desc: send keyboard status.
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(TextListenerInnerApiTest, testSendKeyboardStatus01, TestSize.Level0)
{
IMSA_HILOGI("TextListenerInnerApiTest testSendKeyboardStatus01 START");
TextListener::ResetParam();
imc_->Attach(textListener_);
int32_t status = 1;
TextListenerInnerApiTest::imc_->SendKeyboardStatus(status);
EXPECT_EQ(TextListener::keyboardStatus_, static_cast<KeyboardStatus>(status));
}
/**
* @tc.name: testSendKeyboardStatus02
* @tc.desc: get right without Attach.
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(TextListenerInnerApiTest, testSendKeyboardStatus02, TestSize.Level0)
{
IMSA_HILOGI("TextListenerInnerApiTest testSendKeyboardStatus02 START");
TextListener::ResetParam();
int32_t status = 1;
TextListenerInnerApiTest::imc_->Attach(TextListenerInnerApiTest::textListener_);
TextListenerInnerApiTest::imc_->textListener_ = nullptr;
TextListenerInnerApiTest::imc_->SendKeyboardStatus(status);
EXPECT_NE(TextListener::keyboardStatus_, static_cast<KeyboardStatus>(status));
TextListener::ResetParam();
TextListenerInnerApiTest::imc_->Close();
TextListenerInnerApiTest::imc_->SendKeyboardStatus(status);
EXPECT_NE(TextListener::keyboardStatus_, static_cast<KeyboardStatus>(status));
}
/**
* @tc.name: testSendFunctionKey01
* @tc.desc: send keyboard status.
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(TextListenerInnerApiTest, testSendFunctionKey01, TestSize.Level0)
{
IMSA_HILOGI("TextListenerInnerApiTest testSendFunctionKey01 START");
TextListener::ResetParam();
imc_->Attach(textListener_);
int32_t key = 1;
TextListenerInnerApiTest::imc_->SendFunctionKey(key);
EXPECT_EQ(TextListener::key_, key);
}
/**
* @tc.name: testSendFunctionKey02
* @tc.desc: get right without Attach.
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(TextListenerInnerApiTest, testSendFunctionKey02, TestSize.Level0)
{
IMSA_HILOGI("TextListenerInnerApiTest testSendFunctionKey02 START");
TextListener::ResetParam();
int32_t key = 1;
TextListenerInnerApiTest::imc_->Attach(TextListenerInnerApiTest::textListener_);
TextListenerInnerApiTest::imc_->textListener_ = nullptr;
TextListenerInnerApiTest::imc_->SendFunctionKey(key);
EXPECT_NE(TextListener::key_, key);
TextListener::ResetParam();
TextListenerInnerApiTest::imc_->Close();
TextListenerInnerApiTest::imc_->SendFunctionKey(key);
EXPECT_NE(TextListener::key_, key);
}
/**
* @tc.name: testMoveCursor01
* @tc.desc: move cursor.
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(TextListenerInnerApiTest, testMoveCursor01, TestSize.Level0)
{
IMSA_HILOGI("TextListenerInnerApiTest testMoveCursor01 START");
TextListener::ResetParam();
imc_->Attach(textListener_);
int32_t direction = 2;
int32_t ret = TextListenerInnerApiTest::imc_->MoveCursor(static_cast<Direction>(direction));
EXPECT_EQ(ret, ErrorCode::NO_ERROR);
EXPECT_EQ(TextListener::direction_, direction);
}
/**
* @tc.name: testMoveCursor02
* @tc.desc: move cursor without Attach.
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(TextListenerInnerApiTest, testMoveCursor02, TestSize.Level0)
{
IMSA_HILOGI("TextListenerInnerApiTest testMoveCursor02 START");
TextListener::ResetParam();
int32_t direction = 2;
TextListenerInnerApiTest::imc_->Attach(TextListenerInnerApiTest::textListener_);
TextListenerInnerApiTest::imc_->textListener_ = nullptr;
int32_t ret = TextListenerInnerApiTest::imc_->MoveCursor(static_cast<Direction>(direction));
EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_EDITABLE);
EXPECT_NE(TextListener::direction_, direction);
TextListener::ResetParam();
TextListenerInnerApiTest::imc_->Close();
ret = TextListenerInnerApiTest::imc_->MoveCursor(static_cast<Direction>(direction));
EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_EDITABLE);
EXPECT_NE(TextListener::direction_, direction);
}
/**
* @tc.name: testSelectByRange01
* @tc.desc: select by range.
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(TextListenerInnerApiTest, testSelectByRange01, TestSize.Level0)
{
IMSA_HILOGI("TextListenerInnerApiTest testSelectByRange01 START");
TextListener::ResetParam();
imc_->Attach(textListener_);
int32_t start = 1;
int32_t end = 1;
TextListenerInnerApiTest::imc_->SelectByRange(start, end);
EXPECT_EQ(TextListener::selectionStart_, start);
EXPECT_EQ(TextListener::selectionEnd_, end);
}
/**
* @tc.name: testSelectByRange02
* @tc.desc: select by range without Attach.
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(TextListenerInnerApiTest, testSelectByRange02, TestSize.Level0)
{
IMSA_HILOGI("TextListenerInnerApiTest testSelectByRange02 START");
TextListener::ResetParam();
int32_t start = 1;
int32_t end = 1;
TextListenerInnerApiTest::imc_->Attach(TextListenerInnerApiTest::textListener_);
TextListenerInnerApiTest::imc_->textListener_ = nullptr;
TextListenerInnerApiTest::imc_->SelectByRange(start, end);
EXPECT_NE(TextListener::selectionStart_, start);
EXPECT_NE(TextListener::selectionEnd_, end);
TextListener::ResetParam();
TextListenerInnerApiTest::imc_->Close();
TextListenerInnerApiTest::imc_->SelectByRange(start, end);
EXPECT_NE(TextListener::selectionStart_, start);
EXPECT_NE(TextListener::selectionEnd_, end);
}
/**
* @tc.name: testSelectByMovement01
* @tc.desc: select by movement.
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(TextListenerInnerApiTest, testSelectByMovement01, TestSize.Level0)
{
IMSA_HILOGI("TextListenerInnerApiTest testSelectByMovement01 START");
TextListener::ResetParam();
imc_->Attach(textListener_);
int32_t direction = 1;
int32_t cursorSkip = 2;
TextListenerInnerApiTest::imc_->SelectByMovement(direction, cursorSkip);
EXPECT_EQ(TextListener::selectionDirection_, direction + CURSOR_DIRECTION_BASE_VALUE);
EXPECT_EQ(TextListener::selectionSkip_, cursorSkip);
}
/**
* @tc.name: testSelectByMovement02
* @tc.desc: select by movement without Attach.
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(TextListenerInnerApiTest, testSelectByMovement02, TestSize.Level0)
{
IMSA_HILOGI("TextListenerInnerApiTest testSelectByMovement02 START");
TextListener::ResetParam();
int32_t direction = 1;
int32_t skip = 2;
TextListenerInnerApiTest::imc_->Attach(TextListenerInnerApiTest::textListener_);
TextListenerInnerApiTest::imc_->textListener_ = nullptr;
TextListenerInnerApiTest::imc_->SelectByMovement(direction, skip);
EXPECT_NE(TextListener::direction_, direction);
EXPECT_NE(TextListener::selectionSkip_, skip);
TextListener::ResetParam();
TextListenerInnerApiTest::imc_->Close();
TextListenerInnerApiTest::imc_->SelectByMovement(direction, skip);
EXPECT_NE(TextListener::selectionDirection_, direction + CURSOR_DIRECTION_BASE_VALUE);
EXPECT_NE(TextListener::selectionSkip_, skip);
}
/**
* @tc.name: testHandleExtendAction01
* @tc.desc: move cursor.
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(TextListenerInnerApiTest, testHandleExtendAction01, TestSize.Level0)
{
IMSA_HILOGI("TextListenerInnerApiTest testHandleExtendAction01 START");
TextListener::ResetParam();
imc_->Attach(textListener_);
int32_t action = 2;
int32_t ret = TextListenerInnerApiTest::imc_->HandleExtendAction(action);
EXPECT_EQ(ret, ErrorCode::NO_ERROR);
EXPECT_EQ(TextListener::action_, action);
}
/**
* @tc.name: testHandleExtendAction02
* @tc.desc: move cursor without Attach.
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(TextListenerInnerApiTest, testHandleExtendAction02, TestSize.Level0)
{
IMSA_HILOGI("TextListenerInnerApiTest testHandleExtendAction02 START");
TextListener::ResetParam();
int32_t action = 2;
TextListenerInnerApiTest::imc_->Attach(TextListenerInnerApiTest::textListener_);
TextListenerInnerApiTest::imc_->textListener_ = nullptr;
int32_t ret = TextListenerInnerApiTest::imc_->HandleExtendAction(action);
EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_EDITABLE);
EXPECT_NE(TextListener::action_, action);
TextListener::ResetParam();
TextListenerInnerApiTest::imc_->Close();
ret = TextListenerInnerApiTest::imc_->HandleExtendAction(action);
EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_EDITABLE);
EXPECT_NE(TextListener::action_, action);
}
/**
* @tc.name: testGetTextIndexAtCursor01
* @tc.desc: get text index at cursor.
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(TextListenerInnerApiTest, testGetTextIndexAtCursor01, TestSize.Level0)
{
IMSA_HILOGI("TextListenerInnerApiTest testGetTextIndexAtCursor01 START");
TextListener::ResetParam();
imc_->Attach(textListener_);
int32_t index = -1;
int32_t ret = TextListenerInnerApiTest::imc_->GetTextIndexAtCursor(index);
EXPECT_EQ(ret, ErrorCode::NO_ERROR);
EXPECT_EQ(index, TextListener::TEXT_INDEX);
}
/**
* @tc.name: testGetTextIndexAtCursor02
* @tc.desc: get text index at cursor without Attach.
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(TextListenerInnerApiTest, testGetTextIndexAtCursor02, TestSize.Level0)
{
IMSA_HILOGI("TextListenerInnerApiTest testGetTextIndexAtCursor02 START");
TextListener::ResetParam();
int32_t index = -1;
TextListenerInnerApiTest::imc_->Attach(TextListenerInnerApiTest::textListener_);
TextListenerInnerApiTest::imc_->textListener_ = nullptr;
int32_t ret = TextListenerInnerApiTest::imc_->GetTextIndexAtCursor(index);
EXPECT_NE(ret, ErrorCode::NO_ERROR);
EXPECT_NE(index, TextListener::TEXT_INDEX);
index = -1;
TextListener::ResetParam();
TextListenerInnerApiTest::imc_->Close();
ret = TextListenerInnerApiTest::imc_->GetTextIndexAtCursor(index);
EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_EDITABLE);
EXPECT_NE(index, TextListener::TEXT_INDEX);
}
} // namespace MiscServices
} // namespace OHOS