CP GuestUserInteractorTest.kt that got left out of CP somehow.
Bug: 369137871
Flag: TEST_ONLY
Test: presubmits
Change-Id: Ia67cd0dde70d58bbc1c28642cb2b1f45a9a8415d
Merged-In: I002033537eb0a5a591dd2b30546a0446c8b99225
diff --git a/packages/SystemUI/tests/src/com/android/systemui/user/domain/interactor/GuestUserInteractorTest.kt b/packages/SystemUI/tests/src/com/android/systemui/user/domain/interactor/GuestUserInteractorTest.kt
index 20b273a..dbcc689 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/user/domain/interactor/GuestUserInteractorTest.kt
+++ b/packages/SystemUI/tests/src/com/android/systemui/user/domain/interactor/GuestUserInteractorTest.kt
@@ -35,8 +35,9 @@
import com.android.systemui.util.mockito.kotlinArgumentCaptor
import com.android.systemui.util.mockito.whenever
import kotlinx.coroutines.Dispatchers
-import kotlinx.coroutines.runBlocking
-import kotlinx.coroutines.test.TestCoroutineScope
+import kotlinx.coroutines.test.TestScope
+import kotlinx.coroutines.test.UnconfinedTestDispatcher
+import kotlinx.coroutines.test.runTest
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
@@ -62,18 +63,17 @@
@Mock private lateinit var resetOrExitSessionReceiver: GuestResetOrExitSessionReceiver
@Mock private lateinit var otherContext: Context
+ private val testDispatcher = UnconfinedTestDispatcher()
+ private val testScope = TestScope(testDispatcher)
private lateinit var underTest: GuestUserInteractor
- private lateinit var scope: TestCoroutineScope
- private lateinit var repository: FakeUserRepository
+ private val repository = FakeUserRepository()
@Before
fun setUp() {
MockitoAnnotations.initMocks(this)
whenever(manager.createGuest(any())).thenReturn(GUEST_USER_INFO)
- scope = TestCoroutineScope()
- repository = FakeUserRepository()
repository.setUserInfos(ALL_USERS)
underTest = initGuestUserInteractor(context)
@@ -82,17 +82,17 @@
private fun initGuestUserInteractor(context: Context) =
GuestUserInteractor(
applicationContext = context,
- applicationScope = scope,
- mainDispatcher = IMMEDIATE,
- backgroundDispatcher = IMMEDIATE,
+ applicationScope = testScope,
+ mainDispatcher = testDispatcher,
+ backgroundDispatcher = testDispatcher,
manager = manager,
repository = repository,
deviceProvisionedController = deviceProvisionedController,
devicePolicyManager = devicePolicyManager,
refreshUsersScheduler =
RefreshUsersScheduler(
- applicationScope = scope,
- mainDispatcher = IMMEDIATE,
+ applicationScope = testScope,
+ mainDispatcher = testDispatcher,
repository = repository,
),
uiEventLogger = uiEventLogger,
@@ -118,7 +118,7 @@
@Test
fun onDeviceBootCompleted_allowedToAdd_createGuest() =
- runBlocking(IMMEDIATE) {
+ testScope.runTest {
setAllowedToAdd()
underTest.onDeviceBootCompleted()
@@ -129,7 +129,7 @@
@Test
fun onDeviceBootCompleted_awaitProvisioning_andCreateGuest() =
- runBlocking(IMMEDIATE) {
+ testScope.runTest {
setAllowedToAdd(isAllowed = false)
underTest.onDeviceBootCompleted()
val captor =
@@ -145,7 +145,7 @@
@Test
fun createAndSwitchTo() =
- runBlocking(IMMEDIATE) {
+ testScope.runTest {
underTest.createAndSwitchTo(
showDialog = showDialog,
dismissDialog = dismissDialog,
@@ -160,7 +160,7 @@
@Test
fun createAndSwitchTo_failsToCreate_doesNotSwitchTo() =
- runBlocking(IMMEDIATE) {
+ testScope.runTest {
whenever(manager.createGuest(any())).thenReturn(null)
underTest.createAndSwitchTo(
@@ -177,7 +177,7 @@
@Test
fun exit_returnsToTargetUser() =
- runBlocking(IMMEDIATE) {
+ testScope.runTest {
repository.setSelectedUserInfo(GUEST_USER_INFO)
val targetUserId = NON_GUEST_USER_INFO.id
@@ -197,7 +197,7 @@
@Test
fun exit_returnsToLastNonGuest() =
- runBlocking(IMMEDIATE) {
+ testScope.runTest {
val expectedUserId = NON_GUEST_USER_INFO.id
whenever(manager.getUserInfo(expectedUserId)).thenReturn(NON_GUEST_USER_INFO)
repository.lastSelectedNonGuestUserId = expectedUserId
@@ -219,7 +219,7 @@
@Test
fun exit_lastNonGuestWasRemoved_returnsToMainUser() =
- runBlocking(IMMEDIATE) {
+ testScope.runTest {
val removedUserId = 310
val mainUserId = 10
repository.lastSelectedNonGuestUserId = removedUserId
@@ -242,7 +242,7 @@
@Test
fun exit_guestWasEphemeral_itIsRemoved() =
- runBlocking(IMMEDIATE) {
+ testScope.runTest {
whenever(manager.markGuestForDeletion(anyInt())).thenReturn(true)
repository.setUserInfos(listOf(NON_GUEST_USER_INFO, EPHEMERAL_GUEST_USER_INFO))
repository.setSelectedUserInfo(EPHEMERAL_GUEST_USER_INFO)
@@ -265,7 +265,7 @@
@Test
fun exit_forceRemoveGuest_itIsRemoved() =
- runBlocking(IMMEDIATE) {
+ testScope.runTest {
whenever(manager.markGuestForDeletion(anyInt())).thenReturn(true)
repository.setSelectedUserInfo(GUEST_USER_INFO)
val targetUserId = NON_GUEST_USER_INFO.id
@@ -287,7 +287,7 @@
@Test
fun exit_selectedDifferentFromGuestUser_doNothing() =
- runBlocking(IMMEDIATE) {
+ testScope.runTest {
repository.setSelectedUserInfo(NON_GUEST_USER_INFO)
underTest.exit(
@@ -304,7 +304,7 @@
@Test
fun exit_selectedIsActuallyNotAguestUser_doNothing() =
- runBlocking(IMMEDIATE) {
+ testScope.runTest {
repository.setSelectedUserInfo(NON_GUEST_USER_INFO)
underTest.exit(
@@ -321,7 +321,7 @@
@Test
fun remove_returnsToTargetUser() =
- runBlocking(IMMEDIATE) {
+ testScope.runTest {
whenever(manager.markGuestForDeletion(anyInt())).thenReturn(true)
repository.setSelectedUserInfo(GUEST_USER_INFO)
@@ -342,7 +342,7 @@
@Test
fun remove_selectedDifferentFromGuestUser_doNothing() =
- runBlocking(IMMEDIATE) {
+ testScope.runTest {
whenever(manager.markGuestForDeletion(anyInt())).thenReturn(true)
repository.setSelectedUserInfo(NON_GUEST_USER_INFO)
@@ -359,7 +359,7 @@
@Test
fun remove_selectedIsActuallyNotAguestUser_doNothing() =
- runBlocking(IMMEDIATE) {
+ testScope.runTest {
whenever(manager.markGuestForDeletion(anyInt())).thenReturn(true)
repository.setSelectedUserInfo(NON_GUEST_USER_INFO)
@@ -395,11 +395,7 @@
companion object {
private val IMMEDIATE = Dispatchers.Main.immediate
private val NON_GUEST_USER_INFO =
- UserInfo(
- /* id= */ 818,
- /* name= */ "non_guest",
- /* flags= */ UserInfo.FLAG_FULL,
- )
+ UserInfo(/* id= */ 818, /* name= */ "non_guest", /* flags= */ UserInfo.FLAG_FULL)
private val GUEST_USER_INFO =
UserInfo(
/* id= */ 669,
@@ -416,10 +412,6 @@
/* flags= */ UserInfo.FLAG_EPHEMERAL or UserInfo.FLAG_FULL,
UserManager.USER_TYPE_FULL_GUEST,
)
- private val ALL_USERS =
- listOf(
- NON_GUEST_USER_INFO,
- GUEST_USER_INFO,
- )
+ private val ALL_USERS = listOf(NON_GUEST_USER_INFO, GUEST_USER_INFO)
}
}