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 import static com.android.settings.core.BasePreferenceController.AVAILABLE;
23 
24 import static com.google.common.truth.Truth.assertThat;
25 
26 import static org.mockito.ArgumentMatchers.anyInt;
27 import static org.mockito.Mockito.doReturn;
28 import static org.mockito.Mockito.when;
29 
30 import android.content.Context;
31 import android.os.Flags;
32 import android.platform.test.flag.junit.SetFlagsRule;
33 
34 import androidx.preference.Preference;
35 import androidx.test.core.app.ApplicationProvider;
36 import androidx.test.ext.junit.runners.AndroidJUnit4;
37 
38 import com.android.internal.widget.LockPatternUtils;
39 import com.android.settings.privatespace.onelock.UseOneLockController;
40 import com.android.settings.testutils.FakeFeatureFactory;
41 
42 import org.junit.Before;
43 import org.junit.Ignore;
44 import org.junit.Rule;
45 import org.junit.Test;
46 import org.junit.runner.RunWith;
47 import org.mockito.Mock;
48 import org.mockito.MockitoAnnotations;
49 
50 @RunWith(AndroidJUnit4.class)
51 public class UseOneLockControllerTest {
52     @Mock private Context mContext;
53     @Rule public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();
54     private UseOneLockController mUseOneLockController;
55     private Preference mPreference;
56 
57     @Mock
58     LockPatternUtils mLockPatternUtils;
59 
60     /** Required setup before a test. */
61     @Before
setUp()62     public void setUp() {
63         MockitoAnnotations.initMocks(this);
64         mContext = ApplicationProvider.getApplicationContext();
65         final String preferenceKey = "private_space_use_one_lock";
66         mPreference = new Preference(mContext);
67 
68         final FakeFeatureFactory featureFactory = FakeFeatureFactory.setupForTest();
69         when(featureFactory.securityFeatureProvider.getLockPatternUtils(mContext))
70                 .thenReturn(mLockPatternUtils);
71         doReturn(true).when(mLockPatternUtils).isSecure(anyInt());
72         mUseOneLockController = new UseOneLockController(mContext, preferenceKey);
73 
74     }
75 
76     /** Tests that the controller is always available. */
77     @Test
getAvailabilityStatus_returnsAvailable()78     public void getAvailabilityStatus_returnsAvailable() {
79         mSetFlagsRule.enableFlags(Flags.FLAG_ALLOW_PRIVATE_PROFILE,
80                 android.multiuser.Flags.FLAG_ENABLE_PRIVATE_SPACE_FEATURES);
81 
82         assertThat(mUseOneLockController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
83     }
84 
85 
86     /** Tests that summary in controller is Pattern. */
87     @Ignore("b/323652985")
88     @Test
getSummary_whenProfileLockPattern()89     public void getSummary_whenProfileLockPattern() {
90         doReturn(true)
91                 .when(mLockPatternUtils).isSeparateProfileChallengeEnabled(anyInt());
92         doReturn(CREDENTIAL_TYPE_PATTERN)
93                 .when(mLockPatternUtils).getCredentialTypeForUser(anyInt());
94         mSetFlagsRule.enableFlags(Flags.FLAG_ALLOW_PRIVATE_PROFILE,
95                 android.multiuser.Flags.FLAG_ENABLE_PRIVATE_SPACE_FEATURES);
96 
97         mUseOneLockController.updateState(mPreference);
98         assertThat(mUseOneLockController.getSummary().toString()).isEqualTo("Pattern");
99     }
100 
101     /** Tests that summary in controller is PIN. */
102     @Ignore("b/323652985")
103     @Test
getSummary_whenProfileLockPin()104     public void getSummary_whenProfileLockPin() {
105         doReturn(true)
106                 .when(mLockPatternUtils).isSeparateProfileChallengeEnabled(anyInt());
107         doReturn(CREDENTIAL_TYPE_PIN).when(mLockPatternUtils).getCredentialTypeForUser(anyInt());
108         mSetFlagsRule.enableFlags(Flags.FLAG_ALLOW_PRIVATE_PROFILE,
109                 android.multiuser.Flags.FLAG_ENABLE_PRIVATE_SPACE_FEATURES);
110 
111         mUseOneLockController.updateState(mPreference);
112         assertThat(mUseOneLockController.getSummary().toString()).isEqualTo("PIN");
113     }
114 
115     /** Tests that summary in controller is Password. */
116     @Ignore("b/323652985")
117     @Test
getSummary_whenProfileLockPassword()118     public void getSummary_whenProfileLockPassword() {
119         doReturn(true)
120                 .when(mLockPatternUtils).isSeparateProfileChallengeEnabled(anyInt());
121         doReturn(CREDENTIAL_TYPE_PASSWORD)
122                 .when(mLockPatternUtils).getCredentialTypeForUser(anyInt());
123         mSetFlagsRule.enableFlags(Flags.FLAG_ALLOW_PRIVATE_PROFILE,
124                 android.multiuser.Flags.FLAG_ENABLE_PRIVATE_SPACE_FEATURES);
125 
126         mUseOneLockController.updateState(mPreference);
127         assertThat(mUseOneLockController.getSummary().toString()).isEqualTo("Password");
128     }
129 }
130