KeyguardTransitionInteractor#transitionValue.

This is a function that returns a flow of all transitions in or out of
the given KeyguardState. Should be useful when a consumer cares about a
specific state but doesn't care about which other state we're move in
from or moving out to.

Bug: 274159734
Flag: N/A
Test: unit test included
Test: actively calling this from the next CL
Change-Id: Iab6a756892a388e306562eae1a18a17e32112417
This commit is contained in:
Alejandro Nijamkin
2023-03-23 14:32:36 -07:00
parent 0628dfba68
commit 1197d0e142
2 changed files with 147 additions and 119 deletions

View File

@@ -39,7 +39,7 @@ import kotlinx.coroutines.flow.merge
class KeyguardTransitionInteractor
@Inject
constructor(
repository: KeyguardTransitionRepository,
private val repository: KeyguardTransitionRepository,
) {
/** (any)->GONE transition information */
val anyStateToGoneTransition: Flow<TransitionStep> =
@@ -62,10 +62,6 @@ constructor(
/** LOCKSCREEN->AOD transition information. */
val lockscreenToAodTransition: Flow<TransitionStep> = repository.transition(LOCKSCREEN, AOD)
/** LOCKSCREEN->PRIMARY_BOUNCER transition information. */
val mLockscreenToPrimaryBouncerTransition: Flow<TransitionStep> =
repository.transition(LOCKSCREEN, PRIMARY_BOUNCER)
/** LOCKSCREEN->DREAMING transition information. */
val lockscreenToDreamingTransition: Flow<TransitionStep> =
repository.transition(LOCKSCREEN, DREAMING)
@@ -92,19 +88,39 @@ constructor(
lockscreenToAodTransition,
)
/* The last [TransitionStep] with a [TransitionState] of STARTED */
/** The last [TransitionStep] with a [TransitionState] of STARTED */
val startedKeyguardTransitionStep: Flow<TransitionStep> =
repository.transitions.filter { step -> step.transitionState == TransitionState.STARTED }
/* The last [TransitionStep] with a [TransitionState] of CANCELED */
/** The last [TransitionStep] with a [TransitionState] of CANCELED */
val canceledKeyguardTransitionStep: Flow<TransitionStep> =
repository.transitions.filter { step -> step.transitionState == TransitionState.CANCELED }
/* The last [TransitionStep] with a [TransitionState] of FINISHED */
/** The last [TransitionStep] with a [TransitionState] of FINISHED */
val finishedKeyguardTransitionStep: Flow<TransitionStep> =
repository.transitions.filter { step -> step.transitionState == TransitionState.FINISHED }
/* The last completed [KeyguardState] transition */
/** The last completed [KeyguardState] transition */
val finishedKeyguardState: Flow<KeyguardState> =
finishedKeyguardTransitionStep.map { step -> step.to }
/**
* The amount of transition into or out of the given [KeyguardState].
*
* The value will be `0` (or close to `0`, due to float point arithmetic) if not in this step or
* `1` when fully in the given state.
*/
fun transitionValue(
state: KeyguardState,
): Flow<Float> {
return repository.transitions
.filter { it.from == state || it.to == state }
.map {
if (it.from == state) {
1 - it.value
} else {
it.value
}
}
}
}

View File

@@ -20,9 +20,10 @@ package com.android.systemui.keyguard.domain.interactor
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.android.systemui.SysuiTestCase
import com.android.systemui.coroutines.collectValues
import com.android.systemui.keyguard.data.repository.FakeKeyguardTransitionRepository
import com.android.systemui.keyguard.shared.model.KeyguardState
import com.android.systemui.keyguard.shared.model.KeyguardState.AOD
import com.android.systemui.keyguard.shared.model.KeyguardState.DOZING
import com.android.systemui.keyguard.shared.model.KeyguardState.GONE
import com.android.systemui.keyguard.shared.model.KeyguardState.LOCKSCREEN
import com.android.systemui.keyguard.shared.model.TransitionState.FINISHED
@@ -30,9 +31,7 @@ import com.android.systemui.keyguard.shared.model.TransitionState.RUNNING
import com.android.systemui.keyguard.shared.model.TransitionState.STARTED
import com.android.systemui.keyguard.shared.model.TransitionStep
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.test.runCurrent
import kotlinx.coroutines.test.runTest
import org.junit.Before
import org.junit.Test
@@ -53,138 +52,151 @@ class KeyguardTransitionInteractorTest : SysuiTestCase() {
}
@Test
fun `transition collectors receives only appropriate events`() =
runTest(UnconfinedTestDispatcher()) {
var lockscreenToAodSteps = mutableListOf<TransitionStep>()
val job1 =
underTest.lockscreenToAodTransition
.onEach { lockscreenToAodSteps.add(it) }
.launchIn(this)
fun `transition collectors receives only appropriate events`() = runTest {
val lockscreenToAodSteps by collectValues(underTest.lockscreenToAodTransition)
val aodToLockscreenSteps by collectValues(underTest.aodToLockscreenTransition)
var aodToLockscreenSteps = mutableListOf<TransitionStep>()
val job2 =
underTest.aodToLockscreenTransition
.onEach { aodToLockscreenSteps.add(it) }
.launchIn(this)
val steps = mutableListOf<TransitionStep>()
steps.add(TransitionStep(AOD, GONE, 0f, STARTED))
steps.add(TransitionStep(AOD, GONE, 1f, FINISHED))
steps.add(TransitionStep(AOD, LOCKSCREEN, 0f, STARTED))
steps.add(TransitionStep(AOD, LOCKSCREEN, 0.5f, RUNNING))
steps.add(TransitionStep(AOD, LOCKSCREEN, 1f, FINISHED))
steps.add(TransitionStep(LOCKSCREEN, AOD, 0f, STARTED))
steps.add(TransitionStep(LOCKSCREEN, AOD, 0.1f, RUNNING))
steps.add(TransitionStep(LOCKSCREEN, AOD, 0.2f, RUNNING))
val steps = mutableListOf<TransitionStep>()
steps.add(TransitionStep(AOD, GONE, 0f, STARTED))
steps.add(TransitionStep(AOD, GONE, 1f, FINISHED))
steps.add(TransitionStep(AOD, LOCKSCREEN, 0f, STARTED))
steps.add(TransitionStep(AOD, LOCKSCREEN, 0.5f, RUNNING))
steps.add(TransitionStep(AOD, LOCKSCREEN, 1f, FINISHED))
steps.add(TransitionStep(LOCKSCREEN, AOD, 0f, STARTED))
steps.add(TransitionStep(LOCKSCREEN, AOD, 0.1f, RUNNING))
steps.add(TransitionStep(LOCKSCREEN, AOD, 0.2f, RUNNING))
steps.forEach { repository.sendTransitionStep(it) }
assertThat(aodToLockscreenSteps).isEqualTo(steps.subList(2, 5))
assertThat(lockscreenToAodSteps).isEqualTo(steps.subList(5, 8))
job1.cancel()
job2.cancel()
steps.forEach {
repository.sendTransitionStep(it)
runCurrent()
}
assertThat(aodToLockscreenSteps).isEqualTo(steps.subList(2, 5))
assertThat(lockscreenToAodSteps).isEqualTo(steps.subList(5, 8))
}
@Test
fun dozeAmountTransitionTest() =
runTest(UnconfinedTestDispatcher()) {
var dozeAmountSteps = mutableListOf<TransitionStep>()
val job = underTest.dozeAmountTransition.onEach { dozeAmountSteps.add(it) }.launchIn(this)
fun dozeAmountTransitionTest() = runTest {
val dozeAmountSteps by collectValues(underTest.dozeAmountTransition)
val steps = mutableListOf<TransitionStep>()
val steps = mutableListOf<TransitionStep>()
steps.add(TransitionStep(AOD, LOCKSCREEN, 0f, STARTED))
steps.add(TransitionStep(AOD, LOCKSCREEN, 0.5f, RUNNING))
steps.add(TransitionStep(AOD, LOCKSCREEN, 1f, FINISHED))
steps.add(TransitionStep(LOCKSCREEN, AOD, 0f, STARTED))
steps.add(TransitionStep(LOCKSCREEN, AOD, 0.8f, RUNNING))
steps.add(TransitionStep(LOCKSCREEN, AOD, 0.9f, RUNNING))
steps.add(TransitionStep(LOCKSCREEN, AOD, 1f, FINISHED))
steps.add(TransitionStep(AOD, LOCKSCREEN, 0f, STARTED))
steps.add(TransitionStep(AOD, LOCKSCREEN, 0.5f, RUNNING))
steps.add(TransitionStep(AOD, LOCKSCREEN, 1f, FINISHED))
steps.add(TransitionStep(LOCKSCREEN, AOD, 0f, STARTED))
steps.add(TransitionStep(LOCKSCREEN, AOD, 0.8f, RUNNING))
steps.add(TransitionStep(LOCKSCREEN, AOD, 0.9f, RUNNING))
steps.add(TransitionStep(LOCKSCREEN, AOD, 1f, FINISHED))
steps.forEach { repository.sendTransitionStep(it) }
steps.forEach {
repository.sendTransitionStep(it)
runCurrent()
}
assertThat(dozeAmountSteps.subList(0, 3))
.isEqualTo(
listOf(
steps[0].copy(value = 1f - steps[0].value),
steps[1].copy(value = 1f - steps[1].value),
steps[2].copy(value = 1f - steps[2].value),
)
assertThat(dozeAmountSteps.subList(0, 3))
.isEqualTo(
listOf(
steps[0].copy(value = 1f - steps[0].value),
steps[1].copy(value = 1f - steps[1].value),
steps[2].copy(value = 1f - steps[2].value),
)
assertThat(dozeAmountSteps.subList(3, 7)).isEqualTo(steps.subList(3, 7))
job.cancel()
}
)
assertThat(dozeAmountSteps.subList(3, 7)).isEqualTo(steps.subList(3, 7))
}
@Test
fun keyguardStateTests() =
runTest(UnconfinedTestDispatcher()) {
var finishedSteps = mutableListOf<KeyguardState>()
val job = underTest.finishedKeyguardState.onEach { finishedSteps.add(it) }.launchIn(this)
fun keyguardStateTests() = runTest {
val finishedSteps by collectValues(underTest.finishedKeyguardState)
val steps = mutableListOf<TransitionStep>()
val steps = mutableListOf<TransitionStep>()
steps.add(TransitionStep(AOD, LOCKSCREEN, 0f, STARTED))
steps.add(TransitionStep(AOD, LOCKSCREEN, 0.5f, RUNNING))
steps.add(TransitionStep(AOD, LOCKSCREEN, 1f, FINISHED))
steps.add(TransitionStep(LOCKSCREEN, AOD, 0f, STARTED))
steps.add(TransitionStep(LOCKSCREEN, AOD, 0.9f, RUNNING))
steps.add(TransitionStep(LOCKSCREEN, AOD, 1f, FINISHED))
steps.add(TransitionStep(AOD, GONE, 1f, STARTED))
steps.add(TransitionStep(AOD, LOCKSCREEN, 0f, STARTED))
steps.add(TransitionStep(AOD, LOCKSCREEN, 0.5f, RUNNING))
steps.add(TransitionStep(AOD, LOCKSCREEN, 1f, FINISHED))
steps.add(TransitionStep(LOCKSCREEN, AOD, 0f, STARTED))
steps.add(TransitionStep(LOCKSCREEN, AOD, 0.9f, RUNNING))
steps.add(TransitionStep(LOCKSCREEN, AOD, 1f, FINISHED))
steps.add(TransitionStep(AOD, GONE, 1f, STARTED))
steps.forEach { repository.sendTransitionStep(it) }
assertThat(finishedSteps).isEqualTo(listOf(LOCKSCREEN, AOD))
job.cancel()
steps.forEach {
repository.sendTransitionStep(it)
runCurrent()
}
assertThat(finishedSteps).isEqualTo(listOf(LOCKSCREEN, AOD))
}
@Test
fun finishedKeyguardTransitionStepTests() =
runTest(UnconfinedTestDispatcher()) {
var finishedSteps = mutableListOf<TransitionStep>()
val job =
underTest.finishedKeyguardTransitionStep.onEach { finishedSteps.add(it) }.launchIn(this)
fun finishedKeyguardTransitionStepTests() = runTest {
val finishedSteps by collectValues(underTest.finishedKeyguardTransitionStep)
val steps = mutableListOf<TransitionStep>()
val steps = mutableListOf<TransitionStep>()
steps.add(TransitionStep(AOD, LOCKSCREEN, 0f, STARTED))
steps.add(TransitionStep(AOD, LOCKSCREEN, 0.5f, RUNNING))
steps.add(TransitionStep(AOD, LOCKSCREEN, 1f, FINISHED))
steps.add(TransitionStep(LOCKSCREEN, AOD, 0f, STARTED))
steps.add(TransitionStep(LOCKSCREEN, AOD, 0.9f, RUNNING))
steps.add(TransitionStep(LOCKSCREEN, AOD, 1f, FINISHED))
steps.add(TransitionStep(AOD, GONE, 1f, STARTED))
steps.add(TransitionStep(AOD, LOCKSCREEN, 0f, STARTED))
steps.add(TransitionStep(AOD, LOCKSCREEN, 0.5f, RUNNING))
steps.add(TransitionStep(AOD, LOCKSCREEN, 1f, FINISHED))
steps.add(TransitionStep(LOCKSCREEN, AOD, 0f, STARTED))
steps.add(TransitionStep(LOCKSCREEN, AOD, 0.9f, RUNNING))
steps.add(TransitionStep(LOCKSCREEN, AOD, 1f, FINISHED))
steps.add(TransitionStep(AOD, GONE, 1f, STARTED))
steps.forEach { repository.sendTransitionStep(it) }
assertThat(finishedSteps).isEqualTo(listOf(steps[2], steps[5]))
job.cancel()
steps.forEach {
repository.sendTransitionStep(it)
runCurrent()
}
assertThat(finishedSteps).isEqualTo(listOf(steps[2], steps[5]))
}
@Test
fun startedKeyguardTransitionStepTests() =
runTest(UnconfinedTestDispatcher()) {
var startedSteps = mutableListOf<TransitionStep>()
val job =
underTest.startedKeyguardTransitionStep.onEach { startedSteps.add(it) }.launchIn(this)
fun startedKeyguardTransitionStepTests() = runTest {
val startedSteps by collectValues(underTest.startedKeyguardTransitionStep)
val steps = mutableListOf<TransitionStep>()
val steps = mutableListOf<TransitionStep>()
steps.add(TransitionStep(AOD, LOCKSCREEN, 0f, STARTED))
steps.add(TransitionStep(AOD, LOCKSCREEN, 0.5f, RUNNING))
steps.add(TransitionStep(AOD, LOCKSCREEN, 1f, FINISHED))
steps.add(TransitionStep(LOCKSCREEN, AOD, 0f, STARTED))
steps.add(TransitionStep(LOCKSCREEN, AOD, 0.9f, RUNNING))
steps.add(TransitionStep(LOCKSCREEN, AOD, 1f, FINISHED))
steps.add(TransitionStep(AOD, GONE, 1f, STARTED))
steps.add(TransitionStep(AOD, LOCKSCREEN, 0f, STARTED))
steps.add(TransitionStep(AOD, LOCKSCREEN, 0.5f, RUNNING))
steps.add(TransitionStep(AOD, LOCKSCREEN, 1f, FINISHED))
steps.add(TransitionStep(LOCKSCREEN, AOD, 0f, STARTED))
steps.add(TransitionStep(LOCKSCREEN, AOD, 0.9f, RUNNING))
steps.add(TransitionStep(LOCKSCREEN, AOD, 1f, FINISHED))
steps.add(TransitionStep(AOD, GONE, 1f, STARTED))
steps.forEach { repository.sendTransitionStep(it) }
assertThat(startedSteps).isEqualTo(listOf(steps[0], steps[3], steps[6]))
job.cancel()
steps.forEach {
repository.sendTransitionStep(it)
runCurrent()
}
assertThat(startedSteps).isEqualTo(listOf(steps[0], steps[3], steps[6]))
}
@Test
fun transitionValue() = runTest {
val startedSteps by collectValues(underTest.transitionValue(state = DOZING))
val toSteps =
listOf(
TransitionStep(AOD, DOZING, 0f, STARTED),
TransitionStep(AOD, DOZING, 0.5f, RUNNING),
TransitionStep(AOD, DOZING, 1f, FINISHED),
)
toSteps.forEach {
repository.sendTransitionStep(it)
runCurrent()
}
val fromSteps =
listOf(
TransitionStep(DOZING, LOCKSCREEN, 0f, STARTED),
TransitionStep(DOZING, LOCKSCREEN, 0.5f, RUNNING),
TransitionStep(DOZING, LOCKSCREEN, 1f, FINISHED),
)
fromSteps.forEach {
repository.sendTransitionStep(it)
runCurrent()
}
assertThat(startedSteps).isEqualTo(listOf(0f, 0.5f, 1f, 1f, 0.5f, 0f))
}
}