• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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 import static org.mockito.Matchers.anyInt;
21 import static org.mockito.Matchers.anyString;
22 import static org.mockito.Mockito.mock;
23 import static org.mockito.Mockito.when;
24 
25 import android.content.pm.ApplicationInfo;
26 import android.content.pm.PackageManager;
27 import android.content.pm.PackageManager.NameNotFoundException;
28 import android.graphics.drawable.Drawable;
29 import android.support.v7.preference.Preference;
30 import android.util.ArraySet;
31 
32 import com.android.settings.testutils.SettingsRobolectricTestRunner;
33 
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.mockito.Mock;
38 import org.mockito.MockitoAnnotations;
39 import org.robolectric.RuntimeEnvironment;
40 
41 @RunWith(SettingsRobolectricTestRunner.class)
42 public class AppPrefLoaderTest {
43 
44     @Mock
45     private PackageManager mPackageManager;
46 
47     private AppPrefLoader mLoader;
48 
49     @Before
setUp()50     public void setUp() throws Exception {
51         MockitoAnnotations.initMocks(this);
52         final ArraySet<String> pkgs = new ArraySet<>(2);
53         pkgs.add("pkg0");
54         pkgs.add("pkg1");
55         mLoader = new AppPrefLoader(RuntimeEnvironment.application, pkgs, mPackageManager);
56     }
57 
58     @Test
loadInBackground_packageNotFound_shouldReturnEmptySet()59     public void loadInBackground_packageNotFound_shouldReturnEmptySet()
60             throws NameNotFoundException {
61         when(mPackageManager.getApplicationInfo(anyString(), anyInt()))
62             .thenThrow(new NameNotFoundException());
63 
64         assertThat(mLoader.loadInBackground()).isEmpty();
65     }
66 
67     @Test
loadInBackground_shouldReturnPreference()68     public void loadInBackground_shouldReturnPreference() throws NameNotFoundException {
69         ApplicationInfo info = mock(ApplicationInfo.class);
70         when(mPackageManager.getApplicationInfo(anyString(), anyInt())).thenReturn(info);
71         final Drawable drawable = mock(Drawable.class);
72         final String label = "Label1";
73         when(info.loadIcon(mPackageManager)).thenReturn(drawable);
74         when(info.loadLabel(mPackageManager)).thenReturn(label);
75 
76         Preference preference = mLoader.loadInBackground().valueAt(0);
77         assertThat(preference.getTitle()).isEqualTo(label);
78         assertThat(preference.getIcon()).isEqualTo(drawable);
79         assertThat(preference.isSelectable()).isFalse();
80     }
81 }
82