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.search;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.mockito.Mockito.doReturn;
22 import static org.mockito.Mockito.spy;
23 
24 import android.content.Context;
25 import android.provider.SearchIndexableResource;
26 
27 import com.android.settings.R;
28 import com.android.settings.core.BasePreferenceController;
29 import com.android.settings.core.PreferenceControllerMixin;
30 import com.android.settingslib.core.AbstractPreferenceController;
31 import com.android.settingslib.search.SearchIndexableRaw;
32 
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.mockito.MockitoAnnotations;
37 import org.robolectric.RobolectricTestRunner;
38 import org.robolectric.RuntimeEnvironment;
39 import org.robolectric.annotation.Config;
40 
41 import java.util.ArrayList;
42 import java.util.Collections;
43 import java.util.List;
44 
45 @RunWith(RobolectricTestRunner.class)
46 public class BaseSearchIndexProviderTest {
47 
48     private static final String TEST_PREF_KEY = "test_pref_key";
49 
50     private Context mContext;
51     private BaseSearchIndexProvider mIndexProvider;
52 
53     @Before
setUp()54     public void setUp() {
55         MockitoAnnotations.initMocks(this);
56         mContext = RuntimeEnvironment.application;
57         mIndexProvider = spy(BaseSearchIndexProvider.class);
58     }
59 
60     @Test
getNonIndexableKeys_noPreferenceController_shouldReturnEmptyList()61     public void getNonIndexableKeys_noPreferenceController_shouldReturnEmptyList() {
62         assertThat(mIndexProvider.getNonIndexableKeys(mContext)).isEmpty();
63     }
64 
65     public static class AvailablePreferenceController
66         extends AbstractPreferenceController
67         implements PreferenceControllerMixin {
AvailablePreferenceController(Context context)68         private AvailablePreferenceController(Context context) {
69             super(context);
70         }
71 
72         @Override
isAvailable()73         public boolean isAvailable() {
74             return true;
75         }
76 
77         @Override
getPreferenceKey()78         public String getPreferenceKey() {
79             return TEST_PREF_KEY;
80         }
81 
82         @Override
updateDynamicRawDataToIndex(List<SearchIndexableRaw> rawData)83         public void updateDynamicRawDataToIndex(List<SearchIndexableRaw> rawData) {
84             final SearchIndexableRaw raw = new SearchIndexableRaw(this.mContext);
85             rawData.add(raw);
86         }
87     }
88 
89     @Test
getNonIndexableKeys_preferenceIsAvailable_shouldReturnEmptyList()90     public void getNonIndexableKeys_preferenceIsAvailable_shouldReturnEmptyList() {
91         List<AbstractPreferenceController> controllers = new ArrayList<>();
92         controllers.add(new AvailablePreferenceController(mContext));
93         doReturn(controllers).when(mIndexProvider).createPreferenceControllers(mContext);
94 
95         assertThat(mIndexProvider.getNonIndexableKeys(mContext)).isEqualTo(Collections.EMPTY_LIST);
96     }
97 
98     @Test
99     @Config(qualifiers = "mcc999")
getAllPreferenceControllers_shouldCreateControllerFromCodeAndXml()100     public void getAllPreferenceControllers_shouldCreateControllerFromCodeAndXml() {
101 
102         final BaseSearchIndexProvider provider = new BaseSearchIndexProvider() {
103             @Override
104             public List<SearchIndexableResource> getXmlResourcesToIndex(Context context,
105                     boolean enabled) {
106                 final SearchIndexableResource sir = new SearchIndexableResource(context);
107                 sir.xmlResId = R.xml.location_settings;
108                 return Collections.singletonList(sir);
109             }
110 
111             @Override
112             public List<AbstractPreferenceController> createPreferenceControllers(Context context) {
113                 final List<AbstractPreferenceController> controllersFromCode = new ArrayList<>();
114                 controllersFromCode.add(new BasePreferenceController(mContext, "TEST_KEY") {
115                     @Override
116                     public int getAvailabilityStatus() {
117                         return AVAILABLE;
118                     }
119                 });
120                 return controllersFromCode;
121             }
122         };
123 
124         final List<AbstractPreferenceController> controllers =
125                 provider.getPreferenceControllers(mContext);
126 
127         assertThat(controllers).hasSize(2);
128     }
129 
130     public static class NotAvailablePreferenceController
131         extends AbstractPreferenceController
132         implements PreferenceControllerMixin {
133 
NotAvailablePreferenceController(Context context)134         private NotAvailablePreferenceController(Context context) {
135             super(context);
136         }
137 
138         @Override
isAvailable()139         public boolean isAvailable() {
140             return false;
141         }
142 
143         @Override
getPreferenceKey()144         public String getPreferenceKey() {
145             return TEST_PREF_KEY;
146         }
147     }
148 
149     @Test
getNonIndexableKeys_preferenceIsNotAvailable_shouldReturnKey()150     public void getNonIndexableKeys_preferenceIsNotAvailable_shouldReturnKey() {
151         List<AbstractPreferenceController> controllers = new ArrayList<>();
152         controllers.add(new NotAvailablePreferenceController(mContext));
153         doReturn(controllers).when(mIndexProvider).createPreferenceControllers(mContext);
154 
155         assertThat(mIndexProvider.getNonIndexableKeys(mContext)).contains(TEST_PREF_KEY);
156     }
157 
158     @Test
getNonIndexableKeys_pageSearchIsDisabled_shouldSuppressEverything()159     public void getNonIndexableKeys_pageSearchIsDisabled_shouldSuppressEverything() {
160         final BaseSearchIndexProvider provider = new BaseSearchIndexProvider() {
161             @Override
162             public List<SearchIndexableResource> getXmlResourcesToIndex(Context context,
163                 boolean enabled) {
164                 final SearchIndexableResource sir = new SearchIndexableResource(context);
165                 sir.xmlResId = R.xml.data_usage;
166                 return Collections.singletonList(sir);
167             }
168 
169             @Override
170             protected boolean isPageSearchEnabled(Context context) {
171                 return false;
172             }
173         };
174 
175         final List<String> nonIndexableKeys =
176             provider.getNonIndexableKeys(RuntimeEnvironment.application);
177 
178         assertThat(nonIndexableKeys).contains("status_header");
179     }
180 
181     @Test
182     @Config(qualifiers = "mcc999")
getNonIndexableKeys_hasSearchableAttributeInXml_shouldSuppressUnsearchable()183     public void getNonIndexableKeys_hasSearchableAttributeInXml_shouldSuppressUnsearchable() {
184         final BaseSearchIndexProvider provider = new BaseSearchIndexProvider() {
185             @Override
186             public List<SearchIndexableResource> getXmlResourcesToIndex(Context context,
187                     boolean enabled) {
188                 final SearchIndexableResource sir = new SearchIndexableResource(context);
189                 sir.xmlResId = R.xml.display_settings;
190                 return Collections.singletonList(sir);
191             }
192 
193         };
194 
195         final List<String> nonIndexableKeys =
196                 provider.getNonIndexableKeys(RuntimeEnvironment.application);
197 
198         assertThat(nonIndexableKeys).contains("pref_key_5");
199     }
200 
201     @Test
getDynamicRawDataToIndex_noPreferenceController_shouldReturnEmptyList()202     public void getDynamicRawDataToIndex_noPreferenceController_shouldReturnEmptyList() {
203         assertThat(mIndexProvider.getDynamicRawDataToIndex(mContext, true)).isEmpty();
204     }
205 
206     @Test
getDynamicRawDataToIndex_disablePageSearch_shouldReturnEmptyList()207     public void getDynamicRawDataToIndex_disablePageSearch_shouldReturnEmptyList() {
208         List<AbstractPreferenceController> controllers = new ArrayList<>();
209         controllers.add(new AvailablePreferenceController(mContext));
210         doReturn(controllers).when(mIndexProvider).createPreferenceControllers(mContext);
211         doReturn(false).when(mIndexProvider).isPageSearchEnabled(mContext);
212 
213         assertThat(mIndexProvider.getDynamicRawDataToIndex(mContext, true)).isEmpty();
214     }
215 
216     @Test
getDynamicRawDataToIndex_hasDynamicRaw_shouldNotEmpty()217     public void getDynamicRawDataToIndex_hasDynamicRaw_shouldNotEmpty() {
218         List<AbstractPreferenceController> controllers = new ArrayList<>();
219         controllers.add(new AvailablePreferenceController(mContext));
220         doReturn(controllers).when(mIndexProvider).createPreferenceControllers(mContext);
221 
222         assertThat(mIndexProvider.getDynamicRawDataToIndex(mContext, true)).isNotEmpty();
223     }
224 }
225