1 /* 2 * Copyright (C) 2018 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 17 package com.android.settings.security; 18 19 import static com.google.common.truth.Truth.assertThat; 20 import static org.mockito.ArgumentMatchers.anyInt; 21 import static org.mockito.Mockito.when; 22 23 import android.content.Context; 24 import android.os.UserHandle; 25 import android.os.UserManager; 26 import android.support.v7.preference.Preference; 27 import android.support.v7.preference.PreferenceScreen; 28 29 import com.android.internal.widget.LockPatternUtils; 30 import com.android.settings.testutils.FakeFeatureFactory; 31 import com.android.settings.testutils.SettingsRobolectricTestRunner; 32 33 import org.junit.Before; 34 import org.junit.Test; 35 import org.junit.runner.RunWith; 36 import org.mockito.Mock; 37 import org.mockito.MockitoAnnotations; 38 import org.robolectric.RuntimeEnvironment; 39 import org.robolectric.shadows.ShadowApplication; 40 import org.robolectric.util.ReflectionHelpers; 41 42 @RunWith(SettingsRobolectricTestRunner.class) 43 public class LockUnificationPreferenceControllerTest { 44 45 private static final int FAKE_PROFILE_USER_ID = 1234; 46 47 @Mock 48 private LockPatternUtils mLockPatternUtils; 49 @Mock 50 private UserManager mUm; 51 @Mock 52 private PreferenceScreen mScreen; 53 @Mock 54 private SecuritySettings mHost; 55 56 private FakeFeatureFactory mFeatureFactory; 57 private Context mContext; 58 private LockUnificationPreferenceController mController; 59 private Preference mPreference; 60 61 @Before setUp()62 public void setUp() { 63 MockitoAnnotations.initMocks(this); 64 mContext = RuntimeEnvironment.application; 65 ShadowApplication.getInstance().setSystemService(Context.USER_SERVICE, mUm); 66 when(mUm.getProfileIdsWithDisabled(anyInt())).thenReturn(new int[] {FAKE_PROFILE_USER_ID}); 67 68 mFeatureFactory = FakeFeatureFactory.setupForTest(); 69 when(mFeatureFactory.securityFeatureProvider.getLockPatternUtils(mContext)) 70 .thenReturn(mLockPatternUtils); 71 72 mController = new LockUnificationPreferenceController(mContext, mHost); 73 when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference); 74 mPreference = new Preference(mContext); 75 } 76 77 @Test isAvailable_noProfile_false()78 public void isAvailable_noProfile_false() { 79 ReflectionHelpers.setField(mController, "mProfileChallengeUserId", UserHandle.USER_NULL); 80 81 assertThat(mController.isAvailable()).isFalse(); 82 } 83 84 @Test isAvailable_separateChallengeNotAllowed_false()85 public void isAvailable_separateChallengeNotAllowed_false() { 86 when(mLockPatternUtils.isSeparateProfileChallengeAllowed(anyInt())).thenReturn(false); 87 88 assertThat(mController.isAvailable()).isFalse(); 89 } 90 91 @Test isAvailable_separateChallengeAllowed_true()92 public void isAvailable_separateChallengeAllowed_true() { 93 when(mLockPatternUtils.isSeparateProfileChallengeAllowed(anyInt())).thenReturn(true); 94 95 assertThat(mController.isAvailable()).isTrue(); 96 } 97 } 98