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

Commit 42127db4 authored by Caitlin Shkuratov's avatar Caitlin Shkuratov Committed by Automerger Merge Worker
Browse files

Merge "[SB Refactor] Update the wifi network flow to use stateIn so that new...

Merge "[SB Refactor] Update the wifi network flow to use stateIn so that new subscribers will receive the current value." into tm-qpr-dev am: 716ba8ea

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/19755228



Change-Id: I20c711f860eceae9cad25a02bc234d08245387f7
Signed-off-by: default avatarAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
parents 6a250181 716ba8ea
Loading
Loading
Loading
Loading
+10 −3
Original line number Diff line number Diff line
@@ -46,7 +46,7 @@ import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.shareIn
import kotlinx.coroutines.flow.stateIn

/**
 * Provides data related to the wifi state.
@@ -118,12 +118,19 @@ class WifiRepositoryImpl @Inject constructor(
            }
        }

        trySend(WIFI_NETWORK_DEFAULT)
        connectivityManager.registerNetworkCallback(WIFI_NETWORK_CALLBACK_REQUEST, callback)

        awaitClose { connectivityManager.unregisterNetworkCallback(callback) }
    }
        .shareIn(scope, started = SharingStarted.WhileSubscribed())
        // There will be multiple wifi icons in different places that will frequently
        // subscribe/unsubscribe to flows as the views attach/detach. Using [stateIn] ensures that
        // new subscribes will get the latest value immediately upon subscription. Otherwise, the
        // views could show stale data. See b/244173280.
        .stateIn(
            scope,
            started = SharingStarted.WhileSubscribed(),
            initialValue = WIFI_NETWORK_DEFAULT
        )

    override val wifiActivity: Flow<WifiActivityModel> =
            if (wifiManager == null) {
+34 −0
Original line number Diff line number Diff line
@@ -473,6 +473,40 @@ class WifiRepositoryImplTest : SysuiTestCase() {
        job.cancel()
    }

    /** Regression test for b/244173280. */
    @Test
    fun wifiNetwork_multipleSubscribers_newSubscribersGetCurrentValue() = runBlocking(IMMEDIATE) {
        var latest1: WifiNetworkModel? = null
        val job1 = underTest
            .wifiNetwork
            .onEach { latest1 = it }
            .launchIn(this)

        getNetworkCallback()
            .onCapabilitiesChanged(NETWORK, createWifiNetworkCapabilities(PRIMARY_WIFI_INFO))

        assertThat(latest1 is WifiNetworkModel.Active).isTrue()
        val latest1Active = latest1 as WifiNetworkModel.Active
        assertThat(latest1Active.networkId).isEqualTo(NETWORK_ID)
        assertThat(latest1Active.ssid).isEqualTo(SSID)

        // WHEN we add a second subscriber after having already emitted a value
        var latest2: WifiNetworkModel? = null
        val job2 = underTest
            .wifiNetwork
            .onEach { latest2 = it }
            .launchIn(this)

        // THEN the second subscribe receives the already-emitted value
        assertThat(latest2 is WifiNetworkModel.Active).isTrue()
        val latest2Active = latest2 as WifiNetworkModel.Active
        assertThat(latest2Active.networkId).isEqualTo(NETWORK_ID)
        assertThat(latest2Active.ssid).isEqualTo(SSID)

        job1.cancel()
        job2.cancel()
    }

    @Test
    fun wifiActivity_nullWifiManager_receivesDefault() = runBlocking(IMMEDIATE) {
        underTest = WifiRepositoryImpl(