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.enterprise;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.mockito.Mockito.any;
22 import static org.mockito.Mockito.doAnswer;
23 import static org.mockito.Mockito.doReturn;
24 import static org.mockito.Mockito.eq;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.when;
27 
28 import android.app.admin.DevicePolicyManager;
29 import android.content.Context;
30 
31 import androidx.preference.Preference;
32 
33 import com.android.settings.R;
34 import com.android.settings.applications.ApplicationFeatureProvider;
35 import com.android.settings.testutils.FakeFeatureFactory;
36 import com.android.settingslib.utils.StringUtil;
37 
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.mockito.Answers;
41 import org.mockito.Mock;
42 import org.mockito.MockitoAnnotations;
43 import org.mockito.invocation.InvocationOnMock;
44 import org.mockito.stubbing.Answer;
45 
46 /**
47  * Common base for testing subclasses of {@link AdminGrantedPermissionsPreferenceControllerBase}.
48  */
49 public abstract class AdminGrantedPermissionsPreferenceControllerTestBase {
50 
51     private final String mKey;
52     final String[] mPermissions;
53 
54     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
55     protected Context mContext;
56     private FakeFeatureFactory mFeatureFactory;
57 
58     protected AdminGrantedPermissionsPreferenceControllerBase mController;
59 
AdminGrantedPermissionsPreferenceControllerTestBase(String key, String[] permissions)60     AdminGrantedPermissionsPreferenceControllerTestBase(String key, String[] permissions) {
61         mKey = key;
62         mPermissions = permissions;
63     }
64 
65     @Before
setUp()66     public void setUp() {
67         MockitoAnnotations.initMocks(this);
68         doReturn(mock(DevicePolicyManager.class)).when(mContext)
69                 .getSystemService(Context.DEVICE_POLICY_SERVICE);
70         mFeatureFactory = FakeFeatureFactory.setupForTest();
71         mController = createController(true /* async */);
72     }
73 
setNumberOfPackagesWithAdminGrantedPermissions(int number, boolean async)74     private void setNumberOfPackagesWithAdminGrantedPermissions(int number, boolean async) {
75         doAnswer(new Answer() {
76             public Object answer(InvocationOnMock invocation) {
77                 ((ApplicationFeatureProvider.NumberOfAppsCallback)
78                         invocation.getArguments()[2]).onNumberOfAppsResult(number);
79                 return null;
80             }
81         }).when(mFeatureFactory.applicationFeatureProvider)
82                 .calculateNumberOfAppsWithAdminGrantedPermissions(eq(mPermissions),
83                         eq(async), any());
84         when(mContext.getResources().getString(R.string.enterprise_privacy_number_packages_lower_bound))
85                 .thenReturn("Minimum # apps");
86     }
87 
88     @Test
testUpdateState()89     public void testUpdateState() {
90         final Preference preference = new Preference(mContext, null, 0, 0);
91         preference.setVisible(true);
92 
93         setNumberOfPackagesWithAdminGrantedPermissions(0, true /* async */);
94         mController.updateState(preference);
95         assertThat(preference.isVisible()).isFalse();
96 
97         setNumberOfPackagesWithAdminGrantedPermissions(20, true /* async */);
98         when(StringUtil.getIcuPluralsString(mContext, 20,
99                 R.string.enterprise_privacy_number_packages_lower_bound))
100                 .thenReturn("minimum 20 apps");
101         mController.updateState(preference);
102         assertThat(preference.getSummary()).isEqualTo("minimum 20 apps");
103         assertThat(preference.isVisible()).isTrue();
104     }
105 
106     @Test
testIsAvailableSync()107     public void testIsAvailableSync() {
108         final AdminGrantedPermissionsPreferenceControllerBase controller
109                 = createController(false /* async */);
110 
111         setNumberOfPackagesWithAdminGrantedPermissions(0, false /* async */);
112         assertThat(controller.isAvailable()).isFalse();
113 
114         setNumberOfPackagesWithAdminGrantedPermissions(20, false /* async */);
115         assertThat(controller.isAvailable()).isTrue();
116     }
117 
118     @Test
testIsAvailableAsync()119     public void testIsAvailableAsync() {
120         setNumberOfPackagesWithAdminGrantedPermissions(0, true /* async */);
121         assertThat(mController.isAvailable()).isTrue();
122 
123         setNumberOfPackagesWithAdminGrantedPermissions(20, true /* async */);
124         assertThat(mController.isAvailable()).isTrue();
125     }
126 
127     @Test
testHandlePreferenceTreeClick()128     public void testHandlePreferenceTreeClick() {
129         assertThat(mController.handlePreferenceTreeClick(new Preference(mContext, null, 0, 0)))
130                 .isFalse();
131     }
132 
133     @Test
testGetPreferenceKey()134     public void testGetPreferenceKey() {
135         assertThat(mController.getPreferenceKey()).isEqualTo(mKey);
136     }
137 
createController( boolean async)138     protected abstract AdminGrantedPermissionsPreferenceControllerBase createController(
139             boolean async);
140 }
141