blob: 37441963e32dbac3a4f74b7c0d440cd96605b0c7 [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;
Gus Prevas9abc5062018-10-31 16:11:04 -040020import static android.app.NotificationManager.IMPORTANCE_DEFAULT;
21import static android.app.NotificationManager.IMPORTANCE_HIGH;
Julia Reynolds437cdb12018-01-03 12:27:24 -050022import static android.app.NotificationManager.IMPORTANCE_LOW;
Julia Reynoldse0341482018-03-08 14:42:50 -050023import static android.app.NotificationManager.IMPORTANCE_MIN;
24import static android.app.NotificationManager.IMPORTANCE_NONE;
Julia Reynoldsc65656a2018-02-12 09:55:14 -050025import static android.app.NotificationManager.IMPORTANCE_UNSPECIFIED;
Julia Reynoldsf7321592017-05-24 16:09:19 -040026import static android.print.PrintManager.PRINT_SPOOLER_PACKAGE_NAME;
Gus Prevas9abc5062018-10-31 16:11:04 -040027import static android.provider.Settings.Secure.NOTIFICATION_NEW_INTERRUPTION_MODEL;
Julia Reynolds437cdb12018-01-03 12:27:24 -050028import static android.view.View.GONE;
29import static android.view.View.VISIBLE;
Julia Reynoldsf7321592017-05-24 16:09:19 -040030
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050031import static junit.framework.Assert.assertEquals;
32import static junit.framework.Assert.assertFalse;
33import static junit.framework.Assert.assertTrue;
Julia Reynolds5a311932017-03-01 16:33:44 -050034
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050035import static org.mockito.Mockito.any;
36import static org.mockito.Mockito.anyBoolean;
37import static org.mockito.Mockito.anyInt;
38import static org.mockito.Mockito.anyString;
Rohan Shahda5dcdd2018-04-27 17:21:50 -070039import static org.mockito.Mockito.doCallRealMethod;
40import static org.mockito.Mockito.doNothing;
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050041import static org.mockito.Mockito.eq;
42import static org.mockito.Mockito.mock;
43import static org.mockito.Mockito.never;
Rohan Shahda5dcdd2018-04-27 17:21:50 -070044import static org.mockito.Mockito.spy;
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050045import static org.mockito.Mockito.times;
46import static org.mockito.Mockito.verify;
47import static org.mockito.Mockito.when;
48
49import android.app.INotificationManager;
Julia Reynolds3aedded2017-03-31 14:42:09 -040050import android.app.Notification;
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050051import android.app.NotificationChannel;
52import android.app.NotificationChannelGroup;
Julia Reynolds3aedded2017-03-31 14:42:09 -040053import android.content.Intent;
54import android.content.pm.ActivityInfo;
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050055import android.content.pm.ApplicationInfo;
56import android.content.pm.PackageInfo;
57import android.content.pm.PackageManager;
Julia Reynolds3aedded2017-03-31 14:42:09 -040058import android.content.pm.ResolveInfo;
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050059import android.graphics.drawable.Drawable;
Rohan Shahda5dcdd2018-04-27 17:21:50 -070060import android.os.IBinder;
Julia Reynolds3aedded2017-03-31 14:42:09 -040061import android.os.UserHandle;
Gus Prevas9abc5062018-10-31 16:11:04 -040062import android.provider.Settings;
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050063import android.service.notification.StatusBarNotification;
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050064import android.test.suitebuilder.annotation.SmallTest;
Jason Monk340b0e52017-03-08 14:57:56 -050065import android.testing.AndroidTestingRunner;
Julia Reynolds437cdb12018-01-03 12:27:24 -050066import android.testing.PollingCheck;
Rohan Shahca0447e2018-03-30 15:18:27 -070067import android.testing.TestableLooper;
Jason Monk340b0e52017-03-08 14:57:56 -050068import android.testing.UiThreadTest;
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050069import android.view.LayoutInflater;
70import android.view.View;
71import android.widget.ImageView;
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050072import android.widget.TextView;
Jason Monk340b0e52017-03-08 14:57:56 -050073
Rohan Shahda5dcdd2018-04-27 17:21:50 -070074import com.android.internal.logging.MetricsLogger;
Rohan Shahca0447e2018-03-30 15:18:27 -070075import com.android.systemui.Dependency;
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050076import com.android.systemui.R;
77import com.android.systemui.SysuiTestCase;
Jason Monkba364322017-03-06 11:19:20 -050078
Gus Prevas9abc5062018-10-31 16:11:04 -040079import org.junit.After;
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050080import org.junit.Before;
Rohan Shah524cf7b2018-03-15 14:40:02 -070081import org.junit.Rule;
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050082import org.junit.Test;
Jason Monk340b0e52017-03-08 14:57:56 -050083import org.junit.runner.RunWith;
Julia Reynolds8ceb5792017-04-11 11:32:44 -040084import org.mockito.ArgumentCaptor;
Rohan Shah524cf7b2018-03-15 14:40:02 -070085import org.mockito.Mock;
86import org.mockito.junit.MockitoJUnit;
87import org.mockito.junit.MockitoRule;
Julia Reynolds3aedded2017-03-31 14:42:09 -040088
89import java.util.ArrayList;
Geoffrey Pitschd0856f02017-02-16 10:51:18 -050090import java.util.List;
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050091import java.util.concurrent.CountDownLatch;
92
93@SmallTest
Jason Monk340b0e52017-03-08 14:57:56 -050094@RunWith(AndroidTestingRunner.class)
Rohan Shahca0447e2018-03-30 15:18:27 -070095@TestableLooper.RunWithLooper
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050096public class NotificationInfoTest extends SysuiTestCase {
97 private static final String TEST_PACKAGE_NAME = "test_package";
Julia Reynoldsf7321592017-05-24 16:09:19 -040098 private static final String TEST_SYSTEM_PACKAGE_NAME = PRINT_SPOOLER_PACKAGE_NAME;
Geoffrey Pitschd034d292017-05-12 11:59:20 -040099 private static final int TEST_UID = 1;
Rohan Shah63411fc2018-03-28 19:05:52 -0700100 private static final int MULTIPLE_CHANNEL_COUNT = 2;
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500101 private static final String TEST_CHANNEL = "test_channel";
102 private static final String TEST_CHANNEL_NAME = "TEST CHANNEL NAME";
103
Rohan Shahca0447e2018-03-30 15:18:27 -0700104 private TestableLooper mTestableLooper;
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500105 private NotificationInfo mNotificationInfo;
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500106 private NotificationChannel mNotificationChannel;
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500107 private NotificationChannel mDefaultNotificationChannel;
Julia Reynolds3aedded2017-03-31 14:42:09 -0400108 private StatusBarNotification mSbn;
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500109
Rohan Shah524cf7b2018-03-15 14:40:02 -0700110 @Rule public MockitoRule mockito = MockitoJUnit.rule();
Rohan Shahda5dcdd2018-04-27 17:21:50 -0700111 @Mock private MetricsLogger mMetricsLogger;
Rohan Shah524cf7b2018-03-15 14:40:02 -0700112 @Mock private INotificationManager mMockINotificationManager;
113 @Mock private PackageManager mMockPackageManager;
114 @Mock private NotificationBlockingHelperManager mBlockingHelperManager;
115
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500116 @Before
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500117 public void setUp() throws Exception {
Rohan Shah524cf7b2018-03-15 14:40:02 -0700118 mDependency.injectTestDependency(
119 NotificationBlockingHelperManager.class,
120 mBlockingHelperManager);
Rohan Shahca0447e2018-03-30 15:18:27 -0700121 mTestableLooper = TestableLooper.get(this);
122 mDependency.injectTestDependency(Dependency.BG_LOOPER, mTestableLooper.getLooper());
Rohan Shahda5dcdd2018-04-27 17:21:50 -0700123 mDependency.injectTestDependency(MetricsLogger.class, mMetricsLogger);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500124 // Inflate the layout
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500125 final LayoutInflater layoutInflater = LayoutInflater.from(mContext);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500126 mNotificationInfo = (NotificationInfo) layoutInflater.inflate(R.layout.notification_info,
127 null);
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500128 mNotificationInfo.setGutsParent(mock(NotificationGuts.class));
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500129
130 // PackageManager must return a packageInfo and applicationInfo.
131 final PackageInfo packageInfo = new PackageInfo();
132 packageInfo.packageName = TEST_PACKAGE_NAME;
Julia Reynoldsf7321592017-05-24 16:09:19 -0400133 when(mMockPackageManager.getPackageInfo(eq(TEST_PACKAGE_NAME), anyInt()))
134 .thenReturn(packageInfo);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500135 final ApplicationInfo applicationInfo = new ApplicationInfo();
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400136 applicationInfo.uid = TEST_UID; // non-zero
Daniel Sandlerd0a52b62018-10-31 20:13:22 +0000137 when(mMockPackageManager.getApplicationInfo(anyString(), anyInt())).thenReturn(
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500138 applicationInfo);
Julia Reynoldsf7321592017-05-24 16:09:19 -0400139 final PackageInfo systemPackageInfo = new PackageInfo();
140 systemPackageInfo.packageName = TEST_SYSTEM_PACKAGE_NAME;
141 when(mMockPackageManager.getPackageInfo(eq(TEST_SYSTEM_PACKAGE_NAME), anyInt()))
142 .thenReturn(systemPackageInfo);
143 when(mMockPackageManager.getPackageInfo(eq("android"), anyInt()))
144 .thenReturn(packageInfo);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500145
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500146 // Package has one channel by default.
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500147 when(mMockINotificationManager.getNumNotificationChannelsForPackage(
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400148 eq(TEST_PACKAGE_NAME), eq(TEST_UID), anyBoolean())).thenReturn(1);
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500149
150 // Some test channels.
151 mNotificationChannel = new NotificationChannel(
Julia Reynolds437cdb12018-01-03 12:27:24 -0500152 TEST_CHANNEL, TEST_CHANNEL_NAME, IMPORTANCE_LOW);
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500153 mDefaultNotificationChannel = new NotificationChannel(
154 NotificationChannel.DEFAULT_CHANNEL_ID, TEST_CHANNEL_NAME,
Julia Reynolds437cdb12018-01-03 12:27:24 -0500155 IMPORTANCE_LOW);
Julia Reynolds33bef2c2017-09-05 11:07:18 -0400156 mSbn = new StatusBarNotification(TEST_PACKAGE_NAME, TEST_PACKAGE_NAME, 0, null, TEST_UID, 0,
Julia Reynolds3aedded2017-03-31 14:42:09 -0400157 new Notification(), UserHandle.CURRENT, null, 0);
Gus Prevas9abc5062018-10-31 16:11:04 -0400158
159 Settings.Secure.putInt(mContext.getContentResolver(),
160 NOTIFICATION_NEW_INTERRUPTION_MODEL, 1);
161 }
162
163 @After
164 public void tearDown() {
165 Settings.Secure.putInt(mContext.getContentResolver(),
166 NOTIFICATION_NEW_INTERRUPTION_MODEL, 0);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500167 }
168
Julia Reynolds437cdb12018-01-03 12:27:24 -0500169 // TODO: if tests are taking too long replace this with something that makes the animation
170 // finish instantly.
171 private void waitForUndoButton() {
172 PollingCheck.waitFor(1000,
173 () -> VISIBLE == mNotificationInfo.findViewById(R.id.confirmation).getVisibility());
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500174 }
Gus Prevas533836a2018-09-24 17:15:32 -0400175 private void ensureNoUndoButton() {
176 PollingCheck.waitFor(1000,
177 () -> GONE == mNotificationInfo.findViewById(R.id.confirmation).getVisibility()
178 && !mNotificationInfo.isAnimating());
179 }
Julia Reynolds437cdb12018-01-03 12:27:24 -0500180 private void waitForStopButton() {
181 PollingCheck.waitFor(1000,
182 () -> VISIBLE == mNotificationInfo.findViewById(R.id.prompt).getVisibility());
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500183 }
184
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500185 @Test
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500186 public void testBindNotification_SetsTextApplicationName() throws Exception {
187 when(mMockPackageManager.getApplicationLabel(any())).thenReturn("App Name");
188 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Gus Prevas9abc5062018-10-31 16:11:04 -0400189 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true, false,
190 false, IMPORTANCE_DEFAULT);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500191 final TextView textView = mNotificationInfo.findViewById(R.id.pkgname);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500192 assertTrue(textView.getText().toString().contains("App Name"));
Julia Reynoldse0341482018-03-08 14:42:50 -0500193 assertEquals(VISIBLE, mNotificationInfo.findViewById(R.id.header).getVisibility());
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500194 }
195
196 @Test
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500197 public void testBindNotification_SetsPackageIcon() throws Exception {
198 final Drawable iconDrawable = mock(Drawable.class);
199 when(mMockPackageManager.getApplicationIcon(any(ApplicationInfo.class)))
200 .thenReturn(iconDrawable);
201 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Gus Prevas9abc5062018-10-31 16:11:04 -0400202 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true, false,
203 false, IMPORTANCE_DEFAULT);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500204 final ImageView iconView = mNotificationInfo.findViewById(R.id.pkgicon);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500205 assertEquals(iconDrawable, iconView.getDrawable());
206 }
207
208 @Test
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500209 public void testBindNotification_GroupNameHiddenIfNoGroup() throws Exception {
210 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Gus Prevas9abc5062018-10-31 16:11:04 -0400211 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true, false,
212 false, IMPORTANCE_DEFAULT);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500213 final TextView groupNameView = mNotificationInfo.findViewById(R.id.group_name);
214 assertEquals(GONE, groupNameView.getVisibility());
215 final TextView groupDividerView = mNotificationInfo.findViewById(R.id.pkg_group_divider);
216 assertEquals(GONE, groupDividerView.getVisibility());
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500217 }
218
219 @Test
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500220 public void testBindNotification_SetsGroupNameIfNonNull() throws Exception {
221 mNotificationChannel.setGroup("test_group_id");
222 final NotificationChannelGroup notificationChannelGroup =
223 new NotificationChannelGroup("test_group_id", "Test Group Name");
224 when(mMockINotificationManager.getNotificationChannelGroupForPackage(
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400225 eq("test_group_id"), eq(TEST_PACKAGE_NAME), eq(TEST_UID)))
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500226 .thenReturn(notificationChannelGroup);
227 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Gus Prevas9abc5062018-10-31 16:11:04 -0400228 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true, false,
229 false, IMPORTANCE_DEFAULT);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500230 final TextView groupNameView = mNotificationInfo.findViewById(R.id.group_name);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500231 assertEquals(View.VISIBLE, groupNameView.getVisibility());
232 assertEquals("Test Group Name", groupNameView.getText());
Julia Reynolds437cdb12018-01-03 12:27:24 -0500233 final TextView groupDividerView = mNotificationInfo.findViewById(R.id.pkg_group_divider);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500234 assertEquals(View.VISIBLE, groupDividerView.getVisibility());
235 }
236
237 @Test
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500238 public void testBindNotification_SetsTextChannelName() throws Exception {
239 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Gus Prevas9abc5062018-10-31 16:11:04 -0400240 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true, false,
241 false, IMPORTANCE_DEFAULT);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500242 final TextView textView = mNotificationInfo.findViewById(R.id.channel_name);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500243 assertEquals(TEST_CHANNEL_NAME, textView.getText());
244 }
245
246 @Test
Geoffrey Pitsch1fbf66e2017-05-09 11:32:27 -0400247 public void testBindNotification_DefaultChannelDoesNotUseChannelName() throws Exception {
248 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds35765d82018-08-17 11:39:19 -0400249 TEST_PACKAGE_NAME, mDefaultNotificationChannel, 1, mSbn, null, null, null, true,
Gus Prevas9abc5062018-10-31 16:11:04 -0400250 false, false, IMPORTANCE_DEFAULT);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500251 final TextView textView = mNotificationInfo.findViewById(R.id.channel_name);
252 assertEquals(GONE, textView.getVisibility());
Geoffrey Pitsch1fbf66e2017-05-09 11:32:27 -0400253 }
254
255 @Test
Rohan Shahdbd64e72018-03-28 14:46:50 -0700256 public void testBindNotification_DefaultChannelUsesChannelNameIfMoreChannelsExist()
257 throws Exception {
258 // Package has one channel by default.
259 when(mMockINotificationManager.getNumNotificationChannelsForPackage(
260 eq(TEST_PACKAGE_NAME), eq(TEST_UID), anyBoolean())).thenReturn(10);
261 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds35765d82018-08-17 11:39:19 -0400262 TEST_PACKAGE_NAME, mDefaultNotificationChannel, 1, mSbn, null, null, null, true,
Gus Prevas9abc5062018-10-31 16:11:04 -0400263 false, false, IMPORTANCE_DEFAULT);
Rohan Shahdbd64e72018-03-28 14:46:50 -0700264 final TextView textView = mNotificationInfo.findViewById(R.id.channel_name);
265 assertEquals(VISIBLE, textView.getVisibility());
266 }
267
268 @Test
Julia Reynolds437cdb12018-01-03 12:27:24 -0500269 public void testBindNotification_UnblockablePackageUsesChannelName() throws Exception {
Geoffrey Pitschb43486a2017-06-01 13:45:59 -0400270 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Gus Prevas9abc5062018-10-31 16:11:04 -0400271 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true, true,
272 false, IMPORTANCE_DEFAULT);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500273 final TextView textView = mNotificationInfo.findViewById(R.id.channel_name);
274 assertEquals(VISIBLE, textView.getVisibility());
Geoffrey Pitsch1fbf66e2017-05-09 11:32:27 -0400275 }
276
277 @Test
Julia Reynoldse0341482018-03-08 14:42:50 -0500278 public void testBindNotification_BlockButton() throws Exception {
279 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Gus Prevas9abc5062018-10-31 16:11:04 -0400280 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true, false,
281 false, IMPORTANCE_DEFAULT);
Julia Reynoldse0341482018-03-08 14:42:50 -0500282 final View block = mNotificationInfo.findViewById(R.id.block);
Gus Prevas9abc5062018-10-31 16:11:04 -0400283 final View toggleSilent = mNotificationInfo.findViewById(R.id.toggle_silent);
Julia Reynoldse0341482018-03-08 14:42:50 -0500284 final View minimize = mNotificationInfo.findViewById(R.id.minimize);
285 assertEquals(VISIBLE, block.getVisibility());
Gus Prevas9abc5062018-10-31 16:11:04 -0400286 assertEquals(GONE, toggleSilent.getVisibility());
Julia Reynoldse0341482018-03-08 14:42:50 -0500287 assertEquals(GONE, minimize.getVisibility());
288 }
289
290 @Test
Gus Prevas9abc5062018-10-31 16:11:04 -0400291 public void testBindNotification_SilenceButton() throws Exception {
292 mNotificationChannel.setImportance(IMPORTANCE_DEFAULT);
293 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
294 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true, false,
295 true, IMPORTANCE_DEFAULT);
296 final TextView toggleSilent = mNotificationInfo.findViewById(R.id.toggle_silent);
297 assertEquals(VISIBLE, toggleSilent.getVisibility());
298 assertEquals(
299 mContext.getString(R.string.inline_silent_button_silent), toggleSilent.getText());
300 }
301
302 @Test
303 public void testBindNotification_UnSilenceButton() throws Exception {
304 mNotificationChannel.setImportance(IMPORTANCE_LOW);
305 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
306 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true, false,
307 true, IMPORTANCE_LOW);
308 final TextView toggleSilent = mNotificationInfo.findViewById(R.id.toggle_silent);
309 assertEquals(VISIBLE, toggleSilent.getVisibility());
310 assertEquals(
311 mContext.getString(R.string.inline_silent_button_alert), toggleSilent.getText());
312 }
313
314 @Test
315 public void testBindNotification_SilenceButton_ChannelImportanceUnspecified() throws Exception {
316 mNotificationChannel.setImportance(IMPORTANCE_UNSPECIFIED);
317 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
318 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true, false,
319 true, IMPORTANCE_DEFAULT);
320 final TextView toggleSilent = mNotificationInfo.findViewById(R.id.toggle_silent);
321 assertEquals(VISIBLE, toggleSilent.getVisibility());
322 assertEquals(
323 mContext.getString(R.string.inline_silent_button_silent), toggleSilent.getText());
324 }
325
326 @Test
327 public void testBindNotification_UnSilenceButton_ChannelImportanceUnspecified()
328 throws Exception {
329 mNotificationChannel.setImportance(IMPORTANCE_UNSPECIFIED);
330 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
331 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true, false,
332 true, IMPORTANCE_LOW);
333 final TextView toggleSilent = mNotificationInfo.findViewById(R.id.toggle_silent);
334 assertEquals(VISIBLE, toggleSilent.getVisibility());
335 assertEquals(
336 mContext.getString(R.string.inline_silent_button_alert), toggleSilent.getText());
337 }
338
339 @Test
Julia Reynoldse0341482018-03-08 14:42:50 -0500340 public void testBindNotification_MinButton() throws Exception {
341 mSbn.getNotification().flags = Notification.FLAG_FOREGROUND_SERVICE;
342 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Gus Prevas9abc5062018-10-31 16:11:04 -0400343 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true, false,
344 false, IMPORTANCE_DEFAULT);
Julia Reynoldse0341482018-03-08 14:42:50 -0500345 final View block = mNotificationInfo.findViewById(R.id.block);
346 final View minimize = mNotificationInfo.findViewById(R.id.minimize);
347 assertEquals(GONE, block.getVisibility());
348 assertEquals(VISIBLE, minimize.getVisibility());
349 }
350
351 @Test
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500352 public void testBindNotification_SetsOnClickListenerForSettings() throws Exception {
353 final CountDownLatch latch = new CountDownLatch(1);
354 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds437cdb12018-01-03 12:27:24 -0500355 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null,
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500356 (View v, NotificationChannel c, int appUid) -> {
Julia Reynolds3aedded2017-03-31 14:42:09 -0400357 assertEquals(mNotificationChannel, c);
358 latch.countDown();
Gus Prevas9abc5062018-10-31 16:11:04 -0400359 }, null, true, false, false, IMPORTANCE_DEFAULT);
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500360
Julia Reynolds437cdb12018-01-03 12:27:24 -0500361 final View settingsButton = mNotificationInfo.findViewById(R.id.info);
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500362 settingsButton.performClick();
363 // Verify that listener was triggered.
364 assertEquals(0, latch.getCount());
365 }
366
367 @Test
Geoffrey Pitsch029a3fa2017-03-30 13:01:57 -0400368 public void testBindNotification_SettingsButtonInvisibleWhenNoClickListener() throws Exception {
369 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Gus Prevas9abc5062018-10-31 16:11:04 -0400370 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true, false,
371 false, IMPORTANCE_DEFAULT);
Julia Reynolds35765d82018-08-17 11:39:19 -0400372 final View settingsButton = mNotificationInfo.findViewById(R.id.info);
373 assertTrue(settingsButton.getVisibility() != View.VISIBLE);
374 }
375
376 @Test
377 public void testBindNotification_SettingsButtonInvisibleWhenDeviceUnprovisioned()
378 throws Exception {
379 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
380 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null,
381 (View v, NotificationChannel c, int appUid) -> {
382 assertEquals(mNotificationChannel, c);
Gus Prevas9abc5062018-10-31 16:11:04 -0400383 }, null, false, false, false, IMPORTANCE_DEFAULT);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500384 final View settingsButton = mNotificationInfo.findViewById(R.id.info);
Geoffrey Pitsch029a3fa2017-03-30 13:01:57 -0400385 assertTrue(settingsButton.getVisibility() != View.VISIBLE);
386 }
387
388 @Test
Julia Reynolds437cdb12018-01-03 12:27:24 -0500389 public void testBindNotification_SettingsButtonReappearsAfterSecondBind() throws Exception {
Geoffrey Pitsch029a3fa2017-03-30 13:01:57 -0400390 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Gus Prevas9abc5062018-10-31 16:11:04 -0400391 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true, false,
392 false, IMPORTANCE_DEFAULT);
Geoffrey Pitsch029a3fa2017-03-30 13:01:57 -0400393 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds437cdb12018-01-03 12:27:24 -0500394 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null,
395 (View v, NotificationChannel c, int appUid) -> {
Gus Prevas9abc5062018-10-31 16:11:04 -0400396 }, null, true, false, false, IMPORTANCE_DEFAULT);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500397 final View settingsButton = mNotificationInfo.findViewById(R.id.info);
Geoffrey Pitsch029a3fa2017-03-30 13:01:57 -0400398 assertEquals(View.VISIBLE, settingsButton.getVisibility());
399 }
400
401 @Test
Rohan Shahda5dcdd2018-04-27 17:21:50 -0700402 public void testLogBlockingHelperCounter_doesntLogForNormalGutsView() throws Exception {
403 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Gus Prevas9abc5062018-10-31 16:11:04 -0400404 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true, false,
405 false, IMPORTANCE_DEFAULT);
Rohan Shahda5dcdd2018-04-27 17:21:50 -0700406 mNotificationInfo.logBlockingHelperCounter("HowCanNotifsBeRealIfAppsArent");
407 verify(mMetricsLogger, times(0)).count(anyString(), anyInt());
408 }
409
410 @Test
411 public void testLogBlockingHelperCounter_logsForBlockingHelper() throws Exception {
Rohan Shahda5dcdd2018-04-27 17:21:50 -0700412 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
413 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, false, true,
Gus Prevas9abc5062018-10-31 16:11:04 -0400414 true, true, false, IMPORTANCE_DEFAULT);
Rohan Shahda5dcdd2018-04-27 17:21:50 -0700415 mNotificationInfo.logBlockingHelperCounter("HowCanNotifsBeRealIfAppsArent");
Rohan Shahc6b0d542018-05-10 13:00:54 -0700416 verify(mMetricsLogger, times(1)).count(anyString(), anyInt());
Rohan Shahda5dcdd2018-04-27 17:21:50 -0700417 }
418
419 @Test
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500420 public void testOnClickListenerPassesNullChannelForBundle() throws Exception {
421 final CountDownLatch latch = new CountDownLatch(1);
422 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Rohan Shah63411fc2018-03-28 19:05:52 -0700423 TEST_PACKAGE_NAME, mNotificationChannel, MULTIPLE_CHANNEL_COUNT, mSbn, null,
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500424 (View v, NotificationChannel c, int appUid) -> {
Julia Reynolds3aedded2017-03-31 14:42:09 -0400425 assertEquals(null, c);
426 latch.countDown();
Gus Prevas9abc5062018-10-31 16:11:04 -0400427 }, null, true, true, false, IMPORTANCE_DEFAULT);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500428
Julia Reynolds437cdb12018-01-03 12:27:24 -0500429 mNotificationInfo.findViewById(R.id.info).performClick();
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500430 // Verify that listener was triggered.
431 assertEquals(0, latch.getCount());
432 }
433
434 @Test
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500435 @UiThreadTest
Julia Reynolds437cdb12018-01-03 12:27:24 -0500436 public void testBindNotification_ChannelNameInvisibleWhenBundleFromDifferentChannels()
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500437 throws Exception {
438 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Rohan Shah63411fc2018-03-28 19:05:52 -0700439 TEST_PACKAGE_NAME, mNotificationChannel, MULTIPLE_CHANNEL_COUNT, mSbn, null, null,
Gus Prevas9abc5062018-10-31 16:11:04 -0400440 null, true, true, false, IMPORTANCE_DEFAULT);
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500441 final TextView channelNameView =
Julia Reynolds437cdb12018-01-03 12:27:24 -0500442 mNotificationInfo.findViewById(R.id.channel_name);
443 assertEquals(GONE, channelNameView.getVisibility());
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500444 }
445
446 @Test
447 @UiThreadTest
Julia Reynolds437cdb12018-01-03 12:27:24 -0500448 public void testStopInvisibleIfBundleFromDifferentChannels() throws Exception {
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500449 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Rohan Shah63411fc2018-03-28 19:05:52 -0700450 TEST_PACKAGE_NAME, mNotificationChannel, MULTIPLE_CHANNEL_COUNT, mSbn, null, null,
Gus Prevas9abc5062018-10-31 16:11:04 -0400451 null, true, true, false, IMPORTANCE_DEFAULT);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500452 final TextView blockView = mNotificationInfo.findViewById(R.id.block);
453 assertEquals(GONE, blockView.getVisibility());
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500454 }
455
456 @Test
Julia Reynolds0ef7d842018-01-24 17:50:31 -0500457 public void testbindNotification_BlockingHelper() throws Exception {
458 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Rohan Shah63411fc2018-03-28 19:05:52 -0700459 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, false, false,
Gus Prevas9abc5062018-10-31 16:11:04 -0400460 true, true, false, IMPORTANCE_DEFAULT);
Julia Reynolds0ef7d842018-01-24 17:50:31 -0500461 final TextView view = mNotificationInfo.findViewById(R.id.block_prompt);
462 assertEquals(View.VISIBLE, view.getVisibility());
463 assertEquals(mContext.getString(R.string.inline_blocking_helper), view.getText());
464 }
465
466 @Test
Geoffrey Pitschb43486a2017-06-01 13:45:59 -0400467 public void testbindNotification_UnblockableTextVisibleWhenAppUnblockable() throws Exception {
468 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Gus Prevas9abc5062018-10-31 16:11:04 -0400469 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true, true,
470 false, IMPORTANCE_DEFAULT);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500471 final TextView view = mNotificationInfo.findViewById(R.id.block_prompt);
472 assertEquals(View.VISIBLE, view.getVisibility());
Geoffrey Pitschb43486a2017-06-01 13:45:59 -0400473 assertEquals(mContext.getString(R.string.notification_unblockable_desc),
Julia Reynolds437cdb12018-01-03 12:27:24 -0500474 view.getText());
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500475 }
476
477 @Test
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500478 public void testBindNotification_DoesNotUpdateNotificationChannel() throws Exception {
479 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Gus Prevas9abc5062018-10-31 16:11:04 -0400480 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true, false,
481 false, IMPORTANCE_DEFAULT);
Rohan Shahca0447e2018-03-30 15:18:27 -0700482 mTestableLooper.processAllMessages();
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500483 verify(mMockINotificationManager, never()).updateNotificationChannelForPackage(
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400484 anyString(), eq(TEST_UID), any());
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500485 }
486
487 @Test
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500488 public void testDoesNotUpdateNotificationChannelAfterImportanceChanged() throws Exception {
Julia Reynolds437cdb12018-01-03 12:27:24 -0500489 mNotificationChannel.setImportance(IMPORTANCE_LOW);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500490 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Gus Prevas9abc5062018-10-31 16:11:04 -0400491 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true, false,
492 false, IMPORTANCE_DEFAULT);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500493
Julia Reynolds437cdb12018-01-03 12:27:24 -0500494 mNotificationInfo.findViewById(R.id.block).performClick();
Rohan Shahca0447e2018-03-30 15:18:27 -0700495 mTestableLooper.processAllMessages();
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500496 verify(mMockINotificationManager, never()).updateNotificationChannelForPackage(
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400497 anyString(), eq(TEST_UID), any());
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500498 }
499
500 @Test
Julia Reynoldse0341482018-03-08 14:42:50 -0500501 public void testDoesNotUpdateNotificationChannelAfterImportanceChangedMin()
502 throws Exception {
503 mNotificationChannel.setImportance(IMPORTANCE_LOW);
504 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Gus Prevas9abc5062018-10-31 16:11:04 -0400505 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true, false,
506 false, IMPORTANCE_DEFAULT);
Julia Reynoldse0341482018-03-08 14:42:50 -0500507
508 mNotificationInfo.findViewById(R.id.minimize).performClick();
Rohan Shahca0447e2018-03-30 15:18:27 -0700509 mTestableLooper.processAllMessages();
Julia Reynoldse0341482018-03-08 14:42:50 -0500510 verify(mMockINotificationManager, never()).updateNotificationChannelForPackage(
511 anyString(), eq(TEST_UID), any());
512 }
513
514 @Test
Gus Prevas9abc5062018-10-31 16:11:04 -0400515 public void testDoesNotUpdateNotificationChannelAfterImportanceChangedSilenced()
516 throws Exception {
517 mNotificationChannel.setImportance(IMPORTANCE_DEFAULT);
518 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
519 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true, false,
520 true, IMPORTANCE_DEFAULT);
521
522 mNotificationInfo.findViewById(R.id.toggle_silent).performClick();
523 mTestableLooper.processAllMessages();
524 verify(mMockINotificationManager, never()).updateNotificationChannelForPackage(
525 anyString(), eq(TEST_UID), any());
526 }
527
528 @Test
529 public void testDoesNotUpdateNotificationChannelAfterImportanceChangedUnSilenced()
530 throws Exception {
531 mNotificationChannel.setImportance(IMPORTANCE_LOW);
532 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
533 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true, false,
534 true, IMPORTANCE_DEFAULT);
535
536 mNotificationInfo.findViewById(R.id.toggle_silent).performClick();
537 mTestableLooper.processAllMessages();
538 verify(mMockINotificationManager, never()).updateNotificationChannelForPackage(
539 anyString(), eq(TEST_UID), any());
540 }
541
542 @Test
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500543 public void testHandleCloseControls_DoesNotUpdateNotificationChannelIfUnchanged()
544 throws Exception {
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500545 int originalImportance = mNotificationChannel.getImportance();
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500546 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Gus Prevas9abc5062018-10-31 16:11:04 -0400547 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true, false,
548 false, IMPORTANCE_DEFAULT);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500549
Mady Mellorc2dbe492017-03-30 13:22:03 -0700550 mNotificationInfo.handleCloseControls(true, false);
Rohan Shahca0447e2018-03-30 15:18:27 -0700551 mTestableLooper.processAllMessages();
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500552 verify(mMockINotificationManager, times(1)).updateNotificationChannelForPackage(
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400553 anyString(), eq(TEST_UID), any());
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500554 assertEquals(originalImportance, mNotificationChannel.getImportance());
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500555 }
556
557 @Test
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500558 public void testHandleCloseControls_DoesNotUpdateNotificationChannelIfUnspecified()
559 throws Exception {
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500560 mNotificationChannel.setImportance(IMPORTANCE_UNSPECIFIED);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500561 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Gus Prevas9abc5062018-10-31 16:11:04 -0400562 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true, false,
563 false, IMPORTANCE_DEFAULT);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500564
Mady Mellorc2dbe492017-03-30 13:22:03 -0700565 mNotificationInfo.handleCloseControls(true, false);
Rohan Shahca0447e2018-03-30 15:18:27 -0700566
567 mTestableLooper.processAllMessages();
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500568 verify(mMockINotificationManager, times(1)).updateNotificationChannelForPackage(
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400569 anyString(), eq(TEST_UID), any());
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500570 assertEquals(IMPORTANCE_UNSPECIFIED, mNotificationChannel.getImportance());
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500571 }
572
573 @Test
Rohan Shah590e1b22018-04-10 23:48:47 -0400574 public void testHandleCloseControls_setsNotificationsDisabledForMultipleChannelNotifications()
575 throws Exception {
576 mNotificationChannel.setImportance(IMPORTANCE_LOW);
577 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
578 TEST_PACKAGE_NAME, mNotificationChannel /* notificationChannel */,
579 10 /* numUniqueChannelsInRow */, mSbn, null /* checkSaveListener */,
580 null /* onSettingsClick */, null /* onAppSettingsClick */ ,
Gus Prevas9abc5062018-10-31 16:11:04 -0400581 true, false /* isNonblockable */, false, /* isNoisy */IMPORTANCE_DEFAULT);
Rohan Shah590e1b22018-04-10 23:48:47 -0400582
583 mNotificationInfo.findViewById(R.id.block).performClick();
584 waitForUndoButton();
585 mNotificationInfo.handleCloseControls(true, false);
586
587 mTestableLooper.processAllMessages();
588 verify(mMockINotificationManager, times(1))
589 .setNotificationsEnabledWithImportanceLockForPackage(
590 anyString(), eq(TEST_UID), eq(false));
591 }
592
593
594 @Test
595 public void testHandleCloseControls_keepsNotificationsEnabledForMultipleChannelNotifications()
596 throws Exception {
597 mNotificationChannel.setImportance(IMPORTANCE_LOW);
598 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
599 TEST_PACKAGE_NAME, mNotificationChannel /* notificationChannel */,
600 10 /* numUniqueChannelsInRow */, mSbn, null /* checkSaveListener */,
601 null /* onSettingsClick */, null /* onAppSettingsClick */ ,
Gus Prevas9abc5062018-10-31 16:11:04 -0400602 true, false /* isNonblockable */, false, /* isNoisy */IMPORTANCE_DEFAULT);
Rohan Shah590e1b22018-04-10 23:48:47 -0400603
604 mNotificationInfo.findViewById(R.id.block).performClick();
605 waitForUndoButton();
606 mNotificationInfo.handleCloseControls(true, false);
607
608 mTestableLooper.processAllMessages();
609 verify(mMockINotificationManager, times(1))
610 .setNotificationsEnabledWithImportanceLockForPackage(
611 anyString(), eq(TEST_UID), eq(false));
612 }
613
614 @Test
615 public void testCloseControls_blockingHelperSavesImportanceForMultipleChannelNotifications()
616 throws Exception {
617 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
618 TEST_PACKAGE_NAME, mNotificationChannel /* notificationChannel */,
619 10 /* numUniqueChannelsInRow */, mSbn, null /* checkSaveListener */,
620 null /* onSettingsClick */, null /* onAppSettingsClick */ ,
Julia Reynolds35765d82018-08-17 11:39:19 -0400621 true /* provisioned */,
Rohan Shah590e1b22018-04-10 23:48:47 -0400622 false /* isNonblockable */, true /* isForBlockingHelper */,
Gus Prevas9abc5062018-10-31 16:11:04 -0400623 true /* isUserSentimentNegative */, false, /* isNoisy */IMPORTANCE_DEFAULT);
Rohan Shah590e1b22018-04-10 23:48:47 -0400624
Rohan Shahda5dcdd2018-04-27 17:21:50 -0700625 NotificationGuts guts = spy(new NotificationGuts(mContext, null));
626 when(guts.getWindowToken()).thenReturn(mock(IBinder.class));
627 doNothing().when(guts).animateClose(anyInt(), anyInt(), anyBoolean());
628 doNothing().when(guts).setExposed(anyBoolean(), anyBoolean());
629 guts.setGutsContent(mNotificationInfo);
630 mNotificationInfo.setGutsParent(guts);
631
Rohan Shah590e1b22018-04-10 23:48:47 -0400632 mNotificationInfo.findViewById(R.id.keep).performClick();
633
634 verify(mBlockingHelperManager).dismissCurrentBlockingHelper();
635 mTestableLooper.processAllMessages();
636 verify(mMockINotificationManager, times(1))
637 .setNotificationsEnabledWithImportanceLockForPackage(
638 anyString(), eq(TEST_UID), eq(true));
639 }
640
Rohan Shahdd588c72018-05-09 20:32:15 -0700641 @Test
642 public void testCloseControls_nonNullCheckSaveListenerDoesntDelayKeepShowing()
643 throws Exception {
644 NotificationInfo.CheckSaveListener listener =
645 mock(NotificationInfo.CheckSaveListener.class);
646 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
647 TEST_PACKAGE_NAME, mNotificationChannel /* notificationChannel */,
648 10 /* numUniqueChannelsInRow */, mSbn, listener /* checkSaveListener */,
Julia Reynolds35765d82018-08-17 11:39:19 -0400649 null /* onSettingsClick */, null /* onAppSettingsClick */ , true /* provisioned */,
Rohan Shahdd588c72018-05-09 20:32:15 -0700650 false /* isNonblockable */, true /* isForBlockingHelper */,
Gus Prevas9abc5062018-10-31 16:11:04 -0400651 true /* isUserSentimentNegative */, false, /* isNoisy */IMPORTANCE_DEFAULT);
Rohan Shahdd588c72018-05-09 20:32:15 -0700652
653 NotificationGuts guts = spy(new NotificationGuts(mContext, null));
654 when(guts.getWindowToken()).thenReturn(mock(IBinder.class));
655 doNothing().when(guts).animateClose(anyInt(), anyInt(), anyBoolean());
656 doNothing().when(guts).setExposed(anyBoolean(), anyBoolean());
657 guts.setGutsContent(mNotificationInfo);
658 mNotificationInfo.setGutsParent(guts);
659
660 mNotificationInfo.findViewById(R.id.keep).performClick();
661
662 verify(mBlockingHelperManager).dismissCurrentBlockingHelper();
663 mTestableLooper.processAllMessages();
664 verify(mMockINotificationManager, times(1))
665 .setNotificationsEnabledWithImportanceLockForPackage(
666 anyString(), eq(TEST_UID), eq(true));
667 }
668
Rohan Shah593e8f12018-05-22 10:06:23 -0700669 @Test
670 public void testCloseControls_nonNullCheckSaveListenerDoesntDelayDismiss()
671 throws Exception {
672 NotificationInfo.CheckSaveListener listener =
673 mock(NotificationInfo.CheckSaveListener.class);
674 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
675 TEST_PACKAGE_NAME, mNotificationChannel /* notificationChannel */,
676 10 /* numUniqueChannelsInRow */, mSbn, listener /* checkSaveListener */,
677 null /* onSettingsClick */, null /* onAppSettingsClick */ ,
678 false /* isNonblockable */, true /* isForBlockingHelper */,
Gus Prevas9abc5062018-10-31 16:11:04 -0400679 true, true /* isUserSentimentNegative */, false, /* isNoisy */
680 IMPORTANCE_DEFAULT);
Rohan Shah593e8f12018-05-22 10:06:23 -0700681
682 mNotificationInfo.handleCloseControls(true /* save */, false /* force */);
683
684 mTestableLooper.processAllMessages();
685 verify(listener, times(0)).checkSave(any(Runnable.class), eq(mSbn));
686 }
687
688 @Test
689 public void testCloseControls_checkSaveListenerDelaysStopNotifications()
690 throws Exception {
691 NotificationInfo.CheckSaveListener listener =
692 mock(NotificationInfo.CheckSaveListener.class);
693 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
694 TEST_PACKAGE_NAME, mNotificationChannel /* notificationChannel */,
695 10 /* numUniqueChannelsInRow */, mSbn, listener /* checkSaveListener */,
Julia Reynolds35765d82018-08-17 11:39:19 -0400696 null /* onSettingsClick */, null /* onAppSettingsClick */,
697 true /* provisioned */,
Rohan Shah593e8f12018-05-22 10:06:23 -0700698 false /* isNonblockable */, true /* isForBlockingHelper */,
Gus Prevas9abc5062018-10-31 16:11:04 -0400699 true /* isUserSentimentNegative */, false, /* isNoisy */IMPORTANCE_DEFAULT);
Rohan Shah593e8f12018-05-22 10:06:23 -0700700
701 mNotificationInfo.findViewById(R.id.block).performClick();
Rohan Shah593e8f12018-05-22 10:06:23 -0700702 mTestableLooper.processAllMessages();
703 verify(listener).checkSave(any(Runnable.class), eq(mSbn));
704 }
Rohan Shahdd588c72018-05-09 20:32:15 -0700705
Rohan Shah590e1b22018-04-10 23:48:47 -0400706 @Test
Rohan Shah524cf7b2018-03-15 14:40:02 -0700707 public void testCloseControls_blockingHelperDismissedIfShown() throws Exception {
708 mNotificationInfo.bindNotification(
709 mMockPackageManager,
710 mMockINotificationManager,
711 TEST_PACKAGE_NAME,
712 mNotificationChannel,
713 1 /* numChannels */,
714 mSbn,
715 null /* checkSaveListener */,
716 null /* onSettingsClick */,
717 null /* onAppSettingsClick */,
Rohan Shah63411fc2018-03-28 19:05:52 -0700718 false /* isNonblockable */,
Rohan Shah524cf7b2018-03-15 14:40:02 -0700719 true /* isForBlockingHelper */,
Julia Reynolds35765d82018-08-17 11:39:19 -0400720 true,
Gus Prevas9abc5062018-10-31 16:11:04 -0400721 false /* isUserSentimentNegative */,
722 false, /* isNoisy */IMPORTANCE_DEFAULT);
Rohan Shahda5dcdd2018-04-27 17:21:50 -0700723 NotificationGuts guts = mock(NotificationGuts.class);
724 doCallRealMethod().when(guts).closeControls(anyInt(), anyInt(), anyBoolean(), anyBoolean());
725 mNotificationInfo.setGutsParent(guts);
Rohan Shah524cf7b2018-03-15 14:40:02 -0700726
727 mNotificationInfo.closeControls(mNotificationInfo);
728
729 verify(mBlockingHelperManager).dismissCurrentBlockingHelper();
730 }
731
732 @Test
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500733 public void testNonBlockableAppDoesNotBecomeBlocked() throws Exception {
Julia Reynolds437cdb12018-01-03 12:27:24 -0500734 mNotificationChannel.setImportance(IMPORTANCE_LOW);
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500735 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Gus Prevas9abc5062018-10-31 16:11:04 -0400736 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true, true,
737 false, IMPORTANCE_DEFAULT);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500738 mNotificationInfo.findViewById(R.id.block).performClick();
739 waitForUndoButton();
Rohan Shahca0447e2018-03-30 15:18:27 -0700740
741 mTestableLooper.processAllMessages();
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500742 verify(mMockINotificationManager, never()).updateNotificationChannelForPackage(
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400743 anyString(), eq(TEST_UID), any());
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500744 }
745
746 @Test
Julia Reynolds437cdb12018-01-03 12:27:24 -0500747 public void testBlockChangedCallsUpdateNotificationChannel() throws Exception {
748 mNotificationChannel.setImportance(IMPORTANCE_LOW);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500749 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Gus Prevas9abc5062018-10-31 16:11:04 -0400750 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true, false,
751 false, IMPORTANCE_DEFAULT);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500752
Julia Reynolds437cdb12018-01-03 12:27:24 -0500753 mNotificationInfo.findViewById(R.id.block).performClick();
754 waitForUndoButton();
Mady Mellorc2dbe492017-03-30 13:22:03 -0700755 mNotificationInfo.handleCloseControls(true, false);
Julia Reynolds8ceb5792017-04-11 11:32:44 -0400756
Rohan Shahca0447e2018-03-30 15:18:27 -0700757 mTestableLooper.processAllMessages();
Julia Reynolds8ceb5792017-04-11 11:32:44 -0400758 ArgumentCaptor<NotificationChannel> updated =
759 ArgumentCaptor.forClass(NotificationChannel.class);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500760 verify(mMockINotificationManager, times(1)).updateNotificationChannelForPackage(
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400761 anyString(), eq(TEST_UID), updated.capture());
Julia Reynolds8ceb5792017-04-11 11:32:44 -0400762 assertTrue((updated.getValue().getUserLockedFields()
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500763 & USER_LOCKED_IMPORTANCE) != 0);
Julia Reynoldse0341482018-03-08 14:42:50 -0500764 assertEquals(IMPORTANCE_NONE, updated.getValue().getImportance());
765 }
766
767 @Test
Rohan Shahca0447e2018-03-30 15:18:27 -0700768 public void testBlockChangedCallsUpdateNotificationChannel_blockingHelper() throws Exception {
769 mNotificationChannel.setImportance(IMPORTANCE_LOW);
770 mNotificationInfo.bindNotification(
771 mMockPackageManager,
772 mMockINotificationManager,
773 TEST_PACKAGE_NAME,
774 mNotificationChannel,
775 1 /* numChannels */,
776 mSbn,
777 null /* checkSaveListener */,
778 null /* onSettingsClick */,
779 null /* onAppSettingsClick */,
Julia Reynolds35765d82018-08-17 11:39:19 -0400780 true /*provisioned */,
Rohan Shahca0447e2018-03-30 15:18:27 -0700781 false /* isNonblockable */,
782 true /* isForBlockingHelper */,
Gus Prevas9abc5062018-10-31 16:11:04 -0400783 true /* isUserSentimentNegative */,
784 false, /* isNoisy */IMPORTANCE_DEFAULT);
Rohan Shahca0447e2018-03-30 15:18:27 -0700785
786 mNotificationInfo.findViewById(R.id.block).performClick();
787 waitForUndoButton();
788 mNotificationInfo.handleCloseControls(true, false);
789
790 mTestableLooper.processAllMessages();
791 ArgumentCaptor<NotificationChannel> updated =
792 ArgumentCaptor.forClass(NotificationChannel.class);
793 verify(mMockINotificationManager, times(1)).updateNotificationChannelForPackage(
794 anyString(), eq(TEST_UID), updated.capture());
795 assertTrue((updated.getValue().getUserLockedFields()
796 & USER_LOCKED_IMPORTANCE) != 0);
797 assertEquals(IMPORTANCE_NONE, updated.getValue().getImportance());
798 }
799
800
801 @Test
Julia Reynoldse0341482018-03-08 14:42:50 -0500802 public void testNonBlockableAppDoesNotBecomeMin() throws Exception {
803 mNotificationChannel.setImportance(IMPORTANCE_LOW);
804 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Gus Prevas9abc5062018-10-31 16:11:04 -0400805 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true, true,
806 false, IMPORTANCE_DEFAULT);
Julia Reynoldse0341482018-03-08 14:42:50 -0500807 mNotificationInfo.findViewById(R.id.minimize).performClick();
808 waitForUndoButton();
Rohan Shahca0447e2018-03-30 15:18:27 -0700809
810 mTestableLooper.processAllMessages();
Julia Reynoldse0341482018-03-08 14:42:50 -0500811 verify(mMockINotificationManager, never()).updateNotificationChannelForPackage(
812 anyString(), eq(TEST_UID), any());
813 }
814
815 @Test
816 public void testMinChangedCallsUpdateNotificationChannel() throws Exception {
817 mNotificationChannel.setImportance(IMPORTANCE_LOW);
818 mSbn.getNotification().flags = Notification.FLAG_FOREGROUND_SERVICE;
819 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Gus Prevas9abc5062018-10-31 16:11:04 -0400820 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true, false,
821 false, IMPORTANCE_DEFAULT);
Julia Reynoldse0341482018-03-08 14:42:50 -0500822
823 mNotificationInfo.findViewById(R.id.minimize).performClick();
824 waitForUndoButton();
825 mNotificationInfo.handleCloseControls(true, false);
826
Rohan Shahca0447e2018-03-30 15:18:27 -0700827 mTestableLooper.processAllMessages();
Julia Reynoldse0341482018-03-08 14:42:50 -0500828 ArgumentCaptor<NotificationChannel> updated =
829 ArgumentCaptor.forClass(NotificationChannel.class);
830 verify(mMockINotificationManager, times(1)).updateNotificationChannelForPackage(
831 anyString(), eq(TEST_UID), updated.capture());
832 assertTrue((updated.getValue().getUserLockedFields()
833 & USER_LOCKED_IMPORTANCE) != 0);
834 assertEquals(IMPORTANCE_MIN, updated.getValue().getImportance());
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500835 }
836
837 @Test
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500838 public void testKeepUpdatesNotificationChannel() throws Exception {
Julia Reynolds437cdb12018-01-03 12:27:24 -0500839 mNotificationChannel.setImportance(IMPORTANCE_LOW);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500840 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Gus Prevas9abc5062018-10-31 16:11:04 -0400841 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true, false,
842 false, IMPORTANCE_DEFAULT);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500843
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500844 mNotificationInfo.handleCloseControls(true, false);
845
Rohan Shahca0447e2018-03-30 15:18:27 -0700846 mTestableLooper.processAllMessages();
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500847 ArgumentCaptor<NotificationChannel> updated =
848 ArgumentCaptor.forClass(NotificationChannel.class);
849 verify(mMockINotificationManager, times(1)).updateNotificationChannelForPackage(
850 anyString(), eq(TEST_UID), updated.capture());
851 assertTrue(0 != (mNotificationChannel.getUserLockedFields() & USER_LOCKED_IMPORTANCE));
852 assertEquals(IMPORTANCE_LOW, mNotificationChannel.getImportance());
853 }
854
855 @Test
856 public void testBlockUndoDoesNotBlockNotificationChannel() throws Exception {
857 mNotificationChannel.setImportance(IMPORTANCE_LOW);
858 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Gus Prevas9abc5062018-10-31 16:11:04 -0400859 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true, false,
860 false, IMPORTANCE_DEFAULT);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500861
862 mNotificationInfo.findViewById(R.id.block).performClick();
863 waitForUndoButton();
864 mNotificationInfo.findViewById(R.id.undo).performClick();
865 waitForStopButton();
866 mNotificationInfo.handleCloseControls(true, false);
867
Rohan Shahca0447e2018-03-30 15:18:27 -0700868 mTestableLooper.processAllMessages();
Julia Reynolds437cdb12018-01-03 12:27:24 -0500869 ArgumentCaptor<NotificationChannel> updated =
870 ArgumentCaptor.forClass(NotificationChannel.class);
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500871 verify(mMockINotificationManager, times(1)).updateNotificationChannelForPackage(
Julia Reynolds437cdb12018-01-03 12:27:24 -0500872 anyString(), eq(TEST_UID), updated.capture());
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500873 assertTrue(0 != (mNotificationChannel.getUserLockedFields() & USER_LOCKED_IMPORTANCE));
Julia Reynolds437cdb12018-01-03 12:27:24 -0500874 assertEquals(IMPORTANCE_LOW, mNotificationChannel.getImportance());
875 }
876
877 @Test
Julia Reynoldse0341482018-03-08 14:42:50 -0500878 public void testMinUndoDoesNotMinNotificationChannel() throws Exception {
879 mNotificationChannel.setImportance(IMPORTANCE_LOW);
880 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Gus Prevas9abc5062018-10-31 16:11:04 -0400881 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true, false,
882 false, IMPORTANCE_DEFAULT);
Julia Reynoldse0341482018-03-08 14:42:50 -0500883
884 mNotificationInfo.findViewById(R.id.minimize).performClick();
885 waitForUndoButton();
886 mNotificationInfo.findViewById(R.id.undo).performClick();
887 waitForStopButton();
888 mNotificationInfo.handleCloseControls(true, false);
889
Rohan Shahca0447e2018-03-30 15:18:27 -0700890 mTestableLooper.processAllMessages();
Julia Reynoldse0341482018-03-08 14:42:50 -0500891 ArgumentCaptor<NotificationChannel> updated =
892 ArgumentCaptor.forClass(NotificationChannel.class);
893 verify(mMockINotificationManager, times(1)).updateNotificationChannelForPackage(
894 anyString(), eq(TEST_UID), updated.capture());
895 assertTrue(0 != (mNotificationChannel.getUserLockedFields() & USER_LOCKED_IMPORTANCE));
896 assertEquals(IMPORTANCE_LOW, mNotificationChannel.getImportance());
897 }
898
899 @Test
Gus Prevas9abc5062018-10-31 16:11:04 -0400900 public void testSilenceCallsUpdateNotificationChannel() throws Exception {
901 mNotificationChannel.setImportance(IMPORTANCE_DEFAULT);
902 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
903 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true, false,
904 true, IMPORTANCE_DEFAULT);
905
906 mNotificationInfo.findViewById(R.id.toggle_silent).performClick();
907 waitForUndoButton();
908 mNotificationInfo.handleCloseControls(true, false);
909
910 mTestableLooper.processAllMessages();
911 ArgumentCaptor<NotificationChannel> updated =
912 ArgumentCaptor.forClass(NotificationChannel.class);
913 verify(mMockINotificationManager, times(1)).updateNotificationChannelForPackage(
914 anyString(), eq(TEST_UID), updated.capture());
915 assertTrue((updated.getValue().getUserLockedFields()
916 & USER_LOCKED_IMPORTANCE) != 0);
917 assertEquals(IMPORTANCE_LOW, updated.getValue().getImportance());
918 }
919
920 @Test
921 public void testUnSilenceCallsUpdateNotificationChannel() throws Exception {
922 mNotificationChannel.setImportance(IMPORTANCE_LOW);
923 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
924 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true, false,
925 false, IMPORTANCE_DEFAULT);
926
927 mNotificationInfo.findViewById(R.id.toggle_silent).performClick();
928 waitForUndoButton();
929 mNotificationInfo.handleCloseControls(true, false);
930
931 mTestableLooper.processAllMessages();
932 ArgumentCaptor<NotificationChannel> updated =
933 ArgumentCaptor.forClass(NotificationChannel.class);
934 verify(mMockINotificationManager, times(1)).updateNotificationChannelForPackage(
935 anyString(), eq(TEST_UID), updated.capture());
936 assertTrue((updated.getValue().getUserLockedFields()
937 & USER_LOCKED_IMPORTANCE) != 0);
938 assertEquals(IMPORTANCE_HIGH, updated.getValue().getImportance());
939 }
940
941 @Test
942 public void testSilenceCallsUpdateNotificationChannel_channelImportanceUnspecified()
943 throws Exception {
944 mNotificationChannel.setImportance(IMPORTANCE_UNSPECIFIED);
945 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
946 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true, false,
947 true, IMPORTANCE_DEFAULT);
948
949 mNotificationInfo.findViewById(R.id.toggle_silent).performClick();
950 waitForUndoButton();
951 mNotificationInfo.handleCloseControls(true, false);
952
953 mTestableLooper.processAllMessages();
954 ArgumentCaptor<NotificationChannel> updated =
955 ArgumentCaptor.forClass(NotificationChannel.class);
956 verify(mMockINotificationManager, times(1)).updateNotificationChannelForPackage(
957 anyString(), eq(TEST_UID), updated.capture());
958 assertTrue((updated.getValue().getUserLockedFields()
959 & USER_LOCKED_IMPORTANCE) != 0);
960 assertEquals(IMPORTANCE_LOW, updated.getValue().getImportance());
961 }
962
963 @Test
964 public void testUnSilenceCallsUpdateNotificationChannel_channelImportanceUnspecified()
965 throws Exception {
966 mNotificationChannel.setImportance(IMPORTANCE_UNSPECIFIED);
967 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
968 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true, false,
969 false, IMPORTANCE_LOW);
970
971 mNotificationInfo.findViewById(R.id.toggle_silent).performClick();
972 waitForUndoButton();
973 mNotificationInfo.handleCloseControls(true, false);
974
975 mTestableLooper.processAllMessages();
976 ArgumentCaptor<NotificationChannel> updated =
977 ArgumentCaptor.forClass(NotificationChannel.class);
978 verify(mMockINotificationManager, times(1)).updateNotificationChannelForPackage(
979 anyString(), eq(TEST_UID), updated.capture());
980 assertTrue((updated.getValue().getUserLockedFields()
981 & USER_LOCKED_IMPORTANCE) != 0);
982 assertEquals(IMPORTANCE_HIGH, updated.getValue().getImportance());
983 }
984
985 @Test
Julia Reynoldse0341482018-03-08 14:42:50 -0500986 public void testCloseControlsDoesNotUpdateiMinIfSaveIsFalse() throws Exception {
987 mNotificationChannel.setImportance(IMPORTANCE_LOW);
988 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Gus Prevas9abc5062018-10-31 16:11:04 -0400989 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true, true,
990 false, IMPORTANCE_DEFAULT);
Julia Reynoldse0341482018-03-08 14:42:50 -0500991
992 mNotificationInfo.findViewById(R.id.minimize).performClick();
993 waitForUndoButton();
994 mNotificationInfo.handleCloseControls(false, false);
Rohan Shahca0447e2018-03-30 15:18:27 -0700995
996 mTestableLooper.processAllMessages();
Julia Reynoldse0341482018-03-08 14:42:50 -0500997 verify(mMockINotificationManager, never()).updateNotificationChannelForPackage(
998 eq(TEST_PACKAGE_NAME), eq(TEST_UID), eq(mNotificationChannel));
999 }
1000
1001 @Test
Julia Reynolds437cdb12018-01-03 12:27:24 -05001002 public void testCloseControlsDoesNotUpdateIfSaveIsFalse() throws Exception {
1003 mNotificationChannel.setImportance(IMPORTANCE_LOW);
1004 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Gus Prevas9abc5062018-10-31 16:11:04 -04001005 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true, true,
1006 false, IMPORTANCE_DEFAULT);
Julia Reynolds437cdb12018-01-03 12:27:24 -05001007
1008 mNotificationInfo.findViewById(R.id.block).performClick();
1009 waitForUndoButton();
Mady Mellorc2dbe492017-03-30 13:22:03 -07001010 mNotificationInfo.handleCloseControls(false, false);
Rohan Shahca0447e2018-03-30 15:18:27 -07001011
1012 mTestableLooper.processAllMessages();
Geoffrey Pitschdf44b602017-02-03 13:31:50 -05001013 verify(mMockINotificationManager, never()).updateNotificationChannelForPackage(
Geoffrey Pitschd034d292017-05-12 11:59:20 -04001014 eq(TEST_PACKAGE_NAME), eq(TEST_UID), eq(mNotificationChannel));
Geoffrey Pitschdf44b602017-02-03 13:31:50 -05001015 }
Geoffrey Pitsch5278d3d2017-03-29 13:39:10 -04001016
1017 @Test
Gus Prevas533836a2018-09-24 17:15:32 -04001018 public void testBlockDoesNothingIfCheckSaveListenerIsNoOp() throws Exception {
Julia Reynolds437cdb12018-01-03 12:27:24 -05001019 mNotificationChannel.setImportance(IMPORTANCE_LOW);
Geoffrey Pitsch5278d3d2017-03-29 13:39:10 -04001020 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds437cdb12018-01-03 12:27:24 -05001021 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn,
Eliot Courtney47098cb2017-10-18 17:30:30 +09001022 (Runnable saveImportance, StatusBarNotification sbn) -> {
Gus Prevas9abc5062018-10-31 16:11:04 -04001023 }, null, null, true, true, false, IMPORTANCE_DEFAULT);
Geoffrey Pitsch5278d3d2017-03-29 13:39:10 -04001024
Julia Reynolds437cdb12018-01-03 12:27:24 -05001025 mNotificationInfo.findViewById(R.id.block).performClick();
Gus Prevas533836a2018-09-24 17:15:32 -04001026 mTestableLooper.processAllMessages();
1027 ensureNoUndoButton();
Mady Mellorc2dbe492017-03-30 13:22:03 -07001028 mNotificationInfo.handleCloseControls(true, false);
Rohan Shahca0447e2018-03-30 15:18:27 -07001029
Geoffrey Pitsch5278d3d2017-03-29 13:39:10 -04001030 verify(mMockINotificationManager, never()).updateNotificationChannelForPackage(
Geoffrey Pitschd034d292017-05-12 11:59:20 -04001031 eq(TEST_PACKAGE_NAME), eq(TEST_UID), eq(mNotificationChannel));
Geoffrey Pitsch5278d3d2017-03-29 13:39:10 -04001032 }
1033
1034 @Test
1035 public void testCloseControlsUpdatesWhenCheckSaveListenerUsesCallback() throws Exception {
Julia Reynolds437cdb12018-01-03 12:27:24 -05001036 mNotificationChannel.setImportance(IMPORTANCE_LOW);
Geoffrey Pitsch5278d3d2017-03-29 13:39:10 -04001037 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds437cdb12018-01-03 12:27:24 -05001038 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn,
Eliot Courtney47098cb2017-10-18 17:30:30 +09001039 (Runnable saveImportance, StatusBarNotification sbn) -> {
Julia Reynolds3aedded2017-03-31 14:42:09 -04001040 saveImportance.run();
Gus Prevas9abc5062018-10-31 16:11:04 -04001041 }, null, null, true, false, false, IMPORTANCE_DEFAULT);
Geoffrey Pitsch5278d3d2017-03-29 13:39:10 -04001042
Julia Reynolds437cdb12018-01-03 12:27:24 -05001043 mNotificationInfo.findViewById(R.id.block).performClick();
Gus Prevas533836a2018-09-24 17:15:32 -04001044 mTestableLooper.processAllMessages();
1045 verify(mMockINotificationManager, never()).updateNotificationChannelForPackage(
1046 eq(TEST_PACKAGE_NAME), eq(TEST_UID), eq(mNotificationChannel));
1047
Julia Reynolds437cdb12018-01-03 12:27:24 -05001048 waitForUndoButton();
Mady Mellorc2dbe492017-03-30 13:22:03 -07001049 mNotificationInfo.handleCloseControls(true, false);
Rohan Shahca0447e2018-03-30 15:18:27 -07001050
1051 mTestableLooper.processAllMessages();
Geoffrey Pitsch5278d3d2017-03-29 13:39:10 -04001052 verify(mMockINotificationManager, times(1)).updateNotificationChannelForPackage(
Geoffrey Pitschd034d292017-05-12 11:59:20 -04001053 eq(TEST_PACKAGE_NAME), eq(TEST_UID), eq(mNotificationChannel));
Geoffrey Pitsch5278d3d2017-03-29 13:39:10 -04001054 }
Julia Reynolds3aedded2017-03-31 14:42:09 -04001055
1056 @Test
1057 public void testDisplaySettingsLink() throws Exception {
1058 final CountDownLatch latch = new CountDownLatch(1);
1059 final String settingsText = "work chats";
1060 final ResolveInfo ri = new ResolveInfo();
1061 ri.activityInfo = new ActivityInfo();
1062 ri.activityInfo.packageName = TEST_PACKAGE_NAME;
1063 ri.activityInfo.name = "something";
1064 List<ResolveInfo> ris = new ArrayList<>();
1065 ris.add(ri);
1066 when(mMockPackageManager.queryIntentActivities(any(), anyInt())).thenReturn(ris);
Julia Reynolds437cdb12018-01-03 12:27:24 -05001067 mNotificationChannel.setImportance(IMPORTANCE_LOW);
Julia Reynolds3aedded2017-03-31 14:42:09 -04001068 Notification n = new Notification.Builder(mContext, mNotificationChannel.getId())
1069 .setSettingsText(settingsText).build();
1070 StatusBarNotification sbn = new StatusBarNotification(TEST_PACKAGE_NAME, TEST_PACKAGE_NAME,
1071 0, null, 0, 0, n, UserHandle.CURRENT, null, 0);
1072
1073 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds437cdb12018-01-03 12:27:24 -05001074 TEST_PACKAGE_NAME, mNotificationChannel, 1, sbn, null, null,
Julia Reynolds3aedded2017-03-31 14:42:09 -04001075 (View v, Intent intent) -> {
1076 latch.countDown();
Gus Prevas9abc5062018-10-31 16:11:04 -04001077 }, true, false, false, IMPORTANCE_DEFAULT);
Julia Reynolds3aedded2017-03-31 14:42:09 -04001078 final TextView settingsLink = mNotificationInfo.findViewById(R.id.app_settings);
1079 assertEquals(View.VISIBLE, settingsLink.getVisibility());
Julia Reynolds3aedded2017-03-31 14:42:09 -04001080 settingsLink.performClick();
1081 assertEquals(0, latch.getCount());
1082 }
1083
1084 @Test
1085 public void testDisplaySettingsLink_multipleChannels() throws Exception {
1086 final CountDownLatch latch = new CountDownLatch(1);
1087 final String settingsText = "work chats";
1088 final ResolveInfo ri = new ResolveInfo();
1089 ri.activityInfo = new ActivityInfo();
1090 ri.activityInfo.packageName = TEST_PACKAGE_NAME;
1091 ri.activityInfo.name = "something";
1092 List<ResolveInfo> ris = new ArrayList<>();
1093 ris.add(ri);
1094 when(mMockPackageManager.queryIntentActivities(any(), anyInt())).thenReturn(ris);
Julia Reynolds437cdb12018-01-03 12:27:24 -05001095 mNotificationChannel.setImportance(IMPORTANCE_LOW);
Julia Reynolds3aedded2017-03-31 14:42:09 -04001096 Notification n = new Notification.Builder(mContext, mNotificationChannel.getId())
1097 .setSettingsText(settingsText).build();
1098 StatusBarNotification sbn = new StatusBarNotification(TEST_PACKAGE_NAME, TEST_PACKAGE_NAME,
1099 0, null, 0, 0, n, UserHandle.CURRENT, null, 0);
1100
1101 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Rohan Shah63411fc2018-03-28 19:05:52 -07001102 TEST_PACKAGE_NAME, mNotificationChannel, MULTIPLE_CHANNEL_COUNT, sbn, null, null,
Julia Reynolds437cdb12018-01-03 12:27:24 -05001103 (View v, Intent intent) -> {
Julia Reynolds3aedded2017-03-31 14:42:09 -04001104 latch.countDown();
Gus Prevas9abc5062018-10-31 16:11:04 -04001105 }, true, false, false, IMPORTANCE_DEFAULT);
Julia Reynolds3aedded2017-03-31 14:42:09 -04001106 final TextView settingsLink = mNotificationInfo.findViewById(R.id.app_settings);
1107 assertEquals(View.VISIBLE, settingsLink.getVisibility());
1108 settingsLink.performClick();
1109 assertEquals(0, latch.getCount());
1110 }
1111
1112 @Test
1113 public void testNoSettingsLink_noHandlingActivity() throws Exception {
1114 final String settingsText = "work chats";
1115 when(mMockPackageManager.queryIntentActivities(any(), anyInt())).thenReturn(null);
Julia Reynolds437cdb12018-01-03 12:27:24 -05001116 mNotificationChannel.setImportance(IMPORTANCE_LOW);
Julia Reynolds3aedded2017-03-31 14:42:09 -04001117 Notification n = new Notification.Builder(mContext, mNotificationChannel.getId())
1118 .setSettingsText(settingsText).build();
1119 StatusBarNotification sbn = new StatusBarNotification(TEST_PACKAGE_NAME, TEST_PACKAGE_NAME,
1120 0, null, 0, 0, n, UserHandle.CURRENT, null, 0);
1121
1122 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Rohan Shah63411fc2018-03-28 19:05:52 -07001123 TEST_PACKAGE_NAME, mNotificationChannel, MULTIPLE_CHANNEL_COUNT, sbn, null, null,
Gus Prevas9abc5062018-10-31 16:11:04 -04001124 null, true, false, false, IMPORTANCE_DEFAULT);
Julia Reynolds3aedded2017-03-31 14:42:09 -04001125 final TextView settingsLink = mNotificationInfo.findViewById(R.id.app_settings);
Julia Reynolds437cdb12018-01-03 12:27:24 -05001126 assertEquals(GONE, settingsLink.getVisibility());
Julia Reynolds3aedded2017-03-31 14:42:09 -04001127 }
1128
1129 @Test
1130 public void testNoSettingsLink_noLinkText() throws Exception {
1131 final ResolveInfo ri = new ResolveInfo();
1132 ri.activityInfo = new ActivityInfo();
1133 ri.activityInfo.packageName = TEST_PACKAGE_NAME;
1134 ri.activityInfo.name = "something";
1135 List<ResolveInfo> ris = new ArrayList<>();
1136 ris.add(ri);
1137 when(mMockPackageManager.queryIntentActivities(any(), anyInt())).thenReturn(ris);
Julia Reynolds437cdb12018-01-03 12:27:24 -05001138 mNotificationChannel.setImportance(IMPORTANCE_LOW);
Julia Reynolds3aedded2017-03-31 14:42:09 -04001139 Notification n = new Notification.Builder(mContext, mNotificationChannel.getId()).build();
1140 StatusBarNotification sbn = new StatusBarNotification(TEST_PACKAGE_NAME, TEST_PACKAGE_NAME,
1141 0, null, 0, 0, n, UserHandle.CURRENT, null, 0);
1142
1143 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Gus Prevas9abc5062018-10-31 16:11:04 -04001144 TEST_PACKAGE_NAME, mNotificationChannel, 1, sbn, null, null, null, true, false,
1145 false, IMPORTANCE_DEFAULT);
Julia Reynolds3aedded2017-03-31 14:42:09 -04001146 final TextView settingsLink = mNotificationInfo.findViewById(R.id.app_settings);
Julia Reynolds437cdb12018-01-03 12:27:24 -05001147 assertEquals(GONE, settingsLink.getVisibility());
Julia Reynolds3aedded2017-03-31 14:42:09 -04001148 }
1149
Rohan Shah7c6b37a2018-03-30 21:07:45 +00001150 @Test
1151 public void testBindHeader_noSettingsLinkWhenIsForBlockingHelper() throws Exception {
1152 final String settingsText = "work chats";
1153 final ResolveInfo ri = new ResolveInfo();
1154 ri.activityInfo = new ActivityInfo();
1155 ri.activityInfo.packageName = TEST_PACKAGE_NAME;
1156 ri.activityInfo.name = "something";
1157 List<ResolveInfo> ris = new ArrayList<>();
1158 ris.add(ri);
1159 when(mMockPackageManager.queryIntentActivities(any(), anyInt())).thenReturn(ris);
1160 mNotificationChannel.setImportance(IMPORTANCE_LOW);
1161 Notification n = new Notification.Builder(mContext, mNotificationChannel.getId())
1162 .setSettingsText(settingsText).build();
1163 StatusBarNotification sbn = new StatusBarNotification(TEST_PACKAGE_NAME, TEST_PACKAGE_NAME,
1164 0, null, 0, 0, n, UserHandle.CURRENT, null, 0);
1165
1166 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
1167 TEST_PACKAGE_NAME, mNotificationChannel, 1, sbn, null, null, null, false, true,
Gus Prevas9abc5062018-10-31 16:11:04 -04001168 true, true, false, IMPORTANCE_DEFAULT);
Rohan Shah7c6b37a2018-03-30 21:07:45 +00001169 final TextView settingsLink = mNotificationInfo.findViewById(R.id.app_settings);
1170 assertEquals(GONE, settingsLink.getVisibility());
1171 }
1172
Geoffrey Pitsch8c8dbde2017-04-20 11:44:33 -04001173
1174 @Test
1175 public void testWillBeRemovedReturnsFalseBeforeBind() throws Exception {
1176 assertFalse(mNotificationInfo.willBeRemoved());
1177 }
Julia Reynoldse0341482018-03-08 14:42:50 -05001178
1179 @Test
1180 public void testUndoText_min() throws Exception {
1181 mSbn.getNotification().flags = Notification.FLAG_FOREGROUND_SERVICE;
1182 mNotificationChannel.setImportance(IMPORTANCE_LOW);
1183 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Gus Prevas9abc5062018-10-31 16:11:04 -04001184 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true, true,
1185 false, IMPORTANCE_DEFAULT);
Julia Reynoldse0341482018-03-08 14:42:50 -05001186
1187 mNotificationInfo.findViewById(R.id.minimize).performClick();
1188 waitForUndoButton();
1189 TextView confirmationText = mNotificationInfo.findViewById(R.id.confirmation_text);
1190 assertTrue(confirmationText.getText().toString().contains("minimized"));
1191 }
1192
1193 @Test
1194 public void testUndoText_block() throws Exception {
1195 mNotificationChannel.setImportance(IMPORTANCE_LOW);
1196 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Gus Prevas9abc5062018-10-31 16:11:04 -04001197 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true, true,
1198 false, IMPORTANCE_DEFAULT);
Julia Reynoldse0341482018-03-08 14:42:50 -05001199
1200 mNotificationInfo.findViewById(R.id.block).performClick();
1201 waitForUndoButton();
1202 TextView confirmationText = mNotificationInfo.findViewById(R.id.confirmation_text);
1203 assertTrue(confirmationText.getText().toString().contains("won't see"));
1204 }
1205
1206 @Test
Gus Prevas9abc5062018-10-31 16:11:04 -04001207 public void testUndoText_silence() throws Exception {
1208 mNotificationChannel.setImportance(IMPORTANCE_DEFAULT);
1209 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
1210 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true, true,
1211 true, IMPORTANCE_DEFAULT);
1212
1213 mNotificationInfo.findViewById(R.id.toggle_silent).performClick();
1214 waitForUndoButton();
1215 TextView confirmationText = mNotificationInfo.findViewById(R.id.confirmation_text);
1216 assertEquals(mContext.getString(R.string.notification_channel_silenced),
1217 confirmationText.getText());
1218 }
1219
1220 @Test
1221 public void testUndoText_unsilence() throws Exception {
1222 mNotificationChannel.setImportance(IMPORTANCE_LOW);
1223 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
1224 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true, true,
1225 true, IMPORTANCE_DEFAULT);
1226
1227 mNotificationInfo.findViewById(R.id.toggle_silent).performClick();
1228 waitForUndoButton();
1229 TextView confirmationText = mNotificationInfo.findViewById(R.id.confirmation_text);
1230 assertEquals(mContext.getString(R.string.notification_channel_unsilenced),
1231 confirmationText.getText());
1232 }
1233
1234 @Test
Julia Reynoldse0341482018-03-08 14:42:50 -05001235 public void testNoHeaderOnConfirmation() throws Exception {
1236 mNotificationChannel.setImportance(IMPORTANCE_LOW);
1237 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Gus Prevas9abc5062018-10-31 16:11:04 -04001238 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true, true,
1239 false, IMPORTANCE_DEFAULT);
Julia Reynoldse0341482018-03-08 14:42:50 -05001240
1241 mNotificationInfo.findViewById(R.id.block).performClick();
1242 waitForUndoButton();
1243 assertEquals(GONE, mNotificationInfo.findViewById(R.id.header).getVisibility());
1244 }
1245
1246 @Test
1247 public void testHeaderOnUndo() throws Exception {
1248 mNotificationChannel.setImportance(IMPORTANCE_LOW);
1249 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Gus Prevas9abc5062018-10-31 16:11:04 -04001250 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true, true,
1251 false, IMPORTANCE_DEFAULT);
Julia Reynoldse0341482018-03-08 14:42:50 -05001252
1253 mNotificationInfo.findViewById(R.id.block).performClick();
1254 waitForUndoButton();
1255 mNotificationInfo.findViewById(R.id.undo).performClick();
1256 waitForStopButton();
1257 assertEquals(VISIBLE, mNotificationInfo.findViewById(R.id.header).getVisibility());
1258 }
Geoffrey Pitschdf44b602017-02-03 13:31:50 -05001259}