blob: 88fa659cd0e4da0b37f99b62ed853064a3672a9f [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 Reynoldsf7321592017-05-24 16:09:19 -040019import static android.print.PrintManager.PRINT_SPOOLER_PACKAGE_NAME;
20
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050021import static junit.framework.Assert.assertEquals;
22import static junit.framework.Assert.assertFalse;
Julia Reynolds3aedded2017-03-31 14:42:09 -040023import static junit.framework.Assert.assertNull;
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050024import static junit.framework.Assert.assertTrue;
Julia Reynolds5a311932017-03-01 16:33:44 -050025
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050026import static org.mockito.Mockito.any;
27import static org.mockito.Mockito.anyBoolean;
28import static org.mockito.Mockito.anyInt;
29import static org.mockito.Mockito.anyString;
30import static org.mockito.Mockito.eq;
31import static org.mockito.Mockito.mock;
32import static org.mockito.Mockito.never;
33import static org.mockito.Mockito.times;
34import static org.mockito.Mockito.verify;
35import static org.mockito.Mockito.when;
36
37import android.app.INotificationManager;
Julia Reynolds3aedded2017-03-31 14:42:09 -040038import android.app.Notification;
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050039import android.app.NotificationChannel;
40import android.app.NotificationChannelGroup;
41import android.app.NotificationManager;
Julia Reynolds3aedded2017-03-31 14:42:09 -040042import android.content.Intent;
43import android.content.pm.ActivityInfo;
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050044import android.content.pm.ApplicationInfo;
45import android.content.pm.PackageInfo;
46import android.content.pm.PackageManager;
Julia Reynolds3aedded2017-03-31 14:42:09 -040047import android.content.pm.ResolveInfo;
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050048import android.graphics.drawable.Drawable;
Julia Reynolds3aedded2017-03-31 14:42:09 -040049import android.os.UserHandle;
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050050import android.service.notification.StatusBarNotification;
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050051import android.test.suitebuilder.annotation.SmallTest;
Jason Monk340b0e52017-03-08 14:57:56 -050052import android.testing.AndroidTestingRunner;
53import android.testing.UiThreadTest;
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050054import android.view.LayoutInflater;
55import android.view.View;
56import android.widget.ImageView;
57import android.widget.Switch;
58import android.widget.TextView;
Jason Monk340b0e52017-03-08 14:57:56 -050059
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050060import com.android.systemui.R;
61import com.android.systemui.SysuiTestCase;
Jason Monkba364322017-03-06 11:19:20 -050062
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050063import org.junit.Before;
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050064import org.junit.Test;
Jason Monk340b0e52017-03-08 14:57:56 -050065import org.junit.runner.RunWith;
Julia Reynolds8ceb5792017-04-11 11:32:44 -040066import org.mockito.ArgumentCaptor;
Julia Reynolds3aedded2017-03-31 14:42:09 -040067
68import java.util.ArrayList;
Geoffrey Pitschd0856f02017-02-16 10:51:18 -050069import java.util.Arrays;
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050070import java.util.Collections;
Geoffrey Pitschd0856f02017-02-16 10:51:18 -050071import java.util.List;
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050072import java.util.concurrent.CountDownLatch;
73
74@SmallTest
Jason Monk340b0e52017-03-08 14:57:56 -050075@RunWith(AndroidTestingRunner.class)
Jason Monkba364322017-03-06 11:19:20 -050076@UiThreadTest
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050077public class NotificationInfoTest extends SysuiTestCase {
78 private static final String TEST_PACKAGE_NAME = "test_package";
Julia Reynoldsf7321592017-05-24 16:09:19 -040079 private static final String TEST_SYSTEM_PACKAGE_NAME = PRINT_SPOOLER_PACKAGE_NAME;
Geoffrey Pitschd034d292017-05-12 11:59:20 -040080 private static final int TEST_UID = 1;
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050081 private static final String TEST_CHANNEL = "test_channel";
82 private static final String TEST_CHANNEL_NAME = "TEST CHANNEL NAME";
83
84 private NotificationInfo mNotificationInfo;
85 private final INotificationManager mMockINotificationManager = mock(INotificationManager.class);
86 private final PackageManager mMockPackageManager = mock(PackageManager.class);
87 private NotificationChannel mNotificationChannel;
Geoffrey Pitschd0856f02017-02-16 10:51:18 -050088 private NotificationChannel mDefaultNotificationChannel;
Julia Reynolds3aedded2017-03-31 14:42:09 -040089 private StatusBarNotification mSbn;
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050090
91 @Before
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050092 public void setUp() throws Exception {
93 // Inflate the layout
Geoffrey Pitschd0856f02017-02-16 10:51:18 -050094 final LayoutInflater layoutInflater = LayoutInflater.from(mContext);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050095 mNotificationInfo = (NotificationInfo) layoutInflater.inflate(R.layout.notification_info,
96 null);
97
98 // PackageManager must return a packageInfo and applicationInfo.
99 final PackageInfo packageInfo = new PackageInfo();
100 packageInfo.packageName = TEST_PACKAGE_NAME;
Julia Reynoldsf7321592017-05-24 16:09:19 -0400101 when(mMockPackageManager.getPackageInfo(eq(TEST_PACKAGE_NAME), anyInt()))
102 .thenReturn(packageInfo);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500103 final ApplicationInfo applicationInfo = new ApplicationInfo();
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400104 applicationInfo.uid = TEST_UID; // non-zero
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500105 when(mMockPackageManager.getApplicationInfo(anyString(), anyInt())).thenReturn(
106 applicationInfo);
Julia Reynoldsf7321592017-05-24 16:09:19 -0400107 final PackageInfo systemPackageInfo = new PackageInfo();
108 systemPackageInfo.packageName = TEST_SYSTEM_PACKAGE_NAME;
109 when(mMockPackageManager.getPackageInfo(eq(TEST_SYSTEM_PACKAGE_NAME), anyInt()))
110 .thenReturn(systemPackageInfo);
111 when(mMockPackageManager.getPackageInfo(eq("android"), anyInt()))
112 .thenReturn(packageInfo);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500113
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500114 // Package has one channel by default.
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500115 when(mMockINotificationManager.getNumNotificationChannelsForPackage(
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400116 eq(TEST_PACKAGE_NAME), eq(TEST_UID), anyBoolean())).thenReturn(1);
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500117
118 // Some test channels.
119 mNotificationChannel = new NotificationChannel(
120 TEST_CHANNEL, TEST_CHANNEL_NAME, NotificationManager.IMPORTANCE_LOW);
121 mDefaultNotificationChannel = new NotificationChannel(
122 NotificationChannel.DEFAULT_CHANNEL_ID, TEST_CHANNEL_NAME,
123 NotificationManager.IMPORTANCE_LOW);
Julia Reynolds33bef2c2017-09-05 11:07:18 -0400124 mSbn = new StatusBarNotification(TEST_PACKAGE_NAME, TEST_PACKAGE_NAME, 0, null, TEST_UID, 0,
Julia Reynolds3aedded2017-03-31 14:42:09 -0400125 new Notification(), UserHandle.CURRENT, null, 0);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500126 }
127
128 private CharSequence getStringById(int resId) {
129 return mContext.getString(resId);
130 }
131
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500132 private CharSequence getNumChannelsDescString(int numChannels) {
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500133 return String.format(
134 mContext.getResources().getQuantityString(
135 R.plurals.notification_num_channels_desc, numChannels),
136 numChannels);
137 }
138
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500139 private CharSequence getChannelsListDescString(NotificationChannel... channels) {
140 if (channels.length == 2) {
141 return mContext.getString(R.string.notification_channels_list_desc_2,
142 channels[0].getName(), channels[1].getName());
143 } else {
144 final int numOthers = channels.length - 2;
145 return String.format(
146 mContext.getResources().getQuantityString(
147 R.plurals.notification_channels_list_desc_2_and_others, numOthers),
148 channels[0].getName(), channels[1].getName(), numOthers);
149 }
150 }
151
152 private CharSequence getNumChannelsString(int numChannels) {
153 return mContext.getString(R.string.notification_num_channels, numChannels);
154 }
155
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500156 @Test
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500157 public void testBindNotification_SetsTextApplicationName() throws Exception {
158 when(mMockPackageManager.getApplicationLabel(any())).thenReturn("App Name");
159 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds3aedded2017-03-31 14:42:09 -0400160 TEST_PACKAGE_NAME, Arrays.asList(mNotificationChannel),
161 mNotificationChannel.getImportance(), mSbn, null, null, null,
162 null, null);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500163 final TextView textView = (TextView) mNotificationInfo.findViewById(R.id.pkgname);
164 assertTrue(textView.getText().toString().contains("App Name"));
165 }
166
167 @Test
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500168 public void testBindNotification_SetsPackageIcon() throws Exception {
169 final Drawable iconDrawable = mock(Drawable.class);
170 when(mMockPackageManager.getApplicationIcon(any(ApplicationInfo.class)))
171 .thenReturn(iconDrawable);
172 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds3aedded2017-03-31 14:42:09 -0400173 TEST_PACKAGE_NAME, Arrays.asList(mNotificationChannel),
174 mNotificationChannel.getImportance(), mSbn, null, null, null,
175 null, null);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500176 final ImageView iconView = (ImageView) mNotificationInfo.findViewById(R.id.pkgicon);
177 assertEquals(iconDrawable, iconView.getDrawable());
178 }
179
180 @Test
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500181 public void testBindNotification_GroupNameHiddenIfNoGroup() throws Exception {
182 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds3aedded2017-03-31 14:42:09 -0400183 TEST_PACKAGE_NAME, Arrays.asList(mNotificationChannel),
184 mNotificationChannel.getImportance(), mSbn, null, null, null,
185 null, null);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500186 final TextView groupNameView = (TextView) mNotificationInfo.findViewById(R.id.group_name);
187 assertEquals(View.GONE, groupNameView.getVisibility());
188 final TextView groupDividerView =
189 (TextView) mNotificationInfo.findViewById(R.id.pkg_group_divider);
190 assertEquals(View.GONE, groupDividerView.getVisibility());
191 }
192
193 @Test
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500194 public void testBindNotification_SetsGroupNameIfNonNull() throws Exception {
195 mNotificationChannel.setGroup("test_group_id");
196 final NotificationChannelGroup notificationChannelGroup =
197 new NotificationChannelGroup("test_group_id", "Test Group Name");
198 when(mMockINotificationManager.getNotificationChannelGroupForPackage(
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400199 eq("test_group_id"), eq(TEST_PACKAGE_NAME), eq(TEST_UID)))
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500200 .thenReturn(notificationChannelGroup);
201 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds3aedded2017-03-31 14:42:09 -0400202 TEST_PACKAGE_NAME, Arrays.asList(mNotificationChannel),
203 mNotificationChannel.getImportance(), mSbn, null, null, null,
204 null, null);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500205 final TextView groupNameView = (TextView) mNotificationInfo.findViewById(R.id.group_name);
206 assertEquals(View.VISIBLE, groupNameView.getVisibility());
207 assertEquals("Test Group Name", groupNameView.getText());
208 final TextView groupDividerView =
209 (TextView) mNotificationInfo.findViewById(R.id.pkg_group_divider);
210 assertEquals(View.VISIBLE, groupDividerView.getVisibility());
211 }
212
213 @Test
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500214 public void testBindNotification_SetsTextChannelName() throws Exception {
215 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds3aedded2017-03-31 14:42:09 -0400216 TEST_PACKAGE_NAME, Arrays.asList(mNotificationChannel),
217 mNotificationChannel.getImportance(), mSbn, null, null, null,
218 null, null);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500219 final TextView textView = (TextView) mNotificationInfo.findViewById(R.id.channel_name);
220 assertEquals(TEST_CHANNEL_NAME, textView.getText());
221 }
222
223 @Test
Geoffrey Pitsch1fbf66e2017-05-09 11:32:27 -0400224 public void testBindNotification_DefaultChannelDoesNotUseChannelName() throws Exception {
225 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
226 TEST_PACKAGE_NAME, Arrays.asList(mDefaultNotificationChannel),
227 mNotificationChannel.getImportance(), mSbn, null, null, null,
228 null, null);
229 final TextView textView = (TextView) mNotificationInfo.findViewById(R.id.channel_name);
230 assertEquals(mContext.getString(R.string.notification_header_default_channel),
231 textView.getText());
232 }
233
234 @Test
Geoffrey Pitschb43486a2017-06-01 13:45:59 -0400235 public void testBindNotification_UnblockablePackageDoesNotUseChannelName() throws Exception {
236 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
237 TEST_PACKAGE_NAME, Arrays.asList(mNotificationChannel),
238 mNotificationChannel.getImportance(), mSbn, null, null, null,
239 null, Collections.singleton(TEST_PACKAGE_NAME));
240 final TextView textView = (TextView) mNotificationInfo.findViewById(R.id.channel_name);
241 assertEquals(mContext.getString(R.string.notification_header_default_channel),
242 textView.getText());
243 }
244
245 @Test
Geoffrey Pitsch1fbf66e2017-05-09 11:32:27 -0400246 public void testBindNotification_DefaultChannelUsesNameWhenMoreThanOneChannelExists()
247 throws Exception {
248 when(mMockINotificationManager.getNumNotificationChannelsForPackage(
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400249 eq(TEST_PACKAGE_NAME), eq(TEST_UID), anyBoolean())).thenReturn(2);
Geoffrey Pitsch1fbf66e2017-05-09 11:32:27 -0400250 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
251 TEST_PACKAGE_NAME, Arrays.asList(mDefaultNotificationChannel),
252 mNotificationChannel.getImportance(), mSbn, null, null, null,
253 null, null);
254 final TextView textView = (TextView) mNotificationInfo.findViewById(R.id.channel_name);
255 assertEquals(mDefaultNotificationChannel.getName(), textView.getText());
256 }
257
258 @Test
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500259 public void testBindNotification_SetsOnClickListenerForSettings() throws Exception {
260 final CountDownLatch latch = new CountDownLatch(1);
261 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500262 TEST_PACKAGE_NAME, Arrays.asList(mNotificationChannel),
Julia Reynolds3aedded2017-03-31 14:42:09 -0400263 mNotificationChannel.getImportance(), mSbn,
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500264 (View v, NotificationChannel c, int appUid) -> {
Julia Reynolds3aedded2017-03-31 14:42:09 -0400265 assertEquals(mNotificationChannel, c);
266 latch.countDown();
267 }, null, null, null, null);
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500268
269 final TextView settingsButton =
270 (TextView) mNotificationInfo.findViewById(R.id.more_settings);
271 settingsButton.performClick();
272 // Verify that listener was triggered.
273 assertEquals(0, latch.getCount());
274 }
275
276 @Test
Geoffrey Pitsch029a3fa2017-03-30 13:01:57 -0400277 public void testBindNotification_SettingsButtonInvisibleWhenNoClickListener() throws Exception {
278 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
279 TEST_PACKAGE_NAME, Arrays.asList(mNotificationChannel),
Julia Reynolds3aedded2017-03-31 14:42:09 -0400280 mNotificationChannel.getImportance(), mSbn, null, null, null, null, null);
Geoffrey Pitsch029a3fa2017-03-30 13:01:57 -0400281 final TextView settingsButton =
282 (TextView) mNotificationInfo.findViewById(R.id.more_settings);
283 assertTrue(settingsButton.getVisibility() != View.VISIBLE);
284 }
285
286 @Test
287 public void testBindNotification_SettingsButtonReappersAfterSecondBind() throws Exception {
288 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
289 TEST_PACKAGE_NAME, Arrays.asList(mNotificationChannel),
Julia Reynolds4740b612017-04-06 12:31:56 -0400290 mNotificationChannel.getImportance(), mSbn, null, null, null, null, null);
Geoffrey Pitsch029a3fa2017-03-30 13:01:57 -0400291 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
292 TEST_PACKAGE_NAME, Arrays.asList(mNotificationChannel),
Julia Reynolds3aedded2017-03-31 14:42:09 -0400293 mNotificationChannel.getImportance(), mSbn,
294 (View v, NotificationChannel c, int appUid) -> {}, null, null, null, null);
Geoffrey Pitsch029a3fa2017-03-30 13:01:57 -0400295 final TextView settingsButton =
296 (TextView) mNotificationInfo.findViewById(R.id.more_settings);
297 assertEquals(View.VISIBLE, settingsButton.getVisibility());
298 }
299
300 @Test
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500301 public void testOnClickListenerPassesNullChannelForBundle() throws Exception {
302 final CountDownLatch latch = new CountDownLatch(1);
303 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
304 TEST_PACKAGE_NAME,
305 Arrays.asList(mNotificationChannel, mDefaultNotificationChannel),
Julia Reynolds3aedded2017-03-31 14:42:09 -0400306 mNotificationChannel.getImportance(), mSbn,
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500307 (View v, NotificationChannel c, int appUid) -> {
Julia Reynolds3aedded2017-03-31 14:42:09 -0400308 assertEquals(null, c);
309 latch.countDown();
310 }, null, null, null, null);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500311
312 final TextView settingsButton =
313 (TextView) mNotificationInfo.findViewById(R.id.more_settings);
314 settingsButton.performClick();
315 // Verify that listener was triggered.
316 assertEquals(0, latch.getCount());
317 }
318
319 @Test
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500320 public void testBindNotification_SettingsTextWithOneChannel() throws Exception {
321 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500322 TEST_PACKAGE_NAME, Arrays.asList(mNotificationChannel),
Julia Reynolds3aedded2017-03-31 14:42:09 -0400323 mNotificationChannel.getImportance(), mSbn,
324 (View v, NotificationChannel c, int appUid) -> {
325 }, null, null, null, null);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500326 final TextView settingsButton =
327 (TextView) mNotificationInfo.findViewById(R.id.more_settings);
328 assertEquals(getStringById(R.string.notification_more_settings), settingsButton.getText());
329 }
330
331 @Test
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500332 public void testBindNotification_SettingsTextWithMultipleChannels() throws Exception {
333 when(mMockINotificationManager.getNumNotificationChannelsForPackage(
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400334 eq(TEST_PACKAGE_NAME), eq(TEST_UID), anyBoolean())).thenReturn(2);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500335 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500336 TEST_PACKAGE_NAME, Arrays.asList(mNotificationChannel),
Julia Reynolds3aedded2017-03-31 14:42:09 -0400337 mNotificationChannel.getImportance(), mSbn,
338 (View v, NotificationChannel c, int appUid) -> {
339 }, null, null, null, null);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500340 final TextView settingsButton =
341 (TextView) mNotificationInfo.findViewById(R.id.more_settings);
342 assertEquals(getStringById(R.string.notification_all_categories), settingsButton.getText());
343 }
344
345 @Test
Geoffrey Pitschb43486a2017-06-01 13:45:59 -0400346 public void testBindNotification_SettingsTextWithMultipleChannelsForUnblockableApp()
347 throws Exception {
348 when(mMockINotificationManager.getNumNotificationChannelsForPackage(
349 eq(TEST_PACKAGE_NAME), eq(TEST_UID), anyBoolean())).thenReturn(2);
350 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
351 TEST_PACKAGE_NAME, Arrays.asList(mNotificationChannel),
352 mNotificationChannel.getImportance(), mSbn,
353 (View v, NotificationChannel c, int appUid) -> {
354 }, null, null, null, Collections.singleton(TEST_PACKAGE_NAME));
355 final TextView settingsButton =
356 (TextView) mNotificationInfo.findViewById(R.id.more_settings);
357 assertEquals(getStringById(R.string.notification_more_settings), settingsButton.getText());
358 }
359
360 @Test
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500361 public void testBindNotification_SetsOnClickListenerForDone() throws Exception {
362 final CountDownLatch latch = new CountDownLatch(1);
363 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds3aedded2017-03-31 14:42:09 -0400364 TEST_PACKAGE_NAME, Arrays.asList(mNotificationChannel),
365 mNotificationChannel.getImportance(), mSbn, null,
366 null, (View v) -> {
367 latch.countDown();
368 },
Geoffrey Pitsch5278d3d2017-03-29 13:39:10 -0400369 null, null);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500370
371 final TextView doneButton = (TextView) mNotificationInfo.findViewById(R.id.done);
372 doneButton.performClick();
373 // Verify that listener was triggered.
374 assertEquals(0, latch.getCount());
375 }
376
377 @Test
Geoffrey Pitsch0a8f0552017-04-20 13:56:40 -0400378 public void testBindNotification_NumChannelsTextHiddenWhenDefaultChannel() throws Exception {
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500379 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Geoffrey Pitsch5278d3d2017-03-29 13:39:10 -0400380 TEST_PACKAGE_NAME, Arrays.asList(mDefaultNotificationChannel),
Julia Reynolds3aedded2017-03-31 14:42:09 -0400381 mNotificationChannel.getImportance(), mSbn, null, null,
382 null, null, null);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500383 final TextView numChannelsView =
384 (TextView) mNotificationInfo.findViewById(R.id.num_channels_desc);
Geoffrey Pitsch0a8f0552017-04-20 13:56:40 -0400385 assertEquals(View.INVISIBLE, numChannelsView.getVisibility());
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500386 }
387
388 @Test
Geoffrey Pitsch1fbf66e2017-05-09 11:32:27 -0400389 public void testBindNotification_NumChannelsTextDisplaysWhenMoreThanOneChannelExists()
390 throws Exception {
391 when(mMockINotificationManager.getNumNotificationChannelsForPackage(
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400392 eq(TEST_PACKAGE_NAME), eq(TEST_UID), anyBoolean())).thenReturn(2);
Geoffrey Pitsch1fbf66e2017-05-09 11:32:27 -0400393 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
394 TEST_PACKAGE_NAME, Arrays.asList(mDefaultNotificationChannel),
395 mNotificationChannel.getImportance(), mSbn, null, null,
396 null, null, null);
397 final TextView numChannelsView =
398 (TextView) mNotificationInfo.findViewById(R.id.num_channels_desc);
399 assertEquals(numChannelsView.getVisibility(), View.VISIBLE);
400 assertEquals(getNumChannelsDescString(2), numChannelsView.getText());
401 }
402
403 @Test
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500404 public void testBindNotification_NumChannelsTextDisplaysWhenNotDefaultChannel()
405 throws Exception {
406 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds3aedded2017-03-31 14:42:09 -0400407 TEST_PACKAGE_NAME, Arrays.asList(mNotificationChannel),
408 mNotificationChannel.getImportance(), mSbn, null, null, null,
409 null, null);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500410 final TextView numChannelsView =
411 (TextView) mNotificationInfo.findViewById(R.id.num_channels_desc);
412 assertEquals(numChannelsView.getVisibility(), View.VISIBLE);
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500413 assertEquals(getNumChannelsDescString(1), numChannelsView.getText());
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500414 }
415
416 @Test
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500417 public void testBindNotification_NumChannelsTextScalesWithNumberOfChannels()
418 throws Exception {
419 when(mMockINotificationManager.getNumNotificationChannelsForPackage(
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400420 eq(TEST_PACKAGE_NAME), eq(TEST_UID), anyBoolean())).thenReturn(2);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500421 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds3aedded2017-03-31 14:42:09 -0400422 TEST_PACKAGE_NAME, Arrays.asList(mNotificationChannel),
423 mNotificationChannel.getImportance(), mSbn, null, null, null,
424 null, null);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500425 final TextView numChannelsView =
426 (TextView) mNotificationInfo.findViewById(R.id.num_channels_desc);
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500427 assertEquals(getNumChannelsDescString(2), numChannelsView.getText());
428 }
429
430 @Test
431 @UiThreadTest
432 public void testBindNotification_NumChannelsTextListsChannelsWhenTwoInBundle()
433 throws Exception {
434 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
435 TEST_PACKAGE_NAME, Arrays.asList(mNotificationChannel, mDefaultNotificationChannel),
Julia Reynolds3aedded2017-03-31 14:42:09 -0400436 mNotificationChannel.getImportance(), mSbn, null, null, null, null, null);
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500437 final TextView numChannelsView =
438 (TextView) mNotificationInfo.findViewById(R.id.num_channels_desc);
439 assertEquals(getChannelsListDescString(mNotificationChannel, mDefaultNotificationChannel),
440 numChannelsView.getText());
441 }
442
443 @Test
444 @UiThreadTest
445 public void testBindNotification_NumChannelsTextListsChannelsWhenThreeInBundle()
446 throws Exception {
447 NotificationChannel thirdChannel = new NotificationChannel(
448 "third_channel", "third_channel", NotificationManager.IMPORTANCE_LOW);
449 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
450 TEST_PACKAGE_NAME,
451 Arrays.asList(mNotificationChannel, mDefaultNotificationChannel, thirdChannel),
Julia Reynolds3aedded2017-03-31 14:42:09 -0400452 mNotificationChannel.getImportance(), mSbn, null, null, null, null, null);
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500453 final TextView numChannelsView =
454 (TextView) mNotificationInfo.findViewById(R.id.num_channels_desc);
455 assertEquals(
456 getChannelsListDescString(mNotificationChannel, mDefaultNotificationChannel,
457 thirdChannel),
458 numChannelsView.getText());
459 }
460
461 @Test
462 @UiThreadTest
463 public void testBindNotification_NumChannelsTextListsChannelsWhenFourInBundle()
464 throws Exception {
465 NotificationChannel thirdChannel = new NotificationChannel(
466 "third_channel", "third_channel", NotificationManager.IMPORTANCE_LOW);
467 NotificationChannel fourthChannel = new NotificationChannel(
468 "fourth_channel", "fourth_channel", NotificationManager.IMPORTANCE_LOW);
469 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
470 TEST_PACKAGE_NAME,
471 Arrays.asList(mNotificationChannel, mDefaultNotificationChannel, thirdChannel,
Julia Reynolds3aedded2017-03-31 14:42:09 -0400472 fourthChannel), mNotificationChannel.getImportance(), mSbn, null, null,
473 null, null, null);
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500474 final TextView numChannelsView =
475 (TextView) mNotificationInfo.findViewById(R.id.num_channels_desc);
476 assertEquals(
477 getChannelsListDescString(mNotificationChannel, mDefaultNotificationChannel,
478 thirdChannel, fourthChannel),
479 numChannelsView.getText());
480 }
481
482 @Test
483 @UiThreadTest
484 public void testBindNotification_ChannelNameChangesWhenBundleFromDifferentChannels()
485 throws Exception {
486 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
487 TEST_PACKAGE_NAME, Arrays.asList(mNotificationChannel, mDefaultNotificationChannel),
Julia Reynolds3aedded2017-03-31 14:42:09 -0400488 mNotificationChannel.getImportance(), mSbn, null, null, null, null, null);
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500489 final TextView channelNameView =
490 (TextView) mNotificationInfo.findViewById(R.id.channel_name);
491 assertEquals(getNumChannelsString(2), channelNameView.getText());
492 }
493
494 @Test
495 @UiThreadTest
496 public void testEnabledSwitchInvisibleIfBundleFromDifferentChannels() throws Exception {
497 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
498 TEST_PACKAGE_NAME, Arrays.asList(mNotificationChannel, mDefaultNotificationChannel),
Julia Reynolds3aedded2017-03-31 14:42:09 -0400499 mNotificationChannel.getImportance(), mSbn, null, null, null, null, null);
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500500 Switch enabledSwitch = (Switch) mNotificationInfo.findViewById(R.id.channel_enabled_switch);
501 assertEquals(View.INVISIBLE, enabledSwitch.getVisibility());
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500502 }
503
504 @Test
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500505 public void testbindNotification_ChannelDisabledTextGoneWhenNotDisabled() throws Exception {
506 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds3aedded2017-03-31 14:42:09 -0400507 TEST_PACKAGE_NAME, Arrays.asList(mNotificationChannel),
508 mNotificationChannel.getImportance(), mSbn, null, null, null, null, null);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500509 final TextView channelDisabledView =
510 (TextView) mNotificationInfo.findViewById(R.id.channel_disabled);
511 assertEquals(channelDisabledView.getVisibility(), View.GONE);
512 }
513
514 @Test
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500515 public void testbindNotification_ChannelDisabledTextVisibleWhenDisabled() throws Exception {
516 mNotificationChannel.setImportance(NotificationManager.IMPORTANCE_NONE);
517 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds3aedded2017-03-31 14:42:09 -0400518 TEST_PACKAGE_NAME, Arrays.asList(mNotificationChannel),
519 mNotificationChannel.getImportance(), mSbn, null, null, null,
520 null, null);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500521 final TextView channelDisabledView =
522 (TextView) mNotificationInfo.findViewById(R.id.channel_disabled);
523 assertEquals(channelDisabledView.getVisibility(), View.VISIBLE);
524 // Replaces the numChannelsView
525 final TextView numChannelsView =
526 (TextView) mNotificationInfo.findViewById(R.id.num_channels_desc);
527 assertEquals(numChannelsView.getVisibility(), View.GONE);
528 }
529
530 @Test
Geoffrey Pitschb43486a2017-06-01 13:45:59 -0400531 public void testbindNotification_UnblockableTextVisibleWhenAppUnblockable() throws Exception {
532 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
533 TEST_PACKAGE_NAME, Arrays.asList(mNotificationChannel),
534 mNotificationChannel.getImportance(), mSbn, null, null, null,
535 null, Collections.singleton(TEST_PACKAGE_NAME));
536 final TextView numChannelsView =
537 (TextView) mNotificationInfo.findViewById(R.id.num_channels_desc);
538 assertEquals(View.VISIBLE, numChannelsView.getVisibility());
539 assertEquals(mContext.getString(R.string.notification_unblockable_desc),
540 numChannelsView.getText());
541 }
542
543 @Test
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500544 @UiThreadTest
Geoffrey Pitschee8c81e2017-03-23 11:38:56 -0400545 public void testBindNotification_ChannelDisabledTextShowsForDefaultChannel()
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500546 throws Exception {
Geoffrey Pitschee8c81e2017-03-23 11:38:56 -0400547 mDefaultNotificationChannel.setImportance(NotificationManager.IMPORTANCE_NONE);
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500548 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Geoffrey Pitsch5278d3d2017-03-29 13:39:10 -0400549 TEST_PACKAGE_NAME, Arrays.asList(mDefaultNotificationChannel),
Julia Reynolds3aedded2017-03-31 14:42:09 -0400550 mDefaultNotificationChannel.getImportance(), mSbn, null, null,
551 null, null, null);
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500552 final TextView channelDisabledView =
553 (TextView) mNotificationInfo.findViewById(R.id.channel_disabled);
Geoffrey Pitschee8c81e2017-03-23 11:38:56 -0400554 assertEquals(View.VISIBLE, channelDisabledView.getVisibility());
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500555 }
556
557 @Test
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500558 public void testBindNotification_DoesNotUpdateNotificationChannel() throws Exception {
559 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds3aedded2017-03-31 14:42:09 -0400560 TEST_PACKAGE_NAME, Arrays.asList(mNotificationChannel),
561 mNotificationChannel.getImportance(), mSbn, null, null, null,
562 null, null);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500563 verify(mMockINotificationManager, never()).updateNotificationChannelForPackage(
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400564 anyString(), eq(TEST_UID), any());
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500565 }
566
567 @Test
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500568 public void testDoesNotUpdateNotificationChannelAfterImportanceChanged() throws Exception {
569 mNotificationChannel.setImportance(NotificationManager.IMPORTANCE_LOW);
570 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds3aedded2017-03-31 14:42:09 -0400571 TEST_PACKAGE_NAME, Arrays.asList(mNotificationChannel),
572 mNotificationChannel.getImportance(), mSbn, null, null, null,
573 null, null);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500574
575 Switch enabledSwitch = (Switch) mNotificationInfo.findViewById(R.id.channel_enabled_switch);
576 enabledSwitch.setChecked(false);
577 verify(mMockINotificationManager, never()).updateNotificationChannelForPackage(
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400578 anyString(), eq(TEST_UID), any());
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500579 }
580
581 @Test
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500582 public void testHandleCloseControls_DoesNotUpdateNotificationChannelIfUnchanged()
583 throws Exception {
584 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds3aedded2017-03-31 14:42:09 -0400585 TEST_PACKAGE_NAME, Arrays.asList(mNotificationChannel),
586 mNotificationChannel.getImportance(), mSbn, null, null, null,
587 null, null);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500588
Mady Mellorc2dbe492017-03-30 13:22:03 -0700589 mNotificationInfo.handleCloseControls(true, false);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500590 verify(mMockINotificationManager, never()).updateNotificationChannelForPackage(
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400591 anyString(), eq(TEST_UID), any());
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500592 }
593
594 @Test
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500595 public void testHandleCloseControls_DoesNotUpdateNotificationChannelIfUnspecified()
596 throws Exception {
597 mNotificationChannel.setImportance(NotificationManager.IMPORTANCE_UNSPECIFIED);
598 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds3aedded2017-03-31 14:42:09 -0400599 TEST_PACKAGE_NAME, Arrays.asList(mNotificationChannel),
600 mNotificationChannel.getImportance(), mSbn, null, null, null,
601 null, null);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500602
Mady Mellorc2dbe492017-03-30 13:22:03 -0700603 mNotificationInfo.handleCloseControls(true, false);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500604 verify(mMockINotificationManager, never()).updateNotificationChannelForPackage(
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400605 anyString(), eq(TEST_UID), any());
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500606 }
607
608 @Test
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500609 public void testEnabledSwitchOnByDefault() throws Exception {
610 mNotificationChannel.setImportance(NotificationManager.IMPORTANCE_LOW);
611 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds3aedded2017-03-31 14:42:09 -0400612 TEST_PACKAGE_NAME, Arrays.asList(mNotificationChannel),
613 mNotificationChannel.getImportance(), mSbn, null, null, null,
614 null, null);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500615
616 Switch enabledSwitch = (Switch) mNotificationInfo.findViewById(R.id.channel_enabled_switch);
617 assertTrue(enabledSwitch.isChecked());
618 }
619
620 @Test
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500621 public void testEnabledButtonOffWhenAlreadyBanned() throws Exception {
622 mNotificationChannel.setImportance(NotificationManager.IMPORTANCE_NONE);
623 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds3aedded2017-03-31 14:42:09 -0400624 TEST_PACKAGE_NAME, Arrays.asList(mNotificationChannel),
625 mNotificationChannel.getImportance(), mSbn, null, null, null,
626 null, null);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500627
628 Switch enabledSwitch = (Switch) mNotificationInfo.findViewById(R.id.channel_enabled_switch);
629 assertFalse(enabledSwitch.isChecked());
630 }
631
632 @Test
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500633 public void testEnabledSwitchVisibleByDefault() throws Exception {
634 mNotificationChannel.setImportance(NotificationManager.IMPORTANCE_LOW);
635 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds3aedded2017-03-31 14:42:09 -0400636 TEST_PACKAGE_NAME, Arrays.asList(mNotificationChannel),
637 mNotificationChannel.getImportance(), mSbn, null, null, null,
638 null, null);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500639
640 Switch enabledSwitch = (Switch) mNotificationInfo.findViewById(R.id.channel_enabled_switch);
641 assertEquals(View.VISIBLE, enabledSwitch.getVisibility());
642 }
643
644 @Test
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500645 public void testEnabledSwitchInvisibleIfNonBlockable() throws Exception {
646 mNotificationChannel.setImportance(NotificationManager.IMPORTANCE_LOW);
647 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds3aedded2017-03-31 14:42:09 -0400648 TEST_PACKAGE_NAME, Arrays.asList(mNotificationChannel),
649 mNotificationChannel.getImportance(), mSbn, null, null, null,
650 null, Collections.singleton(TEST_PACKAGE_NAME));
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500651
652 Switch enabledSwitch = (Switch) mNotificationInfo.findViewById(R.id.channel_enabled_switch);
653 assertEquals(View.INVISIBLE, enabledSwitch.getVisibility());
654 }
655
656 @Test
Julia Reynoldsf7321592017-05-24 16:09:19 -0400657 public void testEnabledSwitchInvisibleIfNonBlockableSystemChannel() throws Exception {
658 mNotificationChannel.setImportance(NotificationManager.IMPORTANCE_LOW);
659 mNotificationChannel.setBlockableSystem(false);
660 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
661 TEST_SYSTEM_PACKAGE_NAME, Arrays.asList(mNotificationChannel),
662 mNotificationChannel.getImportance(), mSbn, null, null, null,
663 null, null);
664
665 Switch enabledSwitch = (Switch) mNotificationInfo.findViewById(R.id.channel_enabled_switch);
666 assertEquals(View.INVISIBLE, enabledSwitch.getVisibility());
667 }
668
669 @Test
670 public void testEnabledSwitchVisibleIfBlockableSystemChannel() throws Exception {
671 mNotificationChannel.setImportance(NotificationManager.IMPORTANCE_LOW);
672 mNotificationChannel.setBlockableSystem(true);
673 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
674 TEST_SYSTEM_PACKAGE_NAME, Arrays.asList(mNotificationChannel),
675 mNotificationChannel.getImportance(), mSbn, null, null, null,
676 null, null);
677
678 Switch enabledSwitch = (Switch) mNotificationInfo.findViewById(R.id.channel_enabled_switch);
679 assertEquals(View.VISIBLE, enabledSwitch.getVisibility());
680 }
681
682 @Test
683 public void testEnabledSwitchInvisibleIfMultiChannelSummary() throws Exception {
684 mNotificationChannel.setImportance(NotificationManager.IMPORTANCE_LOW);
685 mNotificationChannel.setBlockableSystem(true);
686 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
687 TEST_PACKAGE_NAME, Arrays.asList(mNotificationChannel, mDefaultNotificationChannel),
688 mNotificationChannel.getImportance(), mSbn, null, null, null,
689 null, Collections.singleton(TEST_PACKAGE_NAME));
690
691 Switch enabledSwitch = (Switch) mNotificationInfo.findViewById(R.id.channel_enabled_switch);
692 assertEquals(View.INVISIBLE, enabledSwitch.getVisibility());
693 }
694
695 @Test
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500696 public void testNonBlockableAppDoesNotBecomeBlocked() throws Exception {
697 mNotificationChannel.setImportance(NotificationManager.IMPORTANCE_LOW);
698 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds3aedded2017-03-31 14:42:09 -0400699 TEST_PACKAGE_NAME, Arrays.asList(mNotificationChannel),
700 mNotificationChannel.getImportance(), mSbn, null, null, null,
701 null, Collections.singleton(TEST_PACKAGE_NAME));
Mady Mellorc2dbe492017-03-30 13:22:03 -0700702 mNotificationInfo.handleCloseControls(true, false);
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500703 verify(mMockINotificationManager, never()).updateNotificationChannelForPackage(
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400704 anyString(), eq(TEST_UID), any());
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500705 }
706
707 @Test
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500708 public void testEnabledSwitchChangedCallsUpdateNotificationChannel() throws Exception {
709 mNotificationChannel.setImportance(NotificationManager.IMPORTANCE_LOW);
710 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds3aedded2017-03-31 14:42:09 -0400711 TEST_PACKAGE_NAME, Arrays.asList(mNotificationChannel),
712 mNotificationChannel.getImportance(), mSbn, null, null, null,
713 null, Collections.singleton(TEST_PACKAGE_NAME));
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500714
Julia Reynolds8ceb5792017-04-11 11:32:44 -0400715 Switch enabledSwitch = mNotificationInfo.findViewById(R.id.channel_enabled_switch);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500716 enabledSwitch.setChecked(false);
Mady Mellorc2dbe492017-03-30 13:22:03 -0700717 mNotificationInfo.handleCloseControls(true, false);
Julia Reynolds8ceb5792017-04-11 11:32:44 -0400718
719 ArgumentCaptor<NotificationChannel> updated =
720 ArgumentCaptor.forClass(NotificationChannel.class);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500721 verify(mMockINotificationManager, times(1)).updateNotificationChannelForPackage(
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400722 anyString(), eq(TEST_UID), updated.capture());
Julia Reynolds8ceb5792017-04-11 11:32:44 -0400723 assertTrue((updated.getValue().getUserLockedFields()
724 & NotificationChannel.USER_LOCKED_IMPORTANCE) != 0);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500725 }
726
727 @Test
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500728 public void testCloseControlsDoesNotUpdateIfSaveIsFalse() throws Exception {
729 mNotificationChannel.setImportance(NotificationManager.IMPORTANCE_LOW);
730 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds3aedded2017-03-31 14:42:09 -0400731 TEST_PACKAGE_NAME, Arrays.asList(mNotificationChannel),
732 mNotificationChannel.getImportance(), mSbn, null, null, null,
733 null, Collections.singleton(TEST_PACKAGE_NAME));
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500734
735 Switch enabledSwitch = (Switch) mNotificationInfo.findViewById(R.id.channel_enabled_switch);
736 enabledSwitch.setChecked(false);
Mady Mellorc2dbe492017-03-30 13:22:03 -0700737 mNotificationInfo.handleCloseControls(false, false);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500738 verify(mMockINotificationManager, never()).updateNotificationChannelForPackage(
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400739 eq(TEST_PACKAGE_NAME), eq(TEST_UID), eq(mNotificationChannel));
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500740 }
Geoffrey Pitsch5278d3d2017-03-29 13:39:10 -0400741
742 @Test
743 public void testCloseControlsDoesNotUpdateIfCheckSaveListenerIsNoOp() throws Exception {
744 mNotificationChannel.setImportance(NotificationManager.IMPORTANCE_LOW);
745 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds3aedded2017-03-31 14:42:09 -0400746 TEST_PACKAGE_NAME, Arrays.asList(mNotificationChannel),
747 mNotificationChannel.getImportance(), mSbn, null, null, null,
748 (Runnable saveImportance) -> {
749 },
Geoffrey Pitsch5278d3d2017-03-29 13:39:10 -0400750 Collections.singleton(TEST_PACKAGE_NAME));
751
752 Switch enabledSwitch = (Switch) mNotificationInfo.findViewById(R.id.channel_enabled_switch);
753 enabledSwitch.setChecked(false);
Mady Mellorc2dbe492017-03-30 13:22:03 -0700754 mNotificationInfo.handleCloseControls(true, false);
Geoffrey Pitsch5278d3d2017-03-29 13:39:10 -0400755 verify(mMockINotificationManager, never()).updateNotificationChannelForPackage(
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400756 eq(TEST_PACKAGE_NAME), eq(TEST_UID), eq(mNotificationChannel));
Geoffrey Pitsch5278d3d2017-03-29 13:39:10 -0400757 }
758
759 @Test
760 public void testCloseControlsUpdatesWhenCheckSaveListenerUsesCallback() throws Exception {
761 mNotificationChannel.setImportance(NotificationManager.IMPORTANCE_LOW);
762 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds3aedded2017-03-31 14:42:09 -0400763 TEST_PACKAGE_NAME, Arrays.asList(mNotificationChannel),
764 mNotificationChannel.getImportance(), mSbn, null, null, null,
765 (Runnable saveImportance) -> {
766 saveImportance.run();
767 },
Geoffrey Pitsch5278d3d2017-03-29 13:39:10 -0400768 Collections.singleton(TEST_PACKAGE_NAME));
769
770 Switch enabledSwitch = (Switch) mNotificationInfo.findViewById(R.id.channel_enabled_switch);
771 enabledSwitch.setChecked(false);
Mady Mellorc2dbe492017-03-30 13:22:03 -0700772 mNotificationInfo.handleCloseControls(true, false);
Geoffrey Pitsch5278d3d2017-03-29 13:39:10 -0400773 verify(mMockINotificationManager, times(1)).updateNotificationChannelForPackage(
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400774 eq(TEST_PACKAGE_NAME), eq(TEST_UID), eq(mNotificationChannel));
Geoffrey Pitsch5278d3d2017-03-29 13:39:10 -0400775 }
Julia Reynolds3aedded2017-03-31 14:42:09 -0400776
777 @Test
778 public void testDisplaySettingsLink() throws Exception {
779 final CountDownLatch latch = new CountDownLatch(1);
780 final String settingsText = "work chats";
781 final ResolveInfo ri = new ResolveInfo();
782 ri.activityInfo = new ActivityInfo();
783 ri.activityInfo.packageName = TEST_PACKAGE_NAME;
784 ri.activityInfo.name = "something";
785 List<ResolveInfo> ris = new ArrayList<>();
786 ris.add(ri);
787 when(mMockPackageManager.queryIntentActivities(any(), anyInt())).thenReturn(ris);
788 mNotificationChannel.setImportance(NotificationManager.IMPORTANCE_LOW);
789 Notification n = new Notification.Builder(mContext, mNotificationChannel.getId())
790 .setSettingsText(settingsText).build();
791 StatusBarNotification sbn = new StatusBarNotification(TEST_PACKAGE_NAME, TEST_PACKAGE_NAME,
792 0, null, 0, 0, n, UserHandle.CURRENT, null, 0);
793
794 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
795 TEST_PACKAGE_NAME, Arrays.asList(mNotificationChannel),
796 mNotificationChannel.getImportance(), sbn, null,
797 (View v, Intent intent) -> {
798 latch.countDown();
799 }, null, null, null);
800 final TextView settingsLink = mNotificationInfo.findViewById(R.id.app_settings);
801 assertEquals(View.VISIBLE, settingsLink.getVisibility());
802 assertTrue(settingsLink.getText().toString().length() > settingsText.length());
803 assertTrue(settingsLink.getText().toString().contains(settingsText));
804 settingsLink.performClick();
805 assertEquals(0, latch.getCount());
806 }
807
808 @Test
809 public void testDisplaySettingsLink_multipleChannels() throws Exception {
810 final CountDownLatch latch = new CountDownLatch(1);
811 final String settingsText = "work chats";
812 final ResolveInfo ri = new ResolveInfo();
813 ri.activityInfo = new ActivityInfo();
814 ri.activityInfo.packageName = TEST_PACKAGE_NAME;
815 ri.activityInfo.name = "something";
816 List<ResolveInfo> ris = new ArrayList<>();
817 ris.add(ri);
818 when(mMockPackageManager.queryIntentActivities(any(), anyInt())).thenReturn(ris);
819 mNotificationChannel.setImportance(NotificationManager.IMPORTANCE_LOW);
820 Notification n = new Notification.Builder(mContext, mNotificationChannel.getId())
821 .setSettingsText(settingsText).build();
822 StatusBarNotification sbn = new StatusBarNotification(TEST_PACKAGE_NAME, TEST_PACKAGE_NAME,
823 0, null, 0, 0, n, UserHandle.CURRENT, null, 0);
824
825 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
826 TEST_PACKAGE_NAME, Arrays.asList(mNotificationChannel, mDefaultNotificationChannel),
827 mNotificationChannel.getImportance(), sbn, null, (View v, Intent intent) -> {
828 latch.countDown();
829 }, null, null, null);
830 final TextView settingsLink = mNotificationInfo.findViewById(R.id.app_settings);
831 assertEquals(View.VISIBLE, settingsLink.getVisibility());
832 settingsLink.performClick();
833 assertEquals(0, latch.getCount());
834 }
835
836 @Test
837 public void testNoSettingsLink_noHandlingActivity() throws Exception {
838 final String settingsText = "work chats";
839 when(mMockPackageManager.queryIntentActivities(any(), anyInt())).thenReturn(null);
840 mNotificationChannel.setImportance(NotificationManager.IMPORTANCE_LOW);
841 Notification n = new Notification.Builder(mContext, mNotificationChannel.getId())
842 .setSettingsText(settingsText).build();
843 StatusBarNotification sbn = new StatusBarNotification(TEST_PACKAGE_NAME, TEST_PACKAGE_NAME,
844 0, null, 0, 0, n, UserHandle.CURRENT, null, 0);
845
846 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
847 TEST_PACKAGE_NAME, Arrays.asList(mNotificationChannel),
848 mNotificationChannel.getImportance(), sbn, null, null, null,
849 null, null);
850 final TextView settingsLink = mNotificationInfo.findViewById(R.id.app_settings);
851 assertEquals(View.GONE, settingsLink.getVisibility());
852 }
853
854 @Test
855 public void testNoSettingsLink_noLinkText() throws Exception {
856 final ResolveInfo ri = new ResolveInfo();
857 ri.activityInfo = new ActivityInfo();
858 ri.activityInfo.packageName = TEST_PACKAGE_NAME;
859 ri.activityInfo.name = "something";
860 List<ResolveInfo> ris = new ArrayList<>();
861 ris.add(ri);
862 when(mMockPackageManager.queryIntentActivities(any(), anyInt())).thenReturn(ris);
863 mNotificationChannel.setImportance(NotificationManager.IMPORTANCE_LOW);
864 Notification n = new Notification.Builder(mContext, mNotificationChannel.getId()).build();
865 StatusBarNotification sbn = new StatusBarNotification(TEST_PACKAGE_NAME, TEST_PACKAGE_NAME,
866 0, null, 0, 0, n, UserHandle.CURRENT, null, 0);
867
868 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
869 TEST_PACKAGE_NAME, Arrays.asList(mNotificationChannel),
870 mNotificationChannel.getImportance(), sbn, null, null, null,
871 null, null);
872 final TextView settingsLink = mNotificationInfo.findViewById(R.id.app_settings);
873 assertEquals(View.GONE, settingsLink.getVisibility());
874 }
875
876 @Test
877 public void testNoSettingsLink_afterBlockingChannel() throws Exception {
878 final String settingsText = "work chats";
879 final ResolveInfo ri = new ResolveInfo();
880 ri.activityInfo = new ActivityInfo();
881 ri.activityInfo.packageName = TEST_PACKAGE_NAME;
882 ri.activityInfo.name = "something";
883 List<ResolveInfo> ris = new ArrayList<>();
884 ris.add(ri);
885 when(mMockPackageManager.queryIntentActivities(any(), anyInt())).thenReturn(ris);
886 mNotificationChannel.setImportance(NotificationManager.IMPORTANCE_LOW);
887 Notification n = new Notification.Builder(mContext, mNotificationChannel.getId())
888 .setSettingsText(settingsText).build();
889 StatusBarNotification sbn = new StatusBarNotification(TEST_PACKAGE_NAME, TEST_PACKAGE_NAME,
890 0, null, 0, 0, n, UserHandle.CURRENT, null, 0);
891
892 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
893 TEST_PACKAGE_NAME, Arrays.asList(mNotificationChannel),
894 mNotificationChannel.getImportance(), sbn, null, null, null,
895 null, null);
896 final TextView settingsLink = mNotificationInfo.findViewById(R.id.app_settings);
897 assertEquals(View.VISIBLE, settingsLink.getVisibility());
898
899 // Block channel
900 Switch enabledSwitch = (Switch) mNotificationInfo.findViewById(R.id.channel_enabled_switch);
901 enabledSwitch.setChecked(false);
902
903 assertEquals(View.GONE, settingsLink.getVisibility());
904
905 //unblock
906 enabledSwitch.setChecked(true);
907 assertEquals(View.VISIBLE, settingsLink.getVisibility());
908 }
Geoffrey Pitsch8c8dbde2017-04-20 11:44:33 -0400909
910 @Test
911 public void testWillBeRemovedReturnsFalseBeforeBind() throws Exception {
912 assertFalse(mNotificationInfo.willBeRemoved());
913 }
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500914}