Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 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 | package com.android.server.notification; |
| 17 | |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 18 | import static android.app.NotificationManager.IMPORTANCE_HIGH; |
| 19 | import static android.app.NotificationManager.IMPORTANCE_UNSPECIFIED; |
Julia Reynolds | ccc6ae6 | 2018-03-01 16:24:49 -0500 | [diff] [blame] | 20 | import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_BADGE; |
| 21 | import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_LIGHTS; |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 22 | |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 23 | import static junit.framework.Assert.assertFalse; |
| 24 | import static junit.framework.Assert.assertTrue; |
| 25 | |
| 26 | import static org.mockito.Mockito.when; |
| 27 | |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 28 | import android.app.ActivityManager; |
| 29 | import android.app.Notification; |
| 30 | import android.app.Notification.Builder; |
| 31 | import android.app.NotificationChannel; |
Lyn Han | 43fbcbd | 2020-06-03 17:43:30 -0700 | [diff] [blame] | 32 | import android.app.PendingIntent; |
| 33 | import android.content.Intent; |
| 34 | import android.graphics.drawable.Icon; |
| 35 | |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 36 | import android.os.UserHandle; |
| 37 | import android.service.notification.StatusBarNotification; |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 38 | import android.test.suitebuilder.annotation.SmallTest; |
| 39 | |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 40 | import androidx.test.runner.AndroidJUnit4; |
| 41 | |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 42 | import com.android.server.UiServiceTestCase; |
| 43 | |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 44 | import org.junit.Before; |
| 45 | import org.junit.Test; |
| 46 | import org.junit.runner.RunWith; |
| 47 | import org.mockito.Mock; |
| 48 | import org.mockito.MockitoAnnotations; |
| 49 | |
| 50 | @SmallTest |
| 51 | @RunWith(AndroidJUnit4.class) |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 52 | public class BadgeExtractorTest extends UiServiceTestCase { |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 53 | |
| 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 Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 68 | 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 Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 73 | 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 Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 82 | NotificationRecord r = new NotificationRecord(getContext(), sbn, channel); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 83 | return r; |
| 84 | } |
| 85 | |
Lyn Han | 43fbcbd | 2020-06-03 17:43:30 -0700 | [diff] [blame] | 86 | 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 Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 117 | // |
| 118 | // Tests |
| 119 | // |
| 120 | |
| 121 | @Test |
| 122 | public void testAppYesChannelNo() throws Exception { |
| 123 | BadgeExtractor extractor = new BadgeExtractor(); |
| 124 | extractor.setConfig(mConfig); |
| 125 | |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 126 | when(mConfig.badgingEnabled(mUser)).thenReturn(true); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 127 | when(mConfig.canShowBadge(mPkg, mUid)).thenReturn(true); |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 128 | NotificationRecord r = getNotificationRecord(false, IMPORTANCE_UNSPECIFIED); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 129 | |
| 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 Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 140 | when(mConfig.badgingEnabled(mUser)).thenReturn(true); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 141 | when(mConfig.canShowBadge(mPkg, mUid)).thenReturn(false); |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 142 | NotificationRecord r = getNotificationRecord(true, IMPORTANCE_HIGH); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 143 | |
| 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 Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 154 | when(mConfig.badgingEnabled(mUser)).thenReturn(true); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 155 | when(mConfig.canShowBadge(mPkg, mUid)).thenReturn(true); |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 156 | NotificationRecord r = getNotificationRecord(true, IMPORTANCE_UNSPECIFIED); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 157 | |
| 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 Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 168 | when(mConfig.badgingEnabled(mUser)).thenReturn(true); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 169 | when(mConfig.canShowBadge(mPkg, mUid)).thenReturn(false); |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 170 | NotificationRecord r = getNotificationRecord(false, IMPORTANCE_UNSPECIFIED); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 171 | |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 172 | 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 Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 185 | |
| 186 | extractor.process(r); |
| 187 | |
| 188 | assertFalse(r.canShowBadge()); |
| 189 | } |
Julia Reynolds | ccc6ae6 | 2018-03-01 16:24:49 -0500 | [diff] [blame] | 190 | |
| 191 | @Test |
Lyn Han | 43fbcbd | 2020-06-03 17:43:30 -0700 | [diff] [blame] | 192 | 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 Reynolds | ccc6ae6 | 2018-03-01 16:24:49 -0500 | [diff] [blame] | 206 | 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 Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 252 | } |