blob: c9c31bfcde0890241908f35fb1a4a28229d826f4 [file] [log] [blame]
Julia Reynolds924eed12017-01-19 09:52:07 -05001/*
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 */
16package com.android.server.notification;
17
Chris Wren89aa2262017-05-05 18:05:56 -040018import static android.app.NotificationManager.IMPORTANCE_HIGH;
19import static android.app.NotificationManager.IMPORTANCE_UNSPECIFIED;
Julia Reynoldsccc6ae62018-03-01 16:24:49 -050020import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_BADGE;
21import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_LIGHTS;
Chris Wren89aa2262017-05-05 18:05:56 -040022
Brett Chabot84151d92019-02-27 15:37:59 -080023import static junit.framework.Assert.assertFalse;
24import static junit.framework.Assert.assertTrue;
25
26import static org.mockito.Mockito.when;
27
Julia Reynolds924eed12017-01-19 09:52:07 -050028import android.app.ActivityManager;
29import android.app.Notification;
30import android.app.Notification.Builder;
31import android.app.NotificationChannel;
Lyn Han43fbcbd2020-06-03 17:43:30 -070032import android.app.PendingIntent;
33import android.content.Intent;
34import android.graphics.drawable.Icon;
35
Julia Reynolds924eed12017-01-19 09:52:07 -050036import android.os.UserHandle;
37import android.service.notification.StatusBarNotification;
Julia Reynolds924eed12017-01-19 09:52:07 -050038import android.test.suitebuilder.annotation.SmallTest;
39
Brett Chabot84151d92019-02-27 15:37:59 -080040import androidx.test.runner.AndroidJUnit4;
41
Jason Monk74f5e362017-12-06 08:56:33 -050042import com.android.server.UiServiceTestCase;
43
Julia Reynolds924eed12017-01-19 09:52:07 -050044import org.junit.Before;
45import org.junit.Test;
46import org.junit.runner.RunWith;
47import org.mockito.Mock;
48import org.mockito.MockitoAnnotations;
49
50@SmallTest
51@RunWith(AndroidJUnit4.class)
Jason Monk74f5e362017-12-06 08:56:33 -050052public class BadgeExtractorTest extends UiServiceTestCase {
Julia Reynolds924eed12017-01-19 09:52:07 -050053
54 @Mock RankingConfig mConfig;
55
56 private String mPkg = "com.android.server.notification";
57 private int mId = 1001;
58 private String mTag = null;
59 private int mUid = 1000;
60 private int mPid = 2000;
61 private UserHandle mUser = UserHandle.of(ActivityManager.getCurrentUser());
62
63 @Before
64 public void setUp() {
65 MockitoAnnotations.initMocks(this);
66 }
67
Chris Wren89aa2262017-05-05 18:05:56 -040068 private NotificationRecord getNotificationRecord(boolean showBadge, int importanceHigh) {
69 NotificationChannel channel = new NotificationChannel("a", "a", importanceHigh);
70 channel.setShowBadge(showBadge);
71 when(mConfig.getNotificationChannel(mPkg, mUid, "a", false)).thenReturn(channel);
72
Julia Reynolds924eed12017-01-19 09:52:07 -050073 final Builder builder = new Builder(getContext())
74 .setContentTitle("foo")
75 .setSmallIcon(android.R.drawable.sym_def_app_icon)
76 .setPriority(Notification.PRIORITY_HIGH)
77 .setDefaults(Notification.DEFAULT_SOUND);
78
79 Notification n = builder.build();
80 StatusBarNotification sbn = new StatusBarNotification(mPkg, mPkg, mId, mTag, mUid,
81 mPid, n, mUser, null, System.currentTimeMillis());
Geoffrey Pitscha22f6442017-05-05 16:47:38 +000082 NotificationRecord r = new NotificationRecord(getContext(), sbn, channel);
Julia Reynolds924eed12017-01-19 09:52:07 -050083 return r;
84 }
85
Lyn Han43fbcbd2020-06-03 17:43:30 -070086 private NotificationRecord getNotificationRecordWithBubble(boolean suppressNotif) {
87 NotificationChannel channel = new NotificationChannel("a", "a", IMPORTANCE_UNSPECIFIED);
88 channel.setShowBadge(/* showBadge */ true);
89 when(mConfig.getNotificationChannel(mPkg, mUid, "a", false)).thenReturn(channel);
90
91 Notification.BubbleMetadata metadata = new Notification.BubbleMetadata.Builder(
92 PendingIntent.getActivity(mContext, 0, new Intent(), 0),
93 Icon.createWithResource("", 0)).build();
94
95 int flags = metadata.getFlags();
96 if (suppressNotif) {
97 flags |= Notification.BubbleMetadata.FLAG_SUPPRESS_NOTIFICATION;
98 } else {
99 flags &= ~Notification.BubbleMetadata.FLAG_SUPPRESS_NOTIFICATION;
100 }
101 metadata.setFlags(flags);
102
103 final Builder builder = new Builder(getContext())
104 .setContentTitle("foo")
105 .setSmallIcon(android.R.drawable.sym_def_app_icon)
106 .setPriority(Notification.PRIORITY_HIGH)
107 .setDefaults(Notification.DEFAULT_SOUND)
108 .setBubbleMetadata(metadata);
109
110 Notification n = builder.build();
111 StatusBarNotification sbn = new StatusBarNotification(mPkg, mPkg, mId, mTag, mUid,
112 mPid, n, mUser, null, System.currentTimeMillis());
113 NotificationRecord r = new NotificationRecord(getContext(), sbn, channel);
114 return r;
115 }
116
Julia Reynolds924eed12017-01-19 09:52:07 -0500117 //
118 // Tests
119 //
120
121 @Test
122 public void testAppYesChannelNo() throws Exception {
123 BadgeExtractor extractor = new BadgeExtractor();
124 extractor.setConfig(mConfig);
125
Chris Wren89aa2262017-05-05 18:05:56 -0400126 when(mConfig.badgingEnabled(mUser)).thenReturn(true);
Julia Reynolds924eed12017-01-19 09:52:07 -0500127 when(mConfig.canShowBadge(mPkg, mUid)).thenReturn(true);
Chris Wren89aa2262017-05-05 18:05:56 -0400128 NotificationRecord r = getNotificationRecord(false, IMPORTANCE_UNSPECIFIED);
Julia Reynolds924eed12017-01-19 09:52:07 -0500129
130 extractor.process(r);
131
132 assertFalse(r.canShowBadge());
133 }
134
135 @Test
136 public void testAppNoChannelYes() throws Exception {
137 BadgeExtractor extractor = new BadgeExtractor();
138 extractor.setConfig(mConfig);
139
Chris Wren89aa2262017-05-05 18:05:56 -0400140 when(mConfig.badgingEnabled(mUser)).thenReturn(true);
Julia Reynolds924eed12017-01-19 09:52:07 -0500141 when(mConfig.canShowBadge(mPkg, mUid)).thenReturn(false);
Chris Wren89aa2262017-05-05 18:05:56 -0400142 NotificationRecord r = getNotificationRecord(true, IMPORTANCE_HIGH);
Julia Reynolds924eed12017-01-19 09:52:07 -0500143
144 extractor.process(r);
145
146 assertFalse(r.canShowBadge());
147 }
148
149 @Test
150 public void testAppYesChannelYes() throws Exception {
151 BadgeExtractor extractor = new BadgeExtractor();
152 extractor.setConfig(mConfig);
153
Chris Wren89aa2262017-05-05 18:05:56 -0400154 when(mConfig.badgingEnabled(mUser)).thenReturn(true);
Julia Reynolds924eed12017-01-19 09:52:07 -0500155 when(mConfig.canShowBadge(mPkg, mUid)).thenReturn(true);
Chris Wren89aa2262017-05-05 18:05:56 -0400156 NotificationRecord r = getNotificationRecord(true, IMPORTANCE_UNSPECIFIED);
Julia Reynolds924eed12017-01-19 09:52:07 -0500157
158 extractor.process(r);
159
160 assertTrue(r.canShowBadge());
161 }
162
163 @Test
164 public void testAppNoChannelNo() throws Exception {
165 BadgeExtractor extractor = new BadgeExtractor();
166 extractor.setConfig(mConfig);
167
Chris Wren89aa2262017-05-05 18:05:56 -0400168 when(mConfig.badgingEnabled(mUser)).thenReturn(true);
Julia Reynolds924eed12017-01-19 09:52:07 -0500169 when(mConfig.canShowBadge(mPkg, mUid)).thenReturn(false);
Chris Wren89aa2262017-05-05 18:05:56 -0400170 NotificationRecord r = getNotificationRecord(false, IMPORTANCE_UNSPECIFIED);
Julia Reynolds924eed12017-01-19 09:52:07 -0500171
Chris Wren89aa2262017-05-05 18:05:56 -0400172 extractor.process(r);
173
174 assertFalse(r.canShowBadge());
175 }
176
177 @Test
178 public void testAppYesChannelYesUserNo() throws Exception {
179 BadgeExtractor extractor = new BadgeExtractor();
180 extractor.setConfig(mConfig);
181
182 when(mConfig.badgingEnabled(mUser)).thenReturn(false);
183 when(mConfig.canShowBadge(mPkg, mUid)).thenReturn(true);
184 NotificationRecord r = getNotificationRecord(true, IMPORTANCE_HIGH);
Julia Reynolds924eed12017-01-19 09:52:07 -0500185
186 extractor.process(r);
187
188 assertFalse(r.canShowBadge());
189 }
Julia Reynoldsccc6ae62018-03-01 16:24:49 -0500190
191 @Test
Lyn Han43fbcbd2020-06-03 17:43:30 -0700192 public void testHideNotifOverridesYes() throws Exception {
193 BadgeExtractor extractor = new BadgeExtractor();
194 extractor.setConfig(mConfig);
195
196 when(mConfig.badgingEnabled(mUser)).thenReturn(true);
197 when(mConfig.canShowBadge(mPkg, mUid)).thenReturn(true);
198 NotificationRecord r = getNotificationRecordWithBubble(/* suppressNotif */ true);
199
200 extractor.process(r);
201
202 assertFalse(r.canShowBadge());
203 }
204
205 @Test
Julia Reynoldsccc6ae62018-03-01 16:24:49 -0500206 public void testDndOverridesYes() {
207 BadgeExtractor extractor = new BadgeExtractor();
208 extractor.setConfig(mConfig);
209
210 when(mConfig.badgingEnabled(mUser)).thenReturn(true);
211 when(mConfig.canShowBadge(mPkg, mUid)).thenReturn(true);
212 NotificationRecord r = getNotificationRecord(true, IMPORTANCE_UNSPECIFIED);
213 r.setIntercepted(true);
214 r.setSuppressedVisualEffects(SUPPRESSED_EFFECT_BADGE);
215
216 extractor.process(r);
217
218 assertFalse(r.canShowBadge());
219 }
220
221 @Test
222 public void testDndOConsidersInterception() {
223 BadgeExtractor extractor = new BadgeExtractor();
224 extractor.setConfig(mConfig);
225
226 when(mConfig.badgingEnabled(mUser)).thenReturn(true);
227 when(mConfig.canShowBadge(mPkg, mUid)).thenReturn(true);
228 NotificationRecord r = getNotificationRecord(true, IMPORTANCE_UNSPECIFIED);
229 r.setIntercepted(false);
230 r.setSuppressedVisualEffects(SUPPRESSED_EFFECT_BADGE);
231
232 extractor.process(r);
233
234 assertTrue(r.canShowBadge());
235 }
236
237 @Test
238 public void testDndConsidersSuppressedVisualEffects() {
239 BadgeExtractor extractor = new BadgeExtractor();
240 extractor.setConfig(mConfig);
241
242 when(mConfig.badgingEnabled(mUser)).thenReturn(true);
243 when(mConfig.canShowBadge(mPkg, mUid)).thenReturn(true);
244 NotificationRecord r = getNotificationRecord(true, IMPORTANCE_UNSPECIFIED);
245 r.setIntercepted(true);
246 r.setSuppressedVisualEffects(SUPPRESSED_EFFECT_LIGHTS);
247
248 extractor.process(r);
249
250 assertTrue(r.canShowBadge());
251 }
Julia Reynolds924eed12017-01-19 09:52:07 -0500252}