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

Commit cc8b372e authored by Ankit Shetgaonkar's avatar Ankit Shetgaonkar Committed by Android (Google) Code Review
Browse files

Merge "Use ProcessWrapper to avoid static mocking" into main

parents a21cce9a 3191405e
Loading
Loading
Loading
Loading
+4 −3
Original line number Diff line number Diff line
@@ -61,7 +61,6 @@ import android.os.IBinder;
import android.os.IRemoteCallback;
import android.os.Looper;
import android.os.PatternMatcher;
import android.os.Process;
import android.os.RemoteException;
import android.os.SystemClock;
import android.os.UserHandle;
@@ -100,6 +99,7 @@ import com.android.systemui.navigationbar.NavigationModeController;
import com.android.systemui.navigationbar.views.NavigationBar;
import com.android.systemui.navigationbar.views.NavigationBarView;
import com.android.systemui.navigationbar.views.buttons.KeyButtonView;
import com.android.systemui.process.ProcessWrapper;
import com.android.systemui.recents.OverviewProxyService.OverviewProxyListener;
import com.android.systemui.scene.domain.interactor.SceneInteractor;
import com.android.systemui.scene.shared.flag.SceneContainerFlag;
@@ -675,11 +675,12 @@ public class OverviewProxyService implements CallbackController<OverviewProxyLis
            DumpManager dumpManager,
            Optional<UnfoldTransitionProgressForwarder> unfoldTransitionProgressForwarder,
            BroadcastDispatcher broadcastDispatcher,
            Optional<BackAnimation> backAnimation
            Optional<BackAnimation> backAnimation,
            ProcessWrapper processWrapper
    ) {
        // b/241601880: This component should only be running for primary users or
        // secondaryUsers when visibleBackgroundUsers are supported.
        boolean isSystemUser = Process.myUserHandle().equals(UserHandle.SYSTEM);
        boolean isSystemUser = processWrapper.isSystemUser();
        boolean isVisibleBackgroundUser =
                userManager.isVisibleBackgroundUsersSupported() && !userManager.isUserForeground();
        if (!isSystemUser && isVisibleBackgroundUser) {
+31 −53
Original line number Diff line number Diff line
@@ -21,14 +21,11 @@ import android.content.Context
import android.content.pm.PackageManager
import android.content.pm.ResolveInfo
import android.os.PowerManager
import android.os.Process
import android.os.UserHandle
import android.os.UserManager
import android.testing.TestableContext
import android.testing.TestableLooper
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.android.dx.mockito.inline.extended.ExtendedMockito
import com.android.internal.app.AssistUtils
import com.android.internal.logging.UiEventLogger
import com.android.systemui.SysuiTestCase
@@ -42,6 +39,7 @@ import com.android.systemui.model.SysUiState
import com.android.systemui.model.sceneContainerPlugin
import com.android.systemui.navigationbar.NavigationBarController
import com.android.systemui.navigationbar.NavigationModeController
import com.android.systemui.process.ProcessWrapper
import com.android.systemui.recents.OverviewProxyService.ACTION_QUICKSTEP
import com.android.systemui.settings.FakeDisplayTracker
import com.android.systemui.settings.UserTracker
@@ -92,6 +90,7 @@ class OverviewProxyServiceTest : SysuiTestCase() {
    private val kosmos = testKosmos()
    private lateinit var subject: OverviewProxyService
    @Mock private val dumpManager = DumpManager()
    @Mock private val processWrapper = ProcessWrapper()
    private val displayTracker = FakeDisplayTracker(mContext)
    private val fakeSystemClock = FakeSystemClock()
    private val sysUiState = SysUiState(displayTracker, kosmos.sceneContainerPlugin)
@@ -146,6 +145,8 @@ class OverviewProxyServiceTest : SysuiTestCase() {

        mSetFlagsRule.disableFlags(com.android.systemui.Flags.FLAG_KEYGUARD_WM_STATE_REFACTOR)

        // return isSystemUser as true by default.
        `when`(processWrapper.isSystemUser).thenReturn(true)
        subject = createOverviewProxyService(context)
    }

@@ -202,68 +203,44 @@ class OverviewProxyServiceTest : SysuiTestCase() {

    @Test
    fun connectToOverviewService_primaryUserNoVisibleBgUsersSupported_expectBindService() {
        val mockitoSession =
            ExtendedMockito.mockitoSession().spyStatic(Process::class.java).startMocking()
        try {
            `when`(Process.myUserHandle()).thenReturn(UserHandle.SYSTEM)
        `when`(processWrapper.isSystemUser).thenReturn(true)
        `when`(userManager.isVisibleBackgroundUsersSupported()).thenReturn(false)
        val spyContext = spy(context)
        val ops = createOverviewProxyService(spyContext)
        ops.startConnectionToCurrentUser()
        verify(spyContext, atLeast(1)).bindServiceAsUser(any(), any(), anyInt(), any())
        } finally {
            mockitoSession.finishMocking()
        }
    }

    @Test
    fun connectToOverviewService_nonPrimaryUserNoVisibleBgUsersSupported_expectNoBindService() {
        val mockitoSession =
            ExtendedMockito.mockitoSession().spyStatic(Process::class.java).startMocking()
        try {
            `when`(Process.myUserHandle()).thenReturn(UserHandle.of(12345))
        `when`(processWrapper.isSystemUser).thenReturn(false)
        `when`(userManager.isVisibleBackgroundUsersSupported()).thenReturn(false)
        val spyContext = spy(context)
        val ops = createOverviewProxyService(spyContext)
        ops.startConnectionToCurrentUser()
        verify(spyContext, times(0)).bindServiceAsUser(any(), any(), anyInt(), any())
        } finally {
            mockitoSession.finishMocking()
        }
    }

    @Test
    fun connectToOverviewService_nonPrimaryBgUserVisibleBgUsersSupported_expectBindService() {
        val mockitoSession =
            ExtendedMockito.mockitoSession().spyStatic(Process::class.java).startMocking()
        try {
            `when`(Process.myUserHandle()).thenReturn(UserHandle.of(12345))
        `when`(processWrapper.isSystemUser).thenReturn(false)
        `when`(userManager.isVisibleBackgroundUsersSupported()).thenReturn(true)
        `when`(userManager.isUserForeground()).thenReturn(false)
        val spyContext = spy(context)
        val ops = createOverviewProxyService(spyContext)
        ops.startConnectionToCurrentUser()
        verify(spyContext, atLeast(1)).bindServiceAsUser(any(), any(), anyInt(), any())
        } finally {
            mockitoSession.finishMocking()
        }
    }

    @Test
    fun connectToOverviewService_nonPrimaryFgUserVisibleBgUsersSupported_expectNoBindService() {
        val mockitoSession =
            ExtendedMockito.mockitoSession().spyStatic(Process::class.java).startMocking()
        try {
            `when`(Process.myUserHandle()).thenReturn(UserHandle.of(12345))
        `when`(processWrapper.isSystemUser).thenReturn(false)
        `when`(userManager.isVisibleBackgroundUsersSupported()).thenReturn(true)
        `when`(userManager.isUserForeground()).thenReturn(true)
        val spyContext = spy(context)
        val ops = createOverviewProxyService(spyContext)
        ops.startConnectionToCurrentUser()
        verify(spyContext, times(0)).bindServiceAsUser(any(), any(), anyInt(), any())
        } finally {
            mockitoSession.finishMocking()
        }
    }

    private fun createOverviewProxyService(ctx: Context): OverviewProxyService {
@@ -292,6 +269,7 @@ class OverviewProxyServiceTest : SysuiTestCase() {
            unfoldTransitionProgressForwarder,
            broadcastDispatcher,
            backAnimation,
            processWrapper,
        )
    }
}