From d834bd91d93fea35740694ef751713aff0d454d2 Mon Sep 17 00:00:00 2001 From: chenqinxin Date: Wed, 19 Jan 2022 12:41:02 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=AE=E7=81=AD=E5=B1=8F?= =?UTF-8?q?=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: chenqinxin Change-Id: Id9b4cd1b0e9a0792d84884affd68af7e3af0d75b --- dm/include/display_manager_adapter.h | 4 +- dm/src/display_manager.cpp | 37 +- dm/src/display_manager_adapter.cpp | 8 +- dm/test/systemtest/display_power_test.cpp | 142 ++++++-- dm/test/unittest/BUILD.gn | 12 + dm/test/unittest/display_power_unit_test.cpp | 334 ++++++++++++++++++ .../unittest/mock_display_manager_adapter.h | 11 + .../display_manager_agent_controller.h | 4 +- dmserver/include/display_manager_interface.h | 4 +- dmserver/include/display_manager_proxy.h | 4 +- dmserver/include/display_manager_service.h | 4 +- .../src/display_manager_agent_controller.cpp | 8 +- dmserver/src/display_manager_proxy.cpp | 20 +- dmserver/src/display_manager_service.cpp | 12 +- dmserver/src/display_manager_stub.cpp | 4 +- interfaces/innerkits/dm/display_manager.h | 4 +- utils/include/client_agent_container.h | 20 +- 17 files changed, 537 insertions(+), 95 deletions(-) create mode 100644 dm/test/unittest/display_power_unit_test.cpp diff --git a/dm/include/display_manager_adapter.h b/dm/include/display_manager_adapter.h index 7ea1e65546..8e194e8f68 100644 --- a/dm/include/display_manager_adapter.h +++ b/dm/include/display_manager_adapter.h @@ -41,9 +41,9 @@ public: virtual DMError DestroyVirtualScreen(ScreenId screenId); virtual std::shared_ptr GetDisplaySnapshot(DisplayId displayId); - virtual void RegisterDisplayManagerAgent(const sptr& displayManagerAgent, + virtual bool RegisterDisplayManagerAgent(const sptr& displayManagerAgent, DisplayManagerAgentType type); - virtual void UnregisterDisplayManagerAgent(const sptr& displayManagerAgent, + virtual bool UnregisterDisplayManagerAgent(const sptr& displayManagerAgent, DisplayManagerAgentType type); virtual bool WakeUpBegin(PowerStateChangeReason reason); virtual bool WakeUpEnd(); diff --git a/dm/src/display_manager.cpp b/dm/src/display_manager.cpp index 7b6f824b91..108ef0bbef 100644 --- a/dm/src/display_manager.cpp +++ b/dm/src/display_manager.cpp @@ -26,6 +26,7 @@ namespace OHOS::Rosen { namespace { constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, 0, "DisplayManager"}; + constexpr uint32_t MAX_SCREEN_BRIGHTNESS_VALUE = 100; } WM_IMPLEMENT_SINGLE_INSTANCE(DisplayManager) @@ -173,42 +174,50 @@ std::vector> DisplayManager::GetAllDisplays() return res; } -void DisplayManager::RegisterDisplayPowerEventListener(sptr listener) +bool DisplayManager::RegisterDisplayPowerEventListener(sptr listener) { if (listener == nullptr) { WLOGFE("listener is nullptr"); - return; + return false; } std::lock_guard lock(mutex_); powerEventListeners_.push_back(listener); + bool ret = true; if (powerEventListenerAgent_ == nullptr) { powerEventListenerAgent_ = new DisplayManagerAgent(); - SingletonContainer::Get().RegisterDisplayManagerAgent(powerEventListenerAgent_, + ret = SingletonContainer::Get().RegisterDisplayManagerAgent(powerEventListenerAgent_, DisplayManagerAgentType::DISPLAY_POWER_EVENT_LISTENER); + if (!ret) { + WLOGFW("RegisterDisplayManagerAgent failed ! remove listener!"); + powerEventListeners_.pop_back(); + } } WLOGFI("RegisterDisplayPowerEventListener end"); + return ret; } -void DisplayManager::UnregisterDisplayPowerEventListener(sptr listener) +bool DisplayManager::UnregisterDisplayPowerEventListener(sptr listener) { if (listener == nullptr) { WLOGFE("listener is nullptr"); - return; + return false; } std::lock_guard lock(mutex_); auto iter = std::find(powerEventListeners_.begin(), powerEventListeners_.end(), listener); if (iter == powerEventListeners_.end()) { WLOGFE("could not find this listener"); - return; + return false; } powerEventListeners_.erase(iter); + bool ret = true; if (powerEventListeners_.empty() && powerEventListenerAgent_ != nullptr) { - SingletonContainer::Get().UnregisterDisplayManagerAgent(powerEventListenerAgent_, + ret = SingletonContainer::Get().UnregisterDisplayManagerAgent(powerEventListenerAgent_, DisplayManagerAgentType::DISPLAY_POWER_EVENT_LISTENER); powerEventListenerAgent_ = nullptr; } WLOGFI("UnregisterDisplayPowerEventListener end"); + return ret; } void DisplayManager::NotifyDisplayPowerEvent(DisplayPowerEvent event, EventStatus status) @@ -302,18 +311,19 @@ bool DisplayManager::SetDisplayState(DisplayState state, DisplayStateCallback ca { WLOGFI("state:%{public}u", state); std::lock_guard lock(mutex_); - if (displayStateCallback_ != nullptr) { - WLOGFI("previous callback not called, can't reset"); + if (displayStateCallback_ != nullptr || callback == nullptr) { + WLOGFI("previous callback not called or callback invalid"); return false; } displayStateCallback_ = callback; + bool ret = true; if (displayStateAgent_ == nullptr) { displayStateAgent_ = new DisplayManagerAgent(); - SingletonContainer::Get().RegisterDisplayManagerAgent(displayStateAgent_, + ret = SingletonContainer::Get().RegisterDisplayManagerAgent(displayStateAgent_, DisplayManagerAgentType::DISPLAY_STATE_LISTENER); } - bool ret = SingletonContainer::Get().SetDisplayState(state); + ret &= SingletonContainer::Get().SetDisplayState(state); if (!ret) { ClearDisplayStateCallback(); } @@ -328,6 +338,11 @@ DisplayState DisplayManager::GetDisplayState(uint64_t displayId) bool DisplayManager::SetScreenBrightness(uint64_t screenId, uint32_t level) { WLOGFI("screenId:%{public}" PRIu64", level:%{public}u,", screenId, level); + if (level > MAX_SCREEN_BRIGHTNESS_VALUE) { + level = MAX_SCREEN_BRIGHTNESS_VALUE; + WLOGFW("level:%{public}u, exceed max value!", level); + return false; + } RSInterfaces::GetInstance().SetScreenBacklight(screenId, level); return true; } diff --git a/dm/src/display_manager_adapter.cpp b/dm/src/display_manager_adapter.cpp index 2205b97548..441b0f2e40 100644 --- a/dm/src/display_manager_adapter.cpp +++ b/dm/src/display_manager_adapter.cpp @@ -96,22 +96,22 @@ DMError DisplayManagerAdapter::DestroyVirtualScreen(ScreenId screenId) return displayManagerServiceProxy_->DestroyVirtualScreen(screenId); } -void DisplayManagerAdapter::RegisterDisplayManagerAgent(const sptr& displayManagerAgent, +bool DisplayManagerAdapter::RegisterDisplayManagerAgent(const sptr& displayManagerAgent, DisplayManagerAgentType type) { std::lock_guard lock(mutex_); if (!InitDMSProxyLocked()) { - return; + return false; } return displayManagerServiceProxy_->RegisterDisplayManagerAgent(displayManagerAgent, type); } -void DisplayManagerAdapter::UnregisterDisplayManagerAgent(const sptr& displayManagerAgent, +bool DisplayManagerAdapter::UnregisterDisplayManagerAgent(const sptr& displayManagerAgent, DisplayManagerAgentType type) { std::lock_guard lock(mutex_); if (!InitDMSProxyLocked()) { - return; + return false; } return displayManagerServiceProxy_->UnregisterDisplayManagerAgent(displayManagerAgent, type); } diff --git a/dm/test/systemtest/display_power_test.cpp b/dm/test/systemtest/display_power_test.cpp index 06f64d731a..5308157d07 100644 --- a/dm/test/systemtest/display_power_test.cpp +++ b/dm/test/systemtest/display_power_test.cpp @@ -24,7 +24,8 @@ namespace OHOS { namespace Rosen { namespace { constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, 0, "DisplayPowerTest"}; - constexpr uint32_t MAX_TIME_WAITING_FOR_CALLBACK = 5; + constexpr uint32_t MAX_TIME_WAITING_FOR_CALLBACK = 40; + constexpr uint32_t SLEEP_TIME_IN_US = 50000; } class DisplayPowerEventListener : public IDisplayPowerEventListener { @@ -52,6 +53,7 @@ public: static inline DisplayId defaultId_; static inline uint32_t brightnessLevel_ = 80; + static inline uint32_t invalidBrightnessLevel_ = 180; static inline uint32_t times_ = 0; static inline bool isDisplayStateCallbackCalled_ = false; static sptr listener_; @@ -101,7 +103,7 @@ void DisplayPowerTest::CheckDisplayStateCallback(bool valueExpected) if (isDisplayStateCallbackCalled_ == valueExpected) { return; } - sleep(1); + usleep(SLEEP_TIME_IN_US); ++times_; } while (times_ <= MAX_TIME_WAITING_FOR_CALLBACK); } @@ -112,18 +114,41 @@ void DisplayPowerTest::CheckDisplayPowerEventCallback(bool valueExpected) if (listener_->isCallbackCalled_ == valueExpected) { return; } - sleep(1); + usleep(SLEEP_TIME_IN_US); ++times_; } while (times_ <= MAX_TIME_WAITING_FOR_CALLBACK); } namespace { /** - * @tc.name: SetDisplayStateValid + * @tc.name: register_display_power_event_listener_001 + * @tc.desc: call RegisterDisplayPowerEventListener with a valid listener and check return value + * @tc.type: FUNC + */ +HWTEST_F(DisplayPowerTest, register_display_power_event_listener_001, Function | SmallTest | Level2) +{ + sptr listener = new DisplayPowerEventListener(); + bool ret = DisplayManager::GetInstance().RegisterDisplayPowerEventListener(listener); + ASSERT_EQ(true, ret); +} + +/** + * @tc.name: register_display_power_event_listener_002 + * @tc.desc: call RegisterDisplayPowerEventListener with an invalid listener and check return value + * @tc.type: FUNC + */ +HWTEST_F(DisplayPowerTest, register_display_power_event_listener_002, Function | SmallTest | Level2) +{ + bool ret = DisplayManager::GetInstance().RegisterDisplayPowerEventListener(nullptr); + ASSERT_EQ(false, ret); +} + +/** + * @tc.name: set_display_state_001 * @tc.desc: Call SetDisplayState and check if it the state set is the same as calling GetDisplayState * @tc.type: FUNC */ -HWTEST_F(DisplayPowerTest, SetDisplayStateValid01, Function | MediumTest | Level2) +HWTEST_F(DisplayPowerTest, set_display_state_001, Function | MediumTest | Level2) { DisplayState initialState = DisplayManager::GetInstance().GetDisplayState(defaultId_); DisplayState stateToSet = (initialState == DisplayState::OFF ? DisplayState::ON : DisplayState::OFF); @@ -135,11 +160,11 @@ HWTEST_F(DisplayPowerTest, SetDisplayStateValid01, Function | MediumTest | Level } /** - * @tc.name: SetDisplayStateValid + * @tc.name: set_display_state_002 * @tc.desc: Call SetDisplayState to set a value already set and check the return value * @tc.type: FUNC */ -HWTEST_F(DisplayPowerTest, SetDisplayStateValid02, Function | MediumTest | Level2) +HWTEST_F(DisplayPowerTest, set_display_state_002, Function | MediumTest | Level2) { DisplayState initialState = DisplayManager::GetInstance().GetDisplayState(defaultId_); bool ret = DisplayManager::GetInstance().SetDisplayState(initialState, callback_); @@ -151,11 +176,26 @@ HWTEST_F(DisplayPowerTest, SetDisplayStateValid02, Function | MediumTest | Level } /** - * @tc.name: SetDisplayStateCallbackValid + * @tc.name: set_display_state_003 + * @tc.desc: Call SetDisplayState with an invalid value and check the return value + * @tc.type: FUNC + */ +HWTEST_F(DisplayPowerTest, set_display_state_003, Function | MediumTest | Level2) +{ + bool ret = DisplayManager::GetInstance().SetDisplayState(DisplayState::UNKNOWN, callback_); + ASSERT_EQ(false, ret); + CheckDisplayStateCallback(false); + ASSERT_EQ(false, isDisplayStateCallbackCalled_); + CheckDisplayPowerEventCallback(false); + ASSERT_EQ(false, listener_->isCallbackCalled_); +} + +/** + * @tc.name: set_display_state_callback_001 * @tc.desc: Call SetDisplayState and check if callback state is correct * @tc.type: FUNC */ -HWTEST_F(DisplayPowerTest, SetDisplayStateCallbackValid01, Function | MediumTest | Level2) +HWTEST_F(DisplayPowerTest, set_display_state_callback_001, Function | MediumTest | Level2) { DisplayState initialState = DisplayManager::GetInstance().GetDisplayState(defaultId_); DisplayState stateToSet = (initialState == DisplayState::OFF ? DisplayState::ON : DisplayState::OFF); @@ -166,11 +206,11 @@ HWTEST_F(DisplayPowerTest, SetDisplayStateCallbackValid01, Function | MediumTest } /** - * @tc.name: SetDisplayStateCallbackValid + * @tc.name: set_display_state_callback_002 * @tc.desc: Call SetDisplayState to set a value already set and check the DisplayStateCallback * @tc.type: FUNC */ -HWTEST_F(DisplayPowerTest, SetDisplayStateCallbackValid02, Function | MediumTest | Level2) +HWTEST_F(DisplayPowerTest, set_display_state_callback_002, Function | MediumTest | Level2) { DisplayState initialState = DisplayManager::GetInstance().GetDisplayState(defaultId_); DisplayManager::GetInstance().SetDisplayState(initialState, callback_); @@ -179,11 +219,11 @@ HWTEST_F(DisplayPowerTest, SetDisplayStateCallbackValid02, Function | MediumTest } /** - * @tc.name: WakeUpBeginCallbackValid + * @tc.name: wake_up_begin_callback_001 * @tc.desc: Call WakeUpBegin and check the OnDisplayPowerEvent callback is called * @tc.type: FUNC */ -HWTEST_F(DisplayPowerTest, WakeUpBeginCallbackValid01, Function | MediumTest | Level2) +HWTEST_F(DisplayPowerTest, wake_up_begin_callback_001, Function | MediumTest | Level2) { bool ret = DisplayManager::GetInstance().WakeUpBegin(PowerStateChangeReason::POWER_BUTTON); ASSERT_EQ(true, ret); @@ -194,11 +234,11 @@ HWTEST_F(DisplayPowerTest, WakeUpBeginCallbackValid01, Function | MediumTest | L } /** - * @tc.name: WakeUpEndCallbackValid - * @tc.desc: Call WakeUpBegin and check the OnDisplayPowerEvent callback is called + * @tc.name: wake_up_end_callback_001 + * @tc.desc: Call WakeUpEnd and check the OnDisplayPowerEvent callback is called * @tc.type: FUNC */ -HWTEST_F(DisplayPowerTest, WakeUpEndCallbackValid01, Function | MediumTest | Level2) +HWTEST_F(DisplayPowerTest, wake_up_end_callback_001, Function | MediumTest | Level2) { bool ret = DisplayManager::GetInstance().WakeUpEnd(); ASSERT_EQ(true, ret); @@ -209,11 +249,11 @@ HWTEST_F(DisplayPowerTest, WakeUpEndCallbackValid01, Function | MediumTest | Lev } /** - * @tc.name: SuspendBeginCallbackValid + * @tc.name: suspend_begin_callback_001 * @tc.desc: Call SuspendBegin and check the OnDisplayPowerEvent callback is called * @tc.type: FUNC */ -HWTEST_F(DisplayPowerTest, SuspendBeginCallbackValid01, Function | MediumTest | Level2) +HWTEST_F(DisplayPowerTest, suspend_begin_callback_001, Function | MediumTest | Level2) { bool ret = DisplayManager::GetInstance().SuspendBegin(PowerStateChangeReason::POWER_BUTTON); ASSERT_EQ(true, ret); @@ -224,11 +264,11 @@ HWTEST_F(DisplayPowerTest, SuspendBeginCallbackValid01, Function | MediumTest | } /** -* @tc.name: SuspendEndCallbackValid +* @tc.name: suspend_end_callback_001 * @tc.desc: Call SuspendEnd and check the OnDisplayPowerEvent callback is called * @tc.type: FUNC */ -HWTEST_F(DisplayPowerTest, SuspendEndCallbackValid01, Function | MediumTest | Level2) +HWTEST_F(DisplayPowerTest, suspend_end_callback_001, Function | MediumTest | Level2) { bool ret = DisplayManager::GetInstance().SuspendEnd(); ASSERT_EQ(true, ret); @@ -239,11 +279,11 @@ HWTEST_F(DisplayPowerTest, SuspendEndCallbackValid01, Function | MediumTest | Le } /** -* @tc.name: SetScreenPowerForAllCallbackValid +* @tc.name: set_screen_power_for_all_001 * @tc.desc: Call SetScreenPowerForAll OFF and check the OnDisplayPowerEvent callback is called * @tc.type: FUNC */ -HWTEST_F(DisplayPowerTest, SetScreenPowerForAllCallbackValid01, Function | MediumTest | Level2) +HWTEST_F(DisplayPowerTest, set_screen_power_for_all_001, Function | MediumTest | Level2) { bool ret = DisplayManager::GetInstance().SetScreenPowerForAll(DisplayPowerState::POWER_OFF, PowerStateChangeReason::POWER_BUTTON); @@ -255,11 +295,11 @@ HWTEST_F(DisplayPowerTest, SetScreenPowerForAllCallbackValid01, Function | Mediu } /** -* @tc.name: SetScreenPowerForAllCallbackValid +* @tc.name: set_screen_power_for_all_002 * @tc.desc: Call SetScreenPowerForAll ON and check the OnDisplayPowerEvent callback is called * @tc.type: FUNC */ -HWTEST_F(DisplayPowerTest, SetScreenPowerForAllCallbackValid02, Function | MediumTest | Level2) +HWTEST_F(DisplayPowerTest, set_screen_power_for_all_002, Function | MediumTest | Level2) { bool ret = DisplayManager::GetInstance().SetScreenPowerForAll(DisplayPowerState::POWER_ON, PowerStateChangeReason::POWER_BUTTON); @@ -271,11 +311,25 @@ HWTEST_F(DisplayPowerTest, SetScreenPowerForAllCallbackValid02, Function | Mediu } /** -* @tc.name: SetDisplayStatePowerCallbackValid -* @tc.desc: Call SetScreenPowerForAll OFF and check the OnDisplayPowerEvent callback is called +* @tc.name: set_screen_power_for_all_003 +* @tc.desc: Call SetScreenPowerForAll with an invalid value and check the return value * @tc.type: FUNC */ -HWTEST_F(DisplayPowerTest, SetDisplayStatePowerCallbackValid01, Function | MediumTest | Level2) +HWTEST_F(DisplayPowerTest, set_screen_power_for_all_003, Function | MediumTest | Level2) +{ + bool ret = DisplayManager::GetInstance().SetScreenPowerForAll(DisplayPowerState::INVALID_STATE, + PowerStateChangeReason::POWER_BUTTON); + ASSERT_EQ(false, ret); + CheckDisplayPowerEventCallback(true); + ASSERT_EQ(false, listener_->isCallbackCalled_); +} + +/** +* @tc.name: set_display_state_power_event_callback_001 +* @tc.desc: Call SetDisplayState OFF and check the OnDisplayPowerEvent callback is called +* @tc.type: FUNC +*/ +HWTEST_F(DisplayPowerTest, set_display_state_power_event_callback_001, Function | MediumTest | Level2) { bool ret = DisplayManager::GetInstance().SetDisplayState(DisplayState::OFF, callback_); ASSERT_EQ(true, ret); @@ -286,11 +340,11 @@ HWTEST_F(DisplayPowerTest, SetDisplayStatePowerCallbackValid01, Function | Mediu } /** -* @tc.name: SetDisplayStatePowerCallbackValid -* @tc.desc: Call SetScreenPowerForAll ON and check the OnDisplayPowerEvent callback is called +* @tc.name: set_display_state_power_event_callback_002 +* @tc.desc: Call SetDisplayState ON and check the OnDisplayPowerEvent callback is called * @tc.type: FUNC */ -HWTEST_F(DisplayPowerTest, SetDisplayStatePowerCallbackValid02, Function | MediumTest | Level2) +HWTEST_F(DisplayPowerTest, set_display_state_power_event_callback_002, Function | MediumTest | Level2) { bool ret = DisplayManager::GetInstance().SetDisplayState(DisplayState::ON, callback_); ASSERT_EQ(true, ret); @@ -301,11 +355,11 @@ HWTEST_F(DisplayPowerTest, SetDisplayStatePowerCallbackValid02, Function | Mediu } /** -* @tc.name: SetScreenPowerForAllValid +* @tc.name: get_display_power_001 * @tc.desc: Call SetScreenPowerForAll OFF and check the GetScreenPower return value * @tc.type: FUNC */ -HWTEST_F(DisplayPowerTest, SetScreenPowerForAllValid01, Function | MediumTest | Level2) +HWTEST_F(DisplayPowerTest, get_display_power_001, Function | MediumTest | Level2) { DisplayPowerState stateToSet = DisplayPowerState::POWER_OFF; bool ret = DisplayManager::GetInstance().SetScreenPowerForAll(stateToSet, PowerStateChangeReason::POWER_BUTTON); @@ -315,11 +369,11 @@ HWTEST_F(DisplayPowerTest, SetScreenPowerForAllValid01, Function | MediumTest | } /** -* @tc.name: SetScreenPowerForAllValid +* @tc.name: get_display_power_002 * @tc.desc: Call SetScreenPowerForAll ON and check the GetScreenPower return value * @tc.type: FUNC */ -HWTEST_F(DisplayPowerTest, SetScreenPowerForAllValid02, Function | MediumTest | Level2) +HWTEST_F(DisplayPowerTest, get_display_power_002, Function | MediumTest | Level2) { DisplayPowerState stateToSet = DisplayPowerState::POWER_ON; bool ret = DisplayManager::GetInstance().SetScreenPowerForAll(stateToSet, PowerStateChangeReason::POWER_BUTTON); @@ -329,17 +383,31 @@ HWTEST_F(DisplayPowerTest, SetScreenPowerForAllValid02, Function | MediumTest | } /** -* @tc.name: SetScreenBrightnessValid -* @tc.desc: Call SetScreenBrightness and check the GetScreenBrightness return value +* @tc.name: set_screen_brightness_001 +* @tc.desc: Call SetScreenBrightness with a valid value and check the GetScreenBrightness return value * @tc.type: FUNC */ -HWTEST_F(DisplayPowerTest, SetScreenBrightnessValid01, Function | MediumTest | Level2) +HWTEST_F(DisplayPowerTest, set_screen_brightness_001, Function | MediumTest | Level2) { bool ret = DisplayManager::GetInstance().SetScreenBrightness(defaultId_, brightnessLevel_); ASSERT_EQ(true, ret); uint32_t level = DisplayManager::GetInstance().GetScreenBrightness(defaultId_); ASSERT_EQ(level, brightnessLevel_); } + +/** +* @tc.name: set_screen_brightness_002 +* @tc.desc: Call SetScreenBrightness with an invalid value and check the GetScreenBrightness return value +* @tc.type: FUNC +*/ +HWTEST_F(DisplayPowerTest, set_screen_brightness_002, Function | MediumTest | Level2) +{ + uint32_t initialLevel = DisplayManager::GetInstance().GetScreenBrightness(defaultId_); + bool ret = DisplayManager::GetInstance().SetScreenBrightness(defaultId_, invalidBrightnessLevel_); + ASSERT_EQ(false, ret); + uint32_t level = DisplayManager::GetInstance().GetScreenBrightness(defaultId_); + ASSERT_EQ(level, initialLevel); +} } // namespace } // namespace Rosen } // namespace OHOS \ No newline at end of file diff --git a/dm/test/unittest/BUILD.gn b/dm/test/unittest/BUILD.gn index aa6313958c..ce638248aa 100644 --- a/dm/test/unittest/BUILD.gn +++ b/dm/test/unittest/BUILD.gn @@ -19,11 +19,23 @@ group("unittest") { testonly = true deps = [ + ":dm_display_power_unit_test", ":dm_screenshot_test", ":dm_snapshot_utils_test", ] } +## UnitTest dm_display_power_unit_test {{{ +ohos_unittest("dm_display_power_unit_test") { + module_out_path = module_out_path + + sources = [ "display_power_unit_test.cpp" ] + + deps = [ ":dm_unittest_common" ] +} + +## UnitTest dm_snapshot_utils_test }}} + ## UnitTest dm_snapshot_utils_test {{{ ohos_unittest("dm_snapshot_utils_test") { module_out_path = module_out_path diff --git a/dm/test/unittest/display_power_unit_test.cpp b/dm/test/unittest/display_power_unit_test.cpp new file mode 100644 index 0000000000..11cc693ee0 --- /dev/null +++ b/dm/test/unittest/display_power_unit_test.cpp @@ -0,0 +1,334 @@ +/* + * 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 "display_manager.h" +#include "mock_display_manager_adapter.h" +#include "singleton_mocker.h" + +using namespace testing; +using namespace testing::ext; + +namespace OHOS { +namespace Rosen { +using Mocker = SingletonMocker; + +class DisplayPowerEventListener : public IDisplayPowerEventListener { +public: + virtual void OnDisplayPowerEvent(DisplayPowerEvent event, EventStatus status) {} +}; + +class DisplayPowerUnitTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + virtual void SetUp() override; + virtual void TearDown() override; + static inline sptr listener_ = new DisplayPowerEventListener(); + static inline DisplayId defaultId_ = 0; + static inline uint32_t brightnessLevel_ = 80; + static inline uint32_t invalidBrightnessLevel_ = 180; + static inline DisplayPowerState initialPowerState_; + static inline DisplayState initialState_; +}; + +void DisplayPowerUnitTest::SetUpTestCase() +{ +} + +void DisplayPowerUnitTest::TearDownTestCase() +{ +} + +void DisplayPowerUnitTest::SetUp() +{ + initialPowerState_ = DisplayManager::GetInstance().GetScreenPower(defaultId_); + initialState_ = DisplayManager::GetInstance().GetDisplayState(defaultId_); +} + +void DisplayPowerUnitTest::TearDown() +{ + DisplayManager::GetInstance().SetScreenPowerForAll(initialPowerState_, PowerStateChangeReason::POWER_BUTTON); + DisplayStateCallback callback; + DisplayManager::GetInstance().SetDisplayState(initialState_, callback); +} + +namespace { +/** + * @tc.name: register_display_power_event_listener_001 + * @tc.desc: call Register/UnregisterDisplayPowerEventListener with a valid listener and check return value + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(DisplayPowerUnitTest, register_display_power_event_listener_001, Function | SmallTest | Level2) +{ + Mocker m; + + EXPECT_CALL(m.Mock(), RegisterDisplayManagerAgent(_, DisplayManagerAgentType::DISPLAY_POWER_EVENT_LISTENER)) + .Times(1).WillOnce(Return(true)); + bool ret = DisplayManager::GetInstance().RegisterDisplayPowerEventListener(listener_); + ASSERT_EQ(true, ret); + + EXPECT_CALL(m.Mock(), UnregisterDisplayManagerAgent(_, DisplayManagerAgentType::DISPLAY_POWER_EVENT_LISTENER)) + .Times(1).WillOnce(Return(true)); + ret = DisplayManager::GetInstance().UnregisterDisplayPowerEventListener(listener_); + ASSERT_EQ(true, ret); +} + +/** + * @tc.name: register_display_power_event_listener_002 + * @tc.desc: call Register/UnregisterDisplayPowerEventListener with nullptr and check return value + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(DisplayPowerUnitTest, register_display_power_event_listener_002, Function | SmallTest | Level2) +{ + Mocker m; + EXPECT_CALL(m.Mock(), RegisterDisplayManagerAgent(_, DisplayManagerAgentType::DISPLAY_POWER_EVENT_LISTENER)) + .Times(0); + bool ret = DisplayManager::GetInstance().RegisterDisplayPowerEventListener(nullptr); + ASSERT_EQ(false, ret); + + EXPECT_CALL(m.Mock(), UnregisterDisplayManagerAgent(_, DisplayManagerAgentType::DISPLAY_POWER_EVENT_LISTENER)) + .Times(0); + ret = DisplayManager::GetInstance().UnregisterDisplayPowerEventListener(nullptr); + ASSERT_EQ(false, ret); +} + +/** + * @tc.name: register_display_power_event_listener_003 + * @tc.desc: call Register/UnregisterDisplayPowerEventListener with ipc failed and check return value + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(DisplayPowerUnitTest, register_display_power_event_listener_003, Function | SmallTest | Level2) +{ + Mocker m; + EXPECT_CALL(m.Mock(), RegisterDisplayManagerAgent(_, DisplayManagerAgentType::DISPLAY_POWER_EVENT_LISTENER)) + .Times(1).WillOnce(Return(false)); + bool ret = DisplayManager::GetInstance().RegisterDisplayPowerEventListener(listener_); + ASSERT_EQ(false, ret); + + EXPECT_CALL(m.Mock(), UnregisterDisplayManagerAgent(_, DisplayManagerAgentType::DISPLAY_POWER_EVENT_LISTENER)) + .Times(0); + ret = DisplayManager::GetInstance().UnregisterDisplayPowerEventListener(listener_); + ASSERT_EQ(false, ret); +} + +/** + * @tc.name: unregister_display_power_event_listener_001 + * @tc.desc: call UnregisterDisplayPowerEventListener with a listener never registered and check return value + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(DisplayPowerUnitTest, unregister_display_power_event_listener_001, Function | SmallTest | Level2) +{ + Mocker m; + EXPECT_CALL(m.Mock(), UnregisterDisplayManagerAgent(_, DisplayManagerAgentType::DISPLAY_POWER_EVENT_LISTENER)) + .Times(0); + bool ret = DisplayManager::GetInstance().UnregisterDisplayPowerEventListener(listener_); + ASSERT_EQ(false, ret); +} + +/** + * @tc.name: unregister_display_power_event_listener_002 + * @tc.desc: call UnregisterDisplayPowerEventListener with nullptr and check return value + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(DisplayPowerUnitTest, unregister_display_power_event_listener_002, Function | SmallTest | Level2) +{ + Mocker m; + EXPECT_CALL(m.Mock(), UnregisterDisplayManagerAgent(_, DisplayManagerAgentType::DISPLAY_POWER_EVENT_LISTENER)) + .Times(0); + bool ret = DisplayManager::GetInstance().UnregisterDisplayPowerEventListener(nullptr); + ASSERT_EQ(false, ret); +} + +/** + * @tc.name: wake_up_begin_001 + * @tc.desc: call WakeUpBegin and check return value + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(DisplayPowerUnitTest, wake_up_begin_001, Function | SmallTest | Level2) +{ + Mocker m; + EXPECT_CALL(m.Mock(), WakeUpBegin(PowerStateChangeReason::POWER_BUTTON)).Times(1).WillOnce(Return(true));; + bool ret = DisplayManager::GetInstance().WakeUpBegin(PowerStateChangeReason::POWER_BUTTON); + ASSERT_EQ(true, ret); + + EXPECT_CALL(m.Mock(), WakeUpBegin(PowerStateChangeReason::POWER_BUTTON)).Times(1).WillOnce(Return(false));; + ret = DisplayManager::GetInstance().WakeUpBegin(PowerStateChangeReason::POWER_BUTTON); + ASSERT_EQ(false, ret); +} + +/** + * @tc.name: wake_up_end_001 + * @tc.desc: call WakeUpEnd and check return value + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(DisplayPowerUnitTest, wake_up_end_001, Function | SmallTest | Level2) +{ + Mocker m; + EXPECT_CALL(m.Mock(), WakeUpEnd()).Times(1).WillOnce(Return(true)); + bool ret = DisplayManager::GetInstance().WakeUpEnd(); + ASSERT_EQ(true, ret); + + EXPECT_CALL(m.Mock(), WakeUpEnd()).Times(1).WillOnce(Return(false)); + ret = DisplayManager::GetInstance().WakeUpEnd(); + ASSERT_EQ(false, ret); +} + +/** + * @tc.name: suspend_begin_001 + * @tc.desc: call SuspendBegin and check return value + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(DisplayPowerUnitTest, suspend_begin_001, Function | SmallTest | Level2) +{ + Mocker m; + EXPECT_CALL(m.Mock(), SuspendBegin(PowerStateChangeReason::POWER_BUTTON)).Times(1).WillOnce(Return(true));; + bool ret = DisplayManager::GetInstance().SuspendBegin(PowerStateChangeReason::POWER_BUTTON); + ASSERT_EQ(true, ret); + + EXPECT_CALL(m.Mock(), SuspendBegin(PowerStateChangeReason::POWER_BUTTON)).Times(1).WillOnce(Return(false));; + ret = DisplayManager::GetInstance().SuspendBegin(PowerStateChangeReason::POWER_BUTTON); + ASSERT_EQ(false, ret); +} + +/** +* @tc.name: suspend_end_001 +* @tc.desc: call SuspendEnd and check return value +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(DisplayPowerUnitTest, suspend_end_001, Function | SmallTest | Level2) +{ + Mocker m; + EXPECT_CALL(m.Mock(), SuspendEnd()).Times(1).WillOnce(Return(true)); + bool ret = DisplayManager::GetInstance().SuspendEnd(); + ASSERT_EQ(true, ret); + + EXPECT_CALL(m.Mock(), SuspendEnd()).Times(1).WillOnce(Return(false)); + ret = DisplayManager::GetInstance().SuspendEnd(); + ASSERT_EQ(false, ret); +} + +/** +* @tc.name: set_screen_brightness_001 +* @tc.desc: Call SetScreenBrightness with a valid value and check the GetScreenBrightness return value +* @tc.type: FUNC +*/ +HWTEST_F(DisplayPowerUnitTest, set_screen_brightness_001, Function | MediumTest | Level2) +{ + bool ret = DisplayManager::GetInstance().SetScreenBrightness(defaultId_, brightnessLevel_); + ASSERT_EQ(true, ret); + uint32_t level = DisplayManager::GetInstance().GetScreenBrightness(defaultId_); + ASSERT_EQ(level, brightnessLevel_); +} + +/** +* @tc.name: set_screen_brightness_002 +* @tc.desc: Call SetScreenBrightness with an invalid value and check the GetScreenBrightness return value +* @tc.type: FUNC +*/ +HWTEST_F(DisplayPowerUnitTest, set_screen_brightness_002, Function | MediumTest | Level2) +{ + uint32_t initialLevel = DisplayManager::GetInstance().GetScreenBrightness(defaultId_); + bool ret = DisplayManager::GetInstance().SetScreenBrightness(defaultId_, invalidBrightnessLevel_); + ASSERT_EQ(false, ret); + uint32_t level = DisplayManager::GetInstance().GetScreenBrightness(defaultId_); + ASSERT_EQ(level, initialLevel); +} + +/** +* @tc.name: set_screen_power_for_all_001 +* @tc.desc: Call SetScreenPowerForAll with valid value and check the GetScreenPower return value +* @tc.type: FUNC +*/ +HWTEST_F(DisplayPowerUnitTest, set_screen_power_for_all_001, Function | MediumTest | Level2) +{ + Mocker m; + EXPECT_CALL(m.Mock(), GetDefaultDisplayId()).Times(1).WillOnce(Return(defaultId_)); + EXPECT_CALL(m.Mock(), SetScreenPowerForAll(_, PowerStateChangeReason::POWER_BUTTON)) + .Times(1).WillOnce(Return(true)); + + bool ret = DisplayManager::GetInstance().SetScreenPowerForAll(DisplayPowerState::POWER_OFF, + PowerStateChangeReason::POWER_BUTTON); + ASSERT_EQ(true, ret); + + DisplayPowerState state = DisplayManager::GetInstance().GetScreenPower(defaultId_); + ASSERT_EQ(state, DisplayPowerState::POWER_OFF); +} + +/** +* @tc.name: set_screen_power_for_all_002 +* @tc.desc: Call SetScreenPowerForAll with invalid value and check the GetScreenPower return value +* @tc.type: FUNC +*/ +HWTEST_F(DisplayPowerUnitTest, set_screen_power_for_all_002, Function | MediumTest | Level2) +{ + Mocker m; + EXPECT_CALL(m.Mock(), GetDefaultDisplayId()).Times(1).WillOnce(Return(defaultId_)); + EXPECT_CALL(m.Mock(), SetScreenPowerForAll(_, PowerStateChangeReason::POWER_BUTTON)).Times(0); + + bool ret = DisplayManager::GetInstance().SetScreenPowerForAll(DisplayPowerState::INVALID_STATE, + PowerStateChangeReason::POWER_BUTTON); + ASSERT_EQ(false, ret); + + DisplayPowerState state = DisplayManager::GetInstance().GetScreenPower(defaultId_); + ASSERT_EQ(state, initialPowerState_); +} + +/** +* @tc.name: set_display_state_001 +* @tc.desc: Call SetDisplayState with valid value and check the GetDisplayState return value +* @tc.type: FUNC +*/ +HWTEST_F(DisplayPowerUnitTest, set_display_state_001, Function | MediumTest | Level2) +{ + DisplayState stateToSet = (initialState_ == DisplayState::OFF ? DisplayState::ON : DisplayState::OFF); + Mocker m; + EXPECT_CALL(m.Mock(), RegisterDisplayManagerAgent(_, DisplayManagerAgentType::DISPLAY_STATE_LISTENER)) + .Times(1).WillOnce(Return(true)); + EXPECT_CALL(m.Mock(), SetDisplayState(stateToSet)).Times(1).WillOnce(Return(true)); + DisplayStateCallback callback = [](DisplayState state) {}; + bool ret = DisplayManager::GetInstance().SetDisplayState(stateToSet, callback); + ASSERT_EQ(true, ret); + + EXPECT_CALL(m.Mock(), GetDisplayState(defaultId_)).Times(1).WillOnce(Return(stateToSet)); + DisplayState state = DisplayManager::GetInstance().GetDisplayState(defaultId_); + ASSERT_EQ(state, stateToSet); +} + +/** +* @tc.name: set_display_state_002 +* @tc.desc: Call SetDisplayState with invalid callback and check the GetDisplayState return value +* @tc.type: FUNC +*/ +HWTEST_F(DisplayPowerUnitTest, set_display_state_002, Function | MediumTest | Level2) +{ + Mocker m; + EXPECT_CALL(m.Mock(), SetDisplayState(_)).Times(0); + DisplayState stateToSet = (initialState_ == DisplayState::OFF ? DisplayState::ON : DisplayState::OFF); + bool ret = DisplayManager::GetInstance().SetDisplayState(stateToSet, nullptr); + ASSERT_EQ(false, ret); +} +} +} +} \ No newline at end of file diff --git a/dm/test/unittest/mock_display_manager_adapter.h b/dm/test/unittest/mock_display_manager_adapter.h index 97310c7773..ae01aae8dc 100644 --- a/dm/test/unittest/mock_display_manager_adapter.h +++ b/dm/test/unittest/mock_display_manager_adapter.h @@ -29,6 +29,17 @@ public: MOCK_METHOD1(DestroyVirtualScreen, DMError(ScreenId screenId)); MOCK_METHOD1(GetDisplaySnapshot, std::shared_ptr(DisplayId displayId)); MOCK_METHOD0(Clear, void()); + MOCK_METHOD2(RegisterDisplayManagerAgent, bool(const sptr& displayManagerAgent, + DisplayManagerAgentType type)); + MOCK_METHOD2(UnregisterDisplayManagerAgent, bool(const sptr& displayManagerAgent, + DisplayManagerAgentType type)); + MOCK_METHOD1(WakeUpBegin, bool(PowerStateChangeReason reason)); + MOCK_METHOD0(WakeUpEnd, bool()); + MOCK_METHOD1(SuspendBegin, bool(PowerStateChangeReason reason)); + MOCK_METHOD0(SuspendEnd, bool()); + MOCK_METHOD2(SetScreenPowerForAll, bool(DisplayPowerState state, PowerStateChangeReason reason)); + MOCK_METHOD1(SetDisplayState, bool(DisplayState state)); + MOCK_METHOD1(GetDisplayState, DisplayState(uint64_t displayId)); }; } } // namespace OHOS diff --git a/dmserver/include/display_manager_agent_controller.h b/dmserver/include/display_manager_agent_controller.h index dec0d87f9a..c5f4e92afc 100644 --- a/dmserver/include/display_manager_agent_controller.h +++ b/dmserver/include/display_manager_agent_controller.h @@ -26,9 +26,9 @@ namespace Rosen { class DisplayManagerAgentController { WM_DECLARE_SINGLE_INSTANCE_BASE(DisplayManagerAgentController) public: - void RegisterDisplayManagerAgent(const sptr& displayManagerAgent, + bool RegisterDisplayManagerAgent(const sptr& displayManagerAgent, DisplayManagerAgentType type); - void UnregisterDisplayManagerAgent(const sptr& displayManagerAgent, + bool UnregisterDisplayManagerAgent(const sptr& displayManagerAgent, DisplayManagerAgentType type); bool NotifyDisplayPowerEvent(DisplayPowerEvent event, EventStatus status); diff --git a/dmserver/include/display_manager_interface.h b/dmserver/include/display_manager_interface.h index 4c889daf3c..743602d6d2 100644 --- a/dmserver/include/display_manager_interface.h +++ b/dmserver/include/display_manager_interface.h @@ -56,9 +56,9 @@ public: virtual DMError DestroyVirtualScreen(ScreenId screenId) = 0; virtual std::shared_ptr GetDispalySnapshot(DisplayId displayId) = 0; - virtual void RegisterDisplayManagerAgent(const sptr& displayManagerAgent, + virtual bool RegisterDisplayManagerAgent(const sptr& displayManagerAgent, DisplayManagerAgentType type) = 0; - virtual void UnregisterDisplayManagerAgent(const sptr& displayManagerAgent, + virtual bool UnregisterDisplayManagerAgent(const sptr& displayManagerAgent, DisplayManagerAgentType type) = 0; virtual bool WakeUpBegin(PowerStateChangeReason reason) = 0; virtual bool WakeUpEnd() = 0; diff --git a/dmserver/include/display_manager_proxy.h b/dmserver/include/display_manager_proxy.h index dd1232fba0..a3c33988c1 100644 --- a/dmserver/include/display_manager_proxy.h +++ b/dmserver/include/display_manager_proxy.h @@ -38,9 +38,9 @@ public: DMError DestroyVirtualScreen(ScreenId screenId) override; std::shared_ptr GetDispalySnapshot(DisplayId displayId) override; - void RegisterDisplayManagerAgent(const sptr& displayManagerAgent, + bool RegisterDisplayManagerAgent(const sptr& displayManagerAgent, DisplayManagerAgentType type) override; - void UnregisterDisplayManagerAgent(const sptr& displayManagerAgent, + bool UnregisterDisplayManagerAgent(const sptr& displayManagerAgent, DisplayManagerAgentType type) override; bool WakeUpBegin(PowerStateChangeReason reason) override; bool WakeUpEnd() override; diff --git a/dmserver/include/display_manager_service.h b/dmserver/include/display_manager_service.h index 898c3cab4d..989a9601d4 100644 --- a/dmserver/include/display_manager_service.h +++ b/dmserver/include/display_manager_service.h @@ -46,9 +46,9 @@ public: DisplayInfo GetDisplayInfoById(DisplayId displayId) override; std::shared_ptr GetDispalySnapshot(DisplayId displayId) override; - void RegisterDisplayManagerAgent(const sptr& displayManagerAgent, + bool RegisterDisplayManagerAgent(const sptr& displayManagerAgent, DisplayManagerAgentType type) override; - void UnregisterDisplayManagerAgent(const sptr& displayManagerAgent, + bool UnregisterDisplayManagerAgent(const sptr& displayManagerAgent, DisplayManagerAgentType type) override; bool WakeUpBegin(PowerStateChangeReason reason) override; bool WakeUpEnd() override; diff --git a/dmserver/src/display_manager_agent_controller.cpp b/dmserver/src/display_manager_agent_controller.cpp index ff46111ccf..dd00f4a534 100644 --- a/dmserver/src/display_manager_agent_controller.cpp +++ b/dmserver/src/display_manager_agent_controller.cpp @@ -23,18 +23,18 @@ namespace { } WM_IMPLEMENT_SINGLE_INSTANCE(DisplayManagerAgentController) -void DisplayManagerAgentController::RegisterDisplayManagerAgent(const sptr& displayManagerAgent, +bool DisplayManagerAgentController::RegisterDisplayManagerAgent(const sptr& displayManagerAgent, DisplayManagerAgentType type) { std::lock_guard lock(mutex_); - dmAgentContainer_.RegisterAgentLocked(displayManagerAgent, type); + return dmAgentContainer_.RegisterAgentLocked(displayManagerAgent, type); } -void DisplayManagerAgentController::UnregisterDisplayManagerAgent(const sptr& displayManagerAgent, +bool DisplayManagerAgentController::UnregisterDisplayManagerAgent(const sptr& displayManagerAgent, DisplayManagerAgentType type) { std::lock_guard lock(mutex_); - dmAgentContainer_.UnregisterAgentLocked(displayManagerAgent, type); + return dmAgentContainer_.UnregisterAgentLocked(displayManagerAgent, type); } bool DisplayManagerAgentController::NotifyDisplayPowerEvent(DisplayPowerEvent event, EventStatus status) diff --git a/dmserver/src/display_manager_proxy.cpp b/dmserver/src/display_manager_proxy.cpp index b8d3906bcd..8ff32975a6 100644 --- a/dmserver/src/display_manager_proxy.cpp +++ b/dmserver/src/display_manager_proxy.cpp @@ -171,7 +171,7 @@ std::shared_ptr DisplayManagerProxy::GetDispalySnapshot(Display return pixelMap; } -void DisplayManagerProxy::RegisterDisplayManagerAgent(const sptr& displayManagerAgent, +bool DisplayManagerProxy::RegisterDisplayManagerAgent(const sptr& displayManagerAgent, DisplayManagerAgentType type) { MessageParcel data; @@ -179,25 +179,27 @@ void DisplayManagerProxy::RegisterDisplayManagerAgent(const sptrAsObject())) { WLOGFE("Write IDisplayManagerAgent failed"); - return; + return false; } if (!data.WriteUint32(static_cast(type))) { WLOGFE("Write DisplayManagerAgent type failed"); - return; + return false; } if (Remote()->SendRequest(TRANS_ID_REGISTER_DISPLAY_MANAGER_AGENT, data, reply, option) != ERR_NONE) { WLOGFE("SendRequest failed"); + return false; } + return reply.ReadBool(); } -void DisplayManagerProxy::UnregisterDisplayManagerAgent(const sptr& displayManagerAgent, +bool DisplayManagerProxy::UnregisterDisplayManagerAgent(const sptr& displayManagerAgent, DisplayManagerAgentType type) { MessageParcel data; @@ -205,22 +207,24 @@ void DisplayManagerProxy::UnregisterDisplayManagerAgent(const sptrAsObject())) { WLOGFE("Write IWindowManagerAgent failed"); - return; + return false; } if (!data.WriteUint32(static_cast(type))) { WLOGFE("Write DisplayManagerAgent type failed"); - return; + return false; } if (Remote()->SendRequest(TRANS_ID_UNREGISTER_DISPLAY_MANAGER_AGENT, data, reply, option) != ERR_NONE) { WLOGFE("SendRequest failed"); + return false; } + return reply.ReadBool(); } bool DisplayManagerProxy::WakeUpBegin(PowerStateChangeReason reason) diff --git a/dmserver/src/display_manager_service.cpp b/dmserver/src/display_manager_service.cpp index f9bcf6ba9d..1809fe8a20 100644 --- a/dmserver/src/display_manager_service.cpp +++ b/dmserver/src/display_manager_service.cpp @@ -136,24 +136,24 @@ void DisplayManagerService::OnStop() WLOGFI("ready to stop display service."); } -void DisplayManagerService::RegisterDisplayManagerAgent(const sptr& displayManagerAgent, +bool DisplayManagerService::RegisterDisplayManagerAgent(const sptr& displayManagerAgent, DisplayManagerAgentType type) { if ((displayManagerAgent == nullptr) || (displayManagerAgent->AsObject() == nullptr)) { WLOGFE("displayManagerAgent invalid"); - return; + return false; } - DisplayManagerAgentController::GetInstance().RegisterDisplayManagerAgent(displayManagerAgent, type); + return DisplayManagerAgentController::GetInstance().RegisterDisplayManagerAgent(displayManagerAgent, type); } -void DisplayManagerService::UnregisterDisplayManagerAgent(const sptr& displayManagerAgent, +bool DisplayManagerService::UnregisterDisplayManagerAgent(const sptr& displayManagerAgent, DisplayManagerAgentType type) { if ((displayManagerAgent == nullptr) || (displayManagerAgent->AsObject() == nullptr)) { WLOGFE("displayManagerAgent invalid"); - return; + return false; } - DisplayManagerAgentController::GetInstance().UnregisterDisplayManagerAgent(displayManagerAgent, type); + return DisplayManagerAgentController::GetInstance().UnregisterDisplayManagerAgent(displayManagerAgent, type); } bool DisplayManagerService::WakeUpBegin(PowerStateChangeReason reason) diff --git a/dmserver/src/display_manager_stub.cpp b/dmserver/src/display_manager_stub.cpp index 7e089ace0b..927bd23230 100644 --- a/dmserver/src/display_manager_stub.cpp +++ b/dmserver/src/display_manager_stub.cpp @@ -87,13 +87,13 @@ int32_t DisplayManagerStub::OnRemoteRequest(uint32_t code, MessageParcel &data, case TRANS_ID_REGISTER_DISPLAY_MANAGER_AGENT: { auto agent = iface_cast(data.ReadRemoteObject()); auto type = static_cast(data.ReadUint32()); - RegisterDisplayManagerAgent(agent, type); + reply.WriteBool(RegisterDisplayManagerAgent(agent, type)); break; } case TRANS_ID_UNREGISTER_DISPLAY_MANAGER_AGENT: { auto agent = iface_cast(data.ReadRemoteObject()); auto type = static_cast(data.ReadUint32()); - UnregisterDisplayManagerAgent(agent, type); + reply.WriteBool(UnregisterDisplayManagerAgent(agent, type)); break; } case TRANS_ID_WAKE_UP_BEGIN: { diff --git a/interfaces/innerkits/dm/display_manager.h b/interfaces/innerkits/dm/display_manager.h index 617633238f..aa09f768f0 100644 --- a/interfaces/innerkits/dm/display_manager.h +++ b/interfaces/innerkits/dm/display_manager.h @@ -47,8 +47,8 @@ public: std::shared_ptr GetScreenshot(DisplayId displayId, const Media::Rect &rect, const Media::Size &size, int rotation); - void RegisterDisplayPowerEventListener(sptr listener); - void UnregisterDisplayPowerEventListener(sptr listener); + bool RegisterDisplayPowerEventListener(sptr listener); + bool UnregisterDisplayPowerEventListener(sptr listener); bool WakeUpBegin(PowerStateChangeReason reason); bool WakeUpEnd(); bool SuspendBegin(PowerStateChangeReason reason); diff --git a/utils/include/client_agent_container.h b/utils/include/client_agent_container.h index 3d46b4f920..5fbe72d228 100644 --- a/utils/include/client_agent_container.h +++ b/utils/include/client_agent_container.h @@ -30,8 +30,8 @@ public: ClientAgentContainer(std::mutex& mutex); virtual ~ClientAgentContainer() = default; - void RegisterAgentLocked(const sptr& agent, T2 type); - void UnregisterAgentLocked(const sptr& agent, T2 type); + bool RegisterAgentLocked(const sptr& agent, T2 type); + bool UnregisterAgentLocked(const sptr& agent, T2 type); std::vector> GetAgentsByType(T2 type); private: @@ -60,29 +60,27 @@ ClientAgentContainer::ClientAgentContainer(std::mutex& mutex) std::bind(&ClientAgentContainer::RemoveAgent, this, std::placeholders::_1))) {} template -void ClientAgentContainer::RegisterAgentLocked(const sptr& agent, T2 type) +bool ClientAgentContainer::RegisterAgentLocked(const sptr& agent, T2 type) { agentMap_[type].push_back(agent); WLOG_I("ClientAgentContainer agent registered type:%{public}u", type); - if (deathRecipient_ == nullptr) { - WLOG_I("death Recipient is nullptr"); - return; - } - if (!agent->AsObject()->AddDeathRecipient(deathRecipient_)) { + if (deathRecipient_ == nullptr || !agent->AsObject()->AddDeathRecipient(deathRecipient_)) { WLOG_I("ClientAgentContainer failed to add death recipient"); } + return true; } template -void ClientAgentContainer::UnregisterAgentLocked(const sptr& agent, T2 type) +bool ClientAgentContainer::UnregisterAgentLocked(const sptr& agent, T2 type) { if (agent == nullptr || agentMap_.count(type) == 0) { WLOG_E("ClientAgentContainer agent or type is invalid"); - return; + return false; } auto& agents = agentMap_.at(type); - UnregisterAgentLocked(agents, agent->AsObject()); + bool ret = UnregisterAgentLocked(agents, agent->AsObject()); agent->AsObject()->RemoveDeathRecipient(deathRecipient_); + return ret; } template