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