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.development; 18 19 import static com.android.settings.development.DevelopmentOptionsActivityRequestCodes 20 .REQUEST_CODE_ENABLE_OEM_UNLOCK; 21 22 import static com.google.common.truth.Truth.assertThat; 23 24 import static org.mockito.Mockito.doNothing; 25 import static org.mockito.Mockito.doReturn; 26 import static org.mockito.Mockito.mock; 27 import static org.mockito.Mockito.spy; 28 import static org.mockito.Mockito.verify; 29 import static org.mockito.Mockito.when; 30 31 import android.app.Activity; 32 import android.content.Context; 33 import android.content.pm.PackageManager; 34 import android.content.res.Resources; 35 import android.os.SystemProperties; 36 import android.os.UserManager; 37 import android.service.oemlock.OemLockManager; 38 import android.telephony.TelephonyManager; 39 40 import androidx.fragment.app.FragmentManager; 41 import androidx.preference.PreferenceScreen; 42 43 import com.android.settingslib.RestrictedSwitchPreference; 44 45 import org.junit.Before; 46 import org.junit.Test; 47 import org.junit.runner.RunWith; 48 import org.mockito.Answers; 49 import org.mockito.Mock; 50 import org.mockito.MockitoAnnotations; 51 import org.robolectric.RobolectricTestRunner; 52 53 @RunWith(RobolectricTestRunner.class) 54 public class OemUnlockPreferenceControllerTest { 55 56 private static final String OEM_UNLOCK_SUPPORTED_KEY = "ro.oem_unlock_supported"; 57 private static final String UNSUPPORTED = "-9999"; 58 private static final String SUPPORTED = "1"; 59 60 @Mock 61 private Context mContext; 62 @Mock 63 private Activity mActivity; 64 @Mock 65 private DevelopmentSettingsDashboardFragment mFragment; 66 @Mock 67 private RestrictedSwitchPreference mPreference; 68 @Mock 69 private PackageManager mPackageManager; 70 @Mock 71 private PreferenceScreen mPreferenceScreen; 72 @Mock 73 private OemLockManager mOemLockManager; 74 @Mock 75 private UserManager mUserManager; 76 @Mock 77 private TelephonyManager mTelephonyManager; 78 @Mock 79 private Resources mResources; 80 private OemUnlockPreferenceController mController; 81 82 @Before setup()83 public void setup() { 84 MockitoAnnotations.initMocks(this); 85 SystemProperties.set(OEM_UNLOCK_SUPPORTED_KEY, SUPPORTED); 86 when(mContext.getPackageManager()).thenReturn(mPackageManager); 87 when(mContext.getPackageManager().hasSystemFeature(PackageManager 88 .FEATURE_TELEPHONY_CARRIERLOCK)).thenReturn(true); 89 when(mContext.getSystemService(Context.OEM_LOCK_SERVICE)).thenReturn(mOemLockManager); 90 when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager); 91 when(mContext.getSystemService(Context.TELEPHONY_SERVICE)).thenReturn(mTelephonyManager); 92 when(mContext.getResources()).thenReturn(mResources); 93 mController = new OemUnlockPreferenceController(mContext, mActivity, mFragment); 94 when(mPreferenceScreen.findPreference(mController.getPreferenceKey())) 95 .thenReturn(mPreference); 96 when(mFragment.getChildFragmentManager()) 97 .thenReturn(mock(FragmentManager.class, Answers.RETURNS_DEEP_STUBS)); 98 mController.displayPreference(mPreferenceScreen); 99 } 100 101 @Test OemUnlockPreferenceController_oemUnlockUnsupported_shouldNotCrash()102 public void OemUnlockPreferenceController_oemUnlockUnsupported_shouldNotCrash() { 103 SystemProperties.set(OEM_UNLOCK_SUPPORTED_KEY, UNSUPPORTED); 104 105 new OemUnlockPreferenceController(mContext, mActivity, mFragment); 106 } 107 108 @Test OemUnlockPreferenceController_oemUnlockSupported_shouldNotCrash()109 public void OemUnlockPreferenceController_oemUnlockSupported_shouldNotCrash() { 110 new OemUnlockPreferenceController(mContext, mActivity, mFragment); 111 } 112 113 @Test isAvailable_shouldReturnTrueWhenOemLockManagerIsNotNull()114 public void isAvailable_shouldReturnTrueWhenOemLockManagerIsNotNull() { 115 boolean returnValue = mController.isAvailable(); 116 117 assertThat(returnValue).isTrue(); 118 } 119 120 @Test isAvailable_shouldReturnFalseWhenOemLockManagerIsNull()121 public void isAvailable_shouldReturnFalseWhenOemLockManagerIsNull() { 122 when(mContext.getSystemService(Context.OEM_LOCK_SERVICE)).thenReturn(null); 123 mController = new OemUnlockPreferenceController(mContext, mActivity, mFragment); 124 boolean returnValue = mController.isAvailable(); 125 126 assertThat(returnValue).isFalse(); 127 } 128 129 @Test onPreferenceChanged_turnOnUnlock()130 public void onPreferenceChanged_turnOnUnlock() { 131 mController = spy(mController); 132 doReturn(false).when(mController).showKeyguardConfirmation(mResources, 133 REQUEST_CODE_ENABLE_OEM_UNLOCK); 134 doNothing().when(mController).confirmEnableOemUnlock(); 135 136 mController.onPreferenceChange(null, true); 137 138 verify(mController).confirmEnableOemUnlock(); 139 } 140 141 @Test onPreferenceChanged_turnOffUnlock()142 public void onPreferenceChanged_turnOffUnlock() { 143 mController = spy(mController); 144 mController.onPreferenceChange(null, false); 145 doReturn(false).when(mController).isBootloaderUnlocked(); 146 147 verify(mFragment).getChildFragmentManager(); 148 } 149 150 @Test updateState_preferenceShouldBeCheckedAndShouldBeDisabled()151 public void updateState_preferenceShouldBeCheckedAndShouldBeDisabled() { 152 mController = spy(mController); 153 doReturn(true).when(mController).isOemUnlockedAllowed(); 154 doReturn(true).when(mController).isOemUnlockAllowedByUserAndCarrier(); 155 doReturn(true).when(mController).isBootloaderUnlocked(); 156 157 mController.updateState(mPreference); 158 159 verify(mPreference).setChecked(true); 160 verify(mPreference).setEnabled(false); 161 } 162 163 @Test updateState_preferenceShouldBeUncheckedAndShouldBeDisabled()164 public void updateState_preferenceShouldBeUncheckedAndShouldBeDisabled() { 165 mController = spy(mController); 166 doReturn(false).when(mController).isOemUnlockedAllowed(); 167 doReturn(true).when(mController).isOemUnlockAllowedByUserAndCarrier(); 168 doReturn(true).when(mController).isBootloaderUnlocked(); 169 170 mController.updateState(mPreference); 171 172 verify(mPreference).setChecked(false); 173 verify(mPreference).setEnabled(false); 174 } 175 176 @Test updateState_preferenceShouldBeCheckedAndShouldBeEnabled()177 public void updateState_preferenceShouldBeCheckedAndShouldBeEnabled() { 178 mController = spy(mController); 179 doReturn(true).when(mController).isOemUnlockedAllowed(); 180 doReturn(true).when(mController).isOemUnlockAllowedByUserAndCarrier(); 181 doReturn(false).when(mController).isBootloaderUnlocked(); 182 183 mController.updateState(mPreference); 184 185 verify(mPreference).setChecked(true); 186 verify(mPreference).setEnabled(true); 187 } 188 189 @Test onActivityResult_shouldReturnTrue()190 public void onActivityResult_shouldReturnTrue() { 191 final boolean result = mController.onActivityResult(REQUEST_CODE_ENABLE_OEM_UNLOCK, 192 Activity.RESULT_OK, null); 193 194 assertThat(result).isTrue(); 195 } 196 197 @Test onActivityResult_shouldReturnFalse()198 public void onActivityResult_shouldReturnFalse() { 199 final boolean result = mController.onActivityResult(123454, 1434, null); 200 201 assertThat(result).isFalse(); 202 } 203 204 @Test onDeveloperOptionsEnabled_preferenceShouldCheckRestriction()205 public void onDeveloperOptionsEnabled_preferenceShouldCheckRestriction() { 206 mController = spy(mController); 207 doReturn(false).when(mController).isOemUnlockAllowedByUserAndCarrier(); 208 doReturn(false).when(mController).isBootloaderUnlocked(); 209 when(mPreference.isEnabled()).thenReturn(true); 210 211 mController.onDeveloperOptionsEnabled(); 212 213 verify(mPreference).checkRestrictionAndSetDisabled(UserManager.DISALLOW_FACTORY_RESET); 214 } 215 216 @Test onOemUnlockConfirmed_oemManagerShouldSetUnlockAllowedByUser()217 public void onOemUnlockConfirmed_oemManagerShouldSetUnlockAllowedByUser() { 218 mController.onOemUnlockConfirmed(); 219 220 verify(mOemLockManager).setOemUnlockAllowedByUser(true); 221 } 222 } 223