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 20 import android.content.Context; 21 import android.provider.SearchIndexableResource; 22 23 import com.android.settings.R; 24 import com.android.settings.SettingsRobolectricTestRunner; 25 import com.android.settings.TestConfig; 26 import com.android.settings.core.PreferenceController; 27 28 import org.junit.Before; 29 import org.junit.Test; 30 import org.junit.runner.RunWith; 31 import org.mockito.Mock; 32 import org.mockito.MockitoAnnotations; 33 import org.robolectric.RuntimeEnvironment; 34 import org.robolectric.annotation.Config; 35 36 import java.util.ArrayList; 37 import java.util.Arrays; 38 import java.util.Collections; 39 import java.util.List; 40 41 import static com.google.common.truth.Truth.assertThat; 42 import static org.mockito.Mockito.doReturn; 43 import static org.mockito.Mockito.spy; 44 45 @RunWith(SettingsRobolectricTestRunner.class) 46 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) 47 public class BaseSearchIndexProviderTest { 48 49 private static final String TEST_PREF_KEY = "test_pref_key"; 50 51 @Mock 52 private Context mContext; 53 private BaseSearchIndexProvider mIndexProvider; 54 55 @Before 56 public void setUp() { 57 MockitoAnnotations.initMocks(this); 58 mIndexProvider = spy(BaseSearchIndexProvider.class); 59 } 60 61 @Test 62 public void getNonIndexableKeys_noPreferenceController_shouldReturnEmptyList() { 63 assertThat(mIndexProvider.getNonIndexableKeys(mContext)).isEqualTo(Collections.EMPTY_LIST); 64 } 65 66 @Test 67 public void getNonIndexableKeys_preferenceIsAvailable_shouldReturnEmptyList() { 68 List<PreferenceController> controllers = new ArrayList<>(); 69 controllers.add(new PreferenceController(mContext) { 70 @Override 71 public boolean isAvailable() { 72 return true; 73 } 74 75 @Override 76 public String getPreferenceKey() { 77 return TEST_PREF_KEY; 78 } 79 }); 80 doReturn(controllers).when(mIndexProvider).getPreferenceControllers(mContext); 81 82 assertThat(mIndexProvider.getNonIndexableKeys(mContext)).isEqualTo(Collections.EMPTY_LIST); 83 } 84 85 @Test 86 public void getNonIndexableKeys_preferenceIsNotAvailable_shouldReturnKey() { 87 List<PreferenceController> controllers = new ArrayList<>(); 88 controllers.add(new PreferenceController(mContext) { 89 @Override 90 public boolean isAvailable() { 91 return false; 92 } 93 94 @Override 95 public String getPreferenceKey() { 96 return TEST_PREF_KEY; 97 } 98 }); 99 doReturn(controllers).when(mIndexProvider).getPreferenceControllers(mContext); 100 101 assertThat(mIndexProvider.getNonIndexableKeys(mContext)).contains(TEST_PREF_KEY); 102 } 103 104 @Test 105 public void getNonIndexableKeys_pageSearchIsDisabled_shouldSuppressEverything() { 106 final BaseSearchIndexProvider provider = new BaseSearchIndexProvider() { 107 @Override 108 public List<SearchIndexableResource> getXmlResourcesToIndex(Context context, 109 boolean enabled) { 110 final SearchIndexableResource sir = new SearchIndexableResource(context); 111 sir.xmlResId = R.xml.data_usage; 112 return Arrays.asList(sir); 113 } 114 115 @Override 116 protected boolean isPageSearchEnabled(Context context) { 117 return false; 118 } 119 }; 120 121 final List<String> nonIndexableKeys = provider 122 .getNonIndexableKeys(RuntimeEnvironment.application); 123 124 assertThat(nonIndexableKeys).containsAllOf("status_header", "limit_summary", 125 "restrict_background"); 126 } 127 } 128