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

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

Merge changes Ibbe1b0b1,I007751b4 into udc-qpr-dev

* changes:
  [Status Bar] Use FakeBroadcastDispatcher.sendToMatch in pipeline tests.
  [Status Bar] Use `defaultDataSubId` flow to trigger config re-fetch.
parents 1f29987f 5a99b795
Loading
Loading
Loading
Loading
+1 −6
Original line number Diff line number Diff line
@@ -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<Unit> =
        MutableSharedFlow(extraBufferCapacity = 1)

    override val defaultDataSubId: StateFlow<Int> =
        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<Config> =
        merge(defaultDataSubIdChangeEvent, carrierConfigChangedEvent)
        merge(defaultDataSubId, carrierConfigChangedEvent)
            .onStart { emit(Unit) }
            .mapLatest { Config.readConfig(context) }
            .distinctUntilChanged()
+5 −7
Original line number Diff line number Diff line
@@ -152,14 +152,12 @@ class CarrierConfigRepositoryTest : SysuiTestCase() {
        }

    private fun sendConfig(subId: Int) {
        fakeBroadcastDispatcher.registeredReceivers.forEach { receiver ->
            receiver.onReceive(
        fakeBroadcastDispatcher.sendIntentToMatchingReceiversOnly(
            context,
            Intent(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED)
                    .putExtra(CarrierConfigManager.EXTRA_SUBSCRIPTION_INDEX, subId)
                .putExtra(CarrierConfigManager.EXTRA_SUBSCRIPTION_INDEX, subId),
        )
    }
    }

    companion object {
        private const val SUB_ID_1 = 1
+9 −19
Original line number Diff line number Diff line
@@ -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)

+20 −33
Original line number Diff line number Diff line
@@ -626,23 +626,17 @@ class MobileConnectionsRepositoryTest : SysuiTestCase() {

            assertThat(latest).isEqualTo(INVALID_SUBSCRIPTION_ID)

            fakeBroadcastDispatcher.registeredReceivers.forEach { receiver ->
                receiver.onReceive(
                    context,
            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,
            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,
            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(
            fakeBroadcastDispatcher.sendIntentToMatchingReceiversOnly(
                context,
                    Intent(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED)
                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(
            fakeBroadcastDispatcher.sendIntentToMatchingReceiversOnly(
                context,
                    Intent(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED)
                Intent(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED),
            )
            }

            // WHEN collection starts AFTER the broadcast is sent out
            val latest by collectLastValue(underTest.defaultDataSubRatConfig)
+18 −40
Original line number Diff line number Diff line
@@ -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<Unit>()
            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<Unit>()
            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,
Loading