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

Commit 5e4b83bc authored by Caitlin Shkuratov's avatar Caitlin Shkuratov Committed by Android (Google) Code Review
Browse files

Merge changes Ib89eb3c6,I077d716c into main

* changes:
  [SB][Wifi] Don't expose WifiRepositoryImpl.selectedUserContext.
  [SB][Wifi] Small cleanups of WifiRepositoryImplTest.
parents b0394891 4319969d
Loading
Loading
Loading
Loading
+49 −67
Original line number Original line Diff line number Diff line
@@ -27,10 +27,14 @@ import android.platform.test.annotations.DisableFlags
import android.platform.test.annotations.EnableFlags
import android.platform.test.annotations.EnableFlags
import android.telephony.SubscriptionManager.INVALID_SUBSCRIPTION_ID
import android.telephony.SubscriptionManager.INVALID_SUBSCRIPTION_ID
import android.testing.TestableLooper
import android.testing.TestableLooper
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import androidx.test.filters.SmallTest
import com.android.systemui.Flags.FLAG_MULTIUSER_WIFI_PICKER_TRACKER_SUPPORT
import com.android.systemui.Flags.FLAG_MULTIUSER_WIFI_PICKER_TRACKER_SUPPORT
import com.android.systemui.SysuiTestCase
import com.android.systemui.SysuiTestCase
import com.android.systemui.coroutines.collectLastValue
import com.android.systemui.coroutines.collectLastValue
import com.android.systemui.kosmos.testDispatcher
import com.android.systemui.kosmos.testScope
import com.android.systemui.kosmos.useUnconfinedTestDispatcher
import com.android.systemui.log.LogBuffer
import com.android.systemui.log.LogBuffer
import com.android.systemui.log.table.logcatTableLogBuffer
import com.android.systemui.log.table.logcatTableLogBuffer
import com.android.systemui.statusbar.connectivity.WifiPickerTrackerFactory
import com.android.systemui.statusbar.connectivity.WifiPickerTrackerFactory
@@ -40,9 +44,7 @@ import com.android.systemui.statusbar.pipeline.wifi.shared.model.WifiNetworkMode
import com.android.systemui.statusbar.pipeline.wifi.shared.model.WifiScanEntry
import com.android.systemui.statusbar.pipeline.wifi.shared.model.WifiScanEntry
import com.android.systemui.testKosmos
import com.android.systemui.testKosmos
import com.android.systemui.user.data.repository.fakeUserRepository
import com.android.systemui.user.data.repository.fakeUserRepository
import com.android.systemui.user.data.repository.userRepository
import com.android.systemui.util.concurrency.FakeExecutor
import com.android.systemui.util.concurrency.FakeExecutor
import com.android.systemui.util.mockito.argumentCaptor
import com.android.systemui.util.time.fakeSystemClock
import com.android.systemui.util.time.fakeSystemClock
import com.android.wifitrackerlib.HotspotNetworkEntry
import com.android.wifitrackerlib.HotspotNetworkEntry
import com.android.wifitrackerlib.HotspotNetworkEntry.DeviceType
import com.android.wifitrackerlib.HotspotNetworkEntry.DeviceType
@@ -53,31 +55,23 @@ import com.android.wifitrackerlib.WifiEntry.WIFI_LEVEL_MIN
import com.android.wifitrackerlib.WifiEntry.WIFI_LEVEL_UNREACHABLE
import com.android.wifitrackerlib.WifiEntry.WIFI_LEVEL_UNREACHABLE
import com.android.wifitrackerlib.WifiPickerTracker
import com.android.wifitrackerlib.WifiPickerTracker
import com.google.common.truth.Truth.assertThat
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.StandardTestDispatcher
import kotlinx.coroutines.test.TestScope
import kotlinx.coroutines.test.runCurrent
import kotlinx.coroutines.test.runTest
import kotlinx.coroutines.test.runTest
import org.junit.Before
import org.junit.Before
import org.junit.Test
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.kotlin.any
import org.mockito.kotlin.any
import org.mockito.kotlin.capture
import org.mockito.kotlin.argumentCaptor
import org.mockito.kotlin.mock
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.verify
import org.mockito.kotlin.whenever
import org.mockito.kotlin.whenever


/**
 * Note: Most of these tests are duplicates of [WifiRepositoryImplTest] tests.
 *
 * Any new tests added here may also need to be added to [WifiRepositoryImplTest].
 */
@Suppress("EXPERIMENTAL_IS_NOT_ENABLED")
@OptIn(ExperimentalCoroutinesApi::class)
@SmallTest
@SmallTest
@RunWith(AndroidJUnit4::class)
@TestableLooper.RunWithLooper(setAsMainLooper = true)
@TestableLooper.RunWithLooper(setAsMainLooper = true)
class WifiRepositoryImplTest() : SysuiTestCase() {
class WifiRepositoryImplTest : SysuiTestCase() {
    private val kosmos = testKosmos()
    private val kosmos = testKosmos().useUnconfinedTestDispatcher()
    private val userRepository = kosmos.fakeUserRepository
    private val userRepository = kosmos.fakeUserRepository


    // Using lazy means that the class will only be constructed once it's fetched. Because the
    // Using lazy means that the class will only be constructed once it's fetched. Because the
@@ -108,13 +102,13 @@ class WifiRepositoryImplTest() : SysuiTestCase() {


    private val callbackCaptor = argumentCaptor<WifiPickerTracker.WifiPickerTrackerCallback>()
    private val callbackCaptor = argumentCaptor<WifiPickerTracker.WifiPickerTrackerCallback>()


    private val dispatcher = StandardTestDispatcher()
    private val dispatcher = kosmos.testDispatcher
    private val testScope = TestScope(dispatcher)
    private val testScope = kosmos.testScope


    @Before
    @Before
    fun setUp() {
    fun setUp() {
        userRepository.setUserInfos(listOf(PRIMARY_USER, ANOTHER_USER))
        userRepository.setUserInfos(listOf(PRIMARY_USER, ANOTHER_USER))
        whenever(wifiPickerTrackerFactory.create(any(), any(), capture(callbackCaptor), any()))
        whenever(wifiPickerTrackerFactory.create(any(), any(), callbackCaptor.capture(), any()))
            .thenReturn(wifiPickerTracker)
            .thenReturn(wifiPickerTracker)
    }
    }


@@ -122,7 +116,6 @@ class WifiRepositoryImplTest() : SysuiTestCase() {
    fun wifiPickerTrackerCreation_scansDisabled() =
    fun wifiPickerTrackerCreation_scansDisabled() =
        testScope.runTest {
        testScope.runTest {
            collectLastValue(underTest.wifiNetwork)
            collectLastValue(underTest.wifiNetwork)
            testScope.runCurrent()


            verify(wifiPickerTracker).disableScanning()
            verify(wifiPickerTracker).disableScanning()
        }
        }
@@ -1001,7 +994,6 @@ class WifiRepositoryImplTest() : SysuiTestCase() {
                mock<WifiEntry>().apply { whenever(this.isPrimaryNetwork).thenReturn(false) }
                mock<WifiEntry>().apply { whenever(this.isPrimaryNetwork).thenReturn(false) }
            whenever(wifiPickerTracker.connectedWifiEntry).thenReturn(wifiEntry)
            whenever(wifiPickerTracker.connectedWifiEntry).thenReturn(wifiEntry)
            getCallback().onWifiEntriesChanged()
            getCallback().onWifiEntriesChanged()
            testScope.runCurrent()


            assertThat(underTest.isWifiConnectedWithValidSsid()).isFalse()
            assertThat(underTest.isWifiConnectedWithValidSsid()).isFalse()
        }
        }
@@ -1017,7 +1009,6 @@ class WifiRepositoryImplTest() : SysuiTestCase() {
                }
                }
            whenever(wifiPickerTracker.connectedWifiEntry).thenReturn(wifiEntry)
            whenever(wifiPickerTracker.connectedWifiEntry).thenReturn(wifiEntry)
            getCallback().onWifiEntriesChanged()
            getCallback().onWifiEntriesChanged()
            testScope.runCurrent()


            assertThat(underTest.isWifiConnectedWithValidSsid()).isFalse()
            assertThat(underTest.isWifiConnectedWithValidSsid()).isFalse()
        }
        }
@@ -1034,7 +1025,6 @@ class WifiRepositoryImplTest() : SysuiTestCase() {
                }
                }
            whenever(wifiPickerTracker.connectedWifiEntry).thenReturn(wifiEntry)
            whenever(wifiPickerTracker.connectedWifiEntry).thenReturn(wifiEntry)
            getCallback().onWifiEntriesChanged()
            getCallback().onWifiEntriesChanged()
            testScope.runCurrent()


            assertThat(underTest.isWifiConnectedWithValidSsid()).isFalse()
            assertThat(underTest.isWifiConnectedWithValidSsid()).isFalse()
        }
        }
@@ -1051,7 +1041,6 @@ class WifiRepositoryImplTest() : SysuiTestCase() {
                }
                }
            whenever(wifiPickerTracker.connectedWifiEntry).thenReturn(wifiEntry)
            whenever(wifiPickerTracker.connectedWifiEntry).thenReturn(wifiEntry)
            getCallback().onWifiEntriesChanged()
            getCallback().onWifiEntriesChanged()
            testScope.runCurrent()


            assertThat(underTest.isWifiConnectedWithValidSsid()).isFalse()
            assertThat(underTest.isWifiConnectedWithValidSsid()).isFalse()
        }
        }
@@ -1068,7 +1057,6 @@ class WifiRepositoryImplTest() : SysuiTestCase() {
                }
                }
            whenever(wifiPickerTracker.connectedWifiEntry).thenReturn(wifiEntry)
            whenever(wifiPickerTracker.connectedWifiEntry).thenReturn(wifiEntry)
            getCallback().onWifiEntriesChanged()
            getCallback().onWifiEntriesChanged()
            testScope.runCurrent()


            assertThat(underTest.isWifiConnectedWithValidSsid()).isFalse()
            assertThat(underTest.isWifiConnectedWithValidSsid()).isFalse()
        }
        }
@@ -1085,7 +1073,6 @@ class WifiRepositoryImplTest() : SysuiTestCase() {
                }
                }
            whenever(wifiPickerTracker.connectedWifiEntry).thenReturn(wifiEntry)
            whenever(wifiPickerTracker.connectedWifiEntry).thenReturn(wifiEntry)
            getCallback().onWifiEntriesChanged()
            getCallback().onWifiEntriesChanged()
            testScope.runCurrent()


            assertThat(underTest.isWifiConnectedWithValidSsid()).isTrue()
            assertThat(underTest.isWifiConnectedWithValidSsid()).isTrue()
        }
        }
@@ -1103,14 +1090,12 @@ class WifiRepositoryImplTest() : SysuiTestCase() {
                }
                }
            whenever(wifiPickerTracker.connectedWifiEntry).thenReturn(wifiEntry)
            whenever(wifiPickerTracker.connectedWifiEntry).thenReturn(wifiEntry)
            getCallback().onWifiEntriesChanged()
            getCallback().onWifiEntriesChanged()
            testScope.runCurrent()


            assertThat(underTest.isWifiConnectedWithValidSsid()).isTrue()
            assertThat(underTest.isWifiConnectedWithValidSsid()).isTrue()


            // WHEN the network is lost
            // WHEN the network is lost
            whenever(wifiPickerTracker.connectedWifiEntry).thenReturn(null)
            whenever(wifiPickerTracker.connectedWifiEntry).thenReturn(null)
            getCallback().onWifiEntriesChanged()
            getCallback().onWifiEntriesChanged()
            testScope.runCurrent()


            // THEN the isWifiConnected updates
            // THEN the isWifiConnected updates
            assertThat(underTest.isWifiConnectedWithValidSsid()).isFalse()
            assertThat(underTest.isWifiConnectedWithValidSsid()).isFalse()
@@ -1216,9 +1201,6 @@ class WifiRepositoryImplTest() : SysuiTestCase() {
            assertThat(latest).isEmpty()
            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
    @Test
    @EnableFlags(FLAG_MULTIUSER_WIFI_PICKER_TRACKER_SUPPORT)
    @EnableFlags(FLAG_MULTIUSER_WIFI_PICKER_TRACKER_SUPPORT)
    fun oneUserVerifyCreatingWifiPickerTracker_multiuserFlagEnabled() =
    fun oneUserVerifyCreatingWifiPickerTracker_multiuserFlagEnabled() =
@@ -1230,16 +1212,16 @@ class WifiRepositoryImplTest() : SysuiTestCase() {
            )
            )


            userRepository.setSelectedUserInfo(PRIMARY_USER)
            userRepository.setSelectedUserInfo(PRIMARY_USER)
            runCurrent()
            val currentUserContext by collectLastValue(underTest.selectedUserContext)


            assertThat(currentUserContext).isEqualTo(primaryUserMockContext)
            collectLastValue(underTest.wifiNetwork)
            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
    @Test
    @EnableFlags(FLAG_MULTIUSER_WIFI_PICKER_TRACKER_SUPPORT)
    @EnableFlags(FLAG_MULTIUSER_WIFI_PICKER_TRACKER_SUPPORT)
    fun changeUserVerifyCreatingWifiPickerTracker_multiuserEnabled() =
    fun changeUserVerifyCreatingWifiPickerTracker_multiuserEnabled() =
@@ -1249,32 +1231,30 @@ class WifiRepositoryImplTest() : SysuiTestCase() {
                UserHandle.of(PRIMARY_USER_ID),
                UserHandle.of(PRIMARY_USER_ID),
                primaryUserMockContext,
                primaryUserMockContext,
            )
            )

            runCurrent()
            userRepository.setSelectedUserInfo(PRIMARY_USER)
            userRepository.setSelectedUserInfo(PRIMARY_USER)
            runCurrent()
            val currentUserContext by collectLastValue(underTest.selectedUserContext)


            assertThat(currentUserContext).isEqualTo(primaryUserMockContext)
            collectLastValue(underTest.wifiNetwork)

            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>()
            val otherUserMockContext = mock<Context>()
            mContext.prepareCreateContextAsUser(
            mContext.prepareCreateContextAsUser(
                UserHandle.of(ANOTHER_USER_ID),
                UserHandle.of(ANOTHER_USER_ID),
                otherUserMockContext,
                otherUserMockContext,
            )
            )

            runCurrent()
            userRepository.setSelectedUserInfo(ANOTHER_USER)
            userRepository.setSelectedUserInfo(ANOTHER_USER)
            runCurrent()
            val otherUserContext by collectLastValue(underTest.selectedUserContext)


            assertThat(otherUserContext).isEqualTo(otherUserMockContext)
            // THEN we use the different user's context to create WifiPickerTracker
            verify(wifiPickerTrackerFactory, times(2)).create(any(), any(), any(), any())
            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
    @Test
    @DisableFlags(FLAG_MULTIUSER_WIFI_PICKER_TRACKER_SUPPORT)
    @DisableFlags(FLAG_MULTIUSER_WIFI_PICKER_TRACKER_SUPPORT)
    fun changeUserVerifyCreatingWifiPickerTracker_multiuserDisabled() =
    fun changeUserVerifyCreatingWifiPickerTracker_multiuserDisabled() =
@@ -1285,36 +1265,39 @@ class WifiRepositoryImplTest() : SysuiTestCase() {
                primaryUserMockContext,
                primaryUserMockContext,
            )
            )


            runCurrent()
            userRepository.setSelectedUserInfo(PRIMARY_USER)
            userRepository.setSelectedUserInfo(PRIMARY_USER)
            runCurrent()
            val currentUserContext by collectLastValue(underTest.selectedUserContext)


            assertThat(currentUserContext).isEqualTo(primaryUserMockContext)
            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)

            reset(wifiPickerTrackerFactory)


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

            runCurrent()
            userRepository.setSelectedUserInfo(ANOTHER_USER)
            userRepository.setSelectedUserInfo(ANOTHER_USER)
            runCurrent()


            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 {
    private fun getCallback(): WifiPickerTracker.WifiPickerTrackerCallback {
        testScope.runCurrent()
        return callbackCaptor.firstValue
        return callbackCaptor.value
    }
    }


    private fun getTrafficStateCallback(): WifiManager.TrafficStateCallback {
    private fun getTrafficStateCallback(): WifiManager.TrafficStateCallback {
        testScope.runCurrent()
        val callbackCaptor = argumentCaptor<WifiManager.TrafficStateCallback>()
        val callbackCaptor = argumentCaptor<WifiManager.TrafficStateCallback>()
        verify(wifiManager).registerTrafficStateCallback(any(), callbackCaptor.capture())
        verify(wifiManager).registerTrafficStateCallback(any(), callbackCaptor.capture())
        return callbackCaptor.value!!
        return callbackCaptor.firstValue
    }
    }


    private fun createHotspotWithType(@DeviceType type: Int): HotspotNetworkEntry {
    private fun createHotspotWithType(@DeviceType type: Int): HotspotNetworkEntry {
@@ -1325,10 +1308,9 @@ class WifiRepositoryImplTest() : SysuiTestCase() {
    }
    }


    private fun getScanResultsCallback(): WifiManager.ScanResultsCallback {
    private fun getScanResultsCallback(): WifiManager.ScanResultsCallback {
        testScope.runCurrent()
        val callbackCaptor = argumentCaptor<WifiManager.ScanResultsCallback>()
        val callbackCaptor = argumentCaptor<WifiManager.ScanResultsCallback>()
        verify(wifiManager).registerScanResultsCallback(any(), callbackCaptor.capture())
        verify(wifiManager).registerScanResultsCallback(any(), callbackCaptor.capture())
        return callbackCaptor.value!!
        return callbackCaptor.firstValue
    }
    }


    private companion object {
    private companion object {
+1 −2
Original line number Original line Diff line number Diff line
@@ -92,8 +92,7 @@ constructor(


    private var wifiPickerTracker: WifiPickerTracker? = null
    private var wifiPickerTracker: WifiPickerTracker? = null


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