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

Commit ed5d295c authored by Josh's avatar Josh
Browse files

Refactored PhoneStatusBarView to remove Dependency.get()

Dependency.get() in the view constructor leads to faulty test behaviour
as unrelated dagger dependencies are getting instantiated.

This is also a legacy way of handling dependency injections in general.

The Logic related to statusBarWindowControllerStore has been moved from
inside PhoneStatusBarView to PhoneStatusBarViewController with the store
injected into the constructor by dagger, removing the need for
Dependency.get and also allowing Test environments to inject their
version of statusBarWindowControllerStore

Flag: EXEMPT Refactor
Test: PhoneStatusBarViewControllerTest, PhoneStatusBarViewTest
Fix: 442337871
Change-Id: I18d995f89b59560fc2429843de55cdc853977c73
parent ccfd479b
Loading
Loading
Loading
Loading
+29 −5
Original line number Diff line number Diff line
@@ -37,7 +37,6 @@ import android.window.DesktopExperienceFlags;
import androidx.annotation.NonNull;

import com.android.internal.policy.SystemBarUtils;
import com.android.systemui.Dependency;
import com.android.systemui.Gefingerpoken;
import com.android.systemui.res.R;
import com.android.systemui.shade.ShadeExpandsOnStatusBarLongPress;
@@ -54,8 +53,9 @@ import java.util.function.BooleanSupplier;

public class PhoneStatusBarView extends FrameLayout {
    private static final String TAG = "PhoneStatusBarView";
    private final StatusBarWindowControllerStore mStatusBarWindowControllerStore;

    private StatusBarWindowControllerStore mStatusBarWindowControllerStore;
    private boolean mShouldUpdateStatusBarHeightWhenControllerSet = false;
    private int mRotationOrientation = -1;
    @Nullable
    private View mCutoutSpace;
@@ -84,7 +84,6 @@ public class PhoneStatusBarView extends FrameLayout {

    public PhoneStatusBarView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mStatusBarWindowControllerStore = Dependency.get(StatusBarWindowControllerStore.class);
    }

    void setLongPressGestureDetector(
@@ -281,6 +280,7 @@ public class PhoneStatusBarView extends FrameLayout {
    public boolean onInterceptTouchEvent(MotionEvent event) {
        return mTouchEventHandler.onInterceptTouchEvent(event);
    }

    public void updateResources() {
        mCutoutSideNudge = getResources().getDimensionPixelSize(
                R.dimen.display_cutout_margin_consumption);
@@ -288,6 +288,25 @@ public class PhoneStatusBarView extends FrameLayout {
        updateStatusBarHeight();
    }

    /**
     * Sets the store responsible for managing the status bar window controller.
     *
     * <p>This setter is used to facilitate dependency injection for the
     * {@link PhoneStatusBarViewController}, which receives the store via Dagger. This avoids
     * using the legacy {@link com.android.systemui.Dependency} pattern directly in the constructor.
     *
     * @param statusBarWindowControllerStore The {@link StatusBarWindowControllerStore} instance
     * to set
     */
    public void setStatusBarWindowControllerStore(
            StatusBarWindowControllerStore statusBarWindowControllerStore) {
        mStatusBarWindowControllerStore = statusBarWindowControllerStore;
        if (mShouldUpdateStatusBarHeightWhenControllerSet) {
            mShouldUpdateStatusBarHeightWhenControllerSet = false;
            updateWindowHeight();
        }
    }

    private void updateStatusBarHeight() {
        final int waterfallTopInset =
                mDisplayCutout == null ? 0 : mDisplayCutout.getWaterfallInsets().top;
@@ -386,7 +405,12 @@ public class PhoneStatusBarView extends FrameLayout {
            // Handled directly from StatusBarWindowControllerImpl (for each display)
            return;
        }
        if (mStatusBarWindowControllerStore != null) {
            mStatusBarWindowControllerStore.getDefaultDisplay().refreshStatusBarHeight();
        } else {
            Log.e(TAG, "mStatusBarWindowControllerStore unexpectedly null");
            mShouldUpdateStatusBarHeightWhenControllerSet = true;
        }
    }

    interface HasCornerCutoutFetcher {
+7 −0
Original line number Diff line number Diff line
@@ -46,6 +46,7 @@ import com.android.systemui.statusbar.data.repository.StatusBarConfigurationCont
import com.android.systemui.statusbar.data.repository.StatusBarContentInsetsProviderStore
import com.android.systemui.statusbar.policy.Clock
import com.android.systemui.statusbar.policy.ConfigurationController
import com.android.systemui.statusbar.window.StatusBarWindowControllerStore
import com.android.systemui.statusbar.window.StatusBarWindowStateController
import com.android.systemui.unfold.UNFOLD_STATUS_BAR
import com.android.systemui.unfold.util.ScopedUnfoldTransitionProgressProvider
@@ -83,6 +84,7 @@ private constructor(
    private val statusBarContentInsetsProviderStore: StatusBarContentInsetsProviderStore,
    private val lazyStatusBarShadeDisplayPolicy: Lazy<StatusBarTouchShadeDisplayPolicy>,
    private val lazyShadeDisplaysRepository: Lazy<ShadeDisplaysRepository>,
    private val statusBarWindowControllerStore: StatusBarWindowControllerStore,
) : ViewController<PhoneStatusBarView>(view) {

    private lateinit var battery: BatteryMeterView
@@ -196,6 +198,9 @@ private constructor(
            // the clock handles the config change itself.
            configurationController.addCallback(configurationListener)
        }
        if (!StatusBarConnectedDisplays.isEnabled) {
            mView.setStatusBarWindowControllerStore(statusBarWindowControllerStore)
        }
    }

    private fun addCursorSupportToIconContainers() {
@@ -441,6 +446,7 @@ private constructor(
        private val statusBarContentInsetsProviderStore: StatusBarContentInsetsProviderStore,
        private val lazyStatusBarShadeDisplayPolicy: Lazy<StatusBarTouchShadeDisplayPolicy>,
        private val lazyShadeDisplaysRepository: Lazy<ShadeDisplaysRepository>,
        private val statusBarWindowControllerStore: StatusBarWindowControllerStore,
    ) {
        fun create(view: PhoneStatusBarView): PhoneStatusBarViewController {
            return PhoneStatusBarViewController(
@@ -463,6 +469,7 @@ private constructor(
                statusBarContentInsetsProviderStore,
                lazyStatusBarShadeDisplayPolicy,
                lazyShadeDisplaysRepository,
                statusBarWindowControllerStore,
            )
        }
    }
+23 −0
Original line number Diff line number Diff line
@@ -71,6 +71,7 @@ import com.android.systemui.statusbar.core.StatusBarConnectedDisplays
import com.android.systemui.statusbar.data.repository.fakeStatusBarContentInsetsProviderStore
import com.android.systemui.statusbar.policy.Clock
import com.android.systemui.statusbar.policy.mockStatusBarConfigurationController
import com.android.systemui.statusbar.window.StatusBarWindowControllerStore
import com.android.systemui.statusbar.window.StatusBarWindowStateController
import com.android.systemui.testKosmos
import com.android.systemui.unfold.util.ScopedUnfoldTransitionProgressProvider
@@ -129,6 +130,7 @@ class PhoneStatusBarViewControllerTest(flags: FlagsParameterization) : SysuiTest
    @Mock private lateinit var mStatusBarLongPressGestureDetector: StatusBarLongPressGestureDetector
    @Mock private lateinit var statusBarTouchShadeDisplayPolicy: StatusBarTouchShadeDisplayPolicy
    @Mock private lateinit var shadeDisplayRepository: ShadeDisplaysRepository
    @Mock private lateinit var statusBarWindowControllerStore: StatusBarWindowControllerStore
    private lateinit var statusBarWindowStateController: StatusBarWindowStateController
    private lateinit var view: PhoneStatusBarView
    private lateinit var controller: PhoneStatusBarViewController
@@ -848,6 +850,26 @@ class PhoneStatusBarViewControllerTest(flags: FlagsParameterization) : SysuiTest
        verify(shadeControllerImpl, never()).animateExpandShade()
    }

    @Test
    @DisableFlags(StatusBarConnectedDisplays.FLAG_NAME)
    fun connectedDisplayFlagOff_windowControllerIsSetInView() {
        attachToWindow(view)

        controller = createAndInitController(view)

        verify(view).setStatusBarWindowControllerStore(statusBarWindowControllerStore)
    }

    @Test
    @EnableFlags(StatusBarConnectedDisplays.FLAG_NAME)
    fun connectedDisplayFlagOn_windowControllerIsSetInView() {
        attachToWindow(view)

        controller = createAndInitController(view)

        verify(view, never()).setStatusBarWindowControllerStore(statusBarWindowControllerStore)
    }

    private fun getCommandQueueCallback(): CommandQueue.Callbacks {
        val captor = argumentCaptor<CommandQueue.Callbacks>()
        verify(commandQueue).addCallback(captor.capture())
@@ -892,6 +914,7 @@ class PhoneStatusBarViewControllerTest(flags: FlagsParameterization) : SysuiTest
                statusBarContentInsetsProviderStore,
                { statusBarTouchShadeDisplayPolicy },
                { shadeDisplayRepository },
                statusBarWindowControllerStore,
            )
            .create(view)
            .also { it.init() }
+33 −4
Original line number Diff line number Diff line
@@ -80,10 +80,6 @@ class PhoneStatusBarViewTest : SysuiTestCase() {
    fun setUp() {
        MockitoAnnotations.initMocks(this)
        whenever(windowControllerStore.defaultDisplay).thenReturn(windowController)
        mDependency.injectTestDependency(
            StatusBarWindowControllerStore::class.java,
            windowControllerStore,
        )
        context.ensureTestableResources()
        view = spy(createStatusBarView(context))
        whenever(view.rootWindowInsets).thenReturn(emptyWindowInsets())
@@ -256,14 +252,43 @@ class PhoneStatusBarViewTest : SysuiTestCase() {
    @Test
    @DisableFlags(FLAG_STATUS_BAR_CONNECTED_DISPLAYS)
    fun onAttachedToWindow_connectedDisplayFlagOff_updatesWindowHeight() {
        view.setStatusBarWindowControllerStore(windowControllerStore)

        view.onAttachedToWindow()

        verify(windowController).refreshStatusBarHeight()
    }

    @Test
    @DisableFlags(FLAG_STATUS_BAR_CONNECTED_DISPLAYS)
    fun onAttachedToWindow_connectedDisplayFlagOff_updatesWindowHeightAfterControllerStoreSet() {
        // windowControllerStore is not yet set in the view
        view.onAttachedToWindow()

        view.setStatusBarWindowControllerStore(windowControllerStore)

        verify(windowController).refreshStatusBarHeight()
    }

    @Test
    @DisableFlags(FLAG_STATUS_BAR_CONNECTED_DISPLAYS)
    fun onAttachedToWindow_connectedDisplayFlagOff_updatesWindowHeightOnceAfterControllerStoreSet() {
        // windowControllerStore is not yet set in the view
        view.onAttachedToWindow()

        view.setStatusBarWindowControllerStore(windowControllerStore)
        view.setStatusBarWindowControllerStore(windowControllerStore)
        view.setStatusBarWindowControllerStore(windowControllerStore)
        view.setStatusBarWindowControllerStore(windowControllerStore)

        verify(windowController, times(1)).refreshStatusBarHeight()
    }

    @Test
    @EnableFlags(FLAG_STATUS_BAR_CONNECTED_DISPLAYS)
    fun onAttachedToWindow_connectedDisplayFlagOn_doesNotUpdateWindowHeight() {
        view.setStatusBarWindowControllerStore(windowControllerStore)

        view.onAttachedToWindow()

        verify(windowController, never()).refreshStatusBarHeight()
@@ -272,6 +297,8 @@ class PhoneStatusBarViewTest : SysuiTestCase() {
    @Test
    @DisableFlags(FLAG_STATUS_BAR_CONNECTED_DISPLAYS)
    fun onConfigurationChanged_connectedDisplayFlagOff_updatesWindowHeight() {
        view.setStatusBarWindowControllerStore(windowControllerStore)

        view.onConfigurationChanged(Configuration())
        view.onConfigurationChanged(Configuration())
        view.onConfigurationChanged(Configuration())
@@ -283,6 +310,8 @@ class PhoneStatusBarViewTest : SysuiTestCase() {
    @Test
    @EnableFlags(FLAG_STATUS_BAR_CONNECTED_DISPLAYS)
    fun onConfigurationChanged_connectedDisplayFlagOn_neverUpdatesWindowHeight() {
        view.setStatusBarWindowControllerStore(windowControllerStore)

        view.onConfigurationChanged(Configuration())
        view.onConfigurationChanged(Configuration())
        view.onConfigurationChanged(Configuration())