Home | History | Annotate | Download | only in search
      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 import static org.mockito.Mockito.doReturn;
     21 import static org.mockito.Mockito.spy;
     22 
     23 import android.content.Context;
     24 import android.provider.SearchIndexableResource;
     25 
     26 import com.android.settings.R;
     27 import com.android.settings.core.BasePreferenceController;
     28 import com.android.settings.core.PreferenceControllerMixin;
     29 import com.android.settings.testutils.SettingsRobolectricTestRunner;
     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.RuntimeEnvironment;
     37 import org.robolectric.annotation.Config;
     38 
     39 import java.util.ArrayList;
     40 import java.util.Collections;
     41 import java.util.List;
     42 
     43 @RunWith(SettingsRobolectricTestRunner.class)
     44 public class BaseSearchIndexProviderTest {
     45 
     46     private static final String TEST_PREF_KEY = "test_pref_key";
     47 
     48     private Context mContext;
     49     private BaseSearchIndexProvider mIndexProvider;
     50 
     51     @Before
     52     public void setUp() {
     53         MockitoAnnotations.initMocks(this);
     54         mContext = RuntimeEnvironment.application;
     55         mIndexProvider = spy(BaseSearchIndexProvider.class);
     56     }
     57 
     58     @Test
     59     public void getNonIndexableKeys_noPreferenceController_shouldReturnEmptyList() {
     60         assertThat(mIndexProvider.getNonIndexableKeys(mContext)).isEmpty();
     61     }
     62 
     63     public static class AvailablePreferenceController
     64         extends AbstractPreferenceController
     65         implements PreferenceControllerMixin {
     66         private AvailablePreferenceController(Context context) {
     67             super(context);
     68         }
     69 
     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 
     81     @Test
     82     public void getNonIndexableKeys_preferenceIsAvailable_shouldReturnEmptyList() {
     83         List<AbstractPreferenceController> controllers = new ArrayList<>();
     84         controllers.add(new AvailablePreferenceController(mContext));
     85         doReturn(controllers).when(mIndexProvider).createPreferenceControllers(mContext);
     86 
     87         assertThat(mIndexProvider.getNonIndexableKeys(mContext)).isEqualTo(Collections.EMPTY_LIST);
     88     }
     89 
     90     @Test
     91     @Config(qualifiers = "mcc999")
     92     public void getAllPreferenceControllers_shouldCreateControllerFromCodeAndXml() {
     93 
     94         final BaseSearchIndexProvider provider = new BaseSearchIndexProvider() {
     95             @Override
     96             public List<SearchIndexableResource> getXmlResourcesToIndex(Context context,
     97                     boolean enabled) {
     98                 final SearchIndexableResource sir = new SearchIndexableResource(context);
     99                 sir.xmlResId = R.xml.location_settings;
    100                 return Collections.singletonList(sir);
    101             }
    102 
    103             @Override
    104             public List<AbstractPreferenceController> createPreferenceControllers(Context context) {
    105                 final List<AbstractPreferenceController> controllersFromCode = new ArrayList<>();
    106                 controllersFromCode.add(new BasePreferenceController(mContext, "TEST_KEY") {
    107                     @Override
    108                     public int getAvailabilityStatus() {
    109                         return AVAILABLE;
    110                     }
    111                 });
    112                 return controllersFromCode;
    113             }
    114         };
    115 
    116         final List<AbstractPreferenceController> controllers =
    117                 provider.getPreferenceControllers(mContext);
    118 
    119         assertThat(controllers).hasSize(2);
    120     }
    121 
    122     public static class NotAvailablePreferenceController
    123         extends AbstractPreferenceController
    124         implements PreferenceControllerMixin {
    125 
    126         private NotAvailablePreferenceController(Context context) {
    127             super(context);
    128         }
    129 
    130         @Override
    131         public boolean isAvailable() {
    132             return false;
    133         }
    134 
    135         @Override
    136         public String getPreferenceKey() {
    137             return TEST_PREF_KEY;
    138         }
    139     }
    140 
    141     @Test
    142     public void getNonIndexableKeys_preferenceIsNotAvailable_shouldReturnKey() {
    143         List<AbstractPreferenceController> controllers = new ArrayList<>();
    144         controllers.add(new NotAvailablePreferenceController(mContext));
    145         doReturn(controllers).when(mIndexProvider).createPreferenceControllers(mContext);
    146 
    147         assertThat(mIndexProvider.getNonIndexableKeys(mContext)).contains(TEST_PREF_KEY);
    148     }
    149 
    150     @Test
    151     public void getNonIndexableKeys_pageSearchIsDisabled_shouldSuppressEverything() {
    152         final BaseSearchIndexProvider provider = new BaseSearchIndexProvider() {
    153             @Override
    154             public List<SearchIndexableResource> getXmlResourcesToIndex(Context context,
    155                 boolean enabled) {
    156                 final SearchIndexableResource sir = new SearchIndexableResource(context);
    157                 sir.xmlResId = R.xml.data_usage;
    158                 return Collections.singletonList(sir);
    159             }
    160 
    161             @Override
    162             protected boolean isPageSearchEnabled(Context context) {
    163                 return false;
    164             }
    165         };
    166 
    167         final List<String> nonIndexableKeys =
    168             provider.getNonIndexableKeys(RuntimeEnvironment.application);
    169 
    170         assertThat(nonIndexableKeys).contains("status_header");
    171     }
    172 }
    173