diff --git a/services/native/src/suspend/suspend_controller.cpp b/services/native/src/suspend/suspend_controller.cpp index 5cce7a29def1fbb3825083a210f9125e8eb62573..c57cbe72f52d295833e893584dbdd1b2bffb1a90 100644 --- a/services/native/src/suspend/suspend_controller.cpp +++ b/services/native/src/suspend/suspend_controller.cpp @@ -747,9 +747,16 @@ bool PowerKeySuspendMonitor::Init() POWER_HILOGE(FEATURE_SUSPEND, "PowerKeySuspendMonitorInit inputManager is null"); return false; } + std::weak_ptr weak = weak_from_this(); powerkeyReleaseId_ = inputManager->SubscribeKeyEvent( - keyOption, [this](std::shared_ptr keyEvent) { - ReceivePowerkeyCallback(keyEvent); + keyOption, [weak](std::shared_ptr keyEvent) { + std::shared_ptr strong = weak.lock(); + if (!strong) { + POWER_HILOGE(FEATURE_SUSPEND, "[UL_POWER] PowerKeySuspendMonitor is invaild, return"); + return; + } + + strong->ReceivePowerkeyCallback(keyEvent); }); POWER_HILOGI(FEATURE_SUSPEND, "powerkeyReleaseId_=%{public}d", powerkeyReleaseId_); return powerkeyReleaseId_ >= 0 ? true : false; @@ -902,17 +909,17 @@ bool TPCoverSuspendMonitor::Init() keyOption->SetPreKeys(preKeys); keyOption->SetFinalKey(OHOS::MMI::KeyEvent::KEYCODE_SLEEP); keyOption->SetFinalKeyDownDuration(0); - std::weak_ptr weak = weak_from_this(); auto inputManager = InputManager::GetInstance(); if (!inputManager) { POWER_HILOGE(FEATURE_SUSPEND, "TPCoverSuspendMonitorInit inputManager is null"); return false; } + std::weak_ptr weak = weak_from_this(); TPCoverReleaseId_ = inputManager->SubscribeKeyEvent( keyOption, [weak](std::shared_ptr keyEvent) { std::shared_ptr strong = weak.lock(); if (!strong) { - POWER_HILOGI(FEATURE_WAKEUP, "[UL_POWER] TPCoverSuspendMonitor is invaild, return"); + POWER_HILOGE(FEATURE_SUSPEND, "[UL_POWER] TPCoverSuspendMonitor is invaild, return"); return; } POWER_HILOGI(FEATURE_SUSPEND, "[UL_POWER] Received TPCover event"); diff --git a/services/native/src/suspend/suspend_controller.h b/services/native/src/suspend/suspend_controller.h index 5c406612a4459b1707e9a7bd04b083638ff85689..022b7897220de9025b9e3dc580743fd510d76550 100644 --- a/services/native/src/suspend/suspend_controller.h +++ b/services/native/src/suspend/suspend_controller.h @@ -185,7 +185,7 @@ protected: SuspendListener listener_; }; -class PowerKeySuspendMonitor : public SuspendMonitor { +class PowerKeySuspendMonitor : public SuspendMonitor, public std::enable_shared_from_this { public: explicit PowerKeySuspendMonitor(SuspendSource& source) : SuspendMonitor(source) {} ~PowerKeySuspendMonitor() override = default; diff --git a/services/native/src/wakeup/wakeup_controller.cpp b/services/native/src/wakeup/wakeup_controller.cpp index b168796b053f411530c6f60a8975381a95a9b94c..ac024e93b9376f498d08765c2eb5cba5db712c10 100644 --- a/services/native/src/wakeup/wakeup_controller.cpp +++ b/services/native/src/wakeup/wakeup_controller.cpp @@ -945,17 +945,17 @@ bool PowerkeyWakeupMonitor::Init() keyOption->SetFinalKey(OHOS::MMI::KeyEvent::KEYCODE_POWER); keyOption->SetFinalKeyDown(true); keyOption->SetFinalKeyDownDuration(0); - std::weak_ptr weak = weak_from_this(); auto inputManager = InputManager::GetInstance(); if (!inputManager) { POWER_HILOGE(FEATURE_WAKEUP, "PowerkeyWakeupMonitorInit inputManager is null"); return false; } + std::weak_ptr weak = weak_from_this(); powerkeyShortPressId_ = inputManager->SubscribeKeyEvent( keyOption, [weak](std::shared_ptr keyEvent) { std::shared_ptr strong = weak.lock(); if (!strong) { - POWER_HILOGI(FEATURE_WAKEUP, "[UL_POWER] PowerkeyWakeupMonitor is invaild, return"); + POWER_HILOGE(FEATURE_WAKEUP, "[UL_POWER] PowerkeyWakeupMonitor is invaild, return"); return; } diff --git a/test/unittest/src/interface_test/power_suspend_controller_test.cpp b/test/unittest/src/interface_test/power_suspend_controller_test.cpp index 260abce3f8a5949023ac91553bd826a4bee22296..b55ed9a62c8545b927c285af47f785487d79c53c 100644 --- a/test/unittest/src/interface_test/power_suspend_controller_test.cpp +++ b/test/unittest/src/interface_test/power_suspend_controller_test.cpp @@ -411,4 +411,39 @@ HWTEST_F(PowerSuspendControllerTest, PowerSuspendControllerTest016, TestSize.Lev GTEST_LOG_(INFO) << "PowerSuspendControllerTest016: end"; POWER_HILOGI(LABEL_TEST, "PowerSuspendControllerTest016 function end!"); } + +/** + * @tc.name: PowerSuspendControllerTest017 + * @tc.desc: test simulate powerkey event when screenon + * @tc.type: FUNC + */ +HWTEST_F(PowerSuspendControllerTest, PowerSuspendControllerTest017, TestSize.Level0) +{ + POWER_HILOGI(LABEL_TEST, "PowerSuspendControllerTest017 function start!"); + GTEST_LOG_(INFO) << "PowerSuspendControllerTest017: start"; + + g_service->WakeupControllerInit(); + g_service->SuspendControllerInit(); + g_service->WakeupDevice( + static_cast(time(nullptr)), WakeupDeviceType::WAKEUP_DEVICE_PLUG_CHANGE, "plug change"); + EXPECT_TRUE(g_service->IsScreenOn()); + std::shared_ptr suspendController = g_service->GetSuspendController(); + suspendController->monitorMap_.clear(); + + auto inputManager = MMI::InputManager::GetInstance(); + std::shared_ptr keyEventPowerkeyDown = MMI::KeyEvent::Create(); + keyEventPowerkeyDown->SetKeyAction(MMI::KeyEvent::KEY_ACTION_DOWN); + keyEventPowerkeyDown->SetKeyCode(MMI::KeyEvent::KEYCODE_POWER); + std::shared_ptr keyEventPowerkeyUp = MMI::KeyEvent::Create(); + keyEventPowerkeyUp->SetKeyAction(MMI::KeyEvent::KEY_ACTION_UP); + keyEventPowerkeyUp->SetKeyCode(MMI::KeyEvent::KEYCODE_POWER); + + inputManager->SimulateInputEvent(keyEventPowerkeyDown); + inputManager->SimulateInputEvent(keyEventPowerkeyUp); + sleep(2); + EXPECT_FALSE(g_service->IsScreenOn()); + + GTEST_LOG_(INFO) << "PowerSuspendControllerTest017: end"; + POWER_HILOGI(LABEL_TEST, "PowerSuspendControllerTest017 function end!"); +} } // namespace \ No newline at end of file diff --git a/test/unittest/src/interface_test/power_wakeup_controller_test.cpp b/test/unittest/src/interface_test/power_wakeup_controller_test.cpp index 89d94a26828853044a8939047ee01bb43fc28b2c..1645ff5ebde5abf07fbebd725ebe8f468983ac13 100644 --- a/test/unittest/src/interface_test/power_wakeup_controller_test.cpp +++ b/test/unittest/src/interface_test/power_wakeup_controller_test.cpp @@ -416,6 +416,7 @@ HWTEST_F(PowerWakeupControllerTest, PowerWakeupControllerTest011, TestSize.Level HWTEST_F(PowerWakeupControllerTest, PowerWakeupControllerTest012, TestSize.Level0) { POWER_HILOGI(LABEL_TEST, "PowerWakeupControllerTest012 function start!"); + GTEST_LOG_(INFO) << "PowerWakeupControllerTest012: start"; g_service->WakeupControllerInit(); g_service->SetDisplayOffTime(DISPLAY_OFF_TIME_MS); g_service->WakeupDevice(static_cast(time(nullptr)), @@ -428,6 +429,7 @@ HWTEST_F(PowerWakeupControllerTest, PowerWakeupControllerTest012, TestSize.Level usleep(SLEEP_WAIT_TIME_MS * 1000); EXPECT_TRUE(g_service->IsScreenOn()); g_service->SetDisplayOffTime(RECOVER_DISPLAY_OFF_TIME_S); + GTEST_LOG_(INFO) << "PowerWakeupControllerTest012: end"; POWER_HILOGI(LABEL_TEST, "PowerWakeupControllerTest012 function end!"); } @@ -439,6 +441,7 @@ HWTEST_F(PowerWakeupControllerTest, PowerWakeupControllerTest012, TestSize.Level HWTEST_F(PowerWakeupControllerTest, PowerWakeupControllerTest013, TestSize.Level0) { POWER_HILOGI(LABEL_TEST, "PowerWakeupControllerTest013 function start!"); + GTEST_LOG_(INFO) << "PowerWakeupControllerTest013: start"; g_service->WakeupControllerInit(); g_service->SetDisplayOffTime(DISPLAY_OFF_TIME_MS); g_service->WakeupDevice(static_cast(time(nullptr)), @@ -451,6 +454,7 @@ HWTEST_F(PowerWakeupControllerTest, PowerWakeupControllerTest013, TestSize.Level usleep(SLEEP_WAIT_TIME_MS * 1000); EXPECT_TRUE(g_service->IsScreenOn()); g_service->SetDisplayOffTime(RECOVER_DISPLAY_OFF_TIME_S); + GTEST_LOG_(INFO) << "PowerWakeupControllerTest013: end"; POWER_HILOGI(LABEL_TEST, "PowerWakeupControllerTest013 function end!"); } @@ -462,6 +466,7 @@ HWTEST_F(PowerWakeupControllerTest, PowerWakeupControllerTest013, TestSize.Level HWTEST_F(PowerWakeupControllerTest, PowerWakeupControllerTest014, TestSize.Level0) { POWER_HILOGI(LABEL_TEST, "PowerWakeupControllerTest014 function start!"); + GTEST_LOG_(INFO) << "PowerWakeupControllerTest014: start"; g_service->WakeupControllerInit(); g_service->SetDisplayOffTime(DISPLAY_OFF_TIME_MS); g_service->WakeupDevice(static_cast(time(nullptr)), @@ -474,6 +479,7 @@ HWTEST_F(PowerWakeupControllerTest, PowerWakeupControllerTest014, TestSize.Level usleep(SLEEP_WAIT_TIME_MS * 1000); EXPECT_TRUE(g_service->IsScreenOn()); g_service->SetDisplayOffTime(RECOVER_DISPLAY_OFF_TIME_S); + GTEST_LOG_(INFO) << "PowerWakeupControllerTest014: end"; POWER_HILOGI(LABEL_TEST, "PowerWakeupControllerTest014 function end!"); } @@ -486,12 +492,14 @@ HWTEST_F(PowerWakeupControllerTest, PowerWakeupControllerTest014, TestSize.Level HWTEST_F(PowerWakeupControllerTest, PowerWakeupControllerTest015, TestSize.Level0) { POWER_HILOGI(LABEL_TEST, "PowerWakeupControllerTest015 function start!"); + GTEST_LOG_(INFO) << "PowerWakeupControllerTest015: start"; g_service->WakeupControllerInit(); auto wakeupController_ = g_service->GetWakeupController(); EXPECT_TRUE(wakeupController_ != nullptr); wakeupController_->ChangeWakeupSourceConfig(true); auto resCode = wakeupController_->SetWakeupDoubleClickSensor(true); EXPECT_TRUE(resCode != -1); + GTEST_LOG_(INFO) << "PowerWakeupControllerTest015: end"; POWER_HILOGI(LABEL_TEST, "PowerWakeupControllerTest015 function end!"); } #endif @@ -504,11 +512,13 @@ HWTEST_F(PowerWakeupControllerTest, PowerWakeupControllerTest015, TestSize.Level HWTEST_F(PowerWakeupControllerTest, PowerWakeupControllerTest016, TestSize.Level0) { POWER_HILOGI(LABEL_TEST, "PowerWakeupControllerTest016 function start!"); + GTEST_LOG_(INFO) << "PowerWakeupControllerTest016: start"; g_service->WakeupControllerInit(); auto wakeupController_ = g_service->GetWakeupController(); EXPECT_TRUE(wakeupController_ != nullptr); wakeupController_->ChangePickupWakeupSourceConfig(true); wakeupController_->PickupConnectMotionConfig(true); + GTEST_LOG_(INFO) << "PowerWakeupControllerTest016: end"; POWER_HILOGI(LABEL_TEST, "PowerWakeupControllerTest016 function end!"); } #endif @@ -559,12 +569,15 @@ HWTEST_F(PowerWakeupControllerTest, PowerWakeupControllerTest018, TestSize.Level HWTEST_F(PowerWakeupControllerTest, PowerWakeupControllerTest019, TestSize.Level0) { POWER_HILOGI(LABEL_TEST, "PowerWakeupControllerTest019 function start!"); + GTEST_LOG_(INFO) << "PowerWakeupControllerTest019: start"; g_service->WakeupControllerInit(); g_service->SuspendControllerInit(); g_service->SuspendDevice( static_cast(time(nullptr)), SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false); EXPECT_FALSE(g_service->IsScreenOn()); + std::shared_ptr wakeupController = g_service->GetWakeupController(); + wakeupController->monitorMap_.clear(); auto inputManager = MMI::InputManager::GetInstance(); std::shared_ptr keyEventPowerkeyDown = MMI::KeyEvent::Create(); @@ -585,6 +598,7 @@ HWTEST_F(PowerWakeupControllerTest, PowerWakeupControllerTest019, TestSize.Level UserActivityType::USER_ACTIVITY_TYPE_TOUCH, true); EXPECT_TRUE(g_service->IsScreenOn()); + GTEST_LOG_(INFO) << "PowerWakeupControllerTest019: end"; POWER_HILOGI(LABEL_TEST, "PowerWakeupControllerTest019 function end!"); } @@ -597,6 +611,7 @@ HWTEST_F(PowerWakeupControllerTest, PowerWakeupControllerTest019, TestSize.Level HWTEST_F(PowerWakeupControllerTest, PowerWakeupControllerTest020, TestSize.Level0) { POWER_HILOGI(LABEL_TEST, "PowerWakeupControllerTest020 function start!"); + GTEST_LOG_(INFO) << "PowerWakeupControllerTest020: start"; #ifdef POWER_MANAGER_ENABLE_EXTERNAL_SCREEN_MANAGEMENT g_service->SuspendControllerInit(); g_service->WakeupControllerInit(); @@ -619,6 +634,7 @@ HWTEST_F(PowerWakeupControllerTest, PowerWakeupControllerTest020, TestSize.Level wakeupController->ControlListener(WakeupDeviceType::WAKEUP_DEVICE_SWITCH); EXPECT_TRUE(powerStateMachine->IsScreenOn()); #endif + GTEST_LOG_(INFO) << "PowerWakeupControllerTest020: end"; POWER_HILOGI(LABEL_TEST, "PowerWakeupControllerTest020 function end!"); } @@ -630,7 +646,7 @@ HWTEST_F(PowerWakeupControllerTest, PowerWakeupControllerTest020, TestSize.Level */ HWTEST_F(PowerWakeupControllerTest, PowerWakeupControllerTest021, TestSize.Level0) { - POWER_HILOGI(LABEL_TEST, "PowerWakeupControllerTest021: start"); + POWER_HILOGI(LABEL_TEST, "PowerWakeupControllerTest021 function start!"); auto powerStateMachine = g_service->GetPowerStateMachine(); g_service->WakeupControllerInit(); auto wakeupController_ = g_service->GetWakeupController(); @@ -677,7 +693,7 @@ HWTEST_F(PowerWakeupControllerTest, PowerWakeupControllerTest021, TestSize.Level axisEvent = nullptr; callbackThird->OnInputEvent(axisEvent); delete callbackThird; - POWER_HILOGI(LABEL_TEST, "PowerWakeupControllerTest021: end"); + POWER_HILOGI(LABEL_TEST, "PowerWakeupControllerTest021 function end!"); } /** @@ -688,7 +704,8 @@ HWTEST_F(PowerWakeupControllerTest, PowerWakeupControllerTest021, TestSize.Level */ HWTEST_F(PowerWakeupControllerTest, PowerWakeupControllerTest022, TestSize.Level0) { - POWER_HILOGI(LABEL_TEST, "PowerWakeupControllerTest022: start"); + POWER_HILOGI(LABEL_TEST, "PowerWakeupControllerTest022 function start!"); + GTEST_LOG_(INFO) << "PowerWakeupControllerTest022: start"; auto powerStateMachine = g_service->GetPowerStateMachine(); g_service->WakeupControllerInit(); auto wakeupController_ = g_service->GetWakeupController(); @@ -701,6 +718,7 @@ HWTEST_F(PowerWakeupControllerTest, PowerWakeupControllerTest022, TestSize.Level g_service->InputMonitorCancel(); g_service->SwitchSubscriberCancel(); - POWER_HILOGI(LABEL_TEST, "PowerWakeupControllerTest022: end"); + GTEST_LOG_(INFO) << "PowerWakeupControllerTest022: end"; + POWER_HILOGI(LABEL_TEST, "PowerWakeupControllerTest022 function end!"); } } // namespace \ No newline at end of file