1 /*
2  * Copyright (C) 2017 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 android.content.Context;
22 import android.os.UserManager;
23 
24 import androidx.lifecycle.LifecycleOwner;
25 
26 import com.android.settings.testutils.shadow.ShadowUserManager;
27 import com.android.settingslib.core.lifecycle.Lifecycle;
28 
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.robolectric.RobolectricTestRunner;
33 import org.robolectric.RuntimeEnvironment;
34 import org.robolectric.annotation.Config;
35 
36 @RunWith(RobolectricTestRunner.class)
37 @Config(shadows = ShadowUserManager.class)
38 public class RestrictedEncryptionPreferenceControllerTest {
39 
40     private Context mContext;
41     private ShadowUserManager mUserManager;
42     private InstallCertificatePreferenceController mInstallCertificatePreferenceController;
43     private ResetCredentialsPreferenceController mResetCredentialsPreferenceController;
44     private UserCredentialsPreferenceController mUserCredentialsPreferenceController;
45     private InstallCaCertificatePreferenceController mInstallCaCertificatePreferenceController;
46     private InstallUserCertificatePreferenceController mInstallUserCertificatePreferenceController;
47     private InstallWifiCertificatePreferenceController mInstallWifiCertificatePreferenceController;
48     private Lifecycle mLifecycle;
49     private LifecycleOwner mLifecycleOwner;
50 
51     @Before
setUp()52     public void setUp() {
53         mContext = RuntimeEnvironment.application;
54         mLifecycleOwner = () -> mLifecycle;
55         mLifecycle = new Lifecycle(mLifecycleOwner);
56         mInstallCertificatePreferenceController =
57                 new InstallCertificatePreferenceController(mContext);
58         mResetCredentialsPreferenceController =
59                 new ResetCredentialsPreferenceController(mContext, mLifecycle);
60         mUserCredentialsPreferenceController =
61                 new UserCredentialsPreferenceController(mContext);
62         mInstallCaCertificatePreferenceController =
63                 new InstallCaCertificatePreferenceController(mContext);
64         mInstallUserCertificatePreferenceController =
65                 new InstallUserCertificatePreferenceController(mContext);
66         mInstallWifiCertificatePreferenceController =
67                 new InstallWifiCertificatePreferenceController(mContext);
68         mUserManager = ShadowUserManager.getShadow();
69     }
70 
71     @Test
isAvailable_noRestriction_shouldReturnTrue()72     public void isAvailable_noRestriction_shouldReturnTrue() {
73         assertThat(mInstallCertificatePreferenceController.isAvailable()).isTrue();
74         assertThat(mResetCredentialsPreferenceController.isAvailable()).isTrue();
75         assertThat(mUserCredentialsPreferenceController.isAvailable()).isTrue();
76         assertThat(mInstallCaCertificatePreferenceController.isAvailable()).isTrue();
77         assertThat(mInstallUserCertificatePreferenceController.isAvailable()).isTrue();
78         assertThat(mInstallWifiCertificatePreferenceController.isAvailable()).isTrue();
79     }
80 
81     @Test
isAvailable_hasRestriction_shouldReturnFalse()82     public void isAvailable_hasRestriction_shouldReturnFalse() {
83         mUserManager.addBaseUserRestriction(UserManager.DISALLOW_CONFIG_CREDENTIALS);
84 
85         assertThat(mInstallCertificatePreferenceController.isAvailable()).isFalse();
86         assertThat(mResetCredentialsPreferenceController.isAvailable()).isFalse();
87         assertThat(mUserCredentialsPreferenceController.isAvailable()).isFalse();
88     }
89 }