diff --git a/frameworks/native/inputmethod_ability/include/input_method_panel.h b/frameworks/native/inputmethod_ability/include/input_method_panel.h index 0ca9838269167bf850a8daef4e7754ebe5c9bb1d..bc995a06d9735a976339bc595b3cda88dce6ae9c 100644 --- a/frameworks/native/inputmethod_ability/include/input_method_panel.h +++ b/frameworks/native/inputmethod_ability/include/input_method_panel.h @@ -60,13 +60,6 @@ public: int32_t AdjustPanelRect(const PanelFlag panelFlag, const LayoutParams &layoutParams, bool needUpdateRegion = true); int32_t AdjustPanelRect(PanelFlag panelFlag, EnhancedLayoutParams params, HotAreas hotAreas); int32_t UpdateRegion(std::vector region); - int32_t ParsePanelRect(const PanelFlag panelFlag, const LayoutParams &layoutParams); - int32_t GetSysPanelAdjust(const PanelFlag panelFlag, - std::tuple, std::vector> &keys, const LayoutParams &layoutParams); - int32_t CalculatePanelRect(const PanelFlag panelFlag, PanelAdjustInfo &lanIterValue, PanelAdjustInfo &porIterValue, - const LayoutParams &layoutParams); - int32_t CalculateLandscapeRect(sptr &defaultDisplay, const LayoutParams &layoutParams, - PanelAdjustInfo &lanIterValue); std::tuple, std::vector> GetScreenStatus(const PanelFlag panelFlag); int32_t ChangePanelFlag(PanelFlag panelFlag); PanelType GetPanelType(); @@ -81,6 +74,7 @@ public: int32_t GetCallingWindowInfo(CallingWindowInfo &windowInfo); int32_t SetPrivacyMode(bool isPrivacyMode); bool IsShowing(); + bool IsDisplayPortrait(); int32_t SetTextFieldAvoidInfo(double positionY, double height); void SetPanelHeightCallback(CallbackFunc heightCallback); int32_t IsEnhancedParamValid(PanelFlag panelFlag, EnhancedLayoutParams ¶ms); @@ -123,11 +117,7 @@ private: bool IsSizeValid(uint32_t width, uint32_t height); bool IsSizeValid(PanelFlag panelFlag, uint32_t width, uint32_t height, int32_t displayWidth, int32_t displayHeight); bool IsRectValid(const Rosen::Rect &rect, const WindowSize &displaySize); - bool CheckSize(PanelFlag panelFlag, uint32_t width, uint32_t height, bool isDataPortrait); - bool GetDisplaySize(bool isPortrait, WindowSize &size); - int32_t CalculateFloatRect(const LayoutParams &layoutParams, PanelAdjustInfo &lanIterValue, - PanelAdjustInfo &porIterValue); - int32_t CalculateNoConfigRect(const PanelFlag panelFlag, const LayoutParams &layoutParams); + bool IsRectValid(PanelFlag panelFlag, const Rosen::Rect &rect, const WindowSize &displaySize); int32_t GetResizeParams(Rosen::Rect &portrait, Rosen::Rect &landscape, uint32_t width, uint32_t height); int32_t ParseEnhancedParams( @@ -137,6 +127,10 @@ private: int32_t CalculateAvoidHeight(EnhancedLayoutParam &layoutParam, const WindowSize &displaySize, PanelFlag panelFlag, const PanelAdjustInfo &adjustInfo); + int32_t ParseParams(PanelFlag panelFlag, const LayoutParams &input, Rosen::KeyboardLayoutParams &output); + void ParseParam(PanelFlag panelFlag, const PanelAdjustInfo &adjustInfo, const WindowSize &displaySize, + const Rosen::Rect &inputRect, EnhancedLayoutParam &outputParam); + void CalculateHotAreas(const EnhancedLayoutParams &enhancedParams, const Rosen::KeyboardLayoutParams ¶ms, const FullPanelAdjustInfo &adjustInfo, HotAreas &hotAreas); void CalculateDefaultHotArea(const Rosen::Rect &keyboard, const Rosen::Rect &panel, @@ -145,7 +139,7 @@ private: HotArea &hotArea, uint32_t changeY); void CalculateEnhancedHotArea( const EnhancedLayoutParam &layout, const PanelAdjustInfo &adjustInfo, HotArea &hotArea, uint32_t changeY); - void RectifyAreas(const std::vector availableAreas, std::vector &areas); + void RectifyAreas(const std::vector &availableAreas, std::vector &areas); Rosen::Rect GetRectIntersection(Rosen::Rect a, Rosen::Rect b); uint32_t SafeSubtract(uint32_t minuend, uint32_t subtrahend); @@ -158,7 +152,6 @@ private: int32_t MovePanelRect(int32_t x, int32_t y); int32_t MoveEnhancedPanelRect(int32_t x, int32_t y); - bool IsDisplayPortrait(); bool IsDisplayUnfolded(); int32_t GetDisplaySize(DisplaySize &size); int32_t GetDensityDpi(float &densityDpi); @@ -176,23 +169,38 @@ private: int32_t GetKeyboardArea(PanelFlag panelFlag, const WindowSize &size, PanelAdjustInfo &keyboardArea); int32_t GetWindowOrientation(PanelFlag panelFlag, uint32_t windowWidth, bool &isPortrait); - void SetHotAreas(const HotAreas &hotAreas); - HotAreas GetHotAreas(); sptr GetCurDisplay(); uint64_t GetCurDisplayId(); - void SetIgnoreAdjustInputTypes(const std::vector &inputTypes); - std::vector GetIgnoreAdjustInputTypes(); bool IsNeedConfig(); - int32_t IsValidParam(const ImmersiveEffect &effect); + int32_t IsValidParam(const ImmersiveEffect &effect, const Rosen::KeyboardLayoutParams &layoutParams); int32_t AdjustLayout(const Rosen::KeyboardLayoutParams ¶m); - int32_t FullScreenPrepare(Rosen::KeyboardLayoutParams ¶m); - int32_t NormalImePrepare(Rosen::KeyboardLayoutParams ¶m); - int32_t PrepareAdjustLayout(Rosen::KeyboardLayoutParams ¶m); + int32_t AdjustLayout(const Rosen::KeyboardLayoutParams ¶m, const ImmersiveEffect &effect); + int32_t FullScreenPrepare(Rosen::KeyboardLayoutParams ¶m, const ImmersiveEffect &effect); + int32_t NormalImePrepare(Rosen::KeyboardLayoutParams ¶m, const ImmersiveEffect &effect); + int32_t PrepareAdjustLayout(Rosen::KeyboardLayoutParams ¶m, const ImmersiveEffect &effect); bool IsImmersiveEffectSupported(); Rosen::KeyboardEffectOption ConvertToWmEffect(ImmersiveMode mode, const ImmersiveEffect &effect); void SetImmersiveEffectToNone(); void UpdateImmersiveHotArea(); bool IsValidGradientHeight(uint32_t gradientHeight); + + // Locked read and write functions for concurrent protection + Rosen::KeyboardLayoutParams GetKeyboardLayoutParams(); + void SetKeyboardLayoutParams(Rosen::KeyboardLayoutParams params); + EnhancedLayoutParams GetEnhancedLayoutParams(); + void SetEnhancedLayoutParams(EnhancedLayoutParams params); + std::vector GetIgnoreAdjustInputTypes(); + void SetIgnoreAdjustInputTypes(const std::vector &inputTypes); + ImmersiveEffect LoadImmersiveEffect(); + void StoreImmersiveEffect(ImmersiveEffect effect); + HotAreas GetHotAreas(); + void SetHotAreas(const HotAreas &hotAreas); + ChangeY GetChangeY(); + void SetChangeY(ChangeY changeY); + + void StoreImmersiveMode(ImmersiveMode mode); + CallbackFunc GetPanelHeightCallback(); + sptr window_ = nullptr; sptr winOption_ = nullptr; PanelType panelType_ = PanelType::SOFT_KEYBOARD; @@ -219,30 +227,38 @@ private: std::mutex hotAreasLock_; HotAreas hotAreas_; + std::mutex enhancedLayoutParamMutex_; EnhancedLayoutParams enhancedLayoutParams_; + std::mutex keyboardLayoutParamsMutex_; Rosen::KeyboardLayoutParams keyboardLayoutParams_; std::mutex keyboardSizeLock_; WindowSize keyboardSize_ { 0, 0 }; std::mutex windowListenerLock_; sptr windowChangedListener_ = nullptr; + std::mutex heightCallbackMutex_; CallbackFunc panelHeightCallback_ = nullptr; + std::mutex foldResizeParamMutex_; LayoutParams resizePanelFoldParams_ { // FoldDefaultValue { FOLD_TOP, FOLD_LEFT, FOLD_RIGHT, FOLD_BOTTOM }, { FOLD_TOP, FOLD_LEFT, FOLD_RIGHT, COMMON_BOTTOM } }; + std::mutex unfoldResizeParamMutex_; LayoutParams resizePanelUnfoldParams_ { // UnfoldDefaultValue { UNFOLD_TOP, UNFOLD_LEFT, UNFOLD_RIGHT, UNFOLD_BOTTOM }, { UNFOLD_TOP, UNFOLD_LEFT, UNFOLD_RIGHT, COMMON_BOTTOM } }; std::atomic isWaitSetUiContent_ { true }; std::atomic isInEnhancedAdjust_{ false }; + + std::mutex immersiveModeMutex_; ImmersiveMode immersiveMode_ { ImmersiveMode::NONE_IMMERSIVE }; + std::mutex immersiveEffectMutex_; ImmersiveEffect immersiveEffect_ { 0, GradientMode::NONE, FluidLightMode::NONE }; - uint32_t portraitChangeY_ = 0; - uint32_t landscapeChangeY_ = 0; + std::mutex changeYMutex_; + ChangeY changeY_; }; } // namespace MiscServices } // namespace OHOS diff --git a/frameworks/native/inputmethod_ability/include/panel_common.h b/frameworks/native/inputmethod_ability/include/panel_common.h index 9f8bc09b10bb16fc984cde41f2c7fa58cafc7fb1..9d303fd65369e283ee145a7356977e1cf95a3d50 100644 --- a/frameworks/native/inputmethod_ability/include/panel_common.h +++ b/frameworks/native/inputmethod_ability/include/panel_common.h @@ -27,6 +27,11 @@ struct WindowSize { uint32_t height = 0; }; +struct ChangeY { + uint32_t portrait = 0; + uint32_t landscape = 0; +}; + struct LayoutParams { Rosen::Rect landscapeRect{ 0, 0, 0, 0 }; Rosen::Rect portraitRect{ 0, 0, 0, 0 }; @@ -53,7 +58,8 @@ struct HotAreas { }; struct EnhancedLayoutParam { - Rosen::Rect rect{ 0, 0, 0, 0 }; + Rosen::Rect rect{ 0, 0, 0, 0 }; // rect of keyboard panel + Rosen::Rect panelRect{ 0, 0, 0, 0 }; // rect of system panel int32_t avoidY{ 0 }; uint32_t avoidHeight{ 0 }; inline std::string ToString() const @@ -82,8 +88,8 @@ struct PanelAdjustInfo { int32_t bottom{ 0 }; bool operator==(const PanelAdjustInfo &panelAdjust) const { - return (top == panelAdjust.top && left == panelAdjust.left && right == panelAdjust.right - && bottom == panelAdjust.bottom); + return (top == panelAdjust.top && left == panelAdjust.left && right == panelAdjust.right && + bottom == panelAdjust.bottom); } inline std::string ToString() const { diff --git a/frameworks/native/inputmethod_ability/src/input_method_panel.cpp b/frameworks/native/inputmethod_ability/src/input_method_panel.cpp index 27ae4e5b0e6ebaf707a293a21d232d744d025823..5c2307489598b32a2d7e477b6bb2394a5defb6f5 100644 --- a/frameworks/native/inputmethod_ability/src/input_method_panel.cpp +++ b/frameworks/native/inputmethod_ability/src/input_method_panel.cpp @@ -35,7 +35,6 @@ using WindowState = OHOS::Rosen::WindowState; using namespace Rosen; constexpr float FIXED_SOFT_KEYBOARD_PANEL_RATIO = 0.7; constexpr float NON_FIXED_SOFT_KEYBOARD_PANEL_RATIO = 1; -constexpr int32_t NUMBER_ZERO = 0; constexpr int32_t FIXED_PANEL_POS_X = 0; constexpr int32_t ORIGIN_POS_X = 0; constexpr int32_t ORIGIN_POS_Y = 0; @@ -96,10 +95,10 @@ std::string InputMethodPanel::GeneratePanelName() return windowName; } -int32_t InputMethodPanel::FullScreenPrepare(Rosen::KeyboardLayoutParams ¶m) +int32_t InputMethodPanel::FullScreenPrepare(Rosen::KeyboardLayoutParams ¶m, const ImmersiveEffect &effect) { - portraitChangeY_ = 0; - landscapeChangeY_ = 0; + ChangeY changeY = { .portrait = 0, .landscape = 0 }; + SetChangeY(changeY); if (param.portraitAvoidHeight_ < 0 || param.landscapeAvoidHeight_ < 0 || param.PortraitPanelRect_.posY_ < 0 || param.LandscapePanelRect_.posY_ < 0) { IMSA_HILOGE("invalid portraitAvoidHeight_:%{public}d, landscapeAvoidHeight_:%{public}d, portraitPosY_:\ @@ -108,29 +107,30 @@ int32_t InputMethodPanel::FullScreenPrepare(Rosen::KeyboardLayoutParams ¶m) return ErrorCode::ERROR_INVALID_RANGE; } - uint32_t avoidHeightTmp = static_cast(param.portraitAvoidHeight_) + immersiveEffect_.gradientHeight; + // calculate changeY + uint32_t avoidHeightTmp = static_cast(param.portraitAvoidHeight_) + effect.gradientHeight; if (param.PortraitPanelRect_.height_ < avoidHeightTmp) { - portraitChangeY_ = avoidHeightTmp - param.PortraitPanelRect_.height_; + changeY.portrait = avoidHeightTmp - param.PortraitPanelRect_.height_; param.PortraitPanelRect_.height_ = avoidHeightTmp; param.PortraitPanelRect_.posY_ = static_cast(SafeSubtract( - static_cast(param.PortraitPanelRect_.posY_), portraitChangeY_)); + static_cast(param.PortraitPanelRect_.posY_), changeY.portrait)); } - - avoidHeightTmp = static_cast(param.landscapeAvoidHeight_) + immersiveEffect_.gradientHeight; + avoidHeightTmp = static_cast(param.landscapeAvoidHeight_) + effect.gradientHeight; if (param.LandscapePanelRect_.height_ < avoidHeightTmp) { - landscapeChangeY_ = avoidHeightTmp - param.LandscapePanelRect_.height_; + changeY.landscape = avoidHeightTmp - param.LandscapePanelRect_.height_; param.LandscapePanelRect_.height_ = avoidHeightTmp; param.LandscapePanelRect_.posY_ = static_cast(SafeSubtract( - static_cast(param.LandscapePanelRect_.posY_), landscapeChangeY_)); + static_cast(param.LandscapePanelRect_.posY_), changeY.landscape)); } + SetChangeY(changeY); - int32_t gradientHeightTemp = static_cast(immersiveEffect_.gradientHeight); + // calculate avoid height + auto gradientHeightTemp = static_cast(effect.gradientHeight); if (param.landscapeAvoidHeight_ > INT32_MAX - gradientHeightTemp) { IMSA_HILOGE("landscapeAvoidHeight_ overflow detected"); return ErrorCode::ERROR_INVALID_RANGE; } param.landscapeAvoidHeight_ += gradientHeightTemp; - if (param.portraitAvoidHeight_ > INT32_MAX - gradientHeightTemp) { IMSA_HILOGE("portraitAvoidHeight_ overflow detected"); return ErrorCode::ERROR_INVALID_RANGE; @@ -139,35 +139,35 @@ int32_t InputMethodPanel::FullScreenPrepare(Rosen::KeyboardLayoutParams ¶m) return ErrorCode::NO_ERROR; } -int32_t InputMethodPanel::NormalImePrepare(Rosen::KeyboardLayoutParams ¶m) +int32_t InputMethodPanel::NormalImePrepare(Rosen::KeyboardLayoutParams ¶m, const ImmersiveEffect &effect) { if (param.PortraitPanelRect_.posY_ < 0 || param.LandscapePanelRect_.posY_ < 0) { IMSA_HILOGE("invalid portraitPosY_:%{public}d, landscapePosY_:%{public}d", param.PortraitPanelRect_.posY_, param.LandscapePanelRect_.posY_); return ErrorCode::ERROR_INVALID_RANGE; } - uint32_t portraitHeight = param.PortraitPanelRect_.height_ + immersiveEffect_.gradientHeight; - uint32_t landscapeHeight = param.LandscapePanelRect_.height_ + immersiveEffect_.gradientHeight; + uint32_t portraitHeight = param.PortraitPanelRect_.height_ + effect.gradientHeight; + uint32_t landscapeHeight = param.LandscapePanelRect_.height_ + effect.gradientHeight; param.PortraitPanelRect_.height_ = portraitHeight; param.LandscapePanelRect_.height_ = landscapeHeight; param.LandscapePanelRect_.posY_ = static_cast(SafeSubtract( - static_cast(param.LandscapePanelRect_.posY_), immersiveEffect_.gradientHeight)); + static_cast(param.LandscapePanelRect_.posY_), effect.gradientHeight)); param.PortraitPanelRect_.posY_ = static_cast(SafeSubtract( - static_cast(param.PortraitPanelRect_.posY_), immersiveEffect_.gradientHeight)); - portraitChangeY_ = immersiveEffect_.gradientHeight; - landscapeChangeY_ = immersiveEffect_.gradientHeight; + static_cast(param.PortraitPanelRect_.posY_), effect.gradientHeight)); + + SetChangeY({ .portrait = effect.gradientHeight, .landscape = effect.gradientHeight }); return ErrorCode::NO_ERROR; } -int32_t InputMethodPanel::PrepareAdjustLayout(Rosen::KeyboardLayoutParams ¶m) +int32_t InputMethodPanel::PrepareAdjustLayout(Rosen::KeyboardLayoutParams ¶m, const ImmersiveEffect &effect) { int32_t ret = ErrorCode::NO_ERROR; // full screen keyboard if (param.landscapeAvoidHeight_ != DEFAULT_AVOID_HEIGHT && param.portraitAvoidHeight_ != DEFAULT_AVOID_HEIGHT) { - ret = FullScreenPrepare(param); + ret = FullScreenPrepare(param, effect); } else { - ret = NormalImePrepare(param); + ret = NormalImePrepare(param, effect); } if (ret != ErrorCode::NO_ERROR) { IMSA_HILOGE("prepare failed"); @@ -177,6 +177,11 @@ int32_t InputMethodPanel::PrepareAdjustLayout(Rosen::KeyboardLayoutParams ¶m } int32_t InputMethodPanel::AdjustLayout(const Rosen::KeyboardLayoutParams ¶m) +{ + return AdjustLayout(param, LoadImmersiveEffect()); +} + +int32_t InputMethodPanel::AdjustLayout(const Rosen::KeyboardLayoutParams ¶m, const ImmersiveEffect &effect) { if (window_ == nullptr) { IMSA_HILOGE("window is nullptr!"); @@ -184,15 +189,14 @@ int32_t InputMethodPanel::AdjustLayout(const Rosen::KeyboardLayoutParams ¶m) } Rosen::KeyboardLayoutParams paramTmp = param; - if (immersiveEffect_.gradientHeight != 0) { - IMSA_HILOGI("gradientHeight:%{public}u is not zero, need adjust layout", immersiveEffect_.gradientHeight); - auto ret = PrepareAdjustLayout(paramTmp); + if (effect.gradientHeight != 0) { + IMSA_HILOGI("gradientHeight:%{public}u is not zero, need adjust layout", effect.gradientHeight); + auto ret = PrepareAdjustLayout(paramTmp, effect); if (ret != ErrorCode::NO_ERROR) { return ret; } } else { - portraitChangeY_ = 0; - landscapeChangeY_ = 0; + SetChangeY({ 0, 0 }); } // The actual system panel height includes the gradient height, which may not be consistent with the cached value. auto wmRet = window_->AdjustKeyboardLayout(paramTmp); @@ -238,12 +242,14 @@ int32_t InputMethodPanel::SetPanelProperties() } return ErrorCode::NO_ERROR; } - keyboardLayoutParams_.gravity_ = gravity; - auto ret = AdjustLayout(keyboardLayoutParams_); + auto params = GetKeyboardLayoutParams(); + params.gravity_ = gravity; + auto ret = AdjustLayout(params); if (ret != ErrorCode::NO_ERROR) { IMSA_HILOGE("SetWindowGravity failed, ret is %{public}d, start destroy window!", ret); return ErrorCode::ERROR_OPERATE_PANEL; } + SetKeyboardLayoutParams(params); return ErrorCode::NO_ERROR; } @@ -286,14 +292,16 @@ int32_t InputMethodPanel::GetResizeParams( } if (IsDisplayUnfolded()) { + std::lock_guard lock(unfoldResizeParamMutex_); IMSA_HILOGI("foldable device without fold state"); - if (!isInEnhancedAdjust_) { + if (!isInEnhancedAdjust_.load()) { RectifyResizeParams(resizePanelUnfoldParams_, displaySize); } currParams = resizePanelUnfoldParams_; } else { + std::lock_guard lock(foldResizeParamMutex_); IMSA_HILOGI("foldable device with fold state or non-foldable device"); - if (!isInEnhancedAdjust_) { + if (!isInEnhancedAdjust_.load()) { RectifyResizeParams(resizePanelFoldParams_, displaySize); } currParams = resizePanelFoldParams_; @@ -339,20 +347,21 @@ void InputMethodPanel::UpdateRectParams( void InputMethodPanel::UpdateResizeParams() { + auto layoutParams = GetKeyboardLayoutParams(); if (IsDisplayUnfolded()) { + std::lock_guard lock(unfoldResizeParamMutex_); IMSA_HILOGI("foldable device without fold state"); - resizePanelUnfoldParams_ = { keyboardLayoutParams_.LandscapeKeyboardRect_, - keyboardLayoutParams_.PortraitKeyboardRect_ }; + resizePanelUnfoldParams_ = { layoutParams.LandscapeKeyboardRect_, layoutParams.PortraitKeyboardRect_ }; } else { + std::lock_guard lock(foldResizeParamMutex_); IMSA_HILOGI("foldable device in fold state or non-foldable device"); - resizePanelFoldParams_ = { keyboardLayoutParams_.LandscapeKeyboardRect_, - keyboardLayoutParams_.PortraitKeyboardRect_ }; + resizePanelFoldParams_ = { layoutParams.LandscapeKeyboardRect_, layoutParams.PortraitKeyboardRect_ }; } } int32_t InputMethodPanel::ResizeEnhancedPanel(uint32_t width, uint32_t height) { - EnhancedLayoutParams layoutParam = enhancedLayoutParams_; + auto layoutParam = GetEnhancedLayoutParams(); auto ret = GetResizeParams(layoutParam.portrait.rect, layoutParam.landscape.rect, width, height); if (ret != ErrorCode::NO_ERROR) { IMSA_HILOGE("failed to GetResizeParams, ret: %{public}d", ret); @@ -364,7 +373,6 @@ int32_t InputMethodPanel::ResizeEnhancedPanel(uint32_t width, uint32_t height) IMSA_HILOGE("failed to AdjustPanelRect, ret: %{public}d", ret); return ErrorCode::ERROR_OPERATE_PANEL; } - UpdateResizeParams(); std::lock_guard lock(keyboardSizeLock_); keyboardSize_ = { width, height }; IMSA_HILOGI("success, width/height: %{public}u/%{public}u.", width, height); @@ -394,18 +402,18 @@ int32_t InputMethodPanel::ResizePanel(uint32_t width, uint32_t height) IMSA_HILOGE("size is invalid!"); return ErrorCode::ERROR_BAD_PARAMETERS; } - LayoutParams params = { enhancedLayoutParams_.landscape.rect, enhancedLayoutParams_.portrait.rect }; - auto ret = GetResizeParams(params.portraitRect, params.landscapeRect, width, height); + auto currentParams = GetEnhancedLayoutParams(); + LayoutParams targetParams = { currentParams.landscape.rect, currentParams.portrait.rect }; + auto ret = GetResizeParams(targetParams.portraitRect, targetParams.landscapeRect, width, height); if (ret != ErrorCode::NO_ERROR) { IMSA_HILOGE("failed to GetResizeParams, ret: %{public}d", ret); return ret; } - ret = AdjustPanelRect(panelFlag_, params); + ret = AdjustPanelRect(panelFlag_, targetParams); if (ret != ErrorCode::NO_ERROR) { IMSA_HILOGE("failed to resize, ret: %{public}d", ret); return ErrorCode::ERROR_OPERATE_PANEL; } - UpdateResizeParams(); std::lock_guard lock(keyboardSizeLock_); keyboardSize_ = { width, height }; IMSA_HILOGI("success, width/height: %{public}u/%{public}u.", width, height); @@ -429,7 +437,8 @@ int32_t InputMethodPanel::Resize(uint32_t width, uint32_t height) int32_t InputMethodPanel::MovePanelRect(int32_t x, int32_t y) { - LayoutParams params = { enhancedLayoutParams_.landscape.rect, enhancedLayoutParams_.portrait.rect }; + auto currentParams = GetEnhancedLayoutParams(); + LayoutParams params = { currentParams.landscape.rect, currentParams.portrait.rect }; if (IsDisplayPortrait()) { params.portraitRect.posX_ = x; params.portraitRect.posY_ = y; @@ -446,7 +455,7 @@ int32_t InputMethodPanel::MovePanelRect(int32_t x, int32_t y) int32_t InputMethodPanel::MoveEnhancedPanelRect(int32_t x, int32_t y) { - auto params = enhancedLayoutParams_; + auto params = GetEnhancedLayoutParams(); if (IsDisplayPortrait()) { params.portrait.rect.posX_ = x; params.portrait.rect.posY_ = y; @@ -524,8 +533,8 @@ int32_t InputMethodPanel::AdjustKeyboard() { isAdjustInfoInitialized_.store(false); int32_t ret = 0; + auto params = GetEnhancedLayoutParams(); if (isInEnhancedAdjust_.load()) { - EnhancedLayoutParams params = enhancedLayoutParams_; DisplaySize displaySize; ret = GetDisplaySize(displaySize); if (ret != ErrorCode::NO_ERROR) { @@ -537,8 +546,8 @@ int32_t InputMethodPanel::AdjustKeyboard() auto hotAreas = GetHotAreas(); ret = AdjustPanelRect(panelFlag_, params, hotAreas); } else { - LayoutParams params = { enhancedLayoutParams_.landscape.rect, enhancedLayoutParams_.portrait.rect }; - ret = AdjustPanelRect(panelFlag_, params); + LayoutParams layoutParams = { params.landscape.rect, params.portrait.rect }; + ret = AdjustPanelRect(panelFlag_, layoutParams); } if (ret != ErrorCode::NO_ERROR) { IMSA_HILOGE("failed to adjust keyboard, ret: %{public}d", ret); @@ -555,37 +564,19 @@ int32_t InputMethodPanel::AdjustPanelRect( IMSA_HILOGE("window_ is nullptr!"); return ErrorCode::ERROR_WINDOW_MANAGER; } - if (layoutParams.portraitRect.posX_ < 0 || layoutParams.portraitRect.posY_ < 0 || - layoutParams.landscapeRect.posX_ < 0 || layoutParams.landscapeRect.posY_ < 0) { - IMSA_HILOGE("posX_ and posY_ cannot be less than 0!"); - return ErrorCode::ERROR_PARAMETER_CHECK_FAILED; - } - if (!CheckSize(panelFlag, layoutParams.portraitRect.width_, layoutParams.portraitRect.height_, true)) { - IMSA_HILOGE("portrait invalid size!"); - return ErrorCode::ERROR_PARAMETER_CHECK_FAILED; - } - if (!CheckSize(panelFlag, layoutParams.landscapeRect.width_, layoutParams.landscapeRect.height_, false)) { - IMSA_HILOGE("landscape invalid size!"); - return ErrorCode::ERROR_PARAMETER_CHECK_FAILED; - } - auto result = ParsePanelRect(panelFlag, layoutParams); + KeyboardLayoutParams resultParams; + int32_t result = ParseParams(panelFlag, layoutParams, resultParams); if (result != ErrorCode::NO_ERROR) { IMSA_HILOGE("failed to parse panel rect, result: %{public}d!", result); - return ErrorCode::ERROR_WINDOW_MANAGER; + return result; } - - keyboardLayoutParams_.landscapeAvoidHeight_ = DEFAULT_AVOID_HEIGHT; - keyboardLayoutParams_.portraitAvoidHeight_ = DEFAULT_AVOID_HEIGHT; - auto ret = AdjustLayout(keyboardLayoutParams_); + auto ret = AdjustLayout(resultParams); if (ret != ErrorCode::NO_ERROR) { IMSA_HILOGE("AdjustPanelRect error, err: %{public}d!", ret); return ErrorCode::ERROR_WINDOW_MANAGER; } - if (panelFlag_ != panelFlag) { - InputMethodAbility::GetInstance().NotifyPanelStatus(true, panelFlag); - } + UpdateLayoutInfo(panelFlag, layoutParams, {}, resultParams, false); UpdateResizeParams(); - UpdateLayoutInfo(panelFlag, layoutParams, {}, keyboardLayoutParams_, false); if (needUpdateRegion) { UpdateHotAreas(); } @@ -604,9 +595,9 @@ Rosen::KeyboardLayoutParams InputMethodPanel::ConvertToWMSParam( wmsParams.gravity_ = WindowGravity::WINDOW_GRAVITY_FLOAT; } wmsParams.LandscapeKeyboardRect_ = layoutParams.landscape.rect; - wmsParams.LandscapePanelRect_ = layoutParams.landscape.rect; + wmsParams.LandscapePanelRect_ = layoutParams.landscape.panelRect; wmsParams.PortraitKeyboardRect_ = layoutParams.portrait.rect; - wmsParams.PortraitPanelRect_ = layoutParams.portrait.rect; + wmsParams.PortraitPanelRect_ = layoutParams.portrait.panelRect; wmsParams.portraitAvoidHeight_ = layoutParams.portrait.avoidHeight; wmsParams.landscapeAvoidHeight_ = layoutParams.landscape.avoidHeight; return wmsParams; @@ -664,6 +655,7 @@ int32_t InputMethodPanel::AdjustPanelRect(PanelFlag panelFlag, EnhancedLayoutPar return ret; } // adjust rect + auto lastWmsParam = GetKeyboardLayoutParams(); auto wmsParams = ConvertToWMSParam(panelFlag, params); ret = AdjustLayout(wmsParams); if (ret != ErrorCode::NO_ERROR) { @@ -677,14 +669,11 @@ int32_t InputMethodPanel::AdjustPanelRect(PanelFlag panelFlag, EnhancedLayoutPar auto result = window_->SetKeyboardTouchHotAreas(wmsHotAreas); if (result != WMError::WM_OK) { IMSA_HILOGE("SetKeyboardTouchHotAreas error, err: %{public}d!", result); - ret = AdjustLayout(keyboardLayoutParams_); + ret = AdjustLayout(lastWmsParam); IMSA_HILOGE("restore layout param, result: %{public}d", ret); return ErrorCode::ERROR_WINDOW_MANAGER; } SetHotAreas(hotAreas); - if (panelFlag_ != panelFlag) { - InputMethodAbility::GetInstance().NotifyPanelStatus(true, panelFlag); - } UpdateLayoutInfo(panelFlag, {}, params, wmsParams, true); UpdateResizeParams(); IMSA_HILOGI("success, type/flag: %{public}d/%{public}d.", static_cast(panelType_), @@ -695,14 +684,17 @@ int32_t InputMethodPanel::AdjustPanelRect(PanelFlag panelFlag, EnhancedLayoutPar void InputMethodPanel::UpdateLayoutInfo(PanelFlag panelFlag, const LayoutParams ¶ms, const EnhancedLayoutParams &enhancedParams, const KeyboardLayoutParams &wmsParams, bool isEnhanced) { + SetKeyboardLayoutParams(wmsParams); if (isEnhanced) { - enhancedLayoutParams_ = enhancedParams; - keyboardLayoutParams_ = wmsParams; + SetEnhancedLayoutParams(enhancedParams); } else { EnhancedLayoutParams enhancedLayoutParams = { .isFullScreen = false, .portrait = { .rect = params.portraitRect }, .landscape = { .rect = params.landscapeRect } }; - enhancedLayoutParams_ = std::move(enhancedLayoutParams); + SetEnhancedLayoutParams(enhancedLayoutParams); + } + if (panelFlag_ != panelFlag) { + InputMethodAbility::GetInstance().NotifyPanelStatus(true, panelFlag); } panelFlag_ = panelFlag; isInEnhancedAdjust_.store(isEnhanced); @@ -760,11 +752,21 @@ bool InputMethodPanel::IsRectValid(const Rosen::Rect &rect, const WindowSize &di return true; } +bool InputMethodPanel::IsRectValid(PanelFlag panelFlag, const Rosen::Rect &rect, const WindowSize &displaySize) +{ + if (rect.posX_ < 0 || rect.posY_ < 0) { + IMSA_HILOGE("posX_ and posY_ cannot be less than 0!"); + return false; + } + return IsSizeValid(panelFlag, rect.width_, rect.height_, displaySize.width, displaySize.height); +} + int32_t InputMethodPanel::RectifyRect(bool isFullScreen, EnhancedLayoutParam &layoutParam, const WindowSize &displaySize, PanelFlag panelFlag, const PanelAdjustInfo &adjustInfo) { if (isFullScreen) { layoutParam.rect = { ORIGIN_POS_X, ORIGIN_POS_Y, displaySize.width, displaySize.height }; + layoutParam.panelRect = layoutParam.rect; return ErrorCode::NO_ERROR; } if (!IsRectValid(layoutParam.rect, displaySize)) { @@ -775,6 +777,7 @@ int32_t InputMethodPanel::RectifyRect(bool isFullScreen, EnhancedLayoutParam &la layoutParam.rect = { FIXED_PANEL_POS_X, static_cast(displaySize.height - layoutParam.rect.height_), displaySize.width, layoutParam.rect.height_ }; } + layoutParam.panelRect = layoutParam.rect; return ErrorCode::NO_ERROR; } @@ -820,10 +823,12 @@ void InputMethodPanel::UpdateHotAreas() IMSA_HILOGE("GetAdjustInfo failed ret: %{public}d", ret); return; } - CalculateDefaultHotArea(keyboardLayoutParams_.LandscapeKeyboardRect_, keyboardLayoutParams_.LandscapePanelRect_, - adjustInfo.landscape, hotAreas.landscape, landscapeChangeY_); - CalculateDefaultHotArea(keyboardLayoutParams_.PortraitKeyboardRect_, keyboardLayoutParams_.PortraitPanelRect_, - adjustInfo.portrait, hotAreas.portrait, portraitChangeY_); + auto changeY = GetChangeY(); + auto layoutParams = GetKeyboardLayoutParams(); + CalculateDefaultHotArea(layoutParams.LandscapeKeyboardRect_, layoutParams.LandscapePanelRect_, + adjustInfo.landscape, hotAreas.landscape, changeY.landscape); + CalculateDefaultHotArea(layoutParams.PortraitKeyboardRect_, layoutParams.PortraitPanelRect_, adjustInfo.portrait, + hotAreas.portrait, changeY.portrait); auto wmsHotAreas = ConvertToWMSHotArea(hotAreas); WMError result = window_->SetKeyboardTouchHotAreas(wmsHotAreas); if (result != WMError::WM_OK) { @@ -839,14 +844,15 @@ void InputMethodPanel::UpdateHotAreas() void InputMethodPanel::CalculateHotAreas(const EnhancedLayoutParams &enhancedParams, const Rosen::KeyboardLayoutParams ¶ms, const FullPanelAdjustInfo &adjustInfo, HotAreas &hotAreas) { + auto changeY = GetChangeY(); if (isInEnhancedAdjust_.load()) { - CalculateEnhancedHotArea(enhancedParams.portrait, adjustInfo.portrait, hotAreas.portrait, portraitChangeY_); - CalculateEnhancedHotArea(enhancedParams.landscape, adjustInfo.landscape, hotAreas.landscape, landscapeChangeY_); + CalculateEnhancedHotArea(enhancedParams.portrait, adjustInfo.portrait, hotAreas.portrait, changeY.portrait); + CalculateEnhancedHotArea(enhancedParams.landscape, adjustInfo.landscape, hotAreas.landscape, changeY.landscape); } else { CalculateHotArea(params.PortraitKeyboardRect_, params.PortraitPanelRect_, adjustInfo.portrait, - hotAreas.portrait, portraitChangeY_); + hotAreas.portrait, changeY.portrait); CalculateHotArea(params.LandscapeKeyboardRect_, params.LandscapePanelRect_, adjustInfo.landscape, - hotAreas.landscape, landscapeChangeY_); + hotAreas.landscape, changeY.landscape); } hotAreas.isSet = true; IMSA_HILOGD("portrait keyboard: %{public}s, panel: %{public}s", @@ -868,9 +874,10 @@ void InputMethodPanel::CalculateHotArea(const Rosen::Rect &keyboard, const Rosen std::vector availableAreas = { { { ORIGIN_POS_X, ORIGIN_POS_Y, keyboard.width_, keyboard.height_ } } }; RectifyAreas(availableAreas, hotArea.keyboardHotArea); // calculate panel hot area - Rosen::Rect left = { ORIGIN_POS_X, ORIGIN_POS_Y + changeY, static_cast(adjustInfo.left), panel.height_ }; + Rosen::Rect left = { ORIGIN_POS_X, ORIGIN_POS_Y + static_cast(changeY), + static_cast(adjustInfo.left), panel.height_ }; Rosen::Rect right = { .posX_ = static_cast(panel.width_) - adjustInfo.right, - .posY_ = ORIGIN_POS_Y + changeY, + .posY_ = ORIGIN_POS_Y + static_cast(changeY), .width_ = static_cast(adjustInfo.right), .height_ = panel.height_ }; Rosen::Rect bottom = { .posX_ = ORIGIN_POS_X, @@ -896,10 +903,10 @@ void InputMethodPanel::CalculateEnhancedHotArea( .height_ = SafeSubtract(layout.avoidHeight, static_cast(adjustInfo.bottom)) }); RectifyAreas(availableAreas, hotArea.keyboardHotArea); // calculate panel hot area - Rosen::Rect left = { ORIGIN_POS_X, layout.avoidY + changeY, static_cast(adjustInfo.left), - layout.avoidHeight }; + Rosen::Rect left = { ORIGIN_POS_X, layout.avoidY + static_cast(changeY), + static_cast(adjustInfo.left), layout.avoidHeight }; Rosen::Rect right = { .posX_ = static_cast(layout.rect.width_) - adjustInfo.right, - .posY_ = layout.avoidY + changeY, + .posY_ = layout.avoidY + static_cast(changeY), .width_ = static_cast(adjustInfo.right), .height_ = layout.avoidHeight }; Rosen::Rect bottom = { .posX_ = ORIGIN_POS_X, @@ -917,9 +924,10 @@ void InputMethodPanel::CalculateDefaultHotArea(const Rosen::Rect &keyboard, cons hotArea.keyboardHotArea.clear(); hotArea.keyboardHotArea.push_back({ ORIGIN_POS_X, ORIGIN_POS_Y, keyboard.width_, keyboard.height_ }); // calculate panel hot area - Rosen::Rect left = { ORIGIN_POS_X, ORIGIN_POS_Y + changeY, static_cast(adjustInfo.left), panel.height_ }; + Rosen::Rect left = { ORIGIN_POS_X, ORIGIN_POS_Y + static_cast(changeY), + static_cast(adjustInfo.left), panel.height_ }; Rosen::Rect right = { .posX_ = static_cast(panel.width_) - adjustInfo.right, - .posY_ = ORIGIN_POS_Y + changeY, + .posY_ = ORIGIN_POS_Y + static_cast(changeY), .width_ = static_cast(adjustInfo.right), .height_ = panel.height_ }; Rosen::Rect bottom = { .posX_ = ORIGIN_POS_X, @@ -929,7 +937,7 @@ void InputMethodPanel::CalculateDefaultHotArea(const Rosen::Rect &keyboard, cons hotArea.panelHotArea = { left, right, bottom }; } -void InputMethodPanel::RectifyAreas(const std::vector availableAreas, std::vector &areas) +void InputMethodPanel::RectifyAreas(const std::vector &availableAreas, std::vector &areas) { std::vector validAreas; for (const auto &availableArea : availableAreas) { @@ -999,7 +1007,7 @@ int32_t InputMethodPanel::UpdateRegion(std::vector region) } else { hotAreas.landscape.keyboardHotArea = region; } - CalculateHotAreas(enhancedLayoutParams_, keyboardLayoutParams_, adjustInfo, hotAreas); + CalculateHotAreas(GetEnhancedLayoutParams(), GetKeyboardLayoutParams(), adjustInfo, hotAreas); auto wmsHotAreas = ConvertToWMSHotArea(hotAreas); WMError result = window_->SetKeyboardTouchHotAreas(wmsHotAreas); if (result != WMError::WM_OK) { @@ -1049,74 +1057,72 @@ int32_t InputMethodPanel::InitAdjustInfo() return ErrorCode::NO_ERROR; } -int32_t InputMethodPanel::ParsePanelRect(const PanelFlag panelFlag, const LayoutParams &layoutParams) +int32_t InputMethodPanel::ParseParams(PanelFlag panelFlag, const LayoutParams &input, KeyboardLayoutParams &output) { - keyboardLayoutParams_.landscapeAvoidHeight_ = DEFAULT_AVOID_HEIGHT; - keyboardLayoutParams_.portraitAvoidHeight_ = DEFAULT_AVOID_HEIGHT; - std::vector configs; - auto isSuccess = SysCfgParser::ParsePanelAdjust(configs); - if (isSuccess) { - InitAdjustInfo(); - } else { - IMSA_HILOGE("there is no configuration file!"); - auto ret = CalculateNoConfigRect(panelFlag, layoutParams); - if (ret != ErrorCode::NO_ERROR) { - IMSA_HILOGE("failed to calculate NoConfigRect, err: %{public}d!", ret); - return ret; - } - return ErrorCode::NO_ERROR; - } - std::tuple, std::vector> keys = GetScreenStatus(panelFlag); - auto ret = GetSysPanelAdjust(panelFlag, keys, layoutParams); + // 1 - check parameters + DisplaySize displaySize; + int32_t ret = GetDisplaySize(displaySize); if (ret != ErrorCode::NO_ERROR) { - IMSA_HILOGE("GetSysPanelAdjust failed!"); - return ErrorCode::ERROR_BAD_PARAMETERS; + IMSA_HILOGE("failed to GetDisplaySize ret: %{public}d", ret); + return ErrorCode::ERROR_WINDOW_MANAGER; + } + if (!IsRectValid(panelFlag, input.portraitRect, displaySize.portrait)) { + IMSA_HILOGE("invalid portrait rect!"); + return ErrorCode::ERROR_PARAMETER_CHECK_FAILED; } + if (!IsRectValid(panelFlag, input.landscapeRect, displaySize.landscape)) { + IMSA_HILOGE("invalid landscape rect!"); + return ErrorCode::ERROR_PARAMETER_CHECK_FAILED; + } + + // 2 - calculate parameters + FullPanelAdjustInfo adjustInfo; + if (IsNeedConfig()) { + ret = GetAdjustInfo(panelFlag, adjustInfo); + IMSA_HILOGD("get adjust info: %{public}d", ret); + } + EnhancedLayoutParams tempOutput; + ParseParam(panelFlag, adjustInfo.portrait, displaySize.portrait, input.portraitRect, tempOutput.portrait); + ParseParam(panelFlag, adjustInfo.landscape, displaySize.landscape, input.landscapeRect, tempOutput.landscape); + output = ConvertToWMSParam(panelFlag, tempOutput); + output.portraitAvoidHeight_ = DEFAULT_AVOID_HEIGHT; + output.landscapeAvoidHeight_ = DEFAULT_AVOID_HEIGHT; + IMSA_HILOGD("success, portrait: %{public}s, landscape: %{public}s", tempOutput.portrait.ToString().c_str(), + tempOutput.landscape.ToString().c_str()); return ErrorCode::NO_ERROR; } -int32_t InputMethodPanel::CalculateNoConfigRect(const PanelFlag panelFlag, const LayoutParams &layoutParams) +void InputMethodPanel::ParseParam(PanelFlag panelFlag, const PanelAdjustInfo &adjustInfo, + const WindowSize &displaySize, const Rosen::Rect &inputRect, EnhancedLayoutParam &outputParam) { + Rosen::Rect panelRect{}; + Rosen::Rect keyboardRect{}; if (panelFlag == PanelFlag::FLG_FIXED) { - keyboardLayoutParams_.gravity_ = WindowGravity::WINDOW_GRAVITY_BOTTOM; - WindowSize portraitDisplaySize; - if (!GetDisplaySize(true, portraitDisplaySize)) { - IMSA_HILOGE("GetPortraitDisplaySize failed!"); - return ErrorCode::ERROR_WINDOW_MANAGER; - } - keyboardLayoutParams_.PortraitPanelRect_.width_ = portraitDisplaySize.width; - keyboardLayoutParams_.PortraitPanelRect_.height_ = layoutParams.portraitRect.height_; - keyboardLayoutParams_.PortraitPanelRect_.posY_ = static_cast(SafeSubtract( - portraitDisplaySize.height, keyboardLayoutParams_.PortraitPanelRect_.height_)); - keyboardLayoutParams_.PortraitPanelRect_.posX_ = NUMBER_ZERO; - // fixed Portraitkeyboard - keyboardLayoutParams_.PortraitKeyboardRect_.width_ = keyboardLayoutParams_.PortraitPanelRect_.width_; - keyboardLayoutParams_.PortraitKeyboardRect_.height_ = keyboardLayoutParams_.PortraitPanelRect_.height_; - keyboardLayoutParams_.PortraitKeyboardRect_.posY_ = keyboardLayoutParams_.PortraitPanelRect_.posY_; - keyboardLayoutParams_.PortraitKeyboardRect_.posX_ = keyboardLayoutParams_.PortraitPanelRect_.posX_; - WindowSize landscapeDisplaySize; - if (!GetDisplaySize(false, landscapeDisplaySize)) { - IMSA_HILOGE("GetLandscapeDisplaySize failed!"); - return ErrorCode::ERROR_WINDOW_MANAGER; - } - keyboardLayoutParams_.LandscapePanelRect_.width_ = landscapeDisplaySize.width; - keyboardLayoutParams_.LandscapePanelRect_.height_ = layoutParams.landscapeRect.height_; - keyboardLayoutParams_.LandscapePanelRect_.posY_ = static_cast(SafeSubtract( - landscapeDisplaySize.height, keyboardLayoutParams_.LandscapePanelRect_.height_)); - keyboardLayoutParams_.LandscapePanelRect_.posX_ = NUMBER_ZERO; - // Landscapekeyboard - keyboardLayoutParams_.LandscapeKeyboardRect_.width_ = keyboardLayoutParams_.LandscapePanelRect_.width_; - keyboardLayoutParams_.LandscapeKeyboardRect_.height_ = keyboardLayoutParams_.LandscapePanelRect_.height_; - keyboardLayoutParams_.LandscapeKeyboardRect_.posY_ = keyboardLayoutParams_.LandscapePanelRect_.posY_; - keyboardLayoutParams_.LandscapeKeyboardRect_.posX_ = keyboardLayoutParams_.LandscapePanelRect_.posX_; + // fixed panel rect + panelRect.width_ = displaySize.width; + panelRect.height_ = inputRect.height_ + static_cast(adjustInfo.top + adjustInfo.bottom); + panelRect.height_ = std::min(panelRect.height_, + static_cast(static_cast(displaySize.height) * FIXED_SOFT_KEYBOARD_PANEL_RATIO)); + panelRect.posX_ = ORIGIN_POS_X; + panelRect.posY_ = static_cast(SafeSubtract(displaySize.height, panelRect.height_)); + // fixed keyboard rect + keyboardRect.width_ = + SafeSubtract(panelRect.width_, static_cast(adjustInfo.left + adjustInfo.right)); + keyboardRect.height_ = + SafeSubtract(panelRect.height_, static_cast(adjustInfo.top + adjustInfo.bottom)); + keyboardRect.posY_ = panelRect.posY_ + static_cast(adjustInfo.top); + keyboardRect.posX_ = panelRect.posX_ + static_cast(adjustInfo.left); } else { - keyboardLayoutParams_.gravity_ = WindowGravity::WINDOW_GRAVITY_FLOAT; - keyboardLayoutParams_.LandscapeKeyboardRect_ = layoutParams.landscapeRect; - keyboardLayoutParams_.PortraitKeyboardRect_ = layoutParams.portraitRect; - keyboardLayoutParams_.LandscapePanelRect_ = layoutParams.landscapeRect; - keyboardLayoutParams_.PortraitPanelRect_ = layoutParams.portraitRect; + // floating keyboard rect + keyboardRect = inputRect; + // floating panel rect + panelRect.width_ = keyboardRect.width_ + static_cast(adjustInfo.left + adjustInfo.right); + panelRect.height_ = keyboardRect.height_ + static_cast(adjustInfo.top + adjustInfo.bottom); + panelRect.posY_ = keyboardRect.posY_ - adjustInfo.top; + panelRect.posX_ = keyboardRect.posX_ - adjustInfo.left; } - return ErrorCode::NO_ERROR; + outputParam.rect = keyboardRect; + outputParam.panelRect = panelRect; } std::tuple, std::vector> InputMethodPanel::GetScreenStatus( @@ -1126,10 +1132,8 @@ std::tuple, std::vector> InputMethodPanel: std::string foldStatus = "default"; if (panelFlag == PanelFlag::FLG_FIXED) { flag = "fix"; - keyboardLayoutParams_.gravity_ = WindowGravity::WINDOW_GRAVITY_BOTTOM; } else if (panelFlag == PanelFlag::FLG_FLOATING) { flag = "floating"; - keyboardLayoutParams_.gravity_ = WindowGravity::WINDOW_GRAVITY_FLOAT; } if (Rosen::DisplayManager::GetInstance().IsFoldable() && Rosen::DisplayManager::GetInstance().GetFoldStatus() != Rosen::FoldStatus::FOLDED) { @@ -1164,141 +1168,6 @@ int32_t InputMethodPanel::GetAdjustInfo(PanelFlag panelFlag, FullPanelAdjustInfo return ErrorCode::NO_ERROR; } -int32_t InputMethodPanel::GetSysPanelAdjust(const PanelFlag panelFlag, - std::tuple, std::vector> &keys, const LayoutParams &layoutParams) -{ - PanelAdjustInfo lanIterValue; - PanelAdjustInfo porIterValue; - std::lock_guard lock(panelAdjustLock_); - auto lanPanel = std::get<0>(keys); - auto porPanel = std::get<1>(keys); - auto lanIter = panelAdjust_.find(lanPanel); - auto porIter = panelAdjust_.find(porPanel); - if (lanIter != panelAdjust_.end()) { - lanIterValue = lanIter->second; - } - if (porIter != panelAdjust_.end()) { - porIterValue = porIter->second; - } - IMSA_HILOGI("GetSysPanelAdjust, portrait: %{public}s, landscape: %{public}s", - porIterValue.ToString().c_str(), lanIterValue.ToString().c_str()); - return CalculatePanelRect(panelFlag, lanIterValue, porIterValue, layoutParams); -} - -int32_t InputMethodPanel::CalculatePanelRect(const PanelFlag panelFlag, PanelAdjustInfo &lanIterValue, - PanelAdjustInfo &porIterValue, const LayoutParams &layoutParams) -{ - if (!IsNeedConfig()) { - IMSA_HILOGI("The security keyboard is handled according to no configuration file"); - return CalculateNoConfigRect(panelFlag, layoutParams); - } - auto defaultDisplay = GetCurDisplay(); - if (defaultDisplay == nullptr) { - IMSA_HILOGE("GetDefaultDisplay failed!"); - return ErrorCode::ERROR_EX_SERVICE_SPECIFIC; - } - IMSA_HILOGI("defaultDisplay height: %{public}d, width %{public}d", - defaultDisplay->GetHeight(), defaultDisplay->GetWidth()); - if (panelFlag == PanelFlag::FLG_FIXED) { - // fixed PortraitPanel - WindowSize portraitDisplaySize; - if (!GetDisplaySize(true, portraitDisplaySize)) { - IMSA_HILOGE("GetDisplaySize failed!"); - return ErrorCode::ERROR_WINDOW_MANAGER; - } - keyboardLayoutParams_.PortraitPanelRect_.width_ = portraitDisplaySize.width; - keyboardLayoutParams_.PortraitPanelRect_.height_ = layoutParams.portraitRect.height_ + - static_cast(porIterValue.top + porIterValue.bottom); - if (keyboardLayoutParams_.PortraitPanelRect_.height_ > - portraitDisplaySize.height * FIXED_SOFT_KEYBOARD_PANEL_RATIO) { - keyboardLayoutParams_.PortraitPanelRect_.height_ = - portraitDisplaySize.height * FIXED_SOFT_KEYBOARD_PANEL_RATIO; - } - keyboardLayoutParams_.PortraitPanelRect_.posY_ = - static_cast(portraitDisplaySize.height - keyboardLayoutParams_.PortraitPanelRect_.height_); - keyboardLayoutParams_.PortraitPanelRect_.posX_ = NUMBER_ZERO; - // fixed Portraitkeyboard - keyboardLayoutParams_.PortraitKeyboardRect_.width_ = keyboardLayoutParams_.PortraitPanelRect_.width_ - - static_cast(porIterValue.left + porIterValue.right); - keyboardLayoutParams_.PortraitKeyboardRect_.height_ = keyboardLayoutParams_.PortraitPanelRect_.height_ - - static_cast(porIterValue.top + porIterValue.bottom); - keyboardLayoutParams_.PortraitKeyboardRect_.posY_ = - keyboardLayoutParams_.PortraitPanelRect_.posY_ + static_cast(porIterValue.top); - keyboardLayoutParams_.PortraitKeyboardRect_.posX_ = - keyboardLayoutParams_.PortraitPanelRect_.posX_ + static_cast(porIterValue.left); - return CalculateLandscapeRect(defaultDisplay, layoutParams, lanIterValue); - } - return CalculateFloatRect(layoutParams, lanIterValue, porIterValue); -} - -int32_t InputMethodPanel::CalculateFloatRect( - const LayoutParams &layoutParams, PanelAdjustInfo &lanIterValue, PanelAdjustInfo &porIterValue) -{ - keyboardLayoutParams_.gravity_ = WindowGravity::WINDOW_GRAVITY_FLOAT; - // portrait floating keyboard - keyboardLayoutParams_.PortraitKeyboardRect_.width_ = layoutParams.portraitRect.width_; - keyboardLayoutParams_.PortraitKeyboardRect_.height_ = layoutParams.portraitRect.height_; - keyboardLayoutParams_.PortraitKeyboardRect_.posY_ = layoutParams.portraitRect.posY_; - keyboardLayoutParams_.PortraitKeyboardRect_.posX_ = layoutParams.portraitRect.posX_; - // portrait floating panel - keyboardLayoutParams_.PortraitPanelRect_.width_ = keyboardLayoutParams_.PortraitKeyboardRect_.width_ + - static_cast(porIterValue.left + porIterValue.right); - keyboardLayoutParams_.PortraitPanelRect_.height_ = keyboardLayoutParams_.PortraitKeyboardRect_.height_ + - static_cast(porIterValue.top + porIterValue.bottom); - keyboardLayoutParams_.PortraitPanelRect_.posY_ = - keyboardLayoutParams_.PortraitKeyboardRect_.posY_ - static_cast(porIterValue.top); - keyboardLayoutParams_.PortraitPanelRect_.posX_ = - keyboardLayoutParams_.PortraitKeyboardRect_.posX_ - static_cast(porIterValue.left); - - // landscape floating keyboard - keyboardLayoutParams_.LandscapeKeyboardRect_.width_ = layoutParams.landscapeRect.width_; - keyboardLayoutParams_.LandscapeKeyboardRect_.height_ = layoutParams.landscapeRect.height_; - keyboardLayoutParams_.LandscapeKeyboardRect_.posY_ = layoutParams.landscapeRect.posY_; - keyboardLayoutParams_.LandscapeKeyboardRect_.posX_ = layoutParams.landscapeRect.posX_; - // landscape floating panel - keyboardLayoutParams_.LandscapePanelRect_.width_ = keyboardLayoutParams_.LandscapeKeyboardRect_.width_ + - static_cast(lanIterValue.left + lanIterValue.right); - keyboardLayoutParams_.LandscapePanelRect_.height_ = keyboardLayoutParams_.LandscapeKeyboardRect_.height_ + - static_cast(lanIterValue.top + lanIterValue.bottom); - keyboardLayoutParams_.LandscapePanelRect_.posY_ = - keyboardLayoutParams_.LandscapeKeyboardRect_.posY_ - static_cast(lanIterValue.top); - keyboardLayoutParams_.LandscapePanelRect_.posX_ = - keyboardLayoutParams_.LandscapeKeyboardRect_.posX_ - static_cast(lanIterValue.left); - return ErrorCode::NO_ERROR; -} - -int32_t InputMethodPanel::CalculateLandscapeRect(sptr &defaultDisplay, - const LayoutParams &layoutParams, PanelAdjustInfo &lanIterValue) -{ - // LandscapePanel - WindowSize landscapeDisplaySize; - if (!GetDisplaySize(false, landscapeDisplaySize)) { - IMSA_HILOGE("GetDisplaySize failed!"); - return ErrorCode::ERROR_WINDOW_MANAGER; - } - keyboardLayoutParams_.LandscapePanelRect_.width_ = landscapeDisplaySize.width; - keyboardLayoutParams_.LandscapePanelRect_.height_ = layoutParams.landscapeRect.height_ + - static_cast(lanIterValue.top + lanIterValue.bottom); - if (keyboardLayoutParams_.LandscapePanelRect_.height_ > - landscapeDisplaySize.height * FIXED_SOFT_KEYBOARD_PANEL_RATIO) { - keyboardLayoutParams_.LandscapePanelRect_.height_ = - landscapeDisplaySize.height * FIXED_SOFT_KEYBOARD_PANEL_RATIO; - } - keyboardLayoutParams_.LandscapePanelRect_.posY_ = - static_cast(landscapeDisplaySize.height - keyboardLayoutParams_.LandscapePanelRect_.height_); - keyboardLayoutParams_.LandscapePanelRect_.posX_ = NUMBER_ZERO; - // Landscapekeyboard - keyboardLayoutParams_.LandscapeKeyboardRect_.width_ = keyboardLayoutParams_.LandscapePanelRect_.width_ - - static_cast(lanIterValue.left + lanIterValue.right); - keyboardLayoutParams_.LandscapeKeyboardRect_.height_ = keyboardLayoutParams_.LandscapePanelRect_.height_ - - static_cast(lanIterValue.top + lanIterValue.bottom); - keyboardLayoutParams_.LandscapeKeyboardRect_.posY_ = - keyboardLayoutParams_.LandscapePanelRect_.posY_ + static_cast(lanIterValue.top); - keyboardLayoutParams_.LandscapeKeyboardRect_.posX_ = - keyboardLayoutParams_.LandscapePanelRect_.posX_ + static_cast(lanIterValue.left); - return ErrorCode::NO_ERROR; -} - int32_t InputMethodPanel::ChangePanelFlag(PanelFlag panelFlag) { if (window_ == nullptr) { @@ -1335,10 +1204,12 @@ int32_t InputMethodPanel::ChangePanelFlag(PanelFlag panelFlag) IMSA_HILOGI("flag: %{public}d, ret: %{public}d.", panelFlag, ret); return ret == WMError::WM_OK ? ErrorCode::NO_ERROR : ErrorCode::ERROR_OPERATE_PANEL; } - keyboardLayoutParams_.gravity_ = gravity; - auto ret = AdjustLayout(keyboardLayoutParams_); + auto layoutParams = GetKeyboardLayoutParams(); + layoutParams.gravity_ = gravity; + auto ret = AdjustLayout(layoutParams); if (ret == ErrorCode::NO_ERROR) { panelFlag_ = panelFlag; + SetKeyboardLayoutParams(layoutParams); } InputMethodAbility::GetInstance().NotifyPanelStatus(true, panelFlag); IMSA_HILOGI("flag: %{public}d, ret: %{public}d.", panelFlag, ret); @@ -1374,7 +1245,7 @@ int32_t InputMethodPanel::ShowPanel() } auto ret = WMError::WM_OK; { - KeyboardEffectOption option = ConvertToWmEffect(immersiveMode_, immersiveEffect_); + KeyboardEffectOption option = ConvertToWmEffect(GetImmersiveMode(), LoadImmersiveEffect()); InputMethodSyncTrace tracer("InputMethodPanel_ShowPanel"); ret = window_->ShowKeyboard(option); } @@ -1604,8 +1475,8 @@ bool InputMethodPanel::SetPanelStatusListener( bool InputMethodPanel::SetPanelSizeChangeListener(std::shared_ptr statusListener) { - if (panelType_ != PanelType::SOFT_KEYBOARD - || (panelFlag_ != PanelFlag::FLG_FIXED && panelFlag_ != PanelFlag::FLG_FLOATING)) { + if (panelType_ != PanelType::SOFT_KEYBOARD || + (panelFlag_ != PanelFlag::FLG_FIXED && panelFlag_ != PanelFlag::FLG_FLOATING)) { return true; } if (panelStatusListener_ == nullptr && statusListener != nullptr) { @@ -1755,10 +1626,10 @@ int32_t InputMethodPanel::GetKeyboardArea(PanelFlag panelFlag, const WindowSize } if (isPortrait) { keyboardArea = adjustInfo.portrait; - keyboardArea.top = enhancedLayoutParams_.portrait.avoidY; + keyboardArea.top = GetEnhancedLayoutParams().portrait.avoidY; } else { keyboardArea = adjustInfo.landscape; - keyboardArea.top = enhancedLayoutParams_.landscape.avoidY; + keyboardArea.top = GetEnhancedLayoutParams().landscape.avoidY; } return ErrorCode::NO_ERROR; } @@ -1806,11 +1677,12 @@ void InputMethodPanel::RegisterKeyboardPanelInfoChangeListener() void InputMethodPanel::OnPanelHeightChange(const Rosen::KeyboardPanelInfo &keyboardPanelInfo) { - if (panelHeightCallback_ == nullptr) { + auto heightChangeHandler = GetPanelHeightCallback(); + if (heightChangeHandler == nullptr) { return; } if (!isInEnhancedAdjust_.load()) { - panelHeightCallback_(keyboardPanelInfo.rect_.height_, panelFlag_); + heightChangeHandler(keyboardPanelInfo.rect_.height_, panelFlag_); return; } bool isPortrait = false; @@ -1819,9 +1691,9 @@ void InputMethodPanel::OnPanelHeightChange(const Rosen::KeyboardPanelInfo &keybo return; } if (isPortrait) { - panelHeightCallback_(enhancedLayoutParams_.portrait.avoidHeight, panelFlag_); + heightChangeHandler(GetEnhancedLayoutParams().portrait.avoidHeight, panelFlag_); } else { - panelHeightCallback_(enhancedLayoutParams_.landscape.avoidHeight, panelFlag_); + heightChangeHandler(GetEnhancedLayoutParams().landscape.avoidHeight, panelFlag_); } } @@ -1845,24 +1717,6 @@ void InputMethodPanel::HandleKbPanelInfoChange(const KeyboardPanelInfo &keyboard PanelStatusChangeToImc(status, keyboardPanelInfo.rect_); } -bool InputMethodPanel::GetDisplaySize(bool isPortrait, WindowSize &size) -{ - auto defaultDisplay = GetCurDisplay(); - if (defaultDisplay == nullptr) { - IMSA_HILOGE("GetDefaultDisplay failed!"); - return false; - } - auto width = defaultDisplay->GetWidth(); - auto height = defaultDisplay->GetHeight(); - bool isDisplayPortrait = width < height; - if (isPortrait != isDisplayPortrait) { - size = { .width = height, .height = width }; - } else { - size = { .width = width, .height = height }; - } - return true; -} - bool InputMethodPanel::IsDisplayPortrait() { auto defaultDisplay = GetCurDisplay(); @@ -1887,18 +1741,8 @@ bool InputMethodPanel::IsDisplayPortrait() bool InputMethodPanel::IsDisplayUnfolded() { - return Rosen::DisplayManager::GetInstance().IsFoldable() - && Rosen::DisplayManager::GetInstance().GetFoldStatus() != Rosen::FoldStatus::FOLDED; -} - -bool InputMethodPanel::CheckSize(PanelFlag panelFlag, uint32_t width, uint32_t height, bool isDataPortrait) -{ - WindowSize displaySize; - if (!GetDisplaySize(isDataPortrait, displaySize)) { - IMSA_HILOGE("GetDisplaySize failed!"); - return false; - } - return IsSizeValid(panelFlag, width, height, displaySize.width, displaySize.height); + return Rosen::DisplayManager::GetInstance().IsFoldable() && + Rosen::DisplayManager::GetInstance().GetFoldStatus() != Rosen::FoldStatus::FOLDED; } bool InputMethodPanel::IsSizeValid( @@ -1922,9 +1766,16 @@ bool InputMethodPanel::IsSizeValid( void InputMethodPanel::SetPanelHeightCallback(CallbackFunc heightCallback) { + std::lock_guard lock(heightCallbackMutex_); panelHeightCallback_ = std::move(heightCallback); } +InputMethodPanel::CallbackFunc InputMethodPanel::GetPanelHeightCallback() +{ + std::lock_guard lock(heightCallbackMutex_); + return panelHeightCallback_; +} + int32_t InputMethodPanel::GetDensityDpi(float &densityDpi) { auto defaultDisplay = GetCurDisplay(); @@ -1966,29 +1817,31 @@ int32_t InputMethodPanel::GetDisplaySize(DisplaySize &size) void InputMethodPanel::SetImmersiveEffectToNone() { - if (immersiveEffect_.gradientHeight == 0) { - immersiveEffect_.gradientMode = GradientMode::NONE; - immersiveEffect_.fluidLightMode = FluidLightMode::NONE; + auto currentEffect = LoadImmersiveEffect(); + if (currentEffect.gradientHeight == 0) { + currentEffect.gradientMode = GradientMode::NONE; + currentEffect.fluidLightMode = FluidLightMode::NONE; + StoreImmersiveEffect(currentEffect); return; } // The SetImmersiveEffect API must be called after the AdjustPanelRect API is called. + auto layoutParams = GetKeyboardLayoutParams(); Rosen::KeyboardLayoutParams emptyParams; - if (keyboardLayoutParams_ == emptyParams) { + if (layoutParams == emptyParams) { IMSA_HILOGW("set gradientHeight to zero"); return; } - auto effectTmp = immersiveEffect_; - immersiveEffect_.gradientHeight = 0; - immersiveEffect_.gradientMode = GradientMode::NONE; - immersiveEffect_.fluidLightMode = FluidLightMode::NONE; - auto ret = AdjustLayout(keyboardLayoutParams_); + currentEffect.gradientHeight = 0; + currentEffect.gradientMode = GradientMode::NONE; + currentEffect.fluidLightMode = FluidLightMode::NONE; + auto ret = AdjustLayout(layoutParams, currentEffect); if (ret != ErrorCode::NO_ERROR) { IMSA_HILOGE("adjust failed, ret: %{public}d", ret); - immersiveEffect_ = effectTmp; return; } + StoreImmersiveEffect(currentEffect); UpdateImmersiveHotArea(); IMSA_HILOGW("set gradientHeight to zero and adjust layout success"); } @@ -2000,11 +1853,13 @@ int32_t InputMethodPanel::SetImmersiveMode(ImmersiveMode mode) IMSA_HILOGE("invalid mode: %{public}d", mode); return ErrorCode::ERROR_PARAMETER_CHECK_FAILED; } + + auto currentMode = GetImmersiveMode(); if (!IsShowing()) { - if (mode != immersiveMode_ && mode == ImmersiveMode::NONE_IMMERSIVE) { + if (mode != currentMode && mode == ImmersiveMode::NONE_IMMERSIVE) { SetImmersiveEffectToNone(); } - immersiveMode_ = mode; + StoreImmersiveMode(mode); IMSA_HILOGD("window is not show, mode: %{public}d", mode); return ErrorCode::NO_ERROR; } @@ -2014,21 +1869,21 @@ int32_t InputMethodPanel::SetImmersiveMode(ImmersiveMode mode) return ErrorCode::ERROR_IME; } - if (mode != immersiveMode_ && mode == ImmersiveMode::NONE_IMMERSIVE) { + if (mode != currentMode && mode == ImmersiveMode::NONE_IMMERSIVE) { SetImmersiveEffectToNone(); } - KeyboardEffectOption option = ConvertToWmEffect(mode, immersiveEffect_); + KeyboardEffectOption option = ConvertToWmEffect(mode, LoadImmersiveEffect()); // call window manager to set immersive mode auto ret = window_->ChangeKeyboardEffectOption(option); if (ret == WMError::WM_DO_NOTHING) { - IMSA_HILOGW("repeat set mode new:%{public}d, old:%{public}d", mode, immersiveMode_); + IMSA_HILOGW("repeat set mode new:%{public}d, old:%{public}d", mode, currentMode); return ErrorCode::NO_ERROR; } if (ret != WMError::WM_OK) { IMSA_HILOGE("ChangeKeyboardViewMode failed, ret: %{public}d", ret); return ErrorCode::ERROR_WINDOW_MANAGER; } - immersiveMode_ = mode; + StoreImmersiveMode(mode); IMSA_HILOGI("SetImmersiveMode success, mode: %{public}d", mode); return ErrorCode::NO_ERROR; } @@ -2061,7 +1916,7 @@ bool InputMethodPanel::IsValidGradientHeight(uint32_t gradientHeight) return true; } -int32_t InputMethodPanel::IsValidParam(const ImmersiveEffect &effect) +int32_t InputMethodPanel::IsValidParam(const ImmersiveEffect &effect, const KeyboardLayoutParams &layoutParams) { if (effect.gradientMode < GradientMode::NONE || effect.gradientMode >= GradientMode::END || effect.fluidLightMode < FluidLightMode::NONE || effect.fluidLightMode >= FluidLightMode::END) { @@ -2071,7 +1926,7 @@ int32_t InputMethodPanel::IsValidParam(const ImmersiveEffect &effect) } // The gradient mode and the fluid light mode can only be used when the immersive mode is enabled. - if (immersiveMode_ == ImmersiveMode::NONE_IMMERSIVE) { + if (GetImmersiveMode() == ImmersiveMode::NONE_IMMERSIVE) { if (effect.gradientMode != GradientMode::NONE || effect.fluidLightMode != FluidLightMode::NONE || effect.gradientHeight != 0) { IMSA_HILOGE("immersiveMode is NONE, but gradientMode=%{public}d, fluidLightMode=%{public}d", @@ -2105,7 +1960,7 @@ int32_t InputMethodPanel::IsValidParam(const ImmersiveEffect &effect) // The SetImmersiveEffect API must be called after the AdjustPanelRect API is called. Rosen::KeyboardLayoutParams emptyParams; - if (keyboardLayoutParams_ == emptyParams) { + if (layoutParams == emptyParams) { IMSA_HILOGE("adjust is not call, %{public}s", effect.ToString().c_str()); return ErrorCode::ERROR_IMA_PRECONDITION_REQUIRED; } @@ -2162,7 +2017,7 @@ void InputMethodPanel::UpdateImmersiveHotArea() return; } - CalculateHotAreas(enhancedLayoutParams_, keyboardLayoutParams_, adjustInfo, hotAreas); + CalculateHotAreas(GetEnhancedLayoutParams(), GetKeyboardLayoutParams(), adjustInfo, hotAreas); auto wmsHotAreas = ConvertToWMSHotArea(hotAreas); WMError result = window_->SetKeyboardTouchHotAreas(wmsHotAreas); if (result != WMError::WM_OK) { @@ -2180,60 +2035,67 @@ int32_t InputMethodPanel::SetImmersiveEffect(const ImmersiveEffect &effect) IMSA_HILOGE("immersive effect is not supported"); return ErrorCode::ERROR_DEVICE_UNSUPPORTED; } - - auto ret = IsValidParam(effect); + auto layoutParams = GetKeyboardLayoutParams(); + auto ret = IsValidParam(effect, layoutParams); if (ret != ErrorCode::NO_ERROR) { IMSA_HILOGE("invalid param, ret:%{public}d", ret); return ret; } // adjust again - ImmersiveEffect effectTmp = immersiveEffect_; - immersiveEffect_ = effect; - if (effectTmp.gradientMode != effect.gradientMode && effect.gradientMode == GradientMode::NONE) { - immersiveEffect_.gradientHeight = 0; - immersiveEffect_.gradientMode = GradientMode::NONE; - immersiveEffect_.fluidLightMode = FluidLightMode::NONE; + auto currentEffect = LoadImmersiveEffect(); + ImmersiveEffect targetEffect = effect; + if (currentEffect.gradientMode != effect.gradientMode && effect.gradientMode == GradientMode::NONE) { + targetEffect = + { .gradientHeight = 0, .gradientMode = GradientMode::NONE, .fluidLightMode = FluidLightMode::NONE }; } - ret = AdjustLayout(keyboardLayoutParams_); + ret = AdjustLayout(layoutParams, targetEffect); if (ret != ErrorCode::NO_ERROR) { - immersiveEffect_ = effectTmp; IMSA_HILOGE("AdjustLayout failed, ret:%{public}d", ret); return ret; } UpdateImmersiveHotArea(); if (!IsShowing()) { + StoreImmersiveEffect(targetEffect); IMSA_HILOGW("keyboard is not showing, do nothing"); return ErrorCode::NO_ERROR; } - if (window_ == nullptr) { IMSA_HILOGE("window_ is nullptr"); return ErrorCode::ERROR_NULL_POINTER; } - KeyboardEffectOption option = ConvertToWmEffect(immersiveMode_, immersiveEffect_); + auto mode = GetImmersiveMode(); + KeyboardEffectOption option = ConvertToWmEffect(mode, targetEffect); // call window manager to set immersive mode auto wmRet = window_->ChangeKeyboardEffectOption(option); if (wmRet == WMError::WM_DO_NOTHING) { - IMSA_HILOGW("repeat set mode new:, old:%{public}d", immersiveMode_); + IMSA_HILOGW("repeat set mode new:, old:%{public}d", mode); + StoreImmersiveEffect(targetEffect); return ErrorCode::NO_ERROR; } if (wmRet != WMError::WM_OK) { IMSA_HILOGE("ChangeKeyboardViewMode failed, wmRet: %{public}d", wmRet); - immersiveEffect_ = effectTmp; return ErrorCode::ERROR_WINDOW_MANAGER; } + StoreImmersiveEffect(targetEffect); IMSA_HILOGI("success, %{public}s", effect.ToString().c_str()); return ErrorCode::NO_ERROR; } ImmersiveMode InputMethodPanel::GetImmersiveMode() { - IMSA_HILOGD("GetImmersiveMode mode: %{public}d", immersiveMode_); + std::lock_guard lock(immersiveModeMutex_); + IMSA_HILOGD("mode: %{public}d", immersiveMode_); return immersiveMode_; } +void InputMethodPanel::StoreImmersiveMode(ImmersiveMode mode) +{ + std::lock_guard lock(immersiveModeMutex_); + immersiveMode_ = mode; +} + void InputMethodPanel::SetHotAreas(const HotAreas &hotAreas) { std::lock_guard lock(hotAreasLock_); @@ -2327,5 +2189,53 @@ int32_t InputMethodPanel::SetKeepScreenOn(bool isKeepScreenOn) IMSA_HILOGI("SetKeepScreenOn success"); return ErrorCode::NO_ERROR; } + +Rosen::KeyboardLayoutParams InputMethodPanel::GetKeyboardLayoutParams() +{ + std::lock_guard lock(keyboardLayoutParamsMutex_); + return keyboardLayoutParams_; +} + +void InputMethodPanel::SetKeyboardLayoutParams(Rosen::KeyboardLayoutParams params) +{ + std::lock_guard lock(keyboardLayoutParamsMutex_); + keyboardLayoutParams_ = std::move(params); +} + +EnhancedLayoutParams InputMethodPanel::GetEnhancedLayoutParams() +{ + std::lock_guard lock(enhancedLayoutParamMutex_); + return enhancedLayoutParams_; +} + +void InputMethodPanel::SetEnhancedLayoutParams(EnhancedLayoutParams params) +{ + std::lock_guard lock(enhancedLayoutParamMutex_); + enhancedLayoutParams_ = params; +} + +ImmersiveEffect InputMethodPanel::LoadImmersiveEffect() +{ + std::lock_guard lock(immersiveEffectMutex_); + return immersiveEffect_; +} + +void InputMethodPanel::StoreImmersiveEffect(ImmersiveEffect effect) +{ + std::lock_guard lock(immersiveEffectMutex_); + immersiveEffect_ = effect; +} + +ChangeY InputMethodPanel::GetChangeY() +{ + std::lock_guard lock(changeYMutex_); + return changeY_; +} + +void InputMethodPanel::SetChangeY(ChangeY changeY) +{ + std::lock_guard lock(changeYMutex_); + changeY_ = changeY; +} } // namespace MiscServices } // namespace OHOS \ No newline at end of file diff --git a/test/unittest/cpp_test/src/input_method_panel_adjust_test.cpp b/test/unittest/cpp_test/src/input_method_panel_adjust_test.cpp index 711e8d198bba45e5062d5c5cb573eea4d05e12d2..d11e95f8f8bca28d9c198c577643377170e55ade 100644 --- a/test/unittest/cpp_test/src/input_method_panel_adjust_test.cpp +++ b/test/unittest/cpp_test/src/input_method_panel_adjust_test.cpp @@ -257,8 +257,8 @@ HWTEST_F(InputMethodPanelAdjustTest, testAdjustEnhancedPanelRect_002, TestSize.L Rosen::Rect validRect = { 1, 1, 100, 100 }; EnhancedLayoutParams params = { .isFullScreen = false, - .portrait = { portraitRect, 50, 50 }, - .landscape = { validRect, 50, 50 }, + .portrait = { portraitRect, {}, 50, 50 }, + .landscape = { validRect, {}, 50, 50 }, }; HotAreas hotAreas; auto ret = inputMethodPanel->AdjustPanelRect(panelFlag, params, hotAreas); @@ -283,8 +283,8 @@ HWTEST_F(InputMethodPanelAdjustTest, testAdjustEnhancedPanelRect_003, TestSize.L Rosen::Rect validRect = { 1, 1, 100, 100 }; EnhancedLayoutParams params = { .isFullScreen = false, - .portrait = { validRect, 50, 50 }, - .landscape = { portraitRect, 50, 50 }, + .portrait = { validRect, {}, 50, 50 }, + .landscape = { portraitRect, {}, 50, 50 }, }; HotAreas hotAreas; auto ret = inputMethodPanel->AdjustPanelRect(panelFlag, params, hotAreas); @@ -311,8 +311,8 @@ HWTEST_F(InputMethodPanelAdjustTest, testAdjustEnhancedPanelRect_004, TestSize.L Rosen::Rect landscapeRect = { 0, 0, display.landscape.width - 1, display.landscape.height - 1 }; EnhancedLayoutParams params = { .isFullScreen = false, - .portrait = { portraitRect, 50, 50 }, - .landscape = { landscapeRect, 50, 50 }, + .portrait = { portraitRect, {}, 50, 50 }, + .landscape = { landscapeRect, {}, 50, 50 }, }; HotAreas hotAreas; auto ret = inputMethodPanel->AdjustPanelRect(panelFlag, params, hotAreas); @@ -339,8 +339,8 @@ HWTEST_F(InputMethodPanelAdjustTest, testAdjustEnhancedPanelRect_005, TestSize.L Rosen::Rect landscapeRect = { 0, 0, display.landscape.width - 1, display.landscape.height - 1 }; EnhancedLayoutParams params = { .isFullScreen = false, - .portrait = { portraitRect, 50, 50 }, - .landscape = { landscapeRect, 50, 50 }, + .portrait = { portraitRect, {}, 50, 50 }, + .landscape = { landscapeRect, {}, 50, 50 }, }; HotAreas hotAreas; auto ret = inputMethodPanel->AdjustPanelRect(panelFlag, params, hotAreas); @@ -367,8 +367,8 @@ HWTEST_F(InputMethodPanelAdjustTest, testAdjustEnhancedPanelRect_006, TestSize.L Rosen::Rect landscapeRect = { 0, 0, display.landscape.width + 1, display.landscape.height - 1 }; EnhancedLayoutParams params = { .isFullScreen = false, - .portrait = { portraitRect, 50, 50 }, - .landscape = { landscapeRect, 50, 50 }, + .portrait = { portraitRect, {}, 50, 50 }, + .landscape = { landscapeRect, {}, 50, 50 }, }; HotAreas hotAreas; auto ret = inputMethodPanel->AdjustPanelRect(panelFlag, params, hotAreas); @@ -395,8 +395,8 @@ HWTEST_F(InputMethodPanelAdjustTest, testAdjustEnhancedPanelRect_007, TestSize.L Rosen::Rect landscapeRect = { 0, 0, display.landscape.width - 1, display.landscape.height + 1 }; EnhancedLayoutParams params = { .isFullScreen = false, - .portrait = { portraitRect, 50, 50 }, - .landscape = { landscapeRect, 50, 50 }, + .portrait = { portraitRect, {}, 50, 50 }, + .landscape = { landscapeRect, {}, 50, 50 }, }; HotAreas hotAreas; auto ret = inputMethodPanel->AdjustPanelRect(panelFlag, params, hotAreas); @@ -423,8 +423,8 @@ HWTEST_F(InputMethodPanelAdjustTest, testAdjustEnhancedPanelRect_008, TestSize.L Rosen::Rect landscapeRect = { 0, 0, display.landscape.width - 1, display.landscape.height - 1 }; EnhancedLayoutParams params = { .isFullScreen = false, - .portrait = { portraitRect, -1, 0 }, - .landscape = { landscapeRect, 1, 0 }, + .portrait = { portraitRect, {}, -1, 0 }, + .landscape = { landscapeRect, {}, 1, 0 }, }; HotAreas hotAreas; auto ret = inputMethodPanel->AdjustPanelRect(panelFlag, params, hotAreas); @@ -451,8 +451,8 @@ HWTEST_F(InputMethodPanelAdjustTest, testAdjustEnhancedPanelRect_009, TestSize.L Rosen::Rect landscapeRect = { 0, 0, display.landscape.width - 1, display.landscape.height - 1 }; EnhancedLayoutParams params = { .isFullScreen = false, - .portrait = { portraitRect, static_cast(portraitRect.height_) + 1, 0 }, - .landscape = { landscapeRect, static_cast(landscapeRect.height_) - 1, 0 }, + .portrait = { portraitRect, {}, static_cast(portraitRect.height_) + 1, 0 }, + .landscape = { landscapeRect, {}, static_cast(landscapeRect.height_) - 1, 0 }, }; HotAreas hotAreas; auto ret = inputMethodPanel->AdjustPanelRect(panelFlag, params, hotAreas); @@ -479,8 +479,8 @@ HWTEST_F(InputMethodPanelAdjustTest, testAdjustEnhancedPanelRect_010, TestSize.L Rosen::Rect landscapeRect = { 0, 0, display.landscape.width - 1, display.landscape.height - 1 }; EnhancedLayoutParams params = { .isFullScreen = false, - .portrait = { portraitRect, 1, 0 }, - .landscape = { landscapeRect, -1, 0 }, + .portrait = { portraitRect, {}, 1, 0 }, + .landscape = { landscapeRect, {}, -1, 0 }, }; HotAreas hotAreas; auto ret = inputMethodPanel->AdjustPanelRect(panelFlag, params, hotAreas); @@ -507,8 +507,8 @@ HWTEST_F(InputMethodPanelAdjustTest, testAdjustEnhancedPanelRect_011, TestSize.L Rosen::Rect landscapeRect = { 0, 0, display.landscape.width - 1, display.landscape.height - 1 }; EnhancedLayoutParams params = { .isFullScreen = false, - .portrait = { portraitRect, static_cast(portraitRect.height_) - 1, 0 }, - .landscape = { landscapeRect, static_cast(landscapeRect.height_) + 1, 0 }, + .portrait = { portraitRect, {}, static_cast(portraitRect.height_) - 1, 0 }, + .landscape = { landscapeRect, {}, static_cast(landscapeRect.height_) + 1, 0 }, }; HotAreas hotAreas; auto ret = inputMethodPanel->AdjustPanelRect(panelFlag, params, hotAreas); @@ -537,8 +537,8 @@ HWTEST_F(InputMethodPanelAdjustTest, testAdjustEnhancedPanelRect_012, TestSize.L uint32_t landscapeAvoidY = display.landscape.height - landscapeAvoidHeight; EnhancedLayoutParams params = { .isFullScreen = true, - .portrait = { {}, static_cast(portraitAvoidY), 0 }, - .landscape = { {}, static_cast(landscapeAvoidY), 0 }, + .portrait = { {}, {}, static_cast(portraitAvoidY), 0 }, + .landscape = { {}, {}, static_cast(landscapeAvoidY), 0 }, }; HotAreas hotAreas; auto ret = inputMethodPanel->AdjustPanelRect(panelFlag, params, hotAreas); @@ -567,8 +567,8 @@ HWTEST_F(InputMethodPanelAdjustTest, testAdjustEnhancedPanelRect_013, TestSize.L uint32_t landscapeAvoidY = display.landscape.height - landscapeAvoidHeight; EnhancedLayoutParams params = { .isFullScreen = true, - .portrait = { {}, static_cast(portraitAvoidY), 0 }, - .landscape = { {}, static_cast(landscapeAvoidY), 0 }, + .portrait = { {}, {}, static_cast(portraitAvoidY), 0 }, + .landscape = { {}, {}, static_cast(landscapeAvoidY), 0 }, }; HotAreas hotAreas; auto ret = inputMethodPanel->AdjustPanelRect(panelFlag, params, hotAreas); @@ -599,8 +599,8 @@ HWTEST_F(InputMethodPanelAdjustTest, testAdjustEnhancedPanelRect_014, TestSize.L uint32_t landscapeAvoidY = landscapeRect.height_ - landscapeAvoidHeight; EnhancedLayoutParams params = { .isFullScreen = false, - .portrait = { portraitRect, static_cast(portraitAvoidY), 0 }, - .landscape = { landscapeRect, static_cast(landscapeAvoidY), 0 }, + .portrait = { portraitRect, {}, static_cast(portraitAvoidY), 0 }, + .landscape = { landscapeRect, {}, static_cast(landscapeAvoidY), 0 }, }; HotAreas hotAreas; auto ret = inputMethodPanel->AdjustPanelRect(panelFlag, params, hotAreas); @@ -631,8 +631,8 @@ HWTEST_F(InputMethodPanelAdjustTest, testAdjustEnhancedPanelRect_015, TestSize.L uint32_t landscapeAvoidY = landscapeRect.height_ - landscapeAvoidHeight; EnhancedLayoutParams params = { .isFullScreen = false, - .portrait = { portraitRect, static_cast(portraitAvoidY), 0 }, - .landscape = { landscapeRect, static_cast(landscapeAvoidY), 0 }, + .portrait = { portraitRect, {}, static_cast(portraitAvoidY), 0 }, + .landscape = { landscapeRect, {}, static_cast(landscapeAvoidY), 0 }, }; HotAreas hotAreas; auto ret = inputMethodPanel->AdjustPanelRect(panelFlag, params, hotAreas); @@ -663,8 +663,8 @@ HWTEST_F(InputMethodPanelAdjustTest, testAdjustEnhancedPanelRect_016, TestSize.L uint32_t landscapeAvoidY = display.landscape.height - landscapeAvoidHeight; EnhancedLayoutParams params = { .isFullScreen = true, - .portrait = { portraitRect, static_cast(portraitAvoidY), 0 }, - .landscape = { landscapeRect, static_cast(landscapeAvoidY), 0 }, + .portrait = { portraitRect, {}, static_cast(portraitAvoidY), 0 }, + .landscape = { landscapeRect, {}, static_cast(landscapeAvoidY), 0 }, }; HotAreas hotAreas; auto ret = inputMethodPanel->AdjustPanelRect(panelFlag, params, hotAreas); @@ -829,8 +829,8 @@ HWTEST_F(InputMethodPanelAdjustTest, testAdjustEnhancedPanelRect_020, TestSize.L uint32_t landscapeAvoidY = display.landscape.height - landscapeAvoidHeight; EnhancedLayoutParams params = { .isFullScreen = false, - .portrait = { portraitRect, static_cast(portraitAvoidY), 0 }, - .landscape = { landscapeRect, static_cast(landscapeAvoidY), 0 }, + .portrait = { portraitRect, {}, static_cast(portraitAvoidY), 0 }, + .landscape = { landscapeRect, {}, static_cast(landscapeAvoidY), 0 }, }; auto ret = inputMethodPanel->AdjustPanelRect(panelFlag, params, {}); EXPECT_EQ(ret, ErrorCode::NO_ERROR); @@ -886,7 +886,7 @@ HWTEST_F(InputMethodPanelAdjustTest, testSetImmersiveEffect_001, TestSize.Level0 /** * @tc.name: testSetImmersiveEffect_002 - * @tc.desc: Test SetImmersiveEffect invalid parameter. + * @tc.desc: Test SetImmersiveEffect invalid GradientMode or invalid FluidLightMode. * @tc.type: FUNC */ HWTEST_F(InputMethodPanelAdjustTest, testSetImmersiveEffect_002, TestSize.Level0) @@ -941,6 +941,11 @@ HWTEST_F(InputMethodPanelAdjustTest, testSetImmersiveEffect_003, TestSize.Level0 immersiveEffect = { .gradientMode = GradientMode::NONE, .fluidLightMode = FluidLightMode::BACKGROUND_FLUID_LIGHT }; ret = inputMethodPanel->SetImmersiveEffect(immersiveEffect); EXPECT_EQ(ret, ErrorCode::ERROR_IMA_INVALID_IMMERSIVE_EFFECT); + immersiveEffect = { .gradientHeight = 1, + .gradientMode = GradientMode::NONE, + .fluidLightMode = FluidLightMode::BACKGROUND_FLUID_LIGHT }; + ret = inputMethodPanel->SetImmersiveEffect(immersiveEffect); + EXPECT_EQ(ret, ErrorCode::ERROR_IMA_INVALID_IMMERSIVE_EFFECT); immersiveEffect = { .gradientMode = GradientMode::NONE, .fluidLightMode = FluidLightMode::NONE }; ret = inputMethodPanel->SetImmersiveEffect(immersiveEffect); EXPECT_EQ(ret, ErrorCode::NO_ERROR); @@ -974,18 +979,31 @@ HWTEST_F(InputMethodPanelAdjustTest, testSetImmersiveEffect_004, TestSize.Level0 .fluidLightMode = FluidLightMode::BACKGROUND_FLUID_LIGHT }; ret = inputMethodPanel->SetImmersiveEffect(immersiveEffect); EXPECT_EQ(ret, ErrorCode::ERROR_STATUS_SYSTEM_PERMISSION); + immersiveEffect = { .gradientMode = GradientMode::LINEAR_GRADIENT, + .fluidLightMode = FluidLightMode::NONE }; + ret = inputMethodPanel->SetImmersiveEffect(immersiveEffect); + EXPECT_NE(ret, ErrorCode::ERROR_STATUS_SYSTEM_PERMISSION); - // The blur height cannot be greater than the screen height. + // gradientHeight can not over INT32_MAX or display height IdentityCheckerMock::SetSystemApp(true); - WindowSize displaySize{ 0, 0 }; + DisplaySize displaySize; + ASSERT_EQ(InputMethodPanelAdjustTest::GetDisplaySize(displaySize), ErrorCode::NO_ERROR); bool isPortrait = inputMethodPanel->IsDisplayPortrait(); - EXPECT_TRUE(inputMethodPanel->GetDisplaySize(isPortrait, displaySize)); - immersiveEffect = { .gradientHeight = displaySize.height + 1, + uint32_t gradientHeight = static_cast(INT32_MAX) + 1; + ret = inputMethodPanel->SetImmersiveEffect(immersiveEffect); + EXPECT_EQ(ret, ErrorCode::ERROR_PARAMETER_CHECK_FAILED); + if (isPortrait) { + gradientHeight = displaySize.portrait.height + 1; + } else { + gradientHeight = displaySize.landscape.height + 1; + } + immersiveEffect = { .gradientHeight = gradientHeight, .gradientMode = GradientMode::LINEAR_GRADIENT, .fluidLightMode = FluidLightMode::BACKGROUND_FLUID_LIGHT }; ret = inputMethodPanel->SetImmersiveEffect(immersiveEffect); EXPECT_EQ(ret, ErrorCode::ERROR_PARAMETER_CHECK_FAILED); + // The SetImmersiveEffect API must be called after the AdjustPanelRect API is called. IdentityCheckerMock::SetSystemApp(true); immersiveEffect = { .gradientHeight = 0, .gradientMode = GradientMode::LINEAR_GRADIENT, @@ -1042,7 +1060,7 @@ HWTEST_F(InputMethodPanelAdjustTest, testSetImmersiveEffect_005, TestSize.Level0 /** * @tc.name: testSetImmersiveEffect_006 - * @tc.desc: Test SetImmersiveEffect, FullScreenPrepare check portrait parameter failed. + * @tc.desc: Test SetImmersiveEffect, invalid gradient height. * @tc.type: FUNC */ HWTEST_F(InputMethodPanelAdjustTest, testSetImmersiveEffect_006, TestSize.Level0) @@ -1061,22 +1079,27 @@ HWTEST_F(InputMethodPanelAdjustTest, testSetImmersiveEffect_006, TestSize.Level0 PanelFlag panelFlag = PanelFlag::FLG_FIXED; uint32_t portraitAvoidHeight = display.portrait.height * FIXED_SOFT_KEYBOARD_PANEL_RATIO; uint32_t portraitAvoidY = display.portrait.height - portraitAvoidHeight; - uint32_t landscapeAvoidHeight = display.landscape.height * FIXED_SOFT_KEYBOARD_PANEL_RATIO - 1; + uint32_t landscapeAvoidHeight = display.landscape.height * FIXED_SOFT_KEYBOARD_PANEL_RATIO; uint32_t landscapeAvoidY = display.landscape.height - landscapeAvoidHeight; EnhancedLayoutParams params = { .isFullScreen = true, - .portrait = { {}, static_cast(portraitAvoidY), 0 }, - .landscape = { {}, static_cast(landscapeAvoidY), 0 }, + .portrait = { {}, {}, static_cast(portraitAvoidY), 0 }, + .landscape = { {}, {}, static_cast(landscapeAvoidY), 0 }, }; HotAreas hotAreas; auto ret = inputMethodPanel->AdjustPanelRect(panelFlag, params, hotAreas); EXPECT_EQ(ret, ErrorCode::NO_ERROR); - WindowSize displaySize { 0, 0 }; - bool isPortrait = inputMethodPanel->IsDisplayPortrait(); - EXPECT_TRUE(inputMethodPanel->GetDisplaySize(isPortrait, displaySize)); - ImmersiveEffect immersiveEffect; - immersiveEffect = { .gradientHeight = displaySize.height * GRADIENT_HEIGHT_RATIO, + DisplaySize displaySize; + ASSERT_EQ(InputMethodPanelAdjustTest::GetDisplaySize(displaySize), ErrorCode::NO_ERROR); + uint32_t gradientHeight = displaySize.portrait.height * GRADIENT_HEIGHT_RATIO + 1; + ImmersiveEffect immersiveEffect = { .gradientHeight = gradientHeight, + .gradientMode = GradientMode::LINEAR_GRADIENT, + .fluidLightMode = FluidLightMode::BACKGROUND_FLUID_LIGHT }; + ret = inputMethodPanel->SetImmersiveEffect(immersiveEffect); + EXPECT_EQ(ret, ErrorCode::ERROR_PARAMETER_CHECK_FAILED); + gradientHeight = displaySize.landscape.height * GRADIENT_HEIGHT_RATIO + 1; + immersiveEffect = { .gradientHeight = gradientHeight, .gradientMode = GradientMode::LINEAR_GRADIENT, .fluidLightMode = FluidLightMode::BACKGROUND_FLUID_LIGHT }; ret = inputMethodPanel->SetImmersiveEffect(immersiveEffect); @@ -1086,7 +1109,7 @@ HWTEST_F(InputMethodPanelAdjustTest, testSetImmersiveEffect_006, TestSize.Level0 /** * @tc.name: testSetImmersiveEffect_007 - * @tc.desc: Test SetImmersiveEffect, FullScreenPrepare check landscape parameter failed. + * @tc.desc: Test SetImmersiveEffect, check invalid gradient height. * @tc.type: FUNC */ HWTEST_F(InputMethodPanelAdjustTest, testSetImmersiveEffect_007, TestSize.Level0) @@ -1103,24 +1126,29 @@ HWTEST_F(InputMethodPanelAdjustTest, testSetImmersiveEffect_007, TestSize.Level0 DisplaySize display; ASSERT_EQ(InputMethodPanelAdjustTest::GetDisplaySize(display), ErrorCode::NO_ERROR); PanelFlag panelFlag = PanelFlag::FLG_FIXED; - uint32_t portraitAvoidHeight = display.portrait.height * FIXED_SOFT_KEYBOARD_PANEL_RATIO - 1; + uint32_t portraitAvoidHeight = display.portrait.height * FIXED_SOFT_KEYBOARD_PANEL_RATIO; uint32_t portraitAvoidY = display.portrait.height - portraitAvoidHeight; uint32_t landscapeAvoidHeight = display.landscape.height * FIXED_SOFT_KEYBOARD_PANEL_RATIO; uint32_t landscapeAvoidY = display.landscape.height - landscapeAvoidHeight; EnhancedLayoutParams params = { .isFullScreen = true, - .portrait = { {}, static_cast(portraitAvoidY), 0 }, - .landscape = { {}, static_cast(landscapeAvoidY), 0 }, + .portrait = { {}, {}, static_cast(portraitAvoidY), 0 }, + .landscape = { {}, {}, static_cast(landscapeAvoidY), 0 }, }; HotAreas hotAreas; auto ret = inputMethodPanel->AdjustPanelRect(panelFlag, params, hotAreas); EXPECT_EQ(ret, ErrorCode::NO_ERROR); - WindowSize displaySize { 0, 0 }; + DisplaySize displaySize; + ASSERT_EQ(InputMethodPanelAdjustTest::GetDisplaySize(displaySize), ErrorCode::NO_ERROR); bool isPortrait = inputMethodPanel->IsDisplayPortrait(); - EXPECT_TRUE(inputMethodPanel->GetDisplaySize(isPortrait, displaySize)); - ImmersiveEffect immersiveEffect; - immersiveEffect = { .gradientHeight = displaySize.height * GRADIENT_HEIGHT_RATIO + 1, + uint32_t gradientHeight = 0; + if (isPortrait) { + gradientHeight = displaySize.portrait.height * GRADIENT_HEIGHT_RATIO + 1; + } else { + gradientHeight = displaySize.landscape.height * GRADIENT_HEIGHT_RATIO + 1; + } + ImmersiveEffect immersiveEffect = { .gradientHeight = gradientHeight, .gradientMode = GradientMode::LINEAR_GRADIENT, .fluidLightMode = FluidLightMode::BACKGROUND_FLUID_LIGHT }; ret = inputMethodPanel->SetImmersiveEffect(immersiveEffect); @@ -1189,8 +1217,8 @@ HWTEST_F(InputMethodPanelAdjustTest, testSetImmersiveEffect_009, TestSize.Level0 uint32_t landscapeAvoidY = display.landscape.height - landscapeAvoidHeight; EnhancedLayoutParams params = { .isFullScreen = true, - .portrait = { {}, static_cast(portraitAvoidY), 0 }, - .landscape = { {}, static_cast(landscapeAvoidY), 0 }, + .portrait = { {}, {}, static_cast(portraitAvoidY), 0 }, + .landscape = { {}, {}, static_cast(landscapeAvoidY), 0 }, }; HotAreas hotAreas; auto ret = inputMethodPanel->AdjustPanelRect(panelFlag, params, hotAreas); @@ -1224,11 +1252,16 @@ HWTEST_F(InputMethodPanelAdjustTest, testSetImmersiveEffect_010, TestSize.Level0 InputMethodPanelAdjustTest::ImaCreatePanel(panelInfo, inputMethodPanel); inputMethodPanel->immersiveMode_ = ImmersiveMode::IMMERSIVE; - WindowSize displaySize { 0, 0 }; + DisplaySize displaySize; + ASSERT_EQ(InputMethodPanelAdjustTest::GetDisplaySize(displaySize), ErrorCode::NO_ERROR); bool isPortrait = inputMethodPanel->IsDisplayPortrait(); - EXPECT_TRUE(inputMethodPanel->GetDisplaySize(isPortrait, displaySize)); - ImmersiveEffect immersiveEffect; - immersiveEffect = { .gradientHeight = displaySize.width * GRADIENT_HEIGHT_RATIO + 1, + uint32_t gradientHeight = 0; + if (isPortrait) { + gradientHeight = displaySize.portrait.width * GRADIENT_HEIGHT_RATIO + 1; + } else { + gradientHeight = displaySize.landscape.width * GRADIENT_HEIGHT_RATIO + 1; + } + ImmersiveEffect immersiveEffect = { .gradientHeight = gradientHeight, .gradientMode = GradientMode::LINEAR_GRADIENT, .fluidLightMode = FluidLightMode::BACKGROUND_FLUID_LIGHT }; auto ret = inputMethodPanel->SetImmersiveEffect(immersiveEffect); @@ -1260,5 +1293,100 @@ HWTEST_F(InputMethodPanelAdjustTest, testSetImmersiveEffect_011, TestSize.Level0 EXPECT_EQ(ret, ErrorCode::ERROR_PARAMETER_CHECK_FAILED); InputMethodPanelAdjustTest::ImaDestroyPanel(inputMethodPanel); } + +/** + * @tc.name: testAdjustPanelRect_001 + * @tc.desc: Test AdjustPanelRect window_ nullptr. + * @tc.type: FUNC + */ +HWTEST_F(InputMethodPanelAdjustTest, testAdjustPanelRect_001, TestSize.Level0) +{ + IMSA_HILOGI("InputMethodPanelAdjustTest testAdjustPanelRect_001 Test START"); + auto inputMethodPanel = std::make_shared(); + PanelFlag panelFlag = PanelFlag::FLG_FIXED; + LayoutParams params; + auto ret = inputMethodPanel->AdjustPanelRect(panelFlag, params); + EXPECT_EQ(ret, ErrorCode::ERROR_WINDOW_MANAGER); +} + +/** + * @tc.name: testAdjustPanelRect_002 + * @tc.desc: Test AdjustPanelRect ParseParams failed: invalid portrait rect. + * @tc.type: FUNC + */ +HWTEST_F(InputMethodPanelAdjustTest, testAdjustPanelRect_002, TestSize.Level0) +{ + IMSA_HILOGI("InputMethodPanelAdjustTest testAdjustPanelRect_002 Test START"); + auto inputMethodPanel = std::make_shared(); + PanelInfo panelInfo; + panelInfo.panelType = SOFT_KEYBOARD; + InputMethodPanelAdjustTest::ImaCreatePanel(panelInfo, inputMethodPanel); + + DisplaySize display; + ASSERT_EQ(InputMethodPanelAdjustTest::GetDisplaySize(display), ErrorCode::NO_ERROR); + PanelFlag panelFlag = PanelFlag::FLG_FIXED; + LayoutParams params; + // 1 - posX < 0 + params.portraitRect = { -1, 0, display.portrait.width, 0 }; + params.landscapeRect = { 0, 0, display.landscape.width, 0 }; + auto ret = inputMethodPanel->AdjustPanelRect(panelFlag, params); + EXPECT_EQ(ret, ErrorCode::ERROR_PARAMETER_CHECK_FAILED); + // 2 - posY < 0 + params.portraitRect = { 0, -1, display.portrait.width, 0 }; + params.landscapeRect = { 0, 0, display.landscape.width, 0 }; + ret = inputMethodPanel->AdjustPanelRect(panelFlag, params); + EXPECT_EQ(ret, ErrorCode::ERROR_PARAMETER_CHECK_FAILED); + // 3 - width > INT32_MAX + params.portraitRect = { 0, 0, static_cast(INT32_MAX) + 1, 0 }; + params.landscapeRect = { 0, 0, display.landscape.width, 0 }; + ret = inputMethodPanel->AdjustPanelRect(panelFlag, params); + EXPECT_EQ(ret, ErrorCode::ERROR_PARAMETER_CHECK_FAILED); + // 4 - height > INT32_MAX + params.portraitRect = { 0, 0, display.portrait.width, static_cast(INT32_MAX) + 1 }; + params.landscapeRect = { 0, 0, display.landscape.width, 0 }; + ret = inputMethodPanel->AdjustPanelRect(panelFlag, params); + EXPECT_EQ(ret, ErrorCode::ERROR_PARAMETER_CHECK_FAILED); + InputMethodPanelAdjustTest::ImaDestroyPanel(inputMethodPanel); +} + +/** + * @tc.name: testAdjustPanelRect_003 + * @tc.desc: Test AdjustPanelRect ParseParams failed: invalid landscape rect. + * @tc.type: FUNC + */ +HWTEST_F(InputMethodPanelAdjustTest, testAdjustPanelRect_003, TestSize.Level0) +{ + IMSA_HILOGI("InputMethodPanelAdjustTest testAdjustPanelRect_003 Test START"); + auto inputMethodPanel = std::make_shared(); + PanelInfo panelInfo; + panelInfo.panelType = SOFT_KEYBOARD; + InputMethodPanelAdjustTest::ImaCreatePanel(panelInfo, inputMethodPanel); + + DisplaySize display; + ASSERT_EQ(InputMethodPanelAdjustTest::GetDisplaySize(display), ErrorCode::NO_ERROR); + PanelFlag panelFlag = PanelFlag::FLG_FIXED; + LayoutParams params; + // 1 - posX < 0 + params.portraitRect = { 0, 0, display.portrait.width, 0 }; + params.landscapeRect = { -1, 0, display.landscape.width, 0 }; + auto ret = inputMethodPanel->AdjustPanelRect(panelFlag, params); + EXPECT_EQ(ret, ErrorCode::ERROR_PARAMETER_CHECK_FAILED); + // 2 - posY < 0 + params.portraitRect = { 0, 0, display.portrait.width, 0 }; + params.landscapeRect = { 0, -1, display.landscape.width, 0 }; + ret = inputMethodPanel->AdjustPanelRect(panelFlag, params); + EXPECT_EQ(ret, ErrorCode::ERROR_PARAMETER_CHECK_FAILED); + // 3 - width > INT32_MAX + params.portraitRect = { 0, 0, display.portrait.width, 0 }; + params.landscapeRect = { 0, 0, static_cast(INT32_MAX) + 1, 0 }; + ret = inputMethodPanel->AdjustPanelRect(panelFlag, params); + EXPECT_EQ(ret, ErrorCode::ERROR_PARAMETER_CHECK_FAILED); + // 4 - height > INT32_MAX + params.portraitRect = { 0, 0, display.portrait.width, 0 }; + params.landscapeRect = { 0, 0, display.landscape.width, static_cast(INT32_MAX) + 1 }; + ret = inputMethodPanel->AdjustPanelRect(panelFlag, params); + EXPECT_EQ(ret, ErrorCode::ERROR_PARAMETER_CHECK_FAILED); + InputMethodPanelAdjustTest::ImaDestroyPanel(inputMethodPanel); +} } // namespace MiscServices } // namespace OHOS \ No newline at end of file diff --git a/test/unittest/cpp_test/src/input_method_panel_test.cpp b/test/unittest/cpp_test/src/input_method_panel_test.cpp index 2727465760f777bda5cbc9a5b9f3860ebe0c4506..5ec49b7a5183c633db7bb546fc2b78bc14ce7f60 100644 --- a/test/unittest/cpp_test/src/input_method_panel_test.cpp +++ b/test/unittest/cpp_test/src/input_method_panel_test.cpp @@ -102,6 +102,7 @@ public: static void TestHidePanel(const std::shared_ptr &panel); static void TestIsPanelShown(const PanelInfo &info, bool expectedResult); static void TriggerPanelStatusChangeToImc(const std::shared_ptr &panel, InputWindowStatus status); + static int32_t GetDisplaySize(DisplaySize &size); class PanelStatusListenerImpl : public PanelStatusListener { public: PanelStatusListenerImpl() @@ -464,6 +465,25 @@ void InputMethodPanelTest::TriggerPanelStatusChangeToImc( } } +int32_t InputMethodPanelTest::GetDisplaySize(DisplaySize &size) +{ + auto defaultDisplay = Rosen::DisplayManager::GetInstance().GetDefaultDisplay(); + if (defaultDisplay == nullptr) { + IMSA_HILOGE("GetDefaultDisplay failed!"); + return ErrorCode::ERROR_WINDOW_MANAGER; + } + auto width = defaultDisplay->GetWidth(); + auto height = defaultDisplay->GetHeight(); + if (width < height) { + size.portrait = { .width = width, .height = height }; + size.landscape = { .width = height, .height = width }; + } else { + size.portrait = { .width = height, .height = width }; + size.landscape = { .width = width, .height = height }; + } + return ErrorCode::NO_ERROR; +} + /** * @tc.name: testCreatePanel * @tc.desc: Test CreatePanel. @@ -1234,6 +1254,7 @@ HWTEST_F(InputMethodPanelTest, testKeyboardPanelInfoChangeListenerRegister_002, */ HWTEST_F(InputMethodPanelTest, testAdjustPanelRect_001, TestSize.Level0) { + IMSA_HILOGI("InputMethodPanelTest::testAdjustPanelRect_001 start."); InputMethodPanelTest::Attach(); auto inputMethodPanel = std::make_shared(); PanelInfo panelInfo; @@ -1258,6 +1279,7 @@ HWTEST_F(InputMethodPanelTest, testAdjustPanelRect_001, TestSize.Level0) */ HWTEST_F(InputMethodPanelTest, testAdjustPanelRect_002, TestSize.Level0) { + IMSA_HILOGI("InputMethodPanelTest::testAdjustPanelRect_002 start."); InputMethodPanelTest::Attach(); auto inputMethodPanel = std::make_shared(); PanelInfo panelInfo; @@ -1282,6 +1304,7 @@ HWTEST_F(InputMethodPanelTest, testAdjustPanelRect_002, TestSize.Level0) */ HWTEST_F(InputMethodPanelTest, testAdjustPanelRect_003, TestSize.Level0) { + IMSA_HILOGI("InputMethodPanelTest::testAdjustPanelRect_003 start."); InputMethodPanelTest::Attach(); auto inputMethodPanel = std::make_shared(); PanelInfo panelInfo; @@ -1306,6 +1329,7 @@ HWTEST_F(InputMethodPanelTest, testAdjustPanelRect_003, TestSize.Level0) */ HWTEST_F(InputMethodPanelTest, testAdjustPanelRect_004, TestSize.Level0) { + IMSA_HILOGI("InputMethodPanelTest::testAdjustPanelRect_004 start."); InputMethodPanelTest::Attach(); auto inputMethodPanel = std::make_shared(); PanelInfo panelInfo; @@ -1330,6 +1354,7 @@ HWTEST_F(InputMethodPanelTest, testAdjustPanelRect_004, TestSize.Level0) */ HWTEST_F(InputMethodPanelTest, testAdjustPanelRect_005, TestSize.Level0) { + IMSA_HILOGI("InputMethodPanelTest::testAdjustPanelRect_005 start."); InputMethodPanelTest::Attach(); auto inputMethodPanel = std::make_shared(); PanelInfo panelInfo; @@ -1354,20 +1379,19 @@ HWTEST_F(InputMethodPanelTest, testAdjustPanelRect_005, TestSize.Level0) */ HWTEST_F(InputMethodPanelTest, testAdjustPanelRect_006, TestSize.Level0) { + IMSA_HILOGI("InputMethodPanelTest::testAdjustPanelRect_006 start."); InputMethodPanelTest::Attach(); auto inputMethodPanel = std::make_shared(); PanelInfo panelInfo; panelInfo.panelType = SOFT_KEYBOARD; panelInfo.panelFlag = FLG_FIXED; InputMethodPanelTest::ImaCreatePanel(panelInfo, inputMethodPanel); - auto defaultDisplay = Rosen::DisplayManager::GetInstance().GetDefaultDisplay(); - ASSERT_TRUE(defaultDisplay != nullptr); - windowWidth_ = defaultDisplay->GetWidth(); - windowHeight_ = defaultDisplay->GetHeight(); + DisplaySize displaySize; + ASSERT_EQ(InputMethodPanelTest::GetDisplaySize(displaySize), ErrorCode::NO_ERROR); PanelFlag panelFlag = PanelFlag::FLG_FIXED; LayoutParams layoutParams; - layoutParams.landscapeRect = { 0, 0, windowHeight_, 0 }; - layoutParams.portraitRect = { 0, 0, windowWidth_, 0 }; + layoutParams.landscapeRect = { 0, 0, displaySize.landscape.width, 0 }; + layoutParams.portraitRect = { 0, 0, displaySize.portrait.width, 0 }; auto ret = inputMethodPanel->AdjustPanelRect(panelFlag, layoutParams); EXPECT_EQ(ret, ErrorCode::NO_ERROR); InputMethodPanelTest::ImaDestroyPanel(inputMethodPanel); @@ -1382,20 +1406,19 @@ HWTEST_F(InputMethodPanelTest, testAdjustPanelRect_006, TestSize.Level0) */ HWTEST_F(InputMethodPanelTest, testAdjustPanelRect_007, TestSize.Level0) { + IMSA_HILOGI("InputMethodPanelTest::testAdjustPanelRect_007 start."); InputMethodPanelTest::Attach(); auto inputMethodPanel = std::make_shared(); PanelInfo panelInfo; panelInfo.panelType = SOFT_KEYBOARD; panelInfo.panelFlag = FLG_FIXED; InputMethodPanelTest::ImaCreatePanel(panelInfo, inputMethodPanel); - auto defaultDisplay = Rosen::DisplayManager::GetInstance().GetDefaultDisplay(); - ASSERT_TRUE(defaultDisplay != nullptr); - windowWidth_ = defaultDisplay->GetWidth(); - windowHeight_ = defaultDisplay->GetHeight(); + DisplaySize displaySize; + ASSERT_EQ(InputMethodPanelTest::GetDisplaySize(displaySize), ErrorCode::NO_ERROR); PanelFlag panelFlag = PanelFlag::FLG_FIXED; LayoutParams layoutParams; - layoutParams.landscapeRect = { 0, 0, windowHeight_ + 1, 0 }; - layoutParams.portraitRect = { 0, 0, windowWidth_ + 1, 0 }; + layoutParams.landscapeRect = { 0, 0, displaySize.landscape.width + 1, 0 }; + layoutParams.portraitRect = { 0, 0, displaySize.portrait.width + 1, 0 }; auto ret = inputMethodPanel->AdjustPanelRect(panelFlag, layoutParams); EXPECT_EQ(ret, ErrorCode::ERROR_PARAMETER_CHECK_FAILED); InputMethodPanelTest::ImaDestroyPanel(inputMethodPanel); @@ -1410,20 +1433,21 @@ HWTEST_F(InputMethodPanelTest, testAdjustPanelRect_007, TestSize.Level0) */ HWTEST_F(InputMethodPanelTest, testAdjustPanelRect_008, TestSize.Level0) { + IMSA_HILOGI("InputMethodPanelTest::testAdjustPanelRect_008 start."); InputMethodPanelTest::Attach(); auto inputMethodPanel = std::make_shared(); PanelInfo panelInfo; panelInfo.panelType = SOFT_KEYBOARD; panelInfo.panelFlag = FLG_FIXED; InputMethodPanelTest::ImaCreatePanel(panelInfo, inputMethodPanel); - auto defaultDisplay = Rosen::DisplayManager::GetInstance().GetDefaultDisplay(); - ASSERT_TRUE(defaultDisplay != nullptr); - windowWidth_ = defaultDisplay->GetWidth(); - windowHeight_ = defaultDisplay->GetHeight(); + DisplaySize displaySize; + ASSERT_EQ(InputMethodPanelTest::GetDisplaySize(displaySize), ErrorCode::NO_ERROR); PanelFlag panelFlag = PanelFlag::FLG_FIXED; LayoutParams layoutParams; - layoutParams.landscapeRect = { 0, 0, windowHeight_, windowWidth_ * 0.7 + 1 }; - layoutParams.portraitRect = { 0, 0, windowWidth_, windowHeight_ * 0.7 + 1 }; + layoutParams.landscapeRect = { 0, 0, displaySize.landscape.width, + static_cast(static_cast(displaySize.landscape.height) * 0.7) + 1 }; + layoutParams.portraitRect = { 0, 0, displaySize.portrait.width, + static_cast(static_cast(displaySize.portrait.height) * 0.7) + 1 }; auto ret = inputMethodPanel->AdjustPanelRect(panelFlag, layoutParams); EXPECT_EQ(ret, ErrorCode::ERROR_PARAMETER_CHECK_FAILED); InputMethodPanelTest::ImaDestroyPanel(inputMethodPanel); @@ -1438,20 +1462,21 @@ HWTEST_F(InputMethodPanelTest, testAdjustPanelRect_008, TestSize.Level0) */ HWTEST_F(InputMethodPanelTest, testAdjustPanelRect_009, TestSize.Level0) { + IMSA_HILOGI("InputMethodPanelTest::testAdjustPanelRect_009 start."); InputMethodPanelTest::Attach(); auto inputMethodPanel = std::make_shared(); PanelInfo panelInfo; panelInfo.panelType = SOFT_KEYBOARD; panelInfo.panelFlag = FLG_FIXED; InputMethodPanelTest::ImaCreatePanel(panelInfo, inputMethodPanel); - auto defaultDisplay = Rosen::DisplayManager::GetInstance().GetDefaultDisplay(); - ASSERT_TRUE(defaultDisplay != nullptr); - windowWidth_ = defaultDisplay->GetWidth(); - windowHeight_ = defaultDisplay->GetHeight(); + DisplaySize displaySize; + ASSERT_EQ(InputMethodPanelTest::GetDisplaySize(displaySize), ErrorCode::NO_ERROR); PanelFlag panelFlag = PanelFlag::FLG_FIXED; LayoutParams layoutParams; - layoutParams.landscapeRect = { 0, 0, windowHeight_, windowWidth_ * 0.7 }; - layoutParams.portraitRect = { 0, 0, windowWidth_, windowHeight_ * 0.7 }; + layoutParams.landscapeRect = { 0, 0, displaySize.landscape.width, + static_cast(static_cast(displaySize.landscape.height) * 0.7) }; + layoutParams.portraitRect = { 0, 0, displaySize.portrait.width, + static_cast(static_cast(displaySize.portrait.height) * 0.7) }; auto ret = inputMethodPanel->AdjustPanelRect(panelFlag, layoutParams); EXPECT_EQ(ret, ErrorCode::NO_ERROR); InputMethodPanelTest::ImaDestroyPanel(inputMethodPanel); @@ -1466,6 +1491,7 @@ HWTEST_F(InputMethodPanelTest, testAdjustPanelRect_009, TestSize.Level0) */ HWTEST_F(InputMethodPanelTest, testAdjustPanelRect_010, TestSize.Level0) { + IMSA_HILOGI("InputMethodPanelTest::testAdjustPanelRect_010 start."); InputMethodPanelTest::Attach(); auto inputMethodPanel = std::make_shared(); PanelInfo panelInfo; @@ -1490,6 +1516,7 @@ HWTEST_F(InputMethodPanelTest, testAdjustPanelRect_010, TestSize.Level0) */ HWTEST_F(InputMethodPanelTest, testAdjustPanelRect_011, TestSize.Level0) { + IMSA_HILOGI("InputMethodPanelTest::testAdjustPanelRect_011 start."); InputMethodPanelTest::Attach(); auto inputMethodPanel = std::make_shared(); PanelInfo panelInfo; @@ -1514,6 +1541,7 @@ HWTEST_F(InputMethodPanelTest, testAdjustPanelRect_011, TestSize.Level0) */ HWTEST_F(InputMethodPanelTest, testAdjustPanelRect_012, TestSize.Level0) { + IMSA_HILOGI("InputMethodPanelTest::testAdjustPanelRect_012 start."); InputMethodPanelTest::Attach(); auto inputMethodPanel = std::make_shared(); PanelInfo panelInfo; @@ -1538,6 +1566,7 @@ HWTEST_F(InputMethodPanelTest, testAdjustPanelRect_012, TestSize.Level0) */ HWTEST_F(InputMethodPanelTest, testAdjustPanelRect_013, TestSize.Level0) { + IMSA_HILOGI("InputMethodPanelTest::testAdjustPanelRect_013 start."); InputMethodPanelTest::Attach(); auto inputMethodPanel = std::make_shared(); PanelInfo panelInfo; @@ -1562,20 +1591,19 @@ HWTEST_F(InputMethodPanelTest, testAdjustPanelRect_013, TestSize.Level0) */ HWTEST_F(InputMethodPanelTest, testAdjustPanelRect_014, TestSize.Level0) { + IMSA_HILOGI("InputMethodPanelTest::testAdjustPanelRect_014 start."); InputMethodPanelTest::Attach(); auto inputMethodPanel = std::make_shared(); PanelInfo panelInfo; panelInfo.panelType = SOFT_KEYBOARD; panelInfo.panelFlag = FLG_FLOATING; InputMethodPanelTest::ImaCreatePanel(panelInfo, inputMethodPanel); - auto defaultDisplay = Rosen::DisplayManager::GetInstance().GetDefaultDisplay(); - ASSERT_TRUE(defaultDisplay != nullptr); - windowWidth_ = defaultDisplay->GetWidth(); - windowHeight_ = defaultDisplay->GetHeight(); + DisplaySize displaySize; + ASSERT_EQ(InputMethodPanelTest::GetDisplaySize(displaySize), ErrorCode::NO_ERROR); PanelFlag panelFlag = PanelFlag::FLG_FLOATING; LayoutParams layoutParams; - layoutParams.landscapeRect = { 0, 0, windowWidth_, 0 }; - layoutParams.portraitRect = { 0, 0, windowWidth_, 0 }; + layoutParams.landscapeRect = { 0, 0, displaySize.landscape.width, 0 }; + layoutParams.portraitRect = { 0, 0, displaySize.portrait.width, 0 }; auto ret = inputMethodPanel->AdjustPanelRect(panelFlag, layoutParams); EXPECT_EQ(ret, ErrorCode::NO_ERROR); InputMethodPanelTest::ImaDestroyPanel(inputMethodPanel); @@ -1590,20 +1618,19 @@ HWTEST_F(InputMethodPanelTest, testAdjustPanelRect_014, TestSize.Level0) */ HWTEST_F(InputMethodPanelTest, testAdjustPanelRect_015, TestSize.Level0) { + IMSA_HILOGI("InputMethodPanelTest::testAdjustPanelRect_015 start."); InputMethodPanelTest::Attach(); auto inputMethodPanel = std::make_shared(); PanelInfo panelInfo; panelInfo.panelType = SOFT_KEYBOARD; panelInfo.panelFlag = FLG_FLOATING; InputMethodPanelTest::ImaCreatePanel(panelInfo, inputMethodPanel); - auto defaultDisplay = Rosen::DisplayManager::GetInstance().GetDefaultDisplay(); - ASSERT_TRUE(defaultDisplay != nullptr); - windowWidth_ = defaultDisplay->GetWidth(); - windowHeight_ = defaultDisplay->GetHeight(); + DisplaySize displaySize; + ASSERT_EQ(InputMethodPanelTest::GetDisplaySize(displaySize), ErrorCode::NO_ERROR); PanelFlag panelFlag = PanelFlag::FLG_FLOATING; LayoutParams layoutParams; - layoutParams.landscapeRect = { 0, 0, windowHeight_ + 1, 0 }; - layoutParams.portraitRect = { 0, 0, windowWidth_ + 1, 0 }; + layoutParams.landscapeRect = { 0, 0, displaySize.landscape.width + 1, 0 }; + layoutParams.portraitRect = { 0, 0, displaySize.portrait.width + 1, 0 }; auto ret = inputMethodPanel->AdjustPanelRect(panelFlag, layoutParams); EXPECT_EQ(ret, ErrorCode::ERROR_PARAMETER_CHECK_FAILED); InputMethodPanelTest::ImaDestroyPanel(inputMethodPanel); @@ -1618,20 +1645,21 @@ HWTEST_F(InputMethodPanelTest, testAdjustPanelRect_015, TestSize.Level0) */ HWTEST_F(InputMethodPanelTest, testAdjustPanelRect_016, TestSize.Level0) { + IMSA_HILOGI("InputMethodPanelTest::testAdjustPanelRect_016 start."); InputMethodPanelTest::Attach(); auto inputMethodPanel = std::make_shared(); PanelInfo panelInfo; panelInfo.panelType = SOFT_KEYBOARD; panelInfo.panelFlag = FLG_FLOATING; InputMethodPanelTest::ImaCreatePanel(panelInfo, inputMethodPanel); - auto defaultDisplay = Rosen::DisplayManager::GetInstance().GetDefaultDisplay(); - ASSERT_TRUE(defaultDisplay != nullptr); - windowWidth_ = defaultDisplay->GetWidth(); - windowHeight_ = defaultDisplay->GetHeight(); + DisplaySize displaySize; + ASSERT_EQ(InputMethodPanelTest::GetDisplaySize(displaySize), ErrorCode::NO_ERROR); PanelFlag panelFlag = PanelFlag::FLG_FLOATING; LayoutParams layoutParams; - layoutParams.landscapeRect = { 0, 0, windowHeight_, windowWidth_ * 0.7 + 1 }; - layoutParams.portraitRect = { 0, 0, windowWidth_, windowHeight_ * 0.7 + 1 }; + layoutParams.landscapeRect = { 0, 0, displaySize.landscape.width, + static_cast(static_cast(displaySize.landscape.height) * 0.7) + 1 }; + layoutParams.portraitRect = { 0, 0, displaySize.portrait.width, + static_cast(static_cast(displaySize.portrait.height) * 0.7) + 1 }; auto ret = inputMethodPanel->AdjustPanelRect(panelFlag, layoutParams); EXPECT_EQ(ret, ErrorCode::NO_ERROR); InputMethodPanelTest::ImaDestroyPanel(inputMethodPanel); @@ -1646,20 +1674,18 @@ HWTEST_F(InputMethodPanelTest, testAdjustPanelRect_016, TestSize.Level0) */ HWTEST_F(InputMethodPanelTest, testAdjustPanelRect_017, TestSize.Level0) { + IMSA_HILOGI("InputMethodPanelTest::testAdjustPanelRect_017 start."); InputMethodPanelTest::Attach(); auto inputMethodPanel = std::make_shared(); PanelInfo panelInfo; panelInfo.panelType = SOFT_KEYBOARD; panelInfo.panelFlag = FLG_FLOATING; InputMethodPanelTest::ImaCreatePanel(panelInfo, inputMethodPanel); - auto defaultDisplay = Rosen::DisplayManager::GetInstance().GetDefaultDisplay(); - ASSERT_TRUE(defaultDisplay != nullptr); - windowWidth_ = defaultDisplay->GetWidth(); - windowHeight_ = defaultDisplay->GetHeight(); + DisplaySize displaySize; + ASSERT_EQ(InputMethodPanelTest::GetDisplaySize(displaySize), ErrorCode::NO_ERROR); PanelFlag panelFlag = PanelFlag::FLG_FLOATING; - LayoutParams layoutParams; - layoutParams.landscapeRect = { 0, 0, windowHeight_, windowWidth_ }; - layoutParams.portraitRect = { 0, 0, windowWidth_, windowHeight_ }; + LayoutParams layoutParams = { .landscapeRect = { 0, 0, displaySize.landscape.width, displaySize.landscape.height }, + .portraitRect = { 0, 0, displaySize.portrait.width, displaySize.portrait.height } }; auto ret = inputMethodPanel->AdjustPanelRect(panelFlag, layoutParams); EXPECT_EQ(ret, ErrorCode::NO_ERROR); InputMethodPanelTest::ImaDestroyPanel(inputMethodPanel); @@ -1674,20 +1700,19 @@ HWTEST_F(InputMethodPanelTest, testAdjustPanelRect_017, TestSize.Level0) */ HWTEST_F(InputMethodPanelTest, testAdjustPanelRect_018, TestSize.Level0) { + IMSA_HILOGI("InputMethodPanelTest::testAdjustPanelRect_018 start."); InputMethodPanelTest::Attach(); auto inputMethodPanel = std::make_shared(); PanelInfo panelInfo; panelInfo.panelType = SOFT_KEYBOARD; panelInfo.panelFlag = FLG_FLOATING; InputMethodPanelTest::ImaCreatePanel(panelInfo, inputMethodPanel); - auto defaultDisplay = Rosen::DisplayManager::GetInstance().GetDefaultDisplay(); - ASSERT_TRUE(defaultDisplay != nullptr); - windowWidth_ = defaultDisplay->GetWidth(); - windowHeight_ = defaultDisplay->GetHeight(); + DisplaySize displaySize; + ASSERT_EQ(InputMethodPanelTest::GetDisplaySize(displaySize), ErrorCode::NO_ERROR); PanelFlag panelFlag = PanelFlag::FLG_FLOATING; LayoutParams layoutParams; - layoutParams.landscapeRect = { 0, 0, windowHeight_, windowWidth_ + 1 }; - layoutParams.portraitRect = { 0, 0, windowWidth_, windowHeight_ + 1 }; + layoutParams.landscapeRect = { 0, 0, displaySize.landscape.width, displaySize.landscape.height + 1 }; + layoutParams.portraitRect = { 0, 0, displaySize.portrait.width, displaySize.portrait.height + 1 }; auto ret = inputMethodPanel->AdjustPanelRect(panelFlag, layoutParams); EXPECT_EQ(ret, ErrorCode::ERROR_PARAMETER_CHECK_FAILED); InputMethodPanelTest::ImaDestroyPanel(inputMethodPanel); @@ -1702,6 +1727,7 @@ HWTEST_F(InputMethodPanelTest, testAdjustPanelRect_018, TestSize.Level0) */ HWTEST_F(InputMethodPanelTest, testAdjustKeyboard_001, TestSize.Level0) { + IMSA_HILOGI("InputMethodPanelTest::testAdjustKeyboard_001 start."); InputMethodPanelTest::Attach(); auto inputMethodPanel = std::make_shared(); PanelInfo panelInfo; @@ -2148,7 +2174,6 @@ HWTEST_F(InputMethodPanelTest, testGetImmersiveMode, TestSize.Level0) EXPECT_EQ(ErrorCode::NO_ERROR, ret); } - /** * @tc.name: testParameterValidationInterface * @tc.desc: Test Parameter validation interface @@ -2212,8 +2237,8 @@ HWTEST_F(InputMethodPanelTest, testParameterValidationInterface, TestSize.Level0 ret = inputMethodPanel->DestroyPanel(); EXPECT_EQ(ErrorCode::ERROR_NULL_POINTER, ret); } - - /** + +/** * @tc.name: testMoveEnhancedPanelRect * @tc.desc: Test Move Enhanced Panel Rect * @tc.type: FUNC @@ -2334,22 +2359,22 @@ HWTEST_F(InputMethodPanelTest, testInvalidParams, TestSize.Level0) inputMethodPanel->immersiveEffect_.gradientHeight = GRADIENT_HEIGHT; // Configure gradient KeyboardLayoutParams param; param.portraitAvoidHeight_ = -1; // Set invalid negative value - auto ret = inputMethodPanel->FullScreenPrepare(param); + auto ret = inputMethodPanel->FullScreenPrepare(param, inputMethodPanel->immersiveEffect_); EXPECT_EQ(ret, ErrorCode::ERROR_INVALID_RANGE); param.portraitAvoidHeight_ = PORTRAIT_AVOID_HEIGHT; param.landscapeAvoidHeight_ = -1; // Set invalid negative value - ret = inputMethodPanel->FullScreenPrepare(param); + ret = inputMethodPanel->FullScreenPrepare(param, inputMethodPanel->immersiveEffect_); EXPECT_EQ(ret, ErrorCode::ERROR_INVALID_RANGE); param.landscapeAvoidHeight_ = LANDSCAPE_AVOID_HEIGHT; param.PortraitPanelRect_.posY_ = -1; // Set invalid negative value - ret = inputMethodPanel->FullScreenPrepare(param); + ret = inputMethodPanel->FullScreenPrepare(param, inputMethodPanel->immersiveEffect_); EXPECT_EQ(ret, ErrorCode::ERROR_INVALID_RANGE); param.PortraitPanelRect_.posY_ = VALID_POS_Y; param.LandscapePanelRect_.posY_ = -1; // Set invalid negative value - ret = inputMethodPanel->FullScreenPrepare(param); + ret = inputMethodPanel->FullScreenPrepare(param, inputMethodPanel->immersiveEffect_); EXPECT_EQ(ret, ErrorCode::ERROR_INVALID_RANGE); } @@ -2369,7 +2394,7 @@ HWTEST_F(InputMethodPanelTest, testPortraitAdjustmentNeeded, TestSize.Level0) param.PortraitPanelRect_.height_ = INITIAL_PORTRAIT_HEIGHT; // 25 < 20+10=30 → needs adjustment param.PortraitPanelRect_.posY_ = INITIAL_PORTRAIT_POS_Y; - auto ret = inputMethodPanel->FullScreenPrepare(param); + auto ret = inputMethodPanel->FullScreenPrepare(param, inputMethodPanel->immersiveEffect_); EXPECT_EQ(ret, ErrorCode::NO_ERROR); // Calculate expected values @@ -2377,7 +2402,7 @@ HWTEST_F(InputMethodPanelTest, testPortraitAdjustmentNeeded, TestSize.Level0) const uint32_t expectedChangeY = expectedHeight - INITIAL_PORTRAIT_HEIGHT; const int32_t expectedPosY = INITIAL_PORTRAIT_POS_Y - static_cast(expectedChangeY); - EXPECT_EQ(inputMethodPanel->portraitChangeY_, expectedChangeY); + EXPECT_EQ(inputMethodPanel->changeY_.portrait, expectedChangeY); EXPECT_EQ(param.PortraitPanelRect_.height_, expectedHeight); EXPECT_EQ(param.PortraitPanelRect_.posY_, expectedPosY); } @@ -2398,11 +2423,11 @@ HWTEST_F(InputMethodPanelTest, testLargegradientHeight, TestSize.Level0) param.PortraitPanelRect_.posY_ = INITIAL_PORTRAIT_POS_Y; inputMethodPanel->immersiveEffect_.gradientHeight = static_cast(INT32_MAX); - auto ret = inputMethodPanel->FullScreenPrepare(param); + auto ret = inputMethodPanel->FullScreenPrepare(param, inputMethodPanel->immersiveEffect_); EXPECT_EQ(ret, ErrorCode::ERROR_INVALID_RANGE); inputMethodPanel->immersiveEffect_.gradientHeight = static_cast(INT32_MAX - LANDSCAPE_AVOID_HEIGHT - 1); - ret = inputMethodPanel->FullScreenPrepare(param); + ret = inputMethodPanel->FullScreenPrepare(param, inputMethodPanel->immersiveEffect_); EXPECT_EQ(ret, ErrorCode::ERROR_INVALID_RANGE); } /** @@ -2420,7 +2445,7 @@ HWTEST_F(InputMethodPanelTest, testLandscapeAdjustmentNeeded, TestSize.Level0) param.LandscapePanelRect_.height_ = INITIAL_LANDSCAPE_HEIGHT; // 20 < 15+10=25 → needs adjustment param.LandscapePanelRect_.posY_ = INITIAL_LANDSCAPE_POS_Y; - auto ret = inputMethodPanel->FullScreenPrepare(param); + auto ret = inputMethodPanel->FullScreenPrepare(param, inputMethodPanel->immersiveEffect_); EXPECT_EQ(ret, ErrorCode::NO_ERROR); // Calculate expected values @@ -2428,7 +2453,7 @@ HWTEST_F(InputMethodPanelTest, testLandscapeAdjustmentNeeded, TestSize.Level0) const uint32_t expectedChangeY = expectedHeight - INITIAL_LANDSCAPE_HEIGHT; const int32_t expectedPosY = INITIAL_LANDSCAPE_POS_Y - static_cast(expectedChangeY); - EXPECT_EQ(inputMethodPanel->landscapeChangeY_, expectedChangeY); + EXPECT_EQ(inputMethodPanel->changeY_.landscape, expectedChangeY); EXPECT_EQ(param.LandscapePanelRect_.height_, expectedHeight); EXPECT_EQ(param.LandscapePanelRect_.posY_, expectedPosY); } @@ -2450,7 +2475,7 @@ HWTEST_F(InputMethodPanelTest, ShouldRejectNegativePortraitPosition, TestSize.Le param.LandscapePanelRect_ = { 0, VALID_POS_Y, LANDSCAPE_WIDTH, LANDSCAPE_HEIGHT }; // Verify error handling - ASSERT_EQ(panel->NormalImePrepare(param), ErrorCode::ERROR_INVALID_RANGE); + ASSERT_EQ(panel->NormalImePrepare(param, panel->immersiveEffect_), ErrorCode::ERROR_INVALID_RANGE); } /** @@ -2467,7 +2492,7 @@ HWTEST_F(InputMethodPanelTest, ShouldRejectNegativeLandscapePosition, TestSize.L param.PortraitPanelRect_ = { 0, VALID_POS_Y, DEFAULT_WIDTH, DEFAULT_HEIGHT }; param.LandscapePanelRect_ = { 0, INVALID_POS_Y, LANDSCAPE_WIDTH, LANDSCAPE_HEIGHT }; - ASSERT_EQ(panel->NormalImePrepare(param), ErrorCode::ERROR_INVALID_RANGE); + ASSERT_EQ(panel->NormalImePrepare(param, panel->immersiveEffect_), ErrorCode::ERROR_INVALID_RANGE); } /** @@ -2488,7 +2513,7 @@ HWTEST_F(InputMethodPanelTest, ShouldAdjustValidParametersCorrectly, TestSize.Le param.LandscapePanelRect_ = { 0, VALID_POS_Y, LANDSCAPE_WIDTH, originalLandscapeHeight }; // Execute operation - ASSERT_EQ(panel->NormalImePrepare(param), ErrorCode::NO_ERROR); + ASSERT_EQ(panel->NormalImePrepare(param, panel->immersiveEffect_), ErrorCode::NO_ERROR); // Verify height adjustments EXPECT_EQ(param.PortraitPanelRect_.height_, originalPortraitHeight + GRADIENT_HEIGHT); @@ -2499,8 +2524,8 @@ HWTEST_F(InputMethodPanelTest, ShouldAdjustValidParametersCorrectly, TestSize.Le EXPECT_EQ(param.LandscapePanelRect_.posY_, 0); // Verify member variables - EXPECT_EQ(panel->portraitChangeY_, GRADIENT_HEIGHT); - EXPECT_EQ(panel->landscapeChangeY_, GRADIENT_HEIGHT); + EXPECT_EQ(panel->changeY_.portrait, GRADIENT_HEIGHT); + EXPECT_EQ(panel->changeY_.landscape, GRADIENT_HEIGHT); } /** diff --git a/test/unittest/functional_test/inputmethod_private_command_capi_test.cpp b/test/unittest/functional_test/inputmethod_private_command_capi_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c708c21d4c933cd36b40511c9c7d2ea06cb0ddc0 --- /dev/null +++ b/test/unittest/functional_test/inputmethod_private_command_capi_test.cpp @@ -0,0 +1,433 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include "global.h" +#include "native_inputmethod_types.h" + +HWTEST_F(InputMethod_PrivateCommandTest, Create_ValidKey_ShouldCreateObject) +{ + char key[] = "testKey"; + InputMethod_PrivateCommand *command = OH_PrivateCommand_Create(key, sizeof(key) - 1); + EXPECT_NE(command, nullptr); + OH_PrivateCommand_Destroy(command); +} + +HWTEST_F(InputMethod_PrivateCommandTest, Create_EmptyKey_ShouldCreateObject) +{ + char key[] = ""; + InputMethod_PrivateCommand *command = OH_PrivateCommand_Create(key, 0); + EXPECT_NE(command, nullptr); + OH_PrivateCommand_Destroy(command); +} + +HWTEST_F(InputMethod_PrivateCommandTest, Destroy_ValidCommand_ShouldNotCrash) +{ + char key[] = "testKey"; + InputMethod_PrivateCommand *command = OH_PrivateCommand_Create(key, sizeof(key) - 1); + EXPECT_NE(command, nullptr); + OH_PrivateCommand_Destroy(command); +} + +HWTEST_F(InputMethod_PrivateCommandTest, Destroy_NullCommand_ShouldLogError) +{ + OH_PrivateCommand_Destroy(nullptr); +} + +HWTEST_F(InputMethod_PrivateCommandTest, SetKey_ValidCommandAndKey_ShouldSetKey) +{ + char key[] = "testKey"; + InputMethod_PrivateCommand *command = OH_PrivateCommand_Create(key, sizeof(key) - 1); + EXPECT_NE(command, nullptr); + + char newKey[] = "newKey"; + InputMethod_ErrorCode result = OH_PrivateCommand_SetKey(command, newKey, sizeof(newKey) - 1); + EXPECT_EQ(result, IME_ERR_OK); + + const char *actualKey; + size_t keyLength; + result = OH_PrivateCommand_GetKey(command, &actualKey, &keyLength); + EXPECT_EQ(result, IME_ERR_OK); + EXPECT_STREQ(actualKey, newKey); + EXPECT_EQ(keyLength, sizeof(newKey) - 1); + + OH_PrivateCommand_Destroy(command); +} + +HWTEST_F(InputMethod_PrivateCommandTest, SetKey_NullCommand_ShouldReturnError) +{ + char key[] = "testKey"; + InputMethod_ErrorCode result = OH_PrivateCommand_SetKey(nullptr, key, sizeof(key) - 1); + EXPECT_EQ(result, IME_ERR_NULL_POINTER); +} + +HWTEST_F(InputMethod_PrivateCommandTest, SetKey_NullKey_ShouldReturnError) +{ + char key[] = "testKey"; + InputMethod_PrivateCommand *command = OH_PrivateCommand_Create(key, sizeof(key) - 1); + EXPECT_NE(command, nullptr); + + InputMethod_ErrorCode result = OH_PrivateCommand_SetKey(command, nullptr, sizeof(key) - 1); + EXPECT_EQ(result, IME_ERR_NULL_POINTER); + + OH_PrivateCommand_Destroy(command); +} + +HWTEST_F(InputMethod_PrivateCommandTest, SetBoolValue_ValidCommand_ShouldSetValue) +{ + char key[] = "testKey"; + InputMethod_PrivateCommand *command = OH_PrivateCommand_Create(key, sizeof(key) - 1); + EXPECT_NE(command, nullptr); + + InputMethod_ErrorCode result = OH_PrivateCommand_SetBoolValue(command, true); + EXPECT_EQ(result, IME_ERR_OK); + + bool value; + result = OH_PrivateCommand_GetBoolValue(command, &value); + EXPECT_EQ(result, IME_ERR_OK); + EXPECT_TRUE(value); + + OH_PrivateCommand_Destroy(command); +} + +HWTEST_F(InputMethod_PrivateCommandTest, SetBoolValue_NullCommand_ShouldReturnError) +{ + InputMethod_ErrorCode result = OH_PrivateCommand_SetBoolValue(nullptr, true); + EXPECT_EQ(result, IME_ERR_NULL_POINTER); +} + +HWTEST_F(InputMethod_PrivateCommandTest, SetIntValue_ValidCommand_ShouldSetValue) +{ + char key[] = "testKey"; + InputMethod_PrivateCommand *command = OH_PrivateCommand_Create(key, sizeof(key) - 1); + EXPECT_NE(command, nullptr); + + InputMethod_ErrorCode result = OH_PrivateCommand_SetIntValue(command, 42); + EXPECT_EQ(result, IME_ERR_OK); + + int32_t value; + result = OH_PrivateCommand_GetIntValue(command, &value); + EXPECT_EQ(result, IME_ERR_OK); + EXPECT_EQ(value, 42); + + OH_PrivateCommand_Destroy(command); +} + +HWTEST_F(InputMethod_PrivateCommandTest, SetIntValue_NullCommand_ShouldReturnError) +{ + InputMethod_ErrorCode result = OH_PrivateCommand_SetIntValue(nullptr, 42); + EXPECT_EQ(result, IME_ERR_NULL_POINTER); +} + +HWTEST_F(InputMethod_PrivateCommandTest, SetStrValue_ValidCommandAndValue_ShouldSetValue) +{ + char key[] = "testKey"; + InputMethod_PrivateCommand *command = OH_PrivateCommand_Create(key, sizeof(key) - 1); + EXPECT_NE(command, nullptr); + + char value[] = "testValue"; + InputMethod_ErrorCode result = OH_PrivateCommand_SetStrValue(command, value, sizeof(value) - 1); + EXPECT_EQ(result, IME_ERR_OK); + + const char *actualValue; + size_t valueLength; + result = OH_PrivateCommand_GetStrValue(command, &actualValue, &valueLength); + EXPECT_EQ(result, IME_ERR_OK); + EXPECT_STREQ(actualValue, value); + EXPECT_EQ(valueLength, sizeof(value) - 1); + + OH_PrivateCommand_Destroy(command); +} + +HWTEST_F(InputMethod_PrivateCommandTest, SetStrValue_NullCommand_ShouldReturnError) +{ + char value[] = "testValue"; + InputMethod_ErrorCode result = OH_PrivateCommand_SetStrValue(nullptr, value, sizeof(value) - 1); + EXPECT_EQ(result, IME_ERR_NULL_POINTER); +} + +HWTEST_F(InputMethod_PrivateCommandTest, SetStrValue_NullValue_ShouldReturnError) +{ + char key[] = "testKey"; + InputMethod_PrivateCommand *command = OH_PrivateCommand_Create(key, sizeof(key) - 1); + EXPECT_NE(command, nullptr); + + InputMethod_ErrorCode result = OH_PrivateCommand_SetStrValue(command, nullptr, 0); + EXPECT_EQ(result, IME_ERR_NULL_POINTER); + + OH_PrivateCommand_Destroy(command); +} + +HWTEST_F(InputMethod_PrivateCommandTest, GetKey_ValidCommand_ShouldReturnKey) +{ + char key[] = "testKey"; + InputMethod_PrivateCommand *command = OH_PrivateCommand_Create(key, sizeof(key) - 1); + EXPECT_NE(command, nullptr); + + const char *actualKey; + size_t keyLength; + InputMethod_ErrorCode result = OH_PrivateCommand_GetKey(command, &actualKey, &keyLength); + EXPECT_EQ(result, IME_ERR_OK); + EXPECT_STREQ(actualKey, key); + EXPECT_EQ(keyLength, sizeof(key) - 1); + + OH_PrivateCommand_Destroy(command); +} + +HWTEST_F(InputMethod_PrivateCommandTest, GetKey_NullCommand_ShouldReturnError) +{ + const char *key; + size_t keyLength; + InputMethod_ErrorCode result = OH_PrivateCommand_GetKey(nullptr, &key, &keyLength); + EXPECT_EQ(result, IME_ERR_NULL_POINTER); +} + +HWTEST_F(InputMethod_PrivateCommandTest, GetKey_NullKey_ShouldReturnError) +{ + char key[] = "testKey"; + InputMethod_PrivateCommand *command = OH_PrivateCommand_Create(key, sizeof(key) - 1); + EXPECT_NE(command, nullptr); + + size_t keyLength; + InputMethod_ErrorCode result = OH_PrivateCommand_GetKey(command, nullptr, &keyLength); + EXPECT_EQ(result, IME_ERR_NULL_POINTER); + + OH_PrivateCommand_Destroy(command); +} + +HWTEST_F(InputMethod_PrivateCommandTest, GetKey_NullKeyLength_ShouldReturnError) +{ + char key[] = "testKey"; + InputMethod_PrivateCommand *command = OH_PrivateCommand_Create(key, sizeof(key) - 1); + EXPECT_NE(command, nullptr); + + const char *actualKey; + InputMethod_ErrorCode result = OH_PrivateCommand_GetKey(command, &actualKey, nullptr); + EXPECT_EQ(result, IME_ERR_NULL_POINTER); + + OH_PrivateCommand_Destroy(command); +} + +HWTEST_F(InputMethod_PrivateCommandTest, GetValueType_ValidCommand_ShouldReturnCorrectType) +{ + char key[] = "testKey"; + InputMethod_PrivateCommand *command = OH_PrivateCommand_Create(key, sizeof(key) - 1); + EXPECT_NE(command, nullptr); + + InputMethod_ErrorCode result = OH_PrivateCommand_SetBoolValue(command, true); + EXPECT_EQ(result, IME_ERR_OK); + + InputMethod_CommandValueType type; + result = OH_PrivateCommand_GetValueType(command, &type); + EXPECT_EQ(result, IME_ERR_OK); + EXPECT_EQ(type, IME_COMMAND_VALUE_TYPE_BOOL); + + OH_PrivateCommand_Destroy(command); +} + +HWTEST_F(InputMethod_PrivateCommandTest, GetValueType_NullCommand_ShouldReturnError) +{ + InputMethod_CommandValueType type; + InputMethod_ErrorCode result = OH_PrivateCommand_GetValueType(nullptr, &type); + EXPECT_EQ(result, IME_ERR_NULL_POINTER); +} + +HWTEST_F(InputMethod_PrivateCommandTest, GetValueType_NullType_ShouldReturnError) +{ + char key[] = "testKey"; + InputMethod_PrivateCommand *command = OH_PrivateCommand_Create(key, sizeof(key) - 1); + EXPECT_NE(command, nullptr); + + InputMethod_ErrorCode result = OH_PrivateCommand_GetValueType(command, nullptr); + EXPECT_EQ(result, IME_ERR_NULL_POINTER); + + OH_PrivateCommand_Destroy(command); +} + +HWTEST_F(InputMethod_PrivateCommandTest, GetBoolValue_ValidCommand_ShouldReturnBool) +{ + char key[] = "testKey"; + InputMethod_PrivateCommand *command = OH_PrivateCommand_Create(key, sizeof(key) - 1); + EXPECT_NE(command, nullptr); + + InputMethod_ErrorCode result = OH_PrivateCommand_SetBoolValue(command, true); + EXPECT_EQ(result, IME_ERR_OK); + + bool value; + result = OH_PrivateCommand_GetBoolValue(command, &value); + EXPECT_EQ(result, IME_ERR_OK); + EXPECT_TRUE(value); + + OH_PrivateCommand_Destroy(command); +} + +HWTEST_F(InputMethod_PrivateCommandTest, GetBoolValue_NullCommand_ShouldReturnError) +{ + bool value; + InputMethod_ErrorCode result = OH_PrivateCommand_GetBoolValue(nullptr, &value); + EXPECT_EQ(result, IME_ERR_NULL_POINTER); +} + +HWTEST_F(InputMethod_PrivateCommandTest, GetBoolValue_NullValue_ShouldReturnError) +{ + char key[] = "testKey"; + InputMethod_PrivateCommand *command = OH_PrivateCommand_Create(key, sizeof(key) - 1); + EXPECT_NE(command, nullptr); + + InputMethod_ErrorCode result = OH_PrivateCommand_GetBoolValue(command, nullptr); + EXPECT_EQ(result, IME_ERR_NULL_POINTER); + + OH_PrivateCommand_Destroy(command); +} + +HWTEST_F(InputMethod_PrivateCommandTest, GetBoolValue_NonBoolValue_ShouldReturnError) +{ + char key[] = "testKey"; + InputMethod_PrivateCommand *command = OH_PrivateCommand_Create(key, sizeof(key) - 1); + EXPECT_NE(command, nullptr); + + InputMethod_ErrorCode result = OH_PrivateCommand_SetIntValue(command, 42); + EXPECT_EQ(result, IME_ERR_OK); + + bool value; + result = OH_PrivateCommand_GetBoolValue(command, &value); + EXPECT_EQ(result, IME_ERR_QUERY_FAILED); + + OH_PrivateCommand_Destroy(command); +} + +HWTEST_F(InputMethod_PrivateCommandTest, GetIntValue_ValidCommand_ShouldReturnInt) +{ + char key[] = "testKey"; + InputMethod_PrivateCommand *command = OH_PrivateCommand_Create(key, sizeof(key) - 1); + EXPECT_NE(command, nullptr); + + InputMethod_ErrorCode result = OH_PrivateCommand_SetIntValue(command, 42); + EXPECT_EQ(result, IME_ERR_OK); + + int32_t value; + result = OH_PrivateCommand_GetIntValue(command, &value); + EXPECT_EQ(result, IME_ERR_OK); + EXPECT_EQ(value, 42); + + OH_PrivateCommand_Destroy(command); +} + +HWTEST_F(InputMethod_PrivateCommandTest, GetIntValue_NullCommand_ShouldReturnError) +{ + int32_t value; + InputMethod_ErrorCode result = OH_PrivateCommand_GetIntValue(nullptr, &value); + EXPECT_EQ(result, IME_ERR_NULL_POINTER); +} + +HWTEST_F(InputMethod_PrivateCommandTest, GetIntValue_NullValue_ShouldReturnError) +{ + char key[] = "testKey"; + InputMethod_PrivateCommand *command = OH_PrivateCommand_Create(key, sizeof(key) - 1); + EXPECT_NE(command, nullptr); + + InputMethod_ErrorCode result = OH_PrivateCommand_GetIntValue(command, nullptr); + EXPECT_EQ(result, IME_ERR_NULL_POINTER); + + OH_PrivateCommand_Destroy(command); +} + +HWTEST_F(InputMethod_PrivateCommandTest, GetIntValue_NonIntValue_ShouldReturnError) +{ + char key[] = "testKey"; + InputMethod_PrivateCommand *command = OH_PrivateCommand_Create(key, sizeof(key) - 1); + EXPECT_NE(command, nullptr); + + InputMethod_ErrorCode result = OH_PrivateCommand_SetBoolValue(command, true); + EXPECT_EQ(result, IME_ERR_OK); + + int32_t value; + result = OH_PrivateCommand_GetIntValue(command, &value); + EXPECT_EQ(result, IME_ERR_QUERY_FAILED); + + OH_PrivateCommand_Destroy(command); +} + +HWTEST_F(InputMethod_PrivateCommandTest, GetStrValue_ValidCommand_ShouldReturnString) +{ + char key[] = "testKey"; + InputMethod_PrivateCommand *command = OH_PrivateCommand_Create(key, sizeof(key) - 1); + EXPECT_NE(command, nullptr); + + char value[] = "testValue"; + InputMethod_ErrorCode result = OH_PrivateCommand_SetStrValue(command, value, sizeof(value) - 1); + EXPECT_EQ(result, IME_ERR_OK); + + const char *actualValue; + size_t valueLength; + result = OH_PrivateCommand_GetStrValue(command, &actualValue, &valueLength); + EXPECT_EQ(result, IME_ERR_OK); + EXPECT_STREQ(actualValue, value); + EXPECT_EQ(valueLength, sizeof(value) - 1); + + OH_PrivateCommand_Destroy(command); +} + +HWTEST_F(InputMethod_PrivateCommandTest, GetStrValue_NullCommand_ShouldReturnError) +{ + const char *value; + size_t valueLength; + InputMethod_ErrorCode result = OH_PrivateCommand_GetStrValue(nullptr, &value, &valueLength); + EXPECT_EQ(result, IME_ERR_NULL_POINTER); +} + +HWTEST_F(InputMethod_PrivateCommandTest, GetStrValue_NullValue_ShouldReturnError) +{ + char key[] = "testKey"; + InputMethod_PrivateCommand *command = OH_PrivateCommand_Create(key, sizeof(key) - 1); + EXPECT_NE(command, nullptr); + + size_t valueLength; + InputMethod_ErrorCode result = OH_PrivateCommand_GetStrValue(command, nullptr, &valueLength); + EXPECT_EQ(result, IME_ERR_NULL_POINTER); + + OH_PrivateCommand_Destroy(command); +} + +HWTEST_F(InputMethod_PrivateCommandTest, GetStrValue_NullValueLength_ShouldReturnError) +{ + char key[] = "testKey"; + InputMethod_PrivateCommand *command = OH_PrivateCommand_Create(key, sizeof(key) - 1); + EXPECT_NE(command, nullptr); + + const char *value; + InputMethod_ErrorCode result = OH_PrivateCommand_GetStrValue(command, &value, nullptr); + EXPECT_EQ(result, IME_ERR_NULL_POINTER); + + OH_PrivateCommand_Destroy(command); +} + +HWTEST_F(InputMethod_PrivateCommandTest, GetStrValue_NonStringValue_ShouldReturnError) +{ + char key[] = "testKey"; + InputMethod_PrivateCommand *command = OH_PrivateCommand_Create(key, sizeof(key) - 1); + EXPECT_NE(command, nullptr); + + InputMethod_ErrorCode result = OH_PrivateCommand_SetIntValue(command, 42); + EXPECT_EQ(result, IME_ERR_OK); + + const char *value; + size_t valueLength; + result = OH_PrivateCommand_GetStrValue(command, &value, &valueLength); + EXPECT_EQ(result, IME_ERR_QUERY_FAILED); + + OH_PrivateCommand_Destroy(command); +} \ No newline at end of file diff --git a/test/unittest/functional_test/peruser_session_test.cpp b/test/unittest/functional_test/peruser_session_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..10a4c7f03404aa3c1470385f1de82b98367c4242 --- /dev/null +++ b/test/unittest/functional_test/peruser_session_test.cpp @@ -0,0 +1,459 @@ +/* + * Copyright (C) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "peruser_session.h" + +#include + +#include + +#include "input_client_info.h" +#include "mock_input_client.h" +#include "mock_input_method_core.h" + +using namespace OHOS; +using namespace MiscServices; +using namespace testing; + +class PerUserSessionTest : public Test { +protected: + static void SetUpTestCase() + { + userId = 100; + eventHandler = std::make_shared(AppExecFwk::EventRunner::Create(true)); + perUserSession = std::make_unique(userId, eventHandler); + } + + static void TearDownTestCase() + { + // clear shared resources after all tests + } + + void SetUp() override + { + // prepare resource before each test + } + + void TearDown() override + { + // clear resource after each test + } + int userId; + std::shared_ptr eventHandler; + std::unique_ptr perUserSession; +}; +/** + * @tc.name: AddClientInfoTest + * @tc.desc: Verify that the PerUserSession::AddClientInfo method + * returns ErrorCode::NO_ERROR when adding client information + * @tc.type: FUNC + */ +TEST_F(PerUserSessionTest, AddClientInfoTest) +{ + auto mockClient = std::make_shared(); + InputClientInfo clientInfo; + clientInfo.client = mockClient; + clientInfo.config = { /* initialize configuration */ }; + clientInfo.uiExtensionTokenId = 123; + + EXPECT_EQ(perUserSession->AddClientInfo(mockClient, clientInfo, START_LISTENING), ErrorCode::NO_ERROR); +} + +/** + * @tc.name: RemoveClientInfoTest + * @tc.desc: Verify that the PerUserSession::RemoveClientInfo method works normally when removing client information + * @tc.type: FUNC + */ +TEST_F(PerUserSessionTest, RemoveClientInfoTest) +{ + auto mockClient = std::make_shared(); + InputClientInfo clientInfo; + clientInfo.client = mockClient; + clientInfo.config = { /* initialize configuration */ }; + clientInfo.uiExtensionTokenId = 123; + + perUserSession->AddClientInfo(mockClient, clientInfo, START_LISTENING); + perUserSession->RemoveClientInfo(mockClient, false); +} + +/** + * @tc.name: UpdateClientInfoTest + * @tc.desc: Verify that the PerUserSession::UpdateClientInfo method works normally when updating client information + * @tc.type: FUNC + */ +TEST_F(PerUserSessionTest, UpdateClientInfoTest) +{ + auto mockClient = std::make_shared(); + InputClientInfo clientInfo; + clientInfo.client = mockClient; + clientInfo.config = { /* initialize configuration */ }; + clientInfo.uiExtensionTokenId = 123; + + perUserSession->AddClientInfo(mockClient, clientInfo, START_LISTENING); + + std::unordered_map> updateInfos; + updateInfos[UpdateFlag::ISSHOWKEYBOARD] = true; + + perUserSession->UpdateClientInfo(mockClient, updateInfos); +} + +/** + * @tc.name: HideKeyboardTest + * @tc.desc: Verify that the PerUserSession::HideKeyboard method returns ErrorCode::NO_ERROR when hiding the keyboard + * @tc.type: FUNC + */ +TEST_F(PerUserSessionTest, HideKeyboardTest) +{ + auto mockClient = std::make_shared(); + InputClientInfo clientInfo; + clientInfo.client = mockClient; + clientInfo.config = { /* initialize configuration */ }; + clientInfo.uiExtensionTokenId = 123; + clientInfo.bindImeType = ImeType::IME; + + perUserSession->AddClientInfo(mockClient, clientInfo, START_LISTENING); + + EXPECT_CALL(*mockClient, AsObject()).WillOnce(Return(mockClient->AsObject())); + EXPECT_EQ(perUserSession->HideKeyboard(mockClient), ErrorCode::NO_ERROR); +} + +/** + * @tc.name: ShowKeyboardTest + * @tc.desc: Verify that the PerUserSession::ShowKeyboard method returns ErrorCode::NO_ERROR when showing the keyboard + * @tc.type: FUNC + */ +TEST_F(PerUserSessionTest, ShowKeyboardTest) +{ + auto mockClient = std::make_shared(); + InputClientInfo clientInfo; + clientInfo.client = mockClient; + clientInfo.config = { /* initialize configuration */ }; + clientInfo.uiExtensionTokenId = 123; + clientInfo.bindImeType = ImeType::IME; + + perUserSession->AddClientInfo(mockClient, clientInfo, START_LISTENING); + + EXPECT_CALL(*mockClient, AsObject()).WillOnce(Return(mockClient->AsObject())); + EXPECT_EQ(perUserSession->ShowKeyboard(mockClient), ErrorCode::NO_ERROR); +} + +/** + * @tc.name: OnClientDiedTest + * @tc.desc: Verify that the PerUserSession::OnClientDied method works normally when the client dies + * @tc.type: FUNC + */ +TEST_F(PerUserSessionTest, OnClientDiedTest) +{ + auto mockClient = std::make_shared(); + InputClientInfo clientInfo; + clientInfo.client = mockClient; + clientInfo.config = { /* initialize configuration */ }; + clientInfo.uiExtensionTokenId = 123; + clientInfo.bindImeType = ImeType::IME; + + perUserSession->AddClientInfo(mockClient, clientInfo, START_LISTENING); + perUserSession->OnClientDied(mockClient); +} + +/** + * @tc.name: OnImeDiedTest + * @tc.desc: Verify that the PerUserSession::OnImeDied method works normally when the input method dies + * @tc.type: FUNC + */ +TEST_F(PerUserSessionTest, OnImeDiedTest) +{ + auto mockImeCore = std::make_shared(); + perUserSession->OnImeDied(mockImeCore, ImeType::IME); +} + +/** + * @tc.name: RemoveImeTest + * @tc.desc: Verify that the PerUserSession::RemoveIme method + * returns ErrorCode::ERROR_IME_NOT_STARTED when removing the input method + * @tc.type: FUNC + */ +TEST_F(PerUserSessionTest, RemoveImeTest) +{ + auto mockImeCore = std::make_shared(); + EXPECT_EQ(perUserSession->RemoveIme(mockImeCore, ImeType::IME), ErrorCode::ERROR_IME_NOT_STARTED); +} + +/** + * @tc.name: OnHideCurrentInputTest + * @tc.desc: Verify that the PerUserSession::OnHideCurrentInput method + * returns ErrorCode::NO_ERROR when hiding the current input + * @tc.type: FUNC + */ +TEST_F(PerUserSessionTest, OnHideCurrentInputTest) +{ + auto mockClient = std::make_shared(); + InputClientInfo clientInfo; + clientInfo.client = mockClient; + clientInfo.config = { /* initialize configuration */ }; + clientInfo.uiExtensionTokenId = 123; + clientInfo.bindImeType = ImeType::IME; + + perUserSession->AddClientInfo(mockClient, clientInfo, START_LISTENING); + perUserSession->SetCurrentClient(mockClient); + EXPECT_EQ(perUserSession->OnHideCurrentInput(), ErrorCode::NO_ERROR); +} + +/** + * @tc.name: OnShowCurrentInputTest + * @tc.desc: Verify that the PerUserSession::OnShowCurrentInput method + * returns ErrorCode::NO_ERROR when showing the current input + * @tc.type: FUNC + */ +TEST_F(PerUserSessionTest, OnShowCurrentInputTest) +{ + auto mockClient = std::make_shared(); + InputClientInfo clientInfo; + clientInfo.client = mockClient; + clientInfo.config = { /* initialize configuration */ }; + clientInfo.uiExtensionTokenId = 123; + clientInfo.bindImeType = ImeType::IME; + + perUserSession->AddClientInfo(mockClient, clientInfo, START_LISTENING); + perUserSession->SetCurrentClient(mockClient); + EXPECT_EQ(perUserSession->OnShowCurrentInput(), ErrorCode::NO_ERROR); +} + +/** + * @tc.name: OnHideInputTest + * @tc.desc: Verify that the PerUserSession::OnHideInput method returns ErrorCode::NO_ERROR when hiding the input + * @tc.type: FUNC + */ +TEST_F(PerUserSessionTest, OnHideInputTest) +{ + auto mockClient = std::make_shared(); + InputClientInfo clientInfo; + clientInfo.client = mockClient; + clientInfo.config = { /* initialize configuration */ }; + clientInfo.uiExtensionTokenId = 123; + clientInfo.bindImeType = ImeType::IME; + + perUserSession->AddClientInfo(mockClient, clientInfo, START_LISTENING); + perUserSession->SetCurrentClient(mockClient); + EXPECT_EQ(perUserSession->OnHideInput(mockClient), ErrorCode::NO_ERROR); +} + +/** + * @tc.name: OnShowInputTest + * @tc.desc: Verify that the PerUserSession::OnShowInput method returns ErrorCode::NO_ERROR when showing the input + * @tc.type: FUNC + */ +TEST_F(PerUserSessionTest, OnShowInputTest) +{ + auto mockClient = std::make_shared(); + InputClientInfo clientInfo; + clientInfo.client = mockClient; + clientInfo.config = { /* initialize configuration */ }; + clientInfo.uiExtensionTokenId = 123; + clientInfo.bindImeType = ImeType::IME; + + perUserSession->AddClientInfo(mockClient, clientInfo, START_LISTENING); + perUserSession->SetCurrentClient(mockClient); + EXPECT_EQ(perUserSession->OnShowInput(mockClient), ErrorCode::NO_ERROR); +} + +/** + * @tc.name: OnHideSoftKeyBoardSelfTest + * @tc.desc: Verify that the PerUserSession::OnHideSoftKeyBoardSelf method works normally + * when hiding the soft keyboard + * @tc.type: FUNC + */ +TEST_F(PerUserSessionTest, OnHideSoftKeyBoardSelfTest) +{ + auto mockClient = std::make_shared(); + InputClientInfo clientInfo; + clientInfo.client = mockClient; + clientInfo.config = { /* initialize configuration */ }; + clientInfo.uiExtensionTokenId = 123; + clientInfo.bindImeType = ImeType::IME; + + perUserSession->AddClientInfo(mockClient, clientInfo, START_LISTENING); + perUserSession->SetCurrentClient(mockClient); + perUserSession->OnHideSoftKeyBoardSelf(); +} + +/** + * @tc.name: OnRequestShowInputTest + * @tc.desc: Verify that the PerUserSession::OnRequestShowInput method + * returns ErrorCode::ERROR_IME_NOT_STARTED when requesting to show the input + * @tc.type: FUNC + */ +TEST_F(PerUserSessionTest, OnRequestShowInputTest) +{ + EXPECT_EQ(perUserSession->OnRequestShowInput(), ErrorCode::ERROR_IME_NOT_STARTED); +} + +/** + * @tc.name: OnRequestHideInputTest + * @tc.desc: Verify that the PerUserSession::OnRequestHideInput method + * returns ErrorCode::NO_ERROR when requesting to hide the input + * @tc.type: FUNC + */ +TEST_F(PerUserSessionTest, OnRequestHideInputTest) +{ + EXPECT_EQ(perUserSession->OnRequestHideInput(getpid()), ErrorCode::NO_ERROR); +} + +/** + * @tc.name: OnPrepareInputTest + * @tc.desc: Verify that the PerUserSession::OnPrepareInput method + * returns ErrorCode::NO_ERROR when preparing the input + * @tc.type: FUNC + */ +TEST_F(PerUserSessionTest, OnPrepareInputTest) +{ + auto mockClient = std::make_shared(); + InputClientInfo clientInfo; + clientInfo.client = mockClient; + clientInfo.config = { /* initialize configuration */ }; + clientInfo.uiExtensionTokenId = 123; + + EXPECT_EQ(perUserSession->OnPrepareInput(clientInfo), ErrorCode::NO_ERROR); +} + +/** + * @tc.name: OnReleaseInputTest + * @tc.desc: Verify that the PerUserSession::OnReleaseInput method returns ErrorCode::NO_ERROR when releasing the input + * @tc.type: FUNC + */ +TEST_F(PerUserSessionTest, OnReleaseInputTest) +{ + auto mockClient = std::make_shared(); + InputClientInfo clientInfo; + clientInfo.client = mockClient; + clientInfo.config = { /* initialize configuration */ }; + clientInfo.uiExtensionTokenId = 123; + + perUserSession->AddClientInfo(mockClient, clientInfo, START_LISTENING); + EXPECT_EQ(perUserSession->OnReleaseInput(mockClient), ErrorCode::NO_ERROR); +} + +/** + * @tc.name: RemoveClientTest + * @tc.desc: Verify that the PerUserSession::RemoveClient method returns ErrorCode::NO_ERROR when removing the client + * @tc.type: FUNC + */ +TEST_F(PerUserSessionTest, RemoveClientTest) +{ + auto mockClient = std::make_shared(); + InputClientInfo clientInfo; + clientInfo.client = mockClient; + clientInfo.config = { /* initialize configuration */ }; + clientInfo.uiExtensionTokenId = 123; + + perUserSession->AddClientInfo(mockClient, clientInfo, START_LISTENING); + EXPECT_EQ(perUserSession->RemoveClient(mockClient, true), ErrorCode::NO_ERROR); +} + +/** + * @tc.name: DeactivateClientTest + * @tc.desc: Verify that the PerUserSession::DeactivateClient method works normally when deactivating the client + * @tc.type: FUNC + */ +TEST_F(PerUserSessionTest, DeactivateClientTest) +{ + auto mockClient = std::make_shared(); + InputClientInfo clientInfo; + clientInfo.client = mockClient; + clientInfo.config = { /* initialize configuration */ }; + clientInfo.uiExtensionTokenId = 123; + + perUserSession->AddClientInfo(mockClient, clientInfo, START_LISTENING); + perUserSession->DeactivateClient(mockClient); +} + +/** + * @tc.name: IsProxyImeEnableTest + * @tc.desc: Verify that the PerUserSession::IsProxyImeEnable method + * returns false when checking if the proxy IME is enabled + * @tc.type: FUNC + */ +TEST_F(PerUserSessionTest, IsProxyImeEnableTest) +{ + EXPECT_FALSE(perUserSession->IsProxyImeEnable()); +} + +/** + * @tc.name: OnStartInputTest + * @tc.desc: Verify that the PerUserSession::OnStartInput method + * returns ErrorCode::ERROR_IME_NOT_STARTED when starting the input + * @tc.type: FUNC + */ +TEST_F(PerUserSessionTest, OnStartInputTest) +{ + auto mockClient = std::make_shared(); + InputClientInfo clientInfo; + clientInfo.client = mockClient; + clientInfo.config = { /* initialize configuration */ }; + clientInfo.uiExtensionTokenId = 123; + + sptr agent; + EXPECT_EQ(perUserSession->OnStartInput(clientInfo, agent), ErrorCode::ERROR_IME_NOT_STARTED); +} + +/** + * @tc.name: BindClientWithImeTest + * @tc.desc: Verify that the PerUserSession::BindClientWithIme method + * returns ErrorCode::ERROR_IME_NOT_STARTED when binding the client with the input method + * @tc.type: FUNC + */ +TEST_F(PerUserSessionTest, BindClientWithImeTest) +{ + auto mockClient = std::make_shared(); + InputClientInfo clientInfo; + clientInfo.client = mockClient; + clientInfo.config = { /* initialize configuration */ }; + clientInfo.uiExtensionTokenId = 123; + + EXPECT_EQ(perUserSession->BindClientWithIme(std::make_shared(clientInfo), ImeType::IME, true), + ErrorCode::ERROR_IME_NOT_STARTED); +} + +/** + * @tc.name: UnBindClientWithImeTest + * @tc.desc: Verify that the PerUserSession::UnBindClientWithIme method works normally + * when unbinding the client with the input method + * @tc.type: FUNC + */ +TEST_F(PerUserSessionTest, UnBindClientWithImeTest) +{ + auto mockClient = std::make_shared(); + InputClientInfo clientInfo; + clientInfo.client = mockClient; + clientInfo.config = { /* initialize configuration */ }; + clientInfo.uiExtensionTokenId = 123; + + perUserSession->UnBindClientWithIme(std::make_shared(clientInfo), true); +} + +/** + * @tc.name: StopClientInputTest + * @tc.desc: Verify that the PerUserSession::StopClientInput method works normally when stopping the client input + * @tc.type: FUNC + */ +TEST_F(PerUserSessionTest, StopClientInputTest) +{ + auto mockClient = std::make_shared(); + InputClientInfo clientInfo; + clientInfo.client = mockClient; + clientInfo.config = { /* initialize configuration */ }; + clientInfo.uiExtensionTokenId = 123; + + perUserSession->StopClientInput(std::make_shared(clientInfo)); +} \ No newline at end of file