1 package com.android.settings.search;
2 
3 import static com.google.common.truth.Truth.assertThat;
4 
5 import static org.mockito.Mockito.mock;
6 import static org.mockito.Mockito.spy;
7 
8 import android.Manifest;
9 import android.content.pm.ProviderInfo;
10 import android.database.Cursor;
11 import android.net.Uri;
12 import android.provider.SearchIndexablesContract;
13 
14 import com.android.settings.R;
15 import com.android.settings.testutils.FakeFeatureFactory;
16 
17 import org.junit.After;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.junit.runner.RunWith;
21 import org.robolectric.RobolectricTestRunner;
22 import org.robolectric.RuntimeEnvironment;
23 import org.robolectric.annotation.Config;
24 
25 import java.util.ArrayList;
26 import java.util.List;
27 
28 @RunWith(RobolectricTestRunner.class)
29 public class SettingsSearchIndexablesProviderTest {
30 
31     private static final String BASE_AUTHORITY = "com.android.settings";
32 
33     private SettingsSearchIndexablesProvider mProvider;
34     private FakeFeatureFactory mFakeFeatureFactory;
35 
36     @Before
setUp()37     public void setUp() {
38         mProvider = spy(new SettingsSearchIndexablesProvider());
39         ProviderInfo info = new ProviderInfo();
40         info.exported = true;
41         info.grantUriPermissions = true;
42         info.authority = BASE_AUTHORITY;
43         info.readPermission = Manifest.permission.READ_SEARCH_INDEXABLES;
44         mProvider.attachInfo(RuntimeEnvironment.application, info);
45 
46         final SearchFeatureProvider featureProvider = new SearchFeatureProviderImpl();
47         featureProvider.getSearchIndexableResources().getProviderValues().clear();
48         featureProvider.getSearchIndexableResources().getProviderValues()
49                 .add(FakeSettingsFragment.class);
50         mFakeFeatureFactory = FakeFeatureFactory.setupForTest();
51         mFakeFeatureFactory.searchFeatureProvider = featureProvider;
52     }
53 
54     @After
cleanUp()55     public void cleanUp() {
56         mFakeFeatureFactory.searchFeatureProvider = mock(SearchFeatureProvider.class);
57     }
58 
59     @Test
testRawColumnFetched()60     public void testRawColumnFetched() {
61         Uri rawUri = Uri.parse("content://" + BASE_AUTHORITY + "/" +
62                 SearchIndexablesContract.INDEXABLES_RAW_PATH);
63 
64         final Cursor cursor = mProvider.query(rawUri,
65                 SearchIndexablesContract.INDEXABLES_RAW_COLUMNS, null, null, null);
66 
67         cursor.moveToFirst();
68         assertThat(cursor.getString(1)).isEqualTo(FakeSettingsFragment.TITLE);
69         assertThat(cursor.getString(2)).isEqualTo(FakeSettingsFragment.SUMMARY_ON);
70         assertThat(cursor.getString(3)).isEqualTo(FakeSettingsFragment.SUMMARY_OFF);
71         assertThat(cursor.getString(4)).isEqualTo(FakeSettingsFragment.ENTRIES);
72         assertThat(cursor.getString(5)).isEqualTo(FakeSettingsFragment.KEYWORDS);
73         assertThat(cursor.getString(6)).isEqualTo(FakeSettingsFragment.SCREEN_TITLE);
74         assertThat(cursor.getString(7)).isEqualTo(FakeSettingsFragment.CLASS_NAME);
75         assertThat(cursor.getInt(8)).isEqualTo(FakeSettingsFragment.ICON);
76         assertThat(cursor.getString(9)).isEqualTo(FakeSettingsFragment.INTENT_ACTION);
77         assertThat(cursor.getString(10)).isEqualTo(FakeSettingsFragment.TARGET_PACKAGE);
78         assertThat(cursor.getString(11)).isEqualTo(FakeSettingsFragment.TARGET_CLASS);
79         assertThat(cursor.getString(12)).isEqualTo(FakeSettingsFragment.KEY);
80     }
81 
82     @Test
testResourcesColumnFetched()83     public void testResourcesColumnFetched() {
84         Uri rawUri = Uri.parse("content://" + BASE_AUTHORITY + "/" +
85                 SearchIndexablesContract.INDEXABLES_XML_RES_PATH);
86 
87         final Cursor cursor = mProvider.query(rawUri,
88                 SearchIndexablesContract.INDEXABLES_XML_RES_COLUMNS, null, null, null);
89 
90         cursor.moveToFirst();
91         assertThat(cursor.getCount()).isEqualTo(1);
92         assertThat(cursor.getInt(1)).isEqualTo(R.xml.display_settings);
93         assertThat(cursor.getString(2)).isEqualTo(FakeSettingsFragment.CLASS_NAME);
94         assertThat(cursor.getInt(3)).isEqualTo(0);
95         assertThat(cursor.getString(4)).isNull();
96         assertThat(cursor.getString(5)).isNull();
97         assertThat(cursor.getString(6)).isNull();
98     }
99 
100     @Test
101     @Config(qualifiers = "mcc999")
testNonIndexablesColumnFetched()102     public void testNonIndexablesColumnFetched() {
103         final Uri rawUri = Uri.parse("content://" + BASE_AUTHORITY + "/" +
104                 SearchIndexablesContract.NON_INDEXABLES_KEYS_PATH);
105 
106         final List<String> keys = new ArrayList<>();
107 
108         try (Cursor cursor = mProvider.query(rawUri,
109                 SearchIndexablesContract.NON_INDEXABLES_KEYS_COLUMNS, null, null, null)) {
110             while (cursor.moveToNext()) {
111                 keys.add(cursor.getString(0));
112             }
113         }
114 
115         assertThat(keys).hasSize(3);
116         assertThat(keys).containsAllOf("pref_key_1", "pref_key_3", "pref_key_5");
117     }
118 }
119