blob: ea77d31f69695376f2ea0ec088d66e70e200181f [file] [log] [blame]
Julia Reynolds901bf282018-08-14 10:09:36 -04001/**
2 * Copyright (C) 2018 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
17package android.ext.services.notification;
18
19import static android.app.NotificationManager.IMPORTANCE_DEFAULT;
20import static android.app.NotificationManager.IMPORTANCE_HIGH;
Julia Reynolds11e085a2019-02-05 15:49:56 -050021import static android.app.NotificationManager.IMPORTANCE_LOW;
Julia Reynolds901bf282018-08-14 10:09:36 -040022import static android.app.NotificationManager.IMPORTANCE_MIN;
23import static android.os.Process.FIRST_APPLICATION_UID;
24
25import static junit.framework.Assert.assertFalse;
26import static junit.framework.Assert.assertTrue;
27
28import static org.junit.Assert.assertEquals;
29import static org.mockito.Mockito.when;
30
31import android.app.Notification;
32import android.app.NotificationChannel;
33import android.os.Process;
34import android.service.notification.StatusBarNotification;
Julia Reynolds901bf282018-08-14 10:09:36 -040035import android.testing.TestableContext;
36
Brett Chabot84151d92019-02-27 15:37:59 -080037import androidx.test.InstrumentationRegistry;
38import androidx.test.runner.AndroidJUnit4;
39
Julia Reynolds901bf282018-08-14 10:09:36 -040040import org.junit.Before;
41import org.junit.Rule;
42import org.junit.Test;
43import org.junit.runner.RunWith;
44import org.mockito.Mock;
45import org.mockito.MockitoAnnotations;
46
47@RunWith(AndroidJUnit4.class)
48public class NotificationCategorizerTest {
49 @Mock
50 private NotificationEntry mEntry;
51 @Mock
52 private StatusBarNotification mSbn;
53
54 @Rule
55 public final TestableContext mContext =
56 new TestableContext(InstrumentationRegistry.getContext(), null);
57
58 @Before
59 public void setUp() {
60 MockitoAnnotations.initMocks(this);
61
62 when(mEntry.getSbn()).thenReturn(mSbn);
63 when(mSbn.getUid()).thenReturn(Process.myUid());
64 when(mSbn.getPackageName()).thenReturn(mContext.getPackageName());
65 }
66
67 @Test
68 public void testPeopleCategory() {
69 NotificationCategorizer nc = new NotificationCategorizer();
70
71 when(mEntry.getChannel()).thenReturn(new NotificationChannel("", "", IMPORTANCE_DEFAULT));
72 when(mEntry.involvesPeople()).thenReturn(true);
73
74 assertEquals(NotificationCategorizer.CATEGORY_PEOPLE, nc.getCategory(mEntry));
75 assertFalse(nc.shouldSilence(NotificationCategorizer.CATEGORY_PEOPLE));
76 }
77
78 @Test
79 public void testMin() {
80 NotificationCategorizer nc = new NotificationCategorizer();
81
82 when(mEntry.getChannel()).thenReturn(new NotificationChannel("", "", IMPORTANCE_MIN));
83 when(mEntry.involvesPeople()).thenReturn(true);
84
85 assertEquals(NotificationCategorizer.CATEGORY_MIN, nc.getCategory(mEntry));
86 assertTrue(nc.shouldSilence(NotificationCategorizer.CATEGORY_MIN));
87 }
88
89 @Test
Julia Reynolds98f6e692019-01-29 16:31:56 -050090 public void testHigh() {
91 NotificationCategorizer nc = new NotificationCategorizer();
92
93 when(mEntry.getChannel()).thenReturn(new NotificationChannel("", "", IMPORTANCE_HIGH));
94
95 assertEquals(NotificationCategorizer.CATEGORY_HIGH, nc.getCategory(mEntry));
96 assertFalse(nc.shouldSilence(NotificationCategorizer.CATEGORY_HIGH));
97 }
98
99 @Test
Julia Reynolds901bf282018-08-14 10:09:36 -0400100 public void testOngoingCategory() {
101 NotificationCategorizer nc = new NotificationCategorizer();
102
103 when(mEntry.getChannel()).thenReturn(new NotificationChannel("", "", IMPORTANCE_DEFAULT));
104 when(mEntry.isOngoing()).thenReturn(true);
105
106 assertEquals(NotificationCategorizer.CATEGORY_ONGOING, nc.getCategory(mEntry));
107 assertTrue(nc.shouldSilence(NotificationCategorizer.CATEGORY_ONGOING));
108
109 when(mEntry.isOngoing()).thenReturn(false);
110 assertEquals(NotificationCategorizer.CATEGORY_EVERYTHING_ELSE, nc.getCategory(mEntry));
111 assertTrue(nc.shouldSilence(NotificationCategorizer.CATEGORY_EVERYTHING_ELSE));
112 }
113
114 @Test
115 public void testAlarmCategory() {
116 NotificationCategorizer nc = new NotificationCategorizer();
117
118 when(mEntry.getChannel()).thenReturn(new NotificationChannel("", "", IMPORTANCE_DEFAULT));
119 when(mEntry.isCategory(Notification.CATEGORY_ALARM)).thenReturn(true);
120
121 assertEquals(NotificationCategorizer.CATEGORY_ALARM, nc.getCategory(mEntry));
122 assertFalse(nc.shouldSilence(NotificationCategorizer.CATEGORY_ALARM));
123
124 when(mEntry.isCategory(Notification.CATEGORY_ALARM)).thenReturn(false);
125 assertEquals(NotificationCategorizer.CATEGORY_EVERYTHING_ELSE, nc.getCategory(mEntry));
126 assertTrue(nc.shouldSilence(NotificationCategorizer.CATEGORY_EVERYTHING_ELSE));
127 }
128
129 @Test
130 public void testCallCategory() {
131 NotificationCategorizer nc = new NotificationCategorizer();
132
133 when(mEntry.getChannel()).thenReturn(new NotificationChannel("", "", IMPORTANCE_DEFAULT));
134 when(mEntry.isCategory(Notification.CATEGORY_CALL)).thenReturn(true);
135
136 assertEquals(NotificationCategorizer.CATEGORY_CALL, nc.getCategory(mEntry));
137 assertFalse(nc.shouldSilence(NotificationCategorizer.CATEGORY_CALL));
138
139 when(mEntry.isCategory(Notification.CATEGORY_CALL)).thenReturn(false);
140 assertEquals(NotificationCategorizer.CATEGORY_EVERYTHING_ELSE, nc.getCategory(mEntry));
141 assertTrue(nc.shouldSilence(NotificationCategorizer.CATEGORY_EVERYTHING_ELSE));
142 }
143
144 @Test
145 public void testReminderCategory() {
146 NotificationCategorizer nc = new NotificationCategorizer();
147
148 when(mEntry.getChannel()).thenReturn(new NotificationChannel("", "", IMPORTANCE_DEFAULT));
149 when(mEntry.isCategory(Notification.CATEGORY_REMINDER)).thenReturn(true);
150
151 assertEquals(NotificationCategorizer.CATEGORY_REMINDER, nc.getCategory(mEntry));
152 assertFalse(nc.shouldSilence(NotificationCategorizer.CATEGORY_REMINDER));
153
154 when(mEntry.isCategory(Notification.CATEGORY_REMINDER)).thenReturn(false);
155 assertEquals(NotificationCategorizer.CATEGORY_EVERYTHING_ELSE, nc.getCategory(mEntry));
156 assertTrue(nc.shouldSilence(NotificationCategorizer.CATEGORY_EVERYTHING_ELSE));
157 }
158
159 @Test
160 public void testEventCategory() {
161 NotificationCategorizer nc = new NotificationCategorizer();
162
163 when(mEntry.getChannel()).thenReturn(new NotificationChannel("", "", IMPORTANCE_DEFAULT));
164 when(mEntry.isCategory(Notification.CATEGORY_EVENT)).thenReturn(true);
165
166 assertEquals(NotificationCategorizer.CATEGORY_EVENT, nc.getCategory(mEntry));
167 assertFalse(nc.shouldSilence(NotificationCategorizer.CATEGORY_EVENT));
168
169 when(mEntry.isCategory(Notification.CATEGORY_EVENT)).thenReturn(false);
170 assertEquals(NotificationCategorizer.CATEGORY_EVERYTHING_ELSE, nc.getCategory(mEntry));
171 }
172
173 @Test
174 public void testSystemCategory() {
175 NotificationCategorizer nc = new NotificationCategorizer();
176
Julia Reynolds11e085a2019-02-05 15:49:56 -0500177 when(mEntry.getChannel()).thenReturn(new NotificationChannel("", "", IMPORTANCE_DEFAULT));
178 when(mEntry.getImportance()).thenReturn(IMPORTANCE_DEFAULT);
Julia Reynolds901bf282018-08-14 10:09:36 -0400179 when(mSbn.getUid()).thenReturn(FIRST_APPLICATION_UID - 1);
180
181 assertEquals(NotificationCategorizer.CATEGORY_SYSTEM, nc.getCategory(mEntry));
182 assertFalse(nc.shouldSilence(NotificationCategorizer.CATEGORY_SYSTEM));
183
184 when(mSbn.getUid()).thenReturn(FIRST_APPLICATION_UID);
Julia Reynolds11e085a2019-02-05 15:49:56 -0500185 assertEquals(NotificationCategorizer.CATEGORY_EVERYTHING_ELSE, nc.getCategory(mEntry));
Julia Reynolds901bf282018-08-14 10:09:36 -0400186 }
187
188 @Test
189 public void testSystemLowCategory() {
190 NotificationCategorizer nc = new NotificationCategorizer();
191
Julia Reynolds11e085a2019-02-05 15:49:56 -0500192 when(mEntry.getChannel()).thenReturn(new NotificationChannel("", "", IMPORTANCE_LOW));
193 when(mEntry.getImportance()).thenReturn(IMPORTANCE_LOW);
Julia Reynolds901bf282018-08-14 10:09:36 -0400194 when(mSbn.getUid()).thenReturn(FIRST_APPLICATION_UID - 1);
195
196 assertEquals(NotificationCategorizer.CATEGORY_SYSTEM_LOW, nc.getCategory(mEntry));
197 assertTrue(nc.shouldSilence(NotificationCategorizer.CATEGORY_SYSTEM_LOW));
198
199 when(mSbn.getUid()).thenReturn(FIRST_APPLICATION_UID);
200 assertEquals(NotificationCategorizer.CATEGORY_EVERYTHING_ELSE, nc.getCategory(mEntry));
201 }
202}