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.android.settings.security.EncryptionStatusPreferenceController
20         .PREF_KEY_ENCRYPTION_DETAIL_PAGE;
21 import static com.android.settings.security.EncryptionStatusPreferenceController
22         .PREF_KEY_ENCRYPTION_SECURITY_PAGE;
23 
24 import static com.google.common.truth.Truth.assertThat;
25 
26 import android.content.Context;
27 
28 import androidx.preference.Preference;
29 
30 import com.android.settings.R;
31 import com.android.settings.testutils.shadow.ShadowLockPatternUtils;
32 import com.android.settings.testutils.shadow.ShadowUserManager;
33 
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.robolectric.RobolectricTestRunner;
38 import org.robolectric.RuntimeEnvironment;
39 import org.robolectric.annotation.Config;
40 
41 @RunWith(RobolectricTestRunner.class)
42 @Config(shadows = {ShadowLockPatternUtils.class, ShadowUserManager.class})
43 public class EncryptionStatusPreferenceControllerTest {
44 
45     private Context mContext;
46     private EncryptionStatusPreferenceController mController;
47     private Preference mPreference;
48     private ShadowUserManager mShadowUserManager;
49 
50     @Before
setUp()51     public void setUp() {
52         mContext = RuntimeEnvironment.application;
53         mController =
54                 new EncryptionStatusPreferenceController(mContext, PREF_KEY_ENCRYPTION_DETAIL_PAGE);
55         mShadowUserManager = ShadowUserManager.getShadow();
56         mPreference = new Preference(mContext);
57     }
58 
59     @Test
isAvailable_admin_true()60     public void isAvailable_admin_true() {
61         mShadowUserManager.setIsAdminUser(true);
62 
63         assertThat(mController.isAvailable()).isTrue();
64     }
65 
66     @Test
isAvailable_notAdmin_false()67     public void isAvailable_notAdmin_false() {
68         mShadowUserManager.setIsAdminUser(false);
69 
70         assertThat(mController.isAvailable()).isFalse();
71     }
72 
73     @Test
74     @Config(qualifiers = "mcc999")
isAvailable_notVisible_false()75     public void isAvailable_notVisible_false() {
76         assertThat(mController.isAvailable()).isFalse();
77     }
78 
79     @Test
80     @Config(qualifiers = "mcc999")
isAvailable_notVisible_butNotDetailPage_true()81     public void isAvailable_notVisible_butNotDetailPage_true() {
82         mController = new EncryptionStatusPreferenceController(mContext,
83                 PREF_KEY_ENCRYPTION_SECURITY_PAGE);
84 
85         mShadowUserManager.setIsAdminUser(true);
86         assertThat(mController.isAvailable()).isTrue();
87     }
88 
89     @Test
updateSummary_encrypted_shouldSayEncrypted()90     public void updateSummary_encrypted_shouldSayEncrypted() {
91         ShadowLockPatternUtils.setDeviceEncryptionEnabled(true);
92 
93         mController.updateState(mPreference);
94 
95         assertThat(mPreference.getFragment()).isNull();
96         assertThat(mPreference.getSummary())
97                 .isEqualTo(mContext.getText(R.string.crypt_keeper_encrypted_summary));
98     }
99 
100     @Test
updateSummary_unencrypted_shouldHasEncryptionFragment()101     public void updateSummary_unencrypted_shouldHasEncryptionFragment() {
102         ShadowLockPatternUtils.setDeviceEncryptionEnabled(false);
103 
104         mController.updateState(mPreference);
105 
106         final CharSequence summary = mContext.getText(R.string.decryption_settings_summary);
107         assertThat(mPreference.getSummary()).isEqualTo(summary);
108         assertThat(mController.getPreferenceKey()).isNotEqualTo(PREF_KEY_ENCRYPTION_SECURITY_PAGE);
109         assertThat(mPreference.getFragment()).isEqualTo(CryptKeeperSettings.class.getName());
110     }
111 
112     @Test
updateSummary_unencrypted_securityPage_shouldNotHaveEncryptionFragment()113     public void updateSummary_unencrypted_securityPage_shouldNotHaveEncryptionFragment() {
114         mController =
115                 new EncryptionStatusPreferenceController(mContext,
116                         PREF_KEY_ENCRYPTION_SECURITY_PAGE);
117         ShadowLockPatternUtils.setDeviceEncryptionEnabled(false);
118 
119         mController.updateState(mPreference);
120 
121         final CharSequence summary = mContext.getText(R.string.decryption_settings_summary);
122         assertThat(mPreference.getSummary()).isEqualTo(summary);
123 
124         assertThat(mPreference.getFragment()).isNotEqualTo(CryptKeeperSettings.class.getName());
125     }
126 }
127