From d7f5e6d02be212a575550945741fad1f73c689dc Mon Sep 17 00:00:00 2001 From: cjw123qq Date: Sat, 5 Jul 2025 10:40:09 +0800 Subject: [PATCH] fix: power_stage_2 taihe idl for power Signed-off-by: cjw123qq --- bundle.json | 11 +- frameworks/ets/taihe/BUILD.gn | 29 ++ frameworks/ets/taihe/power/BUILD.gn | 82 +++++ .../ets/taihe/power/idl/ohos.power.taihe | 48 +++ .../ets/taihe/power/src/ani_constructor.cpp | 32 ++ .../ets/taihe/power/src/ohos.power.impl.cpp | 214 ++++++++++++ .../ets/taihe/power/test/unittest/BUILD.gn | 68 ++++ .../unittest/power_ani_constructor_test.cpp | 113 +++++++ .../test/unittest/power_taihe_native_test.cpp | 319 ++++++++++++++++++ frameworks/ets/taihe/runninglock/BUILD.gn | 78 +++++ .../runninglock/idl/ohos.runningLock.taihe | 34 ++ .../taihe/runninglock/src/ani_constructor.cpp | 32 ++ .../runninglock/src/ohos.runningLock.impl.cpp | 113 +++++++ .../taihe/runninglock/test/unittest/BUILD.gn | 57 ++++ .../running_lock_ani_constructor_test.cpp | 113 +++++++ .../running_lock_taihe_native_test.cpp | 166 +++++++++ powermgr.gni | 2 + 17 files changed, 1510 insertions(+), 1 deletion(-) create mode 100644 frameworks/ets/taihe/BUILD.gn create mode 100644 frameworks/ets/taihe/power/BUILD.gn create mode 100644 frameworks/ets/taihe/power/idl/ohos.power.taihe create mode 100644 frameworks/ets/taihe/power/src/ani_constructor.cpp create mode 100644 frameworks/ets/taihe/power/src/ohos.power.impl.cpp create mode 100644 frameworks/ets/taihe/power/test/unittest/BUILD.gn create mode 100644 frameworks/ets/taihe/power/test/unittest/power_ani_constructor_test.cpp create mode 100644 frameworks/ets/taihe/power/test/unittest/power_taihe_native_test.cpp create mode 100644 frameworks/ets/taihe/runninglock/BUILD.gn create mode 100644 frameworks/ets/taihe/runninglock/idl/ohos.runningLock.taihe create mode 100644 frameworks/ets/taihe/runninglock/src/ani_constructor.cpp create mode 100644 frameworks/ets/taihe/runninglock/src/ohos.runningLock.impl.cpp create mode 100644 frameworks/ets/taihe/runninglock/test/unittest/BUILD.gn create mode 100644 frameworks/ets/taihe/runninglock/test/unittest/running_lock_ani_constructor_test.cpp create mode 100644 frameworks/ets/taihe/runninglock/test/unittest/running_lock_taihe_native_test.cpp diff --git a/bundle.json b/bundle.json index 8c986d99..8e913c0f 100644 --- a/bundle.json +++ b/bundle.json @@ -85,6 +85,7 @@ "libxml2", "napi", "os_account", + "runtime_core", "safwk", "samgr", "sensor", @@ -112,6 +113,7 @@ ], "fwk_group": [ "//base/powermgr/power_manager/frameworks:power_napi", + "//base/powermgr/power_manager/frameworks/ets/taihe:power_manager_taihe", "//base/powermgr/power_manager/interfaces/inner_api:powermgr_client" ], "service_group": [ @@ -213,13 +215,20 @@ ], "header_base": "//base/powermgr/power_manager/frameworks/cj/power/include" } + }, + { + "name": "//base/powermgr/power_manager/frameworks/ets/taihe/runninglock:copy_power_manager_runninglock" + }, + { + "name": "//base/powermgr/power_manager/frameworks/ets/taihe/power:copy_power_manager" } ], "test": [ "//base/powermgr/power_manager/test:apitest", "//base/powermgr/power_manager/test:unittest", "//base/powermgr/power_manager/test:fuzztest", - "//base/powermgr/power_manager/test:systemtest" + "//base/powermgr/power_manager/test:systemtest", + "//base/powermgr/power_manager/frameworks/ets/taihe:power_manager_taihe_test" ] } } diff --git a/frameworks/ets/taihe/BUILD.gn b/frameworks/ets/taihe/BUILD.gn new file mode 100644 index 00000000..b690cbd6 --- /dev/null +++ b/frameworks/ets/taihe/BUILD.gn @@ -0,0 +1,29 @@ +# Copyright (c) 2025 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. + +group("power_manager_taihe") { + deps = [ + "power:power_abc_etc", + "power:power_manager_taihe_native", + "runninglock:runningLock_etc", + "runninglock:power_manager_runninglock_taihe_native", + ] +} + +group("power_manager_taihe_test") { + testonly = true + deps = [ + "power/test/unittest:power_taihe_native_test", + "runninglock/test/unittest:runninglock_taihe_native_test", + ] +} \ No newline at end of file diff --git a/frameworks/ets/taihe/power/BUILD.gn b/frameworks/ets/taihe/power/BUILD.gn new file mode 100644 index 00000000..b731ecdb --- /dev/null +++ b/frameworks/ets/taihe/power/BUILD.gn @@ -0,0 +1,82 @@ +# Copyright (c) 2025 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. + +import("//build/config/components/ets_frontend/ets2abc_config.gni") +import("//build/ohos.gni") +import("//build/ohos/taihe_idl/taihe.gni") +import("../../../../powermgr.gni") + +copy_taihe_idl("copy_power_manager") { + sources = [ "idl/ohos.power.taihe" ] +} + +ohos_taihe("run_taihe") { + taihe_generated_file_path = "${taihe_generated_file_path_power}" + deps = [ ":copy_power_manager" ] + outputs = [ + "$taihe_generated_file_path/src/ohos.power.ani.cpp", + "$taihe_generated_file_path/src/ohos.power.abi.c", + ] +} + +taihe_shared_library("power_manager_taihe_native") { + branch_protector_ret = "pac_ret" + sanitize = { + cfi = true + cfi_cross_dso = true + debug = false + } + taihe_generated_file_path = "${taihe_generated_file_path_power}" + sources = get_target_outputs(":run_taihe") + include_dirs = [ "./include" ] + configs = [ + "${powermgr_utils_path}:utils_config", + "${powermgr_utils_path}:coverage_flags", + ] + sources += [ + "src/ani_constructor.cpp", + "src/ohos.power.impl.cpp", + ] + deps = [ + ":run_taihe", + "${powermgr_inner_api}:powermgr_client", + "${powermgr_utils_path}/appmgr:power_appmgr", + ] + external_deps = [ + "c_utils:utils", + "hilog:libhilog", + "ipc:ipc_core", + ] + if (has_ability_runtime_part) { + external_deps += [ "ability_runtime:app_manager" ] + } + + subsystem_name = "powermgr" + part_name = "${powermgr_part_name}" +} + +generate_static_abc("power_abc") { + base_url = "${taihe_generated_file_path_power}" + files = [ "${taihe_generated_file_path_power}/@ohos.power.ets" ] + is_boot_abc = "True" + device_dst_file = "/system/framework/power_abc.abc" + dependencies = [ ":run_taihe" ] +} + +ohos_prebuilt_etc("power_abc_etc") { + source = "$target_out_dir/power_abc.abc" + module_install_dir = "framework" + subsystem_name = "powermgr" + part_name = "${powermgr_part_name}" + deps = [ ":power_abc" ] +} diff --git a/frameworks/ets/taihe/power/idl/ohos.power.taihe b/frameworks/ets/taihe/power/idl/ohos.power.taihe new file mode 100644 index 00000000..ff6a90b5 --- /dev/null +++ b/frameworks/ets/taihe/power/idl/ohos.power.taihe @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2025 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. + */ + +@!namespace("@ohos.power", "power") + +@!sts_inject(""" +static { loadLibrary("power_manager_taihe_native.z") }""") + +function Shutdown(reason: String): void; + +function Reboot(reason: String): void; + +function IsActive(): bool; + +function Wakeup(detail: String): void; + +function Suspend(isImmediate: Optional): void; + +function GetPowerMode(): DevicePowerMode; + +@gen_async("setPowerMode") +@gen_promise("setPowerMode") +function SetPowerModeSync(mode: DevicePowerMode): void; + +function IsStandby(): bool; + +function Hibernate(clearMemory: bool): void; + +function SetScreenOffTime(timeout: i64): void; + +enum DevicePowerMode : i32 { + MODE_NORMAL = 600, + MODE_POWER_SAVE, + MODE_PERFORMANCE, + MODE_EXTREME_POWER_SAVE +} \ No newline at end of file diff --git a/frameworks/ets/taihe/power/src/ani_constructor.cpp b/frameworks/ets/taihe/power/src/ani_constructor.cpp new file mode 100644 index 00000000..023e99b0 --- /dev/null +++ b/frameworks/ets/taihe/power/src/ani_constructor.cpp @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2025 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 "ohos.power.ani.hpp" +#include "power_log.h" +using namespace OHOS::PowerMgr; +ANI_EXPORT ani_status ANI_Constructor(ani_vm *vm, uint32_t *result) +{ + ani_env *env; + if (ANI_OK != vm->GetEnv(ANI_VERSION_1, &env)) { + POWER_HILOGE(COMP_FWK, "Error from GetEnv"); + return ANI_ERROR; + } + if (ANI_OK != ohos::power::ANIRegister(env)) { + POWER_HILOGE(COMP_FWK, "Error from ohos::power::ANIRegister"); + return ANI_ERROR; + } + *result = ANI_VERSION_1; + return ANI_OK; +} diff --git a/frameworks/ets/taihe/power/src/ohos.power.impl.cpp b/frameworks/ets/taihe/power/src/ohos.power.impl.cpp new file mode 100644 index 00000000..25547fd8 --- /dev/null +++ b/frameworks/ets/taihe/power/src/ohos.power.impl.cpp @@ -0,0 +1,214 @@ +/* + * Copyright (c) 2025 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 "ohos.power.proj.hpp" +#include "ohos.power.impl.hpp" +#include "taihe/runtime.hpp" +#include "stdexcept" +#include "power_common.h" +#include "power_log.h" +#include "power_mgr_client.h" +#include +#include +#include +#include "app_manager_utils.h" + +#define SET_REBOOT _IOW(BOOT_DETECTOR_IOCTL_BASE, 109, int) + +using namespace taihe; +using namespace ohos::power; +using namespace OHOS::PowerMgr; + +namespace { +std::map g_errorTable = { + {PowerErrors::ERR_CONNECTION_FAIL, "Failed to connect to the service."}, + {PowerErrors::ERR_PERMISSION_DENIED, "Permission is denied" }, + {PowerErrors::ERR_SYSTEM_API_DENIED, "System permission is denied" }, + {PowerErrors::ERR_PARAM_INVALID, "Invalid input parameter." } +}; +static PowerMgrClient& g_powerMgrClient = PowerMgrClient::GetInstance(); +constexpr int32_t RESTORE_DEFAULT_SCREENOFF_TIME = -1; + +static void SetFrameworkBootStage(bool isReboot) +{ + int fd = open("/dev/bbox", O_WRONLY); + if (fd < 0) { + POWER_HILOGE(FEATURE_SHUTDOWN, "open /dev/bbox failed!"); + return; + } + + fdsan_exchange_owner_tag(fd, 0, DOMAIN_FEATURE_SHUTDOWN); + POWER_HILOGI(FEATURE_SHUTDOWN, "Set shutdown fw start timeout."); + + int rebootFlag = isReboot ? 1 : 0; + int ret = ioctl(fd, SET_REBOOT, &rebootFlag); + if (ret < 0) { + POWER_HILOGE(FEATURE_SHUTDOWN, "set reboot flag failed!"); + fdsan_close_with_tag(fd, DOMAIN_FEATURE_SHUTDOWN); + return; + } + + int stage = SHUT_STAGE_FRAMEWORK_START; + ret = ioctl(fd, SET_SHUT_STAGE, &stage); + if (ret < 0) { + POWER_HILOGE(FEATURE_SHUTDOWN, "set shut stage failed!"); + } + + POWER_HILOGI(FEATURE_SHUTDOWN, "Set shutdown timeout mechanism started."); + fdsan_close_with_tag(fd, DOMAIN_FEATURE_SHUTDOWN); + + return; +} + +void Shutdown(string_view reason) +{ + SetFrameworkBootStage(false); + POWER_HILOGD(FEATURE_SHUTDOWN, "reboot: %{public}d, reason: %{public}s", false, reason.c_str()); + PowerErrors code = g_powerMgrClient.ShutDownDevice(std::string(reason)); + if (code != PowerErrors::ERR_OK && code != PowerErrors::ERR_FAILURE) { + taihe::set_business_error(static_cast(code), g_errorTable[code]); + } +} + +void Reboot(string_view reason) +{ + SetFrameworkBootStage(true); + POWER_HILOGD(FEATURE_SHUTDOWN, "reboot: %{public}d, reason: %{public}s", true, reason.c_str()); + PowerErrors code = g_powerMgrClient.RebootDevice(std::string(reason)); + if (code != PowerErrors::ERR_OK && code != PowerErrors::ERR_FAILURE) { + taihe::set_business_error(static_cast(code), g_errorTable[code]); + } +} + +bool IsActive() +{ + return g_powerMgrClient.IsScreenOn(); +} + +void Wakeup(string_view detail) +{ + POWER_HILOGD(FEATURE_WAKEUP, "Wakeup type: APPLICATION, reason: %{public}s", detail.c_str()); + int32_t apiVersion = AppManagerUtils::GetApiTargetVersion(); + PowerErrors code = g_powerMgrClient.WakeupDevice( + WakeupDeviceType::WAKEUP_DEVICE_APPLICATION, std::string(detail), std::to_string(apiVersion)); + if (code != PowerErrors::ERR_OK && code != PowerErrors::ERR_FAILURE) { + taihe::set_business_error(static_cast(code), g_errorTable[code]); + } +} + +void Suspend(optional_view isImmediate) +{ + bool isForce = isImmediate.value_or(false); + + PowerErrors code; + int32_t apiVersion = AppManagerUtils::GetApiTargetVersion(); + if (isForce) { + code = g_powerMgrClient.ForceSuspendDevice(std::to_string(apiVersion)); + } else { + code = g_powerMgrClient.SuspendDevice( + SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false, std::to_string(apiVersion)); + } + if (code != PowerErrors::ERR_OK && code != PowerErrors::ERR_FAILURE) { + POWER_HILOGE(FEATURE_SUSPEND, "Suspend Device fail, isForce:%{public}d", isForce); + taihe::set_business_error(static_cast(code), g_errorTable[code]); + } +} + +DevicePowerMode GetPowerMode() +{ + PowerMode mode = g_powerMgrClient.GetDeviceMode(); + DevicePowerMode deviceMode(DevicePowerMode::key_t::MODE_NORMAL); + switch (mode) { + case PowerMode::NORMAL_MODE: + deviceMode = DevicePowerMode::key_t::MODE_NORMAL; + break; + case PowerMode::POWER_SAVE_MODE: + deviceMode = DevicePowerMode::key_t::MODE_POWER_SAVE; + break; + case PowerMode::PERFORMANCE_MODE: + deviceMode = DevicePowerMode::key_t::MODE_PERFORMANCE; + break; + case PowerMode::EXTREME_POWER_SAVE_MODE: + deviceMode = DevicePowerMode::key_t::MODE_EXTREME_POWER_SAVE; + break; + default: + POWER_HILOGE(FEATURE_POWER_MODE, "Unknown mode"); + } + return deviceMode; +} + +void SetPowerModeSync(DevicePowerMode mode) +{ + PowerErrors code = g_powerMgrClient.SetDeviceMode(PowerMode(static_cast(mode))); + if (code != PowerErrors::ERR_OK && code != PowerErrors::ERR_FAILURE) { + taihe::set_business_error(static_cast(code), g_errorTable[code]); + } +} + +bool IsStandby() +{ + bool isStandby = false; + PowerErrors code = g_powerMgrClient.IsStandby(isStandby); + if (code != PowerErrors::ERR_OK && code != PowerErrors::ERR_FAILURE) { + taihe::set_business_error(static_cast(code), g_errorTable[code]); + } + return isStandby; +} + +void Hibernate(bool clearMemory) +{ + int32_t apiVersion = AppManagerUtils::GetApiTargetVersion(); + PowerErrors code = g_powerMgrClient.Hibernate(clearMemory, "", std::to_string(apiVersion)); + if (code != PowerErrors::ERR_OK && code != PowerErrors::ERR_FAILURE) { + POWER_HILOGE(FEATURE_WAKEUP, "Hibernate failed."); + taihe::set_business_error(static_cast(code), g_errorTable[code]); + } +} + +void SetScreenOffTime(int64_t timeout) +{ + if (timeout == 0 || (timeout < 0 && timeout != RESTORE_DEFAULT_SCREENOFF_TIME)) { + POWER_HILOGE(FEATURE_WAKEUP, "timeout is not right."); + taihe::set_business_error( + static_cast(PowerErrors::ERR_PARAM_INVALID), g_errorTable[PowerErrors::ERR_PARAM_INVALID]); + } + + PowerErrors code; + int32_t apiVersion = AppManagerUtils::GetApiTargetVersion(); + if (timeout == RESTORE_DEFAULT_SCREENOFF_TIME) { + code = g_powerMgrClient.RestoreScreenOffTime(std::to_string(apiVersion)); + } else { + code = g_powerMgrClient.OverrideScreenOffTime(timeout, std::to_string(apiVersion)); + } + if (code != PowerErrors::ERR_OK && code != PowerErrors::ERR_FAILURE) { + POWER_HILOGE(FEATURE_WAKEUP, "SetScreenOffTime failed."); + taihe::set_business_error(static_cast(code), g_errorTable[code]); + } +} +} // namespace + +// Since these macros are auto-generate, lint will cause false positive +// NOLINTBEGIN +TH_EXPORT_CPP_API_Shutdown(Shutdown); +TH_EXPORT_CPP_API_Reboot(Reboot); +TH_EXPORT_CPP_API_IsActive(IsActive); +TH_EXPORT_CPP_API_Wakeup(Wakeup); +TH_EXPORT_CPP_API_Suspend(Suspend); +TH_EXPORT_CPP_API_GetPowerMode(GetPowerMode); +TH_EXPORT_CPP_API_SetPowerModeSync(SetPowerModeSync); +TH_EXPORT_CPP_API_IsStandby(IsStandby); +TH_EXPORT_CPP_API_Hibernate(Hibernate); +TH_EXPORT_CPP_API_SetScreenOffTime(SetScreenOffTime); +// NOLINTEND \ No newline at end of file diff --git a/frameworks/ets/taihe/power/test/unittest/BUILD.gn b/frameworks/ets/taihe/power/test/unittest/BUILD.gn new file mode 100644 index 00000000..337bef91 --- /dev/null +++ b/frameworks/ets/taihe/power/test/unittest/BUILD.gn @@ -0,0 +1,68 @@ +# Copyright (c) 2025 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. + +import("//build/test.gni") +import("//build/ohos/taihe_idl/taihe.gni") +import("../../../../../../powermgr.gni") + +module_output_path = "power_manager/power_manager" + +config("module_private_config") { + visibility = [ ":*" ] + + include_dirs = [ + "${taihe_generated_file_path_power}/include", + "${taihe_toolchain_path}/include" + ] +} + +ohos_unittest("power_taihe_native_test") { + module_out_path = module_output_path + + sanitize = { + cfi = true + cfi_cross_dso = true + debug = false + } + + sources = [ + "power_ani_constructor_test.cpp", + "power_taihe_native_test.cpp", + "../../src/ohos.power.impl.cpp" + ] + + configs = [ + ":module_private_config", + "${powermgr_utils_path}:coverage_flags", + ] + + deps = [ + "${powermgr_framework}/ets/taihe/power:power_manager_taihe_native", + "${powermgr_framework}/ets/taihe/power:run_taihe", + "${powermgr_inner_api}:powermgr_client", + "${powermgr_utils_path}/appmgr:power_appmgr", + "${powermgr_utils_path}:power_utils", + ] + + external_deps = [ + "c_utils:utils", + "googletest:gtest_main", + "hilog:libhilog", + "ipc:ipc_core", + "runtime_core:ani", + ] + + if (has_ability_runtime_part) { + external_deps += [ "ability_runtime:app_manager" ] + } +} \ No newline at end of file diff --git a/frameworks/ets/taihe/power/test/unittest/power_ani_constructor_test.cpp b/frameworks/ets/taihe/power/test/unittest/power_ani_constructor_test.cpp new file mode 100644 index 00000000..0f74eed0 --- /dev/null +++ b/frameworks/ets/taihe/power/test/unittest/power_ani_constructor_test.cpp @@ -0,0 +1,113 @@ +/* + * Copyright (c) 2025 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 "ohos.power.ani.hpp" +#include "power_log.h" + +using namespace OHOS::PowerMgr; + +using namespace testing; +using namespace testing::ext; +namespace { +ani_status g_status1 = ANI_OK; +ani_status g_status2 = ANI_OK; +} + +namespace ohos::power { +ani_status ANIRegister(ani_env *env) +{ + return g_status1; +} +} + +namespace { +class PowerAniConstructorTest : public ::testing::Test { +public: + static void SetUpTestCase() {} + static void TearDownTestCase() {} + void SetUp() {} + void TearDown() {} +}; + +static ani_status GetEnv(ani_vm *vm, uint32_t version, ani_env **result) +{ + return g_status2; +} +/** + * @tc.name: PowerAniConstructorTest_001 + * @tc.desc: test AniConstructor + * @tc.type: FUNC + * @tc.require: issue#ICAK9Z + */ +HWTEST_F(PowerAniConstructorTest, PowerAniConstructorTest_001, TestSize.Level1) +{ + POWER_HILOGI(LABEL_TEST, "PowerAniConstructorTest_001 start"); + ani_vm vm; + __ani_vm_api mock_c_api = {.GetEnv = GetEnv}; + vm.c_api = &mock_c_api; + uint32_t result = 0; + + ani_status status = ANI_Constructor(&vm, &result); + + EXPECT_EQ(status, ANI_OK); + EXPECT_EQ(result, ANI_VERSION_1); + POWER_HILOGI(LABEL_TEST, "PowerAniConstructorTest_001 end"); +} + +/** + * @tc.name: PowerAniConstructorTest_002 + * @tc.desc: test AniConstructor + * @tc.type: FUNC + * @tc.require: issue#ICAK9Z + */ +HWTEST_F(PowerAniConstructorTest, PowerAniConstructorTest_002, TestSize.Level1) +{ + POWER_HILOGI(LABEL_TEST, "PowerAniConstructorTest_002 start"); + ani_vm vm; + __ani_vm_api mock_c_api = {.GetEnv = GetEnv}; + vm.c_api = &mock_c_api; + uint32_t result = 0; + + g_status1 = ANI_ERROR; + ani_status status = ANI_Constructor(&vm, &result); + + EXPECT_EQ(status, ANI_ERROR); + POWER_HILOGI(LABEL_TEST, "PowerAniConstructorTest_002 end"); +} + +/** + * @tc.name: PowerAniConstructorTest_003 + * @tc.desc: test AniConstructor + * @tc.type: FUNC + * @tc.require: issue#ICAK9Z + */ +HWTEST_F(PowerAniConstructorTest, PowerAniConstructorTest_003, TestSize.Level1) +{ + POWER_HILOGI(LABEL_TEST, "PowerAniConstructorTest_003 start"); + ani_vm vm; + __ani_vm_api mock_c_api = {.GetEnv = GetEnv}; + vm.c_api = &mock_c_api; + uint32_t result = 0; + + g_status1 = ANI_OK; + g_status2 = ANI_ERROR; + ani_status status = ANI_Constructor(&vm, &result); + + EXPECT_EQ(status, ANI_ERROR); + POWER_HILOGI(LABEL_TEST, "PowerAniConstructorTest_003 end"); +} +} \ No newline at end of file diff --git a/frameworks/ets/taihe/power/test/unittest/power_taihe_native_test.cpp b/frameworks/ets/taihe/power/test/unittest/power_taihe_native_test.cpp new file mode 100644 index 00000000..27716027 --- /dev/null +++ b/frameworks/ets/taihe/power/test/unittest/power_taihe_native_test.cpp @@ -0,0 +1,319 @@ +/* + * Copyright (c) 2025 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 "ohos.power.proj.hpp" +#include "ohos.power.impl.hpp" +#include "ohos.power.user.hpp" +#include "taihe/runtime.hpp" +#include "power_mgr_client.h" +#include "power_errors.h" +#include "power_log.h" + +using namespace taihe; +using namespace ohos::power; +using namespace OHOS::PowerMgr; + +using namespace testing; +using namespace testing::ext; + +namespace { +PowerErrors g_error = PowerErrors::ERR_OK; +bool g_pass = false; +PowerMode g_mode = PowerMode::NORMAL_MODE; +int32_t g_errCode = 0; +} + +namespace taihe { +void set_business_error(int32_t err_code, taihe::string_view msg) +{ + g_errCode = err_code; + (void)msg; +} +} + +namespace OHOS::PowerMgr { +bool PowerMgrClient::IsScreenOn(bool needPrintLog) +{ + g_pass = true; + return false; +} + +PowerErrors PowerMgrClient::WakeupDevice( + WakeupDeviceType reason, const std::string& detail, const std::string& apiVersion) +{ + g_pass = true; + return g_error; +} + +PowerErrors PowerMgrClient::ForceSuspendDevice(const std::string& apiVersion) +{ + g_pass = true; + return g_error; +} + +PowerErrors PowerMgrClient::SuspendDevice( + SuspendDeviceType reason, bool suspendImmed, const std::string& apiVersion) +{ + g_pass = true; + return g_error; +} + +PowerMode PowerMgrClient::GetDeviceMode() +{ + g_pass = true; + return g_mode; +} + +PowerErrors PowerMgrClient::SetDeviceMode(const PowerMode mode) +{ + g_pass = true; + return g_error; +} + +PowerErrors PowerMgrClient::IsStandby(bool& isStandby) +{ + g_pass = true; + return g_error; +} + +PowerErrors PowerMgrClient::Hibernate(bool clearMemory, const std::string& reason, const std::string& apiVersion) +{ + g_pass = true; + return g_error; +} + +PowerErrors PowerMgrClient::RestoreScreenOffTime(const std::string& apiVersion) +{ + g_pass = true; + return g_error; +} + +PowerErrors PowerMgrClient::OverrideScreenOffTime(int64_t timeout, const std::string& apiVersion) +{ + g_pass = true; + return g_error; +} +} // namespace OHOS::PowerMgr + +namespace { +class PowerTaiheNativeTest : public ::testing::Test { +public: + static void SetUpTestCase() {} + static void TearDownTestCase() {} + void SetUp() + { + EXPECT_FALSE(g_pass); + } + void TearDown() + { + g_pass = false; + g_mode = PowerMode::NORMAL_MODE; + g_error = PowerErrors::ERR_OK; + g_errCode = 0; + } +}; + +/** + * @tc.name: PowerTaiheNativeTest_001 + * @tc.desc: test power taihe native + * @tc.type: FUNC + * @tc.require: issue#ICAK9Z + */ +HWTEST_F(PowerTaiheNativeTest, PowerTaiheNativeTest_001, TestSize.Level1) +{ + POWER_HILOGI(LABEL_TEST, "PowerTaiheNativeTest_001 start"); + IsActive(); + EXPECT_TRUE(g_pass); + POWER_HILOGI(LABEL_TEST, "PowerTaiheNativeTest_001 end"); +} + +/** + * @tc.name: PowerTaiheNativeTest_002 + * @tc.desc: test power taihe native + * @tc.type: FUNC + * @tc.require: issue#ICAK9Z + */ +HWTEST_F(PowerTaiheNativeTest, PowerTaiheNativeTest_002, TestSize.Level1) +{ + POWER_HILOGI(LABEL_TEST, "PowerTaiheNativeTest_002 start"); + string_view detail = "PowerTaiheNativeTest_002"; + Wakeup(detail); + + g_error = PowerErrors::ERR_FAILURE; + Wakeup(detail); + EXPECT_TRUE(g_errCode != static_cast(PowerErrors::ERR_FAILURE)); + + g_error = PowerErrors::ERR_SYSTEM_API_DENIED; + Wakeup(detail); + EXPECT_TRUE(g_errCode == static_cast(PowerErrors::ERR_SYSTEM_API_DENIED)); + EXPECT_TRUE(g_pass); + POWER_HILOGI(LABEL_TEST, "PowerTaiheNativeTest_002 end"); +} + +/** + * @tc.name: PowerTaiheNativeTest_003 + * @tc.desc: test power taihe native + * @tc.type: FUNC + * @tc.require: issue#ICAK9Z + */ +HWTEST_F(PowerTaiheNativeTest, PowerTaiheNativeTest_003, TestSize.Level1) +{ + POWER_HILOGI(LABEL_TEST, "PowerTaiheNativeTest_003 start"); + bool isImmediate = false; + Suspend(&isImmediate); + + isImmediate = true; + Suspend(&isImmediate); + + g_error = PowerErrors::ERR_FAILURE; + Suspend(&isImmediate); + EXPECT_TRUE(g_errCode != static_cast(PowerErrors::ERR_FAILURE)); + + g_error = PowerErrors::ERR_SYSTEM_API_DENIED; + Suspend(&isImmediate); + EXPECT_TRUE(g_errCode == static_cast(PowerErrors::ERR_SYSTEM_API_DENIED)); + EXPECT_TRUE(g_pass); + POWER_HILOGI(LABEL_TEST, "PowerTaiheNativeTest_003 end"); +} + +/** + * @tc.name: PowerTaiheNativeTest_004 + * @tc.desc: test power taihe native + * @tc.type: FUNC + * @tc.require: issue#ICAK9Z + */ +HWTEST_F(PowerTaiheNativeTest, PowerTaiheNativeTest_004, TestSize.Level1) +{ + POWER_HILOGI(LABEL_TEST, "PowerTaiheNativeTest_004 start"); + GetPowerMode(); + g_mode = PowerMode::POWER_SAVE_MODE; + GetPowerMode(); + g_mode = PowerMode::PERFORMANCE_MODE; + GetPowerMode(); + g_mode = PowerMode::EXTREME_POWER_SAVE_MODE; + GetPowerMode(); + g_mode = static_cast(0); + GetPowerMode(); + EXPECT_TRUE(g_pass); + POWER_HILOGI(LABEL_TEST, "PowerTaiheNativeTest_004 end"); +} + +/** + * @tc.name: PowerTaiheNativeTest_005 + * @tc.desc: test power taihe native + * @tc.type: FUNC + * @tc.require: issue#ICAK9Z + */ +HWTEST_F(PowerTaiheNativeTest, PowerTaiheNativeTest_005, TestSize.Level1) +{ + POWER_HILOGI(LABEL_TEST, "PowerTaiheNativeTest_005 start"); + DevicePowerMode mode(DevicePowerMode::key_t::MODE_NORMAL); + SetPowerModeSync(mode); + + g_error = PowerErrors::ERR_FAILURE; + SetPowerModeSync(mode); + EXPECT_TRUE(g_errCode != static_cast(PowerErrors::ERR_FAILURE)); + + g_error = PowerErrors::ERR_CONNECTION_FAIL; + SetPowerModeSync(mode); + EXPECT_TRUE(g_errCode == static_cast(PowerErrors::ERR_CONNECTION_FAIL)); + EXPECT_TRUE(g_pass); + POWER_HILOGI(LABEL_TEST, "PowerTaiheNativeTest_005 end"); +} + +/** + * @tc.name: PowerTaiheNativeTest_006 + * @tc.desc: test power taihe native + * @tc.type: FUNC + * @tc.require: issue#ICAK9Z + */ +HWTEST_F(PowerTaiheNativeTest, PowerTaiheNativeTest_006, TestSize.Level1) +{ + POWER_HILOGI(LABEL_TEST, "PowerTaiheNativeTest_006 start"); + IsStandby(); + + g_error = PowerErrors::ERR_FAILURE; + IsStandby(); + EXPECT_TRUE(g_errCode != static_cast(PowerErrors::ERR_FAILURE)); + + g_error = PowerErrors::ERR_CONNECTION_FAIL; + IsStandby(); + EXPECT_TRUE(g_errCode == static_cast(PowerErrors::ERR_CONNECTION_FAIL)); + EXPECT_TRUE(g_pass); + POWER_HILOGI(LABEL_TEST, "PowerTaiheNativeTest_006 end"); +} + +/** + * @tc.name: PowerTaiheNativeTest_007 + * @tc.desc: test power taihe native + * @tc.type: FUNC + * @tc.require: issue#ICAK9Z + */ +HWTEST_F(PowerTaiheNativeTest, PowerTaiheNativeTest_007, TestSize.Level1) +{ + POWER_HILOGI(LABEL_TEST, "PowerTaiheNativeTest_007 start"); + bool clearMemory = false; + Hibernate(clearMemory); + + g_error = PowerErrors::ERR_FAILURE; + Hibernate(clearMemory); + EXPECT_TRUE(g_errCode != static_cast(PowerErrors::ERR_FAILURE)); + + clearMemory = true; + g_error = PowerErrors::ERR_SYSTEM_API_DENIED; + Hibernate(clearMemory); + EXPECT_TRUE(g_errCode == static_cast(PowerErrors::ERR_SYSTEM_API_DENIED)); + EXPECT_TRUE(g_pass); + POWER_HILOGI(LABEL_TEST, "PowerTaiheNativeTest_007 end"); +} + +/** + * @tc.name: PowerTaiheNativeTest_008 + * @tc.desc: test power taihe native + * @tc.type: FUNC + * @tc.require: issue#ICAK9Z + */ +HWTEST_F(PowerTaiheNativeTest, PowerTaiheNativeTest_008, TestSize.Level1) +{ + POWER_HILOGI(LABEL_TEST, "PowerTaiheNativeTest_008 start"); + int64_t timeout = 0; + SetScreenOffTime(timeout); + EXPECT_TRUE(g_errCode == static_cast(PowerErrors::ERR_PARAM_INVALID)); + + constexpr int32_t INVALID_TIMEOUT = -2; + SetScreenOffTime(INVALID_TIMEOUT); + EXPECT_TRUE(g_errCode == static_cast(PowerErrors::ERR_PARAM_INVALID)); + + constexpr int32_t RESTORE_DEFAULT_SCREENOFF_TIME = -1; + SetScreenOffTime(RESTORE_DEFAULT_SCREENOFF_TIME); + EXPECT_TRUE(g_errCode == static_cast(PowerErrors::ERR_PARAM_INVALID)); + + timeout = 30000; // ms + SetScreenOffTime(timeout); + + g_error = PowerErrors::ERR_FAILURE; + SetScreenOffTime(timeout); + EXPECT_TRUE(g_errCode != static_cast(PowerErrors::ERR_FAILURE)); + + g_error = PowerErrors::ERR_SYSTEM_API_DENIED; + SetScreenOffTime(timeout); + EXPECT_TRUE(g_errCode == static_cast(PowerErrors::ERR_SYSTEM_API_DENIED)); + EXPECT_TRUE(g_pass); + POWER_HILOGI(LABEL_TEST, "PowerTaiheNativeTest_008 end"); +} +} // namespace + diff --git a/frameworks/ets/taihe/runninglock/BUILD.gn b/frameworks/ets/taihe/runninglock/BUILD.gn new file mode 100644 index 00000000..85ba9b62 --- /dev/null +++ b/frameworks/ets/taihe/runninglock/BUILD.gn @@ -0,0 +1,78 @@ +# Copyright (c) 2025 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. + +import("//build/config/components/ets_frontend/ets2abc_config.gni") +import("//build/ohos.gni") +import("//build/ohos/taihe_idl/taihe.gni") +import("../../../../powermgr.gni") + +copy_taihe_idl("copy_power_manager_runninglock") { + sources = [ "idl/ohos.runningLock.taihe" ] +} + +ohos_taihe("run_taihe") { + taihe_generated_file_path = "${taihe_generated_file_path_runningLock}" + deps = [ ":copy_power_manager_runninglock" ] + outputs = [ + "$taihe_generated_file_path/src/ohos.runningLock.ani.cpp", + "$taihe_generated_file_path/src/ohos.runningLock.abi.c", + ] +} + +taihe_shared_library("power_manager_runninglock_taihe_native") { + branch_protector_ret = "pac_ret" + sanitize = { + cfi = true + cfi_cross_dso = true + debug = false + } + taihe_generated_file_path = "${taihe_generated_file_path_runningLock}" + sources = get_target_outputs(":run_taihe") + include_dirs = [ "./include" ] + configs = [ + "${powermgr_utils_path}:utils_config", + "${powermgr_utils_path}:coverage_flags", + ] + sources += [ + "src/ani_constructor.cpp", + "src/ohos.runningLock.impl.cpp", + ] + deps = [ + ":run_taihe", + "${powermgr_inner_api}:powermgr_client", + ] + external_deps = [ + "c_utils:utils", + "hilog:libhilog", + "ipc:ipc_core", + ] + + subsystem_name = "powermgr" + part_name = "${powermgr_part_name}" +} + +generate_static_abc("runningLock_abc") { + base_url = "${taihe_generated_file_path_runningLock}" + files = [ "${taihe_generated_file_path_runningLock}/@ohos.runningLock.ets" ] + is_boot_abc = "True" + device_dst_file = "/system/framework/runningLock_abc.abc" + dependencies = [ ":run_taihe" ] +} + +ohos_prebuilt_etc("runningLock_etc") { + source = "$target_out_dir/runningLock_abc.abc" + module_install_dir = "framework" + subsystem_name = "powermgr" + part_name = "${powermgr_part_name}" + deps = [ ":runningLock_abc" ] +} diff --git a/frameworks/ets/taihe/runninglock/idl/ohos.runningLock.taihe b/frameworks/ets/taihe/runninglock/idl/ohos.runningLock.taihe new file mode 100644 index 00000000..8ea02223 --- /dev/null +++ b/frameworks/ets/taihe/runninglock/idl/ohos.runningLock.taihe @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2025 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. + */ + +@!namespace("@ohos.runningLock", "runningLock") + +@!sts_inject(""" +static { loadLibrary("power_manager_runninglock_taihe_native.z") }""") + +enum RunningLockType: i32 { PROXIMITY_SCREEN_CONTROL = 2 } + +@class +interface RunningLock { + Hold(timeout: i32): void; + IsHolding(): bool; + Unhold(): void; +} + +@gen_async("create") +@gen_promise("create") +function CreateSync(name: String, type: RunningLockType): RunningLock; + +function IsSupported(type: RunningLockType): bool; \ No newline at end of file diff --git a/frameworks/ets/taihe/runninglock/src/ani_constructor.cpp b/frameworks/ets/taihe/runninglock/src/ani_constructor.cpp new file mode 100644 index 00000000..cf17df9f --- /dev/null +++ b/frameworks/ets/taihe/runninglock/src/ani_constructor.cpp @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2025 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 "ohos.runningLock.ani.hpp" +#include "power_log.h" +using namespace OHOS::PowerMgr; +ANI_EXPORT ani_status ANI_Constructor(ani_vm *vm, uint32_t *result) +{ + ani_env *env; + if (ANI_OK != vm->GetEnv(ANI_VERSION_1, &env)) { + POWER_HILOGE(COMP_FWK, "Error from GetEnv"); + return ANI_ERROR; + } + if (ANI_OK != ohos::runningLock::ANIRegister(env)) { + POWER_HILOGE(COMP_FWK, "Error from ohos::runningLock::ANIRegister"); + return ANI_ERROR; + } + *result = ANI_VERSION_1; + return ANI_OK; +} diff --git a/frameworks/ets/taihe/runninglock/src/ohos.runningLock.impl.cpp b/frameworks/ets/taihe/runninglock/src/ohos.runningLock.impl.cpp new file mode 100644 index 00000000..3147f071 --- /dev/null +++ b/frameworks/ets/taihe/runninglock/src/ohos.runningLock.impl.cpp @@ -0,0 +1,113 @@ +/* + * Copyright (c) 2025 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 "ohos.runningLock.proj.hpp" +#include "taihe/runtime.hpp" +#include "stdexcept" +#include "power_mgr_client.h" +#include "power_log.h" +#include "power_mgr_errors.h" +#include "power_errors.h" +#include "ohos.runningLock.impl.hpp" + +using namespace taihe; +using namespace ohos::runningLock; +using namespace OHOS::PowerMgr; + +namespace { +std::map g_errorTable = { + {PowerErrors::ERR_CONNECTION_FAIL, "Failed to connect to the service."}, + {PowerErrors::ERR_PERMISSION_DENIED, "Permission is denied" }, + {PowerErrors::ERR_SYSTEM_API_DENIED, "System permission is denied" }, + {PowerErrors::ERR_PARAM_INVALID, "Invalid input parameter." } +}; + +class RunningLockImpl { +public: + RunningLockImpl() {} + + explicit RunningLockImpl(std::shared_ptr runLock) + { + runningLock_ = runLock; + } + + void Hold(int32_t timeout) + { + POWER_HILOGD(FEATURE_RUNNING_LOCK, "ets Hold interface"); + if (runningLock_ == nullptr) { + POWER_HILOGE(FEATURE_RUNNING_LOCK, "runningLock_ is nullptr"); + return; + } + OHOS::ErrCode code = runningLock_->Lock(timeout); + if (code == E_PERMISSION_DENIED) { + taihe::set_business_error(static_cast(PowerErrors::ERR_PERMISSION_DENIED), + g_errorTable[PowerErrors::ERR_PERMISSION_DENIED]); + } + } + + bool IsHolding() + { + POWER_HILOGD(FEATURE_RUNNING_LOCK, "ets IsHolding interface"); + if (runningLock_ == nullptr) { + POWER_HILOGE(FEATURE_RUNNING_LOCK, "runningLock_ is nullptr"); + return false; + } + bool isUsed = runningLock_->IsUsed(); + return isUsed; + } + + void Unhold() + { + POWER_HILOGD(FEATURE_RUNNING_LOCK, "ets Unhold interface"); + if (runningLock_ == nullptr) { + POWER_HILOGE(FEATURE_RUNNING_LOCK, "runningLock_ is nullptr"); + return; + } + OHOS::ErrCode code = runningLock_->UnLock(); + if (code == E_PERMISSION_DENIED) { + taihe::set_business_error(static_cast(PowerErrors::ERR_PERMISSION_DENIED), + g_errorTable[PowerErrors::ERR_PERMISSION_DENIED]); + } + } + +private: + std::shared_ptr runningLock_ = nullptr; +}; + +ohos::runningLock::RunningLock CreateSync(string_view name, ohos::runningLock::RunningLockType type) +{ + std::shared_ptr runLock = nullptr; + OHOS::PowerMgr::RunningLockType tp = static_cast(type.get_value()); + runLock = PowerMgrClient::GetInstance().CreateRunningLock(std::string(name), tp); + PowerErrors code = PowerMgrClient::GetInstance().GetError(); + if (code != PowerErrors::ERR_OK && code != PowerErrors::ERR_FAILURE) { + taihe::set_business_error(static_cast(code), g_errorTable[code]); + } + return make_holder(runLock); +} + +bool IsSupported(ohos::runningLock::RunningLockType type) +{ + OHOS::PowerMgr::RunningLockType tp = static_cast(type.get_value()); + return tp == OHOS::PowerMgr::RunningLockType::RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL; +} +} // namespace + +// Since these macros are auto-generate, lint will cause false positive +// NOLINTBEGIN +TH_EXPORT_CPP_API_CreateSync(CreateSync); +TH_EXPORT_CPP_API_IsSupported(IsSupported); +// NOLINTEND \ No newline at end of file diff --git a/frameworks/ets/taihe/runninglock/test/unittest/BUILD.gn b/frameworks/ets/taihe/runninglock/test/unittest/BUILD.gn new file mode 100644 index 00000000..1367779d --- /dev/null +++ b/frameworks/ets/taihe/runninglock/test/unittest/BUILD.gn @@ -0,0 +1,57 @@ +# Copyright (c) 2025 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. + +import("//build/test.gni") +import("//build/ohos/taihe_idl/taihe.gni") +import("../../../../../../powermgr.gni") + +module_output_path = "power_manager/power_manager" + +config("module_private_config") { + visibility = [ ":*" ] + + include_dirs = [ + "${taihe_generated_file_path_runningLock}/include", + "${taihe_toolchain_path}/include" + ] +} + +ohos_unittest("runninglock_taihe_native_test") { + module_out_path = module_output_path + + sources = [ + "running_lock_ani_constructor_test.cpp", + "running_lock_taihe_native_test.cpp", + "../../src/ohos.runningLock.impl.cpp" + ] + + configs = [ + ":module_private_config", + "${powermgr_utils_path}:coverage_flags", + ] + + deps = [ + "${powermgr_framework}/ets/taihe/runninglock:power_manager_runninglock_taihe_native", + "${powermgr_framework}/ets/taihe/runninglock:run_taihe", + "${powermgr_inner_api}:powermgr_client", + "${powermgr_utils_path}:power_utils", + ] + + external_deps = [ + "c_utils:utils", + "googletest:gtest_main", + "hilog:libhilog", + "ipc:ipc_core", + "runtime_core:ani", + ] +} \ No newline at end of file diff --git a/frameworks/ets/taihe/runninglock/test/unittest/running_lock_ani_constructor_test.cpp b/frameworks/ets/taihe/runninglock/test/unittest/running_lock_ani_constructor_test.cpp new file mode 100644 index 00000000..dcf3f83f --- /dev/null +++ b/frameworks/ets/taihe/runninglock/test/unittest/running_lock_ani_constructor_test.cpp @@ -0,0 +1,113 @@ +/* + * Copyright (c) 2025 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 "ohos.runningLock.ani.hpp" +#include "power_log.h" + +using namespace OHOS::PowerMgr; + +using namespace testing; +using namespace testing::ext; +namespace { +ani_status g_status1 = ANI_OK; +ani_status g_status2 = ANI_OK; +} + +namespace ohos::runningLock { +ani_status ANIRegister(ani_env *env) +{ + return g_status1; +} +} + +namespace { +class RunningLockAniConstructorTest : public ::testing::Test { +public: + static void SetUpTestCase() {} + static void TearDownTestCase() {} + void SetUp() {} + void TearDown() {} +}; + +static ani_status GetEnv(ani_vm *vm, uint32_t version, ani_env **result) +{ + return g_status2; +} +/** + * @tc.name: RunningLockAniConstructorTest_001 + * @tc.desc: test AniConstructor + * @tc.type: FUNC + * @tc.require: issue#ICAK9Z + */ +HWTEST_F(RunningLockAniConstructorTest, RunningLockAniConstructorTest_001, TestSize.Level1) +{ + POWER_HILOGI(LABEL_TEST, "RunningLockAniConstructorTest_001 start"); + ani_vm vm; + __ani_vm_api mock_c_api = {.GetEnv = GetEnv}; + vm.c_api = &mock_c_api; + uint32_t result = 0; + + ani_status status = ANI_Constructor(&vm, &result); + + EXPECT_EQ(status, ANI_OK); + EXPECT_EQ(result, ANI_VERSION_1); + POWER_HILOGI(LABEL_TEST, "RunningLockAniConstructorTest_001 end"); +} + +/** + * @tc.name: RunningLockAniConstructorTest_002 + * @tc.desc: test AniConstructor + * @tc.type: FUNC + * @tc.require: issue#ICAK9Z + */ +HWTEST_F(RunningLockAniConstructorTest, RunningLockAniConstructorTest_002, TestSize.Level1) +{ + POWER_HILOGI(LABEL_TEST, "RunningLockAniConstructorTest_002 start"); + ani_vm vm; + __ani_vm_api mock_c_api = {.GetEnv = GetEnv}; + vm.c_api = &mock_c_api; + uint32_t result = 0; + + g_status1 = ANI_ERROR; + ani_status status = ANI_Constructor(&vm, &result); + + EXPECT_EQ(status, ANI_ERROR); + POWER_HILOGI(LABEL_TEST, "RunningLockAniConstructorTest_002 end"); +} + +/** + * @tc.name: RunningLockAniConstructorTest_003 + * @tc.desc: test AniConstructor + * @tc.type: FUNC + * @tc.require: issue#ICAK9Z + */ +HWTEST_F(RunningLockAniConstructorTest, RunningLockAniConstructorTest_003, TestSize.Level1) +{ + POWER_HILOGI(LABEL_TEST, "RunningLockAniConstructorTest_003 start"); + ani_vm vm; + __ani_vm_api mock_c_api = {.GetEnv = GetEnv}; + vm.c_api = &mock_c_api; + uint32_t result = 0; + + g_status1 = ANI_OK; + g_status2 = ANI_ERROR; + ani_status status = ANI_Constructor(&vm, &result); + + EXPECT_EQ(status, ANI_ERROR); + POWER_HILOGI(LABEL_TEST, "RunningLockAniConstructorTest_003 end"); +} +} \ No newline at end of file diff --git a/frameworks/ets/taihe/runninglock/test/unittest/running_lock_taihe_native_test.cpp b/frameworks/ets/taihe/runninglock/test/unittest/running_lock_taihe_native_test.cpp new file mode 100644 index 00000000..bafcf105 --- /dev/null +++ b/frameworks/ets/taihe/runninglock/test/unittest/running_lock_taihe_native_test.cpp @@ -0,0 +1,166 @@ +/* + * Copyright (c) 2025 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 "ohos.runningLock.proj.hpp" +#include "ohos.runningLock.impl.hpp" +#include "ohos.runningLock.user.hpp" +#include "taihe/runtime.hpp" +#include "power_mgr_errors.h" +#include "power_mgr_client.h" +#include "power_errors.h" +#include "power_log.h" + +using namespace taihe; +using namespace ohos::runningLock; +using namespace OHOS::PowerMgr; + +using namespace testing; +using namespace testing::ext; + +namespace { +PowerErrors g_error = PowerErrors::ERR_OK; +std::shared_ptr g_runningLock {nullptr}; +bool g_pass = false; +int g_lockReturn = OHOS::ERR_OK; +const int32_t TIMEOUT = -1; +} + +namespace taihe { +void set_business_error(int32_t err_code, taihe::string_view msg) +{ + (void)err_code; + (void)msg; +} +} + +namespace OHOS::PowerMgr { +std::shared_ptr PowerMgrClient::CreateRunningLock(const std::string& name, RunningLockType type) +{ + g_pass = true; + return g_runningLock; +} + +PowerErrors PowerMgrClient::GetError() +{ + return g_error; +} + +ErrCode RunningLock::Lock(int32_t timeOutMs) +{ + return g_lockReturn; +} + +ErrCode RunningLock::UnLock() +{ + return g_lockReturn; +} + +bool RunningLock::IsUsed() +{ + if (g_lockReturn == OHOS::PowerMgr::E_PERMISSION_DENIED) { + return false; + } + return true; +} +} // namespace OHOS::PowerMgr + +namespace { +class RunningLockTaiheNativeTest : public ::testing::Test { +public: + static void SetUpTestCase() {} + static void TearDownTestCase() {} + void SetUp() {} + void TearDown() + { + g_error = PowerErrors::ERR_OK; + g_runningLock = nullptr; + g_pass = false; + } +}; + +/** + * @tc.name: RunningLockTaiheNativeTest_001 + * @tc.desc: test running lock taihe native + * @tc.type: FUNC + * @tc.require: issue#ICAK9Z + */ +HWTEST_F(RunningLockTaiheNativeTest, RunningLockTaiheNativeTest_001, TestSize.Level1) +{ + POWER_HILOGI(LABEL_TEST, "RunningLockTaiheNativeTest_001 start"); + const string_view name = "RunningLockTaiheNativeTest_001"; + const ohos::runningLock::RunningLockType type( + ohos::runningLock::RunningLockType::key_t::PROXIMITY_SCREEN_CONTROL); + ohos::runningLock::RunningLock lock = CreateSync(name, type); + EXPECT_FALSE(lock.is_error()); + lock->Hold(TIMEOUT); + bool isHolding = lock->IsHolding(); + EXPECT_FALSE(isHolding); + lock->Unhold(); + + g_error = PowerErrors::ERR_FAILURE; + CreateSync(name, type); + + g_error = PowerErrors::ERR_PERMISSION_DENIED; + CreateSync(name, type); + EXPECT_TRUE(g_pass); + POWER_HILOGI(LABEL_TEST, "RunningLockTaiheNativeTest_001 end"); +} + +/** + * @tc.name: RunningLockTaiheNativeTest_002 + * @tc.desc: test running lock taihe native + * @tc.type: FUNC + * @tc.require: issue#ICAK9Z + */ +HWTEST_F(RunningLockTaiheNativeTest, RunningLockTaiheNativeTest_002, TestSize.Level1) +{ + POWER_HILOGI(LABEL_TEST, "RunningLockTaiheNativeTest_002 start"); + const string_view name = "RunningLockTaiheNativeTest_002"; + const ohos::runningLock::RunningLockType type( + ohos::runningLock::RunningLockType::key_t::PROXIMITY_SCREEN_CONTROL); + g_runningLock = std::make_shared( + nullptr, std::string(name), OHOS::PowerMgr::RunningLockType::RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL); + ohos::runningLock::RunningLock lock = CreateSync(name, type); + EXPECT_FALSE(lock.is_error()); + lock->Hold(TIMEOUT); + bool isHolding = lock->IsHolding(); + EXPECT_TRUE(isHolding); + lock->Unhold(); + + g_lockReturn = E_PERMISSION_DENIED; + lock->Hold(TIMEOUT); + isHolding = lock->IsHolding(); + EXPECT_FALSE(isHolding); + lock->Unhold(); + EXPECT_TRUE(g_pass); + POWER_HILOGI(LABEL_TEST, "RunningLockTaiheNativeTest_002 end"); +} + +/** + * @tc.name: RunningLockTaiheNativeTest_003 + * @tc.desc: test running lock taihe native + * @tc.type: FUNC + * @tc.require: issue#ICAK9Z + */ +HWTEST_F(RunningLockTaiheNativeTest, RunningLockTaiheNativeTest_003, TestSize.Level1) +{ + POWER_HILOGI(LABEL_TEST, "RunningLockTaiheNativeTest_003 start"); + ohos::runningLock::RunningLockType type(ohos::runningLock::RunningLockType::key_t::PROXIMITY_SCREEN_CONTROL); + EXPECT_TRUE(IsSupported(type)); + POWER_HILOGI(LABEL_TEST, "RunningLockTaiheNativeTest_003 end"); +} +} // namespace \ No newline at end of file diff --git a/powermgr.gni b/powermgr.gni index 61a3dd86..f79802c6 100644 --- a/powermgr.gni +++ b/powermgr.gni @@ -146,3 +146,5 @@ powermgr_inner_api = "${powermgr_root_path}/interfaces/inner_api" powermgr_utils_path = "${powermgr_root_path}/utils" system_type = "default" +taihe_generated_file_path_power = "${root_out_dir}/taihe/out/powermgr/power_manager/power" +taihe_generated_file_path_runningLock = "${root_out_dir}/taihe/out/powermgr/power_manager/runningLock" \ No newline at end of file -- Gitee