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.applications;
18 
19 import static com.android.settings.core.BasePreferenceController.AVAILABLE;
20 
21 import static com.google.common.truth.Truth.assertThat;
22 
23 import static org.mockito.Matchers.anyInt;
24 import static org.mockito.Mockito.doReturn;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.spy;
27 import static org.mockito.Mockito.when;
28 
29 import android.content.Context;
30 import android.content.pm.ApplicationInfo;
31 import android.content.pm.ModuleInfo;
32 import android.content.pm.PackageManager;
33 
34 import androidx.preference.Preference;
35 import androidx.preference.PreferenceScreen;
36 
37 import com.android.settings.R;
38 import com.android.settings.datausage.AppStateDataUsageBridge;
39 import com.android.settings.testutils.shadow.ShadowApplicationsState;
40 import com.android.settings.testutils.shadow.ShadowUserManager;
41 import com.android.settingslib.applications.ApplicationsState;
42 
43 import org.junit.Before;
44 import org.junit.Test;
45 import org.junit.runner.RunWith;
46 import org.mockito.Mock;
47 import org.mockito.MockitoAnnotations;
48 import org.robolectric.RobolectricTestRunner;
49 import org.robolectric.RuntimeEnvironment;
50 import org.robolectric.annotation.Config;
51 
52 import java.util.ArrayList;
53 
54 @RunWith(RobolectricTestRunner.class)
55 @Config(shadows = {ShadowUserManager.class, ShadowApplicationsState.class})
56 public class SpecialAppAccessPreferenceControllerTest {
57 
58     private Context mContext;
59     @Mock
60     private ApplicationsState.Session mSession;
61     @Mock
62     private PreferenceScreen mScreen;
63     @Mock
64     private PackageManager mPackageManager;
65 
66     private SpecialAppAccessPreferenceController mController;
67     private Preference mPreference;
68 
69     @Before
setUp()70     public void setUp() {
71         MockitoAnnotations.initMocks(this);
72         mContext = spy(RuntimeEnvironment.application);
73         when(mContext.getApplicationContext()).thenReturn(mContext);
74         ShadowUserManager.getShadow().setProfileIdsWithDisabled(new int[]{0});
75         doReturn(mPackageManager).when(mContext).getPackageManager();
76         doReturn(new ArrayList<ModuleInfo>()).when(mPackageManager).getInstalledModules(anyInt());
77         mController = new SpecialAppAccessPreferenceController(mContext, "test_key");
78         mPreference = new Preference(mContext);
79         when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
80 
81         mController.mSession = mSession;
82     }
83 
84     @Test
getAvailabilityState_unsearchable()85     public void getAvailabilityState_unsearchable() {
86         assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
87     }
88 
89     @Test
updateState_shouldSetSummary()90     public void updateState_shouldSetSummary() {
91         final ArrayList<ApplicationsState.AppEntry> apps = new ArrayList<>();
92         final ApplicationsState.AppEntry entry = mock(ApplicationsState.AppEntry.class);
93         entry.hasLauncherEntry = true;
94         entry.info = new ApplicationInfo();
95         entry.extraInfo = new AppStateDataUsageBridge.DataUsageState(
96                 true /* whitelisted */, false /* blacklisted */);
97         apps.add(entry);
98         when(mSession.getAllApps()).thenReturn(apps);
99 
100         mController.displayPreference(mScreen);
101         mController.onExtraInfoUpdated();
102 
103         assertThat(mPreference.getSummary())
104                 .isEqualTo(mContext.getResources().getQuantityString(
105                         R.plurals.special_access_summary, 1, 1));
106     }
107 
108     @Test
updateState_wrongExtraInfo_shouldNotIncludeInSummary()109     public void updateState_wrongExtraInfo_shouldNotIncludeInSummary() {
110         final ArrayList<ApplicationsState.AppEntry> apps = new ArrayList<>();
111         final ApplicationsState.AppEntry entry = mock(ApplicationsState.AppEntry.class);
112         entry.hasLauncherEntry = true;
113         entry.info = new ApplicationInfo();
114         entry.extraInfo = new AppStateNotificationBridge.NotificationsSentState();
115         apps.add(entry);
116         when(mSession.getAllApps()).thenReturn(apps);
117 
118         mController.displayPreference(mScreen);
119         mController.onExtraInfoUpdated();
120 
121         assertThat(mPreference.getSummary())
122                 .isEqualTo(mContext.getResources().getQuantityString(
123                         R.plurals.special_access_summary, 0, 0));
124     }
125 }
126