Merge "Update DND people Settings pages" into rvc-dev am: a33c8e8048
am: c21f849fe7
am: f5eac3f0cf
am: 95d85c0e34
Change-Id: I7335593a093a8e25c0296434e710f7ccd667d140
This commit is contained in:
@@ -19,173 +19,131 @@ 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 com.android.settings.notification.zen.ZenModePriorityConversationsPreferenceController.KEY_ALL;
|
||||
import static com.android.settings.notification.zen.ZenModePriorityConversationsPreferenceController.KEY_IMPORTANT;
|
||||
import static com.android.settings.notification.zen.ZenModePriorityConversationsPreferenceController.KEY_NONE;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.reset;
|
||||
import static org.mockito.Mockito.times;
|
||||
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.Preference;
|
||||
import androidx.preference.PreferenceCategory;
|
||||
import androidx.preference.PreferenceScreen;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.notification.NotificationBackend;
|
||||
import com.android.settingslib.core.lifecycle.Lifecycle;
|
||||
import com.android.settingslib.widget.RadioButtonPreference;
|
||||
|
||||
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.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
import org.robolectric.shadows.ShadowApplication;
|
||||
import org.robolectric.util.ReflectionHelpers;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public class ZenModePriorityConversationsPreferenceControllerTest {
|
||||
|
||||
private ZenModePriorityConversationsPreferenceController mController;
|
||||
|
||||
@Mock
|
||||
private ZenModeBackend mBackend;
|
||||
private ZenModeBackend mZenBackend;
|
||||
@Mock
|
||||
private NotificationManager mNotificationManager;
|
||||
@Mock
|
||||
private ListPreference mockPref;
|
||||
private PreferenceCategory mMockPrefCategory;
|
||||
@Mock
|
||||
private NotificationManager.Policy mPolicy;
|
||||
@Mock
|
||||
private PreferenceScreen mPreferenceScreen;
|
||||
@Mock
|
||||
private NotificationBackend mNotifBackend;
|
||||
|
||||
private List<RadioButtonPreference> mRadioButtonPreferences;
|
||||
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);
|
||||
mContext, "test_key", mock(Lifecycle.class), mNotifBackend);
|
||||
ReflectionHelpers.setField(mController, "mBackend", mZenBackend);
|
||||
|
||||
when(mPreferenceScreen.findPreference(mController.getPreferenceKey())).thenReturn(mockPref);
|
||||
when(mMockPrefCategory.getContext()).thenReturn(mContext);
|
||||
when(mPreferenceScreen.findPreference(mController.getPreferenceKey()))
|
||||
.thenReturn(mMockPrefCategory);
|
||||
captureRadioButtons();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void displayPreference_radioButtonsCreatedOnlyOnce() {
|
||||
when(mMockPrefCategory.findPreference(any())).thenReturn(mock(Preference.class));
|
||||
|
||||
// radio buttons were already created, so don't re-create them
|
||||
mController.displayPreference(mPreferenceScreen);
|
||||
verify(mMockPrefCategory, never()).addPreference(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateState_TotalSilence() {
|
||||
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_NO_INTERRUPTIONS);
|
||||
public void clickAllConversations() {
|
||||
RadioButtonPreference allConversationsRb = getButton(KEY_ALL);
|
||||
allConversationsRb.onClick();
|
||||
|
||||
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);
|
||||
verify(mZenBackend).saveConversationSenders(CONVERSATION_SENDERS_ANYONE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateState_AlarmsOnly() {
|
||||
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_ALARMS);
|
||||
public void clickImportantConversations() {
|
||||
RadioButtonPreference importantConversationsRb = getButton(KEY_IMPORTANT);
|
||||
importantConversationsRb.onClick();
|
||||
|
||||
final ListPreference mockPref = mock(ListPreference.class);
|
||||
mController.updateState(mockPref);
|
||||
|
||||
verify(mockPref).setEnabled(false);
|
||||
verify(mockPref).setSummary(R.string.zen_mode_from_no_conversations);
|
||||
verify(mZenBackend).saveConversationSenders(CONVERSATION_SENDERS_IMPORTANT);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateState_Priority_important() {
|
||||
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_IMPORTANT_INTERRUPTIONS);
|
||||
when(mBackend.isPriorityCategoryEnabled(PRIORITY_CATEGORY_CONVERSATIONS)).thenReturn(true);
|
||||
public void clickNoConversations() {
|
||||
RadioButtonPreference noConversationsRb = getButton(KEY_NONE);
|
||||
noConversationsRb.onClick();
|
||||
|
||||
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));
|
||||
verify(mZenBackend)
|
||||
.saveConversationSenders(CONVERSATION_SENDERS_NONE);
|
||||
}
|
||||
|
||||
@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);
|
||||
private void captureRadioButtons() {
|
||||
ArgumentCaptor<RadioButtonPreference> rbCaptor =
|
||||
ArgumentCaptor.forClass(RadioButtonPreference.class);
|
||||
mController.displayPreference(mPreferenceScreen);
|
||||
|
||||
// verifies 3 buttons were added
|
||||
verify(mMockPrefCategory, times(3)).addPreference(rbCaptor.capture());
|
||||
mRadioButtonPreferences = rbCaptor.getAllValues();
|
||||
assertThat(mRadioButtonPreferences.size()).isEqualTo(3);
|
||||
|
||||
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));
|
||||
reset(mMockPrefCategory);
|
||||
}
|
||||
|
||||
@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();
|
||||
private RadioButtonPreference getButton(String key) {
|
||||
for (RadioButtonPreference pref : mRadioButtonPreferences) {
|
||||
if (key.equals(pref.getKey())) {
|
||||
return pref;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
@@ -1,178 +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.zen;
|
||||
|
||||
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.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 ZenModePriorityMessagesPreferenceControllerTest {
|
||||
|
||||
private ZenModePriorityMessagesPreferenceController 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;
|
||||
|
||||
/**
|
||||
* Array Values Key
|
||||
* 0: anyone
|
||||
* 1: contacts
|
||||
* 2: starred
|
||||
* 3: none
|
||||
*/
|
||||
private String[] mValues;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
ShadowApplication shadowApplication = ShadowApplication.getInstance();
|
||||
shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNotificationManager);
|
||||
|
||||
mContext = RuntimeEnvironment.application;
|
||||
mValues = mContext.getResources().getStringArray(R.array.zen_mode_contacts_values);
|
||||
mContentResolver = RuntimeEnvironment.application.getContentResolver();
|
||||
when(mNotificationManager.getNotificationPolicy()).thenReturn(mPolicy);
|
||||
|
||||
when(mBackend.getPriorityMessageSenders())
|
||||
.thenReturn(NotificationManager.Policy.PRIORITY_SENDERS_STARRED);
|
||||
when(mBackend.getAlarmsTotalSilencePeopleSummary(
|
||||
NotificationManager.Policy.PRIORITY_CATEGORY_MESSAGES)).thenCallRealMethod();
|
||||
when(mBackend.getContactsSummary(NotificationManager.Policy.PRIORITY_CATEGORY_MESSAGES))
|
||||
.thenCallRealMethod();
|
||||
|
||||
mController = new ZenModePriorityMessagesPreferenceController(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(
|
||||
NotificationManager.Policy.PRIORITY_CATEGORY_MESSAGES))
|
||||
.thenReturn(false);
|
||||
final ListPreference mockPref = mock(ListPreference.class);
|
||||
mController.updateState(mockPref);
|
||||
|
||||
verify(mockPref).setEnabled(false);
|
||||
verify(mockPref).setSummary(R.string.zen_mode_from_none_messages);
|
||||
}
|
||||
|
||||
@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_none_messages);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateState_Priority() {
|
||||
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_IMPORTANT_INTERRUPTIONS);
|
||||
|
||||
when(mBackend.isPriorityCategoryEnabled(
|
||||
NotificationManager.Policy.PRIORITY_CATEGORY_MESSAGES))
|
||||
.thenReturn(true);
|
||||
|
||||
mController.updateState(mockPref);
|
||||
|
||||
verify(mockPref).setEnabled(true);
|
||||
verify(mockPref).setSummary(R.string.zen_mode_from_starred);
|
||||
}
|
||||
|
||||
@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)]);
|
||||
}
|
||||
}
|
@@ -0,0 +1,193 @@
|
||||
/*
|
||||
* 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.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 com.android.settings.notification.zen.ZenModePrioritySendersPreferenceController.KEY_ANY;
|
||||
import static com.android.settings.notification.zen.ZenModePrioritySendersPreferenceController.KEY_CONTACTS;
|
||||
import static com.android.settings.notification.zen.ZenModePrioritySendersPreferenceController.KEY_NONE;
|
||||
import static com.android.settings.notification.zen.ZenModePrioritySendersPreferenceController.KEY_STARRED;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.reset;
|
||||
import static org.mockito.Mockito.times;
|
||||
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 androidx.preference.Preference;
|
||||
import androidx.preference.PreferenceCategory;
|
||||
import androidx.preference.PreferenceScreen;
|
||||
|
||||
import com.android.settings.notification.NotificationBackend;
|
||||
import com.android.settingslib.core.lifecycle.Lifecycle;
|
||||
import com.android.settingslib.widget.RadioButtonPreference;
|
||||
|
||||
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.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
import org.robolectric.util.ReflectionHelpers;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public class ZenModePrioritySendersPreferenceControllerTest {
|
||||
|
||||
private ZenModePrioritySendersPreferenceController mMessagesController;
|
||||
|
||||
@Mock
|
||||
private ZenModeBackend mZenBackend;
|
||||
@Mock
|
||||
private PreferenceCategory mMockPrefCategory;
|
||||
@Mock
|
||||
private NotificationManager.Policy mPolicy;
|
||||
@Mock
|
||||
private PreferenceScreen mPreferenceScreen;
|
||||
@Mock
|
||||
private NotificationBackend mNotifBackend;
|
||||
|
||||
private List<RadioButtonPreference> mRadioButtonPreferences;
|
||||
private ContentResolver mContentResolver;
|
||||
private Context mContext;
|
||||
@Before
|
||||
public void setup() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
mContext = RuntimeEnvironment.application;
|
||||
mMessagesController = new ZenModePrioritySendersPreferenceController(
|
||||
mContext, "test_key_messages", mock(Lifecycle.class), true);
|
||||
ReflectionHelpers.setField(mMessagesController, "mBackend", mZenBackend);
|
||||
|
||||
when(mMockPrefCategory.getContext()).thenReturn(mContext);
|
||||
when(mPreferenceScreen.findPreference(mMessagesController.getPreferenceKey()))
|
||||
.thenReturn(mMockPrefCategory);
|
||||
captureRadioButtons();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void displayPreference_radioButtonsCreatedOnlyOnce() {
|
||||
when(mMockPrefCategory.findPreference(any())).thenReturn(mock(Preference.class));
|
||||
|
||||
// radio buttons were already created, so don't re-create them
|
||||
mMessagesController.displayPreference(mPreferenceScreen);
|
||||
verify(mMockPrefCategory, never()).addPreference(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void clickAnySenders() {
|
||||
// GIVEN current priority message senders are STARRED
|
||||
when(mZenBackend.getPriorityMessageSenders()).thenReturn(PRIORITY_SENDERS_STARRED);
|
||||
|
||||
// WHEN user clicks the any senders option
|
||||
RadioButtonPreference allSendersRb = getButton(KEY_ANY);
|
||||
allSendersRb.onClick();
|
||||
|
||||
// THEN any senders gets saved as priority senders for messages
|
||||
verify(mZenBackend).saveSenders(PRIORITY_CATEGORY_MESSAGES, PRIORITY_SENDERS_ANY);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void clickStarredSenders() {
|
||||
// GIVEN current priority message senders are ANY
|
||||
when(mZenBackend.getPriorityMessageSenders()).thenReturn(PRIORITY_SENDERS_ANY);
|
||||
|
||||
// WHEN user clicks the starred contacts option
|
||||
RadioButtonPreference starredRb = getButton(KEY_STARRED);
|
||||
starredRb.onClick();
|
||||
|
||||
// THEN starred contacts gets saved as priority senders for messages
|
||||
verify(mZenBackend).saveSenders(PRIORITY_CATEGORY_MESSAGES, PRIORITY_SENDERS_STARRED);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void clickContactsSenders() {
|
||||
// GIVEN current priority message senders are ANY
|
||||
when(mZenBackend.getPriorityMessageSenders()).thenReturn(PRIORITY_SENDERS_ANY);
|
||||
|
||||
// WHEN user clicks the contacts only option
|
||||
RadioButtonPreference contactsRb = getButton(KEY_CONTACTS);
|
||||
contactsRb.onClick();
|
||||
|
||||
// THEN contacts gets saved as priority senders for messages
|
||||
verify(mZenBackend).saveSenders(PRIORITY_CATEGORY_MESSAGES, PRIORITY_SENDERS_CONTACTS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void clickNoSenders() {
|
||||
// GIVEN current priority message senders are ANY
|
||||
when(mZenBackend.getPriorityMessageSenders()).thenReturn(PRIORITY_SENDERS_ANY);
|
||||
|
||||
// WHEN user clicks the no senders option
|
||||
RadioButtonPreference noSenders = getButton(KEY_NONE);
|
||||
noSenders.onClick();
|
||||
|
||||
// THEN no senders gets saved as priority senders for messages
|
||||
verify(mZenBackend).saveSenders(PRIORITY_CATEGORY_MESSAGES, ZenModeBackend.SOURCE_NONE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void clickSameOptionMultipleTimes() {
|
||||
// GIVEN current priority message senders are ANY
|
||||
when(mZenBackend.getPriorityMessageSenders()).thenReturn(PRIORITY_SENDERS_ANY);
|
||||
|
||||
// WHEN user clicks the any senders option multiple times again
|
||||
RadioButtonPreference anySenders = getButton(KEY_ANY);
|
||||
anySenders.onClick();
|
||||
anySenders.onClick();
|
||||
anySenders.onClick();
|
||||
|
||||
// THEN no senders are saved because this setting is already in effect
|
||||
verify(mZenBackend, never()).saveSenders(PRIORITY_CATEGORY_MESSAGES, PRIORITY_SENDERS_ANY);
|
||||
}
|
||||
|
||||
private void captureRadioButtons() {
|
||||
ArgumentCaptor<RadioButtonPreference> rbCaptor =
|
||||
ArgumentCaptor.forClass(RadioButtonPreference.class);
|
||||
mMessagesController.displayPreference(mPreferenceScreen);
|
||||
|
||||
// verifies 4 buttons were added
|
||||
verify(mMockPrefCategory, times(4)).addPreference(rbCaptor.capture());
|
||||
mRadioButtonPreferences = rbCaptor.getAllValues();
|
||||
assertThat(mRadioButtonPreferences.size()).isEqualTo(4);
|
||||
|
||||
reset(mMockPrefCategory);
|
||||
}
|
||||
|
||||
private RadioButtonPreference getButton(String key) {
|
||||
for (RadioButtonPreference pref : mRadioButtonPreferences) {
|
||||
if (key.equals(pref.getKey())) {
|
||||
return pref;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
@@ -1,164 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2018 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.PRIORITY_CATEGORY_CALLS;
|
||||
import static android.app.NotificationManager.Policy.PRIORITY_CATEGORY_MESSAGES;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import android.app.NotificationManager;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
|
||||
import androidx.preference.Preference;
|
||||
import androidx.preference.PreferenceScreen;
|
||||
|
||||
import com.android.settings.notification.zen.ZenModeBackend;
|
||||
import com.android.settings.notification.zen.ZenModeStarredContactsPreferenceController;
|
||||
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 ZenModeStarredContactsPreferenceControllerTest {
|
||||
|
||||
private ZenModeStarredContactsPreferenceController mCallsController;
|
||||
private ZenModeStarredContactsPreferenceController mMessagesController;
|
||||
|
||||
@Mock
|
||||
private ZenModeBackend mBackend;
|
||||
@Mock
|
||||
private NotificationManager mNotificationManager;
|
||||
@Mock
|
||||
private Preference mockPref;
|
||||
@Mock
|
||||
private NotificationManager.Policy mPolicy;
|
||||
@Mock
|
||||
private PreferenceScreen mPreferenceScreen;
|
||||
@Mock
|
||||
private Intent testIntent;
|
||||
@Mock
|
||||
private ComponentName mComponentName;
|
||||
private Context mContext;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
ShadowApplication shadowApplication = ShadowApplication.getInstance();
|
||||
shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNotificationManager);
|
||||
|
||||
mContext = RuntimeEnvironment.application;
|
||||
when(mNotificationManager.getNotificationPolicy()).thenReturn(mPolicy);
|
||||
when(testIntent.resolveActivity(any())).thenReturn(mComponentName);
|
||||
|
||||
mCallsController = new ZenModeStarredContactsPreferenceController(
|
||||
mContext, mock(Lifecycle.class), PRIORITY_CATEGORY_CALLS,
|
||||
"zen_mode_starred_contacts_callers");
|
||||
ReflectionHelpers.setField(mCallsController, "mBackend", mBackend);
|
||||
ReflectionHelpers.setField(mCallsController, "mStarredContactsIntent", testIntent);
|
||||
when(mPreferenceScreen.findPreference(mCallsController.getPreferenceKey()))
|
||||
.thenReturn(mockPref);
|
||||
mCallsController.displayPreference(mPreferenceScreen);
|
||||
|
||||
mMessagesController = new ZenModeStarredContactsPreferenceController(
|
||||
mContext, mock(Lifecycle.class), PRIORITY_CATEGORY_MESSAGES,
|
||||
"zen_mode_starred_contacts_messages");
|
||||
ReflectionHelpers.setField(mMessagesController, "mBackend", mBackend);
|
||||
ReflectionHelpers.setField(mMessagesController, "mStarredContactsIntent", testIntent);
|
||||
when(mPreferenceScreen.findPreference(mMessagesController.getPreferenceKey()))
|
||||
.thenReturn(mockPref);
|
||||
mMessagesController.displayPreference(mPreferenceScreen);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isAvailable_noCallers() {
|
||||
when(mBackend.isPriorityCategoryEnabled(NotificationManager.Policy.PRIORITY_CATEGORY_CALLS))
|
||||
.thenReturn(false);
|
||||
assertThat(mCallsController.isAvailable()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isAvailable_anyCallers() {
|
||||
when(mBackend.isPriorityCategoryEnabled(NotificationManager.Policy.PRIORITY_CATEGORY_CALLS))
|
||||
.thenReturn(true);
|
||||
when(mBackend.getPriorityCallSenders())
|
||||
.thenReturn(NotificationManager.Policy.PRIORITY_SENDERS_ANY);
|
||||
|
||||
assertThat(mCallsController.isAvailable()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isAvailable_starredCallers() {
|
||||
when(mBackend.isPriorityCategoryEnabled(NotificationManager.Policy.PRIORITY_CATEGORY_CALLS))
|
||||
.thenReturn(true);
|
||||
when(mBackend.getPriorityCallSenders())
|
||||
.thenReturn(NotificationManager.Policy.PRIORITY_SENDERS_STARRED);
|
||||
|
||||
assertThat(mCallsController.isAvailable()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isAvailable_noMessages() {
|
||||
when(mBackend.isPriorityCategoryEnabled(
|
||||
NotificationManager.Policy.PRIORITY_CATEGORY_MESSAGES)).thenReturn(false);
|
||||
assertThat(mMessagesController.isAvailable()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isAvailable_anyMessages() {
|
||||
when(mBackend.isPriorityCategoryEnabled(
|
||||
NotificationManager.Policy.PRIORITY_CATEGORY_MESSAGES)).thenReturn(true);
|
||||
when(mBackend.getPriorityMessageSenders())
|
||||
.thenReturn(NotificationManager.Policy.PRIORITY_SENDERS_ANY);
|
||||
|
||||
assertThat(mMessagesController.isAvailable()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isAvailable_starredMessageContacts() {
|
||||
when(mBackend.isPriorityCategoryEnabled(
|
||||
NotificationManager.Policy.PRIORITY_CATEGORY_MESSAGES)).thenReturn(true);
|
||||
when(mBackend.getPriorityMessageSenders())
|
||||
.thenReturn(NotificationManager.Policy.PRIORITY_SENDERS_STARRED);
|
||||
|
||||
assertThat(mMessagesController.isAvailable()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void nullPreference_displayPreference() {
|
||||
when(mPreferenceScreen.findPreference(mMessagesController.getPreferenceKey()))
|
||||
.thenReturn(null);
|
||||
|
||||
// should not throw a null pointer
|
||||
mMessagesController.displayPreference(mPreferenceScreen);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user