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