diff --git a/frameworks/include/power_manager_napi.h b/frameworks/include/power_manager_napi.h new file mode 100644 index 0000000000000000000000000000000000000000..7b787694364da41ee24974f02bb9035eb400566c --- /dev/null +++ b/frameworks/include/power_manager_napi.h @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2021 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. + */ + +#ifndef POWERMGR_POWER_MANAGER_NAPI_H +#define POWERMGR_POWER_MANAGER_NAPI_H + +#include "napi/native_api.h" +#include "napi/native_node_api.h" +#include "power_mgr_client.h" +#include "power_napi_context.h" + +namespace OHOS { +namespace PowerMgr { +class ShutdownContext : public PowerNapiContext { +public: + using PowerNapiContext::PowerNapiContext; + const std::string reason_; +protected: + virtual void Init(napi_value* args, uint32_t argc) override; +}; + +class PowerManagerNapi { +public: + static napi_value Init(napi_env env, napi_value exports); +private: + PowerManagerNapi() = default; + ~PowerManagerNapi() = default; + static napi_value IsRunningLockTypeSupported(napi_env env, napi_callback_info info); + static napi_value CreateRunningLock(napi_env env, napi_callback_info info); + static napi_value ShutdownDevice(napi_env env, napi_callback_info info); + static napi_value RebootDevice(napi_env env, napi_callback_info info); + static napi_value IsScreenOn(napi_env env, napi_callback_info info); + static napi_value GetState(napi_env env, napi_callback_info info); + static napi_value GetMode(napi_env env, napi_callback_info info); + static napi_value InitRunningLockType(napi_env env, napi_value exports); + static napi_value InitPowerState(napi_env env, napi_value exports); + static napi_value InitPowerMode(napi_env env, napi_value exports); + static napi_value EnumRunningLockTypeConstructor(napi_env env, napi_callback_info info); + static napi_value EnumPowerStateConstructor(napi_env env, napi_callback_info info); + static napi_value EnumPowerModeConstructor(napi_env env, napi_callback_info info); + + static napi_ref runningLockTypeConstructor_; + static napi_ref powerStateConstructor_; + static napi_ref powerModeConstructor_; +}; +} // namespace PowerMgr +} // namespace OHOS + +#endif // POWERMGR_POWER_MANAGER_NAPI_H diff --git a/frameworks/include/power_napi_context.h b/frameworks/include/power_napi_context.h new file mode 100644 index 0000000000000000000000000000000000000000..d6ff102cdf3ff2d696dbafffca8d9c8444b7217a --- /dev/null +++ b/frameworks/include/power_napi_context.h @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2021 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. + */ + +#ifndef POWERMGR_POWER_NAPI_BASE_H +#define POWERMGR_POWER_NAPI_BASE_H + +#include "napi/native_api.h" +#include "napi/native_node_api.h" +#include "power_mgr_client.h" + +namespace OHOS { +namespace PowerMgr { +enum AsyncStatus { + PENDING, + RESOLVED, + REJECTED, +}; + +class PowerNapiContext { +using FreeFunc = std::function; +using ExecuteFunc = std::function; + +public: + PowerNapiContext(napi_env env, napi_value* args, uint32_t argc, + int32_t callbackArg, napi_value object = nullptr); + virtual ~PowerNapiContext(); + bool StartAsyncWork(const char* workName, ExecuteFunc exeFunc, FreeFunc freeFunc); + napi_value GetPromise() + { + return promise_; + } + napi_env env_; + napi_value outValue_; + napi_value outError_; +protected: + static void ExecuteAsyncWork(napi_env env, void *data); + static void CompleteAsyncWork(napi_env env, napi_status status, void *data); + + virtual void Init(napi_value* args, uint32_t argc); + virtual napi_value GetValue(); // default function is for Promise + virtual napi_value GetError(); // default function is always return undefined + + uint32_t cbParamCount_; + napi_value object_; + napi_deferred deferred_; + napi_ref callbackRef_; + napi_async_work asyncWork_; + napi_value promise_; + ExecuteFunc exeFunc_; + FreeFunc freeFunc_; + AsyncStatus status_; +}; +} // namespace PowerMgr +} // namespace OHOS + +#endif // POWERMGR_POWER_NAPI_BASE_H diff --git a/frameworks/include/running_lock_napi.h b/frameworks/include/running_lock_napi.h new file mode 100644 index 0000000000000000000000000000000000000000..b50f7f057634a78c8e84b8f46a93f5bdefb6b32c --- /dev/null +++ b/frameworks/include/running_lock_napi.h @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2021 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. + */ + +#ifndef POWERMGR_RUNNING_LOCK_NAPI_H +#define POWERMGR_RUNNING_LOCK_NAPI_H + +#include "napi/native_api.h" +#include "napi/native_node_api.h" +#include "power_mgr_client.h" +#include "power_napi_context.h" + +namespace OHOS { +namespace PowerMgr { +class RunningLockWrapper { +public: + RunningLockWrapper(std::shared_ptr lock) : lock_ptr(lock) {} + ~RunningLockWrapper() = default; + std::shared_ptr lock_ptr; +}; + +class RunningLockNapi { +public: + static napi_value InitRunningLockClass(napi_env env, napi_value exports); + static bool CreateRunningLock(napi_env env, napi_callback_info info, + napi_value* result, napi_value* error); +private: + RunningLockNapi() = default; + ~RunningLockNapi() = default; + static napi_value Lock(napi_env env, napi_callback_info info); + static napi_value IsUsed(napi_env env, napi_callback_info info); + static napi_value Unlock(napi_env env, napi_callback_info info); + + static void RemoveRunningLock(napi_env env, void* finalize_data, void* finalize_hint); + static napi_value ConstructRunningLockObject(napi_env env, napi_callback_info info); + static bool GetNativeLock(napi_env env, napi_value object, RunningLockWrapper** lock); + + static napi_ref runningLockConstructor_; +}; +} // namespace PowerMgr +} // namespace OHOS + +#endif // POWERMGR_RUNNING_LOCK_NAPI_H diff --git a/interfaces/kits/js/napi/power/BUILD.gn b/frameworks/napi/power/BUILD.gn similarity index 100% rename from interfaces/kits/js/napi/power/BUILD.gn rename to frameworks/napi/power/BUILD.gn diff --git a/interfaces/kits/js/napi/power/power.cpp b/frameworks/napi/power/power.cpp similarity index 100% rename from interfaces/kits/js/napi/power/power.cpp rename to frameworks/napi/power/power.cpp diff --git a/frameworks/napi/power_manager_napi.cpp b/frameworks/napi/power_manager_napi.cpp new file mode 100644 index 0000000000000000000000000000000000000000..67da77a23004996a8fe142cf23c6c98e27dbd588 --- /dev/null +++ b/frameworks/napi/power_manager_napi.cpp @@ -0,0 +1,418 @@ +/* + * Copyright (c) 2021 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 "power_manager_napi.h" + +#include +#include +#include +#include +#include "power_common.h" +#include "running_lock_napi.h" + +using namespace OHOS::PowerMgr; + +static const uint8_t ARG_0 = 0; +static const uint8_t ARG_1 = 1; +static const uint8_t ARG_2 = 2; +static const uint8_t ARG_3 = 3; +static const uint8_t REASON_STRING_MAX = 128; +static const uint8_t LOCK_NAME_STRING_MAX = 128; + +napi_ref PowerManagerNapi::runningLockTypeConstructor_ = nullptr; +napi_ref PowerManagerNapi::powerStateConstructor_ = nullptr; +napi_ref PowerManagerNapi::powerModeConstructor_ = nullptr; + +void ShutdownContext::Init(napi_value* args, uint32_t argc) +{ + if (argc < 1) { + return; + } + size_t out; + char reason[REASON_STRING_MAX]; + napi_get_value_string_latin1(env_, args[ARG_0], reason, REASON_STRING_MAX, &out); + reason_.copy(reason, strlen(reason), 0); +} + +napi_value PowerManagerNapi::Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = { + DECLARE_NAPI_STATIC_FUNCTION("isRunningLockTypeSupported", IsRunningLockTypeSupported), + DECLARE_NAPI_STATIC_FUNCTION("createRunningLock", CreateRunningLock), + DECLARE_NAPI_STATIC_FUNCTION("shutownDevice", ShutdownDevice), + DECLARE_NAPI_STATIC_FUNCTION("rebootDevice", RebootDevice), + DECLARE_NAPI_STATIC_FUNCTION("isScreenOn", IsScreenOn), + DECLARE_NAPI_STATIC_FUNCTION("getState", GetState), + DECLARE_NAPI_STATIC_FUNCTION("getMode", GetMode), + }; + NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc)); + + RunningLockNapi::InitRunningLockClass(env, exports); + PowerManagerNapi::InitRunningLockType(env, exports); + PowerManagerNapi::InitPowerState(env, exports); + PowerManagerNapi::InitPowerMode(env, exports); + + return exports; +} + +napi_value PowerManagerNapi::IsRunningLockTypeSupported(napi_env env, napi_callback_info info) +{ + POWER_HILOGD(MODULE_JS_NAPI, "enter"); + size_t argc = ARG_2; + napi_value args[ARG_2] = { 0 }; + napi_value jsthis; + void *data = nullptr; + uint32_t result; + + napi_status status = napi_get_cb_info(env, info, &argc, args, &jsthis, &data); + NAPI_ASSERT(env, (status == napi_ok) && (argc >= ARG_2 - 1), "Not enough parameters"); + + napi_valuetype type; + NAPI_CALL(env, napi_typeof(env, args[ARG_0], &type)); + NAPI_ASSERT(env, type == napi_number, "Wrong type of 1st parameter. number expected."); + + napi_get_value_uint32(env, args[ARG_0], &result); + + PowerNapiContext* context = new PowerNapiContext(env, args, argc, ARG_2 - 1, ARG_1); + + napi_value promise = context->GetPromise(); + context->StartAsyncWork("Power::IsRunningLockTypeSupported", + [context, result] { + bool ret = PowerMgrClient::GetInstance().IsRunningLockTypeSupported(result); + napi_get_boolean(context->env_, ret, &context->outValue_); + return true; + }, + [](PowerNapiContext* object) { delete object; } + ); + + POWER_HILOGD(MODULE_JS_NAPI, "return"); + return promise; +} + +napi_value PowerManagerNapi::CreateRunningLock(napi_env env, napi_callback_info info) +{ + POWER_HILOGD(MODULE_JS_NAPI, "enter"); + size_t argc = ARG_3; + int argcnumber = 2; + napi_value args[ARG_3] = { 0 }; + napi_value jsthis; + void *data = nullptr; + + napi_status status = napi_get_cb_info(env, info, &argc, args, &jsthis, &data); + NAPI_ASSERT(env, (status == napi_ok) && (argc >= argcnumber), "Not enough parameters"); + + napi_valuetype type; + NAPI_CALL(env, napi_typeof(env, args[ARG_0], &type)); + NAPI_ASSERT(env, type == napi_string, "Wrong type of 1st parameter. string expected."); + NAPI_CALL(env, napi_typeof(env, args[ARG_1], &type)); + NAPI_ASSERT(env, type == napi_number, "Wrong type of 2st parameter. number expected."); + + char name[LOCK_NAME_STRING_MAX]; + size_t size; + napi_get_value_string_latin1(env, args[ARG_0], name, LOCK_NAME_STRING_MAX, &size); + uint32_t lockType; + napi_get_value_uint32(env, args[1], &lockType); + NAPI_ASSERT(env, lockType < static_cast(RunningLockType::RUNNINGLOCK_BUTT), + "RunningLock Type is not supported"); + + PowerNapiContext* context = new PowerNapiContext(env, args, argc, ARG_3 - 1, ARG_1); + + napi_value promise = context->GetPromise(); + context->StartAsyncWork("Power::CreateRunningLock", + [env, info, context] { + return RunningLockNapi::CreateRunningLock(env, info, &context->outValue_); + }, + [](PowerNapiContext* object) { delete object; } + ); + + POWER_HILOGD(MODULE_JS_NAPI, "return"); + return promise; +} + +napi_value PowerManagerNapi::ShutdownDevice(napi_env env, napi_callback_info info) +{ + POWER_HILOGD(MODULE_JS_NAPI, "enter"); + uint32_t argc = ARG_2; + napi_value args[ARG_2] = { 0 }; + napi_value jsthis; + void *data = nullptr; + + napi_status status = napi_get_cb_info(env, info, &argc, args, &jsthis, &data); + NAPI_ASSERT(env, (status == napi_ok) && (argc >= ARG_1), "Not enough parameters"); + + napi_valuetype type; + NAPI_CALL(env, napi_typeof(env, args[ARG_0], &type)); + NAPI_ASSERT(env, type == napi_string, "Wrong argument type. string expected."); + + ShutdownContext* context = new ShutdownContext(env, args, argc, ARG_2 - 1, ARG_0); + + napi_value promise = context->GetPromise(); + context->StartAsyncWork("Power::ShutdownDevice", + [context] { + PowerMgrClient::GetInstance().ShutDownDevice(context->reason_); + return true; + }, + [](PowerNapiContext* object) { delete object; } + ); + + POWER_HILOGD(MODULE_JS_NAPI, "return"); + return promise; +} + +napi_value PowerManagerNapi::RebootDevice(napi_env env, napi_callback_info info) +{ + POWER_HILOGD(MODULE_JS_NAPI, "enter"); + uint32_t argc = ARG_2; + napi_value args[ARG_2] = { 0 }; + napi_value jsthis; + void *data = nullptr; + + napi_status status = napi_get_cb_info(env, info, &argc, args, &jsthis, &data); + NAPI_ASSERT(env, (status == napi_ok) && (argc >= 1), "Not enough parameters"); + + napi_valuetype type; + NAPI_CALL(env, napi_typeof(env, args[0], &type)); + NAPI_ASSERT(env, type == napi_string, "Wrong argument type. string expected."); + + ShutdownContext* context = new ShutdownContext(env, args, argc, ARG_2 - 1, ARG_0); + + napi_value promise = context->GetPromise(); + context->StartAsyncWork("Power::RebootDevice", + [context] { + PowerMgrClient::GetInstance().RebootDevice(context->reason_); + return true; + }, + [](PowerNapiContext* object) { delete object; } + ); + + POWER_HILOGD(MODULE_JS_NAPI, "return"); + return promise; +} + +napi_value PowerManagerNapi::IsScreenOn(napi_env env, napi_callback_info info) +{ + POWER_HILOGD(MODULE_JS_NAPI, "enter"); + size_t argc = ARG_1; + napi_value args[ARG_1] = { 0 }; + napi_value jsthis; + void *data = nullptr; + + napi_status status = napi_get_cb_info(env, info, &argc, args, &jsthis, &data); + NAPI_ASSERT(env, (status == napi_ok) && (argc >= 0), "Bad parameters"); + + PowerNapiContext* context = new PowerNapiContext(env, args, argc, ARG_1 - 1, ARG_1); + + napi_value promise = context->GetPromise(); + context->StartAsyncWork("Power::IsScreenOn", + [context] { + bool ret = PowerMgrClient::GetInstance().IsScreenOn(); + napi_get_boolean(context->env_, ret, &context->outValue_); + return true; + }, + [](PowerNapiContext* object) { delete object; } + ); + + POWER_HILOGD(MODULE_JS_NAPI, "return"); + return promise; +} + +napi_value PowerManagerNapi::GetState(napi_env env, napi_callback_info info) +{ + POWER_HILOGD(MODULE_JS_NAPI, "enter"); + size_t argc = ARG_1; + napi_value args[ARG_1] = { 0 }; + napi_value jsthis; + void *data = nullptr; + + napi_status status = napi_get_cb_info(env, info, &argc, args, &jsthis, &data); + NAPI_ASSERT(env, (status == napi_ok) && (argc >= 0), "Bad parameters"); + + PowerNapiContext* context = new PowerNapiContext(env, args, argc, ARG_1 - 1, ARG_1); + + napi_value promise = context->GetPromise(); + context->StartAsyncWork("Power::GetState", + [context] { + PowerState state = PowerMgrClient::GetInstance().GetState(); + napi_create_uint32(context->env_, static_cast(state), &context->outValue_); + return true; + }, + [](PowerNapiContext* object) { delete object; } + ); + + POWER_HILOGD(MODULE_JS_NAPI, "return"); + return promise; +} + +napi_value PowerManagerNapi::GetMode(napi_env env, napi_callback_info info) +{ + POWER_HILOGD(MODULE_JS_NAPI, "enter"); + size_t argc = ARG_1; + napi_value args[ARG_1] = { 0 }; + napi_value jsthis; + void *data = nullptr; + napi_status status = napi_get_cb_info(env, info, &argc, args, &jsthis, &data); + NAPI_ASSERT(env, (status == napi_ok) && (argc >= 0), "Bad parameters"); + PowerNapiContext* context = new PowerNapiContext(env, args, argc, 0, 1); + napi_value promise = context->GetPromise(); + context->StartAsyncWork("Power::GetState", + [context] { + uint32_t mode = PowerMgrClient::GetInstance().GetDeviceMode(); + napi_create_uint32(context->env_, mode, &context->outValue_); + return true; + }, + [context](PowerNapiContext* object) { delete context; } + ); + POWER_HILOGD(MODULE_JS_NAPI, "return"); + return promise; +} + +napi_value PowerManagerNapi::InitRunningLockType(napi_env env, napi_value exports) +{ + napi_value background; + napi_value proximity; + int32_t refCount = 1; + + napi_create_uint32(env, static_cast(RunningLockType::RUNNINGLOCK_BACKGROUND), &background); + napi_create_uint32(env, static_cast(RunningLockType::RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL), &proximity); + + napi_property_descriptor desc[] = { + DECLARE_NAPI_STATIC_PROPERTY("BACKGROUND", background), + DECLARE_NAPI_STATIC_PROPERTY("PROXIMITY_SCREEN_CONTROL", proximity), + }; + + napi_value result = nullptr; + napi_define_class(env, "RunningLockType", NAPI_AUTO_LENGTH, EnumRunningLockTypeConstructor, nullptr, + sizeof(desc) / sizeof(*desc), desc, &result); + napi_create_reference(env, result, refCount, &runningLockTypeConstructor_); + napi_set_named_property(env, exports, "RunningLockType", result); + + return exports; +} + +napi_value PowerManagerNapi::InitPowerState(napi_env env, napi_value exports) +{ + napi_value awake; + napi_value inactive; + napi_value sleep; + int32_t refCount = 1; + + napi_create_uint32(env, static_cast(PowerState::AWAKE), &awake); + napi_create_uint32(env, static_cast(PowerState::INACTIVE), &inactive); + napi_create_uint32(env, static_cast(PowerState::SLEEP), &sleep); + + napi_property_descriptor desc[] = { + DECLARE_NAPI_STATIC_PROPERTY("AWAKE", awake), + DECLARE_NAPI_STATIC_PROPERTY("INACTIVE", inactive), + DECLARE_NAPI_STATIC_PROPERTY("SLEEP", sleep), + }; + + napi_value result = nullptr; + napi_define_class(env, "PowerState", NAPI_AUTO_LENGTH, EnumPowerStateConstructor, nullptr, + sizeof(desc) / sizeof(*desc), desc, &result); + napi_create_reference(env, result, refCount, &powerStateConstructor_); + napi_set_named_property(env, exports, "PowerState", result); + + return exports; +} + +napi_value PowerManagerNapi::InitPowerMode(napi_env env, napi_value exports) +{ + napi_value normal; + napi_value power_save; + int32_t refCount = 1; + + napi_create_uint32(env, 0, &normal); + napi_create_uint32(env, 1, &power_save); + + napi_property_descriptor desc[] = { + DECLARE_NAPI_STATIC_PROPERTY("NORMAL", normal), + DECLARE_NAPI_STATIC_PROPERTY("POWER_SAVE", power_save), + }; + + napi_value result = nullptr; + napi_define_class(env, "PowerMode", NAPI_AUTO_LENGTH, EnumPowerModeConstructor, nullptr, + sizeof(desc) / sizeof(*desc), desc, &result); + napi_create_reference(env, result, refCount, &powerModeConstructor_); + napi_set_named_property(env, exports, "PowerMode", result); + + return exports; +} + +napi_value PowerManagerNapi::EnumRunningLockTypeConstructor(napi_env env, napi_callback_info info) +{ + napi_value thisArg = nullptr; + void *data = nullptr; + + napi_get_cb_info(env, info, nullptr, nullptr, &thisArg, &data); + + return thisArg; +} + +napi_value PowerManagerNapi::EnumPowerStateConstructor(napi_env env, napi_callback_info info) +{ + napi_value thisArg = nullptr; + void *data = nullptr; + + napi_get_cb_info(env, info, nullptr, nullptr, &thisArg, &data); + + return thisArg; +} + +napi_value PowerManagerNapi::EnumPowerModeConstructor(napi_env env, napi_callback_info info) +{ + napi_value thisArg = nullptr; + void *data = nullptr; + + napi_get_cb_info(env, info, nullptr, nullptr, &thisArg, &data); + + return thisArg; +} + +EXTERN_C_START +/* + * function for module exports + */ +static napi_value PowerInit(napi_env env, napi_value exports) +{ + POWER_HILOGD(MODULE_JS_NAPI, "enter"); + + napi_value ret = PowerManagerNapi::Init(env, exports); + + POWER_HILOGD(MODULE_JS_NAPI, "return"); + + return ret; +} +EXTERN_C_END + +/* + * Module definition + */ +static napi_module g_module = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = "power", + .nm_register_func = PowerInit, + .nm_modname = "power", + .nm_priv = ((void *)0), + .reserved = {0} +}; + +/* + * Module registration + */ +extern "C" __attribute__((constructor)) void RegisterModule(void) +{ + napi_module_register(&g_module); +} diff --git a/interfaces/kits/js/napi/runninglock/BUILD.gn b/frameworks/napi/runninglock/BUILD.gn similarity index 100% rename from interfaces/kits/js/napi/runninglock/BUILD.gn rename to frameworks/napi/runninglock/BUILD.gn diff --git a/interfaces/kits/js/napi/runninglock/runninglock.cpp b/frameworks/napi/runninglock/runninglock.cpp similarity index 98% rename from interfaces/kits/js/napi/runninglock/runninglock.cpp rename to frameworks/napi/runninglock/runninglock.cpp index 52353b6bf1fcce1f7c1f8253eb15464b1a5adf14..5b5be4387e27c0b3bdebe4c8851c5d8a52fdbca5 100644 --- a/interfaces/kits/js/napi/runninglock/runninglock.cpp +++ b/frameworks/napi/runninglock/runninglock.cpp @@ -268,10 +268,15 @@ static void IsRunningLockTypeSupportedCallBack(napi_env env, resource, [](napi_env env, void *data) { RunningLockAsyncCallbackInfo *asyncCallbackInfo = (RunningLockAsyncCallbackInfo *)data; - if (asyncCallbackInfo->type == RunningLockType::RUNNINGLOCK_BACKGROUND) { + if (asyncCallbackInfo->type == RunningLockType::RUNNINGLOCK_BACKGROUND + || asyncCallbackInfo->type == RunningLockType::RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL) { asyncCallbackInfo->isSupported = true; + } else { + asyncCallbackInfo->isSupported = false; } - POWER_HILOGD(MODULE_JS_NAPI, "%{public}s: runningLock isSupported %{public}s", __func__, + POWER_HILOGD(MODULE_JS_NAPI, "%{public}s: runningLock(%{public}d) isSupported %{public}s", + __func__, + asyncCallbackInfo->type, asyncCallbackInfo->isSupported ? "true" : "false"); }, [](napi_env env, napi_status status, void *data) { diff --git a/interfaces/kits/js/test/power_manager_unit.test.js b/frameworks/napi/test/power_manager_unit.test.js similarity index 99% rename from interfaces/kits/js/test/power_manager_unit.test.js rename to frameworks/napi/test/power_manager_unit.test.js index 003ff7981296824f7e0ed97cb8cf9f667b7388fb..c01e133cb6fdbd5eec036b860f1461508431a09f 100644 --- a/interfaces/kits/js/test/power_manager_unit.test.js +++ b/frameworks/napi/test/power_manager_unit.test.js @@ -133,7 +133,7 @@ describe('appInfoTest', function () { runningLock.isRunningLockTypeSupported(runningLock.RunningLockType.PROXIMITY_SCREEN_CONTROL) .then(supported => { console.info('is_runninglock_type_supported_test_1 PROXIMITY_SCREEN_CONTROL supported is ' + supported); - expect(supported).assertFalse(); + expect(supported).assertTrue(); console.info('is_runninglock_type_supported_test_1 success'); done(); }) diff --git a/interfaces/innerkits/native/src/power_mgr_client.cpp b/frameworks/native/power_mgr_client.cpp similarity index 78% rename from interfaces/innerkits/native/src/power_mgr_client.cpp rename to frameworks/native/power_mgr_client.cpp index c47bff089b004a1dfd57983b06d7916258a531dd..76d7f1014657ebfa0cf5250f3c06cc1cb27d9602 100644 --- a/interfaces/innerkits/native/src/power_mgr_client.cpp +++ b/frameworks/native/power_mgr_client.cpp @@ -28,7 +28,7 @@ namespace OHOS { namespace PowerMgr { namespace { -constexpr int APP_FIRST_UID = 10000; +constexpr int APP_FIRST_UID = APP_FIRST_UID_VALUE; } PowerMgrClient::PowerMgrClient() {} @@ -119,36 +119,33 @@ void PowerMgrClient::SuspendDevice(SuspendDeviceType reason, bool suspendImmed) POWER_HILOGI(MODULE_INNERKIT, " Calling SuspendDevice success."); } -void PowerMgrClient::WakeupDevice(WakeupDeviceType reason, const std::string& details) +void PowerMgrClient::WakeupDevice(WakeupDeviceType reason, const std::string& detail) { RETURN_IF(Connect() != ERR_OK); - // Param details's length must > 0 - if (details.empty()) { - POWER_HILOGE(MODULE_INNERKIT, "%{public}s : detail length is 0, Wakeup failed!", __func__); - return; - } - std::string subDetails; - if (details.length() > MAX_STRING_LENGTH) { - POWER_HILOGI(MODULE_INNERKIT, "%{public}s : detail = %{public}s, length is larger than %{public}d, \ - do substring!", __func__, details.c_str(), MAX_STRING_LENGTH); - subDetails = details.substr(0, MAX_STRING_LENGTH); - } else { - subDetails = details; - } - proxy_->WakeupDevice(GetTickCount(), reason, subDetails); + proxy_->WakeupDevice(GetTickCount(), reason, detail); POWER_HILOGI(MODULE_INNERKIT, " Calling WakeupDevice success."); } -void PowerMgrClient::RefreshActivity(UserActivityType type, bool needChangeBacklight) +void PowerMgrClient::RefreshActivity(UserActivityType type) { RETURN_IF_WITH_LOG(type == UserActivityType::USER_ACTIVITY_TYPE_ATTENTION, " is not supported!"); RETURN_IF(Connect() != ERR_OK); - proxy_->RefreshActivity(GetTickCount(), type, needChangeBacklight); + proxy_->RefreshActivity(GetTickCount(), type, true); POWER_HILOGI(MODULE_INNERKIT, " Calling RefreshActivity Success!"); } +bool PowerMgrClient::IsRunningLockTypeSupported(uint32_t type) +{ + if (type >= static_cast(RunningLockType::RUNNINGLOCK_BUTT)) { + return false; + } + + RETURN_IF_WITH_RET(Connect() != ERR_OK, false); + return proxy_->IsRunningLockTypeSupported(type); +} + bool PowerMgrClient::ForceSuspendDevice() { RETURN_IF_WITH_RET(Connect() != ERR_OK, false); @@ -166,8 +163,13 @@ bool PowerMgrClient::IsScreenOn() return ret; } -std::shared_ptr PowerMgrClient::CreateRunningLock(const std::string& name, RunningLockType type, - const int screenOnFlag) +PowerState PowerMgrClient::GetState() +{ + RETURN_IF_WITH_RET(Connect() != ERR_OK, PowerState::UNKNOWN); + return proxy_->GetState(); +} + +std::shared_ptr PowerMgrClient::CreateRunningLock(const std::string& name, RunningLockType type) { auto uid = IPCSkeleton::GetCallingUid(); if (uid >= APP_FIRST_UID && !Permission::CheckSelfPermission("ohos.permission.RUNNING_LOCK")) { @@ -176,15 +178,6 @@ std::shared_ptr PowerMgrClient::CreateRunningLock(const std::string } RETURN_IF_WITH_RET(Connect() != ERR_OK, nullptr); - if (screenOnFlag == RunningLock::CREATE_WITH_SCREEN_ON) { - if (type != RunningLockType::RUNNINGLOCK_SCREEN) { - POWER_HILOGE(MODULE_INNERKIT, "%{public}s failed to create RunningLock due to screenOnFlag error", - __func__); - return nullptr; - } else { - WakeupDevice(WakeupDeviceType::WAKEUP_DEVICE_APPLICATION, "RunningLockWakeUpScreen"); - } - } int nameLen = (name.size() > RunningLock::MAX_NAME_LEN) ? RunningLock::MAX_NAME_LEN : name.size(); std::shared_ptr runningLock = std::make_shared(proxy_, name.substr(0, nameLen), type); @@ -200,14 +193,6 @@ std::shared_ptr PowerMgrClient::CreateRunningLock(const std::string return runningLock; } -void PowerMgrClient::ProxyRunningLock(bool proxyLock, pid_t uid, pid_t pid) -{ - RETURN_IF(Connect() != ERR_OK); - POWER_HILOGI(MODULE_INNERKIT, "%{public}s :proxyLock = %d, uid = %d, pid = %d", __func__, - proxyLock, uid, pid); - proxy_->ProxyRunningLock(proxyLock, uid, pid); -} - void PowerMgrClient::RegisterPowerStateCallback(const sptr& callback) { RETURN_IF((callback == nullptr) || (Connect() != ERR_OK)); @@ -225,13 +210,50 @@ void PowerMgrClient::UnRegisterPowerStateCallback(const sptr& callback) { RETURN_IF((callback == nullptr) || (Connect() != ERR_OK)); + POWER_HILOGI(MODULE_INNERKIT, "%{public}s.", __func__); proxy_->RegisterShutdownCallback(callback); } void PowerMgrClient::UnRegisterShutdownCallback(const sptr& callback) { RETURN_IF((callback == nullptr) || (Connect() != ERR_OK)); + POWER_HILOGI(MODULE_INNERKIT, "%{public}s.", __func__); proxy_->UnRegisterShutdownCallback(callback); } + +void PowerMgrClient::RegisterPowerModeCallback(const sptr& callback) +{ + RETURN_IF((callback == nullptr) || (Connect() != ERR_OK)); + POWER_HILOGI(MODULE_INNERKIT, "%{public}s.", __func__); + proxy_->RegisterPowerModeCallback(callback); +} + +void PowerMgrClient::UnRegisterPowerModeCallback(const sptr& callback) +{ + RETURN_IF((callback == nullptr) || (Connect() != ERR_OK)); + POWER_HILOGI(MODULE_INNERKIT, "%{public}s.", __func__); + proxy_->UnRegisterPowerModeCallback(callback); +} + +void PowerMgrClient::SetDisplaySuspend(bool enable) +{ + RETURN_IF(Connect() != ERR_OK); + POWER_HILOGI(MODULE_INNERKIT, "%{public}s.", __func__); + proxy_->SetDisplaySuspend(enable); +} + +void PowerMgrClient::SetDeviceMode(const uint32_t mode) +{ + RETURN_IF(Connect() != ERR_OK); + POWER_HILOGE(MODULE_INNERKIT, "%{public}s called.", __func__); + proxy_->SetDeviceMode(mode); +} + +uint32_t PowerMgrClient::GetDeviceMode() +{ + RETURN_IF_WITH_RET(Connect() != ERR_OK, 0); + POWER_HILOGE(MODULE_INNERKIT, "%{public}s called.", __func__); + return proxy_->GetDeviceMode(); +} } // namespace PowerMgr } // namespace OHOS diff --git a/interfaces/innerkits/native/src/running_lock.cpp b/frameworks/native/running_lock.cpp similarity index 78% rename from interfaces/innerkits/native/src/running_lock.cpp rename to frameworks/native/running_lock.cpp index cf57661cc766d3e7d6f144845888ed81cb7638ba..276cbd87d7d34c60d2635ce2d6fea365405e0a58 100644 --- a/interfaces/innerkits/native/src/running_lock.cpp +++ b/frameworks/native/running_lock.cpp @@ -31,23 +31,24 @@ RunningLock::RunningLock(const wptr& proxy, const std::string& name, RunningLock::~RunningLock() { - UnLock(); + if (token_ != nullptr) { + Release(); + } } bool RunningLock::Init() { - lock_guard lock(mutex_); token_ = new (std::nothrow)RunningLockTokenStub(); if (token_ == nullptr) { POWER_HILOGE(MODULE_INNERKIT, "RunningLock::%{public}s :creating RunningLockTokenStub error.", __func__); return false; } + Create(); return true; } ErrCode RunningLock::Lock(uint32_t timeOutMs) { - lock_guard lock(mutex_); POWER_HILOGI(MODULE_INNERKIT, "RunningLock::%{public}s :timeOutMs = %u.", __func__, timeOutMs); sptr proxy = proxy_.promote(); @@ -57,17 +58,12 @@ ErrCode RunningLock::Lock(uint32_t timeOutMs) } POWER_HILOGI(MODULE_INNERKIT, "RunningLock::%{public}s :service lock is called", __func__); proxy->Lock(token_, runningLockInfo_, timeOutMs); - if (timeOutMs != 0) { - usedState_ = RunningLockState::UNKNOWN; - } else { - usedState_ = RunningLockState::USED; - } + return ERR_OK; } ErrCode RunningLock::UnLock() { - lock_guard lock(mutex_); POWER_HILOGI(MODULE_INNERKIT, "RunningLock::%{public}s.", __func__); if (!CheckUsedNoLock()) { return ERR_OK; @@ -79,40 +75,30 @@ ErrCode RunningLock::UnLock() } POWER_HILOGI(MODULE_INNERKIT, "RunningLock::%{public}s :really called service UnLock.", __func__); proxy->UnLock(token_); - usedState_ = RunningLockState::UNUSED; return ERR_OK; } bool RunningLock::CheckUsedNoLock() { - if (usedState_ <= RunningLockState::USED) { - return (usedState_ == RunningLockState::USED); - } sptr proxy = proxy_.promote(); if (proxy == nullptr) { POWER_HILOGE(MODULE_INNERKIT, "RunningLock::%{public}s :proxy nullptr.", __func__); return false; } bool ret = proxy->IsUsed(token_); - if (!ret) { - // only ret false can update the unknown state. - usedState_ = RunningLockState::UNUSED; - return false; - } - POWER_HILOGI(MODULE_INNERKIT, "RunningLock::%{public}s, usedState_ = %d.", __func__, usedState_); - return true; + + POWER_HILOGI(MODULE_INNERKIT, "RunningLock::%{public}s, ret = %d.", __func__, ret); + return ret; } bool RunningLock::IsUsed() { - lock_guard lock(mutex_); POWER_HILOGI(MODULE_INNERKIT, "RunningLock::%{public}s.", __func__); return CheckUsedNoLock(); } ErrCode RunningLock::SetWorkTriggerList(const WorkTriggerList& workTriggerList) { - lock_guard lock(mutex_); auto& list = runningLockInfo_.workTriggerlist; list.clear(); for (auto& w : workTriggerList) { @@ -139,5 +125,31 @@ const WorkTriggerList& RunningLock::GetWorkTriggerList() const { return runningLockInfo_.workTriggerlist; } + +void RunningLock::Create() +{ + POWER_HILOGI(MODULE_INNERKIT, "RunningLock::%{public}s", __func__); + + sptr proxy = proxy_.promote(); + if (proxy == nullptr) { + POWER_HILOGE(MODULE_INNERKIT, "RunningLock::%{public}s :proxy nullptr.", __func__); + return; + } + POWER_HILOGI(MODULE_INNERKIT, "RunningLock::%{public}s :service lock is called", __func__); + proxy->CreateRunningLock(token_, runningLockInfo_); +} + +void RunningLock::Release() +{ + POWER_HILOGI(MODULE_INNERKIT, "RunningLock::%{public}s ", __func__); + + sptr proxy = proxy_.promote(); + if (proxy == nullptr) { + POWER_HILOGE(MODULE_INNERKIT, "RunningLock::%{public}s :proxy nullptr.", __func__); + return; + } + POWER_HILOGI(MODULE_INNERKIT, "RunningLock::%{public}s :service lock is called", __func__); + proxy->ReleaseRunningLock(token_); +} } // namespace PowerMgr } // namespace OHOS diff --git a/interfaces/innerkits/native/src/running_lock_info.cpp b/frameworks/native/running_lock_info.cpp similarity index 100% rename from interfaces/innerkits/native/src/running_lock_info.cpp rename to frameworks/native/running_lock_info.cpp diff --git a/interfaces/innerkits/native/src/running_lock_token_stub.cpp b/frameworks/native/running_lock_token_stub.cpp similarity index 100% rename from interfaces/innerkits/native/src/running_lock_token_stub.cpp rename to frameworks/native/running_lock_token_stub.cpp diff --git a/hdi/BUILD.gn b/hdi/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..d20b88e7536abcb9bb80cee3a78b4d2ace2485a4 --- /dev/null +++ b/hdi/BUILD.gn @@ -0,0 +1,21 @@ +# Copyright (c) 2021 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/ohos.gni") + +group("hdi_group") { + deps = [ + "client:power_hdf_client", + "service:power_hdf_service", + ] +} diff --git a/hdi/api/include/power_hdf_client.h b/hdi/api/include/power_hdf_client.h new file mode 100644 index 0000000000000000000000000000000000000000..d853ee9c9ad3ca8b85bf0de95f4ac8e5e869e68c --- /dev/null +++ b/hdi/api/include/power_hdf_client.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2021 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. + */ + +#ifndef POWER_HDF_CLIENT_H +#define POWER_HDF_CLIENT_H + +#include +#include "ipc_object_proxy.h" +#include "power_hdf_info.h" + +namespace OHOS { +namespace PowerMgr { +class PowerHdfClient { +public: + PowerHdfClient() = default; + ~PowerHdfClient() = default; + ErrCode Suspend(); + ErrCode ReadWakeCount(std::string& count); + ErrCode WriteWakeCount(const std::string& count); + ErrCode WakeLock(const std::string& name); + ErrCode WakeUnlock(const std::string& name); + ErrCode Dump(std::string& info); +private: + ErrCode Dispatch(uint32_t cmd, MessageParcel &data, MessageParcel &reply); + sptr GetService(); +}; +} // namespace PowerMgr +} // namespace OHOS + +#endif // POWER_HDF_CLIENT_H \ No newline at end of file diff --git a/hdi/api/include/power_hdf_info.h b/hdi/api/include/power_hdf_info.h new file mode 100644 index 0000000000000000000000000000000000000000..8c40d7b7165da91d149d2e451fea655969d4af1e --- /dev/null +++ b/hdi/api/include/power_hdf_info.h @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2021 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. + */ + +#ifndef POWER_HDF_INFO_H +#define POWER_HDF_INFO_H + +enum PowerHdfCmd { + CMD_SUSPEND = 0, + CMD_READ_WAKE_COUNT, + CMD_WRITE_WAKE_COUNT, + CMD_WAKE_LOCK, + CMD_WAKE_UNLOCK, + CMD_DUMP, +}; + +enum PowerHdfState { + AWAKE = 0, + INACTIVE, + SLEEP, +}; + +#endif // POWER_HDF_INFO_H \ No newline at end of file diff --git a/hdi/client/BUILD.gn b/hdi/client/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..0872c3f1022beb65293d0a3584ab36e5da14e715 --- /dev/null +++ b/hdi/client/BUILD.gn @@ -0,0 +1,45 @@ +# Copyright (c) 2021 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("//base/powermgr/power_manager/powermgr.gni") +import("//build/ohos.gni") +import("//drivers/adapter/uhdf2/uhdf.gni") + +config("power_client_public_config") { + include_dirs = [ + "include", + "${powermgr_root_path}/hdi/api/include", + ] +} + +ohos_shared_library("power_hdf_client") { + sources = [ "src/power_hdf_client.cpp" ] + + configs = [ "${powermgr_utils_path}:utils_config" ] + + public_configs = [ ":power_client_public_config" ] + + deps = [ + "${hdf_uhdf_path}/hdi:libhdi", + "${hdf_uhdf_path}/host:libhdf_host", + "${hdf_uhdf_path}/utils:libhdf_utils", + "//utils/native/base:utils", + ] + + external_deps = [ + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + ] + + part_name = "power_manager_native" +} diff --git a/hdi/client/src/power_hdf_client.cpp b/hdi/client/src/power_hdf_client.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5af7ee6e3a9378a0778cdabd2cb6ec42b1f82819 --- /dev/null +++ b/hdi/client/src/power_hdf_client.cpp @@ -0,0 +1,136 @@ +/* + * Copyright (c) 2021 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 "power_hdf_client.h" + +#include "iservmgr_hdi.h" +#include "power_common.h" + +namespace OHOS { +namespace PowerMgr { +namespace { +const std::string POWER_HDF_SERVICE = "power_hdf"; +} + +using OHOS::HDI::ServiceManager::V1_0::IServiceManager; + +ErrCode PowerHdfClient::Suspend() +{ + POWER_HILOGW(MODULE_SERVICE, "PowerHdfClient::Suspend: fun is start"); + MessageParcel data; + MessageParcel reply; + return Dispatch(CMD_SUSPEND, data, reply); +} + +ErrCode PowerHdfClient::ReadWakeCount(std::string& count) +{ + POWER_HILOGW(MODULE_SERVICE, "PowerHdfClient::ReadWakeCount: fun is start"); + MessageParcel data; + MessageParcel reply; + ErrCode ret = Dispatch(CMD_READ_WAKE_COUNT, data, reply); + if (ret != ERR_OK) { + return ret; + } + count = reply.ReadString(); + POWER_HILOGW(MODULE_SERVICE, "PowerHdfClient::ReadWakeCount: %{public}s", count.c_str()); + return ERR_OK; +} + +ErrCode PowerHdfClient::WriteWakeCount(const std::string& count) +{ + POWER_HILOGW(MODULE_SERVICE, "PowerHdfClient::WriteWakeCount: fun is start"); + MessageParcel data; + MessageParcel reply; + data.WriteString(count); + ErrCode ret = Dispatch(CMD_WRITE_WAKE_COUNT, data, reply); + if (ret != ERR_OK) { + return ret; + } + return ERR_OK; +} + +ErrCode PowerHdfClient::WakeLock(const std::string& name) +{ + POWER_HILOGW(MODULE_SERVICE, "PowerHdfClient::WakeLock: fun is start"); + MessageParcel data; + MessageParcel reply; + data.WriteString(name); + return Dispatch(CMD_WAKE_LOCK, data, reply); +} + +ErrCode PowerHdfClient::WakeUnlock(const std::string& name) +{ + POWER_HILOGW(MODULE_SERVICE, "PowerHdfClient::WakeUnlock: fun is start"); + MessageParcel data; + MessageParcel reply; + data.WriteString(name); + return Dispatch(CMD_WAKE_UNLOCK, data, reply); +} + +sptr PowerHdfClient::GetService() +{ + POWER_HILOGW(MODULE_SERVICE, "PowerHdfClient::GetService: fun is start"); + auto serviceManager = IServiceManager::Get(); + if (serviceManager == nullptr) { + POWER_HILOGW(MODULE_SERVICE, "service manager is nullptr"); + return nullptr; + } + auto service = serviceManager->GetService(POWER_HDF_SERVICE.c_str()); + if (service == nullptr) { + POWER_HILOGW(MODULE_SERVICE, "power_hdf service is nullptr"); + return nullptr; + } + return service; +} + +ErrCode PowerHdfClient::Dump(std::string& info) +{ + POWER_HILOGW(MODULE_SERVICE, "PowerHdfClient::Dump: fun is start"); + MessageParcel data; + MessageParcel reply; + ErrCode ret = Dispatch(CMD_DUMP, data, reply); + if (ret != ERR_OK) { + return ret; + } + info = reply.ReadString(); + return ERR_OK; +} + +ErrCode PowerHdfClient::Dispatch(uint32_t cmd, MessageParcel &data, MessageParcel &reply) +{ + POWER_HILOGD(MODULE_SERVICE, "Start to dispatch cmd: %{public}d", cmd); + auto service = GetService(); + POWER_HILOGD(MODULE_SERVICE, "PowerHdfClient::Dispatch: GetService"); + if (service == nullptr) { + POWER_HILOGD(MODULE_SERVICE, "PowerHdfClient::Dispatch: service nullptr)"); + return ERR_NO_INIT; + } + POWER_HILOGD(MODULE_SERVICE, "PowerHdfClient::Dispatch: service not nullptr)"); + + MessageOption option; + POWER_HILOGD(MODULE_SERVICE, "PowerHdfClient::Dispatch: option)"); + auto ret = service->SendRequest(cmd, data, reply, option); + POWER_HILOGD(MODULE_SERVICE, "PowerHdfClient::Dispatch: auto ret)"); + if (ret != ERR_OK) { + POWER_HILOGE(MODULE_SERVICE, "failed to send request, cmd: %{public}d, ret: %{public}d", cmd, ret); + return ret; + } else { + ret = reply.ReadInt32(); + } + + return ret; +} +} // namespace PowerMgr +} // namespace OHOS diff --git a/hdi/service/BUILD.gn b/hdi/service/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..dafa08697c024a6f2bb69be5cac2f6a4106751cf --- /dev/null +++ b/hdi/service/BUILD.gn @@ -0,0 +1,53 @@ +# Copyright (c) 2021 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("//base/powermgr/power_manager/powermgr.gni") +import("//build/ohos.gni") +import("//drivers/adapter/uhdf2/uhdf.gni") + +config("power_hdf_private_config") { + include_dirs = [ + "//drivers/ability/sbuf/include", + "//drivers/framework/include", + "//drivers/framework/include/core", + ] +} + +config("power_hdf_public_config") { + include_dirs = [ + "include", + "${powermgr_root_path}/hdi/api/include", + ] +} + +ohos_shared_library("power_hdf_service") { + sources = [ "src/power_hdf_service.cpp" ] + + configs = [ + "${powermgr_utils_path}:utils_config", + ":power_hdf_private_config", + ] + + public_configs = [ ":power_hdf_public_config" ] + + deps = [ + "${hdf_uhdf_path}/host:libhdf_host", + "${hdf_uhdf_path}/ipc:libhdf_ipc_adapter", + "${hdf_uhdf_path}/utils:libhdf_utils", + "//utils/native/base:utils", + ] + + external_deps = [ "hiviewdfx_hilog_native:libhilog" ] + + part_name = "power_manager_native" +} diff --git a/hdi/service/include/power_hdf_service.h b/hdi/service/include/power_hdf_service.h new file mode 100644 index 0000000000000000000000000000000000000000..b46d8c9dc91105f2c4256d14733f26c1df05cfd4 --- /dev/null +++ b/hdi/service/include/power_hdf_service.h @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2021 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. + */ + +#ifndef POWER_HDF_SERVICE_H +#define POWER_HDF_SERVICE_H + +#include +#include +#include "errors.h" +#include "hdf_device_desc.h" + +namespace OHOS { +namespace PowerMgr { +class PowerHdfService { +public: + PowerHdfService() = default; + ~PowerHdfService() = default; + static int32_t Bind(struct HdfDeviceObject *device); + static int32_t Init(struct HdfDeviceObject *device); + static void Release(struct HdfDeviceObject *device); + + static int32_t Dispatch(struct HdfDeviceIoClient *client, + int cmdId, struct HdfSBuf *data, struct HdfSBuf *reply); + static int32_t Suspend(); + static int32_t ReadWakeCount(struct HdfSBuf *reply); + static int32_t WriteWakeCount(struct HdfSBuf *data); + static int32_t WakeLock(struct HdfSBuf *data); + static int32_t WakeUnlock(struct HdfSBuf *data); + static int32_t Dump(struct HdfSBuf *data); + + struct IDeviceIoService ioService; + struct HdfDeviceObject *device; +private: + static constexpr const char * const SUSPEND_STATE = "mem"; + static constexpr const char * const SUSPEND_STATE_PATH = "/sys/power/state"; + static constexpr const char * const LOCK_PATH = "/sys/power/wake_lock"; + static constexpr const char * const UNLOCK_PATH = "/sys/power/wake_unlock"; + static constexpr const char * const WAKEUP_COUNT_PATH = "/sys/power/wakeup_count"; + static std::mutex mutex_; +}; +} // namespace PowerMgr +} // namespace OHOS + +#endif // POWER_HDF_SERVICE_H \ No newline at end of file diff --git a/hdi/service/src/power_hdf_service.cpp b/hdi/service/src/power_hdf_service.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8307f19b13bdb23094f9ee964856526ea91d4ed1 --- /dev/null +++ b/hdi/service/src/power_hdf_service.cpp @@ -0,0 +1,268 @@ +/* + * Copyright (c) 2021 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 "power_hdf_service.h" + +#include +#include +#include "errors.h" +#include "hdf_sbuf.h" +#include "power_hdf_info.h" +#include "pubdef.h" +#include "unique_fd.h" +#include "utils/hdf_log.h" + +namespace OHOS { +namespace PowerMgr { +std::mutex PowerHdfService::mutex_; + +int32_t PowerHdfService::Bind(struct HdfDeviceObject *device) +{ + HDF_LOGD("%{public}s enter", __func__); + if (device == NULL) { + HDF_LOGE("%{public}s device is NULL", __func__); + return HDF_ERR_INVALID_OBJECT; + } + PowerHdfService* service = new PowerHdfService(); + service->device = device; + service->ioService.Open = nullptr; + service->ioService.Dispatch = PowerHdfService::Dispatch; + service->ioService.Release = nullptr; + + device->service = &(service->ioService); + device->priv = reinterpret_cast(service); + + return HDF_SUCCESS; +} + +int32_t PowerHdfService::Init(struct HdfDeviceObject *device) +{ + HDF_LOGD("%{public}s enter", __func__); + if (device == NULL) { + HDF_LOGE("%{public}s device is NULL", __func__); + return HDF_ERR_INVALID_OBJECT; + } + + PowerHdfService* service = reinterpret_cast(device->priv); + if (service == NULL) { + return HDF_ERR_INVALID_OBJECT; + } + + return HDF_SUCCESS; +} + +void PowerHdfService::Release(struct HdfDeviceObject *device) +{ + HDF_LOGD("%{public}s enter", __func__); + if (device == NULL) { + return; + } + PowerHdfService* service = reinterpret_cast(device->priv); + if (service == NULL) { + return; + } + delete service; +} + +int32_t PowerHdfService::Dispatch(struct HdfDeviceIoClient *client, + int cmdId, struct HdfSBuf *data, struct HdfSBuf *reply) +{ + HDF_LOGD("%{public}s enter", __func__); + if (client == NULL || client->device == NULL) { + HDF_LOGE("%{public}s: client or client->device is NULL", __func__); + return HDF_ERR_INVALID_PARAM; + } + int ret = HDF_ERR_NOT_SUPPORT; + switch (cmdId) { + case CMD_SUSPEND: { + ret = PowerHdfService::Suspend(); + break; + } + case CMD_READ_WAKE_COUNT: { + ret = PowerHdfService::ReadWakeCount(reply); + break; + } + case CMD_WRITE_WAKE_COUNT: { + ret = PowerHdfService::WriteWakeCount(data); + break; + } + case CMD_WAKE_LOCK: { + ret = PowerHdfService::WakeLock(data); + break; + } + case CMD_WAKE_UNLOCK: { + ret = PowerHdfService::WakeUnlock(data); + break; + } + case CMD_DUMP: { + ret = PowerHdfService::Dump(reply); + break; + } + default: + break; + } + if (ret == HDF_SUCCESS) { + HdfSbufWriteInt32(reply, ERR_OK); + } else if (ret == HDF_FAILURE) { + HdfSbufWriteInt32(reply, ERR_INVALID_OPERATION); + } + return ret; +} + +int32_t PowerHdfService::Suspend() +{ + HDF_LOGD("%{public}s enter", __func__); + std::lock_guard lock(mutex_); + UniqueFd suspendStateFd(TEMP_FAILURE_RETRY(open(SUSPEND_STATE_PATH, O_RDWR | O_CLOEXEC))); + if (suspendStateFd < 0) { + HDF_LOGD("Failed to open the suspending state fd!"); + return HDF_FAILURE; + } + bool ret = SaveStringToFd(suspendStateFd, SUSPEND_STATE); + if (!ret) { + HDF_LOGE("Failed to write the suspend state!"); + return HDF_FAILURE; + } + return HDF_SUCCESS; +} + +int32_t PowerHdfService::ReadWakeCount(struct HdfSBuf *reply) +{ + HDF_LOGD("%{public}s enter", __func__); + std::lock_guard lock(mutex_); + UniqueFd fd(TEMP_FAILURE_RETRY(open(WAKEUP_COUNT_PATH, O_RDWR | O_CLOEXEC))); + if (fd < 0) { + HDF_LOGD("Failed to open the suspending state fd!"); + return HDF_FAILURE; + } + std::string count; + bool ret = LoadStringFromFd(fd, count); + if (!ret) { + HDF_LOGE("Failed to read wake count from kernel!"); + return HDF_FAILURE; + } + HdfSbufWriteString(reply, count.c_str()); + return HDF_SUCCESS; +} + +int32_t PowerHdfService::WriteWakeCount(struct HdfSBuf *data) +{ + HDF_LOGD("%{public}s enter", __func__); + std::lock_guard lock(mutex_); + if (data == nullptr) { + return HDF_ERR_INVALID_PARAM; + } + const char* count = HdfSbufReadString(data); + if (count == nullptr) { + return HDF_ERR_INVALID_PARAM; + } + UniqueFd fd(TEMP_FAILURE_RETRY(open(WAKEUP_COUNT_PATH, O_RDWR | O_CLOEXEC))); + bool ret = SaveStringToFd(fd, count); + if (!ret) { + HDF_LOGE("Failed to write the lock to kernel!"); + return HDF_FAILURE; + } + return HDF_SUCCESS; +} + +int32_t PowerHdfService::WakeLock(struct HdfSBuf *data) +{ + HDF_LOGD("%{public}s enter", __func__); + std::lock_guard lock(mutex_); + if (data == nullptr) { + return HDF_ERR_INVALID_PARAM; + } + const char* name = HdfSbufReadString(data); + if (name == nullptr) { + return HDF_ERR_INVALID_PARAM; + } + UniqueFd fd(TEMP_FAILURE_RETRY(open(LOCK_PATH, O_RDWR | O_CLOEXEC))); + bool ret = SaveStringToFd(fd, name); + if (!ret) { + HDF_LOGE("Failed to write the lock to kernel!"); + return HDF_FAILURE; + } + return HDF_SUCCESS; +} + +int32_t PowerHdfService::WakeUnlock(struct HdfSBuf *data) +{ + HDF_LOGD("%{public}s enter", __func__); + std::lock_guard lock(mutex_); + if (data == nullptr) { + return HDF_ERR_INVALID_PARAM; + } + const char* name = HdfSbufReadString(data); + if (name == nullptr) { + return HDF_ERR_INVALID_PARAM; + } + UniqueFd fd(TEMP_FAILURE_RETRY(open(UNLOCK_PATH, O_RDWR | O_CLOEXEC))); + bool ret = SaveStringToFd(fd, name); + if (!ret) { + HDF_LOGE("Failed to write the lock to kernel!"); + return HDF_FAILURE; + } + return HDF_SUCCESS; +} + +static void loadSystemInfo(const char* const path, std::string& info) +{ + UniqueFd fd(TEMP_FAILURE_RETRY(open(path, O_RDWR | O_CLOEXEC))); + std::string str; + if (fd >= 0) { + bool ret = LoadStringFromFd(fd, str); + if (!ret) { + str = "# Failed to read"; + } + } else { + str = "# Failed to open"; + } + info.append(path); + info.append(": " + str + "\n"); +} + +int32_t PowerHdfService::Dump(struct HdfSBuf *reply) +{ + HDF_LOGD("%{public}s enter", __func__); + std::string dumpInfo(""); + loadSystemInfo(SUSPEND_STATE_PATH, dumpInfo); + loadSystemInfo(WAKEUP_COUNT_PATH, dumpInfo); + loadSystemInfo(LOCK_PATH, dumpInfo); + loadSystemInfo(UNLOCK_PATH, dumpInfo); + + HdfSbufWriteString(reply, dumpInfo.c_str()); + return HDF_SUCCESS; +} + +struct HdfDriverEntry g_powerHdfEntry = { + .moduleVersion = 1, + .moduleName = "power_hdf", + .Bind = PowerHdfService::Bind, + .Init = PowerHdfService::Init, + .Release = PowerHdfService::Release, +}; +} // namespace PowerMgr +} // namespace OHOS + +#ifndef __cplusplus +extern "C" { +#endif + +HDF_INIT(OHOS::PowerMgr::g_powerHdfEntry); + +#ifndef __cplusplus +} +#endif + diff --git a/interfaces/innerkits/BUILD.gn b/interfaces/innerkits/BUILD.gn index db852c3c6e5cc1e2878933550d4c1a4a8ff367cf..e932a39201e2f0ab742c96fe9efc09c95a703caf 100644 --- a/interfaces/innerkits/BUILD.gn +++ b/interfaces/innerkits/BUILD.gn @@ -26,12 +26,14 @@ config("powermgr_public_config") { ohos_shared_library("powermgr_client") { sources = [ + "${powermgr_framework_path}/native/power_mgr_client.cpp", + "${powermgr_framework_path}/native/running_lock.cpp", + "${powermgr_framework_path}/native/running_lock_info.cpp", + "${powermgr_framework_path}/native/running_lock_token_stub.cpp", "${powermgr_service_path}/zidl/src/power_mgr_proxy.cpp", + "${powermgr_service_path}/zidl/src/power_mode_callback_proxy.cpp", + "${powermgr_service_path}/zidl/src/power_shutdown_callback_proxy.cpp", "${powermgr_service_path}/zidl/src/power_state_callback_proxy.cpp", - "native/src/power_mgr_client.cpp", - "native/src/running_lock.cpp", - "native/src/running_lock_info.cpp", - "native/src/running_lock_token_stub.cpp", ] configs = [ diff --git a/interfaces/innerkits/native/include/ipower_mgr.h b/interfaces/innerkits/native/include/ipower_mgr.h index d8c224e87cde86929cef4f6cae56f41fa4bbc32d..7c25be4b05d4b0e75ed8ef7044e65d56c5f41388 100644 --- a/interfaces/innerkits/native/include/ipower_mgr.h +++ b/interfaces/innerkits/native/include/ipower_mgr.h @@ -23,6 +23,7 @@ #include "ipower_state_callback.h" #include "ishutdown_callback.h" +#include "ipower_mode_callback.h" #include "power_state_machine_info.h" #include "running_lock_info.h" @@ -31,7 +32,10 @@ namespace PowerMgr { class IPowerMgr : public IRemoteBroker { public: enum { - RUNNINGLOCK_LOCK = 0, + CREATE_RUNNINGLOCK = 0, + RELEASE_RUNNINGLOCK, + IS_RUNNINGLOCK_TYPE_SUPPORTED, + RUNNINGLOCK_LOCK, RUNNINGLOCK_UNLOCK, RUNNINGLOCK_SET_WORK_TRIGGER_LIST, RUNNINGLOCK_ISUSED, @@ -39,6 +43,7 @@ public: WAKEUP_DEVICE, SUSPEND_DEVICE, REFRESH_ACTIVITY, + GET_STATE, IS_SCREEN_ON, FORCE_DEVICE_SUSPEND, REBOOT_DEVICE, @@ -46,9 +51,17 @@ public: REG_POWER_STATE_CALLBACK, UNREG_POWER_STATE_CALLBACK, REG_SHUTDOWN_CALLBACK, - UNREG_SHUTDOWN_CALLBACK + UNREG_SHUTDOWN_CALLBACK, + REG_POWER_MODE_CALLBACK, + UNREG_POWER_MODE_CALLBACK, + SET_DISPLAY_SUSPEND, + SETMODE_DEVICE, + GETMODE_DEVICE }; + virtual void CreateRunningLock(const sptr& token, const RunningLockInfo& runningLockInfo) = 0; + virtual void ReleaseRunningLock(const sptr& token) = 0; + virtual bool IsRunningLockTypeSupported(uint32_t type) = 0; virtual void Lock(const sptr& token, const RunningLockInfo& runningLockInfo, uint32_t timeOutMs) = 0; virtual void UnLock(const sptr& token) = 0; @@ -62,6 +75,7 @@ public: virtual void SuspendDevice(int64_t callTimeMs, SuspendDeviceType reason, bool suspendImmed) = 0; virtual void WakeupDevice(int64_t callTimeMs, WakeupDeviceType reason, const std::string& details) = 0; virtual void RefreshActivity(int64_t callTimeMs, UserActivityType type, bool needChangeBacklight) = 0; + virtual PowerState GetState() = 0; virtual bool IsScreenOn() = 0; virtual bool ForceSuspendDevice(int64_t callTimeMs) = 0; virtual void RegisterPowerStateCallback(const sptr& callback) = 0; @@ -71,6 +85,14 @@ public: virtual void RegisterShutdownCallback(const sptr& callback) = 0; virtual void UnRegisterShutdownCallback(const sptr& callback) = 0; + // Used for callback registration upon power mode. + virtual void RegisterPowerModeCallback(const sptr& callback) = 0; + virtual void UnRegisterPowerModeCallback(const sptr& callback) = 0; + + virtual void SetDisplaySuspend(bool enable) = 0; + virtual void SetDeviceMode(const uint32_t& mode) = 0; + virtual uint32_t GetDeviceMode() = 0; + DECLARE_INTERFACE_DESCRIPTOR(u"ohos.powermgr.IPowerMgr"); }; } // namespace PowerMgr diff --git a/interfaces/innerkits/native/include/ipower_mode_callback.h b/interfaces/innerkits/native/include/ipower_mode_callback.h new file mode 100644 index 0000000000000000000000000000000000000000..ba987e798150404f4d2d1179ddd5da69ac5da677 --- /dev/null +++ b/interfaces/innerkits/native/include/ipower_mode_callback.h @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2021 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. + */ + +#ifndef IPOWER_MGR_CHANGE_MODE_CALLBACK_H +#define IPOWER_MGR_CHANGE_MODE_CALLBACK_H + +#include +#include +#include +#include + +namespace OHOS { +namespace PowerMgr { +class IPowerModeCallback : public IRemoteBroker { +public: + enum { + POWER_MODE_CHANGED = 0, + }; + + virtual void PowerModeCallback() = 0; + + DECLARE_INTERFACE_DESCRIPTOR(u"ohos.powermgr.IPowerModeCallback"); +}; +} // namespace PowerMgr +} // namespace OHOS +#endif // IPOWER_MGR_CHANGE_MODE_CALLBACK_H diff --git a/interfaces/innerkits/native/include/ishutdown_callback.h b/interfaces/innerkits/native/include/ishutdown_callback.h index 259223838e8d1c2bec9aba27c87a439ac8c360bb..31dfb614053dcd8234e5ae038f67b281eb0d551f 100644 --- a/interfaces/innerkits/native/include/ishutdown_callback.h +++ b/interfaces/innerkits/native/include/ishutdown_callback.h @@ -18,15 +18,20 @@ #include #include +#include +#include namespace OHOS { namespace PowerMgr { class IShutdownCallback : public IRemoteBroker { public: - virtual void ShutdownCallback() = 0; + enum { + POWER_SHUTDOWN_CHANGED = 0, + }; + virtual void ShutdownCallback() = 0; DECLARE_INTERFACE_DESCRIPTOR(u"ohos.powermgr.IShutdownCallback"); }; } // namespace PowerMgr } // namespace OHOS -#endif // IPOWER_MGR_SHUTDOWN_CALLBACK_H \ No newline at end of file +#endif // IPOWER_MGR_SHUTDOWN_CALLBACK_H diff --git a/interfaces/innerkits/native/include/power_mgr_client.h b/interfaces/innerkits/native/include/power_mgr_client.h index e5a73f10c9396ac24a8857833e6a156b6d5291c1..cac7d3706816da9134c4ce3b5ae8df831ad48d95 100644 --- a/interfaces/innerkits/native/include/power_mgr_client.h +++ b/interfaces/innerkits/native/include/power_mgr_client.h @@ -23,6 +23,8 @@ #include "power_state_machine_info.h" #include "running_lock.h" +#define APP_FIRST_UID_VALUE 10000 + namespace OHOS { namespace PowerMgr { class PowerMgrClient final : public DelayedRefSingleton { @@ -49,19 +51,17 @@ public: * Suspend device and set screen off. * * @param reason The reason why will you suspend the device, such as timeout/powerkey/forcesuspend and so on. - * @param suspendImmed The flag indicating whether the system will go to sleep immediately. */ void SuspendDevice(SuspendDeviceType reason = SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, - bool suspendImmed = true); + bool suspendImmed = false); /** * Wake up the device and set the screen on. * * @param reason The reason for waking up the device, such as powerkey/plugin/application. - * @param details Details of the wakeup reason. */ void WakeupDevice(WakeupDeviceType reason = WakeupDeviceType::WAKEUP_DEVICE_APPLICATION, - const std::string& details = "SoftWare Wakeup"); + const std::string& detail = std::string("app call")); /** * Refresh the screentimeout time, and keep the screen on. RefreshActivity works only when the screen is on. @@ -70,26 +70,53 @@ public: * @param needChangeBacklight Whether to change the backlight state, for example, from DIM to BRIGHT. * Set it to false if you don't want to change the backlight state. */ - void RefreshActivity(UserActivityType type = UserActivityType::USER_ACTIVITY_TYPE_OTHER, - bool needChangeBacklight = true); + void RefreshActivity(UserActivityType type = UserActivityType::USER_ACTIVITY_TYPE_OTHER); /** * Check whether the device screen is on. The result may be true or false, depending on the system state. */ bool IsScreenOn(); + /** + * Get Power state. The result is PowerState type. + */ + PowerState GetState(); + /** * Forcibly suspend the device into deepsleep, and return the suspend result. */ bool ForceSuspendDevice(); - std::shared_ptr CreateRunningLock(const std::string& name, RunningLockType type, - const int screenOnFlag = 0); - void ProxyRunningLock(bool proxyLock, pid_t uid, pid_t pid = INVALID_PID); + /** + * Check whether the type of running lock is supported + */ + bool IsRunningLockTypeSupported(uint32_t type); + + /** + * Enable/disable display suspend state + */ + void SetDisplaySuspend(bool enable); + + /* Set the device mode. + * + * @param set The mode the device. + */ + void SetDeviceMode(const uint32_t mode); + + /** + * Get the device mode. + * + * @param Get The mode the device. + */ + uint32_t GetDeviceMode(); + + std::shared_ptr CreateRunningLock(const std::string& name, RunningLockType type); void RegisterPowerStateCallback(const sptr& callback); void UnRegisterPowerStateCallback(const sptr& callback); void RegisterShutdownCallback(const sptr& callback); void UnRegisterShutdownCallback(const sptr& callback); + void RegisterPowerModeCallback(const sptr& callback); + void UnRegisterPowerModeCallback(const sptr& callback); private: class PowerMgrDeathRecipient : public IRemoteObject::DeathRecipient { diff --git a/interfaces/innerkits/native/include/power_state_machine_info.h b/interfaces/innerkits/native/include/power_state_machine_info.h index 86621c075a1284f858b556c57f01c78b86df1419..720cf8c513616002114b56456ef35e8c7325c1e2 100644 --- a/interfaces/innerkits/native/include/power_state_machine_info.h +++ b/interfaces/innerkits/native/include/power_state_machine_info.h @@ -51,7 +51,7 @@ enum class PowerState : uint32_t { /** * Power State: screen on and cpu on. */ - AWAKE, + AWAKE = 0, /** * Power State: screen off and cpu on. @@ -62,6 +62,21 @@ enum class PowerState : uint32_t { * Power State: screen off and cpu off. */ SLEEP, + + /** + * Power State: unknown. + */ + UNKNOWN, +}; + +/** + * Display State of Device. + */ +enum class DisplayState : uint32_t { + DISPLAY_OFF = 0, + DISPLAY_DIM = 1, + DISPLAY_ON = 2, + DISPLAY_SUSPEND = 3, }; // UserActivityType list, must sync with A_PMS @@ -72,6 +87,7 @@ enum class UserActivityType : uint32_t { USER_ACTIVITY_TYPE_ACCESSIBILITY = 3, USER_ACTIVITY_TYPE_ATTENTION = 4, USER_ACTIVITY_TYPE_SOFTWARE = 5, + USER_ACTIVITY_TYPE_MAX = USER_ACTIVITY_TYPE_SOFTWARE, }; // WakeupReasonType list @@ -86,6 +102,7 @@ enum class WakeupDeviceType : uint32_t { WAKEUP_DEVICE_WAKE_MOTION = 7, WAKEUP_DEVICE_HDMI = 8, WAKEUP_DEVICE_LID = 9, + WAKEUP_DEVICE_MAX = WAKEUP_DEVICE_LID, }; // SuspendDeviceType list @@ -102,6 +119,28 @@ enum class SuspendDeviceType : uint32_t { SUSPEND_DEVICE_REASON_FORCE_SUSPEND = 8, SUSPEND_DEVICE_REASON_MAX = SUSPEND_DEVICE_REASON_FORCE_SUSPEND, }; + +enum class StateChangeReason : uint32_t { + STATE_CHANGE_REASON_INIT = 0, + STATE_CHANGE_REASON_TIMEOUT = 1, + STATE_CHANGE_REASON_RUNNING_LOCK = 2, + STATE_CHANGE_REASON_BATTERY = 3, + STATE_CHANGE_REASON_THERMAL = 4, + STATE_CHANGE_REASON_WORK = 5, + STATE_CHANGE_REASON_SYSTEM = 6, + STATE_CHANGE_REASON_APPLICATION = 10, + STATE_CHANGE_REASON_SETTINGS = 11, + STATE_CHANGE_REASON_HARD_KEY = 12, + STATE_CHANGE_REASON_TOUCH = 13, + STATE_CHANGE_REASON_CABLE = 14, + STATE_CHANGE_REASON_SENSOR = 15, + STATE_CHANGE_REASON_LID = 16, + STATE_CHANGE_REASON_CAMERA = 17, + STATE_CHANGE_REASON_ACCESSIBILITY = 18, + STATE_CHANGE_REASON_RESET = 19, + STATE_CHANGE_REASON_REMOTE = 100, + STATE_CHANGE_REASON_UNKNOWN = 1000, +}; } // namespace PowerMgr } // namespace OHOS #endif // POWERMGR_POWER_STATE_MACHINE_INFO_H diff --git a/interfaces/innerkits/native/include/running_lock.h b/interfaces/innerkits/native/include/running_lock.h index 182d1e716e83b3014361bab5f6765cd190088805..7c07ebf25410e41ad167e1376ae1afafc44f9dce 100644 --- a/interfaces/innerkits/native/include/running_lock.h +++ b/interfaces/innerkits/native/include/running_lock.h @@ -32,13 +32,6 @@ public: ~RunningLock(); DISALLOW_COPY_AND_MOVE(RunningLock); - enum class RunningLockState { - UNUSED = 0, - USED, - UNKNOWN, - BUTT, - }; - bool Init(); bool IsUsed(); @@ -68,9 +61,10 @@ public: static constexpr uint32_t CREATE_WITH_SCREEN_ON = 0x10000000; private: + void Create(); + void Release(); std::mutex mutex_; RunningLockInfo runningLockInfo_; - RunningLockState usedState_ {RunningLockState::UNUSED}; sptr token_; wptr proxy_; bool CheckUsedNoLock(); diff --git a/ohos.build b/ohos.build index f961952636bc36278e0d5206e138fd8d9f953b5c..c48be5c0f0fcdca20ad38496b481455fa47e1cde 100644 --- a/ohos.build +++ b/ohos.build @@ -4,10 +4,12 @@ "power_manager_native": { "module_list": [ "//base/powermgr/power_manager/interfaces/innerkits:powermgr_client", + "//base/powermgr/power_manager/frameworks/napi/power:power", + "//base/powermgr/power_manager/frameworks/napi/runninglock:runninglock", "//base/powermgr/power_manager/sa_profile:powermgr_sa_profile", "//base/powermgr/power_manager/services:powermgrservice", - "//base/powermgr/power_manager/interfaces/kits/js/napi/power:power", - "//base/powermgr/power_manager/interfaces/kits/js/napi/runninglock:runninglock" + "//base/powermgr/power_manager/hdi:hdi_group", + "//base/powermgr/power_manager/services:power_service" ], "inner_kits": [ { @@ -28,7 +30,8 @@ } ], "test_list": [ - "//base/powermgr/power_manager/services/native/test:powermgr_test" + "//base/powermgr/power_manager/services/native/test:powermgr_test", + "//base/powermgr/power_manager/test:systemtest" ] } } diff --git a/powermgr.gni b/powermgr.gni index c9a674986a44d780314551bceb458b425fb0c463..6064d0282e382d72bd08fdf0bb317488ef98fc32 100644 --- a/powermgr.gni +++ b/powermgr.gni @@ -19,6 +19,10 @@ powermgr_root_path = "//base/powermgr/power_manager" powermgr_service_path = "${powermgr_root_path}/services" +powermgr_framework_path = "${powermgr_root_path}/frameworks" + +powermgr_hdi_path = "${powermgr_root_path}/hdi" + powermgr_interfaces_path = "${powermgr_root_path}/interfaces" powermgr_native_innerkits_path = "${powermgr_interfaces_path}/innerkits" diff --git a/services/BUILD.gn b/services/BUILD.gn index 6d240a2462520abf06bb4dbb507d38f09c147fcf..d6edb3e36b41e81dc5df2c6ac3099cb0fc4e9dad 100644 --- a/services/BUILD.gn +++ b/services/BUILD.gn @@ -31,12 +31,17 @@ ohos_shared_library("powermgrservice") { "native/src/power_mgr_monitor.cpp", "native/src/power_mgr_notify.cpp", "native/src/power_mgr_service.cpp", + "native/src/power_mode_module.cpp", + "native/src/power_mode_policy.cpp", + "native/src/power_save_model.cpp", "native/src/power_state_machine.cpp", "native/src/powerms_event_handler.cpp", "native/src/running_lock_inner.cpp", "native/src/running_lock_mgr.cpp", "native/src/shutdown_service.cpp", "zidl/src/power_mgr_stub.cpp", + "zidl/src/power_mode_callback_stub.cpp", + "zidl/src/power_shutdown_callback_stub.cpp", "zidl/src/power_state_callback_stub.cpp", ] @@ -51,6 +56,7 @@ ohos_shared_library("powermgrservice") { "${powermgr_native_innerkits_path}:powermgr_client", "${powermgr_utils_path}:powermgr_utils", "native/src/actions:powermgr_actions", + "//drivers/peripheral/display/hal:hdi_display_device", "//utils/native/base:utils", ] @@ -69,3 +75,7 @@ ohos_shared_library("powermgrservice") { part_name = "${powermgr_native_part_name}" } + +group("power_service") { + deps = [ "native/profile:power_mode_config" ] +} diff --git a/services/native/include/actions/idevice_state_action.h b/services/native/include/actions/idevice_state_action.h index 9289012c59fd75830f493d6dca76e887209198f6..5dbcc817d95fb6ad98584438bc684566b094c7dc 100644 --- a/services/native/include/actions/idevice_state_action.h +++ b/services/native/include/actions/idevice_state_action.h @@ -22,6 +22,11 @@ namespace OHOS { namespace PowerMgr { +enum ActionResult { + SUCCESS = 0, + FAILED = 1, +}; + class IDeviceStateAction { public: IDeviceStateAction() = default; @@ -32,6 +37,9 @@ public: virtual void Wakeup(int64_t callTimeMs, WakeupDeviceType type, const std::string& details, const std::string& pkgName) = 0; virtual void RefreshActivity(int64_t callTimeMs, UserActivityType type, uint32_t flags) = 0; + virtual DisplayState GetDisplayState() = 0; + virtual uint32_t SetDisplayState(DisplayState state) = 0; + virtual uint32_t GoToSleep(std::function onSuspend, std::function onWakeup, bool force) = 0; }; } // namespace PowerMgr } // namespace OHOS diff --git a/services/native/include/actions/irunning_lock_action.h b/services/native/include/actions/irunning_lock_action.h index 89b80058d76a2383a41dfcdb9f476e145f6c7a4b..36d0aaccda9df8d698e346a8ef4d4756b65f1a0d 100644 --- a/services/native/include/actions/irunning_lock_action.h +++ b/services/native/include/actions/irunning_lock_action.h @@ -34,8 +34,8 @@ public: return LOCK_TAGS[ToUnderlying(type)]; } - void Acquire(RunningLockType type); - void Release(RunningLockType type); + virtual void Acquire(RunningLockType type); + virtual void Release(RunningLockType type); virtual void Lock(RunningLockType type, const std::string& tag) = 0; virtual void Unlock(RunningLockType type, const std::string& tag) = 0; diff --git a/services/native/include/power_mgr_service.h b/services/native/include/power_mgr_service.h index e7647fc37dfa69376ec7308129add4133baa7f88..d626eed41107f50e02bb9e7d8e88d622c3b4c7e4 100644 --- a/services/native/include/power_mgr_service.h +++ b/services/native/include/power_mgr_service.h @@ -28,6 +28,10 @@ #include "running_lock_mgr.h" #include "shutdown_service.h" #include "sp_singleton.h" +#include "power_mode_module.h" +#include "power_save_mode.h" + +#define APP_FIRST_UID_VALUE 10000 namespace OHOS { namespace PowerMgr { @@ -42,21 +46,36 @@ public: int32_t Dump(int32_t fd, const std::vector& args) override; virtual void RebootDevice(const std::string& reason) override; virtual void ShutDownDevice(const std::string& reason) override; - virtual void SuspendDevice(int64_t callTimeMs, SuspendDeviceType reason, bool suspendImmed) override; - virtual void WakeupDevice(int64_t callTimeMs, WakeupDeviceType reason, const std::string& details) override; - virtual void RefreshActivity(int64_t callTimeMs, UserActivityType type, bool needChangeBacklight) override; + virtual void SuspendDevice(int64_t callTimeMs, SuspendDeviceType reason, + bool suspendImmed) override; + virtual void WakeupDevice(int64_t callTimeMs, WakeupDeviceType reason, + const std::string& details) override; + virtual void RefreshActivity(int64_t callTimeMs, UserActivityType type, + bool needChangeBacklight) override; + virtual PowerState GetState() override; virtual bool IsScreenOn() override; virtual bool ForceSuspendDevice(int64_t callTimeMs) override; - void Lock(const sptr& token, const RunningLockInfo& runningLockInfo, uint32_t timeOutMS) override; - void UnLock(const sptr& token) override; - void ForceUnLock(const sptr& token); - bool IsUsed(const sptr& token) override; - void SetWorkTriggerList(const sptr& token, const WorkTriggerList& workTriggerList) override; - void ProxyRunningLock(bool proxyLock, pid_t uid, pid_t pid) override; - void RegisterPowerStateCallback(const sptr& callback) override; - void UnRegisterPowerStateCallback(const sptr& callback) override; - void RegisterShutdownCallback(const sptr& callback) override; - void UnRegisterShutdownCallback(const sptr& callback) override; + virtual void CreateRunningLock(const sptr& token, + const RunningLockInfo& runningLockInfo) override; + virtual void ReleaseRunningLock(const sptr& token) override; + virtual bool IsRunningLockTypeSupported(uint32_t type) override; + virtual void Lock(const sptr& token, + const RunningLockInfo& runningLockInfo, uint32_t timeOutMS) override; + virtual void UnLock(const sptr& token) override; + virtual void ForceUnLock(const sptr& token); + virtual bool IsUsed(const sptr& token) override; + virtual void SetWorkTriggerList(const sptr& token, + const WorkTriggerList& workTriggerList) override; + virtual void ProxyRunningLock(bool proxyLock, pid_t uid, pid_t pid) override; + virtual void RegisterPowerStateCallback(const sptr& callback) override; + virtual void UnRegisterPowerStateCallback(const sptr& callback) override; + virtual void RegisterShutdownCallback(const sptr& callback) override; + virtual void UnRegisterShutdownCallback(const sptr& callback) override; + virtual void RegisterPowerModeCallback(const sptr& callback) override; + virtual void UnRegisterPowerModeCallback(const sptr& callback) override; + virtual void SetDisplaySuspend(bool enable) override; + virtual void SetDeviceMode(const uint32_t& mode) override; + virtual uint32_t GetDeviceMode() override; std::shared_ptr GetHandler() const { @@ -82,7 +101,33 @@ public: { return ready_; } + void SetDisplayOffTime(int64_t time) + { + powerStateMachine_->SetDisplayOffTime(time); + } + void SetSleepTime(int64_t time) + { + powerStateMachine_->SetSleepTime(time); + } + void EnableMock(IDeviceStateAction* stateAction, IDevicePowerAction* powerAction, + IRunningLockAction* lockAction) + { + POWER_HILOGE(MODULE_SERVICE, "Service EnableMock:%{public}d", mockCount_++); + runningLockMgr_->EnableMock(lockAction); + powerStateMachine_->EnableMock(stateAction); + shutdownService_.EnableMock(powerAction); + } + void MockProximity(uint32_t status) + { + POWER_HILOGE(MODULE_SERVICE, "MockProximity: fun is start"); + runningLockMgr_->SetProximity(status); + POWER_HILOGE(MODULE_SERVICE, "MockProximity: fun is end"); + } + void MockSystemWakeup() + { + PowerStateMachine::onWakeup(); + } private: bool Init(); bool PowerStateMachineInit(); @@ -96,6 +141,8 @@ private: std::shared_ptr powerStateMachine_; std::shared_ptr powerMgrNotify_; ShutdownService shutdownService_; + PowerModeModule powerModeModule_; + uint32_t mockCount_ {0}; }; } // namespace PowerMgr } // namespace OHOS diff --git a/services/native/include/power_mode_module.h b/services/native/include/power_mode_module.h new file mode 100644 index 0000000000000000000000000000000000000000..aaee0b11b43258dcd5e1d84d6bd32666810fb4b7 --- /dev/null +++ b/services/native/include/power_mode_module.h @@ -0,0 +1,91 @@ +/* + * Copyright (c) 2021 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. + */ + + +#ifndef POWER_MODE_THREAD_H +#define POWER_MODE_THREAD_H + +#include +#include +#include +#include + +#include "ipower_mode_callback.h" + +#define FLAG_FALSE (-1) +#define LAST_MODE_FLAG 0 +#define SETTINGS_PRIVIDER_VALUE_LCD_BRIGHTNESS 99 +#define SETTINGS_PRIVIDER_VALUE_VIBRATION 1 +#define SETTINGS_PRIVIDER_VALUE_ROTATION 1 + +namespace OHOS { +namespace PowerMgr { +class PowerModeModule { +public: + enum { + POWER_MODE_MIN = 600, + DEFAULT_MODE = POWER_MODE_MIN, + EXTREAM_MODE, + NORMAL_MODE, + SAVE_MODE, + LOWPOWER_MODE, + POWER_MODE_MAX = LOWPOWER_MODE + }; + PowerModeModule(); + ~PowerModeModule() = default; + void SetModeItem(uint32_t mode); + uint32_t GetModeItem(); + void EnableMode(uint32_t mode); + void AddPowerModeCallback(const sptr& callback); + void DelPowerModeCallback(const sptr& callback); + +private: + using IntentWant = OHOS::AAFwk::Want; + + class CallbackManager : public IRemoteObject::DeathRecipient { + public: + void OnRemoteDied(const wptr& remote) override; + void AddCallback(const sptr& callback); + void RemoveCallback(const sptr& callback); + void WaitingCallback(); + + private: + std::mutex mutex_; + std::set> callbacks_; + }; + + uint32_t mode_; + uint32_t lastMode_; + + void Prepare(); + void PublishPowerModeEvent(); + + CallbackManager callbackMgr_; + void UpdateModepolicy(); + void RunAction(); + void SetDisplayOffTime(); + void SetSleepTime(); + void SetLcdBrightness(); + void SetVibration(); + void OnOffRotation(); + + std::atomic started_; + std::map recoverValue; + std::map::iterator recoverValueiter; + std::mutex mutex_; +}; +} // namespace PowerMgr +} // namespace OHOS +#endif // POWER_MODE_THREAD_H diff --git a/services/native/include/power_mode_policy.h b/services/native/include/power_mode_policy.h new file mode 100644 index 0000000000000000000000000000000000000000..48cd1c95903ef9c2398588d2fa6205d6d12aa81d --- /dev/null +++ b/services/native/include/power_mode_policy.h @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2021 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. + */ + +#ifndef POWER_MODE_POLICY_H +#define POWER_MODE_POLICY_H + +#include +#include +#include +#include "power_save_mode.h" + +#define INIT_VALUE_FALSE (-1) +#define LAST_MODE_FLAG 0 + +namespace OHOS { +namespace PowerMgr { +class PowerModePolicy { +public: + class ServiceType { + public: + static const uint32_t displayOffTime = 101; + static const uint32_t sleepTime = 102; + static const uint32_t autoWindownRoration = 107; + static const uint32_t smartBacklight = 115; + static const uint32_t vibratorsState = 120; + }; + + ~PowerModePolicy() = default; + int32_t GetPowerModeValuePolicy(uint32_t type); + int32_t GetPowerModeRecoverPolicy(uint32_t type); + void SetPowerModePolicy(uint32_t mode, uint32_t lastMode); + void ListenSetting(); + void AddAction(uint32_t type, std::function action); + void TriggerAction(uint32_t type); + void TriggerAllActions(); + bool IsValidType(uint32_t type); + +private: + std::list openPolicy; + std::list closePolicy; + std::list recoverPolicy; + + std::map> actionMap; + std::map valueModePolicy; + std::map recoverModePolicy; + std::map::iterator valueiter; + std::map::iterator recoveriter; + + std::list::iterator openlit; + std::list::iterator closelit; + std::list::iterator recoverlit; + void ReadOpenPolicy(uint32_t mode); + void ReadRecoverPolicy(uint32_t lastMode); + void CompareModeItem(uint32_t mode, uint32_t lastMode); + int32_t GetPolicyFromMap(uint32_t type); + int32_t GetRecoverPolicyFromMap(uint32_t type); +}; +} // namespace PowerMgr +} // namespace OHOS +#endif // POWER_MODE_POLICY_H diff --git a/services/native/include/power_save_mode.h b/services/native/include/power_save_mode.h new file mode 100644 index 0000000000000000000000000000000000000000..518279eaea5262531ea264c53638025322478580 --- /dev/null +++ b/services/native/include/power_save_mode.h @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2021 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. + */ + +#ifndef POWERMGR_POWER_MGR_PowerSaveMode_H +#define POWERMGR_POWER_MGR_PowerSaveMode_H + +#include +#include +#include + +#include +#include "sp_singleton.h" +#include "ipc_object_stub.h" + +#define RETURN_FLAG_FALSE (-1) +#define SLEEP_FILTER_VALUE 124 + +namespace OHOS { +namespace PowerMgr { +struct ModePolicy { + int32_t id; + int32_t value; + int32_t recover_flag; +}; +enum ValueProp { + value, + recover +}; + +class PowerSaveMode : public RefBase { +public: + PowerSaveMode(); + ~PowerSaveMode()=default; + bool GetValuePolicy(std::list &openPolicy, int32_t mode); + bool GetRecoverPolicy(std::list &recoverPolicy, int32_t mode); + bool GetFilterPolicy(std::list &policy, int32_t mode, int32_t value); + int32_t GetSleepTime(int32_t mode); + std::list GetLastMode(); + std::list SetLastMode(std::list &policy); + +private: + std::map> policyCache_; + bool StartXMlParse(std::string path); +}; +} // namespace PowerMgr +} // namespace OHOS +#endif // POWERMGR_POWER_MGR_PowerSaveMode_H diff --git a/services/native/include/power_state_machine.h b/services/native/include/power_state_machine.h index 5aecd6acf9265f97cd587eb383893270ead26c9c..d249a264111594f7e9d3e78ae86ff12fbab909d3 100644 --- a/services/native/include/power_state_machine.h +++ b/services/native/include/power_state_machine.h @@ -25,21 +25,20 @@ #include "power_common.h" #include "power_mgr_monitor.h" #include "power_state_machine_info.h" +#include "running_lock_info.h" + +#define DEFAULT_DISPLAY_OFF_TIME 30000 +#define DEFAULT_SLEEP_TIME 5000 namespace OHOS { namespace PowerMgr { class RunningLockMgr; class PowerMgrService; -enum class ScreenStateType { - SCREEN_OFF = 0, - SCREEN_ON = 1, - SCREEN_DIM = 2, -}; - struct ScreenState { - ScreenStateType state; - int64_t lastUpdateTime; + DisplayState state; + int64_t lastOnTime; + int64_t lastOffTime; }; struct DevicePowerState { @@ -54,11 +53,22 @@ struct DevicePowerState { int64_t lastSuspendDeviceTime; }; -class PowerStateMachine { +enum class TransitResult { + SUCCESS = 0, + ALREADY_IN_STATE = 1, + LOCKING = 2, + HDI_ERR = 3, + OTHER_ERR = 99 +}; + +class PowerStateMachine : public std::enable_shared_from_this { public: explicit PowerStateMachine(const wptr& pms); ~PowerStateMachine(); + static void onSuspend(); + static void onWakeup(); + bool Init(); void SuspendDeviceInner(pid_t pid, int64_t callTimeMs, SuspendDeviceType type, bool suspendImmed, bool ignoreScreenState = false); @@ -67,12 +77,21 @@ public: void RefreshActivityInner(pid_t pid, int64_t callTimeMs, UserActivityType type, bool needChangeBacklight); void ReceiveScreenEvent(bool isScreenOn); bool IsScreenOn(); + PowerState GetState() + { + return currentState_; + }; bool ForceSuspendDeviceInner(pid_t pid, int64_t callTimeMs); void RegisterPowerStateCallback(const sptr& callback); void UnRegisterPowerStateCallback(const sptr& callback); void SetDelayTimer(int64_t delayTime, int32_t event); void CancelDelayTimer(int32_t event); - void HandleDelayTimer(); + void ResetInactiveTimer(); + void ResetSleepTimer(); + void HandleDelayTimer(int32_t event); + bool SetState(PowerState state, StateChangeReason reason, bool force = false); + void SetDisplaySuspend(bool enable); + // only use for test int64_t GetLastSuspendDeviceTime() const { @@ -96,25 +115,64 @@ public: virtual void OnRemoteDied(const wptr& remote); virtual ~PowerStateCallbackDeathRecipient() = default; }; - + void DumpInfo(std::string& result); + void EnableMock(IDeviceStateAction* mockAction); + void SetDisplayOffTime(int64_t time); + void SetSleepTime(int64_t time); private: + class StateController { + public: + StateController(PowerState state, std::shared_ptr owner, + std::function action) + : state_(state), owner_(owner), action_(action) {} + ~StateController() = default; + PowerState GetState() + { + return state_; + } + TransitResult TransitTo(StateChangeReason reason, bool ignoreLock = false); + StateChangeReason lastReason_; + int64_t lastTime_; + protected: + bool CheckState(); + PowerState state_; + std::weak_ptr owner_; + std::function action_; + }; + struct classcomp { bool operator() (const sptr& l, const sptr& r) const { return l->AsObject() < r->AsObject(); } }; - + void InitStateMap(); void NotifyPowerStateChanged(PowerState state); void SendEventToPowerMgrNotify(PowerState state, int64_t callTime); + bool CheckRunningLock(PowerState state); + int64_t GetDisplayOffTime(); + int64_t GetSleepTime(); + void HandleActivityTimeout(); + void HandleActivityOffTimeout(); + void HandleActivitySleepTimeout(); + void HandleSystemWakeup(); + StateChangeReason GetReasonByUserActivity(UserActivityType type); + StateChangeReason GetReasonByWakeType(WakeupDeviceType type); + StateChangeReason GetReasionBySuspendType(SuspendDeviceType type); const wptr pms_; PowerMgrMonitor powerMgrMonitor_; + PowerState currentState_; + std::map>> lockMap_; + std::map> controllerMap_; std::mutex mutex_; DevicePowerState mDeviceState_; sptr powerStateCBDeathRecipient_; std::set, classcomp> powerStateListeners_; std::unique_ptr stateAction_; + int64_t displayOffTime_ {DEFAULT_DISPLAY_OFF_TIME}; + int64_t sleepTime_ {DEFAULT_SLEEP_TIME}; + bool enableDisplaySuspend_ {false}; }; } // namespace PowerMgr } // namespace OHOS diff --git a/services/native/include/powerms_event_handler.h b/services/native/include/powerms_event_handler.h index c52788d5c990d66ba2373834d0c40bda4b8927b1..5bad9de908a6cb7dc6d8f33df3ab0e1ca6d988d9 100644 --- a/services/native/include/powerms_event_handler.h +++ b/services/native/include/powerms_event_handler.h @@ -28,6 +28,9 @@ public: enum { CHECK_RUNNINGLOCK_OVERTIME_MSG = 1, CHECK_USER_ACTIVITY_TIMEOUT_MSG = 2, + CHECK_USER_ACTIVITY_OFF_TIMEOUT_MSG = 3, + CHECK_USER_ACTIVITY_SLEEP_TIMEOUT_MSG = 4, + SYSTEM_WAKE_UP_MSG = 5, }; PowermsEventHandler(const std::shared_ptr& runner, diff --git a/services/native/include/running_lock_inner.h b/services/native/include/running_lock_inner.h index 59008007130cfe54293d4961c947acab0ecf7066..4a39f3686cfdda42dea80262a057a066ba350a54 100644 --- a/services/native/include/running_lock_inner.h +++ b/services/native/include/running_lock_inner.h @@ -95,7 +95,7 @@ private: std::mutex mutex_; RunningLockInfo runningLockInfo_; UserIPCInfo userIPCinfo_; - bool disabled_ {false}; + bool disabled_ {true}; bool reallyLocked_ {false}; bool overTimeFlag_ {false}; int64_t lockTimeMs_ {0}; diff --git a/services/native/include/running_lock_mgr.h b/services/native/include/running_lock_mgr.h index 3c93480d4e4f0d65e3e8268e53970b8950fb1c3a..0165364c6cde5ca16b54dbbfb539e479ef5e6967 100644 --- a/services/native/include/running_lock_mgr.h +++ b/services/native/include/running_lock_mgr.h @@ -31,10 +31,20 @@ namespace OHOS { namespace PowerMgr { class PowerMgrService; class PowermsEventHandler; +class PowerStateMachine; using RunningLockMap = std::map, std::shared_ptr>; class RunningLockMgr { public: + enum class SystemLockType : uint32_t { + SYSTEM_LOCK_APP = 0, + SYSTEM_LOCK_DISPLAY = 1, + SYSTEM_LOCK_OTHER + }; + enum { + PROXIMITY_AWAY = 0, + PROXIMITY_CLOSE + }; using RunningLockProxyMap = std::unordered_map>; explicit RunningLockMgr(const wptr& pms) : pms_(pms) {} ~RunningLockMgr(); @@ -42,6 +52,9 @@ public: void Lock(const sptr& token, const RunningLockInfo& runningLockInfo, const UserIPCInfo &userIPCinfo, uint32_t timeOutMS = 0); void UnLock(const sptr token); + std::shared_ptr CreateRunningLock(const sptr& token, + const RunningLockInfo& runningLockInfo, const UserIPCInfo &userIPCinfo); + void ReleaseLock(const sptr token); uint32_t GetRunningLockNum(RunningLockType type = RunningLockType::RUNNINGLOCK_BUTT); uint32_t GetValidRunningLockNum(RunningLockType type = RunningLockType::RUNNINGLOCK_BUTT); void SetWorkTriggerList(const sptr& token, const WorkTriggerList& workTriggerList); @@ -61,27 +74,107 @@ public: static constexpr uint32_t CHECK_TIMEOUT_INTERVAL_MS = 60 * 1000; static constexpr uint32_t MAX_DUMP_NUM = 10; void CheckOverTime(); + void SetProximity(uint32_t status); void DumpInfo(std::string& result); - + void EnableMock(IRunningLockAction* mockAction); private: + static constexpr const char * const LOCK_TAG_APP = "lock_app"; + static constexpr const char * const LOCK_TAG_DISPLAY = "lock_display"; + static constexpr const char * const LOCK_TAG_OTHER = "lock_other"; + + void InitLocksTypeScreen(); + void InitLocksTypeBackground(); + void nitLocksTypeProximity(); + + class SystemLock { + public: + SystemLock(std::shared_ptr action, const char * const tag) + : action_(action), tag_(tag), locking_(false) {}; + ~SystemLock() = default; + void Lock(); + void Unlock(); + bool IsLocking() + { + return locking_; + }; + void EnableMock(std::shared_ptr& mock) + { + locking_ = false; + action_ = mock; + } + private: + std::shared_ptr action_; + const std::string tag_; + bool locking_; + }; + + class LockCounter { + public: + LockCounter(RunningLockType type, std::function activate) + : type_(type), activate_(activate), counter_(0) {} + ~LockCounter() = default; + uint32_t Increase(); + uint32_t Decrease(); + void Clear(); + uint32_t GetCount() + { + return counter_; + } + RunningLockType GetType() + { + return type_; + } + private: + const RunningLockType type_; + std::shared_ptr action_; + std::function activate_; + uint32_t counter_; + }; + + class ProximityController { + public: + ProximityController(); + ~ProximityController(); + void Enable(); + void Disable(); + bool IsEnabled() + { + return enabled_; + } + bool IsClose(); + void OnClose(); + void OnAway(); + uint32_t GetStatus() + { + return status_ ; + } + void Clear(); + private: + bool enabled_; + bool isClose {false}; + uint32_t status_; + }; + class RunningLockDeathRecipient : public IRemoteObject::DeathRecipient { public: RunningLockDeathRecipient() = default; virtual void OnRemoteDied(const wptr& remote); virtual ~RunningLockDeathRecipient() = default; }; + bool InitLocks(); bool MatchProxyMap(const UserIPCInfo& userIPCinfo); void SetRunningLockDisableFlag(std::shared_ptr& lockInner, bool forceRefresh = false); void LockReally(const sptr& token, std::shared_ptr& lockInner); void UnLockReally(const sptr& token, std::shared_ptr& lockInner); void ProxyRunningLockInner(bool proxyLock); void RemoveAndPostUnlockTask(const sptr& token, uint32_t timeOutMS = 0); - std::shared_ptr CreateRunningLockInner(const sptr& token, - const RunningLockInfo& runningLockInfo, const UserIPCInfo &userIPCinfo); const wptr pms_; + ProximityController proximityController_; std::weak_ptr handler_; std::mutex mutex_; RunningLockMap runningLocks_; + std::map> systemLocks_; + std::map> lockCounters_; RunningLockProxyMap proxyMap_; sptr runningLockDeathRecipient_; std::shared_ptr runningLockAction_; diff --git a/services/native/include/shutdown_service.h b/services/native/include/shutdown_service.h index 3e1c8650bde06e0a1db071afed1cccf7e654d343..7e156b838ec3d73352b1f533aeb88c57e60b5c22 100644 --- a/services/native/include/shutdown_service.h +++ b/services/native/include/shutdown_service.h @@ -35,7 +35,13 @@ public: void Shutdown(const std::string& reason); void AddShutdownCallback(const sptr& callback); void DelShutdownCallback(const sptr& callback); - + bool IsShuttingDown(); + void EnableMock(IDevicePowerAction* mockAction) + { + std::unique_ptr mock(mockAction); + devicePowerAction_ = std::move(mock); + started_ = false; + } private: using IntentWant = OHOS::AAFwk::Want; class CallbackManager : public IRemoteObject::DeathRecipient { diff --git a/services/native/profile/BUILD.gn b/services/native/profile/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..f9e892fa864c6dc1452c5936edc0097f2480cf1e --- /dev/null +++ b/services/native/profile/BUILD.gn @@ -0,0 +1,22 @@ +# Copyright (C) 2021 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("//base/powermgr/power_manager/powermgr.gni") +import("//build/ohos.gni") + +## Install power_mode_config.xml to /system/etc/power_config/power_mode_config.xml +ohos_prebuilt_etc("power_mode_config") { + source = "power_mode_config.xml" + relative_install_dir = "power_config" + part_name = "${powermgr_native_part_name}" + subsystem_name = "powermgr" +} diff --git a/services/native/profile/power_mode_config.xml b/services/native/profile/power_mode_config.xml new file mode 100644 index 0000000000000000000000000000000000000000..18f6320801158f7f17eae9aa1278214503f35e7d --- /dev/null +++ b/services/native/profile/power_mode_config.xml @@ -0,0 +1,40 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/services/native/src/actions/default/BUILD.gn b/services/native/src/actions/default/BUILD.gn index 4006e670a02a7bd170567b56aa00cc91269f1dc9..df2144caa8cf5d18953df79ada9b253da1d6fb5a 100644 --- a/services/native/src/actions/default/BUILD.gn +++ b/services/native/src/actions/default/BUILD.gn @@ -15,7 +15,11 @@ import("//base/powermgr/power_manager/powermgr.gni") import("../actions.gni") config("powermgr_actions_impl_public_config") { - include_dirs = [ "." ] + include_dirs = [ + ".", + "${powermgr_hdi_path}/api/include", + "//base/startup/syspara_lite/adapter/native/syspara/include", + ] } ohos_source_set("${powermgr_actions_default_target}") { @@ -31,13 +35,16 @@ ohos_source_set("${powermgr_actions_default_target}") { public_configs = [ ":powermgr_actions_impl_public_config" ] deps = [ + "${powermgr_hdi_path}/client:power_hdf_client", "../:powermgr_actions_common", + "//base/startup/syspara_lite/interfaces/innerkits/native/syspara:syspara", "//utils/native/base:utils", ] external_deps = [ "display_manager_native:displaymgr", "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", ] part_name = "${powermgr_native_part_name}" diff --git a/services/native/src/actions/default/device_power_action.cpp b/services/native/src/actions/default/device_power_action.cpp index 73df909701c399ab9c15962f9b47c5b6f2a58544..d451612c95c3d92f650b41e7532ef7a393c214a8 100644 --- a/services/native/src/actions/default/device_power_action.cpp +++ b/services/native/src/actions/default/device_power_action.cpp @@ -13,27 +13,38 @@ * limitations under the License. */ -#include "device_power_action.h" - #include #include #include #include +#include "parameters.h" +#include "securec.h" #include "hilog_wrapper.h" +#include "device_power_action.h" namespace OHOS { namespace PowerMgr { void DevicePowerAction::Reboot(const std::string& reason) { + int32_t propertyMaxSize = PROPERTY_MAX_SIZE; + char updateCmd[propertyMaxSize]; + if (snprintf_s(updateCmd, propertyMaxSize, propertyMaxSize - 1, "reboot,%s", reason.c_str()) == 0) { + return; + } POWER_HILOGI(MODULE_SERVICE, "Reboot executing."); - syscall(__NR_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, reason.c_str()); + OHOS::system::SetParameter("sys.powerctl", updateCmd); } void DevicePowerAction::Shutdown(const std::string& reason) { + int32_t propertyMaxSize = PROPERTY_MAX_SIZE; + char updateCmd[propertyMaxSize]; + if (snprintf_s(updateCmd, propertyMaxSize, propertyMaxSize - 1, "shutdown,%s", reason.c_str()) == 0) { + return; + } POWER_HILOGI(MODULE_SERVICE, "Shutdown executing."); - syscall(__NR_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_POWER_OFF, reason.c_str()); + OHOS::system::SetParameter("sys.powerctl", updateCmd); } } // namespace PowerMgr } // namespace OHOS diff --git a/services/native/src/actions/default/device_power_action.h b/services/native/src/actions/default/device_power_action.h index df2808ab5deb65df67bb260c238e93c05e51b77d..1e862469c03390c4db7b6fb4f33c43b85a3e626f 100644 --- a/services/native/src/actions/default/device_power_action.h +++ b/services/native/src/actions/default/device_power_action.h @@ -24,6 +24,7 @@ namespace OHOS { namespace PowerMgr { class DevicePowerAction : public IDevicePowerAction { public: + int PROPERTY_MAX_SIZE = 92; void Reboot(const std::string& reason) override; void Shutdown(const std::string& reason) override; }; diff --git a/services/native/src/actions/default/device_state_action.cpp b/services/native/src/actions/default/device_state_action.cpp index 94abc7aafba2ef68f2b20f2e3aaf83958e53f703..64fc7d216483245d73c8a1a28bc496858812b728 100644 --- a/services/native/src/actions/default/device_state_action.cpp +++ b/services/native/src/actions/default/device_state_action.cpp @@ -15,31 +15,84 @@ #include "device_state_action.h" -#include "display_manager.h" +#include "display_power_mgr_client.h" #include "system_suspend_controller.h" +#include "power_state_machine_info.h" -using namespace OHOS::DisplayMgr; using namespace std; namespace OHOS { namespace PowerMgr { +using namespace DisplayPowerMgr; + void DeviceStateAction::Suspend(int64_t callTimeMs, SuspendDeviceType type, uint32_t flags) { - DisplayManager::SetScreenState(ScreenState::SCREEN_STATE_OFF); - SystemSuspendController::GetInstance().EnableSuspend(); + // Display is controlled by PowerStateMachine + // Don't suspend until GoToSleep is called } void DeviceStateAction::ForceSuspend() { - DisplayManager::SetScreenState(ScreenState::SCREEN_STATE_OFF); - SystemSuspendController::GetInstance().ForceSuspend(); + GoToSleep(nullptr, nullptr, true); } void DeviceStateAction::Wakeup(int64_t callTimeMs, WakeupDeviceType type, const string& details, const string& pkgName) { - SystemSuspendController::GetInstance().DisableSuspend(); - DisplayManager::SetScreenState(ScreenState::SCREEN_STATE_ON); + SystemSuspendController::GetInstance().Wakeup(); +} + +DisplayState DeviceStateAction::GetDisplayState() +{ + DisplayPowerMgr::DisplayState state = DisplayPowerMgrClient::GetInstance().GetDisplayState(); + DisplayState ret = DisplayState::DISPLAY_ON; + switch (state) { + case DisplayPowerMgr::DisplayState::DISPLAY_ON: + ret = DisplayState::DISPLAY_ON; + break; + case DisplayPowerMgr::DisplayState::DISPLAY_DIM: + ret = DisplayState::DISPLAY_DIM; + break; + case DisplayPowerMgr::DisplayState::DISPLAY_OFF: + ret = DisplayState::DISPLAY_OFF; + break; + case DisplayPowerMgr::DisplayState::DISPLAY_SUSPEND: + ret = DisplayState::DISPLAY_SUSPEND; + break; + default: + break; + } + return ret; +} + +uint32_t DeviceStateAction::SetDisplayState(DisplayState state) +{ + DisplayPowerMgr::DisplayState dispState = DisplayPowerMgr::DisplayState::DISPLAY_ON; + switch (state) { + case DisplayState::DISPLAY_ON: + dispState = DisplayPowerMgr::DisplayState::DISPLAY_ON; + break; + case DisplayState::DISPLAY_DIM: + dispState = DisplayPowerMgr::DisplayState::DISPLAY_DIM; + break; + case DisplayState::DISPLAY_OFF: + dispState = DisplayPowerMgr::DisplayState::DISPLAY_OFF; + break; + case DisplayState::DISPLAY_SUSPEND: + dispState = DisplayPowerMgr::DisplayState::DISPLAY_SUSPEND; + break; + default: + break; + } + + bool ret = DisplayPowerMgrClient::GetInstance().SetDisplayState(dispState); + return ret ? ActionResult::SUCCESS : ActionResult::FAILED; +} + +uint32_t DeviceStateAction::GoToSleep(std::function onSuspend, std::function onWakeup, bool force) +{ + SystemSuspendController::GetInstance().Suspend(onSuspend, onWakeup, force); + return ActionResult::SUCCESS; } } // namespace PowerMgr } // namespace OHOS diff --git a/services/native/src/actions/default/device_state_action.h b/services/native/src/actions/default/device_state_action.h index 55bf08d7700f0f63958ada20e1a81df17c132e1b..7e02b25d1458912271ed5865a8c854c6330fe2bf 100644 --- a/services/native/src/actions/default/device_state_action.h +++ b/services/native/src/actions/default/device_state_action.h @@ -26,9 +26,11 @@ public: void ForceSuspend() override; void Wakeup(int64_t callTimeMs, WakeupDeviceType type, const std::string& details, const std::string& pkgName) override; - void RefreshActivity(int64_t callTimeMs __attribute__((__unused__)), - UserActivityType type __attribute__((__unused__)), - uint32_t flags __attribute__((__unused__))) override {} + void RefreshActivity(int64_t callTimeMs, UserActivityType type, + uint32_t flags) override {} + DisplayState GetDisplayState() override; + uint32_t SetDisplayState(DisplayState state) override; + uint32_t GoToSleep(std::function onSuspend, std::function onWakeup, bool force) override; }; } // namespace PowerMgr } // namespace OHOS diff --git a/services/native/src/actions/default/suspend/isuspend_controller.h b/services/native/src/actions/default/suspend/isuspend_controller.h index 4653e93d73d52cf7442a8a8f388177413d6ff234..34b3a41c03759c76f4a1a45c3aecab2a4d1b6aa9 100644 --- a/services/native/src/actions/default/suspend/isuspend_controller.h +++ b/services/native/src/actions/default/suspend/isuspend_controller.h @@ -21,11 +21,12 @@ namespace PowerMgr { namespace Suspend { class ISuspendController { public: + using SuspendCallback = std::function; ISuspendController() = default; virtual ~ISuspendController() = default; - virtual void EnableSuspend() = 0; - virtual void ForceSuspend() = 0; + virtual void Suspend(SuspendCallback onSuspend, SuspendCallback onWakeup, bool force) = 0; + virtual void Wakeup(); virtual void IncSuspendBlockCounter() = 0; virtual void DecSuspendBlockCounter() = 0; }; diff --git a/services/native/src/actions/default/suspend/suspend_controller.cpp b/services/native/src/actions/default/suspend/suspend_controller.cpp index 7aaca14a8b9273940493da6408826e3de8f23fc0..a0d3d64bd88493bf0bb46aa04824fc85ec5c1e6c 100644 --- a/services/native/src/actions/default/suspend/suspend_controller.cpp +++ b/services/native/src/actions/default/suspend/suspend_controller.cpp @@ -26,6 +26,8 @@ namespace OHOS { namespace PowerMgr { namespace Suspend { +bool SuspendController::AutoSuspend::started = false; + SuspendController::SuspendController() { auto f = std::bind(&SuspendController::WaitingSuspendCondition, this); @@ -34,36 +36,77 @@ SuspendController::SuspendController() void SuspendController::AutoSuspend::AutoSuspendLoop() { + POWER_HILOGD(MODULE_SERVICE, "AutoSuspendLoop start"); while (true) { std::this_thread::sleep_for(waitTime_); + if (!started) { + POWER_HILOGW(MODULE_SERVICE, "AutoSuspend is stopped"); + break; + } const std::string wakeupCount = WaitWakeupCount(); if (wakeupCount.empty()) { + POWER_HILOGD(MODULE_SERVICE, "Can't read wake count, continue"); continue; } waitingFunc_(); if (!WriteWakeupCount(wakeupCount)) { + POWER_HILOGD(MODULE_SERVICE, "Can't write wake count, continue"); continue; } + if (onSuspend_ != nullptr) { + onSuspend_(); + } + POWER_HILOGD(MODULE_SERVICE, "SuspendEnter"); bool success = SuspendEnter(); if (!success) { POWER_HILOGE(MODULE_SERVICE, "Start suspend failed!"); } + if (onWakeup_ != nullptr) { + onWakeup_(); + } + break; } + started = false; + + POWER_HILOGD(MODULE_SERVICE, "AutoSuspendLoop end"); } -void SuspendController::AutoSuspend::Start() +void SuspendController::AutoSuspend::Start(SuspendCallback onSuspend, SuspendCallback onWakeup) { - static bool started = false; + POWER_HILOGD(MODULE_SERVICE, "AutoSuspend Start"); + onSuspend_ = onSuspend; + onWakeup_ = onWakeup; if (started) { + POWER_HILOGW(MODULE_SERVICE, "AutoSuspend is already started"); return; } + client_ = std::make_unique(); daemon_ = std::make_unique(&AutoSuspend::AutoSuspendLoop, this); daemon_->detach(); + POWER_HILOGD(MODULE_SERVICE, "AutoSuspend Start detach"); started = true; } +void SuspendController::AutoSuspend::Stop() +{ + POWER_HILOGD(MODULE_SERVICE, "AutoSuspend Stop"); + if (started && daemon_.get() != nullptr) { + POWER_HILOGD(MODULE_SERVICE, "daemon join start"); + started = false; + daemon_->join(); + POWER_HILOGD(MODULE_SERVICE, "daemon join end"); + } +} + bool SuspendController::AutoSuspend::SuspendEnter() { + POWER_HILOGE(MODULE_SERVICE, "SuspendController::AutoSuspend::SuspendEnter: fun is start!"); +#ifndef POWER_SUSPEND_NO_HDI + POWER_HILOGE(MODULE_SERVICE, "Before suspend!"); + ErrCode ret = client_->Suspend(); + POWER_HILOGE(MODULE_SERVICE, "After suspend!"); + return ret == ERR_OK ? true : false; +#else static bool inited = false; static UniqueFd suspendStateFd(TEMP_FAILURE_RETRY(open(SUSPEND_STATE_PATH, O_RDWR | O_CLOEXEC))); if (!inited) { @@ -73,15 +116,24 @@ bool SuspendController::AutoSuspend::SuspendEnter() } inited = true; } + POWER_HILOGE(MODULE_SERVICE, "Before suspend!"); bool ret = SaveStringToFd(suspendStateFd, SUSPEND_STATE); + POWER_HILOGE(MODULE_SERVICE, "After suspend!"); if (!ret) { POWER_HILOGE(MODULE_SERVICE, "Failed to write the suspending state!"); } return ret; +#endif } std::string SuspendController::AutoSuspend::WaitWakeupCount() { + POWER_HILOGI(MODULE_SERVICE, "SuspendController::AutoSuspend::WaitWakeupCount: fun is start"); +#ifndef POWER_SUSPEND_NO_HDI + std::string count; + client_->ReadWakeCount(count); + return count; +#else if (wakeupCountFd < 0) { wakeupCountFd = UniqueFd(TEMP_FAILURE_RETRY(open(WAKEUP_COUNT_PATH, O_RDWR | O_CLOEXEC))); } @@ -92,10 +144,16 @@ std::string SuspendController::AutoSuspend::WaitWakeupCount() return std::string(); } return wakeupCount; +#endif } bool SuspendController::AutoSuspend::WriteWakeupCount(std::string wakeupCount) { + POWER_HILOGI(MODULE_SERVICE, "SuspendController::AutoSuspend::WriteWakeupCount: fun is start"); +#ifndef POWER_SUSPEND_NO_HDI + ErrCode ret = client_->WriteWakeCount(wakeupCount); + return ret == ERR_OK ? true : false; +#else if (wakeupCountFd < 0) { return false; } @@ -104,19 +162,30 @@ bool SuspendController::AutoSuspend::WriteWakeupCount(std::string wakeupCount) POWER_HILOGE(MODULE_SERVICE, "Failed to write the wakeup count!"); } return ret; +#endif } -void SuspendController::EnableSuspend() +void SuspendController::Suspend(SuspendCallback onSuspend, SuspendCallback onWakeup, bool force) { - suspend_->Start(); - POWER_HILOGI(MODULE_SERVICE, "AutoSuspend enabled"); + POWER_HILOGI(MODULE_SERVICE, "SuspendController::Suspend: fun is start"); + if (force) { + POWER_HILOGI(MODULE_SERVICE, "SuspendController Suspend: force"); + if (onSuspend != nullptr) { + onSuspend(); + } + suspend_->SuspendEnter(); + if (onWakeup != nullptr) { + onWakeup(); + } + } else { + POWER_HILOGI(MODULE_SERVICE, "SuspendController Suspend: not force"); + suspend_->Start(onSuspend, onWakeup); + } } -void SuspendController::ForceSuspend() +void SuspendController::Wakeup() { - std::lock_guard lock(suspendMutex_); - bool success = suspend_->SuspendEnter(); - POWER_HILOGI(MODULE_SERVICE, "Forced suspend %{public}s", success ? "succeeded." : "failed!"); + suspend_->Stop(); } void SuspendController::IncSuspendBlockCounter() diff --git a/services/native/src/actions/default/suspend/suspend_controller.h b/services/native/src/actions/default/suspend/suspend_controller.h index 72e19c94d4cc41bc50ccde438a14f742f833506f..a6631166ce1dd0614ebcf7623fa43e72842cee13 100644 --- a/services/native/src/actions/default/suspend/suspend_controller.h +++ b/services/native/src/actions/default/suspend/suspend_controller.h @@ -22,8 +22,12 @@ #include #include +#include "power_hdf_client.h" #include "suspend/isuspend_controller.h" + +#ifdef POWER_SUSPEND_NO_HDI #include "unique_fd.h" +#endif namespace OHOS { namespace PowerMgr { @@ -35,8 +39,8 @@ public: SuspendController(); ~SuspendController() = default; - void EnableSuspend() override; - void ForceSuspend() override; + void Suspend(SuspendCallback onSuspend, SuspendCallback onWakeup, bool force) override; + void Wakeup() override; void IncSuspendBlockCounter() override; void DecSuspendBlockCounter() override; @@ -48,21 +52,28 @@ private: ~AutoSuspend() = default; void AutoSuspendLoop(); - void Start(); + void Start(SuspendCallback onSuspend, SuspendCallback onWakeup); + void Stop(); bool SuspendEnter(); private: + static bool started; std::string WaitWakeupCount(); bool WriteWakeupCount(std::string wakeupCount); +#ifdef POWER_SUSPEND_NO_HDI static constexpr const char * const SUSPEND_STATE = "mem"; static constexpr const char * const SUSPEND_STATE_PATH = "/sys/power/state"; static constexpr const char * const WAKEUP_COUNT_PATH = "/sys/power/wakeup_count"; - UniqueFd wakeupCountFd {-1}; +#endif + std::chrono::milliseconds waitTime_ {100ms}; std::unique_ptr daemon_; WaitingSuspendConditionFunc waitingFunc_; + SuspendCallback onSuspend_; + SuspendCallback onWakeup_; + std::unique_ptr client_; }; bool SuspendConditionSatisfied(); diff --git a/services/native/src/actions/default/system_suspend_controller.cpp b/services/native/src/actions/default/system_suspend_controller.cpp index bdc9deaa57534733af0bb1901963cb95e26627ae..a948a9514a499471364ea0b07ab4484d949cdd90 100644 --- a/services/native/src/actions/default/system_suspend_controller.cpp +++ b/services/native/src/actions/default/system_suspend_controller.cpp @@ -23,44 +23,35 @@ namespace PowerMgr { SystemSuspendController::SystemSuspendController() { sc_ = std::make_shared(); - rlh_ = std::make_unique(sc_); - rlh_->Acquire(WAKEUP_HOLDER); + client_ = std::make_unique(); } SystemSuspendController::~SystemSuspendController() = default; -void SystemSuspendController::EnableSuspend() +void SystemSuspendController::Suspend(std::function onSuspend, + std::function onWakeup, bool force) { - std::lock_guard lock(mutex_); - sc_->EnableSuspend(); - if (!suspendEnabled_) { - rlh_->Release(WAKEUP_HOLDER); - suspendEnabled_ = true; - } + sc_->Suspend(onSuspend, onWakeup, force); } -void SystemSuspendController::ForceSuspend() +void SystemSuspendController::Wakeup() { - sc_->ForceSuspend(); + sc_->Wakeup(); } -void SystemSuspendController::DisableSuspend() +void SystemSuspendController::AcquireRunningLock(const std::string& name) { - std::lock_guard lock(mutex_); - if (suspendEnabled_) { - rlh_->Acquire(WAKEUP_HOLDER); - suspendEnabled_ = false; - } + client_->WakeLock(name); } -void SystemSuspendController::AcquireRunningLock(const std::string& name) +void SystemSuspendController::ReleaseRunningLock(const std::string& name) { - rlh_->Acquire(name); + client_->WakeUnlock(name); } -void SystemSuspendController::ReleaseRunningLock(const std::string& name) +void SystemSuspendController::Dump(std::string& info) { - rlh_->Release(name); + client_->Dump(info); } } // namespace PowerMgr } // namespace OHOS diff --git a/services/native/src/actions/default/system_suspend_controller.h b/services/native/src/actions/default/system_suspend_controller.h index 42784b78729a0909c0e2e86669e2623f8b147801..a5a2b8de91814677789bd1e09aa421f4c5e1f225 100644 --- a/services/native/src/actions/default/system_suspend_controller.h +++ b/services/native/src/actions/default/system_suspend_controller.h @@ -21,6 +21,7 @@ #include +#include "power_hdf_client.h" #include "suspend/irunning_lock_hub.h" #include "suspend/isuspend_controller.h" @@ -28,20 +29,19 @@ namespace OHOS { namespace PowerMgr { class SystemSuspendController : public DelayedRefSingleton { public: - void EnableSuspend(); - void ForceSuspend(); - void DisableSuspend(); + void Suspend(std::function onSuspend, std::function onWakeup, bool force); + void Wakeup(); void AcquireRunningLock(const std::string& name); void ReleaseRunningLock(const std::string& name); + void Dump(std::string& info); private: DECLARE_DELAYED_REF_SINGLETON(SystemSuspendController); inline static const std::string WAKEUP_HOLDER = "OHOSPowerMgr.WakeupHolder"; - bool suspendEnabled_ {false}; std::mutex mutex_; - std::unique_ptr rlh_; std::shared_ptr sc_; + std::unique_ptr client_; }; } // namespace PowerMgr } // namespace OHOS diff --git a/services/native/src/power_mgr_dumper.cpp b/services/native/src/power_mgr_dumper.cpp index 25c5703e688559348cdd516ccfe05e4d2af0c886..081ee57bf280e3bead1dece21e0f0fe6ec9324fe 100644 --- a/services/native/src/power_mgr_dumper.cpp +++ b/services/native/src/power_mgr_dumper.cpp @@ -17,12 +17,15 @@ #include "power_common.h" #include "power_mgr_service.h" +#include "system_suspend_controller.h" namespace OHOS { namespace PowerMgr { namespace { const std::string ARGS_HELP = "-h"; const std::string ARGS_RUNNINGLOCK = "-runninglock"; +const std::string ARGS_STATE = "-state"; +const std::string ARGS_HDF = "-hdf"; } bool PowerMgrDumper::Dump(const std::vector& args, std::string& result) @@ -33,16 +36,26 @@ bool PowerMgrDumper::Dump(const std::vector& args, std::string& res ShowUsage(result); return true; } - if (args[0] == ARGS_RUNNINGLOCK) { - auto pms = DelayedSpSingleton::GetInstance(); - if (pms == nullptr) { - return true; - } - auto runningLockMgr = pms->GetRunningLockMgr(); - if (runningLockMgr == nullptr) { - return true; + auto pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + return true; + } + for (auto it = args.begin(); it != args.end(); it++) { + if (*it == ARGS_RUNNINGLOCK) { + auto runningLockMgr = pms->GetRunningLockMgr(); + if (runningLockMgr == nullptr) { + continue; + } + runningLockMgr->DumpInfo(result); + } else if (*it == ARGS_STATE) { + auto stateMachine = pms->GetPowerStateMachine(); + if (stateMachine == nullptr) { + continue; + } + stateMachine->DumpInfo(result); + } else if (*it == ARGS_HDF) { + SystemSuspendController::GetInstance().Dump(result); } - runningLockMgr->DumpInfo(result); } return true; } diff --git a/services/native/src/power_mgr_service.cpp b/services/native/src/power_mgr_service.cpp index 535e8682ff7ba6d45b0dcfc798d34756138c2a0a..d3a3ba81cef955e2260ecd8ebb78263e0ecc9d18 100644 --- a/services/native/src/power_mgr_service.cpp +++ b/services/native/src/power_mgr_service.cpp @@ -33,7 +33,7 @@ namespace PowerMgr { namespace { const std::string POWERMGR_SERVICE_NAME = "PowerMgrService"; const std::string TASK_RUNNINGLOCK_UNLOCK = "RunningLock_UnLock"; -constexpr int APP_FIRST_UID = 10000; +constexpr int APP_FIRST_UID = APP_FIRST_UID_VALUE; auto pms = DelayedSpSingleton::GetInstance(); const bool G_REGISTER_RESULT = SystemAbility::MakeAndRegisterAbility(pms.GetRefPtr()); } @@ -88,6 +88,11 @@ bool PowerMgrService::Init() if (!PowerStateMachineInit()) { POWER_HILOGE(MODULE_SERVICE, "power state machine init fail!"); } + if (DelayedSpSingleton::GetInstance()) { + powerModeModule_.SetModeItem(PowerModeModule::DEFAULT_MODE); + } else { + POWER_HILOGE(MODULE_SERVICE, "power mode init fail!"); + } POWER_HILOGI(MODULE_SERVICE, "Init success"); return true; } @@ -121,14 +126,20 @@ void PowerMgrService::OnStop() int32_t PowerMgrService::Dump(int32_t fd, const std::vector& args) { + std::lock_guard lock(mutex_); std::vector argsInStr; - std::transform(args.begin(), args.end(), std::back_inserter(argsInStr), [](const std::u16string &arg) { - return Str16ToStr8(arg); + std::transform(args.begin(), args.end(), std::back_inserter(argsInStr), + [](const std::u16string &arg) { + std::string ret = Str16ToStr8(arg); + POWER_HILOGI(MODULE_SERVICE, "arg: %{public}s", ret.c_str()); + return ret; }); std::string result; PowerMgrDumper::Dump(argsInStr, result); if (!SaveStringToFd(fd, result)) { POWER_HILOGE(MODULE_SERVICE, "PowerMgrService::Dump failed, save to fd failed."); + POWER_HILOGE(MODULE_SERVICE, "Dump Info:\n"); + POWER_HILOGE(MODULE_SERVICE, "%{public}s", result.c_str()); return ERR_OK; } return ERR_OK; @@ -136,78 +147,140 @@ int32_t PowerMgrService::Dump(int32_t fd, const std::vector& arg void PowerMgrService::RebootDevice(const std::string& reason) { + std::lock_guard lock(mutex_); pid_t pid = IPCSkeleton::GetCallingPid(); auto uid = IPCSkeleton::GetCallingUid(); if (reason.find("recovery") != std::string::npos) { if (!Permission::CheckCallingPermission("ohos.permission.REBOOT_RECOVERY")) { - POWER_HILOGE(MODULE_SERVICE, "%{public}s Request failed, %{public}d permission check fail", __func__, pid); + POWER_HILOGE(MODULE_SERVICE, + "%{public}s Request failed, %{public}d permission check fail", + __func__, pid); return; } } else { - if ((uid >= APP_FIRST_UID) && !Permission::CheckCallingPermission("ohos.permission.REBOOT")) { - POWER_HILOGE(MODULE_SERVICE, "%{public}s Request failed, %{public}d permission check fail", __func__, pid); + if ((uid >= APP_FIRST_UID) + && !Permission::CheckCallingPermission("ohos.permission.REBOOT")) { + POWER_HILOGE(MODULE_SERVICE, + "%{public}s Request failed, %{public}d permission check fail", + __func__, pid); return; } } + POWER_HILOGI(MODULE_SERVICE, "Cancel auto sleep timer"); + powerStateMachine_->CancelDelayTimer( + PowermsEventHandler::CHECK_USER_ACTIVITY_TIMEOUT_MSG); + powerStateMachine_->CancelDelayTimer( + PowermsEventHandler::CHECK_USER_ACTIVITY_OFF_TIMEOUT_MSG); + powerStateMachine_->CancelDelayTimer( + PowermsEventHandler::CHECK_USER_ACTIVITY_SLEEP_TIMEOUT_MSG); + POWER_HILOGI(MODULE_SERVICE, "PID: %{public}d Call %{public}s !", pid, __func__); shutdownService_.Reboot(reason); } void PowerMgrService::ShutDownDevice(const std::string& reason) { + std::lock_guard lock(mutex_); pid_t pid = IPCSkeleton::GetCallingPid(); auto uid = IPCSkeleton::GetCallingUid(); - if ((uid >= APP_FIRST_UID) && !Permission::CheckCallingPermission("ohos.permission.REBOOT")) { - POWER_HILOGE(MODULE_SERVICE, "%{public}s Request failed, %{public}d permission check fail", __func__, pid); + if ((uid >= APP_FIRST_UID) + && !Permission::CheckCallingPermission("ohos.permission.SHUTDOWN")) { + POWER_HILOGE(MODULE_SERVICE, + "%{public}s Request failed, %{public}d permission check fail", + __func__, pid); return; } + POWER_HILOGI(MODULE_SERVICE, "Cancel auto sleep timer"); + powerStateMachine_->CancelDelayTimer( + PowermsEventHandler::CHECK_USER_ACTIVITY_TIMEOUT_MSG); + powerStateMachine_->CancelDelayTimer( + PowermsEventHandler::CHECK_USER_ACTIVITY_OFF_TIMEOUT_MSG); + powerStateMachine_->CancelDelayTimer( + PowermsEventHandler::CHECK_USER_ACTIVITY_SLEEP_TIMEOUT_MSG); + POWER_HILOGI(MODULE_SERVICE, "PID: %{public}d Call %{public}s !", pid, __func__); shutdownService_.Shutdown(reason); } -void PowerMgrService::SuspendDevice(int64_t callTimeMs, SuspendDeviceType reason, bool suspendImmed) +void PowerMgrService::SuspendDevice(int64_t callTimeMs, + SuspendDeviceType reason, + bool suspendImmed) { + std::lock_guard lock(mutex_); auto uid = IPCSkeleton::GetCallingUid(); if (uid >= APP_FIRST_UID) { - POWER_HILOGE(MODULE_SERVICE, "%{public}s Request failed, illegal calling uid %{public}d.", __func__, uid); + POWER_HILOGE(MODULE_SERVICE, + "%{public}s Request failed, illegal calling uid %{public}d.", + __func__, uid); + return; + } + if (shutdownService_.IsShuttingDown()) { + POWER_HILOGI(MODULE_SERVICE, "System is shutting down, can't suspend"); return; } pid_t pid = IPCSkeleton::GetCallingPid(); powerStateMachine_->SuspendDeviceInner(pid, callTimeMs, reason, suspendImmed); } -void PowerMgrService::WakeupDevice(int64_t callTimeMs, WakeupDeviceType reason, const std::string& details) +void PowerMgrService::WakeupDevice(int64_t callTimeMs, + WakeupDeviceType reason, + const std::string& details) { + std::lock_guard lock(mutex_); auto uid = IPCSkeleton::GetCallingUid(); if (uid >= APP_FIRST_UID) { - POWER_HILOGE(MODULE_SERVICE, "%{public}s Request failed, illegal calling uid %{public}d.", __func__, uid); + POWER_HILOGE(MODULE_SERVICE, + "%{public}s Request failed, illegal calling uid %{public}d.", + __func__, uid); return; } pid_t pid = IPCSkeleton::GetCallingPid(); powerStateMachine_->WakeupDeviceInner(pid, callTimeMs, reason, details, "OHOS"); } -void PowerMgrService::RefreshActivity(int64_t callTimeMs, UserActivityType type, bool needChangeBacklight) +void PowerMgrService::RefreshActivity(int64_t callTimeMs, + UserActivityType type, + bool needChangeBacklight) { + std::lock_guard lock(mutex_); auto uid = IPCSkeleton::GetCallingUid(); - if ((uid >= APP_FIRST_UID) || !Permission::CheckCallingPermission("ohos.permission.REFRESH_USER_ACTION")) { - POWER_HILOGE(MODULE_SERVICE, "%{public}s Request failed, illegal calling uid %{public}d.", __func__, uid); + if ((uid >= APP_FIRST_UID) + || !Permission::CheckCallingPermission("ohos.permission.REFRESH_USER_ACTION")) { + POWER_HILOGE(MODULE_SERVICE, + "%{public}s Request failed, illegal calling uid %{public}d.", + __func__, uid); return; } pid_t pid = IPCSkeleton::GetCallingPid(); powerStateMachine_->RefreshActivityInner(pid, callTimeMs, type, needChangeBacklight); } +PowerState PowerMgrService::GetState() +{ + std::lock_guard lock(mutex_); + POWER_HILOGI(MODULE_SERVICE, "GetState"); + return powerStateMachine_->GetState(); +} + bool PowerMgrService::IsScreenOn() { + std::lock_guard lock(mutex_); + POWER_HILOGI(MODULE_SERVICE, "IsScreenOn"); return powerStateMachine_->IsScreenOn(); } bool PowerMgrService::ForceSuspendDevice(int64_t callTimeMs) { + std::lock_guard lock(mutex_); auto uid = IPCSkeleton::GetCallingUid(); if (uid >= APP_FIRST_UID) { - POWER_HILOGE(MODULE_SERVICE, "%{public}s Request failed, illegal calling uid %{public}d.", __func__, uid); + POWER_HILOGE(MODULE_SERVICE, + "%{public}s Request failed, illegal calling uid %{public}d.", + __func__, uid); + return false; + } + if (shutdownService_.IsShuttingDown()) { + POWER_HILOGI(MODULE_SERVICE, "System is shutting down, can't force suspend"); return false; } pid_t pid = IPCSkeleton::GetCallingPid(); @@ -220,46 +293,105 @@ inline void PowerMgrService::FillUserIPCInfo(UserIPCInfo &userIPCinfo) userIPCinfo.uid = IPCSkeleton::GetCallingUid(); } -void PowerMgrService::Lock(const sptr& token, const RunningLockInfo& runningLockInfo, uint32_t timeOutMS) +void PowerMgrService::CreateRunningLock(const sptr& token, + const RunningLockInfo& runningLockInfo) +{ + std::lock_guard lock(mutex_); + auto uid = IPCSkeleton::GetCallingUid(); + if ((uid >= APP_FIRST_UID) + && !Permission::CheckCallingPermission("ohos.permission.RUNNING_LOCK")) { + POWER_HILOGE(MODULE_SERVICE, + "%{public}s Request failed, %{public}d permission check fail", + __func__, uid); + return; + } + + POWER_HILOGI(MODULE_SERVICE, "%{public}s :name = %s, type = %d", __func__, + runningLockInfo.name.c_str(), runningLockInfo.type); + + UserIPCInfo userIPCInfo; + FillUserIPCInfo(userIPCInfo); + runningLockMgr_->CreateRunningLock(token, runningLockInfo, userIPCInfo); +} + +void PowerMgrService::ReleaseRunningLock(const sptr& token) +{ + std::lock_guard lock(mutex_); + auto uid = IPCSkeleton::GetCallingUid(); + if ((uid >= APP_FIRST_UID) + && !Permission::CheckCallingPermission("ohos.permission.RUNNING_LOCK")) { + POWER_HILOGE(MODULE_SERVICE, + "%{public}s Request failed, %{public}d permission check fail", + __func__, uid); + return; + } + + POWER_HILOGI(MODULE_SERVICE, "%{public}s called.", __func__); + runningLockMgr_->ReleaseLock(token); +} + +bool PowerMgrService::IsRunningLockTypeSupported(uint32_t type) +{ + std::lock_guard lock(mutex_); + if (type >= static_cast(RunningLockType::RUNNINGLOCK_BUTT)) { + return false; + } + return true; +} + +void PowerMgrService::Lock(const sptr& token, + const RunningLockInfo& runningLockInfo, + uint32_t timeOutMS) { + std::lock_guard lock(mutex_); auto uid = IPCSkeleton::GetCallingUid(); - if ((uid >= APP_FIRST_UID) && !Permission::CheckCallingPermission("ohos.permission.RUNNING_LOCK")) { - POWER_HILOGE(MODULE_SERVICE, "%{public}s Request failed, %{public}d permission check fail", __func__, uid); + if ((uid >= APP_FIRST_UID) + && !Permission::CheckCallingPermission("ohos.permission.RUNNING_LOCK")) { + POWER_HILOGE(MODULE_SERVICE, + "%{public}s Request failed, %{public}d permission check fail", + __func__, uid); return; } - POWER_HILOGI(MODULE_SERVICE, "%{public}s :timeOutMS = %d, name = %s, type = %d", __func__, - timeOutMS, runningLockInfo.name.c_str(), runningLockInfo.type); + POWER_HILOGI(MODULE_SERVICE, + "%{public}s :timeOutMS = %{public}d, name = %{public}s, type = %{public}d", + __func__, + timeOutMS, + runningLockInfo.name.c_str(), + runningLockInfo.type); UserIPCInfo userIPCInfo; FillUserIPCInfo(userIPCInfo); runningLockMgr_->Lock(token, runningLockInfo, userIPCInfo, timeOutMS); - // notify runninglock is changed, true means unlock, false means lock - NotifyRunningLockChanged(false); } void PowerMgrService::UnLock(const sptr& token) { + std::lock_guard lock(mutex_); auto uid = IPCSkeleton::GetCallingUid(); - if ((uid >= APP_FIRST_UID) && !Permission::CheckCallingPermission("ohos.permission.RUNNING_LOCK")) { - POWER_HILOGE(MODULE_SERVICE, "%{public}s Request failed, %{public}d permission check fail", __func__, uid); + if ((uid >= APP_FIRST_UID) + && !Permission::CheckCallingPermission("ohos.permission.RUNNING_LOCK")) { + POWER_HILOGE(MODULE_SERVICE, + "%{public}s Request failed, %{public}d permission check fail", + __func__, uid); return; } POWER_HILOGI(MODULE_SERVICE, "%{public}s called.", __func__); runningLockMgr_->UnLock(token); - // notify runninglock is changed, true means unlock, false means lock - NotifyRunningLockChanged(true); } void PowerMgrService::ForceUnLock(const sptr& token) { + std::lock_guard lock(mutex_); POWER_HILOGI(MODULE_SERVICE, "%{public}s called.", __func__); - UnLock(token); + runningLockMgr_->UnLock(token); + runningLockMgr_->ReleaseLock(token); } bool PowerMgrService::IsUsed(const sptr& token) { + std::lock_guard lock(mutex_); POWER_HILOGI(MODULE_SERVICE, "%{public}s called.", __func__); return runningLockMgr_->IsUsed(token); } @@ -268,20 +400,29 @@ void PowerMgrService::NotifyRunningLockChanged(bool isUnLock) { if (isUnLock) { // When unlock we try to suspend - if (!runningLockMgr_->ExistValidRunningLock() && !powerStateMachine_->IsScreenOn()) { - // runninglock is empty and Screen is off, so we try to suspend device from Z side. - POWER_HILOGI(MODULE_SERVICE, "%{public}s :RunningLock is empty, try to suspend from Z Side!", __func__); + if (!runningLockMgr_->ExistValidRunningLock() + && !powerStateMachine_->IsScreenOn()) { + // runninglock is empty and Screen is off, + // so we try to suspend device from Z side. + POWER_HILOGI(MODULE_SERVICE, + "%{public}s :RunningLock is empty, try to suspend from Z Side!", + __func__); powerStateMachine_->SuspendDeviceInner(getpid(), GetTickCount(), - SuspendDeviceType::SUSPEND_DEVICE_REASON_MIN, true, true); + SuspendDeviceType::SUSPEND_DEVICE_REASON_MIN, true, true); } } } -void PowerMgrService::SetWorkTriggerList(const sptr& token, const WorkTriggerList& workTriggerList) +void PowerMgrService::SetWorkTriggerList(const sptr& token, + const WorkTriggerList& workTriggerList) { + std::lock_guard lock(mutex_); auto uid = IPCSkeleton::GetCallingUid(); - if ((uid >= APP_FIRST_UID) && !Permission::CheckCallingPermission("ohos.permission.RUNNING_LOCK")) { - POWER_HILOGE(MODULE_SERVICE, "%{public}s Request failed, %{public}d permission check fail", __func__, uid); + if ((uid >= APP_FIRST_UID) + && !Permission::CheckCallingPermission("ohos.permission.RUNNING_LOCK")) { + POWER_HILOGE(MODULE_SERVICE, + "%{public}s Request failed, %{public}d permission check fail", + __func__, uid); return; } @@ -291,9 +432,12 @@ void PowerMgrService::SetWorkTriggerList(const sptr& token, const void PowerMgrService::ProxyRunningLock(bool proxyLock, pid_t uid, pid_t pid) { + std::lock_guard lock(mutex_); auto calllingUid = IPCSkeleton::GetCallingUid(); if (calllingUid >= APP_FIRST_UID) { - POWER_HILOGE(MODULE_SERVICE, "%{public}s Request failed, illegal calling uid %{public}d.", __func__, + POWER_HILOGE(MODULE_SERVICE, + "%{public}s Request failed, illegal calling uid %{public}d.", + __func__, calllingUid); return; } @@ -302,9 +446,13 @@ void PowerMgrService::ProxyRunningLock(bool proxyLock, pid_t uid, pid_t pid) void PowerMgrService::RegisterPowerStateCallback(const sptr& callback) { + std::lock_guard lock(mutex_); auto uid = IPCSkeleton::GetCallingUid(); - if ((uid >= APP_FIRST_UID) && !Permission::CheckCallingPermission("ohos.permission.POWER_MANAGER")) { - POWER_HILOGE(MODULE_SERVICE, "%{public}s Request failed, %{public}d permission check fail", __func__, uid); + if ((uid >= APP_FIRST_UID) + && !Permission::CheckCallingPermission("ohos.permission.POWER_MANAGER")) { + POWER_HILOGE(MODULE_SERVICE, + "%{public}s Request failed, %{public}d permission check fail", + __func__, uid); return; } powerStateMachine_->RegisterPowerStateCallback(callback); @@ -312,9 +460,13 @@ void PowerMgrService::RegisterPowerStateCallback(const sptr void PowerMgrService::UnRegisterPowerStateCallback(const sptr& callback) { + std::lock_guard lock(mutex_); auto uid = IPCSkeleton::GetCallingUid(); - if ((uid >= APP_FIRST_UID) && !Permission::CheckCallingPermission("ohos.permission.POWER_MANAGER")) { - POWER_HILOGE(MODULE_SERVICE, "%{public}s Request failed, %{public}d permission check fail", __func__, uid); + if ((uid >= APP_FIRST_UID) + && !Permission::CheckCallingPermission("ohos.permission.POWER_MANAGER")) { + POWER_HILOGE(MODULE_SERVICE, + "%{public}s Request failed, %{public}d permission check fail", + __func__, uid); return; } powerStateMachine_->UnRegisterPowerStateCallback(callback); @@ -322,6 +474,7 @@ void PowerMgrService::UnRegisterPowerStateCallback(const sptr& callback) { + std::lock_guard lock(mutex_); auto uid = IPCSkeleton::GetCallingUid(); if (uid >= APP_FIRST_UID) { POWER_HILOGE(MODULE_SERVICE, "Register failed, %{public}d fail", uid); @@ -333,6 +486,7 @@ void PowerMgrService::RegisterShutdownCallback(const sptr& ca void PowerMgrService::UnRegisterShutdownCallback(const sptr& callback) { + std::lock_guard lock(mutex_); auto uid = IPCSkeleton::GetCallingUid(); if (uid >= APP_FIRST_UID) { POWER_HILOGE(MODULE_SERVICE, "UnRegister failed, %{public}d fail", uid); @@ -341,5 +495,56 @@ void PowerMgrService::UnRegisterShutdownCallback(const sptr& POWER_HILOGE(MODULE_SERVICE, "UnRegister shutdown callback: %{public}d", uid); shutdownService_.DelShutdownCallback(callback); } + +void PowerMgrService::RegisterPowerModeCallback(const sptr& callback) +{ + std::lock_guard lock(mutex_); + auto uid = IPCSkeleton::GetCallingUid(); + if (uid >= APP_FIRST_UID) { + POWER_HILOGE(MODULE_SERVICE, "Register failed, %{public}d fail", uid); + return; + } + POWER_HILOGE(MODULE_SERVICE, "Register power mode callback: %{public}d", uid); + powerModeModule_.AddPowerModeCallback(callback); +} + +void PowerMgrService::UnRegisterPowerModeCallback(const sptr& callback) +{ + std::lock_guard lock(mutex_); + auto uid = IPCSkeleton::GetCallingUid(); + if (uid >= APP_FIRST_UID) { + POWER_HILOGE(MODULE_SERVICE, "UnRegister failed, %{public}d fail", uid); + return; + } + POWER_HILOGE(MODULE_SERVICE, "UnRegister power mode callback: %{public}d", uid); + powerModeModule_.DelPowerModeCallback(callback); +} + +void PowerMgrService::SetDisplaySuspend(bool enable) +{ + std::lock_guard lock(mutex_); + auto uid = IPCSkeleton::GetCallingUid(); + if (uid >= APP_FIRST_UID) { + POWER_HILOGE(MODULE_SERVICE, "SetDisplaySuspend failed, %{public}d fail", uid); + return; + } + powerStateMachine_->SetDisplaySuspend(enable); +} + +void PowerMgrService::SetDeviceMode(const uint32_t& mode) +{ + std::lock_guard lock(mutex_); + pid_t pid = IPCSkeleton::GetCallingPid(); + POWER_HILOGI(MODULE_SERVICE, "PID: %{public}d Call %{public}s !", pid, __func__); + powerModeModule_.SetModeItem(mode); +} + +uint32_t PowerMgrService::GetDeviceMode() +{ + std::lock_guard lock(mutex_); + pid_t pid = IPCSkeleton::GetCallingPid(); + POWER_HILOGI(MODULE_SERVICE, "PID: %{public}d Call %{public}s !", pid, __func__); + return powerModeModule_.GetModeItem(); +} } // namespace PowerMgr } // namespace OHOS diff --git a/services/native/src/power_mode_module.cpp b/services/native/src/power_mode_module.cpp new file mode 100644 index 0000000000000000000000000000000000000000..af9d5d044edea8592e98d824bcdda15ffd3db576 --- /dev/null +++ b/services/native/src/power_mode_module.cpp @@ -0,0 +1,356 @@ +/* + * Copyright (c) 2021 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 "power_mode_module.h" + +#include "hilog_wrapper.h" + +#include "power_common.h" +#include "power_mode_policy.h" +#include "power_mgr_service.h" +#include "power_save_mode.h" +#include "power_state_machine.h" + +#include "singleton.h" + +using namespace OHOS::AAFwk; +using namespace OHOS::EventFwk; + +using namespace std; + +using namespace OHOS; + +namespace OHOS { +namespace PowerMgr { +PowerModeModule::PowerModeModule() : started_(false) +{ + POWER_HILOGI(MODULE_SERVICE, "PowerModeModule create"); + auto policy = DelayedSingleton::GetInstance(); + policy->AddAction(PowerModePolicy::ServiceType::displayOffTime, + std::bind(&PowerModeModule::SetDisplayOffTime, this)); + policy->AddAction(PowerModePolicy::ServiceType::sleepTime, + std::bind(&PowerModeModule::SetSleepTime, this)); + policy->AddAction(PowerModePolicy::ServiceType::smartBacklight, + std::bind(&PowerModeModule::SetLcdBrightness, this)); + policy->AddAction(PowerModePolicy::ServiceType::vibratorsState, + std::bind(&PowerModeModule::SetVibration, this)); + policy->AddAction(PowerModePolicy::ServiceType::autoWindownRoration, + std::bind(&PowerModeModule::OnOffRotation, this)); +} + +void PowerModeModule::SetModeItem(uint32_t mode) +{ + POWER_HILOGD(MODULE_SERVICE, "Set Mode Item : %{public}u", mode); + + /* Same as the previous mode */ + if (this->mode_ == mode) { + return; + } + + /* If it's a valid mode */ + if (mode < POWER_MODE_MIN || mode > POWER_MODE_MAX) { + POWER_HILOGE(MODULE_SERVICE, "Unknow mode %{public}d", mode); + return; + } + + /* start set mode thread */ + EnableMode(mode); +} + +uint32_t PowerModeModule::GetModeItem() +{ + POWER_HILOGD(MODULE_SERVICE, "Get Mode Item : %{public}u", mode_); + /* get power mode */ + return mode_; +} + +void PowerModeModule::EnableMode(uint32_t mode) +{ + if (started_) { + POWER_HILOGE(MODULE_SERVICE, "Power Mode is already running."); + return; + } + + started_ = true; + mode_ = mode; + + /* Update power mode policy */ + UpdateModepolicy(); + + /* Send state change */ + Prepare(); + + /* Set action */ + RunAction(); + + this->lastMode_ = mode; + started_ = false; +} + +void PowerModeModule::UpdateModepolicy() +{ + /* update policy */ + DelayedSingleton::GetInstance()->SetPowerModePolicy(this->mode_, this->lastMode_); +} + +void PowerModeModule::AddPowerModeCallback(const sptr& callback) +{ + callbackMgr_.AddCallback(callback); +} + +void PowerModeModule::DelPowerModeCallback(const sptr& callback) +{ + callbackMgr_.RemoveCallback(callback); +} + +void PowerModeModule::Prepare() +{ + PublishPowerModeEvent(); + callbackMgr_.WaitingCallback(); +} + +void PowerModeModule::CallbackManager::AddCallback(const sptr& callback) +{ + unique_lock lock(mutex_); + RETURN_IF((callback == nullptr) || (callback->AsObject() == nullptr)); + auto object = callback->AsObject(); + auto retIt = callbacks_.insert(object); + if (retIt.second) { + object->AddDeathRecipient(this); + } + POWER_HILOGI(MODULE_SERVICE, "object = %{public}p, callback = %{public}p, callbacks.size = %{public}zu," + " insertOk = %{public}d", object.GetRefPtr(), + callback.GetRefPtr(), callbacks_.size(), retIt.second); +} + +void PowerModeModule::CallbackManager::RemoveCallback(const sptr& callback) +{ + unique_lock lock(mutex_); + RETURN_IF((callback == nullptr) || (callback->AsObject() == nullptr)); + auto object = callback->AsObject(); + auto it = find(callbacks_.begin(), callbacks_.end(), object); + if (it != callbacks_.end()) { + callbacks_.erase(it); + object->RemoveDeathRecipient(this); + } + POWER_HILOGI(MODULE_SERVICE, "object = %{public}p, callback = %{public}p, callbacks.size = %{public}zu,", + object.GetRefPtr(), callback.GetRefPtr(), callbacks_.size()); +} + +void PowerModeModule::CallbackManager::OnRemoteDied(const wptr& remote) +{ + POWER_HILOGI(MODULE_SERVICE, "OnRemoteDied"); + RETURN_IF(remote.promote() == nullptr); + RemoveCallback(iface_cast(remote.promote())); +} + +void PowerModeModule::CallbackManager::WaitingCallback() +{ + POWER_HILOGD(MODULE_SERVICE, "mode callback started."); + unique_lock lock(mutex_); + for (auto &obj : callbacks_) { + sptr callback = iface_cast(obj); + if (callback != nullptr) { + POWER_HILOGD(MODULE_SERVICE, "callback->PowerModeCallback()"); + callback->PowerModeCallback(); + } + } +} + +void PowerModeModule::PublishPowerModeEvent() +{ + POWER_HILOGD(MODULE_SERVICE, "Start of publishing mode event"); + /* send event */ + CommonEventPublishInfo publishInfo; + publishInfo.SetOrdered(false); + IntentWant setModeWant; + CommonEventData event(setModeWant); + switch (mode_) { + case PowerModeModule::EXTREAM_MODE: + setModeWant.SetAction(CommonEventSupport::COMMON_EVENT_DEVICE_IDLE_MODE_CHANGED); + event.SetCode(PowerModeModule::EXTREAM_MODE); + break; + case PowerModeModule::NORMAL_MODE: + setModeWant.SetAction(CommonEventSupport::COMMON_EVENT_DEVICE_IDLE_MODE_CHANGED); + event.SetCode(PowerModeModule::NORMAL_MODE); + break; + case PowerModeModule::SAVE_MODE: + setModeWant.SetAction(CommonEventSupport::COMMON_EVENT_POWER_SAVE_MODE_CHANGED); + event.SetCode(PowerModeModule::SAVE_MODE); + break; + case PowerModeModule::LOWPOWER_MODE: + setModeWant.SetAction(CommonEventSupport::COMMON_EVENT_POWER_SAVE_MODE_CHANGED); + event.SetCode(PowerModeModule::LOWPOWER_MODE); + break; + default: + POWER_HILOGE(MODULE_SERVICE, "Unknow mode"); + return; + } + if (!CommonEventManager::PublishCommonEvent(event, publishInfo, nullptr)) { + POWER_HILOGE(MODULE_SERVICE, "Failed to publish the mode event!"); + return; + } + POWER_HILOGD(MODULE_SERVICE, "End of publishing mode event"); +} + +void PowerModeModule::RunAction() +{ + POWER_HILOGI(MODULE_SERVICE, "PowerModeModule::RunAction"); + auto policy = DelayedSingleton::GetInstance(); + policy->TriggerAllActions(); + return; +} + +void PowerModeModule::SetDisplayOffTime() +{ + POWER_HILOGI(MODULE_SERVICE, "PowerModeModule::SetDisplayOffTime"); + int32_t time = DelayedSingleton::GetInstance() + ->GetPowerModeValuePolicy(PowerModePolicy::ServiceType::displayOffTime); + auto pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + POWER_HILOGI(MODULE_SERVICE, "No service instance"); + return; + } + pms->GetPowerStateMachine()->SetDisplayOffTime(static_cast(time)); +} + +void PowerModeModule::SetSleepTime() +{ + POWER_HILOGI(MODULE_SERVICE, "PowerModeModule::SetSleepTime"); + int32_t time = DelayedSingleton::GetInstance() + ->GetPowerModeValuePolicy(PowerModePolicy::ServiceType::sleepTime); + auto pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + POWER_HILOGI(MODULE_SERVICE, "No service instance"); + return; + } + pms->GetPowerStateMachine()->SetSleepTime(static_cast(time)); +} + +void PowerModeModule::SetLcdBrightness() +{ + POWER_HILOGD(MODULE_SERVICE, "set lcd brightness"); + int32_t lcdBrightness = DelayedSingleton::GetInstance() + ->GetPowerModeValuePolicy(PowerModePolicy::ServiceType::smartBacklight); + POWER_HILOGD(MODULE_SERVICE, "GetPowerModeValuePolicy lcdBrightness=%{public}d", lcdBrightness); + if (lcdBrightness != FLAG_FALSE) { + // set lastmode value to recoverValue + if (lastMode_ == LAST_MODE_FLAG) { + POWER_HILOGD(MODULE_SERVICE, "first set lcdBrightness=%{public}d", lcdBrightness); + recoverValue[PowerModePolicy::ServiceType::smartBacklight] = lcdBrightness; + } else { + // get value from setting privider value + POWER_HILOGD(MODULE_SERVICE, "Setting lcdBrightness=%{public}d", SETTINGS_PRIVIDER_VALUE_LCD_BRIGHTNESS); + recoverValue[PowerModePolicy::ServiceType::smartBacklight] = SETTINGS_PRIVIDER_VALUE_LCD_BRIGHTNESS; + } + // set lcd brightness + POWER_HILOGD(MODULE_SERVICE, "please set lcdBrightness"); + } else { + lcdBrightness = DelayedSingleton::GetInstance() + ->GetPowerModeRecoverPolicy(PowerModePolicy::ServiceType::smartBacklight); + POWER_HILOGD(MODULE_SERVICE, "GetPowerModeRecoverPolicy lcdBrightness=%{public}d", lcdBrightness); + if (lcdBrightness != FLAG_FALSE) { + // get recoverValue + std::lock_guard lock(mutex_); + recoverValueiter = recoverValue.find(PowerModePolicy::ServiceType::smartBacklight); + if (recoverValueiter != recoverValue.end()) { + lcdBrightness = recoverValueiter->second; + POWER_HILOGD(MODULE_SERVICE, "Get recovervalue lcdBrightness=%{public}d", lcdBrightness); + // delete map + recoverValue.erase(recoverValueiter); + } + POWER_HILOGD(MODULE_SERVICE, "please set lcdBrightness"); + } + } + return; +} + +void PowerModeModule::SetVibration() +{ + POWER_HILOGD(MODULE_SERVICE, "set vibration"); + int32_t vibration = DelayedSingleton::GetInstance() + ->GetPowerModeValuePolicy(PowerModePolicy::ServiceType::vibratorsState); + POWER_HILOGD(MODULE_SERVICE, "GetPowerModeValuePolicy vibrate=%{public}d", vibration); + if (vibration != FLAG_FALSE) { + // set lastmode value to recoverValue + if (lastMode_ == LAST_MODE_FLAG) { + POWER_HILOGD(MODULE_SERVICE, "first set vibration=%{public}d", vibration); + recoverValue[PowerModePolicy::ServiceType::vibratorsState] = vibration; + } else { + // get value from setting privider value + POWER_HILOGD(MODULE_SERVICE, "Setting vibration=%{public}d", SETTINGS_PRIVIDER_VALUE_VIBRATION); + recoverValue[PowerModePolicy::ServiceType::vibratorsState] = SETTINGS_PRIVIDER_VALUE_VIBRATION; + } + // set vibration + POWER_HILOGD(MODULE_SERVICE, "please set vibration"); + } else { + vibration = DelayedSingleton::GetInstance() + ->GetPowerModeRecoverPolicy(PowerModePolicy::ServiceType::vibratorsState); + POWER_HILOGD(MODULE_SERVICE, "GetPowerModeRecoverPolicy vibration=%{public}d", vibration); + if (vibration != FLAG_FALSE) { + // get recoverValue + std::lock_guard lock(mutex_); + recoverValueiter = recoverValue.find(PowerModePolicy::ServiceType::vibratorsState); + if (recoverValueiter != recoverValue.end()) { + vibration = recoverValueiter->second; + POWER_HILOGD(MODULE_SERVICE, "Get recovervalue vibration=%{public}d", vibration); + // delete map + recoverValue.erase(recoverValueiter); + } + POWER_HILOGD(MODULE_SERVICE, "please set vibration"); + } + } + return; +} + +void PowerModeModule::OnOffRotation() +{ + POWER_HILOGD(MODULE_SERVICE, "on or off rotation"); + int32_t rotation = DelayedSingleton::GetInstance() + ->GetPowerModeValuePolicy(PowerModePolicy::ServiceType::autoWindownRoration); + POWER_HILOGD(MODULE_SERVICE, "GetPowerModeValuePolicy rotation=%{public}d", rotation); + if (rotation != FLAG_FALSE) { + // set lastmode value to recoverValue + if (lastMode_ == LAST_MODE_FLAG) { + POWER_HILOGD(MODULE_SERVICE, "first set rotation=%{public}d", rotation); + recoverValue[PowerModePolicy::ServiceType::autoWindownRoration] = rotation; + } else { + // get value from setting privider value + POWER_HILOGD(MODULE_SERVICE, "Setting rotation=%{public}d", SETTINGS_PRIVIDER_VALUE_ROTATION); + recoverValue[PowerModePolicy::ServiceType::autoWindownRoration] = SETTINGS_PRIVIDER_VALUE_ROTATION; + } + // set lcd vibrate + POWER_HILOGD(MODULE_SERVICE, "please on or off rotation"); + } else { + rotation = DelayedSingleton::GetInstance() + ->GetPowerModeRecoverPolicy(PowerModePolicy::ServiceType::autoWindownRoration); + POWER_HILOGD(MODULE_SERVICE, "GetPowerModeRecoverPolicy rotation=%{public}d", rotation); + if (rotation != FLAG_FALSE) { + // get recoverValue + std::lock_guard lock(mutex_); + recoverValueiter = recoverValue.find(PowerModePolicy::ServiceType::vibratorsState); + if (recoverValueiter != recoverValue.end()) { + rotation = recoverValueiter->second; + POWER_HILOGD(MODULE_SERVICE, "Get recovervalue rotation=%{public}d", rotation); + // delete map + recoverValue.erase(recoverValueiter); + } + POWER_HILOGD(MODULE_SERVICE, "please on or off rotation"); + } + } + return; +} +} // namespace PowerMgr +} // namespace OHOS diff --git a/services/native/src/power_mode_policy.cpp b/services/native/src/power_mode_policy.cpp new file mode 100644 index 0000000000000000000000000000000000000000..271896dabba1bb2c5b6938b78fcf8e607e10ea35 --- /dev/null +++ b/services/native/src/power_mode_policy.cpp @@ -0,0 +1,147 @@ +/* + * Copyright (c) 2021 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 "power_mode_policy.h" +#include "power_save_mode.h" +#include "singleton.h" +#include "hilog_wrapper.h" + +using namespace std; + +namespace OHOS { +namespace PowerMgr { +int32_t PowerModePolicy::GetPowerModeValuePolicy(uint32_t type) +{ + int32_t ret = INIT_VALUE_FALSE; + if (IsValidType(type)) { + ret = GetPolicyFromMap(type); + } + + return ret; +} + +int32_t PowerModePolicy::GetPolicyFromMap(uint32_t type) +{ + int32_t ret = INIT_VALUE_FALSE; + valueiter = valueModePolicy.find(type); + if (valueiter != valueModePolicy.end()) { + ret = valueiter->second; + return ret; + } else { + return ret; + } +} + +int32_t PowerModePolicy::GetRecoverPolicyFromMap(uint32_t type) +{ + int32_t ret = INIT_VALUE_FALSE; + recoveriter = recoverModePolicy.find(type); + if (recoveriter != recoverModePolicy.end()) { + ret = recoveriter->second; + POWER_HILOGD(MODULE_SERVICE, "recover value=%{public}d", ret); + } + return ret; +} + +int32_t PowerModePolicy::GetPowerModeRecoverPolicy(uint32_t type) +{ + int32_t ret = INIT_VALUE_FALSE; + if (IsValidType(type)) { + ret = GetRecoverPolicyFromMap(type); + } + + return ret; +} + +void PowerModePolicy::SetPowerModePolicy(uint32_t mode, uint32_t lastMode) +{ + POWER_HILOGD(MODULE_SERVICE, "mode=%{public}d, lastMode=%{public}d", mode, lastMode); + if (lastMode != LAST_MODE_FLAG) { + ReadRecoverPolicy(lastMode); + } + + ReadOpenPolicy(mode); + + CompareModeItem(mode, lastMode); +} + +void PowerModePolicy::ReadOpenPolicy(uint32_t mode) +{ + DelayedSpSingleton::GetInstance()->GetValuePolicy(openPolicy, mode); +} + +void PowerModePolicy::ReadRecoverPolicy(uint32_t mode) +{ + DelayedSpSingleton::GetInstance()->GetRecoverPolicy(recoverPolicy, mode); +} + +void PowerModePolicy::CompareModeItem(uint32_t mode, uint32_t lastMode) +{ + recoverModePolicy.clear(); + valueModePolicy.clear(); + + for (auto openlit = openPolicy.begin(); openlit != openPolicy.end(); openlit++) { + valueModePolicy[(*openlit).id] = (*openlit).value; + } + + for (recoverlit = recoverPolicy.begin(); recoverlit != recoverPolicy.end(); recoverlit++) { + recoverModePolicy[(*recoverlit).id] = (*recoverlit).value; + POWER_HILOGD(MODULE_SERVICE, + "(*recoverlit).id=%{public}d, (*recoverlit).value=%{public}d", (*recoverlit).id, (*recoverlit).value); + } + + openPolicy.clear(); + recoverPolicy.clear(); +} + +void PowerModePolicy::AddAction(uint32_t type, std::function action) +{ + POWER_HILOGW(MODULE_SERVICE, "AddAction: type=(%{public}d)", type); + actionMap.emplace(type, action); +} + +void PowerModePolicy::TriggerAction(uint32_t type) +{ + auto iterator = actionMap.find(type); + if (iterator == actionMap.end()) { + POWER_HILOGW(MODULE_SERVICE, "TriggerAction: no such type=(%{public}d)", type); + return; + } + POWER_HILOGW(MODULE_SERVICE, "TriggerAction: type=(%{public}d)", type); + iterator->second(); +} + +void PowerModePolicy::TriggerAllActions() +{ + POWER_HILOGW(MODULE_SERVICE, "TriggerAllActions start"); + for (auto iterator = actionMap.begin() ; iterator != actionMap.end(); iterator++) { + POWER_HILOGW(MODULE_SERVICE, "TriggerAllActions: type=(%{public}d)", iterator->first); + iterator->second(); + } +} + +bool PowerModePolicy::IsValidType(uint32_t type) +{ + auto iterator = actionMap.find(type); + if (iterator == actionMap.end()) { + POWER_HILOGW(MODULE_SERVICE, "IsValidType: false (%{public}d)", type); + return false; + } + + POWER_HILOGW(MODULE_SERVICE, "IsValidType: true (%{public}d)", type); + return true; +} +} // namespace PowerMgr +} // namespace OHOS diff --git a/services/native/src/power_save_model.cpp b/services/native/src/power_save_model.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9f1e33518fd33bdd0914edf9f7aea25f2ada5b49 --- /dev/null +++ b/services/native/src/power_save_model.cpp @@ -0,0 +1,116 @@ +/* + * Copyright (c) 2021 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 "power_save_mode.h" +#include "libxml/parser.h" +#include "libxml/tree.h" +#include "hilog_wrapper.h" + +#include + +namespace OHOS { +namespace PowerMgr { +constexpr auto TAG_ROOT_ = "switch_policy"; +constexpr uint32_t SLEEP_FILTER = SLEEP_FILTER_VALUE; + +PowerSaveMode::PowerSaveMode() +{ + POWER_HILOGD(MODULE_SERVICE, "Start powersave.xml parse"); + StartXMlParse("/system/etc/power_config/power_mode_config.xml"); +} + +bool IsNodeLegal(const xmlNodePtr nodePtr, const char* tagName) +{ + return nodePtr != nullptr && nodePtr->type != XML_COMMENT_NODE && nodePtr->name != nullptr && + xmlStrEqual(nodePtr->name, BAD_CAST(tagName)) == 0; +} + +bool PowerSaveMode::StartXMlParse(std::string path) +{ + std::unique_ptr docPtr( + xmlReadFile(path.c_str(), nullptr, XML_PARSE_NOBLANKS), xmlFreeDoc); + if (docPtr == nullptr) { + POWER_HILOGE(MODULE_SERVICE, "Parse failed, read file failed."); + return false; + } + + auto rootPtr = xmlDocGetRootElement(docPtr.get()); + if (!IsNodeLegal(rootPtr, TAG_ROOT_)) { + POWER_HILOGE(MODULE_SERVICE, "Parse failed, root node is illegal."); + return false; + } + + for (auto nodePtr = rootPtr->xmlChildrenNode; nodePtr != nullptr; nodePtr = nodePtr->next) { + int32_t policyId = atoi((char *)xmlGetProp(nodePtr, BAD_CAST("id"))); + std::list listPolicy; + for (auto policyNodePtr = nodePtr->xmlChildrenNode; + policyNodePtr != nullptr; policyNodePtr = policyNodePtr->next) { + ModePolicy pmp; + pmp.id = atoi((char *)xmlGetProp(policyNodePtr, BAD_CAST("id"))); + pmp.recover_flag = atoi((char *)xmlGetProp(policyNodePtr, BAD_CAST("recover_flag"))); + pmp.value = atoi((char *)xmlGetProp(policyNodePtr, BAD_CAST("value"))); + listPolicy.push_back(pmp); + POWER_HILOGE(MODULE_SERVICE, "id=%{public}d", pmp.id); + POWER_HILOGE(MODULE_SERVICE, "value=%{public}d", pmp.value); + POWER_HILOGE(MODULE_SERVICE, "recover_flag=%{public}d", pmp.recover_flag); + } + std::pair> policyPair(policyId, listPolicy); + this->policyCache_.insert(policyPair); + POWER_HILOGI(MODULE_SERVICE, "policyId = %{public}d.", policyId); + } + return true; +} + +bool PowerSaveMode::GetValuePolicy(std::list &openPolicy, int32_t mode) +{ + bool result = GetFilterPolicy(openPolicy, mode, ValueProp::value); + return result; +} + +bool PowerSaveMode::GetRecoverPolicy(std::list &recoverPolicy, int32_t mode) +{ + bool result = GetFilterPolicy(recoverPolicy, mode, ValueProp::recover); + return result; +} + +bool PowerSaveMode::GetFilterPolicy(std::list &policyList, int32_t mode, int32_t value) +{ + if (this->policyCache_.size() == 0) { + return false; + } + for (ModePolicy modePolicy :this->policyCache_[mode]) { + policyList.push_back(modePolicy); + } + if (ValueProp::recover == value) { + policyList.remove_if([&](ModePolicy mp) {return mp.recover_flag != ValueProp::recover;}); + } + return true; +} + +int32_t PowerSaveMode::GetSleepTime(int32_t mode) +{ + if (this->policyCache_.size() == 0) { + return RETURN_FLAG_FALSE; + } + std::list modePolicyList = this->policyCache_[mode]; + for (auto modePolicy:modePolicyList) { + if (modePolicy.id == SLEEP_FILTER) { + return modePolicy.value; + } + } + return RETURN_FLAG_FALSE; +} +} // namespace PowerMgr +} // namespace OHOS diff --git a/services/native/src/power_state_machine.cpp b/services/native/src/power_state_machine.cpp index 24b64fb336fbf70ff36fbce93ebfc2cc93339c29..85e932c201fcbc62c244cdb61eae49b78d822794 100644 --- a/services/native/src/power_state_machine.cpp +++ b/services/native/src/power_state_machine.cpp @@ -30,15 +30,32 @@ namespace OHOS { namespace PowerMgr { -PowerStateMachine::PowerStateMachine(const wptr& pms) : pms_(pms) +PowerStateMachine::PowerStateMachine(const wptr& pms) + : pms_(pms), currentState_(PowerState::UNKNOWN) { - // NOTICE Need get screen state when device startup, rightnow we set screen is on as default - mDeviceState_.screenState.state = ScreenStateType::SCREEN_ON; - mDeviceState_.screenState.lastUpdateTime = GetTickCount(); + POWER_HILOGI(MODULE_SERVICE, "PowerStateMachine_currentState: func is Start."); + // NOTICE Need get screen state when device startup, + // rightnow we set screen is on as default + mDeviceState_.screenState.lastOnTime = GetTickCount(); + mDeviceState_.screenState.lastOffTime = 0; mDeviceState_.lastWakeupEventTime = 0; mDeviceState_.lastRefreshActivityTime = 0; mDeviceState_.lastWakeupDeviceTime = 0; mDeviceState_.lastSuspendDeviceTime = 0; + + // init lock map which will block state transit + std::vector awakeBlocker {}; + std::vector inactiveBlocker {RunningLockType::RUNNINGLOCK_SCREEN, + RunningLockType::RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL}; + std::vector sleepBlocker {RunningLockType::RUNNINGLOCK_BACKGROUND}; + lockMap_.emplace(PowerState::AWAKE, + std::make_shared>(awakeBlocker)); + lockMap_.emplace(PowerState::INACTIVE, + std::make_shared>(inactiveBlocker)); + lockMap_.emplace(PowerState::SLEEP, + std::make_shared>(sleepBlocker)); + + POWER_HILOGI(MODULE_SERVICE, "PowerStateMachine_currentState: func is End."); } PowerStateMachine::~PowerStateMachine() {} @@ -46,7 +63,10 @@ PowerStateMachine::~PowerStateMachine() {} bool PowerStateMachine::Init() { POWER_HILOGI(MODULE_SERVICE, "PowerStateMachine:: Init start"); + stateAction_ = PowerMgrFactory::GetDeviceStateAction(); + InitStateMap(); + if (powerStateCBDeathRecipient_ == nullptr) { powerStateCBDeathRecipient_ = new PowerStateCallbackDeathRecipient(); } @@ -55,14 +75,102 @@ bool PowerStateMachine::Init() POWER_HILOGE(MODULE_SERVICE, "Failed to start monitor"); return false; } + + if (IsScreenOn()) { + SetState(PowerState::AWAKE, StateChangeReason::STATE_CHANGE_REASON_INIT, true); + } else { + SetState(PowerState::INACTIVE, StateChangeReason::STATE_CHANGE_REASON_INIT, true); + } POWER_HILOGI(MODULE_SERVICE, "PowerStateMachine:: Init success!"); return true; } -void PowerStateMachine::SuspendDeviceInner(pid_t pid, int64_t callTimeMs, SuspendDeviceType type, bool suspendImmed, +void PowerStateMachine::InitStateMap() +{ + // init state controller map + controllerMap_.emplace(PowerState::AWAKE, + std::make_shared(PowerState::AWAKE, shared_from_this(), [this] { + mDeviceState_.screenState.lastOnTime = GetTickCount(); + uint32_t ret = this->stateAction_->SetDisplayState(DisplayState::DISPLAY_ON); + if (ret != ActionResult::SUCCESS) { + POWER_HILOGE(MODULE_SERVICE, "Failed to go to AWAKE, Display Err"); + return TransitResult::HDI_ERR; + } + ResetInactiveTimer(); + return TransitResult::SUCCESS; + }) + ); + controllerMap_.emplace(PowerState::INACTIVE, + std::make_shared(PowerState::INACTIVE, shared_from_this(), [this] { + POWER_HILOGI(MODULE_SERVICE, "StateController_INACTIVE: func is Start."); + mDeviceState_.screenState.lastOffTime = GetTickCount(); + DisplayState state = DisplayState::DISPLAY_OFF; + if (enableDisplaySuspend_) { + POWER_HILOGI(MODULE_SERVICE, "display suspend enabled"); + state = DisplayState::DISPLAY_SUSPEND; + } + uint32_t ret = this->stateAction_->SetDisplayState(state); + if (ret != ActionResult::SUCCESS) { + POWER_HILOGE(MODULE_SERVICE, "Failed to go to INACTIVE, Display Err"); + return TransitResult::HDI_ERR; + } + ResetSleepTimer(); + return TransitResult::SUCCESS; + }) + ); + controllerMap_.emplace(PowerState::SLEEP, + std::make_shared(PowerState::SLEEP, shared_from_this(), [this] { + DisplayState state = DisplayState::DISPLAY_OFF; + if (enableDisplaySuspend_) { + POWER_HILOGI(MODULE_SERVICE, "display suspend enabled"); + state = DisplayState::DISPLAY_SUSPEND; + } + uint32_t ret = this->stateAction_->SetDisplayState(state); + if (ret != ActionResult::SUCCESS) { + POWER_HILOGE(MODULE_SERVICE, "Failed to go to SLEEP, Display Err"); + return TransitResult::HDI_ERR; + } + ret = this->stateAction_->GoToSleep(onSuspend, onWakeup, false); + if (ret != ActionResult::SUCCESS) { + POWER_HILOGE(MODULE_SERVICE, "Failed to go to SLEEP, Sleep Err"); + return TransitResult::HDI_ERR; + } + return TransitResult::SUCCESS; + }) + ); +} + +void PowerStateMachine::onSuspend() +{ + POWER_HILOGI(MODULE_SERVICE, "System is suspending"); +} + +void PowerStateMachine::onWakeup() +{ + POWER_HILOGI(MODULE_SERVICE, "System is awake"); + auto pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + return; + } + auto handler = pms->GetHandler(); + if (handler == nullptr) { + POWER_HILOGE(MODULE_SERVICE, "SetDelayTimer handler is null"); + return; + } + handler->SendEvent(PowermsEventHandler::SYSTEM_WAKE_UP_MSG, 0, 0); +} + +void PowerStateMachine::SuspendDeviceInner(pid_t pid, + int64_t callTimeMs, + SuspendDeviceType type, + bool suspendImmed, bool ignoreScreenState) { POWER_HILOGI(MODULE_SERVICE, "PID: %{public}d Try to Suspend Device!!", pid); + if (type > SuspendDeviceType::SUSPEND_DEVICE_REASON_MAX) { + POWER_HILOGE(MODULE_SERVICE, "Invalid type: %{public}d", type); + return; + } // Check the screen state if (!ignoreScreenState) { if (stateAction_ != nullptr) { @@ -74,24 +182,44 @@ void PowerStateMachine::SuspendDeviceInner(pid_t pid, int64_t callTimeMs, Suspen } else { POWER_HILOGE(MODULE_SERVICE, "Suspend Device Failed, Screen State is ignored!"); } + + SetState(PowerState::INACTIVE, GetReasionBySuspendType(type), true); + POWER_HILOGI(MODULE_SERVICE, "SuspendDeviceInner: fun is End!"); } -void PowerStateMachine::WakeupDeviceInner(pid_t pid, int64_t callTimeMs, WakeupDeviceType type, - const std::string& details, const std::string& pkgName) +void PowerStateMachine::WakeupDeviceInner(pid_t pid, + int64_t callTimeMs, + WakeupDeviceType type, + const std::string& details, + const std::string& pkgName) { POWER_HILOGI(MODULE_SERVICE, "PID: %{public}d Try to Wakeup Device!!", pid); - // Check the screen state + if (type > WakeupDeviceType::WAKEUP_DEVICE_MAX) { + POWER_HILOGE(MODULE_SERVICE, "Invalid type: %{public}d", type); + return; + } + // Call legacy wakeup, Check the screen state if (stateAction_ != nullptr) { stateAction_->Wakeup(callTimeMs, type, details, pkgName); } mDeviceState_.lastWakeupDeviceTime = callTimeMs; - POWER_HILOGD(MODULE_SERVICE, "Wakeup Device Call Binder Success!!"); + + ResetInactiveTimer(); + SetState(PowerState::AWAKE, GetReasonByWakeType(type), true); + + POWER_HILOGD(MODULE_SERVICE, "Wakeup Device Call"); } -void PowerStateMachine::RefreshActivityInner(pid_t pid, int64_t callTimeMs, UserActivityType type, +void PowerStateMachine::RefreshActivityInner(pid_t pid, + int64_t callTimeMs, + UserActivityType type, bool needChangeBacklight) { POWER_HILOGI(MODULE_SERVICE, "PID: %{public}d Start to RefreshActivity!!", pid); + if (type > UserActivityType::USER_ACTIVITY_TYPE_MAX) { + POWER_HILOGE(MODULE_SERVICE, "Invalid type: %{public}d", type); + return; + } // The minimum refreshactivity interval is 100ms!! int64_t now = GetTickCount(); if ((mDeviceState_.lastRefreshActivityTime + MIN_TIME_MS_BETWEEN_USERACTIVITIES) > now) { @@ -104,26 +232,36 @@ void PowerStateMachine::RefreshActivityInner(pid_t pid, int64_t callTimeMs, User if (stateAction_ != nullptr) { stateAction_->RefreshActivity(callTimeMs, type, needChangeBacklight ? REFRESH_ACTIVITY_NEED_CHANGE_LIGHTS : REFRESH_ACTIVITY_NO_CHANGE_LIGHTS); + mDeviceState_.screenState.lastOnTime = GetTickCount(); + stateAction_->SetDisplayState(DisplayState::DISPLAY_ON); } + // reset timer + ResetInactiveTimer(); POWER_HILOGD(MODULE_SERVICE, "Refresh Activity Call Binder Success!!"); } else { POWER_HILOGE(MODULE_SERVICE, "RefreshActivity Failed, Screen is Off!"); } + POWER_HILOGI(MODULE_SERVICE, "RefreshActivityInner: fun is End!"); } bool PowerStateMachine::ForceSuspendDeviceInner(pid_t pid, int64_t callTimeMs) { + POWER_HILOGI(MODULE_SERVICE, "ForceSuspendDeviceInner: fun is Start!"); if (stateAction_ != nullptr) { - stateAction_->ForceSuspend(); + currentState_ = PowerState::SLEEP; + stateAction_->GoToSleep(onSuspend, onWakeup, true); } + + POWER_HILOGI(MODULE_SERVICE, "ForceSuspendDeviceInner: fun is End!"); return true; } bool PowerStateMachine::IsScreenOn() { - std::lock_guard lock(mutex_); - if (mDeviceState_.screenState.state == ScreenStateType::SCREEN_ON || - mDeviceState_.screenState.state == ScreenStateType::SCREEN_DIM) { + POWER_HILOGI(MODULE_SERVICE, "IsScreenOn: fun is Start!"); + DisplayState state = stateAction_->GetDisplayState(); + if (state == DisplayState::DISPLAY_ON || + state == DisplayState::DISPLAY_DIM) { POWER_HILOGI(MODULE_SERVICE, "Current Screen State: On!"); return true; } @@ -133,19 +271,22 @@ bool PowerStateMachine::IsScreenOn() void PowerStateMachine::ReceiveScreenEvent(bool isScreenOn) { + POWER_HILOGI(MODULE_SERVICE, "ReceiveScreenEvent: fun is Start!"); std::lock_guard lock(mutex_); auto prestate = mDeviceState_.screenState.state; - mDeviceState_.screenState.lastUpdateTime = GetTickCount(); - mDeviceState_.screenState.state = isScreenOn ? ScreenStateType::SCREEN_ON : ScreenStateType::SCREEN_OFF; + if (isScreenOn) { + mDeviceState_.screenState.lastOnTime = GetTickCount(); + } else { + mDeviceState_.screenState.lastOffTime = GetTickCount(); + } if (prestate != mDeviceState_.screenState.state) { NotifyPowerStateChanged(isScreenOn ? PowerState::AWAKE : PowerState::INACTIVE); } - POWER_HILOGI(MODULE_SERVICE, "receive new screen event, new state is %{public}d, at %{public}" PRId64 "", - mDeviceState_.screenState.state, mDeviceState_.screenState.lastUpdateTime); } void PowerStateMachine::RegisterPowerStateCallback(const sptr& callback) { + POWER_HILOGI(MODULE_SERVICE, "RegisterPowerStateCallback: fun is Start!"); std::lock_guard lock(mutex_); RETURN_IF(callback == nullptr); auto object = callback->AsObject(); @@ -154,14 +295,19 @@ void PowerStateMachine::RegisterPowerStateCallback(const sptrAddDeathRecipient(powerStateCBDeathRecipient_); } - POWER_HILOGI(MODULE_SERVICE, "%{public}s, object = %p, callback = %p, listeners.size = %d," - " insertOk = %d", __func__, object.GetRefPtr(), callback.GetRefPtr(), + POWER_HILOGI(MODULE_SERVICE, + "%{public}s, object = %{public}p, callback = %{public}p, listeners.size = %{public}d," + " insertOk = %{public}d", + __func__, + object.GetRefPtr(), + callback.GetRefPtr(), static_cast(powerStateListeners_.size()), retIt.second); } void PowerStateMachine::UnRegisterPowerStateCallback(const sptr& callback) { + POWER_HILOGI(MODULE_SERVICE, "UnRegisterPowerStateCallback: fun is Start!"); std::lock_guard lock(mutex_); RETURN_IF(callback == nullptr); auto object = callback->AsObject(); @@ -170,16 +316,121 @@ void PowerStateMachine::UnRegisterPowerStateCallback(const sptrRemoveDeathRecipient(powerStateCBDeathRecipient_); } - POWER_HILOGI(MODULE_SERVICE, "%{public}s, object = %p, callback = %p, listeners.size = %d," - " eraseNum = %zu", __func__, object.GetRefPtr(), callback.GetRefPtr(), + POWER_HILOGI(MODULE_SERVICE, + "%{public}s, object = %{public}p, callback = %{public}p, listeners.size = %{public}d," + " eraseNum = %zu", + __func__, + object.GetRefPtr(), + callback.GetRefPtr(), static_cast(powerStateListeners_.size()), eraseNum); } +static const char* GetReasonTypeString(StateChangeReason type) +{ + switch (type) { + case StateChangeReason::STATE_CHANGE_REASON_INIT: + return "INIT"; + case StateChangeReason::STATE_CHANGE_REASON_TIMEOUT: + return "TIMEOUT"; + case StateChangeReason::STATE_CHANGE_REASON_RUNNING_LOCK: + return "RUNNING_LOCK"; + case StateChangeReason::STATE_CHANGE_REASON_BATTERY: + return "BATTERY"; + case StateChangeReason::STATE_CHANGE_REASON_THERMAL: + return "THERMAL"; + case StateChangeReason::STATE_CHANGE_REASON_WORK: + return "WORK"; + case StateChangeReason::STATE_CHANGE_REASON_SYSTEM: + return "SYSTEM"; + case StateChangeReason::STATE_CHANGE_REASON_APPLICATION: + return "APPLICATION"; + case StateChangeReason::STATE_CHANGE_REASON_SETTINGS: + return "SETTINGS"; + case StateChangeReason::STATE_CHANGE_REASON_HARD_KEY: + return "HARD_KEY"; + case StateChangeReason::STATE_CHANGE_REASON_TOUCH: + return "TOUCH"; + case StateChangeReason::STATE_CHANGE_REASON_CABLE: + return "CABLE"; + case StateChangeReason::STATE_CHANGE_REASON_SENSOR: + return "SENSOR"; + case StateChangeReason::STATE_CHANGE_REASON_LID: + return "LID"; + case StateChangeReason::STATE_CHANGE_REASON_CAMERA: + return "CAMERA"; + case StateChangeReason::STATE_CHANGE_REASON_ACCESSIBILITY: + return "ACCESSIBILITY"; + case StateChangeReason::STATE_CHANGE_REASON_REMOTE: + return "REMOTE"; + case StateChangeReason::STATE_CHANGE_REASON_UNKNOWN: + return "UNKNOWN"; + default: + break; + } + + return "UNKNOWN"; +} + +static const char* GetPowerStateString(PowerState state) +{ + switch (state) { + case PowerState::AWAKE: + return "AWAKE"; + case PowerState::INACTIVE: + return "INACTIVE"; + case PowerState::SLEEP: + return "SLEEP"; + case PowerState::UNKNOWN: + return "UNKNOWN"; + default: + break; + } + + return "UNKNOWN"; +} + +static const char* GetRunningLockTypeString(RunningLockType type) +{ + switch (type) { + case RunningLockType::RUNNINGLOCK_SCREEN: + return "SCREEN"; + case RunningLockType::RUNNINGLOCK_BACKGROUND: + return "BACKGROUND"; + case RunningLockType::RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL: + return "PROXIMITY_SCREEN_CONTROL"; + case RunningLockType::RUNNINGLOCK_BUTT: + return "BUTT"; + default: + break; + } + + return "UNKNOWN"; +} + +void PowerStateMachine::EnableMock(IDeviceStateAction* mockAction) +{ + POWER_HILOGI(MODULE_SERVICE, "enableMock: fun is Start!"); + std::lock_guard lock(mutex_); + // reset to awake state when mock and default off/sleep time + currentState_ = PowerState::AWAKE; + displayOffTime_ = DEFAULT_DISPLAY_OFF_TIME; + sleepTime_ = DEFAULT_SLEEP_TIME; + ResetInactiveTimer(); + + std::unique_ptr mock(mockAction); + stateAction_.reset(); + stateAction_ = std::move(mock); +} + void PowerStateMachine::NotifyPowerStateChanged(PowerState state) { - POWER_HILOGI(MODULE_SERVICE, "%{public}s state = %u, listeners.size = %d", __func__, - static_cast(state), static_cast(powerStateListeners_.size())); + POWER_HILOGI(MODULE_SERVICE, + "%{public}s state = %u, listeners.size = %{public}d", + __func__, + static_cast(state), + static_cast(powerStateListeners_.size())); + std::lock_guard lock(mutex_); int64_t now = GetTickCount(); // Send Notification event SendEventToPowerMgrNotify(state, now); @@ -192,6 +443,7 @@ void PowerStateMachine::NotifyPowerStateChanged(PowerState state) void PowerStateMachine::SendEventToPowerMgrNotify(PowerState state, int64_t callTime) { + POWER_HILOGD(MODULE_SERVICE, "SendEventToPowerMgrNotify: fun is Start!"); auto pms = DelayedSpSingleton::GetInstance(); if (pms == nullptr) { POWER_HILOGE(MODULE_SERVICE, "SendEventToPowerMgrNotify pms is Null, Fail!!"); @@ -204,15 +456,20 @@ void PowerStateMachine::SendEventToPowerMgrNotify(PowerState state, int64_t call } if (state == PowerState::AWAKE) { notify->PublishScreenOnEvents(callTime); - } else { + } else if (state == PowerState::INACTIVE) { notify->PublishScreenOffEvents(callTime); + } else { + POWER_HILOGI(MODULE_SERVICE, "No need to publish event, state:%{public}d", state); } + POWER_HILOGD(MODULE_SERVICE, "SendEventToPowerMgrNotify: fun is End!"); } const std::string TASK_UNREG_POWER_STATE_CALLBACK = "PowerState_UnRegPowerStateCB"; -void PowerStateMachine::PowerStateCallbackDeathRecipient::OnRemoteDied(const wptr& remote) +void PowerStateMachine::PowerStateCallbackDeathRecipient::OnRemoteDied( + const wptr& remote) { + POWER_HILOGD(MODULE_SERVICE, "OnRemoteDied: fun is Start!"); if (remote == nullptr || remote.promote() == nullptr) { return; } @@ -227,12 +484,15 @@ void PowerStateMachine::PowerStateCallbackDeathRecipient::OnRemoteDied(const wpt return; } sptr callback = iface_cast(remote.promote()); - std::function unRegFunc = std::bind(&PowerMgrService::UnRegisterPowerStateCallback, pms, callback); + std::function unRegFunc = + std::bind(&PowerMgrService::UnRegisterPowerStateCallback, pms, callback); handler->PostTask(unRegFunc, TASK_UNREG_POWER_STATE_CALLBACK); + POWER_HILOGD(MODULE_SERVICE, "OnRemoteDied: fun is End!"); } void PowerStateMachine::SetDelayTimer(int64_t delayTime, int32_t event) { + POWER_HILOGD(MODULE_SERVICE, "SetDelayTimer: fun is Start!"); auto pms = DelayedSpSingleton::GetInstance(); if (pms == nullptr) { return; @@ -243,10 +503,12 @@ void PowerStateMachine::SetDelayTimer(int64_t delayTime, int32_t event) return; } handler->SendEvent(event, 0, delayTime); + POWER_HILOGD(MODULE_SERVICE, "SetDelayTimer: fun is End!"); } void PowerStateMachine::CancelDelayTimer(int32_t event) { + POWER_HILOGD(MODULE_SERVICE, "CancelDelayTimer (%{public}d): fun is Start!", event); auto pms = DelayedSpSingleton::GetInstance(); if (pms == nullptr) { return; @@ -257,12 +519,427 @@ void PowerStateMachine::CancelDelayTimer(int32_t event) return; } handler->RemoveEvent(event); - POWER_HILOGI(MODULE_SERVICE, "remove event success"); + POWER_HILOGD(MODULE_SERVICE, "CancelDelayTimer: fun is End!"); +} + +void PowerStateMachine::ResetInactiveTimer() +{ + POWER_HILOGD(MODULE_SERVICE, "ResetInactiveTimer: fun is Start!"); + CancelDelayTimer(PowermsEventHandler::CHECK_USER_ACTIVITY_TIMEOUT_MSG); + CancelDelayTimer(PowermsEventHandler::CHECK_USER_ACTIVITY_OFF_TIMEOUT_MSG); + CancelDelayTimer(PowermsEventHandler::CHECK_USER_ACTIVITY_SLEEP_TIMEOUT_MSG); + if (this->GetDisplayOffTime() < 0) { + POWER_HILOGI(MODULE_SERVICE, "Display Auto OFF is disabled"); + return; + } + + if (this->CheckRunningLock(PowerState::INACTIVE)) { + const uint32_t TWO = 2; + const uint32_t THREE = 3; + this->SetDelayTimer(this->GetDisplayOffTime() * TWO / THREE, + PowermsEventHandler::CHECK_USER_ACTIVITY_TIMEOUT_MSG); + } + POWER_HILOGD(MODULE_SERVICE, "ResetInactiveTimer: fun is End!"); +} + +void PowerStateMachine::ResetSleepTimer() +{ + POWER_HILOGD(MODULE_SERVICE, "ResetSleepTimer: fun is Start!"); + CancelDelayTimer(PowermsEventHandler::CHECK_USER_ACTIVITY_TIMEOUT_MSG); + CancelDelayTimer(PowermsEventHandler::CHECK_USER_ACTIVITY_OFF_TIMEOUT_MSG); + CancelDelayTimer(PowermsEventHandler::CHECK_USER_ACTIVITY_SLEEP_TIMEOUT_MSG); + if (this->GetSleepTime() < 0) { + POWER_HILOGI(MODULE_SERVICE, "Auto Sleep is disabled"); + return; + } + + if (this->CheckRunningLock(PowerState::SLEEP)) { + this->SetDelayTimer(this->GetSleepTime(), + PowermsEventHandler::CHECK_USER_ACTIVITY_SLEEP_TIMEOUT_MSG); + } + POWER_HILOGD(MODULE_SERVICE, "ResetSleepTimer: fun is End!"); +} + +void PowerStateMachine::HandleDelayTimer(int32_t event) +{ + POWER_HILOGD(MODULE_SERVICE, "handle delay timer: (%{public}d)", event); + switch (event) { + case PowermsEventHandler::CHECK_USER_ACTIVITY_TIMEOUT_MSG: + HandleActivityTimeout(); + break; + case PowermsEventHandler::CHECK_USER_ACTIVITY_OFF_TIMEOUT_MSG: + HandleActivityOffTimeout(); + break; + case PowermsEventHandler::CHECK_USER_ACTIVITY_SLEEP_TIMEOUT_MSG: + HandleActivitySleepTimeout(); + break; + case PowermsEventHandler::SYSTEM_WAKE_UP_MSG: + HandleSystemWakeup(); + break; + default: + break; + } + POWER_HILOGD(MODULE_SERVICE, "ResetSleepTimer: fun is End!"); +} + +void PowerStateMachine::HandleActivityTimeout() +{ + POWER_HILOGI(MODULE_SERVICE, "HandleActivityTimeout (%{public}d)", + stateAction_->GetDisplayState()); + DisplayState dispState = stateAction_->GetDisplayState(); + const uint32_t THREE = 3; + if (!this->CheckRunningLock(PowerState::INACTIVE)) { + POWER_HILOGW(MODULE_SERVICE, "RunningLock is blocking to transit to INACTIVE"); + return; + } + if (dispState == DisplayState::DISPLAY_ON) { + stateAction_->SetDisplayState(DisplayState::DISPLAY_DIM); + if (this->GetDisplayOffTime() < 0) { + POWER_HILOGI(MODULE_SERVICE, "Display Auto OFF is disabled"); + return; + } else { + SetDelayTimer(GetDisplayOffTime() / THREE, + PowermsEventHandler::CHECK_USER_ACTIVITY_OFF_TIMEOUT_MSG); + } + } else { + POWER_HILOGW(MODULE_SERVICE, + "HandleActivityTimeout when display: %{public}d", dispState); + } + POWER_HILOGI(MODULE_SERVICE, "HandleActivityTimeout: fun is End!"); } -void PowerStateMachine::HandleDelayTimer() +void PowerStateMachine::HandleActivityOffTimeout() { - POWER_HILOGI(MODULE_SERVICE, "handle delay timer success"); + POWER_HILOGI(MODULE_SERVICE, "HandleActivityOffTimeout (%{public}d)", + stateAction_->GetDisplayState()); + if (!this->CheckRunningLock(PowerState::INACTIVE)) { + POWER_HILOGW(MODULE_SERVICE, "RunningLock is blocking to transit to INACTIVE"); + return; + } + DisplayState dispState = stateAction_->GetDisplayState(); + // Also transit state when ON if system not support DIM + if (dispState == DisplayState::DISPLAY_ON + || dispState == DisplayState::DISPLAY_DIM) { + SetState(PowerState::INACTIVE, StateChangeReason::STATE_CHANGE_REASON_TIMEOUT); + } else { + POWER_HILOGW(MODULE_SERVICE, + "HandleActivityOffTimeout when display: %{public}d", dispState); + } + POWER_HILOGI(MODULE_SERVICE, "HandleActivityOffTimeOut: fun is End!"); +} + +void PowerStateMachine::HandleActivitySleepTimeout() +{ + POWER_HILOGI(MODULE_SERVICE, "HandleActivitySleepTimeout (%{public}d)", + stateAction_->GetDisplayState()); + if (!this->CheckRunningLock(PowerState::SLEEP)) { + POWER_HILOGW(MODULE_SERVICE, "RunningLock is blocking to transit to SLEEP"); + return; + } + DisplayState dispState = stateAction_->GetDisplayState(); + if (dispState == DisplayState::DISPLAY_OFF) { + SetState(PowerState::SLEEP, StateChangeReason::STATE_CHANGE_REASON_TIMEOUT); + } else { + POWER_HILOGW(MODULE_SERVICE, + "HandleActivitySleepTimeout when display: %{public}d", dispState); + } + POWER_HILOGI(MODULE_SERVICE, "HandleActivitySleepTimeout: fun is End!"); +} + +void PowerStateMachine::HandleSystemWakeup() +{ + POWER_HILOGI(MODULE_SERVICE, "HandleSystemWakeup (%{public}d)", + stateAction_->GetDisplayState()); + if (IsScreenOn()) { + SetState(PowerState::AWAKE, StateChangeReason::STATE_CHANGE_REASON_SYSTEM, true); + } else { + SetState(PowerState::INACTIVE, StateChangeReason::STATE_CHANGE_REASON_SYSTEM, true); + } +} + +bool PowerStateMachine::CheckRunningLock(PowerState state) +{ + POWER_HILOGI(MODULE_SERVICE, "CheckRunningLock: fun is Start!"); + auto pms = pms_.promote(); + if (pms == nullptr) { + POWER_HILOGE(MODULE_SERVICE, "CheckRunningLock: promote failed!"); + return false; + } + auto runningLockMgr = pms->GetRunningLockMgr(); + if (runningLockMgr == nullptr) { + POWER_HILOGE(MODULE_SERVICE, "CheckRunningLock: GetRunningLockMgr failed!"); + return false; + } + auto iterator = lockMap_.find(state); + if (iterator == lockMap_.end()) { + POWER_HILOGE(MODULE_SERVICE, "CheckRunningLock: map find failed!"); + return false; + } + + std::shared_ptr> pLock = iterator->second; + for (std::vector::const_iterator iter = pLock->begin(); + iter != pLock->end(); ++iter) { + uint32_t count = runningLockMgr->GetValidRunningLockNum(*iter); + if (count > 0) { + POWER_HILOGE(MODULE_SERVICE, + "RunningLock %{public}s is locking (count=%{public}d), blocking %{public}s", + GetRunningLockTypeString(*iter), + count, + GetPowerStateString(state)); + return false; + } + } + + POWER_HILOGI(MODULE_SERVICE, "No RunningLock block for state (%{public}d)", state); + return true; +} + +void PowerStateMachine::SetDisplayOffTime(int64_t time) +{ + displayOffTime_ = time; + if (currentState_ == PowerState::AWAKE) { + ResetInactiveTimer(); + } +} + +void PowerStateMachine::SetSleepTime(int64_t time) +{ + sleepTime_ = time; + if (currentState_ == PowerState::INACTIVE) { + ResetSleepTimer(); + } +} + +int64_t PowerStateMachine::GetDisplayOffTime() +{ + return displayOffTime_; +} + +int64_t PowerStateMachine::GetSleepTime() +{ + return sleepTime_; +} + +bool PowerStateMachine::SetState(PowerState state, StateChangeReason reason, bool force) +{ + POWER_HILOGI(MODULE_SERVICE, + "SetState state=%{public}d, reason=%{public}d, force=%{public}d", + state, reason, force); + auto iterator = controllerMap_.find(state); + if (iterator == controllerMap_.end()) { + return false; + } + std::shared_ptr pController = iterator->second; + if (pController == nullptr) { + POWER_HILOGE(MODULE_SERVICE, "AWAKE State not initiated"); + return false; + } + TransitResult ret = pController->TransitTo(reason, true); + POWER_HILOGI(MODULE_SERVICE, "SetState: fun is End!"); + return (ret == TransitResult::SUCCESS || ret == TransitResult::ALREADY_IN_STATE); +} + +void PowerStateMachine::SetDisplaySuspend(bool enable) +{ + POWER_HILOGI(MODULE_SERVICE, "SetDisplaySuspend:%{public}d", enable); + enableDisplaySuspend_ = enable; + if (GetState() == PowerState::INACTIVE) { + POWER_HILOGI(MODULE_SERVICE, "Change display state"); + if (enable) { + stateAction_->SetDisplayState(DisplayState::DISPLAY_SUSPEND); + } else { + stateAction_->SetDisplayState(DisplayState::DISPLAY_OFF); + } + } +} + +StateChangeReason PowerStateMachine::GetReasonByUserActivity(UserActivityType type) +{ + POWER_HILOGI(MODULE_SERVICE, "GetReasonByUserActivity Start:%{public}d", type); + StateChangeReason ret = StateChangeReason::STATE_CHANGE_REASON_UNKNOWN; + switch (type) { + case UserActivityType::USER_ACTIVITY_TYPE_BUTTON: + ret = StateChangeReason::STATE_CHANGE_REASON_HARD_KEY; + break; + case UserActivityType::USER_ACTIVITY_TYPE_TOUCH: + ret = StateChangeReason::STATE_CHANGE_REASON_TOUCH; + break; + case UserActivityType::USER_ACTIVITY_TYPE_ACCESSIBILITY: + ret = StateChangeReason::STATE_CHANGE_REASON_ACCESSIBILITY; + break; + case UserActivityType::USER_ACTIVITY_TYPE_SOFTWARE: + ret = StateChangeReason::STATE_CHANGE_REASON_APPLICATION; + break; + case UserActivityType::USER_ACTIVITY_TYPE_ATTENTION: // fail through + case UserActivityType::USER_ACTIVITY_TYPE_OTHER: // fail through + default: + break; + } + POWER_HILOGI(MODULE_SERVICE, "GetReasonByUserActivity: fun is End!"); + return ret; +} + +StateChangeReason PowerStateMachine::GetReasonByWakeType(WakeupDeviceType type) +{ + POWER_HILOGI(MODULE_SERVICE, "GetReasonByWakeType Start:%{public}d", type); + StateChangeReason ret = StateChangeReason::STATE_CHANGE_REASON_UNKNOWN; + switch (type) { + case WakeupDeviceType::WAKEUP_DEVICE_POWER_BUTTON: // fall through + case WakeupDeviceType::WAKEUP_DEVICE_WAKE_KEY: + ret = StateChangeReason::STATE_CHANGE_REASON_HARD_KEY; + break; + case WakeupDeviceType::WAKEUP_DEVICE_APPLICATION: + ret = StateChangeReason::STATE_CHANGE_REASON_APPLICATION; + break; + case WakeupDeviceType::WAKEUP_DEVICE_PLUGGED_IN: // fall through + case WakeupDeviceType::WAKEUP_DEVICE_HDMI: + ret = StateChangeReason::STATE_CHANGE_REASON_CABLE; + break; + case WakeupDeviceType::WAKEUP_DEVICE_GESTURE: + ret = StateChangeReason::STATE_CHANGE_REASON_TOUCH; + break; + case WakeupDeviceType::WAKEUP_DEVICE_CAMERA_LAUNCH: + ret = StateChangeReason::STATE_CHANGE_REASON_CAMERA; + break; + case WakeupDeviceType::WAKEUP_DEVICE_WAKE_MOTION: + ret = StateChangeReason::STATE_CHANGE_REASON_SENSOR; + break; + case WakeupDeviceType::WAKEUP_DEVICE_LID: + ret = StateChangeReason::STATE_CHANGE_REASON_LID; + break; + case WakeupDeviceType::WAKEUP_DEVICE_UNKNOWN: // fail through + default: + break; + } + POWER_HILOGI(MODULE_SERVICE, "GetReasonByWakeType: fun is End!"); + return ret; +} + +StateChangeReason PowerStateMachine::GetReasionBySuspendType(SuspendDeviceType type) +{ + POWER_HILOGI(MODULE_SERVICE, "GetReasionBySuspendType Start:%{public}d", type); + StateChangeReason ret = StateChangeReason::STATE_CHANGE_REASON_UNKNOWN; + switch (type) { + case SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION: + ret = StateChangeReason::STATE_CHANGE_REASON_APPLICATION; + break; + case SuspendDeviceType::SUSPEND_DEVICE_REASON_DEVICE_ADMIN: + ret = StateChangeReason::STATE_CHANGE_REASON_REMOTE; + break; + case SuspendDeviceType::SUSPEND_DEVICE_REASON_TIMEOUT: + ret = StateChangeReason::STATE_CHANGE_REASON_TIMEOUT; + break; + case SuspendDeviceType::SUSPEND_DEVICE_REASON_LID_SWITCH: + ret = StateChangeReason::STATE_CHANGE_REASON_LID; + break; + case SuspendDeviceType::SUSPEND_DEVICE_REASON_POWER_BUTTON: // fall through + case SuspendDeviceType::SUSPEND_DEVICE_REASON_SLEEP_BUTTON: + ret = StateChangeReason::STATE_CHANGE_REASON_HARD_KEY; + break; + case SuspendDeviceType::SUSPEND_DEVICE_REASON_HDMI: + ret = StateChangeReason::STATE_CHANGE_REASON_CABLE; + break; + case SuspendDeviceType::SUSPEND_DEVICE_REASON_ACCESSIBILITY: + ret = StateChangeReason::STATE_CHANGE_REASON_ACCESSIBILITY; + break; + case SuspendDeviceType::SUSPEND_DEVICE_REASON_FORCE_SUSPEND: + ret = StateChangeReason::STATE_CHANGE_REASON_SYSTEM; + break; + default: + break; + } + POWER_HILOGI(MODULE_SERVICE, "GetReasionBySuspendType: fun is End!"); + return ret; +} + +void PowerStateMachine::DumpInfo(std::string& result) +{ + result.append("POWER MANAGER DUMP (hidumper -PowerStateMachine):\n"); + result.append("Current State: ") + .append(GetPowerStateString(GetState())) + .append(" Reasion: ") + .append(ToString(static_cast( + controllerMap_.find(GetState())->second->lastReason_))) + .append(" Time: ") + .append(ToString(controllerMap_.find(GetState())->second->lastTime_)) + .append("\n"); + + result.append("DUMP DETAILS:\n"); + result.append("Last Screen On: ") + .append(ToString(mDeviceState_.screenState.lastOnTime)) + .append("\n"); + result.append("Last Screen Off: ") + .append(ToString(mDeviceState_.screenState.lastOffTime)) + .append("\n"); + result.append("Last SuspendDevice: ") + .append(ToString(mDeviceState_.lastSuspendDeviceTime)) + .append("\n"); + result.append("Last WakeupDevice: ") + .append(ToString(mDeviceState_.lastWakeupDeviceTime)) + .append("\n"); + result.append("Last Refresh: ") + .append(ToString(mDeviceState_.lastRefreshActivityTime)) + .append("\n"); + + result.append("DUMP EACH STATES:\n"); + for (auto it = controllerMap_.begin(); it != controllerMap_.end(); it++) { + result.append("State: ") + .append(GetPowerStateString(it->second->GetState())) + .append(" Reason:") + .append(GetReasonTypeString(it->second->lastReason_)) + .append(" Time:") + .append(ToString(it->second->lastTime_)) + .append("\n"); + } +} + +TransitResult PowerStateMachine::StateController::TransitTo( + StateChangeReason reason, + bool ignoreLock) +{ + POWER_HILOGI(MODULE_SERVICE, "TransitTo start"); + std::shared_ptr owner = owner_.lock(); + if (owner == nullptr) { + POWER_HILOGE(MODULE_SERVICE, "TransitTo: no owner"); + return TransitResult::OTHER_ERR; + } + POWER_HILOGI(MODULE_SERVICE, + "Transit from %{public}s to %{public}s for %{public}s ignoreLock=%{public}d", + GetPowerStateString(owner->currentState_), + GetPowerStateString(this->state_), + GetReasonTypeString(reason), + ignoreLock); + TransitResult ret = TransitResult::OTHER_ERR; + if (!CheckState()) { + POWER_HILOGE(MODULE_SERVICE, "TransitTo: already in %{public}d", + owner->currentState_); + return TransitResult::ALREADY_IN_STATE; + } + if (!ignoreLock && !owner->CheckRunningLock(GetState())) { + POWER_HILOGE(MODULE_SERVICE, "TransitTo: running lock block"); + return TransitResult::LOCKING; + } + ret = action_(); + if (ret == TransitResult::SUCCESS) { + lastReason_ = reason; + lastTime_ = GetTickCount(); + owner->currentState_ = GetState(); + owner->NotifyPowerStateChanged(owner->currentState_); + } + + POWER_HILOGI(MODULE_SERVICE, "Transit End, result=%{public}d", ret); + return ret; +} + +bool PowerStateMachine::StateController::CheckState() +{ + POWER_HILOGI(MODULE_SERVICE, "CheckState: fun is Start!"); + std::shared_ptr owner = owner_.lock(); + if (owner == nullptr) { + return false; + } + POWER_HILOGI(MODULE_SERVICE, "CheckState: fun is End!"); + return !(GetState() == owner->currentState_); } } // namespace PowerMgr } // namespace OHOS diff --git a/services/native/src/powerms_event_handler.cpp b/services/native/src/powerms_event_handler.cpp index b793e47c2e0771e1c53c12f1d18391f7042141da..0ee31b926db4941a4f131e625c6194f279775648 100644 --- a/services/native/src/powerms_event_handler.cpp +++ b/services/native/src/powerms_event_handler.cpp @@ -44,16 +44,19 @@ void PowermsEventHandler::ProcessEvent([[maybe_unused]] const AppExecFwk::InnerE runningLockMgr->CheckOverTime(); break; } - case CHECK_USER_ACTIVITY_TIMEOUT_MSG: { + case CHECK_USER_ACTIVITY_TIMEOUT_MSG: // fallthrough + case CHECK_USER_ACTIVITY_OFF_TIMEOUT_MSG: // fallthrough + case CHECK_USER_ACTIVITY_SLEEP_TIMEOUT_MSG: // fallthrough + case SYSTEM_WAKE_UP_MSG: { auto powerStateMachine = pmsptr->GetPowerStateMachine(); if (powerStateMachine == nullptr) { return; } - powerStateMachine->HandleDelayTimer(); + powerStateMachine->HandleDelayTimer(event->GetInnerEventId()); break; } default: - POWER_HILOGD(MODULE_SERVICE, "PowermsEventHandler::no event id matched."); + POWER_HILOGD(MODULE_SERVICE, "PowermsEventHandler::no event id matched."); } } } // namespace PowerMgr diff --git a/services/native/src/running_lock_mgr.cpp b/services/native/src/running_lock_mgr.cpp index 94481fbd904bc21a70369b897733ddea76ce718f..9eab1bf502cc3e01c95cd8df29dc8459bc1f3526 100644 --- a/services/native/src/running_lock_mgr.cpp +++ b/services/native/src/running_lock_mgr.cpp @@ -38,13 +38,15 @@ RunningLockMgr::~RunningLockMgr() {} bool RunningLockMgr::Init() { POWER_HILOGI(MODULE_SERVICE, "RunningLockMgr::Init start"); + std::lock_guard lock(mutex_); if (runningLockDeathRecipient_ == nullptr) { runningLockDeathRecipient_ = new RunningLockDeathRecipient(); } if (runningLockAction_ == nullptr) { runningLockAction_ = PowerMgrFactory::GetRunningLockAction(); if (!runningLockAction_) { - POWER_HILOGE(MODULE_SERVICE, "RunningLockMgr::Init create RunningLockMgr fail"); + POWER_HILOGE(MODULE_SERVICE, + "RunningLockMgr::Init create RunningLockMgr fail"); return false; } } @@ -53,12 +55,156 @@ bool RunningLockMgr::Init() return false; } handler_ = pmsptr->GetHandler(); + + systemLocks_.emplace(SystemLockType::SYSTEM_LOCK_APP, + std::make_shared(runningLockAction_, LOCK_TAG_APP)); + systemLocks_.emplace(SystemLockType::SYSTEM_LOCK_DISPLAY, + std::make_shared(runningLockAction_, LOCK_TAG_DISPLAY)); + + bool ret = InitLocks(); + POWER_HILOGI(MODULE_SERVICE, "RunningLockMgr::Init success"); + return ret; +} + +bool RunningLockMgr::InitLocks() +{ + InitLocksTypeScreen(); + InitLocksTypeBackground(); + nitLocksTypeProximity(); return true; } -std::shared_ptr RunningLockMgr::GetRunningLockInner(const sptr& token) +void RunningLockMgr::InitLocksTypeScreen() +{ + lockCounters_.emplace(RunningLockType::RUNNINGLOCK_SCREEN, + std::make_shared( + RunningLockType::RUNNINGLOCK_SCREEN, [this](bool active) { + POWER_HILOGI(MODULE_SERVICE, "RUNNINGLOCK_SCREEN action start!"); + auto pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + return; + } + auto stateMachine = pms->GetPowerStateMachine(); + if (stateMachine == nullptr) { + return; + } + if (active) { + POWER_HILOGI(MODULE_SERVICE, "RUNNINGLOCK_SCREEN active!"); + stateMachine->SetState(PowerState::AWAKE, + StateChangeReason::STATE_CHANGE_REASON_RUNNING_LOCK); + stateMachine->CancelDelayTimer( + PowermsEventHandler::CHECK_USER_ACTIVITY_TIMEOUT_MSG); + stateMachine->CancelDelayTimer( + PowermsEventHandler::CHECK_USER_ACTIVITY_OFF_TIMEOUT_MSG); + } else { + POWER_HILOGI(MODULE_SERVICE, "RUNNINGLOCK_SCREEN inactive!"); + if (stateMachine->GetState() == PowerState::AWAKE) { + stateMachine->ResetInactiveTimer(); + } else { + POWER_HILOGD(MODULE_SERVICE, + "Screen On unlock in state: %{public}d", + stateMachine->GetState()); + } + } + }) + ); +} + +void RunningLockMgr::InitLocksTypeBackground() { + lockCounters_.emplace(RunningLockType::RUNNINGLOCK_BACKGROUND, + std::make_shared( + RunningLockType::RUNNINGLOCK_BACKGROUND, [this](bool active) { + POWER_HILOGI(MODULE_SERVICE, "RUNNINGLOCK_BACKGROUND action start!"); + auto pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + return; + } + auto stateMachine = pms->GetPowerStateMachine(); + if (stateMachine == nullptr) { + return; + } + auto iterator = systemLocks_.find(SystemLockType::SYSTEM_LOCK_APP); + if (iterator == systemLocks_.end()) { + return; + } + std::shared_ptr pSysLock = iterator->second; + if (active) { + POWER_HILOGI(MODULE_SERVICE, "RUNNINGLOCK_BACKGROUND active!"); + stateMachine->CancelDelayTimer( + PowermsEventHandler::CHECK_USER_ACTIVITY_SLEEP_TIMEOUT_MSG); + pSysLock->Lock(); + } else { + POWER_HILOGI(MODULE_SERVICE, "RUNNINGLOCK_BACKGROUND inactive!"); + if (stateMachine->GetState() == PowerState::INACTIVE) { + stateMachine->ResetSleepTimer(); + } else { + POWER_HILOGD(MODULE_SERVICE, + "Background unlock in state: %{public}d", + stateMachine->GetState()); + } + pSysLock->Unlock(); + } + }) + ); +} + +void RunningLockMgr::nitLocksTypeProximity() +{ + lockCounters_.emplace(RunningLockType::RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL, + std::make_shared( + RunningLockType::RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL, [this](bool active) { + POWER_HILOGI(MODULE_SERVICE, + "RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL action start!"); + auto pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + return; + } + auto stateMachine = pms->GetPowerStateMachine(); + if (stateMachine == nullptr) { + return; + } + auto iterator = systemLocks_.find(SystemLockType::SYSTEM_LOCK_APP); + if (iterator == systemLocks_.end()) { + return; + } + std::shared_ptr pSysLock = iterator->second; + if (active) { + POWER_HILOGI(MODULE_SERVICE, + "RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL active!"); + proximityController_.Enable(); + if (proximityController_.IsClose()) { + POWER_HILOGI(MODULE_SERVICE, "INACTIVE when close"); + stateMachine->SetState(PowerState::INACTIVE, + StateChangeReason::STATE_CHANGE_REASON_RUNNING_LOCK, true); + } else { + POWER_HILOGI(MODULE_SERVICE, "AWAKE when away"); + stateMachine->SetState(PowerState::AWAKE, + StateChangeReason::STATE_CHANGE_REASON_RUNNING_LOCK, true); + } + stateMachine->CancelDelayTimer( + PowermsEventHandler::CHECK_USER_ACTIVITY_TIMEOUT_MSG); + stateMachine->CancelDelayTimer( + PowermsEventHandler::CHECK_USER_ACTIVITY_OFF_TIMEOUT_MSG); + pSysLock->Lock(); + } else { + POWER_HILOGI(MODULE_SERVICE, + "RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL inactive!"); + stateMachine->SetState(PowerState::AWAKE, + StateChangeReason::STATE_CHANGE_REASON_RUNNING_LOCK); + stateMachine->ResetInactiveTimer(); + pSysLock->Unlock(); + proximityController_.Disable(); + } + }) + ); +} + +std::shared_ptr RunningLockMgr::GetRunningLockInner( + const sptr& token) +{ + std::lock_guard lock(mutex_); auto iterator = runningLocks_.find(token); if (iterator != runningLocks_.end()) { POWER_HILOGD(MODULE_SERVICE, "RunningLockMgr::%{public}s :find by token.", __func__); @@ -67,31 +213,59 @@ std::shared_ptr RunningLockMgr::GetRunningLockInner(const sptr return nullptr; } -std::shared_ptr RunningLockMgr::CreateRunningLockInner(const sptr& token, - const RunningLockInfo& runningLockInfo, const UserIPCInfo& userIPCinfo) +std::shared_ptr RunningLockMgr::CreateRunningLock( + const sptr& token, + const RunningLockInfo& runningLockInfo, + const UserIPCInfo& userIPCinfo) { auto lockInner = RunningLockInner::CreateRunningLockInner(runningLockInfo, userIPCinfo); if (lockInner == nullptr) { return nullptr; } - POWER_HILOGD(MODULE_SERVICE, "RunningLockMgr::%{public}s : ok,name = %s,type = %d", __func__, - runningLockInfo.name.c_str(), runningLockInfo.type); + POWER_HILOGD(MODULE_SERVICE, + "RunningLockMgr::%{public}s : ok,name = %{public}s,type = %{public}d", + __func__, + runningLockInfo.name.c_str(), + runningLockInfo.type); + mutex_.lock(); runningLocks_.emplace(token, lockInner); + mutex_.unlock(); token->AddDeathRecipient(runningLockDeathRecipient_); - SetRunningLockDisableFlag(lockInner); return lockInner; } -void RunningLockMgr::RemoveAndPostUnlockTask(const sptr& token, uint32_t timeOutMS) +void RunningLockMgr::ReleaseLock(const sptr token) +{ + POWER_HILOGI(MODULE_SERVICE, "RunningLockMgr::%{public}s :token = %{public}p", + __func__, token.GetRefPtr()); + auto lockInner = GetRunningLockInner(token); + if (lockInner == nullptr) { + return; + } + if (!lockInner->GetDisabled()) { + UnLock(token); + } + mutex_.lock(); + runningLocks_.erase(token); + mutex_.unlock(); + token->RemoveDeathRecipient(runningLockDeathRecipient_); +} + +void RunningLockMgr::RemoveAndPostUnlockTask( + const sptr& token, uint32_t timeOutMS) { auto handler = handler_.lock(); if (handler == nullptr) { return; } const string& tokenStr = to_string(reinterpret_cast(token.GetRefPtr())); - POWER_HILOGI(MODULE_SERVICE, "RunningLockMgr::%{public}s :token = %p,tokenStr = %s,timeOutMS = %d", __func__, - token.GetRefPtr(), tokenStr.c_str(), timeOutMS); + POWER_HILOGI(MODULE_SERVICE, + "RunningLockMgr::%{public}s :token = %p,tokenStr = %s,timeOutMS = %d", + __func__, + token.GetRefPtr(), + tokenStr.c_str(), + timeOutMS); handler->RemoveTask(tokenStr); if (timeOutMS != 0) { std::function unLockFunc = std::bind(&RunningLockMgr::UnLock, this, token); @@ -99,57 +273,81 @@ void RunningLockMgr::RemoveAndPostUnlockTask(const sptr& token, u } } -void RunningLockMgr::Lock(const sptr& token, const RunningLockInfo& runningLockInfo, +void RunningLockMgr::Lock(const sptr& token, + const RunningLockInfo& runningLockInfo, const UserIPCInfo& userIPCinfo, uint32_t timeOutMS) { - std::lock_guard lock(mutex_); - POWER_HILOGI(MODULE_SERVICE, "RunningLockMgr::%{public}s :token = %p,name = %s,type = %d", __func__, - token.GetRefPtr(), runningLockInfo.name.c_str(), runningLockInfo.type); + POWER_HILOGI(MODULE_SERVICE, + "RunningLockMgr::%{public}s :token = %p,name = %s,type = %d", + __func__, + token.GetRefPtr(), + runningLockInfo.name.c_str(), + runningLockInfo.type); auto lockInner = GetRunningLockInner(token); if (lockInner == nullptr) { - lockInner = CreateRunningLockInner(token, runningLockInfo, userIPCinfo); - if (lockInner == nullptr) { - POWER_HILOGE(MODULE_SERVICE, "RunningLockMgr::%{public}s :CreateRunningLockInner err,name = %s,type = %d", - __func__, runningLockInfo.name.c_str(), runningLockInfo.type); - return; - } + POWER_HILOGD(MODULE_SERVICE, "RunningLockMgr::Lock failed, can't find %{public}p", + token.GetRefPtr()); + return; + } + if (!lockInner->GetDisabled()) { + POWER_HILOGD(MODULE_SERVICE, "RunningLockMgr::Lock, this lock(%s) is already on", + lockInner->GetRunningLockName().c_str()); + return; + } + lockInner->SetDisabled(false); - LockReally(token, lockInner); - POWER_HILOGD(MODULE_SERVICE, "RunningLockMgr::%{public}s :insert ok,name = %s,type = %d", __func__, - runningLockInfo.name.c_str(), runningLockInfo.type); - } else { - POWER_HILOGD(MODULE_SERVICE, "RunningLockMgr::%{public}s :GetRunningLockInner ok,name = %s,type = %d", - __func__, runningLockInfo.name.c_str(), runningLockInfo.type); + auto iterator = lockCounters_.find(lockInner->GetRunningLockType()); + if (iterator == lockCounters_.end()) { + POWER_HILOGD(MODULE_SERVICE, + "RunningLockMgr::Lock failed, unsupported type %{public}d", + lockInner->GetRunningLockType()); + return; + } + std::shared_ptr counter = iterator->second; + counter->Increase(); + POWER_HILOGD(MODULE_SERVICE, + "LockCounter: %{public}d, %{public}d", + lockInner->GetRunningLockType(), + counter->GetCount()); + if (timeOutMS > 0) { + RemoveAndPostUnlockTask(token, timeOutMS); } - RemoveAndPostUnlockTask(token, timeOutMS); } void RunningLockMgr::UnLock(const sptr token) { - std::lock_guard lock(mutex_); - POWER_HILOGI(MODULE_SERVICE, "RunningLockMgr::%{public}s :token = %p", __func__, token.GetRefPtr()); + POWER_HILOGI(MODULE_SERVICE, "RunningLockMgr::%{public}s :token = %p", + __func__, token.GetRefPtr()); auto lockInner = GetRunningLockInner(token); if (lockInner == nullptr) { return; } - RemoveAndPostUnlockTask(token); - runningLocks_.erase(token); - token->RemoveDeathRecipient(runningLockDeathRecipient_); - // Only disabled lock can really unlocked it. + if (lockInner->GetDisabled()) { + POWER_HILOGD(MODULE_SERVICE, + "RunningLockMgr::Unlock, this lock(%{public}s) is already off", + lockInner->GetRunningLockName().c_str()); + return; + } lockInner->SetDisabled(true); - UnLockReally(token, lockInner); - POWER_HILOGD(MODULE_SERVICE, "RunningLockMgr::%{public}s :name = %s,type = %d,use_count = %ld,erase ok", __func__, - lockInner->GetRunningLockName().c_str(), lockInner->GetRunningLockType(), - lockInner.use_count()); + RemoveAndPostUnlockTask(token); + + auto iterator = lockCounters_.find(lockInner->GetRunningLockType()); + if (iterator == lockCounters_.end()) { + POWER_HILOGD(MODULE_SERVICE, + "RunningLockMgr::Unlock failed, unsupported type %{public}d", + lockInner->GetRunningLockType()); + return; + } + std::shared_ptr counter = iterator->second; + counter->Decrease(); } bool RunningLockMgr::IsUsed(const sptr& token) { - std::lock_guard lock(mutex_); auto lockInner = GetRunningLockInner(token); - if (lockInner == nullptr) { + if (lockInner == nullptr || lockInner->GetDisabled()) { return false; } return true; @@ -169,13 +367,15 @@ uint32_t RunningLockMgr::GetRunningLockNum(RunningLockType type) uint32_t RunningLockMgr::GetValidRunningLockNum(RunningLockType type) { - std::lock_guard lock(mutex_); - return std::count_if(runningLocks_.begin(), runningLocks_.end(), - [&type](const auto& pair) { - auto& lockinner = pair.second; - return (((type == lockinner->GetRunningLockType()) || (type == RunningLockType::RUNNINGLOCK_BUTT)) && - (lockinner->GetDisabled() == false)); - }); + auto iterator = lockCounters_.find(type); + if (iterator == lockCounters_.end()) { + POWER_HILOGD(MODULE_SERVICE, + "GetValidRunningLockNum failed, unsupported type %d", type); + return 0; + } + std::shared_ptr counter = iterator->second; + + return counter->GetCount(); } bool RunningLockMgr::ExistValidRunningLock() @@ -190,10 +390,12 @@ bool RunningLockMgr::ExistValidRunningLock() return false; } -void RunningLockMgr::SetWorkTriggerList(const sptr& token, const WorkTriggerList& workTriggerList) +void RunningLockMgr::SetWorkTriggerList(const sptr& token, + const WorkTriggerList& workTriggerList) { - std::lock_guard lock(mutex_); - POWER_HILOGI(MODULE_SERVICE, "RunningLockMgr::%{public}s :token = %p", __func__, token.GetRefPtr()); + POWER_HILOGI(MODULE_SERVICE, "RunningLockMgr::%{public}s :token = %p", + __func__, + token.GetRefPtr()); auto lockInner = GetRunningLockInner(token); if (lockInner == nullptr) { @@ -208,10 +410,13 @@ void RunningLockMgr::SetWorkTriggerList(const sptr& token, const // If disabled, we really unlock here. UnLockReally(token, lockInner); } -void RunningLockMgr::NotifyHiViewRunningLockInfo(const string& tokenStr, const RunningLockInner& lockInner, + +void RunningLockMgr::NotifyHiViewRunningLockInfo(const string& tokenStr, + const RunningLockInner& lockInner, RunningLockChangedType changeType) const { - string lockName = lockInner.GetRunningLockName().empty() ? "NULL" : lockInner.GetRunningLockName(); + string lockName = lockInner.GetRunningLockName().empty() + ? "NULL" : lockInner.GetRunningLockName(); string msg = "token=" + tokenStr + " lockName=" + lockName + " type=" + to_string(ToUnderlying(lockInner.GetRunningLockType())); auto MakeNotifyInfo = [](int uid, int pid, const string& tag) { @@ -233,7 +438,6 @@ void RunningLockMgr::NotifyHiViewRunningLockInfo(const string& tokenStr, const R void RunningLockMgr::CheckOverTime() { - std::lock_guard lock(mutex_); auto handler = handler_.lock(); if (handler == nullptr) { return; @@ -244,8 +448,11 @@ void RunningLockMgr::CheckOverTime() } int64_t curTime = GetTickCount(); int64_t detectTime = curTime - CHECK_TIMEOUT_INTERVAL_MS; - POWER_HILOGI(MODULE_SERVICE, "RunningLockMgr::%{public}s :cur = %" PRId64 " detectTime=%" PRId64 "", __func__, - curTime, detectTime); + POWER_HILOGI(MODULE_SERVICE, + "RunningLockMgr::%{public}s :cur = %" PRId64 " detectTime=%" PRId64 "", + __func__, + curTime, + detectTime); if (detectTime < 0) { return; } @@ -275,7 +482,8 @@ void RunningLockMgr::SendCheckOverTimeMsg(int64_t delayTime) if (handler == nullptr) { return; } - POWER_HILOGI(MODULE_SERVICE, "RunningLockMgr::%{public}s ,delay = %" PRId64 "", __func__, + POWER_HILOGI(MODULE_SERVICE, "RunningLockMgr::%{public}s ,delay = %" PRId64 "", + __func__, delayTime); handler->SendEvent(PowermsEventHandler::CHECK_RUNNINGLOCK_OVERTIME_MSG, 0, delayTime); } @@ -303,8 +511,10 @@ void RunningLockMgr::NotifyRunningLockChanged(const sptr& token, break; } case NOTIFY_RUNNINGLOCK_OVERTIME: { - POWER_HILOGI(MODULE_SERVICE, "RunningLockMgr::%{public}s :%s token=%s", __func__, - runninglockNotifyStr_.at(changeType).c_str(), tokenStr.c_str()); + POWER_HILOGI(MODULE_SERVICE, "RunningLockMgr::%{public}s :%s token=%s", + __func__, + runninglockNotifyStr_.at(changeType).c_str(), + tokenStr.c_str()); break; } default: { @@ -315,41 +525,56 @@ void RunningLockMgr::NotifyRunningLockChanged(const sptr& token, bool RunningLockMgr::MatchProxyMap(const UserIPCInfo& userIPCinfo) { if (proxyMap_.empty()) { - POWER_HILOGD(MODULE_SERVICE, "%{public}s ret false by proxyMap_.empty(), useripcinfo uid = %d pid = %d.", - __func__, userIPCinfo.uid, userIPCinfo.pid); + POWER_HILOGD(MODULE_SERVICE, + "%{public}s ret false by proxyMap_.empty(), useripcinfo uid = %d pid = %d.", + __func__, + userIPCinfo.uid, + userIPCinfo.pid); return false; } auto it = proxyMap_.find(userIPCinfo.uid); // 1. Find pidset by uid. if (it == proxyMap_.end()) { - POWER_HILOGD(MODULE_SERVICE, "%{public}s not find uidmap, useripcinfo uid = %d pid = %d.", - __func__, userIPCinfo.uid, userIPCinfo.pid); + POWER_HILOGD(MODULE_SERVICE, + "%{public}s not find uidmap, useripcinfo uid = %d pid = %d.", + __func__, + userIPCinfo.uid, + userIPCinfo.pid); return false; } auto& pidset = it->second; // 2. Count by owner pid. if (pidset.count(userIPCinfo.pid) > 0) { - POWER_HILOGD(MODULE_SERVICE, "%{public}s find uidmap and count pid > 1, useripcinfo uid = %d pid = %d.", - __func__, userIPCinfo.uid, userIPCinfo.pid); + POWER_HILOGD(MODULE_SERVICE, + "%{public}s find uidmap and count pid > 1, useripcinfo uid = %d pid = %d.", + __func__, + userIPCinfo.uid, + userIPCinfo.pid); return true; } - POWER_HILOGD(MODULE_SERVICE, "%{public}s find uidmap and count pid = 0,count(-1) = %d, useripcinfo uid = %d " + POWER_HILOGD(MODULE_SERVICE, + "%{public}s find uidmap and count pid = 0,count(-1) = %d, useripcinfo uid = %d " "pid = %d.", __func__, static_cast(pidset.count(INVALID_PID)), - userIPCinfo.uid, userIPCinfo.pid); + userIPCinfo.uid, + userIPCinfo.pid); // 3. Count by INVALID_PID, return true when proxy (uid, -1). return (pidset.count(INVALID_PID) > 0); } -void RunningLockMgr::SetRunningLockDisableFlag(std::shared_ptr& lockInner, bool forceRefresh) +void RunningLockMgr::SetRunningLockDisableFlag( + std::shared_ptr& lockInner, + bool forceRefresh) { if (proxyMap_.empty() && (!forceRefresh)) { /** * Generally when proxymap empty keep the default value false. - * When PGManager cancel proxy uid and pid, because we update the proxy map before, so we should refresh + * When PGManager cancel proxy uid and pid, + * because we update the proxy map before, so we should refresh * all of the runninglock disable flag always. */ - POWER_HILOGD(MODULE_SERVICE, "%{public}s ret false by proxyMap_.empty() and forceRefresh = false", __func__); + POWER_HILOGD(MODULE_SERVICE, + "%{public}s ret false by proxyMap_.empty() and forceRefresh = false", __func__); lockInner->SetDisabled(false); return; } @@ -358,7 +583,8 @@ void RunningLockMgr::SetRunningLockDisableFlag(std::shared_ptr if (matched) { // Matched the lock owner useripcinfo, set disabled directly. lockInner->SetDisabled(true); - POWER_HILOGD(MODULE_SERVICE, "%{public}s MatchProxyMap matched by useripcinfo uid = %d pid = %d.", + POWER_HILOGD(MODULE_SERVICE, + "%{public}s MatchProxyMap matched by useripcinfo uid = %d pid = %d.", __func__, userIPCinfo.uid, userIPCinfo.pid); return; } @@ -367,33 +593,45 @@ void RunningLockMgr::SetRunningLockDisableFlag(std::shared_ptr if (list.empty()) { // Not matched, and no trigger list. lockInner->SetDisabled(false); - POWER_HILOGD(MODULE_SERVICE, "%{public}s useripcinfo not matched and list is empty().", __func__); + POWER_HILOGD(MODULE_SERVICE, + "%{public}s useripcinfo not matched and list is empty().", __func__); return; } bool triggerMatched = true; - // Have trigger list, need to judge whether or not all of the list matched, otherwise we should hold the lock. + // Have trigger list, need to judge whether or not all of the list matched, + // otherwise we should hold the lock. for (auto& workTrigger : list) { UserIPCInfo triggerIPCInfo {workTrigger->GetUid(), workTrigger->GetPid()}; if (!MatchProxyMap(triggerIPCInfo)) { triggerMatched = false; - POWER_HILOGD(MODULE_SERVICE, "%{public}s workTrigger not matched uid = %d, pid = %d.", __func__, - triggerIPCInfo.uid, triggerIPCInfo.pid); + POWER_HILOGD(MODULE_SERVICE, + "%{public}s workTrigger not matched uid = %d, pid = %d.", + __func__, + triggerIPCInfo.uid, + triggerIPCInfo.pid); break; } } lockInner->SetDisabled(triggerMatched); - POWER_HILOGD(MODULE_SERVICE, "%{public}s useripcinfo uid = %d pid = %d, triggerMatched = %d.", __func__, - userIPCinfo.uid, userIPCinfo.pid, triggerMatched); + POWER_HILOGD(MODULE_SERVICE, + "%{public}s useripcinfo uid = %d pid = %d, triggerMatched = %d.", + __func__, + userIPCinfo.uid, + userIPCinfo.pid, + triggerMatched); } -void RunningLockMgr::LockReally(const sptr& token, std::shared_ptr& lockInner) +void RunningLockMgr::LockReally(const sptr& token, + std::shared_ptr& lockInner) { if (lockInner->GetReallyLocked()) { - POWER_HILOGD(MODULE_SERVICE, "%{public}s :return by lockInner->GetReallyLocked() == true.", __func__); + POWER_HILOGD(MODULE_SERVICE, + "%{public}s :return by lockInner->GetReallyLocked() == true.", __func__); return; } if (lockInner->GetDisabled()) { - POWER_HILOGD(MODULE_SERVICE, "%{public}s :return by lockInner->GetDisabled() == true.", __func__); + POWER_HILOGD(MODULE_SERVICE, + "%{public}s :return by lockInner->GetDisabled() == true.", __func__); return; } runningLockAction_->Acquire(lockInner->GetRunningLockType()); @@ -401,22 +639,27 @@ void RunningLockMgr::LockReally(const sptr& token, std::shared_pt NotifyRunningLockChanged(token, lockInner, NOTIFY_RUNNINGLOCK_ADD); POWER_HILOGD(MODULE_SERVICE, "%{public}s :called end.", __func__); } -void RunningLockMgr::UnLockReally(const sptr& token, std::shared_ptr& lockInner) +void RunningLockMgr::UnLockReally(const sptr& token, + std::shared_ptr& lockInner) { /** - * Case 1: Firstly PGManager ProxyRunningLock by uid and pid, secondly application lock by the same uid and - * pid, when call Lock() we matched the proxymap, and no really locked to kernel. So we don't need to - * unlocked to kernel. - * Case 2: Firstly application create the runninglock and call Lock(), and then PGManager Proxyed it, + * Case 1: Firstly PGManager ProxyRunningLock by uid and pid, + * secondly application lock by the same uid and + * pid, when call Lock() we matched the proxymap, and no really locked to kernel. + * So we don't need to unlocked to kernel. + * Case 2: Firstly application create the runninglock and call Lock(), + * and then PGManager Proxyed it, * At this time, lock should be unlocked to kernel because we have locked to kernel for it. */ if (!lockInner->GetReallyLocked()) { - POWER_HILOGD(MODULE_SERVICE, "%{public}s :return by lockInner->GetReallyLocked() == false.", __func__); + POWER_HILOGD(MODULE_SERVICE, + "%{public}s :return by lockInner->GetReallyLocked() == false.", __func__); return; } // If disabled, unlock to the kernel. if (!lockInner->GetDisabled()) { - POWER_HILOGD(MODULE_SERVICE, "%{public}s :return by lockInner->GetDisabled() == false.", __func__); + POWER_HILOGD(MODULE_SERVICE, + "%{public}s :return by lockInner->GetDisabled() == false.", __func__); return; } runningLockAction_->Release(lockInner->GetRunningLockType()); @@ -442,8 +685,8 @@ void RunningLockMgr::ProxyRunningLockInner(bool proxyLock) void RunningLockMgr::ProxyRunningLock(bool proxyLock, pid_t uid, pid_t pid) { - std::lock_guard lock(mutex_); - POWER_HILOGD(MODULE_SERVICE, "%{public}s :proxyLock = %d, uid = %d, pid = %d", __func__, + POWER_HILOGD(MODULE_SERVICE, + "%{public}s :proxyLock = %d, uid = %d, pid = %d", __func__, proxyLock, uid, pid); auto it = proxyMap_.find(uid); if (proxyLock) { @@ -451,7 +694,8 @@ void RunningLockMgr::ProxyRunningLock(bool proxyLock, pid_t uid, pid_t pid) if (it == proxyMap_.end()) { unordered_set pidset({pid}); proxyMap_.emplace(uid, pidset); - POWER_HILOGD(MODULE_SERVICE, "%{public}s :proxyLock = true first emplace {uid = %d, pid = %d}", + POWER_HILOGD(MODULE_SERVICE, + "%{public}s :proxyLock = true first emplace {uid = %d, pid = %d}", __func__, uid, pid); } else { if (pid == INVALID_PID) { @@ -463,37 +707,43 @@ void RunningLockMgr::ProxyRunningLock(bool proxyLock, pid_t uid, pid_t pid) auto& pidset = it->second; pidset.insert(pid); } - POWER_HILOGD(MODULE_SERVICE, "%{public}s :proxyLock = true uid = %d exist insert pid = %d", + POWER_HILOGD(MODULE_SERVICE, + "%{public}s :proxyLock = true uid = %d exist insert pid = %d", __func__, uid, pid); } // 1. Set the matched runninglock inner disabled flag. } else { if (it == proxyMap_.end()) { // No insert proxy info, nothing to erase. - POWER_HILOGD(MODULE_SERVICE, "%{public}s :proxyLock = false not find by uid = %d", + POWER_HILOGD(MODULE_SERVICE, + "%{public}s :proxyLock = false not find by uid = %d", __func__, uid); return; } // 1. Clear the runninglock inner disabled flag 2.removed from proxyMap_ if (pid == INVALID_PID) { proxyMap_.erase(uid); - POWER_HILOGD(MODULE_SERVICE, "%{public}s :proxyLock = false pid = -1 rmv uid = %d map", + POWER_HILOGD(MODULE_SERVICE, + "%{public}s :proxyLock = false pid = -1 rmv uid = %d map", __func__, uid); } else { auto& pidset = it->second; pidset.erase(pid); - POWER_HILOGD(MODULE_SERVICE, "%{public}s :proxyLock = false uid = %d erase single pid = %d", + POWER_HILOGD(MODULE_SERVICE, + "%{public}s :proxyLock = false uid = %d erase single pid = %d", __func__, uid, pid); if (pidset.size() == 0) { proxyMap_.erase(uid); - POWER_HILOGD(MODULE_SERVICE, "%{public}s :pidset.size()=0 erase uid keymap", __func__); + POWER_HILOGD(MODULE_SERVICE, + "%{public}s :pidset.size()=0 erase uid keymap", __func__); } } } ProxyRunningLockInner(proxyLock); } -void RunningLockMgr::NotifyHiView(RunningLockChangedType changeType, const std::string& msg) const +void RunningLockMgr::NotifyHiView(RunningLockChangedType changeType, + const std::string& msg) const { if (msg.empty()) { return; @@ -501,8 +751,51 @@ void RunningLockMgr::NotifyHiView(RunningLockChangedType changeType, const std:: const int logLevel = 2; const string &tag = runninglockNotifyStr_.at(changeType); HiviewDFX::HiSysEvent::Write(HiviewDFX::HiSysEvent::Domain::POWERMGR, "Lock", - HiviewDFX::HiSysEvent::EventType::FAULT, "LOG_LEVEL", logLevel, "TAG", tag, "MESSAGE", msg); - POWER_HILOGI(MODULE_SERVICE, "RunningLockMgr::%{public}s: %s %s", __func__, tag.c_str(), msg.c_str()); + HiviewDFX::HiSysEvent::EventType::FAULT, + "LOG_LEVEL", + logLevel, + "TAG", + tag, + "MESSAGE", + msg); + POWER_HILOGI(MODULE_SERVICE, "RunningLockMgr::%{public}s: %s %s", + __func__, + tag.c_str(), + msg.c_str()); +} + +void RunningLockMgr::EnableMock(IRunningLockAction* mockAction) +{ + // reset lock list + runningLocks_.clear(); + for (auto it = lockCounters_.begin(); it != lockCounters_.end(); it++) { + it->second->Clear(); + } + proximityController_.Clear(); + + std::shared_ptr mock(mockAction); + for (auto it = systemLocks_.begin(); it != systemLocks_.end(); it++) { + it->second->EnableMock(mock); + } + runningLockAction_ = mock; +} + +static const char* GetRunningLockTypeString(RunningLockType type) +{ + switch (type) { + case RunningLockType::RUNNINGLOCK_SCREEN: + return "SCREEN"; + case RunningLockType::RUNNINGLOCK_BACKGROUND: + return "BACKGROUND"; + case RunningLockType::RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL: + return "PROXIMITY_SCREEN_CONTROL"; + case RunningLockType::RUNNINGLOCK_BUTT: + return "BUTT"; + default: + break; + } + + return "UNKNOWN"; } void RunningLockMgr::DumpInfo(std::string& result) @@ -513,11 +806,20 @@ void RunningLockMgr::DumpInfo(std::string& result) result.append("POWER MANAGER DUMP (hidumper -runninglock):\n"); result.append(" totalSize=").append(ToString(runningLocks_.size())) .append(" validSize=").append(ToString(validSize)).append("\n"); + result.append("Summary By Type: \n"); + for (auto it = lockCounters_.begin(); it != lockCounters_.end(); it++) { + result.append(GetRunningLockTypeString(it->first)) + .append(": ") + .append(ToString(it->second->GetCount())) + .append("\n"); + } if (runningLocks_.empty()) { + result.append("Lock List is Empty. \n"); return; } + result.append("Dump Lock List: \n"); auto curTick = GetTickCount(); int index = 0; for (auto& it : runningLocks_) { @@ -532,26 +834,21 @@ void RunningLockMgr::DumpInfo(std::string& result) auto& ipcinfo = lockInner->GetUserIPCInfo(); result.append(" index=").append(ToString(index)) .append(" time=").append(ToString(curTick - lockInner->GetLockTimeMs())) - .append(" type=").append(ToString(static_cast(lockinfo.type))) + .append(" type=").append(GetRunningLockTypeString(lockinfo.type)) .append(" name=").append(lockinfo.name) .append(" uid=").append(ToString(ipcinfo.uid)) .append(" pid=").append(ToString(ipcinfo.pid)) .append(" disable=").append(ToString(lockInner->GetDisabled())) - .append(" reallyLocked=").append(ToString(lockInner->GetReallyLocked())) - .append(" overTimeFlag=").append(ToString(lockInner->GetOverTimeFlag())).append("\n"); - - auto& worklist = lockinfo.workTriggerlist; - result.append(" workTrigger: size=").append(ToString(worklist.size())).append("\n"); - if (worklist.size() != 0) { - for (auto& work : worklist) { - result.append(" name=").append(work->GetName()) - .append(" uid=").append(ToString(work->GetUid())) - .append(" pid=").append(ToString(work->GetPid())) - .append(" abilityid=").append(ToString(work->GetAbilityId())).append("\n"); - } - } - result.append("\n"); + .append("\n"); } + + result.append("Peripherals Info: \n") + .append("Proximity: ") + .append("Enabled: ") + .append(ToString(proximityController_.IsEnabled())) + .append("Status: ") + .append(ToString(proximityController_.GetStatus())) + .append("\n"); } void RunningLockMgr::RunningLockDeathRecipient::OnRemoteDied(const wptr& remote) @@ -569,9 +866,142 @@ void RunningLockMgr::RunningLockDeathRecipient::OnRemoteDied(const wptr forceUnLockFunc = std::bind(&PowerMgrService::ForceUnLock, pms, remote.promote()); - POWER_HILOGI(MODULE_SERVICE, "RunningLockDeathRecipient::%{public}s :remote.promote() = %p", __func__, + POWER_HILOGI(MODULE_SERVICE, + "RunningLockDeathRecipient::%{public}s :remote.promote() = %p", + __func__, remote.promote().GetRefPtr()); handler->PostTask(forceUnLockFunc, TASK_RUNNINGLOCK_FORCEUNLOCK); } + +void RunningLockMgr::SystemLock::Lock() +{ + if (!locking_) { + action_->Lock(RunningLockType::RUNNINGLOCK_BUTT, tag_.c_str()); + locking_ = true; + } +} + +void RunningLockMgr::SystemLock::Unlock() +{ + if (locking_) { + action_->Unlock(RunningLockType::RUNNINGLOCK_BUTT, tag_.c_str()); + locking_ = false; + } +} + +uint32_t RunningLockMgr::LockCounter::Increase() +{ + ++counter_; + if (counter_ == 1) { + activate_(true); + } + return counter_; +} + +uint32_t RunningLockMgr::LockCounter::Decrease() +{ + --counter_; + if (counter_ == 0) { + activate_(false); + } + return counter_; +} + +void RunningLockMgr::LockCounter::Clear() +{ + counter_ = 0; +} + +RunningLockMgr::ProximityController::ProximityController() + : enabled_(false), status_(0) +{ +} + +RunningLockMgr::ProximityController::~ProximityController() +{ +} + +void RunningLockMgr::ProximityController::Enable() +{ + enabled_ = true; +} + +void RunningLockMgr::ProximityController::Disable() +{ + enabled_ = false; +} + +bool RunningLockMgr::ProximityController::IsClose() +{ + POWER_HILOGD(MODULE_SERVICE, "IsClose:%{public}d", isClose); + return isClose; +} + +void RunningLockMgr::ProximityController::OnClose() +{ + if (!enabled_) { + return; + } + auto pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + return; + } + auto stateMachine = pms->GetPowerStateMachine(); + if (stateMachine == nullptr) { + return; + } + isClose = true; + auto runningLock = pms->GetRunningLockMgr(); + if (runningLock->GetValidRunningLockNum( + RunningLockType::RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL) > 0) { + POWER_HILOGD(MODULE_SERVICE, "Change to INACITVE when come close"); + stateMachine->SetState(PowerState::INACTIVE, + StateChangeReason::STATE_CHANGE_REASON_SENSOR, + true); + } +} + +void RunningLockMgr::ProximityController::OnAway() +{ + if (!enabled_) { + return; + } + auto pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + return; + } + auto stateMachine = pms->GetPowerStateMachine(); + if (stateMachine == nullptr) { + return; + } + isClose = false; + auto runningLock = pms->GetRunningLockMgr(); + if (runningLock->GetValidRunningLockNum( + RunningLockType::RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL) > 0) { + POWER_HILOGD(MODULE_SERVICE, "Change to AWAKE when go away"); + stateMachine->SetState(PowerState::AWAKE, + StateChangeReason::STATE_CHANGE_REASON_SENSOR, + true); + } +} + +void RunningLockMgr::ProximityController::Clear() +{ + isClose = false; +} + +void RunningLockMgr::SetProximity(uint32_t status) +{ + switch (status) { + case PROXIMITY_CLOSE: + proximityController_.OnClose(); + break; + case PROXIMITY_AWAY: + proximityController_.OnAway(); + break; + default: + break; + } +} } // namespace PowerMgr } // namespace OHOS diff --git a/services/native/src/shutdown_service.cpp b/services/native/src/shutdown_service.cpp index f3528b542d14069a233199599bf81d02527fa222..5847af3a91eec32dd0d40f69da6cf9017fb8968a 100644 --- a/services/native/src/shutdown_service.cpp +++ b/services/native/src/shutdown_service.cpp @@ -62,6 +62,12 @@ void ShutdownService::DelShutdownCallback(const sptr& callbac callbackMgr_.RemoveCallback(callback); } +bool ShutdownService::IsShuttingDown() +{ + POWER_HILOGE(MODULE_SERVICE, "IsShuttingDown"); + return started_; +} + void ShutdownService::RebootOrShutdown(const std::string& reason, bool isReboot) { if (started_) { diff --git a/services/native/test/unittest/BUILD.gn b/services/native/test/unittest/BUILD.gn index b4504f2c2192fc3962ed41f6ff332b0a36ad47d1..60707f5d0ab16bc65b1d0933d0a8bd5e7e7a22ad 100644 --- a/services/native/test/unittest/BUILD.gn +++ b/services/native/test/unittest/BUILD.gn @@ -22,7 +22,30 @@ config("module_private_config") { include_dirs = [ "include", + "mock", "//utils/system/safwk/native/include", + "//third_party/googletest/googletest/include", + "//base/powermgr/display_manager/interfaces/innerkits/native/include/", + "//base/powermgr/display_manager/service/native/include/", + "//base/powermgr/display_manager/service/zidl/include/", + "//drivers/peripheral/display/interfaces/include/", + "//base/powermgr/power_manager/services/native/test/unittest/include", + "//foundation/graphic/standard/utils/include/", + ] +} +config("module_private_event_config") { + visibility = [ ":*" ] + + include_dirs = [ + "//utils/native/base/include", + "//foundation/appexecfwk/standard/common/log/include", + "//foundation/appexecfwk/standard/interfaces/innerkits/libeventhandler/include", + "//base/notification/ces_standard/cesfwk/innerkits/include", + "//base/notification/ces_standard/cesfwk/services/include", + "//base/notification/ces_standard/cesfwk/kits/native/include", + "//foundation/aafwk/standard/interfaces/innerkits/base/include", + "//foundation/aafwk/standard/interfaces/innerkits/want/include/ohos/aafwk/content", + "//base/notification/ces_standard/common/log/include", ] } @@ -38,7 +61,166 @@ deps_ex = [ "samgr_L2:samgr_proxy", ] +############################shutdown_callback_test############################# +ohos_unittest("shutdown_callback_test") { + module_out_path = module_output_path + sources = [ "src/power_shutdown_callback_test.cpp" ] + configs = [ + "${powermgr_utils_path}:utils_config", + ":module_private_config", + ] + deps = [ + "${powermgr_native_innerkits_path}:powermgr_client", + "${powermgr_service_path}:powermgrservice", + "${powermgr_service_path}/native/src/actions:powermgr_actions", + "//third_party/googletest:gtest_main", + "//utils/native/base:utils", + ] + external_deps = deps_ex +} + +##############################shutdown_test##################################### +ohos_unittest("shutdown_test") { + module_out_path = module_output_path + sources = [ "src/shutdown_test.cpp" ] + configs = [ + "${powermgr_utils_path}:utils_config", + ":module_private_config", + ] + deps = [ + "${powermgr_native_innerkits_path}:powermgr_client", + "${powermgr_service_path}:powermgrservice", + "${powermgr_service_path}/native/src/actions:powermgr_actions", + "//third_party/googletest:gtest_main", + "//utils/native/base:utils", + ] + external_deps = deps_ex +} + +##############################reboot_test##################################### +ohos_unittest("reboot_test") { + module_out_path = module_output_path + sources = [ "src/reboot_test.cpp" ] + configs = [ + "${powermgr_utils_path}:utils_config", + ":module_private_config", + ] + deps = [ + "${powermgr_native_innerkits_path}:powermgr_client", + "${powermgr_service_path}:powermgrservice", + "${powermgr_service_path}/native/src/actions:powermgr_actions", + "//third_party/googletest:gtest_main", + "//utils/native/base:utils", + ] + external_deps = deps_ex +} + ##############################unittest########################################## +ohos_unittest("test_register_callback_mode") { + module_out_path = module_output_path + + sources = [ "src/power_register_callback_mode_test.cpp" ] + + configs = [ + "${powermgr_utils_path}:utils_config", + ":module_private_config", + ] + + deps = [ + "${powermgr_native_innerkits_path}:powermgr_client", + "${powermgr_service_path}:powermgrservice", + "${powermgr_service_path}/native/src/actions:powermgr_actions", + "//third_party/googletest:gtest_main", + "//utils/native/base:utils", + ] + + external_deps = deps_ex +} + +ohos_unittest("test_power_get_mode") { + module_out_path = module_output_path + + sources = [ "src/power_get_mode_test.cpp" ] + + configs = [ + "${powermgr_utils_path}:utils_config", + ":module_private_config", + ] + + deps = [ + "${powermgr_native_innerkits_path}:powermgr_client", + "${powermgr_service_path}:powermgrservice", + "${powermgr_service_path}/native/src/actions:powermgr_actions", + "//third_party/googletest:gtest_main", + "//utils/native/base:utils", + ] + + external_deps = deps_ex +} + +ohos_unittest("test_power_set_mode") { + module_out_path = module_output_path + + sources = [ "src/power_set_mode_test.cpp" ] + + configs = [ + "${powermgr_utils_path}:utils_config", + ":module_private_config", + ] + + deps = [ + "${powermgr_native_innerkits_path}:powermgr_client", + "${powermgr_service_path}:powermgrservice", + "${powermgr_service_path}/native/src/actions:powermgr_actions", + "//third_party/googletest:gtest_main", + "//utils/native/base:utils", + ] + + external_deps = deps_ex +} + +ohos_unittest("test_power_shutdown") { + module_out_path = module_output_path + + sources = [ "src/power_shutdown_test.cpp" ] + + configs = [ + "${powermgr_utils_path}:utils_config", + ":module_private_config", + ] + + deps = [ + "${powermgr_native_innerkits_path}:powermgr_client", + "${powermgr_service_path}:powermgrservice", + "${powermgr_service_path}/native/src/actions:powermgr_actions", + "//third_party/googletest:gtest_main", + "//utils/native/base:utils", + ] + + external_deps = deps_ex +} + +ohos_unittest("test_power_device_mode") { + module_out_path = module_output_path + + sources = [ "src/power_device_mode_test.cpp" ] + + configs = [ + "${powermgr_utils_path}:utils_config", + ":module_private_config", + ] + + deps = [ + "${powermgr_native_innerkits_path}:powermgr_client", + "${powermgr_service_path}:powermgrservice", + "${powermgr_service_path}/native/src/actions:powermgr_actions", + "//third_party/googletest:gtest_main", + "//utils/native/base:utils", + ] + + external_deps = deps_ex +} + ohos_unittest("test_running_lock") { module_out_path = module_output_path @@ -81,6 +263,29 @@ ohos_unittest("test_power_mgr_service") { external_deps = deps_ex } +ohos_unittest("ces_system") { + module_out_path = module_output_path + + sources = [ "src/ces_system_test.cpp" ] + + configs = [ + "${powermgr_utils_path}:utils_config", + ":module_private_config", + ":module_private_event_config", + ] + + deps = [ + "${powermgr_native_innerkits_path}:powermgr_client", + "${powermgr_service_path}:powermgrservice", + "${powermgr_service_path}/native/src/actions:powermgr_actions", + "${powermgr_utils_path}:powermgr_utils", + "//third_party/googletest:gtest_main", + "//utils/native/base:utils", + ] + + external_deps = deps_ex +} + ohos_unittest("test_power_state_machine") { module_out_path = module_output_path @@ -96,6 +301,30 @@ ohos_unittest("test_power_state_machine") { "${powermgr_service_path}:powermgrservice", "${powermgr_service_path}/native/src/actions:powermgr_actions", "${powermgr_utils_path}:powermgr_utils", + "//third_party/googletest:gmock_main", + "//third_party/googletest:gtest_main", + "//utils/native/base:utils", + ] + + external_deps = deps_ex +} + +ohos_unittest("test_power_mgr_mock") { + module_out_path = module_output_path + + sources = [ "src/power_mgr_mock_test.cpp" ] + + configs = [ + "${powermgr_utils_path}:utils_config", + ":module_private_config", + ] + + deps = [ + "${powermgr_native_innerkits_path}:powermgr_client", + "${powermgr_service_path}:powermgrservice", + "${powermgr_service_path}/native/src/actions:powermgr_actions", + "${powermgr_utils_path}:powermgr_utils", + "//third_party/googletest:gmock_main", "//third_party/googletest:gtest_main", "//utils/native/base:utils", ] @@ -117,6 +346,8 @@ if (false) { deps = [ "${powermgr_native_innerkits_path}:powermgr_client", "${powermgr_service_path}:powermgrservice", + "${powermgr_service_path}/native/src/actions:powermgr_actions", + "${powermgr_utils_path}:powermgr_utils", "//third_party/googletest:gtest_main", "//utils/native/base:utils", ] @@ -128,7 +359,10 @@ if (false) { group("unittest") { testonly = true deps = [ + ":shutdown_callback_test", + ":test_power_device_mode", ":test_power_mgr_service", + ":test_power_shutdown", ":test_power_state_machine", ":test_running_lock", diff --git a/services/native/test/unittest/include/ces_system_test.h b/services/native/test/unittest/include/ces_system_test.h new file mode 100644 index 0000000000000000000000000000000000000000..6b0d189c36ac754bc2f56ebc982d1f37fbc4ebdb --- /dev/null +++ b/services/native/test/unittest/include/ces_system_test.h @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2021 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. + */ + +#ifndef CES_SYSTEM_TEST_H +#define CES_SYSTEM_TEST_H + +#include + +#include "power_mgr_service.h" +#include "power_state_callback_stub.h" +#include "common_event_subscriber.h" +#include "common_event_support.h" + +namespace OHOS { +namespace EventFwk { +constexpr int SLEEP_WAIT_TIME_S = 6; +class CesSystemTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); + + bool PublishCommonEventTest(const std::string &eventName); + class CommonEventServiCesSystemTest : public CommonEventSubscriber { + public: + explicit CommonEventServiCesSystemTest(const CommonEventSubscribeInfo &subscriberInfo); + virtual ~CommonEventServiCesSystemTest() {}; + virtual void OnReceiveEvent(const CommonEventData &data); +}; +}; +} // namespace PowerMgr +} // namespace OHOS +#endif // CES_SYSTEM_TEST_H \ No newline at end of file diff --git a/services/native/test/unittest/include/power_device_mode_test.h b/services/native/test/unittest/include/power_device_mode_test.h new file mode 100644 index 0000000000000000000000000000000000000000..dbce7958964007a8e9bc6c4ed42443b075d792e2 --- /dev/null +++ b/services/native/test/unittest/include/power_device_mode_test.h @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2021 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. + */ + +#ifndef POWERMGR_POWER_MODE_TEST_H +#define POWERMGR_POWER_MODE_TEST_H + +#include + +#include "power_mgr_service.h" +#include "power_state_callback_stub.h" + +namespace OHOS { +namespace PowerMgr { +constexpr int SLEEP_WAIT_TIME_S = 6; +class PowerDeviceModeTest : public testing::Test { +public: + + class PowerModeTest1Callback : public IRemoteStub { + public: + PowerModeTest1Callback() {}; + virtual ~PowerModeTest1Callback() {}; + virtual void PowerModeCallback() override; + }; + class PowerModeTest2Callback : public IRemoteStub { + public: + PowerModeTest2Callback() {}; + virtual ~PowerModeTest2Callback() {}; + virtual void PowerModeCallback() override; + }; +}; +} // namespace PowerMgr +} // namespace OHOS +#endif // POWERMGR_POWER_MODE_TEST_H diff --git a/services/native/test/unittest/include/power_get_mode_test.h b/services/native/test/unittest/include/power_get_mode_test.h new file mode 100644 index 0000000000000000000000000000000000000000..eaa03f54671ddaa27ac3f5e82ec629a887a42ccf --- /dev/null +++ b/services/native/test/unittest/include/power_get_mode_test.h @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2021 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. + */ + +#ifndef POWERMGR_GET_MODE_TEST_H +#define POWERMGR_GET_MODE_TEST_H + +#include + +#include "power_mgr_service.h" +#include "power_state_callback_stub.h" + +namespace OHOS { +namespace PowerMgr { +constexpr int SLEEP_WAIT_TIME_S = 6; +class PowerGetModeTest : public testing::Test { +public: +}; +} // namespace PowerMgr +} // namespace OHOS +#endif // POWERMGR_GET_MODE_TEST_H diff --git a/services/native/test/unittest/include/power_mgr_mock_test.h b/services/native/test/unittest/include/power_mgr_mock_test.h new file mode 100644 index 0000000000000000000000000000000000000000..68e4711e1b2aee945f9fac3286c5add5c6158f81 --- /dev/null +++ b/services/native/test/unittest/include/power_mgr_mock_test.h @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2021 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. + */ + +#ifndef POWERMGR_POWERMGR_MOCK_TEST_H +#define POWERMGR_POWERMGR_MOCK_TEST_H + +#include +#include + +#include "mock_lock_action.h" +#include "mock_power_action.h" +#include "mock_state_action.h" +#include "power_common.h" +#include "power_mgr_service.h" + +namespace OHOS { +namespace PowerMgr { +constexpr int NEXT_WAIT_TIME_S = 1; +constexpr int ASYNC_WAIT_TIME_S = 3; +constexpr int REFRESHACTIVITY_WAIT_TIME_S = 8; +constexpr int SCREEN_OFF_WAIT_TIME_S = (DEFAULT_DISPLAY_OFF_TIME / 1000); +constexpr int SCREEN_DIM_WAIT_TIME_S = (SCREEN_OFF_WAIT_TIME_S * 2 / 3); +constexpr int SCREEN_DIM_TO_OFF_TIME_S = (SCREEN_OFF_WAIT_TIME_S / 3); +constexpr int SLEEP_WAIT_TIME_S = (DEFAULT_SLEEP_TIME / 1000); + +class PowerMgrMockTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; +} // namespace PowerMgr +} // namespace OHOS + +#endif // POWERMGR_POWERMGR_MOCK_TEST_H diff --git a/services/native/test/unittest/include/power_reboot_test.h b/services/native/test/unittest/include/power_reboot_test.h new file mode 100644 index 0000000000000000000000000000000000000000..75870b7560737065a1c914f2c4c7b701d17ba965 --- /dev/null +++ b/services/native/test/unittest/include/power_reboot_test.h @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2021 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. + */ +#ifndef POWERMGR_STATE_MACHINE_TEST_H +#define POWERMGR_STATE_MACHINE_TEST_H + +#include + +#include "power_mgr_service.h" +#include "power_state_callback_stub.h" + +namespace OHOS { +namespace PowerMgr { +constexpr int SLEEP_WAIT_TIME_S = 6; +class PowerRebootTest : public testing::Test { +public: +}; +} // namespace PowerMgr +} // namespace OHOS +#endif // POWERMGR_STATE_MACHINE_TEST_H diff --git a/services/native/test/unittest/include/power_register_callback_mode_test.h b/services/native/test/unittest/include/power_register_callback_mode_test.h new file mode 100644 index 0000000000000000000000000000000000000000..c3bde291b231f49ce32ee6f914d7c820121a9384 --- /dev/null +++ b/services/native/test/unittest/include/power_register_callback_mode_test.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2021 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. + */ + +#ifndef POWERMGR_REGISTER_CALLBACK_MODE_TEST_H +#define POWERMGR_REGISTER_CALLBACK_MODE_TEST_H + +#include + +#include "power_mgr_service.h" +#include "power_mode_callback_stub.h" + +namespace OHOS { +namespace PowerMgr { +constexpr int SLEEP_WAIT_TIME_S = 6; +class PowerRegisterCallbackModeTest : public testing::Test { +public: + + class PowerModeTest1Callback : public PowerModeCallbackStub { + public: + PowerModeTest1Callback() {}; + virtual ~PowerModeTest1Callback() {}; + virtual void PowerModeCallback() override; + }; +}; +} // namespace PowerMgr +} // namespace OHOS +#endif // POWERMGR_REGISTER_CALLBACK_MODE_TEST_H diff --git a/services/native/test/unittest/include/power_set_mode_test.h b/services/native/test/unittest/include/power_set_mode_test.h new file mode 100644 index 0000000000000000000000000000000000000000..827b17c836398238efb16df023aca3aa7ceb5fd8 --- /dev/null +++ b/services/native/test/unittest/include/power_set_mode_test.h @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2021 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. + */ + +#ifndef POWERMGR_SET_MODE_TEST_H +#define POWERMGR_SET_MODE_TEST_H + +#include + +#include "power_mgr_service.h" +#include "power_state_callback_stub.h" + +namespace OHOS { +namespace PowerMgr { +constexpr int SLEEP_WAIT_TIME_S = 6; +class PowerSetModeTest : public testing::Test { +public: +}; +} // namespace PowerMgr +} // namespace OHOS +#endif // POWERMGR_SET_MODE_TEST_H \ No newline at end of file diff --git a/services/native/test/unittest/include/power_shutdown_callback_test.h b/services/native/test/unittest/include/power_shutdown_callback_test.h new file mode 100644 index 0000000000000000000000000000000000000000..7f664623ca57a027ab87bb80e8777aa6ac14858c --- /dev/null +++ b/services/native/test/unittest/include/power_shutdown_callback_test.h @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2021 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. + */ + +#ifndef POWERMGR_SHUTDOWN_CALLBACK_TEST_H +#define POWERMGR_SHUTDOWN_CALLBACK_TEST_H + +#include + +#include "power_mgr_service.h" +#include "power_shutdown_callback_stub.h" + +namespace OHOS { +namespace PowerMgr { +constexpr int SLEEP_WAIT_TIME_S = 6; +class PowerShutdownCallbackTest : public testing::Test { +public: + + class PowerShutdownTest1Callback : public PowerShutdownCallbackStub { + public: + PowerShutdownTest1Callback() {}; + virtual ~PowerShutdownTest1Callback() {}; + virtual void ShutdownCallback() override; + }; + class PowerShutdownTest2Callback : public PowerShutdownCallbackStub { + public: + PowerShutdownTest2Callback() {}; + virtual ~PowerShutdownTest2Callback() {}; + virtual void ShutdownCallback() override; + }; +}; +} // namespace PowerMgr +} // namespace OHOS +#endif // POWERMGR_SHUTDOWN_CALLBACK_TEST_H diff --git a/services/native/test/unittest/include/power_shutdown_test.h b/services/native/test/unittest/include/power_shutdown_test.h new file mode 100644 index 0000000000000000000000000000000000000000..7b8907fbb6d9bc47522a8b04460e10eba2cc0083 --- /dev/null +++ b/services/native/test/unittest/include/power_shutdown_test.h @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2021 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. + */ + +#ifndef POWERMGR_STATE_MACHINE_TEST_H +#define POWERMGR_STATE_MACHINE_TEST_H + +#include + +#include "power_mgr_service.h" +#include "power_shutdown_callback_stub.h" + +namespace OHOS { +namespace PowerMgr { +constexpr int SLEEP_WAIT_TIME_S = 6; +class PowerShutdownTest : public testing::Test { +public: + + class PowerShutdownTest1Callback : public PowerShutdownCallbackStub { + public: + PowerShutdownTest1Callback() {}; + virtual ~PowerShutdownTest1Callback() {}; + virtual void ShutdownCallback() override; + }; + class PowerShutdownTest2Callback : public PowerShutdownCallbackStub { + public: + PowerShutdownTest2Callback() {}; + virtual ~PowerShutdownTest2Callback() {}; + virtual void ShutdownCallback() override; + }; +}; +} // namespace PowerMgr +} // namespace OHOS +#endif // POWERMGR_STATE_MACHINE_TEST_H diff --git a/services/native/test/unittest/include/power_state_machine_test.h b/services/native/test/unittest/include/power_state_machine_test.h index b9f75e7bff6409317b1d2c2720b67c816eebfdc0..3ec2b64632cfa0b837e73cca701c1772cc0600d4 100644 --- a/services/native/test/unittest/include/power_state_machine_test.h +++ b/services/native/test/unittest/include/power_state_machine_test.h @@ -17,12 +17,15 @@ #define POWERMGR_STATE_MACHINE_TEST_H #include +#include #include "power_mgr_service.h" #include "power_state_callback_stub.h" +#include "power_hdf_client.h" namespace OHOS { namespace PowerMgr { +constexpr int NEXT_WAIT_TIME_S = 1; constexpr int SLEEP_WAIT_TIME_S = 6; constexpr int REFRESHACTIVITY_WAIT_TIME_S = 8; constexpr int SCREEN_OFF_WAIT_TIME_S = 15; @@ -34,6 +37,13 @@ public: void SetUp(); void TearDown(); bool IsTestSupported(); + void PowerClientInit(); + void CheckWriteWakeCount(); + void CheckReadWakeCount(); + void WakeUpthread(); + void Suspendthread(); + void Rebootthread(); + void Shutdownthread(); class PowerStateTest1Callback : public PowerStateCallbackStub { public: PowerStateTest1Callback() {}; @@ -46,7 +56,9 @@ public: virtual ~PowerStateTest2Callback() {}; virtual void OnPowerStateChanged(PowerState state) override; }; +private: + std::unique_ptr client_; }; } // namespace PowerMgr } // namespace OHOS -#endif // POWERMGR_STATE_MACHINE_TEST_H +#endif // POWERMGR_STATE_MACHINE_TEST_H \ No newline at end of file diff --git a/services/native/test/unittest/mock/mock_lock_action.h b/services/native/test/unittest/mock/mock_lock_action.h new file mode 100644 index 0000000000000000000000000000000000000000..c3bef0f27dbe0c01c7fe7c635d96b4aa2adbb550 --- /dev/null +++ b/services/native/test/unittest/mock/mock_lock_action.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2021 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. + */ + +#ifndef POWERMGR_MOCK_LOCK_ACTION_H +#define POWERMGR_MOCK_LOCK_ACTION_H + +#include +#include "actions/irunning_lock_action.h" + +namespace OHOS { +namespace PowerMgr { +class MockLockAction : public IRunningLockAction { +public: + MockLockAction() = default; + virtual ~MockLockAction() = default; + MOCK_METHOD1(Acquire, void(RunningLockType type)); + MOCK_METHOD1(Release, void(RunningLockType type)); + MOCK_METHOD2(Lock, void(RunningLockType type, const std::string& tag)); + MOCK_METHOD2(Unlock, void(RunningLockType type, const std::string& tag)); +}; +} // namespace PowerMgr +} // namespace OHOS + +#endif // POWERMGR_MOCK_POWER_ACTION_H \ No newline at end of file diff --git a/services/native/test/unittest/mock/mock_power_action.h b/services/native/test/unittest/mock/mock_power_action.h new file mode 100644 index 0000000000000000000000000000000000000000..9946a4fc9478c150739595cf2f84ff036b44a594 --- /dev/null +++ b/services/native/test/unittest/mock/mock_power_action.h @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2021 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. + */ + +#ifndef POWERMGR_MOCK_POWER_ACTION_H +#define POWERMGR_MOCK_POWER_ACTION_H + +#include +#include "actions/idevice_power_action.h" + +namespace OHOS { +namespace PowerMgr { +class MockPowerAction : public IDevicePowerAction { +public: + MockPowerAction() = default; + virtual ~MockPowerAction() = default; + MOCK_METHOD1(Reboot, void(const std::string& reason)); + MOCK_METHOD1(Shutdown, void(const std::string& reason)); +}; +} // namespace PowerMgr +} // namespace OHOS + +#endif // POWERMGR_MOCK_POWER_ACTION_H \ No newline at end of file diff --git a/services/native/test/unittest/mock/mock_state_action.h b/services/native/test/unittest/mock/mock_state_action.h new file mode 100644 index 0000000000000000000000000000000000000000..a0040ed5b92102f6cf2b5b487170650a47103ec0 --- /dev/null +++ b/services/native/test/unittest/mock/mock_state_action.h @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2021 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. + */ + +#ifndef POWERMGR_MOCK_STATE_ACTION_H +#define POWERMGR_MOCK_STATE_ACTION_H + +#include +#include "actions/idevice_state_action.h" + +namespace OHOS { +namespace PowerMgr { +class MockStateAction : public IDeviceStateAction { +public: + MockStateAction() = default; + virtual ~MockStateAction() = default; + MOCK_METHOD3(Suspend, void(int64_t callTimeMs, SuspendDeviceType type, uint32_t flags)); + MOCK_METHOD0(ForceSuspend, void()); + MOCK_METHOD4(Wakeup, void(int64_t callTimeMs, WakeupDeviceType type, + const std::string& details, const std::string& pkgName)); + MOCK_METHOD3(RefreshActivity, void(int64_t callTimeMs, UserActivityType type, uint32_t flags)); + MOCK_METHOD0(GetDisplayState, DisplayState()); + MOCK_METHOD1(SetDisplayState, uint32_t(DisplayState state)); + MOCK_METHOD3(GoToSleep, uint32_t(std::function onSuspend, std::function onWakeup, bool force)); +}; +} // namespace PowerMgr +} // namespace OHOS + +#endif // POWERMGR_MOCK_STATE_ACTION_H \ No newline at end of file diff --git a/services/native/test/unittest/src/ces_system_test.cpp b/services/native/test/unittest/src/ces_system_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b4b30defcff3642fc0bdecb7fdd64fe3dd22a668 --- /dev/null +++ b/services/native/test/unittest/src/ces_system_test.cpp @@ -0,0 +1,92 @@ +/* + * Copyright (c) 2021 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 + +#include "common_event_manager.h" +#include "ces_system_test.h" +#define private public +#define protected public +#undef private +#undef protected + +#include "datetime_ex.h" + +#include "power_common.h" +#include "power_mgr_client.h" +#include "power_mgr_service.h" +#include "power_state_machine.h" +#include "sys_param.h" + +using namespace testing::ext; +using namespace OHOS::EventFwk; +using namespace OHOS::PowerMgr; + +namespace { +std::mutex mtx_; + +const std::string CompareStr = "cesComparesStrForCase"; +const std::string CompareStrFalse = "cesComparesStrForCaseFalse"; +} // namespace + +CesSystemTest::CommonEventServiCesSystemTest::CommonEventServiCesSystemTest( + const CommonEventSubscribeInfo &subscriberInfo) + : CommonEventSubscriber(subscriberInfo) +{} + +void CesSystemTest::CommonEventServiCesSystemTest::OnReceiveEvent(const CommonEventData &data) +{ + std::string action = data.GetWant().GetAction(); + if (action == CommonEventSupport::COMMON_EVENT_SHUTDOWN) { + POWER_HILOGD(MODULE_SERVICE, "CommonEventServiCesSystemTest::OnReceiveEvent."); + } + POWER_HILOGD(MODULE_SERVICE, "CommonEventServiCesSystemTest::OnReceiveEvent other."); +} + +void CesSystemTest::SetUpTestCase() +{} + +void CesSystemTest::TearDownTestCase() +{} + +void CesSystemTest::SetUp() +{} + +void CesSystemTest::TearDown() +{} + +namespace { +/* + * @tc.number: CES_SubscriptionEvent_0100 + * @tc.name: SubscribeCommonEvent + * @tc.desc: Verify the function when the input string is normal + */ +HWTEST_F(CesSystemTest, CES_SubscriptionEvent_0100, Function | MediumTest | Level1) +{ + bool result = false; + MatchingSkills matchingSkills; + matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_SHUTDOWN); + CommonEventSubscribeInfo subscribeInfo(matchingSkills); + auto subscriberPtr = std::make_shared(subscribeInfo); + result = CommonEventManager::SubscribeCommonEvent(subscriberPtr); + EXPECT_TRUE(result); + GTEST_LOG_(INFO) << "CES_SubscriptionEvent_0100: ShutDownDevice start."; + auto& powerMgrClient = PowerMgrClient::GetInstance(); + powerMgrClient.ShutDownDevice(string("ShutDownDeviceTest001")); + sleep(SLEEP_WAIT_TIME_S); + CommonEventManager::UnSubscribeCommonEvent(subscriberPtr); +} +} \ No newline at end of file diff --git a/services/native/test/unittest/src/power_device_mode_test.cpp b/services/native/test/unittest/src/power_device_mode_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a18f04120ed3a69c907cd73d06b8e13768bda27d --- /dev/null +++ b/services/native/test/unittest/src/power_device_mode_test.cpp @@ -0,0 +1,108 @@ +/* + * Copyright (c) 2021 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 "power_device_mode_test.h" + +#include + +#include +#include +#include +#include +#include + +#include "power_common.h" +#include "power_mgr_client.h" +#include "power_mgr_service.h" +#include "power_state_machine.h" +#include "sys_param.h" + +using namespace testing::ext; +using namespace OHOS::PowerMgr; +using namespace OHOS; +using namespace std; + + +void PowerDeviceModeTest::PowerModeTest1Callback::PowerModeCallback() +{ + POWER_HILOGD(MODULE_SERVICE, "PowerModeTest1Callback::PowerModeCallback."); +} + +void PowerDeviceModeTest::PowerModeTest2Callback::PowerModeCallback() +{ + POWER_HILOGD(MODULE_SERVICE, "PowerModeTest2Callback::PowerModeCallback."); +} + +/** + * @tc.name: PowerStateCallback001 + * @tc.desc: test PowerStateCallback + * @tc.type: FUNC + */ +HWTEST_F (PowerDeviceModeTest, PowerDeviceModeCallback001, TestSize.Level0) +{ + auto& powerMgrClient = PowerMgrClient::GetInstance(); + sptr cb1 = new PowerModeTest1Callback(); + powerMgrClient.RegisterPowerModeCallback(cb1); + POWER_HILOGD(MODULE_SERVICE, "PowerDeviceModeCallback001 1."); + { + sptr cb2 = new PowerModeTest2Callback(); + powerMgrClient.UnRegisterPowerModeCallback(cb2); + POWER_HILOGD(MODULE_SERVICE, "PowerDeviceModeCallback001 2."); + powerMgrClient.RegisterPowerModeCallback(cb2); + POWER_HILOGD(MODULE_SERVICE, "PowerDeviceModeCallback001 3."); + powerMgrClient.RegisterPowerModeCallback(cb2); + POWER_HILOGD(MODULE_SERVICE, "PowerDeviceModeCallback001 4."); + } + powerMgrClient.UnRegisterPowerModeCallback(cb1); + POWER_HILOGD(MODULE_SERVICE, "PowerDeviceModeTest::PowerDeviceModeCallback001 end."); +} + +/** + * @tc.name: SetDeviceModeTest001 + * @tc.desc: test SetDeviceMode in proxy + * @tc.type: FUNC + */ +HWTEST_F (PowerDeviceModeTest, SetDeviceModeTest001, TestSize.Level2) +{ + sleep(SLEEP_WAIT_TIME_S); + GTEST_LOG_(INFO) << "SetDeviceModeTest001: SetDeviceMode start."; + auto& powerMgrClient = PowerMgrClient::GetInstance(); + + uint32_t mode = 601; + if (false) { + powerMgrClient.SetDeviceMode(mode); + } + + GTEST_LOG_(INFO) << "SetDeviceModeTest001: SetDeviceMode end."; +} + +/** + * @tc.name: GetDeviceModeTest001 + * @tc.desc: test GetDeviceMode in proxy + * @tc.type: FUNC + */ +HWTEST_F (PowerDeviceModeTest, GetDeviceModeTest001, TestSize.Level2) +{ + uint32_t mode = 0; + sleep(SLEEP_WAIT_TIME_S); + GTEST_LOG_(INFO) << "GetDeviceModeTest001: GetDeviceMode start."; + auto& powerMgrClient = PowerMgrClient::GetInstance(); + + if (false) { + mode = powerMgrClient.GetDeviceMode(); + } + + GTEST_LOG_(INFO) << "GetDeviceModeTest001: GetDeviceMode end. mode == " << mode; +} \ No newline at end of file diff --git a/services/native/test/unittest/src/power_get_mode_test.cpp b/services/native/test/unittest/src/power_get_mode_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8dcc71270e64487d71cce17521b488b4a8ce9765 --- /dev/null +++ b/services/native/test/unittest/src/power_get_mode_test.cpp @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2021 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 "power_get_mode_test.h" + +#include + +#include +#include +#include +#include +#include + +#include "power_common.h" +#include "power_mgr_client.h" +#include "power_mgr_service.h" +#include "power_state_machine.h" +#include "sys_param.h" + +using namespace testing::ext; +using namespace OHOS::PowerMgr; +using namespace OHOS; +using namespace std; + +/** + * @tc.name: GetDeviceModeTest001 + * @tc.desc: test GetDeviceMode in proxy + * @tc.type: FUNC + */ +HWTEST_F (PowerGetModeTest, GetDeviceModeTest001, TestSize.Level0) +{ + uint32_t mode = 0; + sleep(SLEEP_WAIT_TIME_S); + GTEST_LOG_(INFO) << "GetDeviceModeTest001: GetDeviceMode start."; + auto& powerMgrClient = PowerMgrClient::GetInstance(); + + if (true) { + mode = powerMgrClient.GetDeviceMode(); + } + + GTEST_LOG_(INFO) << "GetDeviceModeTest001: GetDeviceMode end. mode == " << mode; +} \ No newline at end of file diff --git a/services/native/test/unittest/src/power_mgr_mock_test.cpp b/services/native/test/unittest/src/power_mgr_mock_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5be4ae5e76e46bd8f6c99268a3b296f97355229c --- /dev/null +++ b/services/native/test/unittest/src/power_mgr_mock_test.cpp @@ -0,0 +1,3119 @@ +/* + * Copyright (c) 2021 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 "power_mgr_mock_test.h" + +using namespace testing::ext; +using namespace OHOS::PowerMgr; +using namespace OHOS; +using namespace std; +using ::testing::_; + +static sptr service; +static MockStateAction* stateAction; +static MockPowerAction* powerAction; +static MockLockAction* lockAction; + +static void ResetMockAction() +{ + POWER_HILOGD(MODULE_SERVICE, "ResetMockAction:Start."); + stateAction = new MockStateAction(); + powerAction = new MockPowerAction(); + lockAction = new MockLockAction(); + service->EnableMock(stateAction, powerAction, lockAction); +} + +void PowerMgrMockTest::SetUpTestCase(void) +{ + // create singleton service object at the beginning + service = DelayedSpSingleton::GetInstance(); + service->OnStart(); + ResetMockAction(); +} + +void PowerMgrMockTest::TearDownTestCase(void) +{ + service->OnStop(); + DelayedSpSingleton::DestroyInstance(); + delete stateAction; + delete powerAction; + delete lockAction; +} + +void PowerMgrMockTest::SetUp(void) +{ +} + +void PowerMgrMockTest::TearDown(void) +{ +} + +namespace { +/** + * @tc.name: PowerMgrUnittest001 + * @tc.desc: test RebootDevice by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest001, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest001: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest001:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest001: Failed to get PowerMgrService"; + } + + EXPECT_CALL(*powerAction, Reboot(std::string("test"))).Times(1); + pms->RebootDevice(std::string("test")); + sleep(ASYNC_WAIT_TIME_S); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest001:End."); + GTEST_LOG_(INFO) << "PowerMgrUnittest001: end."; +} + +/** + * @tc.name: PowerMgrUnittest002 + * @tc.desc: test ShutDownDevice by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest002, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest002: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest002:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest002: Failed to get PowerMgrService"; + } + + EXPECT_CALL(*powerAction, Shutdown(std::string("test"))).Times(1); + pms->ShutDownDevice(std::string("test")); + sleep(ASYNC_WAIT_TIME_S); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest002:End."); + GTEST_LOG_(INFO) << "PowerMgrUnittest002: end."; +} + + +/** + * @tc.name: PowerMgrUnittest003 + * @tc.desc: test SuspendDevice by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest003, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest003: start."; + + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest003:Start."); + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest003: Failed to get PowerMgrService"; + } + + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest003:Start mock."); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_CALL(*stateAction, + Suspend(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false)); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest003:Start suspend."); + pms->SuspendDevice(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest003:end."); + GTEST_LOG_(INFO) << "PowerMgrUnittest003: end."; +} + +/** + * @tc.name: PowerMgrUnittest004 + * @tc.desc: test SuspendDevice by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest004, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest004: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest004:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest004: Failed to get PowerMgrService"; + } + + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_CALL(*stateAction, + Suspend(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_DEVICE_ADMIN, false)); + pms->SuspendDevice(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_DEVICE_ADMIN, false); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest004:End ."); + GTEST_LOG_(INFO) << "PowerMgrUnittest004: end."; +} + +/** + * @tc.name: PowerMgrUnittest005 + * @tc.desc: test SuspendDevice by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest005, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest005: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest005:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest005: Failed to get PowerMgrService"; + } + + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_CALL(*stateAction, + Suspend(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_TIMEOUT, false)); + pms->SuspendDevice(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_TIMEOUT, false); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest005:End"); + GTEST_LOG_(INFO) << "PowerMgrUnittest005: end."; +} + +/** + * @tc.name: PowerMgrUnittest006 + * @tc.desc: test SuspendDevice by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest006, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest006: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest006:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest006: Failed to get PowerMgrService"; + } + + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_CALL(*stateAction, + Suspend(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_LID_SWITCH, false)); + pms->SuspendDevice(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_LID_SWITCH, false); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest006:End."); + GTEST_LOG_(INFO) << "PowerMgrUnittest006: end."; +} + +/** + * @tc.name: PowerMgrUnittest007 + * @tc.desc: test SuspendDevice by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest007, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest007: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest007:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest007: Failed to get PowerMgrService"; + } + + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_CALL(*stateAction, + Suspend(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_POWER_BUTTON, false)); + pms->SuspendDevice(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_POWER_BUTTON, false); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest007:End."); + GTEST_LOG_(INFO) << "PowerMgrUnittest007: end."; +} + +/** + * @tc.name: PowerMgrUnittest008 + * @tc.desc: test SuspendDevice by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest008, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest008: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest008:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest008: Failed to get PowerMgrService"; + } + + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_CALL(*stateAction, + Suspend(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_HDMI, false)); + pms->SuspendDevice(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_HDMI, false); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest008:End."); + GTEST_LOG_(INFO) << "PowerMgrUnittest008: end."; +} + +/** + * @tc.name: PowerMgrUnittest009 + * @tc.desc: test SuspendDevice by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest009, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest009: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest009:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest009: Failed to get PowerMgrService"; + } + + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_CALL(*stateAction, + Suspend(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_SLEEP_BUTTON, false)); + pms->SuspendDevice(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_SLEEP_BUTTON, false); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest009:End."); + GTEST_LOG_(INFO) << "PowerMgrUnittest009: end."; +} + +/** + * @tc.name: PowerMgrUnittest010 + * @tc.desc: test SuspendDevice by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest010, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest010: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest010:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest010: Failed to get PowerMgrService"; + } + + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_CALL(*stateAction, + Suspend(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_ACCESSIBILITY, false)); + pms->SuspendDevice(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_ACCESSIBILITY, false); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest010:End."); + GTEST_LOG_(INFO) << "PowerMgrUnittest010: end."; +} + +/** + * @tc.name: PowerMgrUnittest011 + * @tc.desc: test SuspendDevice by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest011, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest011: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest011:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest011: Failed to get PowerMgrService"; + } + + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_CALL(*stateAction, + Suspend(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_FORCE_SUSPEND, false)); + pms->SuspendDevice(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_FORCE_SUSPEND, false); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest011:End."); + GTEST_LOG_(INFO) << "PowerMgrUnittest011: end."; +} + +/** + * @tc.name: PowerMgrUnittest012 + * @tc.desc: test WakeupDevice by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest012, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest012: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest012:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest012: Failed to get PowerMgrService"; + } + + pms->SuspendDevice(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_ON)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_CALL(*stateAction, + Wakeup(0, WakeupDeviceType::WAKEUP_DEVICE_UNKNOWN, + std::string("test"), _)).Times(1); + pms->WakeupDevice(0, WakeupDeviceType::WAKEUP_DEVICE_UNKNOWN, std::string("test")); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest012:End."); + GTEST_LOG_(INFO) << "PowerMgrUnittest012: end."; +} + +/** + * @tc.name: PowerMgrUnittest013 + * @tc.desc: test WakeupDevice by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest013, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest013: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest013:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest013: Failed to get PowerMgrService"; + } + + pms->SuspendDevice(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_ON)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_CALL(*stateAction, + Wakeup(0, WakeupDeviceType::WAKEUP_DEVICE_POWER_BUTTON, + std::string("test"), _)).Times(1); + pms->WakeupDevice(0, WakeupDeviceType::WAKEUP_DEVICE_POWER_BUTTON, std::string("test")); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest013:End."); + GTEST_LOG_(INFO) << "PowerMgrUnittest013: end."; +} + +/** + * @tc.name: PowerMgrUnittest014 + * @tc.desc: test WakeupDevice by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest014, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest014: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest014:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest014: Failed to get PowerMgrService"; + } + + pms->SuspendDevice(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_ON)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_CALL(*stateAction, + Wakeup(0, WakeupDeviceType::WAKEUP_DEVICE_APPLICATION, + std::string("test"), _)).Times(1); + pms->WakeupDevice(0, WakeupDeviceType::WAKEUP_DEVICE_APPLICATION, std::string("test")); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest014:End."); + GTEST_LOG_(INFO) << "PowerMgrUnittest014: end."; +} + +/** + * @tc.name: PowerMgrUnittest015 + * @tc.desc: test WakeupDevice by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest015, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest015: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest015:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest015: Failed to get PowerMgrService"; + } + + pms->SuspendDevice(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_ON)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_CALL(*stateAction, + Wakeup(0, WakeupDeviceType::WAKEUP_DEVICE_PLUGGED_IN, + std::string("test"), _)).Times(1); + pms->WakeupDevice(0, WakeupDeviceType::WAKEUP_DEVICE_PLUGGED_IN, std::string("test")); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest015:End."); + GTEST_LOG_(INFO) << "PowerMgrUnittest015: end."; +} + +/** + * @tc.name: PowerMgrUnittest016 + * @tc.desc: test WakeupDevice by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest016, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest016: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest016:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest016: Failed to get PowerMgrService"; + } + + pms->SuspendDevice(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_ON)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_CALL(*stateAction, + Wakeup(0, WakeupDeviceType::WAKEUP_DEVICE_GESTURE, + std::string("test"), _)).Times(1); + pms->WakeupDevice(0, WakeupDeviceType::WAKEUP_DEVICE_GESTURE, std::string("test")); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest016:End."); + GTEST_LOG_(INFO) << "PowerMgrUnittest016: end."; +} + +/** + * @tc.name: PowerMgrUnittest017 + * @tc.desc: test WakeupDevice by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest017, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest017: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest017:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest017: Failed to get PowerMgrService"; + } + + pms->SuspendDevice(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_ON)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_CALL(*stateAction, + Wakeup(0, WakeupDeviceType::WAKEUP_DEVICE_CAMERA_LAUNCH, + std::string("test"), _)).Times(1); + pms->WakeupDevice(0, WakeupDeviceType::WAKEUP_DEVICE_CAMERA_LAUNCH, std::string("test")); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest017:End."); + GTEST_LOG_(INFO) << "PowerMgrUnittest017: end."; +} + +/** + * @tc.name: PowerMgrUnittest018 + * @tc.desc: test WakeupDevice by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest018, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest018: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest018:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest018: Failed to get PowerMgrService"; + } + + pms->SuspendDevice(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_ON)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_CALL(*stateAction, + Wakeup(0, WakeupDeviceType::WAKEUP_DEVICE_WAKE_KEY, + std::string("test"), _)).Times(1); + pms->WakeupDevice(0, WakeupDeviceType::WAKEUP_DEVICE_WAKE_KEY, std::string("test")); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest018:End."); + GTEST_LOG_(INFO) << "PowerMgrUnittest018: end."; +} + +/** + * @tc.name: PowerMgrUnittest019 + * @tc.desc: test WakeupDevice by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest019, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest019: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest019:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest019: Failed to get PowerMgrService"; + } + + pms->SuspendDevice(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_ON)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_CALL(*stateAction, + Wakeup(0, WakeupDeviceType::WAKEUP_DEVICE_WAKE_MOTION, + std::string("test"), _)).Times(1); + pms->WakeupDevice(0, WakeupDeviceType::WAKEUP_DEVICE_WAKE_MOTION, std::string("test")); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest019:End."); + GTEST_LOG_(INFO) << "PowerMgrUnittest019: end."; +} + +/** + * @tc.name: PowerMgrUnittest020 + * @tc.desc: test WakeupDevice by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest020, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest020: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest020:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest020: Failed to get PowerMgrService"; + } + + pms->SuspendDevice(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_ON)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_CALL(*stateAction, + Wakeup(0, WakeupDeviceType::WAKEUP_DEVICE_HDMI, + std::string("test"), _)).Times(1); + pms->WakeupDevice(0, WakeupDeviceType::WAKEUP_DEVICE_HDMI, std::string("test")); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest020:End."); + GTEST_LOG_(INFO) << "PowerMgrUnittest020: end."; +} + +/** + * @tc.name: PowerMgrUnittest021 + * @tc.desc: test WakeupDevice by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest021, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest021: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest021:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest021: Failed to get PowerMgrService"; + } + + pms->SuspendDevice(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_ON)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_CALL(*stateAction, + Wakeup(0, WakeupDeviceType::WAKEUP_DEVICE_LID, + std::string("test"), _)).Times(1); + pms->WakeupDevice(0, WakeupDeviceType::WAKEUP_DEVICE_LID, std::string("test")); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest021:End."); + GTEST_LOG_(INFO) << "PowerMgrUnittest021: end."; +} + +/** + * @tc.name: PowerMgrUnittest022 + * @tc.desc: test SuspendDevice and auto sleep by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest022, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest022: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest022:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest022: Failed to get PowerMgrService"; + } + + pms->WakeupDevice(0, WakeupDeviceType::WAKEUP_DEVICE_UNKNOWN, std::string("test")); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)) + .Times(::testing::AtLeast(1)) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_CALL(*stateAction, + Suspend(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false)); + EXPECT_CALL(*stateAction, GoToSleep(_, _, false)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_OFF)); + pms->SuspendDevice(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false); + sleep(SLEEP_WAIT_TIME_S + 1); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest022:End."); + GTEST_LOG_(INFO) << "PowerMgrUnittest022: end."; +} + +/** + * @tc.name: PowerMgrUnittest023 + * @tc.desc: test WakeupDevice and auto suspend and sleep by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest023, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest023: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest023:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest023: Failed to get PowerMgrService"; + } + + EXPECT_CALL(*stateAction, GoToSleep(_, _, false)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_ON)); + pms->WakeupDevice(0, WakeupDeviceType::WAKEUP_DEVICE_LID, std::string("test")); + sleep((SCREEN_OFF_WAIT_TIME_S*2/3) + 1); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_DIM)); + sleep((SCREEN_OFF_WAIT_TIME_S*1/3) + 1); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_OFF)); + sleep(SLEEP_WAIT_TIME_S + 1); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest023:End."); + GTEST_LOG_(INFO) << "PowerMgrUnittest023: end."; +} + +/** + * @tc.name: PowerMgrUnittest024 + * @tc.desc: test RefreshActivity by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest024, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest024: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest024:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest024: Failed to get PowerMgrService"; + } + + EXPECT_CALL(*stateAction, GetDisplayState()) + .Times(::testing::AtLeast(1)) + .WillOnce(::testing::Return(DisplayState::DISPLAY_DIM)); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_ON)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_CALL(*stateAction, + RefreshActivity(0, UserActivityType::USER_ACTIVITY_TYPE_TOUCH, _)).Times(1); + pms->RefreshActivity(0, UserActivityType::USER_ACTIVITY_TYPE_TOUCH, true); + + sleep(SCREEN_OFF_WAIT_TIME_S + SLEEP_WAIT_TIME_S + 1); + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest024:End."); + GTEST_LOG_(INFO) << "PowerMgrUnittest024: end."; +} + +/** + * @tc.name: PowerMgrUnittest025 + * @tc.desc: test IsScreenOn by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest025, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest025: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest025:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest025: Failed to get PowerMgrService"; + } + + EXPECT_CALL(*stateAction, GetDisplayState()) + .Times(::testing::AtLeast(1)) + .WillOnce(::testing::Return(DisplayState::DISPLAY_ON)); + EXPECT_EQ(pms->IsScreenOn(), true); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest025:End."); + GTEST_LOG_(INFO) << "PowerMgrUnittest025: end."; +} + +/** + * @tc.name: PowerMgrUnittest026 + * @tc.desc: test IsScreenOn by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest026, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest026: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest026:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest026: Failed to get PowerMgrService"; + } + + EXPECT_CALL(*stateAction, GetDisplayState()) + .Times(::testing::AtLeast(1)) + .WillOnce(::testing::Return(DisplayState::DISPLAY_DIM)); + EXPECT_EQ(pms->IsScreenOn(), true); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest026:End"); + GTEST_LOG_(INFO) << "PowerMgrUnittest026: end."; +} + +/** + * @tc.name: PowerMgrUnittest027 + * @tc.desc: test IsScreenOn by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest027, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest027: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest027:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest027: Failed to get PowerMgrService"; + } + + EXPECT_CALL(*stateAction, GetDisplayState()) + .Times(::testing::AtLeast(1)) + .WillOnce(::testing::Return(DisplayState::DISPLAY_OFF)); + EXPECT_EQ(pms->IsScreenOn(), false); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest027:End."); + GTEST_LOG_(INFO) << "PowerMgrUnittest027: end."; +} + +/** + * @tc.name: PowerMgrUnittest028 + * @tc.desc: test IsScreenOn by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest028, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest028: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest028:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest028: Failed to get PowerMgrService"; + } + + EXPECT_CALL(*stateAction, GetDisplayState()) + .Times(::testing::AtLeast(1)) + .WillOnce(::testing::Return(DisplayState::DISPLAY_SUSPEND)); + EXPECT_EQ(pms->IsScreenOn(), false); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest028:End."); + GTEST_LOG_(INFO) << "PowerMgrUnittest028: end."; +} + +/** + * @tc.name: PowerMgrUnittest029 + * @tc.desc: test ForceSuspendDevice by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest029, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest029: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest029:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest029: Failed to get PowerMgrService"; + } + + EXPECT_CALL(*stateAction, GoToSleep(_, _, true)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_EQ(pms->ForceSuspendDevice(0), true); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest029:End."); + GTEST_LOG_(INFO) << "PowerMgrUnittest029: end."; +} + +/** + * @tc.name: PowerMgrUnittest030 + * @tc.desc: test Screen on RunningLock by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest030, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest030: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest030:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest030: Failed to get PowerMgrService"; + } + + sptr token = new RunningLockTokenStub(); + RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_SCREEN); + pms->CreateRunningLock(token, info); + pms->Lock(token, info, 0); + EXPECT_EQ(pms->IsUsed(token), true); + + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_DIM)).Times(0); + sleep(SCREEN_DIM_WAIT_TIME_S + 1); + + pms->UnLock(token); + EXPECT_EQ(pms->IsUsed(token), false); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest030:End."); + GTEST_LOG_(INFO) << "PowerMgrUnittest030: end."; +} + +/** + * @tc.name: PowerMgrUnittest031 + * @tc.desc: test background RunningLock by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest031, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest031: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest031:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest031: Failed to get PowerMgrService"; + } + + sptr token = new RunningLockTokenStub(); + RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_BACKGROUND); + pms->CreateRunningLock(token, info); + + EXPECT_CALL(*lockAction, Lock(_, _)).Times(1); + pms->Lock(token, info, 0); + EXPECT_EQ(pms->IsUsed(token), true); + + EXPECT_CALL(*stateAction, GoToSleep(_, _, _)).Times(0); + pms->SuspendDevice(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false); + sleep(SLEEP_WAIT_TIME_S + 1); + + EXPECT_CALL(*lockAction, Unlock(_, _)).Times(1); + pms->UnLock(token); + EXPECT_EQ(pms->IsUsed(token), false); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest031:End."); + GTEST_LOG_(INFO) << "PowerMgrUnittest031: end."; +} + +/** + * @tc.name: PowerMgrUnittest032 + * @tc.desc: test proximity RunningLock by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest032, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest032: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest032:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest032: Failed to get PowerMgrService"; + } + + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_DIM)).Times(0); + sptr token = new RunningLockTokenStub(); + RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL); + pms->CreateRunningLock(token, info); + + EXPECT_CALL(*lockAction, Lock(_, _)).Times(1); + pms->Lock(token, info, 0); + EXPECT_EQ(pms->IsUsed(token), true); + + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_DIM)).Times(0); + sleep(SCREEN_DIM_WAIT_TIME_S + 1); + + EXPECT_CALL(*lockAction, Unlock(_, _)).Times(1); + pms->UnLock(token); + EXPECT_EQ(pms->IsUsed(token), false); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest032:End."); + GTEST_LOG_(INFO) << "PowerMgrUnittest032: end."; +} + +/** + * @tc.name: PowerMgrUnittest033 + * @tc.desc: test RunningLock release by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest033, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest033: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest033:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest033: Failed to get PowerMgrService"; + } + + sptr token = new RunningLockTokenStub(); + RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_BACKGROUND); + pms->CreateRunningLock(token, info); + pms->ReleaseRunningLock(token); + + EXPECT_CALL(*lockAction, Lock(_, _)).Times(0); + pms->Lock(token, info, 0); + EXPECT_EQ(pms->IsUsed(token), false); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest033:End."); + GTEST_LOG_(INFO) << "PowerMgrUnittest033: end."; +} + +/** + * @tc.name: PowerMgrUnittest034 + * @tc.desc: test RefreshActivity by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest034, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest034: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest034:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest034: Failed to get PowerMgrService"; + } + + EXPECT_CALL(*stateAction, GetDisplayState()) + .Times(::testing::AtLeast(1)) + .WillOnce(::testing::Return(DisplayState::DISPLAY_DIM)); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_ON)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_CALL(*stateAction, + RefreshActivity(0, UserActivityType::USER_ACTIVITY_TYPE_OTHER, _)).Times(1); + pms->RefreshActivity(0, UserActivityType::USER_ACTIVITY_TYPE_OTHER, true); + + sleep(SCREEN_OFF_WAIT_TIME_S + SLEEP_WAIT_TIME_S + 1); + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest034:End."); + GTEST_LOG_(INFO) << "PowerMgrUnittest034: end."; +} + +/** + * @tc.name: PowerMgrUnittest035 + * @tc.desc: test RefreshActivity by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest035, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest035: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest035:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest035: Failed to get PowerMgrService"; + } + + EXPECT_CALL(*stateAction, GetDisplayState()) + .Times(::testing::AtLeast(1)) + .WillOnce(::testing::Return(DisplayState::DISPLAY_DIM)); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_ON)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_CALL(*stateAction, + RefreshActivity(0, UserActivityType::USER_ACTIVITY_TYPE_BUTTON, _)).Times(1); + pms->RefreshActivity(0, UserActivityType::USER_ACTIVITY_TYPE_BUTTON, true); + + sleep(SCREEN_OFF_WAIT_TIME_S + SLEEP_WAIT_TIME_S + 1); + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest035:End."); + GTEST_LOG_(INFO) << "PowerMgrUnittest035: end."; +} + +/** + * @tc.name: PowerMgrUnittest036 + * @tc.desc: test RefreshActivity by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest036, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest036: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest036:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest036: Failed to get PowerMgrService"; + } + + EXPECT_CALL(*stateAction, GetDisplayState()) + .Times(::testing::AtLeast(1)) + .WillOnce(::testing::Return(DisplayState::DISPLAY_DIM)); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_ON)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_CALL(*stateAction, + RefreshActivity(0, UserActivityType::USER_ACTIVITY_TYPE_ACCESSIBILITY, _)).Times(1); + pms->RefreshActivity(0, UserActivityType::USER_ACTIVITY_TYPE_ACCESSIBILITY, true); + + sleep(SCREEN_OFF_WAIT_TIME_S + SLEEP_WAIT_TIME_S + 1); + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest036:End."); + GTEST_LOG_(INFO) << "PowerMgrUnittest036: end."; +} + +/** + * @tc.name: PowerMgrUnittest037 + * @tc.desc: test RefreshActivity by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest037, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest037: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest037:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest037: Failed to get PowerMgrService"; + } + + EXPECT_CALL(*stateAction, GetDisplayState()) + .Times(::testing::AtLeast(1)) + .WillOnce(::testing::Return(DisplayState::DISPLAY_DIM)); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_ON)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_CALL(*stateAction, + RefreshActivity(0, UserActivityType::USER_ACTIVITY_TYPE_ATTENTION, _)).Times(1); + pms->RefreshActivity(0, UserActivityType::USER_ACTIVITY_TYPE_ATTENTION, true); + + sleep(SCREEN_OFF_WAIT_TIME_S + SLEEP_WAIT_TIME_S + 1); + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest037:End."); + GTEST_LOG_(INFO) << "PowerMgrUnittest037: end."; +} + +/** + * @tc.name: PowerMgrUnittest038 + * @tc.desc: test RefreshActivity by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest038, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest038: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest038:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest038: Failed to get PowerMgrService"; + } + + EXPECT_CALL(*stateAction, GetDisplayState()) + .Times(::testing::AtLeast(1)) + .WillOnce(::testing::Return(DisplayState::DISPLAY_DIM)); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_ON)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_CALL(*stateAction, + RefreshActivity(0, UserActivityType::USER_ACTIVITY_TYPE_SOFTWARE, _)).Times(1); + pms->RefreshActivity(0, UserActivityType::USER_ACTIVITY_TYPE_SOFTWARE, true); + + sleep(SCREEN_OFF_WAIT_TIME_S + SLEEP_WAIT_TIME_S + 1); + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest038:End."); + GTEST_LOG_(INFO) << "PowerMgrUnittest038: end."; +} + +/** + * @tc.name: PowerMgrUnittest039 + * @tc.desc: test RefreshActivity by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest039, TestSize.Level2) +{ + UserActivityType abnormaltype = {}; + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest039: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest039:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest039: Failed to get PowerMgrService"; + } + + pms->WakeupDevice(0, WakeupDeviceType::WAKEUP_DEVICE_UNKNOWN, std::string("test")); + EXPECT_CALL(*stateAction, GetDisplayState()) + .Times(::testing::AtLeast(1)) + .WillOnce(::testing::Return(DisplayState::DISPLAY_DIM)); + EXPECT_CALL(*stateAction, + RefreshActivity(0, _, _)).Times(1); + pms->RefreshActivity(0, abnormaltype, true); + sleep(SLEEP_WAIT_TIME_S + 1); + EXPECT_EQ(pms->IsScreenOn(), false); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest039:End."); + GTEST_LOG_(INFO) << "PowerMgrUnittest039: end."; +} + +/** + * @tc.name: PowerMgrUnittest040 + * @tc.desc: test RefreshActivity by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest040, TestSize.Level2) +{ + UserActivityType abnormaltype=UserActivityType(6); + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest040: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest040:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest040: Failed to get PowerMgrService"; + } + + pms->WakeupDevice(0, WakeupDeviceType::WAKEUP_DEVICE_UNKNOWN, std::string("test")); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_ON)); + sleep((SCREEN_OFF_WAIT_TIME_S*2/3) + 1); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_DIM)); + EXPECT_CALL(*stateAction, + RefreshActivity(0, _, _)).Times(0); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)) + .Times(::testing::AtLeast(1)) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + pms->RefreshActivity(0, abnormaltype, true); + sleep((SCREEN_OFF_WAIT_TIME_S*1/3) + 1); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest040:End."); + GTEST_LOG_(INFO) << "PowerMgrUnittest040: end."; +} + +/** + * @tc.name: PowerMgrUnittest041 + * @tc.desc: test WakeupDevice by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest041, TestSize.Level2) +{ + WakeupDeviceType abnormaltype=WakeupDeviceType(10); + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest041: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest041:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest041: Failed to get PowerMgrService"; + } + + pms->SuspendDevice(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_ON)) + .Times(0); + EXPECT_CALL(*stateAction, + Wakeup(0, _, + std::string("test"), _)).Times(0); + pms->WakeupDevice(0, abnormaltype, std::string("test")); + EXPECT_EQ(pms->IsScreenOn(), false); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest041:End."); + GTEST_LOG_(INFO) << "PowerMgrUnittest041: end."; +} + +/** + * @tc.name: PowerMgrUnittest042 + * @tc.desc: test WakeupDevice by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest042, TestSize.Level2) +{ + WakeupDeviceType abnormaltype=WakeupDeviceType(999); + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest042: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest042:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest042: Failed to get PowerMgrService"; + } + + pms->SuspendDevice(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_ON)) + .Times(0); + EXPECT_CALL(*stateAction, + Wakeup(0, _, + std::string("test"), _)).Times(0); + pms->WakeupDevice(0, abnormaltype, std::string("test")); + EXPECT_EQ(PowerState::INACTIVE, pms->GetState()); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest042:End."); + GTEST_LOG_(INFO) << "PowerMgrUnittest042: end."; +} + +/** + * @tc.name: PowerMgrUnittest043 + * @tc.desc: test WakeupDevice and auto suspend by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest043, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest043: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest043:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest043: Failed to get PowerMgrService"; + } + + pms->WakeupDevice(0, WakeupDeviceType::WAKEUP_DEVICE_UNKNOWN, std::string("test")); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_ON)); + sleep((SCREEN_OFF_WAIT_TIME_S*2/3) + 1); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_DIM)); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)) + .Times(::testing::AtLeast(1)) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + sleep((SCREEN_OFF_WAIT_TIME_S*1/3) + 1); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest043:End."); + GTEST_LOG_(INFO) << "PowerMgrUnittest043: end."; +} + +/** + * @tc.name: PowerMgrUnittest044 + * @tc.desc: test WakeupDevice and auto suspend by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest044, TestSize.Level2) +{ + WakeupDeviceType abnormaltype = WakeupDeviceType(10); + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest044: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest044:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest044: Failed to get PowerMgrService"; + } + + pms->WakeupDevice(0, WakeupDeviceType::WAKEUP_DEVICE_UNKNOWN, std::string("test")); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_DIM)) + .Times(::testing::AtLeast(1)) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_ON)); + sleep((SCREEN_OFF_WAIT_TIME_S*2/3)/2); + pms->WakeupDevice(0, abnormaltype, std::string("test")); + sleep((SCREEN_OFF_WAIT_TIME_S*2/3)/2 + 1); + + ResetMockAction(); + GTEST_LOG_(INFO) << "PowerMgrUnittest044: end."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest044:End."); +} + +/** + * @tc.name: PowerMgrUnittest045 + * @tc.desc: test WakeupDevice and auto suspend by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest045, TestSize.Level2) +{ + WakeupDeviceType abnormaltype = WakeupDeviceType(999); + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest045: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest045:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest045: Failed to get PowerMgrService"; + } + + pms->WakeupDevice(0, WakeupDeviceType::WAKEUP_DEVICE_UNKNOWN, std::string("test")); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_ON)); + sleep((SCREEN_OFF_WAIT_TIME_S*2/3) + 1); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_DIM)); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)) + .Times(::testing::AtLeast(1)) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + sleep((SCREEN_OFF_WAIT_TIME_S*2/3) /2); + pms->WakeupDevice(0, abnormaltype, std::string("test")); + sleep((SCREEN_OFF_WAIT_TIME_S*1/3)/2 + 1); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest045:End."); + GTEST_LOG_(INFO) << "PowerMgrUnittest045: end."; +} + +/** + * @tc.name: PowerMgrUnittest046 + * @tc.desc: test RefreshActivity by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest046, TestSize.Level2) +{ + UserActivityType abnormaltype = UserActivityType(6); + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest046: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest046:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest046: Failed to get PowerMgrService"; + } + + EXPECT_CALL(*stateAction, GetDisplayState()) + .Times(::testing::AtLeast(1)) + .WillOnce(::testing::Return(DisplayState::DISPLAY_OFF)); + EXPECT_CALL(*stateAction, + RefreshActivity(0, _, true)).Times(0); + sleep(SCREEN_OFF_WAIT_TIME_S + SLEEP_WAIT_TIME_S/2); + pms->RefreshActivity(0, abnormaltype, true); + + sleep(SCREEN_OFF_WAIT_TIME_S + SLEEP_WAIT_TIME_S/2 + 1); + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest046:End."); + GTEST_LOG_(INFO) << "PowerMgrUnittest046: end."; +} + +/** + * @tc.name: PowerMgrUnittest047 + * @tc.desc: test RefreshActivity by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest047, TestSize.Level2) +{ + UserActivityType abnormaltype = {}; + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest047: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest047:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest047: Failed to get PowerMgrService"; + } + + EXPECT_CALL(*stateAction, GetDisplayState()) + .Times(::testing::AtLeast(1)) + .WillOnce(::testing::Return(DisplayState::DISPLAY_OFF)); + EXPECT_CALL(*stateAction, + RefreshActivity(0, _, true)).Times(0); + sleep(SCREEN_OFF_WAIT_TIME_S + SLEEP_WAIT_TIME_S/2); + pms->RefreshActivity(0, abnormaltype, true); + + sleep(SCREEN_OFF_WAIT_TIME_S + SLEEP_WAIT_TIME_S/2 + 1); + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest047:End."); + GTEST_LOG_(INFO) << "PowerMgrUnittest047: end."; +} + +/** + * @tc.name: PowerMgrUnittest048 + * @tc.desc: test SuspendDevice by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest048, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest048: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest048:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest048: Failed to get PowerMgrService"; + } + + pms->WakeupDevice(0, WakeupDeviceType::WAKEUP_DEVICE_POWER_BUTTON, std::string("test")); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)) + .Times(::testing::AtLeast(1)) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_CALL(*stateAction, + Suspend(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false)); + EXPECT_CALL(*stateAction, GoToSleep(_, _, _)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + pms->SuspendDevice(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false); + EXPECT_EQ(PowerState::INACTIVE, pms->GetState()); + sleep(SLEEP_WAIT_TIME_S+1); + EXPECT_EQ(PowerState::SLEEP, pms->GetState()); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest048:End"); + GTEST_LOG_(INFO) << "PowerMgrUnittest048: end."; +} + +/** + * @tc.name: PowerMgrUnittest049 + * @tc.desc: test SuspendDevice by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest049, TestSize.Level2) +{ + SuspendDeviceType abnormaltype = SuspendDeviceType(10); + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest049: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest049:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest049: Failed to get PowerMgrService"; + } + + pms->WakeupDevice(0, WakeupDeviceType::WAKEUP_DEVICE_POWER_BUTTON, std::string("test")); + EXPECT_CALL(*stateAction, + Suspend(0, _, false)).Times(0); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)).Times(0); + pms->SuspendDevice(0, abnormaltype, false); + EXPECT_EQ(PowerState::AWAKE, pms->GetState()); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest049:End."); + GTEST_LOG_(INFO) << "PowerMgrUnittest049: end."; +} + +/** + * @tc.name: PowerMgrUnittest050 + * @tc.desc: test SuspendDevice by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest050, TestSize.Level2) +{ + SuspendDeviceType abnormaltype = SuspendDeviceType(999); + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest050: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest050:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest050: Failed to get PowerMgrService"; + } + + pms->WakeupDevice(0, WakeupDeviceType::WAKEUP_DEVICE_POWER_BUTTON, std::string("test")); + EXPECT_CALL(*stateAction, + Suspend(0, _, false)).Times(0); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)).Times(0); + pms->SuspendDevice(0, abnormaltype, false); + EXPECT_EQ(PowerState::AWAKE, pms->GetState()); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest050:End."); + GTEST_LOG_(INFO) << "PowerMgrUnittest050: end."; +} + +/** + * @tc.name: PowerMgrUnittest051 + * @tc.desc: test SuspendDevice by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest051, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest051: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest051:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest051: Failed to get PowerMgrService"; + } + + pms->SuspendDevice(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_CALL(*stateAction, + Suspend(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false)); + EXPECT_CALL(*stateAction, GoToSleep(_, _, false)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + pms->SuspendDevice(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false); + EXPECT_EQ(PowerState::INACTIVE, pms->GetState()); + sleep(SLEEP_WAIT_TIME_S+1); + EXPECT_EQ(PowerState::SLEEP, pms->GetState()); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest051:End."); + GTEST_LOG_(INFO) << "PowerMgrUnittest051: end."; +} + +/** + * @tc.name: PowerMgrUnittest052 + * @tc.desc: test SuspendDevice by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest052, TestSize.Level2) +{ + SuspendDeviceType abnormaltype = SuspendDeviceType(10); + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest052: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest052:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest052: Failed to get PowerMgrService"; + } + + pms->SuspendDevice(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false); + EXPECT_CALL(*stateAction, + Suspend(0, _, false)).Times(0); + pms->SuspendDevice(0, abnormaltype, false); + EXPECT_EQ(PowerState::INACTIVE, pms->GetState()); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest052:End."); + GTEST_LOG_(INFO) << "PowerMgrUnittest052: end."; +} + +/** + * @tc.name: PowerMgrUnittest053 + * @tc.desc: test SuspendDevice by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest053, TestSize.Level2) +{ + SuspendDeviceType abnormaltype = {}; + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest053: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest053:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest053: Failed to get PowerMgrService"; + } + + pms->SuspendDevice(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false); + EXPECT_CALL(*stateAction, + Suspend(0, _, false)); + pms->SuspendDevice(0, abnormaltype, false); + EXPECT_EQ(PowerState::INACTIVE, pms->GetState()); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest053:End."); + GTEST_LOG_(INFO) << "PowerMgrUnittest053: end."; +} + +/** + * @tc.name: PowerMgrUnittest054 + * @tc.desc: test ForceSuspendDevice by mock during Inactive + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest054, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest054: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest054:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest054: Failed to get PowerMgrService"; + } + + pms->SuspendDevice(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false); + EXPECT_CALL(*stateAction, GoToSleep(_, _, _)) + .Times(::testing::AtLeast(1)) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + pms->ForceSuspendDevice(0); + sleep(SLEEP_WAIT_TIME_S+1); + EXPECT_EQ(PowerState::SLEEP, pms->GetState()); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest054:End."); + GTEST_LOG_(INFO) << "PowerMgrUnittest054: end."; +} + +/** + * @tc.name: PowerMgrUnittest055 + * @tc.desc: test auto suspend by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest055, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest055: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest055:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest055: Failed to get PowerMgrService"; + } + + pms->WakeupDevice(0, WakeupDeviceType::WAKEUP_DEVICE_LID, std::string("test")); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_ON)); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_DIM)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + sleep((SCREEN_OFF_WAIT_TIME_S*2/3) + 1); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_DIM)); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + sleep((SCREEN_OFF_WAIT_TIME_S*1/3) + 1); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest055:End."); + GTEST_LOG_(INFO) << "PowerMgrUnittest055: end."; +} + +/** + * @tc.name: PowerMgrUnittest056 + * @tc.desc: test SCREEN_ON RunningLock by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest056, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest056: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest056:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest056: Failed to get PowerMgrService"; + } + + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_DIM)).Times(0); + sptr token = new RunningLockTokenStub(); + RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_SCREEN); + pms->CreateRunningLock(token, info); + + pms->Lock(token, info, 0); + EXPECT_EQ(pms->IsUsed(token), true); + + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_DIM)).Times(0); + sleep((SCREEN_OFF_WAIT_TIME_S*2/3) + 1); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_ON)); + EXPECT_EQ(PowerState::AWAKE, pms->GetState()); + + pms->UnLock(token); + EXPECT_EQ(pms->IsUsed(token), false); + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest056:End."); + GTEST_LOG_(INFO) << "PowerMgrUnittest056: end."; +} + +/** + * @tc.name: PowerMgrUnittest057 + * @tc.desc: test SCREEN_ON RunningLock by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest057, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest057: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest057:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest057: Failed to get PowerMgrService"; + } + + pms->SuspendDevice(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false); + sptr token = new RunningLockTokenStub(); + RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_SCREEN); + pms->CreateRunningLock(token, info); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_ON)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + pms->Lock(token, info, 0); + EXPECT_EQ(pms->IsUsed(token), true); + EXPECT_EQ(PowerState::AWAKE, pms->GetState()); + + pms->UnLock(token); + EXPECT_EQ(pms->IsUsed(token), false); + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest057:End."); + GTEST_LOG_(INFO) << "PowerMgrUnittest057: end."; +} + +/** + * @tc.name: PowerMgrUnittest058 + * @tc.desc: test Auto DIM by mock after 10min + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest058, TestSize.Level2) +{ + int64_t time =600000; + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest058: start."; + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest058: Failed to get PowerMgrService"; + } + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_ON)); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_DIM)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + pms->SetDisplayOffTime(time); + sleep(time/1000-2); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_DIM)); + sleep(3); + ResetMockAction(); +pms->SetDisplayOffTime(DEFAULT_DISPLAY_OFF_TIME); + GTEST_LOG_(INFO) << "PowerMgrUnittest058: end."; +} + +/** + * @tc.name: PowerMgrUnittest059 + * @tc.desc: test Screen RunningLock by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest059, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest059: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest059:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest059: Failed to get PowerMgrService"; + } + + sptr token = new RunningLockTokenStub(); + RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_SCREEN); + pms->CreateRunningLock(token, info); + pms->Lock(token, info, 0); + EXPECT_EQ(pms->IsUsed(token), true); + + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_DIM)).Times(0); + sleep(SCREEN_DIM_WAIT_TIME_S + 1); + + pms->UnLock(token); + EXPECT_EQ(pms->IsUsed(token), false); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest059:End."); + GTEST_LOG_(INFO) << "PowerMgrUnittest059: end."; +} + +/** + * @tc.name: PowerMgrUnittest060 + * @tc.desc: test Screen RunningLock by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest060, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest060: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest060:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest060: Failed to get PowerMgrService"; + } + + sptr token = new RunningLockTokenStub(); + RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_SCREEN); + pms->CreateRunningLock(token, info); + pms->Lock(token, info, 0); + EXPECT_EQ(pms->IsUsed(token), true); + + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)).Times(0); + sleep(SLEEP_WAIT_TIME_S*10); + + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)).Times(0); + EXPECT_EQ(PowerState::AWAKE, pms->GetState()); + + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest060:End."); + GTEST_LOG_(INFO) << "PowerMgrUnittest060: end."; +} + +/** + * @tc.name: PowerMgrUnittest061 + * @tc.desc: test Screen on RunningLock by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest061, TestSize.Level2) +{ + int i; + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest061: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest061:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest061: Failed to get PowerMgrService"; + } + + sptr token = new RunningLockTokenStub(); + RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_SCREEN); + pms->CreateRunningLock(token, info); + for (i=0; i<10; i++) { + pms->Lock(token, info, 0); + EXPECT_EQ(pms->IsUsed(token), true); + + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_DIM)).Times(0); + sleep(SCREEN_OFF_WAIT_TIME_S + 1); + + pms->UnLock(token); + EXPECT_EQ(pms->IsUsed(token), false); + EXPECT_EQ(PowerState::AWAKE, pms->GetState()); + } + EXPECT_EQ(PowerState::AWAKE, pms->GetState()); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest061:End."); + GTEST_LOG_(INFO) << "PowerMgrUnittest061: end."; +} + +/** + * @tc.name: PowerMgrUnittest062 + * @tc.desc: test Screen RunningLock by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest062, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest062: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest062:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest062: Failed to get PowerMgrService"; + } + + sptr token = new RunningLockTokenStub(); + RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_SCREEN); + pms->CreateRunningLock(token, info); + + pms->Lock(token, info, 0); + EXPECT_EQ(pms->IsUsed(token), true); + + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)) + .Times(::testing::AtLeast(1)) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + + EXPECT_CALL(*stateAction, GoToSleep(_, _, _)).Times(1); + pms->SuspendDevice(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false); + sleep(SLEEP_WAIT_TIME_S + 1); + + pms->UnLock(token); + EXPECT_EQ(pms->IsUsed(token), false); + sleep(SLEEP_WAIT_TIME_S + 1); + EXPECT_EQ(PowerState::SLEEP, pms->GetState()); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest062:End."); + GTEST_LOG_(INFO) << "PowerMgrUnittest062: end."; +} + +/** + * @tc.name: PowerMgrUnittest063 + * @tc.desc: test Screen RunningLock by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest063, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest063: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest063:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest063: Failed to get PowerMgrService"; + } + + sptr token = new RunningLockTokenStub(); + RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_SCREEN); + pms->CreateRunningLock(token, info); + + pms->Lock(token, info, 0); + EXPECT_EQ(pms->IsUsed(token), true); + + + EXPECT_CALL(*stateAction, GoToSleep(_, _, true)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_EQ(pms->ForceSuspendDevice(0), true); + sleep(SLEEP_WAIT_TIME_S + 1); + + pms->UnLock(token); + EXPECT_EQ(pms->IsUsed(token), false); + EXPECT_EQ(PowerState::SLEEP, pms->GetState()); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest063:End."); + GTEST_LOG_(INFO) << "PowerMgrUnittest063: end."; +} + +/** + * @tc.name: PowerMgrUnittest064 + * @tc.desc: test Screen RunningLock by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest064, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest064: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest063:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest064: Failed to get PowerMgrService"; + } + + sptr token = new RunningLockTokenStub(); + RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_SCREEN); + pms->CreateRunningLock(token, info); + + pms->Lock(token, info, 0); + EXPECT_EQ(pms->IsUsed(token), true); + + EXPECT_CALL(*stateAction, Suspend(_, _, _)).Times(0); + + EXPECT_CALL(*stateAction, + Wakeup(0, WakeupDeviceType::WAKEUP_DEVICE_UNKNOWN, + std::string("test"), _)).Times(1); + pms->WakeupDevice(0, WakeupDeviceType::WAKEUP_DEVICE_UNKNOWN, std::string("test")); + + pms->UnLock(token); + EXPECT_EQ(PowerState::AWAKE, pms->GetState()); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest064:End."); + GTEST_LOG_(INFO) << "PowerMgrUnittest064: end."; +} + +/** + * @tc.name: PowerMgrUnittest065 + * @tc.desc: test background RunningLock by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest065, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest065: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest065:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest065: Failed to get PowerMgrService"; + } + + sptr token = new RunningLockTokenStub(); + RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_BACKGROUND); + pms->CreateRunningLock(token, info); + + EXPECT_CALL(*stateAction, GoToSleep(_, _, true)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_EQ(pms->ForceSuspendDevice(0), true); + EXPECT_EQ(PowerState::SLEEP, pms->GetState()); + pms->WakeupDevice(0, WakeupDeviceType::WAKEUP_DEVICE_UNKNOWN, std::string("test")); + pms->Lock(token, info, 0); + EXPECT_EQ(pms->IsUsed(token), true); + + EXPECT_CALL(*lockAction, Unlock(_, _)).Times(1); + pms->UnLock(token); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest065:End."); + GTEST_LOG_(INFO) << "PowerMgrUnittest065: end."; +} + +/** + * @tc.name: PowerMgrUnittest066 + * @tc.desc: test background RunningLock by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest066, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest066: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest066:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest066: Failed to get PowerMgrService"; + } + + sptr token = new RunningLockTokenStub(); + RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_BACKGROUND); + pms->CreateRunningLock(token, info); + + EXPECT_CALL(*lockAction, Lock(_, _)).Times(1); + pms->Lock(token, info, 0); + EXPECT_EQ(pms->IsUsed(token), true); + + EXPECT_CALL(*lockAction, Unlock(_, _)).Times(1); + pms->UnLock(token); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_ON)); + sleep((SCREEN_OFF_WAIT_TIME_S*2/3) + 1); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_DIM)); + sleep((SCREEN_OFF_WAIT_TIME_S*1/3) + 1); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_OFF)); + EXPECT_EQ(pms->IsUsed(token), false); + EXPECT_CALL(*stateAction, GoToSleep(_, _, _)).Times(1); + sleep(SLEEP_WAIT_TIME_S + 1); + EXPECT_EQ(PowerState::SLEEP, pms->GetState()); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest066:End."); + GTEST_LOG_(INFO) << "PowerMgrUnittest066: end."; +} + +/** + * @tc.name: PowerMgrUnittest067 + * @tc.desc: test background RunningLock by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest067, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest067: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest067:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest067: Failed to get PowerMgrService"; + } + + sptr token = new RunningLockTokenStub(); + RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_BACKGROUND); + pms->CreateRunningLock(token, info); + pms->Lock(token, info, 0); + EXPECT_EQ(pms->IsUsed(token), true); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_ON)); + sleep((SCREEN_OFF_WAIT_TIME_S*2/3) + 1); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_DIM)); + + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + sleep((SCREEN_OFF_WAIT_TIME_S*1/3) + 1); + EXPECT_CALL(*stateAction, GoToSleep(_, _, _)).Times(0); + sleep(SLEEP_WAIT_TIME_S*10); + + EXPECT_CALL(*stateAction, GoToSleep(_, _, _)).Times(0); + EXPECT_EQ(PowerState::INACTIVE, pms->GetState()); + + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest067:End."); + GTEST_LOG_(INFO) << "PowerMgrUnittest067: end."; +} + +/** + * @tc.name: PowerMgrUnittest068 + * @tc.desc: test background RunningLock by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest068, TestSize.Level2) +{ + int i; + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest068: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest068:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest068: Failed to get PowerMgrService"; + } + + sptr token = new RunningLockTokenStub(); + RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_BACKGROUND); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_ON)); + sleep((SCREEN_OFF_WAIT_TIME_S*2/3) + 1); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_DIM)); + pms->CreateRunningLock(token, info); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_CALL(*stateAction, GoToSleep(_, _, _)).Times(0); + pms->Lock(token, info, 0); + EXPECT_EQ(pms->IsUsed(token), true); + sleep((SCREEN_OFF_WAIT_TIME_S*1/3) + 1); + pms->UnLock(token); + EXPECT_EQ(pms->IsUsed(token), false); + EXPECT_EQ(PowerState::INACTIVE, pms->GetState()); + for (i=0; i<10; i++) { + pms->Lock(token, info, 0); + EXPECT_EQ(pms->IsUsed(token), true); + sleep(SCREEN_OFF_WAIT_TIME_S + 1); + + pms->UnLock(token); + EXPECT_EQ(pms->IsUsed(token), false); + EXPECT_EQ(PowerState::INACTIVE, pms->GetState()); + } + EXPECT_EQ(PowerState::INACTIVE, pms->GetState()); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest068:End."); + GTEST_LOG_(INFO) << "PowerMgrUnittest068: end."; +} + +/** + * @tc.name: PowerMgrUnittest069 + * @tc.desc: test background RunningLock by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest069, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest069: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest069:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest069: Failed to get PowerMgrService"; + } + + sptr token = new RunningLockTokenStub(); + RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_BACKGROUND); + pms->SuspendDevice(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false); + sleep(SLEEP_WAIT_TIME_S); + pms->CreateRunningLock(token, info); + EXPECT_CALL(*lockAction, Lock(_, _)).Times(1); + pms->Lock(token, info, 0); + EXPECT_EQ(pms->IsUsed(token), true); + sleep(SLEEP_WAIT_TIME_S + 1); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_ON)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + pms->WakeupDevice(0, WakeupDeviceType::WAKEUP_DEVICE_UNKNOWN, std::string("test")); + sleep(SLEEP_WAIT_TIME_S + 1); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)).Times(0); + EXPECT_CALL(*lockAction, Unlock(_, _)).Times(1); + pms->UnLock(token); + EXPECT_EQ(pms->IsUsed(token), false); + EXPECT_EQ(PowerState::AWAKE, pms->GetState()); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest069:End"); + GTEST_LOG_(INFO) << "PowerMgrUnittest069: end."; +} + +/** + * @tc.name: PowerMgrUnittest070 + * @tc.desc: test background RunningLock by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest070, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest070: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest070:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest070: Failed to get PowerMgrService"; + } + + sptr token = new RunningLockTokenStub(); + RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_BACKGROUND); + pms->CreateRunningLock(token, info); + + EXPECT_CALL(*lockAction, Lock(_, _)).Times(1); + pms->Lock(token, info, 0); + EXPECT_EQ(pms->IsUsed(token), true); + sleep(SLEEP_WAIT_TIME_S + 1); + EXPECT_CALL(*stateAction, GoToSleep(_, _, true)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_EQ(pms->ForceSuspendDevice(0), true); + sleep(SLEEP_WAIT_TIME_S + 1); + EXPECT_CALL(*lockAction, Unlock(_, _)).Times(1); + pms->UnLock(token); + EXPECT_EQ(pms->IsUsed(token), false); + EXPECT_EQ(PowerState::SLEEP, pms->GetState()); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest070:End."); + GTEST_LOG_(INFO) << "PowerMgrUnittest070: end."; +} + +/** + * @tc.name: PowerMgrUnittest071 + * @tc.desc: test proximity screen control RunningLock by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest071, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest071: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest071:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest071: Failed to get PowerMgrService"; + } + + sptr token = new RunningLockTokenStub(); + RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL); + pms->CreateRunningLock(token, info); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)) + .Times(::testing::AtLeast(1)) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + pms->SuspendDevice(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false); + EXPECT_EQ(PowerState::INACTIVE, pms->GetState()); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_ON)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + sleep(SCREEN_OFF_WAIT_TIME_S + 1); + pms->Lock(token, info, 0); + EXPECT_EQ(pms->IsUsed(token), true); + EXPECT_EQ(PowerState::AWAKE, pms->GetState()); + pms->UnLock(token); + EXPECT_EQ(pms->IsUsed(token), false); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest071:End."); + GTEST_LOG_(INFO) << "PowerMgrUnittest071: end."; +} + + +/** + * @tc.name: PowerMgrUnittest072 + * @tc.desc: test proximity screen control RunningLock by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest072, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest072: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest072:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest072: Failed to get PowerMgrService"; + } + + sptr token = new RunningLockTokenStub(); + RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL); + pms->CreateRunningLock(token, info); + EXPECT_CALL(*stateAction, GoToSleep(_, _, true)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_EQ(pms->ForceSuspendDevice(0), true); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_ON)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + sleep(SCREEN_OFF_WAIT_TIME_S + 1); + EXPECT_EQ(PowerState::SLEEP, pms->GetState()); + pms->Lock(token, info, 0); + EXPECT_EQ(pms->IsUsed(token), true); + EXPECT_EQ(PowerState::AWAKE, pms->GetState()); + pms->UnLock(token); + EXPECT_EQ(pms->IsUsed(token), false); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest072:End."); + GTEST_LOG_(INFO) << "PowerMgrUnittest072: end."; +} + +/** + * @tc.name: PowerMgrUnittest073 + * @tc.desc: test proximity screen control RunningLock by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest073, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest073: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest073:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest073: Failed to get PowerMgrService"; + } + + sptr token = new RunningLockTokenStub(); + RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL); + pms->CreateRunningLock(token, info); + pms->Lock(token, info, 0); + EXPECT_EQ(pms->IsUsed(token), true); + sleep(SLEEP_WAIT_TIME_S); + pms->UnLock(token); + EXPECT_EQ(pms->IsUsed(token), false); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_ON)); + sleep((SCREEN_OFF_WAIT_TIME_S*2/3) + 1); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_DIM)); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)) + .Times(::testing::AtLeast(1)) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + sleep((SCREEN_OFF_WAIT_TIME_S*1/3) + 1); + EXPECT_EQ(PowerState::INACTIVE, pms->GetState()); + EXPECT_CALL(*stateAction, GoToSleep(_, _, false)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_OFF)); + sleep(SLEEP_WAIT_TIME_S + 1); + EXPECT_EQ(PowerState::SLEEP, pms->GetState()); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest073:End."); + GTEST_LOG_(INFO) << "PowerMgrUnittest073: end."; +} + +/** + * @tc.name: PowerMgrUnittest074 + * @tc.desc: test proximity RunningLock by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest074, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest074: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest074:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest074: Failed to get PowerMgrService"; + } + + sptr token = new RunningLockTokenStub(); + RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL); + pms->CreateRunningLock(token, info); + EXPECT_CALL(*lockAction, Lock(_, _)).Times(1); + pms->Lock(token, info, 0); + EXPECT_EQ(pms->IsUsed(token), true); + sleep(SCREEN_OFF_WAIT_TIME_S+1); + EXPECT_EQ(PowerState::AWAKE, pms->GetState()); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_CALL(*stateAction, GoToSleep(_, _, _)).Times(0); + pms->MockProximity(RunningLockMgr::PROXIMITY_CLOSE); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest074:End."); + GTEST_LOG_(INFO) << "PowerMgrUnittest074: end."; +} + +/** + * @tc.name: PowerMgrUnittest075 + * @tc.desc: test proximity RunningLock by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest075, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest075: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest075:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest075: Failed to get PowerMgrService"; + } + + sptr token = new RunningLockTokenStub(); + RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL); + pms->CreateRunningLock(token, info); + pms->Lock(token, info, 0); + EXPECT_EQ(pms->IsUsed(token), true); + sleep(SLEEP_WAIT_TIME_S*10); + EXPECT_EQ(PowerState::AWAKE, pms->GetState()); + + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_CALL(*stateAction, GoToSleep(_, _, _)).Times(0); + pms->MockProximity(RunningLockMgr::PROXIMITY_CLOSE); + EXPECT_EQ(PowerState::INACTIVE, pms->GetState()); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest075:End."); + GTEST_LOG_(INFO) << "PowerMgrUnittest075: end."; +} + +/** + * @tc.name: PowerMgrUnittest076 + * @tc.desc: test proximity RunningLock by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest076, TestSize.Level2) +{ + int i; + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest076: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest076:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest076: Failed to get PowerMgrService"; + } + + sptr token = new RunningLockTokenStub(); + RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL); + pms->CreateRunningLock(token, info); + for (i=0; i<10; i++) { + pms->Lock(token, info, 0); + EXPECT_EQ(pms->IsUsed(token), true); + sleep(SCREEN_OFF_WAIT_TIME_S + 1); + pms->UnLock(token); + EXPECT_EQ(pms->IsUsed(token), false); + EXPECT_EQ(PowerState::AWAKE, pms->GetState()); + } + pms->Lock(token, info, 0); + EXPECT_EQ(pms->IsUsed(token), true); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_CALL(*stateAction, GoToSleep(_, _, _)).Times(0); + pms->MockProximity(RunningLockMgr::PROXIMITY_CLOSE); + EXPECT_EQ(PowerState::INACTIVE, pms->GetState()); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest076:End."); + GTEST_LOG_(INFO) << "PowerMgrUnittest076: end."; +} + +/** + * @tc.name: PowerMgrUnittest077 + * @tc.desc: test proximity RunningLock by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest077, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest077: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest077:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest077: Failed to get PowerMgrService"; + } + + sptr token = new RunningLockTokenStub(); + RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL); + pms->CreateRunningLock(token, info); + EXPECT_CALL(*lockAction, Lock(_, _)).Times(1); + pms->Lock(token, info, 0); + EXPECT_EQ(pms->IsUsed(token), true); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)) + .Times(::testing::AtLeast(1)) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_CALL(*stateAction, GoToSleep(_, _, false)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + pms->SuspendDevice(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false); + sleep(SLEEP_WAIT_TIME_S + 1); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_ON)); + sleep((SCREEN_OFF_WAIT_TIME_S*2/3) + 1); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_DIM)); + sleep((SCREEN_OFF_WAIT_TIME_S*1/3) + 1); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_OFF)); + EXPECT_EQ(PowerState::SLEEP, pms->GetState()); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest077:End."); + GTEST_LOG_(INFO) << "PowerMgrUnittest077: end."; +} + +/** + * @tc.name: PowerMgrUnittest078 + * @tc.desc: test proximity RunningLock by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest078, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest078: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest078:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest078: Failed to get PowerMgrService"; + } + + sptr token = new RunningLockTokenStub(); + RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL); + pms->CreateRunningLock(token, info); + EXPECT_CALL(*lockAction, Lock(_, _)).Times(1); + pms->Lock(token, info, 0); + EXPECT_EQ(pms->IsUsed(token), true); + EXPECT_CALL(*stateAction, GoToSleep(_, _, true)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + pms->ForceSuspendDevice(0); + EXPECT_EQ(PowerState::SLEEP, pms->GetState()); + EXPECT_CALL(*lockAction, Unlock(_, _)).Times(1); + pms->UnLock(token); + EXPECT_EQ(pms->IsUsed(token), false); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest078:End."); + GTEST_LOG_(INFO) << "PowerMgrUnittest078: end."; +} + +/** + * @tc.name: PowerMgrUnittest079 + * @tc.desc: test proximity RunningLock by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest079, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest079: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest079:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest079: Failed to get PowerMgrService"; + } + + pms->SuspendDevice(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false); + sptr token = new RunningLockTokenStub(); + RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL); + pms->CreateRunningLock(token, info); + EXPECT_CALL(*lockAction, Lock(_, _)).Times(1); + pms->Lock(token, info, 0); + EXPECT_EQ(pms->IsUsed(token), true); + pms->WakeupDevice(0, WakeupDeviceType::WAKEUP_DEVICE_UNKNOWN, std::string("test")); + EXPECT_EQ(PowerState::AWAKE, pms->GetState()); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest079:End."); + GTEST_LOG_(INFO) << "PowerMgrUnittest079: end."; +} + +/** + * @tc.name: PowerMgrUnittest080 + * @tc.desc: test Auto SuspendDevice by mock after 15s + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest080, TestSize.Level2) +{ + int64_t time =15000; + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest080: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest080:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest080: Failed to get PowerMgrService"; + } + + pms->SetDisplayOffTime(time); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_ON)); + sleep(((time/1000)*2/3)+1); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_DIM)); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)) + .Times(::testing::AtLeast(1)) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + sleep(time/1000*1/3+1); + EXPECT_CALL(*stateAction, GoToSleep(_, _, false)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_OFF)); + sleep(SLEEP_WAIT_TIME_S+1); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest080:End."); + pms->SetDisplayOffTime(DEFAULT_DISPLAY_OFF_TIME); + GTEST_LOG_(INFO) << "PowerMgrUnittest080: end."; +} + +/** + * @tc.name: PowerMgrUnittest081 + * @tc.desc: test Auto SuspendDevice by mock after 30s + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest081, TestSize.Level2) +{ + int64_t time =30000; + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest081: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest081:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest081: Failed to get PowerMgrService"; + } + + pms->SetDisplayOffTime(time); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_ON)); + sleep(((time/1000)*2/3)+1); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_DIM)); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)) + .Times(::testing::AtLeast(1)) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + sleep(((time/1000)*1/3)+1); + EXPECT_CALL(*stateAction, GoToSleep(_, _, false)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_OFF)); + sleep(SLEEP_WAIT_TIME_S+1); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest081:End"); + pms->SetDisplayOffTime(DEFAULT_DISPLAY_OFF_TIME); + GTEST_LOG_(INFO) << "PowerMgrUnittest081: end."; +} + +/** + * @tc.name: PowerMgrUnittest082 + * @tc.desc: test Auto SuspendDevice by mock after 1min + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest082, TestSize.Level2) +{ + int64_t time =60000; + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest082: start."; + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest082: Failed to get PowerMgrService"; + } + + pms->SetDisplayOffTime(time); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_ON)); + sleep(((time/1000)*2/3)+1); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_DIM)); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)) + .Times(::testing::AtLeast(1)) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + sleep(time/1000*1/3+1); + EXPECT_CALL(*stateAction, GoToSleep(_, _, false)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_OFF)); + sleep(SLEEP_WAIT_TIME_S+1); + + ResetMockAction(); + pms->SetDisplayOffTime(DEFAULT_DISPLAY_OFF_TIME); + GTEST_LOG_(INFO) << "PowerMgrUnittest082: end."; +} + +/** + * @tc.name: PowerMgrUnittest083 + * @tc.desc: test Auto SuspendDevice by mock after 2mins + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest083, TestSize.Level2) +{ + int64_t time =120000; + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest083: start."; + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest083: Failed to get PowerMgrService"; + } + + pms->SetDisplayOffTime(time); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_ON)); + sleep(((time/1000)*2/3)+1); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_DIM)); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)) + .Times(::testing::AtLeast(1)) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + sleep(time/1000*1/3+1); + EXPECT_CALL(*stateAction, GoToSleep(_, _, false)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_OFF)); + sleep(SLEEP_WAIT_TIME_S+1); + + ResetMockAction(); + pms->SetDisplayOffTime(DEFAULT_DISPLAY_OFF_TIME); + GTEST_LOG_(INFO) << "PowerMgrUnittest083: end."; +} + +/** + * @tc.name: PowerMgrUnittest084 + * @tc.desc: test Auto SuspendDevice by mock after 5mins + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest084, TestSize.Level2) +{ + int64_t time =300000; + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest084: start."; + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest084: Failed to get PowerMgrService"; + } + + pms->SetDisplayOffTime(time); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_ON)); + sleep(((time/1000)*2/3)+1); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_DIM)); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)) + .Times(::testing::AtLeast(1)) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + sleep(time/1000*1/3+1); + EXPECT_CALL(*stateAction, GoToSleep(_, _, false)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_OFF)); + sleep(SLEEP_WAIT_TIME_S+1); + + ResetMockAction(); + pms->SetDisplayOffTime(DEFAULT_DISPLAY_OFF_TIME); + GTEST_LOG_(INFO) << "PowerMgrUnittest084: end."; +} + +/** + * @tc.name: PowerMgrUnittest085 + * @tc.desc: test Auto SuspendDevice by mock after 10mins + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest085, TestSize.Level2) +{ + int64_t time =600000; + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest085: start."; + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest085: Failed to get PowerMgrService"; + } + + pms->SetDisplayOffTime(time); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_ON)); + sleep(((time/1000)*2/3)+1); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_DIM)); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)) + .Times(::testing::AtLeast(1)) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + sleep(time/1000*1/3+1); + EXPECT_CALL(*stateAction, GoToSleep(_, _, false)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_OFF)); + sleep(SLEEP_WAIT_TIME_S+1); + + ResetMockAction(); +pms->SetDisplayOffTime(DEFAULT_DISPLAY_OFF_TIME); + GTEST_LOG_(INFO) << "PowerMgrUnittest085: end."; +} + +/** + * @tc.name: PowerMgrUnittest086 + * @tc.desc: test Auto DIM by mock after 15s + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest086, TestSize.Level2) +{ + int64_t time =15000; + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest086: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest086:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest086: Failed to get PowerMgrService"; + } + pms->SetDisplayOffTime(time); + pms->WakeupDevice(0, WakeupDeviceType::WAKEUP_DEVICE_UNKNOWN, std::string("test")); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest086:DeviceStateAction::SetDisplayState."); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_ON)); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_DIM)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest086:Start sleep."); + sleep(time/1000-2); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_DIM)); + sleep(3); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest086: sleep end."); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest086:End."); + pms->SetDisplayOffTime(DEFAULT_DISPLAY_OFF_TIME); + GTEST_LOG_(INFO) << "PowerMgrUnittest086: end."; +} + +/** + * @tc.name: PowerMgrUnittest087 + * @tc.desc: test Auto DIM by mock after 30s + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest087, TestSize.Level2) +{ + int64_t time =30000; + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest087: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest0087:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest087: Failed to get PowerMgrService"; + } + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_ON)); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_DIM)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + pms->SetDisplayOffTime(time); + sleep(time/1000-2); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_DIM)); + sleep(3); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest087:End."); + pms->SetDisplayOffTime(DEFAULT_DISPLAY_OFF_TIME); + GTEST_LOG_(INFO) << "PowerMgrUnittest087: end."; +} + +/** + * @tc.name: PowerMgrUnittest088 + * @tc.desc: test Auto DIM by mock after 60s + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest088, TestSize.Level2) +{ + int64_t time =60000; + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest088: start."; + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest088: Failed to get PowerMgrService"; + } + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_ON)); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_DIM)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + pms->SetDisplayOffTime(time); + sleep(time/1000-2); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_DIM)); + sleep(3); + + ResetMockAction(); + pms->SetDisplayOffTime(DEFAULT_DISPLAY_OFF_TIME); + GTEST_LOG_(INFO) << "PowerMgrUnittest088: end."; +} + +/** + * @tc.name: PowerMgrUnittest089 + * @tc.desc: test Auto DIM by mock after 2min + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest089, TestSize.Level2) +{ + int64_t time =120000; + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest089: start."; + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest089: Failed to get PowerMgrService"; + } + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_ON)); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_DIM)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + pms->SetDisplayOffTime(time); + sleep(time/1000-2); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_DIM)); + sleep(3); + + ResetMockAction(); + pms->SetDisplayOffTime(DEFAULT_DISPLAY_OFF_TIME); + GTEST_LOG_(INFO) << "PowerMgrUnittest089: end."; +} + +/** + * @tc.name: PowerMgrUnittest090 + * @tc.desc: test Auto DIM by mock after 5min + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest090, TestSize.Level2) +{ + int64_t time =300000; + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest090: start."; + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest090: Failed to get PowerMgrService"; + } + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_ON)); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_DIM)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + pms->SetDisplayOffTime(time); + sleep(time/1000-2); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_DIM)); + sleep(3); + + ResetMockAction(); + pms->SetDisplayOffTime(DEFAULT_DISPLAY_OFF_TIME); + GTEST_LOG_(INFO) << "PowerMgrUnittest090: end."; +} + +/** + * @tc.name: PowerMgrUnittest091 + * @tc.desc: test proximity screen control RunningLock by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockTest, PowerMgrUnittest091, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrUnittest091: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest091:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrUnittest091: Failed to get PowerMgrService"; + } + + sptr token = new RunningLockTokenStub(); + RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL); + pms->CreateRunningLock(token, info); + pms->Lock(token, info, 0); + EXPECT_EQ(pms->IsUsed(token), true); + sleep(SLEEP_WAIT_TIME_S); + pms->UnLock(token); + EXPECT_EQ(pms->IsUsed(token), false); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_ON)); + sleep((SCREEN_OFF_WAIT_TIME_S*2/3)+1); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_DIM)); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)) + .Times(::testing::AtLeast(1)) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + sleep((SCREEN_OFF_WAIT_TIME_S*1/3) + 1); + EXPECT_EQ(PowerState::INACTIVE, pms->GetState()); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnittest091:End."); + GTEST_LOG_(INFO) << "PowerMgrUnittest091: end."; +} +} \ No newline at end of file diff --git a/services/native/test/unittest/src/power_register_callback_mode_test.cpp b/services/native/test/unittest/src/power_register_callback_mode_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a1e20811dacea2b04ad6b7eab4f0ddb04ecfbfb7 --- /dev/null +++ b/services/native/test/unittest/src/power_register_callback_mode_test.cpp @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2021 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 "power_register_callback_mode_test.h" + +#include + +#include +#include +#include +#include +#include + +#include "power_common.h" +#include "power_mgr_client.h" +#include "power_mgr_service.h" +#include "power_state_machine.h" +#include "sys_param.h" + +using namespace testing::ext; +using namespace OHOS::PowerMgr; +using namespace OHOS; +using namespace std; + + +void PowerRegisterCallbackModeTest::PowerModeTest1Callback::PowerModeCallback() +{ + POWER_HILOGD(MODULE_SERVICE, "PowerModeTest1Callback::PowerModeCallback."); +} + +/** + * @tc.name: PowerStateCallback001 + * @tc.desc: test PowerModeCallback + * @tc.type: FUNC + */ +HWTEST_F (PowerRegisterCallbackModeTest, PowerRegisterCallbackModeCallback001, TestSize.Level0) +{ + auto& powerMgrClient = PowerMgrClient::GetInstance(); + sptr cb1 = new PowerModeTest1Callback(); + powerMgrClient.RegisterPowerModeCallback(cb1); + sleep(SLEEP_WAIT_TIME_S); + uint32_t mode = 601; + powerMgrClient.SetDeviceMode(mode); + POWER_HILOGD(MODULE_SERVICE, "PowerRegisterCallbackModeCallback001 1."); +} \ No newline at end of file diff --git a/services/native/test/unittest/src/power_set_mode_test.cpp b/services/native/test/unittest/src/power_set_mode_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4dc071dbc4a18013787108e37511a08bbc229f4a --- /dev/null +++ b/services/native/test/unittest/src/power_set_mode_test.cpp @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2021 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 "power_set_mode_test.h" + +#include + +#include +#include +#include +#include +#include + +#include "power_common.h" +#include "power_mgr_client.h" +#include "power_mgr_service.h" +#include "power_state_machine.h" +#include "sys_param.h" + +using namespace testing::ext; +using namespace OHOS::PowerMgr; +using namespace OHOS; +using namespace std; + +/** + * @tc.name: SetDeviceModeTest001 + * @tc.desc: test SetDeviceMode in proxy + * @tc.type: FUNC + */ +HWTEST_F (PowerSetModeTest, SetModeTest001, TestSize.Level0) +{ + sleep(SLEEP_WAIT_TIME_S); + GTEST_LOG_(INFO) << "SetModeTest001: SetMode start."; + auto& powerMgrClient = PowerMgrClient::GetInstance(); + + uint32_t mode1 = 601; + if (true) { + powerMgrClient.SetDeviceMode(mode1); + } + sleep(SLEEP_WAIT_TIME_S); + uint32_t mode2 = 602; + if (true) { + powerMgrClient.SetDeviceMode(mode2); + } + sleep(SLEEP_WAIT_TIME_S); + uint32_t mode3 = 603; + if (true) { + powerMgrClient.SetDeviceMode(mode3); + } + + GTEST_LOG_(INFO) << "SetModeTest001: SetMode end."; +} \ No newline at end of file diff --git a/services/native/test/unittest/src/power_shutdown_callback_test.cpp b/services/native/test/unittest/src/power_shutdown_callback_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e23409b0ec658992af293d97ea0cd09e2e5a131b --- /dev/null +++ b/services/native/test/unittest/src/power_shutdown_callback_test.cpp @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2021 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 "power_shutdown_callback_test.h" + +#include + +#include +#include +#include +#include +#include + +#include "power_common.h" +#include "power_mgr_client.h" +#include "power_mgr_service.h" +#include "power_state_machine.h" +#include "sys_param.h" + +using namespace testing::ext; +using namespace OHOS::PowerMgr; +using namespace OHOS; +using namespace std; + + +void PowerShutdownCallbackTest::PowerShutdownTest1Callback::ShutdownCallback() +{ + POWER_HILOGD(MODULE_SERVICE, "PowerShutdownTest1Callback::ShutdownCallback."); +} + +void PowerShutdownCallbackTest::PowerShutdownTest2Callback::ShutdownCallback() +{ + POWER_HILOGD(MODULE_SERVICE, "PowerShutdownTest2Callback::ShutdownCallback."); +} + +/** + * @tc.name: PowerShutdownCallback001 + * @tc.desc: test ShutdownCallback + * @tc.type: FUNC + */ +HWTEST_F (PowerShutdownCallbackTest, PowerShutdownCallback001, TestSize.Level0) +{ + auto& powerMgrClient = PowerMgrClient::GetInstance(); + sptr cb1 = new PowerShutdownTest1Callback(); + powerMgrClient.RegisterShutdownCallback(cb1); + sleep(SLEEP_WAIT_TIME_S); + powerMgrClient.ShutDownDevice(string("ShutDownDeviceTest001")); + POWER_HILOGD(MODULE_SERVICE, "PowerShutdownCallback001 1."); +} \ No newline at end of file diff --git a/services/native/test/unittest/src/power_shutdown_test.cpp b/services/native/test/unittest/src/power_shutdown_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..de7eb57b32fb1c7af0e83c6b05ffce2745ee5ed5 --- /dev/null +++ b/services/native/test/unittest/src/power_shutdown_test.cpp @@ -0,0 +1,88 @@ +/* + * Copyright (c) 2021 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 "power_shutdown_test.h" + +#include + +#include +#include +#include +#include +#include + +#include "power_common.h" +#include "power_mgr_client.h" +#include "power_mgr_service.h" +#include "power_state_machine.h" +#include "sys_param.h" + +using namespace testing::ext; +using namespace OHOS::PowerMgr; +using namespace OHOS; +using namespace std; + + +void PowerShutdownTest::PowerShutdownTest1Callback::ShutdownCallback() +{ + POWER_HILOGD(MODULE_SERVICE, "PowerShutdownTest1Callback::ShutdownCallback."); +} + +void PowerShutdownTest::PowerShutdownTest2Callback::ShutdownCallback() +{ + POWER_HILOGD(MODULE_SERVICE, "PowerShutdownTest2Callback::ShutdownCallback."); +} + +/** + * @tc.name: PowerShutdownCallback001 + * @tc.desc: test ShutdownCallback + * @tc.type: FUNC + */ +HWTEST_F (PowerShutdownTest, PowerShutdownCallback001, TestSize.Level0) +{ + auto& powerMgrClient = PowerMgrClient::GetInstance(); + sptr cb1 = new PowerShutdownTest1Callback(); + powerMgrClient.RegisterShutdownCallback(cb1); + POWER_HILOGD(MODULE_SERVICE, "PowerShutdownCallback001 1."); + { + sptr cb2 = new PowerShutdownTest2Callback(); + powerMgrClient.UnRegisterShutdownCallback(cb2); + POWER_HILOGD(MODULE_SERVICE, "PowerShutdownCallback001 2."); + powerMgrClient.RegisterShutdownCallback(cb2); + POWER_HILOGD(MODULE_SERVICE, "PowerShutdownCallback001 3."); + powerMgrClient.RegisterShutdownCallback(cb2); + POWER_HILOGD(MODULE_SERVICE, "PowerShutdownCallback001 4."); + } + powerMgrClient.UnRegisterShutdownCallback(cb1); + POWER_HILOGD(MODULE_SERVICE, "PowerShutdownTest::PowerShutdownCallback001 end."); +} + +/** + * @tc.name: ShutDownDeviceTest001 + * @tc.desc: test ShutDownDevice in proxy + * @tc.type: FUNC + */ +HWTEST_F (PowerShutdownTest, ShutDownDeviceTest001, TestSize.Level2) +{ + sleep(SLEEP_WAIT_TIME_S); + GTEST_LOG_(INFO) << "ShutDownDeviceTest001: ShutDownDevice start."; + auto& powerMgrClient = PowerMgrClient::GetInstance(); + + if (false) { + powerMgrClient.ShutDownDevice(string("ShutDownDeviceTest001")); + } + + GTEST_LOG_(INFO) << "ShutDownDeviceTest001: ShutDownDevice end."; +} diff --git a/services/native/test/unittest/src/power_state_machine_test.cpp b/services/native/test/unittest/src/power_state_machine_test.cpp index 927366286aa437bc55183a4e6db7ff9f1d8fd874..33c235086961038049f46d80a88f84abab4b9d24 100644 --- a/services/native/test/unittest/src/power_state_machine_test.cpp +++ b/services/native/test/unittest/src/power_state_machine_test.cpp @@ -63,6 +63,7 @@ bool PowerStateMachineTest::IsTestSupported() */ HWTEST_F (PowerStateMachineTest, PowerStateMachine003, TestSize.Level0) { + POWER_HILOGI(MODULE_SERVICE, "PowerStateMachine003::fun is start!"); if (!PowerStateMachineTest::IsTestSupported()) { POWER_HILOGI(MODULE_SERVICE, "PowerStateMachine003::test is not supported, do nothing!"); return; @@ -84,6 +85,7 @@ HWTEST_F (PowerStateMachineTest, PowerStateMachine003, TestSize.Level0) sleep(REFRESHACTIVITY_WAIT_TIME_S); EXPECT_EQ(powerMgrClient.IsScreenOn(), false) << "PowerStateMachine003: Suspend Device Fail, Screen is On"; + POWER_HILOGI(MODULE_SERVICE, "PowerStateMachine003::fun is end!"); GTEST_LOG_(INFO) << "PowerStateMachine003: Suspend Device end."; } @@ -94,6 +96,7 @@ HWTEST_F (PowerStateMachineTest, PowerStateMachine003, TestSize.Level0) */ HWTEST_F (PowerStateMachineTest, PowerStateMachine004, TestSize.Level0) { + POWER_HILOGI(MODULE_SERVICE, "PowerStateMachine004::fun is start!"); if (!PowerStateMachineTest::IsTestSupported()) { POWER_HILOGI(MODULE_SERVICE, "PowerStateMachine004::test is not supported, do nothing!"); return; @@ -115,6 +118,7 @@ HWTEST_F (PowerStateMachineTest, PowerStateMachine004, TestSize.Level0) sleep(SLEEP_WAIT_TIME_S); EXPECT_EQ(powerMgrClient.IsScreenOn(), true) << "PowerStateMachine004: Wakeup Device Fail, Screen is Off"; + POWER_HILOGI(MODULE_SERVICE, "PowerStateMachine004::fun is end!"); GTEST_LOG_(INFO) << "PowerStateMachine004: Wakeup Device end."; } @@ -176,9 +180,7 @@ HWTEST_F (PowerStateMachineTest, PowerStateMachine006, TestSize.Level0) // Check illegal para details GTEST_LOG_(INFO) << "PowerStateMachine006: Check illegal para details Begin!"; - powerMgrClient.WakeupDevice(WakeupDeviceType::WAKEUP_DEVICE_APPLICATION, - string("Software Wakeup test!Software Wakeup test! Software Wakeup test! Software Wakeup test! " \ - "Software Wakeup test!Software Wakeup test! Software Wakeup test!")); + powerMgrClient.WakeupDevice(WakeupDeviceType::WAKEUP_DEVICE_APPLICATION); sleep(SLEEP_WAIT_TIME_S); EXPECT_EQ(powerMgrClient.IsScreenOn(), true) << "PowerStateMachine006: Check details test Fail, Screen is Off."; @@ -189,14 +191,14 @@ HWTEST_F (PowerStateMachineTest, PowerStateMachine006, TestSize.Level0) EXPECT_EQ(powerMgrClient.IsScreenOn(), false) << "PowerStateMachine006: Real test tprepare Fail, Screen is On."; GTEST_LOG_(INFO) << "PowerStateMachine006: Screen is Off, Begin to Real Wakeup Device!"; - powerMgrClient.WakeupDevice(WakeupDeviceType::WAKEUP_DEVICE_APPLICATION, string("Software Wakeup")); + powerMgrClient.WakeupDevice(WakeupDeviceType::WAKEUP_DEVICE_APPLICATION); sleep(SLEEP_WAIT_TIME_S); EXPECT_EQ(powerMgrClient.IsScreenOn(), true) << "PowerStateMachine006: Real Wakeup Device Fail, Screen is Off"; GTEST_LOG_(INFO) << "PowerStateMachine006: Wakeup Device end."; } -#endif + /** * @tc.name: PowerStateMachine007 @@ -252,6 +254,24 @@ HWTEST_F (PowerStateMachineTest, PowerStateCallback001, TestSize.Level0) POWER_HILOGD(MODULE_SERVICE, "PowerStateTestCallback::PowerStateCallback001 end."); } +/** + * @tc.name: ShutDownDeviceTest001 + * @tc.desc: test ShutDownDevice in proxy + * @tc.type: FUNC + */ +HWTEST_F (PowerStateMachineTest, ShutDownDeviceTest001, TestSize.Level2) +{ + sleep(SLEEP_WAIT_TIME_S); + GTEST_LOG_(INFO) << "ShutDownDeviceTest001: ShutDownDevice start."; + auto& powerMgrClient = PowerMgrClient::GetInstance(); + + if (false) { + powerMgrClient.ShutDownDevice(string("ShutDownDeviceTest001")); + } + + GTEST_LOG_(INFO) << "ShutDownDeviceTest001: ShutDownDevice end."; +} + /** * @tc.name: RebootDeviceTest001 * @tc.desc: test RebootDevice in proxy @@ -269,21 +289,166 @@ HWTEST_F (PowerStateMachineTest, RebootDeviceTest001, TestSize.Level2) GTEST_LOG_(INFO) << "RebootDeviceTest001: RebootDevice end."; } +#endif + +void PowerStateMachineTest::WakeUpthread() +{ + auto& powerMgrClient = PowerMgrClient::GetInstance(); + powerMgrClient.WakeupDevice(); +} +void PowerStateMachineTest::Suspendthread() +{ + auto& powerMgrClient = PowerMgrClient::GetInstance(); + powerMgrClient.SuspendDevice(); +} + +void PowerStateMachineTest::Rebootthread() +{ + auto& powerMgrClient = PowerMgrClient::GetInstance(); + powerMgrClient.RebootDevice(string("RebootDeviceTestThread")); +} + +void PowerStateMachineTest::Shutdownthread() +{ + auto& powerMgrClient = PowerMgrClient::GetInstance(); + powerMgrClient.ShutDownDevice(string("ShutDownDeviceTestThread")); +} + +#ifdef SHIELD /** - * @tc.name: ShutDownDeviceTest001 - * @tc.desc: test ShutDownDevice in proxy + * @tc.name: PowerStateMachine010 + * @tc.desc: test suspend during wakeup * @tc.type: FUNC */ -HWTEST_F (PowerStateMachineTest, ShutDownDeviceTest001, TestSize.Level2) +HWTEST_F (PowerStateMachineTest, PowerStateMachine010, TestSize.Level0) { + POWER_HILOGI(MODULE_SERVICE, "PowerStateMachine010::fun is start!"); + if (!PowerStateMachineTest::IsTestSupported()) { + POWER_HILOGI(MODULE_SERVICE, "PowerStateMachine010::test is not supported, do nothing!"); + return; + } + sleep(SLEEP_WAIT_TIME_S); - GTEST_LOG_(INFO) << "ShutDownDeviceTest001: ShutDownDevice start."; + GTEST_LOG_(INFO) << "PowerStateMachine010: Suspend Device start."; auto& powerMgrClient = PowerMgrClient::GetInstance(); - if (false) { - powerMgrClient.ShutDownDevice(string("ShutDownDeviceTest001")); + // Wakeup Device before test + GTEST_LOG_(INFO) << "PowerStateMachine010: Wakeup Device before test."; + powerMgrClient.WakeupDevice(); + sleep(SLEEP_WAIT_TIME_S); + EXPECT_EQ(powerMgrClient.IsScreenOn(), true) << "PowerStateMachine010: Prepare Fail, Screen is OFF."; + GTEST_LOG_(INFO) << "PowerStateMachine010: Screen is On, Begin to Suspend Device!"; + + powerMgrClient.SuspendDevice(); + GTEST_LOG_(INFO) << "PowerStateMachine010: Start to check suspend during wakeup."; + std::make_unique(&PowerStateMachineTest::WakeUpthread, this)->detach(); + std::make_unique(&PowerStateMachineTest::Suspendthread, this)->join(); + sleep(NEXT_WAIT_TIME_S); + EXPECT_EQ(powerMgrClient.IsScreenOn(), true) << "PowerStateMachine010: Wakeup Device Lock Fail, Screen is Off"; + + POWER_HILOGI(MODULE_SERVICE, "PowerStateMachine010::fun is end!"); + GTEST_LOG_(INFO) << "PowerStateMachine010: Suspend Device end."; +} + +/** + * @tc.name: PowerStateMachine011 + * @tc.desc: test wakeup during suspend + * @tc.type: FUNC + */ +HWTEST_F (PowerStateMachineTest, PowerStateMachine011, TestSize.Level0) +{ + POWER_HILOGI(MODULE_SERVICE, "PowerStateMachine011::fun is start!"); + if (!PowerStateMachineTest::IsTestSupported()) { + POWER_HILOGI(MODULE_SERVICE, "PowerStateMachine011::test is not supported, do nothing!"); + return; } - GTEST_LOG_(INFO) << "ShutDownDeviceTest001: ShutDownDevice end."; + sleep(SLEEP_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerStateMachine011: Suspend Device start."; + auto& powerMgrClient = PowerMgrClient::GetInstance(); + + // Wakeup Device before test + GTEST_LOG_(INFO) << "PowerStateMachine011: Wakeup Device before test."; + powerMgrClient.WakeupDevice(); + sleep(SLEEP_WAIT_TIME_S); + EXPECT_EQ(powerMgrClient.IsScreenOn(), true) << "PowerStateMachine011: Prepare Fail, Screen is OFF."; + GTEST_LOG_(INFO) << "PowerStateMachine011: Screen is On, Begin to Suspend Device!"; + + GTEST_LOG_(INFO) << "PowerStateMachine011: Start to check wakeup during suspend."; + std::make_unique(&PowerStateMachineTest::Suspendthread, this)->detach(); + std::make_unique(&PowerStateMachineTest::WakeUpthread, this)->join(); + sleep(NEXT_WAIT_TIME_S); + EXPECT_EQ(powerMgrClient.IsScreenOn(), false) << "PowerStateMachine011: SuspendDevice Lock Fail, Screen is Off"; + + POWER_HILOGI(MODULE_SERVICE, "PowerStateMachine011::fun is end!"); + GTEST_LOG_(INFO) << "PowerStateMachine011: Suspend Device end."; +} +#endif + +/** + * @tc.name: RebootDeviceTest001 + * @tc.desc: test wakeup during shutdown + * @tc.type: FUNC + */ +HWTEST_F (PowerStateMachineTest, PowerStateMachine012, TestSize.Level2) +{ + sleep(SLEEP_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerStateMachine012: test wakeup during shutdown start."; + std::make_unique(&PowerStateMachineTest::Shutdownthread, this)->join(); + std::make_unique(&PowerStateMachineTest::WakeUpthread, this)->join(); + GTEST_LOG_(INFO) << "PowerStateMachine012: test wakeup during shutdown end."; +} + +void PowerStateMachineTest::PowerClientInit() +{ + POWER_HILOGD(MODULE_SERVICE, "PowerStateMachineTest::PowerClientInit."); + client_ = std::make_unique(); +} + +void PowerStateMachineTest::CheckWriteWakeCount() +{ + POWER_HILOGD(MODULE_SERVICE, "PowerStateMachineTest::CheckWriteWakeCount."); + client_->WriteWakeCount(std::string("test")); +} + +void PowerStateMachineTest::CheckReadWakeCount() +{ + POWER_HILOGD(MODULE_SERVICE, "PowerStateMachineTest::CheckReadWakeCount."); + std::string count; + client_->ReadWakeCount(count); } + +/** + * @tc.name: PowerStateMachine008 + * @tc.desc: test WriteWakeCount + * @tc.type: FUNC + */ +HWTEST_F (PowerStateMachineTest, PowerStateMachine008, TestSize.Level0) +{ + if (!PowerStateMachineTest::IsTestSupported()) { + POWER_HILOGI(MODULE_SERVICE, "PowerStateMachine008::test is not supported, do nothing!"); + return; + } + sleep(SLEEP_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerStateMachine008: Suspend Device start."; + std::make_unique(&PowerStateMachineTest::CheckWriteWakeCount, this)->detach(); + GTEST_LOG_(INFO) << "PowerStateMachine008: Suspend Device end."; +} + +/** + * @tc.name: PowerStateMachine009 + * @tc.desc: test ReadWakeCount + * @tc.type: FUNC + */ +HWTEST_F (PowerStateMachineTest, PowerStateMachine009, TestSize.Level0) +{ + if (!PowerStateMachineTest::IsTestSupported()) { + POWER_HILOGI(MODULE_SERVICE, "PowerStateMachine009::test is not supported, do nothing!"); + return; + } + sleep(SLEEP_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerStateMachine009: Suspend Device start."; + std::make_unique(&PowerStateMachineTest::CheckReadWakeCount, this)->detach(); + GTEST_LOG_(INFO) << "PowerStateMachine009: Suspend Device end."; +} \ No newline at end of file diff --git a/services/native/test/unittest/src/reboot_test.cpp b/services/native/test/unittest/src/reboot_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..46522ac59a9b6247a5167889b90d2087eb498b8f --- /dev/null +++ b/services/native/test/unittest/src/reboot_test.cpp @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2021 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 +#include +#include +#include +#include +#include "power_reboot_test.h" +#include "power_common.h" +#include "power_mgr_client.h" +#include "power_mgr_service.h" +#include "power_state_machine.h" +#include "sys_param.h" +using namespace testing::ext; +using namespace OHOS::PowerMgr; +using namespace OHOS; +using namespace std; +namespace { +/** + * @tc.name: ShutDownDeviceTest001 + * @tc.desc: test ShutDownDevice in proxy + * @tc.type: FUNC + */ +HWTEST_F (PowerRebootTest, RebootDeviceTest001, TestSize.Level2) +{ + sleep(SLEEP_WAIT_TIME_S); + GTEST_LOG_(INFO) << "RebootDeviceTest001: RebootDevice start."; + auto& powerMgrClient = PowerMgrClient::GetInstance(); + if (true) { + powerMgrClient.RebootDevice(string("RebootDeviceTest001")); + } + GTEST_LOG_(INFO) << "RebootDeviceTest001: RebootDevice end."; +} +} \ No newline at end of file diff --git a/services/native/test/unittest/src/running_lock_test.cpp b/services/native/test/unittest/src/running_lock_test.cpp index 690125cb76fbba632f0360c0e5f0759bea92dfe6..d35d62f38200a87bf36c9e458d3d4f3819c52127 100644 --- a/services/native/test/unittest/src/running_lock_test.cpp +++ b/services/native/test/unittest/src/running_lock_test.cpp @@ -35,6 +35,7 @@ void RunningLockTest::SetUpTestCase(void) ASSERT_TRUE(runningLockMgr_->Init()); } +#ifdef IPC_AVAILABLE /** * @tc.name: RunningLockInnerKit000 * @tc.desc: Test RunningLockInnerKit function, connect PowerMgrService and call member function. @@ -256,45 +257,6 @@ HWTEST_F (RunningLockTest, RunningLockInnerKit004, TestSize.Level1) } } -/** - * @tc.name: RunningLockInnerKit005 - * @tc.desc: Test RunningLockInnerKit function, dfx. - * @tc.type: FUNC - */ -HWTEST_F (RunningLockTest, RunningLockInnerKit005, TestSize.Level2) -{ - auto& powerMgrClient = PowerMgrClient::GetInstance(); - auto runningLock1 = powerMgrClient.CreateRunningLock("runninglock1", RunningLockType::RUNNINGLOCK_SCREEN); - ASSERT_TRUE(runningLock1 != nullptr); - runningLock1->Lock(); - DumpRunningLockInfo(); - auto runningLock2 = powerMgrClient.CreateRunningLock("runninglock2", RunningLockType::RUNNINGLOCK_BACKGROUND); - ASSERT_TRUE(runningLock2 != nullptr); - runningLock2->Lock(); - DumpRunningLockInfo(); - powerMgrClient.ProxyRunningLock(true, getuid(), getpid()); - DumpRunningLockInfo(); - powerMgrClient.ProxyRunningLock(false, getuid(), getpid()); - DumpRunningLockInfo(); - runningLock1->UnLock(); - DumpRunningLockInfo(); - runningLock1->Lock(); - std::shared_ptr worker1 = std::make_shared(1, "worker1"); - std::shared_ptr worker2 = std::make_shared(2, "worker2", 20); - std::shared_ptr worker3 = std::make_shared(3, "worker3", 30); - std::shared_ptr worker4 = std::make_shared(); - WorkTriggerList worklist; - worklist.push_back(worker1); - worklist.push_back(worker2); - worklist.push_back(worker3); - worklist.push_back(worker4); - runningLock1->SetWorkTriggerList(worklist); - DumpRunningLockInfo(); - sleep(70); - DumpRunningLockInfo(); - POWER_HILOGD(MODULE_SERVICE, "PowerMgrUnitTest::RunningLockInnerKit006."); -} - /** * @tc.name: RunningLockMgr001 * @tc.desc: Test RunningLockMgr function, connect PowerMgrService and call member function. @@ -546,3 +508,4 @@ HWTEST_F (RunningLockTest, RunningLockMgr005, TestSize.Level0) runningLockMgr_->UnLock(token); POWER_HILOGD(MODULE_SERVICE, "RunningLockTest::RunningLockMgr005 end."); } +#endif // IPC_AVAILABLE diff --git a/services/native/test/unittest/src/shutdown_test.cpp b/services/native/test/unittest/src/shutdown_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d609914024b22a657ec449501f94d3ce5ae4d410 --- /dev/null +++ b/services/native/test/unittest/src/shutdown_test.cpp @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2021 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 +#include +#include +#include +#include +#include "power_shutdown_test.h" +#include "power_common.h" +#include "power_mgr_client.h" +#include "power_mgr_service.h" +#include "power_state_machine.h" +#include "sys_param.h" +using namespace testing::ext; +using namespace OHOS::PowerMgr; +using namespace OHOS; +using namespace std; + +namespace { +/** + * @tc.name: ShutDownDeviceTest001 + * @tc.desc: test ShutDownDevice in proxy + * @tc.type: FUNC + */ +HWTEST_F (PowerShutdownTest, ShutDownDeviceTest001, TestSize.Level2) +{ + sleep(SLEEP_WAIT_TIME_S); + GTEST_LOG_(INFO) << "ShutDownDeviceTest001: ShutDownDevice start."; + auto& powerMgrClient = PowerMgrClient::GetInstance(); + if (true) { + powerMgrClient.ShutDownDevice(string("ShutDownDeviceTest001")); + } + GTEST_LOG_(INFO) << "ShutDownDeviceTest001: ShutDownDevice end."; +} +} \ No newline at end of file diff --git a/services/zidl/include/power_mgr_proxy.h b/services/zidl/include/power_mgr_proxy.h index 9ad1ab8d4b9255c96689e91022f35dc6c6008ca5..98bae5e2880d3af0cc7443acd9dad6d0dd23c505 100644 --- a/services/zidl/include/power_mgr_proxy.h +++ b/services/zidl/include/power_mgr_proxy.h @@ -31,6 +31,9 @@ public: ~PowerMgrProxy() = default; DISALLOW_COPY_AND_MOVE(PowerMgrProxy); + virtual void CreateRunningLock(const sptr& token, const RunningLockInfo& runningLockInfo) override; + virtual void ReleaseRunningLock(const sptr& token) override; + virtual bool IsRunningLockTypeSupported(uint32_t type) override; virtual void Lock(const sptr& token, const RunningLockInfo& runningLockInfo, uint32_t timeOutMs) override; virtual void UnLock(const sptr& token) override; @@ -41,6 +44,7 @@ public: virtual void SuspendDevice(int64_t callTimeMs, SuspendDeviceType reason, bool suspendImmed) override; virtual void WakeupDevice(int64_t callTimeMs, WakeupDeviceType reason, const std::string& details) override; virtual void RefreshActivity(int64_t callTimeMs, UserActivityType type, bool needChangeBacklight) override; + virtual PowerState GetState() override; virtual bool IsScreenOn() override; virtual bool ForceSuspendDevice(int64_t callTimeMs) override; virtual void RebootDevice(const std::string& reason) override; @@ -49,6 +53,11 @@ public: virtual void UnRegisterPowerStateCallback(const sptr& callback) override; virtual void RegisterShutdownCallback(const sptr& callback) override; virtual void UnRegisterShutdownCallback(const sptr& callback) override; + virtual void RegisterPowerModeCallback(const sptr& callback) override; + virtual void UnRegisterPowerModeCallback(const sptr& callback) override; + virtual void SetDisplaySuspend(bool enable) override; + virtual void SetDeviceMode(const uint32_t& mode) override; + virtual uint32_t GetDeviceMode() override; private: static inline BrokerDelegator delegator_; }; diff --git a/services/zidl/include/power_mgr_stub.h b/services/zidl/include/power_mgr_stub.h index 74f92b23566332ab15e8daec9b6b20975cef1d34..1ff2a3aa075dbcdf7758690c111c35386c9cb52c 100644 --- a/services/zidl/include/power_mgr_stub.h +++ b/services/zidl/include/power_mgr_stub.h @@ -34,9 +34,13 @@ private: int32_t WakeupDeviceStub(MessageParcel& data); int32_t SuspendDeviceStub(MessageParcel& data); int32_t RefreshActivityStub(MessageParcel& data); + int32_t GetStateStub(MessageParcel& reply); int32_t IsScreeOnStub(MessageParcel& reply); int32_t ForceSuspendDeviceStub(MessageParcel& data, MessageParcel& reply); int32_t ProxyRunningLockStub(MessageParcel& data); + int32_t CreateRunningLockStub(MessageParcel& data); + int32_t ReleaseRunningLockStub(MessageParcel& data); + int32_t IsRunningLockTypeSupportedStub(MessageParcel& data); int32_t LockStub(MessageParcel& data); int32_t UnLockStub(MessageParcel& data); int32_t SetWorkTriggerListStub(MessageParcel& data); @@ -47,6 +51,11 @@ private: int32_t UnRegisterPowerStateCallbackStub(MessageParcel& data); int32_t RegisterShutdownCallbackStub(MessageParcel& data); int32_t UnRegisterShutdownCallbackStub(MessageParcel& data); + int32_t RegisterPowerModeCallbackStub(MessageParcel& data); + int32_t UnRegisterPowerModeCallbackStub(MessageParcel& data); + int32_t SetDisplaySuspendStub(MessageParcel& data); + int32_t SetDeviceModeStub(MessageParcel& data); + int32_t GetDeviceModeStub(MessageParcel& reply); }; } // namespace PowerMgr } // namespace OHOS diff --git a/services/zidl/include/power_mode_callback_proxy.h b/services/zidl/include/power_mode_callback_proxy.h new file mode 100644 index 0000000000000000000000000000000000000000..b4155c51e85f590783154f3ceae491b88751bd51 --- /dev/null +++ b/services/zidl/include/power_mode_callback_proxy.h @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2021 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. + */ + +#ifndef POWERMGR_SERVICES_MODE_STATE_CALLBACK_PROXY_H +#define POWERMGR_SERVICES_MODE_STATE_CALLBACK_PROXY_H + +#include +#include + +#include "ipower_mode_callback.h" + +namespace OHOS { +namespace PowerMgr { +class PowerModeCallbackProxy : public IRemoteProxy { +public: + explicit PowerModeCallbackProxy(const sptr& impl) + : IRemoteProxy(impl) {} + virtual ~PowerModeCallbackProxy() = default; + virtual void PowerModeCallback() override; + +private: + static inline BrokerDelegator delegator_; +}; +} // namespace PowerMgr +} // namespace OHOS +#endif // POWERMGR_SERVICES_MODE_STATE_CALLBACK_PROXY_H diff --git a/services/zidl/include/power_mode_callback_stub.h b/services/zidl/include/power_mode_callback_stub.h new file mode 100644 index 0000000000000000000000000000000000000000..307d7d1e9bb121f92c69af72ba35169cf4882e38 --- /dev/null +++ b/services/zidl/include/power_mode_callback_stub.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2021 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. + */ + +#ifndef POWERMGR_SERVICES_IPOWER_MODE_CALLBACK_STUB_H +#define POWERMGR_SERVICES_IPOWER_MODE_CALLBACK_STUB_H + +#include +#include + +#include "ipower_mode_callback.h" + +namespace OHOS { +namespace PowerMgr { +class PowerModeCallbackStub : public IRemoteStub { +public: + DISALLOW_COPY_AND_MOVE(PowerModeCallbackStub); + PowerModeCallbackStub() = default; + virtual ~PowerModeCallbackStub() = default; + int OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override; + void PowerModeCallback() override {}; + +private: + int32_t OnPowerModeCallbackStub(); +}; +} // namespace PowerMgr +} // namespace OHOS +#endif // POWERMGR_SERVICES_IPOWER_MODE_CALLBACK_STUB_H diff --git a/services/zidl/include/power_shutdown_callback_proxy.h b/services/zidl/include/power_shutdown_callback_proxy.h new file mode 100644 index 0000000000000000000000000000000000000000..02a221330e117a3a52c63061915f0f3c40ab1dc3 --- /dev/null +++ b/services/zidl/include/power_shutdown_callback_proxy.h @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2021 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. + */ + +#ifndef POWERMGR_SERVICES_SHUTDOWN_STATE_CALLBACK_PROXY_H +#define POWERMGR_SERVICES_SHUTDOWN_STATE_CALLBACK_PROXY_H + +#include +#include + +#include "ishutdown_callback.h" + +namespace OHOS { +namespace PowerMgr { +class PowerShutdownCallbackProxy : public IRemoteProxy { +public: + explicit PowerShutdownCallbackProxy(const sptr& impl) + : IRemoteProxy(impl) {} + virtual ~PowerShutdownCallbackProxy() = default; + virtual void ShutdownCallback() override; + +private: + static inline BrokerDelegator delegator_; +}; +} // namespace PowerMgr +} // namespace OHOS +#endif // POWERMGR_SERVICES_SHUTDOWN_STATE_CALLBACK_PROXY_H diff --git a/services/zidl/include/power_shutdown_callback_stub.h b/services/zidl/include/power_shutdown_callback_stub.h new file mode 100644 index 0000000000000000000000000000000000000000..8bd6a6e47e0c82c7832abb26b033136b0ca122e3 --- /dev/null +++ b/services/zidl/include/power_shutdown_callback_stub.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2021 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. + */ + +#ifndef POWERMGR_SERVICES_IPOWER_SHUTDOWN_CALLBACK_STUB_H +#define POWERMGR_SERVICES_IPOWER_SHUTDOWN_CALLBACK_STUB_H + +#include +#include + +#include "ishutdown_callback.h" + +namespace OHOS { +namespace PowerMgr { +class PowerShutdownCallbackStub : public IRemoteStub { +public: + DISALLOW_COPY_AND_MOVE(PowerShutdownCallbackStub); + PowerShutdownCallbackStub() = default; + virtual ~PowerShutdownCallbackStub() = default; + int OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override; + void ShutdownCallback() override {}; + +private: + int32_t OnPowerShutdownCallbackStub(); +}; +} // namespace PowerMgr +} // namespace OHOS +#endif // POWERMGR_SERVICES_IPOWER_STATE_CALLBACK_STUB_H diff --git a/services/zidl/src/power_mgr_proxy.cpp b/services/zidl/src/power_mgr_proxy.cpp index c1557ef19c4075107ae3a93e0025ce42560cbf74..9a943ce562226957b9af102e3cb9e3b678056319 100644 --- a/services/zidl/src/power_mgr_proxy.cpp +++ b/services/zidl/src/power_mgr_proxy.cpp @@ -23,6 +23,84 @@ namespace OHOS { namespace PowerMgr { +void PowerMgrProxy::CreateRunningLock(const sptr& token, const RunningLockInfo& runningLockInfo) +{ + sptr remote = Remote(); + RETURN_IF(remote == nullptr); + + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) { + POWER_HILOGE(MODULE_INNERKIT, "PowerMgrProxy::%{public}s write descriptor failed!", __func__); + return; + } + + WRITE_PARCEL_NO_RET(data, RemoteObject, token.GetRefPtr()); + WRITE_PARCEL_NO_RET(data, Parcelable, &runningLockInfo); + + int ret = remote->SendRequest(static_cast(IPowerMgr::CREATE_RUNNINGLOCK), + data, reply, option); + if (ret != ERR_OK) { + POWER_HILOGE(MODULE_INNERKIT, "PowerMgrProxy::%{public}s SendRequest is failed, error code: %d", __func__, ret); + return; + } +} + +void PowerMgrProxy::ReleaseRunningLock(const sptr& token) +{ + sptr remote = Remote(); + RETURN_IF(remote == nullptr); + + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) { + POWER_HILOGE(MODULE_INNERKIT, "PowerMgrProxy::%{public}s write descriptor failed!", __func__); + return; + } + + WRITE_PARCEL_NO_RET(data, RemoteObject, token.GetRefPtr()); + + int ret = remote->SendRequest(static_cast(IPowerMgr::RELEASE_RUNNINGLOCK), + data, reply, option); + if (ret != ERR_OK) { + POWER_HILOGE(MODULE_INNERKIT, "PowerMgrProxy::%{public}s SendRequest is failed, error code: %d", __func__, ret); + return; + } +} + +bool PowerMgrProxy::IsRunningLockTypeSupported(uint32_t type) +{ + sptr remote = Remote(); + RETURN_IF_WITH_RET(remote == nullptr, false); + + bool result = false; + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) { + POWER_HILOGE(MODULE_INNERKIT, "PowerMgrProxy::%{public}s write descriptor failed!", __func__); + return result; + } + + WRITE_PARCEL_WITH_RET(data, Uint32, static_cast(type), false); + int ret = remote->SendRequest(static_cast(IPowerMgr::IS_RUNNINGLOCK_TYPE_SUPPORTED), data, reply, option); + if (ret != ERR_OK) { + POWER_HILOGE(MODULE_INNERKIT, "PowerMgrProxy::%{public}s SendRequest is failed, error code: %d", __func__, ret); + return result; + } + + if (!reply.ReadBool(result)) { + POWER_HILOGE(MODULE_INNERKIT, "PowerMgrProxy::%{public}s Readback fail!", __func__); + } + + return result; +} + void PowerMgrProxy::Lock(const sptr& token, const RunningLockInfo& runningLockInfo, uint32_t timeOutMs) { sptr remote = Remote(); @@ -295,6 +373,34 @@ bool PowerMgrProxy::ForceSuspendDevice(int64_t callTimeMs) return result; } +PowerState PowerMgrProxy::GetState() +{ + sptr remote = Remote(); + RETURN_IF_WITH_RET(remote == nullptr, PowerState::UNKNOWN); + + uint32_t result = 0; + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) { + POWER_HILOGE(MODULE_INNERKIT, "PowerMgrProxy::%{public}s write descriptor failed!", __func__); + return PowerState::UNKNOWN; + } + + int ret = remote->SendRequest(static_cast(IPowerMgr::GET_STATE), data, reply, option); + if (ret != ERR_OK) { + POWER_HILOGE(MODULE_INNERKIT, "PowerMgrProxy::%{public}s SendRequest is failed, error code: %d", __func__, ret); + return PowerState::UNKNOWN; + } + if (!reply.ReadUint32(result)) { + POWER_HILOGE(MODULE_INNERKIT, "PowerMgrProxy::%{public}s Readback fail!", __func__); + return PowerState::UNKNOWN; + } + + return static_cast(result); +} + bool PowerMgrProxy::IsScreenOn() { sptr remote = Remote(); @@ -413,5 +519,125 @@ void PowerMgrProxy::UnRegisterShutdownCallback(const sptr& ca return; } } + +void PowerMgrProxy::RegisterPowerModeCallback(const sptr& callback) +{ + sptr remote = Remote(); + RETURN_IF((remote == nullptr) || (callback == nullptr)); + + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) { + POWER_HILOGE(MODULE_INNERKIT, "Write descriptor failed!"); + return; + } + + WRITE_PARCEL_NO_RET(data, RemoteObject, callback->AsObject()); + + int ret = remote->SendRequest(static_cast(IPowerMgr::REG_POWER_MODE_CALLBACK), data, reply, option); + if (ret != ERR_OK) { + POWER_HILOGE(MODULE_INNERKIT, "SendRequest is failed, error code: %{public}d", ret); + return; + } +} + +void PowerMgrProxy::UnRegisterPowerModeCallback(const sptr& callback) +{ + sptr remote = Remote(); + RETURN_IF((remote == nullptr) || (callback == nullptr)); + + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) { + POWER_HILOGE(MODULE_INNERKIT, "Write descriptor failed!"); + return; + } + + WRITE_PARCEL_NO_RET(data, RemoteObject, callback->AsObject()); + + int ret = remote->SendRequest(static_cast(IPowerMgr::UNREG_POWER_MODE_CALLBACK), data, reply, option); + if (ret != ERR_OK) { + POWER_HILOGE(MODULE_INNERKIT, "SendRequest is failed, error code: %{public}d", ret); + return; + } +} + +void PowerMgrProxy::SetDisplaySuspend(bool enable) +{ + sptr remote = Remote(); + RETURN_IF(remote == nullptr); + + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) { + POWER_HILOGE(MODULE_INNERKIT, "PowerMgrProxy::%{public}s write descriptor failed!", __func__); + return; + } + + WRITE_PARCEL_NO_RET(data, Bool, enable); + + int ret = remote->SendRequest(static_cast(IPowerMgr::SET_DISPLAY_SUSPEND), data, reply, option); + if (ret != ERR_OK) { + POWER_HILOGE(MODULE_INNERKIT, "PowerMgrProxy::%{public}s Transact is failed, error code: %d", __func__, ret); + } +} + +void PowerMgrProxy::SetDeviceMode(const uint32_t& mode) +{ + sptr remote = Remote(); + RETURN_IF(remote == nullptr); + + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) { + POWER_HILOGE(MODULE_INNERKIT, "PowerMgrProxy::%{public}s write descriptor failed!", __func__); + return; + } + + WRITE_PARCEL_NO_RET(data, Uint32, mode); + + int ret = remote->SendRequest(static_cast(IPowerMgr::SETMODE_DEVICE), data, reply, option); + if (ret != ERR_OK) { + POWER_HILOGE(MODULE_INNERKIT, "PowerMgrProxy::%{public}s Transact is failed, error code: %d", __func__, ret); + } +} + +uint32_t PowerMgrProxy::GetDeviceMode() +{ + sptr remote = Remote(); + RETURN_IF_WITH_RET(remote == nullptr, false); + + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) { + POWER_HILOGE(MODULE_INNERKIT, "PowerMgrProxy::%{public}s write descriptor failed!", __func__); + return 0; + } + + int ret = remote->SendRequest(static_cast(IPowerMgr::GETMODE_DEVICE), + data, reply, option); + if (ret != ERR_OK) { + POWER_HILOGE(MODULE_INNERKIT, + "PowerMgrProxy::%{public}s SendRequest is failed, error code: %{public}d", __func__, ret); + return 0; + } + + uint32_t used = 0; + if (!reply.ReadUint32(used)) { + POWER_HILOGE(MODULE_INNERKIT, "PowerMgrProxy::%{public}s Readback fail!", __func__); + } + POWER_HILOGD(MODULE_SERVICE, "PowerMgrStub::GetDeviceMode, cmd = %{public}u.", used); + return used; +} } // namespace PowerMgr } // namespace OHOS diff --git a/services/zidl/src/power_mgr_stub.cpp b/services/zidl/src/power_mgr_stub.cpp index 023fcd64ee4d30b9e16f88eb9ef1ffcbe632f7cd..5363f8e17f0344c287125a1e9604225678381cb5 100644 --- a/services/zidl/src/power_mgr_stub.cpp +++ b/services/zidl/src/power_mgr_stub.cpp @@ -25,13 +25,16 @@ using namespace OHOS::HiviewDFX; namespace OHOS { namespace PowerMgr { -int PowerMgrStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) +int PowerMgrStub::OnRemoteRequest(uint32_t code, MessageParcel &data, + MessageParcel &reply, MessageOption &option) { - POWER_HILOGD(MODULE_SERVICE, "PowerMgrStub::OnRemoteRequest, cmd = %u, flags= %d", code, option.GetFlags()); + POWER_HILOGD(MODULE_SERVICE, + "PowerMgrStub::OnRemoteRequest, cmd = %{public}u, flags= %{public}d", code, option.GetFlags()); std::u16string descripter = PowerMgrStub::GetDescriptor(); std::u16string remoteDescripter = data.ReadInterfaceToken(); if (descripter != remoteDescripter) { - POWER_HILOGE(MODULE_SERVICE, "PowerMgrStub::OnRemoteRequest failed, descriptor is not matched!"); + POWER_HILOGE(MODULE_SERVICE, + "PowerMgrStub::OnRemoteRequest failed, descriptor is not matched!"); return E_GET_POWER_SERVICE_FAILED; } @@ -51,12 +54,24 @@ int PowerMgrStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessagePar case static_cast(IPowerMgr::SHUTDOWN_DEVICE): { return ShutDownDeviceStub(data); } + case static_cast(IPowerMgr::GET_STATE): { + return GetStateStub(reply); + } case static_cast(IPowerMgr::IS_SCREEN_ON): { return IsScreeOnStub(reply); } case static_cast(IPowerMgr::FORCE_DEVICE_SUSPEND): { return ForceSuspendDeviceStub(data, reply); } + case static_cast(IPowerMgr::CREATE_RUNNINGLOCK): { + return CreateRunningLockStub(data); + } + case static_cast(IPowerMgr::RELEASE_RUNNINGLOCK): { + return ReleaseRunningLockStub(data); + } + case static_cast(IPowerMgr::IS_RUNNINGLOCK_TYPE_SUPPORTED): { + return IsRunningLockTypeSupportedStub(data); + } case static_cast(IPowerMgr::RUNNINGLOCK_LOCK): { return LockStub(data); } @@ -84,6 +99,21 @@ int PowerMgrStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessagePar case static_cast(IPowerMgr::UNREG_SHUTDOWN_CALLBACK): { return UnRegisterShutdownCallbackStub(data); } + case static_cast(IPowerMgr::REG_POWER_MODE_CALLBACK): { + return RegisterPowerModeCallbackStub(data); + } + case static_cast(IPowerMgr::UNREG_POWER_MODE_CALLBACK): { + return UnRegisterPowerModeCallbackStub(data); + } + case static_cast(IPowerMgr::SET_DISPLAY_SUSPEND): { + return SetDisplaySuspendStub(data); + } + case static_cast(IPowerMgr::SETMODE_DEVICE): { + return SetDeviceModeStub(data); + } + case static_cast(IPowerMgr::GETMODE_DEVICE): { + return GetDeviceModeStub(reply); + } default: { return IPCObjectStub::OnRemoteRequest(code, data, reply, option); } @@ -91,6 +121,32 @@ int PowerMgrStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessagePar return ERR_OK; } +int32_t PowerMgrStub::CreateRunningLockStub(MessageParcel& data) +{ + sptr token = data.ReadRemoteObject(); + RETURN_IF_WITH_RET((token == nullptr), E_READ_PARCEL_ERROR); + std::unique_ptr runningLockInfo(data.ReadParcelable()); + RETURN_IF_WITH_RET((runningLockInfo == nullptr), E_READ_PARCEL_ERROR); + CreateRunningLock(token, *runningLockInfo); + return ERR_OK; +} + +int32_t PowerMgrStub::ReleaseRunningLockStub(MessageParcel& data) +{ + sptr token = data.ReadRemoteObject(); + RETURN_IF_WITH_RET((token == nullptr), E_READ_PARCEL_ERROR); + ReleaseRunningLock(token); + return ERR_OK; +} + +int32_t PowerMgrStub::IsRunningLockTypeSupportedStub(MessageParcel& data) +{ + uint32_t type = 0; + READ_PARCEL_WITH_RET(data, Uint32, type, E_READ_PARCEL_ERROR); + IsRunningLockTypeSupported(type); + return ERR_OK; +} + int32_t PowerMgrStub::LockStub(MessageParcel& data) { sptr token = data.ReadRemoteObject(); @@ -212,6 +268,16 @@ int32_t PowerMgrStub::ForceSuspendDeviceStub(MessageParcel& data, MessageParcel& return ERR_OK; } +int32_t PowerMgrStub::GetStateStub(MessageParcel& reply) +{ + PowerState ret = GetState(); + if (!reply.WriteUint32(static_cast(ret))) { + POWER_HILOGE(MODULE_SERVICE, "PowerMgrStub:: GetStateStub Writeback Fail!"); + return E_WRITE_PARCEL_ERROR; + } + return ERR_OK; +} + int32_t PowerMgrStub::IsScreeOnStub(MessageParcel& reply) { bool ret = false; @@ -262,5 +328,53 @@ int32_t PowerMgrStub::UnRegisterShutdownCallbackStub(MessageParcel& data) UnRegisterShutdownCallback(callback); return ERR_OK; } + +int32_t PowerMgrStub::RegisterPowerModeCallbackStub(MessageParcel& data) +{ + sptr obj = data.ReadRemoteObject(); + RETURN_IF_WITH_RET((obj == nullptr), E_READ_PARCEL_ERROR); + sptr callback = iface_cast(obj); + RETURN_IF_WITH_RET((callback == nullptr), E_READ_PARCEL_ERROR); + RegisterPowerModeCallback(callback); + return ERR_OK; +} + +int32_t PowerMgrStub::UnRegisterPowerModeCallbackStub(MessageParcel& data) +{ + sptr obj = data.ReadRemoteObject(); + RETURN_IF_WITH_RET((obj == nullptr), E_READ_PARCEL_ERROR); + sptr callback = iface_cast(obj); + RETURN_IF_WITH_RET((callback == nullptr), E_READ_PARCEL_ERROR); + UnRegisterPowerModeCallback(callback); + return ERR_OK; +} + +int32_t PowerMgrStub::SetDisplaySuspendStub(MessageParcel& data) +{ + bool enable = false; + READ_PARCEL_WITH_RET(data, Bool, enable, E_READ_PARCEL_ERROR); + SetDisplaySuspend(enable); + return ERR_OK; +} + +int32_t PowerMgrStub::SetDeviceModeStub(MessageParcel& data) +{ + uint32_t mode = 0; + READ_PARCEL_WITH_RET(data, Uint32, mode, E_READ_PARCEL_ERROR); + SetDeviceMode(mode); + return ERR_OK; +} + +int32_t PowerMgrStub::GetDeviceModeStub(MessageParcel& reply) +{ + uint32_t ret = 0; + ret = GetDeviceMode(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrStub::GetDeviceModeStub, cmd = %{public}u.", ret); + if (!reply.WriteUint32(static_cast(ret))) { + POWER_HILOGE(MODULE_SERVICE, "PowerMgrStub:: Get device mode Fail!"); + return E_WRITE_PARCEL_ERROR; + } + return ERR_OK; +} } // namespace PowerMgr } // namespace OHOS diff --git a/services/zidl/src/power_mode_callback_proxy.cpp b/services/zidl/src/power_mode_callback_proxy.cpp new file mode 100644 index 0000000000000000000000000000000000000000..159f204722a234511c5b0340ac3c95a6dfa5095d --- /dev/null +++ b/services/zidl/src/power_mode_callback_proxy.cpp @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2021 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 "power_mode_callback_proxy.h" + +#include +#include + +#include "power_common.h" +#include "power_mode_callback_stub.h" + +namespace OHOS { +namespace PowerMgr { +void PowerModeCallbackProxy::PowerModeCallback() +{ + sptr remote = Remote(); + RETURN_IF(remote == nullptr); + + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteInterfaceToken(PowerModeCallbackProxy::GetDescriptor())) { + POWER_HILOGE(MODULE_INNERKIT, "PowerMgrProxy::%{public}s write descriptor failed!", __func__); + return; + } + + int ret = remote->SendRequest(static_cast(IPowerModeCallback::POWER_MODE_CHANGED), + data, reply, option); + if (ret != ERR_OK) { + POWER_HILOGE(MODULE_INNERKIT, + "PowerMgrProxy::%{public}s SendRequest is failed, error code: %{private}d", __func__, ret); + } +} +} // namespace PowerMgr +} // namespace OHOS diff --git a/services/zidl/src/power_mode_callback_stub.cpp b/services/zidl/src/power_mode_callback_stub.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8150bf060f18e15504c3e0c95ffa24f2268b1acf --- /dev/null +++ b/services/zidl/src/power_mode_callback_stub.cpp @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2021 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 "power_mode_callback_stub.h" + +#include + +#include "power_common.h" +#include "power_mode_callback_proxy.h" + +namespace OHOS { +namespace PowerMgr { +int PowerModeCallbackStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, + MessageOption &option) + { + POWER_HILOGD(MODULE_SERVICE, "PowerMgrStub::OnRemoteRequest, cmd = %d, flags= %d", code, option.GetFlags()); + std::u16string descripter = PowerModeCallbackStub::GetDescriptor(); + std::u16string remoteDescripter = data.ReadInterfaceToken(); + if (descripter != remoteDescripter) { + POWER_HILOGE(MODULE_SERVICE, "PowerMgrStub::OnRemoteRequest failed, descriptor is not matched!"); + return E_GET_POWER_SERVICE_FAILED; + } + + switch (code) { + case static_cast(IPowerModeCallback::POWER_MODE_CHANGED): { + return OnPowerModeCallbackStub(); + } + default: + return IPCObjectStub::OnRemoteRequest(code, data, reply, option); + } + return ERR_OK; +} + +int32_t PowerModeCallbackStub::OnPowerModeCallbackStub() +{ + PowerModeCallback(); + return ERR_OK; +} +} // namespace PowerMgr +} // namespace OHOS diff --git a/services/zidl/src/power_shutdown_callback_proxy.cpp b/services/zidl/src/power_shutdown_callback_proxy.cpp new file mode 100644 index 0000000000000000000000000000000000000000..57733a6107b81e355cfa88e6a887255db2646715 --- /dev/null +++ b/services/zidl/src/power_shutdown_callback_proxy.cpp @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2021 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 "power_shutdown_callback_proxy.h" + +#include +#include + +#include "power_common.h" +#include "power_shutdown_callback_stub.h" + +namespace OHOS { +namespace PowerMgr { +void PowerShutdownCallbackProxy::ShutdownCallback() +{ + sptr remote = Remote(); + RETURN_IF(remote == nullptr); + + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteInterfaceToken(PowerShutdownCallbackProxy::GetDescriptor())) { + POWER_HILOGE(MODULE_INNERKIT, "PowerMgrProxy::%{public}s write descriptor failed!", __func__); + return; + } + + int ret = remote->SendRequest(static_cast(IShutdownCallback::POWER_SHUTDOWN_CHANGED), + data, reply, option); + if (ret != ERR_OK) { + POWER_HILOGE(MODULE_INNERKIT, + "PowerMgrProxy::%{public}s SendRequest is failed, error code: %{private}d", __func__, ret); + } +} +} // namespace PowerMgr +} // namespace OHOS diff --git a/services/zidl/src/power_shutdown_callback_stub.cpp b/services/zidl/src/power_shutdown_callback_stub.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2790af09a86d96fdfe38f7a3db31de441048273a --- /dev/null +++ b/services/zidl/src/power_shutdown_callback_stub.cpp @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2021 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 "power_shutdown_callback_stub.h" + +#include + +#include "power_common.h" +#include "power_shutdown_callback_proxy.h" + +namespace OHOS { +namespace PowerMgr { +int PowerShutdownCallbackStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, + MessageOption &option) +{ + POWER_HILOGD(MODULE_SERVICE, "PowerMgrStub::OnRemoteRequest, cmd = %d, flags= %d", code, option.GetFlags()); + std::u16string descripter = PowerShutdownCallbackStub::GetDescriptor(); + std::u16string remoteDescripter = data.ReadInterfaceToken(); + if (descripter != remoteDescripter) { + POWER_HILOGE(MODULE_SERVICE, "PowerMgrStub::OnRemoteRequest failed, descriptor is not matched!"); + return E_GET_POWER_SERVICE_FAILED; + } + + switch (code) { + case static_cast(IShutdownCallback::POWER_SHUTDOWN_CHANGED): { + return OnPowerShutdownCallbackStub(); + } + default: + return IPCObjectStub::OnRemoteRequest(code, data, reply, option); + } + return ERR_OK; +} + +int32_t PowerShutdownCallbackStub::OnPowerShutdownCallbackStub() +{ + ShutdownCallback(); + return ERR_OK; +} +} // namespace PowerMgr +} // namespace OHOS diff --git a/test/BUILD.gn b/test/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..0db7448f4235dabce260279d19e9f2fda82b7216 --- /dev/null +++ b/test/BUILD.gn @@ -0,0 +1,20 @@ +# Copyright (c) 2021 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("//base/powermgr/power_manager/powermgr.gni") +import("//build/test.gni") + +group("systemtest") { + testonly = true + deps = [ "systemtest:systemtest_powermgr" ] +} diff --git a/test/systemtest/BUILD.gn b/test/systemtest/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..a6ccbb1878bc80ab2c32b1de38f60bb5e49f783d --- /dev/null +++ b/test/systemtest/BUILD.gn @@ -0,0 +1,164 @@ +# Copyright (c) 2021 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("//base/powermgr/power_manager/powermgr.gni") +import("//build/test.gni") + +module_output_path = "${powermgr_native_part_name}/powermgr_native" + +############################################################################### +config("module_private_config") { + visibility = [ ":*" ] + + include_dirs = [ + "include", + "mock", + "//utils/system/safwk/native/include", + "//third_party/googletest/googletest/include", + "//base/powermgr/power_manager/services/native/include", + "//base/powermgr/power_manager/services/native/src", + "//foundation/communication/ipc/interfaces/innerkits/ipc_core/include", + "//utils/native/base/include", + "//foundation/distributedschedule/safwk/services/safwk/include", + "//base/powermgr/power_manager/interfaces/innerkits/native/include", + "//foundation/appexecfwk/standard/interfaces/innerkits/libeventhandler/include", + "//base/notification/ces_standard/cesfwk/kits/native/include", + "//foundation/aafwk/standard/interfaces/innerkits/want/include", + "//foundation/distributedschedule/dmsfwk/services/dtbschedmgr/include", + "//foundation/aafwk/standard/interfaces/innerkits/base/include", + "//foundation/appexecfwk/standard/interfaces/innerkits/appexecfwk_base/include", + "//base/powermgr/power_manager/services/zidl/include", + "//base/powermgr/battery_manager/utils/native/include", + "//foundation/aafwk/standard/interfaces/innerkits/want/include/ohos/aafwk/content", + "//base/powermgr/power_manager/services/native/src/actions/default", + "//base/powermgr/display_manager/interfaces/innerkits/native/include", + "//base/powermgr/display_manager/service/native/include", + "//base/powermgr/display_manager/service/zidl/include", + "//drivers/peripheral/display/interfaces/include", + "//foundation/graphic/standard/utils/include", + "//foundation/distributedschedule/samgr/interfaces/innerkits/samgr_proxy/include", + "//base/powermgr/power_manager/utils/native/include", + "//base/powermgr/power_manager/services/native/test/unittest/include", + "//base/hiviewdfx/hilog/interfaces/native/innerkits/include", + "//foundation/aafwk/standard/interfaces/innerkits/intent/include", + "//base/notification/ces_standard/cesfwk/innerkits/include", + "//base/notification/ces_standard/cesfwk/services/include", + "//base/notification/ces_standard/common/log/include", + ] +} + +deps_ex = [ + "aafwk_standard:base", + "aafwk_standard:want", + "appexecfwk_standard:appexecfwk_base", + "appexecfwk_standard:libeventhandler", + "ces_standard:cesfwk_innerkits", + "ipc:ipc_core", + "hiviewdfx_hilog_native:libhilog", + "safwk:system_ability_fwk", + "samgr_L2:samgr_proxy", +] + +##############################sttest########################################## + +ohos_systemtest("test_power_st_mgr_mock") { + module_out_path = module_output_path + + sources = [ "src/power_mgr_st_mock_test.cpp" ] + + configs = [ + "${powermgr_utils_path}:utils_config", + ":module_private_config", + ] + + deps = [ + "${powermgr_native_innerkits_path}:powermgr_client", + "${powermgr_service_path}:powermgrservice", + "${powermgr_service_path}/native/src/actions:powermgr_actions", + "${powermgr_utils_path}:powermgr_utils", + "//third_party/googletest:gmock_main", + "//third_party/googletest:gtest_main", + "//utils/native/base:utils", + ] + + external_deps = deps_ex +} + +##############################systemtest########################################## + +ohos_systemtest("test_power_mgr_mock_system") { + module_out_path = module_output_path + + sources = [ "src/power_mgr_mock_system_test.cpp" ] + + configs = [ + "${powermgr_utils_path}:utils_config", + ":module_private_config", + ] + + deps = [ + "${powermgr_native_innerkits_path}:powermgr_client", + "${powermgr_service_path}:powermgrservice", + "${powermgr_service_path}/native/src/actions:powermgr_actions", + "${powermgr_utils_path}:powermgr_utils", + "//third_party/googletest:gmock_main", + "//third_party/googletest:gtest_main", + "//utils/native/base:utils", + ] + + external_deps = deps_ex +} + +################################powersavemode################################ + +ohos_systemtest("test_power_mgr_powersavemode") { + module_out_path = module_output_path + + sources = [ "src/power_mgr_powersavemode_test.cpp" ] + + configs = [ + "${powermgr_utils_path}:utils_config", + ":module_private_config", + ] + + deps = [ + "${powermgr_native_innerkits_path}:powermgr_client", + "${powermgr_service_path}:powermgrservice", + "${powermgr_service_path}/native/src/actions:powermgr_actions", + "${powermgr_utils_path}:powermgr_utils", + "//third_party/googletest:gmock_main", + "//third_party/googletest:gtest_main", + "//utils/native/base:utils", + ] + + external_deps = [ + "aafwk_standard:base", + "aafwk_standard:want", + "appexecfwk_standard:appexecfwk_base", + "appexecfwk_standard:libeventhandler", + "ces_standard:cesfwk_innerkits", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "safwk:system_ability_fwk", + "samgr_L2:samgr_proxy", + ] +} + +group("systemtest_powermgr") { + testonly = true + deps = [ + ":test_power_mgr_mock_system", + ":test_power_mgr_powersavemode", + ":test_power_st_mgr_mock", + ] +} diff --git a/test/systemtest/include/power_mgr_mock_system_test.h b/test/systemtest/include/power_mgr_mock_system_test.h new file mode 100644 index 0000000000000000000000000000000000000000..70bf066cb0f039705342be71787c3fba922cfff0 --- /dev/null +++ b/test/systemtest/include/power_mgr_mock_system_test.h @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2021 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. + */ + +#ifndef POWERMGR_POWERMGR_ST_MOCK_TEST_H +#define POWERMGR_POWERMGR_ST_MOCK_TEST_H + +#include +#include + +#include "mock_lock_action.h" +#include "mock_power_action.h" +#include "mock_state_action.h" +#include "power_common.h" +#include "power_mgr_service.h" + +namespace OHOS { +namespace PowerMgr { +constexpr int NEXT_WAIT_TIME_S = 1; +constexpr int ASYNC_WAIT_TIME_S = 3; +constexpr int REFRESHACTIVITY_WAIT_TIME_S = 8; +constexpr int SCREEN_OFF_WAIT_TIME_S = (DEFAULT_DISPLAY_OFF_TIME / 1000); +constexpr int SCREEN_DIM_WAIT_TIME_S = (SCREEN_OFF_WAIT_TIME_S / 2); +constexpr int SLEEP_WAIT_TIME_S = (DEFAULT_SLEEP_TIME / 1000); + +class PowerMgrMockSystemTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; +} // namespace PowerMgr +} // namespace OHOS + +#endif // POWERMGR_POWERMGR_MOCK_TEST_H diff --git a/test/systemtest/include/power_mgr_powersavemode_test.h b/test/systemtest/include/power_mgr_powersavemode_test.h new file mode 100644 index 0000000000000000000000000000000000000000..a7884a869f627dbd3b131d4bafe4dcd2cb594a17 --- /dev/null +++ b/test/systemtest/include/power_mgr_powersavemode_test.h @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2021 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. + */ + +#ifndef POWERMGR_POWERSAVEMODE_TEST_H +#define POWERMGR_POWERSAVEMODE_TEST_H + +#include + +#include "power_mgr_client.h" +#include "power_mgr_service.h" +#include "power_mode_callback_stub.h" +#include "common_event_subscriber.h" +#include "common_event_support.h" + + +namespace OHOS { +namespace PowerMgr { +constexpr int SLEEP_WAIT_TIME_S = 6; +constexpr int REFRESHACTIVITY_WAIT_TIME_S = 8; +constexpr int SCREEN_OFF_WAIT_TIME_S = 15; + +class PowerMgrPowerSavemodeTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + class PowerModeTest1Callback : public PowerModeCallbackStub { + public: + PowerModeTest1Callback() {}; + virtual ~PowerModeTest1Callback() {}; + virtual void PowerModeCallback() override; + }; + class CommonEventServiCesSystemTest : public EventFwk::CommonEventSubscriber { + public: + CommonEventServiCesSystemTest(const EventFwk::CommonEventSubscribeInfo &subscriberInfo); + virtual ~CommonEventServiCesSystemTest() {}; + virtual void OnReceiveEvent(const EventFwk::CommonEventData &data); +}; +}; +} // namespace PowerMgr +} // namespace OHOS +#endif diff --git a/test/systemtest/include/power_mgr_st_mock_test.h b/test/systemtest/include/power_mgr_st_mock_test.h new file mode 100644 index 0000000000000000000000000000000000000000..e2529ac2b6f82b3709cce4adc993df988b8edafc --- /dev/null +++ b/test/systemtest/include/power_mgr_st_mock_test.h @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2021 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. + */ + +#ifndef POWERMGR_POWERMGR_ST_MOCK_TEST_H +#define POWERMGR_POWERMGR_ST_MOCK_TEST_H + +#include +#include + +#include "mock_lock_action.h" +#include "mock_power_action.h" +#include "mock_state_action.h" +#include "power_common.h" +#include "power_mgr_service.h" + +namespace OHOS { +namespace PowerMgr { +constexpr int NEXT_WAIT_TIME_S = 1; +constexpr int ASYNC_WAIT_TIME_S = 3; +constexpr int REFRESHACTIVITY_WAIT_TIME_S = 8; +constexpr int SCREEN_OFF_WAIT_TIME_S = (DEFAULT_DISPLAY_OFF_TIME / 1000); +constexpr int SCREEN_DIM_WAIT_TIME_S = (SCREEN_OFF_WAIT_TIME_S / 2); +constexpr int SLEEP_WAIT_TIME_S = (DEFAULT_SLEEP_TIME / 1000); + +class PowerMgrSTMockTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; +} // namespace PowerMgr +} // namespace OHOS + +#endif // POWERMGR_POWERMGR_MOCK_TEST_H diff --git a/test/systemtest/mock/mock_lock_action.h b/test/systemtest/mock/mock_lock_action.h new file mode 100644 index 0000000000000000000000000000000000000000..c3bef0f27dbe0c01c7fe7c635d96b4aa2adbb550 --- /dev/null +++ b/test/systemtest/mock/mock_lock_action.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2021 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. + */ + +#ifndef POWERMGR_MOCK_LOCK_ACTION_H +#define POWERMGR_MOCK_LOCK_ACTION_H + +#include +#include "actions/irunning_lock_action.h" + +namespace OHOS { +namespace PowerMgr { +class MockLockAction : public IRunningLockAction { +public: + MockLockAction() = default; + virtual ~MockLockAction() = default; + MOCK_METHOD1(Acquire, void(RunningLockType type)); + MOCK_METHOD1(Release, void(RunningLockType type)); + MOCK_METHOD2(Lock, void(RunningLockType type, const std::string& tag)); + MOCK_METHOD2(Unlock, void(RunningLockType type, const std::string& tag)); +}; +} // namespace PowerMgr +} // namespace OHOS + +#endif // POWERMGR_MOCK_POWER_ACTION_H \ No newline at end of file diff --git a/test/systemtest/mock/mock_power_action.h b/test/systemtest/mock/mock_power_action.h new file mode 100644 index 0000000000000000000000000000000000000000..9946a4fc9478c150739595cf2f84ff036b44a594 --- /dev/null +++ b/test/systemtest/mock/mock_power_action.h @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2021 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. + */ + +#ifndef POWERMGR_MOCK_POWER_ACTION_H +#define POWERMGR_MOCK_POWER_ACTION_H + +#include +#include "actions/idevice_power_action.h" + +namespace OHOS { +namespace PowerMgr { +class MockPowerAction : public IDevicePowerAction { +public: + MockPowerAction() = default; + virtual ~MockPowerAction() = default; + MOCK_METHOD1(Reboot, void(const std::string& reason)); + MOCK_METHOD1(Shutdown, void(const std::string& reason)); +}; +} // namespace PowerMgr +} // namespace OHOS + +#endif // POWERMGR_MOCK_POWER_ACTION_H \ No newline at end of file diff --git a/test/systemtest/mock/mock_state_action.h b/test/systemtest/mock/mock_state_action.h new file mode 100644 index 0000000000000000000000000000000000000000..a0040ed5b92102f6cf2b5b487170650a47103ec0 --- /dev/null +++ b/test/systemtest/mock/mock_state_action.h @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2021 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. + */ + +#ifndef POWERMGR_MOCK_STATE_ACTION_H +#define POWERMGR_MOCK_STATE_ACTION_H + +#include +#include "actions/idevice_state_action.h" + +namespace OHOS { +namespace PowerMgr { +class MockStateAction : public IDeviceStateAction { +public: + MockStateAction() = default; + virtual ~MockStateAction() = default; + MOCK_METHOD3(Suspend, void(int64_t callTimeMs, SuspendDeviceType type, uint32_t flags)); + MOCK_METHOD0(ForceSuspend, void()); + MOCK_METHOD4(Wakeup, void(int64_t callTimeMs, WakeupDeviceType type, + const std::string& details, const std::string& pkgName)); + MOCK_METHOD3(RefreshActivity, void(int64_t callTimeMs, UserActivityType type, uint32_t flags)); + MOCK_METHOD0(GetDisplayState, DisplayState()); + MOCK_METHOD1(SetDisplayState, uint32_t(DisplayState state)); + MOCK_METHOD3(GoToSleep, uint32_t(std::function onSuspend, std::function onWakeup, bool force)); +}; +} // namespace PowerMgr +} // namespace OHOS + +#endif // POWERMGR_MOCK_STATE_ACTION_H \ No newline at end of file diff --git a/test/systemtest/src/power_mgr_mock_system_test.cpp b/test/systemtest/src/power_mgr_mock_system_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8b3f6e20942fcf789c6a5af366bd14f020acb6c4 --- /dev/null +++ b/test/systemtest/src/power_mgr_mock_system_test.cpp @@ -0,0 +1,269 @@ +/* + * Copyright (c) 2021 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 "power_mgr_mock_system_test.h" + +using namespace testing::ext; +using namespace OHOS::PowerMgr; +using namespace OHOS; +using namespace std; +using ::testing::_; + +static sptr service; +static MockStateAction* stateAction; +static MockPowerAction* powerAction; +static MockLockAction* lockAction; + +static void ResetMockAction() +{ + POWER_HILOGD(MODULE_SERVICE, "ResetMockAction:Start."); + stateAction = new MockStateAction(); + powerAction = new MockPowerAction(); + lockAction = new MockLockAction(); + service->EnableMock(stateAction, powerAction, lockAction); +} + +void PowerMgrMockSystemTest::SetUpTestCase(void) +{ + // create singleton service object at the beginning + service = DelayedSpSingleton::GetInstance(); + service->OnStart(); + ResetMockAction(); +} + +void PowerMgrMockSystemTest::TearDownTestCase(void) +{ + service->OnStop(); + DelayedSpSingleton::DestroyInstance(); + delete stateAction; + delete powerAction; + delete lockAction; +} + +void PowerMgrMockSystemTest::SetUp(void) +{ +} + +void PowerMgrMockSystemTest::TearDown(void) +{ +} + +namespace { +/** + * @tc.name: PowerMgrMock102 + * @tc.desc: test SuspendDevice by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockSystemTest, PowerMgrMock102, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock102: start."; + + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock102:Start."); + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock102: Failed to get PowerMgrService"; + } + + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock102:Start mock."); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_SUSPEND)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_CALL(*stateAction, + Suspend(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false)); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock102:Start suspend."); + pms->SetDisplaySuspend(true); + pms->SuspendDevice(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock102:end."); + GTEST_LOG_(INFO) << "PowerMgrMock102: end."; +} + +/** + * @tc.name: PowerMgrMock103 + * @tc.desc: test SuspendDevice and auto sleep by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockSystemTest, PowerMgrMock103, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock103: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock103:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock022: Failed to get PowerMgrService"; + } + + pms->WakeupDevice(0, WakeupDeviceType::WAKEUP_DEVICE_UNKNOWN, std::string("test")); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_SUSPEND)) + .Times(::testing::AtLeast(1)) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_CALL(*stateAction, + Suspend(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false)); + pms->SetDisplaySuspend(true); + pms->SuspendDevice(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false); + sleep(SLEEP_WAIT_TIME_S + 1); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock103:End."); + GTEST_LOG_(INFO) << "PowerMgrMock103: end."; +} + +/** + * @tc.name: PowerMgrMock104 + * @tc.desc: test WakeupDevice and auto suspend by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockSystemTest, PowerMgrMock104, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock104: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock104:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock104: Failed to get PowerMgrService"; + } + + pms->WakeupDevice(0, WakeupDeviceType::WAKEUP_DEVICE_UNKNOWN, std::string("test")); + pms->SetDisplaySuspend(true); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_ON)); + sleep((SCREEN_OFF_WAIT_TIME_S*2/3) + 1); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_DIM)); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_SUSPEND)) + .Times(::testing::AtLeast(1)) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + sleep((SCREEN_OFF_WAIT_TIME_S*1/3) + 1); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock104:End."); + GTEST_LOG_(INFO) << "PowerMgrMock104: end."; +} + +/** + * @tc.name: PowerMgrMock105 + * @tc.desc: test Auto SuspendDevice by mock after 30s + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockSystemTest, PowerMgrMock105, TestSize.Level2) +{ + int64_t time =30000; + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock105: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock105:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock105: Failed to get PowerMgrService"; + } + + pms->SetDisplayOffTime(time); + pms->SetDisplaySuspend(true); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_ON)); + sleep(((time/1000)*2/3)+1); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_DIM)); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_SUSPEND)) + .Times(::testing::AtLeast(1)) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + sleep(((time/1000)*1/3)+1); + + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock105:End"); + pms->SetDisplayOffTime(DEFAULT_DISPLAY_OFF_TIME); + GTEST_LOG_(INFO) << "PowerMgrMock105: end."; +} + +/** + * @tc.name: PowerMgrMock106 + * @tc.desc: test proximity RunningLock by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockSystemTest, PowerMgrMock106, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock106: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock106:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock106: Failed to get PowerMgrService"; + } + + sptr token = new RunningLockTokenStub(); + RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL); + pms->CreateRunningLock(token, info); + pms->SetDisplaySuspend(true); + pms->Lock(token, info, 0); + EXPECT_EQ(pms->IsUsed(token), true); + sleep(SLEEP_WAIT_TIME_S*10); + EXPECT_EQ(PowerState::AWAKE, pms->GetState()); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_SUSPEND)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_CALL(*stateAction, GoToSleep(_, _, _)).Times(0); + pms->MockProximity(RunningLockMgr::PROXIMITY_CLOSE); + EXPECT_EQ(PowerState::INACTIVE, pms->GetState()); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock106:End."); + GTEST_LOG_(INFO) << "PowerMgrMock106: end."; +} + +/** + * @tc.name: PowerMgrMock107 + * @tc.desc: test background RunningLock by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrMockSystemTest, PowerMgrMock107, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock107: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock107:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock068: Failed to get PowerMgrService"; + } + + sptr token = new RunningLockTokenStub(); + RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_BACKGROUND); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_ON)); + sleep((SCREEN_OFF_WAIT_TIME_S*2/3) + 1); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_DIM)); + pms->SetDisplaySuspend(true); + pms->CreateRunningLock(token, info); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_SUSPEND)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_CALL(*stateAction, GoToSleep(_, _, _)).Times(0); + pms->Lock(token, info, 0); + EXPECT_EQ(pms->IsUsed(token), true); + sleep((SCREEN_OFF_WAIT_TIME_S*1/3) + 1); + pms->UnLock(token); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock107:End."); + GTEST_LOG_(INFO) << "PowerMgrMock107: end."; +} +} \ No newline at end of file diff --git a/test/systemtest/src/power_mgr_powersavemode_test.cpp b/test/systemtest/src/power_mgr_powersavemode_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2052122ad0311f697ed5bbbe7131f05c799a22c3 --- /dev/null +++ b/test/systemtest/src/power_mgr_powersavemode_test.cpp @@ -0,0 +1,579 @@ +/* + * Copyright (c) 2021 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 +#include +#include +#include +#include + +#include "power_common.h" +#include "power_mgr_client.h" +#include "power_mgr_service.h" +#include "power_state_machine.h" +#include "sys_param.h" +#include "power_state_machine_info.h" +#include "display_info.h" +#include "running_lock_info.h" +#include "running_lock.h" +#include "ipower_mode_callback.h" +#include "common_event_manager.h" +#include "power_mgr_powersavemode_test.h" + +using namespace testing::ext; +using namespace OHOS::PowerMgr; +using namespace OHOS::EventFwk; +using namespace OHOS; +using namespace std; + +void PowerMgrPowerSavemodeTest::PowerModeTest1Callback::PowerModeCallback() +{ + POWER_HILOGD(MODULE_SERVICE, "PowerModeTest1Callback::PowerModeCallback."); +} + +void PowerMgrPowerSavemodeTest::SetUpTestCase(void) +{ +} + +void PowerMgrPowerSavemodeTest::TearDownTestCase(void) +{ +} + +void PowerMgrPowerSavemodeTest::SetUp(void) +{ +} + +void PowerMgrPowerSavemodeTest::TearDown(void) +{ +} + +namespace { +/** + * @tc.name: PowerSavemode_001 + * @tc.desc: test SetDeviceMode in proxy + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrPowerSavemodeTest, PowerSavemode_001, TestSize.Level2) +{ + sleep(SLEEP_WAIT_TIME_S); + + GTEST_LOG_(INFO) << "PowerSavemode_001: SetDeviceMode start."; + + auto& powerMgrClient = PowerMgrClient::GetInstance(); + uint32_t mode = 601; + uint32_t mode1 = 601; + powerMgrClient.SetDeviceMode(mode); + sleep(6); + mode = powerMgrClient.GetDeviceMode(); + EXPECT_EQ(mode, mode1) << "PowerSavemode_001 fail to SetDeviceMode"; + GTEST_LOG_(INFO) << "PowerSavemode_001: SetDeviceMode end."; +} + + +/** + * @tc.name: PowerSavemode_002 + * @tc.desc: test SetDeviceMode in proxy + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrPowerSavemodeTest, PowerSavemode_002, TestSize.Level2) +{ + sleep(SLEEP_WAIT_TIME_S); + + GTEST_LOG_(INFO) << "PowerSavemode_002: SetDeviceMode start."; + + auto& powerMgrClient = PowerMgrClient::GetInstance(); + uint32_t mode = 602; + uint32_t mode1 = 602; + powerMgrClient.SetDeviceMode(mode); + sleep(3); + mode = powerMgrClient.GetDeviceMode(); + EXPECT_EQ(mode, mode1) << "PowerSavemode_002 fail to SetDeviceMode"; + GTEST_LOG_(INFO) << "PowerSavemode_002: SetDeviceMode end." << mode; +} + +/** + * @tc.name: PowerSavemode_003 + * @tc.desc: test SetDeviceMode in proxy + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrPowerSavemodeTest, PowerSavemode_003, TestSize.Level2) +{ + sleep(SLEEP_WAIT_TIME_S); + + GTEST_LOG_(INFO) << "PowerSavemode_003: SetDeviceMode start."; + + auto& powerMgrClient = PowerMgrClient::GetInstance(); + uint32_t mode = 603; + uint32_t mode1 = 603; + powerMgrClient.SetDeviceMode(mode); + mode = powerMgrClient.GetDeviceMode(); + EXPECT_EQ(mode, mode1) << "PowerSavemode_003 fail to SetDeviceMode"; + GTEST_LOG_(INFO) << "PowerSavemode_003: SetDeviceMode end."; +} + +/** + * @tc.name: PowerSavemode_028 + * @tc.desc: test SetDeviceMode in proxy + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrPowerSavemodeTest, PowerSavemode_028, TestSize.Level2) +{ + sleep(SLEEP_WAIT_TIME_S); + + GTEST_LOG_(INFO) << "PowerSavemode_028: SetDeviceMode start."; + + auto& powerMgrClient = PowerMgrClient::GetInstance(); + uint32_t mode = 1; + uint32_t mode1 = 1; + powerMgrClient.SetDeviceMode(mode); + mode = powerMgrClient.GetDeviceMode(); + EXPECT_NE(mode, mode1) << "PowerSavemode_028 fail to SetDeviceMode abnormal"; + GTEST_LOG_(INFO) << "PowerSavemode_028: SetDeviceMode end."; +} + +/** + * @tc.name: PowerSavemode_029 + * @tc.desc: test SetDeviceMode in proxy + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrPowerSavemodeTest, PowerSavemode_029, TestSize.Level2) +{ + sleep(SLEEP_WAIT_TIME_S); + + GTEST_LOG_(INFO) << "PowerSavemode_029: SetDeviceMode start."; + + auto& powerMgrClient = PowerMgrClient::GetInstance(); + uint32_t mode = 603; + uint32_t mode1 = 603; + for (int i=0; i<100; i++) + { + powerMgrClient.SetDeviceMode(mode); + } + mode = powerMgrClient.GetDeviceMode(); + EXPECT_EQ(mode, mode1) << "PowerSavemode_029 fail to SetDeviceMode"; + GTEST_LOG_(INFO) << "PowerSavemode_029: SetDeviceMode end."; +} + +/** + * @tc.name: PowerSavemode_004 + * @tc.desc: test GetDeviceMode in proxy + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrPowerSavemodeTest, PowerSavemode_004, TestSize.Level2) +{ + sleep(SLEEP_WAIT_TIME_S); + + GTEST_LOG_(INFO) << "PowerSavemode_004: GetDeviceMode start."; + + auto& powerMgrClient = PowerMgrClient::GetInstance(); + uint32_t mode = 601; + uint32_t mode1 = 601; + powerMgrClient.SetDeviceMode(mode); + mode = powerMgrClient.GetDeviceMode(); + EXPECT_EQ(mode, mode1) << "PowerSavemode_004 fail to GetDeviceMode"; + GTEST_LOG_(INFO) << "PowerSavemode_004: GetDeviceMode end. mode == " << mode; +} + +/** + * @tc.name: PowerSavemode_005 + * @tc.desc: test GetDeviceMode in proxy + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrPowerSavemodeTest, PowerSavemode_005, TestSize.Level2) +{ + sleep(SLEEP_WAIT_TIME_S); + + GTEST_LOG_(INFO) << "PowerSavemode_005: GetDeviceMode start."; + + auto& powerMgrClient = PowerMgrClient::GetInstance(); + uint32_t mode = 602; + uint32_t mode1 = 602; + powerMgrClient.SetDeviceMode(mode); + mode = powerMgrClient.GetDeviceMode(); + EXPECT_EQ(mode, mode1) << "PowerSavemode_005 fail to GetDeviceMode"; + GTEST_LOG_(INFO) << "PowerSavemode_005: GetDeviceMode end. mode == " << mode; +} + +/** + * @tc.name: PowerSavemode_006 + * @tc.desc: test GetDeviceMode in proxy + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrPowerSavemodeTest, PowerSavemode_006, TestSize.Level2) +{ + sleep(SLEEP_WAIT_TIME_S); + + GTEST_LOG_(INFO) << "PowerSavemode_006: GetDeviceMode start."; + + auto& powerMgrClient = PowerMgrClient::GetInstance(); + uint32_t mode = 603; + uint32_t mode1 = 603; + powerMgrClient.SetDeviceMode(mode); + mode = powerMgrClient.GetDeviceMode(); + EXPECT_EQ(mode, mode1) << "PowerSavemode_006 fail to GetDeviceMode"; + GTEST_LOG_(INFO) << "PowerSavemode_006: GetDeviceMode end. mode == " << mode; +} + +/** + * @tc.name: PowerSavemode_030 + * @tc.desc: test GetDeviceMode in proxy + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrPowerSavemodeTest, PowerSavemode_030, TestSize.Level2) +{ + sleep(SLEEP_WAIT_TIME_S); + + GTEST_LOG_(INFO) << "PowerSavemode_030: GetDeviceMode start."; + + auto& powerMgrClient = PowerMgrClient::GetInstance(); + uint32_t mode = 0; + uint32_t mode1 = 601; + mode = powerMgrClient.GetDeviceMode(); + EXPECT_NE(mode, mode1) << "PowerSavemode_030 fail to GetDeviceMode"; + GTEST_LOG_(INFO) << "PowerSavemode_030: GetDeviceMode end. mode == " << mode; +} + +/** + * @tc.name: PowerSavemode_031 + * @tc.desc: test GetDeviceMode in proxy + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrPowerSavemodeTest, PowerSavemode_031, TestSize.Level2) +{ + sleep(SLEEP_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerSavemode_031: GetDeviceMode start."; + + auto& powerMgrClient = PowerMgrClient::GetInstance(); + uint32_t mode = 601; + uint32_t mode1 = 601; + powerMgrClient.SetDeviceMode(mode); + for (int i=0; i<100; i++) + { + mode = powerMgrClient.GetDeviceMode(); + } + EXPECT_EQ(mode, mode1) << "PowerSavemode_031 fail to GetDeviceMode"; + GTEST_LOG_(INFO) << "PowerSavemode_031: GetDeviceMode end. mode == " << mode; +} + +/** + * @tc.name: PowerSavemode_032 + * @tc.desc: test PowerModeCallback + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrPowerSavemodeTest, PowerSavemode_032, TestSize.Level0) +{ + GTEST_LOG_(INFO) << "PowerSavemode_032: RegisterPowerModeCallback start."; + + auto& powerMgrClient = PowerMgrClient::GetInstance(); + const sptr cb1 = new PowerModeTest1Callback(); + powerMgrClient.RegisterPowerModeCallback(cb1); + sleep(SLEEP_WAIT_TIME_S); + uint32_t mode = 601; + uint32_t mode1 = 601; + powerMgrClient.SetDeviceMode(mode); + mode = powerMgrClient.GetDeviceMode(); + EXPECT_EQ(mode, mode1) << "PowerSavemode_032 fail to PowerModeCallback"; + + POWER_HILOGD(MODULE_SERVICE, "PowerSavemode_032 1."); +} + +/** + * @tc.name: PowerSavemode_033 + * @tc.desc: test PowerModeCallback + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrPowerSavemodeTest, PowerSavemode_033, TestSize.Level0) +{ + GTEST_LOG_(INFO) << "PowerSavemode_033: RegisterPowerModeCallback start."; + + auto& powerMgrClient = PowerMgrClient::GetInstance(); + POWER_HILOGD(MODULE_SERVICE, "PowerSavemode_033 Start."); + const sptr cb1 = new PowerModeTest1Callback(); + powerMgrClient.RegisterPowerModeCallback(cb1); + sleep(SLEEP_WAIT_TIME_S); + uint32_t mode = 1; + uint32_t mode1 = 1; + powerMgrClient.SetDeviceMode(mode); + mode = powerMgrClient.GetDeviceMode(); + EXPECT_NE(mode, mode1) << "PowerSavemode_033 fail to PowerModeCallback"; + + POWER_HILOGD(MODULE_SERVICE, "PowerSavemode_033 1."); +} + +/** + * @tc.name: PowerSavemode_034 + * @tc.desc: test PowerModeCallback + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrPowerSavemodeTest, PowerSavemode_034, TestSize.Level0) +{ + GTEST_LOG_(INFO) << "PowerSavemode_034: RegisterPowerModeCallback start."; + + auto& powerMgrClient = PowerMgrClient::GetInstance(); + const sptr cb1 = new PowerModeTest1Callback(); + powerMgrClient.RegisterPowerModeCallback(cb1); + sleep(SLEEP_WAIT_TIME_S); + uint32_t mode = 601; + uint32_t mode1 = 601; + powerMgrClient.SetDeviceMode(mode); + mode = powerMgrClient.GetDeviceMode(); + EXPECT_EQ(mode, mode1) << "PowerSavemode_034 fail to PowerModeCallback"; + + POWER_HILOGD(MODULE_SERVICE, "PowerSavemode_034 1."); +} + +/** + * @tc.name: PowerSavemode_035 + * @tc.desc: test PowerModeCallback + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrPowerSavemodeTest, PowerSavemode_035, TestSize.Level0) +{ + GTEST_LOG_(INFO) << "PowerSavemode_035: RegisterPowerModeCallback start."; + + auto& powerMgrClient = PowerMgrClient::GetInstance(); + const sptr cb1 = new PowerModeTest1Callback(); + for (int i=0; i<100; i++) + { + powerMgrClient.RegisterPowerModeCallback(cb1); + } + sleep(SLEEP_WAIT_TIME_S); + uint32_t mode = 601; + uint32_t mode1 = 601; + powerMgrClient.SetDeviceMode(mode); + mode = powerMgrClient.GetDeviceMode(); + EXPECT_EQ(mode, mode1) << "PowerSavemode_035 fail to PowerModeCallback"; + + POWER_HILOGD(MODULE_SERVICE, "PowerSavemode_035 1."); +} + +/** + * @tc.name: PowerSavemode_036 + * @tc.desc: test PowerModeCallback + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrPowerSavemodeTest, PowerSavemode_036, TestSize.Level0) +{ + GTEST_LOG_(INFO) << "PowerSavemode_036: UnRegisterPowerModeCallback start."; + + auto& powerMgrClient = PowerMgrClient::GetInstance(); + const sptr cb1 = new PowerModeTest1Callback(); + powerMgrClient.UnRegisterPowerModeCallback(cb1); + sleep(SLEEP_WAIT_TIME_S); + uint32_t mode = 601; + uint32_t mode1 = 601; + powerMgrClient.SetDeviceMode(mode); + mode = powerMgrClient.GetDeviceMode(); + EXPECT_EQ(mode, mode1) << "PowerSavemode_036 fail to PowerModeCallback"; + + POWER_HILOGD(MODULE_SERVICE, "PowerSavemode_036 1."); +} + +/** + * @tc.name: PowerSavemode_037 + * @tc.desc: test PowerModeCallback + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrPowerSavemodeTest, PowerSavemode_037, TestSize.Level0) +{ + GTEST_LOG_(INFO) << "PowerSavemode_037: UnRegisterPowerModeCallback start."; + + auto& powerMgrClient = PowerMgrClient::GetInstance(); + const sptr cb1 = new PowerModeTest1Callback(); + powerMgrClient.UnRegisterPowerModeCallback(cb1); + sleep(SLEEP_WAIT_TIME_S); + uint32_t mode = 1; + uint32_t mode1 = 1; + powerMgrClient.SetDeviceMode(mode); + mode = powerMgrClient.GetDeviceMode(); + EXPECT_NE(mode, mode1) << "PowerSavemode_036 fail to PowerModeCallback"; + + POWER_HILOGD(MODULE_SERVICE, "PowerSavemode_037 1."); +} + +/** + * @tc.name: PowerSavemode_038 + * @tc.desc: test PowerModeCallback + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrPowerSavemodeTest, PowerSavemode_038, TestSize.Level0) +{ + GTEST_LOG_(INFO) << "PowerSavemode_038: UnRegisterPowerModeCallback start."; + + auto& powerMgrClient = PowerMgrClient::GetInstance(); + const sptr cb1 = new PowerModeTest1Callback(); + powerMgrClient.UnRegisterPowerModeCallback(cb1); + sleep(SLEEP_WAIT_TIME_S); + uint32_t mode = 601; + uint32_t mode1 = 601; + powerMgrClient.SetDeviceMode(mode); + mode = powerMgrClient.GetDeviceMode(); + EXPECT_EQ(mode, mode1) << "PowerSavemode_036 fail to PowerModeCallback"; + + POWER_HILOGD(MODULE_SERVICE, "PowerSavemode_038 1."); +} + +/** + * @tc.name: PowerSavemode_039 + * @tc.desc: test PowerModeCallback + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrPowerSavemodeTest, PowerSavemode_039, TestSize.Level0) +{ + GTEST_LOG_(INFO) << "PowerSavemode_039: UnRegisterPowerModeCallback start."; + + auto& powerMgrClient = PowerMgrClient::GetInstance(); + const sptr cb1 = new PowerModeTest1Callback(); + for (int i=0; i<100; i++) + { + powerMgrClient.UnRegisterPowerModeCallback(cb1); + } + sleep(SLEEP_WAIT_TIME_S); + uint32_t mode = 601; + uint32_t mode1 = 601; + powerMgrClient.SetDeviceMode(mode); + mode = powerMgrClient.GetDeviceMode(); + EXPECT_EQ(mode, mode1) << "PowerSavemode_036 fail to PowerModeCallback"; + + POWER_HILOGD(MODULE_SERVICE, "PowerSavemode_039 1."); +} +} + +PowerMgrPowerSavemodeTest::CommonEventServiCesSystemTest::CommonEventServiCesSystemTest( + const CommonEventSubscribeInfo &subscriberInfo) + : CommonEventSubscriber(subscriberInfo) +{} + +uint32_t i = 0; +int judgeNum = 2; + +void PowerMgrPowerSavemodeTest::CommonEventServiCesSystemTest::OnReceiveEvent(const CommonEventData &data) +{ + std::string action = data.GetWant().GetAction(); + if (action == CommonEventSupport::COMMON_EVENT_DEVICE_IDLE_MODE_CHANGED) { + POWER_HILOGD(MODULE_SERVICE, "CommonEventServiCesSystemTest::OnReceiveEvent."); + i = judgeNum; + } + uint32_t j = 2; + EXPECT_EQ(i, j) << "PowerSavemode_022 fail to PowerModeCallback"; + POWER_HILOGD(MODULE_SERVICE, "CommonEventServiCesSystemTest::OnReceiveEvent other."); +} + +namespace { +/** + * @tc.name: PowerSavemode_022 + * @tc.desc: ReceiveEvent + * @tc.type: FUNC + */ +HWTEST_F(PowerMgrPowerSavemodeTest, PowerSavemode_022, TestSize.Level0) +{ + GTEST_LOG_(INFO) << "PowerSavemode_022: UnRegisterPowerModeCallback start."; + + bool result = false; + MatchingSkills matchingSkills; + matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_DEVICE_IDLE_MODE_CHANGED); + CommonEventSubscribeInfo subscribeInfo(matchingSkills); + auto subscriberPtr = std::make_shared(subscribeInfo); + result = CommonEventManager::SubscribeCommonEvent(subscriberPtr); + EXPECT_TRUE(result); + GTEST_LOG_(INFO) << "PowerSavemode_022: ShutDownDevice start."; + auto& powerMgrClient = PowerMgrClient::GetInstance(); + uint32_t mode = 602; + powerMgrClient.SetDeviceMode(mode); + sleep(SLEEP_WAIT_TIME_S); + CommonEventManager::UnSubscribeCommonEvent(subscriberPtr); + + POWER_HILOGD(MODULE_SERVICE, "PowerSavemode_022 1."); +} + +/** + * @tc.name: PowerSavemode_023 + * @tc.desc: ReceiveEvent + * @tc.type: FUNC + */ +HWTEST_F(PowerMgrPowerSavemodeTest, PowerSavemode_023, TestSize.Level0) +{ + GTEST_LOG_(INFO) << "PowerSavemode_023: UnRegisterPowerModeCallback start."; + + bool result = false; + MatchingSkills matchingSkills; + matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_DEVICE_IDLE_MODE_CHANGED); + CommonEventSubscribeInfo subscribeInfo(matchingSkills); + auto subscriberPtr = std::make_shared(subscribeInfo); + result = CommonEventManager::SubscribeCommonEvent(subscriberPtr); + EXPECT_TRUE(result); + GTEST_LOG_(INFO) << "PowerSavemode_023: ShutDownDevice start."; + auto& powerMgrClient = PowerMgrClient::GetInstance(); + uint32_t mode = 603; + powerMgrClient.SetDeviceMode(mode); + sleep(SLEEP_WAIT_TIME_S); + CommonEventManager::UnSubscribeCommonEvent(subscriberPtr); + + POWER_HILOGD(MODULE_SERVICE, "PowerSavemode_023 1."); +} + +/** + * @tc.name: PowerSavemode_024 + * @tc.desc: ReceiveEvent + * @tc.type: FUNC + */ +HWTEST_F(PowerMgrPowerSavemodeTest, PowerSavemode_024, TestSize.Level0) +{ + GTEST_LOG_(INFO) << "PowerSavemode_024: UnRegisterPowerModeCallback start."; + + bool result = false; + MatchingSkills matchingSkills; + matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_DEVICE_IDLE_MODE_CHANGED); + CommonEventSubscribeInfo subscribeInfo(matchingSkills); + auto subscriberPtr = std::make_shared(subscribeInfo); + result = CommonEventManager::SubscribeCommonEvent(subscriberPtr); + EXPECT_TRUE(result); + GTEST_LOG_(INFO) << "PowerSavemode_024: ShutDownDevice start."; + auto& powerMgrClient = PowerMgrClient::GetInstance(); + uint32_t mode = 602; + powerMgrClient.SetDeviceMode(mode); + sleep(SLEEP_WAIT_TIME_S); + CommonEventManager::UnSubscribeCommonEvent(subscriberPtr); + + POWER_HILOGD(MODULE_SERVICE, "PowerSavemode_024 1."); +} + +/** + * @tc.name: PowerSavemode_025 + * @tc.desc: ReceiveEvent + * @tc.type: FUNC + */ +HWTEST_F(PowerMgrPowerSavemodeTest, PowerSavemode_025, TestSize.Level0) +{ + GTEST_LOG_(INFO) << "PowerSavemode_025: UnRegisterPowerModeCallback start."; + + bool result = false; + MatchingSkills matchingSkills; + matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_DEVICE_IDLE_MODE_CHANGED); + CommonEventSubscribeInfo subscribeInfo(matchingSkills); + auto subscriberPtr = std::make_shared(subscribeInfo); + result = CommonEventManager::SubscribeCommonEvent(subscriberPtr); + EXPECT_TRUE(result); + GTEST_LOG_(INFO) << "PowerSavemode_025: ShutDownDevice start."; + auto& powerMgrClient = PowerMgrClient::GetInstance(); + uint32_t mode = 603; + powerMgrClient.SetDeviceMode(mode); + sleep(SLEEP_WAIT_TIME_S); + CommonEventManager::UnSubscribeCommonEvent(subscriberPtr); + + POWER_HILOGD(MODULE_SERVICE, "PowerSavemode_025 1."); +} +} \ No newline at end of file diff --git a/test/systemtest/src/power_mgr_st_mock_test.cpp b/test/systemtest/src/power_mgr_st_mock_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..85ee1fb0cfb81b066c3db525996e4948d567c22c --- /dev/null +++ b/test/systemtest/src/power_mgr_st_mock_test.cpp @@ -0,0 +1,3458 @@ +/* + * Copyright (c) 2021 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 "power_mgr_st_mock_test.h" + +using namespace testing::ext; +using namespace OHOS::PowerMgr; +using namespace OHOS; +using namespace std; +using ::testing::_; + +static sptr service; +static MockStateAction* stateAction; +static MockPowerAction* powerAction; +static MockLockAction* lockAction; + +static void ResetMockAction() +{ + POWER_HILOGD(MODULE_SERVICE, "ResetMockAction:Start."); + stateAction = new MockStateAction(); + powerAction = new MockPowerAction(); + lockAction = new MockLockAction(); + service->EnableMock(stateAction, powerAction, lockAction); +} + +void PowerMgrSTMockTest::SetUpTestCase(void) +{ + // create singleton service object at the beginning + service = DelayedSpSingleton::GetInstance(); + service->OnStart(); + ResetMockAction(); +} + +void PowerMgrSTMockTest::TearDownTestCase(void) +{ + service->OnStop(); + DelayedSpSingleton::DestroyInstance(); + delete stateAction; + delete powerAction; + delete lockAction; +} + +void PowerMgrSTMockTest::SetUp(void) +{ +} + +void PowerMgrSTMockTest::TearDown(void) +{ +} + +namespace { +/** + * @tc.name: PowerMgrMock001 + * @tc.desc: test RebootDevice by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock001, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock001: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock001:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock001: Failed to get PowerMgrService"; + } + + EXPECT_CALL(*powerAction, Reboot(std::string("test"))).Times(1); + pms->RebootDevice(std::string("test")); + sleep(ASYNC_WAIT_TIME_S); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock001:End."); + GTEST_LOG_(INFO) << "PowerMgrMock001: end."; +} + +/** + * @tc.name: PowerMgrMock002 + * @tc.desc: test ShutDownDevice by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock002, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock002: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock002:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock002: Failed to get PowerMgrService"; + } + + EXPECT_CALL(*powerAction, Shutdown(std::string("test"))).Times(1); + pms->ShutDownDevice(std::string("test")); + sleep(ASYNC_WAIT_TIME_S); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock002:End."); + GTEST_LOG_(INFO) << "PowerMgrMock002: end."; +} + + +/** + * @tc.name: PowerMgrMock003 + * @tc.desc: test SuspendDevice by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock003, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock003: start."; + + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock003:Start."); + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock003: Failed to get PowerMgrService"; + } + + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock003:Start mock."); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_CALL(*stateAction, + Suspend(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false)); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock003:Start suspend."); + pms->SuspendDevice(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock003:end."); + GTEST_LOG_(INFO) << "PowerMgrMock003: end."; +} + +/** + * @tc.name: PowerMgrMock004 + * @tc.desc: test SuspendDevice by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock004, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock004: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock004:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock004: Failed to get PowerMgrService"; + } + + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_CALL(*stateAction, + Suspend(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_DEVICE_ADMIN, false)); + pms->SuspendDevice(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_DEVICE_ADMIN, false); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock004:End ."); + GTEST_LOG_(INFO) << "PowerMgrMock004: end."; +} + +/** + * @tc.name: PowerMgrMock005 + * @tc.desc: test SuspendDevice by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock005, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock005: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock005:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock005: Failed to get PowerMgrService"; + } + + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_CALL(*stateAction, + Suspend(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_TIMEOUT, false)); + pms->SuspendDevice(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_TIMEOUT, false); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock005:End"); + GTEST_LOG_(INFO) << "PowerMgrMock005: end."; +} + +/** + * @tc.name: PowerMgrMock006 + * @tc.desc: test SuspendDevice by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock006, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock006: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock006:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock006: Failed to get PowerMgrService"; + } + + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_CALL(*stateAction, + Suspend(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_LID_SWITCH, false)); + pms->SuspendDevice(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_LID_SWITCH, false); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock006:End."); + GTEST_LOG_(INFO) << "PowerMgrMock006: end."; +} + +/** + * @tc.name: PowerMgrMock007 + * @tc.desc: test SuspendDevice by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock007, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock007: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock007:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock007: Failed to get PowerMgrService"; + } + + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_CALL(*stateAction, + Suspend(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_POWER_BUTTON, false)); + pms->SuspendDevice(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_POWER_BUTTON, false); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock007:End."); + GTEST_LOG_(INFO) << "PowerMgrMock007: end."; +} + +/** + * @tc.name: PowerMgrMock008 + * @tc.desc: test SuspendDevice by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock008, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock008: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock008:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock008: Failed to get PowerMgrService"; + } + + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_CALL(*stateAction, + Suspend(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_HDMI, false)); + pms->SuspendDevice(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_HDMI, false); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock008:End."); + GTEST_LOG_(INFO) << "PowerMgrMock008: end."; +} + +/** + * @tc.name: PowerMgrMock009 + * @tc.desc: test SuspendDevice by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock009, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock009: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock009:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock009: Failed to get PowerMgrService"; + } + + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_CALL(*stateAction, + Suspend(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_SLEEP_BUTTON, false)); + pms->SuspendDevice(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_SLEEP_BUTTON, false); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock009:End."); + GTEST_LOG_(INFO) << "PowerMgrMock009: end."; +} + +/** + * @tc.name: PowerMgrMock010 + * @tc.desc: test SuspendDevice by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock010, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock010: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock010:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock010: Failed to get PowerMgrService"; + } + + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_CALL(*stateAction, + Suspend(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_ACCESSIBILITY, false)); + pms->SuspendDevice(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_ACCESSIBILITY, false); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock010:End."); + GTEST_LOG_(INFO) << "PowerMgrMock010: end."; +} + +/** + * @tc.name: PowerMgrMock011 + * @tc.desc: test SuspendDevice by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock011, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock011: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock011:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock011: Failed to get PowerMgrService"; + } + + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_CALL(*stateAction, + Suspend(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_FORCE_SUSPEND, false)); + pms->SuspendDevice(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_FORCE_SUSPEND, false); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock011:End."); + GTEST_LOG_(INFO) << "PowerMgrMock011: end."; +} + +/** + * @tc.name: PowerMgrMock012 + * @tc.desc: test WakeupDevice by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock012, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock012: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock012:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock012: Failed to get PowerMgrService"; + } + + pms->SuspendDevice(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_ON)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_CALL(*stateAction, + Wakeup(0, WakeupDeviceType::WAKEUP_DEVICE_UNKNOWN, + std::string("test"), _)).Times(1); + pms->WakeupDevice(0, WakeupDeviceType::WAKEUP_DEVICE_UNKNOWN, std::string("test")); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock012:End."); + GTEST_LOG_(INFO) << "PowerMgrMock012: end."; +} + +/** + * @tc.name: PowerMgrMock013 + * @tc.desc: test WakeupDevice by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock013, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock013: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock013:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock013: Failed to get PowerMgrService"; + } + + pms->SuspendDevice(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_ON)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_CALL(*stateAction, + Wakeup(0, WakeupDeviceType::WAKEUP_DEVICE_POWER_BUTTON, + std::string("test"), _)).Times(1); + pms->WakeupDevice(0, WakeupDeviceType::WAKEUP_DEVICE_POWER_BUTTON, std::string("test")); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock013:End."); + GTEST_LOG_(INFO) << "PowerMgrMock013: end."; +} + +/** + * @tc.name: PowerMgrMock014 + * @tc.desc: test WakeupDevice by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock014, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock014: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock014:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock014: Failed to get PowerMgrService"; + } + + pms->SuspendDevice(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_ON)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_CALL(*stateAction, + Wakeup(0, WakeupDeviceType::WAKEUP_DEVICE_APPLICATION, + std::string("test"), _)).Times(1); + pms->WakeupDevice(0, WakeupDeviceType::WAKEUP_DEVICE_APPLICATION, std::string("test")); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock014:End."); + GTEST_LOG_(INFO) << "PowerMgrMock014: end."; +} + +/** + * @tc.name: PowerMgrMock015 + * @tc.desc: test WakeupDevice by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock015, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock015: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock015:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock015: Failed to get PowerMgrService"; + } + + pms->SuspendDevice(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_ON)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_CALL(*stateAction, + Wakeup(0, WakeupDeviceType::WAKEUP_DEVICE_PLUGGED_IN, + std::string("test"), _)).Times(1); + pms->WakeupDevice(0, WakeupDeviceType::WAKEUP_DEVICE_PLUGGED_IN, std::string("test")); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock015:End."); + GTEST_LOG_(INFO) << "PowerMgrMock015: end."; +} + +/** + * @tc.name: PowerMgrMock016 + * @tc.desc: test WakeupDevice by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock016, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock016: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock016:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock016: Failed to get PowerMgrService"; + } + + pms->SuspendDevice(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_ON)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_CALL(*stateAction, + Wakeup(0, WakeupDeviceType::WAKEUP_DEVICE_GESTURE, + std::string("test"), _)).Times(1); + pms->WakeupDevice(0, WakeupDeviceType::WAKEUP_DEVICE_GESTURE, std::string("test")); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock016:End."); + GTEST_LOG_(INFO) << "PowerMgrMock016: end."; +} + +/** + * @tc.name: PowerMgrMock017 + * @tc.desc: test WakeupDevice by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock017, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock017: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock017:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock017: Failed to get PowerMgrService"; + } + + pms->SuspendDevice(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_ON)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_CALL(*stateAction, + Wakeup(0, WakeupDeviceType::WAKEUP_DEVICE_CAMERA_LAUNCH, + std::string("test"), _)).Times(1); + pms->WakeupDevice(0, WakeupDeviceType::WAKEUP_DEVICE_CAMERA_LAUNCH, std::string("test")); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock017:End."); + GTEST_LOG_(INFO) << "PowerMgrMock017: end."; +} + +/** + * @tc.name: PowerMgrMock018 + * @tc.desc: test WakeupDevice by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock018, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock018: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock018:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock018: Failed to get PowerMgrService"; + } + + pms->SuspendDevice(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_ON)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_CALL(*stateAction, + Wakeup(0, WakeupDeviceType::WAKEUP_DEVICE_WAKE_KEY, + std::string("test"), _)).Times(1); + pms->WakeupDevice(0, WakeupDeviceType::WAKEUP_DEVICE_WAKE_KEY, std::string("test")); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock018:End."); + GTEST_LOG_(INFO) << "PowerMgrMock018: end."; +} + +/** + * @tc.name: PowerMgrMock019 + * @tc.desc: test WakeupDevice by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock019, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock019: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock019:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock019: Failed to get PowerMgrService"; + } + + pms->SuspendDevice(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_ON)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_CALL(*stateAction, + Wakeup(0, WakeupDeviceType::WAKEUP_DEVICE_WAKE_MOTION, + std::string("test"), _)).Times(1); + pms->WakeupDevice(0, WakeupDeviceType::WAKEUP_DEVICE_WAKE_MOTION, std::string("test")); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock019:End."); + GTEST_LOG_(INFO) << "PowerMgrMock019: end."; +} + +/** + * @tc.name: PowerMgrMock020 + * @tc.desc: test WakeupDevice by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock020, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock020: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock020:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock020: Failed to get PowerMgrService"; + } + + pms->SuspendDevice(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_ON)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_CALL(*stateAction, + Wakeup(0, WakeupDeviceType::WAKEUP_DEVICE_HDMI, + std::string("test"), _)).Times(1); + pms->WakeupDevice(0, WakeupDeviceType::WAKEUP_DEVICE_HDMI, std::string("test")); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock020:End."); + GTEST_LOG_(INFO) << "PowerMgrMock020: end."; +} + +/** + * @tc.name: PowerMgrMock021 + * @tc.desc: test WakeupDevice by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock021, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock021: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock021:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock021: Failed to get PowerMgrService"; + } + + pms->SuspendDevice(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_ON)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_CALL(*stateAction, + Wakeup(0, WakeupDeviceType::WAKEUP_DEVICE_LID, + std::string("test"), _)).Times(1); + pms->WakeupDevice(0, WakeupDeviceType::WAKEUP_DEVICE_LID, std::string("test")); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock021:End."); + GTEST_LOG_(INFO) << "PowerMgrMock021: end."; +} + +/** + * @tc.name: PowerMgrMock022 + * @tc.desc: test SuspendDevice and auto sleep by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock022, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock022: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock022:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock022: Failed to get PowerMgrService"; + } + + pms->WakeupDevice(0, WakeupDeviceType::WAKEUP_DEVICE_UNKNOWN, std::string("test")); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)) + .Times(::testing::AtLeast(1)) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_CALL(*stateAction, + Suspend(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false)); + EXPECT_CALL(*stateAction, GoToSleep(_, _, false)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_OFF)); + pms->SuspendDevice(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false); + sleep(SLEEP_WAIT_TIME_S + 1); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock022:End."); + GTEST_LOG_(INFO) << "PowerMgrMock022: end."; +} + +/** + * @tc.name: PowerMgrMock023 + * @tc.desc: test WakeupDevice and auto suspend and sleep by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock023, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock023: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock023:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock023: Failed to get PowerMgrService"; + } + + EXPECT_CALL(*stateAction, GoToSleep(_, _, false)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_ON)); + pms->WakeupDevice(0, WakeupDeviceType::WAKEUP_DEVICE_LID, std::string("test")); + sleep((SCREEN_OFF_WAIT_TIME_S*2/3) + 1); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_DIM)); + sleep((SCREEN_OFF_WAIT_TIME_S*1/3) + 1); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_OFF)); + sleep(SLEEP_WAIT_TIME_S + 1); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock023:End."); + GTEST_LOG_(INFO) << "PowerMgrMock023: end."; +} + +/** + * @tc.name: PowerMgrMock024 + * @tc.desc: test RefreshActivity by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock024, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock024: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock024:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock024: Failed to get PowerMgrService"; + } + + EXPECT_CALL(*stateAction, GetDisplayState()) + .Times(::testing::AtLeast(1)) + .WillOnce(::testing::Return(DisplayState::DISPLAY_DIM)); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_ON)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_CALL(*stateAction, + RefreshActivity(0, UserActivityType::USER_ACTIVITY_TYPE_TOUCH, _)).Times(1); + pms->RefreshActivity(0, UserActivityType::USER_ACTIVITY_TYPE_TOUCH, true); + + sleep(SCREEN_OFF_WAIT_TIME_S + SLEEP_WAIT_TIME_S + 1); + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock024:End."); + GTEST_LOG_(INFO) << "PowerMgrMock024: end."; +} + +/** + * @tc.name: PowerMgrMock025 + * @tc.desc: test IsScreenOn by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock025, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock025: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock025:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock025: Failed to get PowerMgrService"; + } + + EXPECT_CALL(*stateAction, GetDisplayState()) + .Times(::testing::AtLeast(1)) + .WillOnce(::testing::Return(DisplayState::DISPLAY_ON)); + EXPECT_EQ(pms->IsScreenOn(), true); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock025:End."); + GTEST_LOG_(INFO) << "PowerMgrMock025: end."; +} + +/** + * @tc.name: PowerMgrMock026 + * @tc.desc: test IsScreenOn by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock026, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock026: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock026:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock026: Failed to get PowerMgrService"; + } + + EXPECT_CALL(*stateAction, GetDisplayState()) + .Times(::testing::AtLeast(1)) + .WillOnce(::testing::Return(DisplayState::DISPLAY_DIM)); + EXPECT_EQ(pms->IsScreenOn(), true); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock026:End"); + GTEST_LOG_(INFO) << "PowerMgrMock026: end."; +} + +/** + * @tc.name: PowerMgrMock027 + * @tc.desc: test IsScreenOn by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock027, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock027: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock027:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock027: Failed to get PowerMgrService"; + } + + EXPECT_CALL(*stateAction, GetDisplayState()) + .Times(::testing::AtLeast(1)) + .WillOnce(::testing::Return(DisplayState::DISPLAY_OFF)); + EXPECT_EQ(pms->IsScreenOn(), false); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock027:End."); + GTEST_LOG_(INFO) << "PowerMgrMock027: end."; +} + +/** + * @tc.name: PowerMgrMock028 + * @tc.desc: test IsScreenOn by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock028, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock028: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock028:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock028: Failed to get PowerMgrService"; + } + + EXPECT_CALL(*stateAction, GetDisplayState()) + .Times(::testing::AtLeast(1)) + .WillOnce(::testing::Return(DisplayState::DISPLAY_SUSPEND)); + EXPECT_EQ(pms->IsScreenOn(), false); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock028:End."); + GTEST_LOG_(INFO) << "PowerMgrMock028: end."; +} + +/** + * @tc.name: PowerMgrMock029 + * @tc.desc: test ForceSuspendDevice by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock029, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock029: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock029:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock029: Failed to get PowerMgrService"; + } + + EXPECT_CALL(*stateAction, GoToSleep(_, _, true)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_EQ(pms->ForceSuspendDevice(0), true); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock029:End."); + GTEST_LOG_(INFO) << "PowerMgrMock029: end."; +} + +/** + * @tc.name: PowerMgrMock030 + * @tc.desc: test Screen on RunningLock by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock030, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock030: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock030:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock030: Failed to get PowerMgrService"; + } + + sptr token = new RunningLockTokenStub(); + RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_SCREEN); + pms->CreateRunningLock(token, info); + pms->Lock(token, info, 0); + EXPECT_EQ(pms->IsUsed(token), true); + + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_DIM)).Times(0); + sleep(SCREEN_DIM_WAIT_TIME_S + 1); + + pms->UnLock(token); + EXPECT_EQ(pms->IsUsed(token), false); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock030:End."); + GTEST_LOG_(INFO) << "PowerMgrMock030: end."; +} + +/** + * @tc.name: PowerMgrMock031 + * @tc.desc: test background RunningLock by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock031, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock031: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock031:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock031: Failed to get PowerMgrService"; + } + + sptr token = new RunningLockTokenStub(); + RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_BACKGROUND); + pms->CreateRunningLock(token, info); + + EXPECT_CALL(*lockAction, Lock(_, _)).Times(1); + pms->Lock(token, info, 0); + EXPECT_EQ(pms->IsUsed(token), true); + + EXPECT_CALL(*stateAction, GoToSleep(_, _, _)).Times(0); + pms->SuspendDevice(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false); + sleep(SLEEP_WAIT_TIME_S + 1); + + EXPECT_CALL(*lockAction, Unlock(_, _)).Times(1); + pms->UnLock(token); + EXPECT_EQ(pms->IsUsed(token), false); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock031:End."); + GTEST_LOG_(INFO) << "PowerMgrMock031: end."; +} + +/** + * @tc.name: PowerMgrMock032 + * @tc.desc: test proximity RunningLock by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock032, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock032: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock032:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock032: Failed to get PowerMgrService"; + } + + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_DIM)).Times(0); + sptr token = new RunningLockTokenStub(); + RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL); + pms->CreateRunningLock(token, info); + + EXPECT_CALL(*lockAction, Lock(_, _)).Times(1); + pms->Lock(token, info, 0); + EXPECT_EQ(pms->IsUsed(token), true); + + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_DIM)).Times(0); + sleep(SCREEN_DIM_WAIT_TIME_S + 1); + + EXPECT_CALL(*lockAction, Unlock(_, _)).Times(1); + pms->UnLock(token); + EXPECT_EQ(pms->IsUsed(token), false); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock032:End."); + GTEST_LOG_(INFO) << "PowerMgrMock032: end."; +} + +/** + * @tc.name: PowerMgrMock033 + * @tc.desc: test RunningLock release by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock033, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock033: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock033:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock033: Failed to get PowerMgrService"; + } + + sptr token = new RunningLockTokenStub(); + RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_BACKGROUND); + pms->CreateRunningLock(token, info); + pms->ReleaseRunningLock(token); + + EXPECT_CALL(*lockAction, Lock(_, _)).Times(0); + pms->Lock(token, info, 0); + EXPECT_EQ(pms->IsUsed(token), false); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock033:End."); + GTEST_LOG_(INFO) << "PowerMgrMock033: end."; +} + +/** + * @tc.name: PowerMgrMock034 + * @tc.desc: test RefreshActivity by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock034, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock034: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock034:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock034: Failed to get PowerMgrService"; + } + + EXPECT_CALL(*stateAction, GetDisplayState()) + .Times(::testing::AtLeast(1)) + .WillOnce(::testing::Return(DisplayState::DISPLAY_DIM)); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_ON)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_CALL(*stateAction, + RefreshActivity(0, UserActivityType::USER_ACTIVITY_TYPE_OTHER, _)).Times(1); + pms->RefreshActivity(0, UserActivityType::USER_ACTIVITY_TYPE_OTHER, true); + + sleep(SCREEN_OFF_WAIT_TIME_S + SLEEP_WAIT_TIME_S + 1); + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock034:End."); + GTEST_LOG_(INFO) << "PowerMgrMock034: end."; +} + +/** + * @tc.name: PowerMgrMock035 + * @tc.desc: test RefreshActivity by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock035, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock035: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock035:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock035: Failed to get PowerMgrService"; + } + + EXPECT_CALL(*stateAction, GetDisplayState()) + .Times(::testing::AtLeast(1)) + .WillOnce(::testing::Return(DisplayState::DISPLAY_DIM)); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_ON)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_CALL(*stateAction, + RefreshActivity(0, UserActivityType::USER_ACTIVITY_TYPE_BUTTON, _)).Times(1); + pms->RefreshActivity(0, UserActivityType::USER_ACTIVITY_TYPE_BUTTON, true); + + sleep(SCREEN_OFF_WAIT_TIME_S + SLEEP_WAIT_TIME_S + 1); + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock035:End."); + GTEST_LOG_(INFO) << "PowerMgrMock035: end."; +} + +/** + * @tc.name: PowerMgrMock036 + * @tc.desc: test RefreshActivity by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock036, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock036: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock036:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock036: Failed to get PowerMgrService"; + } + + EXPECT_CALL(*stateAction, GetDisplayState()) + .Times(::testing::AtLeast(1)) + .WillOnce(::testing::Return(DisplayState::DISPLAY_DIM)); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_ON)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_CALL(*stateAction, + RefreshActivity(0, UserActivityType::USER_ACTIVITY_TYPE_ACCESSIBILITY, _)).Times(1); + pms->RefreshActivity(0, UserActivityType::USER_ACTIVITY_TYPE_ACCESSIBILITY, true); + + sleep(SCREEN_OFF_WAIT_TIME_S + SLEEP_WAIT_TIME_S + 1); + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock036:End."); + GTEST_LOG_(INFO) << "PowerMgrMock036: end."; +} + +/** + * @tc.name: PowerMgrMock037 + * @tc.desc: test RefreshActivity by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock037, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock037: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock037:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock037: Failed to get PowerMgrService"; + } + + EXPECT_CALL(*stateAction, GetDisplayState()) + .Times(::testing::AtLeast(1)) + .WillOnce(::testing::Return(DisplayState::DISPLAY_DIM)); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_ON)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_CALL(*stateAction, + RefreshActivity(0, UserActivityType::USER_ACTIVITY_TYPE_ATTENTION, _)).Times(1); + pms->RefreshActivity(0, UserActivityType::USER_ACTIVITY_TYPE_ATTENTION, true); + + sleep(SCREEN_OFF_WAIT_TIME_S + SLEEP_WAIT_TIME_S + 1); + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock037:End."); + GTEST_LOG_(INFO) << "PowerMgrMock037: end."; +} + +/** + * @tc.name: PowerMgrMock038 + * @tc.desc: test RefreshActivity by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock038, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock038: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock038:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock038: Failed to get PowerMgrService"; + } + + EXPECT_CALL(*stateAction, GetDisplayState()) + .Times(::testing::AtLeast(1)) + .WillOnce(::testing::Return(DisplayState::DISPLAY_DIM)); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_ON)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_CALL(*stateAction, + RefreshActivity(0, UserActivityType::USER_ACTIVITY_TYPE_SOFTWARE, _)).Times(1); + pms->RefreshActivity(0, UserActivityType::USER_ACTIVITY_TYPE_SOFTWARE, true); + + sleep(SCREEN_OFF_WAIT_TIME_S + SLEEP_WAIT_TIME_S + 1); + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock038:End."); + GTEST_LOG_(INFO) << "PowerMgrMock038: end."; +} + +/** + * @tc.name: PowerMgrMock039 + * @tc.desc: test RefreshActivity by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock039, TestSize.Level2) +{ + UserActivityType abnormaltype= {}; + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock039: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock039:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock039: Failed to get PowerMgrService"; + } + pms->WakeupDevice(0, WakeupDeviceType::WAKEUP_DEVICE_UNKNOWN, std::string("test")); + EXPECT_CALL(*stateAction, GetDisplayState()) + .Times(::testing::AtLeast(1)) + .WillOnce(::testing::Return(DisplayState::DISPLAY_DIM)); + EXPECT_CALL(*stateAction, + RefreshActivity(0, _, _)).Times(1); + pms->RefreshActivity(0, abnormaltype, true); + + sleep(SLEEP_WAIT_TIME_S + 1); + EXPECT_EQ(pms->IsScreenOn(), false); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock039:End."); + GTEST_LOG_(INFO) << "PowerMgrMock039: end."; +} + +/** + * @tc.name: PowerMgrMock040 + * @tc.desc: test RefreshActivity by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock040, TestSize.Level2) +{ + UserActivityType abnormaltype=UserActivityType(6); + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock040: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock040:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock040: Failed to get PowerMgrService"; + } + pms->WakeupDevice(0, WakeupDeviceType::WAKEUP_DEVICE_UNKNOWN, std::string("test")); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_ON)); + sleep((SCREEN_OFF_WAIT_TIME_S*2/3) + 1); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_DIM)); + EXPECT_CALL(*stateAction, + RefreshActivity(0, _, _)).Times(0); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)) + .Times(::testing::AtLeast(1)) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + pms->RefreshActivity(0, abnormaltype, true); + sleep((SCREEN_OFF_WAIT_TIME_S*1/3) + 1); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock040:End."); + GTEST_LOG_(INFO) << "PowerMgrMock040: end."; +} + +/** + * @tc.name: PowerMgrMock041 + * @tc.desc: test WakeupDevice by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock041, TestSize.Level2) +{ + WakeupDeviceType abnormaltype=WakeupDeviceType(10); + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock041: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock041:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock041: Failed to get PowerMgrService"; + } + + pms->SuspendDevice(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_ON)) + .Times(0); + EXPECT_CALL(*stateAction, + Wakeup(0, _, + std::string("test"), _)).Times(0); + pms->WakeupDevice(0, abnormaltype, std::string("test")); + EXPECT_EQ(pms->IsScreenOn(), false); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock041:End."); + GTEST_LOG_(INFO) << "PowerMgrMock041: end."; +} + +/** + * @tc.name: PowerMgrMock042 + * @tc.desc: test WakeupDevice by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock042, TestSize.Level2) +{ + WakeupDeviceType abnormaltype=WakeupDeviceType(999); + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock042: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock042:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock042: Failed to get PowerMgrService"; + } + + pms->SuspendDevice(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_ON)) + .Times(0); + EXPECT_CALL(*stateAction, + Wakeup(0, _, + std::string("test"), _)).Times(0); + pms->WakeupDevice(0, abnormaltype, std::string("test")); + EXPECT_EQ(PowerState::INACTIVE, pms->GetState()); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock042:End."); + GTEST_LOG_(INFO) << "PowerMgrMock042: end."; +} + +/** + * @tc.name: PowerMgrMock043 + * @tc.desc: test WakeupDevice and auto suspend by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock043, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock043: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock043:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock043: Failed to get PowerMgrService"; + } + + pms->WakeupDevice(0, WakeupDeviceType::WAKEUP_DEVICE_UNKNOWN, std::string("test")); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_ON)); + sleep((SCREEN_OFF_WAIT_TIME_S*2/3) + 1); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_DIM)); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)) + .Times(::testing::AtLeast(1)) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + sleep((SCREEN_OFF_WAIT_TIME_S*1/3) + 1); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock043:End."); + GTEST_LOG_(INFO) << "PowerMgrMock043: end."; +} + +/** + * @tc.name: PowerMgrMock044 + * @tc.desc: test WakeupDevice and auto suspend by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock044, TestSize.Level2) +{ + WakeupDeviceType abnormaltype = WakeupDeviceType(10); + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock044: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock044:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock044: Failed to get PowerMgrService"; + } + + pms->WakeupDevice(0, WakeupDeviceType::WAKEUP_DEVICE_UNKNOWN, std::string("test")); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_DIM)) + .Times(::testing::AtLeast(1)) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_ON)); + sleep((SCREEN_OFF_WAIT_TIME_S*2/3)/2); + pms->WakeupDevice(0, abnormaltype, std::string("test")); + sleep((SCREEN_OFF_WAIT_TIME_S*2/3)/2 + 1); + + ResetMockAction(); + GTEST_LOG_(INFO) << "PowerMgrMock044: end."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock044:End."); +} + +/** + * @tc.name: PowerMgrMock045 + * @tc.desc: test WakeupDevice and auto suspend by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock045, TestSize.Level2) +{ + WakeupDeviceType abnormaltype = WakeupDeviceType(999); + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock045: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock045:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock045: Failed to get PowerMgrService"; + } + + pms->WakeupDevice(0, WakeupDeviceType::WAKEUP_DEVICE_UNKNOWN, std::string("test")); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_ON)); + sleep((SCREEN_OFF_WAIT_TIME_S*2/3) + 1); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_DIM)); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)) + .Times(::testing::AtLeast(1)) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + sleep((SCREEN_OFF_WAIT_TIME_S*2/3) /2); + pms->WakeupDevice(0, abnormaltype, std::string("test")); + sleep((SCREEN_OFF_WAIT_TIME_S*1/3)/2 + 1); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock045:End."); + GTEST_LOG_(INFO) << "PowerMgrMock045: end."; +} + +/** + * @tc.name: PowerMgrMock046 + * @tc.desc: test RefreshActivity by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock046, TestSize.Level2) +{ + UserActivityType abnormaltype = UserActivityType(6); + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock046: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock046:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock046: Failed to get PowerMgrService"; + } + + EXPECT_CALL(*stateAction, GetDisplayState()) + .Times(::testing::AtLeast(1)) + .WillOnce(::testing::Return(DisplayState::DISPLAY_OFF)); + EXPECT_CALL(*stateAction, + RefreshActivity(0, _, true)).Times(0); + sleep(SCREEN_OFF_WAIT_TIME_S + SLEEP_WAIT_TIME_S/2); + pms->RefreshActivity(0, abnormaltype, true); + + sleep(SCREEN_OFF_WAIT_TIME_S + SLEEP_WAIT_TIME_S/2 + 1); + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock046:End."); + GTEST_LOG_(INFO) << "PowerMgrMock046: end."; +} + +/** + * @tc.name: PowerMgrMock047 + * @tc.desc: test RefreshActivity by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock047, TestSize.Level2) +{ + UserActivityType abnormaltype = {}; + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock047: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock047:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock047: Failed to get PowerMgrService"; + } + + EXPECT_CALL(*stateAction, GetDisplayState()) + .Times(::testing::AtLeast(1)) + .WillOnce(::testing::Return(DisplayState::DISPLAY_OFF)); + EXPECT_CALL(*stateAction, + RefreshActivity(0, _, true)).Times(0); + sleep(SCREEN_OFF_WAIT_TIME_S + SLEEP_WAIT_TIME_S/2); + pms->RefreshActivity(0, abnormaltype, true); + + sleep(SCREEN_OFF_WAIT_TIME_S + SLEEP_WAIT_TIME_S/2 + 1); + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock047:End."); + GTEST_LOG_(INFO) << "PowerMgrMock047: end."; +} + +/** + * @tc.name: PowerMgrMock048 + * @tc.desc: test SuspendDevice by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock048, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock048: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock048:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock048: Failed to get PowerMgrService"; + } + + pms->WakeupDevice(0, WakeupDeviceType::WAKEUP_DEVICE_POWER_BUTTON, std::string("test")); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)) + .Times(::testing::AtLeast(1)) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_CALL(*stateAction, + Suspend(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false)); + EXPECT_CALL(*stateAction, GoToSleep(_, _, _)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + pms->SuspendDevice(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false); + EXPECT_EQ(PowerState::INACTIVE, pms->GetState()); + sleep(SLEEP_WAIT_TIME_S+1); + EXPECT_EQ(PowerState::SLEEP, pms->GetState()); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock048:End"); + GTEST_LOG_(INFO) << "PowerMgrMock048: end."; +} + +/** + * @tc.name: PowerMgrMock049 + * @tc.desc: test SuspendDevice by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock049, TestSize.Level2) +{ + SuspendDeviceType abnormaltype = SuspendDeviceType(10); + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock049: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock049:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock049: Failed to get PowerMgrService"; + } + + pms->WakeupDevice(0, WakeupDeviceType::WAKEUP_DEVICE_POWER_BUTTON, std::string("test")); + EXPECT_CALL(*stateAction, + Suspend(0, _, false)).Times(0); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)).Times(0); + pms->SuspendDevice(0, abnormaltype, false); + EXPECT_EQ(PowerState::AWAKE, pms->GetState()); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock049:End."); + GTEST_LOG_(INFO) << "PowerMgrMock049: end."; +} + +/** + * @tc.name: PowerMgrMock050 + * @tc.desc: test SuspendDevice by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock050, TestSize.Level2) +{ + SuspendDeviceType abnormaltype = SuspendDeviceType(999); + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock050: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock050:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock050: Failed to get PowerMgrService"; + } + + pms->WakeupDevice(0, WakeupDeviceType::WAKEUP_DEVICE_POWER_BUTTON, std::string("test")); + EXPECT_CALL(*stateAction, + Suspend(0, _, false)).Times(0); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)).Times(0); + pms->SuspendDevice(0, abnormaltype, false); + EXPECT_EQ(PowerState::AWAKE, pms->GetState()); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock050:End."); + GTEST_LOG_(INFO) << "PowerMgrMock050: end."; +} + +/** + * @tc.name: PowerMgrMock051 + * @tc.desc: test SuspendDevice by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock051, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock051: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock051:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock051: Failed to get PowerMgrService"; + } + + pms->SuspendDevice(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_CALL(*stateAction, + Suspend(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false)); + EXPECT_CALL(*stateAction, GoToSleep(_, _, false)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + pms->SuspendDevice(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false); + EXPECT_EQ(PowerState::INACTIVE, pms->GetState()); + sleep(SLEEP_WAIT_TIME_S+1); + EXPECT_EQ(PowerState::SLEEP, pms->GetState()); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock051:End."); + GTEST_LOG_(INFO) << "PowerMgrMock051: end."; +} + +/** + * @tc.name: PowerMgrMock052 + * @tc.desc: test SuspendDevice by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock052, TestSize.Level2) +{ + SuspendDeviceType abnormaltype = SuspendDeviceType(10); + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock052: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock052:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock052: Failed to get PowerMgrService"; + } + + pms->SuspendDevice(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false); + EXPECT_CALL(*stateAction, + Suspend(0, _, false)).Times(0); + pms->SuspendDevice(0, abnormaltype, false); + EXPECT_EQ(PowerState::INACTIVE, pms->GetState()); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock052:End."); + GTEST_LOG_(INFO) << "PowerMgrMock052: end."; +} + +/** + * @tc.name: PowerMgrMock053 + * @tc.desc: test SuspendDevice by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock053, TestSize.Level2) +{ + SuspendDeviceType abnormaltype = {}; + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock053: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock053:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock053: Failed to get PowerMgrService"; + } + + pms->SuspendDevice(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false); + EXPECT_CALL(*stateAction, + Suspend(0, _, false)); + pms->SuspendDevice(0, abnormaltype, false); + EXPECT_EQ(PowerState::INACTIVE, pms->GetState()); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock053:End."); + GTEST_LOG_(INFO) << "PowerMgrMock053: end."; +} + +/** + * @tc.name: PowerMgrMock054 + * @tc.desc: test ForceSuspendDevice by mock during Inactive + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock054, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock054: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock054:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock054: Failed to get PowerMgrService"; + } + + pms->SuspendDevice(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false); + EXPECT_CALL(*stateAction, GoToSleep(_, _, _)) + .Times(::testing::AtLeast(1)) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + pms->ForceSuspendDevice(0); + sleep(SLEEP_WAIT_TIME_S+1); + EXPECT_EQ(PowerState::SLEEP, pms->GetState()); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock054:End."); + GTEST_LOG_(INFO) << "PowerMgrMock054: end."; +} + +/** + * @tc.name: PowerMgrMock055 + * @tc.desc: test auto suspend by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock055, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock055: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock055:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock055: Failed to get PowerMgrService"; + } + + pms->WakeupDevice(0, WakeupDeviceType::WAKEUP_DEVICE_LID, std::string("test")); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_ON)); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_DIM)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + sleep((SCREEN_OFF_WAIT_TIME_S*2/3) + 1); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_DIM)); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + sleep((SCREEN_OFF_WAIT_TIME_S*1/3) + 1); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock055:End."); + GTEST_LOG_(INFO) << "PowerMgrMock055: end."; +} + +/** + * @tc.name: PowerMgrMock056 + * @tc.desc: test SCREEN_ON RunningLock by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock056, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock056: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock056:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock056: Failed to get PowerMgrService"; + } + + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_DIM)).Times(0); + sptr token = new RunningLockTokenStub(); + RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_SCREEN); + pms->CreateRunningLock(token, info); + + pms->Lock(token, info, 0); + EXPECT_EQ(pms->IsUsed(token), true); + + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_DIM)).Times(0); + sleep((SCREEN_OFF_WAIT_TIME_S*2/3) + 1); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_ON)); + EXPECT_EQ(PowerState::AWAKE, pms->GetState()); + + pms->UnLock(token); + EXPECT_EQ(pms->IsUsed(token), false); + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock056:End."); + GTEST_LOG_(INFO) << "PowerMgrMock056: end."; +} + +/** + * @tc.name: PowerMgrMock057 + * @tc.desc: test SCREEN_ON RunningLock by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock057, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock057: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock057:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock057: Failed to get PowerMgrService"; + } + + pms->SuspendDevice(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false); + sptr token = new RunningLockTokenStub(); + RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_SCREEN); + pms->CreateRunningLock(token, info); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_ON)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + pms->Lock(token, info, 0); + EXPECT_EQ(pms->IsUsed(token), true); + EXPECT_EQ(PowerState::AWAKE, pms->GetState()); + + pms->UnLock(token); + EXPECT_EQ(pms->IsUsed(token), false); + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock057:End."); + GTEST_LOG_(INFO) << "PowerMgrMock057: end."; +} + +/** + * @tc.name: PowerMgrMock058 + * @tc.desc: test Auto DIM by mock after 10min + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock058, TestSize.Level2) +{ + int64_t time =600000; + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock058: start."; + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock058: Failed to get PowerMgrService"; + } + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_ON)); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_DIM)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + pms->SetDisplayOffTime(time); + sleep(time/1000-2); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_DIM)); + sleep(3); + ResetMockAction(); +pms->SetDisplayOffTime(DEFAULT_DISPLAY_OFF_TIME); + GTEST_LOG_(INFO) << "PowerMgrMock058: end."; +} + +/** + * @tc.name: PowerMgrMock059 + * @tc.desc: test Screen RunningLock by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock059, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock059: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock059:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock059: Failed to get PowerMgrService"; + } + + sptr token = new RunningLockTokenStub(); + RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_SCREEN); + pms->CreateRunningLock(token, info); + pms->Lock(token, info, 0); + EXPECT_EQ(pms->IsUsed(token), true); + + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_DIM)).Times(0); + sleep(SCREEN_DIM_WAIT_TIME_S + 1); + + pms->UnLock(token); + EXPECT_EQ(pms->IsUsed(token), false); + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock059:End."); + GTEST_LOG_(INFO) << "PowerMgrMock059: end."; +} + +/** + * @tc.name: PowerMgrMock060 + * @tc.desc: test Screen RunningLock by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock060, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock060: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock060:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock060: Failed to get PowerMgrService"; + } + + sptr token = new RunningLockTokenStub(); + RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_SCREEN); + pms->CreateRunningLock(token, info); + pms->Lock(token, info, 0); + EXPECT_EQ(pms->IsUsed(token), true); + + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)).Times(0); + sleep(SLEEP_WAIT_TIME_S*10); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)).Times(0); + EXPECT_EQ(PowerState::AWAKE, pms->GetState()); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock060:End."); + GTEST_LOG_(INFO) << "PowerMgrMock060: end."; +} + +/** + * @tc.name: PowerMgrMock061 + * @tc.desc: test Screen on RunningLock by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock061, TestSize.Level2) +{ + int i; + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock061: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock061:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock061: Failed to get PowerMgrService"; + } + + sptr token = new RunningLockTokenStub(); + RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_SCREEN); + pms->CreateRunningLock(token, info); + for (i=0; i<10; i++) { + pms->Lock(token, info, 0); + EXPECT_EQ(pms->IsUsed(token), true); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_DIM)).Times(0); + sleep(SCREEN_OFF_WAIT_TIME_S + 1); + pms->UnLock(token); + EXPECT_EQ(pms->IsUsed(token), false); + EXPECT_EQ(PowerState::AWAKE, pms->GetState()); + } + EXPECT_EQ(PowerState::AWAKE, pms->GetState()); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock061:End."); + GTEST_LOG_(INFO) << "PowerMgrMock061: end."; +} + +/** + * @tc.name: PowerMgrMock062 + * @tc.desc: test Screen RunningLock by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock062, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock062: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock062:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock062: Failed to get PowerMgrService"; + } + + sptr token = new RunningLockTokenStub(); + RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_SCREEN); + pms->CreateRunningLock(token, info); + + pms->Lock(token, info, 0); + EXPECT_EQ(pms->IsUsed(token), true); + + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)) + .Times(::testing::AtLeast(1)) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + + EXPECT_CALL(*stateAction, GoToSleep(_, _, _)).Times(1); + pms->SuspendDevice(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false); + sleep(SLEEP_WAIT_TIME_S + 1); + + pms->UnLock(token); + EXPECT_EQ(pms->IsUsed(token), false); + sleep(SLEEP_WAIT_TIME_S + 1); + EXPECT_EQ(PowerState::SLEEP, pms->GetState()); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock062:End."); + GTEST_LOG_(INFO) << "PowerMgrMock062: end."; +} + +/** + * @tc.name: PowerMgrMock063 + * @tc.desc: test Screen RunningLock by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock063, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock063: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock063:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock063: Failed to get PowerMgrService"; + } + + sptr token = new RunningLockTokenStub(); + RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_SCREEN); + pms->CreateRunningLock(token, info); + + pms->Lock(token, info, 0); + EXPECT_EQ(pms->IsUsed(token), true); + + + EXPECT_CALL(*stateAction, GoToSleep(_, _, true)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_EQ(pms->ForceSuspendDevice(0), true); + sleep(SLEEP_WAIT_TIME_S + 1); + + pms->UnLock(token); + EXPECT_EQ(pms->IsUsed(token), false); + EXPECT_EQ(PowerState::SLEEP, pms->GetState()); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock063:End."); + GTEST_LOG_(INFO) << "PowerMgrMock063: end."; +} + +/** + * @tc.name: PowerMgrMock064 + * @tc.desc: test Screen RunningLock by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock064, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock064: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock063:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock064: Failed to get PowerMgrService"; + } + + sptr token = new RunningLockTokenStub(); + RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_SCREEN); + pms->CreateRunningLock(token, info); + + pms->Lock(token, info, 0); + EXPECT_EQ(pms->IsUsed(token), true); + + EXPECT_CALL(*stateAction, Suspend(_, _, _)).Times(0); + + EXPECT_CALL(*stateAction, + Wakeup(0, WakeupDeviceType::WAKEUP_DEVICE_UNKNOWN, + std::string("test"), _)).Times(1); + pms->WakeupDevice(0, WakeupDeviceType::WAKEUP_DEVICE_UNKNOWN, std::string("test")); + + pms->UnLock(token); + EXPECT_EQ(PowerState::AWAKE, pms->GetState()); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock064:End."); + GTEST_LOG_(INFO) << "PowerMgrMock064: end."; +} + +/** + * @tc.name: PowerMgrMock065 + * @tc.desc: test background RunningLock by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock065, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock065: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock065:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock065: Failed to get PowerMgrService"; + } + + sptr token = new RunningLockTokenStub(); + RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_BACKGROUND); + pms->CreateRunningLock(token, info); + + EXPECT_CALL(*stateAction, GoToSleep(_, _, true)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_EQ(pms->ForceSuspendDevice(0), true); + EXPECT_EQ(PowerState::SLEEP, pms->GetState()); + pms->WakeupDevice(0, WakeupDeviceType::WAKEUP_DEVICE_UNKNOWN, std::string("test")); + pms->Lock(token, info, 0); + EXPECT_EQ(pms->IsUsed(token), true); + EXPECT_CALL(*lockAction, Unlock(_, _)).Times(1); + pms->UnLock(token); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock065:End."); + GTEST_LOG_(INFO) << "PowerMgrMock065: end."; +} + +/** + * @tc.name: PowerMgrMock066 + * @tc.desc: test background RunningLock by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock066, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock066: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock066:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock066: Failed to get PowerMgrService"; + } + + sptr token = new RunningLockTokenStub(); + RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_BACKGROUND); + pms->CreateRunningLock(token, info); + + EXPECT_CALL(*lockAction, Lock(_, _)).Times(1); + pms->Lock(token, info, 0); + EXPECT_EQ(pms->IsUsed(token), true); + + EXPECT_CALL(*lockAction, Unlock(_, _)).Times(1); + pms->UnLock(token); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_ON)); + sleep((SCREEN_OFF_WAIT_TIME_S*2/3) + 1); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_DIM)); + sleep((SCREEN_OFF_WAIT_TIME_S*1/3) + 1); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_OFF)); + EXPECT_EQ(pms->IsUsed(token), false); + EXPECT_CALL(*stateAction, GoToSleep(_, _, _)).Times(1); + sleep(SLEEP_WAIT_TIME_S + 1); + EXPECT_EQ(PowerState::SLEEP, pms->GetState()); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock066:End."); + GTEST_LOG_(INFO) << "PowerMgrMock066: end."; +} + +/** + * @tc.name: PowerMgrMock067 + * @tc.desc: test background RunningLock by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock067, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock067: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock067:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock067: Failed to get PowerMgrService"; + } + + sptr token = new RunningLockTokenStub(); + RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_BACKGROUND); + pms->CreateRunningLock(token, info); + pms->Lock(token, info, 0); + EXPECT_EQ(pms->IsUsed(token), true); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_ON)); + sleep((SCREEN_OFF_WAIT_TIME_S*2/3) + 1); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_DIM)); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + sleep((SCREEN_OFF_WAIT_TIME_S*1/3) + 1); + EXPECT_CALL(*stateAction, GoToSleep(_, _, _)).Times(0); + sleep(SLEEP_WAIT_TIME_S*10); + EXPECT_CALL(*stateAction, GoToSleep(_, _, _)).Times(0); + EXPECT_EQ(PowerState::INACTIVE, pms->GetState()); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock067:End."); + GTEST_LOG_(INFO) << "PowerMgrMock067: end."; +} + +/** + * @tc.name: PowerMgrMock068 + * @tc.desc: test background RunningLock by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock068, TestSize.Level2) +{ + int i; + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock068: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock068:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock068: Failed to get PowerMgrService"; + } + + sptr token = new RunningLockTokenStub(); + RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_BACKGROUND); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_ON)); + sleep((SCREEN_OFF_WAIT_TIME_S*2/3) + 1); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_DIM)); + pms->CreateRunningLock(token, info); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_CALL(*stateAction, GoToSleep(_, _, _)).Times(0); + pms->Lock(token, info, 0); + EXPECT_EQ(pms->IsUsed(token), true); + sleep((SCREEN_OFF_WAIT_TIME_S*1/3) + 1); + pms->UnLock(token); + EXPECT_EQ(pms->IsUsed(token), false); + EXPECT_EQ(PowerState::INACTIVE, pms->GetState()); + for (i=0; i<10; i++) { + pms->Lock(token, info, 0); + EXPECT_EQ(pms->IsUsed(token), true); + sleep(SCREEN_OFF_WAIT_TIME_S + 1); + + pms->UnLock(token); + EXPECT_EQ(pms->IsUsed(token), false); + EXPECT_EQ(PowerState::INACTIVE, pms->GetState()); + } + EXPECT_EQ(PowerState::INACTIVE, pms->GetState()); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock068:End."); + GTEST_LOG_(INFO) << "PowerMgrMock068: end."; +} + +/** + * @tc.name: PowerMgrMock069 + * @tc.desc: test background RunningLock by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock069, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock069: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock069:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock069: Failed to get PowerMgrService"; + } + + sptr token = new RunningLockTokenStub(); + RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_BACKGROUND); + pms->SuspendDevice(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false); + sleep(SLEEP_WAIT_TIME_S); + pms->CreateRunningLock(token, info); + EXPECT_CALL(*lockAction, Lock(_, _)).Times(1); + pms->Lock(token, info, 0); + EXPECT_EQ(pms->IsUsed(token), true); + sleep(SLEEP_WAIT_TIME_S + 1); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_ON)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + pms->WakeupDevice(0, WakeupDeviceType::WAKEUP_DEVICE_UNKNOWN, std::string("test")); + sleep(SLEEP_WAIT_TIME_S + 1); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)).Times(0); + + EXPECT_CALL(*lockAction, Unlock(_, _)).Times(1); + pms->UnLock(token); + EXPECT_EQ(pms->IsUsed(token), false); + EXPECT_EQ(PowerState::AWAKE, pms->GetState()); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock069:End"); + GTEST_LOG_(INFO) << "PowerMgrMock069: end."; +} + +/** + * @tc.name: PowerMgrMock070 + * @tc.desc: test background RunningLock by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock070, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock070: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock070:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock070: Failed to get PowerMgrService"; + } + + sptr token = new RunningLockTokenStub(); + RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_BACKGROUND); + pms->CreateRunningLock(token, info); + + EXPECT_CALL(*lockAction, Lock(_, _)).Times(1); + pms->Lock(token, info, 0); + EXPECT_EQ(pms->IsUsed(token), true); + + sleep(SLEEP_WAIT_TIME_S + 1); + EXPECT_CALL(*stateAction, GoToSleep(_, _, true)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_EQ(pms->ForceSuspendDevice(0), true); + sleep(SLEEP_WAIT_TIME_S + 1); + + EXPECT_CALL(*lockAction, Unlock(_, _)).Times(1); + pms->UnLock(token); + EXPECT_EQ(pms->IsUsed(token), false); + EXPECT_EQ(PowerState::SLEEP, pms->GetState()); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock070:End."); + GTEST_LOG_(INFO) << "PowerMgrMock070: end."; +} + +/** + * @tc.name: PowerMgrMock071 + * @tc.desc: test proximity screen control RunningLock by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock071, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock071: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock071:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock071: Failed to get PowerMgrService"; + } + + sptr token = new RunningLockTokenStub(); + RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL); + pms->CreateRunningLock(token, info); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)) + .Times(::testing::AtLeast(1)) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + pms->SuspendDevice(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false); + EXPECT_EQ(PowerState::INACTIVE, pms->GetState()); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_ON)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + sleep(SCREEN_OFF_WAIT_TIME_S + 1); + pms->Lock(token, info, 0); + EXPECT_EQ(pms->IsUsed(token), true); + EXPECT_EQ(PowerState::AWAKE, pms->GetState()); + + pms->UnLock(token); + EXPECT_EQ(pms->IsUsed(token), false); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock071:End."); + GTEST_LOG_(INFO) << "PowerMgrMock071: end."; +} + + +/** + * @tc.name: PowerMgrMock072 + * @tc.desc: test proximity screen control RunningLock by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock072, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock072: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock072:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock072: Failed to get PowerMgrService"; + } + + sptr token = new RunningLockTokenStub(); + RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL); + pms->CreateRunningLock(token, info); + EXPECT_CALL(*stateAction, GoToSleep(_, _, true)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_EQ(pms->ForceSuspendDevice(0), true); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_ON)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + sleep(SCREEN_OFF_WAIT_TIME_S + 1); + EXPECT_EQ(PowerState::SLEEP, pms->GetState()); + pms->Lock(token, info, 0); + EXPECT_EQ(pms->IsUsed(token), true); + EXPECT_EQ(PowerState::AWAKE, pms->GetState()); + pms->UnLock(token); + EXPECT_EQ(pms->IsUsed(token), false); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock072:End."); + GTEST_LOG_(INFO) << "PowerMgrMock072: end."; +} + +/** + * @tc.name: PowerMgrMock073 + * @tc.desc: test proximity screen control RunningLock by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock073, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock073: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock073:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock073: Failed to get PowerMgrService"; + } + + sptr token = new RunningLockTokenStub(); + RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL); + pms->CreateRunningLock(token, info); + pms->Lock(token, info, 0); + EXPECT_EQ(pms->IsUsed(token), true); + sleep(SLEEP_WAIT_TIME_S); + + pms->UnLock(token); + EXPECT_EQ(pms->IsUsed(token), false); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_ON)); + sleep((SCREEN_OFF_WAIT_TIME_S*2/3) + 1); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_DIM)); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)) + .Times(::testing::AtLeast(1)) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + sleep((SCREEN_OFF_WAIT_TIME_S*1/3) + 1); + EXPECT_EQ(PowerState::INACTIVE, pms->GetState()); + EXPECT_CALL(*stateAction, GoToSleep(_, _, false)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_OFF)); + sleep(SLEEP_WAIT_TIME_S + 1); + EXPECT_EQ(PowerState::SLEEP, pms->GetState()); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock073:End."); + GTEST_LOG_(INFO) << "PowerMgrMock073: end."; +} + +/** + * @tc.name: PowerMgrMock074 + * @tc.desc: test proximity RunningLock by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock074, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock074: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock074:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock074: Failed to get PowerMgrService"; + } + + sptr token = new RunningLockTokenStub(); + RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL); + pms->CreateRunningLock(token, info); + + EXPECT_CALL(*lockAction, Lock(_, _)).Times(1); + pms->Lock(token, info, 0); + EXPECT_EQ(pms->IsUsed(token), true); + sleep(SCREEN_OFF_WAIT_TIME_S+1); + EXPECT_EQ(PowerState::AWAKE, pms->GetState()); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_CALL(*stateAction, GoToSleep(_, _, _)).Times(0); + pms->MockProximity(RunningLockMgr::PROXIMITY_CLOSE); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock074:End."); + GTEST_LOG_(INFO) << "PowerMgrMock074: end."; +} + +/** + * @tc.name: PowerMgrMock075 + * @tc.desc: test proximity RunningLock by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock075, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock075: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock075:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock075: Failed to get PowerMgrService"; + } + + sptr token = new RunningLockTokenStub(); + RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL); + pms->CreateRunningLock(token, info); + pms->Lock(token, info, 0); + EXPECT_EQ(pms->IsUsed(token), true); + sleep(SLEEP_WAIT_TIME_S*10); + EXPECT_EQ(PowerState::AWAKE, pms->GetState()); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_CALL(*stateAction, GoToSleep(_, _, _)).Times(0); + pms->MockProximity(RunningLockMgr::PROXIMITY_CLOSE); + EXPECT_EQ(PowerState::INACTIVE, pms->GetState()); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock075:End."); + GTEST_LOG_(INFO) << "PowerMgrMock075: end."; +} + +/** + * @tc.name: PowerMgrMock076 + * @tc.desc: test proximity RunningLock by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock076, TestSize.Level2) +{ + int i; + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock076: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock076:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock076: Failed to get PowerMgrService"; + } + + sptr token = new RunningLockTokenStub(); + RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL); + pms->CreateRunningLock(token, info); + for (i=0; i<10; i++) { + pms->Lock(token, info, 0); + EXPECT_EQ(pms->IsUsed(token), true); + sleep(SCREEN_OFF_WAIT_TIME_S + 1); + pms->UnLock(token); + EXPECT_EQ(pms->IsUsed(token), false); + EXPECT_EQ(PowerState::AWAKE, pms->GetState()); + } + pms->Lock(token, info, 0); + EXPECT_EQ(pms->IsUsed(token), true); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_CALL(*stateAction, GoToSleep(_, _, _)).Times(0); + pms->MockProximity(RunningLockMgr::PROXIMITY_CLOSE); + EXPECT_EQ(PowerState::INACTIVE, pms->GetState()); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock076:End."); + GTEST_LOG_(INFO) << "PowerMgrMock076: end."; +} + +/** + * @tc.name: PowerMgrMock077 + * @tc.desc: test proximity RunningLock by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock077, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock077: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock077:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock077: Failed to get PowerMgrService"; + } + + sptr token = new RunningLockTokenStub(); + RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL); + pms->CreateRunningLock(token, info); + EXPECT_CALL(*lockAction, Lock(_, _)).Times(1); + pms->Lock(token, info, 0); + EXPECT_EQ(pms->IsUsed(token), true); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)) + .Times(::testing::AtLeast(1)) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_CALL(*stateAction, GoToSleep(_, _, false)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + pms->SuspendDevice(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false); + sleep(SLEEP_WAIT_TIME_S + 1); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_ON)); + sleep((SCREEN_OFF_WAIT_TIME_S*2/3) + 1); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_DIM)); + sleep((SCREEN_OFF_WAIT_TIME_S*1/3) + 1); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_OFF)); + EXPECT_EQ(PowerState::SLEEP, pms->GetState()); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock077:End."); + GTEST_LOG_(INFO) << "PowerMgrMock077: end."; +} + +/** + * @tc.name: PowerMgrMock078 + * @tc.desc: test proximity RunningLock by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock078, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock078: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock078:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock078: Failed to get PowerMgrService"; + } + + sptr token = new RunningLockTokenStub(); + RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL); + pms->CreateRunningLock(token, info); + EXPECT_CALL(*lockAction, Lock(_, _)).Times(1); + pms->Lock(token, info, 0); + EXPECT_EQ(pms->IsUsed(token), true); + + EXPECT_CALL(*stateAction, GoToSleep(_, _, true)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + pms->ForceSuspendDevice(0); + EXPECT_EQ(PowerState::SLEEP, pms->GetState()); + + EXPECT_CALL(*lockAction, Unlock(_, _)).Times(1); + pms->UnLock(token); + EXPECT_EQ(pms->IsUsed(token), false); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock078:End."); + GTEST_LOG_(INFO) << "PowerMgrMock078: end."; +} + +/** + * @tc.name: PowerMgrMock079 + * @tc.desc: test proximity RunningLock by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock079, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock079: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock079:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock079: Failed to get PowerMgrService"; + } + + pms->SuspendDevice(0, SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false); + sptr token = new RunningLockTokenStub(); + RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL); + pms->CreateRunningLock(token, info); + + EXPECT_CALL(*lockAction, Lock(_, _)).Times(1); + pms->Lock(token, info, 0); + EXPECT_EQ(pms->IsUsed(token), true); + + pms->WakeupDevice(0, WakeupDeviceType::WAKEUP_DEVICE_UNKNOWN, std::string("test")); + EXPECT_EQ(PowerState::AWAKE, pms->GetState()); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock079:End."); + GTEST_LOG_(INFO) << "PowerMgrMock079: end."; +} + +/** + * @tc.name: PowerMgrMock080 + * @tc.desc: test Auto SuspendDevice by mock after 15s + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock080, TestSize.Level2) +{ + int64_t time =15000; + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock080: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock080:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock080: Failed to get PowerMgrService"; + } + + pms->SetDisplayOffTime(time); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_ON)); + sleep(((time/1000)*2/3)+1); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_DIM)); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)) + .Times(::testing::AtLeast(1)) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + sleep(time/1000*1/3+1); + EXPECT_CALL(*stateAction, GoToSleep(_, _, false)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_OFF)); + sleep(SLEEP_WAIT_TIME_S+1); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock080:End."); + pms->SetDisplayOffTime(DEFAULT_DISPLAY_OFF_TIME); + GTEST_LOG_(INFO) << "PowerMgrMock080: end."; +} + +/** + * @tc.name: PowerMgrMock081 + * @tc.desc: test Auto SuspendDevice by mock after 30s + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock081, TestSize.Level2) +{ + int64_t time =30000; + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock081: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock081:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock081: Failed to get PowerMgrService"; + } + + pms->SetDisplayOffTime(time); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_ON)); + sleep(((time/1000)*2/3)+1); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_DIM)); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)) + .Times(::testing::AtLeast(1)) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + sleep(((time/1000)*1/3)+1); + EXPECT_CALL(*stateAction, GoToSleep(_, _, false)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_OFF)); + sleep(SLEEP_WAIT_TIME_S+1); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock081:End"); + pms->SetDisplayOffTime(DEFAULT_DISPLAY_OFF_TIME); + GTEST_LOG_(INFO) << "PowerMgrMock081: end."; +} + +/** + * @tc.name: PowerMgrMock082 + * @tc.desc: test Auto SuspendDevice by mock after 1min + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock082, TestSize.Level2) +{ + int64_t time =60000; + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock082: start."; + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock082: Failed to get PowerMgrService"; + } + + pms->SetDisplayOffTime(time); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_ON)); + sleep(((time/1000)*2/3)+1); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_DIM)); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)) + .Times(::testing::AtLeast(1)) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + sleep(time/1000*1/3+1); + EXPECT_CALL(*stateAction, GoToSleep(_, _, false)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_OFF)); + sleep(SLEEP_WAIT_TIME_S+1); + + ResetMockAction(); + pms->SetDisplayOffTime(DEFAULT_DISPLAY_OFF_TIME); + GTEST_LOG_(INFO) << "PowerMgrMock082: end."; +} + +/** + * @tc.name: PowerMgrMock083 + * @tc.desc: test Auto SuspendDevice by mock after 2mins + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock083, TestSize.Level2) +{ + int64_t time =120000; + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock083: start."; + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock083: Failed to get PowerMgrService"; + } + + pms->SetDisplayOffTime(time); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_ON)); + sleep(((time/1000)*2/3)+1); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_DIM)); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)) + .Times(::testing::AtLeast(1)) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + sleep(time/1000*1/3+1); + EXPECT_CALL(*stateAction, GoToSleep(_, _, false)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_OFF)); + sleep(SLEEP_WAIT_TIME_S+1); + + ResetMockAction(); + pms->SetDisplayOffTime(DEFAULT_DISPLAY_OFF_TIME); + GTEST_LOG_(INFO) << "PowerMgrMock083: end."; +} + +/** + * @tc.name: PowerMgrMock084 + * @tc.desc: test Auto SuspendDevice by mock after 5mins + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock084, TestSize.Level2) +{ + int64_t time =300000; + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock084: start."; + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock084: Failed to get PowerMgrService"; + } + + pms->SetDisplayOffTime(time); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_ON)); + sleep(((time/1000)*2/3)+1); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_DIM)); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)) + .Times(::testing::AtLeast(1)) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + sleep(time/1000*1/3+1); + EXPECT_CALL(*stateAction, GoToSleep(_, _, false)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_OFF)); + sleep(SLEEP_WAIT_TIME_S+1); + + ResetMockAction(); + pms->SetDisplayOffTime(DEFAULT_DISPLAY_OFF_TIME); + GTEST_LOG_(INFO) << "PowerMgrMock084: end."; +} + +/** + * @tc.name: PowerMgrMock085 + * @tc.desc: test Auto SuspendDevice by mock after 10mins + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock085, TestSize.Level2) +{ + int64_t time =600000; + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock085: start."; + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock085: Failed to get PowerMgrService"; + } + + pms->SetDisplayOffTime(time); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_ON)); + sleep(((time/1000)*2/3)+1); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_DIM)); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)) + .Times(::testing::AtLeast(1)) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + sleep(time/1000*1/3+1); + EXPECT_CALL(*stateAction, GoToSleep(_, _, false)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_OFF)); + sleep(SLEEP_WAIT_TIME_S+1); + + ResetMockAction(); +pms->SetDisplayOffTime(DEFAULT_DISPLAY_OFF_TIME); + GTEST_LOG_(INFO) << "PowerMgrMock085: end."; +} + +/** + * @tc.name: PowerMgrMock086 + * @tc.desc: test Auto DIM by mock after 15s + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock086, TestSize.Level2) +{ + int64_t time =15000; + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock086: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock086:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock086: Failed to get PowerMgrService"; + } + pms->SetDisplayOffTime(time); + pms->WakeupDevice(0, WakeupDeviceType::WAKEUP_DEVICE_UNKNOWN, std::string("test")); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock086:DeviceStateAction::SetDisplayState."); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_ON)); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_DIM)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock086:Start sleep."); + sleep(time/1000-2); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_DIM)); + sleep(3); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock086: sleep end."); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock086:End."); + pms->SetDisplayOffTime(DEFAULT_DISPLAY_OFF_TIME); + GTEST_LOG_(INFO) << "PowerMgrMock086: end."; +} + +/** + * @tc.name: PowerMgrMock087 + * @tc.desc: test Auto DIM by mock after 30s + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock087, TestSize.Level2) +{ + int64_t time =30000; + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock087: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock0087:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock087: Failed to get PowerMgrService"; + } + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_ON)); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_DIM)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + pms->SetDisplayOffTime(time); + sleep(time/1000-2); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_DIM)); + sleep(3); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock087:End."); + pms->SetDisplayOffTime(DEFAULT_DISPLAY_OFF_TIME); + GTEST_LOG_(INFO) << "PowerMgrMock087: end."; +} + +/** + * @tc.name: PowerMgrMock088 + * @tc.desc: test Auto DIM by mock after 60s + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock088, TestSize.Level2) +{ + int64_t time =60000; + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock088: start."; + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock088: Failed to get PowerMgrService"; + } + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_ON)); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_DIM)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + pms->SetDisplayOffTime(time); + sleep(time/1000-2); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_DIM)); + sleep(3); + + ResetMockAction(); + pms->SetDisplayOffTime(DEFAULT_DISPLAY_OFF_TIME); + GTEST_LOG_(INFO) << "PowerMgrMock088: end."; +} + +/** + * @tc.name: PowerMgrMock089 + * @tc.desc: test Auto DIM by mock after 2min + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock089, TestSize.Level2) +{ + int64_t time =120000; + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock089: start."; + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock089: Failed to get PowerMgrService"; + } + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_ON)); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_DIM)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + pms->SetDisplayOffTime(time); + sleep(time/1000-2); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_DIM)); + sleep(3); + + ResetMockAction(); + pms->SetDisplayOffTime(DEFAULT_DISPLAY_OFF_TIME); + GTEST_LOG_(INFO) << "PowerMgrMock089: end."; +} + +/** + * @tc.name: PowerMgrMock090 + * @tc.desc: test Auto DIM by mock after 5min + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock090, TestSize.Level2) +{ + int64_t time =300000; + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock090: start."; + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock090: Failed to get PowerMgrService"; + } + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_ON)); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_DIM)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + pms->SetDisplayOffTime(time); + sleep(time/1000-2); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_DIM)); + sleep(3); + + ResetMockAction(); + pms->SetDisplayOffTime(DEFAULT_DISPLAY_OFF_TIME); + GTEST_LOG_(INFO) << "PowerMgrMock090: end."; +} + +/** + * @tc.name: PowerMgrMock091 + * @tc.desc: test proximity screen control RunningLock by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock091, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock091: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock091:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock091: Failed to get PowerMgrService"; + } + + sptr token = new RunningLockTokenStub(); + RunningLockInfo info("test1", RunningLockType::RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL); + pms->CreateRunningLock(token, info); + pms->Lock(token, info, 0); + EXPECT_EQ(pms->IsUsed(token), true); + sleep(SLEEP_WAIT_TIME_S); + pms->UnLock(token); + EXPECT_EQ(pms->IsUsed(token), false); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_ON)); + sleep((SCREEN_OFF_WAIT_TIME_S*2/3)+1); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_DIM)); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_OFF)) + .Times(::testing::AtLeast(1)) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + sleep((SCREEN_OFF_WAIT_TIME_S*1/3) + 1); + EXPECT_EQ(PowerState::INACTIVE, pms->GetState()); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock091:End."); + GTEST_LOG_(INFO) << "PowerMgrMock091: end."; +} + +/** + * @tc.name: PowerMgrMock092 + * @tc.desc: test WakeupDevice by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock092, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock092: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock092:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock092: Failed to get PowerMgrService"; + } + + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_ON)); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_DIM)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + sleep((SCREEN_OFF_WAIT_TIME_S*2/3) + 1); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_DIM)); + EXPECT_CALL(*stateAction, + Wakeup(0, WakeupDeviceType::WAKEUP_DEVICE_UNKNOWN, + std::string("test"), _)).Times(1); + pms->WakeupDevice(0, WakeupDeviceType::WAKEUP_DEVICE_UNKNOWN, std::string("test")); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock092:End."); + GTEST_LOG_(INFO) << "PowerMgrMock092: end."; +} + +/** + * @tc.name: PowerMgrMock093 + * @tc.desc: test WakeupDevice by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock093, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock093: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock093:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock093: Failed to get PowerMgrService"; + } + + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_ON)); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_DIM)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + sleep((SCREEN_OFF_WAIT_TIME_S*2/3) + 1); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_DIM)); + EXPECT_CALL(*stateAction, + Wakeup(0, WakeupDeviceType::WAKEUP_DEVICE_POWER_BUTTON, + std::string("test"), _)).Times(1); + pms->WakeupDevice(0, WakeupDeviceType::WAKEUP_DEVICE_POWER_BUTTON, std::string("test")); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock093:End."); + GTEST_LOG_(INFO) << "PowerMgrMock093: end."; +} + +/** + * @tc.name: PowerMgrMock094 + * @tc.desc: test WakeupDevice by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock094, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock094: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock094:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock094: Failed to get PowerMgrService"; + } + + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_ON)); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_DIM)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + sleep((SCREEN_OFF_WAIT_TIME_S*2/3) + 1); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_DIM)); + EXPECT_CALL(*stateAction, + Wakeup(0, WakeupDeviceType::WAKEUP_DEVICE_APPLICATION, + std::string("test"), _)).Times(1); + pms->WakeupDevice(0, WakeupDeviceType::WAKEUP_DEVICE_APPLICATION, std::string("test")); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock094:End."); + GTEST_LOG_(INFO) << "PowerMgrMock094: end."; +} + +/** + * @tc.name: PowerMgrMock095 + * @tc.desc: test WakeupDevice by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock095, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock095: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock095:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock095: Failed to get PowerMgrService"; + } + + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_ON)); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_DIM)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + sleep((SCREEN_OFF_WAIT_TIME_S*2/3) + 1); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_DIM)); + EXPECT_CALL(*stateAction, + Wakeup(0, WakeupDeviceType::WAKEUP_DEVICE_PLUGGED_IN, + std::string("test"), _)).Times(1); + pms->WakeupDevice(0, WakeupDeviceType::WAKEUP_DEVICE_PLUGGED_IN, std::string("test")); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock095:End."); + GTEST_LOG_(INFO) << "PowerMgrMock095: end."; +} + +/** + * @tc.name: PowerMgrMock096 + * @tc.desc: test WakeupDevice by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock096, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock096: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock096:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock096: Failed to get PowerMgrService"; + } + + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_ON)); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_DIM)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + sleep((SCREEN_OFF_WAIT_TIME_S*2/3) + 1); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_DIM)); + EXPECT_CALL(*stateAction, + Wakeup(0, WakeupDeviceType::WAKEUP_DEVICE_GESTURE, + std::string("test"), _)).Times(1); + pms->WakeupDevice(0, WakeupDeviceType::WAKEUP_DEVICE_GESTURE, std::string("test")); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock096:End."); + GTEST_LOG_(INFO) << "PowerMgrMock096: end."; +} + +/** + * @tc.name: PowerMgrMock097 + * @tc.desc: test WakeupDevice by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock097, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock097: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock097:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock097: Failed to get PowerMgrService"; + } + + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_ON)); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_DIM)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + sleep((SCREEN_OFF_WAIT_TIME_S*2/3) + 1); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_DIM)); + EXPECT_CALL(*stateAction, + Wakeup(0, WakeupDeviceType::WAKEUP_DEVICE_CAMERA_LAUNCH, + std::string("test"), _)).Times(1); + pms->WakeupDevice(0, WakeupDeviceType::WAKEUP_DEVICE_CAMERA_LAUNCH, std::string("test")); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock097:End."); + GTEST_LOG_(INFO) << "PowerMgrMock097: end."; +} + +/** + * @tc.name: PowerMgrMock098 + * @tc.desc: test WakeupDevice by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock098, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock098: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock098:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock098: Failed to get PowerMgrService"; + } + + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_ON)); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_DIM)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + sleep((SCREEN_OFF_WAIT_TIME_S*2/3) + 1); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_DIM)); + EXPECT_CALL(*stateAction, + Wakeup(0, WakeupDeviceType::WAKEUP_DEVICE_WAKE_KEY, + std::string("test"), _)).Times(1); + pms->WakeupDevice(0, WakeupDeviceType::WAKEUP_DEVICE_WAKE_KEY, std::string("test")); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock098:End."); + GTEST_LOG_(INFO) << "PowerMgrMock098: end."; +} + +/** + * @tc.name: PowerMgrMock099 + * @tc.desc: test WakeupDevice by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock099, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock099: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock099:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock099: Failed to get PowerMgrService"; + } + + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_ON)); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_DIM)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + sleep((SCREEN_OFF_WAIT_TIME_S*2/3) + 1); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_DIM)); + EXPECT_CALL(*stateAction, + Wakeup(0, WakeupDeviceType::WAKEUP_DEVICE_WAKE_MOTION, + std::string("test"), _)).Times(1); + pms->WakeupDevice(0, WakeupDeviceType::WAKEUP_DEVICE_WAKE_MOTION, std::string("test")); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock099:End."); + GTEST_LOG_(INFO) << "PowerMgrMock099: end."; +} + +/** + * @tc.name: PowerMgrMock100 + * @tc.desc: test WakeupDevice by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock100, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock100: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock100:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock100: Failed to get PowerMgrService"; + } + + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_ON)); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_DIM)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + sleep((SCREEN_OFF_WAIT_TIME_S*2/3) + 1); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_DIM)); + EXPECT_CALL(*stateAction, + Wakeup(0, WakeupDeviceType::WAKEUP_DEVICE_HDMI, + std::string("test"), _)).Times(1); + pms->WakeupDevice(0, WakeupDeviceType::WAKEUP_DEVICE_HDMI, std::string("test")); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock100:End."); + GTEST_LOG_(INFO) << "PowerMgrMock100: end."; +} + +/** + * @tc.name: PowerMgrMock101 + * @tc.desc: test WakeupDevice by mock + * @tc.type: FUNC + */ +HWTEST_F (PowerMgrSTMockTest, PowerMgrMock101, TestSize.Level2) +{ + sleep(NEXT_WAIT_TIME_S); + GTEST_LOG_(INFO) << "PowerMgrMock101: start."; + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock101:Start."); + + sptr pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + GTEST_LOG_(INFO) << "PowerMgrMock101: Failed to get PowerMgrService"; + } + + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_ON)); + EXPECT_CALL(*stateAction, SetDisplayState(DisplayState::DISPLAY_DIM)) + .Times(1) + .WillOnce(::testing::Return(ActionResult::SUCCESS)); + sleep((SCREEN_OFF_WAIT_TIME_S*2/3) + 1); + ON_CALL(*stateAction, GetDisplayState()) + .WillByDefault(::testing::Return(DisplayState::DISPLAY_DIM)); + EXPECT_CALL(*stateAction, + Wakeup(0, WakeupDeviceType::WAKEUP_DEVICE_LID, + std::string("test"), _)).Times(1); + pms->WakeupDevice(0, WakeupDeviceType::WAKEUP_DEVICE_LID, std::string("test")); + + ResetMockAction(); + POWER_HILOGD(MODULE_SERVICE, "PowerMgrMock101:End."); + GTEST_LOG_(INFO) << "PowerMgrMock101: end."; +} +} \ No newline at end of file