Add PrefController in XML support
Add the ability to define a Preference Controller in xml using the 'controller' tag. This is useful for two reasons: - It allows the controllers to be instantiated via reflection for Slices and Dashboard fragment - Removes the requirement that controllers be defined manually in Fragments In order to be instantiable, they must have a unified construction following either: ClassName(Context) ClassName(Context, String) Also added a robotest that verifies that all controllers defined in XML follow the constructor schema, and extend BasePreferenceController. Test: robotests Bug: 67996923 Change-Id: I304b35dc666daebecf0c9e286696f3f2a510704a
This commit is contained in:
@@ -97,6 +97,7 @@
|
|||||||
<!-- For Search -->
|
<!-- For Search -->
|
||||||
<declare-styleable name="Preference">
|
<declare-styleable name="Preference">
|
||||||
<attr name="keywords" format="string" />
|
<attr name="keywords" format="string" />
|
||||||
|
<attr name="controller" format="string" />
|
||||||
</declare-styleable>
|
</declare-styleable>
|
||||||
|
|
||||||
<!-- For DotsPageIndicator -->
|
<!-- For DotsPageIndicator -->
|
||||||
|
@@ -25,7 +25,8 @@
|
|||||||
<Preference
|
<Preference
|
||||||
android:key="brightness"
|
android:key="brightness"
|
||||||
android:title="@string/brightness"
|
android:title="@string/brightness"
|
||||||
settings:keywords="@string/keywords_display_brightness_level">
|
settings:keywords="@string/keywords_display_brightness_level"
|
||||||
|
settings:controller="com.android.settings.display.AutoBrightnessPreferenceController">
|
||||||
<intent android:action="com.android.intent.action.SHOW_BRIGHTNESS_DIALOG" />
|
<intent android:action="com.android.intent.action.SHOW_BRIGHTNESS_DIALOG" />
|
||||||
</Preference>
|
</Preference>
|
||||||
|
|
||||||
|
@@ -26,7 +26,8 @@
|
|||||||
android:title="@string/gesture_preference_title"
|
android:title="@string/gesture_preference_title"
|
||||||
android:icon="@drawable/ic_settings_gestures"
|
android:icon="@drawable/ic_settings_gestures"
|
||||||
android:order="-250"
|
android:order="-250"
|
||||||
android:fragment="com.android.settings.gestures.GestureSettings" />
|
android:fragment="com.android.settings.gestures.GestureSettings"
|
||||||
|
settings:controller="com.android.settings.gestures.GesturesSettingPreferenceController"/>
|
||||||
|
|
||||||
<!-- Backup -->
|
<!-- Backup -->
|
||||||
<Preference
|
<Preference
|
||||||
@@ -34,7 +35,8 @@
|
|||||||
android:title="@string/privacy_settings_title"
|
android:title="@string/privacy_settings_title"
|
||||||
android:summary="@string/summary_placeholder"
|
android:summary="@string/summary_placeholder"
|
||||||
android:icon="@drawable/ic_settings_backup"
|
android:icon="@drawable/ic_settings_backup"
|
||||||
android:order="-60">
|
android:order="-60"
|
||||||
|
settings:controller="com.android.settings.backup.BackupSettingsActivityPreferenceController">
|
||||||
<intent android:action="android.settings.BACKUP_AND_RESET_SETTINGS" />
|
<intent android:action="android.settings.BACKUP_AND_RESET_SETTINGS" />
|
||||||
</Preference>
|
</Preference>
|
||||||
|
|
||||||
@@ -44,14 +46,16 @@
|
|||||||
android:title="@string/system_update_settings_list_item_title"
|
android:title="@string/system_update_settings_list_item_title"
|
||||||
android:summary="@string/summary_placeholder"
|
android:summary="@string/summary_placeholder"
|
||||||
android:icon="@drawable/ic_system_update"
|
android:icon="@drawable/ic_system_update"
|
||||||
android:order="-30">
|
android:order="-30"
|
||||||
|
settings:controller="com.android.settings.deviceinfo.SystemUpdatePreferenceController">
|
||||||
<intent android:action="android.settings.SYSTEM_UPDATE_SETTINGS" />
|
<intent android:action="android.settings.SYSTEM_UPDATE_SETTINGS" />
|
||||||
</Preference>
|
</Preference>
|
||||||
|
|
||||||
<Preference
|
<Preference
|
||||||
android:key="additional_system_update_settings"
|
android:key="additional_system_update_settings"
|
||||||
android:title="@string/additional_system_update_settings_list_item_title"
|
android:title="@string/additional_system_update_settings_list_item_title"
|
||||||
android:order="-31">
|
android:order="-31"
|
||||||
|
settings:controller="com.android.settings.deviceinfo.AdditionalSystemUpdatePreferenceController">
|
||||||
<intent android:action="android.intent.action.MAIN"
|
<intent android:action="android.intent.action.MAIN"
|
||||||
android:targetPackage="@string/additional_system_update"
|
android:targetPackage="@string/additional_system_update"
|
||||||
android:targetClass="@string/additional_system_update_menu" />
|
android:targetClass="@string/additional_system_update_menu" />
|
||||||
|
@@ -22,31 +22,29 @@ import android.os.UserManager;
|
|||||||
import android.support.v7.preference.Preference;
|
import android.support.v7.preference.Preference;
|
||||||
|
|
||||||
import com.android.settings.R;
|
import com.android.settings.R;
|
||||||
|
import com.android.settings.core.BasePreferenceController;
|
||||||
import com.android.settings.core.PreferenceControllerMixin;
|
import com.android.settings.core.PreferenceControllerMixin;
|
||||||
import com.android.settingslib.core.AbstractPreferenceController;
|
import com.android.settingslib.core.AbstractPreferenceController;
|
||||||
|
|
||||||
public class BackupSettingsActivityPreferenceController extends
|
public class BackupSettingsActivityPreferenceController extends BasePreferenceController {
|
||||||
AbstractPreferenceController implements PreferenceControllerMixin {
|
private static final String TAG = "BackupSettingActivityPC";
|
||||||
|
|
||||||
private static final String KEY_BACKUP_SETTINGS = "backup_settings";
|
private static final String KEY_BACKUP_SETTINGS = "backup_settings";
|
||||||
private static final String TAG = "BackupSettingActivityPC" ;
|
|
||||||
|
|
||||||
private final UserManager mUm;
|
private final UserManager mUm;
|
||||||
private final BackupManager mBackupManager;
|
private final BackupManager mBackupManager;
|
||||||
|
|
||||||
public BackupSettingsActivityPreferenceController(Context context) {
|
public BackupSettingsActivityPreferenceController(Context context) {
|
||||||
super(context);
|
super(context, KEY_BACKUP_SETTINGS);
|
||||||
mUm = (UserManager) context.getSystemService(Context.USER_SERVICE);
|
mUm = (UserManager) context.getSystemService(Context.USER_SERVICE);
|
||||||
mBackupManager = new BackupManager(context);
|
mBackupManager = new BackupManager(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isAvailable() {
|
public int getAvailabilityStatus() {
|
||||||
return mUm.isAdminUser();
|
return mUm.isAdminUser()
|
||||||
}
|
? AVAILABLE
|
||||||
|
: DISABLED_UNSUPPORTED;
|
||||||
@Override
|
|
||||||
public String getPreferenceKey() {
|
|
||||||
return KEY_BACKUP_SETTINGS;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@@ -17,26 +17,23 @@ package com.android.settings.deviceinfo;
|
|||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
|
||||||
|
import com.android.settings.core.BasePreferenceController;
|
||||||
import com.android.settings.core.PreferenceControllerMixin;
|
import com.android.settings.core.PreferenceControllerMixin;
|
||||||
import com.android.settingslib.core.AbstractPreferenceController;
|
import com.android.settingslib.core.AbstractPreferenceController;
|
||||||
|
|
||||||
public class AdditionalSystemUpdatePreferenceController extends
|
public class AdditionalSystemUpdatePreferenceController extends BasePreferenceController {
|
||||||
AbstractPreferenceController implements PreferenceControllerMixin {
|
|
||||||
|
|
||||||
private static final String KEY_UPDATE_SETTING = "additional_system_update_settings";
|
private static final String KEY_UPDATE_SETTING = "additional_system_update_settings";
|
||||||
|
|
||||||
public AdditionalSystemUpdatePreferenceController(Context context) {
|
public AdditionalSystemUpdatePreferenceController(Context context) {
|
||||||
super(context);
|
super(context, KEY_UPDATE_SETTING);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isAvailable() {
|
public int getAvailabilityStatus() {
|
||||||
return mContext.getResources().getBoolean(
|
return mContext.getResources().getBoolean(
|
||||||
com.android.settings.R.bool.config_additional_system_update_setting_enable);
|
com.android.settings.R.bool.config_additional_system_update_setting_enable)
|
||||||
}
|
? AVAILABLE
|
||||||
|
: DISABLED_UNSUPPORTED;
|
||||||
@Override
|
|
||||||
public String getPreferenceKey() {
|
|
||||||
return KEY_UPDATE_SETTING;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -30,11 +30,9 @@ import android.util.Log;
|
|||||||
|
|
||||||
import com.android.settings.R;
|
import com.android.settings.R;
|
||||||
import com.android.settings.Utils;
|
import com.android.settings.Utils;
|
||||||
import com.android.settings.core.PreferenceControllerMixin;
|
import com.android.settings.core.BasePreferenceController;
|
||||||
import com.android.settingslib.core.AbstractPreferenceController;
|
|
||||||
|
|
||||||
public class SystemUpdatePreferenceController extends AbstractPreferenceController implements
|
public class SystemUpdatePreferenceController extends BasePreferenceController {
|
||||||
PreferenceControllerMixin {
|
|
||||||
|
|
||||||
private static final String TAG = "SysUpdatePrefContr";
|
private static final String TAG = "SysUpdatePrefContr";
|
||||||
|
|
||||||
@@ -42,19 +40,16 @@ public class SystemUpdatePreferenceController extends AbstractPreferenceControll
|
|||||||
|
|
||||||
private final UserManager mUm;
|
private final UserManager mUm;
|
||||||
|
|
||||||
public SystemUpdatePreferenceController(Context context, UserManager um) {
|
public SystemUpdatePreferenceController(Context context) {
|
||||||
super(context);
|
super(context, KEY_SYSTEM_UPDATE_SETTINGS);
|
||||||
mUm = um;
|
mUm = UserManager.get(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isAvailable() {
|
public int getAvailabilityStatus() {
|
||||||
return mUm.isAdminUser();
|
return mUm.isAdminUser()
|
||||||
}
|
? AVAILABLE
|
||||||
|
: DISABLED_UNSUPPORTED;
|
||||||
@Override
|
|
||||||
public String getPreferenceKey() {
|
|
||||||
return KEY_SYSTEM_UPDATE_SETTINGS;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -62,14 +57,14 @@ public class SystemUpdatePreferenceController extends AbstractPreferenceControll
|
|||||||
super.displayPreference(screen);
|
super.displayPreference(screen);
|
||||||
if (isAvailable()) {
|
if (isAvailable()) {
|
||||||
Utils.updatePreferenceToSpecificActivityOrRemove(mContext, screen,
|
Utils.updatePreferenceToSpecificActivityOrRemove(mContext, screen,
|
||||||
KEY_SYSTEM_UPDATE_SETTINGS,
|
getPreferenceKey(),
|
||||||
Utils.UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY);
|
Utils.UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean handlePreferenceTreeClick(Preference preference) {
|
public boolean handlePreferenceTreeClick(Preference preference) {
|
||||||
if (KEY_SYSTEM_UPDATE_SETTINGS.equals(preference.getKey())) {
|
if (TextUtils.equals(getPreferenceKey(), preference.getKey())) {
|
||||||
CarrierConfigManager configManager =
|
CarrierConfigManager configManager =
|
||||||
(CarrierConfigManager) mContext.getSystemService(CARRIER_CONFIG_SERVICE);
|
(CarrierConfigManager) mContext.getSystemService(CARRIER_CONFIG_SERVICE);
|
||||||
PersistableBundle b = configManager.getConfig();
|
PersistableBundle b = configManager.getConfig();
|
||||||
|
@@ -23,27 +23,26 @@ import android.support.v7.preference.Preference;
|
|||||||
|
|
||||||
import com.android.internal.hardware.AmbientDisplayConfiguration;
|
import com.android.internal.hardware.AmbientDisplayConfiguration;
|
||||||
import com.android.settings.R;
|
import com.android.settings.R;
|
||||||
|
import com.android.settings.core.BasePreferenceController;
|
||||||
import com.android.settings.core.PreferenceControllerMixin;
|
import com.android.settings.core.PreferenceControllerMixin;
|
||||||
import com.android.settings.overlay.FeatureFactory;
|
import com.android.settings.overlay.FeatureFactory;
|
||||||
import com.android.settingslib.core.AbstractPreferenceController;
|
import com.android.settingslib.core.AbstractPreferenceController;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class GesturesSettingPreferenceController extends AbstractPreferenceController
|
public class GesturesSettingPreferenceController extends BasePreferenceController {
|
||||||
implements PreferenceControllerMixin {
|
|
||||||
|
|
||||||
private static final String KEY_GESTURES_SETTINGS = "gesture_settings";
|
|
||||||
|
|
||||||
private final AssistGestureFeatureProvider mFeatureProvider;
|
private final AssistGestureFeatureProvider mFeatureProvider;
|
||||||
private List<AbstractPreferenceController> mGestureControllers;
|
private List<AbstractPreferenceController> mGestureControllers;
|
||||||
|
|
||||||
|
private static final String KEY_GESTURES_SETTINGS = "gesture_settings";
|
||||||
|
|
||||||
public GesturesSettingPreferenceController(Context context) {
|
public GesturesSettingPreferenceController(Context context) {
|
||||||
super(context);
|
super(context, KEY_GESTURES_SETTINGS);
|
||||||
mFeatureProvider = FeatureFactory.getFactory(context).getAssistGestureFeatureProvider();
|
mFeatureProvider = FeatureFactory.getFactory(context).getAssistGestureFeatureProvider();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isAvailable() {
|
public int getAvailabilityStatus() {
|
||||||
if (mGestureControllers == null) {
|
if (mGestureControllers == null) {
|
||||||
mGestureControllers = GestureSettings.buildPreferenceControllers(mContext,
|
mGestureControllers = GestureSettings.buildPreferenceControllers(mContext,
|
||||||
null /* lifecycle */, new AmbientDisplayConfiguration(mContext));
|
null /* lifecycle */, new AmbientDisplayConfiguration(mContext));
|
||||||
@@ -52,12 +51,9 @@ public class GesturesSettingPreferenceController extends AbstractPreferenceContr
|
|||||||
for (AbstractPreferenceController controller : mGestureControllers) {
|
for (AbstractPreferenceController controller : mGestureControllers) {
|
||||||
isAvailable = isAvailable || controller.isAvailable();
|
isAvailable = isAvailable || controller.isAvailable();
|
||||||
}
|
}
|
||||||
return isAvailable;
|
return isAvailable
|
||||||
}
|
? AVAILABLE
|
||||||
|
: DISABLED_UNSUPPORTED;
|
||||||
@Override
|
|
||||||
public String getPreferenceKey() {
|
|
||||||
return KEY_GESTURES_SETTINGS;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -83,5 +79,4 @@ public class GesturesSettingPreferenceController extends AbstractPreferenceContr
|
|||||||
}
|
}
|
||||||
preference.setSummary(summary);
|
preference.setSummary(summary);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@@ -71,6 +71,10 @@ public class XmlParserUtils {
|
|||||||
return getData(context, attrs, R.styleable.Preference, R.styleable.Preference_keywords);
|
return getData(context, attrs, R.styleable.Preference, R.styleable.Preference_keywords);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static String getController(Context context, AttributeSet attrs) {
|
||||||
|
return getData(context, attrs, R.styleable.Preference, R.styleable.Preference_controller);
|
||||||
|
}
|
||||||
|
|
||||||
public static int getDataIcon(Context context, AttributeSet attrs) {
|
public static int getDataIcon(Context context, AttributeSet attrs) {
|
||||||
final TypedArray ta = context.obtainStyledAttributes(attrs,
|
final TypedArray ta = context.obtainStyledAttributes(attrs,
|
||||||
com.android.internal.R.styleable.Preference);
|
com.android.internal.R.styleable.Preference);
|
||||||
|
@@ -67,7 +67,7 @@ public class SystemDashboardFragment extends DashboardFragment {
|
|||||||
|
|
||||||
private static List<AbstractPreferenceController> buildPreferenceControllers(Context context) {
|
private static List<AbstractPreferenceController> buildPreferenceControllers(Context context) {
|
||||||
final List<AbstractPreferenceController> controllers = new ArrayList<>();
|
final List<AbstractPreferenceController> controllers = new ArrayList<>();
|
||||||
controllers.add(new SystemUpdatePreferenceController(context, UserManager.get(context)));
|
controllers.add(new SystemUpdatePreferenceController(context));
|
||||||
controllers.add(new AdditionalSystemUpdatePreferenceController(context));
|
controllers.add(new AdditionalSystemUpdatePreferenceController(context));
|
||||||
controllers.add(new BackupSettingsActivityPreferenceController(context));
|
controllers.add(new BackupSettingsActivityPreferenceController(context));
|
||||||
controllers.add(new GesturesSettingPreferenceController(context));
|
controllers.add(new GesturesSettingPreferenceController(context));
|
||||||
@@ -88,15 +88,16 @@ public class SystemDashboardFragment extends DashboardFragment {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<AbstractPreferenceController> getPreferenceControllers(Context context) {
|
public List<AbstractPreferenceController> getPreferenceControllers(
|
||||||
|
Context context) {
|
||||||
return buildPreferenceControllers(context);
|
return buildPreferenceControllers(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<String> getNonIndexableKeys(Context context) {
|
public List<String> getNonIndexableKeys(Context context) {
|
||||||
List<String> keys = super.getNonIndexableKeys(context);
|
List<String> keys = super.getNonIndexableKeys(context);
|
||||||
keys.add((new BackupSettingsActivityPreferenceController(context)
|
keys.add((new BackupSettingsActivityPreferenceController(
|
||||||
.getPreferenceKey()));
|
context).getPreferenceKey()));
|
||||||
keys.add(KEY_RESET);
|
keys.add(KEY_RESET);
|
||||||
return keys;
|
return keys;
|
||||||
}
|
}
|
||||||
|
@@ -30,5 +30,6 @@
|
|||||||
|
|
||||||
<Preference
|
<Preference
|
||||||
android:key="pref_key_1"
|
android:key="pref_key_1"
|
||||||
android:title="bears_bears_bears"/>
|
android:title="bears_bears_bears"
|
||||||
|
settings:controller="mind_flayer"/>
|
||||||
</PreferenceScreen>
|
</PreferenceScreen>
|
@@ -0,0 +1,275 @@
|
|||||||
|
package com.android.settings.core;
|
||||||
|
|
||||||
|
import static com.google.common.truth.Truth.assertWithMessage;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.res.XmlResourceParser;
|
||||||
|
import android.provider.SearchIndexableResource;
|
||||||
|
import android.text.TextUtils;
|
||||||
|
import android.util.AttributeSet;
|
||||||
|
import android.util.Xml;
|
||||||
|
|
||||||
|
import com.android.settings.R;
|
||||||
|
import com.android.settings.TestConfig;
|
||||||
|
import com.android.settings.search.DatabaseIndexingUtils;
|
||||||
|
import com.android.settings.search.Indexable;
|
||||||
|
import com.android.settings.search.SearchIndexableResources;
|
||||||
|
import com.android.settings.search.XmlParserUtils;
|
||||||
|
import com.android.settings.security.SecuritySettings;
|
||||||
|
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||||
|
|
||||||
|
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 org.xmlpull.v1.XmlPullParser;
|
||||||
|
|
||||||
|
import java.lang.reflect.Constructor;
|
||||||
|
import java.lang.reflect.InvocationTargetException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
@RunWith(SettingsRobolectricTestRunner.class)
|
||||||
|
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
|
||||||
|
public class XmlControllerAttributeTest {
|
||||||
|
|
||||||
|
// List of classes that are too hard to mock in order to retrieve xml information.
|
||||||
|
private final List<Class> illegalClasses = new ArrayList<>(
|
||||||
|
Arrays.asList(
|
||||||
|
SecuritySettings.class
|
||||||
|
));
|
||||||
|
|
||||||
|
// List of XML that could be retrieved from the illegalClasses list.
|
||||||
|
private final List<Integer> whitelistXml = new ArrayList<>(
|
||||||
|
Arrays.asList(
|
||||||
|
R.xml.security_settings_misc,
|
||||||
|
R.xml.security_settings_lockscreen_profile,
|
||||||
|
R.xml.security_settings_lockscreen,
|
||||||
|
R.xml.security_settings_chooser,
|
||||||
|
R.xml.security_settings_pattern_profile,
|
||||||
|
R.xml.security_settings_pin_profile,
|
||||||
|
R.xml.security_settings_password_profile,
|
||||||
|
R.xml.security_settings_pattern,
|
||||||
|
R.xml.security_settings_pin,
|
||||||
|
R.xml.security_settings_password,
|
||||||
|
R.xml.security_settings,
|
||||||
|
R.xml.security_settings_status
|
||||||
|
));
|
||||||
|
|
||||||
|
private static final String NO_VALID_CONSTRUCTOR_ERROR =
|
||||||
|
"Controllers added in XML need a constructor following either:"
|
||||||
|
+ "\n\tClassName(Context)\n\tClassName(Context, String)"
|
||||||
|
+ "\nThese controllers are missing a valid constructor:\n";
|
||||||
|
|
||||||
|
private static final String NOT_BASE_PREF_CONTROLLER_ERROR =
|
||||||
|
"Controllers added in XML need to extend com.android.settings.core"
|
||||||
|
+ ".BasePreferenceController\nThese controllers do not:\n";
|
||||||
|
|
||||||
|
private static final String BAD_CLASSNAME_ERROR =
|
||||||
|
"The following controllers set in the XML did not have valid class names:\n";
|
||||||
|
|
||||||
|
private static final String BAD_CONSTRUCTOR_ERROR =
|
||||||
|
"The constructor provided by the following classes were insufficient to instantiate "
|
||||||
|
+ "the object. It could be due to being an interface, abstract, or an "
|
||||||
|
+ "IllegalAccessException. Please fix the following classes:\n";
|
||||||
|
|
||||||
|
Context mContext;
|
||||||
|
|
||||||
|
private Set<Class> mProviderClassesCopy;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() {
|
||||||
|
mContext = RuntimeEnvironment.application;
|
||||||
|
mProviderClassesCopy = new HashSet<>(SearchIndexableResources.providerValues());
|
||||||
|
}
|
||||||
|
|
||||||
|
@After
|
||||||
|
public void cleanUp() {
|
||||||
|
SearchIndexableResources.providerValues().clear();
|
||||||
|
SearchIndexableResources.providerValues().addAll(mProviderClassesCopy);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testAllIndexableXML_onlyValidBasePreferenceControllersAdded() {
|
||||||
|
Set<Integer> xmlSet = getIndexableXml();
|
||||||
|
xmlSet.addAll(whitelistXml);
|
||||||
|
|
||||||
|
List<String> xmlControllers = new ArrayList<>();
|
||||||
|
Set<String> invalidConstructors = new HashSet<>();
|
||||||
|
Set<String> invalidClassHierarchy = new HashSet<>();
|
||||||
|
Set<String> badClassNameControllers = new HashSet<>();
|
||||||
|
Set<String> badConstructorControllers = new HashSet<>();
|
||||||
|
|
||||||
|
for (int resId : xmlSet) {
|
||||||
|
xmlControllers.addAll(getXmlControllers(resId));
|
||||||
|
}
|
||||||
|
|
||||||
|
for (String controllerClassName : xmlControllers) {
|
||||||
|
Class<?> clazz = getClassFromClassName(controllerClassName);
|
||||||
|
|
||||||
|
if (clazz == null) {
|
||||||
|
badClassNameControllers.add(controllerClassName);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
Constructor<?> constructor = getConstructorFromClass(clazz);
|
||||||
|
|
||||||
|
if (constructor == null) {
|
||||||
|
invalidConstructors.add(controllerClassName);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
Object controller = getObjectFromConstructor(constructor);
|
||||||
|
if (controller == null) {
|
||||||
|
badConstructorControllers.add(controllerClassName);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!(controller instanceof BasePreferenceController)) {
|
||||||
|
invalidClassHierarchy.add(controllerClassName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
final String invalidConstructorError = buildErrorMessage(NO_VALID_CONSTRUCTOR_ERROR,
|
||||||
|
invalidConstructors);
|
||||||
|
final String invalidClassHierarchyError = buildErrorMessage(NOT_BASE_PREF_CONTROLLER_ERROR,
|
||||||
|
invalidClassHierarchy);
|
||||||
|
final String badClassNameError = buildErrorMessage(BAD_CLASSNAME_ERROR,
|
||||||
|
badClassNameControllers);
|
||||||
|
final String badConstructorError = buildErrorMessage(BAD_CONSTRUCTOR_ERROR,
|
||||||
|
badConstructorControllers);
|
||||||
|
|
||||||
|
assertWithMessage(invalidConstructorError).that(invalidConstructors).isEmpty();
|
||||||
|
assertWithMessage(invalidClassHierarchyError).that(invalidClassHierarchy).isEmpty();
|
||||||
|
assertWithMessage(badClassNameError).that(badClassNameControllers).isEmpty();
|
||||||
|
assertWithMessage(badConstructorError).that(badConstructorControllers).isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
private Set<Integer> getIndexableXml() {
|
||||||
|
Set<Integer> xmlResSet = new HashSet();
|
||||||
|
|
||||||
|
Collection<Class> indexableClasses = SearchIndexableResources.providerValues();
|
||||||
|
indexableClasses.removeAll(illegalClasses);
|
||||||
|
|
||||||
|
for (Class clazz : indexableClasses) {
|
||||||
|
|
||||||
|
Indexable.SearchIndexProvider provider = DatabaseIndexingUtils.getSearchIndexProvider(
|
||||||
|
clazz);
|
||||||
|
|
||||||
|
if (provider == null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
List<SearchIndexableResource> resources = provider.getXmlResourcesToIndex(mContext,
|
||||||
|
true);
|
||||||
|
|
||||||
|
if (resources == null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (SearchIndexableResource resource : resources) {
|
||||||
|
// Add '0's anyway. It won't break the test.
|
||||||
|
xmlResSet.add(resource.xmlResId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return xmlResSet;
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<String> getXmlControllers(int xmlResId) {
|
||||||
|
List<String> xmlControllers = new ArrayList<>();
|
||||||
|
|
||||||
|
XmlResourceParser parser;
|
||||||
|
try {
|
||||||
|
parser = mContext.getResources().getXml(xmlResId);
|
||||||
|
|
||||||
|
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();
|
||||||
|
final AttributeSet attrs = Xml.asAttributeSet(parser);
|
||||||
|
String controllerClassName;
|
||||||
|
while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
|
||||||
|
&& (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
|
||||||
|
if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
controllerClassName = XmlParserUtils.getController(mContext, attrs);
|
||||||
|
// If controller is not indexed, then it is not compatible with
|
||||||
|
if (!TextUtils.isEmpty(controllerClassName)) {
|
||||||
|
xmlControllers.add(controllerClassName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
// Assume an issue with robolectric resources
|
||||||
|
}
|
||||||
|
return xmlControllers;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String buildErrorMessage(String errorSummary, Set<String> errorClasses) {
|
||||||
|
final StringBuilder error = new StringBuilder(errorSummary);
|
||||||
|
for (String c : errorClasses) {
|
||||||
|
error.append(c).append("\n");
|
||||||
|
}
|
||||||
|
return error.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
private Class<?> getClassFromClassName(String className) {
|
||||||
|
Class<?> clazz = null;
|
||||||
|
try {
|
||||||
|
clazz = Class.forName(className);
|
||||||
|
} catch (ClassNotFoundException e) {
|
||||||
|
}
|
||||||
|
return clazz;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Constructor<?> getConstructorFromClass(Class<?> clazz) {
|
||||||
|
Constructor<?> constructor = null;
|
||||||
|
try {
|
||||||
|
constructor = clazz.getConstructor(Context.class);
|
||||||
|
} catch (NoSuchMethodException e) {
|
||||||
|
}
|
||||||
|
|
||||||
|
if (constructor != null) {
|
||||||
|
return constructor;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
constructor = clazz.getConstructor(Context.class, String.class);
|
||||||
|
} catch (NoSuchMethodException e) {
|
||||||
|
}
|
||||||
|
|
||||||
|
return constructor;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Object getObjectFromConstructor(Constructor<?> constructor) {
|
||||||
|
Object controller = null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
controller = constructor.newInstance(mContext);
|
||||||
|
} catch (InstantiationException | IllegalAccessException | InvocationTargetException |
|
||||||
|
IllegalArgumentException e) {
|
||||||
|
}
|
||||||
|
|
||||||
|
if (controller != null) {
|
||||||
|
return controller;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
controller = constructor.newInstance(mContext, "key");
|
||||||
|
} catch (InstantiationException | IllegalAccessException | InvocationTargetException |
|
||||||
|
IllegalArgumentException e) {
|
||||||
|
}
|
||||||
|
|
||||||
|
return controller;
|
||||||
|
}
|
||||||
|
}
|
@@ -57,7 +57,9 @@ public class SystemUpdatePreferenceControllerTest {
|
|||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
MockitoAnnotations.initMocks(this);
|
MockitoAnnotations.initMocks(this);
|
||||||
mController = new SystemUpdatePreferenceController(mContext, mUserManager);
|
|
||||||
|
when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
|
||||||
|
mController = new SystemUpdatePreferenceController(mContext);
|
||||||
mPreference = new Preference(RuntimeEnvironment.application);
|
mPreference = new Preference(RuntimeEnvironment.application);
|
||||||
mPreference.setKey(mController.getPreferenceKey());
|
mPreference.setKey(mController.getPreferenceKey());
|
||||||
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
|
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
|
||||||
@@ -82,7 +84,7 @@ public class SystemUpdatePreferenceControllerTest {
|
|||||||
|
|
||||||
mController.updateNonIndexableKeys(keys);
|
mController.updateNonIndexableKeys(keys);
|
||||||
|
|
||||||
assertThat(keys.size()).isEqualTo(1);
|
assertThat(keys).hasSize(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -94,8 +96,8 @@ public class SystemUpdatePreferenceControllerTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void updateState_shouldSetToAndroidVersion() {
|
public void updateState_shouldSetToAndroidVersion() {
|
||||||
mController = new SystemUpdatePreferenceController(
|
mController = new SystemUpdatePreferenceController(RuntimeEnvironment.application);
|
||||||
RuntimeEnvironment.application, mUserManager);
|
|
||||||
mController.updateState(mPreference);
|
mController.updateState(mPreference);
|
||||||
|
|
||||||
assertThat(mPreference.getSummary())
|
assertThat(mPreference.getSummary())
|
||||||
|
@@ -128,6 +128,16 @@ public class XmlParserUtilTest {
|
|||||||
assertThat(key).isNull();
|
assertThat(key).isNull();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Config(qualifiers = "mcc999")
|
||||||
|
public void testControllerAttribute_returnsValidData() {
|
||||||
|
XmlResourceParser parser = getChildByType(R.xml.about_legal, "Preference");
|
||||||
|
final AttributeSet attrs = Xml.asAttributeSet(parser);
|
||||||
|
|
||||||
|
String controller = XmlParserUtils.getController(mContext, attrs);
|
||||||
|
assertThat(controller).isEqualTo("mind_flayer");
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testDataSummaryInvalid_ReturnsNull() {
|
public void testDataSummaryInvalid_ReturnsNull() {
|
||||||
XmlResourceParser parser = getParentPrimedParser(R.xml.display_settings);
|
XmlResourceParser parser = getParentPrimedParser(R.xml.display_settings);
|
||||||
|
Reference in New Issue
Block a user