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

Commit d7438b71 authored by Alejandro Nijamkin's avatar Alejandro Nijamkin
Browse files

[flexiglass] Introduces SceneTestUtils.

Helps increase code reuse in tests.

Bug: 279501596
Test: N/A
Change-Id: Icb51a009da0e4fcdb6bae6f9a38663f7f843d08b
parent eebf1054
Loading
Loading
Loading
Loading
+4 −4
Original line number Diff line number Diff line
@@ -19,9 +19,9 @@ package com.android.systemui.authentication.domain.interactor
import androidx.test.filters.SmallTest
import com.android.systemui.SysuiTestCase
import com.android.systemui.authentication.data.repository.AuthenticationRepository
import com.android.systemui.authentication.data.repository.AuthenticationRepositoryImpl
import com.android.systemui.authentication.shared.model.AuthenticationMethodModel
import com.android.systemui.coroutines.collectLastValue
import com.android.systemui.scene.SceneTestUtils
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.TestScope
@@ -37,10 +37,10 @@ import org.junit.runners.JUnit4
class AuthenticationInteractorTest : SysuiTestCase() {

    private val testScope = TestScope()
    private val repository: AuthenticationRepository = AuthenticationRepositoryImpl()
    private val utils = SceneTestUtils(this, testScope)
    private val repository: AuthenticationRepository = utils.authenticationRepository()
    private val underTest =
        AuthenticationInteractor(
            applicationScope = testScope.backgroundScope,
        utils.authenticationInteractor(
            repository = repository,
        )

+6 −17
Original line number Diff line number Diff line
@@ -19,13 +19,9 @@ package com.android.systemui.bouncer.domain.interactor
import androidx.test.filters.SmallTest
import com.android.systemui.R
import com.android.systemui.SysuiTestCase
import com.android.systemui.authentication.data.repository.AuthenticationRepositoryImpl
import com.android.systemui.authentication.domain.interactor.AuthenticationInteractor
import com.android.systemui.authentication.shared.model.AuthenticationMethodModel
import com.android.systemui.bouncer.data.repo.BouncerRepository
import com.android.systemui.coroutines.collectLastValue
import com.android.systemui.scene.data.repository.fakeSceneContainerRepository
import com.android.systemui.scene.domain.interactor.SceneInteractor
import com.android.systemui.scene.SceneTestUtils
import com.android.systemui.scene.shared.model.SceneKey
import com.android.systemui.scene.shared.model.SceneModel
import com.google.common.truth.Truth.assertThat
@@ -44,23 +40,16 @@ import org.junit.runners.JUnit4
class BouncerInteractorTest : SysuiTestCase() {

    private val testScope = TestScope()
    private val utils = SceneTestUtils(this, testScope)
    private val authenticationInteractor =
        AuthenticationInteractor(
            applicationScope = testScope.backgroundScope,
            repository = AuthenticationRepositoryImpl(),
        )
    private val sceneInteractor =
        SceneInteractor(
            repository = fakeSceneContainerRepository(),
        utils.authenticationInteractor(
            repository = utils.authenticationRepository(),
        )
    private val sceneInteractor = utils.sceneInteractor()
    private val underTest =
        BouncerInteractor(
            applicationScope = testScope.backgroundScope,
            applicationContext = context,
            repository = BouncerRepository(),
        utils.bouncerInteractor(
            authenticationInteractor = authenticationInteractor,
            sceneInteractor = sceneInteractor,
            containerName = "container1",
        )

    @Before
+13 −38
Original line number Diff line number Diff line
@@ -18,14 +18,9 @@ package com.android.systemui.bouncer.ui.viewmodel

import androidx.test.filters.SmallTest
import com.android.systemui.SysuiTestCase
import com.android.systemui.authentication.data.repository.AuthenticationRepositoryImpl
import com.android.systemui.authentication.domain.interactor.AuthenticationInteractor
import com.android.systemui.authentication.shared.model.AuthenticationMethodModel
import com.android.systemui.bouncer.data.repo.BouncerRepository
import com.android.systemui.bouncer.domain.interactor.BouncerInteractor
import com.android.systemui.coroutines.collectLastValue
import com.android.systemui.scene.data.repository.fakeSceneContainerRepository
import com.android.systemui.scene.domain.interactor.SceneInteractor
import com.android.systemui.scene.SceneTestUtils
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.TestScope
@@ -40,33 +35,17 @@ import org.junit.runners.JUnit4
class BouncerViewModelTest : SysuiTestCase() {

    private val testScope = TestScope()
    private val sceneInteractor =
        SceneInteractor(
            repository = fakeSceneContainerRepository(),
        )
    private val mAuthenticationInteractor =
        AuthenticationInteractor(
            applicationScope = testScope.backgroundScope,
            repository = AuthenticationRepositoryImpl(),
    private val utils = SceneTestUtils(this, testScope)
    private val authenticationInteractor =
        utils.authenticationInteractor(
            repository = utils.authenticationRepository(),
        )
    private val underTest =
        BouncerViewModel(
            applicationContext = context,
            applicationScope = testScope.backgroundScope,
            interactorFactory =
                object : BouncerInteractor.Factory {
                    override fun create(containerName: String): BouncerInteractor {
                        return BouncerInteractor(
                            applicationScope = testScope.backgroundScope,
                            applicationContext = context,
                            repository = BouncerRepository(),
                            authenticationInteractor = mAuthenticationInteractor,
                            sceneInteractor = sceneInteractor,
                            containerName = CONTAINER_NAME,
        utils.bouncerViewModel(
            utils.bouncerInteractor(
                authenticationInteractor = authenticationInteractor,
                sceneInteractor = utils.sceneInteractor(),
            )
                    }
                },
            containerName = CONTAINER_NAME,
        )

    @Test
@@ -75,7 +54,7 @@ class BouncerViewModelTest : SysuiTestCase() {
            val authMethodViewModel: AuthMethodBouncerViewModel? by
                collectLastValue(underTest.authMethod)
            authMethodsToTest().forEach { authMethod ->
                mAuthenticationInteractor.setAuthenticationMethod(authMethod)
                authenticationInteractor.setAuthenticationMethod(authMethod)

                if (authMethod.isSecure) {
                    assertThat(authMethodViewModel).isNotNull()
@@ -93,13 +72,13 @@ class BouncerViewModelTest : SysuiTestCase() {
                collectLastValue(underTest.authMethod)
            // First pass, populate our "seen" map:
            authMethodsToTest().forEach { authMethod ->
                mAuthenticationInteractor.setAuthenticationMethod(authMethod)
                authenticationInteractor.setAuthenticationMethod(authMethod)
                authMethodViewModel?.let { seen[authMethod] = it }
            }

            // Second pass, assert same instances are reused:
            authMethodsToTest().forEach { authMethod ->
                mAuthenticationInteractor.setAuthenticationMethod(authMethod)
                authenticationInteractor.setAuthenticationMethod(authMethod)
                authMethodViewModel?.let { assertThat(it).isSameInstanceAs(seen[authMethod]) }
            }
        }
@@ -121,8 +100,4 @@ class BouncerViewModelTest : SysuiTestCase() {
            ),
        )
    }

    companion object {
        private const val CONTAINER_NAME = "container1"
    }
}
+25 −45
Original line number Diff line number Diff line
@@ -19,14 +19,9 @@ package com.android.systemui.bouncer.ui.viewmodel
import androidx.test.filters.SmallTest
import com.android.systemui.R
import com.android.systemui.SysuiTestCase
import com.android.systemui.authentication.data.repository.AuthenticationRepositoryImpl
import com.android.systemui.authentication.domain.interactor.AuthenticationInteractor
import com.android.systemui.authentication.shared.model.AuthenticationMethodModel
import com.android.systemui.bouncer.data.repo.BouncerRepository
import com.android.systemui.bouncer.domain.interactor.BouncerInteractor
import com.android.systemui.coroutines.collectLastValue
import com.android.systemui.scene.data.repository.fakeSceneContainerRepository
import com.android.systemui.scene.domain.interactor.SceneInteractor
import com.android.systemui.scene.SceneTestUtils
import com.android.systemui.scene.shared.model.SceneKey
import com.android.systemui.scene.shared.model.SceneModel
import com.google.common.truth.Truth.assertThat
@@ -44,35 +39,20 @@ import org.junit.runners.JUnit4
class PasswordBouncerViewModelTest : SysuiTestCase() {

    private val testScope = TestScope()
    private val sceneInteractor =
        SceneInteractor(
            repository = fakeSceneContainerRepository(),
        )
    private val mAuthenticationInteractor =
        AuthenticationInteractor(
            applicationScope = testScope.backgroundScope,
            repository = AuthenticationRepositoryImpl(),
    private val utils = SceneTestUtils(this, testScope)
    private val authenticationInteractor =
        utils.authenticationInteractor(
            repository = utils.authenticationRepository(),
        )
    private val sceneInteractor = utils.sceneInteractor()
    private val bouncerInteractor =
        BouncerInteractor(
            applicationScope = testScope.backgroundScope,
            applicationContext = context,
            repository = BouncerRepository(),
            authenticationInteractor = mAuthenticationInteractor,
        utils.bouncerInteractor(
            authenticationInteractor = authenticationInteractor,
            sceneInteractor = sceneInteractor,
            containerName = CONTAINER_NAME,
        )
    private val bouncerViewModel =
        BouncerViewModel(
            applicationContext = context,
            applicationScope = testScope.backgroundScope,
            interactorFactory =
                object : BouncerInteractor.Factory {
                    override fun create(containerName: String): BouncerInteractor {
                        return bouncerInteractor
                    }
                },
            containerName = CONTAINER_NAME,
        utils.bouncerViewModel(
            bouncerInteractor = bouncerInteractor,
        )
    private val underTest =
        PasswordBouncerViewModel(
@@ -88,14 +68,14 @@ class PasswordBouncerViewModelTest : SysuiTestCase() {
    @Test
    fun onShown() =
        testScope.runTest {
            val isUnlocked by collectLastValue(mAuthenticationInteractor.isUnlocked)
            val isUnlocked by collectLastValue(authenticationInteractor.isUnlocked)
            val currentScene by collectLastValue(sceneInteractor.currentScene(CONTAINER_NAME))
            val message by collectLastValue(bouncerViewModel.message)
            val password by collectLastValue(underTest.password)
            mAuthenticationInteractor.setAuthenticationMethod(
            authenticationInteractor.setAuthenticationMethod(
                AuthenticationMethodModel.Password("password")
            )
            mAuthenticationInteractor.lockDevice()
            authenticationInteractor.lockDevice()
            sceneInteractor.setCurrentScene(CONTAINER_NAME, SceneModel(SceneKey.Bouncer))
            assertThat(isUnlocked).isFalse()
            assertThat(currentScene).isEqualTo(SceneModel(SceneKey.Bouncer))
@@ -111,14 +91,14 @@ class PasswordBouncerViewModelTest : SysuiTestCase() {
    @Test
    fun onPasswordInputChanged() =
        testScope.runTest {
            val isUnlocked by collectLastValue(mAuthenticationInteractor.isUnlocked)
            val isUnlocked by collectLastValue(authenticationInteractor.isUnlocked)
            val currentScene by collectLastValue(sceneInteractor.currentScene(CONTAINER_NAME))
            val message by collectLastValue(bouncerViewModel.message)
            val password by collectLastValue(underTest.password)
            mAuthenticationInteractor.setAuthenticationMethod(
            authenticationInteractor.setAuthenticationMethod(
                AuthenticationMethodModel.Password("password")
            )
            mAuthenticationInteractor.lockDevice()
            authenticationInteractor.lockDevice()
            sceneInteractor.setCurrentScene(CONTAINER_NAME, SceneModel(SceneKey.Bouncer))
            assertThat(isUnlocked).isFalse()
            assertThat(currentScene).isEqualTo(SceneModel(SceneKey.Bouncer))
@@ -135,12 +115,12 @@ class PasswordBouncerViewModelTest : SysuiTestCase() {
    @Test
    fun onAuthenticateKeyPressed_whenCorrect() =
        testScope.runTest {
            val isUnlocked by collectLastValue(mAuthenticationInteractor.isUnlocked)
            val isUnlocked by collectLastValue(authenticationInteractor.isUnlocked)
            val currentScene by collectLastValue(sceneInteractor.currentScene(CONTAINER_NAME))
            mAuthenticationInteractor.setAuthenticationMethod(
            authenticationInteractor.setAuthenticationMethod(
                AuthenticationMethodModel.Password("password")
            )
            mAuthenticationInteractor.lockDevice()
            authenticationInteractor.lockDevice()
            sceneInteractor.setCurrentScene(CONTAINER_NAME, SceneModel(SceneKey.Bouncer))
            assertThat(isUnlocked).isFalse()
            assertThat(currentScene).isEqualTo(SceneModel(SceneKey.Bouncer))
@@ -156,14 +136,14 @@ class PasswordBouncerViewModelTest : SysuiTestCase() {
    @Test
    fun onAuthenticateKeyPressed_whenWrong() =
        testScope.runTest {
            val isUnlocked by collectLastValue(mAuthenticationInteractor.isUnlocked)
            val isUnlocked by collectLastValue(authenticationInteractor.isUnlocked)
            val currentScene by collectLastValue(sceneInteractor.currentScene(CONTAINER_NAME))
            val message by collectLastValue(bouncerViewModel.message)
            val password by collectLastValue(underTest.password)
            mAuthenticationInteractor.setAuthenticationMethod(
            authenticationInteractor.setAuthenticationMethod(
                AuthenticationMethodModel.Password("password")
            )
            mAuthenticationInteractor.lockDevice()
            authenticationInteractor.lockDevice()
            sceneInteractor.setCurrentScene(CONTAINER_NAME, SceneModel(SceneKey.Bouncer))
            assertThat(isUnlocked).isFalse()
            assertThat(currentScene).isEqualTo(SceneModel(SceneKey.Bouncer))
@@ -181,14 +161,14 @@ class PasswordBouncerViewModelTest : SysuiTestCase() {
    @Test
    fun onAuthenticateKeyPressed_correctAfterWrong() =
        testScope.runTest {
            val isUnlocked by collectLastValue(mAuthenticationInteractor.isUnlocked)
            val isUnlocked by collectLastValue(authenticationInteractor.isUnlocked)
            val currentScene by collectLastValue(sceneInteractor.currentScene(CONTAINER_NAME))
            val message by collectLastValue(bouncerViewModel.message)
            val password by collectLastValue(underTest.password)
            mAuthenticationInteractor.setAuthenticationMethod(
            authenticationInteractor.setAuthenticationMethod(
                AuthenticationMethodModel.Password("password")
            )
            mAuthenticationInteractor.lockDevice()
            authenticationInteractor.lockDevice()
            sceneInteractor.setCurrentScene(CONTAINER_NAME, SceneModel(SceneKey.Bouncer))
            assertThat(isUnlocked).isFalse()
            assertThat(currentScene).isEqualTo(SceneModel(SceneKey.Bouncer))
+25 −45
Original line number Diff line number Diff line
@@ -19,14 +19,9 @@ package com.android.systemui.bouncer.ui.viewmodel
import androidx.test.filters.SmallTest
import com.android.systemui.R
import com.android.systemui.SysuiTestCase
import com.android.systemui.authentication.data.repository.AuthenticationRepositoryImpl
import com.android.systemui.authentication.domain.interactor.AuthenticationInteractor
import com.android.systemui.authentication.shared.model.AuthenticationMethodModel
import com.android.systemui.bouncer.data.repo.BouncerRepository
import com.android.systemui.bouncer.domain.interactor.BouncerInteractor
import com.android.systemui.coroutines.collectLastValue
import com.android.systemui.scene.data.repository.fakeSceneContainerRepository
import com.android.systemui.scene.domain.interactor.SceneInteractor
import com.android.systemui.scene.SceneTestUtils
import com.android.systemui.scene.shared.model.SceneKey
import com.android.systemui.scene.shared.model.SceneModel
import com.google.common.truth.Truth.assertThat
@@ -45,35 +40,20 @@ import org.junit.runners.JUnit4
class PatternBouncerViewModelTest : SysuiTestCase() {

    private val testScope = TestScope()
    private val sceneInteractor =
        SceneInteractor(
            repository = fakeSceneContainerRepository(),
        )
    private val mAuthenticationInteractor =
        AuthenticationInteractor(
            applicationScope = testScope.backgroundScope,
            repository = AuthenticationRepositoryImpl(),
    private val utils = SceneTestUtils(this, testScope)
    private val authenticationInteractor =
        utils.authenticationInteractor(
            repository = utils.authenticationRepository(),
        )
    private val sceneInteractor = utils.sceneInteractor()
    private val bouncerInteractor =
        BouncerInteractor(
            applicationScope = testScope.backgroundScope,
            applicationContext = context,
            repository = BouncerRepository(),
            authenticationInteractor = mAuthenticationInteractor,
        utils.bouncerInteractor(
            authenticationInteractor = authenticationInteractor,
            sceneInteractor = sceneInteractor,
            containerName = CONTAINER_NAME,
        )
    private val bouncerViewModel =
        BouncerViewModel(
            applicationContext = context,
            applicationScope = testScope.backgroundScope,
            interactorFactory =
                object : BouncerInteractor.Factory {
                    override fun create(containerName: String): BouncerInteractor {
                        return bouncerInteractor
                    }
                },
            containerName = CONTAINER_NAME,
        utils.bouncerViewModel(
            bouncerInteractor = bouncerInteractor,
        )
    private val underTest =
        PatternBouncerViewModel(
@@ -91,15 +71,15 @@ class PatternBouncerViewModelTest : SysuiTestCase() {
    @Test
    fun onShown() =
        testScope.runTest {
            val isUnlocked by collectLastValue(mAuthenticationInteractor.isUnlocked)
            val isUnlocked by collectLastValue(authenticationInteractor.isUnlocked)
            val currentScene by collectLastValue(sceneInteractor.currentScene(CONTAINER_NAME))
            val message by collectLastValue(bouncerViewModel.message)
            val selectedDots by collectLastValue(underTest.selectedDots)
            val currentDot by collectLastValue(underTest.currentDot)
            mAuthenticationInteractor.setAuthenticationMethod(
            authenticationInteractor.setAuthenticationMethod(
                AuthenticationMethodModel.Pattern(CORRECT_PATTERN)
            )
            mAuthenticationInteractor.lockDevice()
            authenticationInteractor.lockDevice()
            sceneInteractor.setCurrentScene(CONTAINER_NAME, SceneModel(SceneKey.Bouncer))
            assertThat(isUnlocked).isFalse()
            assertThat(currentScene).isEqualTo(SceneModel(SceneKey.Bouncer))
@@ -116,15 +96,15 @@ class PatternBouncerViewModelTest : SysuiTestCase() {
    @Test
    fun onDragStart() =
        testScope.runTest {
            val isUnlocked by collectLastValue(mAuthenticationInteractor.isUnlocked)
            val isUnlocked by collectLastValue(authenticationInteractor.isUnlocked)
            val currentScene by collectLastValue(sceneInteractor.currentScene(CONTAINER_NAME))
            val message by collectLastValue(bouncerViewModel.message)
            val selectedDots by collectLastValue(underTest.selectedDots)
            val currentDot by collectLastValue(underTest.currentDot)
            mAuthenticationInteractor.setAuthenticationMethod(
            authenticationInteractor.setAuthenticationMethod(
                AuthenticationMethodModel.Pattern(CORRECT_PATTERN)
            )
            mAuthenticationInteractor.lockDevice()
            authenticationInteractor.lockDevice()
            sceneInteractor.setCurrentScene(CONTAINER_NAME, SceneModel(SceneKey.Bouncer))
            assertThat(isUnlocked).isFalse()
            assertThat(currentScene).isEqualTo(SceneModel(SceneKey.Bouncer))
@@ -142,14 +122,14 @@ class PatternBouncerViewModelTest : SysuiTestCase() {
    @Test
    fun onDragEnd_whenCorrect() =
        testScope.runTest {
            val isUnlocked by collectLastValue(mAuthenticationInteractor.isUnlocked)
            val isUnlocked by collectLastValue(authenticationInteractor.isUnlocked)
            val currentScene by collectLastValue(sceneInteractor.currentScene(CONTAINER_NAME))
            val selectedDots by collectLastValue(underTest.selectedDots)
            val currentDot by collectLastValue(underTest.currentDot)
            mAuthenticationInteractor.setAuthenticationMethod(
            authenticationInteractor.setAuthenticationMethod(
                AuthenticationMethodModel.Pattern(CORRECT_PATTERN)
            )
            mAuthenticationInteractor.lockDevice()
            authenticationInteractor.lockDevice()
            sceneInteractor.setCurrentScene(CONTAINER_NAME, SceneModel(SceneKey.Bouncer))
            assertThat(isUnlocked).isFalse()
            assertThat(currentScene).isEqualTo(SceneModel(SceneKey.Bouncer))
@@ -192,15 +172,15 @@ class PatternBouncerViewModelTest : SysuiTestCase() {
    @Test
    fun onDragEnd_whenWrong() =
        testScope.runTest {
            val isUnlocked by collectLastValue(mAuthenticationInteractor.isUnlocked)
            val isUnlocked by collectLastValue(authenticationInteractor.isUnlocked)
            val currentScene by collectLastValue(sceneInteractor.currentScene(CONTAINER_NAME))
            val message by collectLastValue(bouncerViewModel.message)
            val selectedDots by collectLastValue(underTest.selectedDots)
            val currentDot by collectLastValue(underTest.currentDot)
            mAuthenticationInteractor.setAuthenticationMethod(
            authenticationInteractor.setAuthenticationMethod(
                AuthenticationMethodModel.Pattern(CORRECT_PATTERN)
            )
            mAuthenticationInteractor.lockDevice()
            authenticationInteractor.lockDevice()
            sceneInteractor.setCurrentScene(CONTAINER_NAME, SceneModel(SceneKey.Bouncer))
            assertThat(isUnlocked).isFalse()
            assertThat(currentScene).isEqualTo(SceneModel(SceneKey.Bouncer))
@@ -227,15 +207,15 @@ class PatternBouncerViewModelTest : SysuiTestCase() {
    @Test
    fun onDragEnd_correctAfterWrong() =
        testScope.runTest {
            val isUnlocked by collectLastValue(mAuthenticationInteractor.isUnlocked)
            val isUnlocked by collectLastValue(authenticationInteractor.isUnlocked)
            val currentScene by collectLastValue(sceneInteractor.currentScene(CONTAINER_NAME))
            val message by collectLastValue(bouncerViewModel.message)
            val selectedDots by collectLastValue(underTest.selectedDots)
            val currentDot by collectLastValue(underTest.currentDot)
            mAuthenticationInteractor.setAuthenticationMethod(
            authenticationInteractor.setAuthenticationMethod(
                AuthenticationMethodModel.Pattern(CORRECT_PATTERN)
            )
            mAuthenticationInteractor.lockDevice()
            authenticationInteractor.lockDevice()
            sceneInteractor.setCurrentScene(CONTAINER_NAME, SceneModel(SceneKey.Bouncer))
            assertThat(isUnlocked).isFalse()
            assertThat(currentScene).isEqualTo(SceneModel(SceneKey.Bouncer))
Loading