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 60 @Config(qualifiers = "mcc999") isAvailable_notVisible_false()61 public void isAvailable_notVisible_false() { 62 assertThat(mController.isAvailable()).isFalse(); 63 } 64 65 @Test 66 @Config(qualifiers = "mcc999") isAvailable_notVisible_butNotDetailPage_true()67 public void isAvailable_notVisible_butNotDetailPage_true() { 68 mController = new EncryptionStatusPreferenceController(mContext, 69 PREF_KEY_ENCRYPTION_SECURITY_PAGE); 70 71 assertThat(mController.isAvailable()).isTrue(); 72 } 73 74 @Test updateSummary_encrypted_shouldSayEncrypted()75 public void updateSummary_encrypted_shouldSayEncrypted() { 76 ShadowLockPatternUtils.setDeviceEncryptionEnabled(true); 77 78 mController.updateState(mPreference); 79 80 assertThat(mPreference.getFragment()).isNull(); 81 assertThat(mPreference.getSummary()) 82 .isEqualTo(mContext.getText(R.string.encrypted_summary)); 83 } 84 85 @Test updateSummary_unencrypted_shouldSayUnencrypted()86 public void updateSummary_unencrypted_shouldSayUnencrypted() { 87 ShadowLockPatternUtils.setDeviceEncryptionEnabled(false); 88 89 mController.updateState(mPreference); 90 91 final CharSequence summary = mContext.getText(R.string.not_encrypted_summary); 92 assertThat(mPreference.getSummary()).isEqualTo(summary); 93 assertThat(mController.getPreferenceKey()).isNotEqualTo(PREF_KEY_ENCRYPTION_SECURITY_PAGE); 94 assertThat(mPreference.getFragment()).isNull(); 95 } 96 97 @Test updateSummary_unencrypted_securityPage_shouldSayUnencrypted()98 public void updateSummary_unencrypted_securityPage_shouldSayUnencrypted() { 99 mController = 100 new EncryptionStatusPreferenceController(mContext, 101 PREF_KEY_ENCRYPTION_SECURITY_PAGE); 102 ShadowLockPatternUtils.setDeviceEncryptionEnabled(false); 103 104 mController.updateState(mPreference); 105 106 final CharSequence summary = mContext.getText(R.string.not_encrypted_summary); 107 assertThat(mPreference.getSummary()).isEqualTo(summary); 108 assertThat(mPreference.getFragment()).isNull(); 109 } 110 } 111