1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.settings.notification; 18 19 import static android.provider.Settings.Global.ZEN_MODE; 20 import static android.provider.Settings.Global.ZEN_MODE_ALARMS; 21 import static android.provider.Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS; 22 import static android.provider.Settings.Global.ZEN_MODE_NO_INTERRUPTIONS; 23 24 import static org.mockito.Mockito.mock; 25 import static org.mockito.Mockito.verify; 26 import static org.mockito.Mockito.when; 27 28 import android.app.NotificationManager; 29 import android.content.ContentResolver; 30 import android.content.Context; 31 import android.provider.Settings; 32 import android.support.v7.preference.ListPreference; 33 import android.support.v7.preference.PreferenceScreen; 34 35 import com.android.settings.R; 36 import com.android.settings.testutils.SettingsRobolectricTestRunner; 37 import com.android.settingslib.core.lifecycle.Lifecycle; 38 39 import org.junit.Before; 40 import org.junit.Test; 41 import org.junit.runner.RunWith; 42 import org.mockito.Mock; 43 import org.mockito.MockitoAnnotations; 44 import org.robolectric.RuntimeEnvironment; 45 import org.robolectric.shadows.ShadowApplication; 46 import org.robolectric.util.ReflectionHelpers; 47 48 @RunWith(SettingsRobolectricTestRunner.class) 49 public class ZenModeCallsPreferenceControllerTest { 50 51 private ZenModeCallsPreferenceController mController; 52 53 @Mock 54 private ZenModeBackend mBackend; 55 @Mock 56 private NotificationManager mNotificationManager; 57 @Mock 58 private ListPreference mockPref; 59 @Mock 60 private NotificationManager.Policy mPolicy; 61 @Mock 62 private PreferenceScreen mPreferenceScreen; 63 private ContentResolver mContentResolver; 64 private Context mContext; 65 66 /** 67 * Array Values Key 68 * 0: anyone 69 * 1: contacts 70 * 2: starred 71 * 3: none 72 */ 73 private String[] mValues; 74 75 @Before setup()76 public void setup() { 77 MockitoAnnotations.initMocks(this); 78 ShadowApplication shadowApplication = ShadowApplication.getInstance(); 79 shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNotificationManager); 80 81 mContext = shadowApplication.getApplicationContext(); 82 mValues = mContext.getResources().getStringArray(R.array.zen_mode_contacts_values); 83 mContentResolver = RuntimeEnvironment.application.getContentResolver(); 84 when(mNotificationManager.getNotificationPolicy()).thenReturn(mPolicy); 85 86 when(mBackend.getPriorityCallSenders()) 87 .thenReturn(NotificationManager.Policy.PRIORITY_SENDERS_STARRED); 88 when(mBackend.getContactsSummary(ZenModeBackend.SOURCE_NONE)) 89 .thenCallRealMethod(); 90 when(mBackend.getContactsSummary(NotificationManager.Policy.PRIORITY_CATEGORY_CALLS)) 91 .thenCallRealMethod(); 92 93 mController = new ZenModeCallsPreferenceController(mContext, mock(Lifecycle.class)); 94 ReflectionHelpers.setField(mController, "mBackend", mBackend); 95 96 when(mPreferenceScreen.findPreference(mController.getPreferenceKey())).thenReturn( 97 mockPref); 98 mController.displayPreference(mPreferenceScreen); 99 } 100 101 @Test updateState_TotalSilence()102 public void updateState_TotalSilence() { 103 Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_NO_INTERRUPTIONS); 104 105 when(mBackend.isPriorityCategoryEnabled( 106 NotificationManager.Policy.PRIORITY_CATEGORY_CALLS)) 107 .thenReturn(false); 108 final ListPreference mockPref = mock(ListPreference.class); 109 mController.updateState(mockPref); 110 111 verify(mockPref).setEnabled(false); 112 verify(mockPref).setSummary(R.string.zen_mode_from_none); 113 } 114 115 @Test updateState_AlarmsOnly()116 public void updateState_AlarmsOnly() { 117 Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_ALARMS); 118 119 final ListPreference mockPref = mock(ListPreference.class); 120 mController.updateState(mockPref); 121 122 verify(mockPref).setEnabled(false); 123 verify(mockPref).setSummary(R.string.zen_mode_from_none); 124 } 125 126 @Test updateState_Priority()127 public void updateState_Priority() { 128 Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_IMPORTANT_INTERRUPTIONS); 129 130 when(mBackend.isPriorityCategoryEnabled( 131 NotificationManager.Policy.PRIORITY_CATEGORY_CALLS)) 132 .thenReturn(true); 133 134 mController.updateState(mockPref); 135 136 verify(mockPref).setEnabled(true); 137 verify(mockPref).setSummary(R.string.zen_mode_from_starred); 138 } 139 140 @Test onPreferenceChange_setSelectedContacts_any()141 public void onPreferenceChange_setSelectedContacts_any() { 142 Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_IMPORTANT_INTERRUPTIONS); 143 when(mBackend.getPriorityCallSenders()).thenReturn( 144 NotificationManager.Policy.PRIORITY_SENDERS_ANY); 145 mController.updateState(mockPref); 146 verify(mockPref).setValue(mValues[mController.getIndexOfSendersValue( 147 ZenModeBackend.ZEN_MODE_FROM_ANYONE)]); 148 } 149 150 @Test onPreferenceChange_setSelectedContacts_none()151 public void onPreferenceChange_setSelectedContacts_none() { 152 Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_IMPORTANT_INTERRUPTIONS); 153 when(mBackend.getPriorityCallSenders()).thenReturn(ZenModeBackend.SOURCE_NONE); 154 mController.updateState(mockPref); 155 verify(mockPref).setValue(mValues[mController.getIndexOfSendersValue( 156 ZenModeBackend.ZEN_MODE_FROM_NONE)]); 157 } 158 159 @Test onPreferenceChange_setSelectedContacts_starred()160 public void onPreferenceChange_setSelectedContacts_starred() { 161 Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_IMPORTANT_INTERRUPTIONS); 162 when(mBackend.getPriorityCallSenders()).thenReturn( 163 NotificationManager.Policy.PRIORITY_SENDERS_STARRED); 164 mController.updateState(mockPref); 165 verify(mockPref).setValue(mValues[mController.getIndexOfSendersValue( 166 ZenModeBackend.ZEN_MODE_FROM_STARRED)]); 167 } 168 169 @Test onPreferenceChange_setSelectedContacts_contacts()170 public void onPreferenceChange_setSelectedContacts_contacts() { 171 Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_IMPORTANT_INTERRUPTIONS); 172 when(mBackend.getPriorityCallSenders()).thenReturn( 173 NotificationManager.Policy.PRIORITY_SENDERS_CONTACTS); 174 mController.updateState(mockPref); 175 verify(mockPref).setValue(mValues[mController.getIndexOfSendersValue( 176 ZenModeBackend.ZEN_MODE_FROM_CONTACTS)]); 177 } 178 }