1 package com.android.documentsui; 2 3 import static com.google.common.truth.Truth.assertThat; 4 5 import static org.mockito.ArgumentMatchers.any; 6 import static org.mockito.ArgumentMatchers.anyInt; 7 import static org.mockito.ArgumentMatchers.anyString; 8 import static org.mockito.ArgumentMatchers.eq; 9 import static org.mockito.Mockito.mock; 10 import static org.mockito.Mockito.never; 11 import static org.mockito.Mockito.verify; 12 import static org.mockito.Mockito.when; 13 14 import android.content.Context; 15 import android.content.pm.ApplicationInfo; 16 import android.content.pm.PackageManager; 17 import android.content.pm.ProviderInfo; 18 import android.graphics.drawable.Drawable; 19 20 import androidx.test.InstrumentationRegistry; 21 import androidx.test.filters.SmallTest; 22 23 import com.android.documentsui.testing.TestProvidersAccess; 24 25 import org.junit.Before; 26 import org.junit.Test; 27 28 @SmallTest 29 public class IconUtilsTest { 30 private static final String AUDIO_MIME_TYPE = "audio"; 31 private static final String IMAGE_MIME_TYPE = "image"; 32 private static final String TEXT_MIME_TYPE = "text"; 33 private static final String VIDEO_MIME_TYPE = "video"; 34 private static final String GENERIC_MIME_TYPE = "generic"; 35 36 private Context mTargetContext; 37 38 @Before setUp()39 public void setUp() throws Exception { 40 mTargetContext = InstrumentationRegistry.getTargetContext(); 41 } 42 43 @Test testLoadMimeIcon_isAudioMimeType()44 public void testLoadMimeIcon_isAudioMimeType() { 45 assertThat(IconUtils.loadMimeIcon(mTargetContext, AUDIO_MIME_TYPE)).isNotNull(); 46 } 47 48 @Test testLoadMimeIcon_isImageMimeType()49 public void testLoadMimeIcon_isImageMimeType() { 50 assertThat(IconUtils.loadMimeIcon(mTargetContext, IMAGE_MIME_TYPE)).isNotNull(); 51 } 52 53 @Test testLoadMimeIcon_isGenericMimeType()54 public void testLoadMimeIcon_isGenericMimeType() { 55 assertThat(IconUtils.loadMimeIcon(mTargetContext, GENERIC_MIME_TYPE)).isNotNull(); 56 } 57 58 @Test testLoadMimeIcon_isVideoMimeType()59 public void testLoadMimeIcon_isVideoMimeType() { 60 assertThat(IconUtils.loadMimeIcon(mTargetContext, VIDEO_MIME_TYPE)).isNotNull(); 61 } 62 63 @Test testLoadMimeIcon_isTextMimeType()64 public void testLoadMimeIcon_isTextMimeType() { 65 assertThat(IconUtils.loadMimeIcon(mTargetContext, TEXT_MIME_TYPE)).isNotNull(); 66 } 67 68 @Test testLoadMimeIcon_isMimeTypeNull_shouldReturnNull()69 public void testLoadMimeIcon_isMimeTypeNull_shouldReturnNull() { 70 assertThat(IconUtils.loadMimeIcon(mTargetContext, null)).isNull(); 71 } 72 73 @Test testLoadPackageIcon()74 public void testLoadPackageIcon() throws PackageManager.NameNotFoundException { 75 final String authority = "a"; 76 final int icon = 1234; 77 final ProviderInfo providerInfo = new ProviderInfo(); 78 79 Context context = mock(Context.class); 80 PackageManager packageManager = mock(PackageManager.class); 81 Drawable drawable = mock(Drawable.class); 82 83 when(context.getPackageManager()).thenReturn(packageManager); 84 when(packageManager.resolveContentProvider(eq(authority), anyInt())).thenReturn( 85 providerInfo); 86 when(packageManager.getDrawable(anyString(), eq(icon), any(ApplicationInfo.class))) 87 .thenReturn(drawable); 88 89 IconUtils.loadPackageIcon(context, TestProvidersAccess.USER_ID, authority, icon, 90 /* maybeShowBadge= */false); 91 92 verify(packageManager).getDrawable(any(), eq(icon), any()); 93 verify(packageManager, never()).getUserBadgedIcon(drawable, 94 TestProvidersAccess.USER_HANDLE); 95 } 96 97 @Test testLoadPackageIcon_maybeShowBadge()98 public void testLoadPackageIcon_maybeShowBadge() throws PackageManager.NameNotFoundException { 99 final String authority = "a"; 100 final int icon = 1234; 101 final ProviderInfo providerInfo = new ProviderInfo(); 102 103 Context context = mock(Context.class); 104 PackageManager packageManager = mock(PackageManager.class); 105 Drawable drawable = mock(Drawable.class); 106 107 when(context.getPackageManager()).thenReturn(packageManager); 108 when(packageManager.resolveContentProvider(eq(authority), anyInt())).thenReturn( 109 providerInfo); 110 when(packageManager.getDrawable(any(), eq(icon), any())).thenReturn(drawable); 111 112 IconUtils.loadPackageIcon(context, TestProvidersAccess.USER_ID, authority, icon, 113 /* maybeShowBadge= */true); 114 115 verify(packageManager).getDrawable(any(), eq(icon), any()); 116 verify(packageManager).getUserBadgedIcon(drawable, TestProvidersAccess.USER_HANDLE); 117 } 118 } 119