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 | 8091d9e | 2019-02-26 14:52:33 -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; |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 32 | import android.os.UserHandle; |
| 33 | import android.service.notification.StatusBarNotification; |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 34 | import android.test.suitebuilder.annotation.SmallTest; |
| 35 | |
Brett Chabot | 8091d9e | 2019-02-26 14:52:33 -0800 | [diff] [blame^] | 36 | import androidx.test.runner.AndroidJUnit4; |
| 37 | |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 38 | import com.android.server.UiServiceTestCase; |
| 39 | |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 40 | import org.junit.Before; |
| 41 | import org.junit.Test; |
| 42 | import org.junit.runner.RunWith; |
| 43 | import org.mockito.Mock; |
| 44 | import org.mockito.MockitoAnnotations; |
| 45 | |
| 46 | @SmallTest |
| 47 | @RunWith(AndroidJUnit4.class) |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 48 | public class BadgeExtractorTest extends UiServiceTestCase { |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 49 | |
| 50 | @Mock RankingConfig mConfig; |
| 51 | |
| 52 | private String mPkg = "com.android.server.notification"; |
| 53 | private int mId = 1001; |
| 54 | private String mTag = null; |
| 55 | private int mUid = 1000; |
| 56 | private int mPid = 2000; |
| 57 | private UserHandle mUser = UserHandle.of(ActivityManager.getCurrentUser()); |
| 58 | |
| 59 | @Before |
| 60 | public void setUp() { |
| 61 | MockitoAnnotations.initMocks(this); |
| 62 | } |
| 63 | |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 64 | private NotificationRecord getNotificationRecord(boolean showBadge, int importanceHigh) { |
| 65 | NotificationChannel channel = new NotificationChannel("a", "a", importanceHigh); |
| 66 | channel.setShowBadge(showBadge); |
| 67 | when(mConfig.getNotificationChannel(mPkg, mUid, "a", false)).thenReturn(channel); |
| 68 | |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 69 | final Builder builder = new Builder(getContext()) |
| 70 | .setContentTitle("foo") |
| 71 | .setSmallIcon(android.R.drawable.sym_def_app_icon) |
| 72 | .setPriority(Notification.PRIORITY_HIGH) |
| 73 | .setDefaults(Notification.DEFAULT_SOUND); |
| 74 | |
| 75 | Notification n = builder.build(); |
| 76 | StatusBarNotification sbn = new StatusBarNotification(mPkg, mPkg, mId, mTag, mUid, |
| 77 | mPid, n, mUser, null, System.currentTimeMillis()); |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 78 | NotificationRecord r = new NotificationRecord(getContext(), sbn, channel); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 79 | return r; |
| 80 | } |
| 81 | |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 82 | // |
| 83 | // Tests |
| 84 | // |
| 85 | |
| 86 | @Test |
| 87 | public void testAppYesChannelNo() throws Exception { |
| 88 | BadgeExtractor extractor = new BadgeExtractor(); |
| 89 | extractor.setConfig(mConfig); |
| 90 | |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 91 | when(mConfig.badgingEnabled(mUser)).thenReturn(true); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 92 | when(mConfig.canShowBadge(mPkg, mUid)).thenReturn(true); |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 93 | NotificationRecord r = getNotificationRecord(false, IMPORTANCE_UNSPECIFIED); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 94 | |
| 95 | extractor.process(r); |
| 96 | |
| 97 | assertFalse(r.canShowBadge()); |
| 98 | } |
| 99 | |
| 100 | @Test |
| 101 | public void testAppNoChannelYes() throws Exception { |
| 102 | BadgeExtractor extractor = new BadgeExtractor(); |
| 103 | extractor.setConfig(mConfig); |
| 104 | |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 105 | when(mConfig.badgingEnabled(mUser)).thenReturn(true); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 106 | when(mConfig.canShowBadge(mPkg, mUid)).thenReturn(false); |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 107 | NotificationRecord r = getNotificationRecord(true, IMPORTANCE_HIGH); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 108 | |
| 109 | extractor.process(r); |
| 110 | |
| 111 | assertFalse(r.canShowBadge()); |
| 112 | } |
| 113 | |
| 114 | @Test |
| 115 | public void testAppYesChannelYes() throws Exception { |
| 116 | BadgeExtractor extractor = new BadgeExtractor(); |
| 117 | extractor.setConfig(mConfig); |
| 118 | |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 119 | when(mConfig.badgingEnabled(mUser)).thenReturn(true); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 120 | when(mConfig.canShowBadge(mPkg, mUid)).thenReturn(true); |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 121 | NotificationRecord r = getNotificationRecord(true, IMPORTANCE_UNSPECIFIED); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 122 | |
| 123 | extractor.process(r); |
| 124 | |
| 125 | assertTrue(r.canShowBadge()); |
| 126 | } |
| 127 | |
| 128 | @Test |
| 129 | public void testAppNoChannelNo() throws Exception { |
| 130 | BadgeExtractor extractor = new BadgeExtractor(); |
| 131 | extractor.setConfig(mConfig); |
| 132 | |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 133 | when(mConfig.badgingEnabled(mUser)).thenReturn(true); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 134 | when(mConfig.canShowBadge(mPkg, mUid)).thenReturn(false); |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 135 | NotificationRecord r = getNotificationRecord(false, IMPORTANCE_UNSPECIFIED); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 136 | |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 137 | extractor.process(r); |
| 138 | |
| 139 | assertFalse(r.canShowBadge()); |
| 140 | } |
| 141 | |
| 142 | @Test |
| 143 | public void testAppYesChannelYesUserNo() throws Exception { |
| 144 | BadgeExtractor extractor = new BadgeExtractor(); |
| 145 | extractor.setConfig(mConfig); |
| 146 | |
| 147 | when(mConfig.badgingEnabled(mUser)).thenReturn(false); |
| 148 | when(mConfig.canShowBadge(mPkg, mUid)).thenReturn(true); |
| 149 | NotificationRecord r = getNotificationRecord(true, IMPORTANCE_HIGH); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 150 | |
| 151 | extractor.process(r); |
| 152 | |
| 153 | assertFalse(r.canShowBadge()); |
| 154 | } |
Julia Reynolds | ccc6ae6 | 2018-03-01 16:24:49 -0500 | [diff] [blame] | 155 | |
| 156 | @Test |
| 157 | public void testDndOverridesYes() { |
| 158 | BadgeExtractor extractor = new BadgeExtractor(); |
| 159 | extractor.setConfig(mConfig); |
| 160 | |
| 161 | when(mConfig.badgingEnabled(mUser)).thenReturn(true); |
| 162 | when(mConfig.canShowBadge(mPkg, mUid)).thenReturn(true); |
| 163 | NotificationRecord r = getNotificationRecord(true, IMPORTANCE_UNSPECIFIED); |
| 164 | r.setIntercepted(true); |
| 165 | r.setSuppressedVisualEffects(SUPPRESSED_EFFECT_BADGE); |
| 166 | |
| 167 | extractor.process(r); |
| 168 | |
| 169 | assertFalse(r.canShowBadge()); |
| 170 | } |
| 171 | |
| 172 | @Test |
| 173 | public void testDndOConsidersInterception() { |
| 174 | BadgeExtractor extractor = new BadgeExtractor(); |
| 175 | extractor.setConfig(mConfig); |
| 176 | |
| 177 | when(mConfig.badgingEnabled(mUser)).thenReturn(true); |
| 178 | when(mConfig.canShowBadge(mPkg, mUid)).thenReturn(true); |
| 179 | NotificationRecord r = getNotificationRecord(true, IMPORTANCE_UNSPECIFIED); |
| 180 | r.setIntercepted(false); |
| 181 | r.setSuppressedVisualEffects(SUPPRESSED_EFFECT_BADGE); |
| 182 | |
| 183 | extractor.process(r); |
| 184 | |
| 185 | assertTrue(r.canShowBadge()); |
| 186 | } |
| 187 | |
| 188 | @Test |
| 189 | public void testDndConsidersSuppressedVisualEffects() { |
| 190 | BadgeExtractor extractor = new BadgeExtractor(); |
| 191 | extractor.setConfig(mConfig); |
| 192 | |
| 193 | when(mConfig.badgingEnabled(mUser)).thenReturn(true); |
| 194 | when(mConfig.canShowBadge(mPkg, mUid)).thenReturn(true); |
| 195 | NotificationRecord r = getNotificationRecord(true, IMPORTANCE_UNSPECIFIED); |
| 196 | r.setIntercepted(true); |
| 197 | r.setSuppressedVisualEffects(SUPPRESSED_EFFECT_LIGHTS); |
| 198 | |
| 199 | extractor.process(r); |
| 200 | |
| 201 | assertTrue(r.canShowBadge()); |
| 202 | } |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 203 | } |