diff options
| author | 2022-08-30 16:53:45 +0000 | |
|---|---|---|
| committer | 2022-08-30 16:53:45 +0000 | |
| commit | 716ba8ea29f595bc457af157dccc2064c0486fef (patch) | |
| tree | 5f32405c6aaca96a6f6d1af63368abc7dd892905 | |
| parent | c9e49d8ed2ced81fee1981dcdd1bcc0cdb26f194 (diff) | |
| parent | 78f2f2145a9ba22ab71d165f3d0635eed6e83014 (diff) | |
Merge "[SB Refactor] Update the wifi network flow to use stateIn so that new subscribers will receive the current value." into tm-qpr-dev
2 files changed, 44 insertions, 3 deletions
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/pipeline/wifi/data/repository/WifiRepository.kt b/packages/SystemUI/src/com/android/systemui/statusbar/pipeline/wifi/data/repository/WifiRepository.kt index 77654273bde4..103f3fc21f91 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/pipeline/wifi/data/repository/WifiRepository.kt +++ b/packages/SystemUI/src/com/android/systemui/statusbar/pipeline/wifi/data/repository/WifiRepository.kt @@ -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) { diff --git a/packages/SystemUI/tests/src/com/android/systemui/statusbar/pipeline/wifi/data/repository/WifiRepositoryImplTest.kt b/packages/SystemUI/tests/src/com/android/systemui/statusbar/pipeline/wifi/data/repository/WifiRepositoryImplTest.kt index 982927148a2e..d070ba0e47be 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/statusbar/pipeline/wifi/data/repository/WifiRepositoryImplTest.kt +++ b/packages/SystemUI/tests/src/com/android/systemui/statusbar/pipeline/wifi/data/repository/WifiRepositoryImplTest.kt @@ -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( |