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