• 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.Answers;
41 import org.mockito.Mock;
42 import org.mockito.MockitoAnnotations;
43 import org.robolectric.RuntimeEnvironment;
44 import org.robolectric.shadows.ShadowApplication;
45 import org.robolectric.util.ReflectionHelpers;
46 
47 @RunWith(SettingsRobolectricTestRunner.class)
48 public class ZenModeMediaPreferenceControllerTest {
49     private ZenModeMediaPreferenceController mController;
50 
51     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
52     private Context mContext;
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 
65     @Before
setup()66     public void setup() {
67         MockitoAnnotations.initMocks(this);
68         ShadowApplication shadowApplication = ShadowApplication.getInstance();
69         shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNotificationManager);
70 
71         mContext = shadowApplication.getApplicationContext();
72         mContentResolver = RuntimeEnvironment.application.getContentResolver();
73         when(mNotificationManager.getNotificationPolicy()).thenReturn(mPolicy);
74 
75         mController = new ZenModeMediaPreferenceController(mContext,
76                 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(true);
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_MEDIA)).
112                 thenReturn(true);
113 
114         mController.updateState(mockPref);
115 
116         verify(mockPref).setEnabled(true);
117         verify(mockPref).setChecked(true);
118     }
119 
120     @Test
onPreferenceChanged_EnableEvents()121     public void onPreferenceChanged_EnableEvents() {
122         boolean allow = true;
123         mController.onPreferenceChange(mockPref, allow);
124 
125         verify(mBackend).saveSoundPolicy(
126                 NotificationManager.Policy.PRIORITY_CATEGORY_MEDIA, allow);
127     }
128 
129     @Test
onPreferenceChanged_DisableEvents()130     public void onPreferenceChanged_DisableEvents() {
131         boolean allow = false;
132         mController.onPreferenceChange(mockPref, allow);
133 
134         verify(mBackend).saveSoundPolicy(
135                 NotificationManager.Policy.PRIORITY_CATEGORY_MEDIA, allow);
136     }
137 }