• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 import static org.mockito.Mockito.mock;
24 import static org.mockito.Mockito.verify;
25 import static org.mockito.Mockito.when;
26 
27 import android.app.NotificationManager;
28 import android.content.ContentResolver;
29 import android.content.Context;
30 import android.provider.Settings;
31 import android.support.v14.preference.SwitchPreference;
32 import android.support.v7.preference.PreferenceScreen;
33 
34 import com.android.settings.testutils.SettingsRobolectricTestRunner;
35 import com.android.settingslib.core.lifecycle.Lifecycle;
36 
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 import org.mockito.Mock;
41 import org.mockito.MockitoAnnotations;
42 import org.robolectric.RuntimeEnvironment;
43 import org.robolectric.shadows.ShadowApplication;
44 import org.robolectric.util.ReflectionHelpers;
45 
46 @RunWith(SettingsRobolectricTestRunner.class)
47 public class ZenModeRepeatCallersPreferenceControllerTest {
48 
49     private static final boolean REPEAT_CALLERS_SETTINGS = true;
50 
51     private ZenModeRepeatCallersPreferenceController mController;
52 
53     @Mock
54     private ZenModeBackend mBackend;
55     @Mock
56     private NotificationManager mNotificationManager;
57     @Mock
58     private SwitchPreference mockPref;
59     @Mock
60     private NotificationManager.Policy mPolicy;
61     @Mock
62     private PreferenceScreen mPreferenceScreen;
63     private ContentResolver mContentResolver;
64     private Context mContext;
65 
66     @Before
setup()67     public void setup() {
68         MockitoAnnotations.initMocks(this);
69         ShadowApplication shadowApplication = ShadowApplication.getInstance();
70         shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNotificationManager);
71 
72         mContext = shadowApplication.getApplicationContext();
73         mContentResolver = RuntimeEnvironment.application.getContentResolver();
74         when(mNotificationManager.getNotificationPolicy()).thenReturn(mPolicy);
75 
76         mController = new ZenModeRepeatCallersPreferenceController(mContext, mock(Lifecycle.class),
77                 15);
78         ReflectionHelpers.setField(mController, "mBackend", mBackend);
79 
80         when(mPreferenceScreen.findPreference(mController.getPreferenceKey())).thenReturn(
81                 mockPref);
82         mController.displayPreference(mPreferenceScreen);
83     }
84 
85     @Test
updateState_TotalSilence()86     public void updateState_TotalSilence() {
87         Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_NO_INTERRUPTIONS);
88         final SwitchPreference mockPref = mock(SwitchPreference.class);
89         mController.updateState(mockPref);
90 
91         verify(mockPref).setEnabled(false);
92         verify(mockPref).setChecked(false);
93     }
94 
95     @Test
updateState_AlarmsOnly()96     public void updateState_AlarmsOnly() {
97         Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_ALARMS);
98 
99         final SwitchPreference mockPref = mock(SwitchPreference.class);
100         mController.updateState(mockPref);
101 
102         verify(mockPref).setEnabled(false);
103         verify(mockPref).setChecked(false);
104     }
105 
106     @Test
updateState_Priority()107     public void updateState_Priority() {
108         Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_IMPORTANT_INTERRUPTIONS);
109 
110         when(mBackend.isPriorityCategoryEnabled(
111                 NotificationManager.Policy.PRIORITY_CATEGORY_REPEAT_CALLERS)).
112                 thenReturn(REPEAT_CALLERS_SETTINGS);
113 
114         mController.updateState(mockPref);
115 
116         verify(mockPref).setEnabled(true);
117         verify(mockPref).setChecked(REPEAT_CALLERS_SETTINGS);
118     }
119 
120     @Test
updateState_Priority_anyCallers()121     public void updateState_Priority_anyCallers() {
122         Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_IMPORTANT_INTERRUPTIONS);
123         when(mBackend.isPriorityCategoryEnabled(NotificationManager.Policy.PRIORITY_CATEGORY_CALLS))
124                 .thenReturn(true);
125         when(mBackend.getPriorityCallSenders()).thenReturn(
126                 NotificationManager.Policy.PRIORITY_SENDERS_ANY);
127         when(mBackend.isPriorityCategoryEnabled(
128                 NotificationManager.Policy.PRIORITY_CATEGORY_REPEAT_CALLERS))
129                 .thenReturn(false);
130 
131         mController.updateState(mockPref);
132 
133         verify(mockPref).setEnabled(false);
134         verify(mockPref).setChecked(true);
135     }
136 
137     @Test
onPreferenceChanged_EnableRepeatCallers()138     public void onPreferenceChanged_EnableRepeatCallers() {
139         boolean allow = true;
140         mController.onPreferenceChange(mockPref, allow);
141 
142         verify(mBackend)
143             .saveSoundPolicy(NotificationManager.Policy.PRIORITY_CATEGORY_REPEAT_CALLERS, allow);
144     }
145 
146     @Test
onPreferenceChanged_DisableRepeatCallers()147     public void onPreferenceChanged_DisableRepeatCallers() {
148         boolean allow = false;
149         mController.onPreferenceChange(mockPref, allow);
150 
151         verify(mBackend)
152             .saveSoundPolicy(NotificationManager.Policy.PRIORITY_CATEGORY_REPEAT_CALLERS, allow);
153     }
154 }