From 5a99b795dada9ea9c833f3330c0925721f246459 Mon Sep 17 00:00:00 2001 From: Caitlin Shkuratov Date: Thu, 22 Jun 2023 14:39:03 +0000 Subject: [PATCH] [Status Bar] Use FakeBroadcastDispatcher.sendToMatch in pipeline tests. Bug: 285298304 Test: atest MobileConnectionsRepositoryTest MobileConnectionRepositoryTest WifiRepositoryImplTest CarrierConfigRepositoryTest Change-Id: Ibbe1b0b10bdc6beec1d2106fb6afdb73408a5374 --- .../repository/CarrierConfigRepositoryTest.kt | 12 ++-- .../prod/MobileConnectionRepositoryTest.kt | 28 +++------ .../prod/MobileConnectionsRepositoryTest.kt | 42 +++++--------- .../repository/prod/WifiRepositoryImplTest.kt | 58 ++++++------------- 4 files changed, 48 insertions(+), 92 deletions(-) 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 9aea70f9cdb25..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) } @@ -1096,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() @@ -1120,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,