Remove DynamicAvailabilityPreferenceController.

- This class is over complex to use, and get in the way of show/hide
  pref when availability change.
- The function from DynamicAvailabilityPreferenceController can be done
  with PreferenceCategoryController.

Bug: 68956750
Bug: 32953042
Test: robotests
Change-Id: Ia7b7e214c072b8c77f5de3d959fdb1dc79217f76
This commit is contained in:
Fan Zhang
2017-11-07 10:57:47 -08:00
parent 2260321fd8
commit b5f5ffbece
46 changed files with 202 additions and 1048 deletions

View File

@@ -17,18 +17,16 @@
package com.android.settings.enterprise;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.content.res.XmlResourceParser;
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
import com.android.settings.R;
import com.android.settings.TestConfig;
import com.android.settings.core.DynamicAvailabilityPreferenceController;
import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settings.widget.PreferenceCategoryController;
import com.android.settingslib.core.AbstractPreferenceController;
import org.junit.Before;
@@ -37,14 +35,10 @@ import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowApplication;
import org.xmlpull.v1.XmlPullParser;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* Tests for {@link EnterprisePrivacySettings}.
@@ -53,9 +47,6 @@ import java.util.Set;
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public final class EnterprisePrivacySettingsTest {
private final static String RESOURCES_NAMESPACE = "http://schemas.android.com/apk/res/android";
private final static String ATTR_KEY = "key";
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private Context mContext;
private FakeFeatureFactory mFeatureFactory;
@@ -71,23 +62,11 @@ public final class EnterprisePrivacySettingsTest {
}
@Test
public void testGetMetricsCategory() {
public void verifyConstants() {
assertThat(mSettings.getMetricsCategory())
.isEqualTo(MetricsEvent.ENTERPRISE_PRIVACY_SETTINGS);
}
@Test
public void testGetCategoryKey() {
assertThat(mSettings.getCategoryKey()).isNull();
}
@Test
public void testGetLogTag() {
assertThat(mSettings.getLogTag()).isEqualTo("EnterprisePrivacySettings");
}
@Test
public void testGetPreferenceScreenResId() {
assertThat(mSettings.getCategoryKey()).isNull();
assertThat(mSettings.getPreferenceScreenResId())
.isEqualTo(R.xml.enterprise_privacy_settings);
}
@@ -121,7 +100,7 @@ public final class EnterprisePrivacySettingsTest {
public void getSearchIndexProviderPreferenceControllers() throws Exception {
final List<AbstractPreferenceController> controllers
= EnterprisePrivacySettings.SEARCH_INDEX_DATA_PROVIDER.getPreferenceControllers(
ShadowApplication.getInstance().getApplicationContext());
ShadowApplication.getInstance().getApplicationContext());
verifyPreferenceControllers(controllers);
}
@@ -155,67 +134,11 @@ public final class EnterprisePrivacySettingsTest {
CaCertsCurrentUserPreferenceController.class);
assertThat(controllers.get(position++)).isInstanceOf(
CaCertsManagedProfilePreferenceController.class);
final AbstractPreferenceController exposureChangesCategoryController =
controllers.get(position);
final int exposureChangesCategoryControllerIndex = position;
assertThat(controllers.get(position++)).isInstanceOf(
ExposureChangesCategoryPreferenceController.class);
PreferenceCategoryController.class);
assertThat(controllers.get(position++)).isInstanceOf(
FailedPasswordWipeCurrentUserPreferenceController.class);
assertThat(controllers.get(position++)).isInstanceOf(
FailedPasswordWipeManagedProfilePreferenceController.class);
// The "Changes made by your organization's admin" category is hidden when all Preferences
// inside it become unavailable. To do this correctly, the category's controller must:
// a) Observe the availability of all Preferences in the category and
// b) Be listed after those Preferences' controllers, so that availability is updated in
// the correct order
// Find all Preferences in the category.
final XmlResourceParser parser = RuntimeEnvironment.application.getResources().getXml(
R.xml.enterprise_privacy_settings);
boolean done = false;
int type;
final Set<String> expectedObserved = new HashSet<>();
while (!done && (type = parser.next()) != XmlPullParser.END_DOCUMENT) {
if (type != XmlPullParser.START_TAG || !"exposure_changes_category".equals(
parser.getAttributeValue(RESOURCES_NAMESPACE, ATTR_KEY))) {
continue;
}
int depth = 1;
while ((type = parser.next()) != XmlPullParser.END_DOCUMENT) {
if (type == XmlPullParser.START_TAG) {
final String key = parser.getAttributeValue(RESOURCES_NAMESPACE, ATTR_KEY);
if (key != null) {
expectedObserved.add(key);
}
depth++;
} else if (type == XmlPullParser.END_TAG) {
depth--;
if (depth == 0) {
done = true;
break;
}
}
}
}
// Find all Preferences the category's controller is observing.
final Set<String> actualObserved = new HashSet<>();
int maxObservedIndex = -1;
for (int i = 0; i < controllers.size(); i++) {
final AbstractPreferenceController controller = controllers.get(i);
if (controller instanceof DynamicAvailabilityPreferenceController &&
((DynamicAvailabilityPreferenceController) controller).getAvailabilityObserver()
== exposureChangesCategoryController) {
actualObserved.add(controller.getPreferenceKey());
maxObservedIndex = i;
}
}
// Verify that the category's controller is observing the Preferences inside it.
assertThat(actualObserved).isEqualTo(expectedObserved);
// Verify that the category's controller is listed after the Preferences' controllers.
assertThat(maxObservedIndex).isLessThan(exposureChangesCategoryControllerIndex);
}
}