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

Commit 5d1dac91 authored by Ioana Alexandru's avatar Ioana Alexandru
Browse files

Fix ZenModeRepositoryTest failures

One issue was that the arg captor wasn't accounting for the extra params
we added in ag/28185825. The second issue was that @DisableFlags wasn't
working correctly, so we need to use lazy initialization (b/352296216).

Bug: 352289773
Test: ZenModeRepositoryTest
Flag: EXEMPT trivial change + test-only change
Change-Id: Ibfcf370aea71c94bcacac7399f68f065c7ca6a03
parent d4a7ac67
Loading
Loading
Loading
Loading
+6 −3
Original line number Diff line number Diff line
@@ -56,7 +56,7 @@ class ZenModeRepositoryImpl(
    val backgroundHandler: Handler?,
) : ZenModeRepository {

    private val notificationBroadcasts =
    private val notificationBroadcasts by lazy {
        callbackFlow {
                val receiver =
                    object : BroadcastReceiver() {
@@ -95,8 +95,9 @@ class ZenModeRepositoryImpl(
                    )
                }
            }
    }

    override val consolidatedNotificationPolicy: StateFlow<NotificationManager.Policy?> =
    override val consolidatedNotificationPolicy: StateFlow<NotificationManager.Policy?> by lazy {
        if (Flags.volumePanelBroadcastFix() && android.app.Flags.modesApi())
            flowFromBroadcast(NotificationManager.ACTION_CONSOLIDATED_NOTIFICATION_POLICY_CHANGED) {
                notificationManager.consolidatedNotificationPolicy
@@ -105,11 +106,13 @@ class ZenModeRepositoryImpl(
            flowFromBroadcast(NotificationManager.ACTION_NOTIFICATION_POLICY_CHANGED) {
                notificationManager.consolidatedNotificationPolicy
            }
    }

    override val globalZenMode: StateFlow<Int?> =
    override val globalZenMode: StateFlow<Int?> by lazy {
        flowFromBroadcast(NotificationManager.ACTION_INTERRUPTION_FILTER_CHANGED) {
            notificationManager.zenMode
        }
    }

    private fun <T> flowFromBroadcast(intentAction: String, mapper: () -> T) =
        notificationBroadcasts
+5 −11
Original line number Diff line number Diff line
@@ -48,7 +48,6 @@ import org.robolectric.RobolectricTestRunner
@RunWith(RobolectricTestRunner::class)
@SmallTest
class ZenModeRepositoryTest {

    @Mock private lateinit var context: Context

    @Mock private lateinit var notificationManager: NotificationManager
@@ -73,7 +72,7 @@ class ZenModeRepositoryTest {
            )
    }

    @DisableFlags(android.app.Flags.FLAG_MODES_API, Flags.FLAG_VOLUME_PANEL_BROADCAST_FIX)
    @DisableFlags(Flags.FLAG_VOLUME_PANEL_BROADCAST_FIX)
    @Test
    fun consolidatedPolicyChanges_repositoryEmits_flagsOff() {
        testScope.runTest {
@@ -88,9 +87,7 @@ class ZenModeRepositoryTest {
            triggerIntent(NotificationManager.ACTION_NOTIFICATION_POLICY_CHANGED)
            runCurrent()

            assertThat(values)
                .containsExactlyElementsIn(listOf(null, testPolicy1, testPolicy2))
                .inOrder()
            assertThat(values).containsExactly(null, testPolicy1, testPolicy2).inOrder()
        }
    }

@@ -109,9 +106,7 @@ class ZenModeRepositoryTest {
            triggerIntent(NotificationManager.ACTION_CONSOLIDATED_NOTIFICATION_POLICY_CHANGED)
            runCurrent()

            assertThat(values)
                .containsExactlyElementsIn(listOf(null, testPolicy1, testPolicy2))
                .inOrder()
            assertThat(values).containsExactly(null, testPolicy1, testPolicy2).inOrder()
        }
    }

@@ -128,14 +123,13 @@ class ZenModeRepositoryTest {
            runCurrent()

            assertThat(values)
                .containsExactlyElementsIn(
                    listOf(null, Global.ZEN_MODE_OFF, Global.ZEN_MODE_ALARMS))
                .containsExactly(null, Global.ZEN_MODE_OFF, Global.ZEN_MODE_ALARMS)
                .inOrder()
        }
    }

    private fun triggerIntent(action: String) {
        verify(context).registerReceiver(receiverCaptor.capture(), any())
        verify(context).registerReceiver(receiverCaptor.capture(), any(), any(), any())
        receiverCaptor.value.onReceive(context, Intent(action))
    }