blob: 84ef0c9c7a4ae2640ce5fa3a9de6feaaf6a0ba5f [file] [log] [blame]
Julia Reynolds8f488d32016-10-14 10:59:01 -04001/*
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 */
16package com.android.server.notification;
17
Julia Reynoldsa13b3e22017-08-10 16:58:54 -040018import static junit.framework.Assert.assertEquals;
19import static junit.framework.Assert.assertNotNull;
20
Julia Reynolds8f488d32016-10-14 10:59:01 -040021import static org.mockito.Matchers.anyInt;
22import static org.mockito.Matchers.anyString;
23import static org.mockito.Matchers.eq;
24import static org.mockito.Mockito.never;
25import static org.mockito.Mockito.times;
26import static org.mockito.Mockito.verify;
27
28import org.junit.Before;
29import org.junit.Test;
30import org.junit.runner.RunWith;
31import org.mockito.Mock;
Julia Reynoldsf4af65b2016-12-20 17:05:12 -050032import org.mockito.Mockito;
Julia Reynolds8f488d32016-10-14 10:59:01 -040033import org.mockito.MockitoAnnotations;
34
Julia Reynolds8f488d32016-10-14 10:59:01 -040035import android.app.Notification;
Julia Reynolds8f488d32016-10-14 10:59:01 -040036import android.os.UserHandle;
37import android.service.notification.StatusBarNotification;
Julia Reynolds8f488d32016-10-14 10:59:01 -040038import android.support.test.runner.AndroidJUnit4;
39import android.test.suitebuilder.annotation.SmallTest;
40
Jason Monk74f5e362017-12-06 08:56:33 -050041import com.android.server.UiServiceTestCase;
42
Julia Reynolds8f488d32016-10-14 10:59:01 -040043import java.util.ArrayList;
Julia Reynoldsa13b3e22017-08-10 16:58:54 -040044import java.util.LinkedHashSet;
Julia Reynolds8f488d32016-10-14 10:59:01 -040045import java.util.List;
Julia Reynoldsa13b3e22017-08-10 16:58:54 -040046import java.util.Map;
Julia Reynolds8f488d32016-10-14 10:59:01 -040047
48@SmallTest
49@RunWith(AndroidJUnit4.class)
Jason Monk74f5e362017-12-06 08:56:33 -050050public class GroupHelperTest extends UiServiceTestCase {
Julia Reynolds8f488d32016-10-14 10:59:01 -040051 private @Mock GroupHelper.Callback mCallback;
52
Adora Zhang48dd614a82018-06-25 19:18:41 -070053 private final static int AUTOGROUP_AT_COUNT = 4;
Julia Reynolds8f488d32016-10-14 10:59:01 -040054 private GroupHelper mGroupHelper;
55
Julia Reynolds8f488d32016-10-14 10:59:01 -040056 @Before
57 public void setUp() {
58 MockitoAnnotations.initMocks(this);
59
Adora Zhang48dd614a82018-06-25 19:18:41 -070060 mGroupHelper = new GroupHelper(AUTOGROUP_AT_COUNT, mCallback);
Julia Reynolds8f488d32016-10-14 10:59:01 -040061 }
62
63 private StatusBarNotification getSbn(String pkg, int id, String tag,
64 UserHandle user, String groupKey) {
Geoffrey Pitschaf759c52017-02-15 09:35:38 -050065 Notification.Builder nb = new Notification.Builder(getContext(), "test_channel_id")
Julia Reynolds8f488d32016-10-14 10:59:01 -040066 .setContentTitle("A")
67 .setWhen(1205);
68 if (groupKey != null) {
69 nb.setGroup(groupKey);
70 }
Julia Reynolds924eed12017-01-19 09:52:07 -050071 return new StatusBarNotification(pkg, pkg, id, tag, 0, 0, nb.build(), user, null,
Julia Reynolds423b9fc2016-11-09 09:51:08 -050072 System.currentTimeMillis());
Julia Reynolds8f488d32016-10-14 10:59:01 -040073 }
74
75 private StatusBarNotification getSbn(String pkg, int id, String tag,
76 UserHandle user) {
77 return getSbn(pkg, id, tag, user, null);
78 }
79
80 @Test
81 public void testNoGroup_postingUnderLimit() throws Exception {
82 final String pkg = "package";
Adora Zhang48dd614a82018-06-25 19:18:41 -070083 for (int i = 0; i < AUTOGROUP_AT_COUNT - 1; i++) {
Julia Reynoldsa13b3e22017-08-10 16:58:54 -040084 mGroupHelper.onNotificationPosted(getSbn(pkg, i, String.valueOf(i), UserHandle.SYSTEM),
85 false);
Julia Reynolds8f488d32016-10-14 10:59:01 -040086 }
87 verify(mCallback, never()).addAutoGroupSummary(
88 eq(UserHandle.USER_SYSTEM), eq(pkg), anyString());
89 verify(mCallback, never()).addAutoGroup(anyString());
90 verify(mCallback, never()).removeAutoGroup(anyString());
91 verify(mCallback, never()).removeAutoGroupSummary(anyInt(), anyString());
92 }
93
94 @Test
95 public void testNoGroup_multiPackage() throws Exception {
96 final String pkg = "package";
97 final String pkg2 = "package2";
Adora Zhang48dd614a82018-06-25 19:18:41 -070098 for (int i = 0; i < AUTOGROUP_AT_COUNT - 1; i++) {
Julia Reynoldsa13b3e22017-08-10 16:58:54 -040099 mGroupHelper.onNotificationPosted(getSbn(pkg, i, String.valueOf(i), UserHandle.SYSTEM),
100 false);
Julia Reynolds8f488d32016-10-14 10:59:01 -0400101 }
102 mGroupHelper.onNotificationPosted(
Adora Zhang48dd614a82018-06-25 19:18:41 -0700103 getSbn(pkg2, AUTOGROUP_AT_COUNT, "four", UserHandle.SYSTEM), false);
Julia Reynolds8f488d32016-10-14 10:59:01 -0400104 verify(mCallback, never()).addAutoGroupSummary(
105 eq(UserHandle.USER_SYSTEM), eq(pkg), anyString());
106 verify(mCallback, never()).addAutoGroup(anyString());
107 verify(mCallback, never()).removeAutoGroup(anyString());
108 verify(mCallback, never()).removeAutoGroupSummary(anyInt(), anyString());
109 }
110
111 @Test
112 public void testNoGroup_multiUser() throws Exception {
113 final String pkg = "package";
Adora Zhang48dd614a82018-06-25 19:18:41 -0700114 for (int i = 0; i < AUTOGROUP_AT_COUNT - 1; i++) {
Julia Reynoldsa13b3e22017-08-10 16:58:54 -0400115 mGroupHelper.onNotificationPosted(getSbn(pkg, i, String.valueOf(i), UserHandle.SYSTEM),
116 false);
Julia Reynolds8f488d32016-10-14 10:59:01 -0400117 }
118 mGroupHelper.onNotificationPosted(
Adora Zhang48dd614a82018-06-25 19:18:41 -0700119 getSbn(pkg, AUTOGROUP_AT_COUNT, "four", UserHandle.ALL), false);
Julia Reynolds8f488d32016-10-14 10:59:01 -0400120 verify(mCallback, never()).addAutoGroupSummary(anyInt(), eq(pkg), anyString());
121 verify(mCallback, never()).addAutoGroup(anyString());
122 verify(mCallback, never()).removeAutoGroup(anyString());
123 verify(mCallback, never()).removeAutoGroupSummary(anyInt(), anyString());
124 }
125
126 @Test
127 public void testNoGroup_someAreGrouped() throws Exception {
128 final String pkg = "package";
Adora Zhang48dd614a82018-06-25 19:18:41 -0700129 for (int i = 0; i < AUTOGROUP_AT_COUNT - 1; i++) {
Julia Reynoldsa13b3e22017-08-10 16:58:54 -0400130 mGroupHelper.onNotificationPosted(
131 getSbn(pkg, i, String.valueOf(i), UserHandle.SYSTEM), false);
Julia Reynolds8f488d32016-10-14 10:59:01 -0400132 }
133 mGroupHelper.onNotificationPosted(
Adora Zhang48dd614a82018-06-25 19:18:41 -0700134 getSbn(pkg, AUTOGROUP_AT_COUNT, "four", UserHandle.SYSTEM, "a"), false);
Julia Reynolds8f488d32016-10-14 10:59:01 -0400135 verify(mCallback, never()).addAutoGroupSummary(
136 eq(UserHandle.USER_SYSTEM), eq(pkg), anyString());
137 verify(mCallback, never()).addAutoGroup(anyString());
138 verify(mCallback, never()).removeAutoGroup(anyString());
139 verify(mCallback, never()).removeAutoGroupSummary(anyInt(), anyString());
140 }
141
142
143 @Test
144 public void testPostingOverLimit() throws Exception {
145 final String pkg = "package";
Adora Zhang48dd614a82018-06-25 19:18:41 -0700146 for (int i = 0; i < AUTOGROUP_AT_COUNT; i++) {
Julia Reynoldsa13b3e22017-08-10 16:58:54 -0400147 mGroupHelper.onNotificationPosted(
148 getSbn(pkg, i, String.valueOf(i), UserHandle.SYSTEM), false);
Julia Reynolds8f488d32016-10-14 10:59:01 -0400149 }
150 verify(mCallback, times(1)).addAutoGroupSummary(anyInt(), eq(pkg), anyString());
Adora Zhang48dd614a82018-06-25 19:18:41 -0700151 verify(mCallback, times(AUTOGROUP_AT_COUNT)).addAutoGroup(anyString());
Julia Reynolds8f488d32016-10-14 10:59:01 -0400152 verify(mCallback, never()).removeAutoGroup(anyString());
153 verify(mCallback, never()).removeAutoGroupSummary(anyInt(), anyString());
154 }
155
156 @Test
Julia Reynoldsf4af65b2016-12-20 17:05:12 -0500157 public void testDropToZeroRemoveGroup() throws Exception {
Julia Reynolds8f488d32016-10-14 10:59:01 -0400158 final String pkg = "package";
159 List<StatusBarNotification> posted = new ArrayList<>();
Adora Zhang48dd614a82018-06-25 19:18:41 -0700160 for (int i = 0; i < AUTOGROUP_AT_COUNT; i++) {
Julia Reynolds8f488d32016-10-14 10:59:01 -0400161 final StatusBarNotification sbn = getSbn(pkg, i, String.valueOf(i), UserHandle.SYSTEM);
162 posted.add(sbn);
Julia Reynoldsa13b3e22017-08-10 16:58:54 -0400163 mGroupHelper.onNotificationPosted(sbn, false);
Julia Reynolds8f488d32016-10-14 10:59:01 -0400164 }
Julia Reynolds8f488d32016-10-14 10:59:01 -0400165 verify(mCallback, times(1)).addAutoGroupSummary(anyInt(), eq(pkg), anyString());
Adora Zhang48dd614a82018-06-25 19:18:41 -0700166 verify(mCallback, times(AUTOGROUP_AT_COUNT)).addAutoGroup(anyString());
Julia Reynoldsf4af65b2016-12-20 17:05:12 -0500167 verify(mCallback, never()).removeAutoGroup(anyString());
168 verify(mCallback, never()).removeAutoGroupSummary(anyInt(), anyString());
169 Mockito.reset(mCallback);
170
Adora Zhang48dd614a82018-06-25 19:18:41 -0700171 for (int i = 0; i < AUTOGROUP_AT_COUNT - 1; i++) {
Julia Reynoldsf4af65b2016-12-20 17:05:12 -0500172 mGroupHelper.onNotificationRemoved(posted.remove(0));
173 }
174 verify(mCallback, never()).removeAutoGroup(anyString());
175 verify(mCallback, never()).removeAutoGroupSummary(anyInt(), anyString());
176 Mockito.reset(mCallback);
177
178 mGroupHelper.onNotificationRemoved(posted.remove(0));
179 verify(mCallback, never()).removeAutoGroup(anyString());
180 verify(mCallback, times(1)).removeAutoGroupSummary(anyInt(), anyString());
181 }
182
183 @Test
184 public void testAppStartsGrouping() throws Exception {
185 final String pkg = "package";
186 List<StatusBarNotification> posted = new ArrayList<>();
Adora Zhang48dd614a82018-06-25 19:18:41 -0700187 for (int i = 0; i < AUTOGROUP_AT_COUNT; i++) {
Julia Reynoldsf4af65b2016-12-20 17:05:12 -0500188 final StatusBarNotification sbn = getSbn(pkg, i, String.valueOf(i), UserHandle.SYSTEM);
189 posted.add(sbn);
Julia Reynoldsa13b3e22017-08-10 16:58:54 -0400190 mGroupHelper.onNotificationPosted(sbn, false);
Julia Reynoldsf4af65b2016-12-20 17:05:12 -0500191 }
192 verify(mCallback, times(1)).addAutoGroupSummary(anyInt(), eq(pkg), anyString());
Adora Zhang48dd614a82018-06-25 19:18:41 -0700193 verify(mCallback, times(AUTOGROUP_AT_COUNT)).addAutoGroup(anyString());
Julia Reynoldsf4af65b2016-12-20 17:05:12 -0500194 verify(mCallback, never()).removeAutoGroup(anyString());
195 verify(mCallback, never()).removeAutoGroupSummary(anyInt(), anyString());
196 Mockito.reset(mCallback);
197
198 int i = 0;
Adora Zhang48dd614a82018-06-25 19:18:41 -0700199 for (i = 0; i < AUTOGROUP_AT_COUNT - 2; i++) {
Julia Reynoldsf4af65b2016-12-20 17:05:12 -0500200 final StatusBarNotification sbn =
201 getSbn(pkg, i, String.valueOf(i), UserHandle.SYSTEM, "app group");
Julia Reynoldsa13b3e22017-08-10 16:58:54 -0400202 mGroupHelper.onNotificationPosted(sbn, false);
Julia Reynoldsf4af65b2016-12-20 17:05:12 -0500203 }
Adora Zhang48dd614a82018-06-25 19:18:41 -0700204 verify(mCallback, times(AUTOGROUP_AT_COUNT - 2)).removeAutoGroup(anyString());
Julia Reynoldsf4af65b2016-12-20 17:05:12 -0500205 verify(mCallback, never()).removeAutoGroupSummary(anyInt(), anyString());
206 Mockito.reset(mCallback);
207
Adora Zhang48dd614a82018-06-25 19:18:41 -0700208 for (; i < AUTOGROUP_AT_COUNT; i++) {
Julia Reynoldsf4af65b2016-12-20 17:05:12 -0500209 final StatusBarNotification sbn =
210 getSbn(pkg, i, String.valueOf(i), UserHandle.SYSTEM, "app group");
Julia Reynoldsa13b3e22017-08-10 16:58:54 -0400211 mGroupHelper.onNotificationPosted(sbn, false);
Julia Reynoldsf4af65b2016-12-20 17:05:12 -0500212 }
213 verify(mCallback, times(2)).removeAutoGroup(anyString());
Julia Reynolds8f488d32016-10-14 10:59:01 -0400214 verify(mCallback, times(1)).removeAutoGroupSummary(anyInt(), anyString());
215 }
Julia Reynoldsa13b3e22017-08-10 16:58:54 -0400216
217 @Test
218 public void testNewNotificationsAddedToAutogroup_ifOriginalNotificationsCanceled()
219 throws Exception {
220 final String pkg = "package";
221 List<StatusBarNotification> posted = new ArrayList<>();
Adora Zhang48dd614a82018-06-25 19:18:41 -0700222 for (int i = 0; i < AUTOGROUP_AT_COUNT; i++) {
Julia Reynoldsa13b3e22017-08-10 16:58:54 -0400223 final StatusBarNotification sbn = getSbn(pkg, i, String.valueOf(i), UserHandle.SYSTEM);
224 posted.add(sbn);
225 mGroupHelper.onNotificationPosted(sbn, false);
226 }
227 verify(mCallback, times(1)).addAutoGroupSummary(anyInt(), eq(pkg), anyString());
Adora Zhang48dd614a82018-06-25 19:18:41 -0700228 verify(mCallback, times(AUTOGROUP_AT_COUNT)).addAutoGroup(anyString());
Julia Reynoldsa13b3e22017-08-10 16:58:54 -0400229 verify(mCallback, never()).removeAutoGroup(anyString());
230 verify(mCallback, never()).removeAutoGroupSummary(anyInt(), anyString());
231 Mockito.reset(mCallback);
232
233 for (int i = posted.size() - 2; i >= 0; i--) {
234 mGroupHelper.onNotificationRemoved(posted.remove(i));
235 }
236 verify(mCallback, never()).removeAutoGroup(anyString());
237 verify(mCallback, never()).removeAutoGroupSummary(anyInt(), anyString());
238 Mockito.reset(mCallback);
239
240 // only one child remains
241 Map<String, LinkedHashSet<String>> ungroupedForUser =
242 mGroupHelper.mUngroupedNotifications.get(UserHandle.USER_SYSTEM);
243 assertNotNull(ungroupedForUser);
244 assertEquals(1, ungroupedForUser.get(pkg).size());
245
246 // Add new notification; it should be autogrouped even though the total count is
247 // < AUTOGROUP_AT_COUNT
248 final StatusBarNotification sbn = getSbn(pkg, 5, String.valueOf(5), UserHandle.SYSTEM);
249 posted.add(sbn);
250 mGroupHelper.onNotificationPosted(sbn, true);
251 verify(mCallback, times(posted.size())).addAutoGroup(anyString());
252 verify(mCallback, never()).removeAutoGroup(anyString());
253 verify(mCallback, never()).removeAutoGroupSummary(anyInt(), anyString());
254 }
Julia Reynolds8f488d32016-10-14 10:59:01 -0400255}