diff --git a/packages/SystemUI/tests/src/com/android/systemui/authentication/domain/interactor/AuthenticationInteractorTest.kt b/packages/SystemUI/tests/src/com/android/systemui/authentication/domain/interactor/AuthenticationInteractorTest.kt index 2e62bebfe3f1a..44c99053eb475 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/authentication/domain/interactor/AuthenticationInteractorTest.kt +++ b/packages/SystemUI/tests/src/com/android/systemui/authentication/domain/interactor/AuthenticationInteractorTest.kt @@ -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, ) diff --git a/packages/SystemUI/tests/src/com/android/systemui/bouncer/domain/interactor/BouncerInteractorTest.kt b/packages/SystemUI/tests/src/com/android/systemui/bouncer/domain/interactor/BouncerInteractorTest.kt index 7dd376ede3617..730f89dd76ba8 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/bouncer/domain/interactor/BouncerInteractorTest.kt +++ b/packages/SystemUI/tests/src/com/android/systemui/bouncer/domain/interactor/BouncerInteractorTest.kt @@ -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 diff --git a/packages/SystemUI/tests/src/com/android/systemui/bouncer/ui/viewmodel/BouncerViewModelTest.kt b/packages/SystemUI/tests/src/com/android/systemui/bouncer/ui/viewmodel/BouncerViewModelTest.kt index 3c8c60368060f..954e67d77181b 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/bouncer/ui/viewmodel/BouncerViewModelTest.kt +++ b/packages/SystemUI/tests/src/com/android/systemui/bouncer/ui/viewmodel/BouncerViewModelTest.kt @@ -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, - ) - } - }, - containerName = CONTAINER_NAME, + utils.bouncerViewModel( + utils.bouncerInteractor( + authenticationInteractor = authenticationInteractor, + sceneInteractor = utils.sceneInteractor(), + ) ) @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" - } } diff --git a/packages/SystemUI/tests/src/com/android/systemui/bouncer/ui/viewmodel/PasswordBouncerViewModelTest.kt b/packages/SystemUI/tests/src/com/android/systemui/bouncer/ui/viewmodel/PasswordBouncerViewModelTest.kt index fe808f2f1d85b..e48b6386c7395 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/bouncer/ui/viewmodel/PasswordBouncerViewModelTest.kt +++ b/packages/SystemUI/tests/src/com/android/systemui/bouncer/ui/viewmodel/PasswordBouncerViewModelTest.kt @@ -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)) diff --git a/packages/SystemUI/tests/src/com/android/systemui/bouncer/ui/viewmodel/PatternBouncerViewModelTest.kt b/packages/SystemUI/tests/src/com/android/systemui/bouncer/ui/viewmodel/PatternBouncerViewModelTest.kt index 19d43fb89c251..6ce29e67982c4 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/bouncer/ui/viewmodel/PatternBouncerViewModelTest.kt +++ b/packages/SystemUI/tests/src/com/android/systemui/bouncer/ui/viewmodel/PatternBouncerViewModelTest.kt @@ -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)) diff --git a/packages/SystemUI/tests/src/com/android/systemui/bouncer/ui/viewmodel/PinBouncerViewModelTest.kt b/packages/SystemUI/tests/src/com/android/systemui/bouncer/ui/viewmodel/PinBouncerViewModelTest.kt index 0b6868ffc9939..bb28520ad8a0e 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/bouncer/ui/viewmodel/PinBouncerViewModelTest.kt +++ b/packages/SystemUI/tests/src/com/android/systemui/bouncer/ui/viewmodel/PinBouncerViewModelTest.kt @@ -19,14 +19,10 @@ 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,23 +41,16 @@ import org.junit.runners.JUnit4 class PinBouncerViewModelTest : 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 sceneInteractor = utils.sceneInteractor() + private val authenticationInteractor = + utils.authenticationInteractor( + repository = utils.authenticationRepository(), ) 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( @@ -90,12 +79,12 @@ class PinBouncerViewModelTest : 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 pinLengths by collectLastValue(underTest.pinLengths) - mAuthenticationInteractor.setAuthenticationMethod(AuthenticationMethodModel.PIN(1234)) - mAuthenticationInteractor.lockDevice() + authenticationInteractor.setAuthenticationMethod(AuthenticationMethodModel.PIN(1234)) + authenticationInteractor.lockDevice() sceneInteractor.setCurrentScene(CONTAINER_NAME, SceneModel(SceneKey.Bouncer)) assertThat(isUnlocked).isFalse() assertThat(currentScene).isEqualTo(SceneModel(SceneKey.Bouncer)) @@ -111,12 +100,12 @@ class PinBouncerViewModelTest : SysuiTestCase() { @Test fun onPinButtonClicked() = 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 pinLengths by collectLastValue(underTest.pinLengths) - mAuthenticationInteractor.setAuthenticationMethod(AuthenticationMethodModel.PIN(1234)) - mAuthenticationInteractor.lockDevice() + authenticationInteractor.setAuthenticationMethod(AuthenticationMethodModel.PIN(1234)) + authenticationInteractor.lockDevice() sceneInteractor.setCurrentScene(CONTAINER_NAME, SceneModel(SceneKey.Bouncer)) assertThat(isUnlocked).isFalse() assertThat(currentScene).isEqualTo(SceneModel(SceneKey.Bouncer)) @@ -133,12 +122,12 @@ class PinBouncerViewModelTest : SysuiTestCase() { @Test fun onBackspaceButtonClicked() = 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 pinLengths by collectLastValue(underTest.pinLengths) - mAuthenticationInteractor.setAuthenticationMethod(AuthenticationMethodModel.PIN(1234)) - mAuthenticationInteractor.lockDevice() + authenticationInteractor.setAuthenticationMethod(AuthenticationMethodModel.PIN(1234)) + authenticationInteractor.lockDevice() sceneInteractor.setCurrentScene(CONTAINER_NAME, SceneModel(SceneKey.Bouncer)) assertThat(isUnlocked).isFalse() assertThat(currentScene).isEqualTo(SceneModel(SceneKey.Bouncer)) @@ -157,12 +146,12 @@ class PinBouncerViewModelTest : SysuiTestCase() { @Test fun onBackspaceButtonLongPressed() = 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 pinLengths by collectLastValue(underTest.pinLengths) - mAuthenticationInteractor.setAuthenticationMethod(AuthenticationMethodModel.PIN(1234)) - mAuthenticationInteractor.lockDevice() + authenticationInteractor.setAuthenticationMethod(AuthenticationMethodModel.PIN(1234)) + authenticationInteractor.lockDevice() sceneInteractor.setCurrentScene(CONTAINER_NAME, SceneModel(SceneKey.Bouncer)) assertThat(isUnlocked).isFalse() assertThat(currentScene).isEqualTo(SceneModel(SceneKey.Bouncer)) @@ -187,10 +176,10 @@ class PinBouncerViewModelTest : SysuiTestCase() { @Test fun onAuthenticateButtonClicked_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(AuthenticationMethodModel.PIN(1234)) - mAuthenticationInteractor.lockDevice() + authenticationInteractor.setAuthenticationMethod(AuthenticationMethodModel.PIN(1234)) + authenticationInteractor.lockDevice() sceneInteractor.setCurrentScene(CONTAINER_NAME, SceneModel(SceneKey.Bouncer)) assertThat(isUnlocked).isFalse() assertThat(currentScene).isEqualTo(SceneModel(SceneKey.Bouncer)) @@ -209,12 +198,12 @@ class PinBouncerViewModelTest : SysuiTestCase() { @Test fun onAuthenticateButtonClicked_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 pinLengths by collectLastValue(underTest.pinLengths) - mAuthenticationInteractor.setAuthenticationMethod(AuthenticationMethodModel.PIN(1234)) - mAuthenticationInteractor.lockDevice() + authenticationInteractor.setAuthenticationMethod(AuthenticationMethodModel.PIN(1234)) + authenticationInteractor.lockDevice() sceneInteractor.setCurrentScene(CONTAINER_NAME, SceneModel(SceneKey.Bouncer)) assertThat(isUnlocked).isFalse() assertThat(currentScene).isEqualTo(SceneModel(SceneKey.Bouncer)) @@ -236,12 +225,12 @@ class PinBouncerViewModelTest : SysuiTestCase() { @Test fun onAuthenticateButtonClicked_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 pinLengths by collectLastValue(underTest.pinLengths) - mAuthenticationInteractor.setAuthenticationMethod(AuthenticationMethodModel.PIN(1234)) - mAuthenticationInteractor.lockDevice() + authenticationInteractor.setAuthenticationMethod(AuthenticationMethodModel.PIN(1234)) + authenticationInteractor.lockDevice() sceneInteractor.setCurrentScene(CONTAINER_NAME, SceneModel(SceneKey.Bouncer)) assertThat(isUnlocked).isFalse() assertThat(currentScene).isEqualTo(SceneModel(SceneKey.Bouncer)) diff --git a/packages/SystemUI/tests/src/com/android/systemui/keyguard/domain/interactor/LockScreenSceneInteractorTest.kt b/packages/SystemUI/tests/src/com/android/systemui/keyguard/domain/interactor/LockScreenSceneInteractorTest.kt index 749e7a0481eb5..c2c528a9babfc 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/keyguard/domain/interactor/LockScreenSceneInteractorTest.kt +++ b/packages/SystemUI/tests/src/com/android/systemui/keyguard/domain/interactor/LockScreenSceneInteractorTest.kt @@ -18,14 +18,10 @@ package com.android.systemui.keyguard.domain.interactor 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.android.systemui.scene.SceneTestUtils.Companion.CONTAINER_1 import com.android.systemui.scene.shared.model.SceneKey import com.android.systemui.scene.shared.model.SceneModel import com.google.common.truth.Truth.assertThat @@ -43,34 +39,21 @@ import org.junit.runners.JUnit4 class LockScreenSceneInteractorTest : 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 sceneInteractor = utils.sceneInteractor() + private val authenticationInteractor = + utils.authenticationInteractor( + repository = utils.authenticationRepository(), ) private val underTest = - LockScreenSceneInteractor( - applicationScope = testScope.backgroundScope, - authenticationInteractor = mAuthenticationInteractor, - bouncerInteractorFactory = - object : BouncerInteractor.Factory { - override fun create(containerName: String): BouncerInteractor { - return BouncerInteractor( - applicationScope = testScope.backgroundScope, - applicationContext = context, - repository = BouncerRepository(), - authenticationInteractor = mAuthenticationInteractor, - sceneInteractor = sceneInteractor, - containerName = containerName, - ) - } - }, + utils.lockScreenSceneInteractor( + authenticationInteractor = authenticationInteractor, sceneInteractor = sceneInteractor, - containerName = CONTAINER_NAME, + bouncerInteractor = + utils.bouncerInteractor( + authenticationInteractor = authenticationInteractor, + sceneInteractor = sceneInteractor, + ), ) @Test @@ -78,10 +61,10 @@ class LockScreenSceneInteractorTest : SysuiTestCase() { testScope.runTest { val isDeviceLocked by collectLastValue(underTest.isDeviceLocked) - mAuthenticationInteractor.lockDevice() + authenticationInteractor.lockDevice() assertThat(isDeviceLocked).isTrue() - mAuthenticationInteractor.unlockDevice() + authenticationInteractor.unlockDevice() assertThat(isDeviceLocked).isFalse() } @@ -90,8 +73,8 @@ class LockScreenSceneInteractorTest : SysuiTestCase() { testScope.runTest { val isSwipeToDismissEnabled by collectLastValue(underTest.isSwipeToDismissEnabled) - mAuthenticationInteractor.lockDevice() - mAuthenticationInteractor.setAuthenticationMethod(AuthenticationMethodModel.Swipe) + authenticationInteractor.lockDevice() + authenticationInteractor.setAuthenticationMethod(AuthenticationMethodModel.Swipe) assertThat(isSwipeToDismissEnabled).isTrue() } @@ -101,8 +84,8 @@ class LockScreenSceneInteractorTest : SysuiTestCase() { testScope.runTest { val isSwipeToDismissEnabled by collectLastValue(underTest.isSwipeToDismissEnabled) - mAuthenticationInteractor.unlockDevice() - mAuthenticationInteractor.setAuthenticationMethod(AuthenticationMethodModel.Swipe) + authenticationInteractor.unlockDevice() + authenticationInteractor.setAuthenticationMethod(AuthenticationMethodModel.Swipe) assertThat(isSwipeToDismissEnabled).isFalse() } @@ -110,9 +93,9 @@ class LockScreenSceneInteractorTest : SysuiTestCase() { @Test fun dismissLockScreen_deviceLockedWithSecureAuthMethod_switchesToBouncer() = testScope.runTest { - val currentScene by collectLastValue(sceneInteractor.currentScene(CONTAINER_NAME)) - mAuthenticationInteractor.lockDevice() - mAuthenticationInteractor.setAuthenticationMethod(AuthenticationMethodModel.PIN(1234)) + val currentScene by collectLastValue(sceneInteractor.currentScene(CONTAINER_1)) + authenticationInteractor.lockDevice() + authenticationInteractor.setAuthenticationMethod(AuthenticationMethodModel.PIN(1234)) assertThat(currentScene).isEqualTo(SceneModel(SceneKey.LockScreen)) underTest.dismissLockScreen() @@ -123,9 +106,9 @@ class LockScreenSceneInteractorTest : SysuiTestCase() { @Test fun dismissLockScreen_deviceUnlocked_switchesToGone() = testScope.runTest { - val currentScene by collectLastValue(sceneInteractor.currentScene(CONTAINER_NAME)) - mAuthenticationInteractor.unlockDevice() - mAuthenticationInteractor.setAuthenticationMethod(AuthenticationMethodModel.PIN(1234)) + val currentScene by collectLastValue(sceneInteractor.currentScene(CONTAINER_1)) + authenticationInteractor.unlockDevice() + authenticationInteractor.setAuthenticationMethod(AuthenticationMethodModel.PIN(1234)) assertThat(currentScene).isEqualTo(SceneModel(SceneKey.LockScreen)) underTest.dismissLockScreen() @@ -136,9 +119,9 @@ class LockScreenSceneInteractorTest : SysuiTestCase() { @Test fun dismissLockScreen_deviceLockedWithInsecureAuthMethod_switchesToGone() = testScope.runTest { - val currentScene by collectLastValue(sceneInteractor.currentScene(CONTAINER_NAME)) - mAuthenticationInteractor.lockDevice() - mAuthenticationInteractor.setAuthenticationMethod(AuthenticationMethodModel.Swipe) + val currentScene by collectLastValue(sceneInteractor.currentScene(CONTAINER_1)) + authenticationInteractor.lockDevice() + authenticationInteractor.setAuthenticationMethod(AuthenticationMethodModel.Swipe) assertThat(currentScene).isEqualTo(SceneModel(SceneKey.LockScreen)) underTest.dismissLockScreen() @@ -149,15 +132,15 @@ class LockScreenSceneInteractorTest : SysuiTestCase() { @Test fun deviceLockedInNonLockScreenScene_switchesToLockScreenScene() = testScope.runTest { - val currentScene by collectLastValue(sceneInteractor.currentScene(CONTAINER_NAME)) + val currentScene by collectLastValue(sceneInteractor.currentScene(CONTAINER_1)) runCurrent() - sceneInteractor.setCurrentScene(CONTAINER_NAME, SceneModel(SceneKey.Gone)) + sceneInteractor.setCurrentScene(CONTAINER_1, SceneModel(SceneKey.Gone)) runCurrent() - mAuthenticationInteractor.unlockDevice() + authenticationInteractor.unlockDevice() runCurrent() assertThat(currentScene).isEqualTo(SceneModel(SceneKey.Gone)) - mAuthenticationInteractor.lockDevice() + authenticationInteractor.lockDevice() assertThat(currentScene).isEqualTo(SceneModel(SceneKey.LockScreen)) } @@ -165,15 +148,15 @@ class LockScreenSceneInteractorTest : SysuiTestCase() { @Test fun deviceBiometricUnlockedInLockScreen_bypassEnabled_switchesToGone() = testScope.runTest { - val currentScene by collectLastValue(sceneInteractor.currentScene(CONTAINER_NAME)) - mAuthenticationInteractor.lockDevice() - sceneInteractor.setCurrentScene(CONTAINER_NAME, SceneModel(SceneKey.LockScreen)) - if (!mAuthenticationInteractor.isBypassEnabled.value) { - mAuthenticationInteractor.toggleBypassEnabled() + val currentScene by collectLastValue(sceneInteractor.currentScene(CONTAINER_1)) + authenticationInteractor.lockDevice() + sceneInteractor.setCurrentScene(CONTAINER_1, SceneModel(SceneKey.LockScreen)) + if (!authenticationInteractor.isBypassEnabled.value) { + authenticationInteractor.toggleBypassEnabled() } assertThat(currentScene).isEqualTo(SceneModel(SceneKey.LockScreen)) - mAuthenticationInteractor.biometricUnlock() + authenticationInteractor.biometricUnlock() assertThat(currentScene).isEqualTo(SceneModel(SceneKey.Gone)) } @@ -181,15 +164,15 @@ class LockScreenSceneInteractorTest : SysuiTestCase() { @Test fun deviceBiometricUnlockedInLockScreen_bypassNotEnabled_doesNotSwitch() = testScope.runTest { - val currentScene by collectLastValue(sceneInteractor.currentScene(CONTAINER_NAME)) - mAuthenticationInteractor.lockDevice() - sceneInteractor.setCurrentScene(CONTAINER_NAME, SceneModel(SceneKey.LockScreen)) - if (mAuthenticationInteractor.isBypassEnabled.value) { - mAuthenticationInteractor.toggleBypassEnabled() + val currentScene by collectLastValue(sceneInteractor.currentScene(CONTAINER_1)) + authenticationInteractor.lockDevice() + sceneInteractor.setCurrentScene(CONTAINER_1, SceneModel(SceneKey.LockScreen)) + if (authenticationInteractor.isBypassEnabled.value) { + authenticationInteractor.toggleBypassEnabled() } assertThat(currentScene).isEqualTo(SceneModel(SceneKey.LockScreen)) - mAuthenticationInteractor.biometricUnlock() + authenticationInteractor.biometricUnlock() assertThat(currentScene).isEqualTo(SceneModel(SceneKey.LockScreen)) } @@ -197,12 +180,12 @@ class LockScreenSceneInteractorTest : SysuiTestCase() { @Test fun switchFromLockScreenToGone_authMethodSwipe_unlocksDevice() = testScope.runTest { - val isUnlocked by collectLastValue(mAuthenticationInteractor.isUnlocked) - sceneInteractor.setCurrentScene(CONTAINER_NAME, SceneModel(SceneKey.LockScreen)) - mAuthenticationInteractor.setAuthenticationMethod(AuthenticationMethodModel.Swipe) + val isUnlocked by collectLastValue(authenticationInteractor.isUnlocked) + sceneInteractor.setCurrentScene(CONTAINER_1, SceneModel(SceneKey.LockScreen)) + authenticationInteractor.setAuthenticationMethod(AuthenticationMethodModel.Swipe) assertThat(isUnlocked).isFalse() - sceneInteractor.setCurrentScene(CONTAINER_NAME, SceneModel(SceneKey.Gone)) + sceneInteractor.setCurrentScene(CONTAINER_1, SceneModel(SceneKey.Gone)) assertThat(isUnlocked).isTrue() } @@ -210,12 +193,12 @@ class LockScreenSceneInteractorTest : SysuiTestCase() { @Test fun switchFromLockScreenToGone_authMethodNotSwipe_doesNotUnlockDevice() = testScope.runTest { - val isUnlocked by collectLastValue(mAuthenticationInteractor.isUnlocked) - sceneInteractor.setCurrentScene(CONTAINER_NAME, SceneModel(SceneKey.LockScreen)) - mAuthenticationInteractor.setAuthenticationMethod(AuthenticationMethodModel.PIN(1234)) + val isUnlocked by collectLastValue(authenticationInteractor.isUnlocked) + sceneInteractor.setCurrentScene(CONTAINER_1, SceneModel(SceneKey.LockScreen)) + authenticationInteractor.setAuthenticationMethod(AuthenticationMethodModel.PIN(1234)) assertThat(isUnlocked).isFalse() - sceneInteractor.setCurrentScene(CONTAINER_NAME, SceneModel(SceneKey.Gone)) + sceneInteractor.setCurrentScene(CONTAINER_1, SceneModel(SceneKey.Gone)) assertThat(isUnlocked).isFalse() } @@ -223,15 +206,15 @@ class LockScreenSceneInteractorTest : SysuiTestCase() { @Test fun switchFromNonLockScreenToGone_authMethodSwipe_doesNotUnlockDevice() = testScope.runTest { - val isUnlocked by collectLastValue(mAuthenticationInteractor.isUnlocked) + val isUnlocked by collectLastValue(authenticationInteractor.isUnlocked) runCurrent() - sceneInteractor.setCurrentScene(CONTAINER_NAME, SceneModel(SceneKey.Shade)) + sceneInteractor.setCurrentScene(CONTAINER_1, SceneModel(SceneKey.Shade)) runCurrent() - mAuthenticationInteractor.setAuthenticationMethod(AuthenticationMethodModel.Swipe) + authenticationInteractor.setAuthenticationMethod(AuthenticationMethodModel.Swipe) runCurrent() assertThat(isUnlocked).isFalse() - sceneInteractor.setCurrentScene(CONTAINER_NAME, SceneModel(SceneKey.Gone)) + sceneInteractor.setCurrentScene(CONTAINER_1, SceneModel(SceneKey.Gone)) assertThat(isUnlocked).isFalse() } @@ -239,12 +222,12 @@ class LockScreenSceneInteractorTest : SysuiTestCase() { @Test fun authMethodChangedToNone_onLockScreenScene_dismissesLockScreen() = testScope.runTest { - val currentScene by collectLastValue(sceneInteractor.currentScene(CONTAINER_NAME)) - sceneInteractor.setCurrentScene(CONTAINER_NAME, SceneModel(SceneKey.LockScreen)) - mAuthenticationInteractor.setAuthenticationMethod(AuthenticationMethodModel.Swipe) + val currentScene by collectLastValue(sceneInteractor.currentScene(CONTAINER_1)) + sceneInteractor.setCurrentScene(CONTAINER_1, SceneModel(SceneKey.LockScreen)) + authenticationInteractor.setAuthenticationMethod(AuthenticationMethodModel.Swipe) assertThat(currentScene).isEqualTo(SceneModel(SceneKey.LockScreen)) - mAuthenticationInteractor.setAuthenticationMethod(AuthenticationMethodModel.None) + authenticationInteractor.setAuthenticationMethod(AuthenticationMethodModel.None) assertThat(currentScene).isEqualTo(SceneModel(SceneKey.Gone)) } @@ -252,19 +235,15 @@ class LockScreenSceneInteractorTest : SysuiTestCase() { @Test fun authMethodChangedToNone_notOnLockScreenScene_doesNotDismissLockScreen() = testScope.runTest { - val currentScene by collectLastValue(sceneInteractor.currentScene(CONTAINER_NAME)) - mAuthenticationInteractor.setAuthenticationMethod(AuthenticationMethodModel.Swipe) + val currentScene by collectLastValue(sceneInteractor.currentScene(CONTAINER_1)) + authenticationInteractor.setAuthenticationMethod(AuthenticationMethodModel.Swipe) runCurrent() - sceneInteractor.setCurrentScene(CONTAINER_NAME, SceneModel(SceneKey.QuickSettings)) + sceneInteractor.setCurrentScene(CONTAINER_1, SceneModel(SceneKey.QuickSettings)) runCurrent() assertThat(currentScene).isEqualTo(SceneModel(SceneKey.QuickSettings)) - mAuthenticationInteractor.setAuthenticationMethod(AuthenticationMethodModel.None) + authenticationInteractor.setAuthenticationMethod(AuthenticationMethodModel.None) assertThat(currentScene).isEqualTo(SceneModel(SceneKey.QuickSettings)) } - - companion object { - private const val CONTAINER_NAME = "container1" - } } diff --git a/packages/SystemUI/tests/src/com/android/systemui/keyguard/ui/viewmodel/LockScreenSceneViewModelTest.kt b/packages/SystemUI/tests/src/com/android/systemui/keyguard/ui/viewmodel/LockScreenSceneViewModelTest.kt index d335b09b196a1..9e8be3e7608f5 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/keyguard/ui/viewmodel/LockScreenSceneViewModelTest.kt +++ b/packages/SystemUI/tests/src/com/android/systemui/keyguard/ui/viewmodel/LockScreenSceneViewModelTest.kt @@ -19,16 +19,12 @@ package com.android.systemui.keyguard.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.common.shared.model.Icon import com.android.systemui.coroutines.collectLastValue import com.android.systemui.keyguard.domain.interactor.LockScreenSceneInteractor -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.SceneTestUtils.Companion.CONTAINER_1 import com.android.systemui.scene.shared.model.SceneKey import com.android.systemui.scene.shared.model.SceneModel import com.google.common.truth.Truth.assertThat @@ -46,14 +42,11 @@ import org.junit.runners.JUnit4 class LockScreenSceneViewModelTest : 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 sceneInteractor = utils.sceneInteractor() + private val authenticationInteractor = + utils.authenticationInteractor( + repository = utils.authenticationRepository(), ) private val underTest = @@ -62,38 +55,28 @@ class LockScreenSceneViewModelTest : SysuiTestCase() { interactorFactory = object : LockScreenSceneInteractor.Factory { override fun create(containerName: String): LockScreenSceneInteractor { - return LockScreenSceneInteractor( - applicationScope = testScope.backgroundScope, - authenticationInteractor = mAuthenticationInteractor, - bouncerInteractorFactory = - object : BouncerInteractor.Factory { - override fun create(containerName: String): BouncerInteractor { - return BouncerInteractor( - applicationScope = testScope.backgroundScope, - applicationContext = context, - repository = BouncerRepository(), - authenticationInteractor = mAuthenticationInteractor, - sceneInteractor = sceneInteractor, - containerName = containerName, - ) - } - }, + return utils.lockScreenSceneInteractor( + authenticationInteractor = authenticationInteractor, sceneInteractor = sceneInteractor, - containerName = CONTAINER_NAME, + bouncerInteractor = + utils.bouncerInteractor( + authenticationInteractor = authenticationInteractor, + sceneInteractor = sceneInteractor, + ), ) } }, - containerName = CONTAINER_NAME + containerName = CONTAINER_1 ) @Test fun lockButtonIcon_whenLocked() = testScope.runTest { val lockButtonIcon by collectLastValue(underTest.lockButtonIcon) - mAuthenticationInteractor.setAuthenticationMethod( + authenticationInteractor.setAuthenticationMethod( AuthenticationMethodModel.Password("password") ) - mAuthenticationInteractor.lockDevice() + authenticationInteractor.lockDevice() assertThat((lockButtonIcon as? Icon.Resource)?.res) .isEqualTo(R.drawable.ic_device_lock_on) @@ -103,10 +86,10 @@ class LockScreenSceneViewModelTest : SysuiTestCase() { fun lockButtonIcon_whenUnlocked() = testScope.runTest { val lockButtonIcon by collectLastValue(underTest.lockButtonIcon) - mAuthenticationInteractor.setAuthenticationMethod( + authenticationInteractor.setAuthenticationMethod( AuthenticationMethodModel.Password("password") ) - mAuthenticationInteractor.unlockDevice() + authenticationInteractor.unlockDevice() assertThat((lockButtonIcon as? Icon.Resource)?.res) .isEqualTo(R.drawable.ic_device_lock_off) @@ -116,8 +99,8 @@ class LockScreenSceneViewModelTest : SysuiTestCase() { fun upTransitionSceneKey_swipeToUnlockedEnabled_gone() = testScope.runTest { val upTransitionSceneKey by collectLastValue(underTest.upDestinationSceneKey) - mAuthenticationInteractor.setAuthenticationMethod(AuthenticationMethodModel.Swipe) - mAuthenticationInteractor.lockDevice() + authenticationInteractor.setAuthenticationMethod(AuthenticationMethodModel.Swipe) + authenticationInteractor.lockDevice() assertThat(upTransitionSceneKey).isEqualTo(SceneKey.Gone) } @@ -126,8 +109,8 @@ class LockScreenSceneViewModelTest : SysuiTestCase() { fun upTransitionSceneKey_swipeToUnlockedNotEnabled_bouncer() = testScope.runTest { val upTransitionSceneKey by collectLastValue(underTest.upDestinationSceneKey) - mAuthenticationInteractor.setAuthenticationMethod(AuthenticationMethodModel.PIN(1234)) - mAuthenticationInteractor.lockDevice() + authenticationInteractor.setAuthenticationMethod(AuthenticationMethodModel.PIN(1234)) + authenticationInteractor.lockDevice() assertThat(upTransitionSceneKey).isEqualTo(SceneKey.Bouncer) } @@ -135,9 +118,9 @@ class LockScreenSceneViewModelTest : SysuiTestCase() { @Test fun onLockButtonClicked_deviceLockedSecurely_switchesToBouncer() = testScope.runTest { - val currentScene by collectLastValue(sceneInteractor.currentScene(CONTAINER_NAME)) - mAuthenticationInteractor.setAuthenticationMethod(AuthenticationMethodModel.PIN(1234)) - mAuthenticationInteractor.lockDevice() + val currentScene by collectLastValue(sceneInteractor.currentScene(CONTAINER_1)) + authenticationInteractor.setAuthenticationMethod(AuthenticationMethodModel.PIN(1234)) + authenticationInteractor.lockDevice() runCurrent() underTest.onLockButtonClicked() @@ -148,9 +131,9 @@ class LockScreenSceneViewModelTest : SysuiTestCase() { @Test fun onContentClicked_deviceUnlocked_switchesToGone() = testScope.runTest { - val currentScene by collectLastValue(sceneInteractor.currentScene(CONTAINER_NAME)) - mAuthenticationInteractor.setAuthenticationMethod(AuthenticationMethodModel.PIN(1234)) - mAuthenticationInteractor.unlockDevice() + val currentScene by collectLastValue(sceneInteractor.currentScene(CONTAINER_1)) + authenticationInteractor.setAuthenticationMethod(AuthenticationMethodModel.PIN(1234)) + authenticationInteractor.unlockDevice() runCurrent() underTest.onContentClicked() @@ -161,9 +144,9 @@ class LockScreenSceneViewModelTest : SysuiTestCase() { @Test fun onContentClicked_deviceLockedSecurely_switchesToBouncer() = testScope.runTest { - val currentScene by collectLastValue(sceneInteractor.currentScene(CONTAINER_NAME)) - mAuthenticationInteractor.setAuthenticationMethod(AuthenticationMethodModel.PIN(1234)) - mAuthenticationInteractor.lockDevice() + val currentScene by collectLastValue(sceneInteractor.currentScene(CONTAINER_1)) + authenticationInteractor.setAuthenticationMethod(AuthenticationMethodModel.PIN(1234)) + authenticationInteractor.lockDevice() runCurrent() underTest.onContentClicked() @@ -174,17 +157,13 @@ class LockScreenSceneViewModelTest : SysuiTestCase() { @Test fun onLockButtonClicked_deviceUnlocked_switchesToGone() = testScope.runTest { - val currentScene by collectLastValue(sceneInteractor.currentScene(CONTAINER_NAME)) - mAuthenticationInteractor.setAuthenticationMethod(AuthenticationMethodModel.PIN(1234)) - mAuthenticationInteractor.unlockDevice() + val currentScene by collectLastValue(sceneInteractor.currentScene(CONTAINER_1)) + authenticationInteractor.setAuthenticationMethod(AuthenticationMethodModel.PIN(1234)) + authenticationInteractor.unlockDevice() runCurrent() underTest.onLockButtonClicked() assertThat(currentScene).isEqualTo(SceneModel(SceneKey.Gone)) } - - companion object { - private const val CONTAINER_NAME = "container1" - } } diff --git a/packages/SystemUI/tests/src/com/android/systemui/qs/ui/viewmodel/QuickSettingsSceneViewModelTest.kt b/packages/SystemUI/tests/src/com/android/systemui/qs/ui/viewmodel/QuickSettingsSceneViewModelTest.kt index e8875bee5276d..3f838e6516f16 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/qs/ui/viewmodel/QuickSettingsSceneViewModelTest.kt +++ b/packages/SystemUI/tests/src/com/android/systemui/qs/ui/viewmodel/QuickSettingsSceneViewModelTest.kt @@ -18,15 +18,11 @@ package com.android.systemui.qs.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.keyguard.domain.interactor.LockScreenSceneInteractor -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.SceneTestUtils.Companion.CONTAINER_1 import com.android.systemui.scene.shared.model.SceneKey import com.android.systemui.scene.shared.model.SceneModel import com.google.common.truth.Truth.assertThat @@ -44,14 +40,11 @@ import org.junit.runners.JUnit4 class QuickSettingsSceneViewModelTest : 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 sceneInteractor = utils.sceneInteractor() + private val authenticationInteractor = + utils.authenticationInteractor( + repository = utils.authenticationRepository(), ) private val underTest = @@ -59,36 +52,26 @@ class QuickSettingsSceneViewModelTest : SysuiTestCase() { lockScreenSceneInteractorFactory = object : LockScreenSceneInteractor.Factory { override fun create(containerName: String): LockScreenSceneInteractor { - return LockScreenSceneInteractor( - applicationScope = testScope.backgroundScope, - authenticationInteractor = mAuthenticationInteractor, - bouncerInteractorFactory = - object : BouncerInteractor.Factory { - override fun create(containerName: String): BouncerInteractor { - return BouncerInteractor( - applicationScope = testScope.backgroundScope, - applicationContext = context, - repository = BouncerRepository(), - authenticationInteractor = mAuthenticationInteractor, - sceneInteractor = sceneInteractor, - containerName = containerName, - ) - } - }, + return utils.lockScreenSceneInteractor( + authenticationInteractor = authenticationInteractor, sceneInteractor = sceneInteractor, - containerName = CONTAINER_NAME, + bouncerInteractor = + utils.bouncerInteractor( + authenticationInteractor = authenticationInteractor, + sceneInteractor = sceneInteractor, + ), ) } }, - containerName = CONTAINER_NAME + containerName = CONTAINER_1 ) @Test fun onContentClicked_deviceUnlocked_switchesToGone() = testScope.runTest { - val currentScene by collectLastValue(sceneInteractor.currentScene(CONTAINER_NAME)) - mAuthenticationInteractor.setAuthenticationMethod(AuthenticationMethodModel.PIN(1234)) - mAuthenticationInteractor.unlockDevice() + val currentScene by collectLastValue(sceneInteractor.currentScene(CONTAINER_1)) + authenticationInteractor.setAuthenticationMethod(AuthenticationMethodModel.PIN(1234)) + authenticationInteractor.unlockDevice() runCurrent() underTest.onContentClicked() @@ -99,17 +82,13 @@ class QuickSettingsSceneViewModelTest : SysuiTestCase() { @Test fun onContentClicked_deviceLockedSecurely_switchesToBouncer() = testScope.runTest { - val currentScene by collectLastValue(sceneInteractor.currentScene(CONTAINER_NAME)) - mAuthenticationInteractor.setAuthenticationMethod(AuthenticationMethodModel.PIN(1234)) - mAuthenticationInteractor.lockDevice() + val currentScene by collectLastValue(sceneInteractor.currentScene(CONTAINER_1)) + authenticationInteractor.setAuthenticationMethod(AuthenticationMethodModel.PIN(1234)) + authenticationInteractor.lockDevice() runCurrent() underTest.onContentClicked() assertThat(currentScene).isEqualTo(SceneModel(SceneKey.Bouncer)) } - - companion object { - private const val CONTAINER_NAME = "container1" - } } diff --git a/packages/SystemUI/tests/src/com/android/systemui/scene/data/repository/Fakes.kt b/packages/SystemUI/tests/src/com/android/systemui/scene/data/repository/Fakes.kt deleted file mode 100644 index 1cdaec0c6581c..0000000000000 --- a/packages/SystemUI/tests/src/com/android/systemui/scene/data/repository/Fakes.kt +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright (C) 2023 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.systemui.scene.data.repository - -import com.android.systemui.scene.data.model.SceneContainerConfig -import com.android.systemui.scene.shared.model.SceneKey - -fun fakeSceneContainerRepository( - containerConfigurations: Set = - setOf( - fakeSceneContainerConfig("container1"), - fakeSceneContainerConfig("container2"), - ) -): SceneContainerRepository { - return SceneContainerRepository(containerConfigurations) -} - -fun fakeSceneKeys(): List { - return listOf( - SceneKey.QuickSettings, - SceneKey.Shade, - SceneKey.LockScreen, - SceneKey.Bouncer, - SceneKey.Gone, - ) -} - -fun fakeSceneContainerConfig( - name: String, - sceneKeys: List = fakeSceneKeys(), -): SceneContainerConfig { - return SceneContainerConfig( - name = name, - sceneKeys = sceneKeys, - initialSceneKey = SceneKey.LockScreen, - ) -} diff --git a/packages/SystemUI/tests/src/com/android/systemui/scene/data/repository/SceneContainerRepositoryTest.kt b/packages/SystemUI/tests/src/com/android/systemui/scene/data/repository/SceneContainerRepositoryTest.kt index 9e264db845e1c..6c7017bac68e7 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/scene/data/repository/SceneContainerRepositoryTest.kt +++ b/packages/SystemUI/tests/src/com/android/systemui/scene/data/repository/SceneContainerRepositoryTest.kt @@ -21,6 +21,7 @@ package com.android.systemui.scene.data.repository import androidx.test.filters.SmallTest import com.android.systemui.SysuiTestCase import com.android.systemui.coroutines.collectLastValue +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 @@ -34,9 +35,11 @@ import org.junit.runners.JUnit4 @RunWith(JUnit4::class) class SceneContainerRepositoryTest : SysuiTestCase() { + private val utils = SceneTestUtils(this) + @Test fun allSceneKeys() { - val underTest = fakeSceneContainerRepository() + val underTest = utils.fakeSceneContainerRepository() assertThat(underTest.allSceneKeys("container1")) .isEqualTo( listOf( @@ -51,13 +54,13 @@ class SceneContainerRepositoryTest : SysuiTestCase() { @Test(expected = IllegalStateException::class) fun allSceneKeys_noSuchContainer_throws() { - val underTest = fakeSceneContainerRepository() + val underTest = utils.fakeSceneContainerRepository() underTest.allSceneKeys("nonExistingContainer") } @Test fun currentScene() = runTest { - val underTest = fakeSceneContainerRepository() + val underTest = utils.fakeSceneContainerRepository() val currentScene by collectLastValue(underTest.currentScene("container1")) assertThat(currentScene).isEqualTo(SceneModel(SceneKey.LockScreen)) @@ -67,23 +70,23 @@ class SceneContainerRepositoryTest : SysuiTestCase() { @Test(expected = IllegalStateException::class) fun currentScene_noSuchContainer_throws() { - val underTest = fakeSceneContainerRepository() + val underTest = utils.fakeSceneContainerRepository() underTest.currentScene("nonExistingContainer") } @Test(expected = IllegalStateException::class) fun setCurrentScene_noSuchContainer_throws() { - val underTest = fakeSceneContainerRepository() + val underTest = utils.fakeSceneContainerRepository() underTest.setCurrentScene("nonExistingContainer", SceneModel(SceneKey.Shade)) } @Test(expected = IllegalStateException::class) fun setCurrentScene_noSuchSceneInContainer_throws() { val underTest = - fakeSceneContainerRepository( + utils.fakeSceneContainerRepository( setOf( - fakeSceneContainerConfig("container1"), - fakeSceneContainerConfig( + utils.fakeSceneContainerConfig("container1"), + utils.fakeSceneContainerConfig( "container2", listOf(SceneKey.QuickSettings, SceneKey.LockScreen) ), @@ -94,7 +97,7 @@ class SceneContainerRepositoryTest : SysuiTestCase() { @Test fun isVisible() = runTest { - val underTest = fakeSceneContainerRepository() + val underTest = utils.fakeSceneContainerRepository() val isVisible by collectLastValue(underTest.isVisible("container1")) assertThat(isVisible).isTrue() @@ -107,19 +110,19 @@ class SceneContainerRepositoryTest : SysuiTestCase() { @Test(expected = IllegalStateException::class) fun isVisible_noSuchContainer_throws() { - val underTest = fakeSceneContainerRepository() + val underTest = utils.fakeSceneContainerRepository() underTest.isVisible("nonExistingContainer") } @Test(expected = IllegalStateException::class) fun setVisible_noSuchContainer_throws() { - val underTest = fakeSceneContainerRepository() + val underTest = utils.fakeSceneContainerRepository() underTest.setVisible("nonExistingContainer", false) } @Test fun sceneTransitionProgress() = runTest { - val underTest = fakeSceneContainerRepository() + val underTest = utils.fakeSceneContainerRepository() val sceneTransitionProgress by collectLastValue(underTest.sceneTransitionProgress("container1")) assertThat(sceneTransitionProgress).isEqualTo(1f) @@ -133,7 +136,7 @@ class SceneContainerRepositoryTest : SysuiTestCase() { @Test(expected = IllegalStateException::class) fun sceneTransitionProgress_noSuchContainer_throws() { - val underTest = fakeSceneContainerRepository() + val underTest = utils.fakeSceneContainerRepository() underTest.sceneTransitionProgress("nonExistingContainer") } } diff --git a/packages/SystemUI/tests/src/com/android/systemui/scene/domain/interactor/SceneInteractorTest.kt b/packages/SystemUI/tests/src/com/android/systemui/scene/domain/interactor/SceneInteractorTest.kt index c5ce092468627..cf99e3b09b32b 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/scene/domain/interactor/SceneInteractorTest.kt +++ b/packages/SystemUI/tests/src/com/android/systemui/scene/domain/interactor/SceneInteractorTest.kt @@ -21,8 +21,7 @@ package com.android.systemui.scene.domain.interactor import androidx.test.filters.SmallTest import com.android.systemui.SysuiTestCase import com.android.systemui.coroutines.collectLastValue -import com.android.systemui.scene.data.repository.fakeSceneContainerRepository -import com.android.systemui.scene.data.repository.fakeSceneKeys +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 @@ -36,14 +35,12 @@ import org.junit.runners.JUnit4 @RunWith(JUnit4::class) class SceneInteractorTest : SysuiTestCase() { - private val underTest = - SceneInteractor( - repository = fakeSceneContainerRepository(), - ) + private val utils = SceneTestUtils(this) + private val underTest = utils.sceneInteractor() @Test fun allSceneKeys() { - assertThat(underTest.allSceneKeys("container1")).isEqualTo(fakeSceneKeys()) + assertThat(underTest.allSceneKeys("container1")).isEqualTo(utils.fakeSceneKeys()) } @Test diff --git a/packages/SystemUI/tests/src/com/android/systemui/scene/ui/viewmodel/SceneContainerViewModelTest.kt b/packages/SystemUI/tests/src/com/android/systemui/scene/ui/viewmodel/SceneContainerViewModelTest.kt index ab61ddddaeab5..6105c87357149 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/scene/ui/viewmodel/SceneContainerViewModelTest.kt +++ b/packages/SystemUI/tests/src/com/android/systemui/scene/ui/viewmodel/SceneContainerViewModelTest.kt @@ -21,9 +21,7 @@ package com.android.systemui.scene.ui.viewmodel import androidx.test.filters.SmallTest import com.android.systemui.SysuiTestCase import com.android.systemui.coroutines.collectLastValue -import com.android.systemui.scene.data.repository.fakeSceneContainerRepository -import com.android.systemui.scene.data.repository.fakeSceneKeys -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 @@ -36,10 +34,9 @@ import org.junit.runners.JUnit4 @SmallTest @RunWith(JUnit4::class) class SceneContainerViewModelTest : SysuiTestCase() { - private val interactor = - SceneInteractor( - repository = fakeSceneContainerRepository(), - ) + + private val utils = SceneTestUtils(this) + private val interactor = utils.sceneInteractor() private val underTest = SceneContainerViewModel( interactor = interactor, @@ -60,7 +57,7 @@ class SceneContainerViewModelTest : SysuiTestCase() { @Test fun allSceneKeys() { - assertThat(underTest.allSceneKeys).isEqualTo(fakeSceneKeys()) + assertThat(underTest.allSceneKeys).isEqualTo(utils.fakeSceneKeys()) } @Test diff --git a/packages/SystemUI/tests/src/com/android/systemui/shade/ui/viewmodel/ShadeSceneViewModelTest.kt b/packages/SystemUI/tests/src/com/android/systemui/shade/ui/viewmodel/ShadeSceneViewModelTest.kt index 688cce83a55d3..2e7f83d597f2c 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/shade/ui/viewmodel/ShadeSceneViewModelTest.kt +++ b/packages/SystemUI/tests/src/com/android/systemui/shade/ui/viewmodel/ShadeSceneViewModelTest.kt @@ -18,15 +18,11 @@ package com.android.systemui.shade.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.keyguard.domain.interactor.LockScreenSceneInteractor -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.SceneTestUtils.Companion.CONTAINER_1 import com.android.systemui.scene.shared.model.SceneKey import com.android.systemui.scene.shared.model.SceneModel import com.google.common.truth.Truth.assertThat @@ -44,14 +40,11 @@ import org.junit.runners.JUnit4 class ShadeSceneViewModelTest : 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 sceneInteractor = utils.sceneInteractor() + private val authenticationInteractor = + utils.authenticationInteractor( + repository = utils.authenticationRepository(), ) private val underTest = @@ -60,36 +53,26 @@ class ShadeSceneViewModelTest : SysuiTestCase() { lockScreenSceneInteractorFactory = object : LockScreenSceneInteractor.Factory { override fun create(containerName: String): LockScreenSceneInteractor { - return LockScreenSceneInteractor( - applicationScope = testScope.backgroundScope, - authenticationInteractor = mAuthenticationInteractor, - bouncerInteractorFactory = - object : BouncerInteractor.Factory { - override fun create(containerName: String): BouncerInteractor { - return BouncerInteractor( - applicationScope = testScope.backgroundScope, - applicationContext = context, - repository = BouncerRepository(), - authenticationInteractor = mAuthenticationInteractor, - sceneInteractor = sceneInteractor, - containerName = containerName, - ) - } - }, + return utils.lockScreenSceneInteractor( + authenticationInteractor = authenticationInteractor, sceneInteractor = sceneInteractor, - containerName = CONTAINER_NAME, + bouncerInteractor = + utils.bouncerInteractor( + authenticationInteractor = authenticationInteractor, + sceneInteractor = sceneInteractor, + ), ) } }, - containerName = CONTAINER_NAME + containerName = SceneTestUtils.CONTAINER_1 ) @Test fun upTransitionSceneKey_deviceLocked_lockScreen() = testScope.runTest { val upTransitionSceneKey by collectLastValue(underTest.upDestinationSceneKey) - mAuthenticationInteractor.setAuthenticationMethod(AuthenticationMethodModel.PIN(1234)) - mAuthenticationInteractor.lockDevice() + authenticationInteractor.setAuthenticationMethod(AuthenticationMethodModel.PIN(1234)) + authenticationInteractor.lockDevice() assertThat(upTransitionSceneKey).isEqualTo(SceneKey.LockScreen) } @@ -98,8 +81,8 @@ class ShadeSceneViewModelTest : SysuiTestCase() { fun upTransitionSceneKey_deviceUnlocked_gone() = testScope.runTest { val upTransitionSceneKey by collectLastValue(underTest.upDestinationSceneKey) - mAuthenticationInteractor.setAuthenticationMethod(AuthenticationMethodModel.PIN(1234)) - mAuthenticationInteractor.unlockDevice() + authenticationInteractor.setAuthenticationMethod(AuthenticationMethodModel.PIN(1234)) + authenticationInteractor.unlockDevice() assertThat(upTransitionSceneKey).isEqualTo(SceneKey.Gone) } @@ -107,9 +90,9 @@ class ShadeSceneViewModelTest : SysuiTestCase() { @Test fun onContentClicked_deviceUnlocked_switchesToGone() = testScope.runTest { - val currentScene by collectLastValue(sceneInteractor.currentScene(CONTAINER_NAME)) - mAuthenticationInteractor.setAuthenticationMethod(AuthenticationMethodModel.PIN(1234)) - mAuthenticationInteractor.unlockDevice() + val currentScene by collectLastValue(sceneInteractor.currentScene(CONTAINER_1)) + authenticationInteractor.setAuthenticationMethod(AuthenticationMethodModel.PIN(1234)) + authenticationInteractor.unlockDevice() runCurrent() underTest.onContentClicked() @@ -120,17 +103,13 @@ class ShadeSceneViewModelTest : SysuiTestCase() { @Test fun onContentClicked_deviceLockedSecurely_switchesToBouncer() = testScope.runTest { - val currentScene by collectLastValue(sceneInteractor.currentScene(CONTAINER_NAME)) - mAuthenticationInteractor.setAuthenticationMethod(AuthenticationMethodModel.PIN(1234)) - mAuthenticationInteractor.lockDevice() + val currentScene by collectLastValue(sceneInteractor.currentScene(CONTAINER_1)) + authenticationInteractor.setAuthenticationMethod(AuthenticationMethodModel.PIN(1234)) + authenticationInteractor.lockDevice() runCurrent() underTest.onContentClicked() assertThat(currentScene).isEqualTo(SceneModel(SceneKey.Bouncer)) } - - companion object { - private const val CONTAINER_NAME = "container1" - } } diff --git a/packages/SystemUI/tests/utils/src/com/android/systemui/scene/SceneTestUtils.kt b/packages/SystemUI/tests/utils/src/com/android/systemui/scene/SceneTestUtils.kt new file mode 100644 index 0000000000000..5a350bb540b75 --- /dev/null +++ b/packages/SystemUI/tests/utils/src/com/android/systemui/scene/SceneTestUtils.kt @@ -0,0 +1,161 @@ +/* + * Copyright (C) 2023 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.systemui.scene + +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.domain.interactor.AuthenticationInteractor +import com.android.systemui.bouncer.data.repo.BouncerRepository +import com.android.systemui.bouncer.domain.interactor.BouncerInteractor +import com.android.systemui.bouncer.ui.viewmodel.BouncerViewModel +import com.android.systemui.keyguard.domain.interactor.LockScreenSceneInteractor +import com.android.systemui.scene.data.model.SceneContainerConfig +import com.android.systemui.scene.data.repository.SceneContainerRepository +import com.android.systemui.scene.domain.interactor.SceneInteractor +import com.android.systemui.scene.shared.model.SceneKey +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.test.TestScope + +/** + * Utilities for creating scene container framework related repositories, interactors, and + * view-models for tests. + */ +@OptIn(ExperimentalCoroutinesApi::class) +class SceneTestUtils( + test: SysuiTestCase, + private val testScope: TestScope? = null, +) { + + private val context = test.context + + fun fakeSceneContainerRepository( + containerConfigurations: Set = + setOf( + fakeSceneContainerConfig(CONTAINER_1), + fakeSceneContainerConfig(CONTAINER_2), + ) + ): SceneContainerRepository { + return SceneContainerRepository(containerConfigurations) + } + + fun fakeSceneKeys(): List { + return listOf( + SceneKey.QuickSettings, + SceneKey.Shade, + SceneKey.LockScreen, + SceneKey.Bouncer, + SceneKey.Gone, + ) + } + + fun fakeSceneContainerConfig( + name: String, + sceneKeys: List = fakeSceneKeys(), + ): SceneContainerConfig { + return SceneContainerConfig( + name = name, + sceneKeys = sceneKeys, + initialSceneKey = SceneKey.LockScreen, + ) + } + + fun sceneInteractor(): SceneInteractor { + return SceneInteractor( + repository = fakeSceneContainerRepository(), + ) + } + + fun authenticationRepository(): AuthenticationRepository { + return AuthenticationRepositoryImpl() + } + + fun authenticationInteractor( + repository: AuthenticationRepository, + ): AuthenticationInteractor { + return AuthenticationInteractor( + applicationScope = applicationScope(), + repository = repository, + ) + } + + private fun applicationScope(): CoroutineScope { + return checkNotNull(testScope) { + """ + TestScope not initialized, please create a TestScope and inject it into + SceneTestUtils. + """ + .trimIndent() + } + .backgroundScope + } + + fun bouncerInteractor( + authenticationInteractor: AuthenticationInteractor, + sceneInteractor: SceneInteractor, + ): BouncerInteractor { + return BouncerInteractor( + applicationScope = applicationScope(), + applicationContext = context, + repository = BouncerRepository(), + authenticationInteractor = authenticationInteractor, + sceneInteractor = sceneInteractor, + containerName = CONTAINER_1, + ) + } + + fun bouncerViewModel( + bouncerInteractor: BouncerInteractor, + ): BouncerViewModel { + return BouncerViewModel( + applicationContext = context, + applicationScope = applicationScope(), + interactorFactory = + object : BouncerInteractor.Factory { + override fun create(containerName: String): BouncerInteractor { + return bouncerInteractor + } + }, + containerName = CONTAINER_1, + ) + } + + fun lockScreenSceneInteractor( + authenticationInteractor: AuthenticationInteractor, + sceneInteractor: SceneInteractor, + bouncerInteractor: BouncerInteractor, + ): LockScreenSceneInteractor { + return LockScreenSceneInteractor( + applicationScope = applicationScope(), + authenticationInteractor = authenticationInteractor, + bouncerInteractorFactory = + object : BouncerInteractor.Factory { + override fun create(containerName: String): BouncerInteractor { + return bouncerInteractor + } + }, + sceneInteractor = sceneInteractor, + containerName = CONTAINER_1, + ) + } + + companion object { + const val CONTAINER_1 = "container1" + const val CONTAINER_2 = "container2" + } +}