Remove duplicates between battery and display settings
Change preference keys of duplicate settings between display and battery to avoid duplication in search. Bug: 33701673 Test: make RunSettingsRoboTests Change-Id: I56c82e9e7f163d345065ca478849de9b14c173fe
This commit is contained in:
@@ -70,7 +70,7 @@ public abstract class CodeInspector {
|
||||
return true;
|
||||
}
|
||||
|
||||
protected void initializeGrandfatherList(List<String> grandfather, String filename) {
|
||||
public static void initializeGrandfatherList(List<String> grandfather, String filename) {
|
||||
try {
|
||||
final InputStream in = ShadowApplication.getInstance().getApplicationContext()
|
||||
.getAssets()
|
||||
|
@@ -43,12 +43,13 @@ public class AutoBrightnessPreferenceControllerTest {
|
||||
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
||||
private Context mContext;
|
||||
private AutoBrightnessPreferenceController mController;
|
||||
private final String PREFERENCE_KEY = "auto_brightness";
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
|
||||
mController = new AutoBrightnessPreferenceController(mContext);
|
||||
mController = new AutoBrightnessPreferenceController(mContext, PREFERENCE_KEY);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -72,7 +73,7 @@ public class AutoBrightnessPreferenceControllerTest {
|
||||
@Test
|
||||
public void testPreferenceController_ProperResultPayloadType() {
|
||||
final Context context = ShadowApplication.getInstance().getApplicationContext();
|
||||
mController = new AutoBrightnessPreferenceController(context);
|
||||
mController = new AutoBrightnessPreferenceController(context, PREFERENCE_KEY);
|
||||
ResultPayload payload = mController.getResultPayload();
|
||||
assertThat(payload).isInstanceOf(InlineSwitchPayload.class);
|
||||
}
|
||||
@@ -80,7 +81,7 @@ public class AutoBrightnessPreferenceControllerTest {
|
||||
@Test
|
||||
public void testPreferenceController_CorrectPayload() {
|
||||
final Context context = ShadowApplication.getInstance().getApplicationContext();
|
||||
mController = new AutoBrightnessPreferenceController(context);
|
||||
mController = new AutoBrightnessPreferenceController(context, PREFERENCE_KEY);
|
||||
InlineSwitchPayload payload = (InlineSwitchPayload) mController.getResultPayload();
|
||||
assertThat(payload.settingsUri).isEqualTo("screen_brightness_mode");
|
||||
assertThat(payload.settingSource).isEqualTo(ResultPayload.SettingsSource.SYSTEM);
|
||||
|
@@ -42,11 +42,13 @@ public class TimeoutPreferenceControllerTest {
|
||||
private TimeoutListPreference mPreference;
|
||||
private TimeoutPreferenceController mController;
|
||||
|
||||
private static final String KEY_SCREEN_TIMEOUT = "screen_timeout";
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
|
||||
mController = new TimeoutPreferenceController(mContext);
|
||||
mController = new TimeoutPreferenceController(mContext, KEY_SCREEN_TIMEOUT);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@@ -36,9 +36,11 @@ import com.android.settings.SettingsRobolectricTestRunner;
|
||||
import com.android.settings.TestConfig;
|
||||
import com.android.settings.Utils;
|
||||
import com.android.settings.applications.LayoutPreference;
|
||||
import com.android.settings.core.PreferenceController;
|
||||
import com.android.settings.testutils.FakeFeatureFactory;
|
||||
import com.android.settings.testutils.shadow.SettingsShadowResources;
|
||||
import com.android.settings.testutils.shadow.ShadowDynamicIndexableContentMonitor;
|
||||
import com.android.settings.testutils.XmlTestUtils;
|
||||
import com.android.settingslib.BatteryInfo;
|
||||
|
||||
import org.junit.Before;
|
||||
@@ -423,6 +425,33 @@ public class PowerUsageSummaryTest {
|
||||
TIME_SINCE_LAST_FULL_CHARGE_MS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNonIndexableKeys_MatchPreferenceKeys() {
|
||||
final Context context = RuntimeEnvironment.application;
|
||||
final List<String> niks = PowerUsageSummary.SEARCH_INDEX_DATA_PROVIDER
|
||||
.getNonIndexableKeys(context);
|
||||
|
||||
final List<String> keys = XmlTestUtils.getKeysFromPreferenceXml(context,
|
||||
R.xml.power_usage_summary);
|
||||
|
||||
assertThat(keys).containsAllIn(niks);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPreferenceControllers_getPreferenceKeys_existInPreferenceScreen() {
|
||||
final Context context = RuntimeEnvironment.application;
|
||||
final PowerUsageSummary fragment = new PowerUsageSummary();
|
||||
final List<String> preferenceScreenKeys = XmlTestUtils.getKeysFromPreferenceXml(context,
|
||||
fragment.getPreferenceScreenResId());
|
||||
final List<String> preferenceKeys = new ArrayList<>();
|
||||
|
||||
for (PreferenceController controller : fragment.getPreferenceControllers(context)) {
|
||||
preferenceKeys.add(controller.getPreferenceKey());
|
||||
}
|
||||
|
||||
assertThat(preferenceScreenKeys).containsAllIn(preferenceKeys);
|
||||
}
|
||||
|
||||
public static class TestFragment extends PowerUsageSummary {
|
||||
|
||||
private Context mContext;
|
||||
|
@@ -0,0 +1,123 @@
|
||||
package com.android.settings.search;
|
||||
|
||||
import android.content.Context;
|
||||
import android.provider.SearchIndexableResource;
|
||||
import android.util.ArraySet;
|
||||
import com.android.settings.DateTimeSettings;
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.SecuritySettings;
|
||||
import com.android.settings.SettingsRobolectricTestRunner;
|
||||
import com.android.settings.TestConfig;
|
||||
import com.android.settings.core.codeinspection.CodeInspector;
|
||||
import com.android.settings.datausage.DataUsageSummary;
|
||||
import com.android.settings.search2.DatabaseIndexingUtils;
|
||||
import com.android.settings.testutils.XmlTestUtils;
|
||||
import com.android.settings.testutils.shadow.SettingsShadowResources;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
import org.robolectric.annotation.Config;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
@RunWith(SettingsRobolectricTestRunner.class)
|
||||
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION,
|
||||
assetDir = "/tests/robotests/assets")
|
||||
public class DataIntegrityTest {
|
||||
|
||||
@Test
|
||||
@Config(shadows = {
|
||||
SettingsShadowResources.class,
|
||||
SettingsShadowResources.SettingsShadowTheme.class,
|
||||
})
|
||||
public void testIndexableResources_uniqueKeys() {
|
||||
final Context context = RuntimeEnvironment.application;
|
||||
// Aggregation of all keys
|
||||
final Set<String> masterKeys = new ArraySet<>();
|
||||
// Aggregation of the incorrectly duplicate keys
|
||||
final Set<String> duplicateKeys = new ArraySet<>();
|
||||
// Keys for a specific page
|
||||
final Set<String> pageKeys = new ArraySet<>();
|
||||
// List of all Xml preferences
|
||||
final Set<Integer> xmlList = new ArraySet<>();
|
||||
// Duplicates we know about.
|
||||
List<String> grandfatheredKeys = new ArrayList<>();
|
||||
CodeInspector.initializeGrandfatherList(grandfatheredKeys,
|
||||
"whitelist_duplicate_index_key");
|
||||
|
||||
// Get a list of all Xml.
|
||||
for (SearchIndexableResource val : SearchIndexableResources.values()) {
|
||||
final int xmlResId = val.xmlResId;
|
||||
if (xmlResId != 0) {
|
||||
xmlList.add(xmlResId);
|
||||
} else {
|
||||
// Take class and get all keys
|
||||
final Class clazz = DatabaseIndexingUtils.getIndexableClass(val.className);
|
||||
|
||||
// Skip classes that are invalid or cannot be mocked. Add them as special Xml below.
|
||||
if (clazz == null
|
||||
|| clazz == DateTimeSettings.class
|
||||
|| clazz == DataUsageSummary.class
|
||||
|| clazz == SecuritySettings.class) {
|
||||
continue;
|
||||
}
|
||||
|
||||
Indexable.SearchIndexProvider provider = DatabaseIndexingUtils
|
||||
.getSearchIndexProvider(clazz);
|
||||
|
||||
if (provider == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
List<SearchIndexableResource> subXml =
|
||||
provider.getXmlResourcesToIndex(context, true);
|
||||
|
||||
if (subXml == null) {
|
||||
continue;
|
||||
}
|
||||
for (SearchIndexableResource resource : subXml) {
|
||||
final int subXmlResId = resource.xmlResId;
|
||||
if (subXmlResId != 0) {
|
||||
xmlList.add(subXmlResId);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
addSpecialXml(xmlList);
|
||||
|
||||
// Get keys from all Xml and check for duplicates.
|
||||
for (Integer xmlResId : xmlList) {
|
||||
// Get all keys to be indexed
|
||||
final List<String> prefKeys = XmlTestUtils.getKeysFromPreferenceXml(context, xmlResId);
|
||||
|
||||
pageKeys.addAll(prefKeys);
|
||||
// Remove grandfathered keys.
|
||||
pageKeys.removeAll(grandfatheredKeys);
|
||||
// Find all already-existing keys.
|
||||
pageKeys.retainAll(masterKeys);
|
||||
// Keep list of offending duplicate keys.
|
||||
duplicateKeys.addAll(pageKeys);
|
||||
// Add all keys to master key list.
|
||||
masterKeys.addAll(prefKeys);
|
||||
pageKeys.clear();
|
||||
}
|
||||
assertThat(duplicateKeys).isEmpty();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add XML preferences from Fragments which have issues being instantiated in robolectric.
|
||||
*/
|
||||
private void addSpecialXml(Set<Integer> xmlList) {
|
||||
xmlList.add(R.xml.date_time_prefs);
|
||||
xmlList.add(R.xml.data_usage);
|
||||
xmlList.add(R.xml.data_usage_cellular);
|
||||
xmlList.add(R.xml.data_usage_wifi);
|
||||
xmlList.add(R.xml.security_settings_misc);
|
||||
}
|
||||
|
||||
|
||||
}
|
@@ -19,9 +19,7 @@ package com.android.settings.search;
|
||||
import static android.provider.SearchIndexablesContract.COLUMN_INDEX_NON_INDEXABLE_KEYS_KEY_VALUE;
|
||||
import static com.android.settings.search.SearchIndexableResources.NO_DATA_RES_ID;
|
||||
|
||||
import static com.android.settings.search.SearchIndexableResources.sResMap;
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.spy;
|
||||
|
||||
import android.annotation.DrawableRes;
|
||||
@@ -35,12 +33,14 @@ import com.android.settings.SettingsRobolectricTestRunner;
|
||||
import com.android.settings.TestConfig;
|
||||
import com.android.settings.wifi.WifiSettings;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
import org.robolectric.annotation.Config;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@RunWith(SettingsRobolectricTestRunner.class)
|
||||
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
|
||||
@@ -51,6 +51,21 @@ public class SearchIndexableResourcesTest {
|
||||
@DrawableRes
|
||||
private static final int ICON_RES_ID = R.drawable.ic_settings_language;
|
||||
|
||||
Map<String, SearchIndexableResource> sResMapCopy;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
sResMapCopy = new HashMap<>(SearchIndexableResources.sResMap);
|
||||
}
|
||||
|
||||
@After
|
||||
public void cleanUp() {
|
||||
SearchIndexableResources.sResMap.clear();
|
||||
for (String key : sResMapCopy.keySet()) {
|
||||
SearchIndexableResources.sResMap.put(key, sResMapCopy.get(key));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddIndex() {
|
||||
// Confirms that String.class isn't contained in SearchIndexableResources.
|
||||
|
@@ -0,0 +1,57 @@
|
||||
package com.android.settings.testutils;
|
||||
|
||||
import android.content.res.Resources;
|
||||
import android.content.res.XmlResourceParser;
|
||||
|
||||
import android.content.Context;
|
||||
import android.text.TextUtils;
|
||||
import android.util.AttributeSet;
|
||||
import android.util.Xml;
|
||||
import com.android.settings.search2.XmlParserUtils;
|
||||
import org.xmlpull.v1.XmlPullParser;
|
||||
import org.xmlpull.v1.XmlPullParserException;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Util class for parsing XML
|
||||
*/
|
||||
public class XmlTestUtils {
|
||||
|
||||
/**
|
||||
* Parses a preference screen's xml, collects and returns all keys used by preferences
|
||||
* on the screen.
|
||||
*
|
||||
* @param context of the preference screen.
|
||||
* @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 = XmlParserUtils.getDataKey(context, attrs);
|
||||
if (!TextUtils.isEmpty(key)) {
|
||||
keys.add(key);
|
||||
}
|
||||
} catch (NullPointerException e) {
|
||||
continue;
|
||||
} catch (Resources.NotFoundException e) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
} catch (java.io.IOException e) {
|
||||
return null;
|
||||
} catch (XmlPullParserException e) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return keys;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user