Properly index developer options.

Change-Id: Id9ab629438f52c65fd774341b1200dacb4efd93b
Fix: 37724514
Test: make RunSettingsRoboTests
This commit is contained in:
Fan Zhang
2017-04-26 17:10:52 -07:00
committed by fanzhang172
parent d0417e42f0
commit d57ce3af8f
6 changed files with 55 additions and 38 deletions

View File

@@ -16,6 +16,7 @@
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:settings="http://schemas.android.com/apk/res/com.android.settings" xmlns:settings="http://schemas.android.com/apk/res/com.android.settings"
android:key="development_prefs_screen"
android:title="@string/development_settings_title"> android:title="@string/development_settings_title">
<com.android.settings.BugreportPreference <com.android.settings.BugreportPreference
android:key="bugreport" android:key="bugreport"

View File

@@ -1,18 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2017 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<PreferenceScreen />

View File

@@ -184,7 +184,7 @@ public class ApnSettings extends RestrictedSettingsFragment implements
mUnavailable = isUiRestricted(); mUnavailable = isUiRestricted();
setHasOptionsMenu(!mUnavailable); setHasOptionsMenu(!mUnavailable);
if (mUnavailable) { if (mUnavailable) {
addPreferencesFromResource(R.xml.empty_settings); addPreferencesFromResource(R.xml.placeholder_prefs);
return; return;
} }

View File

@@ -331,12 +331,11 @@ public class DevelopmentSettings extends RestrictedSettingsFragment
private SwitchPreference mColorTemperaturePreference; private SwitchPreference mColorTemperaturePreference;
private final ArrayList<Preference> mAllPrefs = new ArrayList<Preference>(); private final ArrayList<Preference> mAllPrefs = new ArrayList<>();
private final ArrayList<SwitchPreference> mResetSwitchPrefs private final ArrayList<SwitchPreference> mResetSwitchPrefs = new ArrayList<>();
= new ArrayList<SwitchPreference>();
private final HashSet<Preference> mDisabledPrefs = new HashSet<Preference>(); private final HashSet<Preference> mDisabledPrefs = new HashSet<>();
// To track whether a confirmation dialog was clicked. // To track whether a confirmation dialog was clicked.
private boolean mDialogClicked; private boolean mDialogClicked;
private Dialog mEnableDialog; private Dialog mEnableDialog;
@@ -396,7 +395,7 @@ public class DevelopmentSettings extends RestrictedSettingsFragment
// Block access to developer options if the user is not the owner, if user policy // Block access to developer options if the user is not the owner, if user policy
// restricts it, or if the device has not been provisioned // restricts it, or if the device has not been provisioned
mUnavailable = true; mUnavailable = true;
addPreferencesFromResource(R.xml.empty_settings); addPreferencesFromResource(R.xml.placeholder_prefs);
return; return;
} }
@@ -2723,7 +2722,8 @@ public class DevelopmentSettings extends RestrictedSettingsFragment
public static final Indexable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER = public static final Indexable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
new BaseSearchIndexProvider() { new BaseSearchIndexProvider() {
private boolean isShowingDeveloperOptions(Context context) { @Override
protected boolean isPageSearchEnabled(Context context) {
return context.getSharedPreferences(DevelopmentSettings.PREF_FILE, return context.getSharedPreferences(DevelopmentSettings.PREF_FILE,
Context.MODE_PRIVATE).getBoolean( Context.MODE_PRIVATE).getBoolean(
DevelopmentSettings.PREF_SHOW, DevelopmentSettings.PREF_SHOW,
@@ -2734,10 +2734,6 @@ public class DevelopmentSettings extends RestrictedSettingsFragment
public List<SearchIndexableResource> getXmlResourcesToIndex( public List<SearchIndexableResource> getXmlResourcesToIndex(
Context context, boolean enabled) { Context context, boolean enabled) {
if (!isShowingDeveloperOptions(context)) {
return null;
}
final SearchIndexableResource sir = new SearchIndexableResource(context); final SearchIndexableResource sir = new SearchIndexableResource(context);
sir.xmlResId = R.xml.development_prefs; sir.xmlResId = R.xml.development_prefs;
return Arrays.asList(sir); return Arrays.asList(sir);
@@ -2745,11 +2741,8 @@ public class DevelopmentSettings extends RestrictedSettingsFragment
@Override @Override
public List<String> getNonIndexableKeys(Context context) { public List<String> getNonIndexableKeys(Context context) {
if (!isShowingDeveloperOptions(context)) { final List<String> keys = super.getNonIndexableKeys(context);
return null;
}
final List<String> keys = new ArrayList<String>();
if (!showEnableOemUnlockPreference(context)) { if (!showEnableOemUnlockPreference(context)) {
keys.add(ENABLE_OEM_UNLOCK); keys.add(ENABLE_OEM_UNLOCK);
} }

View File

@@ -33,7 +33,6 @@ import org.xmlpull.v1.XmlPullParserException;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import java.util.List;
/** /**
@@ -42,7 +41,6 @@ import java.util.List;
public class BaseSearchIndexProvider implements Indexable.SearchIndexProvider { public class BaseSearchIndexProvider implements Indexable.SearchIndexProvider {
private static final String TAG = "BaseSearchIndex"; private static final String TAG = "BaseSearchIndex";
private static final List<String> EMPTY_LIST = Collections.emptyList();
public BaseSearchIndexProvider() { public BaseSearchIndexProvider() {
} }
@@ -72,7 +70,7 @@ public class BaseSearchIndexProvider implements Indexable.SearchIndexProvider {
} }
return nonIndexableKeys; return nonIndexableKeys;
} else { } else {
return EMPTY_LIST; return new ArrayList<>();
} }
} }
@@ -93,7 +91,7 @@ public class BaseSearchIndexProvider implements Indexable.SearchIndexProvider {
final List<SearchIndexableResource> resources = getXmlResourcesToIndex( final List<SearchIndexableResource> resources = getXmlResourcesToIndex(
context, true /* not used*/); context, true /* not used*/);
if (resources == null || resources.isEmpty()) { if (resources == null || resources.isEmpty()) {
return EMPTY_LIST; return new ArrayList<>();
} }
final List<String> nonIndexableKeys = new ArrayList<>(); final List<String> nonIndexableKeys = new ArrayList<>();
for (SearchIndexableResource res : resources) { for (SearchIndexableResource res : resources) {

View File

@@ -18,13 +18,16 @@ package com.android.settings.development;
import android.app.Activity; import android.app.Activity;
import android.content.Context; import android.content.Context;
import android.provider.SearchIndexableResource;
import android.support.v7.preference.Preference; import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceManager; import android.support.v7.preference.PreferenceManager;
import android.support.v7.preference.PreferenceScreen; import android.support.v7.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settings.SettingsRobolectricTestRunner; import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig; import com.android.settings.TestConfig;
import com.android.settings.testutils.FakeFeatureFactory; import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settings.testutils.shadow.SettingsShadowResources;
import com.android.settingslib.drawer.CategoryKey; import com.android.settingslib.drawer.CategoryKey;
import org.junit.Before; import org.junit.Before;
@@ -33,12 +36,14 @@ import org.junit.runner.RunWith;
import org.mockito.Answers; import org.mockito.Answers;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.MockitoAnnotations; import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config; import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowApplication; import org.robolectric.shadows.ShadowApplication;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Answers.RETURNS_DEEP_STUBS; import static org.mockito.Answers.RETURNS_DEEP_STUBS;
import static org.mockito.Matchers.any; import static org.mockito.Matchers.any;
import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.doReturn;
@@ -48,7 +53,11 @@ import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
@RunWith(SettingsRobolectricTestRunner.class) @RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION,
shadows = {
SettingsShadowResources.class,
SettingsShadowResources.SettingsShadowTheme.class
})
public class DevelopmentSettingsTest { public class DevelopmentSettingsTest {
@Mock(answer = Answers.RETURNS_DEEP_STUBS) @Mock(answer = Answers.RETURNS_DEEP_STUBS)
@@ -90,4 +99,38 @@ public class DevelopmentSettingsTest {
verify(mScreen, times(2)).addPreference(any(Preference.class)); verify(mScreen, times(2)).addPreference(any(Preference.class));
} }
@Test
public void searchIndex_shouldIndexFromPrefXml() {
final List<SearchIndexableResource> index =
DevelopmentSettings.SEARCH_INDEX_DATA_PROVIDER.getXmlResourcesToIndex(
RuntimeEnvironment.application, true);
assertThat(index.size()).isEqualTo(1);
assertThat(index.get(0).xmlResId).isEqualTo(R.xml.development_prefs);
}
@Test
public void searchIndex_pageDisabled_shouldAddAllKeysToNonIndexable() {
final Context appContext = RuntimeEnvironment.application;
new DevelopmentSettingsEnabler(appContext, null /* lifecycle */)
.disableDevelopmentSettings();
final List<String> nonIndexableKeys =
DevelopmentSettings.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(appContext);
assertThat(nonIndexableKeys).contains("development_prefs_screen");
}
@Test
public void searchIndex_pageEnabled_shouldNotAddKeysToNonIndexable() {
final Context appContext = RuntimeEnvironment.application;
new DevelopmentSettingsEnabler(appContext, null /* lifecycle */)
.enableDevelopmentSettings();
final List<String> nonIndexableKeys =
DevelopmentSettings.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(appContext);
assertThat(nonIndexableKeys).doesNotContain("development_prefs_screen");
}
} }