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