Updates to automatic rule pages in Settings
- Re-added metrics for zen behavior preference controllers - Dialogs in zen mode settings are rotate-friendly - Automatic rules are refreshed on update state - User-created (and default) automatic rules are always priority only and user cannot change this - Automatic rules redesigned to have headers Test: make ROBOTEST_FILTER=ZenModeAutomaticRulesPreferenceControllerTest RunSettingsRoboTests -j40 Bug: 63077372 Fixes: 68324465 Fixes: 69057696 Change-Id: I163acef2715dd4e60bfc08207f0e22352c4c0e28
This commit is contained in:
@@ -0,0 +1,166 @@
|
||||
/*
|
||||
* Copyright (C) 2017 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* 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.notification;
|
||||
|
||||
import static junit.framework.Assert.assertEquals;
|
||||
import static junit.framework.Assert.assertTrue;
|
||||
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import android.app.AutomaticZenRule;
|
||||
import android.app.Fragment;
|
||||
import android.app.NotificationManager;
|
||||
import android.content.ContentResolver;
|
||||
import android.content.Context;
|
||||
import android.provider.Settings;
|
||||
import android.support.v7.preference.PreferenceCategory;
|
||||
import android.support.v7.preference.PreferenceScreen;
|
||||
|
||||
import com.android.settings.TestConfig;
|
||||
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||
import com.android.settingslib.core.lifecycle.Lifecycle;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
import org.robolectric.annotation.Config;
|
||||
import org.robolectric.shadows.ShadowApplication;
|
||||
import org.robolectric.util.ReflectionHelpers;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@RunWith(SettingsRobolectricTestRunner.class)
|
||||
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
|
||||
public class ZenModeAutomaticRulesPreferenceControllerTest {
|
||||
private ZenModeAutomaticRulesPreferenceController mController;
|
||||
private final String GENERIC_RULE_NAME = "test";
|
||||
|
||||
@Mock
|
||||
private ZenModeBackend mBackend;
|
||||
@Mock
|
||||
private NotificationManager mNotificationManager;
|
||||
@Mock
|
||||
private PreferenceCategory mockPref;
|
||||
@Mock
|
||||
private NotificationManager.Policy mPolicy;
|
||||
@Mock
|
||||
private PreferenceScreen mPreferenceScreen;
|
||||
|
||||
private Context mContext;
|
||||
private ContentResolver mContentResolver;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
ShadowApplication shadowApplication = ShadowApplication.getInstance();
|
||||
shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNotificationManager);
|
||||
|
||||
mContext = shadowApplication.getApplicationContext();
|
||||
mContentResolver = RuntimeEnvironment.application.getContentResolver();
|
||||
when(mNotificationManager.getNotificationPolicy()).thenReturn(mPolicy);
|
||||
mController = new ZenModeAutomaticRulesPreferenceController(mContext, mock(Fragment.class),
|
||||
mock(Lifecycle.class));
|
||||
ReflectionHelpers.setField(mController, "mBackend", mBackend);
|
||||
|
||||
when(mPreferenceScreen.findPreference(mController.getPreferenceKey())).thenReturn(
|
||||
mockPref);
|
||||
mController.displayPreference(mPreferenceScreen);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateState_checkRuleOrderingDescending() {
|
||||
final int NUM_RULES = 4;
|
||||
when(mNotificationManager.getAutomaticZenRules()).thenReturn(
|
||||
mockAutoZenRulesDecreasingCreationTime(NUM_RULES));
|
||||
|
||||
Map.Entry<String, AutomaticZenRule>[] rules = mController.sortedRules();
|
||||
assertEquals(NUM_RULES, rules.length);
|
||||
|
||||
// check ordering, most recent should be at the bottom/end (ie higher creation time)
|
||||
for (int i = 0; i < NUM_RULES; i++) {
|
||||
assertEquals(rules[i].getKey(), GENERIC_RULE_NAME + (NUM_RULES - 1 - i));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateState_checkRuleOrderingAscending() {
|
||||
final int NUM_RULES = 4;
|
||||
when(mNotificationManager.getAutomaticZenRules()).thenReturn(
|
||||
mockAutoZenRulesAscendingCreationTime(NUM_RULES));
|
||||
|
||||
Map.Entry<String, AutomaticZenRule>[] rules = mController.sortedRules();
|
||||
assertEquals(NUM_RULES, rules.length);
|
||||
|
||||
// check ordering, most recent should be at the bottom/end (ie higher creation time)
|
||||
for (int i = 0; i < NUM_RULES; i++) {
|
||||
assertEquals(rules[i].getKey(), GENERIC_RULE_NAME + i);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateState_checkRuleOrderingMix() {
|
||||
final int NUM_RULES = 4;
|
||||
// map with creation times: 0, 2, 4, 6
|
||||
Map<String,AutomaticZenRule> rMap = mockAutoZenRulesAscendingCreationTime(NUM_RULES);
|
||||
|
||||
final String insertedRule1 = "insertedRule1";
|
||||
rMap.put(insertedRule1, new AutomaticZenRule(insertedRule1, null, null,
|
||||
Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS, true, 5));
|
||||
|
||||
final String insertedRule2 = "insertedRule2";
|
||||
rMap.put(insertedRule2, new AutomaticZenRule(insertedRule2, null, null,
|
||||
Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS, true, 3));
|
||||
|
||||
// rule map with rule creation times, 0, 2, 4, 6, 5, 3
|
||||
// sort should create ordering based on creation times: 0, 2, 3, 4, 5, 6
|
||||
when(mNotificationManager.getAutomaticZenRules()).thenReturn(rMap);
|
||||
|
||||
Map.Entry<String, AutomaticZenRule>[] rules = mController.sortedRules();
|
||||
assertEquals(NUM_RULES + 2, rules.length); // inserted 2 rules
|
||||
|
||||
// check ordering of inserted rules
|
||||
assertEquals(rules[4].getKey(), insertedRule1);
|
||||
assertEquals(rules[2].getKey(), insertedRule2);
|
||||
}
|
||||
|
||||
private Map<String, AutomaticZenRule> mockAutoZenRulesAscendingCreationTime(int numRules) {
|
||||
Map<String, AutomaticZenRule> ruleMap = new HashMap<>();
|
||||
|
||||
for (int i = 0; i < numRules; i++) {
|
||||
ruleMap.put(GENERIC_RULE_NAME + i, new AutomaticZenRule(GENERIC_RULE_NAME + i, null,
|
||||
null, Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS, true, i * 2));
|
||||
}
|
||||
|
||||
return ruleMap;
|
||||
}
|
||||
|
||||
private Map<String, AutomaticZenRule> mockAutoZenRulesDecreasingCreationTime(int numRules) {
|
||||
Map<String, AutomaticZenRule> ruleMap = new HashMap<>();
|
||||
|
||||
for (int i = 0; i < numRules; i++) {
|
||||
ruleMap.put(GENERIC_RULE_NAME + i, new AutomaticZenRule(GENERIC_RULE_NAME + i, null,
|
||||
null, Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS, true, numRules - i));
|
||||
}
|
||||
|
||||
return ruleMap;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user