1 /*
2  * Copyright (C) 2023 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.privatespace;
18 
19 import static com.android.internal.widget.LockPatternUtils.CREDENTIAL_TYPE_PASSWORD;
20 import static com.android.internal.widget.LockPatternUtils.CREDENTIAL_TYPE_PATTERN;
21 import static com.android.internal.widget.LockPatternUtils.CREDENTIAL_TYPE_PIN;
22 
23 import static com.google.common.truth.Truth.assertThat;
24 
25 import static org.mockito.ArgumentMatchers.anyInt;
26 import static org.mockito.Mockito.doReturn;
27 import static org.mockito.Mockito.when;
28 
29 import android.content.Context;
30 import android.os.Flags;
31 import android.platform.test.flag.junit.SetFlagsRule;
32 
33 import androidx.preference.Preference;
34 import androidx.test.core.app.ApplicationProvider;
35 import androidx.test.ext.junit.runners.AndroidJUnit4;
36 
37 import com.android.internal.widget.LockPatternUtils;
38 import com.android.settings.SettingsPreferenceFragment;
39 import com.android.settings.privatespace.onelock.PrivateSpaceLockController;
40 import com.android.settings.testutils.FakeFeatureFactory;
41 
42 import org.junit.Before;
43 import org.junit.Rule;
44 import org.junit.Test;
45 import org.junit.runner.RunWith;
46 import org.mockito.Mock;
47 import org.mockito.MockitoAnnotations;
48 
49 @RunWith(AndroidJUnit4.class)
50 public class PrivateSpaceLockControllerTest {
51     @Mock
52     private Context mContext;
53     @Rule public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();
54 
55     @Mock SettingsPreferenceFragment mSettingsPreferenceFragment;
56     @Mock
57     LockPatternUtils mLockPatternUtils;
58 
59     private Preference mPreference;
60     private PrivateSpaceLockController mPrivateSpaceLockController;
61 
62     /** Required setup before a test. */
63     @Before
setUp()64     public void setUp() {
65         MockitoAnnotations.initMocks(this);
66         mContext = ApplicationProvider.getApplicationContext();
67         final String preferenceKey = "unlock_set_or_change_private_lock";
68 
69         mPreference = new Preference(ApplicationProvider.getApplicationContext());
70         mPreference.setKey(preferenceKey);
71 
72         final FakeFeatureFactory featureFactory = FakeFeatureFactory.setupForTest();
73         when(featureFactory.securityFeatureProvider.getLockPatternUtils(mContext))
74                 .thenReturn(mLockPatternUtils);
75         doReturn(true).when(mLockPatternUtils).isSecure(anyInt());
76 
77         mPrivateSpaceLockController = new PrivateSpaceLockController(mContext,
78                 mSettingsPreferenceFragment);
79     }
80 
81     /** Tests that the controller is always available. */
82     @Test
getAvailabilityStatus_returnsAvailable()83     public void getAvailabilityStatus_returnsAvailable() {
84         mSetFlagsRule.enableFlags(Flags.FLAG_ALLOW_PRIVATE_PROFILE,
85                 android.multiuser.Flags.FLAG_ENABLE_PRIVATE_SPACE_FEATURES);
86 
87         assertThat(mPrivateSpaceLockController.isAvailable()).isEqualTo(true);
88     }
89 
90     /** Tests that preference is disabled and summary says same as device lock. */
91     @Test
getSummary_whenScreenLock()92     public void getSummary_whenScreenLock() {
93         doReturn(false).when(mLockPatternUtils).isSeparateProfileChallengeEnabled(anyInt());
94         mSetFlagsRule.enableFlags(Flags.FLAG_ALLOW_PRIVATE_PROFILE,
95                 android.multiuser.Flags.FLAG_ENABLE_PRIVATE_SPACE_FEATURES);
96 
97         mPrivateSpaceLockController.updateState(mPreference);
98         assertThat(mPreference.isEnabled()).isFalse();
99         assertThat(mPreference.getSummary().toString()).isEqualTo("Same as device screen lock");
100     }
101 
102     /** Tests that preference is enabled and summary is Pattern. */
103     @Test
getSummary_whenProfileLockPattern()104     public void getSummary_whenProfileLockPattern() {
105         doReturn(true)
106                 .when(mLockPatternUtils).isSeparateProfileChallengeEnabled(anyInt());
107         doReturn(CREDENTIAL_TYPE_PATTERN)
108                 .when(mLockPatternUtils).getCredentialTypeForUser(anyInt());
109         mSetFlagsRule.enableFlags(Flags.FLAG_ALLOW_PRIVATE_PROFILE,
110                 android.multiuser.Flags.FLAG_ENABLE_PRIVATE_SPACE_FEATURES);
111 
112         mPrivateSpaceLockController.updateState(mPreference);
113         assertThat(mPreference.isEnabled()).isTrue();
114         assertThat(mPreference.getSummary().toString()).isEqualTo("Pattern");
115     }
116 
117     /** Tests that preference is enabled and summary is Pin. */
118     @Test
getSummary_whenProfileLockPin()119     public void getSummary_whenProfileLockPin() {
120         doReturn(true).when(mLockPatternUtils).isSeparateProfileChallengeEnabled(anyInt());
121         doReturn(CREDENTIAL_TYPE_PIN).when(mLockPatternUtils).getCredentialTypeForUser(anyInt());
122         mSetFlagsRule.enableFlags(Flags.FLAG_ALLOW_PRIVATE_PROFILE,
123                 android.multiuser.Flags.FLAG_ENABLE_PRIVATE_SPACE_FEATURES);
124 
125         mPrivateSpaceLockController.updateState(mPreference);
126         assertThat(mPreference.isEnabled()).isTrue();
127         assertThat(mPreference.getSummary().toString()).isEqualTo("PIN");
128     }
129 
130     /** Tests that preference is enabled and summary is Password. */
131     @Test
getSummary_whenProfileLockPassword()132     public void getSummary_whenProfileLockPassword() {
133         doReturn(true)
134                 .when(mLockPatternUtils).isSeparateProfileChallengeEnabled(anyInt());
135         doReturn(CREDENTIAL_TYPE_PASSWORD)
136                 .when(mLockPatternUtils).getCredentialTypeForUser(anyInt());
137         mSetFlagsRule.enableFlags(Flags.FLAG_ALLOW_PRIVATE_PROFILE,
138                 android.multiuser.Flags.FLAG_ENABLE_PRIVATE_SPACE_FEATURES);
139 
140         mPrivateSpaceLockController.updateState(mPreference);
141         assertThat(mPreference.isEnabled()).isTrue();
142         assertThat(mPreference.getSummary().toString()).isEqualTo("Password");
143     }
144 }
145