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