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
17package com.android.settings.security;
18
19import static com.google.common.truth.Truth.assertThat;
20import static org.mockito.ArgumentMatchers.anyInt;
21import static org.mockito.Mockito.when;
22
23import android.content.Context;
24import android.os.UserHandle;
25import android.os.UserManager;
26import android.support.v7.preference.Preference;
27import android.support.v7.preference.PreferenceScreen;
28
29import com.android.internal.widget.LockPatternUtils;
30import com.android.settings.testutils.FakeFeatureFactory;
31import com.android.settings.testutils.SettingsRobolectricTestRunner;
32
33import org.junit.Before;
34import org.junit.Test;
35import org.junit.runner.RunWith;
36import org.mockito.Mock;
37import org.mockito.MockitoAnnotations;
38import org.robolectric.RuntimeEnvironment;
39import org.robolectric.shadows.ShadowApplication;
40import org.robolectric.util.ReflectionHelpers;
41
42@RunWith(SettingsRobolectricTestRunner.class)
43public 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
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
78    public void isAvailable_noProfile_false() {
79        ReflectionHelpers.setField(mController, "mProfileChallengeUserId", UserHandle.USER_NULL);
80
81        assertThat(mController.isAvailable()).isFalse();
82    }
83
84    @Test
85    public void isAvailable_separateChallengeNotAllowed_false() {
86        when(mLockPatternUtils.isSeparateProfileChallengeAllowed(anyInt())).thenReturn(false);
87
88        assertThat(mController.isAvailable()).isFalse();
89    }
90
91    @Test
92    public void isAvailable_separateChallengeAllowed_true() {
93        when(mLockPatternUtils.isSeparateProfileChallengeAllowed(anyInt())).thenReturn(true);
94
95        assertThat(mController.isAvailable()).isTrue();
96    }
97}
98