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 
21 import static org.mockito.ArgumentMatchers.anyInt;
22 import static org.mockito.Mockito.when;
23 
24 import android.content.Context;
25 import android.os.UserManager;
26 
27 import androidx.preference.Preference;
28 import androidx.preference.PreferenceScreen;
29 
30 import com.android.internal.widget.LockPatternUtils;
31 import com.android.settings.testutils.FakeFeatureFactory;
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.RobolectricTestRunner;
39 import org.robolectric.RuntimeEnvironment;
40 import org.robolectric.shadows.ShadowApplication;
41 
42 @RunWith(RobolectricTestRunner.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 Context mContext;
57     private LockUnificationPreferenceController mController;
58     private Preference mPreference;
59 
60     @Before
setUp()61     public void setUp() {
62         MockitoAnnotations.initMocks(this);
63         mContext = RuntimeEnvironment.application;
64         ShadowApplication.getInstance().setSystemService(Context.USER_SERVICE, mUm);
65         when(mUm.getProfileIdsWithDisabled(anyInt())).thenReturn(new int[] {FAKE_PROFILE_USER_ID});
66 
67         final FakeFeatureFactory featureFactory = FakeFeatureFactory.setupForTest();
68         when(featureFactory.securityFeatureProvider.getLockPatternUtils(mContext))
69                 .thenReturn(mLockPatternUtils);
70     }
71 
init()72     private void init() {
73         mController = new LockUnificationPreferenceController(mContext, mHost);
74         when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
75         mPreference = new Preference(mContext);
76     }
77 
78     @Test
isAvailable_noProfile_false()79     public void isAvailable_noProfile_false() {
80         when(mUm.getProfileIdsWithDisabled(anyInt())).thenReturn(new int[0]);
81         init();
82 
83         assertThat(mController.isAvailable()).isFalse();
84     }
85 
86     @Test
isAvailable_separateChallengeNotAllowed_false()87     public void isAvailable_separateChallengeNotAllowed_false() {
88         when(mLockPatternUtils.isSeparateProfileChallengeAllowed(anyInt())).thenReturn(false);
89         init();
90 
91         assertThat(mController.isAvailable()).isFalse();
92     }
93 
94     @Test
isAvailable_separateChallengeAllowed_true()95     public void isAvailable_separateChallengeAllowed_true() {
96         when(mLockPatternUtils.isSeparateProfileChallengeAllowed(anyInt())).thenReturn(true);
97         init();
98 
99         assertThat(mController.isAvailable()).isTrue();
100     }
101 }
102