blob: d77bf69ad3650a2597b7031b5c10b5beff7b857b [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
14 * limitations under the License.
15 */
16
17package com.android.systemui.statusbar;
18
Julia Reynolds437cdb12018-01-03 12:27:24 -050019import static android.app.NotificationManager.IMPORTANCE_LOW;
Julia Reynoldsf7321592017-05-24 16:09:19 -040020import static android.print.PrintManager.PRINT_SPOOLER_PACKAGE_NAME;
Julia Reynolds437cdb12018-01-03 12:27:24 -050021import static android.view.View.GONE;
22import static android.view.View.VISIBLE;
Julia Reynoldsf7321592017-05-24 16:09:19 -040023
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050024import static junit.framework.Assert.assertEquals;
25import static junit.framework.Assert.assertFalse;
26import static junit.framework.Assert.assertTrue;
Julia Reynolds5a311932017-03-01 16:33:44 -050027
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050028import static org.mockito.Mockito.any;
29import static org.mockito.Mockito.anyBoolean;
30import static org.mockito.Mockito.anyInt;
31import static org.mockito.Mockito.anyString;
32import static org.mockito.Mockito.eq;
33import static org.mockito.Mockito.mock;
34import static org.mockito.Mockito.never;
35import static org.mockito.Mockito.times;
36import static org.mockito.Mockito.verify;
37import static org.mockito.Mockito.when;
38
39import android.app.INotificationManager;
Julia Reynolds3aedded2017-03-31 14:42:09 -040040import android.app.Notification;
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050041import android.app.NotificationChannel;
42import android.app.NotificationChannelGroup;
43import android.app.NotificationManager;
Julia Reynolds3aedded2017-03-31 14:42:09 -040044import android.content.Intent;
45import android.content.pm.ActivityInfo;
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050046import android.content.pm.ApplicationInfo;
47import android.content.pm.PackageInfo;
48import android.content.pm.PackageManager;
Julia Reynolds3aedded2017-03-31 14:42:09 -040049import android.content.pm.ResolveInfo;
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050050import android.graphics.drawable.Drawable;
Julia Reynolds3aedded2017-03-31 14:42:09 -040051import android.os.UserHandle;
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050052import android.service.notification.StatusBarNotification;
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050053import android.test.suitebuilder.annotation.SmallTest;
Jason Monk340b0e52017-03-08 14:57:56 -050054import android.testing.AndroidTestingRunner;
Julia Reynolds437cdb12018-01-03 12:27:24 -050055import android.testing.PollingCheck;
Jason Monk340b0e52017-03-08 14:57:56 -050056import android.testing.UiThreadTest;
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050057import android.view.LayoutInflater;
58import android.view.View;
59import android.widget.ImageView;
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050060import android.widget.TextView;
Jason Monk340b0e52017-03-08 14:57:56 -050061
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050062import com.android.systemui.R;
63import com.android.systemui.SysuiTestCase;
Jason Monkba364322017-03-06 11:19:20 -050064
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050065import org.junit.Before;
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050066import org.junit.Test;
Jason Monk340b0e52017-03-08 14:57:56 -050067import org.junit.runner.RunWith;
Julia Reynolds8ceb5792017-04-11 11:32:44 -040068import org.mockito.ArgumentCaptor;
Julia Reynolds3aedded2017-03-31 14:42:09 -040069
70import java.util.ArrayList;
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050071import java.util.Collections;
Geoffrey Pitschd0856f02017-02-16 10:51:18 -050072import java.util.List;
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050073import java.util.concurrent.CountDownLatch;
74
75@SmallTest
Jason Monk340b0e52017-03-08 14:57:56 -050076@RunWith(AndroidTestingRunner.class)
Jason Monkba364322017-03-06 11:19:20 -050077@UiThreadTest
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050078public class NotificationInfoTest extends SysuiTestCase {
79 private static final String TEST_PACKAGE_NAME = "test_package";
Julia Reynoldsf7321592017-05-24 16:09:19 -040080 private static final String TEST_SYSTEM_PACKAGE_NAME = PRINT_SPOOLER_PACKAGE_NAME;
Geoffrey Pitschd034d292017-05-12 11:59:20 -040081 private static final int TEST_UID = 1;
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050082 private static final String TEST_CHANNEL = "test_channel";
83 private static final String TEST_CHANNEL_NAME = "TEST CHANNEL NAME";
84
85 private NotificationInfo mNotificationInfo;
86 private final INotificationManager mMockINotificationManager = mock(INotificationManager.class);
87 private final PackageManager mMockPackageManager = mock(PackageManager.class);
88 private NotificationChannel mNotificationChannel;
Geoffrey Pitschd0856f02017-02-16 10:51:18 -050089 private NotificationChannel mDefaultNotificationChannel;
Julia Reynolds3aedded2017-03-31 14:42:09 -040090 private StatusBarNotification mSbn;
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050091
92 @Before
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050093 public void setUp() throws Exception {
94 // Inflate the layout
Geoffrey Pitschd0856f02017-02-16 10:51:18 -050095 final LayoutInflater layoutInflater = LayoutInflater.from(mContext);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050096 mNotificationInfo = (NotificationInfo) layoutInflater.inflate(R.layout.notification_info,
97 null);
98
99 // PackageManager must return a packageInfo and applicationInfo.
100 final PackageInfo packageInfo = new PackageInfo();
101 packageInfo.packageName = TEST_PACKAGE_NAME;
Julia Reynoldsf7321592017-05-24 16:09:19 -0400102 when(mMockPackageManager.getPackageInfo(eq(TEST_PACKAGE_NAME), anyInt()))
103 .thenReturn(packageInfo);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500104 final ApplicationInfo applicationInfo = new ApplicationInfo();
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400105 applicationInfo.uid = TEST_UID; // non-zero
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500106 when(mMockPackageManager.getApplicationInfo(anyString(), anyInt())).thenReturn(
107 applicationInfo);
Julia Reynoldsf7321592017-05-24 16:09:19 -0400108 final PackageInfo systemPackageInfo = new PackageInfo();
109 systemPackageInfo.packageName = TEST_SYSTEM_PACKAGE_NAME;
110 when(mMockPackageManager.getPackageInfo(eq(TEST_SYSTEM_PACKAGE_NAME), anyInt()))
111 .thenReturn(systemPackageInfo);
112 when(mMockPackageManager.getPackageInfo(eq("android"), anyInt()))
113 .thenReturn(packageInfo);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500114
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500115 // Package has one channel by default.
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500116 when(mMockINotificationManager.getNumNotificationChannelsForPackage(
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400117 eq(TEST_PACKAGE_NAME), eq(TEST_UID), anyBoolean())).thenReturn(1);
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500118
119 // Some test channels.
120 mNotificationChannel = new NotificationChannel(
Julia Reynolds437cdb12018-01-03 12:27:24 -0500121 TEST_CHANNEL, TEST_CHANNEL_NAME, IMPORTANCE_LOW);
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500122 mDefaultNotificationChannel = new NotificationChannel(
123 NotificationChannel.DEFAULT_CHANNEL_ID, TEST_CHANNEL_NAME,
Julia Reynolds437cdb12018-01-03 12:27:24 -0500124 IMPORTANCE_LOW);
Julia Reynolds33bef2c2017-09-05 11:07:18 -0400125 mSbn = new StatusBarNotification(TEST_PACKAGE_NAME, TEST_PACKAGE_NAME, 0, null, TEST_UID, 0,
Julia Reynolds3aedded2017-03-31 14:42:09 -0400126 new Notification(), UserHandle.CURRENT, null, 0);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500127 }
128
Julia Reynolds437cdb12018-01-03 12:27:24 -0500129 // TODO: if tests are taking too long replace this with something that makes the animation
130 // finish instantly.
131 private void waitForUndoButton() {
132 PollingCheck.waitFor(1000,
133 () -> VISIBLE == mNotificationInfo.findViewById(R.id.confirmation).getVisibility());
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500134 }
Julia Reynolds437cdb12018-01-03 12:27:24 -0500135 private void waitForStopButton() {
136 PollingCheck.waitFor(1000,
137 () -> VISIBLE == mNotificationInfo.findViewById(R.id.prompt).getVisibility());
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500138 }
139
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500140 @Test
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500141 public void testBindNotification_SetsTextApplicationName() throws Exception {
142 when(mMockPackageManager.getApplicationLabel(any())).thenReturn("App Name");
143 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds437cdb12018-01-03 12:27:24 -0500144 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, null);
145 final TextView textView = mNotificationInfo.findViewById(R.id.pkgname);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500146 assertTrue(textView.getText().toString().contains("App Name"));
147 }
148
149 @Test
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500150 public void testBindNotification_SetsPackageIcon() throws Exception {
151 final Drawable iconDrawable = mock(Drawable.class);
152 when(mMockPackageManager.getApplicationIcon(any(ApplicationInfo.class)))
153 .thenReturn(iconDrawable);
154 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds437cdb12018-01-03 12:27:24 -0500155 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, null);
156 final ImageView iconView = mNotificationInfo.findViewById(R.id.pkgicon);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500157 assertEquals(iconDrawable, iconView.getDrawable());
158 }
159
160 @Test
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500161 public void testBindNotification_GroupNameHiddenIfNoGroup() throws Exception {
162 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds437cdb12018-01-03 12:27:24 -0500163 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, null);
164 final TextView groupNameView = mNotificationInfo.findViewById(R.id.group_name);
165 assertEquals(GONE, groupNameView.getVisibility());
166 final TextView groupDividerView = mNotificationInfo.findViewById(R.id.pkg_group_divider);
167 assertEquals(GONE, groupDividerView.getVisibility());
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500168 }
169
170 @Test
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500171 public void testBindNotification_SetsGroupNameIfNonNull() throws Exception {
172 mNotificationChannel.setGroup("test_group_id");
173 final NotificationChannelGroup notificationChannelGroup =
174 new NotificationChannelGroup("test_group_id", "Test Group Name");
175 when(mMockINotificationManager.getNotificationChannelGroupForPackage(
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400176 eq("test_group_id"), eq(TEST_PACKAGE_NAME), eq(TEST_UID)))
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500177 .thenReturn(notificationChannelGroup);
178 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds437cdb12018-01-03 12:27:24 -0500179 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, null);
180 final TextView groupNameView = mNotificationInfo.findViewById(R.id.group_name);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500181 assertEquals(View.VISIBLE, groupNameView.getVisibility());
182 assertEquals("Test Group Name", groupNameView.getText());
Julia Reynolds437cdb12018-01-03 12:27:24 -0500183 final TextView groupDividerView = mNotificationInfo.findViewById(R.id.pkg_group_divider);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500184 assertEquals(View.VISIBLE, groupDividerView.getVisibility());
185 }
186
187 @Test
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500188 public void testBindNotification_SetsTextChannelName() throws Exception {
189 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds437cdb12018-01-03 12:27:24 -0500190 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, null);
191 final TextView textView = mNotificationInfo.findViewById(R.id.channel_name);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500192 assertEquals(TEST_CHANNEL_NAME, textView.getText());
193 }
194
195 @Test
Geoffrey Pitsch1fbf66e2017-05-09 11:32:27 -0400196 public void testBindNotification_DefaultChannelDoesNotUseChannelName() throws Exception {
197 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds437cdb12018-01-03 12:27:24 -0500198 TEST_PACKAGE_NAME, mDefaultNotificationChannel, 1, mSbn, null, null, null, null);
199 final TextView textView = mNotificationInfo.findViewById(R.id.channel_name);
200 assertEquals(GONE, textView.getVisibility());
Geoffrey Pitsch1fbf66e2017-05-09 11:32:27 -0400201 }
202
203 @Test
Julia Reynolds437cdb12018-01-03 12:27:24 -0500204 public void testBindNotification_UnblockablePackageUsesChannelName() throws Exception {
Geoffrey Pitschb43486a2017-06-01 13:45:59 -0400205 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds437cdb12018-01-03 12:27:24 -0500206 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null,
207 Collections.singleton(TEST_PACKAGE_NAME));
208 final TextView textView = mNotificationInfo.findViewById(R.id.channel_name);
209 assertEquals(VISIBLE, textView.getVisibility());
Geoffrey Pitsch1fbf66e2017-05-09 11:32:27 -0400210 }
211
212 @Test
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500213 public void testBindNotification_SetsOnClickListenerForSettings() throws Exception {
214 final CountDownLatch latch = new CountDownLatch(1);
215 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds437cdb12018-01-03 12:27:24 -0500216 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null,
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500217 (View v, NotificationChannel c, int appUid) -> {
Julia Reynolds3aedded2017-03-31 14:42:09 -0400218 assertEquals(mNotificationChannel, c);
219 latch.countDown();
Julia Reynolds437cdb12018-01-03 12:27:24 -0500220 }, null, null);
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500221
Julia Reynolds437cdb12018-01-03 12:27:24 -0500222 final View settingsButton = mNotificationInfo.findViewById(R.id.info);
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500223 settingsButton.performClick();
224 // Verify that listener was triggered.
225 assertEquals(0, latch.getCount());
226 }
227
228 @Test
Geoffrey Pitsch029a3fa2017-03-30 13:01:57 -0400229 public void testBindNotification_SettingsButtonInvisibleWhenNoClickListener() throws Exception {
230 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds437cdb12018-01-03 12:27:24 -0500231 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, null);
232 final View settingsButton = mNotificationInfo.findViewById(R.id.info);
Geoffrey Pitsch029a3fa2017-03-30 13:01:57 -0400233 assertTrue(settingsButton.getVisibility() != View.VISIBLE);
234 }
235
236 @Test
Julia Reynolds437cdb12018-01-03 12:27:24 -0500237 public void testBindNotification_SettingsButtonReappearsAfterSecondBind() throws Exception {
Geoffrey Pitsch029a3fa2017-03-30 13:01:57 -0400238 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds437cdb12018-01-03 12:27:24 -0500239 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, null);
Geoffrey Pitsch029a3fa2017-03-30 13:01:57 -0400240 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds437cdb12018-01-03 12:27:24 -0500241 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null,
242 (View v, NotificationChannel c, int appUid) -> {
243 }, null, null);
244 final View settingsButton = mNotificationInfo.findViewById(R.id.info);
Geoffrey Pitsch029a3fa2017-03-30 13:01:57 -0400245 assertEquals(View.VISIBLE, settingsButton.getVisibility());
246 }
247
248 @Test
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500249 public void testOnClickListenerPassesNullChannelForBundle() throws Exception {
250 final CountDownLatch latch = new CountDownLatch(1);
251 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds437cdb12018-01-03 12:27:24 -0500252 TEST_PACKAGE_NAME, mNotificationChannel, 2, mSbn, null,
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500253 (View v, NotificationChannel c, int appUid) -> {
Julia Reynolds3aedded2017-03-31 14:42:09 -0400254 assertEquals(null, c);
255 latch.countDown();
Julia Reynolds437cdb12018-01-03 12:27:24 -0500256 }, null, null);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500257
Julia Reynolds437cdb12018-01-03 12:27:24 -0500258 mNotificationInfo.findViewById(R.id.info).performClick();
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500259 // Verify that listener was triggered.
260 assertEquals(0, latch.getCount());
261 }
262
263 @Test
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500264 @UiThreadTest
Julia Reynolds437cdb12018-01-03 12:27:24 -0500265 public void testBindNotification_ChannelNameInvisibleWhenBundleFromDifferentChannels()
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500266 throws Exception {
267 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds437cdb12018-01-03 12:27:24 -0500268 TEST_PACKAGE_NAME, mNotificationChannel, 2, mSbn, null, null, null, null);
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500269 final TextView channelNameView =
Julia Reynolds437cdb12018-01-03 12:27:24 -0500270 mNotificationInfo.findViewById(R.id.channel_name);
271 assertEquals(GONE, channelNameView.getVisibility());
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500272 }
273
274 @Test
275 @UiThreadTest
Julia Reynolds437cdb12018-01-03 12:27:24 -0500276 public void testStopInvisibleIfBundleFromDifferentChannels() throws Exception {
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500277 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds437cdb12018-01-03 12:27:24 -0500278 TEST_PACKAGE_NAME, mNotificationChannel, 2, mSbn, null, null, null, null);
279 final TextView blockView = mNotificationInfo.findViewById(R.id.block);
280 assertEquals(GONE, blockView.getVisibility());
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500281 }
282
283 @Test
Geoffrey Pitschb43486a2017-06-01 13:45:59 -0400284 public void testbindNotification_UnblockableTextVisibleWhenAppUnblockable() throws Exception {
285 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds437cdb12018-01-03 12:27:24 -0500286 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null,
Geoffrey Pitschb43486a2017-06-01 13:45:59 -0400287 null, Collections.singleton(TEST_PACKAGE_NAME));
Julia Reynolds437cdb12018-01-03 12:27:24 -0500288 final TextView view = mNotificationInfo.findViewById(R.id.block_prompt);
289 assertEquals(View.VISIBLE, view.getVisibility());
Geoffrey Pitschb43486a2017-06-01 13:45:59 -0400290 assertEquals(mContext.getString(R.string.notification_unblockable_desc),
Julia Reynolds437cdb12018-01-03 12:27:24 -0500291 view.getText());
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500292 }
293
294 @Test
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500295 public void testBindNotification_DoesNotUpdateNotificationChannel() throws Exception {
296 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds437cdb12018-01-03 12:27:24 -0500297 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, null);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500298 verify(mMockINotificationManager, never()).updateNotificationChannelForPackage(
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400299 anyString(), eq(TEST_UID), any());
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500300 }
301
302 @Test
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500303 public void testDoesNotUpdateNotificationChannelAfterImportanceChanged() throws Exception {
Julia Reynolds437cdb12018-01-03 12:27:24 -0500304 mNotificationChannel.setImportance(IMPORTANCE_LOW);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500305 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds437cdb12018-01-03 12:27:24 -0500306 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, null);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500307
Julia Reynolds437cdb12018-01-03 12:27:24 -0500308 mNotificationInfo.findViewById(R.id.block).performClick();
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500309 verify(mMockINotificationManager, never()).updateNotificationChannelForPackage(
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400310 anyString(), eq(TEST_UID), any());
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500311 }
312
313 @Test
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500314 public void testHandleCloseControls_DoesNotUpdateNotificationChannelIfUnchanged()
315 throws Exception {
316 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds437cdb12018-01-03 12:27:24 -0500317 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, null);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500318
Mady Mellorc2dbe492017-03-30 13:22:03 -0700319 mNotificationInfo.handleCloseControls(true, false);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500320 verify(mMockINotificationManager, never()).updateNotificationChannelForPackage(
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400321 anyString(), eq(TEST_UID), any());
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500322 }
323
324 @Test
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500325 public void testHandleCloseControls_DoesNotUpdateNotificationChannelIfUnspecified()
326 throws Exception {
327 mNotificationChannel.setImportance(NotificationManager.IMPORTANCE_UNSPECIFIED);
328 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds437cdb12018-01-03 12:27:24 -0500329 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, null);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500330
Mady Mellorc2dbe492017-03-30 13:22:03 -0700331 mNotificationInfo.handleCloseControls(true, false);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500332 verify(mMockINotificationManager, never()).updateNotificationChannelForPackage(
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400333 anyString(), eq(TEST_UID), any());
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500334 }
335
336 @Test
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500337 public void testNonBlockableAppDoesNotBecomeBlocked() throws Exception {
Julia Reynolds437cdb12018-01-03 12:27:24 -0500338 mNotificationChannel.setImportance(IMPORTANCE_LOW);
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500339 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds437cdb12018-01-03 12:27:24 -0500340 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null,
Julia Reynolds3aedded2017-03-31 14:42:09 -0400341 null, Collections.singleton(TEST_PACKAGE_NAME));
Julia Reynolds437cdb12018-01-03 12:27:24 -0500342 mNotificationInfo.findViewById(R.id.block).performClick();
343 waitForUndoButton();
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500344 verify(mMockINotificationManager, never()).updateNotificationChannelForPackage(
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400345 anyString(), eq(TEST_UID), any());
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500346 }
347
348 @Test
Julia Reynolds437cdb12018-01-03 12:27:24 -0500349 public void testBlockChangedCallsUpdateNotificationChannel() throws Exception {
350 mNotificationChannel.setImportance(IMPORTANCE_LOW);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500351 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds437cdb12018-01-03 12:27:24 -0500352 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, null);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500353
Julia Reynolds437cdb12018-01-03 12:27:24 -0500354 mNotificationInfo.findViewById(R.id.block).performClick();
355 waitForUndoButton();
Mady Mellorc2dbe492017-03-30 13:22:03 -0700356 mNotificationInfo.handleCloseControls(true, false);
Julia Reynolds8ceb5792017-04-11 11:32:44 -0400357
358 ArgumentCaptor<NotificationChannel> updated =
359 ArgumentCaptor.forClass(NotificationChannel.class);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500360 verify(mMockINotificationManager, times(1)).updateNotificationChannelForPackage(
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400361 anyString(), eq(TEST_UID), updated.capture());
Julia Reynolds8ceb5792017-04-11 11:32:44 -0400362 assertTrue((updated.getValue().getUserLockedFields()
363 & NotificationChannel.USER_LOCKED_IMPORTANCE) != 0);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500364 }
365
366 @Test
Julia Reynolds437cdb12018-01-03 12:27:24 -0500367 public void testBlockUndoDoesNotCallUpdateNotificationChannel() throws Exception {
368 mNotificationChannel.setImportance(IMPORTANCE_LOW);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500369 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds437cdb12018-01-03 12:27:24 -0500370 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null,
371 Collections.singleton(TEST_PACKAGE_NAME));
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500372
Julia Reynolds437cdb12018-01-03 12:27:24 -0500373
374 mNotificationInfo.findViewById(R.id.block).performClick();
375 waitForUndoButton();
376 mNotificationInfo.findViewById(R.id.undo).performClick();
377 waitForStopButton();
378 mNotificationInfo.handleCloseControls(true, false);
379
380 ArgumentCaptor<NotificationChannel> updated =
381 ArgumentCaptor.forClass(NotificationChannel.class);
382 verify(mMockINotificationManager, never()).updateNotificationChannelForPackage(
383 anyString(), eq(TEST_UID), updated.capture());
384 assertEquals(IMPORTANCE_LOW, mNotificationChannel.getImportance());
385 }
386
387 @Test
388 public void testCloseControlsDoesNotUpdateIfSaveIsFalse() throws Exception {
389 mNotificationChannel.setImportance(IMPORTANCE_LOW);
390 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
391 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null,
392 Collections.singleton(TEST_PACKAGE_NAME));
393
394 mNotificationInfo.findViewById(R.id.block).performClick();
395 waitForUndoButton();
Mady Mellorc2dbe492017-03-30 13:22:03 -0700396 mNotificationInfo.handleCloseControls(false, false);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500397 verify(mMockINotificationManager, never()).updateNotificationChannelForPackage(
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400398 eq(TEST_PACKAGE_NAME), eq(TEST_UID), eq(mNotificationChannel));
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500399 }
Geoffrey Pitsch5278d3d2017-03-29 13:39:10 -0400400
401 @Test
402 public void testCloseControlsDoesNotUpdateIfCheckSaveListenerIsNoOp() throws Exception {
Julia Reynolds437cdb12018-01-03 12:27:24 -0500403 mNotificationChannel.setImportance(IMPORTANCE_LOW);
Geoffrey Pitsch5278d3d2017-03-29 13:39:10 -0400404 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds437cdb12018-01-03 12:27:24 -0500405 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn,
Eliot Courtney47098cb2017-10-18 17:30:30 +0900406 (Runnable saveImportance, StatusBarNotification sbn) -> {
Julia Reynolds437cdb12018-01-03 12:27:24 -0500407 }, null, null, Collections.singleton(TEST_PACKAGE_NAME));
Geoffrey Pitsch5278d3d2017-03-29 13:39:10 -0400408
Julia Reynolds437cdb12018-01-03 12:27:24 -0500409 mNotificationInfo.findViewById(R.id.block).performClick();
410 waitForUndoButton();
Mady Mellorc2dbe492017-03-30 13:22:03 -0700411 mNotificationInfo.handleCloseControls(true, false);
Geoffrey Pitsch5278d3d2017-03-29 13:39:10 -0400412 verify(mMockINotificationManager, never()).updateNotificationChannelForPackage(
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400413 eq(TEST_PACKAGE_NAME), eq(TEST_UID), eq(mNotificationChannel));
Geoffrey Pitsch5278d3d2017-03-29 13:39:10 -0400414 }
415
416 @Test
417 public void testCloseControlsUpdatesWhenCheckSaveListenerUsesCallback() throws Exception {
Julia Reynolds437cdb12018-01-03 12:27:24 -0500418 mNotificationChannel.setImportance(IMPORTANCE_LOW);
Geoffrey Pitsch5278d3d2017-03-29 13:39:10 -0400419 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds437cdb12018-01-03 12:27:24 -0500420 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn,
Eliot Courtney47098cb2017-10-18 17:30:30 +0900421 (Runnable saveImportance, StatusBarNotification sbn) -> {
Julia Reynolds3aedded2017-03-31 14:42:09 -0400422 saveImportance.run();
Julia Reynolds437cdb12018-01-03 12:27:24 -0500423 }, null, null, null);
Geoffrey Pitsch5278d3d2017-03-29 13:39:10 -0400424
Julia Reynolds437cdb12018-01-03 12:27:24 -0500425 mNotificationInfo.findViewById(R.id.block).performClick();
426 waitForUndoButton();
Mady Mellorc2dbe492017-03-30 13:22:03 -0700427 mNotificationInfo.handleCloseControls(true, false);
Geoffrey Pitsch5278d3d2017-03-29 13:39:10 -0400428 verify(mMockINotificationManager, times(1)).updateNotificationChannelForPackage(
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400429 eq(TEST_PACKAGE_NAME), eq(TEST_UID), eq(mNotificationChannel));
Geoffrey Pitsch5278d3d2017-03-29 13:39:10 -0400430 }
Julia Reynolds3aedded2017-03-31 14:42:09 -0400431
432 @Test
433 public void testDisplaySettingsLink() throws Exception {
434 final CountDownLatch latch = new CountDownLatch(1);
435 final String settingsText = "work chats";
436 final ResolveInfo ri = new ResolveInfo();
437 ri.activityInfo = new ActivityInfo();
438 ri.activityInfo.packageName = TEST_PACKAGE_NAME;
439 ri.activityInfo.name = "something";
440 List<ResolveInfo> ris = new ArrayList<>();
441 ris.add(ri);
442 when(mMockPackageManager.queryIntentActivities(any(), anyInt())).thenReturn(ris);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500443 mNotificationChannel.setImportance(IMPORTANCE_LOW);
Julia Reynolds3aedded2017-03-31 14:42:09 -0400444 Notification n = new Notification.Builder(mContext, mNotificationChannel.getId())
445 .setSettingsText(settingsText).build();
446 StatusBarNotification sbn = new StatusBarNotification(TEST_PACKAGE_NAME, TEST_PACKAGE_NAME,
447 0, null, 0, 0, n, UserHandle.CURRENT, null, 0);
448
449 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds437cdb12018-01-03 12:27:24 -0500450 TEST_PACKAGE_NAME, mNotificationChannel, 1, sbn, null, null,
Julia Reynolds3aedded2017-03-31 14:42:09 -0400451 (View v, Intent intent) -> {
452 latch.countDown();
Julia Reynolds437cdb12018-01-03 12:27:24 -0500453 }, null);
Julia Reynolds3aedded2017-03-31 14:42:09 -0400454 final TextView settingsLink = mNotificationInfo.findViewById(R.id.app_settings);
455 assertEquals(View.VISIBLE, settingsLink.getVisibility());
Julia Reynolds3aedded2017-03-31 14:42:09 -0400456 settingsLink.performClick();
457 assertEquals(0, latch.getCount());
458 }
459
460 @Test
461 public void testDisplaySettingsLink_multipleChannels() throws Exception {
462 final CountDownLatch latch = new CountDownLatch(1);
463 final String settingsText = "work chats";
464 final ResolveInfo ri = new ResolveInfo();
465 ri.activityInfo = new ActivityInfo();
466 ri.activityInfo.packageName = TEST_PACKAGE_NAME;
467 ri.activityInfo.name = "something";
468 List<ResolveInfo> ris = new ArrayList<>();
469 ris.add(ri);
470 when(mMockPackageManager.queryIntentActivities(any(), anyInt())).thenReturn(ris);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500471 mNotificationChannel.setImportance(IMPORTANCE_LOW);
Julia Reynolds3aedded2017-03-31 14:42:09 -0400472 Notification n = new Notification.Builder(mContext, mNotificationChannel.getId())
473 .setSettingsText(settingsText).build();
474 StatusBarNotification sbn = new StatusBarNotification(TEST_PACKAGE_NAME, TEST_PACKAGE_NAME,
475 0, null, 0, 0, n, UserHandle.CURRENT, null, 0);
476
477 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds437cdb12018-01-03 12:27:24 -0500478 TEST_PACKAGE_NAME, mNotificationChannel, 2, sbn, null, null,
479 (View v, Intent intent) -> {
Julia Reynolds3aedded2017-03-31 14:42:09 -0400480 latch.countDown();
Julia Reynolds437cdb12018-01-03 12:27:24 -0500481 }, null);
Julia Reynolds3aedded2017-03-31 14:42:09 -0400482 final TextView settingsLink = mNotificationInfo.findViewById(R.id.app_settings);
483 assertEquals(View.VISIBLE, settingsLink.getVisibility());
484 settingsLink.performClick();
485 assertEquals(0, latch.getCount());
486 }
487
488 @Test
489 public void testNoSettingsLink_noHandlingActivity() throws Exception {
490 final String settingsText = "work chats";
491 when(mMockPackageManager.queryIntentActivities(any(), anyInt())).thenReturn(null);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500492 mNotificationChannel.setImportance(IMPORTANCE_LOW);
Julia Reynolds3aedded2017-03-31 14:42:09 -0400493 Notification n = new Notification.Builder(mContext, mNotificationChannel.getId())
494 .setSettingsText(settingsText).build();
495 StatusBarNotification sbn = new StatusBarNotification(TEST_PACKAGE_NAME, TEST_PACKAGE_NAME,
496 0, null, 0, 0, n, UserHandle.CURRENT, null, 0);
497
498 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds437cdb12018-01-03 12:27:24 -0500499 TEST_PACKAGE_NAME, mNotificationChannel, 2, sbn, null, null, null, null);
Julia Reynolds3aedded2017-03-31 14:42:09 -0400500 final TextView settingsLink = mNotificationInfo.findViewById(R.id.app_settings);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500501 assertEquals(GONE, settingsLink.getVisibility());
Julia Reynolds3aedded2017-03-31 14:42:09 -0400502 }
503
504 @Test
505 public void testNoSettingsLink_noLinkText() throws Exception {
506 final ResolveInfo ri = new ResolveInfo();
507 ri.activityInfo = new ActivityInfo();
508 ri.activityInfo.packageName = TEST_PACKAGE_NAME;
509 ri.activityInfo.name = "something";
510 List<ResolveInfo> ris = new ArrayList<>();
511 ris.add(ri);
512 when(mMockPackageManager.queryIntentActivities(any(), anyInt())).thenReturn(ris);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500513 mNotificationChannel.setImportance(IMPORTANCE_LOW);
Julia Reynolds3aedded2017-03-31 14:42:09 -0400514 Notification n = new Notification.Builder(mContext, mNotificationChannel.getId()).build();
515 StatusBarNotification sbn = new StatusBarNotification(TEST_PACKAGE_NAME, TEST_PACKAGE_NAME,
516 0, null, 0, 0, n, UserHandle.CURRENT, null, 0);
517
518 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds437cdb12018-01-03 12:27:24 -0500519 TEST_PACKAGE_NAME, mNotificationChannel, 1, sbn, null, null, null, null);
Julia Reynolds3aedded2017-03-31 14:42:09 -0400520 final TextView settingsLink = mNotificationInfo.findViewById(R.id.app_settings);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500521 assertEquals(GONE, settingsLink.getVisibility());
Julia Reynolds3aedded2017-03-31 14:42:09 -0400522 }
523
Geoffrey Pitsch8c8dbde2017-04-20 11:44:33 -0400524
525 @Test
526 public void testWillBeRemovedReturnsFalseBeforeBind() throws Exception {
527 assertFalse(mNotificationInfo.willBeRemoved());
528 }
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500529}