• 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.NotificationManager.IMPORTANCE_LOW;
20 import static android.app.NotificationManager.IMPORTANCE_NONE;
21 
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertFalse;
24 import static org.junit.Assert.assertTrue;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.spy;
27 import static org.mockito.Mockito.when;
28 
29 import android.app.NotificationChannel;
30 import android.app.NotificationChannelGroup;
31 import android.app.NotificationManager;
32 import android.content.Context;
33 import android.os.UserManager;
34 
35 import androidx.preference.Preference;
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.RobolectricTestRunner;
43 import org.robolectric.RuntimeEnvironment;
44 import org.robolectric.shadows.ShadowApplication;
45 
46 @RunWith(RobolectricTestRunner.class)
47 public class DescriptionPreferenceControllerTest {
48 
49     private Context mContext;
50     @Mock
51     private NotificationManager mNm;
52     @Mock
53     private UserManager mUm;
54 
55     private DescriptionPreferenceController mController;
56 
57     @Before
setUp()58     public void setUp() {
59         MockitoAnnotations.initMocks(this);
60         ShadowApplication shadowApplication = ShadowApplication.getInstance();
61         shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNm);
62         shadowApplication.setSystemService(Context.USER_SERVICE, mUm);
63         mContext = RuntimeEnvironment.application;
64         mController = spy(new DescriptionPreferenceController(mContext));
65     }
66 
67     @Test
testNoCrashIfNoOnResume()68     public void testNoCrashIfNoOnResume() {
69         mController.isAvailable();
70         mController.updateState(mock(Preference.class));
71     }
72 
73     @Test
testIsAvailable_notIfNull()74     public void testIsAvailable_notIfNull() {
75         mController.onResume(null, null, null, null);
76         assertFalse(mController.isAvailable());
77     }
78 
79     @Test
testIsAvailable_notIfChannelGroupBlocked()80     public void testIsAvailable_notIfChannelGroupBlocked() {
81         NotificationBackend.AppRow appRow = new NotificationBackend.AppRow();
82         NotificationChannelGroup group = mock(NotificationChannelGroup.class);
83         mController.onResume(appRow, null, group, null);
84         assertFalse(mController.isAvailable());
85     }
86 
87     @Test
testIsAvailable_notIfChannelBlocked()88     public void testIsAvailable_notIfChannelBlocked() {
89         NotificationBackend.AppRow appRow = new NotificationBackend.AppRow();
90         NotificationChannel channel = mock(NotificationChannel.class);
91         when(channel.getImportance()).thenReturn(IMPORTANCE_NONE);
92         mController.onResume(appRow, channel, null, null);
93         assertFalse(mController.isAvailable());
94     }
95 
96     @Test
testIsAvailable_notIfNoChannelDesc()97     public void testIsAvailable_notIfNoChannelDesc() {
98         NotificationBackend.AppRow appRow = new NotificationBackend.AppRow();
99         NotificationChannel channel = mock(NotificationChannel.class);
100         when(channel.getImportance()).thenReturn(IMPORTANCE_LOW);
101         mController.onResume(appRow, channel, null, null);
102         assertFalse(mController.isAvailable());
103     }
104 
105     @Test
testIsAvailable_notIfNoChannelGroupDesc()106     public void testIsAvailable_notIfNoChannelGroupDesc() {
107         NotificationBackend.AppRow appRow = new NotificationBackend.AppRow();
108         NotificationChannelGroup group = mock(NotificationChannelGroup.class);
109         mController.onResume(appRow, null, group, null);
110         assertFalse(mController.isAvailable());
111     }
112 
113     @Test
testIsAvailable_channel()114     public void testIsAvailable_channel() {
115         NotificationBackend.AppRow appRow = new NotificationBackend.AppRow();
116         NotificationChannel channel = mock(NotificationChannel.class);
117         when(channel.getImportance()).thenReturn(IMPORTANCE_LOW);
118         when(channel.getDescription()).thenReturn("AAA");
119         mController.onResume(appRow, channel, null, null);
120         assertTrue(mController.isAvailable());
121     }
122 
123     @Test
testIsAvailable_channelGroup()124     public void testIsAvailable_channelGroup() {
125         NotificationBackend.AppRow appRow = new NotificationBackend.AppRow();
126         NotificationChannelGroup group = mock(NotificationChannelGroup.class);
127         when(group.getDescription()).thenReturn("something");
128         when(group.isBlocked()).thenReturn(false);
129         mController.onResume(appRow, null, group, null);
130         assertTrue(mController.isAvailable());
131     }
132 
133     @Test
testUpdateState_channel()134     public void testUpdateState_channel() {
135         NotificationBackend.AppRow appRow = new NotificationBackend.AppRow();
136         NotificationChannel channel = mock(NotificationChannel.class);
137         when(channel.getImportance()).thenReturn(IMPORTANCE_LOW);
138         when(channel.getDescription()).thenReturn("AAA");
139         mController.onResume(appRow, channel, null, null);
140 
141         Preference pref = new Preference(RuntimeEnvironment.application);
142         mController.updateState(pref);
143 
144         assertEquals("AAA", pref.getTitle());
145         assertFalse(pref.isEnabled());
146         assertFalse(pref.isSelectable());
147     }
148 
149     @Test
testUpdateState_channelGroup()150     public void testUpdateState_channelGroup() {
151         NotificationBackend.AppRow appRow = new NotificationBackend.AppRow();
152         NotificationChannelGroup group = mock(NotificationChannelGroup.class);
153         when(group.getDescription()).thenReturn("something");
154         mController.onResume(appRow, null, group, null);
155 
156         Preference pref = new Preference(RuntimeEnvironment.application);
157         mController.updateState(pref);
158 
159         assertEquals("something", pref.getTitle());
160         assertFalse(pref.isEnabled());
161         assertFalse(pref.isSelectable());
162     }
163 }
164