Merge "Remove jank on DND schedule page" into udc-dev am: a0c9ce5b2f
am: f0bb4e8ce7
Original change: https://googleplex-android-review.googlesource.com/c/platform/packages/apps/Settings/+/21650757 Change-Id: I863c9046230f6f0b27d4bfe37d1d0cd61dce2cc9 Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
@@ -66,9 +66,7 @@ abstract public class AbstractZenModeAutomaticRulePreferenceController extends
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected Map.Entry<String, AutomaticZenRule>[] getRules() {
|
protected Map.Entry<String, AutomaticZenRule>[] getRules() {
|
||||||
if (mRules == null) {
|
|
||||||
mRules = mBackend.getAutomaticZenRules();
|
mRules = mBackend.getAutomaticZenRules();
|
||||||
}
|
|
||||||
return mRules;
|
return mRules;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -18,7 +18,6 @@ package com.android.settings.notification.zen;
|
|||||||
|
|
||||||
import android.app.AutomaticZenRule;
|
import android.app.AutomaticZenRule;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.util.ArrayMap;
|
|
||||||
|
|
||||||
import androidx.annotation.VisibleForTesting;
|
import androidx.annotation.VisibleForTesting;
|
||||||
import androidx.fragment.app.Fragment;
|
import androidx.fragment.app.Fragment;
|
||||||
@@ -28,23 +27,21 @@ import androidx.preference.PreferenceScreen;
|
|||||||
|
|
||||||
import com.android.settingslib.core.lifecycle.Lifecycle;
|
import com.android.settingslib.core.lifecycle.Lifecycle;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
public class ZenModeAutomaticRulesPreferenceController extends
|
public class ZenModeAutomaticRulesPreferenceController extends
|
||||||
AbstractZenModeAutomaticRulePreferenceController {
|
AbstractZenModeAutomaticRulePreferenceController {
|
||||||
|
|
||||||
protected static final String KEY = "zen_mode_automatic_rules";
|
protected static final String KEY = "zen_mode_automatic_rules";
|
||||||
|
|
||||||
@VisibleForTesting
|
Map.Entry<String, AutomaticZenRule>[] mSortedRules;
|
||||||
protected PreferenceCategory mPreferenceCategory;
|
|
||||||
|
|
||||||
// Map of rule key -> preference so that we can update each preference as needed
|
|
||||||
@VisibleForTesting
|
|
||||||
protected Map<String, ZenRulePreference> mZenRulePreferences = new ArrayMap<>();
|
|
||||||
|
|
||||||
public ZenModeAutomaticRulesPreferenceController(Context context, Fragment parent, Lifecycle
|
public ZenModeAutomaticRulesPreferenceController(Context context, Fragment parent, Lifecycle
|
||||||
lifecycle) {
|
lifecycle, ZenModeBackend backend) {
|
||||||
super(context, KEY, parent, lifecycle);
|
super(context, KEY, parent, lifecycle);
|
||||||
|
mBackend = backend;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -60,81 +57,69 @@ public class ZenModeAutomaticRulesPreferenceController extends
|
|||||||
@Override
|
@Override
|
||||||
public void displayPreference(PreferenceScreen screen) {
|
public void displayPreference(PreferenceScreen screen) {
|
||||||
super.displayPreference(screen);
|
super.displayPreference(screen);
|
||||||
mPreferenceCategory = screen.findPreference(getPreferenceKey());
|
PreferenceCategory preferenceCategory = screen.findPreference(getPreferenceKey());
|
||||||
mPreferenceCategory.setPersistent(false);
|
preferenceCategory.setPersistent(false);
|
||||||
|
mSortedRules = getRules();
|
||||||
// if mPreferenceCategory was un-set, make sure to clear out mZenRulePreferences too, just
|
updateRules(preferenceCategory);
|
||||||
// in case
|
|
||||||
if (mPreferenceCategory.getPreferenceCount() == 0) {
|
|
||||||
mZenRulePreferences.clear();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void updateState(Preference preference) {
|
public void updateState(Preference preference) {
|
||||||
super.updateState(preference);
|
|
||||||
Map.Entry<String, AutomaticZenRule>[] sortedRules = getRules();
|
Map.Entry<String, AutomaticZenRule>[] sortedRules = getRules();
|
||||||
|
boolean rulesChanged = false;
|
||||||
// refresh the whole preference category list if the total number of rules has changed, or
|
if (sortedRules.length != mSortedRules.length) {
|
||||||
// if any individual rules have changed, so we can rebuild the list & keep things in sync
|
rulesChanged = true;
|
||||||
boolean refreshPrefs = false;
|
|
||||||
if (mPreferenceCategory.getPreferenceCount() != sortedRules.length) {
|
|
||||||
refreshPrefs = true;
|
|
||||||
} else {
|
} else {
|
||||||
// check whether any rules in sortedRules are not in mZenRulePreferences; that should
|
for (int i = 0; i < mSortedRules.length; i++) {
|
||||||
// be enough to see whether something has changed
|
if (!Objects.equals(mSortedRules[i].getKey(), sortedRules[i].getKey())
|
||||||
for (int i = 0; i < sortedRules.length; i++) {
|
|| !Objects.equals(mSortedRules[i].getValue(), sortedRules[i].getValue())) {
|
||||||
if (!mZenRulePreferences.containsKey(sortedRules[i].getKey())) {
|
rulesChanged = true;
|
||||||
refreshPrefs = true;
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// if we need to refresh the whole list, clear the preference category and also start a
|
if (rulesChanged) {
|
||||||
// new map of preferences according to the preference category contents
|
mSortedRules = sortedRules;
|
||||||
// we need to not update the existing one yet, as we'll need to know what preferences
|
updateRules((PreferenceCategory) preference);
|
||||||
// previously existed in order to update and re-attach them to the preference category
|
}
|
||||||
Map<String, ZenRulePreference> newPrefs = new ArrayMap<>();
|
}
|
||||||
if (refreshPrefs) {
|
|
||||||
mPreferenceCategory.removeAll();
|
private void updateRules(PreferenceCategory preferenceCategory) {
|
||||||
|
Map<String, ZenRulePreference> originalPreferences = new HashMap<>();
|
||||||
|
for (int i = 0; i < preferenceCategory.getPreferenceCount(); i++) {
|
||||||
|
ZenRulePreference pref = (ZenRulePreference) preferenceCategory.getPreference(i);
|
||||||
|
originalPreferences.put(pref.getKey(), pref);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Loop through each rule, either updating the existing rule or creating the rule's
|
// Loop through each rule, either updating the existing rule or creating the rule's
|
||||||
// preference if needed (and, in the case where we need to rebuild the preference category
|
// preference
|
||||||
// list, do so as well)
|
for (int i = 0; i < mSortedRules.length; i++) {
|
||||||
for (int i = 0; i < sortedRules.length; i++) {
|
String key = mSortedRules[i].getKey();
|
||||||
String key = sortedRules[i].getKey();
|
|
||||||
if (mZenRulePreferences.containsKey(key)) {
|
|
||||||
// existing rule; update its info if it's changed since the last display
|
|
||||||
AutomaticZenRule rule = sortedRules[i].getValue();
|
|
||||||
ZenRulePreference pref = mZenRulePreferences.get(key);
|
|
||||||
pref.updatePreference(rule);
|
|
||||||
|
|
||||||
// only add to preference category if the overall set of rules has changed so this
|
if (originalPreferences.containsKey(key)) {
|
||||||
// needs to be rearranged
|
// existing rule; update its info if it's changed since the last display
|
||||||
if (refreshPrefs) {
|
AutomaticZenRule rule = mSortedRules[i].getValue();
|
||||||
mPreferenceCategory.addPreference(pref);
|
originalPreferences.get(key).updatePreference(rule);
|
||||||
newPrefs.put(key, pref);
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
// new rule; create a new ZenRulePreference & add it to the preference category
|
// new rule; create a new ZenRulePreference & add it to the preference category
|
||||||
// and the map so we'll know about it later
|
ZenRulePreference pref = createZenRulePreference(
|
||||||
ZenRulePreference pref = createZenRulePreference(sortedRules[i]);
|
mSortedRules[i], preferenceCategory);
|
||||||
mPreferenceCategory.addPreference(pref);
|
preferenceCategory.addPreference(pref);
|
||||||
newPrefs.put(key, pref);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// If anything was new, then make sure we overwrite mZenRulePreferences with our new data
|
originalPreferences.remove(key);
|
||||||
if (refreshPrefs) {
|
}
|
||||||
mZenRulePreferences = newPrefs;
|
// Remove preferences that no longer have a rule
|
||||||
|
for (String key : originalPreferences.keySet()) {
|
||||||
|
preferenceCategory.removePreferenceRecursively(key);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
ZenRulePreference createZenRulePreference(Map.Entry<String, AutomaticZenRule> rule) {
|
ZenRulePreference createZenRulePreference(Map.Entry<String, AutomaticZenRule> rule,
|
||||||
return new ZenRulePreference(mPreferenceCategory.getContext(),
|
PreferenceCategory preferenceCategory) {
|
||||||
rule, mParent, mMetricsFeatureProvider);
|
return new ZenRulePreference(preferenceCategory.getContext(),
|
||||||
|
rule, mParent, mMetricsFeatureProvider, mBackend);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -22,7 +22,6 @@ import android.app.NotificationManager;
|
|||||||
import android.app.settings.SettingsEnums;
|
import android.app.settings.SettingsEnums;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.DialogInterface;
|
import android.content.DialogInterface;
|
||||||
import android.os.Bundle;
|
|
||||||
import android.service.notification.ConditionProviderService;
|
import android.service.notification.ConditionProviderService;
|
||||||
import android.view.Menu;
|
import android.view.Menu;
|
||||||
import android.view.MenuInflater;
|
import android.view.MenuInflater;
|
||||||
@@ -59,10 +58,12 @@ public class ZenModeAutomationSettings extends ZenModeSettingsBase {
|
|||||||
|
|
||||||
private static List<AbstractPreferenceController> buildPreferenceControllers(Context context,
|
private static List<AbstractPreferenceController> buildPreferenceControllers(Context context,
|
||||||
Fragment parent, ZenServiceListing serviceListing, Lifecycle lifecycle) {
|
Fragment parent, ZenServiceListing serviceListing, Lifecycle lifecycle) {
|
||||||
|
ZenModeBackend backend = new ZenModeBackend(context);
|
||||||
List<AbstractPreferenceController> controllers = new ArrayList<>();
|
List<AbstractPreferenceController> controllers = new ArrayList<>();
|
||||||
controllers.add(new ZenModeAddAutomaticRulePreferenceController(context, parent,
|
controllers.add(new ZenModeAddAutomaticRulePreferenceController(context, parent,
|
||||||
serviceListing, lifecycle));
|
serviceListing, lifecycle));
|
||||||
controllers.add(new ZenModeAutomaticRulesPreferenceController(context, parent, lifecycle));
|
controllers.add(new ZenModeAutomaticRulesPreferenceController(
|
||||||
|
context, parent, lifecycle, backend));
|
||||||
|
|
||||||
return controllers;
|
return controllers;
|
||||||
}
|
}
|
||||||
|
@@ -60,13 +60,15 @@ public class ZenRulePreference extends PrimarySwitchPreference {
|
|||||||
|
|
||||||
public ZenRulePreference(Context context,
|
public ZenRulePreference(Context context,
|
||||||
final Map.Entry<String, AutomaticZenRule> ruleEntry,
|
final Map.Entry<String, AutomaticZenRule> ruleEntry,
|
||||||
Fragment parent, MetricsFeatureProvider metricsProvider) {
|
Fragment parent, MetricsFeatureProvider metricsProvider,
|
||||||
|
ZenModeBackend backend) {
|
||||||
super(context);
|
super(context);
|
||||||
mBackend = ZenModeBackend.getInstance(context);
|
mBackend = backend;
|
||||||
mContext = context;
|
mContext = context;
|
||||||
mRule = ruleEntry.getValue();
|
mRule = ruleEntry.getValue();
|
||||||
mName = mRule.getName();
|
mName = mRule.getName();
|
||||||
mId = ruleEntry.getKey();
|
mId = ruleEntry.getKey();
|
||||||
|
setKey(mId);
|
||||||
mParent = parent;
|
mParent = parent;
|
||||||
mPm = mContext.getPackageManager();
|
mPm = mContext.getPackageManager();
|
||||||
mServiceListing = new ZenServiceListing(mContext, CONFIG);
|
mServiceListing = new ZenServiceListing(mContext, CONFIG);
|
||||||
|
@@ -17,13 +17,15 @@
|
|||||||
package com.android.settings.notification.zen;
|
package com.android.settings.notification.zen;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertFalse;
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
import static org.junit.Assert.assertNull;
|
||||||
import static org.junit.Assert.fail;
|
import static org.junit.Assert.fail;
|
||||||
import static org.mockito.ArgumentMatchers.any;
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
import static org.mockito.ArgumentMatchers.anyInt;
|
import static org.mockito.ArgumentMatchers.anyInt;
|
||||||
|
import static org.mockito.ArgumentMatchers.anyString;
|
||||||
import static org.mockito.Mockito.doReturn;
|
import static org.mockito.Mockito.doReturn;
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
import static org.mockito.Mockito.never;
|
|
||||||
import static org.mockito.Mockito.spy;
|
import static org.mockito.Mockito.spy;
|
||||||
import static org.mockito.Mockito.times;
|
import static org.mockito.Mockito.times;
|
||||||
import static org.mockito.Mockito.verify;
|
import static org.mockito.Mockito.verify;
|
||||||
@@ -31,13 +33,19 @@ import static org.mockito.Mockito.when;
|
|||||||
|
|
||||||
import android.app.AutomaticZenRule;
|
import android.app.AutomaticZenRule;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.content.SharedPreferences;
|
||||||
|
import android.content.pm.PackageManager;
|
||||||
|
import android.content.pm.ResolveInfo;
|
||||||
import android.provider.Settings;
|
import android.provider.Settings;
|
||||||
|
|
||||||
import androidx.fragment.app.Fragment;
|
import androidx.fragment.app.Fragment;
|
||||||
import androidx.preference.PreferenceCategory;
|
import androidx.preference.PreferenceCategory;
|
||||||
|
import androidx.preference.PreferenceManager;
|
||||||
import androidx.preference.PreferenceScreen;
|
import androidx.preference.PreferenceScreen;
|
||||||
import androidx.test.core.app.ApplicationProvider;
|
import androidx.test.core.app.ApplicationProvider;
|
||||||
|
|
||||||
|
import com.google.common.collect.ImmutableList;
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
@@ -47,6 +55,7 @@ import org.robolectric.RobolectricTestRunner;
|
|||||||
import org.robolectric.util.ReflectionHelpers;
|
import org.robolectric.util.ReflectionHelpers;
|
||||||
|
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
|
import java.util.AbstractMap;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@@ -56,39 +65,81 @@ public class ZenModeAutomaticRulesPreferenceControllerTest {
|
|||||||
private ZenModeAutomaticRulesPreferenceController mController;
|
private ZenModeAutomaticRulesPreferenceController mController;
|
||||||
@Mock
|
@Mock
|
||||||
private ZenModeBackend mBackend;
|
private ZenModeBackend mBackend;
|
||||||
@Mock
|
private PreferenceCategory mPreferenceCategory;
|
||||||
private PreferenceCategory mockPref;
|
|
||||||
@Mock
|
|
||||||
private PreferenceScreen mPreferenceScreen;
|
private PreferenceScreen mPreferenceScreen;
|
||||||
@Mock
|
|
||||||
private ZenRulePreference mZenRulePreference;
|
|
||||||
private Context mContext;
|
private Context mContext;
|
||||||
|
@Mock
|
||||||
|
PackageManager mPm;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setup() {
|
public void setup() {
|
||||||
MockitoAnnotations.initMocks(this);
|
MockitoAnnotations.initMocks(this);
|
||||||
mContext = ApplicationProvider.getApplicationContext();
|
mContext = spy(ApplicationProvider.getApplicationContext());
|
||||||
mController = spy(new ZenModeAutomaticRulesPreferenceController(mContext, mock(Fragment.class),
|
when(mContext.getPackageManager()).thenReturn(mPm);
|
||||||
null));
|
when(mPm.queryIntentActivities(any(), any())).thenReturn(
|
||||||
ReflectionHelpers.setField(mController, "mBackend", mBackend);
|
ImmutableList.of(mock(ResolveInfo.class)));
|
||||||
when(mPreferenceScreen.findPreference(mController.getPreferenceKey())).thenReturn(
|
when(mBackend.getAutomaticZenRules()).thenReturn(new Map.Entry[0]);
|
||||||
mockPref);
|
mController = spy(new ZenModeAutomaticRulesPreferenceController(
|
||||||
mController.displayPreference(mPreferenceScreen);
|
mContext, mock(Fragment.class), null, mBackend));
|
||||||
doReturn(mZenRulePreference).when(mController).createZenRulePreference(any());
|
final PreferenceManager preferenceManager = new PreferenceManager(mContext);
|
||||||
|
mPreferenceScreen = preferenceManager.createPreferenceScreen(mContext);
|
||||||
|
mPreferenceCategory = spy(new PreferenceCategory(mContext));
|
||||||
|
mPreferenceCategory.setKey(mController.getPreferenceKey());
|
||||||
|
mPreferenceScreen.addPreference(mPreferenceCategory);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testDisplayPreference_resetsPreferencesWhenCategoryEmpty() {
|
public void testDisplayPreference_notPersistent() {
|
||||||
// when the PreferenceCategory is empty (no preferences), make sure we clear out any
|
|
||||||
// stale state in the cached set of zen rule preferences
|
|
||||||
mController.mZenRulePreferences.put("test1_id", mZenRulePreference);
|
|
||||||
when(mockPref.getPreferenceCount()).thenReturn(0);
|
|
||||||
mController.displayPreference(mPreferenceScreen);
|
mController.displayPreference(mPreferenceScreen);
|
||||||
assertTrue(mController.mZenRulePreferences.isEmpty());
|
assertFalse(mPreferenceCategory.isPersistent());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testUpdateState_clearsPreferencesWhenAddingNewPreferences() {
|
public void testDisplayThenUpdateState_onlyAddsOnceRulesUnchanged() {
|
||||||
|
final int NUM_RULES = 1;
|
||||||
|
Map<String, AutomaticZenRule> rMap = new HashMap<>();
|
||||||
|
|
||||||
|
String ruleId1 = "test1_id";
|
||||||
|
|
||||||
|
AutomaticZenRule autoRule1 = new AutomaticZenRule("test_rule_1", null, null,
|
||||||
|
null, null, Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS, true, 10);
|
||||||
|
|
||||||
|
rMap.put(ruleId1, autoRule1);
|
||||||
|
|
||||||
|
// should add 1 new preferences to mockPref
|
||||||
|
mockGetAutomaticZenRules(NUM_RULES, rMap);
|
||||||
|
mController.displayPreference(mPreferenceScreen);
|
||||||
|
mController.updateState(mPreferenceCategory);
|
||||||
|
assertEquals(NUM_RULES, mPreferenceCategory.getPreferenceCount());
|
||||||
|
verify(mPreferenceCategory, times(1)).addPreference(any());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDisplayThenUpdateState_addsIfRulesChange() {
|
||||||
|
Map<String, AutomaticZenRule> rMap = new HashMap<>();
|
||||||
|
|
||||||
|
String ruleId1 = "test1_id";
|
||||||
|
AutomaticZenRule autoRule1 = new AutomaticZenRule("test_rule_1", null, null,
|
||||||
|
null, null, Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS, true, 10);
|
||||||
|
rMap.put(ruleId1, autoRule1);
|
||||||
|
mockGetAutomaticZenRules(1, rMap);
|
||||||
|
// adds one
|
||||||
|
mController.displayPreference(mPreferenceScreen);
|
||||||
|
|
||||||
|
String ruleId2 = "test2_id";
|
||||||
|
AutomaticZenRule autoRule2 = new AutomaticZenRule("test_rule_2", null, null,
|
||||||
|
null, null, Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS, true, 20);
|
||||||
|
rMap.put(ruleId2, autoRule2);
|
||||||
|
mockGetAutomaticZenRules(2, rMap);
|
||||||
|
|
||||||
|
mController.updateState(mPreferenceCategory);
|
||||||
|
assertEquals(2, mPreferenceCategory.getPreferenceCount());
|
||||||
|
verify(mPreferenceCategory, times(2)).addPreference(any());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testUpdateState_addingNewPreferences() {
|
||||||
|
mController.displayPreference(mPreferenceScreen);
|
||||||
final int NUM_RULES = 3;
|
final int NUM_RULES = 3;
|
||||||
Map<String, AutomaticZenRule> rMap = new HashMap<>();
|
Map<String, AutomaticZenRule> rMap = new HashMap<>();
|
||||||
|
|
||||||
@@ -109,17 +160,27 @@ public class ZenModeAutomaticRulesPreferenceControllerTest {
|
|||||||
|
|
||||||
// should add 3 new preferences to mockPref
|
// should add 3 new preferences to mockPref
|
||||||
mockGetAutomaticZenRules(NUM_RULES, rMap);
|
mockGetAutomaticZenRules(NUM_RULES, rMap);
|
||||||
mController.updateState(mockPref);
|
mController.updateState(mPreferenceCategory);
|
||||||
verify(mockPref, times(1)).removeAll();
|
assertEquals(NUM_RULES, mPreferenceCategory.getPreferenceCount());
|
||||||
verify(mockPref, times(NUM_RULES)).addPreference(any());
|
|
||||||
assertEquals(NUM_RULES, mController.mZenRulePreferences.size());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testUpdateState_clearsPreferencesWhenRemovingPreferences(){
|
public void testUpdateState_addsAndRemoves(){
|
||||||
|
mController.displayPreference(mPreferenceScreen);
|
||||||
final int NUM_RULES = 2;
|
final int NUM_RULES = 2;
|
||||||
Map<String, AutomaticZenRule> rMap = new HashMap<>();
|
Map<String, AutomaticZenRule> rMap = new HashMap<>();
|
||||||
|
|
||||||
|
String FAKE_1 = "fake key 1";
|
||||||
|
String FAKE_2 = "fake 2";
|
||||||
|
mPreferenceCategory.addPreference(new ZenRulePreference(mContext,
|
||||||
|
new AbstractMap.SimpleEntry<>(FAKE_1, mock(AutomaticZenRule.class)),
|
||||||
|
null, null, mBackend));
|
||||||
|
mPreferenceCategory.addPreference(new ZenRulePreference(mContext,
|
||||||
|
new AbstractMap.SimpleEntry<>(FAKE_2, mock(AutomaticZenRule.class)),
|
||||||
|
null, null, mBackend));
|
||||||
|
assertNotNull(mPreferenceCategory.findPreference(FAKE_1));
|
||||||
|
assertNotNull(mPreferenceCategory.findPreference(FAKE_2));
|
||||||
|
|
||||||
String ruleId1 = "test1_id";
|
String ruleId1 = "test1_id";
|
||||||
String ruleId2 = "test2_id";
|
String ruleId2 = "test2_id";
|
||||||
|
|
||||||
@@ -131,83 +192,37 @@ public class ZenModeAutomaticRulesPreferenceControllerTest {
|
|||||||
rMap.put(ruleId1, autoRule1);
|
rMap.put(ruleId1, autoRule1);
|
||||||
rMap.put(ruleId2, autoRule2);
|
rMap.put(ruleId2, autoRule2);
|
||||||
|
|
||||||
// Add three preferences to the set of previously-known-about ZenRulePreferences; in this
|
|
||||||
// case, test3_id is "deleted"
|
|
||||||
mController.mZenRulePreferences.put("test1_id", mZenRulePreference);
|
|
||||||
mController.mZenRulePreferences.put("test2_id", mZenRulePreference);
|
|
||||||
mController.mZenRulePreferences.put("test3_id", mZenRulePreference);
|
|
||||||
|
|
||||||
// update state should re-add all preferences since a preference was deleted
|
// update state should re-add all preferences since a preference was deleted
|
||||||
when(mockPref.getPreferenceCount()).thenReturn(NUM_RULES + 1);
|
|
||||||
mockGetAutomaticZenRules(NUM_RULES, rMap);
|
mockGetAutomaticZenRules(NUM_RULES, rMap);
|
||||||
mController.updateState(mockPref);
|
mController.updateState(mPreferenceCategory);
|
||||||
verify(mockPref, times(1)).removeAll();
|
assertNull(mPreferenceCategory.findPreference(FAKE_1));
|
||||||
verify(mockPref, times(NUM_RULES)).addPreference(any());
|
assertNull(mPreferenceCategory.findPreference(FAKE_2));
|
||||||
assertEquals(NUM_RULES, mController.mZenRulePreferences.size());
|
assertNotNull(mPreferenceCategory.findPreference(ruleId1));
|
||||||
|
assertNotNull(mPreferenceCategory.findPreference(ruleId2));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testUpdateState_clearsPreferencesWhenSameNumberButDifferentPrefs() {
|
public void testUpdateState_updateEnableState() {
|
||||||
final int NUM_RULES = 2;
|
mController.displayPreference(mPreferenceScreen);
|
||||||
Map<String, AutomaticZenRule> rMap = new HashMap<>();
|
|
||||||
|
|
||||||
String ruleId1 = "test1_id";
|
|
||||||
String ruleId2 = "test2_id";
|
|
||||||
|
|
||||||
AutomaticZenRule autoRule1 = new AutomaticZenRule("test_rule_1", null, null,
|
|
||||||
null, null, Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS, true, 10);
|
|
||||||
AutomaticZenRule autoRule2 = new AutomaticZenRule("test_rule_2", null, null,
|
|
||||||
null, null, Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS, true, 20);
|
|
||||||
|
|
||||||
rMap.put(ruleId1, autoRule1);
|
|
||||||
rMap.put(ruleId2, autoRule2);
|
|
||||||
|
|
||||||
// Add two preferences to the set of previously-known-about ZenRulePreferences; in this
|
|
||||||
// case, test3_id is "deleted" but test2_id is "added"
|
|
||||||
mController.mZenRulePreferences.put("test1_id", mZenRulePreference);
|
|
||||||
mController.mZenRulePreferences.put("test3_id", mZenRulePreference);
|
|
||||||
|
|
||||||
// update state should re-add all preferences since a preference was deleted
|
|
||||||
when(mockPref.getPreferenceCount()).thenReturn(NUM_RULES);
|
|
||||||
mockGetAutomaticZenRules(NUM_RULES, rMap);
|
|
||||||
mController.updateState(mockPref);
|
|
||||||
verify(mockPref, times(1)).removeAll();
|
|
||||||
verify(mockPref, times(NUM_RULES)).addPreference(any());
|
|
||||||
assertEquals(NUM_RULES, mController.mZenRulePreferences.size());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testUpdateState_updateEnableState() throws NoSuchFieldException {
|
|
||||||
final int NUM_RULES = 1;
|
|
||||||
Map<String, AutomaticZenRule> rMap = new HashMap<>();
|
|
||||||
String testId = "test1_id";
|
String testId = "test1_id";
|
||||||
AutomaticZenRule rule = new AutomaticZenRule("rule_name", null, null,
|
AutomaticZenRule rule = new AutomaticZenRule("rule_name", null, null,
|
||||||
null, null, Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS, true, 10);
|
null, null, Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS, true, 10);
|
||||||
rMap.put(testId, rule);
|
|
||||||
|
|
||||||
when(mockPref.getPreferenceCount()).thenReturn(NUM_RULES);
|
mPreferenceCategory.addPreference(new ZenRulePreference(mContext,
|
||||||
when(mockPref.getPreference(anyInt())).thenReturn(mZenRulePreference);
|
new AbstractMap.SimpleEntry<>(testId, rule),
|
||||||
mController.mZenRulePreferences.put("test1_id", mZenRulePreference);
|
null, null, mBackend));
|
||||||
|
|
||||||
// update state should NOT re-add all the preferences, should only update enable state
|
final int NUM_RULES = 1;
|
||||||
rule.setEnabled(false);
|
Map<String, AutomaticZenRule> rMap = new HashMap<>();
|
||||||
rMap.put(testId, rule);
|
AutomaticZenRule ruleUpdated = new AutomaticZenRule("rule_name", null, null,
|
||||||
|
null, null, Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS, true, 10);
|
||||||
|
ruleUpdated.setEnabled(false);
|
||||||
|
rMap.put(testId, ruleUpdated);
|
||||||
mockGetAutomaticZenRules(NUM_RULES, rMap);
|
mockGetAutomaticZenRules(NUM_RULES, rMap);
|
||||||
setZenRulePreferenceField("mId", testId);
|
|
||||||
mController.updateState(mockPref);
|
|
||||||
verify(mZenRulePreference, times(1)).updatePreference(any());
|
|
||||||
verify(mockPref, never()).removeAll();
|
|
||||||
assertEquals(NUM_RULES, mController.mZenRulePreferences.size());
|
|
||||||
}
|
|
||||||
|
|
||||||
private void setZenRulePreferenceField(String name, Object value) {
|
mController.updateState(mPreferenceCategory);
|
||||||
try {
|
assertFalse(mPreferenceCategory.findPreference(testId).isEnabled());
|
||||||
Field field = ZenRulePreference.class.getDeclaredField("mId");
|
assertEquals(NUM_RULES, mPreferenceCategory.getPreferenceCount());
|
||||||
field.setAccessible(true);
|
|
||||||
field.set(mZenRulePreference, value);
|
|
||||||
} catch (ReflectiveOperationException e) {
|
|
||||||
fail("Unable to set mZenRulePreference field: " + name);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void mockGetAutomaticZenRules(int numRules, Map<String, AutomaticZenRule> rules) {
|
private void mockGetAutomaticZenRules(int numRules, Map<String, AutomaticZenRule> rules) {
|
||||||
|
Reference in New Issue
Block a user