• 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.Context;
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.support.v7.preference.Preference;
31 
32 import android.util.ArraySet;
33 import com.android.settings.SettingsRobolectricTestRunner;
34 import com.android.settings.TestConfig;
35 
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 import org.mockito.Answers;
40 import org.mockito.Mock;
41 import org.mockito.MockitoAnnotations;
42 import org.robolectric.RuntimeEnvironment;
43 import org.robolectric.annotation.Config;
44 
45 
46 @RunWith(SettingsRobolectricTestRunner.class)
47 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
48 public class AppPrefLoaderTest {
49 
50     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
51     private Context mContext;
52     @Mock
53     private PackageManager mPackageManager;
54 
55     private AppPrefLoader mLoader;
56 
57 
58     @Before
setUp()59     public void setUp() throws Exception {
60         MockitoAnnotations.initMocks(this);
61         final ArraySet<String> pkgs = new ArraySet<>();
62         pkgs.add("pkg0");
63         pkgs.add("pkg1");
64         mLoader =
65             new AppPrefLoader(RuntimeEnvironment.application, pkgs, mPackageManager);
66     }
67 
68     @Test
loadInBackground_packageNotFound_shouldReturnEmptySet()69     public void loadInBackground_packageNotFound_shouldReturnEmptySet()
70             throws NameNotFoundException {
71         when(mPackageManager.getApplicationInfo(anyString(), anyInt()))
72             .thenThrow(new NameNotFoundException());
73 
74         ArraySet<Preference> preferences = mLoader.loadInBackground();
75         assertThat(preferences).isEmpty();
76     }
77 
78     @Test
loadInBackground_shouldReturnPreference()79     public void loadInBackground_shouldReturnPreference() throws NameNotFoundException {
80         ApplicationInfo info = mock(ApplicationInfo.class);
81         when(mPackageManager.getApplicationInfo(anyString(), anyInt())).thenReturn(info);
82         final Drawable drawable = mock(Drawable.class);
83         final String label = "Label1";
84         when(info.loadIcon(mPackageManager)).thenReturn(drawable);
85         when(info.loadLabel(mPackageManager)).thenReturn(label);
86 
87         Preference preference = mLoader.loadInBackground().valueAt(0);
88         assertThat(preference.getTitle()).isEqualTo(label);
89         assertThat(preference.getIcon()).isEqualTo(drawable);
90         assertThat(preference.isSelectable()).isFalse();
91     }
92 
93 }
94