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

Commit 3fa7e2c7 authored by Kurt Dresner's avatar Kurt Dresner
Browse files

Add tests to make sure that we don't allow setting/clearing of car mode for other packages.

Bug:194402848
Change-Id: I7a027acaafc80eefd125fc197c51845dceba4c9a
parent c16349a6
Loading
Loading
Loading
Loading
+29 −0
Original line number Diff line number Diff line
@@ -1220,6 +1220,35 @@ public class UiModeManagerServiceTest extends UiServiceTestCase {
        verify(listener, never()).onProjectionStateChanged(anyInt(), any());
    }

    @Test
    public void enableCarMode_failsForBogusPackageName() throws Exception {
        when(mPackageManager.getPackageUidAsUser(eq(PACKAGE_NAME), anyInt()))
            .thenReturn(TestInjector.CALLING_UID + 1);

        assertThrows(SecurityException.class, () -> mService.enableCarMode(0, 0, PACKAGE_NAME));
        assertThat(mService.getCurrentModeType()).isNotEqualTo(Configuration.UI_MODE_TYPE_CAR);
    }

    @Test
    public void disableCarMode_failsForBogusPackageName() throws Exception {
        when(mPackageManager.getPackageUidAsUser(eq(PACKAGE_NAME), anyInt()))
            .thenReturn(TestInjector.CALLING_UID);
        mService.enableCarMode(0, 0, PACKAGE_NAME);
        assertThat(mService.getCurrentModeType()).isEqualTo(Configuration.UI_MODE_TYPE_CAR);
        when(mPackageManager.getPackageUidAsUser(eq(PACKAGE_NAME), anyInt()))
            .thenReturn(TestInjector.CALLING_UID + 1);

        assertThrows(SecurityException.class,
            () -> mService.disableCarModeByCallingPackage(0, PACKAGE_NAME));
        assertThat(mService.getCurrentModeType()).isEqualTo(Configuration.UI_MODE_TYPE_CAR);

        // Clean up
        when(mPackageManager.getPackageUidAsUser(eq(PACKAGE_NAME), anyInt()))
            .thenReturn(TestInjector.CALLING_UID);
         mService.disableCarModeByCallingPackage(0, PACKAGE_NAME);
        assertThat(mService.getCurrentModeType()).isNotEqualTo(Configuration.UI_MODE_TYPE_CAR);
    }

    private void requestAllPossibleProjectionTypes() throws RemoteException {
        for (int i = 0; i < Integer.SIZE; ++i) {
            mService.requestProjection(mBinder, 1 << i, PACKAGE_NAME);