1 /* 2 * Copyright (C) 2018 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 junit.framework.Assert.assertEquals; 25 26 import static org.mockito.Mockito.mock; 27 import static org.mockito.Mockito.verify; 28 import static org.mockito.Mockito.when; 29 30 import android.app.NotificationManager; 31 import android.content.ContentResolver; 32 import android.content.Context; 33 import android.provider.Settings; 34 import android.support.v14.preference.SwitchPreference; 35 import android.support.v7.preference.PreferenceScreen; 36 37 import com.android.settings.testutils.SettingsRobolectricTestRunner; 38 import com.android.settingslib.core.lifecycle.Lifecycle; 39 40 import org.junit.Before; 41 import org.junit.Test; 42 import org.junit.runner.RunWith; 43 import org.mockito.Mock; 44 import org.mockito.MockitoAnnotations; 45 import org.robolectric.annotation.Config; 46 import org.robolectric.shadows.ShadowApplication; 47 import org.robolectric.RuntimeEnvironment; 48 import org.robolectric.util.ReflectionHelpers; 49 50 @RunWith(SettingsRobolectricTestRunner.class) 51 public class ZenModeSystemPreferenceControllerTest { 52 private ZenModeSystemPreferenceController mController; 53 54 @Mock 55 private ZenModeBackend mBackend; 56 @Mock 57 private NotificationManager mNotificationManager; 58 @Mock 59 private SwitchPreference mockPref; 60 @Mock 61 private NotificationManager.Policy mPolicy; 62 @Mock 63 private PreferenceScreen mPreferenceScreen; 64 65 private Context mContext; 66 private ContentResolver mContentResolver; 67 68 @Before 69 public void setup() { 70 MockitoAnnotations.initMocks(this); 71 ShadowApplication shadowApplication = ShadowApplication.getInstance(); 72 shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNotificationManager); 73 74 mContext = shadowApplication.getApplicationContext(); 75 mContentResolver = RuntimeEnvironment.application.getContentResolver(); 76 when(mNotificationManager.getNotificationPolicy()).thenReturn(mPolicy); 77 mController = new ZenModeSystemPreferenceController(mContext, mock(Lifecycle.class)); 78 ReflectionHelpers.setField(mController, "mBackend", mBackend); 79 80 when(mPreferenceScreen.findPreference(mController.getPreferenceKey())).thenReturn( 81 mockPref); 82 mController.displayPreference(mPreferenceScreen); 83 } 84 85 @Test 86 public void updateState_TotalSilence() { 87 Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_NO_INTERRUPTIONS); 88 89 final SwitchPreference mockPref = mock(SwitchPreference.class); 90 mController.updateState(mockPref); 91 92 verify(mockPref).setEnabled(false); 93 verify(mockPref).setChecked(false); 94 } 95 96 @Test 97 public void updateState_AlarmsOnly() { 98 Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_ALARMS); 99 100 final SwitchPreference mockPref = mock(SwitchPreference.class); 101 mController.updateState(mockPref); 102 103 verify(mockPref).setEnabled(false); 104 verify(mockPref).setChecked(false); 105 } 106 107 @Test 108 public void updateState_Priority() { 109 Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_IMPORTANT_INTERRUPTIONS); 110 111 when(mBackend.isPriorityCategoryEnabled( 112 NotificationManager.Policy.PRIORITY_CATEGORY_SYSTEM)).thenReturn(true); 113 114 mController.updateState(mockPref); 115 116 verify(mockPref).setEnabled(true); 117 verify(mockPref).setChecked(true); 118 } 119 120 @Test 121 public void onPreferenceChanged_EnableSystem() { 122 mController.onPreferenceChange(mockPref, true); 123 124 verify(mBackend).saveSoundPolicy(NotificationManager.Policy.PRIORITY_CATEGORY_SYSTEM, 125 true); 126 } 127 128 @Test 129 public void onPreferenceChanged_DisableSystem() { 130 mController.onPreferenceChange(mockPref, false); 131 132 verify(mBackend).saveSoundPolicy(NotificationManager.Policy.PRIORITY_CATEGORY_SYSTEM, 133 false); 134 } 135 }