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