Merge "Declare "searchable" attribute for preferences."

This commit is contained in:
TreeHugger Robot
2018-08-16 17:20:37 +00:00
committed by Android (Google) Code Review
28 changed files with 201 additions and 148 deletions

View File

@@ -17,6 +17,7 @@
package com.android.settings.applications;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
@@ -26,7 +27,6 @@ import android.os.UserManager;
import com.android.settings.notification.EmergencyBroadcastPreferenceController;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settings.testutils.XmlTestUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -42,19 +42,16 @@ public class AppAndNotificationDashboardFragmentTest {
@Test
@Config(shadows = {ShadowEmergencyBroadcastPreferenceController.class})
public void testNonIndexableKeys_existInXmlLayout() {
public void getNonIndexableKeys_shouldIncludeSpecialAppAccess() {
final Context context = spy(RuntimeEnvironment.application);
UserManager manager = mock(UserManager.class);
when(manager.isAdminUser()).thenReturn(true);
when(context.getSystemService(Context.USER_SERVICE)).thenReturn(manager);
final List<String> niks = AppAndNotificationDashboardFragment.SEARCH_INDEX_DATA_PROVIDER
.getNonIndexableKeys(context);
AppAndNotificationDashboardFragment fragment = new AppAndNotificationDashboardFragment();
final int xmlId = fragment.getPreferenceScreenResId();
final List<String> keys = XmlTestUtils.getKeysFromPreferenceXml(context, xmlId);
assertThat(keys).containsAllIn(niks);
assertThat(niks).contains(
new SpecialAppAccessPreferenceController(context).getPreferenceKey());
}
@Implements(EmergencyBroadcastPreferenceController.class)

View File

@@ -165,7 +165,7 @@ public class DefaultAppSettingsTest {
final List<String> niks = DefaultAppSettings.SEARCH_INDEX_DATA_PROVIDER
.getNonIndexableKeys(context);
final int xmlId = (new DefaultAppSettings()).getPreferenceScreenResId();
final int xmlId = new DefaultAppSettings().getPreferenceScreenResId();
final List<String> keys = XmlTestUtils.getKeysFromPreferenceXml(context, xmlId);

View File

@@ -16,7 +16,10 @@
package com.android.settings.core;
import static com.android.settings.core.PreferenceXmlParserUtils.METADATA_KEY;
import static com.android.settings.core.PreferenceXmlParserUtils.METADATA_KEYWORDS;
import static com.android.settings.core.PreferenceXmlParserUtils.METADATA_SEARCHABLE;
import static com.google.common.truth.Truth.assertThat;
import android.content.Context;
@@ -247,8 +250,7 @@ public class PreferenceXmlParserUtilsTest {
@Test
@Config(qualifiers = "mcc999")
public void extractMetadata_requestIncludesKeywords_shouldContainKeywords()
throws IOException, XmlPullParserException {
public void extractMetadata_requestIncludesKeywords_shouldContainKeywords() throws Exception {
final String expectedKeywords = "a, b, c";
final List<Bundle> metadata = PreferenceXmlParserUtils.extractMetadata(mContext,
R.xml.location_settings,
@@ -260,6 +262,32 @@ public class PreferenceXmlParserUtilsTest {
assertThat(keywords).isEqualTo(expectedKeywords);
}
@Test
public void extractMetadata_requestSearchable_shouldDefaultToTrue() throws Exception {
final List<Bundle> metadata = PreferenceXmlParserUtils.extractMetadata(mContext,
R.xml.display_settings, MetadataFlag.FLAG_NEED_SEARCHABLE);
for (Bundle bundle : metadata) {
assertThat(bundle.getBoolean(METADATA_SEARCHABLE)).isTrue();
}
}
@Test
@Config(qualifiers = "mcc999")
public void extractMetadata_requestSearchable_shouldReturnAttributeValue() throws Exception {
final List<Bundle> metadata = PreferenceXmlParserUtils.extractMetadata(mContext,
R.xml.display_settings,
MetadataFlag.FLAG_NEED_KEY | MetadataFlag.FLAG_NEED_SEARCHABLE);
boolean foundKey = false;
for (Bundle bundle : metadata) {
if (TextUtils.equals(bundle.getString(METADATA_KEY), "pref_key_5")) {
assertThat(bundle.getBoolean(METADATA_SEARCHABLE)).isFalse();
foundKey = true;
break;
}
}
assertThat(foundKey).isTrue();
}
/**
* @param resId the ID for the XML preference
* @return an XML resource parser that points to the start tag

View File

@@ -17,6 +17,7 @@
package com.android.settings.language;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
@@ -25,6 +26,9 @@ import android.content.Context;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TtsEngines;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import org.junit.Before;
@@ -38,9 +42,6 @@ import org.robolectric.annotation.Config;
import java.util.ArrayList;
import java.util.List;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
@RunWith(SettingsRobolectricTestRunner.class)
public class TtsPreferenceControllerTest {
@@ -58,7 +59,8 @@ public class TtsPreferenceControllerTest {
MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application);
mController = new TtsPreferenceController(mContext, mTtsEngines);
mController = new TtsPreferenceController(mContext, "test_key");
mController.mTtsEngines = mTtsEngines;
mPreference = new Preference(RuntimeEnvironment.application);
mPreference.setKey(mController.getPreferenceKey());
when(mScreen.findPreference(mPreference.getKey())).thenReturn(mPreference);

View File

@@ -169,4 +169,24 @@ public class BaseSearchIndexProviderTest {
assertThat(nonIndexableKeys).contains("status_header");
}
@Test
@Config(qualifiers = "mcc999")
public void getNonIndexableKeys_hasSearchableAttributeInXml_shouldSuppressUnsearchable() {
final BaseSearchIndexProvider provider = new BaseSearchIndexProvider() {
@Override
public List<SearchIndexableResource> getXmlResourcesToIndex(Context context,
boolean enabled) {
final SearchIndexableResource sir = new SearchIndexableResource(context);
sir.xmlResId = R.xml.display_settings;
return Collections.singletonList(sir);
}
};
final List<String> nonIndexableKeys =
provider.getNonIndexableKeys(RuntimeEnvironment.application);
assertThat(nonIndexableKeys).contains("pref_key_5");
}
}

View File

@@ -1,6 +1,7 @@
package com.android.settings.search;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
@@ -21,6 +22,9 @@ import org.junit.runner.RunWith;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import java.util.ArrayList;
import java.util.List;
@RunWith(SettingsRobolectricTestRunner.class)
public class SettingsSearchIndexablesProviderTest {
@@ -96,16 +100,19 @@ public class SettingsSearchIndexablesProviderTest {
@Test
@Config(qualifiers = "mcc999")
public void testNonIndexablesColumnFetched() {
Uri rawUri = Uri.parse("content://" + BASE_AUTHORITY + "/" +
final Uri rawUri = Uri.parse("content://" + BASE_AUTHORITY + "/" +
SearchIndexablesContract.NON_INDEXABLES_KEYS_PATH);
final Cursor cursor = mProvider.query(rawUri,
SearchIndexablesContract.NON_INDEXABLES_KEYS_COLUMNS, null, null, null);
final List<String> keys = new ArrayList<>();
cursor.moveToFirst();
assertThat(cursor.getCount()).isEqualTo(2);
assertThat(cursor.getString(0)).isEqualTo("pref_key_1");
cursor.moveToNext();
assertThat(cursor.getString(0)).isEqualTo("pref_key_3");
try (Cursor cursor = mProvider.query(rawUri,
SearchIndexablesContract.NON_INDEXABLES_KEYS_COLUMNS, null, null, null)) {
while (cursor.moveToNext()) {
keys.add(cursor.getString(0));
}
}
assertThat(keys).hasSize(3);
assertThat(keys).containsAllOf("pref_key_1", "pref_key_3", "pref_key_5");
}
}

View File

@@ -73,7 +73,7 @@ public class EncryptionAndCredentialTest {
final List<String> expectedKeys = new ArrayList<>();
for (SearchIndexableResource res : index) {
expectedKeys.addAll(((BaseSearchIndexProvider) SEARCH_INDEX_DATA_PROVIDER)
.getNonIndexableKeysFromXml(mContext, res.xmlResId));
.getNonIndexableKeysFromXml(mContext, res.xmlResId, true /* suppressAll */));
}
final List<String> keys = SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(mContext);

View File

@@ -1,15 +1,16 @@
package com.android.settings.testutils;
import static com.android.settings.core.PreferenceXmlParserUtils.METADATA_KEY;
import static com.android.settings.core.PreferenceXmlParserUtils.MetadataFlag
.FLAG_INCLUDE_PREF_SCREEN;
import static com.android.settings.core.PreferenceXmlParserUtils.MetadataFlag.FLAG_NEED_KEY;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.XmlResourceParser;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Xml;
import com.android.settings.core.PreferenceXmlParserUtils;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import java.util.ArrayList;
@@ -25,30 +26,21 @@ public class XmlTestUtils {
* on the screen.
*
* @param context of the preference screen.
* @param xmlId of the Preference Xml to be parsed.
* @param xmlId of the Preference Xml to be parsed.
* @return List of all keys in the preference Xml
*/
public static List<String> getKeysFromPreferenceXml(Context context, int xmlId) {
final XmlResourceParser parser = context.getResources().getXml(xmlId);
final AttributeSet attrs = Xml.asAttributeSet(parser);
final List<String> keys = new ArrayList<>();
String key;
try {
while (parser.next() != XmlPullParser.END_DOCUMENT) {
try {
key = PreferenceXmlParserUtils.getDataKey(context, attrs);
if (!TextUtils.isEmpty(key)) {
keys.add(key);
}
} catch (NullPointerException e) {
continue;
} catch (Resources.NotFoundException e) {
continue;
List<Bundle> metadata = PreferenceXmlParserUtils.extractMetadata(context, xmlId,
FLAG_NEED_KEY | FLAG_INCLUDE_PREF_SCREEN);
for (Bundle bundle : metadata) {
final String key = bundle.getString(METADATA_KEY);
if (!TextUtils.isEmpty(key)) {
keys.add(key);
}
}
} catch (java.io.IOException e) {
return null;
} catch (XmlPullParserException e) {
} catch (java.io.IOException | XmlPullParserException e) {
return null;
}