Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 055512db authored by Michal Brzezinski's avatar Michal Brzezinski Committed by Michał Brzeziński
Browse files

Waiting for a new display state when tracking display switch latency

Currently we're just reading latest state which with enough bad luck can still represent "old" display being on and not new display state.

Bug: 394257187
Test: DisplaySwitchLatencyTrackerTest
Flag: EXEMPT bugfix
Change-Id: Ib8df3d60af2c7682ac03d5ca5b6476c7fd076ff0
parent a3b00840
Loading
Loading
Loading
Loading
+29 −1
Original line number Diff line number Diff line
@@ -56,7 +56,6 @@ import com.android.systemui.unfold.data.repository.UnfoldTransitionRepositoryImp
import com.android.systemui.unfold.domain.interactor.UnfoldTransitionInteractor
import com.android.systemui.unfoldedDeviceState
import com.android.systemui.util.animation.data.repository.fakeAnimationStatusRepository
import com.android.systemui.util.mockito.any
import com.android.systemui.util.mockito.capture
import com.android.systemui.util.time.FakeSystemClock
import com.google.common.truth.Truth.assertThat
@@ -79,8 +78,10 @@ import org.mockito.Mockito.never
import org.mockito.Mockito.verify
import org.mockito.Mockito.`when` as whenever
import org.mockito.MockitoAnnotations
import org.mockito.kotlin.any
import org.mockito.kotlin.mock
import org.mockito.kotlin.times
import org.mockito.kotlin.verifyNoMoreInteractions

@RunWith(AndroidJUnit4::class)
@SmallTest
@@ -603,12 +604,39 @@ class DisplaySwitchLatencyTrackerTest : SysuiTestCase() {
        }
    }

    @Test
    fun foldingStarted_screenStillOn_eventSentOnlyAfterScreenSwitches() {
        // can happen for both folding and unfolding (with animations off) but it's more likely to
        // happen when folding as waiting for screen on is the default case then
        testScope.runTest {
            startInUnfoldedState(displaySwitchLatencyTracker)
            setDeviceState(FOLDED)
            powerInteractor.setScreenPowerState(SCREEN_ON)
            runCurrent()

            verifyNoMoreInteractions(displaySwitchLatencyLogger)

            powerInteractor.setScreenPowerState(SCREEN_OFF)
            runCurrent()
            powerInteractor.setScreenPowerState(SCREEN_ON)
            runCurrent()

            verify(displaySwitchLatencyLogger).log(any())
        }
    }

    private suspend fun TestScope.startInFoldedState(tracker: DisplaySwitchLatencyTracker) {
        setDeviceState(FOLDED)
        tracker.start()
        runCurrent()
    }

    private suspend fun TestScope.startInUnfoldedState(tracker: DisplaySwitchLatencyTracker) {
        setDeviceState(UNFOLDED)
        tracker.start()
        runCurrent()
    }

    private suspend fun TestScope.startUnfolding() {
        setDeviceState(HALF_FOLDED)
        powerInteractor.setScreenPowerState(SCREEN_OFF)
+6 −1
Original line number Diff line number Diff line
@@ -214,7 +214,12 @@ constructor(

    private suspend fun waitForScreenTurnedOn() {
        traceAsync(TAG, "waitForScreenTurnedOn()") {
            powerInteractor.screenPowerState.filter { it == ScreenPowerState.SCREEN_ON }.first()
            // dropping first as it's stateFlow and will always emit latest value but we're
            // only interested in new states
            powerInteractor.screenPowerState
                .drop(1)
                .filter { it == ScreenPowerState.SCREEN_ON }
                .first()
        }
    }

+7 −1
Original line number Diff line number Diff line
@@ -50,6 +50,7 @@ import javax.inject.Inject
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.TimeoutCancellationException
import kotlinx.coroutines.asCoroutineDispatcher
import kotlinx.coroutines.flow.drop
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.flatMapLatest
@@ -160,7 +161,12 @@ constructor(

    private suspend fun waitForScreenTurnedOn() {
        traceAsync(TAG, "waitForScreenTurnedOn()") {
            powerInteractor.screenPowerState.filter { it == ScreenPowerState.SCREEN_ON }.first()
            // dropping first as it's stateFlow and will always emit latest value but we're
            // only interested in new states
            powerInteractor.screenPowerState
                .drop(1)
                .filter { it == ScreenPowerState.SCREEN_ON }
                .first()
        }
    }