diff --git a/service/BUILD.gn b/service/BUILD.gn index 6150504ccf64815b9f0c4a133cc557e2442a46ee..f90aab58734ae5a5a05c82a00b3465507964577d 100644 --- a/service/BUILD.gn +++ b/service/BUILD.gn @@ -40,6 +40,8 @@ ohos_shared_library("displaymgrservice") { "native/src/gradual_animator.cpp", "native/src/screen_action.cpp", "native/src/screen_controller.cpp", + "native/src/operators/display_operator.cpp", + "native/src/operators/screen_operator.cpp", "zidl/src/display_power_callback_proxy.cpp", "zidl/src/display_power_mgr_stub.cpp", ] diff --git a/service/native/include/display_power_mgr_service.h b/service/native/include/display_power_mgr_service.h index aa51a65d5124fa773115c74ca7683eaa6553c0c5..e51082a336d85f3d974df39de6cf635683fef414 100644 --- a/service/native/include/display_power_mgr_service.h +++ b/service/native/include/display_power_mgr_service.h @@ -29,6 +29,7 @@ #include "iremote_object.h" #include "sensor_agent_type.h" #include "idisplay_power_callback.h" +#include "display_log.h" #include "display_power_info.h" #include "display_event_handler.h" #include "display_common.h" @@ -66,6 +67,7 @@ public: void Deinit(); static uint32_t GetSafeBrightness(uint32_t value); static double GetSafeDiscount(double discount, uint32_t brightness); + bool RegisterMockDisplay(IDisplayOperator* displayOperator); private: class CallbackDeathRecipient : public IRemoteObject::DeathRecipient { @@ -127,6 +129,7 @@ private: time_t lastLuxTime_ {0}; float lastLux_ {0}; bool luxChanged_ {false}; + std::shared_ptr displayOperator_ {nullptr}; }; } // namespace DisplayPowerMgr } // namespace OHOS diff --git a/service/native/include/operators/display_operator.h b/service/native/include/operators/display_operator.h new file mode 100644 index 0000000000000000000000000000000000000000..82e11e71d3c4ea5cd66fd4e8d7e57ebdfc475656 --- /dev/null +++ b/service/native/include/operators/display_operator.h @@ -0,0 +1,35 @@ +/* + * 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 DISPLAYMGR_DISPLAY_OPERATOR_H +#define DISPLAYMGR_DISPLAY_OPERATOR_H + +#include + +#include "idisplay_operator.h" + +namespace OHOS { +namespace DisplayPowerMgr { +class DisplayOperator : public IDisplayOperator { +public: + virtual uint32_t GetDefaultDisplayId() override; + virtual std::vector GetAllDisplayIds() override; + virtual bool SetDisplayState(Rosen::DisplayState state, Rosen::DisplayStateCallback callback) override; + virtual bool SetScreenBrightness(uint64_t screenId, uint32_t level) override; + virtual uint32_t GetScreenBrightness(uint64_t screenId) const override; +}; +} // namespace DisplayPowerMgr +} // namespace OHOS +#endif // DISPLAYMGR_DISPLAY_OPERATOR_H diff --git a/service/native/include/operators/idisplay_operator.h b/service/native/include/operators/idisplay_operator.h new file mode 100644 index 0000000000000000000000000000000000000000..fade719081f77967d7dc07035972753ba8d151d3 --- /dev/null +++ b/service/native/include/operators/idisplay_operator.h @@ -0,0 +1,37 @@ +/* + * 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 DISPLAYMGR_IDISPLAY_OPERATOR_H +#define DISPLAYMGR_IDISPLAY_OPERATOR_H + +#include + +#include "dm_common.h" + +namespace OHOS { +namespace DisplayPowerMgr { +class IDisplayOperator { +public: + IDisplayOperator() = default; + virtual ~IDisplayOperator() = default; + virtual uint32_t GetDefaultDisplayId() = 0; + virtual std::vector GetAllDisplayIds() = 0; + virtual bool SetDisplayState(Rosen::DisplayState state, Rosen::DisplayStateCallback callback) = 0; + virtual bool SetScreenBrightness(uint64_t screenId, uint32_t level) = 0; + virtual uint32_t GetScreenBrightness(uint64_t screenId) const = 0; +}; +} // namespace DisplayPowerMgr +} // namespace OHOS +#endif // DISPLAYMGR_IDISPLAY_OPERATOR_H diff --git a/service/native/include/operators/iscreen_operator.h b/service/native/include/operators/iscreen_operator.h new file mode 100644 index 0000000000000000000000000000000000000000..c5e8054425093286bb1d082cca5e8d842295537e --- /dev/null +++ b/service/native/include/operators/iscreen_operator.h @@ -0,0 +1,34 @@ +/* + * 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 DISPLAYMGR_ISCREEN_OPERATOR_H +#define DISPLAYMGR_ISCREEN_OPERATOR_H + +#include + +#include "dm_common.h" + +namespace OHOS { +namespace DisplayPowerMgr { +class IScreenOperator { +public: + IScreenOperator() = default; + virtual ~IScreenOperator() = default; + virtual Rosen::ScreenPowerState GetScreenPower(uint64_t screenId) = 0; + virtual bool SetScreenPowerForAll(Rosen::ScreenPowerState state, Rosen::PowerStateChangeReason reason) = 0; +}; +} // namespace DisplayPowerMgr +} // namespace OHOS +#endif // DISPLAYMGR_ISCREEN_OPERATOR_H diff --git a/service/native/include/operators/screen_operator.h b/service/native/include/operators/screen_operator.h new file mode 100644 index 0000000000000000000000000000000000000000..8f8fa4de3826879de205a40db31b1a27f2766d87 --- /dev/null +++ b/service/native/include/operators/screen_operator.h @@ -0,0 +1,32 @@ +/* + * 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 DISPLAYMGR_SCREEN_OPERATOR_H +#define DISPLAYMGR_SCREEN_OPERATOR_H + +#include + +#include "iscreen_operator.h" + +namespace OHOS { +namespace DisplayPowerMgr { +class ScreenOperator : public IScreenOperator { +public: + virtual Rosen::ScreenPowerState GetScreenPower(uint64_t screenId) override; + virtual bool SetScreenPowerForAll(Rosen::ScreenPowerState state, Rosen::PowerStateChangeReason reason) override; +}; +} // namespace DisplayPowerMgr +} // namespace OHOS +#endif // DISPLAYMGR_SCREEN_OPERATOR_H diff --git a/service/native/include/screen_action.h b/service/native/include/screen_action.h index 5e88bfae08cd15ef86200b21e58bc063c7019517..5420c181473d9064ea433a0d4f75e10407d45a46 100644 --- a/service/native/include/screen_action.h +++ b/service/native/include/screen_action.h @@ -23,17 +23,16 @@ #include #include "display_power_info.h" +#include "operators/idisplay_operator.h" +#include "operators/iscreen_operator.h" namespace OHOS { namespace DisplayPowerMgr { class ScreenAction { public: - ScreenAction(uint32_t displayId); + ScreenAction(uint32_t displayId, const std::shared_ptr& displayOperator); ~ScreenAction() = default; - static uint32_t GetDefaultDisplayId(); - static std::vector GetAllDisplayId(); - uint32_t GetDisplayId(); DisplayState GetDisplayState(); bool SetDisplayState(DisplayState state, const std::function& callback); @@ -46,6 +45,8 @@ private: std::mutex mutexBrightness_; uint32_t brightness_ {102}; uint32_t displayId_ {DEFAULT_DISPLAY_ID}; + std::shared_ptr displayOperator_ {nullptr}; + std::shared_ptr screenOperator_ {nullptr}; }; } // namespace DisplayPowerMgr } // namespace OHOS diff --git a/service/native/include/screen_controller.h b/service/native/include/screen_controller.h index d0ad30be87a3eb262aa1390b467773f47041cd32..de1fcddff2899ba04bfcc888390a9a607805a2d5 100644 --- a/service/native/include/screen_controller.h +++ b/service/native/include/screen_controller.h @@ -30,7 +30,8 @@ namespace OHOS { namespace DisplayPowerMgr { class ScreenController { public: - ScreenController(uint32_t displayId, const std::shared_ptr& handler); + ScreenController(uint32_t displayId, const std::shared_ptr& handler, + const std::shared_ptr& displayOperator); virtual ~ScreenController() = default; class AnimateCallbackImpl : public AnimateCallback { diff --git a/service/native/src/display_auto_brightness.cpp b/service/native/src/display_auto_brightness.cpp index 326e81ae9d96fb385ed318ea516615154228035d..5567dd55641bdc7f6257da17b06f8c2b84cf7222 100644 --- a/service/native/src/display_auto_brightness.cpp +++ b/service/native/src/display_auto_brightness.cpp @@ -59,5 +59,5 @@ uint32_t DisplayAutoBrightness::CalculateAutoBrightness(float scalar) } return brightness; } -} // namespace PowerMgr +} // namespace DisplayPowerMgr } // namespace OHOS \ No newline at end of file diff --git a/service/native/src/display_power_mgr_service.cpp b/service/native/src/display_power_mgr_service.cpp index da2d6f97c1bd79fabc4860fd275ad8effa0402a2..cc84cc5729c686179d021b8332129d007c099aa1 100644 --- a/service/native/src/display_power_mgr_service.cpp +++ b/service/native/src/display_power_mgr_service.cpp @@ -24,10 +24,10 @@ #include "screen_action.h" #include "sensor_agent.h" #include "watchdog.h" -#include "display_log.h" #include "display_auto_brightness.h" #include "display_param_helper.h" #include "permission.h" +#include "operators/display_operator.h" #include "setting_provider.h" namespace OHOS { @@ -63,10 +63,13 @@ void DisplayPowerMgrService::Init() HiviewDFX::Watchdog::GetInstance().AddThread(handlerName, handler_); } - std::vector displayIds = ScreenAction::GetAllDisplayId(); + if (displayOperator_ == nullptr) { + displayOperator_ = std::make_shared(); + } + std::vector displayIds = displayOperator_->GetAllDisplayIds(); for (const auto& id: displayIds) { DISPLAY_HILOGI(COMP_SVC, "find display, id=%{public}u", id); - controllerMap_.emplace(id, std::make_shared(id, handler_)); + controllerMap_.emplace(id, std::make_shared(id, handler_, displayOperator_)); } callback_ = nullptr; @@ -226,7 +229,7 @@ std::vector DisplayPowerMgrService::GetDisplayIds() uint32_t DisplayPowerMgrService::GetMainDisplayId() { - uint32_t id = ScreenAction::GetDefaultDisplayId(); + uint32_t id = displayOperator_->GetDefaultDisplayId(); DISPLAY_HILOGI(COMP_SVC, "GetMainDisplayId %{public}d", id); return id; } @@ -677,6 +680,16 @@ DisplayErrors DisplayPowerMgrService::GetError() return tmpError; } +bool DisplayPowerMgrService::RegisterMockDisplay(IDisplayOperator* displayOperator) +{ + if (displayOperator == nullptr) { + DISPLAY_HILOGI(LABEL_TEST, "mock display operator is nullptr"); + return false; + } + displayOperator_ = std::shared_ptr(displayOperator); + return true; +} + void DisplayPowerMgrService::CallbackDeathRecipient::OnRemoteDied(const wptr& remote) { DISPLAY_HILOGI(COMP_SVC, "CallbackDeathRecipient OnRemoteDied"); diff --git a/service/native/src/operators/display_operator.cpp b/service/native/src/operators/display_operator.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8fc04589fc8ad345caa424c16a0211f57c31be30 --- /dev/null +++ b/service/native/src/operators/display_operator.cpp @@ -0,0 +1,65 @@ +/* + * 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 "operators/display_operator.h" + +#include + +#include "display_manager.h" +#include "display_log.h" + +namespace OHOS { +namespace DisplayPowerMgr { +uint32_t DisplayOperator::GetDefaultDisplayId() +{ + std::string identity = IPCSkeleton::ResetCallingIdentity(); + uint64_t defaultId = Rosen::DisplayManager::GetInstance().GetDefaultDisplayId(); + IPCSkeleton::SetCallingIdentity(identity); + return static_cast(defaultId); +} + +std::vector DisplayOperator::GetAllDisplayIds() +{ + uint32_t defDisplayId = 0; + std::string identity = IPCSkeleton::ResetCallingIdentity(); + std::vector allIds = Rosen::DisplayManager::GetInstance().GetAllDisplayIds(); + IPCSkeleton::SetCallingIdentity(identity); + std::vector displayIds; + if (allIds.empty()) { + displayIds.push_back(defDisplayId); + return displayIds; + } + std::transform(allIds.begin(), allIds.end(), back_inserter(displayIds), [](uint64_t id) { + return static_cast(id); + }); + return displayIds; +} + +bool DisplayOperator::SetDisplayState(Rosen::DisplayState state, Rosen::DisplayStateCallback callback) +{ + return Rosen::DisplayManager::GetInstance().SetDisplayState(state, callback); +} + +bool DisplayOperator::SetScreenBrightness(uint64_t screenId, uint32_t level) +{ + return Rosen::DisplayManager::GetInstance().SetScreenBrightness(screenId, level); +} + +uint32_t DisplayOperator::GetScreenBrightness(uint64_t screenId) const +{ + return Rosen::DisplayManager::GetInstance().GetScreenBrightness(screenId); +} +} // namespace DisplayPowerMgr +} // namespace OHOS \ No newline at end of file diff --git a/service/native/src/operators/screen_operator.cpp b/service/native/src/operators/screen_operator.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1cd4cd89246886610d7f1010a7bd4b559cb098a3 --- /dev/null +++ b/service/native/src/operators/screen_operator.cpp @@ -0,0 +1,33 @@ +/* + * 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 "operators/screen_operator.h" + +#include "display_log.h" +#include "screen_manager.h" + +namespace OHOS { +namespace DisplayPowerMgr { +Rosen::ScreenPowerState ScreenOperator::GetScreenPower(uint64_t screenId) +{ + return Rosen::ScreenManager::GetInstance().GetScreenPower(screenId); +} + +bool ScreenOperator::SetScreenPowerForAll(Rosen::ScreenPowerState state, Rosen::PowerStateChangeReason reason) +{ + return Rosen::ScreenManager::GetInstance().SetScreenPowerForAll(state, reason); +} +} // namespace DisplayPowerMgr +} // namespace OHOS \ No newline at end of file diff --git a/service/native/src/screen_action.cpp b/service/native/src/screen_action.cpp index 0bd6c161838b20a822d5f812a48d6438f50c39be..a4aeb43f2f353cd5d41a386cef7f9dff7c64500f 100644 --- a/service/native/src/screen_action.cpp +++ b/service/native/src/screen_action.cpp @@ -16,40 +16,22 @@ #include "screen_action.h" #include - #include + #include "dm_common.h" -#include "display_manager.h" #include "display_log.h" -#include "screen_manager.h" +#include "operators/display_operator.h" +#include "operators/screen_operator.h" namespace OHOS { namespace DisplayPowerMgr { -ScreenAction::ScreenAction(uint32_t displayId) : displayId_(displayId) -{} - -uint32_t ScreenAction::GetDefaultDisplayId() +ScreenAction::ScreenAction(uint32_t displayId, const std::shared_ptr& displayOperator) + : displayId_(displayId) { - std::string identity = IPCSkeleton::ResetCallingIdentity(); - uint64_t defaultId = Rosen::DisplayManager::GetInstance().GetDefaultDisplayId(); - IPCSkeleton::SetCallingIdentity(identity); - return static_cast(defaultId); -} - -std::vector ScreenAction::GetAllDisplayId() -{ - std::string identity = IPCSkeleton::ResetCallingIdentity(); - std::vector allIds = Rosen::DisplayManager::GetInstance().GetAllDisplayIds(); - IPCSkeleton::SetCallingIdentity(identity); - std::vector displayIds; - if (allIds.empty()) { - displayIds.push_back(DEFAULT_DISPLAY_ID); - return displayIds; + displayOperator_ = displayOperator == nullptr ? std::make_shared() : displayOperator; + if (screenOperator_ == nullptr) { + screenOperator_ = std::make_shared(); } - std::transform(allIds.begin(), allIds.end(), back_inserter(displayIds), [](uint64_t id) { - return static_cast(id); - }); - return displayIds; } uint32_t ScreenAction::GetDisplayId() @@ -60,7 +42,7 @@ uint32_t ScreenAction::GetDisplayId() DisplayState ScreenAction::GetDisplayState() { DisplayState state = DisplayState::DISPLAY_UNKNOWN; - Rosen::ScreenPowerState powerState = Rosen::ScreenManager::GetInstance().GetScreenPower(displayId_); + Rosen::ScreenPowerState powerState = screenOperator_->GetScreenPower(displayId_); DISPLAY_HILOGI(FEAT_STATE, "ScreenPowerState=%{public}d", static_cast(powerState)); switch (powerState) { case Rosen::ScreenPowerState::POWER_ON: @@ -99,7 +81,7 @@ bool ScreenAction::SetDisplayState(DisplayState state, const std::functionSetDisplayState(rds, [callback](Rosen::DisplayState rosenState) { DISPLAY_HILOGI(FEAT_STATE, "SetDisplayState Callback:%{public}d", static_cast(rosenState)); @@ -145,7 +127,7 @@ bool ScreenAction::SetDisplayPower(DisplayState state, uint32_t reason) default: break; } - bool ret = Rosen::ScreenManager::GetInstance().SetScreenPowerForAll(status, + bool ret = screenOperator_->SetScreenPowerForAll(status, Rosen::PowerStateChangeReason::POWER_BUTTON); DISPLAY_HILOGI(FEAT_STATE, "SetScreenPowerForAll:%{public}d", ret); return true; @@ -155,7 +137,7 @@ uint32_t ScreenAction::GetBrightness() { std::lock_guard lock(mutexBrightness_); std::string identity = IPCSkeleton::ResetCallingIdentity(); - brightness_ = Rosen::DisplayManager::GetInstance().GetScreenBrightness(displayId_); + brightness_ = displayOperator_->GetScreenBrightness(displayId_); IPCSkeleton::SetCallingIdentity(identity); DISPLAY_HILOGD(FEAT_BRIGHTNESS, "displayId=%{public}u, brightness=%{public}u", displayId_, brightness_); return brightness_; @@ -168,7 +150,7 @@ bool ScreenAction::SetBrightness(uint32_t value) HiviewDFX::HiSysEvent::Write("DISPLAY", "BRIGHTNESS_NIT", HiviewDFX::HiSysEvent::EventType::STATISTIC, "BRIGHTNESS", value); std::string identity = IPCSkeleton::ResetCallingIdentity(); - bool isSucc = Rosen::DisplayManager::GetInstance().SetScreenBrightness(displayId_, value); + bool isSucc = displayOperator_->SetScreenBrightness(displayId_, value); IPCSkeleton::SetCallingIdentity(identity); std::lock_guard lock(mutexBrightness_); brightness_ = isSucc ? value : brightness_; diff --git a/service/native/src/screen_controller.cpp b/service/native/src/screen_controller.cpp index 21e7957876607a638906fb177de7408c2a87f547..5679e84acce0df2c58429a308a462b5e700ec01a 100644 --- a/service/native/src/screen_controller.cpp +++ b/service/native/src/screen_controller.cpp @@ -33,11 +33,11 @@ namespace { sptr g_brightnessObserver; } -ScreenController::ScreenController(uint32_t displayId, const shared_ptr& handler) - : handler_(handler) +ScreenController::ScreenController(uint32_t displayId, const shared_ptr& handler, + const std::shared_ptr& displayOperator) : handler_(handler) { DISPLAY_HILOGI(COMP_SVC, "ScreenController created for displayId=%{public}u", displayId); - action_ = make_shared(displayId); + action_ = make_shared(displayId, displayOperator); state_ = action_->GetDisplayState(); string name = "BrightnessController_" + to_string(displayId); diff --git a/test/BUILD.gn b/test/BUILD.gn index d89f68281dccd4172e1f70c5522ad482030dd2e1..05928f17f6f2b5a9387bd7dd29e07aa40c213c47 100644 --- a/test/BUILD.gn +++ b/test/BUILD.gn @@ -16,7 +16,10 @@ import("//build/test.gni") group("displaymgr_native_test") { testonly = true - deps = [ "unittest/common/native:unittest_display_mgr_service" ] + deps = [ + "unittest/common/native:unittest_display_mgr_service", + "unittest/common/native:unittest_display_mgr_multi", + ] } group("displaymgr_js_test") { diff --git a/test/unittest/common/native/BUILD.gn b/test/unittest/common/native/BUILD.gn index 112cc319c33d82273e5333326cabfb27c0d9875c..e69921ec0f5d4b61f9363012c486347d5e0103bb 100644 --- a/test/unittest/common/native/BUILD.gn +++ b/test/unittest/common/native/BUILD.gn @@ -21,7 +21,10 @@ module_output_path = "${displaymgr_part_name}/displaymgr" config("module_private_config") { visibility = [ ":*" ] - include_dirs = [ "include" ] + include_dirs = [ + "include", + "mock", + ] } ##############################unittest########################################## @@ -56,3 +59,35 @@ ohos_unittest("unittest_display_mgr_service") { "samgr:samgr_proxy", ] } + +ohos_unittest("unittest_display_mgr_multi") { + module_out_path = module_output_path + + sources = [ "src/display_power_mgr_multi_test.cpp" ] + + configs = [ + "${displaymgr_utils_path}:utils_config", + ":module_private_config", + ] + + deps = [ + "${displaymgr_innerkits}:displaymgr", + "${displaymgr_root_path}/service:displaymgrservice", + "//third_party/googletest:gmock_main", + "//third_party/googletest:gtest_main", + ] + + external_deps = [ + "ability_base:want", + "ability_base:zuri", + "ability_runtime:ability_manager", + "c_utils:utils", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "power_manager:powermgr_client", + "safwk:system_ability_fwk", + "samgr:samgr_proxy", + "sensor:sensor_interface_native", + "window_manager:libdm", + ] +} diff --git a/test/unittest/common/native/include/display_power_mgr_multi_test.h b/test/unittest/common/native/include/display_power_mgr_multi_test.h new file mode 100644 index 0000000000000000000000000000000000000000..a3c756ab33785d9d87eb5f345a3fb417660d7edd --- /dev/null +++ b/test/unittest/common/native/include/display_power_mgr_multi_test.h @@ -0,0 +1,32 @@ +/* + * 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 DISPLAYMGR_DISPLAY_MGR_MULTI_TEST_H +#define DISPLAYMGR_DISPLAY_MGR_MULTI_TEST_H + +#include + +namespace OHOS { +namespace DisplayPowerMgr { +class DisplayPowerMgrMultiTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp() {} + void TearDown() {} +}; +} // namespace DisplayPowerMgr +} // namespace OHOS +#endif // DISPLAYMGR_DISPLAY_MGR_MULTI_TEST_H diff --git a/test/unittest/common/native/mock/mock_display_operator.h b/test/unittest/common/native/mock/mock_display_operator.h new file mode 100644 index 0000000000000000000000000000000000000000..ad7b5de3017eee58adf66d09c6a0266ac6e7cabc --- /dev/null +++ b/test/unittest/common/native/mock/mock_display_operator.h @@ -0,0 +1,35 @@ +/* + * 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 DISPLAY_MOCK_DISPLAY_OPERATOR_H +#define DISPLAY_MOCK_DISPLAY_OPERATOR_H + +#include +#include "operators/idisplay_operator.h" + +namespace OHOS { +namespace DisplayPowerMgr { +class MockDisplayOperator : public IDisplayOperator { +public: + MOCK_METHOD0(GetDefaultDisplayId, uint32_t()); + MOCK_METHOD0(GetAllDisplayIds, std::vector()); + MOCK_METHOD2(SetDisplayState, bool(Rosen::DisplayState state, Rosen::DisplayStateCallback callback)); + MOCK_METHOD2(SetScreenBrightness, bool(uint64_t screenId, uint32_t level)); + MOCK_CONST_METHOD1(GetScreenBrightness, uint32_t(uint64_t screenId)); +}; +} // namespace DisplayPowerMgr +} // namespace OHOS + +#endif // DISPLAY_MOCK_DISPLAY_OPERATOR_H \ No newline at end of file diff --git a/test/unittest/common/native/src/display_power_mgr_multi_test.cpp b/test/unittest/common/native/src/display_power_mgr_multi_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..afd9cc62b7a8de1741b77088039e9636fb368121 --- /dev/null +++ b/test/unittest/common/native/src/display_power_mgr_multi_test.cpp @@ -0,0 +1,219 @@ +/* + * 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 "display_power_mgr_multi_test.h" + +#include "display_log.h" +#include "display_power_mgr_service.h" +#include "mock_display_operator.h" +#include "power_state_machine_info.h" + +using namespace OHOS; +using namespace OHOS::DisplayPowerMgr; +using namespace std; +using namespace testing; +using namespace testing::ext; + +namespace { +static sptr g_displayService = nullptr; +static MockDisplayOperator* g_mockDisplayOperator = nullptr; +static std::vector g_mockDisplayIds {0, 1}; +const uint32_t US_PRE_MS = 1000; +} + +void DisplayPowerMgrMultiTest::SetUpTestCase(void) +{ + g_displayService = DelayedSpSingleton::GetInstance(); + g_mockDisplayOperator = new MockDisplayOperator(); + g_displayService->RegisterMockDisplay(g_mockDisplayOperator); + + EXPECT_CALL(*g_mockDisplayOperator, GetAllDisplayIds()) + .Times(Exactly(1)) + .WillOnce(Return(g_mockDisplayIds)); + EXPECT_CALL(*g_mockDisplayOperator, GetDefaultDisplayId()) + .WillRepeatedly(Return(0)); + EXPECT_CALL(*g_mockDisplayOperator, SetScreenBrightness(_, _)) + .WillRepeatedly(Return(true)); + + g_displayService->Init(); + sleep(1); +} + +void DisplayPowerMgrMultiTest::TearDownTestCase(void) +{ + g_displayService->Deinit(); + DelayedSpSingleton::DestroyInstance(); + delete g_mockDisplayOperator; + g_mockDisplayOperator = nullptr; +} + +namespace { +/** + * @tc.name: DisplayPowerMgrMulti001 + * @tc.desc: test display service function GetDisplayIds by mock display + * @tc.type: FUNC + */ +HWTEST_F(DisplayPowerMgrMultiTest, DisplayPowerMgrMulti001, TestSize.Level0) +{ + DISPLAY_HILOGI(LABEL_TEST, "DisplayPowerMgrMulti001: fun is start"); + ASSERT_NE(g_displayService, nullptr); + std::vector actualDisplayIds = g_displayService->GetDisplayIds(); + EXPECT_EQ(2, actualDisplayIds.size()); + for (uint8_t count = 0; count < actualDisplayIds.size(); count++) { + EXPECT_EQ(g_mockDisplayIds[count], actualDisplayIds[count]); + GTEST_LOG_(INFO) << __func__ << ": mock display count = " << count << " id = " << actualDisplayIds[count]; + } + DISPLAY_HILOGI(LABEL_TEST, "DisplayPowerMgrMulti001: fun is end"); +} + +/** + * @tc.name: DisplayPowerMgrMulti002 + * @tc.desc: test display service function GetScreenBrightness by mock display + * @tc.type: FUNC + */ +HWTEST_F(DisplayPowerMgrMultiTest, DisplayPowerMgrMulti002, TestSize.Level0) +{ + DISPLAY_HILOGI(LABEL_TEST, "DisplayPowerMgrMulti002: fun is start"); + ASSERT_NE(g_displayService, nullptr); + ASSERT_NE(g_mockDisplayOperator, nullptr); + + uint32_t mockDisplayId = 1; + uint32_t mockBrightness = 200; + EXPECT_CALL(*g_mockDisplayOperator, GetScreenBrightness(Eq(mockDisplayId))) + .Times(Exactly(1)) + .WillOnce(Return(mockBrightness)); + uint32_t deviceBrightness = g_displayService->GetDeviceBrightness(mockDisplayId); + EXPECT_EQ(deviceBrightness, mockBrightness); + DISPLAY_HILOGI(LABEL_TEST, "DisplayPowerMgrMulti002: fun is end"); +} + +/** + * @tc.name: DisplayPowerMgrMulti003 + * @tc.desc: test display service function SetBrightness by mock display + * @tc.type: FUNC + * @tc.require: issuesI5I9FM + */ +HWTEST_F(DisplayPowerMgrMultiTest, DisplayPowerMgrMulti003, TestSize.Level0) +{ + DISPLAY_HILOGD(LABEL_TEST, "DisplayPowerMgrMulti003: fun is start"); + ASSERT_NE(g_displayService, nullptr); + ASSERT_NE(g_mockDisplayOperator, nullptr); + + uint32_t waitTime = 100; + uint32_t mockDisplayId = 1; + uint32_t setBrightness = 200; + EXPECT_CALL(*g_mockDisplayOperator, GetDefaultDisplayId()) + .WillRepeatedly(Return(0)); + EXPECT_CALL(*g_mockDisplayOperator, SetDisplayState(_, _)) + .WillRepeatedly(Return(true)); + EXPECT_CALL(*g_mockDisplayOperator, SetScreenBrightness(_, _)) + .WillRepeatedly(Return(true)); + bool ret = g_displayService->SetDisplayState( + mockDisplayId, + DisplayState::DISPLAY_ON, + static_cast(PowerMgr::StateChangeReason::STATE_CHANGE_REASON_UNKNOWN)); + EXPECT_TRUE(ret); + auto state = g_displayService->GetDisplayState(mockDisplayId); + EXPECT_TRUE(state == DisplayState::DISPLAY_ON); + ret = g_displayService->SetBrightness(setBrightness, mockDisplayId); + EXPECT_TRUE(ret); + usleep(waitTime * US_PRE_MS); + uint32_t brightness = g_displayService->GetBrightness(mockDisplayId); + EXPECT_EQ(brightness, setBrightness); + DISPLAY_HILOGD(LABEL_TEST, "DisplayPowerMgrMulti003: fun is end"); +} + +/** + * @tc.name: DisplayPowerMgrMulti004 + * @tc.desc: test display service function DiscountBrightness by mock display + * @tc.type: FUNC + * @tc.require: issuesI5I9FM + */ +HWTEST_F(DisplayPowerMgrMultiTest, DisplayPowerMgrMulti004, TestSize.Level0) +{ + DISPLAY_HILOGD(LABEL_TEST, "DisplayPowerMgrMulti004: fun is start"); + ASSERT_NE(g_displayService, nullptr); + ASSERT_NE(g_mockDisplayOperator, nullptr); + + uint32_t waitTime = 200; + uint32_t mockDisplayId = 1; + uint32_t setBrightness = 200; + double discount = 0.8; + double noDiscount = 1.0; + EXPECT_CALL(*g_mockDisplayOperator, GetDefaultDisplayId()) + .WillRepeatedly(Return(0)); + EXPECT_CALL(*g_mockDisplayOperator, SetDisplayState(_, _)) + .WillRepeatedly(Return(true)); + EXPECT_CALL(*g_mockDisplayOperator, SetScreenBrightness(Eq(mockDisplayId), _)) + .Times(AtLeast(2)) + .WillOnce(Return(true)) + .WillOnce([&](uint64_t screenId, uint32_t level) { + EXPECT_EQ(screenId, mockDisplayId); + EXPECT_EQ(level, static_cast(setBrightness * discount)); + return true; + }) + .WillRepeatedly(Return(true)); + bool ret = g_displayService->SetDisplayState( + mockDisplayId, + DisplayState::DISPLAY_ON, + static_cast(PowerMgr::StateChangeReason::STATE_CHANGE_REASON_UNKNOWN)); + EXPECT_TRUE(ret); + auto state = g_displayService->GetDisplayState(mockDisplayId); + EXPECT_TRUE(state == DisplayState::DISPLAY_ON); + ret = g_displayService->SetBrightness(setBrightness, mockDisplayId); + EXPECT_TRUE(ret); + ret = g_displayService->DiscountBrightness(discount, mockDisplayId); + usleep(waitTime * US_PRE_MS); + //restore discount brightness + ret = g_displayService->DiscountBrightness(noDiscount, mockDisplayId); + DISPLAY_HILOGD(LABEL_TEST, "DisplayPowerMgrMulti004: fun is end"); +} + +/** + * @tc.name: DisplayPowerMgrMulti005 + * @tc.desc: test SetBrightness is ok after test DiscountBrightness by mock display + * @tc.type: FUNC + * @tc.require: issuesI5I9FM + */ +HWTEST_F(DisplayPowerMgrMultiTest, DisplayPowerMgrMulti005, TestSize.Level0) +{ + DISPLAY_HILOGD(LABEL_TEST, "DisplayPowerMgrMulti005: fun is start"); + ASSERT_NE(g_displayService, nullptr); + ASSERT_NE(g_mockDisplayOperator, nullptr); + + uint32_t waitTime = 100; + uint32_t mockDisplayId = 1; + uint32_t setBrightness = 200; + EXPECT_CALL(*g_mockDisplayOperator, GetDefaultDisplayId()) + .WillRepeatedly(Return(0)); + EXPECT_CALL(*g_mockDisplayOperator, SetDisplayState(_, _)) + .WillRepeatedly(Return(true)); + EXPECT_CALL(*g_mockDisplayOperator, SetScreenBrightness(_, _)) + .WillRepeatedly(Return(true)); + bool ret = g_displayService->SetDisplayState( + mockDisplayId, + DisplayState::DISPLAY_ON, + static_cast(PowerMgr::StateChangeReason::STATE_CHANGE_REASON_UNKNOWN)); + EXPECT_TRUE(ret); + auto state = g_displayService->GetDisplayState(mockDisplayId); + EXPECT_TRUE(state == DisplayState::DISPLAY_ON); + ret = g_displayService->SetBrightness(setBrightness, mockDisplayId); + EXPECT_TRUE(ret); + usleep(waitTime * US_PRE_MS); + uint32_t brightness = g_displayService->GetBrightness(mockDisplayId); + EXPECT_EQ(brightness, setBrightness); + DISPLAY_HILOGD(LABEL_TEST, "DisplayPowerMgrMulti005: fun is end"); +} +}