• 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 
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.Answers;
42 import org.mockito.Mock;
43 import org.mockito.MockitoAnnotations;
44 import org.robolectric.RobolectricTestRunner;
45 import org.robolectric.RuntimeEnvironment;
46 import org.robolectric.shadows.ShadowApplication;
47 import org.robolectric.util.ReflectionHelpers;
48 
49 @RunWith(RobolectricTestRunner.class)
50 public class ZenModeMediaPreferenceControllerTest {
51     private ZenModeMediaPreferenceController mController;
52 
53     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
54     private Context mContext;
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     private ContentResolver mContentResolver;
66 
67     @Before
setup()68     public void setup() {
69         MockitoAnnotations.initMocks(this);
70         ShadowApplication shadowApplication = ShadowApplication.getInstance();
71         shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNotificationManager);
72 
73         mContext = RuntimeEnvironment.application;
74         mContentResolver = RuntimeEnvironment.application.getContentResolver();
75         when(mNotificationManager.getNotificationPolicy()).thenReturn(mPolicy);
76 
77         mController = new ZenModeMediaPreferenceController(mContext,
78                 mock(Lifecycle.class));
79         ReflectionHelpers.setField(mController, "mBackend", mBackend);
80 
81         when(mPreferenceScreen.findPreference(mController.getPreferenceKey()))
82                 .thenReturn(mockPref);
83         mController.displayPreference(mPreferenceScreen);
84     }
85 
86     @Test
updateState_TotalSilence()87     public void updateState_TotalSilence() {
88         Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_NO_INTERRUPTIONS);
89 
90         final SwitchPreference mockPref = mock(SwitchPreference.class);
91         mController.updateState(mockPref);
92 
93         verify(mockPref).setEnabled(false);
94         verify(mockPref).setChecked(false);
95     }
96 
97     @Test
updateState_AlarmsOnly()98     public void updateState_AlarmsOnly() {
99         Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_ALARMS);
100 
101         final SwitchPreference mockPref = mock(SwitchPreference.class);
102         mController.updateState(mockPref);
103 
104         verify(mockPref).setEnabled(false);
105         verify(mockPref).setChecked(true);
106     }
107 
108     @Test
updateState_Priority()109     public void updateState_Priority() {
110         Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_IMPORTANT_INTERRUPTIONS);
111 
112         when(mBackend.isPriorityCategoryEnabled(
113                 NotificationManager.Policy.PRIORITY_CATEGORY_MEDIA)).
114                 thenReturn(true);
115 
116         mController.updateState(mockPref);
117 
118         verify(mockPref).setEnabled(true);
119         verify(mockPref).setChecked(true);
120     }
121 
122     @Test
onPreferenceChanged_EnableEvents()123     public void onPreferenceChanged_EnableEvents() {
124         boolean allow = true;
125         mController.onPreferenceChange(mockPref, allow);
126 
127         verify(mBackend).saveSoundPolicy(
128                 NotificationManager.Policy.PRIORITY_CATEGORY_MEDIA, allow);
129     }
130 
131     @Test
onPreferenceChanged_DisableEvents()132     public void onPreferenceChanged_DisableEvents() {
133         boolean allow = false;
134         mController.onPreferenceChange(mockPref, allow);
135 
136         verify(mBackend).saveSoundPolicy(
137                 NotificationManager.Policy.PRIORITY_CATEGORY_MEDIA, allow);
138     }
139 }