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
17package com.android.settings.datausage;
18
19import static com.google.common.truth.Truth.assertThat;
20import static org.mockito.Matchers.anyInt;
21import static org.mockito.Matchers.anyString;
22import static org.mockito.Mockito.mock;
23import static org.mockito.Mockito.when;
24
25import android.content.Context;
26import android.content.pm.ApplicationInfo;
27import android.content.pm.PackageManager;
28import android.content.pm.PackageManager.NameNotFoundException;
29import android.graphics.drawable.Drawable;
30import android.support.v7.preference.Preference;
31
32import android.util.ArraySet;
33import com.android.settings.testutils.SettingsRobolectricTestRunner;
34import com.android.settings.TestConfig;
35
36import org.junit.Before;
37import org.junit.Test;
38import org.junit.runner.RunWith;
39import org.mockito.Answers;
40import org.mockito.Mock;
41import org.mockito.MockitoAnnotations;
42import org.robolectric.RuntimeEnvironment;
43import org.robolectric.annotation.Config;
44
45
46@RunWith(SettingsRobolectricTestRunner.class)
47@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
48public 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
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
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
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