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             public List<SearchIndexableRaw> getRawDataToIndex(Context context, boolean enabled) {
171                 List<SearchIndexableRaw> rawData = super.getRawDataToIndex(context, enabled);
172                 SearchIndexableRaw raw = new SearchIndexableRaw(context);
173                 raw.key = TEST_PREF_KEY;
174                 raw.title = "title";
175                 rawData.add(raw);
176                 return rawData;
177             }
178 
179             @Override
180             protected boolean isPageSearchEnabled(Context context) {
181                 return false;
182             }
183         };
184 
185         final List<String> nonIndexableKeys =
186             provider.getNonIndexableKeys(RuntimeEnvironment.application);
187 
188         assertThat(nonIndexableKeys).contains("status_header");
189         assertThat(nonIndexableKeys).contains(TEST_PREF_KEY);
190     }
191 
192     @Test
193     @Config(qualifiers = "mcc999")
getNonIndexableKeys_hasSearchableAttributeInXml_shouldSuppressUnsearchable()194     public void getNonIndexableKeys_hasSearchableAttributeInXml_shouldSuppressUnsearchable() {
195         final BaseSearchIndexProvider provider = new BaseSearchIndexProvider() {
196             @Override
197             public List<SearchIndexableResource> getXmlResourcesToIndex(Context context,
198                     boolean enabled) {
199                 final SearchIndexableResource sir = new SearchIndexableResource(context);
200                 sir.xmlResId = R.xml.display_settings;
201                 return Collections.singletonList(sir);
202             }
203 
204         };
205 
206         final List<String> nonIndexableKeys =
207                 provider.getNonIndexableKeys(RuntimeEnvironment.application);
208 
209         assertThat(nonIndexableKeys).contains("pref_key_5");
210     }
211 
212     @Test
getDynamicRawDataToIndex_noPreferenceController_shouldReturnEmptyList()213     public void getDynamicRawDataToIndex_noPreferenceController_shouldReturnEmptyList() {
214         assertThat(mIndexProvider.getDynamicRawDataToIndex(mContext, true)).isEmpty();
215     }
216 
217     @Test
getDynamicRawDataToIndex_disablePageSearch_shouldReturnEmptyList()218     public void getDynamicRawDataToIndex_disablePageSearch_shouldReturnEmptyList() {
219         List<AbstractPreferenceController> controllers = new ArrayList<>();
220         controllers.add(new AvailablePreferenceController(mContext));
221         doReturn(controllers).when(mIndexProvider).createPreferenceControllers(mContext);
222         doReturn(false).when(mIndexProvider).isPageSearchEnabled(mContext);
223 
224         assertThat(mIndexProvider.getDynamicRawDataToIndex(mContext, true)).isEmpty();
225     }
226 
227     @Test
getDynamicRawDataToIndex_hasDynamicRaw_shouldNotEmpty()228     public void getDynamicRawDataToIndex_hasDynamicRaw_shouldNotEmpty() {
229         List<AbstractPreferenceController> controllers = new ArrayList<>();
230         controllers.add(new AvailablePreferenceController(mContext));
231         doReturn(controllers).when(mIndexProvider).createPreferenceControllers(mContext);
232 
233         assertThat(mIndexProvider.getDynamicRawDataToIndex(mContext, true)).isNotEmpty();
234     }
235 }
236