• 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 ZenModeEventsPreferenceControllerTest {
48 
49     private static final boolean EVENTS_SETTINGS = true;
50 
51     private ZenModeEventsPreferenceController 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 = RuntimeEnvironment.application;
73         mContentResolver = RuntimeEnvironment.application.getContentResolver();
74         when(mNotificationManager.getNotificationPolicy()).thenReturn(mPolicy);
75 
76         mController = new ZenModeEventsPreferenceController(mContext, mock(Lifecycle.class));
77         ReflectionHelpers.setField(mController, "mBackend", mBackend);
78 
79         when(mPreferenceScreen.findPreference(mController.getPreferenceKey()))
80             .thenReturn(mockPref);
81         mController.displayPreference(mPreferenceScreen);
82     }
83 
84     @Test
updateState_TotalSilence()85     public void updateState_TotalSilence() {
86         Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_NO_INTERRUPTIONS);
87 
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_EVENTS)).thenReturn(EVENTS_SETTINGS);
112 
113         mController.updateState(mockPref);
114 
115         verify(mockPref).setEnabled(true);
116         verify(mockPref).setChecked(EVENTS_SETTINGS);
117     }
118 
119     @Test
onPreferenceChanged_EnableEvents()120     public void onPreferenceChanged_EnableEvents() {
121         boolean allow = true;
122         mController.onPreferenceChange(mockPref, allow);
123 
124         verify(mBackend)
125             .saveSoundPolicy(NotificationManager.Policy.PRIORITY_CATEGORY_EVENTS, allow);
126     }
127 
128     @Test
onPreferenceChanged_DisableEvents()129     public void onPreferenceChanged_DisableEvents() {
130         boolean allow = false;
131         mController.onPreferenceChange(mockPref, allow);
132 
133         verify(mBackend)
134             .saveSoundPolicy(NotificationManager.Policy.PRIORITY_CATEGORY_EVENTS, allow);
135     }
136 }