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

Commit 384e046e authored by David Saff's avatar David Saff
Browse files

Introduce useUnconfinedTestDispatcher, etc, use them

This is the first step in a process that will allow us to use
UnconfinedTestDispatcher in most tests, reducing the need to
use runCurrent except where needed for precise tests of
concurrent logic.

Details at go/thetiger

Bug: 342622417
Test: local unit test runs of changed tests
Flag: TEST_ONLY

Change-Id: Iabc6b609e97109aa28fe4534fb7a0a14c1082cf2
parent 58f46938
Loading
Loading
Loading
Loading
+2 −3
Original line number Diff line number Diff line
@@ -20,9 +20,8 @@ import android.view.Display
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.android.systemui.SysuiTestCase
import com.android.systemui.kosmos.testDispatcher
import com.android.systemui.kosmos.testScope
import com.android.systemui.kosmos.unconfinedTestDispatcher
import com.android.systemui.kosmos.useUnconfinedTestDispatcher
import com.android.systemui.testKosmos
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.runBlocking
@@ -37,7 +36,7 @@ import org.mockito.kotlin.mock
@SmallTest
class PerDisplayStoreImplTest : SysuiTestCase() {

    private val kosmos = testKosmos().also { it.testDispatcher = it.unconfinedTestDispatcher }
    private val kosmos = testKosmos().useUnconfinedTestDispatcher()
    private val testScope = kosmos.testScope
    private val fakeDisplayRepository = kosmos.displayRepository

+17 −31
Original line number Diff line number Diff line
@@ -23,8 +23,9 @@ import android.provider.Settings
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.android.systemui.SysuiTestCase
import com.android.systemui.kosmos.unconfinedTestDispatcher
import com.android.systemui.kosmos.unconfinedTestScope
import com.android.systemui.kosmos.testDispatcher
import com.android.systemui.kosmos.testScope
import com.android.systemui.kosmos.useUnconfinedTestDispatcher
import com.android.systemui.res.R
import com.android.systemui.settings.FakeUserTracker
import com.android.systemui.shared.keyguard.shared.model.KeyguardQuickAffordanceSlots
@@ -33,7 +34,7 @@ import com.android.systemui.util.FakeSharedPreferences
import com.android.systemui.util.mockito.any
import com.android.systemui.util.mockito.mock
import com.android.systemui.util.mockito.whenever
import com.android.systemui.util.settings.unconfinedDispatcherFakeSettings
import com.android.systemui.util.settings.fakeSettings
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.advanceUntilIdle
@@ -51,10 +52,10 @@ import org.mockito.MockitoAnnotations
@RunWith(AndroidJUnit4::class)
class KeyguardQuickAffordanceLegacySettingSyncerTest : SysuiTestCase() {

    private val kosmos = testKosmos()
    private val testDispatcher = kosmos.unconfinedTestDispatcher
    private val testScope = kosmos.unconfinedTestScope
    private val settings = kosmos.unconfinedDispatcherFakeSettings
    private val kosmos = testKosmos().useUnconfinedTestDispatcher()
    private val testDispatcher = kosmos.testDispatcher
    private val testScope = kosmos.testScope
    private val settings = kosmos.fakeSettings

    @Mock private lateinit var sharedPrefs: FakeSharedPreferences

@@ -79,13 +80,7 @@ class KeyguardQuickAffordanceLegacySettingSyncerTest : SysuiTestCase() {
                context = context,
                userFileManager =
                    mock {
                        whenever(
                                getSharedPreferences(
                                    anyString(),
                                    anyInt(),
                                    anyInt(),
                                )
                            )
                        whenever(getSharedPreferences(anyString(), anyInt(), anyInt()))
                            .thenReturn(FakeSharedPreferences())
                    },
                userTracker = FakeUserTracker(),
@@ -109,17 +104,14 @@ class KeyguardQuickAffordanceLegacySettingSyncerTest : SysuiTestCase() {
        testScope.runTest {
            val job = underTest.startSyncing()

            settings.putInt(
                Settings.Secure.LOCKSCREEN_SHOW_CONTROLS,
                1,
            )
            settings.putInt(Settings.Secure.LOCKSCREEN_SHOW_CONTROLS, 1)

            assertThat(
                    selectionManager
                        .getSelections()
                        .getOrDefault(
                            KeyguardQuickAffordanceSlots.SLOT_ID_BOTTOM_START,
                            emptyList()
                            emptyList(),
                        )
                )
                .contains(BuiltInKeyguardQuickAffordanceKeys.HOME_CONTROLS)
@@ -132,21 +124,15 @@ class KeyguardQuickAffordanceLegacySettingSyncerTest : SysuiTestCase() {
        testScope.runTest {
            val job = underTest.startSyncing()

            settings.putInt(
                Settings.Secure.LOCKSCREEN_SHOW_CONTROLS,
                1,
            )
            settings.putInt(
                Settings.Secure.LOCKSCREEN_SHOW_CONTROLS,
                0,
            )
            settings.putInt(Settings.Secure.LOCKSCREEN_SHOW_CONTROLS, 1)
            settings.putInt(Settings.Secure.LOCKSCREEN_SHOW_CONTROLS, 0)

            assertThat(
                    selectionManager
                        .getSelections()
                        .getOrDefault(
                            KeyguardQuickAffordanceSlots.SLOT_ID_BOTTOM_START,
                            emptyList()
                            emptyList(),
                        )
                )
                .doesNotContain(BuiltInKeyguardQuickAffordanceKeys.HOME_CONTROLS)
@@ -161,7 +147,7 @@ class KeyguardQuickAffordanceLegacySettingSyncerTest : SysuiTestCase() {

            selectionManager.setSelections(
                KeyguardQuickAffordanceSlots.SLOT_ID_BOTTOM_END,
                listOf(BuiltInKeyguardQuickAffordanceKeys.QUICK_ACCESS_WALLET)
                listOf(BuiltInKeyguardQuickAffordanceKeys.QUICK_ACCESS_WALLET),
            )

            advanceUntilIdle()
@@ -177,11 +163,11 @@ class KeyguardQuickAffordanceLegacySettingSyncerTest : SysuiTestCase() {

            selectionManager.setSelections(
                KeyguardQuickAffordanceSlots.SLOT_ID_BOTTOM_END,
                listOf(BuiltInKeyguardQuickAffordanceKeys.QUICK_ACCESS_WALLET)
                listOf(BuiltInKeyguardQuickAffordanceKeys.QUICK_ACCESS_WALLET),
            )
            selectionManager.setSelections(
                KeyguardQuickAffordanceSlots.SLOT_ID_BOTTOM_END,
                emptyList()
                emptyList(),
            )

            assertThat(settings.getInt(Settings.Secure.LOCKSCREEN_SHOW_WALLET)).isEqualTo(0)
+2 −3
Original line number Diff line number Diff line
@@ -22,9 +22,8 @@ import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.android.systemui.SysuiTestCase
import com.android.systemui.bouncer.data.repository.fakeKeyguardBouncerRepository
import com.android.systemui.kosmos.testDispatcher
import com.android.systemui.kosmos.testScope
import com.android.systemui.kosmos.unconfinedTestDispatcher
import com.android.systemui.kosmos.useUnconfinedTestDispatcher
import com.android.systemui.plugins.DarkIconDispatcher
import com.android.systemui.plugins.mockPluginDependencyProvider
import com.android.systemui.plugins.statusbar.StatusBarStateController
@@ -57,7 +56,7 @@ import org.mockito.kotlin.verify
@RunWith(AndroidJUnit4::class)
class StatusBarOrchestratorTest : SysuiTestCase() {

    private val kosmos = testKosmos().also { it.testDispatcher = it.unconfinedTestDispatcher }
    private val kosmos = testKosmos().useUnconfinedTestDispatcher()
    private val testScope = kosmos.testScope
    private val fakeStatusBarModePerDisplayRepository = kosmos.fakeStatusBarModePerDisplayRepository
    private val mockPluginDependencyProvider = kosmos.mockPluginDependencyProvider
+2 −3
Original line number Diff line number Diff line
@@ -21,9 +21,8 @@ import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.android.systemui.SysuiTestCase
import com.android.systemui.display.data.repository.displayRepository
import com.android.systemui.kosmos.testDispatcher
import com.android.systemui.kosmos.testScope
import com.android.systemui.kosmos.unconfinedTestDispatcher
import com.android.systemui.kosmos.useUnconfinedTestDispatcher
import com.android.systemui.testKosmos
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.runTest
@@ -37,7 +36,7 @@ import org.mockito.kotlin.verify
@RunWith(AndroidJUnit4::class)
class MultiDisplayStatusBarContentInsetsProviderStoreTest : SysuiTestCase() {

    private val kosmos = testKosmos().also { it.testDispatcher = it.unconfinedTestDispatcher }
    private val kosmos = testKosmos().useUnconfinedTestDispatcher()
    private val testScope = kosmos.testScope
    private val fakeDisplayRepository = kosmos.displayRepository
    private val underTest = kosmos.multiDisplayStatusBarContentInsetsProviderStore
+16 −44
Original line number Diff line number Diff line
@@ -24,14 +24,15 @@ import android.provider.Settings
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.android.systemui.SysuiTestCase
import com.android.systemui.kosmos.unconfinedTestDispatcher
import com.android.systemui.kosmos.unconfinedTestScope
import com.android.systemui.kosmos.testDispatcher
import com.android.systemui.kosmos.testScope
import com.android.systemui.kosmos.useUnconfinedTestDispatcher
import com.android.systemui.settings.FakeUserTracker
import com.android.systemui.testKosmos
import com.android.systemui.user.data.model.SelectedUserModel
import com.android.systemui.user.data.model.SelectionStatus
import com.android.systemui.user.data.model.UserSwitcherSettingsModel
import com.android.systemui.util.settings.unconfinedDispatcherFakeGlobalSettings
import com.android.systemui.util.settings.fakeGlobalSettings
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
@@ -52,10 +53,10 @@ import org.mockito.MockitoAnnotations
@RunWith(AndroidJUnit4::class)
class UserRepositoryImplTest : SysuiTestCase() {

    private val kosmos = testKosmos()
    private val testDispatcher = kosmos.unconfinedTestDispatcher
    private val testScope = kosmos.unconfinedTestScope
    private val globalSettings = kosmos.unconfinedDispatcherFakeGlobalSettings
    private val kosmos = testKosmos().useUnconfinedTestDispatcher()
    private val testDispatcher = kosmos.testDispatcher
    private val testScope = kosmos.testScope
    private val globalSettings = kosmos.fakeGlobalSettings

    @Mock private lateinit var manager: UserManager

@@ -131,11 +132,7 @@ class UserRepositoryImplTest : SysuiTestCase() {
            whenever(mainUser.identifier).thenReturn(mainUserId)

            underTest = create(testScope.backgroundScope)
            val initialExpectedValue =
                setUpUsers(
                    count = 3,
                    selectedIndex = 0,
                )
            val initialExpectedValue = setUpUsers(count = 3, selectedIndex = 0)
            var userInfos: List<UserInfo>? = null
            var selectedUserInfo: UserInfo? = null
            val job1 = underTest.userInfos.onEach { userInfos = it }.launchIn(this)
@@ -146,11 +143,7 @@ class UserRepositoryImplTest : SysuiTestCase() {
            assertThat(selectedUserInfo).isEqualTo(initialExpectedValue[0])
            assertThat(underTest.lastSelectedNonGuestUserId).isEqualTo(selectedUserInfo?.id)

            val secondExpectedValue =
                setUpUsers(
                    count = 4,
                    selectedIndex = 1,
                )
            val secondExpectedValue = setUpUsers(count = 4, selectedIndex = 1)
            underTest.refreshUsers()
            assertThat(userInfos).isEqualTo(secondExpectedValue)
            assertThat(selectedUserInfo).isEqualTo(secondExpectedValue[1])
@@ -158,11 +151,7 @@ class UserRepositoryImplTest : SysuiTestCase() {

            val selectedNonGuestUserId = selectedUserInfo?.id
            val thirdExpectedValue =
                setUpUsers(
                    count = 2,
                    isLastGuestUser = true,
                    selectedIndex = 1,
                )
                setUpUsers(count = 2, isLastGuestUser = true, selectedIndex = 1)
            underTest.refreshUsers()
            assertThat(userInfos).isEqualTo(thirdExpectedValue)
            assertThat(selectedUserInfo).isEqualTo(thirdExpectedValue[1])
@@ -177,12 +166,7 @@ class UserRepositoryImplTest : SysuiTestCase() {
    fun refreshUsers_sortsByCreationTime_guestUserLast() =
        testScope.runTest {
            underTest = create(testScope.backgroundScope)
            val unsortedUsers =
                setUpUsers(
                    count = 3,
                    selectedIndex = 0,
                    isLastGuestUser = true,
                )
            val unsortedUsers = setUpUsers(count = 3, selectedIndex = 0, isLastGuestUser = true)
            unsortedUsers[0].creationTime = 999
            unsortedUsers[1].creationTime = 900
            unsortedUsers[2].creationTime = 950
@@ -207,10 +191,7 @@ class UserRepositoryImplTest : SysuiTestCase() {
    ): List<UserInfo> {
        val userInfos =
            (0 until count).map { index ->
                createUserInfo(
                    index,
                    isGuest = isLastGuestUser && index == count - 1,
                )
                createUserInfo(index, isGuest = isLastGuestUser && index == count - 1)
            }
        whenever(manager.aliveUsers).thenReturn(userInfos)
        tracker.set(userInfos, selectedIndex)
@@ -224,16 +205,10 @@ class UserRepositoryImplTest : SysuiTestCase() {
            var selectedUserInfo: UserInfo? = null
            val job = underTest.selectedUserInfo.onEach { selectedUserInfo = it }.launchIn(this)

            setUpUsers(
                count = 2,
                selectedIndex = 0,
            )
            setUpUsers(count = 2, selectedIndex = 0)
            tracker.onProfileChanged()
            assertThat(selectedUserInfo?.id).isEqualTo(0)
            setUpUsers(
                count = 2,
                selectedIndex = 1,
            )
            setUpUsers(count = 2, selectedIndex = 1)
            tracker.onProfileChanged()
            assertThat(selectedUserInfo?.id).isEqualTo(1)
            job.cancel()
@@ -287,10 +262,7 @@ class UserRepositoryImplTest : SysuiTestCase() {
            job.cancel()
        }

    private fun createUserInfo(
        id: Int,
        isGuest: Boolean,
    ): UserInfo {
    private fun createUserInfo(id: Int, isGuest: Boolean): UserInfo {
        val flags = 0
        return UserInfo(
            id,
Loading