diff --git a/frameworks/napi/brightness.cpp b/frameworks/napi/brightness.cpp index ffddf099d96c383cade4603879e25fe17a999f0a..3404aac28e3138ea8e8cd612d78603025e2042ef 100644 --- a/frameworks/napi/brightness.cpp +++ b/frameworks/napi/brightness.cpp @@ -31,10 +31,6 @@ 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"; @@ -42,18 +38,22 @@ 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 SET_VALUE_ERROR_MGR = "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_MODE_ERROR_MGR = "value is not an available number"; +const std::string SET_MODE_NOT_SUPPORTED_ERROR_MGR = "Auto adjusting brightness is not supported"; const std::string SET_KEEP_SCREENON_ERROR_MGR = "value is not an available boolean"; } +const std::string Brightness::BRIGHTNESS_VALUE = "value"; +const std::string Brightness::BRIGHTNESS_MODE = "mode"; +const std::string Brightness::KEEP_SCREENON = "keepScreenOn"; -Brightness::Brightness(napi_env env) : env_(env) +Brightness::Brightness(napi_env env, std::shared_ptr runningLock) + : env_(env), runningLock_(runningLock) { } -void Brightness::GetValue(napi_value options) +void Brightness::GetValue() { uint32_t brightness = brightnessInfo_.GetBrightness(); if (BRIGHTNESS_OFF >= brightness || brightness > MAX_BRIGHTNESS) { @@ -61,78 +61,78 @@ void Brightness::GetValue(napi_value options) } else { result_.SetResult(BRIGHTNESS_VALUE, brightness); } - ExecuteCallback(options); + ExecuteCallback(); } -void Brightness::SetValue(napi_callback_info info) +void Brightness::SetValue(napi_value& number) { - 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); + 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); +} - 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; +void Brightness::SystemSetValue() +{ + DISPLAY_HILOGD(MODULE_JNI, "System brightness interface"); + if (napiValRef_ == nullptr) { + result_.Error(INPUT_ERROR_CODE, SET_VALUE_ERROR_MGR); + } else { + int32_t brightness = MIN_BRIGHTNESS; + napi_value napiVal = nullptr; + napi_get_reference_value(env_, napiValRef_, &napiVal); + napi_get_value_int32(env_, napiVal, &brightness); + brightnessInfo_.SetBrightness(brightness); + napi_delete_reference(env_, napiValRef_); } - DISPLAY_HILOGW(MODULE_JNI, "SetValue: No valid parameters"); + ExecuteCallback(); } -void Brightness::GetMode(napi_value options) +void Brightness::GetMode() { int32_t isAuto = brightnessInfo_.GetAutoMode(); result_.SetResult(BRIGHTNESS_MODE, isAuto); - ExecuteCallback(options); + ExecuteCallback(); } -void Brightness::SetMode(napi_value options) +void Brightness::SetMode() { DISPLAY_HILOGD(MODULE_JNI, "Set auto brightness"); - napi_value napiMode = GetOptions(options, BRIGHTNESS_MODE, napi_number); - if (napiMode == nullptr) { + if (napiValRef_ == nullptr) { result_.Error(INPUT_ERROR_CODE, SET_MODE_ERROR_MGR); } else { int32_t mode = 0; + napi_value napiMode = nullptr; + napi_get_reference_value(env_, napiValRef_, &napiMode); napi_get_value_int32(env_, napiMode, &mode); if (!brightnessInfo_.SetAutoMode(static_cast(mode))) { result_.Error(COMMON_ERROR_COED, SET_MODE_NOT_SUPPORTED_ERROR_MGR); } + napi_delete_reference(env_, napiValRef_); } - ExecuteCallback(options); + ExecuteCallback(); } -void Brightness::SetKeepScreenOn(napi_value options, std::shared_ptr& runningLock) +void Brightness::SetKeepScreenOn() { DISPLAY_HILOGD(MODULE_JNI, "Set keep screen on"); - napi_value napiKeep = GetOptions(options, KEEP_SCREENON, napi_boolean); - if (napiKeep == nullptr) { + if (napiValRef_ == nullptr) { result_.Error(INPUT_ERROR_CODE, SET_KEEP_SCREENON_ERROR_MGR); } else { + napi_value napiKeep = nullptr; + napi_get_reference_value(env_, napiValRef_, &napiKeep); bool screenOn = false; napi_get_value_bool(env_, napiKeep, &screenOn); - brightnessInfo_.ScreenOn(screenOn, runningLock); + brightnessInfo_.ScreenOn(screenOn, runningLock_); + napi_delete_reference(env_, napiValRef_); } - ExecuteCallback(options); + ExecuteCallback(); } -napi_value Brightness::GetCallbackInfo(napi_callback_info info, napi_valuetype checkType) +napi_value Brightness::GetCallbackInfo(napi_callback_info& info, napi_valuetype checkType) { size_t argc = MAX_ARGC; napi_value argv[argc]; @@ -153,6 +153,35 @@ napi_value Brightness::GetCallbackInfo(napi_callback_info info, napi_valuetype c return options; } +bool Brightness::CreateCallbackRef(napi_value& options) +{ + RETURN_IF_WITH_RET(!CheckValueType(options, napi_object), false); + + napi_value succCallBack = GetOptions(options, FUNC_SUCEESS_NAME, napi_function); + if (succCallBack != nullptr) { + napi_create_reference(env_, succCallBack, 1, &successRef_); + } + + napi_value failCallBack = GetOptions(options, FUNC_FAIL_NAME, napi_function); + if (failCallBack != nullptr) { + napi_create_reference(env_, failCallBack, 1, &failRef_); + } + + napi_value completeCallBack = GetOptions(options, FUNC_COMPLETE_NAME, napi_function); + if (completeCallBack != nullptr) { + napi_create_reference(env_, completeCallBack, 1, &completeRef_); + } + return true; +} + +void Brightness::CreateValueRef(napi_value& options, const std::string& valName, napi_valuetype checkType) +{ + napi_value value = GetOptions(options, valName, checkType); + if (value != nullptr) { + napi_create_reference(env_, value, 1, &napiValRef_); + } +} + void Brightness::Result::Error(int32_t code, const std::string& msg) { code_ = code; @@ -224,18 +253,20 @@ bool Brightness::BrightnessInfo::SetAutoMode(bool mode) 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(); + if (runningLock != nullptr) { + 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) +void Brightness::ExecuteCallback() { 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); + CallFunction(successRef_, argc, result ? &result : nullptr); } if (error) { @@ -243,13 +274,13 @@ void Brightness::ExecuteCallback(napi_value options) size_t argc = MAX_FAIL_ARGC; napi_value argv[argc]; result_.GetError(env_, argv, argc); - CallFunction(options, FUNC_FAIL_NAME, argc, argv); + CallFunction(failRef_, argc, argv); } DISPLAY_HILOGI(MODULE_JNI, "Call the js complete method"); - CallFunction(options, FUNC_COMPLETE_NAME, 0, nullptr); + CallFunction(completeRef_, 0, nullptr); } -bool Brightness::CheckValueType(napi_value value, napi_valuetype checkType) +bool Brightness::CheckValueType(napi_value& value, napi_valuetype checkType) { napi_valuetype valueType = napi_undefined; napi_typeof(env_, value, &valueType); @@ -260,7 +291,7 @@ bool Brightness::CheckValueType(napi_value value, napi_valuetype checkType) return true; } -napi_value Brightness::GetOptions(napi_value options, const std::string& name, napi_valuetype checkType) +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); @@ -275,16 +306,18 @@ napi_value Brightness::GetOptions(napi_value options, const std::string& name, n return property; } -void Brightness::CallFunction(napi_value options, const std::string& name, size_t argc, napi_value* response) +void Brightness::CallFunction(napi_ref& callbackRef, size_t argc, napi_value* response) { - napi_value optionsFunc = GetOptions(options, name, napi_function); - RETURN_IF(optionsFunc == nullptr); + RETURN_IF(callbackRef == nullptr); napi_value callResult = 0; - napi_status status = napi_call_function(env_, nullptr, optionsFunc, argc, response, &callResult); + napi_value callback = nullptr; + napi_get_reference_value(env_, callbackRef, &callback); + napi_status status = napi_call_function(env_, nullptr, callback, argc, response, &callResult); if (status != napi_ok) { - DISPLAY_HILOGW(MODULE_JNI, "Failed to call the %{public}s callback function", name.c_str()); + DISPLAY_HILOGW(MODULE_JNI, "Failed to call the callback function"); } + napi_delete_reference(env_, callbackRef); } } // namespace DisplayPowerMgr } // namespace OHOS diff --git a/frameworks/napi/brightness.h b/frameworks/napi/brightness.h index edb8ee2615497ac4965eedc06c42936e926c2a7b..48aa85b8ca650d66b52782f7b408d95516483557 100644 --- a/frameworks/napi/brightness.h +++ b/frameworks/napi/brightness.h @@ -28,13 +28,22 @@ 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); + explicit Brightness(napi_env env, std::shared_ptr runningLock = nullptr); + void GetValue(); + void SetValue(napi_value& number); + void SystemSetValue(); + void GetMode(); + void SetMode(); + void SetKeepScreenOn(); + napi_value GetCallbackInfo(napi_callback_info& info, napi_valuetype checkType); + bool CreateCallbackRef(napi_value& options); + void CreateValueRef(napi_value& options, const std::string& valName, napi_valuetype checkType); + + napi_async_work asyncWork = nullptr; + + static const std::string BRIGHTNESS_VALUE; + static const std::string BRIGHTNESS_MODE; + static const std::string KEEP_SCREENON; private: class Result { public: @@ -64,14 +73,20 @@ private: 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); + void ExecuteCallback(); + bool CheckValueType(napi_value& value, napi_valuetype checkType); + napi_value GetOptions(napi_value& options, const std::string& name, napi_valuetype checkType); + void CallFunction(napi_ref& callbackRef, size_t argc, napi_value* response); napi_env env_; Result result_; BrightnessInfo brightnessInfo_; + + napi_ref successRef_ = nullptr; + napi_ref failRef_ = nullptr; + napi_ref completeRef_ = nullptr; + napi_ref napiValRef_ = nullptr; + std::shared_ptr runningLock_ = nullptr; }; } // namespace DisplayPowerMgr } // namespace OHOS diff --git a/frameworks/napi/brightness_module.cpp b/frameworks/napi/brightness_module.cpp index 860f74757ae7a0a7dab9ca5e98217f12cd60f143..ca3a8d4a979d3e64af9f1c676841e89d45a679b0 100644 --- a/frameworks/napi/brightness_module.cpp +++ b/frameworks/napi/brightness_module.cpp @@ -31,46 +31,143 @@ std::shared_ptr runningLock = PowerMgrClient::GetInstance().CreateRunningLock(std::string("KeepScreenOn"), RunningLockType::RUNNINGLOCK_SCREEN); } -static napi_value GetValue(napi_env env, napi_callback_info info) +static napi_value SyncWork(napi_env env, const std::string resName, const std::string valName, + napi_callback_info& info, napi_async_complete_callback complete) { - Brightness brightness(env); - napi_value options = brightness.GetCallbackInfo(info, napi_object); + std::unique_ptr asyncBrightness = std::make_unique(env); + RETURN_IF_WITH_RET(asyncBrightness == nullptr, nullptr); + napi_value options = asyncBrightness->GetCallbackInfo(info, napi_object); RETURN_IF_WITH_RET(options == nullptr, nullptr); - brightness.GetValue(options); + RETURN_IF_WITH_RET(!asyncBrightness->CreateCallbackRef(options), nullptr); + if (!valName.empty()) { + asyncBrightness->CreateValueRef(options, valName, napi_number); + } + + napi_value resource = nullptr; + NAPI_CALL(env, napi_create_string_utf8(env, resName.c_str(), NAPI_AUTO_LENGTH, &resource)); + napi_create_async_work( + env, + nullptr, + resource, + [](napi_env env, void *data) {}, + complete, + (void*)asyncBrightness.get(), + &asyncBrightness->asyncWork); + NAPI_CALL(env, napi_queue_async_work(env, asyncBrightness->asyncWork)); + asyncBrightness.release(); + return nullptr; } +static napi_value GetValue(napi_env env, napi_callback_info info) +{ + return SyncWork( + env, + "GetValue", + "", + info, + [](napi_env env, napi_status status, void *data) { + Brightness *asyncBrightness = (Brightness*)data; + if (asyncBrightness != nullptr) { + asyncBrightness->GetValue(); + napi_delete_async_work(env, asyncBrightness->asyncWork); + delete asyncBrightness; + } + } + ); +} + static napi_value SetValue(napi_env env, napi_callback_info info) { - Brightness brightness(env); - brightness.SetValue(info); + SyncWork( + env, + "SetValue", + Brightness::BRIGHTNESS_VALUE, + info, + [](napi_env env, napi_status status, void *data) { + Brightness *asyncBrightness = (Brightness*)data; + if (asyncBrightness != nullptr) { + asyncBrightness->SystemSetValue(); + napi_delete_async_work(env, asyncBrightness->asyncWork); + delete asyncBrightness; + } + } + ); + + std::unique_ptr asyncBrightness = std::make_unique(env); + napi_value number = asyncBrightness->GetCallbackInfo(info, napi_number); + if (number != nullptr) { + Brightness brightness(env); + brightness.SetValue(number); + } 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; + return SyncWork( + env, + "GetMode", + "", + info, + [](napi_env env, napi_status status, void *data) { + Brightness *asyncBrightness = (Brightness*)data; + if (asyncBrightness != nullptr) { + asyncBrightness->GetMode(); + napi_delete_async_work(env, asyncBrightness->asyncWork); + delete asyncBrightness; + } + } + ); } 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; + return SyncWork( + env, + "SetMode", + Brightness::BRIGHTNESS_MODE, + info, + [](napi_env env, napi_status status, void *data) { + Brightness *asyncBrightness = (Brightness*)data; + if (asyncBrightness != nullptr) { + asyncBrightness->SetMode(); + napi_delete_async_work(env, asyncBrightness->asyncWork); + delete asyncBrightness; + } + } + ); } static napi_value SetKeepScreenOn(napi_env env, napi_callback_info info) { - Brightness brightness(env); - napi_value options = brightness.GetCallbackInfo(info, napi_object); + std::unique_ptr asyncBrightness = std::make_unique(env, runningLock); + RETURN_IF_WITH_RET(asyncBrightness == nullptr, nullptr); + napi_value options = asyncBrightness->GetCallbackInfo(info, napi_object); RETURN_IF_WITH_RET(options == nullptr, nullptr); - brightness.SetKeepScreenOn(options, runningLock); + RETURN_IF_WITH_RET(!asyncBrightness->CreateCallbackRef(options), nullptr); + asyncBrightness->CreateValueRef(options, Brightness::KEEP_SCREENON, napi_boolean); + + napi_value resource = nullptr; + NAPI_CALL(env, napi_create_string_utf8(env, "SetKeepScreenOn", NAPI_AUTO_LENGTH, &resource)); + napi_create_async_work( + env, + nullptr, + resource, + [](napi_env env, void *data) {}, + [](napi_env env, napi_status status, void *data) { + Brightness *asyncBrightness = (Brightness*)data; + if (asyncBrightness != nullptr) { + asyncBrightness->SetKeepScreenOn(); + napi_delete_async_work(env, asyncBrightness->asyncWork); + delete asyncBrightness; + } + }, + (void*)asyncBrightness.get(), + &asyncBrightness->asyncWork); + NAPI_CALL(env, napi_queue_async_work(env, asyncBrightness->asyncWork)); + asyncBrightness.release(); + return nullptr; } diff --git a/test/unittest/common/napi/napi_system_brightness.test.js b/test/unittest/common/napi/napi_system_brightness.test.js index 6c4261fa663da30377b6fe7800ac07558763638d..a9c5f2a633dfc970b0c8ae43e64395e5d43506c1 100644 --- a/test/unittest/common/napi/napi_system_brightness.test.js +++ b/test/unittest/common/napi/napi_system_brightness.test.js @@ -19,15 +19,10 @@ 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_VALUE_MSG = "value is not an available number"; +const SET_MODE_MSG = "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)); } @@ -67,8 +62,6 @@ describe('PowerMgrDisplayUnitTest', function () { * @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({ @@ -83,7 +76,6 @@ describe('PowerMgrDisplayUnitTest', function () { }); brightness.getValue({ success: (data) => { - execSucc = true; expect(setValue === data.value).assertTrue(); }, fail: (data, code) => { @@ -91,12 +83,9 @@ describe('PowerMgrDisplayUnitTest', function () { expect().assertFail(); }, complete: () => { - execComplete = true; console.log("The device information is obtained successfully. Procedure"); } }) - expect(execSucc).assertTrue(); - expect(execComplete).assertTrue(); brightness.setValue({ value: currValue @@ -109,18 +98,15 @@ describe('PowerMgrDisplayUnitTest', function () { * @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(); }); /** @@ -129,8 +115,6 @@ describe('PowerMgrDisplayUnitTest', function () { * @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({ @@ -142,7 +126,6 @@ describe('PowerMgrDisplayUnitTest', function () { brightness.setValue({ value: setValue, success: () => { - execSucc = true; brightness.getValue({ success: (data) => { expect(data.value === setValue).assertTrue(); @@ -154,12 +137,9 @@ describe('PowerMgrDisplayUnitTest', function () { expect().assertFail(); }, complete: () => { - execComplete = true; console.log("The device information is obtained successfully. Procedure"); } }) - expect(execSucc).assertTrue(); - expect(execComplete).assertTrue(); brightness.setValue({ value: currValue @@ -285,8 +265,6 @@ describe('PowerMgrDisplayUnitTest', function () { * @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({ @@ -299,7 +277,7 @@ describe('PowerMgrDisplayUnitTest', function () { mode: modeVal ? 0 : 1, fail: (data, code) => { console.log("get_mode_success, data: " + data + ", code: " + code); - exec = isNotSupported(data) ? false : true; + exec = false; } }); if (!exec) { @@ -307,20 +285,16 @@ describe('PowerMgrDisplayUnitTest', function () { } brightness.getMode({ success: (data) => { - execSucc = true; - expect(data.mode === !modeVal).assertTrue() ; + expect(data.mode === (modeVal ? 0 : 1)).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 }); }); @@ -331,18 +305,15 @@ describe('PowerMgrDisplayUnitTest', function () { * @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(); }); /** @@ -351,8 +322,6 @@ describe('PowerMgrDisplayUnitTest', function () { * @tc.desc set mode success */ it('set_mode_success', 0, function () { - let execSucc = false; - let execComplete = false; let modeVal = 0; brightness.getMode({ success: (data) => { @@ -363,32 +332,18 @@ describe('PowerMgrDisplayUnitTest', function () { 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(); + expect(data.mode === (modeVal ? 0 : 1)).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 }); }); @@ -399,7 +354,6 @@ describe('PowerMgrDisplayUnitTest', function () { * @tc.desc set mode string */ it('set_mode_fail', 0, function () { - let execComplete = false; brightness.setMode({ mode: "0", success: () => { @@ -411,11 +365,9 @@ describe('PowerMgrDisplayUnitTest', function () { expect(data === SET_MODE_MSG).assertTrue(); }, complete: () => { - execComplete = true; console.log("The device information is obtained successfully. Procedure"); } }); - expect(execComplete).assertTrue(); }); /** @@ -424,25 +376,20 @@ describe('PowerMgrDisplayUnitTest', function () { * @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 + expect().assertTrue(); }, 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 => { @@ -459,25 +406,20 @@ describe('PowerMgrDisplayUnitTest', function () { * @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; + expect().assertTrue(); }, 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 => { @@ -494,7 +436,6 @@ describe('PowerMgrDisplayUnitTest', function () { * @tc.desc set keep screen on fail */ it('set_keep_screen_on_not_bool', 0, function () { - let execComplete = false; brightness.setKeepScreenOn({ keepScreenOn: "0", success: () => { @@ -506,10 +447,8 @@ describe('PowerMgrDisplayUnitTest', function () { 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