diff --git a/services/native/src/power_state_machine.cpp b/services/native/src/power_state_machine.cpp index b4e468e58209b16555ce4b813d422ef1774ff734..f15cf3125c2ee4a96aa78965d9e2bf6097e5a0ed 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 000310aacb27bc43a51494d41270b670ce26fe58..0d27c1e52fc71f1c88d0bb2321ff616648b961e5 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 54ff5b77da29b7fa190232b14ca70d6da8b9fd11..4f4e8d8f25629be48b7ff1be829c0c2751586853 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 2d2ade98a874e55a62a2357f2ee286fdca8863b1..f89b0fade21fef5294f945457499aff8e0d37bdd 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