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.android.settings.search.SearchIndexableResources.NO_RES_ID;
21 import static com.google.common.truth.Truth.assertThat;
22 import static org.mockito.Mockito.spy;
24 import android.database.Cursor;
25 import android.provider.SearchIndexableResource;
26 import android.text.TextUtils;
28 import com.android.settings.TestConfig;
29 import com.android.settings.testutils.SettingsRobolectricTestRunner;
30 import com.android.settings.wifi.WifiSettings;
32 import org.junit.After;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.robolectric.annotation.Config;
38 import java.util.HashMap;
41 @RunWith(SettingsRobolectricTestRunner.class)
42 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
43 public class SearchIndexableResourcesTest {
45 Map<String, SearchIndexableResource> sResMapCopy;
49 sResMapCopy = new HashMap<>(SearchIndexableResources.sResMap);
53 public void cleanUp() {
54 SearchIndexableResources.sResMap.clear();
55 for (String key : sResMapCopy.keySet()) {
56 SearchIndexableResources.sResMap.put(key, sResMapCopy.get(key));
61 public void testAddIndex() {
62 // Confirms that String.class isn't contained in SearchIndexableResources.
63 assertThat(SearchIndexableResources.getResourceByName("java.lang.String")).isNull();
64 final int beforeCount = SearchIndexableResources.values().size();
66 SearchIndexableResources.addIndex(java.lang.String.class);
67 final SearchIndexableResource index = SearchIndexableResources
68 .getResourceByName("java.lang.String");
70 assertThat(index).isNotNull();
71 assertThat(index.className).isEqualTo("java.lang.String");
72 assertThat(index.xmlResId).isEqualTo(NO_RES_ID);
73 assertThat(index.iconResId).isEqualTo(NO_RES_ID);
74 final int afterCount = SearchIndexableResources.values().size();
75 assertThat(afterCount).isEqualTo(beforeCount + 1);
79 public void testIndexHasWifiSettings() {
80 final SearchIndexableResource index = SearchIndexableResources
81 .getResourceByName(WifiSettings.class.getName());
83 assertThat(index).isNotNull();
84 assertThat(index.className).isEqualTo(WifiSettings.class.getName());
85 assertThat(index.xmlResId).isEqualTo(NO_RES_ID);
86 assertThat(index.iconResId).isEqualTo(NO_RES_ID);
90 public void testNonIndexableKeys_GetsKeyFromProvider() {
91 SearchIndexableResources.sResMap.clear();
92 SearchIndexableResources.addIndex(FakeIndexProvider.class);
94 SettingsSearchIndexablesProvider provider = spy(new SettingsSearchIndexablesProvider());
96 Cursor cursor = provider.queryNonIndexableKeys(null);
97 boolean hasTestKey = false;
98 while (cursor.moveToNext()) {
99 String key = cursor.getString(COLUMN_INDEX_NON_INDEXABLE_KEYS_KEY_VALUE);
100 if (TextUtils.equals(key, FakeIndexProvider.KEY)) {
106 assertThat(hasTestKey).isTrue();