OSDN Git Service

Index database before getting Uri Descendants
[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.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;
25
26 import android.database.Cursor;
27 import android.text.TextUtils;
28
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;
33
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;
39
40 @RunWith(SettingsRobolectricTestRunner.class)
41 public class SearchIndexableResourcesTest {
42
43     private SearchFeatureProviderImpl mSearchProvider;
44     private FakeFeatureFactory mFakeFeatureFactory;
45
46     @Before
47     public void setUp() {
48         mSearchProvider = new SearchFeatureProviderImpl();
49         mFakeFeatureFactory = FakeFeatureFactory.setupForTest();
50         mFakeFeatureFactory.searchFeatureProvider = mSearchProvider;
51     }
52
53     @After
54     public void cleanUp() {
55         mFakeFeatureFactory.searchFeatureProvider = mock(SearchFeatureProvider.class);
56     }
57
58     @Test
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();
65
66         ((SearchIndexableResourcesImpl) mSearchProvider.getSearchIndexableResources())
67                 .addIndex(String.class);
68
69         assertThat(mSearchProvider.getSearchIndexableResources().getProviderValues())
70                 .contains(String.class);
71         final int afterCount =
72                 mSearchProvider.getSearchIndexableResources().getProviderValues().size();
73         assertThat(afterCount).isEqualTo(beforeCount + 1);
74     }
75
76     @Test
77     public void testIndexHasWifiSettings() {
78         assertThat(mSearchProvider.getSearchIndexableResources().getProviderValues())
79                 .contains(WifiSettings.class);
80     }
81
82     @Test
83     public void testNonIndexableKeys_GetsKeyFromProvider() {
84         mSearchProvider.getSearchIndexableResources().getProviderValues().clear();
85         ((SearchIndexableResourcesImpl) mSearchProvider.getSearchIndexableResources())
86                 .addIndex(FakeIndexProvider.class);
87
88         SettingsSearchIndexablesProvider provider = spy(new SettingsSearchIndexablesProvider());
89
90         when(provider.getContext()).thenReturn(RuntimeEnvironment.application);
91
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)) {
97                 hasTestKey = true;
98                 break;
99             }
100         }
101
102         assertThat(hasTestKey).isTrue();
103     }
104
105     @Test
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");
110             }
111         }
112     }
113 }