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