1 /*
2  * Copyright (C) 2017 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 com.google.common.truth.Truth.assertThat;
20 
21 import android.content.Context;
22 import android.database.Cursor;
23 import android.net.Uri;
24 import android.platform.test.annotations.Presubmit;
25 import android.provider.SearchIndexablesContract;
26 
27 import androidx.test.InstrumentationRegistry;
28 import androidx.test.filters.SmallTest;
29 import androidx.test.runner.AndroidJUnit4;
30 
31 import org.junit.After;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 
36 @RunWith(AndroidJUnit4.class)
37 @SmallTest
38 public class SettingsSearchIndexablesProviderTest {
39 
40     private Context mContext;
41 
42     @Before
setUp()43     public void setUp() {
44         mContext = InstrumentationRegistry.getTargetContext();
45     }
46 
47     @After
cleanUp()48     public void cleanUp() {
49         System.clearProperty(SettingsSearchIndexablesProvider.SYSPROP_CRASH_ON_ERROR);
50     }
51 
52     @Test
testSiteMapPairsFetched()53     public void testSiteMapPairsFetched() {
54         final Uri uri = Uri.parse("content://" + mContext.getPackageName() + "/" +
55                 SearchIndexablesContract.SITE_MAP_PAIRS_PATH);
56         final Cursor cursor = mContext.getContentResolver().query(uri, null, null, null, null);
57 
58         final int size = cursor.getCount();
59         assertThat(size).isGreaterThan(0);
60         while (cursor.moveToNext()) {
61             assertThat(cursor.getString(cursor.getColumnIndexOrThrow(
62                     SearchIndexablesContract.SiteMapColumns.PARENT_CLASS)))
63                     .isNotEmpty();
64             assertThat(cursor.getString(cursor.getColumnIndexOrThrow(
65                     SearchIndexablesContract.SiteMapColumns.CHILD_CLASS)))
66                     .isNotEmpty();
67         }
68     }
69 
70     /**
71      * All {@link Indexable.SearchIndexProvider} should collect a list of non-indexable keys
72      * without crashing. This test enables crashing of individual providers in the indexing pipeline
73      * and checks that there are no crashes.
74      */
75     @Test
76     @Presubmit
nonIndexableKeys_shouldNotCrash()77     public void nonIndexableKeys_shouldNotCrash() {
78         // Allow crashes in the indexing pipeline.
79         System.setProperty(SettingsSearchIndexablesProvider.SYSPROP_CRASH_ON_ERROR,
80                 "enabled");
81 
82         final Uri uri = Uri.parse("content://" + mContext.getPackageName() + "/" +
83                 SearchIndexablesContract.NON_INDEXABLES_KEYS_PATH);
84         mContext.getContentResolver().query(uri, null, null, null, null);
85     }
86 }
87