blob: 727e9af94146ddf7f36596c2206fb8f5c3cbc780 [file] [log] [blame]
yoshiki iguchia85c2a02018-01-12 11:28:06 +09001/*
2 * Copyright (C) 2018 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
Rohan Shah20790b82018-07-02 17:21:04 -070017package com.android.systemui.statusbar.notification.row;
yoshiki iguchia85c2a02018-01-12 11:28:06 +090018
Julia Reynoldsb5867452018-02-28 16:31:35 -050019import static android.app.AppOpsManager.OP_CAMERA;
20import static android.app.AppOpsManager.OP_RECORD_AUDIO;
21import static android.app.AppOpsManager.OP_SYSTEM_ALERT_WINDOW;
Gus Prevas9abc5062018-10-31 16:11:04 -040022import static android.app.NotificationManager.IMPORTANCE_DEFAULT;
Gus Prevas5a70a4e2018-11-26 17:16:05 -050023import static android.service.notification.NotificationListenerService.Ranking.USER_SENTIMENT_NEGATIVE;
Julia Reynoldsb5867452018-02-28 16:31:35 -050024
yoshiki iguchia85c2a02018-01-12 11:28:06 +090025import static junit.framework.Assert.assertNotNull;
Kevina5ff1fa2018-08-21 16:35:48 -070026import static junit.framework.Assert.assertNull;
27import static junit.framework.Assert.assertTrue;
yoshiki iguchia85c2a02018-01-12 11:28:06 +090028
29import static org.junit.Assert.assertEquals;
30import static org.junit.Assert.fail;
31import static org.mockito.ArgumentMatchers.any;
32import static org.mockito.ArgumentMatchers.anyBoolean;
33import static org.mockito.ArgumentMatchers.anyInt;
Rohan Shah524cf7b2018-03-15 14:40:02 -070034import static org.mockito.ArgumentMatchers.eq;
yoshiki iguchia85c2a02018-01-12 11:28:06 +090035import static org.mockito.Mockito.doNothing;
Kevina5ff1fa2018-08-21 16:35:48 -070036import static org.mockito.Mockito.doReturn;
yoshiki iguchia85c2a02018-01-12 11:28:06 +090037import static org.mockito.Mockito.mock;
yoshiki iguchia85c2a02018-01-12 11:28:06 +090038import static org.mockito.Mockito.spy;
yoshiki iguchia85c2a02018-01-12 11:28:06 +090039import static org.mockito.Mockito.times;
Gus Prevas9abc5062018-10-31 16:11:04 -040040import static org.mockito.Mockito.verify;
41import static org.mockito.Mockito.when;
yoshiki iguchia85c2a02018-01-12 11:28:06 +090042
Rohan Shah524cf7b2018-03-15 14:40:02 -070043import android.app.INotificationManager;
yoshiki iguchia85c2a02018-01-12 11:28:06 +090044import android.app.Notification;
45import android.app.NotificationChannel;
Julia Reynoldsb5867452018-02-28 16:31:35 -050046import android.content.Intent;
Rohan Shah524cf7b2018-03-15 14:40:02 -070047import android.content.pm.PackageManager;
yoshiki iguchia85c2a02018-01-12 11:28:06 +090048import android.os.Binder;
49import android.os.Handler;
Julia Reynoldsb5867452018-02-28 16:31:35 -050050import android.provider.Settings;
yoshiki iguchia85c2a02018-01-12 11:28:06 +090051import android.service.notification.StatusBarNotification;
52import android.support.test.filters.SmallTest;
53import android.testing.AndroidTestingRunner;
54import android.testing.TestableLooper;
Julia Reynoldsb5867452018-02-28 16:31:35 -050055import android.util.ArraySet;
yoshiki iguchia85c2a02018-01-12 11:28:06 +090056import android.view.View;
57
TreeHugger Robotee8e1ae2019-01-18 19:26:40 +000058import com.android.internal.logging.MetricsLogger;
yoshiki iguchia85c2a02018-01-12 11:28:06 +090059import com.android.systemui.SysuiTestCase;
60import com.android.systemui.plugins.statusbar.NotificationMenuRowPlugin;
Rohan Shah20790b82018-07-02 17:21:04 -070061import com.android.systemui.statusbar.NotificationPresenter;
62import com.android.systemui.statusbar.NotificationTestHelper;
Gus Prevas21437b32018-12-05 10:36:13 -050063import com.android.systemui.statusbar.notification.NotificationActivityStarter;
Ned Burnsf81c4c42019-01-07 14:10:43 -050064import com.android.systemui.statusbar.notification.collection.NotificationEntry;
Gus Prevas5a70a4e2018-11-26 17:16:05 -050065import com.android.systemui.statusbar.notification.row.NotificationGutsManager.OnSettingsClickListener;
Rohan Shah20790b82018-07-02 17:21:04 -070066import com.android.systemui.statusbar.notification.stack.NotificationStackScrollLayout;
Jason Monk297c04e2018-08-23 17:16:59 -040067import com.android.systemui.statusbar.policy.DeviceProvisionedController;
Jason Monka716bac2018-12-05 15:48:21 -050068import com.android.systemui.util.Assert;
yoshiki iguchia85c2a02018-01-12 11:28:06 +090069
70import org.junit.Before;
71import org.junit.Rule;
72import org.junit.Test;
73import org.junit.runner.RunWith;
Julia Reynoldsb5867452018-02-28 16:31:35 -050074import org.mockito.ArgumentCaptor;
yoshiki iguchia85c2a02018-01-12 11:28:06 +090075import org.mockito.Mock;
yoshiki iguchia85c2a02018-01-12 11:28:06 +090076import org.mockito.junit.MockitoJUnit;
Gus Prevas9abc5062018-10-31 16:11:04 -040077import org.mockito.junit.MockitoRule;
yoshiki iguchia85c2a02018-01-12 11:28:06 +090078
Rohan Shah524cf7b2018-03-15 14:40:02 -070079/**
80 * Tests for {@link NotificationGutsManager}.
81 */
yoshiki iguchia85c2a02018-01-12 11:28:06 +090082@SmallTest
83@RunWith(AndroidTestingRunner.class)
Jason Monka716bac2018-12-05 15:48:21 -050084@TestableLooper.RunWithLooper
yoshiki iguchia85c2a02018-01-12 11:28:06 +090085public class NotificationGutsManagerTest extends SysuiTestCase {
86 private static final String TEST_CHANNEL_ID = "NotificationManagerServiceTestChannelId";
87
yoshiki iguchia85c2a02018-01-12 11:28:06 +090088 private NotificationChannel mTestNotificationChannel = new NotificationChannel(
Gus Prevas9abc5062018-10-31 16:11:04 -040089 TEST_CHANNEL_ID, TEST_CHANNEL_ID, IMPORTANCE_DEFAULT);
yoshiki iguchia85c2a02018-01-12 11:28:06 +090090 private TestableLooper mTestableLooper;
91 private Handler mHandler;
92 private NotificationTestHelper mHelper;
93 private NotificationGutsManager mGutsManager;
94
95 @Rule public MockitoRule mockito = MockitoJUnit.rule();
TreeHugger Robotee8e1ae2019-01-18 19:26:40 +000096 @Mock private MetricsLogger mMetricsLogger;
yoshiki iguchia85c2a02018-01-12 11:28:06 +090097 @Mock private NotificationPresenter mPresenter;
Gus Prevas21437b32018-12-05 10:36:13 -050098 @Mock private NotificationActivityStarter mNotificationActivityStarter;
yoshiki iguchia85c2a02018-01-12 11:28:06 +090099 @Mock private NotificationStackScrollLayout mStackScroller;
100 @Mock private NotificationInfo.CheckSaveListener mCheckSaveListener;
Jason Monk297c04e2018-08-23 17:16:59 -0400101 @Mock private OnSettingsClickListener mOnSettingsClickListener;
102 @Mock private DeviceProvisionedController mDeviceProvisionedController;
yoshiki iguchia85c2a02018-01-12 11:28:06 +0900103
104 @Before
105 public void setUp() {
106 mTestableLooper = TestableLooper.get(this);
Jason Monka716bac2018-12-05 15:48:21 -0500107 Assert.sMainLooper = TestableLooper.get(this).getLooper();
Jason Monk297c04e2018-08-23 17:16:59 -0400108 mDependency.injectTestDependency(DeviceProvisionedController.class,
109 mDeviceProvisionedController);
TreeHugger Robotee8e1ae2019-01-18 19:26:40 +0000110 mDependency.injectTestDependency(MetricsLogger.class, mMetricsLogger);
Jason Monk6dceace2018-05-15 20:24:07 -0400111 mHandler = Handler.createAsync(mTestableLooper.getLooper());
yoshiki iguchia85c2a02018-01-12 11:28:06 +0900112
113 mHelper = new NotificationTestHelper(mContext);
114
115 mGutsManager = new NotificationGutsManager(mContext);
Kevina5ff1fa2018-08-21 16:35:48 -0700116 mGutsManager.setUpWithPresenter(mPresenter, mStackScroller,
yoshiki iguchia85c2a02018-01-12 11:28:06 +0900117 mCheckSaveListener, mOnSettingsClickListener);
Gus Prevas21437b32018-12-05 10:36:13 -0500118 mGutsManager.setNotificationActivityStarter(mNotificationActivityStarter);
yoshiki iguchia85c2a02018-01-12 11:28:06 +0900119 }
120
121 ////////////////////////////////////////////////////////////////////////////////////////////////
122 // Test methods:
123
124 @Test
125 public void testOpenAndCloseGuts() {
126 NotificationGuts guts = spy(new NotificationGuts(mContext));
127 when(guts.post(any())).thenAnswer(invocation -> {
128 mHandler.post(((Runnable) invocation.getArguments()[0]));
129 return null;
130 });
131
132 // Test doesn't support animation since the guts view is not attached.
Rohan Shah524cf7b2018-03-15 14:40:02 -0700133 doNothing().when(guts).openControls(
134 eq(true) /* shouldDoCircularReveal */,
135 anyInt(),
136 anyInt(),
137 anyBoolean(),
138 any(Runnable.class));
yoshiki iguchia85c2a02018-01-12 11:28:06 +0900139
140 ExpandableNotificationRow realRow = createTestNotificationRow();
141 NotificationMenuRowPlugin.MenuItem menuItem = createTestMenuItem(realRow);
142
143 ExpandableNotificationRow row = spy(realRow);
144 when(row.getWindowToken()).thenReturn(new Binder());
145 when(row.getGuts()).thenReturn(guts);
146
TreeHugger Robotee8e1ae2019-01-18 19:26:40 +0000147 assertTrue(mGutsManager.openGuts(row, 0, 0, menuItem));
yoshiki iguchia85c2a02018-01-12 11:28:06 +0900148 assertEquals(View.INVISIBLE, guts.getVisibility());
149 mTestableLooper.processAllMessages();
Rohan Shah524cf7b2018-03-15 14:40:02 -0700150 verify(guts).openControls(
151 eq(true),
152 anyInt(),
153 anyInt(),
154 anyBoolean(),
155 any(Runnable.class));
yoshiki iguchia85c2a02018-01-12 11:28:06 +0900156
157 assertEquals(View.VISIBLE, guts.getVisibility());
158 mGutsManager.closeAndSaveGuts(false, false, false, 0, 0, false);
159
160 verify(guts).closeControls(anyBoolean(), anyBoolean(), anyInt(), anyInt(), anyBoolean());
161 verify(row, times(1)).setGutsView(any());
162 }
163
164 @Test
165 public void testChangeDensityOrFontScale() {
166 NotificationGuts guts = spy(new NotificationGuts(mContext));
167 when(guts.post(any())).thenAnswer(invocation -> {
168 mHandler.post(((Runnable) invocation.getArguments()[0]));
169 return null;
170 });
171
172 // Test doesn't support animation since the guts view is not attached.
Rohan Shah524cf7b2018-03-15 14:40:02 -0700173 doNothing().when(guts).openControls(
174 eq(true) /* shouldDoCircularReveal */,
175 anyInt(),
176 anyInt(),
177 anyBoolean(),
178 any(Runnable.class));
yoshiki iguchia85c2a02018-01-12 11:28:06 +0900179
180 ExpandableNotificationRow realRow = createTestNotificationRow();
181 NotificationMenuRowPlugin.MenuItem menuItem = createTestMenuItem(realRow);
182
183 ExpandableNotificationRow row = spy(realRow);
Evan Laird94492852018-10-25 13:43:01 -0400184
yoshiki iguchia85c2a02018-01-12 11:28:06 +0900185 when(row.getWindowToken()).thenReturn(new Binder());
186 when(row.getGuts()).thenReturn(guts);
187 doNothing().when(row).inflateGuts();
188
Ned Burnsf81c4c42019-01-07 14:10:43 -0500189 NotificationEntry realEntry = realRow.getEntry();
190 NotificationEntry entry = spy(realEntry);
Evan Laird94492852018-10-25 13:43:01 -0400191
192 when(entry.getRow()).thenReturn(row);
193 when(entry.getGuts()).thenReturn(guts);
194
TreeHugger Robotee8e1ae2019-01-18 19:26:40 +0000195 assertTrue(mGutsManager.openGuts(row, 0, 0, menuItem));
yoshiki iguchia85c2a02018-01-12 11:28:06 +0900196 mTestableLooper.processAllMessages();
Rohan Shah524cf7b2018-03-15 14:40:02 -0700197 verify(guts).openControls(
198 eq(true),
199 anyInt(),
200 anyInt(),
201 anyBoolean(),
202 any(Runnable.class));
yoshiki iguchia85c2a02018-01-12 11:28:06 +0900203
Evan Laird94492852018-10-25 13:43:01 -0400204 // called once by mGutsManager.bindGuts() in mGutsManager.openGuts()
205 verify(row).setGutsView(any());
206
yoshiki iguchia85c2a02018-01-12 11:28:06 +0900207 row.onDensityOrFontScaleChanged();
Evan Laird94492852018-10-25 13:43:01 -0400208 mGutsManager.onDensityOrFontScaleChanged(entry);
209
yoshiki iguchia85c2a02018-01-12 11:28:06 +0900210 mTestableLooper.processAllMessages();
211
212 mGutsManager.closeAndSaveGuts(false, false, false, 0, 0, false);
213
214 verify(guts).closeControls(anyBoolean(), anyBoolean(), anyInt(), anyInt(), anyBoolean());
Evan Laird94492852018-10-25 13:43:01 -0400215
216 // called again by mGutsManager.bindGuts(), in mGutsManager.onDensityOrFontScaleChanged()
yoshiki iguchia85c2a02018-01-12 11:28:06 +0900217 verify(row, times(2)).setGutsView(any());
218 }
219
Julia Reynoldsb5867452018-02-28 16:31:35 -0500220 @Test
221 public void testAppOpsSettingsIntent_camera() {
222 ArraySet<Integer> ops = new ArraySet<>();
223 ops.add(OP_CAMERA);
224 mGutsManager.startAppOpsSettingsActivity("", 0, ops, null);
225 ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class);
Gus Prevas21437b32018-12-05 10:36:13 -0500226 verify(mNotificationActivityStarter, times(1))
227 .startNotificationGutsIntent(captor.capture(), anyInt(), any());
Julia Reynoldsb5867452018-02-28 16:31:35 -0500228 assertEquals(Intent.ACTION_MANAGE_APP_PERMISSIONS, captor.getValue().getAction());
229 }
230
231 @Test
232 public void testAppOpsSettingsIntent_mic() {
233 ArraySet<Integer> ops = new ArraySet<>();
234 ops.add(OP_RECORD_AUDIO);
235 mGutsManager.startAppOpsSettingsActivity("", 0, ops, null);
236 ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class);
Gus Prevas21437b32018-12-05 10:36:13 -0500237 verify(mNotificationActivityStarter, times(1))
238 .startNotificationGutsIntent(captor.capture(), anyInt(), any());
Julia Reynoldsb5867452018-02-28 16:31:35 -0500239 assertEquals(Intent.ACTION_MANAGE_APP_PERMISSIONS, captor.getValue().getAction());
240 }
241
242 @Test
243 public void testAppOpsSettingsIntent_camera_mic() {
244 ArraySet<Integer> ops = new ArraySet<>();
245 ops.add(OP_CAMERA);
246 ops.add(OP_RECORD_AUDIO);
247 mGutsManager.startAppOpsSettingsActivity("", 0, ops, null);
248 ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class);
Gus Prevas21437b32018-12-05 10:36:13 -0500249 verify(mNotificationActivityStarter, times(1))
250 .startNotificationGutsIntent(captor.capture(), anyInt(), any());
Julia Reynoldsb5867452018-02-28 16:31:35 -0500251 assertEquals(Intent.ACTION_MANAGE_APP_PERMISSIONS, captor.getValue().getAction());
252 }
253
254 @Test
255 public void testAppOpsSettingsIntent_overlay() {
256 ArraySet<Integer> ops = new ArraySet<>();
257 ops.add(OP_SYSTEM_ALERT_WINDOW);
258 mGutsManager.startAppOpsSettingsActivity("", 0, ops, null);
259 ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class);
Gus Prevas21437b32018-12-05 10:36:13 -0500260 verify(mNotificationActivityStarter, times(1))
261 .startNotificationGutsIntent(captor.capture(), anyInt(), any());
Julia Reynoldsb5867452018-02-28 16:31:35 -0500262 assertEquals(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, captor.getValue().getAction());
263 }
264
265 @Test
266 public void testAppOpsSettingsIntent_camera_mic_overlay() {
267 ArraySet<Integer> ops = new ArraySet<>();
268 ops.add(OP_CAMERA);
269 ops.add(OP_RECORD_AUDIO);
270 ops.add(OP_SYSTEM_ALERT_WINDOW);
271 mGutsManager.startAppOpsSettingsActivity("", 0, ops, null);
272 ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class);
Gus Prevas21437b32018-12-05 10:36:13 -0500273 verify(mNotificationActivityStarter, times(1))
274 .startNotificationGutsIntent(captor.capture(), anyInt(), any());
Julia Reynoldsb5867452018-02-28 16:31:35 -0500275 assertEquals(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, captor.getValue().getAction());
276 }
277
278 @Test
279 public void testAppOpsSettingsIntent_camera_overlay() {
280 ArraySet<Integer> ops = new ArraySet<>();
281 ops.add(OP_CAMERA);
282 ops.add(OP_SYSTEM_ALERT_WINDOW);
283 mGutsManager.startAppOpsSettingsActivity("", 0, ops, null);
284 ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class);
Gus Prevas21437b32018-12-05 10:36:13 -0500285 verify(mNotificationActivityStarter, times(1))
286 .startNotificationGutsIntent(captor.capture(), anyInt(), any());
Julia Reynoldsb5867452018-02-28 16:31:35 -0500287 assertEquals(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, captor.getValue().getAction());
288 }
289
290 @Test
291 public void testAppOpsSettingsIntent_mic_overlay() {
292 ArraySet<Integer> ops = new ArraySet<>();
293 ops.add(OP_RECORD_AUDIO);
294 ops.add(OP_SYSTEM_ALERT_WINDOW);
295 mGutsManager.startAppOpsSettingsActivity("", 0, ops, null);
296 ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class);
Gus Prevas21437b32018-12-05 10:36:13 -0500297 verify(mNotificationActivityStarter, times(1))
298 .startNotificationGutsIntent(captor.capture(), anyInt(), any());
Julia Reynoldsb5867452018-02-28 16:31:35 -0500299 assertEquals(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, captor.getValue().getAction());
300 }
301
Rohan Shah524cf7b2018-03-15 14:40:02 -0700302 @Test
303 public void testInitializeNotificationInfoView_showBlockingHelper() throws Exception {
304 NotificationInfo notificationInfoView = mock(NotificationInfo.class);
Rohan Shah63411fc2018-03-28 19:05:52 -0700305 ExpandableNotificationRow row = spy(mHelper.createRow());
Rohan Shah524cf7b2018-03-15 14:40:02 -0700306 row.setBlockingHelperShowing(true);
307 row.getEntry().userSentiment = USER_SENTIMENT_NEGATIVE;
Rohan Shah63411fc2018-03-28 19:05:52 -0700308 when(row.getIsNonblockable()).thenReturn(false);
Rohan Shah524cf7b2018-03-15 14:40:02 -0700309 StatusBarNotification statusBarNotification = row.getStatusBarNotification();
310
Gus Prevas5a70a4e2018-11-26 17:16:05 -0500311 mGutsManager.initializeNotificationInfo(row, notificationInfoView);
Rohan Shah524cf7b2018-03-15 14:40:02 -0700312
313 verify(notificationInfoView).bindNotification(
314 any(PackageManager.class),
315 any(INotificationManager.class),
316 eq(statusBarNotification.getPackageName()),
Julia Reynolds0abae112018-06-08 10:36:57 -0400317 any(NotificationChannel.class),
Rohan Shah524cf7b2018-03-15 14:40:02 -0700318 anyInt(),
319 eq(statusBarNotification),
320 any(NotificationInfo.CheckSaveListener.class),
321 any(NotificationInfo.OnSettingsClickListener.class),
322 any(NotificationInfo.OnAppSettingsClickListener.class),
Rohan Shah63411fc2018-03-28 19:05:52 -0700323 eq(false),
Julia Reynolds35765d82018-08-17 11:39:19 -0400324 eq(false),
Rohan Shah524cf7b2018-03-15 14:40:02 -0700325 eq(true) /* isForBlockingHelper */,
Gus Prevas9abc5062018-10-31 16:11:04 -0400326 eq(true) /* isUserSentimentNegative */,
Gus Prevascaed15c2019-01-18 14:19:51 -0500327 eq(0),
328 eq(false) /* wasShownHighPriority */);
Rohan Shah524cf7b2018-03-15 14:40:02 -0700329 }
330
331 @Test
332 public void testInitializeNotificationInfoView_dontShowBlockingHelper() throws Exception {
333 NotificationInfo notificationInfoView = mock(NotificationInfo.class);
Rohan Shah63411fc2018-03-28 19:05:52 -0700334 ExpandableNotificationRow row = spy(mHelper.createRow());
Rohan Shah524cf7b2018-03-15 14:40:02 -0700335 row.setBlockingHelperShowing(false);
336 row.getEntry().userSentiment = USER_SENTIMENT_NEGATIVE;
Rohan Shah63411fc2018-03-28 19:05:52 -0700337 when(row.getIsNonblockable()).thenReturn(false);
Rohan Shah524cf7b2018-03-15 14:40:02 -0700338 StatusBarNotification statusBarNotification = row.getStatusBarNotification();
339
Gus Prevas5a70a4e2018-11-26 17:16:05 -0500340 mGutsManager.initializeNotificationInfo(row, notificationInfoView);
Rohan Shah524cf7b2018-03-15 14:40:02 -0700341
342 verify(notificationInfoView).bindNotification(
343 any(PackageManager.class),
344 any(INotificationManager.class),
345 eq(statusBarNotification.getPackageName()),
Julia Reynolds0abae112018-06-08 10:36:57 -0400346 any(NotificationChannel.class),
Rohan Shah524cf7b2018-03-15 14:40:02 -0700347 anyInt(),
348 eq(statusBarNotification),
349 any(NotificationInfo.CheckSaveListener.class),
350 any(NotificationInfo.OnSettingsClickListener.class),
351 any(NotificationInfo.OnAppSettingsClickListener.class),
Rohan Shah63411fc2018-03-28 19:05:52 -0700352 eq(false),
Julia Reynolds35765d82018-08-17 11:39:19 -0400353 eq(false),
354 eq(false) /* isForBlockingHelper */,
Gus Prevas9abc5062018-10-31 16:11:04 -0400355 eq(true) /* isUserSentimentNegative */,
Gus Prevascaed15c2019-01-18 14:19:51 -0500356 eq(0),
357 eq(false) /* wasShownHighPriority */);
Gus Prevas9abc5062018-10-31 16:11:04 -0400358 }
359
360 @Test
Gus Prevascaed15c2019-01-18 14:19:51 -0500361 public void testInitializeNotificationInfoView_highPriority() throws Exception {
Gus Prevas9abc5062018-10-31 16:11:04 -0400362 NotificationInfo notificationInfoView = mock(NotificationInfo.class);
363 ExpandableNotificationRow row = spy(mHelper.createRow());
364 row.setBlockingHelperShowing(true);
365 row.getEntry().userSentiment = USER_SENTIMENT_NEGATIVE;
366 row.getEntry().importance = IMPORTANCE_DEFAULT;
Gus Prevascaed15c2019-01-18 14:19:51 -0500367 row.getEntry().setIsHighPriority(true);
Gus Prevas9abc5062018-10-31 16:11:04 -0400368 when(row.getIsNonblockable()).thenReturn(false);
369 StatusBarNotification statusBarNotification = row.getStatusBarNotification();
370
Gus Prevas5a70a4e2018-11-26 17:16:05 -0500371 mGutsManager.initializeNotificationInfo(row, notificationInfoView);
Gus Prevas9abc5062018-10-31 16:11:04 -0400372
373 verify(notificationInfoView).bindNotification(
374 any(PackageManager.class),
375 any(INotificationManager.class),
376 eq(statusBarNotification.getPackageName()),
377 any(NotificationChannel.class),
378 anyInt(),
379 eq(statusBarNotification),
380 any(NotificationInfo.CheckSaveListener.class),
381 any(NotificationInfo.OnSettingsClickListener.class),
382 any(NotificationInfo.OnAppSettingsClickListener.class),
383 eq(false),
384 eq(false),
385 eq(true) /* isForBlockingHelper */,
386 eq(true) /* isUserSentimentNegative */,
Gus Prevascaed15c2019-01-18 14:19:51 -0500387 eq(IMPORTANCE_DEFAULT),
388 eq(true) /* wasShownHighPriority */);
Julia Reynolds35765d82018-08-17 11:39:19 -0400389 }
390
391 @Test
392 public void testInitializeNotificationInfoView_PassesAlongProvisionedState() throws Exception {
393 NotificationInfo notificationInfoView = mock(NotificationInfo.class);
394 ExpandableNotificationRow row = spy(mHelper.createRow());
395 row.setBlockingHelperShowing(false);
396 row.getEntry().userSentiment = USER_SENTIMENT_NEGATIVE;
397 when(row.getIsNonblockable()).thenReturn(false);
398 StatusBarNotification statusBarNotification = row.getStatusBarNotification();
Jason Monk297c04e2018-08-23 17:16:59 -0400399 when(mDeviceProvisionedController.isDeviceProvisioned()).thenReturn(true);
Julia Reynolds35765d82018-08-17 11:39:19 -0400400
Gus Prevas5a70a4e2018-11-26 17:16:05 -0500401 mGutsManager.initializeNotificationInfo(row, notificationInfoView);
Julia Reynolds35765d82018-08-17 11:39:19 -0400402
403 verify(notificationInfoView).bindNotification(
404 any(PackageManager.class),
405 any(INotificationManager.class),
406 eq(statusBarNotification.getPackageName()),
407 any(NotificationChannel.class),
408 anyInt(),
409 eq(statusBarNotification),
410 any(NotificationInfo.CheckSaveListener.class),
411 any(NotificationInfo.OnSettingsClickListener.class),
412 any(NotificationInfo.OnAppSettingsClickListener.class),
413 eq(true),
414 eq(false),
Rohan Shah524cf7b2018-03-15 14:40:02 -0700415 eq(false) /* isForBlockingHelper */,
Gus Prevas9abc5062018-10-31 16:11:04 -0400416 eq(true) /* isUserSentimentNegative */,
Gus Prevascaed15c2019-01-18 14:19:51 -0500417 eq(0),
418 eq(false) /* wasShownHighPriority */);
Gus Prevas894d9152018-11-12 13:51:40 -0500419 }
420
421 @Test
422 public void testInitializeNotificationInfoView_withInitialAction() throws Exception {
423 NotificationInfo notificationInfoView = mock(NotificationInfo.class);
424 ExpandableNotificationRow row = spy(mHelper.createRow());
425 row.setBlockingHelperShowing(true);
426 row.getEntry().userSentiment = USER_SENTIMENT_NEGATIVE;
427 when(row.getIsNonblockable()).thenReturn(false);
428 StatusBarNotification statusBarNotification = row.getStatusBarNotification();
429
Gus Prevas5a70a4e2018-11-26 17:16:05 -0500430 mGutsManager.initializeNotificationInfo(row, notificationInfoView);
Gus Prevas894d9152018-11-12 13:51:40 -0500431
432 verify(notificationInfoView).bindNotification(
433 any(PackageManager.class),
434 any(INotificationManager.class),
435 eq(statusBarNotification.getPackageName()),
436 any(NotificationChannel.class),
437 anyInt(),
438 eq(statusBarNotification),
439 any(NotificationInfo.CheckSaveListener.class),
440 any(NotificationInfo.OnSettingsClickListener.class),
441 any(NotificationInfo.OnAppSettingsClickListener.class),
442 eq(false),
443 eq(false),
444 eq(true) /* isForBlockingHelper */,
445 eq(true) /* isUserSentimentNegative */,
Gus Prevascaed15c2019-01-18 14:19:51 -0500446 eq(0),
447 eq(false) /* wasShownHighPriority */);
Rohan Shah524cf7b2018-03-15 14:40:02 -0700448 }
449
Kevina5ff1fa2018-08-21 16:35:48 -0700450 @Test
451 public void testShouldExtendLifetime() {
452 NotificationGuts guts = new NotificationGuts(mContext);
453 ExpandableNotificationRow row = spy(createTestNotificationRow());
454 doReturn(guts).when(row).getGuts();
Ned Burnsf81c4c42019-01-07 14:10:43 -0500455 NotificationEntry entry = row.getEntry();
Evan Laird94492852018-10-25 13:43:01 -0400456 entry.setRow(row);
Kevina5ff1fa2018-08-21 16:35:48 -0700457 mGutsManager.setExposedGuts(guts);
458
459 assertTrue(mGutsManager.shouldExtendLifetime(entry));
460 }
461
462 @Test
Kevine9e938c2018-09-06 13:38:11 -0700463 public void testSetShouldManageLifetime_setShouldManage() {
Ned Burnsf81c4c42019-01-07 14:10:43 -0500464 NotificationEntry entry = createTestNotificationRow().getEntry();
Kevine9e938c2018-09-06 13:38:11 -0700465 mGutsManager.setShouldManageLifetime(entry, true /* shouldManage */);
Kevina5ff1fa2018-08-21 16:35:48 -0700466
467 assertTrue(entry.key.equals(mGutsManager.mKeyToRemoveOnGutsClosed));
468 }
469
470 @Test
Kevine9e938c2018-09-06 13:38:11 -0700471 public void testSetShouldManageLifetime_setShouldNotManage() {
Ned Burnsf81c4c42019-01-07 14:10:43 -0500472 NotificationEntry entry = createTestNotificationRow().getEntry();
Kevina5ff1fa2018-08-21 16:35:48 -0700473 mGutsManager.mKeyToRemoveOnGutsClosed = entry.key;
Kevine9e938c2018-09-06 13:38:11 -0700474 mGutsManager.setShouldManageLifetime(entry, false /* shouldManage */);
Kevina5ff1fa2018-08-21 16:35:48 -0700475
476 assertNull(mGutsManager.mKeyToRemoveOnGutsClosed);
477 }
478
yoshiki iguchia85c2a02018-01-12 11:28:06 +0900479 ////////////////////////////////////////////////////////////////////////////////////////////////
480 // Utility methods:
481
482 private ExpandableNotificationRow createTestNotificationRow() {
483 Notification.Builder nb = new Notification.Builder(mContext,
484 mTestNotificationChannel.getId())
485 .setContentTitle("foo")
486 .setColorized(true)
487 .setFlag(Notification.FLAG_CAN_COLORIZE, true)
488 .setSmallIcon(android.R.drawable.sym_def_app_icon);
489
490 try {
491 ExpandableNotificationRow row = mHelper.createRow(nb.build());
492 row.getEntry().channel = mTestNotificationChannel;
493 return row;
494 } catch (Exception e) {
495 fail();
496 return null;
497 }
498 }
499
500 private NotificationMenuRowPlugin.MenuItem createTestMenuItem(ExpandableNotificationRow row) {
501 NotificationMenuRowPlugin menuRow = new NotificationMenuRow(mContext);
502 menuRow.createMenu(row, row.getStatusBarNotification());
503
504 NotificationMenuRowPlugin.MenuItem menuItem = menuRow.getLongpressMenuItem(mContext);
505 assertNotNull(menuItem);
506 return menuItem;
507 }
508}