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 
21 import static com.google.common.truth.Truth.assertThat;
22 
23 import static org.junit.Assert.fail;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.spy;
26 import static org.mockito.Mockito.when;
27 
28 import android.database.Cursor;
29 import android.text.TextUtils;
30 
31 import com.android.settings.network.NetworkProviderSettings;
32 import com.android.settings.testutils.FakeFeatureFactory;
33 import com.android.settings.testutils.FakeIndexProvider;
34 import com.android.settingslib.search.SearchIndexableData;
35 
36 import org.junit.After;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 import org.robolectric.RobolectricTestRunner;
41 import org.robolectric.RuntimeEnvironment;
42 import org.robolectric.annotation.Config;
43 
44 @RunWith(RobolectricTestRunner.class)
45 public class SearchIndexableResourcesTest {
46 
47     private SearchFeatureProviderImpl mSearchProvider;
48     private FakeFeatureFactory mFakeFeatureFactory;
49 
50     @Before
setUp()51     public void setUp() {
52         mSearchProvider = new SearchFeatureProviderImpl();
53         mFakeFeatureFactory = FakeFeatureFactory.setupForTest();
54         mFakeFeatureFactory.searchFeatureProvider = mSearchProvider;
55     }
56 
57     @After
cleanUp()58     public void cleanUp() {
59         mFakeFeatureFactory.searchFeatureProvider = mock(SearchFeatureProvider.class);
60     }
61 
62     @Test
testAddIndex()63     public void testAddIndex() {
64         // Confirms that String.class isn't contained in SearchIndexableResources.
65         assertThat(mSearchProvider.getSearchIndexableResources().getProviderValues())
66                 .doesNotContain(String.class);
67         final int beforeCount =
68                 mSearchProvider.getSearchIndexableResources().getProviderValues().size();
69         final SearchIndexableData testBundle = new SearchIndexableData(null, null);
70         mSearchProvider.getSearchIndexableResources().addIndex(testBundle);
71 
72         assertThat(mSearchProvider.getSearchIndexableResources().getProviderValues())
73                 .contains(testBundle);
74         final int afterCount =
75                 mSearchProvider.getSearchIndexableResources().getProviderValues().size();
76         assertThat(afterCount).isEqualTo(beforeCount + 1);
77     }
78 
79     @Test
testIndexHasNetworkProviderSettings()80     public void testIndexHasNetworkProviderSettings() {
81         boolean hasNetworkProvider = false;
82         for (SearchIndexableData bundle :
83                 mSearchProvider.getSearchIndexableResources().getProviderValues()) {
84             if (bundle.getTargetClass().getName().equals(NetworkProviderSettings.class.getName())) {
85                 hasNetworkProvider = true;
86                 break;
87             }
88         }
89         assertThat(hasNetworkProvider).isTrue();
90     }
91 
92     @Test
93     @Config(qualifiers = "mcc999")
testNonIndexableKeys_GetsKeyFromProvider()94     public void testNonIndexableKeys_GetsKeyFromProvider() {
95         mSearchProvider.getSearchIndexableResources().getProviderValues().clear();
96         mSearchProvider.getSearchIndexableResources().addIndex(
97                 new SearchIndexableData(null, FakeIndexProvider.SEARCH_INDEX_DATA_PROVIDER));
98 
99         SettingsSearchIndexablesProvider provider = spy(new SettingsSearchIndexablesProvider());
100 
101         when(provider.getContext()).thenReturn(RuntimeEnvironment.application);
102 
103         Cursor cursor = provider.queryNonIndexableKeys(null);
104         boolean hasTestKey = false;
105         while (cursor.moveToNext()) {
106             String key = cursor.getString(COLUMN_INDEX_NON_INDEXABLE_KEYS_KEY_VALUE);
107             if (TextUtils.equals(key, FakeIndexProvider.KEY)) {
108                 hasTestKey = true;
109                 break;
110             }
111         }
112 
113         assertThat(hasTestKey).isTrue();
114     }
115 
116     @Test
testAllClassNamesHaveProviders()117     public void testAllClassNamesHaveProviders() {
118         for (SearchIndexableData data :
119                 mSearchProvider.getSearchIndexableResources().getProviderValues()) {
120             if (DatabaseIndexingUtils.getSearchIndexProvider(data.getTargetClass()) == null) {
121                 fail(data.getTargetClass().getName() + "is not an index provider");
122             }
123         }
124     }
125 }
126