From a2cb3054ec1e50a8aca35a999088d4471b795614 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E6=B0=B8=E9=91=AB?= Date: Tue, 17 Jun 2025 21:05:29 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=81=AF=E5=B1=8F=E5=BC=82=E5=B8=B8?= =?UTF-8?q?=E7=BB=B4=E6=B5=8B=E6=89=93=E7=82=B9=E9=9C=80=E6=B1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 吴永鑫 --- services/native/src/power_state_machine.cpp | 14 +++--- .../src/interface_test/running_lock_test.cpp | 22 ++++----- .../src/native_power_state_machine_test.cpp | 45 ++++++++++++++++-- .../unittest/src/running_lock_native_test.cpp | 47 +++++++++++++++++++ 4 files changed, 104 insertions(+), 24 deletions(-) diff --git a/services/native/src/power_state_machine.cpp b/services/native/src/power_state_machine.cpp index b4e468e5..f15cf312 100644 --- a/services/native/src/power_state_machine.cpp +++ b/services/native/src/power_state_machine.cpp @@ -2554,15 +2554,17 @@ bool PowerStateMachine::ReportScreenOffInvalidEvent(StateChangeReason reason) return false; } FFRTTask task = [this] { + auto pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + POWER_HILOGE(FEATURE_POWER_STATE, "ReportScreenOffInvalidEvent fail, Pms is nullptr"); + return; + } // condition 1: no audiolock // condition 2: has screenlock - if (!IsRunningLockEnabled(RunningLockType::RUNNINGLOCK_BACKGROUND_AUDIO)) { + bool result = true; + pms->IsRunningLockEnabled(RunningLockType::RUNNINGLOCK_BACKGROUND_AUDIO, result); + if (!result) { std::map screenOnLockLists; - auto pms = DelayedSpSingleton::GetInstance(); - if (pms == nullptr) { - POWER_HILOGE(FEATURE_POWER_STATE, "ReportScreenOffInvalidEvent fail, Pms is nullptr"); - return; - } pms->QueryRunningLockListsInner(screenOnLockLists); for (const auto &it : screenOnLockLists) { HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::POWER, "SCREEN_OFF_EXCEPTION", diff --git a/test/unittest/src/interface_test/running_lock_test.cpp b/test/unittest/src/interface_test/running_lock_test.cpp index 000310aa..0d27c1e5 100644 --- a/test/unittest/src/interface_test/running_lock_test.cpp +++ b/test/unittest/src/interface_test/running_lock_test.cpp @@ -541,25 +541,21 @@ HWTEST_F (RunningLockTest, RunningLockTest019, TestSize.Level1) /** * @tc.name: RunningLockTest020 - * @tc.desc: Test UpdateWorkSource function + * @tc.desc: Test IsExistAudioStream function * @tc.type: FUNC * @tc.require */ HWTEST_F (RunningLockTest, RunningLockTest020, TestSize.Level1) { POWER_HILOGI(LABEL_TEST, "RunningLockTest020 function start!"); - auto runninglockProxy = std::make_shared(); - auto& powerMgrClient = PowerMgrClient::GetInstance(); - std::shared_ptr runningLock = powerMgrClient.CreateRunningLock("backgroudAudio.test020", - RunningLockType::RUNNINGLOCK_BACKGROUND_AUDIO); - ASSERT_NE(runningLock, nullptr); - - pid_t curUid = 1; - runningLock->Lock(); - EXPECT_TRUE(runningLock->IsUsed()); - EXPECT_FALSE(runninglockProxy->IsExistAudioStream(curUid)); - runningLock->UnLock(); - EXPECT_FALSE(runningLock->IsUsed()); + pid_t curUid = -1; + auto pms = DelayedSpSingleton::GetInstance(); + pms->OnStart(); + bool result = pms->IsExistAudioStream(curUid); + EXPECT_FALSE(result); + auto runningLockMgr = pms->GetRunningLockMgr(); + result = runningLockMgr->IsExistAudioStream(curUid); + EXPECT_FALSE(result); POWER_HILOGI(LABEL_TEST, "RunningLockTest020 function end!"); } } // namespace \ No newline at end of file diff --git a/test/unittest/src/native_power_state_machine_test.cpp b/test/unittest/src/native_power_state_machine_test.cpp index 54ff5b77..4f4e8d8f 100644 --- a/test/unittest/src/native_power_state_machine_test.cpp +++ b/test/unittest/src/native_power_state_machine_test.cpp @@ -345,11 +345,6 @@ HWTEST_F(NativePowerStateMachineTest, NativePowerStateMachine007, TestSize.Level pmsTest->UnLock(token); EXPECT_EQ(pmsTest->IsUsed(token), false); - - ret = stateMachine->ReportScreenOffInvalidEvent(StateChangeReason::STATE_CHANGE_REASON_HARD_KEY); - EXPECT_TRUE(ret); - ret = stateMachine->ReportAbnormalScreenOffEvent(StateChangeReason::STATE_CHANGE_REASON_TIMEOUT); - EXPECT_TRUE(ret); POWER_HILOGI(LABEL_TEST, "NativePowerStateMachine007 function end!"); GTEST_LOG_(INFO) << "NativePowerStateMachine007: Suspend Device end."; } @@ -440,4 +435,44 @@ HWTEST_F(NativePowerStateMachineTest, NativePowerStateMachine010, TestSize.Level POWER_HILOGI(LABEL_TEST, "NativePowerStateMachine010 function end!"); } +/** + * @tc.name: NativePowerStateMachine011 + * @tc.desc: test ReportScreenOffInvalidEvent and ReportAbnormalScreenOffEvent in powerStateMachine + * @tc.type: FUNC + */ +HWTEST_F(NativePowerStateMachineTest, NativePowerStateMachine011, TestSize.Level1) +{ + POWER_HILOGI(LABEL_TEST, "NativePowerStateMachine011 function start!"); + auto pmsTest = DelayedSpSingleton::GetInstance(); + pmsTest->OnStart(); + auto stateMachine = pmsTest->GetPowerStateMachine(); + EXPECT_TRUE(stateMachine->Init()); + + StateChangeReason reason = StateChangeReason::STATE_CHANGE_REASON_UNKNOWN; + bool result = stateMachine->ReportScreenOffInvalidEvent(reason); + EXPECT_FALSE(result); + reason = StateChangeReason::STATE_CHANGE_REASON_HARD_KEY; + result = stateMachine->ReportScreenOffInvalidEvent(reason); + EXPECT_TRUE(result); + sptr token = new RunningLockTokenStub(); + RunningLockInfo infoInactive("NativePowerStateMachine011_1", RunningLockType::RUNNINGLOCK_SCREEN); + pmsTest->CreateRunningLock(token, infoInactive); + pmsTest->Lock(token); + result = stateMachine->ReportScreenOffInvalidEvent(reason); + EXPECT_TRUE(result); + + reason = static_cast(999); + result = stateMachine->ReportAbnormalScreenOffEvent(reason); + EXPECT_FALSE(result); + reason = StateChangeReason::STATE_CHANGE_REASON_TIMEOUT; + result = stateMachine->ReportAbnormalScreenOffEvent(reason); + EXPECT_TRUE(result); + stateMachine->forceTimingOut_.store(true); + result = stateMachine->ReportAbnormalScreenOffEvent(reason); + EXPECT_TRUE(result); + reason = StateChangeReason::STATE_CHANGE_REASON_TIMEOUT_NO_SCREEN_LOCK; + result = stateMachine->ReportAbnormalScreenOffEvent(reason); + EXPECT_TRUE(result); + POWER_HILOGI(LABEL_TEST, "NativePowerStateMachine011 function end!"); +} } // namespace diff --git a/test/unittest/src/running_lock_native_test.cpp b/test/unittest/src/running_lock_native_test.cpp index 2d2ade98..f89b0fad 100644 --- a/test/unittest/src/running_lock_native_test.cpp +++ b/test/unittest/src/running_lock_native_test.cpp @@ -732,4 +732,51 @@ HWTEST_F (RunningLockNativeTest, RunningLockNative023, TestSize.Level1) POWER_HILOGI(LABEL_TEST, "RunningLockNative023 function end!"); } + +/** + * @tc.name: RunningLockNative024 + * @tc.desc: test IsExistAudioStream GetRunningLockMgr is nullptr + * @tc.type: FUNC + */ +HWTEST_F (RunningLockNativeTest, RunningLockNative024, TestSize.Level1) +{ + POWER_HILOGI(LABEL_TEST, "RunningLockNative024 function start!"); + auto pmsTest = DelayedSpSingleton::GetInstance(); + + auto runningLockMgr = std::make_shared(pmsTest); + EXPECT_TRUE(runningLockMgr->Init()); + bool result = runningLockMgr->runninglockProxy_->IsExistAudioStream(UID); + EXPECT_FALSE(false); + POWER_HILOGI(LABEL_TEST, "RunningLockNative024 function end!"); +} + +HWTEST_F (RunningLockNativeTest, RunningLockNative025, TestSize.Level1) +{ + POWER_HILOGI(LABEL_TEST, "RunningLockNative025 function start!"); + auto pmsTest = DelayedSpSingleton::GetInstance(); + pmsTest->OnStart(); + auto runningLockMgr = pmsTest->GetRunningLockMgr(); + EXPECT_TRUE(runningLockMgr->Init()); + sptr token0 = new RunningLockTokenStub(); + sptr token1 = new RunningLockTokenStub(); + sptr token2 = new RunningLockTokenStub(); + RunningLockParam runningLockParam0 { + 0, "runningLockNative024_1", "", RunningLockType::RUNNINGLOCK_BACKGROUND_AUDIO, TIMEOUTMS, PID, UID}; + RunningLockParam runningLockParam1 { + 1, "runningLockNative024_1", "", RunningLockType::RUNNINGLOCK_BACKGROUND_SPORT, TIMEOUTMS, PID, UID}; + EXPECT_TRUE(runningLockMgr->CreateRunningLock(token0, runningLockParam0) != nullptr); + EXPECT_TRUE(runningLockMgr->CreateRunningLock(token1, runningLockParam1) != nullptr); + runningLockMgr->runninglockProxy_->AddRunningLock(PID, UID, token2); + runningLockMgr->Lock(token0); + EXPECT_TRUE(runningLockMgr->NeedNotify(RunningLockType::RUNNINGLOCK_BACKGROUND_AUDIO)); + bool result = runningLockMgr->runninglockProxy_->IsExistAudioStream(UID); + EXPECT_FALSE(false); + std::map wks{{UID, ""}}; + runningLockMgr->runninglockProxy_->UpdateWorkSource(PID, UID, token0, wks); + result = runningLockMgr->runninglockProxy_->IsExistAudioStream(UID); + EXPECT_TRUE(result); + runningLockMgr->UnLock(token0); + EXPECT_FALSE(runningLockMgr->ReleaseLock(token0)); + POWER_HILOGI(LABEL_TEST, "RunningLockNative025 function end!"); +} } // namespace -- Gitee