diff --git a/bundle.json b/bundle.json index 6ae1cd392b0ef772f9e52d4310fa5a7ca0f2cfb1..1cbcd2ba21942046d43b57b08f67dcbdbd7777a6 100755 --- a/bundle.json +++ b/bundle.json @@ -58,7 +58,8 @@ } ], "test": [ - "//base/powermgr/display_manager/test/native:displaymgr_native_test" + "//base/powermgr/display_manager/test:displaymgr_js_test", + "//base/powermgr/display_manager/test:displaymgr_native_test" ] } } diff --git a/frameworks/napi/brightness.cpp b/frameworks/napi/brightness.cpp index 5bd129488fc70daa9cd57274bc1ad9bd1c93c690..ffddf099d96c383cade4603879e25fe17a999f0a 100644 --- a/frameworks/napi/brightness.cpp +++ b/frameworks/napi/brightness.cpp @@ -1,114 +1,290 @@ -/* - * 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 "display_power_mgr_client.h" -#include "hilog_wrapper.h" - -using namespace OHOS::DisplayPowerMgr; - -struct BrightnessAsyncCallbackInfo { - napi_async_work asyncWork; - int32_t value; -}; - -static napi_value SetValue(napi_env env, napi_callback_info info) -{ - DISPLAY_HILOGD(MODULE_JS_NAPI, "enter"); - size_t argc = 1; - napi_value args[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 >= 1), "Failed to get cb info"); - - napi_valuetype type; - NAPI_CALL(env, napi_typeof(env, args[0], &type)); - - NAPI_ASSERT(env, type == napi_number, "Wrong argument type. Numbers expected."); - - int32_t value = 0; - NAPI_CALL(env, napi_get_value_int32(env, args[0], &value)); - - BrightnessAsyncCallbackInfo* asyncCallbackInfo = new BrightnessAsyncCallbackInfo { - .asyncWork = nullptr, - }; - - asyncCallbackInfo->value = value; - - napi_value resourceName; - napi_create_string_latin1(env, "setValue", NAPI_AUTO_LENGTH, &resourceName); - - napi_create_async_work( - env, - nullptr, - resourceName, - [](napi_env env, void *data) { - BrightnessAsyncCallbackInfo* asyncCallbackInfo = (BrightnessAsyncCallbackInfo *)data; - if (!DisplayPowerMgrClient::GetInstance().SetBrightness(asyncCallbackInfo->value)) { - DISPLAY_HILOGE(MODULE_JS_NAPI, "Failed to set brightness"); - } else { - DISPLAY_HILOGD(MODULE_JS_NAPI, "Succeed to set brightness"); - } - }, - [](napi_env env, napi_status status, void *data) { - BrightnessAsyncCallbackInfo* asyncCallbackInfo = (BrightnessAsyncCallbackInfo *)data; - napi_delete_async_work(env, asyncCallbackInfo->asyncWork); - delete asyncCallbackInfo; - }, - (void *)asyncCallbackInfo, - &asyncCallbackInfo->asyncWork); - - NAPI_CALL(env, napi_queue_async_work(env, asyncCallbackInfo->asyncWork)); - - DISPLAY_HILOGD(MODULE_JS_NAPI, "return"); - return nullptr; -} - -EXTERN_C_START -static napi_value Init(napi_env env, napi_value exports) -{ - napi_property_descriptor desc[] = { - DECLARE_NAPI_FUNCTION("setValue", SetValue), - }; - NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc)); - - DISPLAY_HILOGD(MODULE_JS_NAPI, "return"); - - return exports; -} -EXTERN_C_END - -static napi_module g_module = { - .nm_version = 1, - .nm_flags = 0, - .nm_filename = "brightness", - .nm_register_func = Init, - .nm_modname = "brightness", - .nm_priv = ((void *)0), - .reserved = { 0 } -}; - -extern "C" __attribute__((constructor)) void RegisterModule(void) -{ - napi_module_register(&g_module); -} +/* + * Copyright (c) 2021-2022 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 "brightness.h" + +#include "display_common.h" +#include "hilog_wrapper.h" +#include "display_power_mgr_client.h" + +using namespace OHOS::PowerMgr; + +namespace OHOS { +namespace DisplayPowerMgr { +namespace { +const uint32_t MAX_ARGC = 1; +const uint32_t ARGV_ONE = 0; +const uint32_t MAX_FAIL_ARGC = 2; +const int32_t MAX_BRIGHTNESS = 255; +const int32_t MIN_BRIGHTNESS = 1; +const int32_t BRIGHTNESS_OFF = 0; + +const std::string BRIGHTNESS_VALUE = "value"; +const std::string BRIGHTNESS_MODE = "mode"; +const std::string KEEP_SCREENON = "keepScreenOn"; + +const std::string FUNC_SUCEESS_NAME = "success"; +const std::string FUNC_FAIL_NAME = "fail"; +const std::string FUNC_COMPLETE_NAME = "complete"; + +const int32_t COMMON_ERROR_COED = 200; +const int32_t INPUT_ERROR_CODE = 202; + +const std::string SET_VALUE_ERROR_MGR = "setValue: value is not an available number"; +const std::string GET_VALUE_ERROR_MGR = "get system screen brightness fail"; +const std::string SET_MODE_ERROR_MGR = "setMode: value is not an available number"; +const std::string SET_MODE_NOT_SUPPORTED_ERROR_MGR = "setMode: Auto adjusting brightness is not supported"; +const std::string SET_KEEP_SCREENON_ERROR_MGR = "value is not an available boolean"; +} + +Brightness::Brightness(napi_env env) : env_(env) +{ +} + +void Brightness::GetValue(napi_value options) +{ + uint32_t brightness = brightnessInfo_.GetBrightness(); + if (BRIGHTNESS_OFF >= brightness || brightness > MAX_BRIGHTNESS) { + result_.Error(COMMON_ERROR_COED, GET_VALUE_ERROR_MGR); + } else { + result_.SetResult(BRIGHTNESS_VALUE, brightness); + } + ExecuteCallback(options); +} + +void Brightness::SetValue(napi_callback_info info) +{ + napi_value options = GetCallbackInfo(info, napi_object); + if (options != nullptr) { + DISPLAY_HILOGD(MODULE_JNI, "System brightness interface"); + napi_value value = GetOptions(options, BRIGHTNESS_VALUE, napi_number); + if (value == nullptr) { + result_.Error(INPUT_ERROR_CODE, SET_VALUE_ERROR_MGR); + } else { + int32_t brightness = MIN_BRIGHTNESS; + napi_get_value_int32(env_, value, &brightness); + brightnessInfo_.SetBrightness(brightness); + } + ExecuteCallback(options); + return; + } + + napi_value number = GetCallbackInfo(info, napi_number); + if (number != nullptr) { + DISPLAY_HILOGD(MODULE_JNI, "Brightness interface"); + int32_t value = MIN_BRIGHTNESS; + if (napi_ok != napi_get_value_int32(env_, number, &value)) { + DISPLAY_HILOGW(MODULE_JNI, "Failed to get the input number"); + return; + } + brightnessInfo_.SetBrightness(value); + return; + } + DISPLAY_HILOGW(MODULE_JNI, "SetValue: No valid parameters"); +} + +void Brightness::GetMode(napi_value options) +{ + int32_t isAuto = brightnessInfo_.GetAutoMode(); + result_.SetResult(BRIGHTNESS_MODE, isAuto); + ExecuteCallback(options); +} + +void Brightness::SetMode(napi_value options) +{ + DISPLAY_HILOGD(MODULE_JNI, "Set auto brightness"); + napi_value napiMode = GetOptions(options, BRIGHTNESS_MODE, napi_number); + if (napiMode == nullptr) { + result_.Error(INPUT_ERROR_CODE, SET_MODE_ERROR_MGR); + } else { + int32_t mode = 0; + napi_get_value_int32(env_, napiMode, &mode); + if (!brightnessInfo_.SetAutoMode(static_cast(mode))) { + result_.Error(COMMON_ERROR_COED, SET_MODE_NOT_SUPPORTED_ERROR_MGR); + } + } + ExecuteCallback(options); +} + +void Brightness::SetKeepScreenOn(napi_value options, std::shared_ptr& runningLock) +{ + DISPLAY_HILOGD(MODULE_JNI, "Set keep screen on"); + napi_value napiKeep = GetOptions(options, KEEP_SCREENON, napi_boolean); + if (napiKeep == nullptr) { + result_.Error(INPUT_ERROR_CODE, SET_KEEP_SCREENON_ERROR_MGR); + } else { + bool screenOn = false; + napi_get_value_bool(env_, napiKeep, &screenOn); + brightnessInfo_.ScreenOn(screenOn, runningLock); + } + ExecuteCallback(options); +} + +napi_value Brightness::GetCallbackInfo(napi_callback_info info, napi_valuetype checkType) +{ + size_t argc = MAX_ARGC; + napi_value argv[argc]; + napi_value thisVar = nullptr; + void *data = nullptr; + if (napi_ok != napi_get_cb_info(env_, info, &argc, argv, &thisVar, &data)) { + DISPLAY_HILOGW(MODULE_JNI, "Failed to get the input parameter"); + return nullptr; + } + + if (argc != MAX_ARGC) { + DISPLAY_HILOGW(MODULE_JNI, "Lack of parameter"); + return nullptr; + } + + napi_value options = argv[ARGV_ONE]; + RETURN_IF_WITH_RET(!CheckValueType(options, checkType), nullptr); + return options; +} + +void Brightness::Result::Error(int32_t code, const std::string& msg) +{ + code_ = code; + msg_ = msg; + DISPLAY_HILOGW(MODULE_JNI, "Error message, code: %{public}d, msg: %{public}s", + code_, msg_.c_str()); +} + +void Brightness::Result::GetError(napi_env env, napi_value* error, size_t& size) +{ + if (!error) { + DISPLAY_HILOGW(MODULE_JNI, "error is null"); + return; + } + napi_value data = nullptr; + napi_value code = nullptr; + napi_create_string_utf8(env, msg_.c_str(), msg_.size(), &data); + napi_create_int32(env, code_, &code); + size = MAX_FAIL_ARGC; + error[ARGV_ONE] = data; + error[MAX_ARGC] = code; +} + +napi_value Brightness::Result::GetResult(napi_env env) +{ + napi_value result = nullptr; + NAPI_CALL(env, napi_create_object(env, &result)); + for (const auto& it : mapResult_) { + napi_value napiValue = 0; + NAPI_CALL(env, napi_create_int32(env, it.second, &napiValue)); + NAPI_CALL(env, napi_set_named_property(env, result, it.first.c_str(), napiValue)); + } + return result; +} + +uint32_t Brightness::BrightnessInfo::GetBrightness() +{ + uint32_t brightness = DisplayPowerMgrClient::GetInstance().GetBrightness(); + DISPLAY_HILOGI(MODULE_JNI, "Get brightness: %{public}d", brightness); + return brightness; +} + +bool Brightness::BrightnessInfo::SetBrightness(int32_t value) +{ + DISPLAY_HILOGI(MODULE_JNI, "Set brightness: %{public}d", value); + value = value > MAX_BRIGHTNESS ? MAX_BRIGHTNESS : value; + value = value < MIN_BRIGHTNESS ? MIN_BRIGHTNESS : value; + bool isSucc = DisplayPowerMgrClient::GetInstance().SetBrightness(value); + if (!isSucc) { + DISPLAY_HILOGW(MODULE_JNI, "Failed to set brightness: %{public}d", value); + } + return isSucc; +} + +int32_t Brightness::BrightnessInfo::GetAutoMode() +{ + bool isAuto = DisplayPowerMgrClient::GetInstance().IsAutoAdjustBrightness(); + DISPLAY_HILOGD(MODULE_JNI, "Automatic brightness adjustment: %{public}d", isAuto); + return static_cast(isAuto); +} + +bool Brightness::BrightnessInfo::SetAutoMode(bool mode) +{ + DISPLAY_HILOGD(MODULE_JNI, "AutoAdjustBrightness begin"); + bool isSucc = DisplayPowerMgrClient::GetInstance().AutoAdjustBrightness(mode); + DISPLAY_HILOGD(MODULE_JNI, "set auto brightness mode: %{public}d, succ: %{public}d", mode, isSucc); + return isSucc; +} + +void Brightness::BrightnessInfo::ScreenOn(bool keep, std::shared_ptr& runningLock) +{ + DISPLAY_HILOGD(MODULE_JNI, "Keep screen on, keep: %{public}d, isUsed: %{public}d", keep, runningLock->IsUsed()); + keep ? runningLock->Lock() : runningLock->UnLock(); +} + +void Brightness::ExecuteCallback(napi_value options) +{ + bool error = result_.IsError(); + if (!error) { + DISPLAY_HILOGI(MODULE_JNI, "Call the js success method"); + napi_value result = result_.GetResult(env_); + size_t argc = result ? MAX_ARGC : 0; + CallFunction(options, FUNC_SUCEESS_NAME, argc, result ? &result : nullptr); + } + + if (error) { + DISPLAY_HILOGI(MODULE_JNI, "Call the js fail method"); + size_t argc = MAX_FAIL_ARGC; + napi_value argv[argc]; + result_.GetError(env_, argv, argc); + CallFunction(options, FUNC_FAIL_NAME, argc, argv); + } + DISPLAY_HILOGI(MODULE_JNI, "Call the js complete method"); + CallFunction(options, FUNC_COMPLETE_NAME, 0, nullptr); +} + +bool Brightness::CheckValueType(napi_value value, napi_valuetype checkType) +{ + napi_valuetype valueType = napi_undefined; + napi_typeof(env_, value, &valueType); + if (valueType != checkType) { + DISPLAY_HILOGW(MODULE_JNI, "Check input parameter error"); + return false; + } + return true; +} + +napi_value Brightness::GetOptions(napi_value options, const std::string& name, napi_valuetype checkType) +{ + napi_value property = nullptr; + napi_status status = napi_get_named_property(env_, options, name.c_str(), &property); + if (status != napi_ok) { + DISPLAY_HILOGW(MODULE_JNI, "Failed to get the %{public}s Options property", name.c_str()); + return nullptr; + } + if (!CheckValueType(property, checkType)) { + DISPLAY_HILOGW(MODULE_JNI, "Get %{public}s Options property type mismatch", name.c_str()); + return nullptr; + } + return property; +} + +void Brightness::CallFunction(napi_value options, const std::string& name, size_t argc, napi_value* response) +{ + napi_value optionsFunc = GetOptions(options, name, napi_function); + RETURN_IF(optionsFunc == nullptr); + + napi_value callResult = 0; + napi_status status = napi_call_function(env_, nullptr, optionsFunc, argc, response, &callResult); + if (status != napi_ok) { + DISPLAY_HILOGW(MODULE_JNI, "Failed to call the %{public}s callback function", name.c_str()); + } +} +} // namespace DisplayPowerMgr +} // namespace OHOS diff --git a/frameworks/napi/brightness.h b/frameworks/napi/brightness.h new file mode 100644 index 0000000000000000000000000000000000000000..edb8ee2615497ac4965eedc06c42936e926c2a7b --- /dev/null +++ b/frameworks/napi/brightness.h @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2022 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_BRIGHTNESS_H +#define POWERMGR_BRIGHTNESS_H + +#include + +#include "errors.h" +#include "napi/native_api.h" +#include "napi/native_node_api.h" + +#include "running_lock.h" + +namespace OHOS { +namespace DisplayPowerMgr { +class Brightness { +public: + explicit Brightness(napi_env env); + void GetValue(napi_value options); + void SetValue(napi_callback_info info); + void GetMode(napi_value options); + void SetMode(napi_value options); + void SetKeepScreenOn(napi_value options, std::shared_ptr& runningLock); + napi_value GetCallbackInfo(napi_callback_info info, napi_valuetype checkType); +private: + class Result { + public: + void Error(int32_t code, const std::string& msg); + void GetError(napi_env env, napi_value* error, size_t& size); + napi_value GetResult(napi_env env); + inline void SetResult(const std::string& key, int32_t value) + { + mapResult_.emplace(key, value); + } + inline bool IsError() + { + return !msg_.empty() && (code_ != ERR_OK); + } + private: + int32_t code_ { ERR_OK }; + std::string msg_; + std::map mapResult_; + }; + + class BrightnessInfo { + public: + uint32_t GetBrightness(); + bool SetBrightness(int32_t value); + int32_t GetAutoMode(); + bool SetAutoMode(bool mode); + void ScreenOn(bool keep, std::shared_ptr& runningLock); + }; + + void ExecuteCallback(napi_value options); + bool CheckValueType(napi_value value, napi_valuetype checkType); + napi_value GetOptions(napi_value options, const std::string& name, napi_valuetype checkType); + void CallFunction(napi_value options, const std::string& name, size_t argc, napi_value* response); + + napi_env env_; + Result result_; + BrightnessInfo brightnessInfo_; +}; +} // namespace DisplayPowerMgr +} // namespace OHOS + +#endif // POWERMGR_BRIGHTNESS_H diff --git a/frameworks/napi/brightness_module.cpp b/frameworks/napi/brightness_module.cpp new file mode 100644 index 0000000000000000000000000000000000000000..860f74757ae7a0a7dab9ca5e98217f12cd60f143 --- /dev/null +++ b/frameworks/napi/brightness_module.cpp @@ -0,0 +1,108 @@ +/* + * Copyright (c) 2022 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 "brightness.h" +#include "display_common.h" +#include "hilog_wrapper.h" +#include "power_mgr_client.h" +#include "running_lock_info.h" + +using namespace OHOS::DisplayPowerMgr; +using namespace OHOS::PowerMgr; +namespace { +std::shared_ptr runningLock = + PowerMgrClient::GetInstance().CreateRunningLock(std::string("KeepScreenOn"), RunningLockType::RUNNINGLOCK_SCREEN); +} + +static napi_value GetValue(napi_env env, napi_callback_info info) +{ + Brightness brightness(env); + napi_value options = brightness.GetCallbackInfo(info, napi_object); + RETURN_IF_WITH_RET(options == nullptr, nullptr); + brightness.GetValue(options); + return nullptr; +} + +static napi_value SetValue(napi_env env, napi_callback_info info) +{ + Brightness brightness(env); + brightness.SetValue(info); + return nullptr; +} + +static napi_value GetMode(napi_env env, napi_callback_info info) +{ + Brightness brightness(env); + napi_value options = brightness.GetCallbackInfo(info, napi_object); + RETURN_IF_WITH_RET(options == nullptr, nullptr); + brightness.GetMode(options); + return nullptr; +} + +static napi_value SetMode(napi_env env, napi_callback_info info) +{ + Brightness brightness(env); + napi_value options = brightness.GetCallbackInfo(info, napi_object); + RETURN_IF_WITH_RET(options == nullptr, nullptr); + brightness.SetMode(options); + return nullptr; +} + +static napi_value SetKeepScreenOn(napi_env env, napi_callback_info info) +{ + Brightness brightness(env); + napi_value options = brightness.GetCallbackInfo(info, napi_object); + RETURN_IF_WITH_RET(options == nullptr, nullptr); + brightness.SetKeepScreenOn(options, runningLock); + return nullptr; +} + +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + DISPLAY_HILOGI(MODULE_JNI, "brightness init"); + napi_property_descriptor desc[] = { + DECLARE_NAPI_FUNCTION("getValue", GetValue), + DECLARE_NAPI_FUNCTION("setValue", SetValue), + DECLARE_NAPI_FUNCTION("getMode", GetMode), + DECLARE_NAPI_FUNCTION("setMode", SetMode), + DECLARE_NAPI_FUNCTION("setKeepScreenOn", SetKeepScreenOn) + }; + + NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc)); + DISPLAY_HILOGI(MODULE_JNI, "brightness init end"); + return exports; +} +EXTERN_C_END + +static napi_module g_module = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = "brightness", + .nm_register_func = Init, + .nm_modname = "brightness", + .nm_priv = ((void *)0), + .reserved = { 0 } +}; + +extern "C" __attribute__((constructor)) void RegisterModule(void) +{ + napi_module_register(&g_module); +} diff --git a/frameworks/native/display_power_mgr_client.cpp b/frameworks/native/display_power_mgr_client.cpp index 470d4eac5a94039013b368089c439f51bfb62151..d6ea6c18df5acff5cdc3b6b6941b258a881355a4 100644 --- a/frameworks/native/display_power_mgr_client.cpp +++ b/frameworks/native/display_power_mgr_client.cpp @@ -163,6 +163,15 @@ bool DisplayPowerMgrClient::AutoAdjustBrightness(bool enable) return proxy->AutoAdjustBrightness(enable); } +bool DisplayPowerMgrClient::IsAutoAdjustBrightness() +{ + auto proxy = GetProxy(); + if (proxy == nullptr) { + return false; + } + return proxy->IsAutoAdjustBrightness(); +} + bool DisplayPowerMgrClient::SetStateConfig(DisplayState state, uint32_t value, uint32_t id) { auto proxy = GetProxy(); diff --git a/interfaces/innerkits/native/include/display_power_mgr_client.h b/interfaces/innerkits/native/include/display_power_mgr_client.h index 068e5ad84e44932d59614fefaa87ecdc988c7f26..0ec87528602f96e78b19347c2fc6689569bdb5aa 100644 --- a/interfaces/innerkits/native/include/display_power_mgr_client.h +++ b/interfaces/innerkits/native/include/display_power_mgr_client.h @@ -44,6 +44,7 @@ public: bool AdjustBrightness(uint32_t value, uint32_t duration, uint32_t id = 0); bool SetStateConfig(DisplayState state, uint32_t value, uint32_t id = 0); bool AutoAdjustBrightness(bool enable); + bool IsAutoAdjustBrightness(); bool RegisterCallback(sptr callback); private: diff --git a/interfaces/innerkits/native/include/idisplay_power_mgr.h b/interfaces/innerkits/native/include/idisplay_power_mgr.h index fa6e87181b185dc86550284996026d98ed4c71da..2bbda9c9775641589c1b5756946ba1d42136e5ab 100644 --- a/interfaces/innerkits/native/include/idisplay_power_mgr.h +++ b/interfaces/innerkits/native/include/idisplay_power_mgr.h @@ -37,6 +37,7 @@ public: GET_BRIGHTNESS, ADJUST_BRIGHTNESS, AUTO_ADJUST_BRIGHTNESS, + IS_AUTO_ADJUST_BRIGHTNESS, SET_STATE_CONFIG, REGISTER_CALLBACK, }; @@ -51,6 +52,7 @@ public: virtual uint32_t GetBrightness(uint32_t displayId) = 0; virtual bool AdjustBrightness(uint32_t id, int32_t value, uint32_t duration) = 0; virtual bool AutoAdjustBrightness(bool enable) = 0; + virtual bool IsAutoAdjustBrightness() = 0; virtual bool SetStateConfig(uint32_t id, DisplayState state, int32_t value) = 0; virtual bool RegisterCallback(sptr callback) = 0; diff --git a/interfaces/kits/js/@system.brightness.d.ts b/interfaces/kits/js/@system.brightness.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..ab9467ce7731068077c93bc25cc1994d6cc73387 --- /dev/null +++ b/interfaces/kits/js/@system.brightness.d.ts @@ -0,0 +1,226 @@ +/* + * Copyright (c) 2022 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. + */ + +/** + * @syscap SystemCapability.PowerManager.DisplayPowerManager + * @since 3 + */ +export interface BrightnessResponse { + /** + * Screen brightness, which ranges from 1 to 100. + * @since 3 + */ + value: number; +} + +/** + * @syscap SystemCapability.PowerManager.DisplayPowerManager + * @since 3 + */ +export interface GetBrightnessOptions { + /** + * Called when the current screen brightness is obtained. + * @since 3 + */ + success?: (data: BrightnessResponse) => void; + + /** + * Called when the current screen brightness fails to be obtained. + * @since 3 + */ + fail?: (data: string, code: number) => void; + + /** + * Called when the execution is completed. + * @since 3 + */ + complete?: () => void; +} + +/** + * @syscap SystemCapability.PowerManager.DisplayPowerManager + * @since 3 + */ +export interface SetBrightnessOptions { + /** + * Screen brightness. The value is an integer ranging from 1 to 100. + * If the value is less than or equal to 0, value 1 will be used. + * If the value is greater than 100, value 100 will be used. + * If the value contains decimals, the integral part of the value will be used. + * For example, if value is 8.1 is set, value 8 will be used. + * @since 3 + */ + value: number; + + /** + * Called when the setting is successful. + * @since 3 + */ + success?: () => void; + + /** + * Called when the setting fails. + * @since 3 + */ + fail?: (data: string, code: number) => void; + + /** + * Called when the execution is completed. + * @since 3 + */ + complete?: () => void +} + +/** + * @syscap SystemCapability.PowerManager.DisplayPowerManager + * @since 3 + */ +export interface BrightnessModeResponse { + /** + * The value can be 0 or 1. + * 0: The screen brightness is manually adjusted. + * 1: The screen brightness is automatically adjusted. + * @since 3 + */ + mode: number; +} + +/** + * @syscap SystemCapability.PowerManager.DisplayPowerManager + * @since 3 + */ +export interface GetBrightnessModeOptions { + /** + * Called when the screen brightness adjustment mode is obtained. + * @since 3 + */ + success?: (data: BrightnessModeResponse) => void; + + /** + * Called when the screen brightness adjustment mode fails to be obtained. + * @since 3 + */ + fail?: (data: string, code: number) => void; + + /** + * Called when the execution is completed. + * @since 3 + */ + complete?: () => void; +} + +/** + * @syscap SystemCapability.PowerManager.DisplayPowerManager + * @since 3 + */ +export interface SetBrightnessModeOptions { + /** + * The screen brightness mode. + * 0: The screen brightness is manually adjusted. + * 1: The screen brightness is automatically adjusted. + * @since 3 + */ + mode: number; + + /** + * Called when the setting is successful. + * @since 3 + */ + success?: () => void; + + /** + * Called when the setting fails. + * @since 3 + */ + fail?: (data: string, code: number) => void; + + /** + * Called when the execution is completed. + * @since 3 + */ + complete?: () => void +} + +/** + * @syscap SystemCapability.PowerManager.DisplayPowerManager + * @since 3 + */ +export interface SetKeepScreenOnOptions { + /** + * Whether to always keep the screen on. + * @since 3 + */ + keepScreenOn: boolean; + + /** + * Called when the setting is successful. + * @since 3 + */ + success?: () => void; + + /** + * Called when the setting fails. + * @since 3 + */ + fail?: (data: string, code: number) => void; + + /** + * Called when the execution is completed. + * @since 3 + */ + complete?: () => void +} + +/** + * @syscap SystemCapability.PowerManager.DisplayPowerManager + * @since 3 + * @import brightness from '@system.brightness'; + */ +export default class Brightness { + /** + * Obtains the current screen brightness. + * @param options Options. + * @since 3 + */ + static getValue(options?: GetBrightnessOptions): void; + + /** + * Sets the screen brightness. + * @param options Options. + * @since 3 + */ + static setValue(options?: SetBrightnessOptions): void; + + /** + * Obtains the screen brightness adjustment mode. + * @param options Options. + * @since 3 + */ + static getMode(options?: GetBrightnessModeOptions): void; + + /** + * Sets the screen brightness adjustment mode. + * @param options Options. + * @since 3 + */ + static setMode(options?: SetBrightnessModeOptions): void; + + /** + * Sets whether to always keep the screen on. + * @param options Options. + * @since 3 + */ + static setKeepScreenOn(options?: SetKeepScreenOnOptions): void; +} diff --git a/interfaces/kits/js/napi/BUILD.gn b/interfaces/kits/js/napi/BUILD.gn index 24566a145b3810a84cb1886920aea6fe8080d7c2..3c8e8d1114f29b17df76f7f1a8e9ffbc7f47fe09 100644 --- a/interfaces/kits/js/napi/BUILD.gn +++ b/interfaces/kits/js/napi/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2021 Huawei Device Co., Ltd. +# Copyright (c) 2021-2022 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 @@ -14,15 +14,14 @@ import("//base/powermgr/display_manager/displaymgr.gni") ohos_shared_library("brightness") { - sources = [ "${displaymgr_framework_path}/napi/brightness.cpp" ] - - include_dirs = [ - "//base/powermgr/power_manager/interfaces/innerkits/native/include", - "//foundation/ace/napi/native_engine", - "//foundation/ace/napi/interfaces/kits", - "//third_party/node/src", + sources = [ + "${displaymgr_framework_path}/napi/brightness.cpp", + "${displaymgr_framework_path}/napi/brightness_module.cpp", ] + include_dirs = + [ "//base/powermgr/power_manager/interfaces/innerkits/native/include" ] + configs = [ "${displaymgr_utils_path}:utils_config" ] deps = [ @@ -33,6 +32,7 @@ ohos_shared_library("brightness") { external_deps = [ "hiviewdfx_hilog_native:libhilog", "ipc:ipc_core", + "power_manager_native:powermgr_client", ] relative_install_dir = "module" diff --git a/service/native/include/display_power_mgr_service.h b/service/native/include/display_power_mgr_service.h index 6559752714f34a7a92e79706ea7dafae2b1c835a..faf556aac0f225baa4331dacd7492fdf29255a8f 100644 --- a/service/native/include/display_power_mgr_service.h +++ b/service/native/include/display_power_mgr_service.h @@ -41,6 +41,7 @@ public: virtual uint32_t GetBrightness(uint32_t displayId) override; virtual bool AdjustBrightness(uint32_t id, int32_t value, uint32_t duration) override; virtual bool AutoAdjustBrightness(bool enable) override; + virtual bool IsAutoAdjustBrightness() override; virtual bool SetStateConfig(uint32_t id, DisplayState state, int32_t value) override; virtual bool RegisterCallback(sptr callback) override; virtual int32_t Dump(int32_t fd, const std::vector& args) override; diff --git a/service/native/src/display_power_mgr_service.cpp b/service/native/src/display_power_mgr_service.cpp index 66c8dbcc01c213c2b3baac856a87c29e017ebd7e..67f953d8d49f0b00c5d8e07d2e504964f64c5a63 100644 --- a/service/native/src/display_power_mgr_service.cpp +++ b/service/native/src/display_power_mgr_service.cpp @@ -169,6 +169,12 @@ bool DisplayPowerMgrService::AutoAdjustBrightness(bool enable) return true; } +bool DisplayPowerMgrService::IsAutoAdjustBrightness() +{ + DISPLAY_HILOGW(MODULE_SERVICE, "Automatic brightness mode: %{public}d", autoBrightness_); + return autoBrightness_; +} + void DisplayPowerMgrService::ActivateAmbientSensor() { DISPLAY_HILOGI(MODULE_SERVICE, "ActivateAmbientSensor"); diff --git a/service/zidl/include/display_power_mgr_proxy.h b/service/zidl/include/display_power_mgr_proxy.h index 24139c5f1205eeabf4ea58ff0dc515ad0c1191f7..60a6519f9ba22403da7d1fc6f4bd74ae331ae85b 100644 --- a/service/zidl/include/display_power_mgr_proxy.h +++ b/service/zidl/include/display_power_mgr_proxy.h @@ -39,6 +39,7 @@ public: virtual uint32_t GetBrightness(uint32_t displayId) override; virtual bool AdjustBrightness(uint32_t id, int32_t value, uint32_t duration) override; virtual bool AutoAdjustBrightness(bool enable) override; + virtual bool IsAutoAdjustBrightness() override; virtual bool SetStateConfig(uint32_t id, DisplayState state, int32_t value) override; virtual bool RegisterCallback(sptr callback) override; diff --git a/service/zidl/include/display_power_mgr_stub.h b/service/zidl/include/display_power_mgr_stub.h index 6f342dfba32a75a112859e13efe56bcb25fb328f..042bccc1cb953e84146d5f154ae2957e98aad593 100644 --- a/service/zidl/include/display_power_mgr_stub.h +++ b/service/zidl/include/display_power_mgr_stub.h @@ -37,6 +37,7 @@ private: int32_t GetBrightnessStub(MessageParcel& data, MessageParcel& reply); int32_t AdjustBrightnessStub(MessageParcel& data, MessageParcel& reply); int32_t AutoAdjustBrightnessStub(MessageParcel& data, MessageParcel& reply); + int32_t IsAutoAdjustBrightnessStub(MessageParcel& data, MessageParcel& reply); int32_t SetStateConfigStub(MessageParcel& data, MessageParcel& reply); int32_t RegisterCallbackStub(MessageParcel& data, MessageParcel& reply); }; diff --git a/service/zidl/src/display_power_mgr_proxy.cpp b/service/zidl/src/display_power_mgr_proxy.cpp index 54e21e2c1113f83bebb0510c9c740db803128e32..40d95bc453d7e93fc7a5df5ebfef5c18fd2052b5 100644 --- a/service/zidl/src/display_power_mgr_proxy.cpp +++ b/service/zidl/src/display_power_mgr_proxy.cpp @@ -357,6 +357,37 @@ bool DisplayPowerMgrProxy::AutoAdjustBrightness(bool enable) return result; } +bool DisplayPowerMgrProxy::IsAutoAdjustBrightness() +{ + sptr remote = Remote(); + RETURN_IF_WITH_RET(remote == nullptr, false); + + bool result = false; + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteInterfaceToken(DisplayPowerMgrProxy::GetDescriptor())) { + DISPLAY_HILOGE(MODULE_INNERKIT, "DisplayPowerMgrProxy::%{public}s write descriptor failed!", __func__); + return result; + } + + int ret = remote->SendRequest(static_cast(IDisplayPowerMgr::IS_AUTO_ADJUST_BRIGHTNESS), + data, reply, option); + if (ret != ERR_OK) { + DISPLAY_HILOGE(MODULE_INNERKIT, "DisplayPowerMgrProxy::%{public}s SendRequest is failed: %{public}d", + __func__, ret); + return result; + } + + if (!reply.ReadBool(result)) { + DISPLAY_HILOGE(MODULE_INNERKIT, "Readback fail!"); + return result; + } + + return result; +} + bool DisplayPowerMgrProxy::SetStateConfig(uint32_t id, DisplayState state, int32_t value) { sptr remote = Remote(); diff --git a/service/zidl/src/display_power_mgr_stub.cpp b/service/zidl/src/display_power_mgr_stub.cpp index b9c6698c17dda41c30677d3d2fbb6e4ccddaf1b2..708c111acf6237276258eb8d24f0334fec66b29e 100644 --- a/service/zidl/src/display_power_mgr_stub.cpp +++ b/service/zidl/src/display_power_mgr_stub.cpp @@ -69,6 +69,9 @@ int32_t DisplayPowerMgrStub::OnRemoteRequest(uint32_t code, MessageParcel &data, case static_cast(IDisplayPowerMgr::AUTO_ADJUST_BRIGHTNESS): ret = AutoAdjustBrightnessStub(data, reply); break; + case static_cast(IDisplayPowerMgr::IS_AUTO_ADJUST_BRIGHTNESS): + ret = IsAutoAdjustBrightnessStub(data, reply); + break; case static_cast(IDisplayPowerMgr::SET_STATE_CONFIG): ret = SetStateConfigStub(data, reply); break; @@ -230,6 +233,16 @@ int32_t DisplayPowerMgrStub::AutoAdjustBrightnessStub(MessageParcel& data, Messa return ERR_OK; } +int32_t DisplayPowerMgrStub::IsAutoAdjustBrightnessStub(MessageParcel& data, MessageParcel& reply) +{ + bool ret = IsAutoAdjustBrightness(); + if (!reply.WriteBool(ret)) { + DISPLAY_HILOGE(MODULE_SERVICE, "Failed to write IsAutoAdjustBrightnessStub return value"); + return E_WRITE_PARCEL_ERROR; + } + return ERR_OK; +} + int32_t DisplayPowerMgrStub::SetStateConfigStub(MessageParcel& data, MessageParcel& reply) { uint32_t id = 0; diff --git a/test/native/BUILD.gn b/test/BUILD.gn similarity index 80% rename from test/native/BUILD.gn rename to test/BUILD.gn index 710c7a5d4bdcaaaa7d6d4611ebf89c687134617b..c18be82fc25b0497db9acd2eff84c96aa664bf3b 100644 --- a/test/native/BUILD.gn +++ b/test/BUILD.gn @@ -16,5 +16,10 @@ import("//build/test.gni") group("displaymgr_native_test") { testonly = true - deps = [ "unittest:unittest_display_mgr_service" ] + deps = [ "unittest/common/native:unittest_display_mgr_service" ] +} + +group("displaymgr_js_test") { + testonly = true + deps = [ "unittest/common/napi:PowerMgrDisplayJsTest" ] } diff --git a/test/unittest/common/napi/BUILD.gn b/test/unittest/common/napi/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..e0250f7bb421f575b06988bd4ede881b9626cdf4 --- /dev/null +++ b/test/unittest/common/napi/BUILD.gn @@ -0,0 +1,23 @@ +# Copyright (c) 2022 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/display_manager/displaymgr.gni") +import("//build/test.gni") + +module_output_path = "${displaymgr_native_part_name}/displaymgr_native" + +ohos_js_unittest("PowerMgrDisplayJsTest") { + module_out_path = module_output_path + hap_profile = "./config.json" + certificate_profile = "//test/developertest/signature/openharmony_sx.p7b" +} diff --git a/test/unittest/common/napi/config.json b/test/unittest/common/napi/config.json new file mode 100644 index 0000000000000000000000000000000000000000..32d81f9c3d868a075e43cadae66c247e5c4591c5 --- /dev/null +++ b/test/unittest/common/napi/config.json @@ -0,0 +1,60 @@ +{ + "app": { + "bundleName": "com.ohos.PowerMgrDisplayUnitTest", + "vendor": "ohos", + "version": { + "code": 1, + "name": "1.0" + }, + "apiVersion": { + "compatible": 4, + "target": 7 + } + }, + "deviceConfig": {}, + "module": { + "package": "com.ohos.PowerMgrDisplayUnitTest", + "name": ".MyApplication", + "deviceType": [ + "phone" + ], + "distro": { + "deliveryWithInstall": true, + "moduleName": "entry", + "moduleType": "entry" + }, + "abilities": [ + { + "visible": true, + "skills": [ + { + "entities": [ + "entity.system.home" + ], + "actions": [ + "action.system.home" + ] + } + ], + "name": "com.ohos.PowerMgrDisplayUnitTest.MainAbility", + "icon": "$media:icon", + "description": "$string:mainability_description", + "label": "MyApplication", + "type": "page", + "launchType": "standard" + } + ], + "js": [ + { + "pages": [ + "pages/index/index" + ], + "name": "default", + "window": { + "designWidth": 720, + "autoDesignWidth": false + } + } + ] + } + } diff --git a/test/unittest/common/napi/napi_system_brightness.test.js b/test/unittest/common/napi/napi_system_brightness.test.js new file mode 100644 index 0000000000000000000000000000000000000000..6c4261fa663da30377b6fe7800ac07558763638d --- /dev/null +++ b/test/unittest/common/napi/napi_system_brightness.test.js @@ -0,0 +1,515 @@ +/* + * Copyright (C) 2022 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 brightness from "@system.brightness"; +import power from '@ohos.power'; + +import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from 'deccjsunit/index'; + +const INPUT_ERROR_CODE_CODE = 202; +const COMMON_ERROR_COED = 200; +const SET_VALUE_MSG = "setValue: value is not an available number"; +const SET_MODE_MSG = "setMode: value is not an available number"; +const SET_KEEP_SCREEN_ON_MSG = "value is not an available boolean"; + +function isNotSupported(data) { + return data === "setMode: Auto adjusting brightness is not supported"; +} + +function sleep(time){ + return new Promise((resolve) => setTimeout(resolve, time)); +} + +describe('PowerMgrDisplayUnitTest', function () { + beforeAll(function() { + /* + * @tc.setup: setup invoked before all test cases + */ + console.info('PowerMgrDisplayUnitTest beforeAll called'); + }) + + afterAll(function() { + /* + * @tc.teardown: teardown invoked after all test cases + */ + console.info('PowerMgrDisplayUnitTest afterAll called'); + }) + + beforeEach(function() { + /* + * @tc.setup: setup invoked before each test case + */ + console.info('PowerMgrDisplayUnitTest beforeEach called'); + }) + + afterEach(function() { + /* + * @tc.teardown: teardown invoked after each test case + */ + console.info('PowerMgrDisplayUnitTest afterEach called'); + }) + + /** + * @tc.number PowerMgrDisplayUnitTest001 + * @tc.name get_value_success + * @tc.desc Get brightness success + */ + it('get_value_success', 0, function () { + let execSucc = false; + let execComplete = false; + let currValue = 100; + let setValue = 120; + brightness.getValue({ + success: (data) => { + currValue = data.value; + let value = (data.value > 0) && (data.value <= 255); + expect(value).assertTrue(); + } + }) + brightness.setValue({ + value: setValue + }); + brightness.getValue({ + success: (data) => { + execSucc = true; + expect(setValue === data.value).assertTrue(); + }, + fail: (data, code) => { + console.log("get_value_success, data: " + data + ", code: " + code) + expect().assertFail(); + }, + complete: () => { + execComplete = true; + console.log("The device information is obtained successfully. Procedure"); + } + }) + expect(execSucc).assertTrue(); + expect(execComplete).assertTrue(); + + brightness.setValue({ + value: currValue + }); + }); + + /** + * @tc.number PowerMgrDisplayUnitTest002 + * @tc.name get_value_success_not_must_test + * @tc.desc Get brightness + */ + it('get_status_test_success_not_must', 0, function () { + let execComplete = false; + brightness.getValue({ + fail: (data, code) => { + console.log("get_status_test_success_not_must, data: " + data + ", code: " + code); + expect().assertFail(); + }, + complete: () => { + execComplete = true; + console.log("The device information is obtained successfully. Procedure"); + } + }); + expect(execComplete).assertTrue(); + }); + + /** + * @tc.number PowerMgrDisplayUnitTest003 + * @tc.name set_value_success_all + * @tc.desc Set brightness success + */ + it('set_value_success_all', 0, function () { + let execSucc = false; + let execComplete = false; + let setValue = 200; + let currValue = 100; + brightness.getValue({ + success: (data) => { + currValue = data.value; + } + }) + + brightness.setValue({ + value: setValue, + success: () => { + execSucc = true; + brightness.getValue({ + success: (data) => { + expect(data.value === setValue).assertTrue(); + } + }) + }, + fail: (data, code) => { + console.log("set_value_success_all, data: " + data + ", code: " + code); + expect().assertFail(); + }, + complete: () => { + execComplete = true; + console.log("The device information is obtained successfully. Procedure"); + } + }) + expect(execSucc).assertTrue(); + expect(execComplete).assertTrue(); + + brightness.setValue({ + value: currValue + }); + }); + + /** + * @tc.number PowerMgrDisplayUnitTest004 + * @tc.name set_value_success_value + * @tc.desc Set brightness success + */ + it('set_value_success_value', 0, function () { + let setValue = 50; + let currValue = 100; + brightness.getValue({ + success: (data) => { + currValue = data.value; + } + }) + brightness.setValue({ value: setValue }); + brightness.getValue({ + success: (data) => { + console.log("set_value_success_value, brightness: " + data.value); + expect(data.value === setValue).assertTrue(); + } + }); + brightness.setValue({ value: currValue }); + }); + + /** + * @tc.number PowerMgrDisplayUnitTest005 + * @tc.name set_value_string + * @tc.desc Set brightness fail + */ + it('set_value_string', 0, function () { + let setValue = "50"; + brightness.setValue({ + value: setValue, + success: () => { + console.log("set_value_string success"); + expect().assertFail(); + }, + fail: (data, code) => { + console.log("set_value_string, data: " + data + ", code: " + code); + expect(code === NPUT_ERROR_CODE).assertTrue(); + expect(data === SET_VALUE_MSG).assertTrue(); + } + }); + }); + + /** + * @tc.number PowerMgrDisplayUnitTest006 + * @tc.name set_value_min_value + * @tc.desc Set brightness min value + */ + it('set_value_min_value', 0, function () { + let setValue = 0; + let currValue = 100; + brightness.getValue({ + success: (data) => { + currValue = data.value; + } + }); + if (false) { + brightness.setValue({ + value: setValue, + success: () => { + console.log("set_value_min_value success"); + brightness.getValue({ + success: (data) => { + console.log("set_value_min_value: value: " + data.value); + expect(1 === data.value).assertTrue(); + } + }); + }, + fail: (data, code) => { + console.log("set_value_min_value, data: " + data + ", code: " + code); + expect().assertFail(); + } + }); + } + + brightness.setValue({ value: currValue }); + }); + + /** + * @tc.number PowerMgrDisplayUnitTest007 + * @tc.name set_value_max_value + * @tc.desc Set brightness max value + */ + it('set_value_max_value', 0, function () { + let setValue = 500; + let currValue = 100; + brightness.getValue({ + success: (data) => { + console.log("set_value_max_value: get value: " + data.value); + currValue = data.value; + } + }); + brightness.setValue({ + value: setValue, + success: () => { + console.log("set_value_max_value success"); + brightness.getValue({ + success: (data) => { + console.log("set_value_max_value: value: " + data.value); + expect(255 === data.value).assertTrue(); + } + }); + }, + fail: (data, code) => { + console.log("set_value_max_value, data: " + data + ", code: " + code); + expect().assertFail(); + } + }); + + brightness.setValue({ value: currValue }); + }); + + /** + * @tc.number PowerMgrDisplayUnitTest008 + * @tc.name get_mode_success + * @tc.desc Get mode success + */ + it('get_mode_success', 0, function () { + let execSucc = false; + let execComplete = false; + let modeVal = 0; + let exec = true; + brightness.getMode({ + success: (data) => { + console.log("get_mode_success: get mode: " + data.mode); + modeVal = data.mode; + } + }); + brightness.setMode({ + mode: modeVal ? 0 : 1, + fail: (data, code) => { + console.log("get_mode_success, data: " + data + ", code: " + code); + exec = isNotSupported(data) ? false : true; + } + }); + if (!exec) { + return; + } + brightness.getMode({ + success: (data) => { + execSucc = true; + expect(data.mode === !modeVal).assertTrue() ; + }, + fail: (data, code) => { + console.log("get_mode_success, data: " + data + ", code: " + code); + expect().assertFail(); + }, + complete: () => { + execComplete = true; + console.log("The device information is obtained successfully. Procedure"); + } + }); + expect(execSucc).assertTrue(); + expect(execComplete).assertTrue(); + + brightness.setMode({ mode: modeVal }); + }); + + /** + * @tc.number PowerMgrDisplayUnitTest009 + * @tc.name get_mode_success_null + * @tc.desc Get mode success is null + */ + it('get_mode_success_null', 0, function () { + let execComplete = false; + brightness.getMode({ + fail: (data, code) => { + console.log("get_mode_success_null, data: " + data + ", code: " + code); + expect().assertFail(); + }, + complete: () => { + execComplete = true; + console.log("The device information is obtained successfully. Procedure"); + } + }); + expect(execComplete).assertTrue(); + }); + + /** + * @tc.number PowerMgrDisplayUnitTest0010 + * @tc.name set_mode_success + * @tc.desc set mode success + */ + it('set_mode_success', 0, function () { + let execSucc = false; + let execComplete = false; + let modeVal = 0; + brightness.getMode({ + success: (data) => { + modeVal = data.mode; + } + }); + + brightness.setMode({ + mode: modeVal ? 0 : 1, + success: () => { + execSucc = true; + console.log("set_mode_success success"); + brightness.getMode({ + success: (data) => { + console.log("set_mode_success, data: " + data.mode); + expect(data.mode === !modeVal).assertTrue(); + } + }); + }, + fail: (data, code) => { + if (!isNotSupported(data)) { + console.log("set_mode_success, data: " + data + ", code: " + code); + expect().assertFail(); + } else { + console.log("set_mode_success not supported"); + execSucc = true; + expect(isNotSupported(data)).assertTrue(); + } + }, + complete: () => { + execComplete = true; + console.log("The device information is obtained successfully. Procedure"); + } + }); + expect(execSucc).assertTrue(); + expect(execComplete).assertTrue(); + + brightness.setMode({ mode: modeVal }); + }); + + /** + * @tc.number PowerMgrDisplayUnitTest011 + * @tc.name set_mode_string + * @tc.desc set mode string + */ + it('set_mode_fail', 0, function () { + let execComplete = false; + brightness.setMode({ + mode: "0", + success: () => { + expect().assertFail(); + }, + fail: (data, code) => { + console.log("set_mode_fail, data: " + data + ", code: " + code); + expect(code === INPUT_ERROR_CODE_CODE).assertTrue(); + expect(data === SET_MODE_MSG).assertTrue(); + }, + complete: () => { + execComplete = true; + console.log("The device information is obtained successfully. Procedure"); + } + }); + expect(execComplete).assertTrue(); + }); + + /** + * @tc.number PowerMgrDisplayUnitTest012 + * @tc.name set_keep_screen_on_true + * @tc.desc set keep screen on true + */ + it('set_keep_screen_on_true', 0, async function () { + let execSucc = false; + let execComplete = false; + let sleepTime = 35 * 1000; + brightness.setKeepScreenOn({ + keepScreenOn: true, + success: () => { + execSucc = true + }, + fail: (data, code) => { + console.log("set_keep_screen_on, data: " + data + ", code: " + code); + expect().assertFail(); + }, + complete: () => { + execComplete = true; + console.log("The device information is obtained successfully. Procedure"); + } + }); + expect(execSucc).assertTrue(); + expect(execComplete).assertTrue(); + + await sleep(sleepTime); + power.isScreenOn().then(screenOn => { + console.info('The current screenOn is ' + screenOn); + expect(screenOn).assertTrue(); + }).catch(error => { + console.log('isScreenOn error: ' + error); + }) + }); + + /** + * @tc.number PowerMgrDisplayUnitTest013 + * @tc.name set_keep_screen_on_false + * @tc.desc set keep screen on false + */ + it('set_keep_screen_on_false', 0, async function () { + let execSucc = false; + let execComplete = false; + let sleepTime = 35 * 1000; + brightness.setKeepScreenOn({ + keepScreenOn: false, + success: () => { + execSucc = true; + }, + fail: (data, code) => { + console.log("set_keep_screen_on_false, data: " + data + ", code: " + code); + expect().assertFail(); + }, + complete: () => { + execComplete = true; + console.log("The device information is obtained successfully. Procedure"); + } + }); + expect(execSucc).assertTrue(); + expect(execComplete).assertTrue(); + + await sleep(sleepTime); + power.isScreenOn().then(screenOn => { + console.info('set_keep_screen_on_false The current screenOn is ' + screenOn); + expect(screenOn).assertFalse(); + }).catch(error => { + console.log('set_keep_screen_on_false isScreenOn error: ' + error); + }) + }); + + /** + * @tc.number PowerMgrDisplayUnitTest014 + * @tc.name set_keep_screen_on_not_bool + * @tc.desc set keep screen on fail + */ + it('set_keep_screen_on_not_bool', 0, function () { + let execComplete = false; + brightness.setKeepScreenOn({ + keepScreenOn: "0", + success: () => { + expect().assertFail(); + }, + fail: (data, code) => { + console.log("set_keep_screen_on_not_bool, data: " + data + ", code: " + code); + expect(data === SET_KEEP_SCREEN_ON_MSG).assertTrue(); + expect(code === INPUT_ERROR_CODE_CODE).assertTrue(); + }, + complete: () => { + execComplete = true; + console.log("The device information is obtained successfully. Procedure"); + } + }); + expect(execComplete).assertTrue(); + }); +}); \ No newline at end of file diff --git a/test/native/unittest/BUILD.gn b/test/unittest/common/native/BUILD.gn similarity index 100% rename from test/native/unittest/BUILD.gn rename to test/unittest/common/native/BUILD.gn diff --git a/test/native/unittest/include/display_power_mgr_service_test.h b/test/unittest/common/native/include/display_power_mgr_service_test.h similarity index 100% rename from test/native/unittest/include/display_power_mgr_service_test.h rename to test/unittest/common/native/include/display_power_mgr_service_test.h diff --git a/test/native/unittest/src/display_power_mgr_brightness_test.cpp b/test/unittest/common/native/src/display_power_mgr_brightness_test.cpp similarity index 100% rename from test/native/unittest/src/display_power_mgr_brightness_test.cpp rename to test/unittest/common/native/src/display_power_mgr_brightness_test.cpp diff --git a/test/native/unittest/src/display_power_mgr_service_test.cpp b/test/unittest/common/native/src/display_power_mgr_service_test.cpp similarity index 100% rename from test/native/unittest/src/display_power_mgr_service_test.cpp rename to test/unittest/common/native/src/display_power_mgr_service_test.cpp