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.android.settings.core.PreferenceXmlParserUtils.METADATA_KEY;
20 import static com.android.settings.core.PreferenceXmlParserUtils.MetadataFlag.FLAG_INCLUDE_PREF_SCREEN;
21 import static com.android.settings.core.PreferenceXmlParserUtils.MetadataFlag.FLAG_NEED_KEY;
22 
23 import static com.google.common.truth.Truth.assertThat;
24 
25 import static org.mockito.Mockito.when;
26 
27 import android.annotation.XmlRes;
28 import android.content.Context;
29 import android.os.Bundle;
30 import android.provider.SearchIndexableResource;
31 
32 import androidx.test.core.app.ApplicationProvider;
33 import androidx.test.ext.junit.runners.AndroidJUnit4;
34 
35 import com.android.settings.core.PreferenceXmlParserUtils;
36 import com.android.settings.search.BaseSearchIndexProvider;
37 import com.android.settings.security.trustagent.TrustAgentManager;
38 import com.android.settings.testutils.FakeFeatureFactory;
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.ArrayList;
47 import java.util.List;
48 
49 
50 @RunWith(AndroidJUnit4.class)
51 public class SecuritySettingsTest {
52 
53     private Context mContext;
54     private SecuritySettingsFeatureProvider mSecuritySettingsFeatureProvider;
55 
56     @Mock
57     private TrustAgentManager mTrustAgentManager;
58 
59     @Before
setup()60     public void setup() {
61         MockitoAnnotations.initMocks(this);
62         mContext = ApplicationProvider.getApplicationContext();
63         FakeFeatureFactory mFeatureFactory = FakeFeatureFactory.setupForTest();
64         mSecuritySettingsFeatureProvider = mFeatureFactory.getSecuritySettingsFeatureProvider();
65         SecurityFeatureProvider mSecurityFeatureProvider =
66                 mFeatureFactory.getSecurityFeatureProvider();
67 
68         when(mSecurityFeatureProvider.getTrustAgentManager()).thenReturn(mTrustAgentManager);
69     }
70 
71     @Test
noAlternativeFragmentAvailable_pageIndexIncluded()72     public void noAlternativeFragmentAvailable_pageIndexIncluded() throws Exception {
73         when(mSecuritySettingsFeatureProvider.hasAlternativeSecuritySettingsFragment())
74                 .thenReturn(false);
75         BaseSearchIndexProvider indexProvider = SecuritySettings.SEARCH_INDEX_DATA_PROVIDER;
76 
77         List<String> allXmlKeys = getAllXmlKeys(indexProvider);
78         List<String> nonIndexableKeys = indexProvider.getNonIndexableKeys(mContext);
79         allXmlKeys.removeAll(nonIndexableKeys);
80 
81         assertThat(allXmlKeys).isNotEmpty();
82     }
83 
84     @Test
alternativeFragmentAvailable_pageIndexExcluded()85     public void alternativeFragmentAvailable_pageIndexExcluded() throws Exception {
86         when(mSecuritySettingsFeatureProvider.hasAlternativeSecuritySettingsFragment())
87                 .thenReturn(true);
88         BaseSearchIndexProvider indexProvider = SecuritySettings.SEARCH_INDEX_DATA_PROVIDER;
89 
90         List<String> allXmlKeys = getAllXmlKeys(indexProvider);
91         List<String> nonIndexableKeys = indexProvider.getNonIndexableKeys(mContext);
92         allXmlKeys.removeAll(nonIndexableKeys);
93 
94         assertThat(allXmlKeys).isEmpty();
95     }
96 
getAllXmlKeys(BaseSearchIndexProvider indexProvider)97     private List<String> getAllXmlKeys(BaseSearchIndexProvider indexProvider) throws Exception {
98         final List<SearchIndexableResource> resources = indexProvider.getXmlResourcesToIndex(
99                 mContext, true /* not used*/);
100         if (resources == null || resources.isEmpty()) {
101             return new ArrayList<>();
102         }
103         final List<String> keys = new ArrayList<>();
104         for (SearchIndexableResource res : resources) {
105             keys.addAll(getKeysFromXml(res.xmlResId));
106         }
107         return keys;
108     }
109 
getKeysFromXml(@mlRes int xmlResId)110     private List<String> getKeysFromXml(@XmlRes int xmlResId) throws Exception {
111         final List<String> keys = new ArrayList<>();
112         final List<Bundle> metadata = PreferenceXmlParserUtils
113                 .extractMetadata(mContext, xmlResId, FLAG_NEED_KEY | FLAG_INCLUDE_PREF_SCREEN);
114         for (Bundle bundle : metadata) {
115             keys.add(bundle.getString(METADATA_KEY));
116         }
117         return keys;
118     }
119 }
120