diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/pipeline/mobile/data/repository/prod/MobileConnectionsRepositoryImpl.kt b/packages/SystemUI/src/com/android/systemui/statusbar/pipeline/mobile/data/repository/prod/MobileConnectionsRepositoryImpl.kt index a47f95d69c65b..74352d29cd9bb 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/pipeline/mobile/data/repository/prod/MobileConnectionsRepositoryImpl.kt +++ b/packages/SystemUI/src/com/android/systemui/statusbar/pipeline/mobile/data/repository/prod/MobileConnectionsRepositoryImpl.kt @@ -57,7 +57,6 @@ import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.asExecutor import kotlinx.coroutines.channels.awaitClose import kotlinx.coroutines.flow.Flow -import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.SharingStarted import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.combine @@ -205,9 +204,6 @@ constructor( } .stateIn(scope, SharingStarted.WhileSubscribed(), null) - private val defaultDataSubIdChangeEvent: MutableSharedFlow = - MutableSharedFlow(extraBufferCapacity = 1) - override val defaultDataSubId: StateFlow = broadcastDispatcher .broadcastFlow( @@ -223,7 +219,6 @@ constructor( initialValue = INVALID_SUBSCRIPTION_ID, ) .onStart { emit(subscriptionManagerProxy.getDefaultDataSubscriptionId()) } - .onEach { defaultDataSubIdChangeEvent.tryEmit(Unit) } .stateIn(scope, SharingStarted.WhileSubscribed(), INVALID_SUBSCRIPTION_ID) private val carrierConfigChangedEvent = @@ -232,7 +227,7 @@ constructor( .onEach { logger.logActionCarrierConfigChanged() } override val defaultDataSubRatConfig: StateFlow = - merge(defaultDataSubIdChangeEvent, carrierConfigChangedEvent) + merge(defaultDataSubId, carrierConfigChangedEvent) .onStart { emit(Unit) } .mapLatest { Config.readConfig(context) } .distinctUntilChanged() diff --git a/packages/SystemUI/tests/src/com/android/systemui/statusbar/pipeline/mobile/data/repository/CarrierConfigRepositoryTest.kt b/packages/SystemUI/tests/src/com/android/systemui/statusbar/pipeline/mobile/data/repository/CarrierConfigRepositoryTest.kt index 6e3af26121bfe..932c4a17cae27 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/statusbar/pipeline/mobile/data/repository/CarrierConfigRepositoryTest.kt +++ b/packages/SystemUI/tests/src/com/android/systemui/statusbar/pipeline/mobile/data/repository/CarrierConfigRepositoryTest.kt @@ -152,13 +152,11 @@ class CarrierConfigRepositoryTest : SysuiTestCase() { } private fun sendConfig(subId: Int) { - fakeBroadcastDispatcher.registeredReceivers.forEach { receiver -> - receiver.onReceive( - context, - Intent(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED) - .putExtra(CarrierConfigManager.EXTRA_SUBSCRIPTION_INDEX, subId) - ) - } + fakeBroadcastDispatcher.sendIntentToMatchingReceiversOnly( + context, + Intent(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED) + .putExtra(CarrierConfigManager.EXTRA_SUBSCRIPTION_INDEX, subId), + ) } companion object { diff --git a/packages/SystemUI/tests/src/com/android/systemui/statusbar/pipeline/mobile/data/repository/prod/MobileConnectionRepositoryTest.kt b/packages/SystemUI/tests/src/com/android/systemui/statusbar/pipeline/mobile/data/repository/prod/MobileConnectionRepositoryTest.kt index d1df6e3c20723..cf832b4ab0597 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/statusbar/pipeline/mobile/data/repository/prod/MobileConnectionRepositoryTest.kt +++ b/packages/SystemUI/tests/src/com/android/systemui/statusbar/pipeline/mobile/data/repository/prod/MobileConnectionRepositoryTest.kt @@ -379,9 +379,10 @@ class MobileConnectionRepositoryTest : SysuiTestCase() { var latest: Int? = null val job = underTest.carrierId.onEach { latest = it }.launchIn(this) - fakeBroadcastDispatcher.registeredReceivers.forEach { receiver -> - receiver.onReceive(context, carrierIdIntent(carrierId = 4321)) - } + fakeBroadcastDispatcher.sendIntentToMatchingReceiversOnly( + context, + carrierIdIntent(carrierId = 4321), + ) assertThat(latest).isEqualTo(4321) @@ -653,10 +654,7 @@ class MobileConnectionRepositoryTest : SysuiTestCase() { val job = underTest.networkName.onEach { latest = it }.launchIn(this) val intent = spnIntent() - - fakeBroadcastDispatcher.registeredReceivers.forEach { receiver -> - receiver.onReceive(context, intent) - } + fakeBroadcastDispatcher.sendIntentToMatchingReceiversOnly(context, intent) assertThat(latest).isEqualTo(intent.toNetworkNameModel(SEP)) @@ -670,17 +668,13 @@ class MobileConnectionRepositoryTest : SysuiTestCase() { val job = underTest.networkName.onEach { latest = it }.launchIn(this) val intent = spnIntent() - fakeBroadcastDispatcher.registeredReceivers.forEach { receiver -> - receiver.onReceive(context, intent) - } + fakeBroadcastDispatcher.sendIntentToMatchingReceiversOnly(context, intent) assertThat(latest).isEqualTo(intent.toNetworkNameModel(SEP)) // WHEN an intent with a different subId is sent val wrongSubIntent = spnIntent(subId = 101) - fakeBroadcastDispatcher.registeredReceivers.forEach { receiver -> - receiver.onReceive(context, wrongSubIntent) - } + fakeBroadcastDispatcher.sendIntentToMatchingReceiversOnly(context, wrongSubIntent) // THEN the previous intent's name is still used assertThat(latest).isEqualTo(intent.toNetworkNameModel(SEP)) @@ -695,9 +689,7 @@ class MobileConnectionRepositoryTest : SysuiTestCase() { val job = underTest.networkName.onEach { latest = it }.launchIn(this) val intent = spnIntent() - fakeBroadcastDispatcher.registeredReceivers.forEach { receiver -> - receiver.onReceive(context, intent) - } + fakeBroadcastDispatcher.sendIntentToMatchingReceiversOnly(context, intent) assertThat(latest).isEqualTo(intent.toNetworkNameModel(SEP)) val intentWithoutInfo = @@ -706,9 +698,7 @@ class MobileConnectionRepositoryTest : SysuiTestCase() { showPlmn = false, ) - fakeBroadcastDispatcher.registeredReceivers.forEach { receiver -> - receiver.onReceive(context, intentWithoutInfo) - } + fakeBroadcastDispatcher.sendIntentToMatchingReceiversOnly(context, intentWithoutInfo) assertThat(latest).isEqualTo(DEFAULT_NAME) diff --git a/packages/SystemUI/tests/src/com/android/systemui/statusbar/pipeline/mobile/data/repository/prod/MobileConnectionsRepositoryTest.kt b/packages/SystemUI/tests/src/com/android/systemui/statusbar/pipeline/mobile/data/repository/prod/MobileConnectionsRepositoryTest.kt index fd156d86c19eb..74bcdeec25cd5 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/statusbar/pipeline/mobile/data/repository/prod/MobileConnectionsRepositoryTest.kt +++ b/packages/SystemUI/tests/src/com/android/systemui/statusbar/pipeline/mobile/data/repository/prod/MobileConnectionsRepositoryTest.kt @@ -626,23 +626,17 @@ class MobileConnectionsRepositoryTest : SysuiTestCase() { assertThat(latest).isEqualTo(INVALID_SUBSCRIPTION_ID) - fakeBroadcastDispatcher.registeredReceivers.forEach { receiver -> - receiver.onReceive( - context, - Intent(TelephonyManager.ACTION_DEFAULT_DATA_SUBSCRIPTION_CHANGED) - .putExtra(PhoneConstants.SUBSCRIPTION_KEY, SUB_2_ID) - ) - } + val intent2 = + Intent(TelephonyManager.ACTION_DEFAULT_DATA_SUBSCRIPTION_CHANGED) + .putExtra(PhoneConstants.SUBSCRIPTION_KEY, SUB_2_ID) + fakeBroadcastDispatcher.sendIntentToMatchingReceiversOnly(context, intent2) assertThat(latest).isEqualTo(SUB_2_ID) - fakeBroadcastDispatcher.registeredReceivers.forEach { receiver -> - receiver.onReceive( - context, - Intent(TelephonyManager.ACTION_DEFAULT_DATA_SUBSCRIPTION_CHANGED) - .putExtra(PhoneConstants.SUBSCRIPTION_KEY, SUB_1_ID) - ) - } + val intent1 = + Intent(TelephonyManager.ACTION_DEFAULT_DATA_SUBSCRIPTION_CHANGED) + .putExtra(PhoneConstants.SUBSCRIPTION_KEY, SUB_1_ID) + fakeBroadcastDispatcher.sendIntentToMatchingReceiversOnly(context, intent1) assertThat(latest).isEqualTo(SUB_1_ID) } @@ -1074,13 +1068,10 @@ class MobileConnectionsRepositoryTest : SysuiTestCase() { assertThat(configFromContext.showAtLeast3G).isTrue() // WHEN the change event is fired - fakeBroadcastDispatcher.registeredReceivers.forEach { receiver -> - receiver.onReceive( - context, - Intent(TelephonyManager.ACTION_DEFAULT_DATA_SUBSCRIPTION_CHANGED) - .putExtra(PhoneConstants.SUBSCRIPTION_KEY, SUB_1_ID) - ) - } + val intent = + Intent(TelephonyManager.ACTION_DEFAULT_DATA_SUBSCRIPTION_CHANGED) + .putExtra(PhoneConstants.SUBSCRIPTION_KEY, SUB_1_ID) + fakeBroadcastDispatcher.sendIntentToMatchingReceiversOnly(context, intent) // THEN the config is updated assertTrue(latest!!.areEqual(configFromContext)) @@ -1099,12 +1090,10 @@ class MobileConnectionsRepositoryTest : SysuiTestCase() { assertThat(configFromContext.showAtLeast3G).isTrue() // WHEN the change event is fired - fakeBroadcastDispatcher.registeredReceivers.forEach { receiver -> - receiver.onReceive( - context, - Intent(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED) - ) - } + fakeBroadcastDispatcher.sendIntentToMatchingReceiversOnly( + context, + Intent(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED), + ) // THEN the config is updated assertThat(latest!!.areEqual(configFromContext)).isTrue() @@ -1123,12 +1112,10 @@ class MobileConnectionsRepositoryTest : SysuiTestCase() { assertThat(configFromContext.showAtLeast3G).isTrue() // WHEN the change event is fired - fakeBroadcastDispatcher.registeredReceivers.forEach { receiver -> - receiver.onReceive( - context, - Intent(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED) - ) - } + fakeBroadcastDispatcher.sendIntentToMatchingReceiversOnly( + context, + Intent(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED), + ) // WHEN collection starts AFTER the broadcast is sent out val latest by collectLastValue(underTest.defaultDataSubRatConfig) diff --git a/packages/SystemUI/tests/src/com/android/systemui/statusbar/pipeline/wifi/data/repository/prod/WifiRepositoryImplTest.kt b/packages/SystemUI/tests/src/com/android/systemui/statusbar/pipeline/wifi/data/repository/prod/WifiRepositoryImplTest.kt index dc68180d962d9..5ed3a5c7aa41c 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/statusbar/pipeline/wifi/data/repository/prod/WifiRepositoryImplTest.kt +++ b/packages/SystemUI/tests/src/com/android/systemui/statusbar/pipeline/wifi/data/repository/prod/WifiRepositoryImplTest.kt @@ -16,6 +16,7 @@ package com.android.systemui.statusbar.pipeline.wifi.data.repository.prod +import android.content.Intent import android.net.ConnectivityManager import android.net.Network import android.net.NetworkCapabilities @@ -33,7 +34,6 @@ import android.net.wifi.WifiManager.UNKNOWN_SSID import android.telephony.SubscriptionManager.INVALID_SUBSCRIPTION_ID import androidx.test.filters.SmallTest import com.android.systemui.SysuiTestCase -import com.android.systemui.broadcast.BroadcastDispatcher import com.android.systemui.log.table.TableLogBuffer import com.android.systemui.statusbar.pipeline.shared.data.model.ConnectivitySlots import com.android.systemui.statusbar.pipeline.shared.data.model.DataActivityModel @@ -46,13 +46,10 @@ import com.android.systemui.util.concurrency.FakeExecutor import com.android.systemui.util.mockito.any import com.android.systemui.util.mockito.argumentCaptor import com.android.systemui.util.mockito.mock -import com.android.systemui.util.mockito.nullable import com.android.systemui.util.time.FakeSystemClock import com.google.common.truth.Truth.assertThat import java.util.concurrent.Executor import kotlinx.coroutines.ExperimentalCoroutinesApi -import kotlinx.coroutines.flow.MutableSharedFlow -import kotlinx.coroutines.flow.flowOf import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.test.TestScope @@ -60,7 +57,6 @@ import kotlinx.coroutines.test.UnconfinedTestDispatcher import kotlinx.coroutines.test.runTest import org.junit.Before import org.junit.Test -import org.mockito.ArgumentMatchers.anyInt import org.mockito.Mock import org.mockito.Mockito.verify import org.mockito.Mockito.`when` as whenever @@ -73,7 +69,6 @@ class WifiRepositoryImplTest : SysuiTestCase() { private lateinit var underTest: WifiRepositoryImpl - @Mock private lateinit var broadcastDispatcher: BroadcastDispatcher @Mock private lateinit var logger: WifiInputLogger @Mock private lateinit var tableLogger: TableLogBuffer @Mock private lateinit var connectivityManager: ConnectivityManager @@ -86,15 +81,6 @@ class WifiRepositoryImplTest : SysuiTestCase() { @Before fun setUp() { MockitoAnnotations.initMocks(this) - whenever( - broadcastDispatcher.broadcastFlow( - any(), - nullable(), - anyInt(), - nullable(), - ) - ) - .thenReturn(flowOf(Unit)) executor = FakeExecutor(FakeSystemClock()) connectivityRepository = @@ -168,27 +154,23 @@ class WifiRepositoryImplTest : SysuiTestCase() { @Test fun isWifiEnabled_intentsReceived_valueUpdated() = testScope.runTest { - val intentFlow = MutableSharedFlow() - whenever( - broadcastDispatcher.broadcastFlow( - any(), - nullable(), - anyInt(), - nullable(), - ) - ) - .thenReturn(intentFlow) underTest = createRepo() val job = underTest.isWifiEnabled.launchIn(this) whenever(wifiManager.isWifiEnabled).thenReturn(true) - intentFlow.emit(Unit) + fakeBroadcastDispatcher.sendIntentToMatchingReceiversOnly( + context, + Intent(WifiManager.WIFI_STATE_CHANGED_ACTION), + ) assertThat(underTest.isWifiEnabled.value).isTrue() whenever(wifiManager.isWifiEnabled).thenReturn(false) - intentFlow.emit(Unit) + fakeBroadcastDispatcher.sendIntentToMatchingReceiversOnly( + context, + Intent(WifiManager.WIFI_STATE_CHANGED_ACTION), + ) assertThat(underTest.isWifiEnabled.value).isFalse() @@ -198,23 +180,16 @@ class WifiRepositoryImplTest : SysuiTestCase() { @Test fun isWifiEnabled_bothIntentAndNetworkUpdates_valueAlwaysUpdated() = testScope.runTest { - val intentFlow = MutableSharedFlow() - whenever( - broadcastDispatcher.broadcastFlow( - any(), - nullable(), - anyInt(), - nullable(), - ) - ) - .thenReturn(intentFlow) underTest = createRepo() val networkJob = underTest.wifiNetwork.launchIn(this) val enabledJob = underTest.isWifiEnabled.launchIn(this) whenever(wifiManager.isWifiEnabled).thenReturn(false) - intentFlow.emit(Unit) + fakeBroadcastDispatcher.sendIntentToMatchingReceiversOnly( + context, + Intent(WifiManager.WIFI_STATE_CHANGED_ACTION), + ) assertThat(underTest.isWifiEnabled.value).isFalse() whenever(wifiManager.isWifiEnabled).thenReturn(true) @@ -227,7 +202,10 @@ class WifiRepositoryImplTest : SysuiTestCase() { assertThat(underTest.isWifiEnabled.value).isFalse() whenever(wifiManager.isWifiEnabled).thenReturn(true) - intentFlow.emit(Unit) + fakeBroadcastDispatcher.sendIntentToMatchingReceiversOnly( + context, + Intent(WifiManager.WIFI_STATE_CHANGED_ACTION), + ) assertThat(underTest.isWifiEnabled.value).isTrue() networkJob.cancel() @@ -1317,7 +1295,7 @@ class WifiRepositoryImplTest : SysuiTestCase() { private fun createRepo(): WifiRepositoryImpl { return WifiRepositoryImpl( - broadcastDispatcher, + fakeBroadcastDispatcher, connectivityManager, connectivityRepository, logger, diff --git a/packages/SystemUI/tests/utils/src/com/android/systemui/broadcast/FakeBroadcastDispatcher.kt b/packages/SystemUI/tests/utils/src/com/android/systemui/broadcast/FakeBroadcastDispatcher.kt index af940e4fa687e..f19e19113b305 100644 --- a/packages/SystemUI/tests/utils/src/com/android/systemui/broadcast/FakeBroadcastDispatcher.kt +++ b/packages/SystemUI/tests/utils/src/com/android/systemui/broadcast/FakeBroadcastDispatcher.kt @@ -18,6 +18,7 @@ package com.android.systemui.broadcast import android.content.BroadcastReceiver import android.content.Context +import android.content.Intent import android.content.IntentFilter import android.os.Handler import android.os.Looper @@ -31,6 +32,14 @@ import java.lang.IllegalStateException import java.util.concurrent.ConcurrentHashMap import java.util.concurrent.Executor +/** + * A fake instance of [BroadcastDispatcher] for tests. + * + * Important: The *real* broadcast dispatcher will only send intents to receivers if the intent + * matches the [IntentFilter] that the [BroadcastReceiver] was registered with. This fake class does + * *not* do that matching by default. Use [sendIntentToMatchingReceiversOnly] to get the same + * matching behavior as the real broadcast dispatcher. + */ class FakeBroadcastDispatcher( context: SysuiTestableContext, mainExecutor: Executor, @@ -52,7 +61,10 @@ class FakeBroadcastDispatcher( PendingRemovalStore(logger) ) { - val registeredReceivers: MutableSet = ConcurrentHashMap.newKeySet() + private val receivers: MutableSet = ConcurrentHashMap.newKeySet() + + val registeredReceivers: Set + get() = receivers.map { it.receiver }.toSet() override fun registerReceiverWithHandler( receiver: BroadcastReceiver, @@ -62,7 +74,7 @@ class FakeBroadcastDispatcher( @Context.RegisterReceiverFlags flags: Int, permission: String? ) { - registeredReceivers.add(receiver) + receivers.add(InternalReceiver(receiver, filter)) } override fun registerReceiver( @@ -73,15 +85,34 @@ class FakeBroadcastDispatcher( @Context.RegisterReceiverFlags flags: Int, permission: String? ) { - registeredReceivers.add(receiver) + receivers.add(InternalReceiver(receiver, filter)) } override fun unregisterReceiver(receiver: BroadcastReceiver) { - registeredReceivers.remove(receiver) + receivers.removeIf { it.receiver == receiver } } override fun unregisterReceiverForUser(receiver: BroadcastReceiver, user: UserHandle) { - registeredReceivers.remove(receiver) + receivers.removeIf { it.receiver == receiver } + } + + /** + * Sends the given [intent] to *only* the receivers that were registered with an [IntentFilter] + * that matches the intent. + */ + fun sendIntentToMatchingReceiversOnly(context: Context, intent: Intent) { + receivers.forEach { + if ( + it.filter.match( + context.contentResolver, + intent, + /* resolve= */ false, + /* logTag= */ "FakeBroadcastDispatcher", + ) > 0 + ) { + it.receiver.onReceive(context, intent) + } + } } fun cleanUpReceivers(testName: String) { @@ -91,6 +122,11 @@ class FakeBroadcastDispatcher( throw IllegalStateException("Receiver not unregistered from dispatcher: $it") } } - registeredReceivers.clear() + receivers.clear() } + + private data class InternalReceiver( + val receiver: BroadcastReceiver, + val filter: IntentFilter, + ) }