Zen Mode messages + calls are ListPreferences (radio button dialog)

Bug: 63077372
Test: ZenModeCallsPreferenceControllerTest
Test: ZenModeMessagesPreferenceControllerTest
Change-Id: I5d2516402b261413c51a1b4eba5153ffadf1459b
This commit is contained in:
Beverly
2018-01-08 16:31:04 -05:00
committed by Beverly Tai
parent 5ff1df89fa
commit daa13a969f
12 changed files with 227 additions and 548 deletions

View File

@@ -47,16 +47,19 @@
android:key="zen_mode_events" android:key="zen_mode_events"
android:title="@string/zen_mode_events"/> android:title="@string/zen_mode_events"/>
<Preference <!-- Messages -->
<ListPreference
android:key="zen_mode_messages" android:key="zen_mode_messages"
android:title="@string/zen_mode_messages" android:title="@string/zen_mode_messages"
android:fragment="com.android.settings.notification.ZenModeMessagesSettings" /> android:entries="@array/zen_mode_contacts_entries"
android:entryValues="@array/zen_mode_contacts_values"/>
<!-- Calls --> <!-- Calls -->
<Preference <ListPreference
android:key="zen_mode_calls" android:key="zen_mode_calls"
android:title="@string/zen_mode_calls" android:title="@string/zen_mode_calls"
android:fragment="com.android.settings.notification.ZenModeCallsSettings" /> android:entries="@array/zen_mode_contacts_entries"
android:entryValues="@array/zen_mode_contacts_values"/>
<!-- Repeat callers --> <!-- Repeat callers -->
<SwitchPreference <SwitchPreference

View File

@@ -1,21 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
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.
-->
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
android:key="zen_mode_calls_settings"
android:title="@string/zen_mode_calls" />

View File

@@ -1,21 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
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.
-->
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
android:key="zen_mode_messages_settings"
android:title="@string/zen_mode_messages" />

View File

@@ -103,12 +103,19 @@ public class ZenModeBackend {
} }
protected int getPriorityCallSenders() { protected int getPriorityCallSenders() {
if (isPriorityCategoryEnabled(NotificationManager.Policy.PRIORITY_CATEGORY_CALLS)) {
return mPolicy.priorityCallSenders; return mPolicy.priorityCallSenders;
} }
return SOURCE_NONE;
}
protected int getPriorityMessageSenders() { protected int getPriorityMessageSenders() {
if (isPriorityCategoryEnabled(NotificationManager.Policy.PRIORITY_CATEGORY_MESSAGES)) {
return mPolicy.priorityMessageSenders; return mPolicy.priorityMessageSenders;
} }
return SOURCE_NONE;
}
protected void saveVisualEffectsPolicy(int category, boolean canBypass) { protected void saveVisualEffectsPolicy(int category, boolean canBypass) {
int suppressedEffects = getNewSuppressedEffects(!canBypass, category); int suppressedEffects = getNewSuppressedEffects(!canBypass, category);

View File

@@ -19,16 +19,27 @@ package com.android.settings.notification;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.content.Context; import android.content.Context;
import android.provider.Settings; import android.provider.Settings;
import android.support.v7.preference.ListPreference;
import android.support.v7.preference.Preference; import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen;
import android.text.TextUtils;
import com.android.internal.annotations.VisibleForTesting;
import com.android.settings.R;
import com.android.settingslib.core.lifecycle.Lifecycle; import com.android.settingslib.core.lifecycle.Lifecycle;
public class ZenModeCallsPreferenceController extends AbstractZenModePreferenceController { public class ZenModeCallsPreferenceController extends AbstractZenModePreferenceController implements
Preference.OnPreferenceChangeListener {
protected static final String KEY = "zen_mode_calls"; protected static final String KEY = "zen_mode_calls";
private final ZenModeBackend mBackend;
private ListPreference mPreference;
private final String[] mListValues;
public ZenModeCallsPreferenceController(Context context, Lifecycle lifecycle) { public ZenModeCallsPreferenceController(Context context, Lifecycle lifecycle) {
super(context, KEY, lifecycle); super(context, KEY, lifecycle);
mBackend = ZenModeBackend.getInstance(context);
mListValues = context.getResources().getStringArray(R.array.zen_mode_contacts_values);
} }
@Override @Override
@@ -41,21 +52,55 @@ public class ZenModeCallsPreferenceController extends AbstractZenModePreferenceC
return true; return true;
} }
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
mPreference = (ListPreference) screen.findPreference(KEY);
}
@Override @Override
public void updateState(Preference preference) { public void updateState(Preference preference) {
super.updateState(preference); super.updateState(preference);
updateFromContactsValue(preference);
}
@Override
public boolean onPreferenceChange(Preference preference, Object selectedContactsFrom) {
mBackend.saveSenders(NotificationManager.Policy.PRIORITY_CATEGORY_CALLS,
ZenModeBackend.getSettingFromPrefKey(selectedContactsFrom.toString()));
updateFromContactsValue(preference);
return true;
}
private void updateFromContactsValue(Preference preference) {
mPreference = (ListPreference) preference;
switch (getZenMode()) { switch (getZenMode()) {
case Settings.Global.ZEN_MODE_NO_INTERRUPTIONS: case Settings.Global.ZEN_MODE_NO_INTERRUPTIONS:
case Settings.Global.ZEN_MODE_ALARMS: case Settings.Global.ZEN_MODE_ALARMS:
preference.setEnabled(false); mPreference.setEnabled(false);
preference.setSummary(mBackend.getContactsSummary(mBackend.SOURCE_NONE)); mPreference.setValue(ZenModeBackend.ZEN_MODE_FROM_NONE);
mPreference.setSummary(mBackend.getContactsSummary(ZenModeBackend.SOURCE_NONE));
break; break;
default: default:
preference.setEnabled(true); preference.setEnabled(true);
preference.setSummary(mBackend.getContactsSummary( preference.setSummary(mBackend.getContactsSummary(
NotificationManager.Policy.PRIORITY_CATEGORY_CALLS)); NotificationManager.Policy.PRIORITY_CATEGORY_CALLS));
final String currentVal = ZenModeBackend.getKeyFromSetting(
mBackend.getPriorityCallSenders());
mPreference.setValue(mListValues[getIndexOfSendersValue(currentVal)]);
}
} }
@VisibleForTesting
protected int getIndexOfSendersValue(String currentVal) {
int index = 3; // defaults to "none" based on R.array.zen_mode_contacts_values
for (int i = 0; i < mListValues.length; i++) {
if (TextUtils.equals(currentVal, mListValues[i])) {
return i;
}
}
return index;
} }
} }

View File

@@ -1,113 +0,0 @@
/*
* 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 android.app.NotificationManager;
import android.content.Context;
import android.graphics.drawable.Drawable;
import com.android.internal.logging.nano.MetricsProto;
import com.android.settings.R;
import com.android.settings.widget.RadioButtonPickerFragment;
import java.util.ArrayList;
import java.util.List;
public class ZenModeCallsSettings extends RadioButtonPickerFragment {
private ZenModeBackend mBackend;
@Override
public void onAttach(Context context) {
super.onAttach(context);
mBackend = ZenModeBackend.getInstance(context);
}
@Override
public int getMetricsCategory() {
return MetricsProto.MetricsEvent.NOTIFICATION_ZEN_MODE_CALLS;
}
@Override
protected int getPreferenceScreenResId() {
return R.xml.zen_mode_calls_settings;
}
@Override
protected List<? extends RadioButtonPickerFragment.CandidateInfo> getCandidates() {
final String[] entries = entries();
final String[] values = keys();
final List<CallsCandidateInfo> candidates = new ArrayList<>();
if (entries == null || entries.length <= 0) return null;
if (values == null || values.length != entries.length) {
throw new IllegalArgumentException("Entries and values must be of the same length.");
}
for (int i = 0; i < entries.length; i++) {
candidates.add(new CallsCandidateInfo(entries[i], values[i]));
}
return candidates;
}
private String[] entries() {
return getResources().getStringArray(R.array.zen_mode_contacts_entries);
}
private String[] keys() {
return getResources().getStringArray(R.array.zen_mode_contacts_values);
}
@Override
protected String getDefaultKey() {
return mBackend.getSendersKey(NotificationManager.Policy.PRIORITY_CATEGORY_CALLS);
}
@Override
protected boolean setDefaultKey(String key) {
mBackend.saveSenders(NotificationManager.Policy.PRIORITY_CATEGORY_CALLS,
mBackend.getSettingFromPrefKey(key));
return true;
}
private static final class CallsCandidateInfo extends RadioButtonPickerFragment.CandidateInfo {
private final String name;
private final String key;
CallsCandidateInfo(String title, String value) {
super(true);
name = title;
key = value;
}
@Override
public CharSequence loadLabel() {
return name;
}
@Override
public Drawable loadIcon() {
return null;
}
@Override
public String getKey() {
return key;
}
}
}

View File

@@ -3,16 +3,28 @@ package com.android.settings.notification;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.content.Context; import android.content.Context;
import android.provider.Settings; import android.provider.Settings;
import android.support.v7.preference.ListPreference;
import android.support.v7.preference.Preference; import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen;
import android.text.TextUtils;
import com.android.internal.annotations.VisibleForTesting;
import com.android.settings.R;
import com.android.settingslib.core.lifecycle.Lifecycle; import com.android.settingslib.core.lifecycle.Lifecycle;
public class ZenModeMessagesPreferenceController extends AbstractZenModePreferenceController { public class ZenModeMessagesPreferenceController extends AbstractZenModePreferenceController
implements Preference.OnPreferenceChangeListener {
protected static final String KEY = "zen_mode_messages"; protected static final String KEY = "zen_mode_messages";
private final ZenModeBackend mBackend;
private ListPreference mPreference;
private final String[] mListValues;
public ZenModeMessagesPreferenceController(Context context, Lifecycle lifecycle) { public ZenModeMessagesPreferenceController(Context context, Lifecycle lifecycle) {
super(context, KEY, lifecycle); super(context, KEY, lifecycle);
mBackend = ZenModeBackend.getInstance(context);
mListValues = context.getResources().getStringArray(R.array.zen_mode_contacts_values);
} }
@Override @Override
@@ -25,20 +37,55 @@ public class ZenModeMessagesPreferenceController extends AbstractZenModePreferen
return true; return true;
} }
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
mPreference = (ListPreference) screen.findPreference(KEY);
}
@Override @Override
public void updateState(Preference preference) { public void updateState(Preference preference) {
super.updateState(preference); super.updateState(preference);
updateFromContactsValue(preference);
}
@Override
public boolean onPreferenceChange(Preference preference, Object selectedContactsFrom) {
mBackend.saveSenders(NotificationManager.Policy.PRIORITY_CATEGORY_MESSAGES,
ZenModeBackend.getSettingFromPrefKey(selectedContactsFrom.toString()));
updateFromContactsValue(preference);
return true;
}
private void updateFromContactsValue(Preference preference) {
mPreference = (ListPreference) preference;
switch (getZenMode()) { switch (getZenMode()) {
case Settings.Global.ZEN_MODE_NO_INTERRUPTIONS: case Settings.Global.ZEN_MODE_NO_INTERRUPTIONS:
case Settings.Global.ZEN_MODE_ALARMS: case Settings.Global.ZEN_MODE_ALARMS:
preference.setEnabled(false); mPreference.setEnabled(false);
preference.setSummary(mBackend.getContactsSummary(mBackend.SOURCE_NONE)); mPreference.setValue(ZenModeBackend.ZEN_MODE_FROM_NONE);
mPreference.setSummary(mBackend.getContactsSummary(ZenModeBackend.SOURCE_NONE));
break; break;
default: default:
preference.setEnabled(true); preference.setEnabled(true);
preference.setSummary(mBackend.getContactsSummary( preference.setSummary(mBackend.getContactsSummary(
NotificationManager.Policy.PRIORITY_CATEGORY_MESSAGES)); NotificationManager.Policy.PRIORITY_CATEGORY_MESSAGES));
final String currentVal = ZenModeBackend.getKeyFromSetting(
mBackend.getPriorityMessageSenders());
mPreference.setValue(mListValues[getIndexOfSendersValue(currentVal)]);
} }
} }
@VisibleForTesting
protected int getIndexOfSendersValue(String currentVal) {
int index = 3; // defaults to "none" based on R.array.zen_mode_contacts_values
for (int i = 0; i < mListValues.length; i++) {
if (TextUtils.equals(currentVal, mListValues[i])) {
return i;
}
}
return index;
}
} }

View File

@@ -1,112 +0,0 @@
/*
* 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 android.app.NotificationManager;
import android.content.Context;
import android.graphics.drawable.Drawable;
import com.android.internal.logging.nano.MetricsProto;
import com.android.settings.R;
import com.android.settings.widget.RadioButtonPickerFragment;
import java.util.ArrayList;
import java.util.List;
public class ZenModeMessagesSettings extends RadioButtonPickerFragment {
private ZenModeBackend mBackend;
@Override
public void onAttach(Context context) {
super.onAttach(context);
mBackend = ZenModeBackend.getInstance(context);
}
@Override
public int getMetricsCategory() {
return MetricsProto.MetricsEvent.NOTIFICATION_ZEN_MODE_MESSAGES;
}
@Override
protected int getPreferenceScreenResId() {
return R.xml.zen_mode_messages_settings;
}
@Override
protected List<? extends RadioButtonPickerFragment.CandidateInfo> getCandidates() {
final String[] entries = entries();
final String[] values = keys();
final List<MessagesCandidateInfo> candidates = new ArrayList<>();
if (entries == null || entries.length <= 0) return null;
if (values == null || values.length != entries.length) {
throw new IllegalArgumentException("Entries and values must be of the same length.");
}
for (int i = 0; i < entries.length; i++) {
candidates.add(new MessagesCandidateInfo(entries[i], values[i]));
}
return candidates;
}
private String[] entries() {
return getResources().getStringArray(R.array.zen_mode_contacts_entries);
}
private String[] keys() {
return getResources().getStringArray(R.array.zen_mode_contacts_values);
}
@Override
protected String getDefaultKey() {
return mBackend.getSendersKey(NotificationManager.Policy.PRIORITY_CATEGORY_MESSAGES);
}
@Override
protected boolean setDefaultKey(String key) {
mBackend.saveSenders(NotificationManager.Policy.PRIORITY_CATEGORY_MESSAGES,
mBackend.getSettingFromPrefKey(key));
return true;
}
private final class MessagesCandidateInfo extends RadioButtonPickerFragment.CandidateInfo {
private final String name;
private final String key;
MessagesCandidateInfo(String title, String value) {
super(true);
name = title;
key = value;
}
@Override
public CharSequence loadLabel() {
return name;
}
@Override
public Drawable loadIcon() {
return null;
}
@Override
public String getKey() {
return key;
}
}
}

View File

@@ -21,9 +21,6 @@ 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_IMPORTANT_INTERRUPTIONS;
import static android.provider.Settings.Global.ZEN_MODE_NO_INTERRUPTIONS; import static android.provider.Settings.Global.ZEN_MODE_NO_INTERRUPTIONS;
import static junit.framework.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
@@ -32,7 +29,7 @@ import android.app.NotificationManager;
import android.content.ContentResolver; import android.content.ContentResolver;
import android.content.Context; import android.content.Context;
import android.provider.Settings; import android.provider.Settings;
import android.support.v7.preference.Preference; import android.support.v7.preference.ListPreference;
import android.support.v7.preference.PreferenceScreen; import android.support.v7.preference.PreferenceScreen;
import com.android.settings.R; import com.android.settings.R;
@@ -45,9 +42,9 @@ import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.MockitoAnnotations; import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config; import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowApplication; import org.robolectric.shadows.ShadowApplication;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.util.ReflectionHelpers; import org.robolectric.util.ReflectionHelpers;
@RunWith(SettingsRobolectricTestRunner.class) @RunWith(SettingsRobolectricTestRunner.class)
@@ -60,7 +57,7 @@ public class ZenModeCallsPreferenceControllerTest {
@Mock @Mock
private NotificationManager mNotificationManager; private NotificationManager mNotificationManager;
@Mock @Mock
private Preference mockPref; private ListPreference mockPref;
@Mock @Mock
private NotificationManager.Policy mPolicy; private NotificationManager.Policy mPolicy;
@Mock @Mock
@@ -68,6 +65,15 @@ public class ZenModeCallsPreferenceControllerTest {
private ContentResolver mContentResolver; private ContentResolver mContentResolver;
private Context mContext; private Context mContext;
/**
* Array Values Key
* 0: anyone
* 1: contacts
* 2: starred
* 3: none
*/
private String[] mValues;
private final boolean CALLS_SETTINGS = true; private final boolean CALLS_SETTINGS = true;
private final int MOCK_CALLS_SENDERS = NotificationManager.Policy.PRIORITY_SENDERS_STARRED; private final int MOCK_CALLS_SENDERS = NotificationManager.Policy.PRIORITY_SENDERS_STARRED;
private final int SUMMARY_ID_MOCK_CALLS_SENDERS = R.string.zen_mode_from_starred; private final int SUMMARY_ID_MOCK_CALLS_SENDERS = R.string.zen_mode_from_starred;
@@ -79,8 +85,11 @@ public class ZenModeCallsPreferenceControllerTest {
shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNotificationManager); shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNotificationManager);
mContext = shadowApplication.getApplicationContext(); mContext = shadowApplication.getApplicationContext();
mValues = mContext.getResources().getStringArray(R.array.zen_mode_contacts_values);
mContentResolver = RuntimeEnvironment.application.getContentResolver(); mContentResolver = RuntimeEnvironment.application.getContentResolver();
when(mNotificationManager.getNotificationPolicy()).thenReturn(mPolicy); when(mNotificationManager.getNotificationPolicy()).thenReturn(mPolicy);
when(mBackend.getPriorityCallSenders()).thenReturn(MOCK_CALLS_SENDERS);
when(mBackend.getContactsSummary(ZenModeBackend.SOURCE_NONE)) when(mBackend.getContactsSummary(ZenModeBackend.SOURCE_NONE))
.thenCallRealMethod(); .thenCallRealMethod();
when(mBackend.getContactsSummary(NotificationManager.Policy.PRIORITY_CATEGORY_CALLS)) when(mBackend.getContactsSummary(NotificationManager.Policy.PRIORITY_CATEGORY_CALLS))
@@ -97,10 +106,11 @@ public class ZenModeCallsPreferenceControllerTest {
@Test @Test
public void updateState_TotalSilence() { public void updateState_TotalSilence() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_NO_INTERRUPTIONS); Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_NO_INTERRUPTIONS);
when(mBackend.isPriorityCategoryEnabled( when(mBackend.isPriorityCategoryEnabled(
NotificationManager.Policy.PRIORITY_CATEGORY_MESSAGES)) NotificationManager.Policy.PRIORITY_CATEGORY_CALLS))
.thenReturn(false); .thenReturn(false);
final Preference mockPref = mock(Preference.class); final ListPreference mockPref = mock(ListPreference.class);
mController.updateState(mockPref); mController.updateState(mockPref);
verify(mockPref).setEnabled(false); verify(mockPref).setEnabled(false);
@@ -111,7 +121,7 @@ public class ZenModeCallsPreferenceControllerTest {
public void updateState_AlarmsOnly() { public void updateState_AlarmsOnly() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_ALARMS); Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_ALARMS);
final Preference mockPref = mock(Preference.class); final ListPreference mockPref = mock(ListPreference.class);
mController.updateState(mockPref); mController.updateState(mockPref);
verify(mockPref).setEnabled(false); verify(mockPref).setEnabled(false);
@@ -121,14 +131,53 @@ public class ZenModeCallsPreferenceControllerTest {
@Test @Test
public void updateState_Priority() { public void updateState_Priority() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_IMPORTANT_INTERRUPTIONS); Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_IMPORTANT_INTERRUPTIONS);
when(mBackend.isPriorityCategoryEnabled( when(mBackend.isPriorityCategoryEnabled(
NotificationManager.Policy.PRIORITY_CATEGORY_CALLS)) NotificationManager.Policy.PRIORITY_CATEGORY_CALLS))
.thenReturn(CALLS_SETTINGS); .thenReturn(CALLS_SETTINGS);
when(mBackend.getPriorityCallSenders()).thenReturn(MOCK_CALLS_SENDERS);
mController.updateState(mockPref); mController.updateState(mockPref);
verify(mockPref).setEnabled(true); verify(mockPref).setEnabled(true);
verify(mockPref).setSummary(SUMMARY_ID_MOCK_CALLS_SENDERS); verify(mockPref).setSummary(SUMMARY_ID_MOCK_CALLS_SENDERS);
} }
@Test
public void onPreferenceChange_setSelectedContacts_any() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_IMPORTANT_INTERRUPTIONS);
when(mBackend.getPriorityCallSenders()).thenReturn(
NotificationManager.Policy.PRIORITY_SENDERS_ANY);
mController.updateState(mockPref);
verify(mockPref).setValue(mValues[mController.getIndexOfSendersValue(
ZenModeBackend.ZEN_MODE_FROM_ANYONE)]);
}
@Test
public void onPreferenceChange_setSelectedContacts_none() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_IMPORTANT_INTERRUPTIONS);
when(mBackend.getPriorityCallSenders()).thenReturn(ZenModeBackend.SOURCE_NONE);
mController.updateState(mockPref);
verify(mockPref).setValue(mValues[mController.getIndexOfSendersValue(
ZenModeBackend.ZEN_MODE_FROM_NONE)]);
}
@Test
public void onPreferenceChange_setSelectedContacts_starred() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_IMPORTANT_INTERRUPTIONS);
when(mBackend.getPriorityCallSenders()).thenReturn(
NotificationManager.Policy.PRIORITY_SENDERS_STARRED);
mController.updateState(mockPref);
verify(mockPref).setValue(mValues[mController.getIndexOfSendersValue(
ZenModeBackend.ZEN_MODE_FROM_STARRED)]);
}
@Test
public void onPreferenceChange_setSelectedContacts_contacts() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_IMPORTANT_INTERRUPTIONS);
when(mBackend.getPriorityCallSenders()).thenReturn(
NotificationManager.Policy.PRIORITY_SENDERS_CONTACTS);
mController.updateState(mockPref);
verify(mockPref).setValue(mValues[mController.getIndexOfSendersValue(
ZenModeBackend.ZEN_MODE_FROM_CONTACTS)]);
}
} }

View File

@@ -1,126 +0,0 @@
/*
* 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 com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.Activity;
import android.app.NotificationManager;
import android.content.Context;
import android.os.UserManager;
import android.provider.Settings;
import com.android.settings.TestConfig;
import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.annotation.Config;
import org.robolectric.util.ReflectionHelpers;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class ZenModeCallsTest {
private ZenModeCallsSettings mCalls;
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private ZenModeBackend mBackend;
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private Activity mActivity;
@Mock
private UserManager mUserManager;
@Mock
private NotificationManager mNotificationManager;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
when(mActivity.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
when(mActivity.getSystemService(Context.NOTIFICATION_SERVICE))
.thenReturn(mNotificationManager);
FakeFeatureFactory.setupForTest();
mCalls = new ZenModeCallsSettings();
mCalls.onAttach((Context)mActivity);
ReflectionHelpers.setField(mCalls, "mBackend", mBackend);
}
@Test
public void getDefaultKeyReturnsBasedOnZen() {
when(mBackend.getSendersKey(NotificationManager.Policy.PRIORITY_CATEGORY_CALLS))
.thenCallRealMethod();
when(mBackend.getZenMode()).thenReturn(Settings.Global.ZEN_MODE_NO_INTERRUPTIONS);
assertThat(mCalls.getDefaultKey())
.isEqualTo(mBackend.getKeyFromSetting(mBackend.SOURCE_NONE));
when(mBackend.getZenMode()).thenReturn(Settings.Global.ZEN_MODE_ALARMS);
assertThat(mCalls.getDefaultKey())
.isEqualTo(mBackend.getKeyFromSetting(mBackend.SOURCE_NONE));
when(mBackend.getZenMode()).thenReturn(Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS);
when(mBackend.isPriorityCategoryEnabled(
NotificationManager.Policy.PRIORITY_CATEGORY_CALLS))
.thenReturn(true);
when(mBackend.getPriorityMessageSenders())
.thenReturn(NotificationManager.Policy.PRIORITY_SENDERS_ANY);
assertThat(mCalls.getDefaultKey())
.isEqualTo(mBackend.getKeyFromSetting(
NotificationManager.Policy.PRIORITY_SENDERS_ANY));
}
@Test
public void setAnySender() {
String key = mBackend.getKeyFromSetting(NotificationManager.Policy.PRIORITY_SENDERS_ANY);
mCalls.setDefaultKey(key);
verify(mBackend).saveSenders(NotificationManager.Policy.PRIORITY_CATEGORY_CALLS,
mBackend.getSettingFromPrefKey(key));
}
@Test
public void setNoSender() {
String key = mBackend.getKeyFromSetting(ZenModeBackend.SOURCE_NONE);
mCalls.setDefaultKey(key);
verify(mBackend).saveSenders(NotificationManager.Policy.PRIORITY_CATEGORY_CALLS,
mBackend.getSettingFromPrefKey(key));
}
@Test
public void setStarredSenders() {
String key = mBackend.getKeyFromSetting(
NotificationManager.Policy.PRIORITY_SENDERS_STARRED);
mCalls.setDefaultKey(key);
verify(mBackend).saveSenders(NotificationManager.Policy.PRIORITY_CATEGORY_CALLS,
mBackend.getSettingFromPrefKey(key));
}
@Test
public void setContactsOnlySenders() {
String key = mBackend.getKeyFromSetting(
NotificationManager.Policy.PRIORITY_SENDERS_CONTACTS);
mCalls.setDefaultKey(key);
verify(mBackend).saveSenders(NotificationManager.Policy.PRIORITY_CATEGORY_CALLS,
mBackend.getSettingFromPrefKey(key));
}
}

View File

@@ -21,9 +21,6 @@ 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_IMPORTANT_INTERRUPTIONS;
import static android.provider.Settings.Global.ZEN_MODE_NO_INTERRUPTIONS; import static android.provider.Settings.Global.ZEN_MODE_NO_INTERRUPTIONS;
import static junit.framework.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
@@ -32,6 +29,7 @@ import android.app.NotificationManager;
import android.content.ContentResolver; import android.content.ContentResolver;
import android.content.Context; import android.content.Context;
import android.provider.Settings; import android.provider.Settings;
import android.support.v7.preference.ListPreference;
import android.support.v7.preference.Preference; import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen; import android.support.v7.preference.PreferenceScreen;
@@ -45,9 +43,9 @@ import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.MockitoAnnotations; import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config; import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowApplication; import org.robolectric.shadows.ShadowApplication;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.util.ReflectionHelpers; import org.robolectric.util.ReflectionHelpers;
@RunWith(SettingsRobolectricTestRunner.class) @RunWith(SettingsRobolectricTestRunner.class)
@@ -60,7 +58,7 @@ public class ZenModeMessagesPreferenceControllerTest {
@Mock @Mock
private NotificationManager mNotificationManager; private NotificationManager mNotificationManager;
@Mock @Mock
private Preference mockPref; private ListPreference mockPref;
@Mock @Mock
private NotificationManager.Policy mPolicy; private NotificationManager.Policy mPolicy;
@Mock @Mock
@@ -68,6 +66,15 @@ public class ZenModeMessagesPreferenceControllerTest {
private ContentResolver mContentResolver; private ContentResolver mContentResolver;
private Context mContext; private Context mContext;
/**
* Array Values Key
* 0: anyone
* 1: contacts
* 2: starred
* 3: none
*/
private String[] mValues;
private final boolean MESSAGES_SETTINGS = true; private final boolean MESSAGES_SETTINGS = true;
private final int MOCK_MESSAGES_SENDERS = NotificationManager.Policy.PRIORITY_SENDERS_STARRED; private final int MOCK_MESSAGES_SENDERS = NotificationManager.Policy.PRIORITY_SENDERS_STARRED;
private final int SUMMARY_ID_MOCK_MESSAGES_SENDERS = R.string.zen_mode_from_starred; private final int SUMMARY_ID_MOCK_MESSAGES_SENDERS = R.string.zen_mode_from_starred;
@@ -79,6 +86,7 @@ public class ZenModeMessagesPreferenceControllerTest {
shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNotificationManager); shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNotificationManager);
mContext = shadowApplication.getApplicationContext(); mContext = shadowApplication.getApplicationContext();
mValues = mContext.getResources().getStringArray(R.array.zen_mode_contacts_values);
mContentResolver = RuntimeEnvironment.application.getContentResolver(); mContentResolver = RuntimeEnvironment.application.getContentResolver();
when(mNotificationManager.getNotificationPolicy()).thenReturn(mPolicy); when(mNotificationManager.getNotificationPolicy()).thenReturn(mPolicy);
@@ -103,7 +111,7 @@ public class ZenModeMessagesPreferenceControllerTest {
when(mBackend.isPriorityCategoryEnabled( when(mBackend.isPriorityCategoryEnabled(
NotificationManager.Policy.PRIORITY_CATEGORY_MESSAGES)) NotificationManager.Policy.PRIORITY_CATEGORY_MESSAGES))
.thenReturn(false); .thenReturn(false);
final Preference mockPref = mock(Preference.class); final ListPreference mockPref = mock(ListPreference.class);
mController.updateState(mockPref); mController.updateState(mockPref);
verify(mockPref).setEnabled(false); verify(mockPref).setEnabled(false);
@@ -114,7 +122,7 @@ public class ZenModeMessagesPreferenceControllerTest {
public void updateState_AlarmsOnly() { public void updateState_AlarmsOnly() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_ALARMS); Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_ALARMS);
final Preference mockPref = mock(Preference.class); final ListPreference mockPref = mock(ListPreference.class);
mController.updateState(mockPref); mController.updateState(mockPref);
verify(mockPref).setEnabled(false); verify(mockPref).setEnabled(false);
@@ -134,4 +142,43 @@ public class ZenModeMessagesPreferenceControllerTest {
verify(mockPref).setEnabled(true); verify(mockPref).setEnabled(true);
verify(mockPref).setSummary(SUMMARY_ID_MOCK_MESSAGES_SENDERS); verify(mockPref).setSummary(SUMMARY_ID_MOCK_MESSAGES_SENDERS);
} }
@Test
public void onPreferenceChange_setSelectedContacts_any() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_IMPORTANT_INTERRUPTIONS);
when(mBackend.getPriorityMessageSenders()).thenReturn(
NotificationManager.Policy.PRIORITY_SENDERS_ANY);
mController.updateState(mockPref);
verify(mockPref).setValue(mValues[mController.getIndexOfSendersValue(
ZenModeBackend.ZEN_MODE_FROM_ANYONE)]);
}
@Test
public void onPreferenceChange_setSelectedContacts_none() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_IMPORTANT_INTERRUPTIONS);
when(mBackend.getPriorityMessageSenders()).thenReturn(ZenModeBackend.SOURCE_NONE);
mController.updateState(mockPref);
verify(mockPref).setValue(mValues[mController.getIndexOfSendersValue(
ZenModeBackend.ZEN_MODE_FROM_NONE)]);
}
@Test
public void onPreferenceChange_setSelectedContacts_starred() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_IMPORTANT_INTERRUPTIONS);
when(mBackend.getPriorityMessageSenders()).thenReturn(
NotificationManager.Policy.PRIORITY_SENDERS_STARRED);
mController.updateState(mockPref);
verify(mockPref).setValue(mValues[mController.getIndexOfSendersValue(
ZenModeBackend.ZEN_MODE_FROM_STARRED)]);
}
@Test
public void onPreferenceChange_setSelectedContacts_contacts() {
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_IMPORTANT_INTERRUPTIONS);
when(mBackend.getPriorityMessageSenders()).thenReturn(
NotificationManager.Policy.PRIORITY_SENDERS_CONTACTS);
mController.updateState(mockPref);
verify(mockPref).setValue(mValues[mController.getIndexOfSendersValue(
ZenModeBackend.ZEN_MODE_FROM_CONTACTS)]);
}
} }

View File

@@ -1,126 +0,0 @@
/*
* 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 com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.Activity;
import android.app.NotificationManager;
import android.content.Context;
import android.os.UserManager;
import android.provider.Settings;
import com.android.settings.TestConfig;
import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.annotation.Config;
import org.robolectric.util.ReflectionHelpers;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class ZenModeMessagesTest {
private ZenModeMessagesSettings mMessages;
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private ZenModeBackend mBackend;
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private Activity mActivity;
@Mock
private UserManager mUserManager;
@Mock
private NotificationManager mNotificationManager;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
when(mActivity.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
when(mActivity.getSystemService(Context.NOTIFICATION_SERVICE))
.thenReturn(mNotificationManager);
FakeFeatureFactory.setupForTest();
mMessages = new ZenModeMessagesSettings();
mMessages.onAttach((Context)mActivity);
ReflectionHelpers.setField(mMessages, "mBackend", mBackend);
}
@Test
public void getDefaultKeyReturnsBasedOnZen() {
when(mBackend.getSendersKey(NotificationManager.Policy.PRIORITY_CATEGORY_MESSAGES))
.thenCallRealMethod();
when(mBackend.getZenMode()).thenReturn(Settings.Global.ZEN_MODE_NO_INTERRUPTIONS);
assertThat(mMessages.getDefaultKey())
.isEqualTo(mBackend.getKeyFromSetting(mBackend.SOURCE_NONE));
when(mBackend.getZenMode()).thenReturn(Settings.Global.ZEN_MODE_ALARMS);
assertThat(mMessages.getDefaultKey())
.isEqualTo(mBackend.getKeyFromSetting(mBackend.SOURCE_NONE));
when(mBackend.getZenMode()).thenReturn(Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS);
when(mBackend.isPriorityCategoryEnabled(
NotificationManager.Policy.PRIORITY_CATEGORY_MESSAGES))
.thenReturn(true);
when(mBackend.getPriorityMessageSenders())
.thenReturn(NotificationManager.Policy.PRIORITY_SENDERS_ANY);
assertThat(mMessages.getDefaultKey())
.isEqualTo(mBackend.getKeyFromSetting(
NotificationManager.Policy.PRIORITY_SENDERS_ANY));
}
@Test
public void setAnySender() {
String key = mBackend.getKeyFromSetting(NotificationManager.Policy.PRIORITY_SENDERS_ANY);
mMessages.setDefaultKey(key);
verify(mBackend).saveSenders(NotificationManager.Policy.PRIORITY_CATEGORY_MESSAGES,
mBackend.getSettingFromPrefKey(key));
}
@Test
public void setNoSender() {
String key = mBackend.getKeyFromSetting(ZenModeBackend.SOURCE_NONE);
mMessages.setDefaultKey(key);
verify(mBackend).saveSenders(NotificationManager.Policy.PRIORITY_CATEGORY_MESSAGES,
mBackend.getSettingFromPrefKey(key));
}
@Test
public void setStarredSenders() {
String key = mBackend.getKeyFromSetting(
NotificationManager.Policy.PRIORITY_SENDERS_STARRED);
mMessages.setDefaultKey(key);
verify(mBackend).saveSenders(NotificationManager.Policy.PRIORITY_CATEGORY_MESSAGES,
mBackend.getSettingFromPrefKey(key));
}
@Test
public void setContactsOnlySenders() {
String key = mBackend.getKeyFromSetting(
NotificationManager.Policy.PRIORITY_SENDERS_CONTACTS);
mMessages.setDefaultKey(key);
verify(mBackend).saveSenders(NotificationManager.Policy.PRIORITY_CATEGORY_MESSAGES,
mBackend.getSettingFromPrefKey(key));
}
}