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
17package com.android.settings.search;
18
19import static android.provider.SearchIndexablesContract.COLUMN_INDEX_NON_INDEXABLE_KEYS_KEY_VALUE;
20import static com.android.settings.search.SearchIndexableResources.NO_DATA_RES_ID;
21
22import static com.google.common.truth.Truth.assertThat;
23import static org.mockito.Mockito.spy;
24
25import android.annotation.DrawableRes;
26import android.annotation.XmlRes;
27import android.database.Cursor;
28import android.provider.SearchIndexableResource;
29
30import android.text.TextUtils;
31import com.android.settings.R;
32import com.android.settings.testutils.SettingsRobolectricTestRunner;
33import com.android.settings.TestConfig;
34import com.android.settings.wifi.WifiSettings;
35
36import org.junit.After;
37import org.junit.Before;
38import org.junit.Test;
39import org.junit.runner.RunWith;
40import org.robolectric.annotation.Config;
41
42import java.util.HashMap;
43import java.util.Map;
44
45@RunWith(SettingsRobolectricTestRunner.class)
46@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
47public class SearchIndexableResourcesTest {
48
49    @XmlRes
50    private static final int XML_RES_ID = R.xml.physical_keyboard_settings;
51    @DrawableRes
52    private static final int ICON_RES_ID = R.drawable.ic_settings_language;
53
54    Map<String, SearchIndexableResource> sResMapCopy;
55
56    @Before
57    public void setUp() {
58        sResMapCopy = new HashMap<>(SearchIndexableResources.sResMap);
59    }
60
61    @After
62    public void cleanUp() {
63        SearchIndexableResources.sResMap.clear();
64        for (String key : sResMapCopy.keySet()) {
65            SearchIndexableResources.sResMap.put(key, sResMapCopy.get(key));
66        }
67    }
68
69    @Test
70    public void testAddIndex() {
71        // Confirms that String.class isn't contained in SearchIndexableResources.
72        assertThat(SearchIndexableResources.getResourceByName("java.lang.String")).isNull();
73        final int beforeCount = SearchIndexableResources.values().size();
74
75        SearchIndexableResources.addIndex(java.lang.String.class, XML_RES_ID, ICON_RES_ID);
76        final SearchIndexableResource index = SearchIndexableResources
77                .getResourceByName("java.lang.String");
78
79        assertThat(index).isNotNull();
80        assertThat(index.className).isEqualTo("java.lang.String");
81        assertThat(index.xmlResId).isEqualTo(XML_RES_ID);
82        assertThat(index.iconResId).isEqualTo(ICON_RES_ID);
83        final int afterCount = SearchIndexableResources.values().size();
84        assertThat(afterCount).isEqualTo(beforeCount + 1);
85    }
86
87    @Test
88    public void testIndexHasWifiSettings() {
89        final SearchIndexableResource index = SearchIndexableResources
90                .getResourceByName(WifiSettings.class.getName());
91
92        assertThat(index).isNotNull();
93        assertThat(index.className).isEqualTo(WifiSettings.class.getName());
94        assertThat(index.xmlResId).isEqualTo(NO_DATA_RES_ID);
95        assertThat(index.iconResId).isEqualTo(R.drawable.ic_settings_wireless);
96    }
97
98    @Test
99    public void testNonIndexableKeys_GetsKeyFromProvider() {
100        SearchIndexableResources.sResMap.clear();
101        SearchIndexableResources.addIndex(FakeIndexProvider.class, 0, 0);
102
103        SettingsSearchIndexablesProvider provider = spy(new SettingsSearchIndexablesProvider());
104
105        Cursor cursor = provider.queryNonIndexableKeys(null);
106        boolean hasTestKey = false;
107        while(cursor.moveToNext()) {
108            String key = cursor.getString(COLUMN_INDEX_NON_INDEXABLE_KEYS_KEY_VALUE);
109            if (TextUtils.equals(key, FakeIndexProvider.KEY)) {
110                hasTestKey = true;
111                break;
112            }
113        }
114
115        assertThat(hasTestKey).isTrue();
116    }
117}
118