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