1 /* 2 * Copyright (C) 2020 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.settings.datausage; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.mockito.ArgumentMatchers.anyInt; 22 import static org.mockito.ArgumentMatchers.anyString; 23 import static org.mockito.Mockito.mock; 24 import static org.mockito.Mockito.when; 25 26 import android.content.pm.ApplicationInfo; 27 import android.content.pm.PackageManager; 28 import android.content.pm.PackageManager.NameNotFoundException; 29 import android.graphics.drawable.Drawable; 30 import android.util.ArraySet; 31 32 import androidx.preference.Preference; 33 import androidx.test.core.app.ApplicationProvider; 34 import androidx.test.ext.junit.runners.AndroidJUnit4; 35 36 import org.junit.Before; 37 import org.junit.Test; 38 import org.junit.runner.RunWith; 39 import org.mockito.Mock; 40 import org.mockito.MockitoAnnotations; 41 42 @RunWith(AndroidJUnit4.class) 43 public class AppPrefLoaderTest { 44 45 @Mock 46 private PackageManager mPackageManager; 47 48 private AppPrefLoader mLoader; 49 50 @Before setUp()51 public void setUp() throws Exception { 52 MockitoAnnotations.initMocks(this); 53 final ArraySet<String> pkgs = new ArraySet<>(2); 54 pkgs.add("pkg0"); 55 pkgs.add("pkg1"); 56 mLoader = new AppPrefLoader( 57 ApplicationProvider.getApplicationContext(), pkgs, mPackageManager); 58 } 59 60 @Test loadInBackground_packageNotFound_shouldReturnEmptySet()61 public void loadInBackground_packageNotFound_shouldReturnEmptySet() 62 throws NameNotFoundException { 63 when(mPackageManager.getApplicationInfo(anyString(), anyInt())) 64 .thenThrow(new NameNotFoundException()); 65 66 assertThat(mLoader.loadInBackground()).isEmpty(); 67 } 68 69 @Test loadInBackground_shouldReturnPreference()70 public void loadInBackground_shouldReturnPreference() throws NameNotFoundException { 71 ApplicationInfo info = mock(ApplicationInfo.class); 72 when(mPackageManager.getApplicationInfo(anyString(), anyInt())).thenReturn(info); 73 final Drawable drawable = mock(Drawable.class); 74 final String label = "Label1"; 75 when(info.loadIcon(mPackageManager)).thenReturn(drawable); 76 when(info.loadLabel(mPackageManager)).thenReturn(label); 77 78 Preference preference = mLoader.loadInBackground().valueAt(0); 79 assertThat(preference.getTitle()).isEqualTo(label); 80 assertThat(preference.getIcon()).isEqualTo(drawable); 81 assertThat(preference.isSelectable()).isFalse(); 82 } 83 } 84