1 /* 2 * Copyright (C) 2021 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 static org.mockito.ArgumentMatchers.any; 22 import static org.mockito.Mockito.spy; 23 import static org.mockito.Mockito.when; 24 25 import android.content.Context; 26 import android.os.Looper; 27 28 import androidx.test.annotation.UiThreadTest; 29 import androidx.test.core.app.ApplicationProvider; 30 import androidx.test.ext.junit.runners.AndroidJUnit4; 31 32 import com.android.settings.safetycenter.SafetyCenterManagerWrapper; 33 import com.android.settings.testutils.FakeFeatureFactory; 34 import com.android.settings.testutils.ResourcesUtils; 35 import com.android.settingslib.drawer.CategoryKey; 36 37 import org.junit.Before; 38 import org.junit.Test; 39 import org.junit.runner.RunWith; 40 import org.mockito.Mock; 41 import org.mockito.MockitoAnnotations; 42 43 @RunWith(AndroidJUnit4.class) 44 public class SecurityAdvancedSettingsTest { 45 private static final String SCREEN_XML_RESOURCE_NAME = "security_advanced_settings"; 46 private static final String ALTERNATIVE_CATEGORY_KEY = "alternative_category_key"; 47 private static final String LEGACY_CATEGORY_KEY = 48 "com.android.settings.category.ia.legacy_advanced_security"; 49 50 private Context mContext; 51 private SecurityAdvancedSettings mSecurityAdvancedSettings; 52 53 @Mock 54 private SafetyCenterManagerWrapper mSafetyCenterManagerWrapper; 55 56 @Before 57 @UiThreadTest setUp()58 public void setUp() { 59 MockitoAnnotations.initMocks(this); 60 if (Looper.myLooper() == null) { 61 Looper.prepare(); 62 } 63 64 SafetyCenterManagerWrapper.sInstance = mSafetyCenterManagerWrapper; 65 mContext = ApplicationProvider.getApplicationContext(); 66 67 mSecurityAdvancedSettings = spy(new SecurityAdvancedSettings()); 68 when(mSecurityAdvancedSettings.getContext()).thenReturn(mContext); 69 } 70 71 @Test getPreferenceXml_returnsAdvancedSettings()72 public void getPreferenceXml_returnsAdvancedSettings() { 73 assertThat(mSecurityAdvancedSettings.getPreferenceScreenResId()) 74 .isEqualTo(getXmlResId(SCREEN_XML_RESOURCE_NAME)); 75 } 76 77 @Test getCategoryKey_whenSafetyCenterIsEnabled_returnsSecurity()78 public void getCategoryKey_whenSafetyCenterIsEnabled_returnsSecurity() { 79 when(mSafetyCenterManagerWrapper.isEnabled(any())).thenReturn(true); 80 81 assertThat(mSecurityAdvancedSettings.getCategoryKey()) 82 .isEqualTo(CategoryKey.CATEGORY_SECURITY_ADVANCED_SETTINGS); 83 } 84 85 @Test getCategoryKey_whenAlternativeFragmentPresented_returnsAlternative()86 public void getCategoryKey_whenAlternativeFragmentPresented_returnsAlternative() { 87 when(mSafetyCenterManagerWrapper.isEnabled(any(Context.class))).thenReturn(false); 88 setupAlternativeFragment(true, ALTERNATIVE_CATEGORY_KEY); 89 90 assertThat(mSecurityAdvancedSettings.getCategoryKey()) 91 .isEqualTo(ALTERNATIVE_CATEGORY_KEY); 92 } 93 94 @Test getCategoryKey_whenNoAlternativeFragmentPresented_returnsLegacy()95 public void getCategoryKey_whenNoAlternativeFragmentPresented_returnsLegacy() { 96 when(mSafetyCenterManagerWrapper.isEnabled(any(Context.class))).thenReturn(false); 97 setupAlternativeFragment(false, null); 98 99 assertThat(mSecurityAdvancedSettings.getCategoryKey()) 100 .isEqualTo(LEGACY_CATEGORY_KEY); 101 } 102 getXmlResId(String resName)103 private int getXmlResId(String resName) { 104 return ResourcesUtils.getResourcesId(mContext, "xml", resName); 105 } 106 setupAlternativeFragment(boolean hasAlternativeFragment, String alternativeCategoryKey)107 private void setupAlternativeFragment(boolean hasAlternativeFragment, 108 String alternativeCategoryKey) { 109 final FakeFeatureFactory fakeFeatureFactory = FakeFeatureFactory.setupForTest(); 110 when(fakeFeatureFactory.securitySettingsFeatureProvider 111 .hasAlternativeSecuritySettingsFragment()) 112 .thenReturn(hasAlternativeFragment); 113 when(fakeFeatureFactory.securitySettingsFeatureProvider 114 .getAlternativeAdvancedSettingsCategoryKey()) 115 .thenReturn(alternativeCategoryKey); 116 } 117 } 118