OSDN Git Service

0e3ce502c2f30090c7bfcd0121742f6035bc9a26
[android-x86/packages-apps-Settings.git] / tests / robotests / src / com / android / settings / search / SearchIndexableResourcesTest.java
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 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;
23
24 import android.database.Cursor;
25 import android.provider.SearchIndexableResource;
26 import android.text.TextUtils;
27
28 import com.android.settings.TestConfig;
29 import com.android.settings.testutils.SettingsRobolectricTestRunner;
30 import com.android.settings.wifi.WifiSettings;
31
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;
37
38 import java.util.HashMap;
39 import java.util.Map;
40
41 @RunWith(SettingsRobolectricTestRunner.class)
42 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
43 public class SearchIndexableResourcesTest {
44
45     Map<String, SearchIndexableResource> sResMapCopy;
46
47     @Before
48     public void setUp() {
49         sResMapCopy = new HashMap<>(SearchIndexableResources.sResMap);
50     }
51
52     @After
53     public void cleanUp() {
54         SearchIndexableResources.sResMap.clear();
55         for (String key : sResMapCopy.keySet()) {
56             SearchIndexableResources.sResMap.put(key, sResMapCopy.get(key));
57         }
58     }
59
60     @Test
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();
65
66         SearchIndexableResources.addIndex(java.lang.String.class);
67         final SearchIndexableResource index = SearchIndexableResources
68                 .getResourceByName("java.lang.String");
69
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);
76     }
77
78     @Test
79     public void testIndexHasWifiSettings() {
80         final SearchIndexableResource index = SearchIndexableResources
81                 .getResourceByName(WifiSettings.class.getName());
82
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);
87     }
88
89     @Test
90     public void testNonIndexableKeys_GetsKeyFromProvider() {
91         SearchIndexableResources.sResMap.clear();
92         SearchIndexableResources.addIndex(FakeIndexProvider.class);
93
94         SettingsSearchIndexablesProvider provider = spy(new SettingsSearchIndexablesProvider());
95
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)) {
101                 hasTestKey = true;
102                 break;
103             }
104         }
105
106         assertThat(hasTestKey).isTrue();
107     }
108 }