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

Commit 49cefa88 authored by Olivier St-Onge's avatar Olivier St-Onge
Browse files

Reapply "Finish BrightnessDialog when shade is opened."

This reverts commit b1dbb465.
The test flakiness observed with the original commit is fixed by using
Flow to track the state of the activity instead of a boolean.

Test: BrightnessDialogTest#testFinishOnQSExpanded --rerun-until-failure 20
Flag: None
Fixes: 243630115
Change-Id: I7574a0ec1bf09e589049fc1c659b2279a4a42336
parent 99988dcd
Loading
Loading
Loading
Loading
+13 −0
Original line number Diff line number Diff line
@@ -21,6 +21,8 @@ import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
import static android.view.WindowManagerPolicyConstants.EXTRA_FROM_BRIGHTNESS_KEY;

import static com.android.systemui.util.kotlin.JavaAdapterKt.collectFlow;

import android.app.Activity;
import android.content.res.Configuration;
import android.graphics.Rect;
@@ -87,6 +89,17 @@ public class BrightnessDialog extends Activity {
        if (mShadeInteractor.isQsExpanded().getValue()) {
            finish();
        }

        View view = findViewById(R.id.brightness_mirror_container);
        if (view != null) {
            collectFlow(view, mShadeInteractor.isQsExpanded(), this::onShadeStateChange);
        }
    }

    private void onShadeStateChange(boolean isQsExpanded) {
        if (isQsExpanded) {
            requestFinish();
        }
    }

    private void setWindowAttributes() {
+24 −6
Original line number Diff line number Diff line
@@ -36,8 +36,12 @@ import com.android.systemui.util.mockito.any
import com.android.systemui.util.mockito.whenever
import com.android.systemui.util.time.FakeSystemClock
import com.google.common.truth.Truth.assertThat
import dagger.Lazy
import kotlin.time.Duration.Companion.milliseconds
import kotlinx.coroutines.FlowPreview
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.takeWhile
import kotlinx.coroutines.flow.timeout
import kotlinx.coroutines.test.runTest
import org.junit.After
import org.junit.Before
import org.junit.Rule
@@ -59,7 +63,6 @@ class BrightnessDialogTest : SysuiTestCase() {
    @Mock private lateinit var brightnessControllerFactory: BrightnessController.Factory
    @Mock private lateinit var brightnessController: BrightnessController
    @Mock private lateinit var accessibilityMgr: AccessibilityManagerWrapper
    @Mock private lateinit var shadeInteractorLazy: Lazy<ShadeInteractor>
    @Mock private lateinit var shadeInteractor: ShadeInteractor

    private val clock = FakeSystemClock()
@@ -89,7 +92,6 @@ class BrightnessDialogTest : SysuiTestCase() {
            .thenReturn(brightnessSliderController)
        `when`(brightnessSliderController.rootView).thenReturn(View(context))
        `when`(brightnessControllerFactory.create(any())).thenReturn(brightnessController)
        whenever(shadeInteractorLazy.get()).thenReturn(shadeInteractor)
        whenever(shadeInteractor.isQsExpanded).thenReturn(MutableStateFlow(false))
    }

@@ -180,6 +182,22 @@ class BrightnessDialogTest : SysuiTestCase() {
        assertThat(activityRule.activity.isFinishing()).isFalse()
    }

    @OptIn(FlowPreview::class)
    @Test
    fun testFinishOnQSExpanded() = runTest {
        val isQSExpanded = MutableStateFlow(false)
        `when`(shadeInteractor.isQsExpanded).thenReturn(isQSExpanded)
        activityRule.launchActivity(Intent(Intent.ACTION_SHOW_BRIGHTNESS_DIALOG))

        assertThat(activityRule.activity.isFinishing()).isFalse()

        isQSExpanded.value = true
        // Observe the activity's state until is it finishing or the timeout is reached, whatever
        // comes first. This fixes the flakiness seen when using advanceUntilIdle().
        activityRule.activity.finishing.timeout(100.milliseconds).takeWhile { !it }.collect {}
        assertThat(activityRule.activity.isFinishing()).isTrue()
    }

    class TestDialog(
        brightnessSliderControllerFactory: BrightnessSliderController.Factory,
        brightnessControllerFactory: BrightnessController.Factory,
@@ -194,14 +212,14 @@ class BrightnessDialogTest : SysuiTestCase() {
            accessibilityMgr,
            shadeInteractor
        ) {
        private var finishing = false
        var finishing = MutableStateFlow(false)

        override fun isFinishing(): Boolean {
            return finishing
            return finishing.value
        }

        override fun requestFinish() {
            finishing = true
            finishing.value = true
        }
    }
}