blob: 638084763ea88ee3e0c4deeaefda4e4f7ff2cbbb [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 Reynolds3aedded2017-03-31 14:42:09 -0400124 mSbn = new StatusBarNotification(TEST_PACKAGE_NAME, TEST_PACKAGE_NAME, 0, null, 0, 0,
125 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
235 public void testBindNotification_DefaultChannelUsesNameWhenMoreThanOneChannelExists()
236 throws Exception {
237 when(mMockINotificationManager.getNumNotificationChannelsForPackage(
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400238 eq(TEST_PACKAGE_NAME), eq(TEST_UID), anyBoolean())).thenReturn(2);
Geoffrey Pitsch1fbf66e2017-05-09 11:32:27 -0400239 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
240 TEST_PACKAGE_NAME, Arrays.asList(mDefaultNotificationChannel),
241 mNotificationChannel.getImportance(), mSbn, null, null, null,
242 null, null);
243 final TextView textView = (TextView) mNotificationInfo.findViewById(R.id.channel_name);
244 assertEquals(mDefaultNotificationChannel.getName(), textView.getText());
245 }
246
247 @Test
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500248 public void testBindNotification_SetsOnClickListenerForSettings() throws Exception {
249 final CountDownLatch latch = new CountDownLatch(1);
250 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500251 TEST_PACKAGE_NAME, Arrays.asList(mNotificationChannel),
Julia Reynolds3aedded2017-03-31 14:42:09 -0400252 mNotificationChannel.getImportance(), mSbn,
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500253 (View v, NotificationChannel c, int appUid) -> {
Julia Reynolds3aedded2017-03-31 14:42:09 -0400254 assertEquals(mNotificationChannel, c);
255 latch.countDown();
256 }, null, null, null, null);
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500257
258 final TextView settingsButton =
259 (TextView) mNotificationInfo.findViewById(R.id.more_settings);
260 settingsButton.performClick();
261 // Verify that listener was triggered.
262 assertEquals(0, latch.getCount());
263 }
264
265 @Test
Geoffrey Pitsch029a3fa2017-03-30 13:01:57 -0400266 public void testBindNotification_SettingsButtonInvisibleWhenNoClickListener() throws Exception {
267 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
268 TEST_PACKAGE_NAME, Arrays.asList(mNotificationChannel),
Julia Reynolds3aedded2017-03-31 14:42:09 -0400269 mNotificationChannel.getImportance(), mSbn, null, null, null, null, null);
Geoffrey Pitsch029a3fa2017-03-30 13:01:57 -0400270 final TextView settingsButton =
271 (TextView) mNotificationInfo.findViewById(R.id.more_settings);
272 assertTrue(settingsButton.getVisibility() != View.VISIBLE);
273 }
274
275 @Test
276 public void testBindNotification_SettingsButtonReappersAfterSecondBind() throws Exception {
277 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
278 TEST_PACKAGE_NAME, Arrays.asList(mNotificationChannel),
Julia Reynolds4740b612017-04-06 12:31:56 -0400279 mNotificationChannel.getImportance(), mSbn, null, null, null, null, null);
Geoffrey Pitsch029a3fa2017-03-30 13:01:57 -0400280 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
281 TEST_PACKAGE_NAME, Arrays.asList(mNotificationChannel),
Julia Reynolds3aedded2017-03-31 14:42:09 -0400282 mNotificationChannel.getImportance(), mSbn,
283 (View v, NotificationChannel c, int appUid) -> {}, null, null, null, null);
Geoffrey Pitsch029a3fa2017-03-30 13:01:57 -0400284 final TextView settingsButton =
285 (TextView) mNotificationInfo.findViewById(R.id.more_settings);
286 assertEquals(View.VISIBLE, settingsButton.getVisibility());
287 }
288
289 @Test
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500290 public void testOnClickListenerPassesNullChannelForBundle() throws Exception {
291 final CountDownLatch latch = new CountDownLatch(1);
292 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
293 TEST_PACKAGE_NAME,
294 Arrays.asList(mNotificationChannel, mDefaultNotificationChannel),
Julia Reynolds3aedded2017-03-31 14:42:09 -0400295 mNotificationChannel.getImportance(), mSbn,
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500296 (View v, NotificationChannel c, int appUid) -> {
Julia Reynolds3aedded2017-03-31 14:42:09 -0400297 assertEquals(null, c);
298 latch.countDown();
299 }, null, null, null, null);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500300
301 final TextView settingsButton =
302 (TextView) mNotificationInfo.findViewById(R.id.more_settings);
303 settingsButton.performClick();
304 // Verify that listener was triggered.
305 assertEquals(0, latch.getCount());
306 }
307
308 @Test
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500309 public void testBindNotification_SettingsTextWithOneChannel() throws Exception {
310 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500311 TEST_PACKAGE_NAME, Arrays.asList(mNotificationChannel),
Julia Reynolds3aedded2017-03-31 14:42:09 -0400312 mNotificationChannel.getImportance(), mSbn,
313 (View v, NotificationChannel c, int appUid) -> {
314 }, null, null, null, null);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500315 final TextView settingsButton =
316 (TextView) mNotificationInfo.findViewById(R.id.more_settings);
317 assertEquals(getStringById(R.string.notification_more_settings), settingsButton.getText());
318 }
319
320 @Test
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500321 public void testBindNotification_SettingsTextWithMultipleChannels() throws Exception {
322 when(mMockINotificationManager.getNumNotificationChannelsForPackage(
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400323 eq(TEST_PACKAGE_NAME), eq(TEST_UID), anyBoolean())).thenReturn(2);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500324 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500325 TEST_PACKAGE_NAME, Arrays.asList(mNotificationChannel),
Julia Reynolds3aedded2017-03-31 14:42:09 -0400326 mNotificationChannel.getImportance(), mSbn,
327 (View v, NotificationChannel c, int appUid) -> {
328 }, null, null, null, null);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500329 final TextView settingsButton =
330 (TextView) mNotificationInfo.findViewById(R.id.more_settings);
331 assertEquals(getStringById(R.string.notification_all_categories), settingsButton.getText());
332 }
333
334 @Test
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500335 public void testBindNotification_SetsOnClickListenerForDone() throws Exception {
336 final CountDownLatch latch = new CountDownLatch(1);
337 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds3aedded2017-03-31 14:42:09 -0400338 TEST_PACKAGE_NAME, Arrays.asList(mNotificationChannel),
339 mNotificationChannel.getImportance(), mSbn, null,
340 null, (View v) -> {
341 latch.countDown();
342 },
Geoffrey Pitsch5278d3d2017-03-29 13:39:10 -0400343 null, null);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500344
345 final TextView doneButton = (TextView) mNotificationInfo.findViewById(R.id.done);
346 doneButton.performClick();
347 // Verify that listener was triggered.
348 assertEquals(0, latch.getCount());
349 }
350
351 @Test
Geoffrey Pitsch0a8f0552017-04-20 13:56:40 -0400352 public void testBindNotification_NumChannelsTextHiddenWhenDefaultChannel() throws Exception {
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500353 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Geoffrey Pitsch5278d3d2017-03-29 13:39:10 -0400354 TEST_PACKAGE_NAME, Arrays.asList(mDefaultNotificationChannel),
Julia Reynolds3aedded2017-03-31 14:42:09 -0400355 mNotificationChannel.getImportance(), mSbn, null, null,
356 null, null, null);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500357 final TextView numChannelsView =
358 (TextView) mNotificationInfo.findViewById(R.id.num_channels_desc);
Geoffrey Pitsch0a8f0552017-04-20 13:56:40 -0400359 assertEquals(View.INVISIBLE, numChannelsView.getVisibility());
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500360 }
361
362 @Test
Geoffrey Pitsch1fbf66e2017-05-09 11:32:27 -0400363 public void testBindNotification_NumChannelsTextDisplaysWhenMoreThanOneChannelExists()
364 throws Exception {
365 when(mMockINotificationManager.getNumNotificationChannelsForPackage(
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400366 eq(TEST_PACKAGE_NAME), eq(TEST_UID), anyBoolean())).thenReturn(2);
Geoffrey Pitsch1fbf66e2017-05-09 11:32:27 -0400367 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
368 TEST_PACKAGE_NAME, Arrays.asList(mDefaultNotificationChannel),
369 mNotificationChannel.getImportance(), mSbn, null, null,
370 null, null, null);
371 final TextView numChannelsView =
372 (TextView) mNotificationInfo.findViewById(R.id.num_channels_desc);
373 assertEquals(numChannelsView.getVisibility(), View.VISIBLE);
374 assertEquals(getNumChannelsDescString(2), numChannelsView.getText());
375 }
376
377 @Test
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500378 public void testBindNotification_NumChannelsTextDisplaysWhenNotDefaultChannel()
379 throws Exception {
380 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds3aedded2017-03-31 14:42:09 -0400381 TEST_PACKAGE_NAME, Arrays.asList(mNotificationChannel),
382 mNotificationChannel.getImportance(), mSbn, null, null, null,
383 null, null);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500384 final TextView numChannelsView =
385 (TextView) mNotificationInfo.findViewById(R.id.num_channels_desc);
386 assertEquals(numChannelsView.getVisibility(), View.VISIBLE);
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500387 assertEquals(getNumChannelsDescString(1), numChannelsView.getText());
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500388 }
389
390 @Test
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500391 public void testBindNotification_NumChannelsTextScalesWithNumberOfChannels()
392 throws Exception {
393 when(mMockINotificationManager.getNumNotificationChannelsForPackage(
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400394 eq(TEST_PACKAGE_NAME), eq(TEST_UID), anyBoolean())).thenReturn(2);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500395 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds3aedded2017-03-31 14:42:09 -0400396 TEST_PACKAGE_NAME, Arrays.asList(mNotificationChannel),
397 mNotificationChannel.getImportance(), mSbn, null, null, null,
398 null, null);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500399 final TextView numChannelsView =
400 (TextView) mNotificationInfo.findViewById(R.id.num_channels_desc);
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500401 assertEquals(getNumChannelsDescString(2), numChannelsView.getText());
402 }
403
404 @Test
405 @UiThreadTest
406 public void testBindNotification_NumChannelsTextListsChannelsWhenTwoInBundle()
407 throws Exception {
408 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
409 TEST_PACKAGE_NAME, Arrays.asList(mNotificationChannel, mDefaultNotificationChannel),
Julia Reynolds3aedded2017-03-31 14:42:09 -0400410 mNotificationChannel.getImportance(), mSbn, null, null, null, null, null);
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500411 final TextView numChannelsView =
412 (TextView) mNotificationInfo.findViewById(R.id.num_channels_desc);
413 assertEquals(getChannelsListDescString(mNotificationChannel, mDefaultNotificationChannel),
414 numChannelsView.getText());
415 }
416
417 @Test
418 @UiThreadTest
419 public void testBindNotification_NumChannelsTextListsChannelsWhenThreeInBundle()
420 throws Exception {
421 NotificationChannel thirdChannel = new NotificationChannel(
422 "third_channel", "third_channel", NotificationManager.IMPORTANCE_LOW);
423 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
424 TEST_PACKAGE_NAME,
425 Arrays.asList(mNotificationChannel, mDefaultNotificationChannel, thirdChannel),
Julia Reynolds3aedded2017-03-31 14:42:09 -0400426 mNotificationChannel.getImportance(), mSbn, null, null, null, null, null);
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500427 final TextView numChannelsView =
428 (TextView) mNotificationInfo.findViewById(R.id.num_channels_desc);
429 assertEquals(
430 getChannelsListDescString(mNotificationChannel, mDefaultNotificationChannel,
431 thirdChannel),
432 numChannelsView.getText());
433 }
434
435 @Test
436 @UiThreadTest
437 public void testBindNotification_NumChannelsTextListsChannelsWhenFourInBundle()
438 throws Exception {
439 NotificationChannel thirdChannel = new NotificationChannel(
440 "third_channel", "third_channel", NotificationManager.IMPORTANCE_LOW);
441 NotificationChannel fourthChannel = new NotificationChannel(
442 "fourth_channel", "fourth_channel", NotificationManager.IMPORTANCE_LOW);
443 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
444 TEST_PACKAGE_NAME,
445 Arrays.asList(mNotificationChannel, mDefaultNotificationChannel, thirdChannel,
Julia Reynolds3aedded2017-03-31 14:42:09 -0400446 fourthChannel), mNotificationChannel.getImportance(), mSbn, null, null,
447 null, null, null);
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500448 final TextView numChannelsView =
449 (TextView) mNotificationInfo.findViewById(R.id.num_channels_desc);
450 assertEquals(
451 getChannelsListDescString(mNotificationChannel, mDefaultNotificationChannel,
452 thirdChannel, fourthChannel),
453 numChannelsView.getText());
454 }
455
456 @Test
457 @UiThreadTest
458 public void testBindNotification_ChannelNameChangesWhenBundleFromDifferentChannels()
459 throws Exception {
460 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
461 TEST_PACKAGE_NAME, Arrays.asList(mNotificationChannel, mDefaultNotificationChannel),
Julia Reynolds3aedded2017-03-31 14:42:09 -0400462 mNotificationChannel.getImportance(), mSbn, null, null, null, null, null);
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500463 final TextView channelNameView =
464 (TextView) mNotificationInfo.findViewById(R.id.channel_name);
465 assertEquals(getNumChannelsString(2), channelNameView.getText());
466 }
467
468 @Test
469 @UiThreadTest
470 public void testEnabledSwitchInvisibleIfBundleFromDifferentChannels() throws Exception {
471 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
472 TEST_PACKAGE_NAME, Arrays.asList(mNotificationChannel, mDefaultNotificationChannel),
Julia Reynolds3aedded2017-03-31 14:42:09 -0400473 mNotificationChannel.getImportance(), mSbn, null, null, null, null, null);
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500474 Switch enabledSwitch = (Switch) mNotificationInfo.findViewById(R.id.channel_enabled_switch);
475 assertEquals(View.INVISIBLE, enabledSwitch.getVisibility());
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500476 }
477
478 @Test
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500479 public void testbindNotification_ChannelDisabledTextGoneWhenNotDisabled() throws Exception {
480 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds3aedded2017-03-31 14:42:09 -0400481 TEST_PACKAGE_NAME, Arrays.asList(mNotificationChannel),
482 mNotificationChannel.getImportance(), mSbn, null, null, null, null, null);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500483 final TextView channelDisabledView =
484 (TextView) mNotificationInfo.findViewById(R.id.channel_disabled);
485 assertEquals(channelDisabledView.getVisibility(), View.GONE);
486 }
487
488 @Test
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500489 public void testbindNotification_ChannelDisabledTextVisibleWhenDisabled() throws Exception {
490 mNotificationChannel.setImportance(NotificationManager.IMPORTANCE_NONE);
491 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds3aedded2017-03-31 14:42:09 -0400492 TEST_PACKAGE_NAME, Arrays.asList(mNotificationChannel),
493 mNotificationChannel.getImportance(), mSbn, null, null, null,
494 null, null);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500495 final TextView channelDisabledView =
496 (TextView) mNotificationInfo.findViewById(R.id.channel_disabled);
497 assertEquals(channelDisabledView.getVisibility(), View.VISIBLE);
498 // Replaces the numChannelsView
499 final TextView numChannelsView =
500 (TextView) mNotificationInfo.findViewById(R.id.num_channels_desc);
501 assertEquals(numChannelsView.getVisibility(), View.GONE);
502 }
503
504 @Test
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500505 @UiThreadTest
Geoffrey Pitschee8c81e2017-03-23 11:38:56 -0400506 public void testBindNotification_ChannelDisabledTextShowsForDefaultChannel()
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500507 throws Exception {
Geoffrey Pitschee8c81e2017-03-23 11:38:56 -0400508 mDefaultNotificationChannel.setImportance(NotificationManager.IMPORTANCE_NONE);
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500509 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Geoffrey Pitsch5278d3d2017-03-29 13:39:10 -0400510 TEST_PACKAGE_NAME, Arrays.asList(mDefaultNotificationChannel),
Julia Reynolds3aedded2017-03-31 14:42:09 -0400511 mDefaultNotificationChannel.getImportance(), mSbn, null, null,
512 null, null, null);
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500513 final TextView channelDisabledView =
514 (TextView) mNotificationInfo.findViewById(R.id.channel_disabled);
Geoffrey Pitschee8c81e2017-03-23 11:38:56 -0400515 assertEquals(View.VISIBLE, channelDisabledView.getVisibility());
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500516 }
517
518 @Test
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500519 public void testBindNotification_DoesNotUpdateNotificationChannel() throws Exception {
520 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds3aedded2017-03-31 14:42:09 -0400521 TEST_PACKAGE_NAME, Arrays.asList(mNotificationChannel),
522 mNotificationChannel.getImportance(), mSbn, null, null, null,
523 null, null);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500524 verify(mMockINotificationManager, never()).updateNotificationChannelForPackage(
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400525 anyString(), eq(TEST_UID), any());
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500526 }
527
528 @Test
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500529 public void testDoesNotUpdateNotificationChannelAfterImportanceChanged() throws Exception {
530 mNotificationChannel.setImportance(NotificationManager.IMPORTANCE_LOW);
531 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds3aedded2017-03-31 14:42:09 -0400532 TEST_PACKAGE_NAME, Arrays.asList(mNotificationChannel),
533 mNotificationChannel.getImportance(), mSbn, null, null, null,
534 null, null);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500535
536 Switch enabledSwitch = (Switch) mNotificationInfo.findViewById(R.id.channel_enabled_switch);
537 enabledSwitch.setChecked(false);
538 verify(mMockINotificationManager, never()).updateNotificationChannelForPackage(
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400539 anyString(), eq(TEST_UID), any());
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500540 }
541
542 @Test
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500543 public void testHandleCloseControls_DoesNotUpdateNotificationChannelIfUnchanged()
544 throws Exception {
545 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds3aedded2017-03-31 14:42:09 -0400546 TEST_PACKAGE_NAME, Arrays.asList(mNotificationChannel),
547 mNotificationChannel.getImportance(), mSbn, null, null, null,
548 null, null);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500549
Mady Mellorc2dbe492017-03-30 13:22:03 -0700550 mNotificationInfo.handleCloseControls(true, false);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500551 verify(mMockINotificationManager, never()).updateNotificationChannelForPackage(
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400552 anyString(), eq(TEST_UID), any());
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500553 }
554
555 @Test
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500556 public void testHandleCloseControls_DoesNotUpdateNotificationChannelIfUnspecified()
557 throws Exception {
558 mNotificationChannel.setImportance(NotificationManager.IMPORTANCE_UNSPECIFIED);
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
Mady Mellorc2dbe492017-03-30 13:22:03 -0700564 mNotificationInfo.handleCloseControls(true, false);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500565 verify(mMockINotificationManager, never()).updateNotificationChannelForPackage(
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400566 anyString(), eq(TEST_UID), any());
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500567 }
568
569 @Test
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500570 public void testEnabledSwitchOnByDefault() throws Exception {
571 mNotificationChannel.setImportance(NotificationManager.IMPORTANCE_LOW);
572 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds3aedded2017-03-31 14:42:09 -0400573 TEST_PACKAGE_NAME, Arrays.asList(mNotificationChannel),
574 mNotificationChannel.getImportance(), mSbn, null, null, null,
575 null, null);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500576
577 Switch enabledSwitch = (Switch) mNotificationInfo.findViewById(R.id.channel_enabled_switch);
578 assertTrue(enabledSwitch.isChecked());
579 }
580
581 @Test
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500582 public void testEnabledButtonOffWhenAlreadyBanned() throws Exception {
583 mNotificationChannel.setImportance(NotificationManager.IMPORTANCE_NONE);
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
589 Switch enabledSwitch = (Switch) mNotificationInfo.findViewById(R.id.channel_enabled_switch);
590 assertFalse(enabledSwitch.isChecked());
591 }
592
593 @Test
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500594 public void testEnabledSwitchVisibleByDefault() throws Exception {
595 mNotificationChannel.setImportance(NotificationManager.IMPORTANCE_LOW);
596 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds3aedded2017-03-31 14:42:09 -0400597 TEST_PACKAGE_NAME, Arrays.asList(mNotificationChannel),
598 mNotificationChannel.getImportance(), mSbn, null, null, null,
599 null, null);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500600
601 Switch enabledSwitch = (Switch) mNotificationInfo.findViewById(R.id.channel_enabled_switch);
602 assertEquals(View.VISIBLE, enabledSwitch.getVisibility());
603 }
604
605 @Test
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500606 public void testEnabledSwitchInvisibleIfNonBlockable() throws Exception {
607 mNotificationChannel.setImportance(NotificationManager.IMPORTANCE_LOW);
608 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds3aedded2017-03-31 14:42:09 -0400609 TEST_PACKAGE_NAME, Arrays.asList(mNotificationChannel),
610 mNotificationChannel.getImportance(), mSbn, null, null, null,
611 null, Collections.singleton(TEST_PACKAGE_NAME));
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500612
613 Switch enabledSwitch = (Switch) mNotificationInfo.findViewById(R.id.channel_enabled_switch);
614 assertEquals(View.INVISIBLE, enabledSwitch.getVisibility());
615 }
616
617 @Test
Julia Reynoldsf7321592017-05-24 16:09:19 -0400618 public void testEnabledSwitchInvisibleIfNonBlockableSystemChannel() throws Exception {
619 mNotificationChannel.setImportance(NotificationManager.IMPORTANCE_LOW);
620 mNotificationChannel.setBlockableSystem(false);
621 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
622 TEST_SYSTEM_PACKAGE_NAME, Arrays.asList(mNotificationChannel),
623 mNotificationChannel.getImportance(), mSbn, null, null, null,
624 null, null);
625
626 Switch enabledSwitch = (Switch) mNotificationInfo.findViewById(R.id.channel_enabled_switch);
627 assertEquals(View.INVISIBLE, enabledSwitch.getVisibility());
628 }
629
630 @Test
631 public void testEnabledSwitchVisibleIfBlockableSystemChannel() throws Exception {
632 mNotificationChannel.setImportance(NotificationManager.IMPORTANCE_LOW);
633 mNotificationChannel.setBlockableSystem(true);
634 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
635 TEST_SYSTEM_PACKAGE_NAME, Arrays.asList(mNotificationChannel),
636 mNotificationChannel.getImportance(), mSbn, null, null, null,
637 null, null);
638
639 Switch enabledSwitch = (Switch) mNotificationInfo.findViewById(R.id.channel_enabled_switch);
640 assertEquals(View.VISIBLE, enabledSwitch.getVisibility());
641 }
642
643 @Test
644 public void testEnabledSwitchInvisibleIfMultiChannelSummary() throws Exception {
645 mNotificationChannel.setImportance(NotificationManager.IMPORTANCE_LOW);
646 mNotificationChannel.setBlockableSystem(true);
647 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
648 TEST_PACKAGE_NAME, Arrays.asList(mNotificationChannel, mDefaultNotificationChannel),
649 mNotificationChannel.getImportance(), mSbn, null, null, null,
650 null, Collections.singleton(TEST_PACKAGE_NAME));
651
652 Switch enabledSwitch = (Switch) mNotificationInfo.findViewById(R.id.channel_enabled_switch);
653 assertEquals(View.INVISIBLE, enabledSwitch.getVisibility());
654 }
655
656 @Test
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500657 public void testNonBlockableAppDoesNotBecomeBlocked() throws Exception {
658 mNotificationChannel.setImportance(NotificationManager.IMPORTANCE_LOW);
659 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds3aedded2017-03-31 14:42:09 -0400660 TEST_PACKAGE_NAME, Arrays.asList(mNotificationChannel),
661 mNotificationChannel.getImportance(), mSbn, null, null, null,
662 null, Collections.singleton(TEST_PACKAGE_NAME));
Mady Mellorc2dbe492017-03-30 13:22:03 -0700663 mNotificationInfo.handleCloseControls(true, false);
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500664 verify(mMockINotificationManager, never()).updateNotificationChannelForPackage(
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400665 anyString(), eq(TEST_UID), any());
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500666 }
667
668 @Test
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500669 public void testEnabledSwitchChangedCallsUpdateNotificationChannel() throws Exception {
670 mNotificationChannel.setImportance(NotificationManager.IMPORTANCE_LOW);
671 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds3aedded2017-03-31 14:42:09 -0400672 TEST_PACKAGE_NAME, Arrays.asList(mNotificationChannel),
673 mNotificationChannel.getImportance(), mSbn, null, null, null,
674 null, Collections.singleton(TEST_PACKAGE_NAME));
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500675
Julia Reynolds8ceb5792017-04-11 11:32:44 -0400676 Switch enabledSwitch = mNotificationInfo.findViewById(R.id.channel_enabled_switch);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500677 enabledSwitch.setChecked(false);
Mady Mellorc2dbe492017-03-30 13:22:03 -0700678 mNotificationInfo.handleCloseControls(true, false);
Julia Reynolds8ceb5792017-04-11 11:32:44 -0400679
680 ArgumentCaptor<NotificationChannel> updated =
681 ArgumentCaptor.forClass(NotificationChannel.class);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500682 verify(mMockINotificationManager, times(1)).updateNotificationChannelForPackage(
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400683 anyString(), eq(TEST_UID), updated.capture());
Julia Reynolds8ceb5792017-04-11 11:32:44 -0400684 assertTrue((updated.getValue().getUserLockedFields()
685 & NotificationChannel.USER_LOCKED_IMPORTANCE) != 0);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500686 }
687
688 @Test
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500689 public void testCloseControlsDoesNotUpdateIfSaveIsFalse() throws Exception {
690 mNotificationChannel.setImportance(NotificationManager.IMPORTANCE_LOW);
691 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds3aedded2017-03-31 14:42:09 -0400692 TEST_PACKAGE_NAME, Arrays.asList(mNotificationChannel),
693 mNotificationChannel.getImportance(), mSbn, null, null, null,
694 null, Collections.singleton(TEST_PACKAGE_NAME));
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500695
696 Switch enabledSwitch = (Switch) mNotificationInfo.findViewById(R.id.channel_enabled_switch);
697 enabledSwitch.setChecked(false);
Mady Mellorc2dbe492017-03-30 13:22:03 -0700698 mNotificationInfo.handleCloseControls(false, false);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500699 verify(mMockINotificationManager, never()).updateNotificationChannelForPackage(
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400700 eq(TEST_PACKAGE_NAME), eq(TEST_UID), eq(mNotificationChannel));
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500701 }
Geoffrey Pitsch5278d3d2017-03-29 13:39:10 -0400702
703 @Test
704 public void testCloseControlsDoesNotUpdateIfCheckSaveListenerIsNoOp() throws Exception {
705 mNotificationChannel.setImportance(NotificationManager.IMPORTANCE_LOW);
706 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds3aedded2017-03-31 14:42:09 -0400707 TEST_PACKAGE_NAME, Arrays.asList(mNotificationChannel),
708 mNotificationChannel.getImportance(), mSbn, null, null, null,
709 (Runnable saveImportance) -> {
710 },
Geoffrey Pitsch5278d3d2017-03-29 13:39:10 -0400711 Collections.singleton(TEST_PACKAGE_NAME));
712
713 Switch enabledSwitch = (Switch) mNotificationInfo.findViewById(R.id.channel_enabled_switch);
714 enabledSwitch.setChecked(false);
Mady Mellorc2dbe492017-03-30 13:22:03 -0700715 mNotificationInfo.handleCloseControls(true, false);
Geoffrey Pitsch5278d3d2017-03-29 13:39:10 -0400716 verify(mMockINotificationManager, never()).updateNotificationChannelForPackage(
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400717 eq(TEST_PACKAGE_NAME), eq(TEST_UID), eq(mNotificationChannel));
Geoffrey Pitsch5278d3d2017-03-29 13:39:10 -0400718 }
719
720 @Test
721 public void testCloseControlsUpdatesWhenCheckSaveListenerUsesCallback() throws Exception {
722 mNotificationChannel.setImportance(NotificationManager.IMPORTANCE_LOW);
723 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds3aedded2017-03-31 14:42:09 -0400724 TEST_PACKAGE_NAME, Arrays.asList(mNotificationChannel),
725 mNotificationChannel.getImportance(), mSbn, null, null, null,
726 (Runnable saveImportance) -> {
727 saveImportance.run();
728 },
Geoffrey Pitsch5278d3d2017-03-29 13:39:10 -0400729 Collections.singleton(TEST_PACKAGE_NAME));
730
731 Switch enabledSwitch = (Switch) mNotificationInfo.findViewById(R.id.channel_enabled_switch);
732 enabledSwitch.setChecked(false);
Mady Mellorc2dbe492017-03-30 13:22:03 -0700733 mNotificationInfo.handleCloseControls(true, false);
Geoffrey Pitsch5278d3d2017-03-29 13:39:10 -0400734 verify(mMockINotificationManager, times(1)).updateNotificationChannelForPackage(
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400735 eq(TEST_PACKAGE_NAME), eq(TEST_UID), eq(mNotificationChannel));
Geoffrey Pitsch5278d3d2017-03-29 13:39:10 -0400736 }
Julia Reynolds3aedded2017-03-31 14:42:09 -0400737
738 @Test
739 public void testDisplaySettingsLink() throws Exception {
740 final CountDownLatch latch = new CountDownLatch(1);
741 final String settingsText = "work chats";
742 final ResolveInfo ri = new ResolveInfo();
743 ri.activityInfo = new ActivityInfo();
744 ri.activityInfo.packageName = TEST_PACKAGE_NAME;
745 ri.activityInfo.name = "something";
746 List<ResolveInfo> ris = new ArrayList<>();
747 ris.add(ri);
748 when(mMockPackageManager.queryIntentActivities(any(), anyInt())).thenReturn(ris);
749 mNotificationChannel.setImportance(NotificationManager.IMPORTANCE_LOW);
750 Notification n = new Notification.Builder(mContext, mNotificationChannel.getId())
751 .setSettingsText(settingsText).build();
752 StatusBarNotification sbn = new StatusBarNotification(TEST_PACKAGE_NAME, TEST_PACKAGE_NAME,
753 0, null, 0, 0, n, UserHandle.CURRENT, null, 0);
754
755 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
756 TEST_PACKAGE_NAME, Arrays.asList(mNotificationChannel),
757 mNotificationChannel.getImportance(), sbn, null,
758 (View v, Intent intent) -> {
759 latch.countDown();
760 }, null, null, null);
761 final TextView settingsLink = mNotificationInfo.findViewById(R.id.app_settings);
762 assertEquals(View.VISIBLE, settingsLink.getVisibility());
763 assertTrue(settingsLink.getText().toString().length() > settingsText.length());
764 assertTrue(settingsLink.getText().toString().contains(settingsText));
765 settingsLink.performClick();
766 assertEquals(0, latch.getCount());
767 }
768
769 @Test
770 public void testDisplaySettingsLink_multipleChannels() throws Exception {
771 final CountDownLatch latch = new CountDownLatch(1);
772 final String settingsText = "work chats";
773 final ResolveInfo ri = new ResolveInfo();
774 ri.activityInfo = new ActivityInfo();
775 ri.activityInfo.packageName = TEST_PACKAGE_NAME;
776 ri.activityInfo.name = "something";
777 List<ResolveInfo> ris = new ArrayList<>();
778 ris.add(ri);
779 when(mMockPackageManager.queryIntentActivities(any(), anyInt())).thenReturn(ris);
780 mNotificationChannel.setImportance(NotificationManager.IMPORTANCE_LOW);
781 Notification n = new Notification.Builder(mContext, mNotificationChannel.getId())
782 .setSettingsText(settingsText).build();
783 StatusBarNotification sbn = new StatusBarNotification(TEST_PACKAGE_NAME, TEST_PACKAGE_NAME,
784 0, null, 0, 0, n, UserHandle.CURRENT, null, 0);
785
786 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
787 TEST_PACKAGE_NAME, Arrays.asList(mNotificationChannel, mDefaultNotificationChannel),
788 mNotificationChannel.getImportance(), sbn, null, (View v, Intent intent) -> {
789 latch.countDown();
790 }, null, null, null);
791 final TextView settingsLink = mNotificationInfo.findViewById(R.id.app_settings);
792 assertEquals(View.VISIBLE, settingsLink.getVisibility());
793 settingsLink.performClick();
794 assertEquals(0, latch.getCount());
795 }
796
797 @Test
798 public void testNoSettingsLink_noHandlingActivity() throws Exception {
799 final String settingsText = "work chats";
800 when(mMockPackageManager.queryIntentActivities(any(), anyInt())).thenReturn(null);
801 mNotificationChannel.setImportance(NotificationManager.IMPORTANCE_LOW);
802 Notification n = new Notification.Builder(mContext, mNotificationChannel.getId())
803 .setSettingsText(settingsText).build();
804 StatusBarNotification sbn = new StatusBarNotification(TEST_PACKAGE_NAME, TEST_PACKAGE_NAME,
805 0, null, 0, 0, n, UserHandle.CURRENT, null, 0);
806
807 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
808 TEST_PACKAGE_NAME, Arrays.asList(mNotificationChannel),
809 mNotificationChannel.getImportance(), sbn, null, null, null,
810 null, null);
811 final TextView settingsLink = mNotificationInfo.findViewById(R.id.app_settings);
812 assertEquals(View.GONE, settingsLink.getVisibility());
813 }
814
815 @Test
816 public void testNoSettingsLink_noLinkText() throws Exception {
817 final ResolveInfo ri = new ResolveInfo();
818 ri.activityInfo = new ActivityInfo();
819 ri.activityInfo.packageName = TEST_PACKAGE_NAME;
820 ri.activityInfo.name = "something";
821 List<ResolveInfo> ris = new ArrayList<>();
822 ris.add(ri);
823 when(mMockPackageManager.queryIntentActivities(any(), anyInt())).thenReturn(ris);
824 mNotificationChannel.setImportance(NotificationManager.IMPORTANCE_LOW);
825 Notification n = new Notification.Builder(mContext, mNotificationChannel.getId()).build();
826 StatusBarNotification sbn = new StatusBarNotification(TEST_PACKAGE_NAME, TEST_PACKAGE_NAME,
827 0, null, 0, 0, n, UserHandle.CURRENT, null, 0);
828
829 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
830 TEST_PACKAGE_NAME, Arrays.asList(mNotificationChannel),
831 mNotificationChannel.getImportance(), sbn, null, null, null,
832 null, null);
833 final TextView settingsLink = mNotificationInfo.findViewById(R.id.app_settings);
834 assertEquals(View.GONE, settingsLink.getVisibility());
835 }
836
837 @Test
838 public void testNoSettingsLink_afterBlockingChannel() throws Exception {
839 final String settingsText = "work chats";
840 final ResolveInfo ri = new ResolveInfo();
841 ri.activityInfo = new ActivityInfo();
842 ri.activityInfo.packageName = TEST_PACKAGE_NAME;
843 ri.activityInfo.name = "something";
844 List<ResolveInfo> ris = new ArrayList<>();
845 ris.add(ri);
846 when(mMockPackageManager.queryIntentActivities(any(), anyInt())).thenReturn(ris);
847 mNotificationChannel.setImportance(NotificationManager.IMPORTANCE_LOW);
848 Notification n = new Notification.Builder(mContext, mNotificationChannel.getId())
849 .setSettingsText(settingsText).build();
850 StatusBarNotification sbn = new StatusBarNotification(TEST_PACKAGE_NAME, TEST_PACKAGE_NAME,
851 0, null, 0, 0, n, UserHandle.CURRENT, null, 0);
852
853 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
854 TEST_PACKAGE_NAME, Arrays.asList(mNotificationChannel),
855 mNotificationChannel.getImportance(), sbn, null, null, null,
856 null, null);
857 final TextView settingsLink = mNotificationInfo.findViewById(R.id.app_settings);
858 assertEquals(View.VISIBLE, settingsLink.getVisibility());
859
860 // Block channel
861 Switch enabledSwitch = (Switch) mNotificationInfo.findViewById(R.id.channel_enabled_switch);
862 enabledSwitch.setChecked(false);
863
864 assertEquals(View.GONE, settingsLink.getVisibility());
865
866 //unblock
867 enabledSwitch.setChecked(true);
868 assertEquals(View.VISIBLE, settingsLink.getVisibility());
869 }
Geoffrey Pitsch8c8dbde2017-04-20 11:44:33 -0400870
871 @Test
872 public void testWillBeRemovedReturnsFalseBeforeBind() throws Exception {
873 assertFalse(mNotificationInfo.willBeRemoved());
874 }
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500875}