diff --git a/res/xml/zen_mode_behavior_settings.xml b/res/xml/zen_mode_behavior_settings.xml index 31d429bb409..311b67b0dd4 100644 --- a/res/xml/zen_mode_behavior_settings.xml +++ b/res/xml/zen_mode_behavior_settings.xml @@ -47,16 +47,19 @@ android:key="zen_mode_events" android:title="@string/zen_mode_events"/> - + + android:entries="@array/zen_mode_contacts_entries" + android:entryValues="@array/zen_mode_contacts_values"/> - + android:entries="@array/zen_mode_contacts_entries" + android:entryValues="@array/zen_mode_contacts_values"/> - - - diff --git a/res/xml/zen_mode_messages_settings.xml b/res/xml/zen_mode_messages_settings.xml deleted file mode 100644 index 4b4a1e2b311..00000000000 --- a/res/xml/zen_mode_messages_settings.xml +++ /dev/null @@ -1,21 +0,0 @@ - - - - diff --git a/src/com/android/settings/notification/ZenModeBackend.java b/src/com/android/settings/notification/ZenModeBackend.java index 6cee28e26aa..158f9acf3e9 100644 --- a/src/com/android/settings/notification/ZenModeBackend.java +++ b/src/com/android/settings/notification/ZenModeBackend.java @@ -103,11 +103,18 @@ public class ZenModeBackend { } protected int getPriorityCallSenders() { - return mPolicy.priorityCallSenders; + if (isPriorityCategoryEnabled(NotificationManager.Policy.PRIORITY_CATEGORY_CALLS)) { + return mPolicy.priorityCallSenders; + } + + return SOURCE_NONE; } protected int getPriorityMessageSenders() { - return mPolicy.priorityMessageSenders; + if (isPriorityCategoryEnabled(NotificationManager.Policy.PRIORITY_CATEGORY_MESSAGES)) { + return mPolicy.priorityMessageSenders; + } + return SOURCE_NONE; } protected void saveVisualEffectsPolicy(int category, boolean canBypass) { diff --git a/src/com/android/settings/notification/ZenModeCallsPreferenceController.java b/src/com/android/settings/notification/ZenModeCallsPreferenceController.java index 3e2f8022f58..ef1b09ab0b9 100644 --- a/src/com/android/settings/notification/ZenModeCallsPreferenceController.java +++ b/src/com/android/settings/notification/ZenModeCallsPreferenceController.java @@ -19,16 +19,27 @@ package com.android.settings.notification; import android.app.NotificationManager; import android.content.Context; import android.provider.Settings; +import android.support.v7.preference.ListPreference; 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; -public class ZenModeCallsPreferenceController extends AbstractZenModePreferenceController { +public class ZenModeCallsPreferenceController extends AbstractZenModePreferenceController implements + Preference.OnPreferenceChangeListener { 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) { super(context, KEY, lifecycle); + mBackend = ZenModeBackend.getInstance(context); + mListValues = context.getResources().getStringArray(R.array.zen_mode_contacts_values); } @Override @@ -41,21 +52,55 @@ public class ZenModeCallsPreferenceController extends AbstractZenModePreferenceC return true; } + @Override + public void displayPreference(PreferenceScreen screen) { + super.displayPreference(screen); + mPreference = (ListPreference) screen.findPreference(KEY); + } + @Override public void updateState(Preference 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()) { case Settings.Global.ZEN_MODE_NO_INTERRUPTIONS: case Settings.Global.ZEN_MODE_ALARMS: - preference.setEnabled(false); - preference.setSummary(mBackend.getContactsSummary(mBackend.SOURCE_NONE)); + mPreference.setEnabled(false); + mPreference.setValue(ZenModeBackend.ZEN_MODE_FROM_NONE); + mPreference.setSummary(mBackend.getContactsSummary(ZenModeBackend.SOURCE_NONE)); break; default: preference.setEnabled(true); preference.setSummary(mBackend.getContactsSummary( 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; } } diff --git a/src/com/android/settings/notification/ZenModeCallsSettings.java b/src/com/android/settings/notification/ZenModeCallsSettings.java deleted file mode 100644 index 6874dc0f775..00000000000 --- a/src/com/android/settings/notification/ZenModeCallsSettings.java +++ /dev/null @@ -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 getCandidates() { - final String[] entries = entries(); - final String[] values = keys(); - final List 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; - } - } -} diff --git a/src/com/android/settings/notification/ZenModeMessagesPreferenceController.java b/src/com/android/settings/notification/ZenModeMessagesPreferenceController.java index 0ffc44d4b7f..93d4aa765e7 100644 --- a/src/com/android/settings/notification/ZenModeMessagesPreferenceController.java +++ b/src/com/android/settings/notification/ZenModeMessagesPreferenceController.java @@ -3,16 +3,28 @@ package com.android.settings.notification; import android.app.NotificationManager; import android.content.Context; import android.provider.Settings; +import android.support.v7.preference.ListPreference; 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; -public class ZenModeMessagesPreferenceController extends AbstractZenModePreferenceController { +public class ZenModeMessagesPreferenceController extends AbstractZenModePreferenceController + implements Preference.OnPreferenceChangeListener { 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) { super(context, KEY, lifecycle); + mBackend = ZenModeBackend.getInstance(context); + mListValues = context.getResources().getStringArray(R.array.zen_mode_contacts_values); } @Override @@ -25,20 +37,55 @@ public class ZenModeMessagesPreferenceController extends AbstractZenModePreferen return true; } + @Override + public void displayPreference(PreferenceScreen screen) { + super.displayPreference(screen); + mPreference = (ListPreference) screen.findPreference(KEY); + } + @Override public void updateState(Preference 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()) { case Settings.Global.ZEN_MODE_NO_INTERRUPTIONS: case Settings.Global.ZEN_MODE_ALARMS: - preference.setEnabled(false); - preference.setSummary(mBackend.getContactsSummary(mBackend.SOURCE_NONE)); + mPreference.setEnabled(false); + mPreference.setValue(ZenModeBackend.ZEN_MODE_FROM_NONE); + mPreference.setSummary(mBackend.getContactsSummary(ZenModeBackend.SOURCE_NONE)); break; default: preference.setEnabled(true); preference.setSummary(mBackend.getContactsSummary( 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; + } } diff --git a/src/com/android/settings/notification/ZenModeMessagesSettings.java b/src/com/android/settings/notification/ZenModeMessagesSettings.java deleted file mode 100644 index 9cbf248b41b..00000000000 --- a/src/com/android/settings/notification/ZenModeMessagesSettings.java +++ /dev/null @@ -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 getCandidates() { - final String[] entries = entries(); - final String[] values = keys(); - final List 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; - } - } -} diff --git a/tests/robotests/src/com/android/settings/notification/ZenModeCallsPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/notification/ZenModeCallsPreferenceControllerTest.java index 21eea0e5149..18f9e717380 100644 --- a/tests/robotests/src/com/android/settings/notification/ZenModeCallsPreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/notification/ZenModeCallsPreferenceControllerTest.java @@ -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_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.verify; import static org.mockito.Mockito.when; @@ -32,7 +29,7 @@ import android.app.NotificationManager; import android.content.ContentResolver; import android.content.Context; import android.provider.Settings; -import android.support.v7.preference.Preference; +import android.support.v7.preference.ListPreference; import android.support.v7.preference.PreferenceScreen; import com.android.settings.R; @@ -45,9 +42,9 @@ 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.RuntimeEnvironment; import org.robolectric.util.ReflectionHelpers; @RunWith(SettingsRobolectricTestRunner.class) @@ -60,7 +57,7 @@ public class ZenModeCallsPreferenceControllerTest { @Mock private NotificationManager mNotificationManager; @Mock - private Preference mockPref; + private ListPreference mockPref; @Mock private NotificationManager.Policy mPolicy; @Mock @@ -68,6 +65,15 @@ public class ZenModeCallsPreferenceControllerTest { private ContentResolver mContentResolver; 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 int MOCK_CALLS_SENDERS = NotificationManager.Policy.PRIORITY_SENDERS_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); mContext = shadowApplication.getApplicationContext(); + mValues = mContext.getResources().getStringArray(R.array.zen_mode_contacts_values); mContentResolver = RuntimeEnvironment.application.getContentResolver(); when(mNotificationManager.getNotificationPolicy()).thenReturn(mPolicy); + + when(mBackend.getPriorityCallSenders()).thenReturn(MOCK_CALLS_SENDERS); when(mBackend.getContactsSummary(ZenModeBackend.SOURCE_NONE)) .thenCallRealMethod(); when(mBackend.getContactsSummary(NotificationManager.Policy.PRIORITY_CATEGORY_CALLS)) @@ -97,10 +106,11 @@ public class ZenModeCallsPreferenceControllerTest { @Test public void updateState_TotalSilence() { Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_NO_INTERRUPTIONS); + when(mBackend.isPriorityCategoryEnabled( - NotificationManager.Policy.PRIORITY_CATEGORY_MESSAGES)) + NotificationManager.Policy.PRIORITY_CATEGORY_CALLS)) .thenReturn(false); - final Preference mockPref = mock(Preference.class); + final ListPreference mockPref = mock(ListPreference.class); mController.updateState(mockPref); verify(mockPref).setEnabled(false); @@ -111,7 +121,7 @@ public class ZenModeCallsPreferenceControllerTest { public void updateState_AlarmsOnly() { Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_ALARMS); - final Preference mockPref = mock(Preference.class); + final ListPreference mockPref = mock(ListPreference.class); mController.updateState(mockPref); verify(mockPref).setEnabled(false); @@ -121,14 +131,53 @@ public class ZenModeCallsPreferenceControllerTest { @Test public void updateState_Priority() { Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_IMPORTANT_INTERRUPTIONS); + when(mBackend.isPriorityCategoryEnabled( NotificationManager.Policy.PRIORITY_CATEGORY_CALLS)) .thenReturn(CALLS_SETTINGS); - when(mBackend.getPriorityCallSenders()).thenReturn(MOCK_CALLS_SENDERS); mController.updateState(mockPref); verify(mockPref).setEnabled(true); 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)]); + } } \ No newline at end of file diff --git a/tests/robotests/src/com/android/settings/notification/ZenModeCallsTest.java b/tests/robotests/src/com/android/settings/notification/ZenModeCallsTest.java deleted file mode 100644 index 8ed0075a470..00000000000 --- a/tests/robotests/src/com/android/settings/notification/ZenModeCallsTest.java +++ /dev/null @@ -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)); - } -} \ No newline at end of file diff --git a/tests/robotests/src/com/android/settings/notification/ZenModeMessagesPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/notification/ZenModeMessagesPreferenceControllerTest.java index 96256238eab..460534ec5f4 100644 --- a/tests/robotests/src/com/android/settings/notification/ZenModeMessagesPreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/notification/ZenModeMessagesPreferenceControllerTest.java @@ -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_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.verify; import static org.mockito.Mockito.when; @@ -32,6 +29,7 @@ import android.app.NotificationManager; import android.content.ContentResolver; import android.content.Context; import android.provider.Settings; +import android.support.v7.preference.ListPreference; import android.support.v7.preference.Preference; import android.support.v7.preference.PreferenceScreen; @@ -45,9 +43,9 @@ 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.RuntimeEnvironment; import org.robolectric.util.ReflectionHelpers; @RunWith(SettingsRobolectricTestRunner.class) @@ -60,7 +58,7 @@ public class ZenModeMessagesPreferenceControllerTest { @Mock private NotificationManager mNotificationManager; @Mock - private Preference mockPref; + private ListPreference mockPref; @Mock private NotificationManager.Policy mPolicy; @Mock @@ -68,6 +66,15 @@ public class ZenModeMessagesPreferenceControllerTest { private ContentResolver mContentResolver; 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 int MOCK_MESSAGES_SENDERS = NotificationManager.Policy.PRIORITY_SENDERS_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); mContext = shadowApplication.getApplicationContext(); + mValues = mContext.getResources().getStringArray(R.array.zen_mode_contacts_values); mContentResolver = RuntimeEnvironment.application.getContentResolver(); when(mNotificationManager.getNotificationPolicy()).thenReturn(mPolicy); @@ -103,7 +111,7 @@ public class ZenModeMessagesPreferenceControllerTest { when(mBackend.isPriorityCategoryEnabled( NotificationManager.Policy.PRIORITY_CATEGORY_MESSAGES)) .thenReturn(false); - final Preference mockPref = mock(Preference.class); + final ListPreference mockPref = mock(ListPreference.class); mController.updateState(mockPref); verify(mockPref).setEnabled(false); @@ -114,7 +122,7 @@ public class ZenModeMessagesPreferenceControllerTest { public void updateState_AlarmsOnly() { Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_ALARMS); - final Preference mockPref = mock(Preference.class); + final ListPreference mockPref = mock(ListPreference.class); mController.updateState(mockPref); verify(mockPref).setEnabled(false); @@ -134,4 +142,43 @@ public class ZenModeMessagesPreferenceControllerTest { verify(mockPref).setEnabled(true); 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)]); + } } \ No newline at end of file diff --git a/tests/robotests/src/com/android/settings/notification/ZenModeMessagesTest.java b/tests/robotests/src/com/android/settings/notification/ZenModeMessagesTest.java deleted file mode 100644 index 181a238fef4..00000000000 --- a/tests/robotests/src/com/android/settings/notification/ZenModeMessagesTest.java +++ /dev/null @@ -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)); - } -} \ No newline at end of file