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