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.junit.runner.RunWith; 41 import org.mockito.Answers; 42 import org.mockito.Mock; 43 import org.mockito.MockitoAnnotations; 44 import org.mockito.invocation.InvocationOnMock; 45 import org.mockito.stubbing.Answer; 46 import org.robolectric.RobolectricTestRunner; 47 48 @RunWith(RobolectricTestRunner.class) 49 public class EnterpriseInstalledPackagesPreferenceControllerTest { 50 51 private static final String KEY_NUMBER_ENTERPRISE_INSTALLED_PACKAGES 52 = "number_enterprise_installed_packages"; 53 54 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 55 private Context mContext; 56 private FakeFeatureFactory mFeatureFactory; 57 58 private EnterpriseInstalledPackagesPreferenceController mController; 59 60 @Before setUp()61 public void setUp() { 62 MockitoAnnotations.initMocks(this); 63 doReturn(mock(DevicePolicyManager.class)).when(mContext) 64 .getSystemService(Context.DEVICE_POLICY_SERVICE); 65 mFeatureFactory = FakeFeatureFactory.setupForTest(); 66 mController = new EnterpriseInstalledPackagesPreferenceController(mContext, 67 true /* async */); 68 } 69 setNumberOfEnterpriseInstalledPackages(int number, boolean async)70 private void setNumberOfEnterpriseInstalledPackages(int number, boolean async) { 71 doAnswer(new Answer() { 72 public Object answer(InvocationOnMock invocation) { 73 ((ApplicationFeatureProvider.NumberOfAppsCallback) 74 invocation.getArguments()[1]).onNumberOfAppsResult(number); 75 return null; 76 } 77 }).when(mFeatureFactory.applicationFeatureProvider) 78 .calculateNumberOfPolicyInstalledApps(eq(async), any()); 79 when(mContext.getResources().getString(R.string.enterprise_privacy_number_packages_lower_bound)) 80 .thenReturn("Minimum # apps"); 81 } 82 83 @Test testUpdateState()84 public void testUpdateState() { 85 final Preference preference = new Preference(mContext, null, 0, 0); 86 preference.setVisible(true); 87 88 setNumberOfEnterpriseInstalledPackages(0, true /* async */); 89 mController.updateState(preference); 90 assertThat(preference.isVisible()).isFalse(); 91 92 setNumberOfEnterpriseInstalledPackages(20, true /* async */); 93 when(StringUtil.getIcuPluralsString(mContext, 20, 94 R.string.enterprise_privacy_number_packages_lower_bound)) 95 .thenReturn("minimum 20 apps"); 96 mController.updateState(preference); 97 assertThat(preference.getSummary()).isEqualTo("minimum 20 apps"); 98 assertThat(preference.isVisible()).isTrue(); 99 } 100 101 @Test testIsAvailableSync()102 public void testIsAvailableSync() { 103 final EnterpriseInstalledPackagesPreferenceController controller 104 = new EnterpriseInstalledPackagesPreferenceController(mContext, false /* async */); 105 106 setNumberOfEnterpriseInstalledPackages(0, false /* async */); 107 assertThat(controller.isAvailable()).isFalse(); 108 109 setNumberOfEnterpriseInstalledPackages(20, false /* async */); 110 assertThat(controller.isAvailable()).isTrue(); 111 } 112 113 @Test testIsAvailableAsync()114 public void testIsAvailableAsync() { 115 setNumberOfEnterpriseInstalledPackages(0, true /* async */); 116 assertThat(mController.isAvailable()).isTrue(); 117 118 setNumberOfEnterpriseInstalledPackages(20, true /* async */); 119 assertThat(mController.isAvailable()).isTrue(); 120 } 121 122 @Test testHandlePreferenceTreeClick()123 public void testHandlePreferenceTreeClick() { 124 assertThat(mController.handlePreferenceTreeClick(new Preference(mContext, null, 0, 0))) 125 .isFalse(); 126 } 127 128 @Test testGetPreferenceKey()129 public void testGetPreferenceKey() { 130 assertThat(mController.getPreferenceKey()) 131 .isEqualTo(KEY_NUMBER_ENTERPRISE_INSTALLED_PACKAGES); 132 } 133 } 134