Merge "Support new conversation DND options"

This commit is contained in:
Julia Reynolds
2020-02-05 12:21:25 +00:00
committed by Android (Google) Code Review
16 changed files with 530 additions and 294 deletions

View File

@@ -1,25 +1,41 @@
package com.android.settings.notification.zen;
import static android.app.NotificationManager.Policy.CONVERSATION_SENDERS_ANYONE;
import static android.app.NotificationManager.Policy.CONVERSATION_SENDERS_IMPORTANT;
import static android.app.NotificationManager.Policy.CONVERSATION_SENDERS_NONE;
import static android.app.NotificationManager.Policy.PRIORITY_CATEGORY_ALARMS;
import static android.app.NotificationManager.Policy.PRIORITY_CATEGORY_CALLS;
import static android.app.NotificationManager.Policy.PRIORITY_CATEGORY_CONVERSATIONS;
import static android.app.NotificationManager.Policy.PRIORITY_CATEGORY_MESSAGES;
import static android.app.NotificationManager.Policy.PRIORITY_SENDERS_ANY;
import static android.app.NotificationManager.Policy.PRIORITY_SENDERS_CONTACTS;
import static android.app.NotificationManager.Policy.PRIORITY_SENDERS_STARRED;
import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_AMBIENT;
import static com.google.common.truth.Truth.assertThat;
import static junit.framework.Assert.assertEquals;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.AutomaticZenRule;
import android.app.NotificationManager;
import android.app.NotificationManager.Policy;
import android.content.Context;
import android.database.Cursor;
import android.provider.Settings;
import android.service.notification.ZenModeConfig;
import com.android.settings.notification.zen.ZenModeBackend;
import com.android.settings.R;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.invocation.InvocationOnMock;
@@ -55,60 +71,6 @@ public class ZenModeBackendTest {
mBackend = new ZenModeBackend(mContext);
}
@Test
public void updateState_checkRuleOrderingDescending() {
final int NUM_RULES = 4;
Map.Entry<String, AutomaticZenRule>[] rules = populateAutoZenRulesDescendingCreationTime(
NUM_RULES, false);
Arrays.sort(rules, ZenModeBackend.RULE_COMPARATOR);
// check ordering, most recent should be at the end
for (int i = 0; i < NUM_RULES; i++) {
assertEquals(GENERIC_RULE_NAME + (NUM_RULES - 1 - i), rules[i].getKey());
}
}
@Test
public void updateState_checkRuleOrderingAscending() {
final int NUM_RULES = 4;
Map.Entry<String, AutomaticZenRule>[] rules = populateAutoZenRulesAscendingCreationTime(
NUM_RULES, false);
Arrays.sort(rules, ZenModeBackend.RULE_COMPARATOR);
// check ordering, most recent should be at the end
for (int i = 0; i < NUM_RULES; i++) {
assertEquals(GENERIC_RULE_NAME + i, rules[i].getKey());
}
}
@Test
public void updateState_checkRuleOrderingDescending_withDefaultRules() {
final int NUM_RULES = 4;
Map.Entry<String, AutomaticZenRule>[] rules = populateAutoZenRulesDescendingCreationTime(NUM_RULES,
true);
Arrays.sort(rules, ZenModeBackend.RULE_COMPARATOR);
assertEquals(rules[0].getKey(), DEFAULT_ID_1);
assertEquals(rules[1].getKey(), DEFAULT_ID_2);
// NON-DEFAULT RULES check ordering, most recent at the bottom/end
for (int i = 0; i < NUM_RULES; i++) {
assertEquals(GENERIC_RULE_NAME + (NUM_RULES - 1 - i), rules[i + 2].getKey());
}
}
@Test
public void updateSummary_nullCursorValues() {
Cursor testCursorWithNullValues = createMockCursor(3);
when(testCursorWithNullValues.getString(0)).thenReturn(null);
// expected - no null values
List<String> contacts = mBackend.getStarredContacts(testCursorWithNullValues);
for (String contact : contacts) {
assertThat(contact).isNotNull();
}
}
private Cursor createMockCursor(int size) {
Cursor mockCursor = mock(Cursor.class);
when(mockCursor.moveToFirst()).thenReturn(true);
@@ -173,4 +135,133 @@ public class ZenModeBackendTest {
ruleMap.entrySet().toArray(toReturn);
return toReturn;
}
@Test
public void updateState_checkRuleOrderingDescending() {
final int NUM_RULES = 4;
Map.Entry<String, AutomaticZenRule>[] rules = populateAutoZenRulesDescendingCreationTime(
NUM_RULES, false);
Arrays.sort(rules, ZenModeBackend.RULE_COMPARATOR);
// check ordering, most recent should be at the end
for (int i = 0; i < NUM_RULES; i++) {
assertEquals(GENERIC_RULE_NAME + (NUM_RULES - 1 - i), rules[i].getKey());
}
}
@Test
public void updateState_checkRuleOrderingAscending() {
final int NUM_RULES = 4;
Map.Entry<String, AutomaticZenRule>[] rules = populateAutoZenRulesAscendingCreationTime(
NUM_RULES, false);
Arrays.sort(rules, ZenModeBackend.RULE_COMPARATOR);
// check ordering, most recent should be at the end
for (int i = 0; i < NUM_RULES; i++) {
assertEquals(GENERIC_RULE_NAME + i, rules[i].getKey());
}
}
@Test
public void updateState_checkRuleOrderingDescending_withDefaultRules() {
final int NUM_RULES = 4;
Map.Entry<String, AutomaticZenRule>[] rules = populateAutoZenRulesDescendingCreationTime(NUM_RULES,
true);
Arrays.sort(rules, ZenModeBackend.RULE_COMPARATOR);
assertEquals(rules[0].getKey(), DEFAULT_ID_1);
assertEquals(rules[1].getKey(), DEFAULT_ID_2);
// NON-DEFAULT RULES check ordering, most recent at the bottom/end
for (int i = 0; i < NUM_RULES; i++) {
assertEquals(GENERIC_RULE_NAME + (NUM_RULES - 1 - i), rules[i + 2].getKey());
}
}
@Test
public void updateSummary_nullCursorValues() {
Cursor testCursorWithNullValues = createMockCursor(3);
when(testCursorWithNullValues.getString(0)).thenReturn(null);
// expected - no null values
List<String> contacts = mBackend.getStarredContacts(testCursorWithNullValues);
for (String contact : contacts) {
assertThat(contact).isNotNull();
}
}
@Test
public void saveConversationSenders_importantToNone() {
when(mNotificationManager.getNotificationPolicy()).thenReturn(
new Policy(PRIORITY_CATEGORY_CALLS | PRIORITY_CATEGORY_CONVERSATIONS
| PRIORITY_CATEGORY_MESSAGES | PRIORITY_CATEGORY_ALARMS,
PRIORITY_SENDERS_CONTACTS,
PRIORITY_SENDERS_STARRED,
SUPPRESSED_EFFECT_AMBIENT,
CONVERSATION_SENDERS_IMPORTANT));
mBackend = new ZenModeBackend(mContext);
mBackend.saveConversationSenders(CONVERSATION_SENDERS_NONE);
ArgumentCaptor<Policy> captor = ArgumentCaptor.forClass(Policy.class);
verify(mNotificationManager, times(1)).setNotificationPolicy(captor.capture());
Policy expected = new Policy(
PRIORITY_CATEGORY_CALLS | PRIORITY_CATEGORY_MESSAGES | PRIORITY_CATEGORY_ALARMS,
PRIORITY_SENDERS_CONTACTS,
PRIORITY_SENDERS_STARRED,
SUPPRESSED_EFFECT_AMBIENT,
CONVERSATION_SENDERS_NONE);
assertEquals(expected, captor.getValue());
}
@Test
public void saveConversationSenders_noneToAll() {
when(mNotificationManager.getNotificationPolicy()).thenReturn(new Policy(
PRIORITY_CATEGORY_CALLS | PRIORITY_CATEGORY_MESSAGES | PRIORITY_CATEGORY_ALARMS,
PRIORITY_SENDERS_CONTACTS,
PRIORITY_SENDERS_STARRED,
SUPPRESSED_EFFECT_AMBIENT,
CONVERSATION_SENDERS_NONE));
mBackend = new ZenModeBackend(mContext);
mBackend.saveConversationSenders(CONVERSATION_SENDERS_ANYONE);
ArgumentCaptor<Policy> captor = ArgumentCaptor.forClass(Policy.class);
verify(mNotificationManager, times(1)).setNotificationPolicy(captor.capture());
Policy expected = new Policy(PRIORITY_CATEGORY_CONVERSATIONS
| PRIORITY_CATEGORY_CALLS | PRIORITY_CATEGORY_MESSAGES | PRIORITY_CATEGORY_ALARMS,
PRIORITY_SENDERS_CONTACTS,
PRIORITY_SENDERS_STARRED,
SUPPRESSED_EFFECT_AMBIENT,
CONVERSATION_SENDERS_ANYONE);
assertEquals(expected, captor.getValue());
}
@Test
public void saveSenders_doesNotChangeConversations() {
when(mNotificationManager.getNotificationPolicy()).thenReturn(
new Policy(PRIORITY_CATEGORY_CALLS | PRIORITY_CATEGORY_CONVERSATIONS
| PRIORITY_CATEGORY_MESSAGES | PRIORITY_CATEGORY_ALARMS,
PRIORITY_SENDERS_CONTACTS,
PRIORITY_SENDERS_STARRED,
SUPPRESSED_EFFECT_AMBIENT,
CONVERSATION_SENDERS_ANYONE));
mBackend = new ZenModeBackend(mContext);
mBackend.saveSenders(PRIORITY_CATEGORY_CALLS, PRIORITY_SENDERS_ANY);
ArgumentCaptor<Policy> captor = ArgumentCaptor.forClass(Policy.class);
verify(mNotificationManager, times(1)).setNotificationPolicy(captor.capture());
Policy expected = new Policy(PRIORITY_CATEGORY_CONVERSATIONS
| PRIORITY_CATEGORY_CALLS | PRIORITY_CATEGORY_MESSAGES | PRIORITY_CATEGORY_ALARMS,
PRIORITY_SENDERS_ANY,
PRIORITY_SENDERS_STARRED,
SUPPRESSED_EFFECT_AMBIENT,
CONVERSATION_SENDERS_ANYONE);
assertEquals(expected, captor.getValue());
}
}

View File

@@ -34,8 +34,6 @@ import androidx.preference.ListPreference;
import androidx.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settings.notification.zen.ZenModeBackend;
import com.android.settings.notification.zen.ZenModePriorityCallsPreferenceController;
import com.android.settingslib.core.lifecycle.Lifecycle;
import org.junit.Before;
@@ -88,7 +86,7 @@ public class ZenModePriorityCallsPreferenceControllerTest {
when(mBackend.getPriorityCallSenders())
.thenReturn(NotificationManager.Policy.PRIORITY_SENDERS_STARRED);
when(mBackend.getAlarmsTotalSilenceCallsMessagesSummary(
when(mBackend.getAlarmsTotalSilencePeopleSummary(
NotificationManager.Policy.PRIORITY_CATEGORY_CALLS)).thenCallRealMethod();
when(mBackend.getContactsSummary(NotificationManager.Policy.PRIORITY_CATEGORY_CALLS))
.thenCallRealMethod();

View File

@@ -0,0 +1,191 @@
/*
* Copyright (C) 2020 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.zen;
import static android.app.NotificationManager.Policy.CONVERSATION_SENDERS_ANYONE;
import static android.app.NotificationManager.Policy.CONVERSATION_SENDERS_IMPORTANT;
import static android.app.NotificationManager.Policy.CONVERSATION_SENDERS_NONE;
import static android.app.NotificationManager.Policy.PRIORITY_CATEGORY_CONVERSATIONS;
import static android.provider.Settings.Global.ZEN_MODE;
import static android.provider.Settings.Global.ZEN_MODE_ALARMS;
import static android.provider.Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS;
import static android.provider.Settings.Global.ZEN_MODE_NO_INTERRUPTIONS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.NotificationManager;
import android.content.ContentResolver;
import android.content.Context;
import android.provider.Settings;
import androidx.preference.ListPreference;
import androidx.preference.PreferenceScreen;
import com.android.settings.R;
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.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.shadows.ShadowApplication;
import org.robolectric.util.ReflectionHelpers;
@RunWith(RobolectricTestRunner.class)
public class ZenModePriorityConversationsPreferenceControllerTest {
private ZenModePriorityConversationsPreferenceController mController;
@Mock
private ZenModeBackend mBackend;
@Mock
private NotificationManager mNotificationManager;
@Mock
private ListPreference mockPref;
@Mock
private NotificationManager.Policy mPolicy;
@Mock
private PreferenceScreen mPreferenceScreen;
private ContentResolver mContentResolver;
private Context mContext;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
ShadowApplication shadowApplication = ShadowApplication.getInstance();
shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNotificationManager);
mContext = RuntimeEnvironment.application;
mContentResolver = RuntimeEnvironment.application.getContentResolver();
when(mNotificationManager.getNotificationPolicy()).thenReturn(mPolicy);
when(mBackend.getPriorityConversationSenders())
.thenReturn(CONVERSATION_SENDERS_IMPORTANT);
when(mBackend.getAlarmsTotalSilencePeopleSummary(PRIORITY_CATEGORY_CONVERSATIONS))
.thenCallRealMethod();
when(mBackend.getConversationSummary()).thenCallRealMethod();
mController = new ZenModePriorityConversationsPreferenceController(
mContext, mock(Lifecycle.class));
ReflectionHelpers.setField(mController, "mBackend", mBackend);
when(mPreferenceScreen.findPreference(mController.getPreferenceKey())).thenReturn(mockPref);
mController.displayPreference(mPreferenceScreen);
}
@Test
public void updateState_TotalSilence() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_NO_INTERRUPTIONS);
when(mBackend.isPriorityCategoryEnabled(PRIORITY_CATEGORY_CONVERSATIONS)).thenReturn(true);
final ListPreference mockPref = mock(ListPreference.class);
mController.updateState(mockPref);
verify(mockPref).setEnabled(false);
verify(mockPref).setSummary(R.string.zen_mode_from_no_conversations);
}
@Test
public void updateState_AlarmsOnly() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_ALARMS);
final ListPreference mockPref = mock(ListPreference.class);
mController.updateState(mockPref);
verify(mockPref).setEnabled(false);
verify(mockPref).setSummary(R.string.zen_mode_from_no_conversations);
}
@Test
public void updateState_Priority_important() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_IMPORTANT_INTERRUPTIONS);
when(mBackend.isPriorityCategoryEnabled(PRIORITY_CATEGORY_CONVERSATIONS)).thenReturn(true);
mController.updateState(mockPref);
verify(mockPref).setEnabled(true);
verify(mockPref).setSummary(R.string.zen_mode_from_important_conversations);
verify(mockPref).setValue(String.valueOf(CONVERSATION_SENDERS_IMPORTANT));
}
@Test
public void updateState_Priority_all() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_IMPORTANT_INTERRUPTIONS);
when(mBackend.getPriorityConversationSenders()).thenReturn(CONVERSATION_SENDERS_ANYONE);
when(mBackend.isPriorityCategoryEnabled(PRIORITY_CATEGORY_CONVERSATIONS)).thenReturn(true);
mController.updateState(mockPref);
verify(mockPref).setEnabled(true);
verify(mockPref).setSummary(R.string.zen_mode_from_all_conversations);
verify(mockPref).setValue(String.valueOf(CONVERSATION_SENDERS_ANYONE));
}
@Test
public void updateState_Priority_none() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_IMPORTANT_INTERRUPTIONS);
when(mBackend.getPriorityConversationSenders()).thenReturn(CONVERSATION_SENDERS_NONE);
when(mBackend.isPriorityCategoryEnabled(PRIORITY_CATEGORY_CONVERSATIONS)).thenReturn(false);
mController.updateState(mockPref);
verify(mockPref).setEnabled(true);
verify(mockPref).setSummary(R.string.zen_mode_from_no_conversations);
verify(mockPref).setValue(String.valueOf(CONVERSATION_SENDERS_NONE));
}
@Test
public void onPreferenceChange_noneToImportant() {
// start with none
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_IMPORTANT_INTERRUPTIONS);
when(mBackend.getPriorityConversationSenders()).thenReturn(CONVERSATION_SENDERS_NONE);
when(mBackend.isPriorityCategoryEnabled(PRIORITY_CATEGORY_CONVERSATIONS)).thenReturn(false);
mController.updateState(mockPref);
reset(mBackend);
mController.onPreferenceChange(mockPref, String.valueOf(CONVERSATION_SENDERS_IMPORTANT));
verify(mBackend).saveConversationSenders(CONVERSATION_SENDERS_IMPORTANT);
verify(mBackend).getPriorityConversationSenders();
}
@Test
public void onPreferenceChange_allToNone() {
// start with none
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_IMPORTANT_INTERRUPTIONS);
when(mBackend.getPriorityConversationSenders()).thenReturn(CONVERSATION_SENDERS_ANYONE);
when(mBackend.isPriorityCategoryEnabled(PRIORITY_CATEGORY_CONVERSATIONS)).thenReturn(true);
mController.updateState(mockPref);
reset(mBackend);
mController.onPreferenceChange(mockPref, String.valueOf(CONVERSATION_SENDERS_NONE));
verify(mBackend).saveConversationSenders(CONVERSATION_SENDERS_NONE);
verify(mBackend).getPriorityConversationSenders();
}
}

View File

@@ -34,8 +34,6 @@ import androidx.preference.ListPreference;
import androidx.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settings.notification.zen.ZenModeBackend;
import com.android.settings.notification.zen.ZenModePriorityMessagesPreferenceController;
import com.android.settingslib.core.lifecycle.Lifecycle;
import org.junit.Before;
@@ -88,7 +86,7 @@ public class ZenModePriorityMessagesPreferenceControllerTest {
when(mBackend.getPriorityMessageSenders())
.thenReturn(NotificationManager.Policy.PRIORITY_SENDERS_STARRED);
when(mBackend.getAlarmsTotalSilenceCallsMessagesSummary(
when(mBackend.getAlarmsTotalSilencePeopleSummary(
NotificationManager.Policy.PRIORITY_CATEGORY_MESSAGES)).thenCallRealMethod();
when(mBackend.getContactsSummary(NotificationManager.Policy.PRIORITY_CATEGORY_MESSAGES))
.thenCallRealMethod();