Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | |
Julia Reynolds | a13b3e2 | 2017-08-10 16:58:54 -0400 | [diff] [blame] | 18 | import static junit.framework.Assert.assertEquals; |
| 19 | import static junit.framework.Assert.assertNotNull; |
| 20 | |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 21 | import static org.mockito.Matchers.anyInt; |
| 22 | import static org.mockito.Matchers.anyString; |
| 23 | import static org.mockito.Matchers.eq; |
| 24 | import static org.mockito.Mockito.never; |
| 25 | import static org.mockito.Mockito.times; |
| 26 | import static org.mockito.Mockito.verify; |
| 27 | |
| 28 | import org.junit.Before; |
| 29 | import org.junit.Test; |
| 30 | import org.junit.runner.RunWith; |
| 31 | import org.mockito.Mock; |
Julia Reynolds | f4af65b | 2016-12-20 17:05:12 -0500 | [diff] [blame] | 32 | import org.mockito.Mockito; |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 33 | import org.mockito.MockitoAnnotations; |
| 34 | |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 35 | import android.app.Notification; |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 36 | import android.os.UserHandle; |
| 37 | import android.service.notification.StatusBarNotification; |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 38 | import android.support.test.runner.AndroidJUnit4; |
| 39 | import android.test.suitebuilder.annotation.SmallTest; |
| 40 | |
| 41 | import java.util.ArrayList; |
Julia Reynolds | a13b3e2 | 2017-08-10 16:58:54 -0400 | [diff] [blame] | 42 | import java.util.LinkedHashSet; |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 43 | import java.util.List; |
Julia Reynolds | a13b3e2 | 2017-08-10 16:58:54 -0400 | [diff] [blame] | 44 | import java.util.Map; |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 45 | |
| 46 | @SmallTest |
| 47 | @RunWith(AndroidJUnit4.class) |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 48 | public class GroupHelperTest extends NotificationTestCase { |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 49 | private @Mock GroupHelper.Callback mCallback; |
| 50 | |
| 51 | private GroupHelper mGroupHelper; |
| 52 | |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 53 | @Before |
| 54 | public void setUp() { |
| 55 | MockitoAnnotations.initMocks(this); |
| 56 | |
| 57 | mGroupHelper = new GroupHelper(mCallback); |
| 58 | } |
| 59 | |
| 60 | private StatusBarNotification getSbn(String pkg, int id, String tag, |
| 61 | UserHandle user, String groupKey) { |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 62 | Notification.Builder nb = new Notification.Builder(getContext(), "test_channel_id") |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 63 | .setContentTitle("A") |
| 64 | .setWhen(1205); |
| 65 | if (groupKey != null) { |
| 66 | nb.setGroup(groupKey); |
| 67 | } |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 68 | return new StatusBarNotification(pkg, pkg, id, tag, 0, 0, nb.build(), user, null, |
Julia Reynolds | 423b9fc | 2016-11-09 09:51:08 -0500 | [diff] [blame] | 69 | System.currentTimeMillis()); |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | private StatusBarNotification getSbn(String pkg, int id, String tag, |
| 73 | UserHandle user) { |
| 74 | return getSbn(pkg, id, tag, user, null); |
| 75 | } |
| 76 | |
| 77 | @Test |
| 78 | public void testNoGroup_postingUnderLimit() throws Exception { |
| 79 | final String pkg = "package"; |
| 80 | for (int i = 0; i < GroupHelper.AUTOGROUP_AT_COUNT - 1; i++) { |
Julia Reynolds | a13b3e2 | 2017-08-10 16:58:54 -0400 | [diff] [blame] | 81 | mGroupHelper.onNotificationPosted(getSbn(pkg, i, String.valueOf(i), UserHandle.SYSTEM), |
| 82 | false); |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 83 | } |
| 84 | verify(mCallback, never()).addAutoGroupSummary( |
| 85 | eq(UserHandle.USER_SYSTEM), eq(pkg), anyString()); |
| 86 | verify(mCallback, never()).addAutoGroup(anyString()); |
| 87 | verify(mCallback, never()).removeAutoGroup(anyString()); |
| 88 | verify(mCallback, never()).removeAutoGroupSummary(anyInt(), anyString()); |
| 89 | } |
| 90 | |
| 91 | @Test |
| 92 | public void testNoGroup_multiPackage() throws Exception { |
| 93 | final String pkg = "package"; |
| 94 | final String pkg2 = "package2"; |
| 95 | for (int i = 0; i < GroupHelper.AUTOGROUP_AT_COUNT - 1; i++) { |
Julia Reynolds | a13b3e2 | 2017-08-10 16:58:54 -0400 | [diff] [blame] | 96 | mGroupHelper.onNotificationPosted(getSbn(pkg, i, String.valueOf(i), UserHandle.SYSTEM), |
| 97 | false); |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 98 | } |
| 99 | mGroupHelper.onNotificationPosted( |
Julia Reynolds | a13b3e2 | 2017-08-10 16:58:54 -0400 | [diff] [blame] | 100 | getSbn(pkg2, GroupHelper.AUTOGROUP_AT_COUNT, "four", UserHandle.SYSTEM), false); |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 101 | verify(mCallback, never()).addAutoGroupSummary( |
| 102 | eq(UserHandle.USER_SYSTEM), eq(pkg), anyString()); |
| 103 | verify(mCallback, never()).addAutoGroup(anyString()); |
| 104 | verify(mCallback, never()).removeAutoGroup(anyString()); |
| 105 | verify(mCallback, never()).removeAutoGroupSummary(anyInt(), anyString()); |
| 106 | } |
| 107 | |
| 108 | @Test |
| 109 | public void testNoGroup_multiUser() throws Exception { |
| 110 | final String pkg = "package"; |
| 111 | for (int i = 0; i < GroupHelper.AUTOGROUP_AT_COUNT - 1; i++) { |
Julia Reynolds | a13b3e2 | 2017-08-10 16:58:54 -0400 | [diff] [blame] | 112 | mGroupHelper.onNotificationPosted(getSbn(pkg, i, String.valueOf(i), UserHandle.SYSTEM), |
| 113 | false); |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 114 | } |
| 115 | mGroupHelper.onNotificationPosted( |
Julia Reynolds | a13b3e2 | 2017-08-10 16:58:54 -0400 | [diff] [blame] | 116 | getSbn(pkg, GroupHelper.AUTOGROUP_AT_COUNT, "four", UserHandle.ALL), |
| 117 | false); |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 118 | verify(mCallback, never()).addAutoGroupSummary(anyInt(), eq(pkg), anyString()); |
| 119 | verify(mCallback, never()).addAutoGroup(anyString()); |
| 120 | verify(mCallback, never()).removeAutoGroup(anyString()); |
| 121 | verify(mCallback, never()).removeAutoGroupSummary(anyInt(), anyString()); |
| 122 | } |
| 123 | |
| 124 | @Test |
| 125 | public void testNoGroup_someAreGrouped() throws Exception { |
| 126 | final String pkg = "package"; |
| 127 | for (int i = 0; i < GroupHelper.AUTOGROUP_AT_COUNT - 1; i++) { |
Julia Reynolds | a13b3e2 | 2017-08-10 16:58:54 -0400 | [diff] [blame] | 128 | mGroupHelper.onNotificationPosted( |
| 129 | getSbn(pkg, i, String.valueOf(i), UserHandle.SYSTEM), false); |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 130 | } |
| 131 | mGroupHelper.onNotificationPosted( |
Julia Reynolds | a13b3e2 | 2017-08-10 16:58:54 -0400 | [diff] [blame] | 132 | getSbn(pkg, GroupHelper.AUTOGROUP_AT_COUNT, "four", UserHandle.SYSTEM, "a"), |
| 133 | false); |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 134 | verify(mCallback, never()).addAutoGroupSummary( |
| 135 | eq(UserHandle.USER_SYSTEM), eq(pkg), anyString()); |
| 136 | verify(mCallback, never()).addAutoGroup(anyString()); |
| 137 | verify(mCallback, never()).removeAutoGroup(anyString()); |
| 138 | verify(mCallback, never()).removeAutoGroupSummary(anyInt(), anyString()); |
| 139 | } |
| 140 | |
| 141 | |
| 142 | @Test |
| 143 | public void testPostingOverLimit() throws Exception { |
| 144 | final String pkg = "package"; |
| 145 | for (int i = 0; i < GroupHelper.AUTOGROUP_AT_COUNT; i++) { |
Julia Reynolds | a13b3e2 | 2017-08-10 16:58:54 -0400 | [diff] [blame] | 146 | mGroupHelper.onNotificationPosted( |
| 147 | getSbn(pkg, i, String.valueOf(i), UserHandle.SYSTEM), false); |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 148 | } |
| 149 | verify(mCallback, times(1)).addAutoGroupSummary(anyInt(), eq(pkg), anyString()); |
| 150 | verify(mCallback, times(GroupHelper.AUTOGROUP_AT_COUNT)).addAutoGroup(anyString()); |
| 151 | verify(mCallback, never()).removeAutoGroup(anyString()); |
| 152 | verify(mCallback, never()).removeAutoGroupSummary(anyInt(), anyString()); |
| 153 | } |
| 154 | |
| 155 | @Test |
Julia Reynolds | f4af65b | 2016-12-20 17:05:12 -0500 | [diff] [blame] | 156 | public void testDropToZeroRemoveGroup() throws Exception { |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 157 | final String pkg = "package"; |
| 158 | List<StatusBarNotification> posted = new ArrayList<>(); |
| 159 | for (int i = 0; i < GroupHelper.AUTOGROUP_AT_COUNT; i++) { |
| 160 | final StatusBarNotification sbn = getSbn(pkg, i, String.valueOf(i), UserHandle.SYSTEM); |
| 161 | posted.add(sbn); |
Julia Reynolds | a13b3e2 | 2017-08-10 16:58:54 -0400 | [diff] [blame] | 162 | mGroupHelper.onNotificationPosted(sbn, false); |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 163 | } |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 164 | verify(mCallback, times(1)).addAutoGroupSummary(anyInt(), eq(pkg), anyString()); |
| 165 | verify(mCallback, times(GroupHelper.AUTOGROUP_AT_COUNT)).addAutoGroup(anyString()); |
Julia Reynolds | f4af65b | 2016-12-20 17:05:12 -0500 | [diff] [blame] | 166 | verify(mCallback, never()).removeAutoGroup(anyString()); |
| 167 | verify(mCallback, never()).removeAutoGroupSummary(anyInt(), anyString()); |
| 168 | Mockito.reset(mCallback); |
| 169 | |
| 170 | for (int i = 0; i < GroupHelper.AUTOGROUP_AT_COUNT - 1; i++) { |
| 171 | mGroupHelper.onNotificationRemoved(posted.remove(0)); |
| 172 | } |
| 173 | verify(mCallback, never()).removeAutoGroup(anyString()); |
| 174 | verify(mCallback, never()).removeAutoGroupSummary(anyInt(), anyString()); |
| 175 | Mockito.reset(mCallback); |
| 176 | |
| 177 | mGroupHelper.onNotificationRemoved(posted.remove(0)); |
| 178 | verify(mCallback, never()).removeAutoGroup(anyString()); |
| 179 | verify(mCallback, times(1)).removeAutoGroupSummary(anyInt(), anyString()); |
| 180 | } |
| 181 | |
| 182 | @Test |
| 183 | public void testAppStartsGrouping() throws Exception { |
| 184 | final String pkg = "package"; |
| 185 | List<StatusBarNotification> posted = new ArrayList<>(); |
| 186 | for (int i = 0; i < GroupHelper.AUTOGROUP_AT_COUNT; i++) { |
| 187 | final StatusBarNotification sbn = getSbn(pkg, i, String.valueOf(i), UserHandle.SYSTEM); |
| 188 | posted.add(sbn); |
Julia Reynolds | a13b3e2 | 2017-08-10 16:58:54 -0400 | [diff] [blame] | 189 | mGroupHelper.onNotificationPosted(sbn, false); |
Julia Reynolds | f4af65b | 2016-12-20 17:05:12 -0500 | [diff] [blame] | 190 | } |
| 191 | verify(mCallback, times(1)).addAutoGroupSummary(anyInt(), eq(pkg), anyString()); |
| 192 | verify(mCallback, times(GroupHelper.AUTOGROUP_AT_COUNT)).addAutoGroup(anyString()); |
| 193 | verify(mCallback, never()).removeAutoGroup(anyString()); |
| 194 | verify(mCallback, never()).removeAutoGroupSummary(anyInt(), anyString()); |
| 195 | Mockito.reset(mCallback); |
| 196 | |
| 197 | int i = 0; |
| 198 | for (i = 0; i < GroupHelper.AUTOGROUP_AT_COUNT - 2; i++) { |
| 199 | final StatusBarNotification sbn = |
| 200 | getSbn(pkg, i, String.valueOf(i), UserHandle.SYSTEM, "app group"); |
Julia Reynolds | a13b3e2 | 2017-08-10 16:58:54 -0400 | [diff] [blame] | 201 | mGroupHelper.onNotificationPosted(sbn, false); |
Julia Reynolds | f4af65b | 2016-12-20 17:05:12 -0500 | [diff] [blame] | 202 | } |
| 203 | verify(mCallback, times(GroupHelper.AUTOGROUP_AT_COUNT - 2)).removeAutoGroup(anyString()); |
| 204 | verify(mCallback, never()).removeAutoGroupSummary(anyInt(), anyString()); |
| 205 | Mockito.reset(mCallback); |
| 206 | |
| 207 | for (; i < GroupHelper.AUTOGROUP_AT_COUNT; i++) { |
| 208 | final StatusBarNotification sbn = |
| 209 | getSbn(pkg, i, String.valueOf(i), UserHandle.SYSTEM, "app group"); |
Julia Reynolds | a13b3e2 | 2017-08-10 16:58:54 -0400 | [diff] [blame] | 210 | mGroupHelper.onNotificationPosted(sbn, false); |
Julia Reynolds | f4af65b | 2016-12-20 17:05:12 -0500 | [diff] [blame] | 211 | } |
| 212 | verify(mCallback, times(2)).removeAutoGroup(anyString()); |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 213 | verify(mCallback, times(1)).removeAutoGroupSummary(anyInt(), anyString()); |
| 214 | } |
Julia Reynolds | a13b3e2 | 2017-08-10 16:58:54 -0400 | [diff] [blame] | 215 | |
| 216 | @Test |
| 217 | public void testNewNotificationsAddedToAutogroup_ifOriginalNotificationsCanceled() |
| 218 | throws Exception { |
| 219 | final String pkg = "package"; |
| 220 | List<StatusBarNotification> posted = new ArrayList<>(); |
| 221 | for (int i = 0; i < GroupHelper.AUTOGROUP_AT_COUNT; i++) { |
| 222 | final StatusBarNotification sbn = getSbn(pkg, i, String.valueOf(i), UserHandle.SYSTEM); |
| 223 | posted.add(sbn); |
| 224 | mGroupHelper.onNotificationPosted(sbn, false); |
| 225 | } |
| 226 | verify(mCallback, times(1)).addAutoGroupSummary(anyInt(), eq(pkg), anyString()); |
| 227 | verify(mCallback, times(GroupHelper.AUTOGROUP_AT_COUNT)).addAutoGroup(anyString()); |
| 228 | verify(mCallback, never()).removeAutoGroup(anyString()); |
| 229 | verify(mCallback, never()).removeAutoGroupSummary(anyInt(), anyString()); |
| 230 | Mockito.reset(mCallback); |
| 231 | |
| 232 | for (int i = posted.size() - 2; i >= 0; i--) { |
| 233 | mGroupHelper.onNotificationRemoved(posted.remove(i)); |
| 234 | } |
| 235 | verify(mCallback, never()).removeAutoGroup(anyString()); |
| 236 | verify(mCallback, never()).removeAutoGroupSummary(anyInt(), anyString()); |
| 237 | Mockito.reset(mCallback); |
| 238 | |
| 239 | // only one child remains |
| 240 | Map<String, LinkedHashSet<String>> ungroupedForUser = |
| 241 | mGroupHelper.mUngroupedNotifications.get(UserHandle.USER_SYSTEM); |
| 242 | assertNotNull(ungroupedForUser); |
| 243 | assertEquals(1, ungroupedForUser.get(pkg).size()); |
| 244 | |
| 245 | // Add new notification; it should be autogrouped even though the total count is |
| 246 | // < AUTOGROUP_AT_COUNT |
| 247 | final StatusBarNotification sbn = getSbn(pkg, 5, String.valueOf(5), UserHandle.SYSTEM); |
| 248 | posted.add(sbn); |
| 249 | mGroupHelper.onNotificationPosted(sbn, true); |
| 250 | verify(mCallback, times(posted.size())).addAutoGroup(anyString()); |
| 251 | verify(mCallback, never()).removeAutoGroup(anyString()); |
| 252 | verify(mCallback, never()).removeAutoGroupSummary(anyInt(), anyString()); |
| 253 | } |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 254 | } |