1 /* 2 * Copyright (C) 2016 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 android.provider.SearchIndexablesContract.COLUMN_INDEX_NON_INDEXABLE_KEYS_KEY_VALUE; 20 21 import static com.google.common.truth.Truth.assertThat; 22 23 import static org.junit.Assert.fail; 24 import static org.mockito.Mockito.mock; 25 import static org.mockito.Mockito.spy; 26 import static org.mockito.Mockito.when; 27 28 import android.database.Cursor; 29 import android.text.TextUtils; 30 31 import com.android.settings.network.NetworkProviderSettings; 32 import com.android.settings.spa.search.SearchablePage; 33 import com.android.settings.testutils.FakeFeatureFactory; 34 import com.android.settings.testutils.FakeIndexProvider; 35 import com.android.settingslib.search.SearchIndexableData; 36 37 import org.junit.After; 38 import org.junit.Before; 39 import org.junit.Test; 40 import org.junit.runner.RunWith; 41 import org.robolectric.RobolectricTestRunner; 42 import org.robolectric.RuntimeEnvironment; 43 import org.robolectric.annotation.Config; 44 45 @RunWith(RobolectricTestRunner.class) 46 public class SearchIndexableResourcesTest { 47 48 private SearchFeatureProviderImpl mSearchProvider; 49 private FakeFeatureFactory mFakeFeatureFactory; 50 51 @Before setUp()52 public void setUp() { 53 mSearchProvider = new SearchFeatureProviderImpl(); 54 mFakeFeatureFactory = FakeFeatureFactory.setupForTest(); 55 mFakeFeatureFactory.searchFeatureProvider = mSearchProvider; 56 } 57 58 @After cleanUp()59 public void cleanUp() { 60 mFakeFeatureFactory.searchFeatureProvider = mock(SearchFeatureProvider.class); 61 } 62 63 @Test testAddIndex()64 public void testAddIndex() { 65 // Confirms that String.class isn't contained in SearchIndexableResources. 66 assertThat(mSearchProvider.getSearchIndexableResources().getProviderValues()) 67 .doesNotContain(String.class); 68 final int beforeCount = 69 mSearchProvider.getSearchIndexableResources().getProviderValues().size(); 70 final SearchIndexableData testBundle = new SearchIndexableData(null, null); 71 mSearchProvider.getSearchIndexableResources().addIndex(testBundle); 72 73 assertThat(mSearchProvider.getSearchIndexableResources().getProviderValues()) 74 .contains(testBundle); 75 final int afterCount = 76 mSearchProvider.getSearchIndexableResources().getProviderValues().size(); 77 assertThat(afterCount).isEqualTo(beforeCount + 1); 78 } 79 80 @Test testIndexHasNetworkProviderSettings()81 public void testIndexHasNetworkProviderSettings() { 82 boolean hasNetworkProvider = false; 83 for (SearchIndexableData bundle : 84 mSearchProvider.getSearchIndexableResources().getProviderValues()) { 85 if (bundle.getTargetClass().getName().equals(NetworkProviderSettings.class.getName())) { 86 hasNetworkProvider = true; 87 break; 88 } 89 } 90 assertThat(hasNetworkProvider).isTrue(); 91 } 92 93 @Test 94 @Config(qualifiers = "mcc999") testNonIndexableKeys_GetsKeyFromProvider()95 public void testNonIndexableKeys_GetsKeyFromProvider() { 96 mSearchProvider.getSearchIndexableResources().getProviderValues().clear(); 97 mSearchProvider.getSearchIndexableResources().addIndex( 98 new SearchIndexableData(null, FakeIndexProvider.SEARCH_INDEX_DATA_PROVIDER)); 99 100 SettingsSearchIndexablesProvider provider = spy(new SettingsSearchIndexablesProvider()); 101 102 when(provider.getContext()).thenReturn(RuntimeEnvironment.application); 103 104 Cursor cursor = provider.queryNonIndexableKeys(null); 105 boolean hasTestKey = false; 106 while (cursor.moveToNext()) { 107 String key = cursor.getString(COLUMN_INDEX_NON_INDEXABLE_KEYS_KEY_VALUE); 108 if (TextUtils.equals(key, FakeIndexProvider.KEY)) { 109 hasTestKey = true; 110 break; 111 } 112 } 113 114 assertThat(hasTestKey).isTrue(); 115 } 116 117 @Test testAllClassNamesHaveProviders()118 public void testAllClassNamesHaveProviders() { 119 for (SearchIndexableData data : 120 mSearchProvider.getSearchIndexableResources().getProviderValues()) { 121 Class<?> targetClass = data.getTargetClass(); 122 if (DatabaseIndexingUtils.getSearchIndexProvider(targetClass) == null 123 && !SearchablePage.class.isAssignableFrom(targetClass)) { 124 fail(targetClass.getName() + " is not an index provider"); 125 } 126 } 127 } 128 } 129