From 00d3d733ce8c578f041158a58ff7ec1e9da34cac Mon Sep 17 00:00:00 2001 From: cjw123qq Date: Thu, 3 Jul 2025 14:10:07 +0800 Subject: [PATCH] fix: add coverage code for power taihe Signed-off-by: cjw123qq --- bundle.json | 3 +- frameworks/ets/taihe/BUILD.gn | 8 + frameworks/ets/taihe/power/BUILD.gn | 14 +- .../ets/taihe/power/src/ohos.power.impl.cpp | 22 +- .../ets/taihe/power/test/unittest/BUILD.gn | 65 ++++ .../unittest/power_ani_constructor_test.cpp | 113 +++++++ .../test/unittest/power_taihe_native_test.cpp | 316 ++++++++++++++++++ frameworks/ets/taihe/runninglock/BUILD.gn | 14 +- .../runninglock/src/ohos.runningLock.impl.cpp | 2 +- .../taihe/runninglock/test/unittest/BUILD.gn | 54 +++ .../running_lock_ani_constructor_test.cpp | 113 +++++++ .../running_lock_taihe_native_test.cpp | 166 +++++++++ powermgr.gni | 2 + 13 files changed, 862 insertions(+), 30 deletions(-) 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/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 5be7c475..41bdb4f9 100644 --- a/bundle.json +++ b/bundle.json @@ -222,7 +222,8 @@ "//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 index 82c6a0b1..b690cbd6 100644 --- a/frameworks/ets/taihe/BUILD.gn +++ b/frameworks/ets/taihe/BUILD.gn @@ -18,4 +18,12 @@ group("power_manager_taihe") { "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 index 4efed390..41db5592 100644 --- a/frameworks/ets/taihe/power/BUILD.gn +++ b/frameworks/ets/taihe/power/BUILD.gn @@ -20,11 +20,6 @@ copy_taihe_idl("copy_power_manager") { sources = [ "idl/ohos.power.taihe" ] } -subsystem_name = "powermgr" -part_name = "power_manager" -taihe_generated_file_path_power = - "$taihe_file_path/out/$subsystem_name/$part_name/power" - ohos_taihe("run_taihe") { taihe_generated_file_path = "${taihe_generated_file_path_power}" deps = [ ":copy_power_manager" ] @@ -42,8 +37,6 @@ taihe_shared_library("power_manager_taihe_native") { debug = false } taihe_generated_file_path = "${taihe_generated_file_path_power}" - part_name = "$part_name" - subsystem_name = "$subsystem_name" sources = get_target_outputs(":run_taihe") include_dirs = [ "./include" ] configs = [ @@ -67,6 +60,9 @@ taihe_shared_library("power_manager_taihe_native") { if (has_ability_runtime_part) { external_deps += [ "ability_runtime:app_manager" ] } + + subsystem_name = "powermgr" + part_name = "${powermgr_part_name}" } generate_static_abc("power_abc") { @@ -90,7 +86,7 @@ generate_static_abc("power_test") { ohos_prebuilt_etc("power_abc_etc") { source = "$target_out_dir/power_abc.abc" module_install_dir = "framework" - part_name = "$part_name" - subsystem_name = "$subsystem_name" + subsystem_name = "powermgr" + part_name = "${powermgr_part_name}" deps = [ ":power_abc" ] } diff --git a/frameworks/ets/taihe/power/src/ohos.power.impl.cpp b/frameworks/ets/taihe/power/src/ohos.power.impl.cpp index bdd3eb2b..d5f69281 100644 --- a/frameworks/ets/taihe/power/src/ohos.power.impl.cpp +++ b/frameworks/ets/taihe/power/src/ohos.power.impl.cpp @@ -69,7 +69,7 @@ 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) { + if (code != PowerErrors::ERR_OK && code != PowerErrors::ERR_FAILURE) { taihe::set_business_error(static_cast(code), errorTable[code]); } } @@ -79,7 +79,7 @@ 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) { + if (code != PowerErrors::ERR_OK && code != PowerErrors::ERR_FAILURE) { taihe::set_business_error(static_cast(code), errorTable[code]); } } @@ -95,7 +95,7 @@ void Wakeup(string_view detail) 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) { + if (code != PowerErrors::ERR_OK && code != PowerErrors::ERR_FAILURE) { taihe::set_business_error(static_cast(code), errorTable[code]); } } @@ -112,7 +112,7 @@ void Suspend(optional_view isImmediate) code = g_powerMgrClient.SuspendDevice( SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false, std::to_string(apiVersion)); } - if (code != PowerErrors::ERR_OK) { + 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), errorTable[code]); } @@ -144,7 +144,7 @@ DevicePowerMode GetPowerMode() void SetPowerModeSync(DevicePowerMode mode) { PowerErrors code = g_powerMgrClient.SetDeviceMode(PowerMode(static_cast(mode))); - if (code != PowerErrors::ERR_OK) { + if (code != PowerErrors::ERR_OK && code != PowerErrors::ERR_FAILURE) { taihe::set_business_error(static_cast(code), errorTable[code]); } } @@ -153,10 +153,9 @@ bool IsStandby() { bool isStandby = false; PowerErrors code = g_powerMgrClient.IsStandby(isStandby); - if (code == PowerErrors::ERR_OK) { - return isStandby; + if (code != PowerErrors::ERR_OK && code != PowerErrors::ERR_FAILURE) { + taihe::set_business_error(static_cast(code), errorTable[code]); } - taihe::set_business_error(static_cast(code), errorTable[code]); return isStandby; } @@ -170,7 +169,7 @@ void Hibernate(bool clearMemory) } } -void SetScreenOffTime(int32_t timeout) +void SetScreenOffTime(int64_t timeout) { if (timeout == 0 || (timeout < 0 && timeout != RESTORE_DEFAULT_SCREENOFF_TIME)) { POWER_HILOGE(FEATURE_WAKEUP, "timeout is not right."); @@ -185,13 +184,15 @@ void SetScreenOffTime(int32_t timeout) } else { code = g_powerMgrClient.OverrideScreenOffTime(timeout, std::to_string(apiVersion)); } - if (code != PowerErrors::ERR_OK) { + if (code != PowerErrors::ERR_OK && code != PowerErrors::ERR_FAILURE) { POWER_HILOGE(FEATURE_WAKEUP, "SetScreenOffTime failed."); taihe::set_business_error(static_cast(code), 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); @@ -202,3 +203,4 @@ 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..e86d0087 --- /dev/null +++ b/frameworks/ets/taihe/power/test/unittest/BUILD.gn @@ -0,0 +1,65 @@ +# 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 = [ + "googletest:gtest_main", + "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..5e591f90 --- /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; + + 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; + + 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; + + 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..cc4824da --- /dev/null +++ b/frameworks/ets/taihe/power/test/unittest/power_taihe_native_test.cpp @@ -0,0 +1,316 @@ +/* + * 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& 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() {} + 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)); + + const int32_t INVALID_TIMEOUT = -2; + SetScreenOffTime(INVALID_TIMEOUT); + EXPECT_TRUE(g_errCode == static_cast(PowerErrors::ERR_PARAM_INVALID)); + + const 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 index 303916b9..28a09e57 100644 --- a/frameworks/ets/taihe/runninglock/BUILD.gn +++ b/frameworks/ets/taihe/runninglock/BUILD.gn @@ -20,11 +20,6 @@ copy_taihe_idl("copy_power_manager_runninglock") { sources = [ "idl/ohos.runningLock.taihe" ] } -subsystem_name = "powermgr" -part_name = "power_manager" -taihe_generated_file_path_runningLock = - "$taihe_file_path/out/$subsystem_name/$part_name/runningLock" - ohos_taihe("run_taihe") { taihe_generated_file_path = "${taihe_generated_file_path_runningLock}" deps = [ ":copy_power_manager_runninglock" ] @@ -42,8 +37,6 @@ taihe_shared_library("power_manager_runninglock_taihe_native") { debug = false } taihe_generated_file_path = "${taihe_generated_file_path_runningLock}" - part_name = "$part_name" - subsystem_name = "$subsystem_name" sources = get_target_outputs(":run_taihe") include_dirs = [ "./include" ] configs = [ @@ -59,6 +52,9 @@ taihe_shared_library("power_manager_runninglock_taihe_native") { "${powermgr_inner_api}:powermgr_client", ] external_deps = [ "hilog:libhilog" ] + + subsystem_name = "powermgr" + part_name = "${powermgr_part_name}" } generate_static_abc("runningLock_abc") { @@ -79,7 +75,7 @@ generate_static_abc("runningLock_test_abc") { ohos_prebuilt_etc("runningLock_etc") { source = "$target_out_dir/runningLock_abc.abc" module_install_dir = "framework" - part_name = "$part_name" - subsystem_name = "$subsystem_name" + subsystem_name = "powermgr" + part_name = "${powermgr_part_name}" deps = [ ":runningLock_abc" ] } diff --git a/frameworks/ets/taihe/runninglock/src/ohos.runningLock.impl.cpp b/frameworks/ets/taihe/runninglock/src/ohos.runningLock.impl.cpp index d113fbd7..4912b3f9 100644 --- a/frameworks/ets/taihe/runninglock/src/ohos.runningLock.impl.cpp +++ b/frameworks/ets/taihe/runninglock/src/ohos.runningLock.impl.cpp @@ -93,7 +93,7 @@ ohos::runningLock::RunningLock CreateSync(string_view name, ohos::runningLock::R 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 && errorTable.find(code) != errorTable.end()) { + if (code != PowerErrors::ERR_OK && code != PowerErrors::ERR_FAILURE) { taihe::set_business_error(static_cast(code), errorTable[code]); } return make_holder(runLock); 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..32188dfa --- /dev/null +++ b/frameworks/ets/taihe/runninglock/test/unittest/BUILD.gn @@ -0,0 +1,54 @@ +# 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 = [ + "googletest:gtest_main", + "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..e3c28568 --- /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; + + 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; + + 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; + + 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 374bd188..3e0b6620 100644 --- a/powermgr.gni +++ b/powermgr.gni @@ -135,3 +135,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" -- Gitee