blob: 02a618b7f82a5b6e64b110b3f184433033ffa5a3 [file] [log] [blame]
Geoffrey Pitschdf44b602017-02-03 13:31:50 -05001/*
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
Rohan Shah20790b82018-07-02 17:21:04 -070014 * limitations under the License
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050015 */
16
Rohan Shah20790b82018-07-02 17:21:04 -070017package com.android.systemui.statusbar.notification.row;
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050018
Julia Reynoldsc65656a2018-02-12 09:55:14 -050019import static android.app.NotificationChannel.USER_LOCKED_IMPORTANCE;
Julia Reynolds437cdb12018-01-03 12:27:24 -050020import static android.app.NotificationManager.IMPORTANCE_LOW;
Julia Reynoldse0341482018-03-08 14:42:50 -050021import static android.app.NotificationManager.IMPORTANCE_MIN;
22import static android.app.NotificationManager.IMPORTANCE_NONE;
Julia Reynoldsc65656a2018-02-12 09:55:14 -050023import static android.app.NotificationManager.IMPORTANCE_UNSPECIFIED;
Julia Reynoldsf7321592017-05-24 16:09:19 -040024import static android.print.PrintManager.PRINT_SPOOLER_PACKAGE_NAME;
Julia Reynolds437cdb12018-01-03 12:27:24 -050025import static android.view.View.GONE;
26import static android.view.View.VISIBLE;
Julia Reynoldsf7321592017-05-24 16:09:19 -040027
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050028import static junit.framework.Assert.assertEquals;
29import static junit.framework.Assert.assertFalse;
30import static junit.framework.Assert.assertTrue;
Julia Reynolds5a311932017-03-01 16:33:44 -050031
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050032import static org.mockito.Mockito.any;
33import static org.mockito.Mockito.anyBoolean;
34import static org.mockito.Mockito.anyInt;
35import static org.mockito.Mockito.anyString;
Rohan Shahda5dcdd2018-04-27 17:21:50 -070036import static org.mockito.Mockito.doCallRealMethod;
37import static org.mockito.Mockito.doNothing;
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050038import static org.mockito.Mockito.eq;
39import static org.mockito.Mockito.mock;
40import static org.mockito.Mockito.never;
Rohan Shahda5dcdd2018-04-27 17:21:50 -070041import static org.mockito.Mockito.spy;
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050042import static org.mockito.Mockito.times;
43import static org.mockito.Mockito.verify;
44import static org.mockito.Mockito.when;
45
46import android.app.INotificationManager;
Julia Reynolds3aedded2017-03-31 14:42:09 -040047import android.app.Notification;
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050048import android.app.NotificationChannel;
49import android.app.NotificationChannelGroup;
Julia Reynolds3aedded2017-03-31 14:42:09 -040050import android.content.Intent;
51import android.content.pm.ActivityInfo;
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050052import android.content.pm.ApplicationInfo;
53import android.content.pm.PackageInfo;
54import android.content.pm.PackageManager;
Julia Reynolds3aedded2017-03-31 14:42:09 -040055import android.content.pm.ResolveInfo;
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050056import android.graphics.drawable.Drawable;
Rohan Shahda5dcdd2018-04-27 17:21:50 -070057import android.os.IBinder;
Julia Reynolds3aedded2017-03-31 14:42:09 -040058import android.os.UserHandle;
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050059import android.service.notification.StatusBarNotification;
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050060import android.test.suitebuilder.annotation.SmallTest;
Jason Monk340b0e52017-03-08 14:57:56 -050061import android.testing.AndroidTestingRunner;
Julia Reynolds437cdb12018-01-03 12:27:24 -050062import android.testing.PollingCheck;
Rohan Shahca0447e2018-03-30 15:18:27 -070063import android.testing.TestableLooper;
Jason Monk340b0e52017-03-08 14:57:56 -050064import android.testing.UiThreadTest;
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050065import android.view.LayoutInflater;
66import android.view.View;
67import android.widget.ImageView;
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050068import android.widget.TextView;
Jason Monk340b0e52017-03-08 14:57:56 -050069
Rohan Shahda5dcdd2018-04-27 17:21:50 -070070import com.android.internal.logging.MetricsLogger;
Rohan Shahca0447e2018-03-30 15:18:27 -070071import com.android.systemui.Dependency;
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050072import com.android.systemui.R;
73import com.android.systemui.SysuiTestCase;
Jason Monkba364322017-03-06 11:19:20 -050074
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050075import org.junit.Before;
Rohan Shah524cf7b2018-03-15 14:40:02 -070076import org.junit.Rule;
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050077import org.junit.Test;
Jason Monk340b0e52017-03-08 14:57:56 -050078import org.junit.runner.RunWith;
Julia Reynolds8ceb5792017-04-11 11:32:44 -040079import org.mockito.ArgumentCaptor;
Rohan Shah524cf7b2018-03-15 14:40:02 -070080import org.mockito.Mock;
81import org.mockito.junit.MockitoJUnit;
82import org.mockito.junit.MockitoRule;
Julia Reynolds3aedded2017-03-31 14:42:09 -040083
84import java.util.ArrayList;
Geoffrey Pitschd0856f02017-02-16 10:51:18 -050085import java.util.List;
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050086import java.util.concurrent.CountDownLatch;
87
88@SmallTest
Jason Monk340b0e52017-03-08 14:57:56 -050089@RunWith(AndroidTestingRunner.class)
Rohan Shahca0447e2018-03-30 15:18:27 -070090@TestableLooper.RunWithLooper
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050091public class NotificationInfoTest extends SysuiTestCase {
92 private static final String TEST_PACKAGE_NAME = "test_package";
Julia Reynoldsf7321592017-05-24 16:09:19 -040093 private static final String TEST_SYSTEM_PACKAGE_NAME = PRINT_SPOOLER_PACKAGE_NAME;
Geoffrey Pitschd034d292017-05-12 11:59:20 -040094 private static final int TEST_UID = 1;
Rohan Shah63411fc2018-03-28 19:05:52 -070095 private static final int MULTIPLE_CHANNEL_COUNT = 2;
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050096 private static final String TEST_CHANNEL = "test_channel";
97 private static final String TEST_CHANNEL_NAME = "TEST CHANNEL NAME";
98
Rohan Shahca0447e2018-03-30 15:18:27 -070099 private TestableLooper mTestableLooper;
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500100 private NotificationInfo mNotificationInfo;
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500101 private NotificationChannel mNotificationChannel;
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500102 private NotificationChannel mDefaultNotificationChannel;
Julia Reynolds3aedded2017-03-31 14:42:09 -0400103 private StatusBarNotification mSbn;
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500104
Rohan Shah524cf7b2018-03-15 14:40:02 -0700105 @Rule public MockitoRule mockito = MockitoJUnit.rule();
Rohan Shahda5dcdd2018-04-27 17:21:50 -0700106 @Mock private MetricsLogger mMetricsLogger;
Rohan Shah524cf7b2018-03-15 14:40:02 -0700107 @Mock private INotificationManager mMockINotificationManager;
108 @Mock private PackageManager mMockPackageManager;
109 @Mock private NotificationBlockingHelperManager mBlockingHelperManager;
110
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500111 @Before
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500112 public void setUp() throws Exception {
Rohan Shah524cf7b2018-03-15 14:40:02 -0700113 mDependency.injectTestDependency(
114 NotificationBlockingHelperManager.class,
115 mBlockingHelperManager);
Rohan Shahca0447e2018-03-30 15:18:27 -0700116 mTestableLooper = TestableLooper.get(this);
117 mDependency.injectTestDependency(Dependency.BG_LOOPER, mTestableLooper.getLooper());
Rohan Shahda5dcdd2018-04-27 17:21:50 -0700118 mDependency.injectTestDependency(MetricsLogger.class, mMetricsLogger);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500119 // Inflate the layout
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500120 final LayoutInflater layoutInflater = LayoutInflater.from(mContext);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500121 mNotificationInfo = (NotificationInfo) layoutInflater.inflate(R.layout.notification_info,
122 null);
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500123 mNotificationInfo.setGutsParent(mock(NotificationGuts.class));
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500124
125 // PackageManager must return a packageInfo and applicationInfo.
126 final PackageInfo packageInfo = new PackageInfo();
127 packageInfo.packageName = TEST_PACKAGE_NAME;
Julia Reynoldsf7321592017-05-24 16:09:19 -0400128 when(mMockPackageManager.getPackageInfo(eq(TEST_PACKAGE_NAME), anyInt()))
129 .thenReturn(packageInfo);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500130 final ApplicationInfo applicationInfo = new ApplicationInfo();
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400131 applicationInfo.uid = TEST_UID; // non-zero
Julia Reynoldsac98aea2018-10-25 16:54:27 -0400132 when(mMockPackageManager.getApplicationInfo(eq(TEST_PACKAGE_NAME), anyInt())).thenReturn(
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500133 applicationInfo);
Julia Reynoldsf7321592017-05-24 16:09:19 -0400134 final PackageInfo systemPackageInfo = new PackageInfo();
135 systemPackageInfo.packageName = TEST_SYSTEM_PACKAGE_NAME;
136 when(mMockPackageManager.getPackageInfo(eq(TEST_SYSTEM_PACKAGE_NAME), anyInt()))
137 .thenReturn(systemPackageInfo);
138 when(mMockPackageManager.getPackageInfo(eq("android"), anyInt()))
139 .thenReturn(packageInfo);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500140
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500141 // Package has one channel by default.
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500142 when(mMockINotificationManager.getNumNotificationChannelsForPackage(
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400143 eq(TEST_PACKAGE_NAME), eq(TEST_UID), anyBoolean())).thenReturn(1);
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500144
145 // Some test channels.
146 mNotificationChannel = new NotificationChannel(
Julia Reynolds437cdb12018-01-03 12:27:24 -0500147 TEST_CHANNEL, TEST_CHANNEL_NAME, IMPORTANCE_LOW);
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500148 mDefaultNotificationChannel = new NotificationChannel(
149 NotificationChannel.DEFAULT_CHANNEL_ID, TEST_CHANNEL_NAME,
Julia Reynolds437cdb12018-01-03 12:27:24 -0500150 IMPORTANCE_LOW);
Julia Reynolds33bef2c2017-09-05 11:07:18 -0400151 mSbn = new StatusBarNotification(TEST_PACKAGE_NAME, TEST_PACKAGE_NAME, 0, null, TEST_UID, 0,
Julia Reynolds3aedded2017-03-31 14:42:09 -0400152 new Notification(), UserHandle.CURRENT, null, 0);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500153 }
154
Julia Reynolds437cdb12018-01-03 12:27:24 -0500155 // TODO: if tests are taking too long replace this with something that makes the animation
156 // finish instantly.
157 private void waitForUndoButton() {
158 PollingCheck.waitFor(1000,
159 () -> VISIBLE == mNotificationInfo.findViewById(R.id.confirmation).getVisibility());
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500160 }
Gus Prevas533836a2018-09-24 17:15:32 -0400161 private void ensureNoUndoButton() {
162 PollingCheck.waitFor(1000,
163 () -> GONE == mNotificationInfo.findViewById(R.id.confirmation).getVisibility()
164 && !mNotificationInfo.isAnimating());
165 }
Julia Reynolds437cdb12018-01-03 12:27:24 -0500166 private void waitForStopButton() {
167 PollingCheck.waitFor(1000,
168 () -> VISIBLE == mNotificationInfo.findViewById(R.id.prompt).getVisibility());
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500169 }
170
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500171 @Test
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500172 public void testBindNotification_SetsTextApplicationName() throws Exception {
173 when(mMockPackageManager.getApplicationLabel(any())).thenReturn("App Name");
174 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds35765d82018-08-17 11:39:19 -0400175 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true, false);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500176 final TextView textView = mNotificationInfo.findViewById(R.id.pkgname);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500177 assertTrue(textView.getText().toString().contains("App Name"));
Julia Reynoldse0341482018-03-08 14:42:50 -0500178 assertEquals(VISIBLE, mNotificationInfo.findViewById(R.id.header).getVisibility());
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500179 }
180
181 @Test
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500182 public void testBindNotification_SetsPackageIcon() throws Exception {
183 final Drawable iconDrawable = mock(Drawable.class);
184 when(mMockPackageManager.getApplicationIcon(any(ApplicationInfo.class)))
185 .thenReturn(iconDrawable);
186 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds35765d82018-08-17 11:39:19 -0400187 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true, false);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500188 final ImageView iconView = mNotificationInfo.findViewById(R.id.pkgicon);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500189 assertEquals(iconDrawable, iconView.getDrawable());
190 }
191
192 @Test
Julia Reynoldsac98aea2018-10-25 16:54:27 -0400193 public void testBindNotification_noDelegate() throws Exception {
194 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
195 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true, false);
196 final TextView nameView = mNotificationInfo.findViewById(R.id.delegate_name);
197 assertEquals(GONE, nameView.getVisibility());
198 final TextView dividerView = mNotificationInfo.findViewById(R.id.pkg_divider);
199 assertEquals(GONE, dividerView.getVisibility());
200 }
201
202 @Test
203 public void testBindNotification_delegate() throws Exception {
204 mSbn = new StatusBarNotification(TEST_PACKAGE_NAME, "other", 0, null, TEST_UID, 0,
205 new Notification(), UserHandle.CURRENT, null, 0);
206 final ApplicationInfo applicationInfo = new ApplicationInfo();
207 applicationInfo.uid = 7; // non-zero
208 when(mMockPackageManager.getApplicationInfo(eq("other"), anyInt())).thenReturn(
209 applicationInfo);
210 when(mMockPackageManager.getApplicationLabel(any())).thenReturn("Other");
211
212 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
213 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true, false);
214 final TextView nameView = mNotificationInfo.findViewById(R.id.delegate_name);
215 assertEquals(VISIBLE, nameView.getVisibility());
216 assertTrue(nameView.getText().toString().contains("Other"));
217 final TextView dividerView = mNotificationInfo.findViewById(R.id.pkg_divider);
218 assertEquals(VISIBLE, dividerView.getVisibility());
219 }
220
221 @Test
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500222 public void testBindNotification_GroupNameHiddenIfNoGroup() throws Exception {
223 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds35765d82018-08-17 11:39:19 -0400224 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true, false);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500225 final TextView groupNameView = mNotificationInfo.findViewById(R.id.group_name);
226 assertEquals(GONE, groupNameView.getVisibility());
227 final TextView groupDividerView = mNotificationInfo.findViewById(R.id.pkg_group_divider);
228 assertEquals(GONE, groupDividerView.getVisibility());
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500229 }
230
231 @Test
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500232 public void testBindNotification_SetsGroupNameIfNonNull() throws Exception {
233 mNotificationChannel.setGroup("test_group_id");
234 final NotificationChannelGroup notificationChannelGroup =
235 new NotificationChannelGroup("test_group_id", "Test Group Name");
236 when(mMockINotificationManager.getNotificationChannelGroupForPackage(
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400237 eq("test_group_id"), eq(TEST_PACKAGE_NAME), eq(TEST_UID)))
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500238 .thenReturn(notificationChannelGroup);
239 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds35765d82018-08-17 11:39:19 -0400240 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true, false);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500241 final TextView groupNameView = mNotificationInfo.findViewById(R.id.group_name);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500242 assertEquals(View.VISIBLE, groupNameView.getVisibility());
243 assertEquals("Test Group Name", groupNameView.getText());
Julia Reynolds437cdb12018-01-03 12:27:24 -0500244 final TextView groupDividerView = mNotificationInfo.findViewById(R.id.pkg_group_divider);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500245 assertEquals(View.VISIBLE, groupDividerView.getVisibility());
246 }
247
248 @Test
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500249 public void testBindNotification_SetsTextChannelName() throws Exception {
250 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds35765d82018-08-17 11:39:19 -0400251 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true, false);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500252 final TextView textView = mNotificationInfo.findViewById(R.id.channel_name);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500253 assertEquals(TEST_CHANNEL_NAME, textView.getText());
254 }
255
256 @Test
Geoffrey Pitsch1fbf66e2017-05-09 11:32:27 -0400257 public void testBindNotification_DefaultChannelDoesNotUseChannelName() throws Exception {
258 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds35765d82018-08-17 11:39:19 -0400259 TEST_PACKAGE_NAME, mDefaultNotificationChannel, 1, mSbn, null, null, null, true,
260 false);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500261 final TextView textView = mNotificationInfo.findViewById(R.id.channel_name);
262 assertEquals(GONE, textView.getVisibility());
Geoffrey Pitsch1fbf66e2017-05-09 11:32:27 -0400263 }
264
265 @Test
Rohan Shahdbd64e72018-03-28 14:46:50 -0700266 public void testBindNotification_DefaultChannelUsesChannelNameIfMoreChannelsExist()
267 throws Exception {
268 // Package has one channel by default.
269 when(mMockINotificationManager.getNumNotificationChannelsForPackage(
270 eq(TEST_PACKAGE_NAME), eq(TEST_UID), anyBoolean())).thenReturn(10);
271 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds35765d82018-08-17 11:39:19 -0400272 TEST_PACKAGE_NAME, mDefaultNotificationChannel, 1, mSbn, null, null, null, true,
273 false);
Rohan Shahdbd64e72018-03-28 14:46:50 -0700274 final TextView textView = mNotificationInfo.findViewById(R.id.channel_name);
275 assertEquals(VISIBLE, textView.getVisibility());
276 }
277
278 @Test
Julia Reynolds437cdb12018-01-03 12:27:24 -0500279 public void testBindNotification_UnblockablePackageUsesChannelName() throws Exception {
Geoffrey Pitschb43486a2017-06-01 13:45:59 -0400280 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds35765d82018-08-17 11:39:19 -0400281 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true, true);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500282 final TextView textView = mNotificationInfo.findViewById(R.id.channel_name);
283 assertEquals(VISIBLE, textView.getVisibility());
Geoffrey Pitsch1fbf66e2017-05-09 11:32:27 -0400284 }
285
286 @Test
Julia Reynoldse0341482018-03-08 14:42:50 -0500287 public void testBindNotification_BlockButton() throws Exception {
288 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds35765d82018-08-17 11:39:19 -0400289 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true, false);
Julia Reynoldse0341482018-03-08 14:42:50 -0500290 final View block = mNotificationInfo.findViewById(R.id.block);
291 final View minimize = mNotificationInfo.findViewById(R.id.minimize);
292 assertEquals(VISIBLE, block.getVisibility());
293 assertEquals(GONE, minimize.getVisibility());
294 }
295
296 @Test
297 public void testBindNotification_MinButton() throws Exception {
298 mSbn.getNotification().flags = Notification.FLAG_FOREGROUND_SERVICE;
299 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds35765d82018-08-17 11:39:19 -0400300 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true, false);
Julia Reynoldse0341482018-03-08 14:42:50 -0500301 final View block = mNotificationInfo.findViewById(R.id.block);
302 final View minimize = mNotificationInfo.findViewById(R.id.minimize);
303 assertEquals(GONE, block.getVisibility());
304 assertEquals(VISIBLE, minimize.getVisibility());
305 }
306
307 @Test
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500308 public void testBindNotification_SetsOnClickListenerForSettings() throws Exception {
309 final CountDownLatch latch = new CountDownLatch(1);
310 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds437cdb12018-01-03 12:27:24 -0500311 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null,
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500312 (View v, NotificationChannel c, int appUid) -> {
Julia Reynolds3aedded2017-03-31 14:42:09 -0400313 assertEquals(mNotificationChannel, c);
314 latch.countDown();
Julia Reynolds35765d82018-08-17 11:39:19 -0400315 }, null, true, false);
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500316
Julia Reynolds437cdb12018-01-03 12:27:24 -0500317 final View settingsButton = mNotificationInfo.findViewById(R.id.info);
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500318 settingsButton.performClick();
319 // Verify that listener was triggered.
320 assertEquals(0, latch.getCount());
321 }
322
323 @Test
Geoffrey Pitsch029a3fa2017-03-30 13:01:57 -0400324 public void testBindNotification_SettingsButtonInvisibleWhenNoClickListener() throws Exception {
325 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds35765d82018-08-17 11:39:19 -0400326 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true, false);
327 final View settingsButton = mNotificationInfo.findViewById(R.id.info);
328 assertTrue(settingsButton.getVisibility() != View.VISIBLE);
329 }
330
331 @Test
332 public void testBindNotification_SettingsButtonInvisibleWhenDeviceUnprovisioned()
333 throws Exception {
334 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
335 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null,
336 (View v, NotificationChannel c, int appUid) -> {
337 assertEquals(mNotificationChannel, c);
338 }, null, false, false);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500339 final View settingsButton = mNotificationInfo.findViewById(R.id.info);
Geoffrey Pitsch029a3fa2017-03-30 13:01:57 -0400340 assertTrue(settingsButton.getVisibility() != View.VISIBLE);
341 }
342
343 @Test
Julia Reynolds437cdb12018-01-03 12:27:24 -0500344 public void testBindNotification_SettingsButtonReappearsAfterSecondBind() throws Exception {
Geoffrey Pitsch029a3fa2017-03-30 13:01:57 -0400345 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds35765d82018-08-17 11:39:19 -0400346 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true, false);
Geoffrey Pitsch029a3fa2017-03-30 13:01:57 -0400347 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds437cdb12018-01-03 12:27:24 -0500348 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null,
349 (View v, NotificationChannel c, int appUid) -> {
Julia Reynolds35765d82018-08-17 11:39:19 -0400350 }, null, true, false);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500351 final View settingsButton = mNotificationInfo.findViewById(R.id.info);
Geoffrey Pitsch029a3fa2017-03-30 13:01:57 -0400352 assertEquals(View.VISIBLE, settingsButton.getVisibility());
353 }
354
355 @Test
Rohan Shahda5dcdd2018-04-27 17:21:50 -0700356 public void testLogBlockingHelperCounter_doesntLogForNormalGutsView() throws Exception {
357 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds35765d82018-08-17 11:39:19 -0400358 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true, false);
Rohan Shahda5dcdd2018-04-27 17:21:50 -0700359 mNotificationInfo.logBlockingHelperCounter("HowCanNotifsBeRealIfAppsArent");
360 verify(mMetricsLogger, times(0)).count(anyString(), anyInt());
361 }
362
363 @Test
364 public void testLogBlockingHelperCounter_logsForBlockingHelper() throws Exception {
Rohan Shahda5dcdd2018-04-27 17:21:50 -0700365 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
366 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, false, true,
Julia Reynolds35765d82018-08-17 11:39:19 -0400367 true, true);
Rohan Shahda5dcdd2018-04-27 17:21:50 -0700368 mNotificationInfo.logBlockingHelperCounter("HowCanNotifsBeRealIfAppsArent");
Rohan Shahc6b0d542018-05-10 13:00:54 -0700369 verify(mMetricsLogger, times(1)).count(anyString(), anyInt());
Rohan Shahda5dcdd2018-04-27 17:21:50 -0700370 }
371
372 @Test
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500373 public void testOnClickListenerPassesNullChannelForBundle() throws Exception {
374 final CountDownLatch latch = new CountDownLatch(1);
375 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Rohan Shah63411fc2018-03-28 19:05:52 -0700376 TEST_PACKAGE_NAME, mNotificationChannel, MULTIPLE_CHANNEL_COUNT, mSbn, null,
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500377 (View v, NotificationChannel c, int appUid) -> {
Julia Reynolds3aedded2017-03-31 14:42:09 -0400378 assertEquals(null, c);
379 latch.countDown();
Julia Reynolds35765d82018-08-17 11:39:19 -0400380 }, null, true, true);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500381
Julia Reynolds437cdb12018-01-03 12:27:24 -0500382 mNotificationInfo.findViewById(R.id.info).performClick();
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500383 // Verify that listener was triggered.
384 assertEquals(0, latch.getCount());
385 }
386
387 @Test
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500388 @UiThreadTest
Julia Reynolds437cdb12018-01-03 12:27:24 -0500389 public void testBindNotification_ChannelNameInvisibleWhenBundleFromDifferentChannels()
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500390 throws Exception {
391 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Rohan Shah63411fc2018-03-28 19:05:52 -0700392 TEST_PACKAGE_NAME, mNotificationChannel, MULTIPLE_CHANNEL_COUNT, mSbn, null, null,
Julia Reynolds35765d82018-08-17 11:39:19 -0400393 null, true, true);
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500394 final TextView channelNameView =
Julia Reynolds437cdb12018-01-03 12:27:24 -0500395 mNotificationInfo.findViewById(R.id.channel_name);
396 assertEquals(GONE, channelNameView.getVisibility());
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500397 }
398
399 @Test
400 @UiThreadTest
Julia Reynolds437cdb12018-01-03 12:27:24 -0500401 public void testStopInvisibleIfBundleFromDifferentChannels() throws Exception {
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500402 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Rohan Shah63411fc2018-03-28 19:05:52 -0700403 TEST_PACKAGE_NAME, mNotificationChannel, MULTIPLE_CHANNEL_COUNT, mSbn, null, null,
Julia Reynolds35765d82018-08-17 11:39:19 -0400404 null, true, true);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500405 final TextView blockView = mNotificationInfo.findViewById(R.id.block);
406 assertEquals(GONE, blockView.getVisibility());
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500407 }
408
409 @Test
Julia Reynolds0ef7d842018-01-24 17:50:31 -0500410 public void testbindNotification_BlockingHelper() throws Exception {
411 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Rohan Shah63411fc2018-03-28 19:05:52 -0700412 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, false, false,
Julia Reynolds35765d82018-08-17 11:39:19 -0400413 true, true);
Julia Reynolds0ef7d842018-01-24 17:50:31 -0500414 final TextView view = mNotificationInfo.findViewById(R.id.block_prompt);
415 assertEquals(View.VISIBLE, view.getVisibility());
416 assertEquals(mContext.getString(R.string.inline_blocking_helper), view.getText());
417 }
418
419 @Test
Geoffrey Pitschb43486a2017-06-01 13:45:59 -0400420 public void testbindNotification_UnblockableTextVisibleWhenAppUnblockable() throws Exception {
421 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds35765d82018-08-17 11:39:19 -0400422 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true, true);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500423 final TextView view = mNotificationInfo.findViewById(R.id.block_prompt);
424 assertEquals(View.VISIBLE, view.getVisibility());
Geoffrey Pitschb43486a2017-06-01 13:45:59 -0400425 assertEquals(mContext.getString(R.string.notification_unblockable_desc),
Julia Reynolds437cdb12018-01-03 12:27:24 -0500426 view.getText());
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500427 }
428
429 @Test
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500430 public void testBindNotification_DoesNotUpdateNotificationChannel() throws Exception {
431 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds35765d82018-08-17 11:39:19 -0400432 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true, false);
Rohan Shahca0447e2018-03-30 15:18:27 -0700433 mTestableLooper.processAllMessages();
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500434 verify(mMockINotificationManager, never()).updateNotificationChannelForPackage(
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400435 anyString(), eq(TEST_UID), any());
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500436 }
437
438 @Test
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500439 public void testDoesNotUpdateNotificationChannelAfterImportanceChanged() throws Exception {
Julia Reynolds437cdb12018-01-03 12:27:24 -0500440 mNotificationChannel.setImportance(IMPORTANCE_LOW);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500441 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds35765d82018-08-17 11:39:19 -0400442 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true, false);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500443
Julia Reynolds437cdb12018-01-03 12:27:24 -0500444 mNotificationInfo.findViewById(R.id.block).performClick();
Rohan Shahca0447e2018-03-30 15:18:27 -0700445 mTestableLooper.processAllMessages();
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500446 verify(mMockINotificationManager, never()).updateNotificationChannelForPackage(
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400447 anyString(), eq(TEST_UID), any());
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500448 }
449
450 @Test
Julia Reynoldse0341482018-03-08 14:42:50 -0500451 public void testDoesNotUpdateNotificationChannelAfterImportanceChangedMin()
452 throws Exception {
453 mNotificationChannel.setImportance(IMPORTANCE_LOW);
454 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds35765d82018-08-17 11:39:19 -0400455 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true, false);
Julia Reynoldse0341482018-03-08 14:42:50 -0500456
457 mNotificationInfo.findViewById(R.id.minimize).performClick();
Rohan Shahca0447e2018-03-30 15:18:27 -0700458 mTestableLooper.processAllMessages();
Julia Reynoldse0341482018-03-08 14:42:50 -0500459 verify(mMockINotificationManager, never()).updateNotificationChannelForPackage(
460 anyString(), eq(TEST_UID), any());
461 }
462
463 @Test
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500464 public void testHandleCloseControls_DoesNotUpdateNotificationChannelIfUnchanged()
465 throws Exception {
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500466 int originalImportance = mNotificationChannel.getImportance();
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500467 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds35765d82018-08-17 11:39:19 -0400468 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true, false);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500469
Mady Mellorc2dbe492017-03-30 13:22:03 -0700470 mNotificationInfo.handleCloseControls(true, false);
Rohan Shahca0447e2018-03-30 15:18:27 -0700471 mTestableLooper.processAllMessages();
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500472 verify(mMockINotificationManager, times(1)).updateNotificationChannelForPackage(
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400473 anyString(), eq(TEST_UID), any());
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500474 assertEquals(originalImportance, mNotificationChannel.getImportance());
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500475 }
476
477 @Test
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500478 public void testHandleCloseControls_DoesNotUpdateNotificationChannelIfUnspecified()
479 throws Exception {
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500480 mNotificationChannel.setImportance(IMPORTANCE_UNSPECIFIED);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500481 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds35765d82018-08-17 11:39:19 -0400482 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true, false);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500483
Mady Mellorc2dbe492017-03-30 13:22:03 -0700484 mNotificationInfo.handleCloseControls(true, false);
Rohan Shahca0447e2018-03-30 15:18:27 -0700485
486 mTestableLooper.processAllMessages();
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500487 verify(mMockINotificationManager, times(1)).updateNotificationChannelForPackage(
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400488 anyString(), eq(TEST_UID), any());
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500489 assertEquals(IMPORTANCE_UNSPECIFIED, mNotificationChannel.getImportance());
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500490 }
491
492 @Test
Rohan Shah590e1b22018-04-10 23:48:47 -0400493 public void testHandleCloseControls_setsNotificationsDisabledForMultipleChannelNotifications()
494 throws Exception {
495 mNotificationChannel.setImportance(IMPORTANCE_LOW);
496 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
497 TEST_PACKAGE_NAME, mNotificationChannel /* notificationChannel */,
498 10 /* numUniqueChannelsInRow */, mSbn, null /* checkSaveListener */,
499 null /* onSettingsClick */, null /* onAppSettingsClick */ ,
Julia Reynolds35765d82018-08-17 11:39:19 -0400500 true, false /* isNonblockable */);
Rohan Shah590e1b22018-04-10 23:48:47 -0400501
502 mNotificationInfo.findViewById(R.id.block).performClick();
503 waitForUndoButton();
504 mNotificationInfo.handleCloseControls(true, false);
505
506 mTestableLooper.processAllMessages();
507 verify(mMockINotificationManager, times(1))
508 .setNotificationsEnabledWithImportanceLockForPackage(
509 anyString(), eq(TEST_UID), eq(false));
510 }
511
512
513 @Test
514 public void testHandleCloseControls_keepsNotificationsEnabledForMultipleChannelNotifications()
515 throws Exception {
516 mNotificationChannel.setImportance(IMPORTANCE_LOW);
517 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
518 TEST_PACKAGE_NAME, mNotificationChannel /* notificationChannel */,
519 10 /* numUniqueChannelsInRow */, mSbn, null /* checkSaveListener */,
520 null /* onSettingsClick */, null /* onAppSettingsClick */ ,
Julia Reynolds35765d82018-08-17 11:39:19 -0400521 true, false /* isNonblockable */);
Rohan Shah590e1b22018-04-10 23:48:47 -0400522
523 mNotificationInfo.findViewById(R.id.block).performClick();
524 waitForUndoButton();
525 mNotificationInfo.handleCloseControls(true, false);
526
527 mTestableLooper.processAllMessages();
528 verify(mMockINotificationManager, times(1))
529 .setNotificationsEnabledWithImportanceLockForPackage(
530 anyString(), eq(TEST_UID), eq(false));
531 }
532
533 @Test
534 public void testCloseControls_blockingHelperSavesImportanceForMultipleChannelNotifications()
535 throws Exception {
536 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
537 TEST_PACKAGE_NAME, mNotificationChannel /* notificationChannel */,
538 10 /* numUniqueChannelsInRow */, mSbn, null /* checkSaveListener */,
539 null /* onSettingsClick */, null /* onAppSettingsClick */ ,
Julia Reynolds35765d82018-08-17 11:39:19 -0400540 true /* provisioned */,
Rohan Shah590e1b22018-04-10 23:48:47 -0400541 false /* isNonblockable */, true /* isForBlockingHelper */,
542 true /* isUserSentimentNegative */);
543
Rohan Shahda5dcdd2018-04-27 17:21:50 -0700544 NotificationGuts guts = spy(new NotificationGuts(mContext, null));
545 when(guts.getWindowToken()).thenReturn(mock(IBinder.class));
546 doNothing().when(guts).animateClose(anyInt(), anyInt(), anyBoolean());
547 doNothing().when(guts).setExposed(anyBoolean(), anyBoolean());
548 guts.setGutsContent(mNotificationInfo);
549 mNotificationInfo.setGutsParent(guts);
550
Rohan Shah590e1b22018-04-10 23:48:47 -0400551 mNotificationInfo.findViewById(R.id.keep).performClick();
552
553 verify(mBlockingHelperManager).dismissCurrentBlockingHelper();
554 mTestableLooper.processAllMessages();
555 verify(mMockINotificationManager, times(1))
556 .setNotificationsEnabledWithImportanceLockForPackage(
557 anyString(), eq(TEST_UID), eq(true));
558 }
559
Rohan Shahdd588c72018-05-09 20:32:15 -0700560 @Test
561 public void testCloseControls_nonNullCheckSaveListenerDoesntDelayKeepShowing()
562 throws Exception {
563 NotificationInfo.CheckSaveListener listener =
564 mock(NotificationInfo.CheckSaveListener.class);
565 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
566 TEST_PACKAGE_NAME, mNotificationChannel /* notificationChannel */,
567 10 /* numUniqueChannelsInRow */, mSbn, listener /* checkSaveListener */,
Julia Reynolds35765d82018-08-17 11:39:19 -0400568 null /* onSettingsClick */, null /* onAppSettingsClick */ , true /* provisioned */,
Rohan Shahdd588c72018-05-09 20:32:15 -0700569 false /* isNonblockable */, true /* isForBlockingHelper */,
570 true /* isUserSentimentNegative */);
571
572 NotificationGuts guts = spy(new NotificationGuts(mContext, null));
573 when(guts.getWindowToken()).thenReturn(mock(IBinder.class));
574 doNothing().when(guts).animateClose(anyInt(), anyInt(), anyBoolean());
575 doNothing().when(guts).setExposed(anyBoolean(), anyBoolean());
576 guts.setGutsContent(mNotificationInfo);
577 mNotificationInfo.setGutsParent(guts);
578
579 mNotificationInfo.findViewById(R.id.keep).performClick();
580
581 verify(mBlockingHelperManager).dismissCurrentBlockingHelper();
582 mTestableLooper.processAllMessages();
583 verify(mMockINotificationManager, times(1))
584 .setNotificationsEnabledWithImportanceLockForPackage(
585 anyString(), eq(TEST_UID), eq(true));
586 }
587
Rohan Shah593e8f12018-05-22 10:06:23 -0700588 @Test
589 public void testCloseControls_nonNullCheckSaveListenerDoesntDelayDismiss()
590 throws Exception {
591 NotificationInfo.CheckSaveListener listener =
592 mock(NotificationInfo.CheckSaveListener.class);
593 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
594 TEST_PACKAGE_NAME, mNotificationChannel /* notificationChannel */,
595 10 /* numUniqueChannelsInRow */, mSbn, listener /* checkSaveListener */,
596 null /* onSettingsClick */, null /* onAppSettingsClick */ ,
597 false /* isNonblockable */, true /* isForBlockingHelper */,
Julia Reynolds35765d82018-08-17 11:39:19 -0400598 true, true /* isUserSentimentNegative */);
Rohan Shah593e8f12018-05-22 10:06:23 -0700599
600 mNotificationInfo.handleCloseControls(true /* save */, false /* force */);
601
602 mTestableLooper.processAllMessages();
603 verify(listener, times(0)).checkSave(any(Runnable.class), eq(mSbn));
604 }
605
606 @Test
607 public void testCloseControls_checkSaveListenerDelaysStopNotifications()
608 throws Exception {
609 NotificationInfo.CheckSaveListener listener =
610 mock(NotificationInfo.CheckSaveListener.class);
611 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
612 TEST_PACKAGE_NAME, mNotificationChannel /* notificationChannel */,
613 10 /* numUniqueChannelsInRow */, mSbn, listener /* checkSaveListener */,
Julia Reynolds35765d82018-08-17 11:39:19 -0400614 null /* onSettingsClick */, null /* onAppSettingsClick */,
615 true /* provisioned */,
Rohan Shah593e8f12018-05-22 10:06:23 -0700616 false /* isNonblockable */, true /* isForBlockingHelper */,
617 true /* isUserSentimentNegative */);
618
619 mNotificationInfo.findViewById(R.id.block).performClick();
Rohan Shah593e8f12018-05-22 10:06:23 -0700620 mTestableLooper.processAllMessages();
621 verify(listener).checkSave(any(Runnable.class), eq(mSbn));
622 }
Rohan Shahdd588c72018-05-09 20:32:15 -0700623
Rohan Shah590e1b22018-04-10 23:48:47 -0400624 @Test
Rohan Shah524cf7b2018-03-15 14:40:02 -0700625 public void testCloseControls_blockingHelperDismissedIfShown() throws Exception {
626 mNotificationInfo.bindNotification(
627 mMockPackageManager,
628 mMockINotificationManager,
629 TEST_PACKAGE_NAME,
630 mNotificationChannel,
631 1 /* numChannels */,
632 mSbn,
633 null /* checkSaveListener */,
634 null /* onSettingsClick */,
635 null /* onAppSettingsClick */,
Rohan Shah63411fc2018-03-28 19:05:52 -0700636 false /* isNonblockable */,
Rohan Shah524cf7b2018-03-15 14:40:02 -0700637 true /* isForBlockingHelper */,
Julia Reynolds35765d82018-08-17 11:39:19 -0400638 true,
Rohan Shah524cf7b2018-03-15 14:40:02 -0700639 false /* isUserSentimentNegative */);
Rohan Shahda5dcdd2018-04-27 17:21:50 -0700640 NotificationGuts guts = mock(NotificationGuts.class);
641 doCallRealMethod().when(guts).closeControls(anyInt(), anyInt(), anyBoolean(), anyBoolean());
642 mNotificationInfo.setGutsParent(guts);
Rohan Shah524cf7b2018-03-15 14:40:02 -0700643
644 mNotificationInfo.closeControls(mNotificationInfo);
645
646 verify(mBlockingHelperManager).dismissCurrentBlockingHelper();
647 }
648
649 @Test
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500650 public void testNonBlockableAppDoesNotBecomeBlocked() throws Exception {
Julia Reynolds437cdb12018-01-03 12:27:24 -0500651 mNotificationChannel.setImportance(IMPORTANCE_LOW);
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500652 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds35765d82018-08-17 11:39:19 -0400653 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true, true);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500654 mNotificationInfo.findViewById(R.id.block).performClick();
655 waitForUndoButton();
Rohan Shahca0447e2018-03-30 15:18:27 -0700656
657 mTestableLooper.processAllMessages();
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500658 verify(mMockINotificationManager, never()).updateNotificationChannelForPackage(
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400659 anyString(), eq(TEST_UID), any());
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500660 }
661
662 @Test
Julia Reynolds437cdb12018-01-03 12:27:24 -0500663 public void testBlockChangedCallsUpdateNotificationChannel() throws Exception {
664 mNotificationChannel.setImportance(IMPORTANCE_LOW);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500665 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds35765d82018-08-17 11:39:19 -0400666 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true, false);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500667
Julia Reynolds437cdb12018-01-03 12:27:24 -0500668 mNotificationInfo.findViewById(R.id.block).performClick();
669 waitForUndoButton();
Mady Mellorc2dbe492017-03-30 13:22:03 -0700670 mNotificationInfo.handleCloseControls(true, false);
Julia Reynolds8ceb5792017-04-11 11:32:44 -0400671
Rohan Shahca0447e2018-03-30 15:18:27 -0700672 mTestableLooper.processAllMessages();
Julia Reynolds8ceb5792017-04-11 11:32:44 -0400673 ArgumentCaptor<NotificationChannel> updated =
674 ArgumentCaptor.forClass(NotificationChannel.class);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500675 verify(mMockINotificationManager, times(1)).updateNotificationChannelForPackage(
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400676 anyString(), eq(TEST_UID), updated.capture());
Julia Reynolds8ceb5792017-04-11 11:32:44 -0400677 assertTrue((updated.getValue().getUserLockedFields()
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500678 & USER_LOCKED_IMPORTANCE) != 0);
Julia Reynoldse0341482018-03-08 14:42:50 -0500679 assertEquals(IMPORTANCE_NONE, updated.getValue().getImportance());
680 }
681
682 @Test
Rohan Shahca0447e2018-03-30 15:18:27 -0700683 public void testBlockChangedCallsUpdateNotificationChannel_blockingHelper() throws Exception {
684 mNotificationChannel.setImportance(IMPORTANCE_LOW);
685 mNotificationInfo.bindNotification(
686 mMockPackageManager,
687 mMockINotificationManager,
688 TEST_PACKAGE_NAME,
689 mNotificationChannel,
690 1 /* numChannels */,
691 mSbn,
692 null /* checkSaveListener */,
693 null /* onSettingsClick */,
694 null /* onAppSettingsClick */,
Julia Reynolds35765d82018-08-17 11:39:19 -0400695 true /*provisioned */,
Rohan Shahca0447e2018-03-30 15:18:27 -0700696 false /* isNonblockable */,
697 true /* isForBlockingHelper */,
698 true /* isUserSentimentNegative */);
699
700 mNotificationInfo.findViewById(R.id.block).performClick();
701 waitForUndoButton();
702 mNotificationInfo.handleCloseControls(true, false);
703
704 mTestableLooper.processAllMessages();
705 ArgumentCaptor<NotificationChannel> updated =
706 ArgumentCaptor.forClass(NotificationChannel.class);
707 verify(mMockINotificationManager, times(1)).updateNotificationChannelForPackage(
708 anyString(), eq(TEST_UID), updated.capture());
709 assertTrue((updated.getValue().getUserLockedFields()
710 & USER_LOCKED_IMPORTANCE) != 0);
711 assertEquals(IMPORTANCE_NONE, updated.getValue().getImportance());
712 }
713
714
715 @Test
Julia Reynoldse0341482018-03-08 14:42:50 -0500716 public void testNonBlockableAppDoesNotBecomeMin() throws Exception {
717 mNotificationChannel.setImportance(IMPORTANCE_LOW);
718 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds35765d82018-08-17 11:39:19 -0400719 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true, true);
Julia Reynoldse0341482018-03-08 14:42:50 -0500720 mNotificationInfo.findViewById(R.id.minimize).performClick();
721 waitForUndoButton();
Rohan Shahca0447e2018-03-30 15:18:27 -0700722
723 mTestableLooper.processAllMessages();
Julia Reynoldse0341482018-03-08 14:42:50 -0500724 verify(mMockINotificationManager, never()).updateNotificationChannelForPackage(
725 anyString(), eq(TEST_UID), any());
726 }
727
728 @Test
729 public void testMinChangedCallsUpdateNotificationChannel() throws Exception {
730 mNotificationChannel.setImportance(IMPORTANCE_LOW);
731 mSbn.getNotification().flags = Notification.FLAG_FOREGROUND_SERVICE;
732 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds35765d82018-08-17 11:39:19 -0400733 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true, false);
Julia Reynoldse0341482018-03-08 14:42:50 -0500734
735 mNotificationInfo.findViewById(R.id.minimize).performClick();
736 waitForUndoButton();
737 mNotificationInfo.handleCloseControls(true, false);
738
Rohan Shahca0447e2018-03-30 15:18:27 -0700739 mTestableLooper.processAllMessages();
Julia Reynoldse0341482018-03-08 14:42:50 -0500740 ArgumentCaptor<NotificationChannel> updated =
741 ArgumentCaptor.forClass(NotificationChannel.class);
742 verify(mMockINotificationManager, times(1)).updateNotificationChannelForPackage(
743 anyString(), eq(TEST_UID), updated.capture());
744 assertTrue((updated.getValue().getUserLockedFields()
745 & USER_LOCKED_IMPORTANCE) != 0);
746 assertEquals(IMPORTANCE_MIN, updated.getValue().getImportance());
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500747 }
748
749 @Test
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500750 public void testKeepUpdatesNotificationChannel() throws Exception {
Julia Reynolds437cdb12018-01-03 12:27:24 -0500751 mNotificationChannel.setImportance(IMPORTANCE_LOW);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500752 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds35765d82018-08-17 11:39:19 -0400753 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true, false);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500754
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500755 mNotificationInfo.handleCloseControls(true, false);
756
Rohan Shahca0447e2018-03-30 15:18:27 -0700757 mTestableLooper.processAllMessages();
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500758 ArgumentCaptor<NotificationChannel> updated =
759 ArgumentCaptor.forClass(NotificationChannel.class);
760 verify(mMockINotificationManager, times(1)).updateNotificationChannelForPackage(
761 anyString(), eq(TEST_UID), updated.capture());
762 assertTrue(0 != (mNotificationChannel.getUserLockedFields() & USER_LOCKED_IMPORTANCE));
763 assertEquals(IMPORTANCE_LOW, mNotificationChannel.getImportance());
764 }
765
766 @Test
767 public void testBlockUndoDoesNotBlockNotificationChannel() throws Exception {
768 mNotificationChannel.setImportance(IMPORTANCE_LOW);
769 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds35765d82018-08-17 11:39:19 -0400770 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true, false);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500771
772 mNotificationInfo.findViewById(R.id.block).performClick();
773 waitForUndoButton();
774 mNotificationInfo.findViewById(R.id.undo).performClick();
775 waitForStopButton();
776 mNotificationInfo.handleCloseControls(true, false);
777
Rohan Shahca0447e2018-03-30 15:18:27 -0700778 mTestableLooper.processAllMessages();
Julia Reynolds437cdb12018-01-03 12:27:24 -0500779 ArgumentCaptor<NotificationChannel> updated =
780 ArgumentCaptor.forClass(NotificationChannel.class);
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500781 verify(mMockINotificationManager, times(1)).updateNotificationChannelForPackage(
Julia Reynolds437cdb12018-01-03 12:27:24 -0500782 anyString(), eq(TEST_UID), updated.capture());
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500783 assertTrue(0 != (mNotificationChannel.getUserLockedFields() & USER_LOCKED_IMPORTANCE));
Julia Reynolds437cdb12018-01-03 12:27:24 -0500784 assertEquals(IMPORTANCE_LOW, mNotificationChannel.getImportance());
785 }
786
787 @Test
Julia Reynoldse0341482018-03-08 14:42:50 -0500788 public void testMinUndoDoesNotMinNotificationChannel() throws Exception {
789 mNotificationChannel.setImportance(IMPORTANCE_LOW);
790 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds35765d82018-08-17 11:39:19 -0400791 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true, false);
Julia Reynoldse0341482018-03-08 14:42:50 -0500792
793 mNotificationInfo.findViewById(R.id.minimize).performClick();
794 waitForUndoButton();
795 mNotificationInfo.findViewById(R.id.undo).performClick();
796 waitForStopButton();
797 mNotificationInfo.handleCloseControls(true, false);
798
Rohan Shahca0447e2018-03-30 15:18:27 -0700799 mTestableLooper.processAllMessages();
Julia Reynoldse0341482018-03-08 14:42:50 -0500800 ArgumentCaptor<NotificationChannel> updated =
801 ArgumentCaptor.forClass(NotificationChannel.class);
802 verify(mMockINotificationManager, times(1)).updateNotificationChannelForPackage(
803 anyString(), eq(TEST_UID), updated.capture());
804 assertTrue(0 != (mNotificationChannel.getUserLockedFields() & USER_LOCKED_IMPORTANCE));
805 assertEquals(IMPORTANCE_LOW, mNotificationChannel.getImportance());
806 }
807
808 @Test
809 public void testCloseControlsDoesNotUpdateiMinIfSaveIsFalse() throws Exception {
810 mNotificationChannel.setImportance(IMPORTANCE_LOW);
811 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds35765d82018-08-17 11:39:19 -0400812 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true, true);
Julia Reynoldse0341482018-03-08 14:42:50 -0500813
814 mNotificationInfo.findViewById(R.id.minimize).performClick();
815 waitForUndoButton();
816 mNotificationInfo.handleCloseControls(false, false);
Rohan Shahca0447e2018-03-30 15:18:27 -0700817
818 mTestableLooper.processAllMessages();
Julia Reynoldse0341482018-03-08 14:42:50 -0500819 verify(mMockINotificationManager, never()).updateNotificationChannelForPackage(
820 eq(TEST_PACKAGE_NAME), eq(TEST_UID), eq(mNotificationChannel));
821 }
822
823 @Test
Julia Reynolds437cdb12018-01-03 12:27:24 -0500824 public void testCloseControlsDoesNotUpdateIfSaveIsFalse() throws Exception {
825 mNotificationChannel.setImportance(IMPORTANCE_LOW);
826 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds35765d82018-08-17 11:39:19 -0400827 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true, true);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500828
829 mNotificationInfo.findViewById(R.id.block).performClick();
830 waitForUndoButton();
Mady Mellorc2dbe492017-03-30 13:22:03 -0700831 mNotificationInfo.handleCloseControls(false, false);
Rohan Shahca0447e2018-03-30 15:18:27 -0700832
833 mTestableLooper.processAllMessages();
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500834 verify(mMockINotificationManager, never()).updateNotificationChannelForPackage(
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400835 eq(TEST_PACKAGE_NAME), eq(TEST_UID), eq(mNotificationChannel));
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500836 }
Geoffrey Pitsch5278d3d2017-03-29 13:39:10 -0400837
838 @Test
Gus Prevas533836a2018-09-24 17:15:32 -0400839 public void testBlockDoesNothingIfCheckSaveListenerIsNoOp() throws Exception {
Julia Reynolds437cdb12018-01-03 12:27:24 -0500840 mNotificationChannel.setImportance(IMPORTANCE_LOW);
Geoffrey Pitsch5278d3d2017-03-29 13:39:10 -0400841 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds437cdb12018-01-03 12:27:24 -0500842 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn,
Eliot Courtney47098cb2017-10-18 17:30:30 +0900843 (Runnable saveImportance, StatusBarNotification sbn) -> {
Julia Reynolds35765d82018-08-17 11:39:19 -0400844 }, null, null, true, true);
Geoffrey Pitsch5278d3d2017-03-29 13:39:10 -0400845
Julia Reynolds437cdb12018-01-03 12:27:24 -0500846 mNotificationInfo.findViewById(R.id.block).performClick();
Gus Prevas533836a2018-09-24 17:15:32 -0400847 mTestableLooper.processAllMessages();
848 ensureNoUndoButton();
Mady Mellorc2dbe492017-03-30 13:22:03 -0700849 mNotificationInfo.handleCloseControls(true, false);
Rohan Shahca0447e2018-03-30 15:18:27 -0700850
Geoffrey Pitsch5278d3d2017-03-29 13:39:10 -0400851 verify(mMockINotificationManager, never()).updateNotificationChannelForPackage(
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400852 eq(TEST_PACKAGE_NAME), eq(TEST_UID), eq(mNotificationChannel));
Geoffrey Pitsch5278d3d2017-03-29 13:39:10 -0400853 }
854
855 @Test
856 public void testCloseControlsUpdatesWhenCheckSaveListenerUsesCallback() throws Exception {
Julia Reynolds437cdb12018-01-03 12:27:24 -0500857 mNotificationChannel.setImportance(IMPORTANCE_LOW);
Geoffrey Pitsch5278d3d2017-03-29 13:39:10 -0400858 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds437cdb12018-01-03 12:27:24 -0500859 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn,
Eliot Courtney47098cb2017-10-18 17:30:30 +0900860 (Runnable saveImportance, StatusBarNotification sbn) -> {
Julia Reynolds3aedded2017-03-31 14:42:09 -0400861 saveImportance.run();
Julia Reynolds35765d82018-08-17 11:39:19 -0400862 }, null, null, true, false);
Geoffrey Pitsch5278d3d2017-03-29 13:39:10 -0400863
Julia Reynolds437cdb12018-01-03 12:27:24 -0500864 mNotificationInfo.findViewById(R.id.block).performClick();
Gus Prevas533836a2018-09-24 17:15:32 -0400865 mTestableLooper.processAllMessages();
866 verify(mMockINotificationManager, never()).updateNotificationChannelForPackage(
867 eq(TEST_PACKAGE_NAME), eq(TEST_UID), eq(mNotificationChannel));
868
Julia Reynolds437cdb12018-01-03 12:27:24 -0500869 waitForUndoButton();
Mady Mellorc2dbe492017-03-30 13:22:03 -0700870 mNotificationInfo.handleCloseControls(true, false);
Rohan Shahca0447e2018-03-30 15:18:27 -0700871
872 mTestableLooper.processAllMessages();
Geoffrey Pitsch5278d3d2017-03-29 13:39:10 -0400873 verify(mMockINotificationManager, times(1)).updateNotificationChannelForPackage(
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400874 eq(TEST_PACKAGE_NAME), eq(TEST_UID), eq(mNotificationChannel));
Geoffrey Pitsch5278d3d2017-03-29 13:39:10 -0400875 }
Julia Reynolds3aedded2017-03-31 14:42:09 -0400876
877 @Test
878 public void testDisplaySettingsLink() throws Exception {
879 final CountDownLatch latch = new CountDownLatch(1);
880 final String settingsText = "work chats";
881 final ResolveInfo ri = new ResolveInfo();
882 ri.activityInfo = new ActivityInfo();
883 ri.activityInfo.packageName = TEST_PACKAGE_NAME;
884 ri.activityInfo.name = "something";
885 List<ResolveInfo> ris = new ArrayList<>();
886 ris.add(ri);
887 when(mMockPackageManager.queryIntentActivities(any(), anyInt())).thenReturn(ris);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500888 mNotificationChannel.setImportance(IMPORTANCE_LOW);
Julia Reynolds3aedded2017-03-31 14:42:09 -0400889 Notification n = new Notification.Builder(mContext, mNotificationChannel.getId())
890 .setSettingsText(settingsText).build();
891 StatusBarNotification sbn = new StatusBarNotification(TEST_PACKAGE_NAME, TEST_PACKAGE_NAME,
892 0, null, 0, 0, n, UserHandle.CURRENT, null, 0);
893
894 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds437cdb12018-01-03 12:27:24 -0500895 TEST_PACKAGE_NAME, mNotificationChannel, 1, sbn, null, null,
Julia Reynolds3aedded2017-03-31 14:42:09 -0400896 (View v, Intent intent) -> {
897 latch.countDown();
Julia Reynolds35765d82018-08-17 11:39:19 -0400898 }, true, false);
Julia Reynolds3aedded2017-03-31 14:42:09 -0400899 final TextView settingsLink = mNotificationInfo.findViewById(R.id.app_settings);
900 assertEquals(View.VISIBLE, settingsLink.getVisibility());
Julia Reynolds3aedded2017-03-31 14:42:09 -0400901 settingsLink.performClick();
902 assertEquals(0, latch.getCount());
903 }
904
905 @Test
906 public void testDisplaySettingsLink_multipleChannels() throws Exception {
907 final CountDownLatch latch = new CountDownLatch(1);
908 final String settingsText = "work chats";
909 final ResolveInfo ri = new ResolveInfo();
910 ri.activityInfo = new ActivityInfo();
911 ri.activityInfo.packageName = TEST_PACKAGE_NAME;
912 ri.activityInfo.name = "something";
913 List<ResolveInfo> ris = new ArrayList<>();
914 ris.add(ri);
915 when(mMockPackageManager.queryIntentActivities(any(), anyInt())).thenReturn(ris);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500916 mNotificationChannel.setImportance(IMPORTANCE_LOW);
Julia Reynolds3aedded2017-03-31 14:42:09 -0400917 Notification n = new Notification.Builder(mContext, mNotificationChannel.getId())
918 .setSettingsText(settingsText).build();
919 StatusBarNotification sbn = new StatusBarNotification(TEST_PACKAGE_NAME, TEST_PACKAGE_NAME,
920 0, null, 0, 0, n, UserHandle.CURRENT, null, 0);
921
922 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Rohan Shah63411fc2018-03-28 19:05:52 -0700923 TEST_PACKAGE_NAME, mNotificationChannel, MULTIPLE_CHANNEL_COUNT, sbn, null, null,
Julia Reynolds437cdb12018-01-03 12:27:24 -0500924 (View v, Intent intent) -> {
Julia Reynolds3aedded2017-03-31 14:42:09 -0400925 latch.countDown();
Julia Reynolds35765d82018-08-17 11:39:19 -0400926 }, true, false);
Julia Reynolds3aedded2017-03-31 14:42:09 -0400927 final TextView settingsLink = mNotificationInfo.findViewById(R.id.app_settings);
928 assertEquals(View.VISIBLE, settingsLink.getVisibility());
929 settingsLink.performClick();
930 assertEquals(0, latch.getCount());
931 }
932
933 @Test
934 public void testNoSettingsLink_noHandlingActivity() throws Exception {
935 final String settingsText = "work chats";
936 when(mMockPackageManager.queryIntentActivities(any(), anyInt())).thenReturn(null);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500937 mNotificationChannel.setImportance(IMPORTANCE_LOW);
Julia Reynolds3aedded2017-03-31 14:42:09 -0400938 Notification n = new Notification.Builder(mContext, mNotificationChannel.getId())
939 .setSettingsText(settingsText).build();
940 StatusBarNotification sbn = new StatusBarNotification(TEST_PACKAGE_NAME, TEST_PACKAGE_NAME,
941 0, null, 0, 0, n, UserHandle.CURRENT, null, 0);
942
943 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Rohan Shah63411fc2018-03-28 19:05:52 -0700944 TEST_PACKAGE_NAME, mNotificationChannel, MULTIPLE_CHANNEL_COUNT, sbn, null, null,
Julia Reynolds35765d82018-08-17 11:39:19 -0400945 null, true, false);
Julia Reynolds3aedded2017-03-31 14:42:09 -0400946 final TextView settingsLink = mNotificationInfo.findViewById(R.id.app_settings);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500947 assertEquals(GONE, settingsLink.getVisibility());
Julia Reynolds3aedded2017-03-31 14:42:09 -0400948 }
949
950 @Test
951 public void testNoSettingsLink_noLinkText() throws Exception {
952 final ResolveInfo ri = new ResolveInfo();
953 ri.activityInfo = new ActivityInfo();
954 ri.activityInfo.packageName = TEST_PACKAGE_NAME;
955 ri.activityInfo.name = "something";
956 List<ResolveInfo> ris = new ArrayList<>();
957 ris.add(ri);
958 when(mMockPackageManager.queryIntentActivities(any(), anyInt())).thenReturn(ris);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500959 mNotificationChannel.setImportance(IMPORTANCE_LOW);
Julia Reynolds3aedded2017-03-31 14:42:09 -0400960 Notification n = new Notification.Builder(mContext, mNotificationChannel.getId()).build();
961 StatusBarNotification sbn = new StatusBarNotification(TEST_PACKAGE_NAME, TEST_PACKAGE_NAME,
962 0, null, 0, 0, n, UserHandle.CURRENT, null, 0);
963
964 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds35765d82018-08-17 11:39:19 -0400965 TEST_PACKAGE_NAME, mNotificationChannel, 1, sbn, null, null, null, true, false);
Julia Reynolds3aedded2017-03-31 14:42:09 -0400966 final TextView settingsLink = mNotificationInfo.findViewById(R.id.app_settings);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500967 assertEquals(GONE, settingsLink.getVisibility());
Julia Reynolds3aedded2017-03-31 14:42:09 -0400968 }
969
Rohan Shah7c6b37a2018-03-30 21:07:45 +0000970 @Test
971 public void testBindHeader_noSettingsLinkWhenIsForBlockingHelper() throws Exception {
972 final String settingsText = "work chats";
973 final ResolveInfo ri = new ResolveInfo();
974 ri.activityInfo = new ActivityInfo();
975 ri.activityInfo.packageName = TEST_PACKAGE_NAME;
976 ri.activityInfo.name = "something";
977 List<ResolveInfo> ris = new ArrayList<>();
978 ris.add(ri);
979 when(mMockPackageManager.queryIntentActivities(any(), anyInt())).thenReturn(ris);
980 mNotificationChannel.setImportance(IMPORTANCE_LOW);
981 Notification n = new Notification.Builder(mContext, mNotificationChannel.getId())
982 .setSettingsText(settingsText).build();
983 StatusBarNotification sbn = new StatusBarNotification(TEST_PACKAGE_NAME, TEST_PACKAGE_NAME,
984 0, null, 0, 0, n, UserHandle.CURRENT, null, 0);
985
986 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
987 TEST_PACKAGE_NAME, mNotificationChannel, 1, sbn, null, null, null, false, true,
Julia Reynolds35765d82018-08-17 11:39:19 -0400988 true, true);
Rohan Shah7c6b37a2018-03-30 21:07:45 +0000989 final TextView settingsLink = mNotificationInfo.findViewById(R.id.app_settings);
990 assertEquals(GONE, settingsLink.getVisibility());
991 }
992
Geoffrey Pitsch8c8dbde2017-04-20 11:44:33 -0400993
994 @Test
995 public void testWillBeRemovedReturnsFalseBeforeBind() throws Exception {
996 assertFalse(mNotificationInfo.willBeRemoved());
997 }
Julia Reynoldse0341482018-03-08 14:42:50 -0500998
999 @Test
1000 public void testUndoText_min() throws Exception {
1001 mSbn.getNotification().flags = Notification.FLAG_FOREGROUND_SERVICE;
1002 mNotificationChannel.setImportance(IMPORTANCE_LOW);
1003 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds35765d82018-08-17 11:39:19 -04001004 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true, true);
Julia Reynoldse0341482018-03-08 14:42:50 -05001005
1006 mNotificationInfo.findViewById(R.id.minimize).performClick();
1007 waitForUndoButton();
1008 TextView confirmationText = mNotificationInfo.findViewById(R.id.confirmation_text);
1009 assertTrue(confirmationText.getText().toString().contains("minimized"));
1010 }
1011
1012 @Test
1013 public void testUndoText_block() throws Exception {
1014 mNotificationChannel.setImportance(IMPORTANCE_LOW);
1015 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds35765d82018-08-17 11:39:19 -04001016 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true, true);
Julia Reynoldse0341482018-03-08 14:42:50 -05001017
1018 mNotificationInfo.findViewById(R.id.block).performClick();
1019 waitForUndoButton();
1020 TextView confirmationText = mNotificationInfo.findViewById(R.id.confirmation_text);
1021 assertTrue(confirmationText.getText().toString().contains("won't see"));
1022 }
1023
1024 @Test
1025 public void testNoHeaderOnConfirmation() throws Exception {
1026 mNotificationChannel.setImportance(IMPORTANCE_LOW);
1027 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds35765d82018-08-17 11:39:19 -04001028 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true, true);
Julia Reynoldse0341482018-03-08 14:42:50 -05001029
1030 mNotificationInfo.findViewById(R.id.block).performClick();
1031 waitForUndoButton();
1032 assertEquals(GONE, mNotificationInfo.findViewById(R.id.header).getVisibility());
1033 }
1034
1035 @Test
1036 public void testHeaderOnUndo() throws Exception {
1037 mNotificationChannel.setImportance(IMPORTANCE_LOW);
1038 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds35765d82018-08-17 11:39:19 -04001039 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true, true);
Julia Reynoldse0341482018-03-08 14:42:50 -05001040
1041 mNotificationInfo.findViewById(R.id.block).performClick();
1042 waitForUndoButton();
1043 mNotificationInfo.findViewById(R.id.undo).performClick();
1044 waitForStopButton();
1045 assertEquals(VISIBLE, mNotificationInfo.findViewById(R.id.header).getVisibility());
1046 }
Geoffrey Pitschdf44b602017-02-03 13:31:50 -05001047}