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.security; 17 18 import static com.android.settings.security.OwnerInfoPreferenceController.KEY_OWNER_INFO; 19 20 import static com.google.common.truth.Truth.assertThat; 21 22 import static org.mockito.ArgumentMatchers.any; 23 import static org.mockito.ArgumentMatchers.anyInt; 24 import static org.mockito.ArgumentMatchers.anyString; 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.content.Context; 32 33 import androidx.fragment.app.FragmentManager; 34 import androidx.fragment.app.FragmentTransaction; 35 import androidx.preference.PreferenceManager; 36 import androidx.preference.PreferenceScreen; 37 38 import com.android.internal.widget.LockPatternUtils; 39 import com.android.settings.users.OwnerInfoSettings; 40 import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin; 41 import com.android.settingslib.RestrictedPreference; 42 import com.android.settingslib.core.lifecycle.Lifecycle; 43 import com.android.settingslib.core.lifecycle.ObservablePreferenceFragment; 44 45 import org.junit.Before; 46 import org.junit.Test; 47 import org.junit.runner.RunWith; 48 import org.mockito.Mock; 49 import org.mockito.MockitoAnnotations; 50 import org.robolectric.RobolectricTestRunner; 51 import org.robolectric.RuntimeEnvironment; 52 import org.robolectric.util.ReflectionHelpers; 53 54 @RunWith(RobolectricTestRunner.class) 55 public class OwnerInfoPreferenceControllerTest { 56 57 @Mock 58 private ObservablePreferenceFragment mFragment; 59 @Mock 60 private PreferenceScreen mScreen; 61 @Mock 62 private PreferenceManager mPreferenceManager; 63 @Mock 64 private FragmentManager mFragmentManager; 65 @Mock 66 private FragmentTransaction mFragmentTransaction; 67 @Mock 68 private RestrictedPreference mPreference; 69 @Mock 70 private LockPatternUtils mLockPatternUtils; 71 72 private Context mContext; 73 private OwnerInfoPreferenceController mController; 74 75 @Before setUp()76 public void setUp() { 77 MockitoAnnotations.initMocks(this); 78 mContext = spy(RuntimeEnvironment.application); 79 80 when(mFragment.isAdded()).thenReturn(true); 81 when(mFragment.getPreferenceScreen()).thenReturn(mScreen); 82 when(mFragment.getPreferenceManager()).thenReturn(mPreferenceManager); 83 when(mPreference.getContext()).thenReturn(mContext); 84 when(mFragment.getFragmentManager()).thenReturn(mFragmentManager); 85 when(mFragment.getSettingsLifecycle()).thenReturn(mock(Lifecycle.class)); 86 when(mFragmentManager.beginTransaction()).thenReturn(mFragmentTransaction); 87 88 mController = spy(new OwnerInfoPreferenceController(mContext, mFragment)); 89 when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference); 90 ReflectionHelpers.setField(mController, "mLockPatternUtils", mLockPatternUtils); 91 } 92 93 @Test isAvailable_shouldReturnTrue()94 public void isAvailable_shouldReturnTrue() { 95 assertThat(mController.isAvailable()).isTrue(); 96 } 97 98 @Test onResume_shouldUpdateEnableState()99 public void onResume_shouldUpdateEnableState() { 100 mController.onResume(); 101 102 verify(mController).updateEnableState(); 103 } 104 105 @Test onResume_shouldUpdateSummary()106 public void onResume_shouldUpdateSummary() { 107 mController.onResume(); 108 109 verify(mController).updateSummary(); 110 } 111 112 @Test updateSummary_deviceOwnerInfoEnabled_shouldSetDeviceOwnerInfoSummary()113 public void updateSummary_deviceOwnerInfoEnabled_shouldSetDeviceOwnerInfoSummary() { 114 final String deviceOwnerInfo = "Test Device Owner Info"; 115 doReturn(true).when(mController).isDeviceOwnerInfoEnabled(); 116 doReturn(deviceOwnerInfo).when(mController).getDeviceOwnerInfo(); 117 mController.displayPreference(mScreen); 118 119 mController.updateSummary(); 120 121 verify(mPreference).setSummary(deviceOwnerInfo); 122 } 123 124 @Test updateSummary_ownerInfoEnabled_shouldSetOwnerInfoSummary()125 public void updateSummary_ownerInfoEnabled_shouldSetOwnerInfoSummary() { 126 final String ownerInfo = "Test Owner Info"; 127 doReturn(false).when(mController).isDeviceOwnerInfoEnabled(); 128 doReturn(true).when(mController).isOwnerInfoEnabled(); 129 doReturn(ownerInfo).when(mController).getOwnerInfo(); 130 mController.displayPreference(mScreen); 131 132 mController.updateSummary(); 133 134 verify(mPreference).setSummary(ownerInfo); 135 } 136 137 @Test updateSummary_ownerInfoDisabled_shouldSetDefaultSummary()138 public void updateSummary_ownerInfoDisabled_shouldSetDefaultSummary() { 139 doReturn(false).when(mController).isDeviceOwnerInfoEnabled(); 140 doReturn(false).when(mController).isOwnerInfoEnabled(); 141 mController.displayPreference(mScreen); 142 143 mController.updateSummary(); 144 145 verify(mPreference).setSummary(mContext.getString( 146 com.android.settings.R.string.owner_info_settings_summary)); 147 } 148 149 @Test updateEnableState_deviceOwnerInfoEnabled_shouldSetDisabledByAdmin()150 public void updateEnableState_deviceOwnerInfoEnabled_shouldSetDisabledByAdmin() { 151 doReturn(true).when(mController).isDeviceOwnerInfoEnabled(); 152 doReturn(mock(EnforcedAdmin.class)).when(mController).getDeviceOwner(); 153 mController.displayPreference(mScreen); 154 155 mController.updateEnableState(); 156 157 verify(mPreference).setDisabledByAdmin(any(EnforcedAdmin.class)); 158 } 159 160 @Test updateEnableState_lockScreenDisabled_shouldDisablePreference()161 public void updateEnableState_lockScreenDisabled_shouldDisablePreference() { 162 doReturn(false).when(mController).isDeviceOwnerInfoEnabled(); 163 doReturn(true).when(mLockPatternUtils).isLockScreenDisabled(anyInt()); 164 mController.displayPreference(mScreen); 165 166 mController.updateEnableState(); 167 168 verify(mPreference).setEnabled(false); 169 } 170 171 @Test updateEnableState_lockScreenEnabled_shouldEnablePreference()172 public void updateEnableState_lockScreenEnabled_shouldEnablePreference() { 173 doReturn(false).when(mController).isDeviceOwnerInfoEnabled(); 174 doReturn(false).when(mLockPatternUtils).isLockScreenDisabled(anyInt()); 175 mController.displayPreference(mScreen); 176 177 mController.updateEnableState(); 178 179 verify(mPreference).setEnabled(true); 180 } 181 182 @Test handlePreferenceTreeClick_shouldLaunchOwnerInfoSettings()183 public void handlePreferenceTreeClick_shouldLaunchOwnerInfoSettings() { 184 final RestrictedPreference preference = new RestrictedPreference(mContext); 185 preference.setKey(KEY_OWNER_INFO); 186 when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(preference); 187 doReturn(false).when(mController).isDeviceOwnerInfoEnabled(); 188 doReturn(false).when(mLockPatternUtils).isLockScreenDisabled(anyInt()); 189 mController.displayPreference(mScreen); 190 mController.updateEnableState(); 191 192 mController.handlePreferenceTreeClick(preference); 193 194 verify(mFragment.getFragmentManager().beginTransaction()) 195 .add(any(OwnerInfoSettings.class), anyString()); 196 } 197 }