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 33 import androidx.preference.PreferenceScreen; 34 import androidx.preference.SwitchPreference; 35 36 import com.android.settingslib.core.lifecycle.Lifecycle; 37 38 import org.junit.Before; 39 import org.junit.Test; 40 import org.junit.runner.RunWith; 41 import org.mockito.Mock; 42 import org.mockito.MockitoAnnotations; 43 import org.robolectric.RobolectricTestRunner; 44 import org.robolectric.RuntimeEnvironment; 45 import org.robolectric.shadows.ShadowApplication; 46 import org.robolectric.util.ReflectionHelpers; 47 48 @RunWith(RobolectricTestRunner.class) 49 public class ZenModeRemindersPreferenceControllerTest { 50 51 private static final boolean REMINDERS_SETTINGS = true; 52 53 private ZenModeRemindersPreferenceController mController; 54 55 @Mock 56 private ZenModeBackend mBackend; 57 @Mock 58 private NotificationManager mNotificationManager; 59 @Mock 60 private SwitchPreference mockPref; 61 @Mock 62 private NotificationManager.Policy mPolicy; 63 @Mock 64 private PreferenceScreen mPreferenceScreen; 65 66 private ContentResolver mContentResolver; 67 private Context mContext; 68 69 @Before setup()70 public void setup() { 71 MockitoAnnotations.initMocks(this); 72 ShadowApplication shadowApplication = ShadowApplication.getInstance(); 73 shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNotificationManager); 74 75 mContext = RuntimeEnvironment.application; 76 mContentResolver = RuntimeEnvironment.application.getContentResolver(); 77 when(mNotificationManager.getNotificationPolicy()).thenReturn(mPolicy); 78 79 mController = new ZenModeRemindersPreferenceController(mContext, mock(Lifecycle.class)); 80 ReflectionHelpers.setField(mController, "mBackend", mBackend); 81 82 when(mPreferenceScreen.findPreference(mController.getPreferenceKey())) 83 .thenReturn(mockPref); 84 mController.displayPreference(mPreferenceScreen); 85 } 86 87 @Test updateState_TotalSilence()88 public void updateState_TotalSilence() { 89 Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_NO_INTERRUPTIONS); 90 91 final SwitchPreference mockPref = mock(SwitchPreference.class); 92 mController.updateState(mockPref); 93 94 verify(mockPref).setEnabled(false); 95 verify(mockPref).setChecked(false); 96 } 97 98 @Test updateState_AlarmsOnly()99 public void updateState_AlarmsOnly() { 100 Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_ALARMS); 101 102 final SwitchPreference mockPref = mock(SwitchPreference.class); 103 mController.updateState(mockPref); 104 105 verify(mockPref).setEnabled(false); 106 verify(mockPref).setChecked(false); 107 } 108 109 @Test updateState_Priority()110 public void updateState_Priority() { 111 Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_IMPORTANT_INTERRUPTIONS); 112 113 when(mBackend.isPriorityCategoryEnabled( 114 NotificationManager.Policy.PRIORITY_CATEGORY_REMINDERS)). 115 thenReturn(REMINDERS_SETTINGS); 116 117 mController.updateState(mockPref); 118 119 verify(mockPref).setEnabled(true); 120 verify(mockPref).setChecked(REMINDERS_SETTINGS); 121 } 122 123 @Test onPreferenceChanged_EnableReminders()124 public void onPreferenceChanged_EnableReminders() { 125 boolean allow = true; 126 mController.onPreferenceChange(mockPref, allow); 127 128 verify(mBackend) 129 .saveSoundPolicy(NotificationManager.Policy.PRIORITY_CATEGORY_REMINDERS, allow); 130 } 131 132 @Test onPreferenceChanged_DisableReminders()133 public void onPreferenceChanged_DisableReminders() { 134 boolean allow = false; 135 mController.onPreferenceChange(mockPref, allow); 136 137 verify(mBackend) 138 .saveSoundPolicy(NotificationManager.Policy.PRIORITY_CATEGORY_REMINDERS, allow); 139 } 140 }