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

Commit 4319969d authored by Caitlin Shkuratov's avatar Caitlin Shkuratov
Browse files

[SB][Wifi] Don't expose WifiRepositoryImpl.selectedUserContext.

We needed to temporarily expose selectedUserContext to easily test the
new multiuser logic in WifiRepositoryImplTest. This CL updates that test
class to reset the wifiPickerTrackerFactory and use a new context captor,
which helps us verify the context via argument capture instead of
exposing the context flow.

Bug: 371586248
Flag: TEST_ONLY
Test: atest WifiRepositoryImplTest
Change-Id: Ib89eb3c63fa57aba240f3faf0cb0e282d01b6be8
parent 4e9ab42b
Loading
Loading
Loading
Loading
+31 −25
Original line number Diff line number Diff line
@@ -62,7 +62,8 @@ import org.junit.runner.RunWith
import org.mockito.kotlin.any
import org.mockito.kotlin.argumentCaptor
import org.mockito.kotlin.mock
import org.mockito.kotlin.times
import org.mockito.kotlin.never
import org.mockito.kotlin.reset
import org.mockito.kotlin.verify
import org.mockito.kotlin.whenever

@@ -1200,9 +1201,6 @@ class WifiRepositoryImplTest : SysuiTestCase() {
            assertThat(latest).isEmpty()
        }

    // TODO(b/371586248): This test currently require currentUserContext to be public for testing,
    // this needs to
    // be updated to capture the argument instead so currentUserContext can be private.
    @Test
    @EnableFlags(FLAG_MULTIUSER_WIFI_PICKER_TRACKER_SUPPORT)
    fun oneUserVerifyCreatingWifiPickerTracker_multiuserFlagEnabled() =
@@ -1215,15 +1213,15 @@ class WifiRepositoryImplTest : SysuiTestCase() {

            userRepository.setSelectedUserInfo(PRIMARY_USER)

            val currentUserContext by collectLastValue(underTest.selectedUserContext)
            collectLastValue(underTest.wifiNetwork)

            assertThat(currentUserContext).isEqualTo(primaryUserMockContext)
            verify(wifiPickerTrackerFactory).create(any(), any(), any(), any())
            val contextCaptor = argumentCaptor<Context>()
            verify(wifiPickerTrackerFactory).create(contextCaptor.capture(), any(), any(), any())
            // If the flag is on, verify that we use the context from #createContextAsUser and we
            // do NOT use the top-level context
            assertThat(contextCaptor.firstValue).isEqualTo(primaryUserMockContext)
        }

    // TODO(b/371586248): This test currently require currentUserContext to be public for testing,
    // this needs to
    // be updated to capture the argument instead so currentUserContext can be private.
    @Test
    @EnableFlags(FLAG_MULTIUSER_WIFI_PICKER_TRACKER_SUPPORT)
    fun changeUserVerifyCreatingWifiPickerTracker_multiuserEnabled() =
@@ -1233,30 +1231,30 @@ class WifiRepositoryImplTest : SysuiTestCase() {
                UserHandle.of(PRIMARY_USER_ID),
                primaryUserMockContext,
            )

            userRepository.setSelectedUserInfo(PRIMARY_USER)

            val currentUserContext by collectLastValue(underTest.selectedUserContext)
            collectLastValue(underTest.wifiNetwork)

            assertThat(currentUserContext).isEqualTo(primaryUserMockContext)
            val contextCaptor = argumentCaptor<Context>()
            verify(wifiPickerTrackerFactory).create(contextCaptor.capture(), any(), any(), any())
            assertThat(contextCaptor.firstValue).isEqualTo(primaryUserMockContext)

            reset(wifiPickerTrackerFactory)

            // WHEN we switch to a different user
            val otherUserMockContext = mock<Context>()
            mContext.prepareCreateContextAsUser(
                UserHandle.of(ANOTHER_USER_ID),
                otherUserMockContext,
            )

            userRepository.setSelectedUserInfo(ANOTHER_USER)

            val otherUserContext by collectLastValue(underTest.selectedUserContext)

            assertThat(otherUserContext).isEqualTo(otherUserMockContext)
            verify(wifiPickerTrackerFactory, times(2)).create(any(), any(), any(), any())
            // THEN we use the different user's context to create WifiPickerTracker
            val newCaptor = argumentCaptor<Context>()
            verify(wifiPickerTrackerFactory).create(newCaptor.capture(), any(), any(), any())
            assertThat(newCaptor.firstValue).isEqualTo(otherUserMockContext)
        }

    // TODO(b/371586248): This test currently require currentUserContext to be public for testing,
    // this needs to
    // be updated to capture the argument instead so currentUserContext can be private.
    @Test
    @DisableFlags(FLAG_MULTIUSER_WIFI_PICKER_TRACKER_SUPPORT)
    fun changeUserVerifyCreatingWifiPickerTracker_multiuserDisabled() =
@@ -1269,19 +1267,27 @@ class WifiRepositoryImplTest : SysuiTestCase() {

            userRepository.setSelectedUserInfo(PRIMARY_USER)

            val currentUserContext by collectLastValue(underTest.selectedUserContext)
            collectLastValue(underTest.wifiNetwork)

            val contextCaptor = argumentCaptor<Context>()
            verify(wifiPickerTrackerFactory).create(contextCaptor.capture(), any(), any(), any())
            // If the flag is off, verify that we do NOT use the context from #createContextAsUser
            // and we instead use the top-level context
            assertThat(contextCaptor.firstValue).isNotEqualTo(primaryUserMockContext)
            assertThat(contextCaptor.firstValue).isEqualTo(mContext)

            assertThat(currentUserContext).isEqualTo(primaryUserMockContext)
            reset(wifiPickerTrackerFactory)

            // WHEN we switch to a different user
            val otherUserMockContext = mock<Context>()
            mContext.prepareCreateContextAsUser(
                UserHandle.of(ANOTHER_USER_ID),
                otherUserMockContext,
            )

            userRepository.setSelectedUserInfo(ANOTHER_USER)

            verify(wifiPickerTrackerFactory, times(1)).create(any(), any(), any(), any())
            // THEN we do NOT re-create WifiPickerTracker because the multiuser flag is off
            verify(wifiPickerTrackerFactory, never()).create(any(), any(), any(), any())
        }

    private fun getCallback(): WifiPickerTracker.WifiPickerTrackerCallback {
+1 −2
Original line number Diff line number Diff line
@@ -92,8 +92,7 @@ constructor(

    private var wifiPickerTracker: WifiPickerTracker? = null

    @VisibleForTesting
    val selectedUserContext: Flow<Context> =
    private val selectedUserContext: Flow<Context> =
        userRepository.selectedUserInfo.map {
            applicationContext.createContextAsUser(UserHandle.of(it.id), /* flags= */ 0)
        }