• 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.app.NotificationChannel.DEFAULT_CHANNEL_ID;
20 import static android.app.NotificationManager.IMPORTANCE_DEFAULT;
21 import static android.app.NotificationManager.IMPORTANCE_HIGH;
22 import static android.app.NotificationManager.IMPORTANCE_LOW;
23 import static android.app.NotificationManager.IMPORTANCE_MIN;
24 import static junit.framework.Assert.assertFalse;
25 import static junit.framework.Assert.assertTrue;
26 import static org.mockito.ArgumentMatchers.any;
27 import static org.mockito.ArgumentMatchers.anyInt;
28 import static org.mockito.Mockito.mock;
29 import static org.mockito.Mockito.spy;
30 import static org.mockito.Mockito.times;
31 import static org.mockito.Mockito.verify;
32 import static org.mockito.Mockito.when;
33 
34 import android.app.NotificationChannel;
35 import android.app.NotificationManager;
36 import android.content.Context;
37 import android.os.UserManager;
38 import android.support.v7.preference.Preference;
39 import android.support.v7.preference.PreferenceScreen;
40 
41 import com.android.settings.testutils.SettingsRobolectricTestRunner;
42 import com.android.settingslib.RestrictedLockUtils;
43 import com.android.settingslib.RestrictedSwitchPreference;
44 import com.android.settingslib.core.lifecycle.Lifecycle;
45 
46 import org.junit.Before;
47 import org.junit.Test;
48 import org.junit.runner.RunWith;
49 import org.mockito.Answers;
50 import org.mockito.Mock;
51 import org.mockito.MockitoAnnotations;
52 import org.robolectric.RuntimeEnvironment;
53 import org.robolectric.shadows.ShadowApplication;
54 
55 @RunWith(SettingsRobolectricTestRunner.class)
56 public class DndPreferenceControllerTest {
57 
58     private Context mContext;
59     @Mock
60     private NotificationBackend mBackend;
61     @Mock
62     private NotificationManager mNm;
63     @Mock
64     private UserManager mUm;
65     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
66     private PreferenceScreen mScreen;
67 
68     private DndPreferenceController mController;
69 
70     @Before
setUp()71     public void setUp() {
72         MockitoAnnotations.initMocks(this);
73         ShadowApplication shadowApplication = ShadowApplication.getInstance();
74         shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNm);
75         shadowApplication.setSystemService(Context.USER_SERVICE, mUm);
76         mContext = RuntimeEnvironment.application;
77         mController = spy(new DndPreferenceController(mContext, mBackend));
78     }
79 
80     @Test
testIsAvailable_app()81     public void testIsAvailable_app() {
82         when(mNm.getNotificationPolicy()).thenReturn(new NotificationManager.Policy(0, 0, 0, 0));
83         NotificationBackend.AppRow appRow = new NotificationBackend.AppRow();
84         mController.onResume(appRow, null, null, null);
85         assertFalse(mController.isAvailable());
86     }
87 
88     @Test
testIsAvailable_channel()89     public void testIsAvailable_channel() {
90         when(mNm.getNotificationPolicy()).thenReturn(new NotificationManager.Policy(0, 0, 0, 1));
91         NotificationBackend.AppRow appRow = new NotificationBackend.AppRow();
92         NotificationChannel channel =
93                 new NotificationChannel(DEFAULT_CHANNEL_ID, "", IMPORTANCE_MIN);
94         mController.onResume(appRow, channel, null, null);
95         assertTrue(mController.isAvailable());
96     }
97 
98     @Test
testUpdateState_disabledByAdmin()99     public void testUpdateState_disabledByAdmin() {
100         NotificationChannel channel = mock(NotificationChannel.class);
101         when(channel.getId()).thenReturn("something");
102         mController.onResume(new NotificationBackend.AppRow(), channel, null, mock(
103                 RestrictedLockUtils.EnforcedAdmin.class));
104 
105         Preference pref = new RestrictedSwitchPreference(RuntimeEnvironment.application);
106         mController.updateState(pref);
107 
108         assertFalse(pref.isEnabled());
109     }
110 
111     @Test
testUpdateState_notConfigurable()112     public void testUpdateState_notConfigurable() {
113         String lockedId = "locked";
114         NotificationBackend.AppRow appRow = new NotificationBackend.AppRow();
115         appRow.lockedChannelId = lockedId;
116         NotificationChannel channel = mock(NotificationChannel.class);
117         when(channel.getId()).thenReturn(lockedId);
118         mController.onResume(appRow, channel, null, null);
119 
120         Preference pref = new RestrictedSwitchPreference(RuntimeEnvironment.application);
121         mController.updateState(pref);
122 
123         assertFalse(pref.isEnabled());
124     }
125 
126     @Test
testUpdateState_configurable()127     public void testUpdateState_configurable() {
128         NotificationBackend.AppRow appRow = new NotificationBackend.AppRow();
129         NotificationChannel channel = mock(NotificationChannel.class);
130         when(channel.getId()).thenReturn("something");
131         mController.onResume(appRow, channel, null, null);
132 
133         Preference pref = new RestrictedSwitchPreference(RuntimeEnvironment.application);
134         mController.updateState(pref);
135 
136         assertTrue(pref.isEnabled());
137     }
138 
139     @Test
testUpdateState_bypassDnd()140     public void testUpdateState_bypassDnd() {
141         NotificationChannel channel = mock(NotificationChannel.class);
142         when(channel.canBypassDnd()).thenReturn(true);
143         mController.onResume(new NotificationBackend.AppRow(), channel, null, null);
144 
145         RestrictedSwitchPreference pref =
146                 new RestrictedSwitchPreference(RuntimeEnvironment.application);
147         mController.updateState(pref);
148         assertTrue(pref.isChecked());
149     }
150 
151     @Test
testUpdateState_noBypassDnd()152     public void testUpdateState_noBypassDnd() {
153         NotificationChannel channel = mock(NotificationChannel.class);
154         when(channel.canBypassDnd()).thenReturn(false);
155         mController.onResume(new NotificationBackend.AppRow(), channel, null, null);
156 
157         RestrictedSwitchPreference pref =
158                 new RestrictedSwitchPreference(RuntimeEnvironment.application);
159         mController.updateState(pref);
160         assertFalse(pref.isChecked());
161     }
162 
163     @Test
testOnPreferenceChange_on()164     public void testOnPreferenceChange_on() {
165         NotificationChannel channel =
166                 new NotificationChannel(DEFAULT_CHANNEL_ID, "a", IMPORTANCE_LOW);
167         mController.onResume(new NotificationBackend.AppRow(), channel, null, null);
168 
169         RestrictedSwitchPreference pref =
170                 new RestrictedSwitchPreference(RuntimeEnvironment.application);
171         when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(pref);
172         mController.displayPreference(mScreen);
173         mController.updateState(pref);
174 
175         mController.onPreferenceChange(pref, true);
176 
177         assertTrue(channel.canBypassDnd());
178         verify(mBackend, times(1)).updateChannel(any(), anyInt(), any());
179     }
180 
181     @Test
testOnPreferenceChange_off()182     public void testOnPreferenceChange_off() {
183         NotificationChannel channel =
184                 new NotificationChannel(DEFAULT_CHANNEL_ID, "a", IMPORTANCE_HIGH);
185         mController.onResume(new NotificationBackend.AppRow(), channel, null, null);
186 
187         RestrictedSwitchPreference pref =
188                 new RestrictedSwitchPreference(RuntimeEnvironment.application);
189         when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(pref);
190         mController.displayPreference(mScreen);
191         mController.updateState(pref);
192 
193         mController.onPreferenceChange(pref, false);
194 
195         assertFalse(channel.canBypassDnd());
196         verify(mBackend, times(1)).updateChannel(any(), anyInt(), any());
197     }
198 }
199