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 package com.android.settings.development; 17 18 import static com.google.common.truth.Truth.assertThat; 19 20 import static org.mockito.ArgumentMatchers.any; 21 import static org.mockito.ArgumentMatchers.anyInt; 22 import static org.mockito.ArgumentMatchers.anyString; 23 import static org.mockito.Mockito.mock; 24 import static org.mockito.Mockito.verify; 25 import static org.mockito.Mockito.when; 26 27 import android.content.Context; 28 import android.content.pm.PackageManager; 29 import android.content.pm.ResolveInfo; 30 import android.provider.Settings; 31 import android.provider.Settings.Global; 32 33 import androidx.preference.PreferenceScreen; 34 35 import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin; 36 import com.android.settingslib.RestrictedSwitchPreference; 37 38 import org.junit.Before; 39 import org.junit.Test; 40 import org.junit.runner.RunWith; 41 import org.mockito.Mock; 42 import org.mockito.MockitoAnnotations; 43 import org.robolectric.RobolectricTestRunner; 44 import org.robolectric.RuntimeEnvironment; 45 import org.robolectric.util.ReflectionHelpers; 46 47 import java.util.Collections; 48 import java.util.List; 49 50 @RunWith(RobolectricTestRunner.class) 51 public class VerifyAppsOverUsbPreferenceControllerTest { 52 53 @Mock 54 private PackageManager mPackageManager; 55 @Mock 56 private PreferenceScreen mScreen; 57 @Mock 58 private RestrictedSwitchPreference mPreference; 59 60 @Mock 61 private VerifyAppsOverUsbPreferenceController.RestrictedLockUtilsDelegate 62 mRestrictedLockUtilsDelegate; 63 64 private Context mContext; 65 private VerifyAppsOverUsbPreferenceController mController; 66 67 /** Convenience class for setting global int settings. */ 68 class GlobalSetter { set(String setting, int value)69 public GlobalSetter set(String setting, int value) { 70 Global.putInt(mContext.getContentResolver(), setting, value); 71 return this; 72 } 73 } 74 75 private final GlobalSetter mGlobals = new GlobalSetter(); 76 77 @Before setUp()78 public void setUp() { 79 MockitoAnnotations.initMocks(this); 80 mContext = RuntimeEnvironment.application; 81 when(mScreen.findPreference(anyString())).thenReturn(mPreference); 82 mController = new VerifyAppsOverUsbPreferenceController(mContext); 83 ReflectionHelpers 84 .setField(mController, "mRestrictedLockUtils", mRestrictedLockUtilsDelegate); 85 ReflectionHelpers.setField(mController, "mPackageManager", mPackageManager); 86 mController.displayPreference(mScreen); 87 } 88 setupVerifyBroadcastReceivers(boolean nonEmpty)89 private void setupVerifyBroadcastReceivers(boolean nonEmpty) { 90 final List<ResolveInfo> resolveInfos = nonEmpty 91 ? Collections.singletonList(mock(ResolveInfo.class)) 92 : Collections.emptyList(); 93 when(mPackageManager.queryBroadcastReceivers(any(), anyInt())) 94 .thenReturn(resolveInfos); 95 } 96 setupEnforcedAdmin(EnforcedAdmin result)97 private void setupEnforcedAdmin(EnforcedAdmin result) { 98 when(mRestrictedLockUtilsDelegate 99 .checkIfRestrictionEnforced(any(), anyString(), anyInt())).thenReturn(result); 100 } 101 102 @Test updateState_settingEnabled_preferenceShouldBeChecked()103 public void updateState_settingEnabled_preferenceShouldBeChecked() { 104 setupVerifyBroadcastReceivers(true); 105 setupEnforcedAdmin(null); 106 mGlobals.set(Global.ADB_ENABLED, 1 /* setting enabled */) 107 .set(Global.PACKAGE_VERIFIER_INCLUDE_ADB, 1 /* setting enabled */); 108 mController.updateState(mPreference); 109 verify(mPreference).setChecked(true); 110 } 111 112 @Test updateState_settingDisabled_preferenceShouldNotBeChecked()113 public void updateState_settingDisabled_preferenceShouldNotBeChecked() { 114 setupVerifyBroadcastReceivers(true); 115 setupEnforcedAdmin(null); 116 mGlobals.set(Global.ADB_ENABLED, 1 /* setting enabled */) 117 .set(Global.PACKAGE_VERIFIER_INCLUDE_ADB, 0 /* setting disabled */); 118 mController.updateState(mPreference); 119 verify(mPreference).setChecked(false); 120 } 121 122 @Test updateState_adbDisabled_preferenceShouldNotBeChecked()123 public void updateState_adbDisabled_preferenceShouldNotBeChecked() { 124 setupVerifyBroadcastReceivers(true); 125 setupEnforcedAdmin(null); 126 mGlobals.set(Global.ADB_ENABLED, 0 /* setting disabled */) 127 .set(Global.PACKAGE_VERIFIER_INCLUDE_ADB, 1 /* setting enabled */); 128 mController.updateState(mPreference); 129 verify(mPreference).setChecked(false); 130 } 131 132 @Test updateState_noBroadcastReceivers_preferenceShouldNotBeChecked()133 public void updateState_noBroadcastReceivers_preferenceShouldNotBeChecked() { 134 setupVerifyBroadcastReceivers(false); 135 setupEnforcedAdmin(null); 136 mGlobals.set(Global.ADB_ENABLED, 1 /* setting enabled */) 137 .set(Global.PACKAGE_VERIFIER_INCLUDE_ADB, 1 /* setting enabled */); 138 mController.updateState(mPreference); 139 verify(mPreference).setChecked(false); 140 } 141 142 @Test updateState_restrictedByAdmin_preferenceShouldBeDisabled()143 public void updateState_restrictedByAdmin_preferenceShouldBeDisabled() { 144 setupVerifyBroadcastReceivers(true); 145 final EnforcedAdmin admin = new EnforcedAdmin(); 146 setupEnforcedAdmin(admin); 147 mGlobals.set(Global.ADB_ENABLED, 1 /* setting enabled */) 148 .set(Global.PACKAGE_VERIFIER_INCLUDE_ADB, 1 /* setting enabled */); 149 mController.updateState(mPreference); 150 verify(mPreference).setDisabledByAdmin(admin); 151 } 152 153 @Test isAvailable_verifierNotVisible_shouldReturnFalse()154 public void isAvailable_verifierNotVisible_shouldReturnFalse() { 155 setupVerifyBroadcastReceivers(true); 156 mGlobals.set(Global.PACKAGE_VERIFIER_SETTING_VISIBLE, 0 /* setting disabled */); 157 158 assertThat(mController.isAvailable()).isFalse(); 159 } 160 161 @Test isAvailable_verifierVisible_shouldReturnTrue()162 public void isAvailable_verifierVisible_shouldReturnTrue() { 163 setupVerifyBroadcastReceivers(true); 164 mGlobals.set(Global.PACKAGE_VERIFIER_SETTING_VISIBLE, 1 /* setting enabled */); 165 166 assertThat(mController.isAvailable()).isTrue(); 167 } 168 169 @Test onPreferenceChange_settingEnabled_shouldEnableUsbVerify()170 public void onPreferenceChange_settingEnabled_shouldEnableUsbVerify() { 171 mController.onPreferenceChange(mPreference, true /* new value */); 172 173 final int mode = Settings.Global.getInt(mContext.getContentResolver(), 174 android.provider.Settings.Global.PACKAGE_VERIFIER_INCLUDE_ADB, -1 /* default */); 175 176 assertThat(mode).isEqualTo(VerifyAppsOverUsbPreferenceController.SETTING_VALUE_ON); 177 } 178 179 @Test onPreferenceChange_settingDisabled_shouldDisableUsbVerify()180 public void onPreferenceChange_settingDisabled_shouldDisableUsbVerify() { 181 mController.onPreferenceChange(mPreference, false /* new value */); 182 183 final int mode = Settings.Global.getInt(mContext.getContentResolver(), 184 android.provider.Settings.Global.PACKAGE_VERIFIER_INCLUDE_ADB, -1 /* default */); 185 186 assertThat(mode).isEqualTo(VerifyAppsOverUsbPreferenceController.SETTING_VALUE_OFF); 187 } 188 } 189