blob: ab207f83a70952ab6db9e0fe48515f9a2c791e07 [file] [log] [blame]
Selim Cinek6fd06b52017-03-07 15:54:10 -08001/*
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
17package com.android.systemui.statusbar;
18
Mady Mellorfc02cc32019-04-01 14:47:55 -070019import static android.app.Notification.FLAG_BUBBLE;
Julia Reynolds0abae112018-06-08 10:36:57 -040020import static android.app.NotificationManager.IMPORTANCE_DEFAULT;
Mady Mellorb4991e62019-01-10 15:14:51 -080021import static android.app.NotificationManager.IMPORTANCE_HIGH;
Julia Reynolds0abae112018-06-08 10:36:57 -040022
Selim Cinekc3fec682019-06-06 18:11:07 -070023import static org.mockito.Mockito.mock;
24
Rohan Shah524cf7b2018-03-15 14:40:02 -070025import android.annotation.Nullable;
Selim Cinek6fd06b52017-03-07 15:54:10 -080026import android.app.ActivityManager;
Selim Cinek5ba22542017-04-20 15:16:10 -070027import android.app.Instrumentation;
Selim Cinek6fd06b52017-03-07 15:54:10 -080028import android.app.Notification;
Mark Renouf08bc42a2019-03-07 13:01:59 -050029import android.app.Notification.BubbleMetadata;
Julia Reynolds0abae112018-06-08 10:36:57 -040030import android.app.NotificationChannel;
Mady Mellorb4991e62019-01-10 15:14:51 -080031import android.app.PendingIntent;
Selim Cinek6fd06b52017-03-07 15:54:10 -080032import android.content.Context;
Mady Mellorb4991e62019-01-10 15:14:51 -080033import android.content.Intent;
Mady Mellorc39b4ae2019-01-09 17:11:37 -080034import android.graphics.drawable.Icon;
Selim Cinek6fd06b52017-03-07 15:54:10 -080035import android.os.UserHandle;
36import android.service.notification.StatusBarNotification;
Rohan Shah524cf7b2018-03-15 14:40:02 -070037import android.text.TextUtils;
Selim Cinek6fd06b52017-03-07 15:54:10 -080038import android.view.LayoutInflater;
39import android.widget.RemoteViews;
40
Brett Chabot84151d92019-02-27 15:37:59 -080041import androidx.test.InstrumentationRegistry;
42
Mark Renoufa9fbb6e2019-02-11 12:48:22 -050043import com.android.systemui.bubbles.BubblesTestActivity;
Selim Cinekc3fec682019-06-06 18:11:07 -070044import com.android.systemui.plugins.statusbar.StatusBarStateController;
Ned Burnsf81c4c42019-01-07 14:10:43 -050045import com.android.systemui.statusbar.notification.collection.NotificationEntry;
Mady Mellorebdbbb92018-11-15 14:36:48 -080046import com.android.systemui.statusbar.notification.row.ExpandableNotificationRow;
Ned Burns1a5e22f2019-02-14 15:11:52 -050047import com.android.systemui.statusbar.notification.row.NotificationContentInflater.InflationFlag;
48import com.android.systemui.statusbar.notification.row.NotificationContentInflaterTest;
yoshiki iguchi4e30e762018-02-06 12:09:23 +090049import com.android.systemui.statusbar.phone.HeadsUpManagerPhone;
Selim Cinekd21232e2019-06-20 14:15:59 -070050import com.android.systemui.statusbar.phone.KeyguardBypassController;
Selim Cinek6fd06b52017-03-07 15:54:10 -080051import com.android.systemui.statusbar.phone.NotificationGroupManager;
Sunny Goyal87fccf02019-08-13 17:39:10 -070052import com.android.systemui.tests.R;
Selim Cinek6fd06b52017-03-07 15:54:10 -080053
54/**
Rohan Shah524cf7b2018-03-15 14:40:02 -070055 * A helper class to create {@link ExpandableNotificationRow} (for both individual and group
56 * notifications).
Selim Cinek6fd06b52017-03-07 15:54:10 -080057 */
58public class NotificationTestHelper {
59
Rohan Shah20790b82018-07-02 17:21:04 -070060 /** Package name for testing purposes. */
61 public static final String PKG = "com.android.systemui";
62 /** System UI id for testing purposes. */
63 public static final int UID = 1000;
Yohei Yukawa72769462019-01-20 09:28:08 -080064 /** Current {@link UserHandle} of the system. */
65 public static final UserHandle USER_HANDLE = UserHandle.of(ActivityManager.getCurrentUser());
Rohan Shah20790b82018-07-02 17:21:04 -070066
Rohan Shah524cf7b2018-03-15 14:40:02 -070067 private static final String GROUP_KEY = "gruKey";
68
Selim Cinek6fd06b52017-03-07 15:54:10 -080069 private final Context mContext;
Selim Cinek5ba22542017-04-20 15:16:10 -070070 private final Instrumentation mInstrumentation;
Selim Cinek6fd06b52017-03-07 15:54:10 -080071 private int mId;
Selim Cinekc3fec682019-06-06 18:11:07 -070072 private final NotificationGroupManager mGroupManager;
Selim Cinek5ba22542017-04-20 15:16:10 -070073 private ExpandableNotificationRow mRow;
Selim Cinekc3fec682019-06-06 18:11:07 -070074 private HeadsUpManagerPhone mHeadsUpManager;
Selim Cinek6fd06b52017-03-07 15:54:10 -080075
76 public NotificationTestHelper(Context context) {
77 mContext = context;
Selim Cinek5ba22542017-04-20 15:16:10 -070078 mInstrumentation = InstrumentationRegistry.getInstrumentation();
Selim Cinekc3fec682019-06-06 18:11:07 -070079 StatusBarStateController stateController = mock(StatusBarStateController.class);
80 mGroupManager = new NotificationGroupManager(stateController);
Selim Cinekd21232e2019-06-20 14:15:59 -070081 mHeadsUpManager = new HeadsUpManagerPhone(mContext, stateController,
82 mock(KeyguardBypassController.class));
Selim Cinekc3fec682019-06-06 18:11:07 -070083 mHeadsUpManager.setUp(null, mGroupManager, null, null);
Kevina97ea052018-09-11 13:53:18 -070084 mGroupManager.setHeadsUpManager(mHeadsUpManager);
Selim Cinek6fd06b52017-03-07 15:54:10 -080085 }
86
Kevind4660b22018-09-27 10:57:35 -070087 /**
88 * Creates a generic row.
89 *
90 * @return a generic row with no special properties.
91 * @throws Exception
92 */
Rohan Shah524cf7b2018-03-15 14:40:02 -070093 public ExpandableNotificationRow createRow() throws Exception {
Yohei Yukawa72769462019-01-20 09:28:08 -080094 return createRow(PKG, UID, USER_HANDLE);
Rohan Shah524cf7b2018-03-15 14:40:02 -070095 }
96
Kevind4660b22018-09-27 10:57:35 -070097 /**
98 * Create a row with the package and user id specified.
99 *
100 * @param pkg package
101 * @param uid user id
102 * @return a row with a notification using the package and user id
103 * @throws Exception
104 */
Yohei Yukawa72769462019-01-20 09:28:08 -0800105 public ExpandableNotificationRow createRow(String pkg, int uid, UserHandle userHandle)
106 throws Exception {
107 return createRow(pkg, uid, userHandle, false /* isGroupSummary */, null /* groupKey */);
Rohan Shah524cf7b2018-03-15 14:40:02 -0700108 }
109
Kevind4660b22018-09-27 10:57:35 -0700110 /**
111 * Creates a row based off the notification given.
112 *
113 * @param notification the notification
114 * @return a row built off the notification
115 * @throws Exception
116 */
Rohan Shah524cf7b2018-03-15 14:40:02 -0700117 public ExpandableNotificationRow createRow(Notification notification) throws Exception {
Yohei Yukawa72769462019-01-20 09:28:08 -0800118 return generateRow(notification, PKG, UID, USER_HANDLE, 0 /* extraInflationFlags */);
Kevind4660b22018-09-27 10:57:35 -0700119 }
120
121 /**
122 * Create a row with the specified content views inflated in addition to the default.
123 *
124 * @param extraInflationFlags the flags corresponding to the additional content views that
125 * should be inflated
126 * @return a row with the specified content views inflated in addition to the default
127 * @throws Exception
128 */
129 public ExpandableNotificationRow createRow(@InflationFlag int extraInflationFlags)
130 throws Exception {
Yohei Yukawa72769462019-01-20 09:28:08 -0800131 return generateRow(createNotification(), PKG, UID, USER_HANDLE, extraInflationFlags);
Rohan Shah524cf7b2018-03-15 14:40:02 -0700132 }
133
134 /**
135 * Returns an {@link ExpandableNotificationRow} group with the given number of child
136 * notifications.
137 */
138 public ExpandableNotificationRow createGroup(int numChildren) throws Exception {
139 ExpandableNotificationRow row = createGroupSummary(GROUP_KEY);
140 for (int i = 0; i < numChildren; i++) {
141 ExpandableNotificationRow childRow = createGroupChild(GROUP_KEY);
142 row.addChildNotification(childRow);
143 }
144 return row;
145 }
146
147 /** Returns a group notification with 2 child notifications. */
148 public ExpandableNotificationRow createGroup() throws Exception {
149 return createGroup(2);
150 }
151
152 private ExpandableNotificationRow createGroupSummary(String groupkey) throws Exception {
Yohei Yukawa72769462019-01-20 09:28:08 -0800153 return createRow(PKG, UID, USER_HANDLE, true /* isGroupSummary */, groupkey);
Rohan Shah524cf7b2018-03-15 14:40:02 -0700154 }
155
156 private ExpandableNotificationRow createGroupChild(String groupkey) throws Exception {
Yohei Yukawa72769462019-01-20 09:28:08 -0800157 return createRow(PKG, UID, USER_HANDLE, false /* isGroupSummary */, groupkey);
Mady Mellorb4991e62019-01-10 15:14:51 -0800158 }
159
160 /**
161 * Returns an {@link ExpandableNotificationRow} that should be shown as a bubble.
Mark Renouffec45da2019-03-13 13:24:27 -0400162 */
163 public ExpandableNotificationRow createBubble()
164 throws Exception {
Mady Mellorfc02cc32019-04-01 14:47:55 -0700165 return createBubble(makeBubbleMetadata(null), PKG);
Mark Renouffec45da2019-03-13 13:24:27 -0400166 }
167
168 /**
169 * Returns an {@link ExpandableNotificationRow} that should be shown as a bubble.
Mark Renouf08bc42a2019-03-07 13:01:59 -0500170 *
171 * @param deleteIntent the intent to assign to {@link BubbleMetadata#deleteIntent}
172 */
173 public ExpandableNotificationRow createBubble(@Nullable PendingIntent deleteIntent)
174 throws Exception {
Mady Mellorfc02cc32019-04-01 14:47:55 -0700175 return createBubble(makeBubbleMetadata(deleteIntent), PKG);
Mady Mellore80930e2019-03-21 16:00:45 -0700176 }
177
178 /**
179 * Returns an {@link ExpandableNotificationRow} that should be shown as a bubble.
180 *
181 * @param bubbleMetadata the {@link BubbleMetadata} to use
182 */
183 public ExpandableNotificationRow createBubble(BubbleMetadata bubbleMetadata, String pkg)
184 throws Exception {
185 Notification n = createNotification(false /* isGroupSummary */,
186 null /* groupKey */, bubbleMetadata);
Mady Mellorfc02cc32019-04-01 14:47:55 -0700187 n.flags |= FLAG_BUBBLE;
Mady Melloraa8fef22019-04-11 13:36:40 -0700188 ExpandableNotificationRow row = generateRow(n, pkg, UID, USER_HANDLE,
189 0 /* extraInflationFlags */, IMPORTANCE_HIGH);
190 row.getEntry().canBubble = true;
191 return row;
Mady Mellore80930e2019-03-21 16:00:45 -0700192 }
193
194 /**
Rohan Shah524cf7b2018-03-15 14:40:02 -0700195 * Creates a notification row with the given details.
196 *
197 * @param pkg package used for creating a {@link StatusBarNotification}
198 * @param uid uid used for creating a {@link StatusBarNotification}
199 * @param isGroupSummary whether the notification row is a group summary
200 * @param groupKey the group key for the notification group used across notifications
201 * @return a row with that's either a standalone notification or a group notification if the
202 * groupKey is non-null
203 * @throws Exception
204 */
205 private ExpandableNotificationRow createRow(
206 String pkg,
207 int uid,
Yohei Yukawa72769462019-01-20 09:28:08 -0800208 UserHandle userHandle,
Rohan Shah524cf7b2018-03-15 14:40:02 -0700209 boolean isGroupSummary,
Mady Mellorb4991e62019-01-10 15:14:51 -0800210 @Nullable String groupKey)
Rohan Shah524cf7b2018-03-15 14:40:02 -0700211 throws Exception {
Kevind4660b22018-09-27 10:57:35 -0700212 Notification notif = createNotification(isGroupSummary, groupKey);
Yohei Yukawa72769462019-01-20 09:28:08 -0800213 return generateRow(notif, pkg, uid, userHandle, 0 /* inflationFlags */);
Kevind4660b22018-09-27 10:57:35 -0700214 }
215
216 /**
217 * Creates a generic notification.
218 *
219 * @return a notification with no special properties
220 */
Julia Reynolds0c245002019-03-27 16:10:11 -0400221 public Notification createNotification() {
Kevind4660b22018-09-27 10:57:35 -0700222 return createNotification(false /* isGroupSummary */, null /* groupKey */);
223 }
224
225 /**
226 * Creates a notification with the given parameters.
227 *
228 * @param isGroupSummary whether the notification is a group summary
229 * @param groupKey the group key for the notification group used across notifications
230 * @return a notification that is in the group specified or standalone if unspecified
231 */
Mady Mellorb4991e62019-01-10 15:14:51 -0800232 private Notification createNotification(boolean isGroupSummary, @Nullable String groupKey) {
Mady Mellore80930e2019-03-21 16:00:45 -0700233 return createNotification(isGroupSummary, groupKey, null /* bubble metadata */);
Mady Mellorb4991e62019-01-10 15:14:51 -0800234 }
235
236 /**
237 * Creates a notification with the given parameters.
238 *
239 * @param isGroupSummary whether the notification is a group summary
240 * @param groupKey the group key for the notification group used across notifications
Mady Mellore80930e2019-03-21 16:00:45 -0700241 * @param bubbleMetadata the bubble metadata to use for this notification if it exists.
Mady Mellorb4991e62019-01-10 15:14:51 -0800242 * @return a notification that is in the group specified or standalone if unspecified
243 */
Mark Renouf9ba6cea2019-04-17 11:53:50 -0400244 public Notification createNotification(boolean isGroupSummary,
Mady Mellore80930e2019-03-21 16:00:45 -0700245 @Nullable String groupKey, @Nullable BubbleMetadata bubbleMetadata) {
Selim Cinek6fd06b52017-03-07 15:54:10 -0800246 Notification publicVersion = new Notification.Builder(mContext).setSmallIcon(
247 R.drawable.ic_person)
248 .setCustomContentView(new RemoteViews(mContext.getPackageName(),
249 R.layout.custom_view_dark))
250 .build();
Kevind4660b22018-09-27 10:57:35 -0700251 Notification.Builder notificationBuilder = new Notification.Builder(mContext, "channelId")
252 .setSmallIcon(R.drawable.ic_person)
253 .setContentTitle("Title")
254 .setContentText("Text")
Kevind5022f92018-10-08 18:30:26 -0700255 .setPublicVersion(publicVersion)
256 .setStyle(new Notification.BigTextStyle().bigText("Big Text"));
Rohan Shah524cf7b2018-03-15 14:40:02 -0700257 if (isGroupSummary) {
258 notificationBuilder.setGroupSummary(true);
259 }
260 if (!TextUtils.isEmpty(groupKey)) {
261 notificationBuilder.setGroup(groupKey);
262 }
Mady Mellore80930e2019-03-21 16:00:45 -0700263 if (bubbleMetadata != null) {
264 notificationBuilder.setBubbleMetadata(bubbleMetadata);
Mady Mellorb4991e62019-01-10 15:14:51 -0800265 }
Kevind4660b22018-09-27 10:57:35 -0700266 return notificationBuilder.build();
Julia Reynoldsfc640012018-02-21 12:25:27 -0500267 }
268
Rohan Shah524cf7b2018-03-15 14:40:02 -0700269 private ExpandableNotificationRow generateRow(
270 Notification notification,
271 String pkg,
272 int uid,
Yohei Yukawa72769462019-01-20 09:28:08 -0800273 UserHandle userHandle,
Mady Mellorb4991e62019-01-10 15:14:51 -0800274 @InflationFlag int extraInflationFlags)
275 throws Exception {
Yohei Yukawa72769462019-01-20 09:28:08 -0800276 return generateRow(notification, pkg, uid, userHandle, extraInflationFlags,
277 IMPORTANCE_DEFAULT);
Mady Mellorb4991e62019-01-10 15:14:51 -0800278 }
279
280 private ExpandableNotificationRow generateRow(
281 Notification notification,
282 String pkg,
283 int uid,
Yohei Yukawa72769462019-01-20 09:28:08 -0800284 UserHandle userHandle,
Mady Mellorb4991e62019-01-10 15:14:51 -0800285 @InflationFlag int extraInflationFlags,
286 int importance)
Julia Reynoldsfc640012018-02-21 12:25:27 -0500287 throws Exception {
Selim Cinek10790672017-03-08 16:33:05 -0800288 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
289 mContext.LAYOUT_INFLATER_SERVICE);
Jason Monk6dceace2018-05-15 20:24:07 -0400290 mRow = (ExpandableNotificationRow) inflater.inflate(
291 R.layout.status_bar_notification_row,
292 null /* root */,
293 false /* attachToRoot */);
Selim Cinek5ba22542017-04-20 15:16:10 -0700294 ExpandableNotificationRow row = mRow;
Selim Cinek10790672017-03-08 16:33:05 -0800295 row.setGroupManager(mGroupManager);
Selim Cinek5cf1d052017-06-01 17:36:46 -0700296 row.setHeadsUpManager(mHeadsUpManager);
297 row.setAboveShelfChangedListener(aboveShelf -> {});
Rohan Shah524cf7b2018-03-15 14:40:02 -0700298 StatusBarNotification sbn = new StatusBarNotification(
299 pkg,
300 pkg,
301 mId++,
302 null /* tag */,
303 uid,
304 2000 /* initialPid */,
305 notification,
Yohei Yukawa72769462019-01-20 09:28:08 -0800306 userHandle,
Rohan Shah524cf7b2018-03-15 14:40:02 -0700307 null /* overrideGroupKey */,
308 System.currentTimeMillis());
Ned Burnsf81c4c42019-01-07 14:10:43 -0500309 NotificationEntry entry = new NotificationEntry(sbn);
Evan Laird94492852018-10-25 13:43:01 -0400310 entry.setRow(row);
Selim Cinek5ba22542017-04-20 15:16:10 -0700311 entry.createIcons(mContext, sbn);
Julia Reynolds0abae112018-06-08 10:36:57 -0400312 entry.channel = new NotificationChannel(
Mady Mellorb4991e62019-01-10 15:14:51 -0800313 notification.getChannelId(), notification.getChannelId(), importance);
Julia Reynolds0abae112018-06-08 10:36:57 -0400314 entry.channel.setBlockableSystem(true);
Kevind4660b22018-09-27 10:57:35 -0700315 row.setEntry(entry);
316 row.getNotificationInflater().addInflationFlags(extraInflationFlags);
Ned Burns1a5e22f2019-02-14 15:11:52 -0500317 NotificationContentInflaterTest.runThenWaitForInflation(
Kevind4660b22018-09-27 10:57:35 -0700318 () -> row.inflateViews(),
Selim Cinek5ba22542017-04-20 15:16:10 -0700319 row.getNotificationInflater());
Selim Cinek6fd06b52017-03-07 15:54:10 -0800320
Rohan Shah524cf7b2018-03-15 14:40:02 -0700321 // This would be done as part of onAsyncInflationFinished, but we skip large amounts of
322 // the callback chain, so we need to make up for not adding it to the group manager
323 // here.
324 mGroupManager.onEntryAdded(entry);
Selim Cinek6fd06b52017-03-07 15:54:10 -0800325 return row;
326 }
Mady Mellorc39b4ae2019-01-09 17:11:37 -0800327
Mark Renouf08bc42a2019-03-07 13:01:59 -0500328 private BubbleMetadata makeBubbleMetadata(PendingIntent deleteIntent) {
Mark Renoufa9fbb6e2019-02-11 12:48:22 -0500329 Intent target = new Intent(mContext, BubblesTestActivity.class);
330 PendingIntent bubbleIntent = PendingIntent.getActivity(mContext, 0, target, 0);
Mark Renouf08bc42a2019-03-07 13:01:59 -0500331
332 return new BubbleMetadata.Builder()
Mady Mellorc39b4ae2019-01-09 17:11:37 -0800333 .setIntent(bubbleIntent)
Mark Renouf08bc42a2019-03-07 13:01:59 -0500334 .setDeleteIntent(deleteIntent)
Mady Mellor9848a6c2019-03-19 15:29:05 -0700335 .setIcon(Icon.createWithResource(mContext, R.drawable.android))
Mady Mellorc39b4ae2019-01-09 17:11:37 -0800336 .setDesiredHeight(314)
337 .build();
338 }
Selim Cinek6fd06b52017-03-07 15:54:10 -0800339}