diff --git a/frameworks/napi/power/power_module.cpp b/frameworks/napi/power/power_module.cpp index 09329e61d9038c22de4b800079f22866cdbc4d1e..5d824d73f6809baeb0eb1eae5d9f057866a0672b 100644 --- a/frameworks/napi/power/power_module.cpp +++ b/frameworks/napi/power/power_module.cpp @@ -15,6 +15,7 @@ #include "napi/native_api.h" #include "napi/native_common.h" +#include "power_state_machine_info.h" #include "napi/native_node_api.h" #include "power.h" @@ -67,6 +68,41 @@ static napi_value CreateDevicePowerMode(napi_env env, napi_value exports) return exports; } +static napi_value EnumPowerKeyFilteringStrategyClassConstructor(napi_env env, napi_callback_info info) +{ + napi_value thisArg = nullptr; + void* data = nullptr; + + napi_get_cb_info(env, info, nullptr, nullptr, &thisArg, &data); + + napi_value global = nullptr; + napi_get_global(env, &global); + + return thisArg; +} + +static napi_value CreatePowerKeyFilteringStrategy(napi_env env, napi_value exports) +{ + napi_value disableLongPressFiltering = nullptr; + napi_value longPressFilteringOnce = nullptr; + + napi_create_int32(env, (int32_t)PowerKeyFilteringStrategy::DISABLE_LONG_PRESS_FILTERING, + &disableLongPressFiltering); + napi_create_int32(env, (int32_t)PowerKeyFilteringStrategy::LONG_PRESS_FILTERING_ONCE, &longPressFilteringOnce); + + napi_property_descriptor desc[] = { + DECLARE_NAPI_STATIC_PROPERTY("DISABLE_LONG_PRESS_FILTERING", disableLongPressFiltering), + DECLARE_NAPI_STATIC_PROPERTY("LONG_PRESS_FILTERING_ONCE", longPressFilteringOnce), + }; + napi_value result = nullptr; + napi_define_class(env, "PowerKeyFilteringStrategy", NAPI_AUTO_LENGTH, EnumPowerKeyFilteringStrategyClassConstructor, + nullptr, sizeof(desc) / sizeof(*desc), desc, &result); + + napi_set_named_property(env, exports, "PowerKeyFilteringStrategy", result); + + return exports; +} + EXTERN_C_START /* * function for module exports @@ -93,9 +129,11 @@ static napi_value PowerInit(napi_env env, napi_value exports) DECLARE_NAPI_FUNCTION("getPowerMode", PowerNapi::GetPowerMode), DECLARE_NAPI_FUNCTION("isStandby", PowerNapi::IsStandby), DECLARE_NAPI_FUNCTION("setScreenOffTime", PowerNapi::SetScreenOffTime), - DECLARE_NAPI_FUNCTION("refreshActivity", PowerNapi::RefreshActivity)}; + DECLARE_NAPI_FUNCTION("refreshActivity", PowerNapi::RefreshActivity), + DECLARE_NAPI_FUNCTION("setPowerKeyFilteringStrategy", PowerNapi::SetPowerKeyFilteringStrategy)}; NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc)); CreateDevicePowerMode(env, exports); + CreatePowerKeyFilteringStrategy(env, exports); POWER_HILOGD(COMP_FWK, "The initialization of the Power module is complete"); return exports; diff --git a/frameworks/napi/power/power_napi.cpp b/frameworks/napi/power/power_napi.cpp index 801fb61ec9c7eebd41290772d7af1c6790972f15..e0c43ce8bbe7020f8ef457e33b91441b90b9e263 100644 --- a/frameworks/napi/power/power_napi.cpp +++ b/frameworks/napi/power/power_napi.cpp @@ -38,6 +38,7 @@ constexpr uint32_t SUSPEND_MAX_ARGC = 1; constexpr uint32_t SET_SCREEN_OFFTIME_ARGC = 1; constexpr uint32_t HIBERNATE_ARGC = 1; constexpr uint32_t REFRESH_ACTIVITY_ARGC = 1; +constexpr uint32_t POWERRKEY_FILTERING_STRATEGY_ARGC = 1; constexpr int32_t INDEX_0 = 0; constexpr int32_t INDEX_1 = 1; constexpr int32_t RESTORE_DEFAULT_SCREENOFF_TIME = -1; @@ -384,5 +385,35 @@ napi_value PowerNapi::RefreshActivity(napi_env env, napi_callback_info info) } return nullptr; } + +napi_value PowerNapi::SetPowerKeyFilteringStrategy(napi_env env, napi_callback_info info) +{ + size_t argc = POWERRKEY_FILTERING_STRATEGY_ARGC; + napi_value argv[argc]; + NapiUtils::GetCallbackInfo(env, info, argc, argv); + + NapiErrors error; + if (argc != POWERRKEY_FILTERING_STRATEGY_ARGC || !NapiUtils::CheckValueType(env, argv[INDEX_0], napi_number)) { + return error.ThrowError(env, PowerErrors::ERR_PARAM_INVALID); + } + + int32_t strategy = 0; + if (napi_ok != napi_get_value_int32(env, argv[INDEX_0], &strategy)) { + POWER_HILOGE(FEATURE_INPUT, "napi get int32 value failed."); + return error.ThrowError(env, PowerErrors::ERR_PARAM_INVALID); + } + + if (strategy < 0 || strategy >= static_cast(PowerKeyFilteringStrategy::STRATEGY_MAX)) { + POWER_HILOGE(FEATURE_INPUT, "strategy is not right."); + return error.ThrowError(env, PowerErrors::ERR_PARAM_INVALID); + } + + PowerErrors code = g_powerMgrClient.SetPowerKeyFilteringStrategy(static_cast(strategy)); + if (code != PowerErrors::ERR_OK) { + POWER_HILOGE(FEATURE_INPUT, "SetPowerKeyFilteringStrategy failed. code:%{public}d", static_cast(code)); + return error.ThrowError(env, code); + } + return nullptr; +} } // namespace PowerMgr } // namespace OHOS diff --git a/frameworks/napi/power/power_napi.h b/frameworks/napi/power/power_napi.h index b6af89630a09a330076191f72c5d3ebc34f64ca1..c351156d21d76ad092c396c61daf52a7f2da8a3b 100644 --- a/frameworks/napi/power/power_napi.h +++ b/frameworks/napi/power/power_napi.h @@ -36,6 +36,7 @@ public: static napi_value IsStandby(napi_env env, napi_callback_info info); static napi_value SetScreenOffTime(napi_env env, napi_callback_info info); static napi_value RefreshActivity(napi_env env, napi_callback_info info); + static napi_value SetPowerKeyFilteringStrategy(napi_env env, napi_callback_info info); private: static napi_value RebootOrShutdown(napi_env env, napi_callback_info info, bool isReboot); diff --git a/frameworks/native/power_mgr_client.cpp b/frameworks/native/power_mgr_client.cpp index bb7f756c2a2f58f4417a6de97c2d12e7c904f3a7..a1a34df633a101578feed83bfaca8e5e99bc9cd9 100644 --- a/frameworks/native/power_mgr_client.cpp +++ b/frameworks/native/power_mgr_client.cpp @@ -671,5 +671,15 @@ PowerErrors PowerMgrClient::RefreshActivity(UserActivityType type, const std::st proxy->RefreshActivityIpc(GetTickCount(), activityType, refreshReason, powerError); return static_cast(powerError); } + +PowerErrors PowerMgrClient::SetPowerKeyFilteringStrategy(PowerKeyFilteringStrategy strategy) +{ + sptr proxy = GetPowerMgrProxy(); + RETURN_IF_WITH_RET(proxy == nullptr, PowerErrors::ERR_CONNECTION_FAIL); + int32_t powerError = static_cast(PowerErrors::ERR_CONNECTION_FAIL); + int32_t strategyValue = static_cast(strategy); + proxy->SetPowerKeyFilteringStrategyIpc(strategyValue, powerError); + return static_cast(powerError); +} } // namespace PowerMgr } // namespace OHOS diff --git a/interfaces/inner_api/IPowerMgr.idl b/interfaces/inner_api/IPowerMgr.idl index f5f4a495a8c2bf92552f68c0c68a397cff68edf7..9f95343f0cd786777c90265f7f59260944ccd1d8 100644 --- a/interfaces/inner_api/IPowerMgr.idl +++ b/interfaces/inner_api/IPowerMgr.idl @@ -109,4 +109,6 @@ interface OHOS.PowerMgr.IPowerMgr { void RefreshActivityIpc([in] long callTimeMs, [in] int activityType, [in] String refreshReason, [out] int powerError); + + void SetPowerKeyFilteringStrategyIpc([in] int strategy, [out] int powerError); } \ No newline at end of file diff --git a/interfaces/inner_api/native/include/power_mgr_client.h b/interfaces/inner_api/native/include/power_mgr_client.h index f7429f41c5676be6dc6f4afc4d9410b78d33765f..4b77ffc3a4e9bb7f964400957665f7db4e427df0 100644 --- a/interfaces/inner_api/native/include/power_mgr_client.h +++ b/interfaces/inner_api/native/include/power_mgr_client.h @@ -216,6 +216,13 @@ public: */ PowerErrors IsRunningLockEnabled(const RunningLockType type, bool& result); + /** + * Set the power key filtering strategy. + * @param strategy The power key filtering strategy. + * @return PowerErrors::ERR_OK if the call success, otherwise return error code + */ + PowerErrors SetPowerKeyFilteringStrategy(PowerKeyFilteringStrategy strategy); + #ifndef POWERMGR_SERVICE_DEATH_UT private: #endif diff --git a/interfaces/inner_api/native/include/power_state_machine_info.h b/interfaces/inner_api/native/include/power_state_machine_info.h index 5873e5edfba4f2d924fad1fb30143ef9fea1c57f..59b82c545c60e5781efc12971e0f135d5e99db05 100644 --- a/interfaces/inner_api/native/include/power_state_machine_info.h +++ b/interfaces/inner_api/native/include/power_state_machine_info.h @@ -258,6 +258,13 @@ enum class StateChangeReason : uint32_t { STATE_CHANGE_REASON_PLUG_CHANGE = 106, STATE_CHANGE_REASON_UNKNOWN = 1000, }; + +// PowerKey Filtering Strategy +enum class PowerKeyFilteringStrategy : uint32_t { + DISABLE_LONG_PRESS_FILTERING = 0, // not filtering + LONG_PRESS_FILTERING_ONCE = 1, // only filtering the current long press, the next not filtering + STRATEGY_MAX +}; } // namespace PowerMgr } // namespace OHOS #endif // POWERMGR_POWER_STATE_MACHINE_INFO_H diff --git a/services/native/include/power_mgr_service.h b/services/native/include/power_mgr_service.h index 324e31a6d3cb81bdea77fd22a839e84fafaac472..a0e13df7b12c5710bb894785598605f13d49c286 100644 --- a/services/native/include/power_mgr_service.h +++ b/services/native/include/power_mgr_service.h @@ -145,6 +145,7 @@ public: virtual PowerErrors IsRunningLockEnabled(const RunningLockType type, bool& result) override; virtual PowerErrors RefreshActivity( int64_t callTimeMs, UserActivityType type, const std::string& refreshReason) override; + virtual PowerErrors SetPowerKeyFilteringStrategy(PowerKeyFilteringStrategy strategy) override; void SetEnableDoze(bool enable); void RegisterShutdownCallback(const sptr& callback, ShutdownPriority priority) override; diff --git a/services/native/include/power_mgr_service_ipc_adapter.h b/services/native/include/power_mgr_service_ipc_adapter.h index 7f7260894080a350ec3224ab90788eda74160d08..a6d25e569934a2575e9f3d480ae843b028961556 100644 --- a/services/native/include/power_mgr_service_ipc_adapter.h +++ b/services/native/include/power_mgr_service_ipc_adapter.h @@ -104,6 +104,7 @@ public: int32_t UnRegisterShutdownCallbackIpc(const sptr& callback) override; virtual int32_t RefreshActivityIpc( int64_t callTimeMs, int32_t activityType, const std::string& refreshReason, int32_t& powerError) override; + virtual int32_t SetPowerKeyFilteringStrategyIpc(int32_t strategy, int32_t& powerError) override; virtual PowerErrors RebootDevice(const std::string& reason) = 0; virtual PowerErrors RebootDeviceForDeprecated(const std::string& reason) = 0; @@ -166,6 +167,7 @@ public: virtual PowerErrors IsRunningLockEnabled(const RunningLockType type, bool& result) = 0; virtual PowerErrors RefreshActivity( int64_t callTimeMs, UserActivityType type, const std::string& refreshReason) = 0; + virtual PowerErrors SetPowerKeyFilteringStrategy(PowerKeyFilteringStrategy strategy) = 0; virtual void RegisterShutdownCallback( const sptr& callback, ShutdownPriority priority) = 0; diff --git a/services/native/src/power_mgr_service.cpp b/services/native/src/power_mgr_service.cpp index 549cbbe160d117645b14be8f8cf606727d1d447e..0519b5e6865b86351d9f6deca15da319351ceb7f 100644 --- a/services/native/src/power_mgr_service.cpp +++ b/services/native/src/power_mgr_service.cpp @@ -2528,5 +2528,33 @@ PowerErrors PowerMgrService::RefreshActivity( return RefreshActivityInner(callTimeMs, type, true) ? PowerErrors::ERR_OK : PowerErrors::ERR_FREQUENT_FUNCTION_CALL; } + +PowerErrors PowerMgrService::SetPowerKeyFilteringStrategy(PowerKeyFilteringStrategy strategy) +{ + if (!Permission::IsSystem()) { + POWER_HILOGI(FEATURE_INPUT, "SetPowerKeyFilteringStrategy failed, System permission intercept"); + return PowerErrors::ERR_SYSTEM_API_DENIED; + } + if (!Permission::IsPermissionGranted("ohos.permission.POWER_MANAGER")) { + POWER_HILOGI(FEATURE_INPUT, "SetPowerKeyFilteringStrategy failed, The caller does not have the permission"); + return PowerErrors::ERR_PERMISSION_DENIED; + } + pid_t pid = IPCSkeleton::GetCallingPid(); + auto uid = IPCSkeleton::GetCallingUid(); + POWER_HILOGI(FEATURE_INPUT, + "SetPowerKeyFilteringStrategy pid: %{public}d, uid: %{public}d, strategy: %{public}d", pid, uid, strategy); + switch (strategy) { + case PowerKeyFilteringStrategy::DISABLE_LONG_PRESS_FILTERING: + shutdownDialog_.SetShutdownDialogForbid(false); + break; + case PowerKeyFilteringStrategy::LONG_PRESS_FILTERING_ONCE: + shutdownDialog_.SetShutdownDialogForbid(true); + break; + default: + POWER_HILOGW(FEATURE_INPUT, "SetPowerKeyFilteringStrategy out of range"); + break; + } + return PowerErrors::ERR_OK; +} } // namespace PowerMgr -} // namespace OHOS +} // namespace OHOS \ No newline at end of file diff --git a/services/native/src/power_mgr_service_ipc_adapter.cpp b/services/native/src/power_mgr_service_ipc_adapter.cpp index 4b55d65144618fec4b9f17b764f75c61f50f7e0b..54aec0ff5281e9991fad65404301d71bd8949ee1 100644 --- a/services/native/src/power_mgr_service_ipc_adapter.cpp +++ b/services/native/src/power_mgr_service_ipc_adapter.cpp @@ -490,5 +490,13 @@ int32_t PowerMgrServiceAdapter::RefreshActivityIpc( powerError = static_cast(RefreshActivity(callTimeMs, type, refreshReason)); return ERR_OK; } + +int32_t PowerMgrServiceAdapter::SetPowerKeyFilteringStrategyIpc(int32_t strategy, int32_t& powerError) +{ + PowerXCollie powerXCollie("PowerMgrServiceAdapter::SetPowerKeyFilteringStrategy", false); + PowerKeyFilteringStrategy filteringStrategy = static_cast(strategy); + powerError = static_cast(SetPowerKeyFilteringStrategy(filteringStrategy)); + return ERR_OK; +} } // namespace PowerMgr } // namespace OHOS diff --git a/services/native/src/shutdown/shutdown_dialog.cpp b/services/native/src/shutdown/shutdown_dialog.cpp index 167ae034c8fca0b3eac8eae50a71801bf9cbfec0..f90bbe465fe40c121b529ca5f5eef88a14acc937 100644 --- a/services/native/src/shutdown/shutdown_dialog.cpp +++ b/services/native/src/shutdown/shutdown_dialog.cpp @@ -31,6 +31,7 @@ #include #include "config_policy_utils.h" +#include "parameters.h" #include "power_cjson_utils.h" #include "power_log.h" #include "power_mgr_service.h" @@ -105,8 +106,7 @@ void ShutdownDialog::KeyMonitorInit() return; } #endif - ConnectSystemUi(); - StartVibrator(); + StartDialog(); }); if (longPressId_ < ERR_OK) { if (g_retryCount <= INIT_LONG_PRESS_RETRY) { @@ -139,6 +139,25 @@ void ShutdownDialog::KeyMonitorCancel() #endif } +void ShutdownDialog::SetShutdownDialogForbid(bool forbid) +{ + isShutdownDialogForbid_.store(forbid, std::memory_order_relaxed); + POWER_HILOGI(FEATURE_SHUTDOWN, "SetShutDownDialogForbid state to %{public}d", static_cast(forbid)); +} + +void ShutdownDialog::StartDialog() +{ + // If the forbid state is enable, reset and not start shutdown dialog + if (isShutdownDialogForbid_.load(std::memory_order_relaxed)) { + isShutdownDialogForbid_.store(false, std::memory_order_relaxed); + POWER_HILOGI(FEATURE_SHUTDOWN, "skip shutdown dialog"); + return; + } + + ConnectSystemUi(); + StartVibrator(); +} + bool ShutdownDialog::ConnectSystemUi() { if (g_isDialogShow) { diff --git a/services/native/src/shutdown/shutdown_dialog.h b/services/native/src/shutdown/shutdown_dialog.h index 27e3e37e52a0abff679444544d23e7bb84498e6b..4a5e583d1d7a5e56043d0353d63bf24d61c7df27 100644 --- a/services/native/src/shutdown/shutdown_dialog.h +++ b/services/native/src/shutdown/shutdown_dialog.h @@ -36,6 +36,7 @@ public: void StartVibrator(); void LoadDialogConfig(); void ParseJsonConfig(std::string& contentStr); + void SetShutdownDialogForbid(bool state); static std::string GetBundleName() { return bundleName_; @@ -61,9 +62,11 @@ private: std::mutex mutex_; }; + void StartDialog(); int32_t longPressId_ {0}; sptr dialogConnectionCallback_ {nullptr}; FFRTQueue queue_ {"shutdown_dialog"}; + std::atomic isShutdownDialogForbid_ {false}; static std::string bundleName_; static std::string abilityName_; static std::string uiExtensionType_; diff --git a/services/native/src/wakeup/wakeup_controller.cpp b/services/native/src/wakeup/wakeup_controller.cpp index a6dc33a75e4438c4f678fa550d97e31c79c1d7d9..f7fb472b003c4950aa26c73f3897d917de5564a1 100644 --- a/services/native/src/wakeup/wakeup_controller.cpp +++ b/services/native/src/wakeup/wakeup_controller.cpp @@ -990,6 +990,10 @@ void PowerkeyWakeupMonitor::ReceivePowerkeyCallback(std::shared_ptrSetPowerKeyFilteringStrategy(PowerKeyFilteringStrategy::DISABLE_LONG_PRESS_FILTERING); + int64_t now = static_cast(time(nullptr)); pms->RefreshActivityInner(now, UserActivityType::USER_ACTIVITY_TYPE_BUTTON, false); diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index 00d3e4290b15fc0be1289edbc8be848ae8013eb2..6835480cf04772b1aa77bce5bb5bbce1bcfeecce 100644 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -122,8 +122,8 @@ ohos_unittest("test_power_mgr_client") { module_out_path = module_output_path sources = [ - "src/interface_test/power_mgr_client_test.cpp", "mock/mock_power_remote_object.cpp", + "src/interface_test/power_mgr_client_test.cpp", ] sanitize = { @@ -350,7 +350,10 @@ ohos_unittest("test_power_mgr_service") { ":module_private_config", "${powermgr_utils_path}:coverage_flags", ] - include_dirs = [ "${powermgr_service_path}/native/src/setting" ] + include_dirs = [ + "${powermgr_service_path}/native/src/setting", + "${powermgr_utils_path}/permission/include", + ] deps = [ "${powermgr_inner_api}:powermgr_client", "${powermgr_service_path}:powermgrservice", @@ -379,9 +382,7 @@ ohos_unittest("test_power_mgr_service_ipc_adapter") { blocklist = "../cfi_blocklist.txt" } - sources = [ - "src/interface_test/power_mgr_service_ipc_adapter_test.cpp", - ] + sources = [ "src/interface_test/power_mgr_service_ipc_adapter_test.cpp" ] if (power_manager_feature_tv_dreaming) { defines += [ "POWER_MANAGER_TV_DREAMING" ] @@ -400,15 +401,15 @@ ohos_unittest("test_power_mgr_service_ipc_adapter") { deps = [ "${powermgr_service_path}:powermgrservice" ] external_deps = deps_ex + [ - "access_token:libaccesstoken_sdk", - "access_token:libnativetoken", - "access_token:libtoken_setproc", - "access_token:libtokenid_sdk", - "cJSON:cjson", - "data_share:datashare_common", - "data_share:datashare_consumer", - "selinux_adapter:librestorecon", - ] + "access_token:libaccesstoken_sdk", + "access_token:libnativetoken", + "access_token:libtoken_setproc", + "access_token:libtokenid_sdk", + "cJSON:cjson", + "data_share:datashare_common", + "data_share:datashare_consumer", + "selinux_adapter:librestorecon", + ] } ohos_unittest("test_power_mgr_service_death") { @@ -1322,9 +1323,9 @@ ohos_unittest("test_power_wakeup_controller_oninputevent") { deps = [ "${powermgr_inner_api}:powermgr_client", + "${powermgr_inner_api}:powermgr_stub", "${powermgr_service_path}:powermgrservice", "${powermgr_service_path}/native/src/actions:powermgr_actions", - "${powermgr_inner_api}:powermgr_stub", "${powermgr_utils_path}/setting:power_setting", ] @@ -1742,9 +1743,7 @@ ohos_unittest("test_power_config_parse_two") { "-Dprotected=public", ] - sources = [ - "src/power_config_parse_test_two.cpp", - ] + sources = [ "src/power_config_parse_test_two.cpp" ] configs = [ "${powermgr_utils_path}:utils_config", diff --git a/test/unittest/src/interface_test/power_mgr_client_test.cpp b/test/unittest/src/interface_test/power_mgr_client_test.cpp index 9191d767cb18d64ec8a9dd4eccda7fc48e365c2c..618692388a1da947e48055617bdc5c90d8d01fd6 100644 --- a/test/unittest/src/interface_test/power_mgr_client_test.cpp +++ b/test/unittest/src/interface_test/power_mgr_client_test.cpp @@ -1509,4 +1509,32 @@ HWTEST_F(PowerMgrClientTest, PowerMgrClient060, TestSize.Level0) { POWER_HILOGI(LABEL_TEST, "PowerMgrClient060 function end!"); } #endif + + +/** + * @tc.name: PowerMgrClient061 + * @tc.desc: Test all PowerKeyFilteringStrategy enum values + * @tc.type: FUNC + */ +HWTEST_F(PowerMgrClientTest, PowerMgrClient061, TestSize.Level2) +{ + POWER_HILOGI(LABEL_TEST, "PowerMgrClient061 start!"); + auto& client = PowerMgrClient::GetInstance(); + PowerErrors ret; + + // Test DISABLE_LONG_PRESS_FILTERING + ret = client.SetPowerKeyFilteringStrategy(PowerKeyFilteringStrategy::DISABLE_LONG_PRESS_FILTERING); + EXPECT_EQ(ret, PowerErrors::ERR_OK) << "DISABLE_LONG_PRESS_FILTERING failed"; + + // Test LONG_PRESS_FILTERING_ONCE + ret = client.SetPowerKeyFilteringStrategy(PowerKeyFilteringStrategy::LONG_PRESS_FILTERING_ONCE); + EXPECT_EQ(ret, PowerErrors::ERR_OK) << "LONG_PRESS_FILTERING_ONCE failed"; + + // Test invalid STRATEGY_MAX + ret = client.SetPowerKeyFilteringStrategy(PowerKeyFilteringStrategy::STRATEGY_MAX); + EXPECT_EQ(ret, PowerErrors::ERR_OK) << "STRATEGY_MAX should return error"; + + POWER_HILOGI(LABEL_TEST, "PowerMgrClient061 end!"); +} + } // namespace \ No newline at end of file diff --git a/test/unittest/src/interface_test/power_mgr_service_test.cpp b/test/unittest/src/interface_test/power_mgr_service_test.cpp index e29243e24abc2825762c7ec4d1d15179fa24b423..81abac9e6bf90c5da39613747548805adfe86ab1 100644 --- a/test/unittest/src/interface_test/power_mgr_service_test.cpp +++ b/test/unittest/src/interface_test/power_mgr_service_test.cpp @@ -25,6 +25,7 @@ #include #include +#include "permission.h" #include "accesstoken_kit.h" #include "display_manager_lite.h" #include "mock_state_action.h" @@ -46,6 +47,25 @@ using namespace OHOS::PowerMgr; using namespace OHOS; using namespace std; +namespace { +bool g_isSystem = true; +bool g_isPermissionGranted = true; +} // namespace + +namespace OHOS::PowerMgr { +bool Permission::IsSystem() +{ + GTEST_LOG_(INFO) << "PowerMgrServiceTest g_isSystem: " << g_isSystem; + return g_isSystem; +} + +bool Permission::IsPermissionGranted(const std::string& perm) +{ + GTEST_LOG_(INFO) << "PowerMgrServiceTest IsPermissionGranted: " << g_isPermissionGranted; + return g_isPermissionGranted; +} +} // namespace OHOS::PowerMgr + void PowerMgrServiceTest::SetUpTestCase(void) { DelayedSpSingleton::GetInstance()->OnStart(); @@ -61,6 +81,8 @@ void PowerMgrServiceTest::SetUp(void) void PowerMgrServiceTest::TearDown(void) { + g_isSystem = true; + g_isPermissionGranted = true; } #ifdef POWER_MANAGER_TV_DREAMING @@ -1012,4 +1034,46 @@ HWTEST_F(PowerMgrServiceTest, PowerMgrService038, TestSize.Level0) { POWER_HILOGI(LABEL_TEST, "PowerMgrServiceTest::PowerMgrService038 function end!"); } #endif + +/** + * @tc.name: PowerMgrService039 + * @tc.desc: Test SetPowerKeyFilteringStrategy with all branches + * @tc.type: FUNC + */ +HWTEST_F(PowerMgrServiceTest, PowerMgrService039, TestSize.Level2) { + POWER_HILOGI(LABEL_TEST, "PowerMgrServiceTest::PowerMgrService039 start!"); + auto pmsTest_ = DelayedSpSingleton::GetInstance(); + PowerKeyFilteringStrategy strategy; + PowerErrors ret; + + // Test case 1: No system permission + g_isSystem = false; + g_isPermissionGranted = true; + ret = pmsTest_->SetPowerKeyFilteringStrategy(PowerKeyFilteringStrategy::DISABLE_LONG_PRESS_FILTERING); + EXPECT_EQ(ret, PowerErrors::ERR_SYSTEM_API_DENIED) << "Test case 1 failed"; + + // Test case 2: No POWER_MANAGER permission + g_isSystem = true; + g_isPermissionGranted = false; + ret = pmsTest_->SetPowerKeyFilteringStrategy(PowerKeyFilteringStrategy::DISABLE_LONG_PRESS_FILTERING); + EXPECT_EQ(ret, PowerErrors::ERR_PERMISSION_DENIED) << "Test case 2 failed"; + + // Test case 3: Valid strategy DISABLE_LONG_PRESS_FILTERING + g_isPermissionGranted = true; + strategy = PowerKeyFilteringStrategy::DISABLE_LONG_PRESS_FILTERING; + ret = pmsTest_->SetPowerKeyFilteringStrategy(strategy); + EXPECT_EQ(ret, PowerErrors::ERR_OK) << "Test case 3 failed"; + + // Test case 4: Valid strategy LONG_PRESS_FILTERING_ONCE + strategy = PowerKeyFilteringStrategy::LONG_PRESS_FILTERING_ONCE; + ret = pmsTest_->SetPowerKeyFilteringStrategy(strategy); + EXPECT_EQ(ret, PowerErrors::ERR_OK) << "Test case 4 failed"; + + // Test case 5: Invalid strategy + strategy = static_cast(PowerKeyFilteringStrategy::STRATEGY_MAX); + ret = pmsTest_->SetPowerKeyFilteringStrategy(strategy); + EXPECT_EQ(ret, PowerErrors::ERR_OK) << "Test case 5 failed"; + + POWER_HILOGI(LABEL_TEST, "PowerMgrServiceTest::PowerMgrService039 end!"); } +} \ No newline at end of file diff --git a/test/unittest/src/power_mgr_powerdialog_test.cpp b/test/unittest/src/power_mgr_powerdialog_test.cpp index 182a336c69b991fc8e66c17c959db80fe3ddf760..2b92c1f04539b1e0677fb37b07ff158abf29aa71 100644 --- a/test/unittest/src/power_mgr_powerdialog_test.cpp +++ b/test/unittest/src/power_mgr_powerdialog_test.cpp @@ -173,4 +173,24 @@ HWTEST_F(PowerMgrPowerDialog, OnAbilityDisconnectDoneTest, TestSize.Level2) POWER_HILOGI(LABEL_TEST, "OnAbilityDisconnectDoneTest function end!"); GTEST_LOG_(INFO) << "OnAbilityDisconnectDoneTest end."; } + +/** + * @tc.name: StartDialogTest + * @tc.desc: test StartDialog + * @tc.type: FUNC + */ +HWTEST_F(PowerMgrPowerDialog, StartDialogTest, TestSize.Level2) +{ + GTEST_LOG_(INFO) << "StartDialogTest start."; + POWER_HILOGI(LABEL_TEST, "StartDialog function start!"); + ShutdownDialog shutdownDialog; + shutdownDialog.SetShutdownDialogForbid(true); + EXPECT_TRUE(shutdownDialog.isShutdownDialogForbid_.load(std::memory_order_relaxed)); + shutdownDialog.StartDialog(); + shutdownDialog.SetShutdownDialogForbid(false); + EXPECT_FALSE(shutdownDialog.isShutdownDialogForbid_.load(std::memory_order_relaxed)); + shutdownDialog.StartDialog(); + POWER_HILOGI(LABEL_TEST, "StartDialog function end!"); + GTEST_LOG_(INFO) << "StartDialogTest end."; +} } // namespace