2 * Copyright (C) 2016 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings.search;
19 import static android.provider.SearchIndexablesContract.COLUMN_INDEX_NON_INDEXABLE_KEYS_KEY_VALUE;
20 import static com.google.common.truth.Truth.assertThat;
21 import static junit.framework.Assert.fail;
22 import static org.mockito.Mockito.mock;
23 import static org.mockito.Mockito.spy;
24 import static org.mockito.Mockito.when;
26 import android.database.Cursor;
27 import android.text.TextUtils;
29 import com.android.settings.testutils.FakeFeatureFactory;
30 import com.android.settings.testutils.FakeIndexProvider;
31 import com.android.settings.testutils.SettingsRobolectricTestRunner;
32 import com.android.settings.wifi.WifiSettings;
34 import org.junit.After;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.robolectric.RuntimeEnvironment;
40 @RunWith(SettingsRobolectricTestRunner.class)
41 public class SearchIndexableResourcesTest {
43 private SearchFeatureProviderImpl mSearchProvider;
44 private FakeFeatureFactory mFakeFeatureFactory;
48 mSearchProvider = new SearchFeatureProviderImpl();
49 mFakeFeatureFactory = FakeFeatureFactory.setupForTest();
50 mFakeFeatureFactory.searchFeatureProvider = mSearchProvider;
54 public void cleanUp() {
55 mFakeFeatureFactory.searchFeatureProvider = mock(SearchFeatureProvider.class);
59 public void testAddIndex() {
60 // Confirms that String.class isn't contained in SearchIndexableResources.
61 assertThat(mSearchProvider.getSearchIndexableResources().getProviderValues())
62 .doesNotContain(String.class);
63 final int beforeCount =
64 mSearchProvider.getSearchIndexableResources().getProviderValues().size();
66 ((SearchIndexableResourcesImpl) mSearchProvider.getSearchIndexableResources())
67 .addIndex(String.class);
69 assertThat(mSearchProvider.getSearchIndexableResources().getProviderValues())
70 .contains(String.class);
71 final int afterCount =
72 mSearchProvider.getSearchIndexableResources().getProviderValues().size();
73 assertThat(afterCount).isEqualTo(beforeCount + 1);
77 public void testIndexHasWifiSettings() {
78 assertThat(mSearchProvider.getSearchIndexableResources().getProviderValues())
79 .contains(WifiSettings.class);
83 public void testNonIndexableKeys_GetsKeyFromProvider() {
84 mSearchProvider.getSearchIndexableResources().getProviderValues().clear();
85 ((SearchIndexableResourcesImpl) mSearchProvider.getSearchIndexableResources())
86 .addIndex(FakeIndexProvider.class);
88 SettingsSearchIndexablesProvider provider = spy(new SettingsSearchIndexablesProvider());
90 when(provider.getContext()).thenReturn(RuntimeEnvironment.application);
92 Cursor cursor = provider.queryNonIndexableKeys(null);
93 boolean hasTestKey = false;
94 while (cursor.moveToNext()) {
95 String key = cursor.getString(COLUMN_INDEX_NON_INDEXABLE_KEYS_KEY_VALUE);
96 if (TextUtils.equals(key, FakeIndexProvider.KEY)) {
102 assertThat(hasTestKey).isTrue();
106 public void testAllClassNamesHaveProviders() {
107 for (Class clazz : mSearchProvider.getSearchIndexableResources().getProviderValues()) {
108 if (DatabaseIndexingUtils.getSearchIndexProvider(clazz) == null) {
109 fail(clazz.getName() + "is not an index provider");