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.Build; 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 import org.robolectric.annotation.Config; 53 import org.robolectric.util.ReflectionHelpers; 54 55 @RunWith(RobolectricTestRunner.class) 56 public class OemUnlockPreferenceControllerTest { 57 58 @Mock 59 private Context mContext; 60 @Mock 61 private Activity mActivity; 62 @Mock 63 private DevelopmentSettingsDashboardFragment mFragment; 64 @Mock 65 private RestrictedSwitchPreference mPreference; 66 @Mock 67 private PackageManager mPackageManager; 68 @Mock 69 private PreferenceScreen mPreferenceScreen; 70 @Mock 71 private OemLockManager mOemLockManager; 72 @Mock 73 private UserManager mUserManager; 74 @Mock 75 private TelephonyManager mTelephonyManager; 76 @Mock 77 private Resources mResources; 78 private OemUnlockPreferenceController mController; 79 80 @Before setup()81 public void setup() { 82 MockitoAnnotations.initMocks(this); 83 when(mContext.getPackageManager()).thenReturn(mPackageManager); 84 when(mContext.getPackageManager().hasSystemFeature(PackageManager 85 .FEATURE_TELEPHONY_CARRIERLOCK)).thenReturn(true); 86 when(mContext.getSystemService(Context.OEM_LOCK_SERVICE)).thenReturn(mOemLockManager); 87 when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager); 88 when(mContext.getSystemService(Context.TELEPHONY_SERVICE)).thenReturn(mTelephonyManager); 89 when(mContext.getResources()).thenReturn(mResources); 90 mController = new OemUnlockPreferenceController(mContext, mActivity, mFragment); 91 when(mPreferenceScreen.findPreference(mController.getPreferenceKey())) 92 .thenReturn(mPreference); 93 when(mFragment.getChildFragmentManager()) 94 .thenReturn(mock(FragmentManager.class, Answers.RETURNS_DEEP_STUBS)); 95 mController.displayPreference(mPreferenceScreen); 96 } 97 98 @Test 99 @Config(qualifiers = "mcc999") OemUnlockPreferenceController_shouldNotCrashInEmulatorEngBuild()100 public void OemUnlockPreferenceController_shouldNotCrashInEmulatorEngBuild() { 101 ReflectionHelpers.setStaticField(Build.class, "IS_EMULATOR", true); 102 ReflectionHelpers.setStaticField(Build.class, "IS_ENG", true); 103 104 new OemUnlockPreferenceController(mContext, mActivity, mFragment); 105 } 106 107 @Test 108 @Config(qualifiers = "mcc999") OemUnlockPreferenceController_shouldNotCrashInOtherBuild()109 public void OemUnlockPreferenceController_shouldNotCrashInOtherBuild() { 110 ReflectionHelpers.setStaticField(Build.class, "IS_EMULATOR", false); 111 ReflectionHelpers.setStaticField(Build.class, "IS_ENG", false); 112 113 new OemUnlockPreferenceController(mContext, mActivity, mFragment); 114 } 115 116 @Test isAvailable_shouldReturnTrueWhenOemLockManagerIsNotNull()117 public void isAvailable_shouldReturnTrueWhenOemLockManagerIsNotNull() { 118 boolean returnValue = mController.isAvailable(); 119 120 assertThat(returnValue).isTrue(); 121 } 122 123 @Test isAvailable_shouldReturnFalseWhenOemLockManagerIsNull()124 public void isAvailable_shouldReturnFalseWhenOemLockManagerIsNull() { 125 when(mContext.getSystemService(Context.OEM_LOCK_SERVICE)).thenReturn(null); 126 mController = new OemUnlockPreferenceController(mContext, mActivity, mFragment); 127 boolean returnValue = mController.isAvailable(); 128 129 assertThat(returnValue).isFalse(); 130 } 131 132 @Test onPreferenceChanged_turnOnUnlock()133 public void onPreferenceChanged_turnOnUnlock() { 134 mController = spy(mController); 135 doReturn(false).when(mController).showKeyguardConfirmation(mResources, 136 REQUEST_CODE_ENABLE_OEM_UNLOCK); 137 doNothing().when(mController).confirmEnableOemUnlock(); 138 139 mController.onPreferenceChange(null, true); 140 141 verify(mController).confirmEnableOemUnlock(); 142 } 143 144 @Test onPreferenceChanged_turnOffUnlock()145 public void onPreferenceChanged_turnOffUnlock() { 146 mController = spy(mController); 147 mController.onPreferenceChange(null, false); 148 doReturn(false).when(mController).isBootloaderUnlocked(); 149 150 verify(mFragment).getChildFragmentManager(); 151 } 152 153 @Test updateState_preferenceShouldBeCheckedAndShouldBeDisabled()154 public void updateState_preferenceShouldBeCheckedAndShouldBeDisabled() { 155 mController = spy(mController); 156 doReturn(true).when(mController).isOemUnlockedAllowed(); 157 doReturn(true).when(mController).isOemUnlockAllowedByUserAndCarrier(); 158 doReturn(true).when(mController).isBootloaderUnlocked(); 159 160 mController.updateState(mPreference); 161 162 verify(mPreference).setChecked(true); 163 verify(mPreference).setEnabled(false); 164 } 165 166 @Test updateState_preferenceShouldBeUncheckedAndShouldBeDisabled()167 public void updateState_preferenceShouldBeUncheckedAndShouldBeDisabled() { 168 mController = spy(mController); 169 doReturn(false).when(mController).isOemUnlockedAllowed(); 170 doReturn(true).when(mController).isOemUnlockAllowedByUserAndCarrier(); 171 doReturn(true).when(mController).isBootloaderUnlocked(); 172 173 mController.updateState(mPreference); 174 175 verify(mPreference).setChecked(false); 176 verify(mPreference).setEnabled(false); 177 } 178 179 @Test updateState_preferenceShouldBeCheckedAndShouldBeEnabled()180 public void updateState_preferenceShouldBeCheckedAndShouldBeEnabled() { 181 mController = spy(mController); 182 doReturn(true).when(mController).isOemUnlockedAllowed(); 183 doReturn(true).when(mController).isOemUnlockAllowedByUserAndCarrier(); 184 doReturn(false).when(mController).isBootloaderUnlocked(); 185 186 mController.updateState(mPreference); 187 188 verify(mPreference).setChecked(true); 189 verify(mPreference).setEnabled(true); 190 } 191 192 @Test onActivityResult_shouldReturnTrue()193 public void onActivityResult_shouldReturnTrue() { 194 final boolean result = mController.onActivityResult(REQUEST_CODE_ENABLE_OEM_UNLOCK, 195 Activity.RESULT_OK, null); 196 197 assertThat(result).isTrue(); 198 } 199 200 @Test onActivityResult_shouldReturnFalse()201 public void onActivityResult_shouldReturnFalse() { 202 final boolean result = mController.onActivityResult(123454, 1434, null); 203 204 assertThat(result).isFalse(); 205 } 206 207 @Test onDeveloperOptionsEnabled_preferenceShouldCheckRestriction()208 public void onDeveloperOptionsEnabled_preferenceShouldCheckRestriction() { 209 mController = spy(mController); 210 doReturn(false).when(mController).isOemUnlockAllowedByUserAndCarrier(); 211 doReturn(false).when(mController).isBootloaderUnlocked(); 212 when(mPreference.isEnabled()).thenReturn(true); 213 214 mController.onDeveloperOptionsEnabled(); 215 216 verify(mPreference).checkRestrictionAndSetDisabled(UserManager.DISALLOW_FACTORY_RESET); 217 } 218 219 @Test onOemUnlockConfirmed_oemManagerShouldSetUnlockAllowedByUser()220 public void onOemUnlockConfirmed_oemManagerShouldSetUnlockAllowedByUser() { 221 mController.onOemUnlockConfirmed(); 222 223 verify(mOemLockManager).setOemUnlockAllowedByUser(true); 224 } 225 } 226