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

Commit 430b0001 authored by Mark Renouf's avatar Mark Renouf
Browse files

Avoid crash when no work profile files app is set

Test: atest WorkProfileMessageControllerTest
Bug: 274350621
Change-Id: I58bc21f0a683ac9df520c2ed2cfb1f9ad961b025
parent 889b436d
Loading
Loading
Loading
Loading
+3 −5
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ package com.android.systemui.screenshot
import android.content.ComponentName
import android.content.Context
import android.content.pm.PackageManager
import android.content.pm.PackageManager.ComponentInfoFlags
import android.graphics.drawable.Drawable
import android.os.UserHandle
import android.os.UserManager
@@ -53,12 +54,9 @@ constructor(
            var badgedIcon: Drawable? = null
            var label: CharSequence? = null
            val fileManager = fileManagerComponentName()
                ?: return WorkProfileFirstRunData(defaultFileAppName(), null)
            try {
                val info =
                    packageManager.getActivityInfo(
                        fileManager,
                        PackageManager.ComponentInfoFlags.of(0)
                    )
                val info = packageManager.getActivityInfo(fileManager, ComponentInfoFlags.of(0L))
                val icon = packageManager.getActivityIcon(fileManager)
                badgedIcon = packageManager.getUserBadgedIcon(icon, userHandle)
                label = info.loadLabel(packageManager)
+35 −15
Original line number Diff line number Diff line
@@ -45,7 +45,6 @@ import com.android.systemui.util.FakeSharedPreferences;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentMatchers;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

@@ -58,8 +57,9 @@ import kotlin.Unit;
@SmallTest
@RunWith(AndroidTestingRunner.class)
public class WorkProfileMessageControllerTest extends SysuiTestCase {
    private static final String DEFAULT_LABEL = "default label";
    private static final String APP_LABEL = "app label";
    private static final String FILES_APP_COMPONENT = "com.android.test/.FilesComponent";
    private static final String FILES_APP_LABEL = "Custom Files App";
    private static final String DEFAULT_FILES_APP_LABEL = "Files";
    private static final UserHandle NON_WORK_USER = UserHandle.of(0);
    private static final UserHandle WORK_USER = UserHandle.of(10);

@@ -88,14 +88,21 @@ public class WorkProfileMessageControllerTest extends SysuiTestCase {
        when(mMockContext.getSharedPreferences(
                eq(WorkProfileMessageController.SHARED_PREFERENCES_NAME),
                eq(Context.MODE_PRIVATE))).thenReturn(mSharedPreferences);
        when(mMockContext.getString(ArgumentMatchers.anyInt())).thenReturn(DEFAULT_LABEL);
        when(mPackageManager.getActivityIcon(any(ComponentName.class)))
        when(mMockContext.getString(R.string.config_sceenshotWorkProfileFilesApp))
                .thenReturn(FILES_APP_COMPONENT);
        when(mMockContext.getString(R.string.screenshot_default_files_app_name))
                .thenReturn(DEFAULT_FILES_APP_LABEL);
        when(mPackageManager.getActivityIcon(
                eq(ComponentName.unflattenFromString(FILES_APP_COMPONENT))))
                .thenReturn(mActivityIcon);
        when(mPackageManager.getUserBadgedIcon(
                any(), any())).thenReturn(mBadgedActivityIcon);
        when(mPackageManager.getActivityInfo(any(),
                any(PackageManager.ComponentInfoFlags.class))).thenReturn(mActivityInfo);
        when(mActivityInfo.loadLabel(eq(mPackageManager))).thenReturn(APP_LABEL);
        when(mPackageManager.getUserBadgedIcon(any(), any()))
                .thenReturn(mBadgedActivityIcon);
        when(mPackageManager.getActivityInfo(
                eq(ComponentName.unflattenFromString(FILES_APP_COMPONENT)),
                any(PackageManager.ComponentInfoFlags.class)))
                .thenReturn(mActivityInfo);
        when(mActivityInfo.loadLabel(eq(mPackageManager)))
                .thenReturn(FILES_APP_LABEL);

        mSharedPreferences.edit().putBoolean(
                WorkProfileMessageController.PREFERENCE_KEY, false).apply();
@@ -120,14 +127,15 @@ public class WorkProfileMessageControllerTest extends SysuiTestCase {
    @Test
    public void testOnScreenshotTaken_packageNotFound()
            throws PackageManager.NameNotFoundException {
        when(mPackageManager.getActivityInfo(any(),
        when(mPackageManager.getActivityInfo(
                eq(ComponentName.unflattenFromString(FILES_APP_COMPONENT)),
                any(PackageManager.ComponentInfoFlags.class))).thenThrow(
                new PackageManager.NameNotFoundException());

        WorkProfileMessageController.WorkProfileFirstRunData data =
                mMessageController.onScreenshotTaken(WORK_USER);

        assertEquals(DEFAULT_LABEL, data.getAppName());
        assertEquals(DEFAULT_FILES_APP_LABEL, data.getAppName());
        assertNull(data.getIcon());
    }

@@ -136,16 +144,28 @@ public class WorkProfileMessageControllerTest extends SysuiTestCase {
        WorkProfileMessageController.WorkProfileFirstRunData data =
                mMessageController.onScreenshotTaken(WORK_USER);

        assertEquals(APP_LABEL, data.getAppName());
        assertEquals(FILES_APP_LABEL, data.getAppName());
        assertEquals(mBadgedActivityIcon, data.getIcon());
    }

    @Test
    public void testOnScreenshotTaken_noFilesAppComponentDefined() {
        when(mMockContext.getString(R.string.config_sceenshotWorkProfileFilesApp))
                .thenReturn("");

        WorkProfileMessageController.WorkProfileFirstRunData data =
                mMessageController.onScreenshotTaken(WORK_USER);

        assertEquals(DEFAULT_FILES_APP_LABEL, data.getAppName());
        assertNull(data.getIcon());
    }

    @Test
    public void testPopulateView() throws InterruptedException {
        ViewGroup layout = (ViewGroup) LayoutInflater.from(mContext).inflate(
                R.layout.screenshot_work_profile_first_run, null);
        WorkProfileMessageController.WorkProfileFirstRunData data =
                new WorkProfileMessageController.WorkProfileFirstRunData(APP_LABEL,
                new WorkProfileMessageController.WorkProfileFirstRunData(FILES_APP_LABEL,
                        mBadgedActivityIcon);
        final CountDownLatch countdown = new CountDownLatch(1);
        mMessageController.populateView(layout, data, () -> {
@@ -157,7 +177,7 @@ public class WorkProfileMessageControllerTest extends SysuiTestCase {
        assertEquals(mBadgedActivityIcon, image.getDrawable());
        TextView text = layout.findViewById(R.id.screenshot_message_content);
        // The app name is used in a template, but at least validate that it was inserted.
        assertTrue(text.getText().toString().contains(APP_LABEL));
        assertTrue(text.getText().toString().contains(FILES_APP_LABEL));

        // Validate that clicking the dismiss button calls back properly.
        assertEquals(1, countdown.getCount());