Merge "Reuse xml parser logic to scrape preference xml files." into pi-dev
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (C) 2016 The Android Open Source Project
|
||||
* Copyright (C) 2018 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.
|
||||
@@ -12,21 +12,21 @@
|
||||
* 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.
|
||||
*
|
||||
*/
|
||||
|
||||
package com.android.settings.search;
|
||||
package com.android.settings.core;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import android.content.Context;
|
||||
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.R;
|
||||
import com.android.settings.core.PreferenceXmlParserUtils;
|
||||
import com.android.settings.core.PreferenceXmlParserUtils.MetadataFlag;
|
||||
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||
|
||||
import org.junit.Before;
|
||||
@@ -169,8 +169,9 @@ public class PreferenceXmlParserUtilTest {
|
||||
@Config(qualifiers = "mcc999")
|
||||
public void extractMetadata_shouldContainKeyAndControllerName()
|
||||
throws IOException, XmlPullParserException {
|
||||
final List<Bundle> metadata =
|
||||
PreferenceXmlParserUtils.extractMetadata(mContext, R.xml.location_settings);
|
||||
List<Bundle> metadata = PreferenceXmlParserUtils.extractMetadata(mContext,
|
||||
R.xml.location_settings,
|
||||
MetadataFlag.FLAG_NEED_KEY | MetadataFlag.FLAG_NEED_PREF_CONTROLLER);
|
||||
|
||||
assertThat(metadata).isNotEmpty();
|
||||
for (Bundle bundle : metadata) {
|
||||
@@ -179,6 +180,70 @@ public class PreferenceXmlParserUtilTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@Config(qualifiers = "mcc999")
|
||||
public void extractMetadata_requestTitle_shouldContainTitle()
|
||||
throws IOException, XmlPullParserException {
|
||||
List<Bundle> metadata = PreferenceXmlParserUtils.extractMetadata(mContext,
|
||||
R.xml.location_settings, MetadataFlag.FLAG_NEED_PREF_TITLE);
|
||||
for (Bundle bundle : metadata) {
|
||||
assertThat(bundle.getString(PreferenceXmlParserUtils.METADATA_TITLE)).isNotNull();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@Config(qualifiers = "mcc999")
|
||||
public void extractMetadata_requestSummary_shouldContainSummary()
|
||||
throws IOException, XmlPullParserException {
|
||||
List<Bundle> metadata = PreferenceXmlParserUtils.extractMetadata(mContext,
|
||||
R.xml.location_settings, MetadataFlag.FLAG_NEED_PREF_SUMMARY);
|
||||
for (Bundle bundle : metadata) {
|
||||
assertThat(bundle.getString(PreferenceXmlParserUtils.METADATA_SUMMARY)).isNotNull();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@Config(qualifiers = "mcc999")
|
||||
public void extractMetadata_requestIcon_shouldContainIcon()
|
||||
throws IOException, XmlPullParserException {
|
||||
List<Bundle> metadata = PreferenceXmlParserUtils.extractMetadata(mContext,
|
||||
R.xml.location_settings, MetadataFlag.FLAG_NEED_PREF_ICON);
|
||||
for (Bundle bundle : metadata) {
|
||||
assertThat(bundle.getInt(PreferenceXmlParserUtils.METADATA_ICON)).isNotEqualTo(0);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@Config(qualifiers = "mcc999")
|
||||
public void extractMetadata_requestPrefType_shouldContainPrefType()
|
||||
throws IOException, XmlPullParserException {
|
||||
List<Bundle> metadata = PreferenceXmlParserUtils.extractMetadata(mContext,
|
||||
R.xml.location_settings, MetadataFlag.FLAG_NEED_PREF_TYPE);
|
||||
for (Bundle bundle : metadata) {
|
||||
assertThat(bundle.getString(PreferenceXmlParserUtils.METADATA_PREF_TYPE)).isNotNull();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@Config(qualifiers = "mcc999")
|
||||
public void extractMetadata_requestIncludeScreen_shouldContainScreen()
|
||||
throws IOException, XmlPullParserException {
|
||||
List<Bundle> metadata = PreferenceXmlParserUtils.extractMetadata(mContext,
|
||||
R.xml.location_settings,
|
||||
MetadataFlag.FLAG_NEED_PREF_TYPE | MetadataFlag.FLAG_INCLUDE_PREF_SCREEN);
|
||||
|
||||
boolean hasPreferenceScreen = false;
|
||||
for (Bundle bundle : metadata) {
|
||||
if (TextUtils.equals(bundle.getString(PreferenceXmlParserUtils.METADATA_PREF_TYPE),
|
||||
PreferenceXmlParserUtils.PREF_SCREEN_TAG)) {
|
||||
hasPreferenceScreen = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
assertThat(hasPreferenceScreen).isTrue();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param resId the ID for the XML preference
|
||||
* @return an XML resource parser that points to the start tag
|
@@ -20,17 +20,16 @@ import static junit.framework.Assert.fail;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.res.Resources;
|
||||
import android.content.res.XmlResourceParser;
|
||||
import android.os.Bundle;
|
||||
import android.platform.test.annotations.Presubmit;
|
||||
import android.provider.SearchIndexableResource;
|
||||
import android.support.test.InstrumentationRegistry;
|
||||
import android.support.test.filters.MediumTest;
|
||||
import android.support.test.runner.AndroidJUnit4;
|
||||
import android.text.TextUtils;
|
||||
import android.util.AttributeSet;
|
||||
import android.util.Log;
|
||||
import android.util.Xml;
|
||||
|
||||
import com.android.settings.core.PreferenceXmlParserUtils.MetadataFlag;
|
||||
import com.android.settings.overlay.FeatureFactory;
|
||||
import com.android.settings.search.DatabaseIndexingUtils;
|
||||
import com.android.settings.search.Indexable;
|
||||
@@ -40,7 +39,6 @@ import com.android.settings.search.SearchIndexableResources;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.xmlpull.v1.XmlPullParser;
|
||||
import org.xmlpull.v1.XmlPullParserException;
|
||||
|
||||
import java.io.IOException;
|
||||
@@ -56,8 +54,7 @@ public class UniquePreferenceTest {
|
||||
private static final String TAG = "UniquePreferenceTest";
|
||||
private static final List<String> IGNORE_PREF_TYPES = Arrays.asList(
|
||||
"com.android.settingslib.widget.FooterPreference");
|
||||
private static final List<String> SUPPORTED_PREF_TYPES = Arrays.asList(
|
||||
"Preference", "PreferenceCategory", "PreferenceScreen");
|
||||
|
||||
private static final List<String> WHITELISTED_DUPLICATE_KEYS = Arrays.asList(
|
||||
"owner_info_settings", // Lock screen message in security - multiple xml files
|
||||
// contain this because security page is constructed by
|
||||
@@ -177,48 +174,32 @@ public class UniquePreferenceTest {
|
||||
}
|
||||
|
||||
for (SearchIndexableResource sir : resourcesToIndex) {
|
||||
if (sir.xmlResId <= 0) {
|
||||
Log.d(TAG, className + " doesn't have a valid xml to index.");
|
||||
continue;
|
||||
}
|
||||
final XmlResourceParser parser = mContext.getResources().getXml(sir.xmlResId);
|
||||
final List<Bundle> metadata = PreferenceXmlParserUtils.extractMetadata(mContext,
|
||||
sir.xmlResId,
|
||||
MetadataFlag.FLAG_INCLUDE_PREF_SCREEN
|
||||
| MetadataFlag.FLAG_NEED_KEY
|
||||
| MetadataFlag.FLAG_NEED_PREF_TYPE);
|
||||
|
||||
int type;
|
||||
while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
|
||||
&& type != XmlPullParser.START_TAG) {
|
||||
// Parse next until start tag is found
|
||||
}
|
||||
final int outerDepth = parser.getDepth();
|
||||
|
||||
do {
|
||||
if (type != XmlPullParser.START_TAG) {
|
||||
for (Bundle bundle : metadata) {
|
||||
final String type = bundle.getString(PreferenceXmlParserUtils.METADATA_PREF_TYPE);
|
||||
if (IGNORE_PREF_TYPES.contains(type)) {
|
||||
continue;
|
||||
}
|
||||
final String nodeName = parser.getName();
|
||||
if (IGNORE_PREF_TYPES.contains(nodeName)) {
|
||||
continue;
|
||||
}
|
||||
if (!SUPPORTED_PREF_TYPES.contains(nodeName) && !nodeName.endsWith("Preference")) {
|
||||
continue;
|
||||
}
|
||||
final AttributeSet attrs = Xml.asAttributeSet(parser);
|
||||
final String key = PreferenceXmlParserUtils.getDataKey(mContext, attrs);
|
||||
final String key = bundle.getString(PreferenceXmlParserUtils.METADATA_KEY);
|
||||
if (TextUtils.isEmpty(key)) {
|
||||
Log.e(TAG, "Every preference must have an key; found null key"
|
||||
+ " in " + className
|
||||
+ " at " + parser.getPositionDescription());
|
||||
+ " in " + className);
|
||||
nullKeyClasses.add(className);
|
||||
continue;
|
||||
}
|
||||
if (uniqueKeys.contains(key) && !WHITELISTED_DUPLICATE_KEYS.contains(key)) {
|
||||
Log.e(TAG, "Every preference key must unique; found " + nodeName
|
||||
Log.e(TAG, "Every preference key must unique; found "
|
||||
+ " in " + className
|
||||
+ " at " + parser.getPositionDescription());
|
||||
+ " / " + key);
|
||||
duplicatedKeys.add(key);
|
||||
}
|
||||
uniqueKeys.add(key);
|
||||
} while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
|
||||
&& (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user