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