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