blob: 658ac73153ab0b7cbf284c6f54b5f9b499edee0c [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 Reynoldsc65656a2018-02-12 09:55:14 -050019import static android.app.NotificationChannel.USER_LOCKED_IMPORTANCE;
Julia Reynolds437cdb12018-01-03 12:27:24 -050020import static android.app.NotificationManager.IMPORTANCE_LOW;
Julia Reynoldse0341482018-03-08 14:42:50 -050021import static android.app.NotificationManager.IMPORTANCE_MIN;
22import static android.app.NotificationManager.IMPORTANCE_NONE;
Julia Reynoldsc65656a2018-02-12 09:55:14 -050023import static android.app.NotificationManager.IMPORTANCE_UNSPECIFIED;
Julia Reynoldsf7321592017-05-24 16:09:19 -040024import static android.print.PrintManager.PRINT_SPOOLER_PACKAGE_NAME;
Julia Reynolds437cdb12018-01-03 12:27:24 -050025import static android.view.View.GONE;
26import static android.view.View.VISIBLE;
Julia Reynoldsf7321592017-05-24 16:09:19 -040027
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050028import static junit.framework.Assert.assertEquals;
29import static junit.framework.Assert.assertFalse;
30import static junit.framework.Assert.assertTrue;
Julia Reynolds5a311932017-03-01 16:33:44 -050031
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050032import static org.mockito.Mockito.any;
33import static org.mockito.Mockito.anyBoolean;
34import static org.mockito.Mockito.anyInt;
35import static org.mockito.Mockito.anyString;
36import static org.mockito.Mockito.eq;
37import static org.mockito.Mockito.mock;
38import static org.mockito.Mockito.never;
39import static org.mockito.Mockito.times;
40import static org.mockito.Mockito.verify;
41import static org.mockito.Mockito.when;
42
43import android.app.INotificationManager;
Julia Reynolds3aedded2017-03-31 14:42:09 -040044import android.app.Notification;
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050045import android.app.NotificationChannel;
46import android.app.NotificationChannelGroup;
47import android.app.NotificationManager;
Julia Reynolds3aedded2017-03-31 14:42:09 -040048import android.content.Intent;
49import android.content.pm.ActivityInfo;
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050050import android.content.pm.ApplicationInfo;
51import android.content.pm.PackageInfo;
52import android.content.pm.PackageManager;
Julia Reynolds3aedded2017-03-31 14:42:09 -040053import android.content.pm.ResolveInfo;
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050054import android.graphics.drawable.Drawable;
Julia Reynolds3aedded2017-03-31 14:42:09 -040055import android.os.UserHandle;
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050056import android.service.notification.StatusBarNotification;
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050057import android.test.suitebuilder.annotation.SmallTest;
Jason Monk340b0e52017-03-08 14:57:56 -050058import android.testing.AndroidTestingRunner;
Julia Reynolds437cdb12018-01-03 12:27:24 -050059import android.testing.PollingCheck;
Jason Monk340b0e52017-03-08 14:57:56 -050060import android.testing.UiThreadTest;
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050061import android.view.LayoutInflater;
62import android.view.View;
63import android.widget.ImageView;
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050064import android.widget.TextView;
Jason Monk340b0e52017-03-08 14:57:56 -050065
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050066import com.android.systemui.R;
67import com.android.systemui.SysuiTestCase;
Jason Monkba364322017-03-06 11:19:20 -050068
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050069import org.junit.Before;
Rohan Shah524cf7b2018-03-15 14:40:02 -070070import org.junit.Rule;
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050071import org.junit.Test;
Jason Monk340b0e52017-03-08 14:57:56 -050072import org.junit.runner.RunWith;
Julia Reynolds8ceb5792017-04-11 11:32:44 -040073import org.mockito.ArgumentCaptor;
Rohan Shah524cf7b2018-03-15 14:40:02 -070074import org.mockito.Mock;
75import org.mockito.junit.MockitoJUnit;
76import org.mockito.junit.MockitoRule;
Julia Reynolds3aedded2017-03-31 14:42:09 -040077
78import java.util.ArrayList;
Geoffrey Pitschd0856f02017-02-16 10:51:18 -050079import java.util.List;
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050080import java.util.concurrent.CountDownLatch;
81
82@SmallTest
Jason Monk340b0e52017-03-08 14:57:56 -050083@RunWith(AndroidTestingRunner.class)
Jason Monkba364322017-03-06 11:19:20 -050084@UiThreadTest
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050085public class NotificationInfoTest extends SysuiTestCase {
86 private static final String TEST_PACKAGE_NAME = "test_package";
Julia Reynoldsf7321592017-05-24 16:09:19 -040087 private static final String TEST_SYSTEM_PACKAGE_NAME = PRINT_SPOOLER_PACKAGE_NAME;
Geoffrey Pitschd034d292017-05-12 11:59:20 -040088 private static final int TEST_UID = 1;
Rohan Shah63411fc2018-03-28 19:05:52 -070089 private static final int MULTIPLE_CHANNEL_COUNT = 2;
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050090 private static final String TEST_CHANNEL = "test_channel";
91 private static final String TEST_CHANNEL_NAME = "TEST CHANNEL NAME";
92
93 private NotificationInfo mNotificationInfo;
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050094 private NotificationChannel mNotificationChannel;
Geoffrey Pitschd0856f02017-02-16 10:51:18 -050095 private NotificationChannel mDefaultNotificationChannel;
Julia Reynolds3aedded2017-03-31 14:42:09 -040096 private StatusBarNotification mSbn;
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050097
Rohan Shah524cf7b2018-03-15 14:40:02 -070098 @Rule public MockitoRule mockito = MockitoJUnit.rule();
99 @Mock private INotificationManager mMockINotificationManager;
100 @Mock private PackageManager mMockPackageManager;
101 @Mock private NotificationBlockingHelperManager mBlockingHelperManager;
102
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500103 @Before
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500104 public void setUp() throws Exception {
Rohan Shah524cf7b2018-03-15 14:40:02 -0700105 mDependency.injectTestDependency(
106 NotificationBlockingHelperManager.class,
107 mBlockingHelperManager);
108
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500109 // Inflate the layout
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500110 final LayoutInflater layoutInflater = LayoutInflater.from(mContext);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500111 mNotificationInfo = (NotificationInfo) layoutInflater.inflate(R.layout.notification_info,
112 null);
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500113 mNotificationInfo.setGutsParent(mock(NotificationGuts.class));
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500114
115 // PackageManager must return a packageInfo and applicationInfo.
116 final PackageInfo packageInfo = new PackageInfo();
117 packageInfo.packageName = TEST_PACKAGE_NAME;
Julia Reynoldsf7321592017-05-24 16:09:19 -0400118 when(mMockPackageManager.getPackageInfo(eq(TEST_PACKAGE_NAME), anyInt()))
119 .thenReturn(packageInfo);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500120 final ApplicationInfo applicationInfo = new ApplicationInfo();
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400121 applicationInfo.uid = TEST_UID; // non-zero
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500122 when(mMockPackageManager.getApplicationInfo(anyString(), anyInt())).thenReturn(
123 applicationInfo);
Julia Reynoldsf7321592017-05-24 16:09:19 -0400124 final PackageInfo systemPackageInfo = new PackageInfo();
125 systemPackageInfo.packageName = TEST_SYSTEM_PACKAGE_NAME;
126 when(mMockPackageManager.getPackageInfo(eq(TEST_SYSTEM_PACKAGE_NAME), anyInt()))
127 .thenReturn(systemPackageInfo);
128 when(mMockPackageManager.getPackageInfo(eq("android"), anyInt()))
129 .thenReturn(packageInfo);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500130
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500131 // Package has one channel by default.
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500132 when(mMockINotificationManager.getNumNotificationChannelsForPackage(
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400133 eq(TEST_PACKAGE_NAME), eq(TEST_UID), anyBoolean())).thenReturn(1);
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500134
135 // Some test channels.
136 mNotificationChannel = new NotificationChannel(
Julia Reynolds437cdb12018-01-03 12:27:24 -0500137 TEST_CHANNEL, TEST_CHANNEL_NAME, IMPORTANCE_LOW);
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500138 mDefaultNotificationChannel = new NotificationChannel(
139 NotificationChannel.DEFAULT_CHANNEL_ID, TEST_CHANNEL_NAME,
Julia Reynolds437cdb12018-01-03 12:27:24 -0500140 IMPORTANCE_LOW);
Julia Reynolds33bef2c2017-09-05 11:07:18 -0400141 mSbn = new StatusBarNotification(TEST_PACKAGE_NAME, TEST_PACKAGE_NAME, 0, null, TEST_UID, 0,
Julia Reynolds3aedded2017-03-31 14:42:09 -0400142 new Notification(), UserHandle.CURRENT, null, 0);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500143 }
144
Julia Reynolds437cdb12018-01-03 12:27:24 -0500145 // TODO: if tests are taking too long replace this with something that makes the animation
146 // finish instantly.
147 private void waitForUndoButton() {
148 PollingCheck.waitFor(1000,
149 () -> VISIBLE == mNotificationInfo.findViewById(R.id.confirmation).getVisibility());
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500150 }
Julia Reynolds437cdb12018-01-03 12:27:24 -0500151 private void waitForStopButton() {
152 PollingCheck.waitFor(1000,
153 () -> VISIBLE == mNotificationInfo.findViewById(R.id.prompt).getVisibility());
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500154 }
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,
Rohan Shah63411fc2018-03-28 19:05:52 -0700160 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, false);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500161 final TextView textView = mNotificationInfo.findViewById(R.id.pkgname);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500162 assertTrue(textView.getText().toString().contains("App Name"));
Julia Reynoldse0341482018-03-08 14:42:50 -0500163 assertEquals(VISIBLE, mNotificationInfo.findViewById(R.id.header).getVisibility());
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500164 }
165
166 @Test
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500167 public void testBindNotification_SetsPackageIcon() throws Exception {
168 final Drawable iconDrawable = mock(Drawable.class);
169 when(mMockPackageManager.getApplicationIcon(any(ApplicationInfo.class)))
170 .thenReturn(iconDrawable);
171 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Rohan Shah63411fc2018-03-28 19:05:52 -0700172 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, false);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500173 final ImageView iconView = mNotificationInfo.findViewById(R.id.pkgicon);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500174 assertEquals(iconDrawable, iconView.getDrawable());
175 }
176
177 @Test
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500178 public void testBindNotification_GroupNameHiddenIfNoGroup() throws Exception {
179 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Rohan Shah63411fc2018-03-28 19:05:52 -0700180 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, false);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500181 final TextView groupNameView = mNotificationInfo.findViewById(R.id.group_name);
182 assertEquals(GONE, groupNameView.getVisibility());
183 final TextView groupDividerView = mNotificationInfo.findViewById(R.id.pkg_group_divider);
184 assertEquals(GONE, groupDividerView.getVisibility());
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500185 }
186
187 @Test
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500188 public void testBindNotification_SetsGroupNameIfNonNull() throws Exception {
189 mNotificationChannel.setGroup("test_group_id");
190 final NotificationChannelGroup notificationChannelGroup =
191 new NotificationChannelGroup("test_group_id", "Test Group Name");
192 when(mMockINotificationManager.getNotificationChannelGroupForPackage(
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400193 eq("test_group_id"), eq(TEST_PACKAGE_NAME), eq(TEST_UID)))
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500194 .thenReturn(notificationChannelGroup);
195 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Rohan Shah63411fc2018-03-28 19:05:52 -0700196 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, false);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500197 final TextView groupNameView = mNotificationInfo.findViewById(R.id.group_name);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500198 assertEquals(View.VISIBLE, groupNameView.getVisibility());
199 assertEquals("Test Group Name", groupNameView.getText());
Julia Reynolds437cdb12018-01-03 12:27:24 -0500200 final TextView groupDividerView = mNotificationInfo.findViewById(R.id.pkg_group_divider);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500201 assertEquals(View.VISIBLE, groupDividerView.getVisibility());
202 }
203
204 @Test
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500205 public void testBindNotification_SetsTextChannelName() throws Exception {
206 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Rohan Shah63411fc2018-03-28 19:05:52 -0700207 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, false);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500208 final TextView textView = mNotificationInfo.findViewById(R.id.channel_name);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500209 assertEquals(TEST_CHANNEL_NAME, textView.getText());
210 }
211
212 @Test
Geoffrey Pitsch1fbf66e2017-05-09 11:32:27 -0400213 public void testBindNotification_DefaultChannelDoesNotUseChannelName() throws Exception {
214 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Rohan Shah63411fc2018-03-28 19:05:52 -0700215 TEST_PACKAGE_NAME, mDefaultNotificationChannel, 1, mSbn, null, null, null, false);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500216 final TextView textView = mNotificationInfo.findViewById(R.id.channel_name);
217 assertEquals(GONE, textView.getVisibility());
Geoffrey Pitsch1fbf66e2017-05-09 11:32:27 -0400218 }
219
220 @Test
Rohan Shahdbd64e72018-03-28 14:46:50 -0700221 public void testBindNotification_DefaultChannelUsesChannelNameIfMoreChannelsExist()
222 throws Exception {
223 // Package has one channel by default.
224 when(mMockINotificationManager.getNumNotificationChannelsForPackage(
225 eq(TEST_PACKAGE_NAME), eq(TEST_UID), anyBoolean())).thenReturn(10);
226 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Rohan Shah63411fc2018-03-28 19:05:52 -0700227 TEST_PACKAGE_NAME, mDefaultNotificationChannel, 1, mSbn, null, null, null, false);
Rohan Shahdbd64e72018-03-28 14:46:50 -0700228 final TextView textView = mNotificationInfo.findViewById(R.id.channel_name);
229 assertEquals(VISIBLE, textView.getVisibility());
230 }
231
232 @Test
Julia Reynolds437cdb12018-01-03 12:27:24 -0500233 public void testBindNotification_UnblockablePackageUsesChannelName() throws Exception {
Geoffrey Pitschb43486a2017-06-01 13:45:59 -0400234 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Rohan Shah63411fc2018-03-28 19:05:52 -0700235 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500236 final TextView textView = mNotificationInfo.findViewById(R.id.channel_name);
237 assertEquals(VISIBLE, textView.getVisibility());
Geoffrey Pitsch1fbf66e2017-05-09 11:32:27 -0400238 }
239
240 @Test
Julia Reynoldse0341482018-03-08 14:42:50 -0500241 public void testBindNotification_BlockButton() throws Exception {
242 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Rohan Shah63411fc2018-03-28 19:05:52 -0700243 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, false);
Julia Reynoldse0341482018-03-08 14:42:50 -0500244 final View block = mNotificationInfo.findViewById(R.id.block);
245 final View minimize = mNotificationInfo.findViewById(R.id.minimize);
246 assertEquals(VISIBLE, block.getVisibility());
247 assertEquals(GONE, minimize.getVisibility());
248 }
249
250 @Test
251 public void testBindNotification_MinButton() throws Exception {
252 mSbn.getNotification().flags = Notification.FLAG_FOREGROUND_SERVICE;
253 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Rohan Shah63411fc2018-03-28 19:05:52 -0700254 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, false);
Julia Reynoldse0341482018-03-08 14:42:50 -0500255 final View block = mNotificationInfo.findViewById(R.id.block);
256 final View minimize = mNotificationInfo.findViewById(R.id.minimize);
257 assertEquals(GONE, block.getVisibility());
258 assertEquals(VISIBLE, minimize.getVisibility());
259 }
260
261 @Test
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500262 public void testBindNotification_SetsOnClickListenerForSettings() throws Exception {
263 final CountDownLatch latch = new CountDownLatch(1);
264 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds437cdb12018-01-03 12:27:24 -0500265 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null,
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500266 (View v, NotificationChannel c, int appUid) -> {
Julia Reynolds3aedded2017-03-31 14:42:09 -0400267 assertEquals(mNotificationChannel, c);
268 latch.countDown();
Rohan Shah63411fc2018-03-28 19:05:52 -0700269 }, null, false);
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500270
Julia Reynolds437cdb12018-01-03 12:27:24 -0500271 final View settingsButton = mNotificationInfo.findViewById(R.id.info);
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500272 settingsButton.performClick();
273 // Verify that listener was triggered.
274 assertEquals(0, latch.getCount());
275 }
276
277 @Test
Geoffrey Pitsch029a3fa2017-03-30 13:01:57 -0400278 public void testBindNotification_SettingsButtonInvisibleWhenNoClickListener() throws Exception {
279 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Rohan Shah63411fc2018-03-28 19:05:52 -0700280 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, false);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500281 final View settingsButton = mNotificationInfo.findViewById(R.id.info);
Geoffrey Pitsch029a3fa2017-03-30 13:01:57 -0400282 assertTrue(settingsButton.getVisibility() != View.VISIBLE);
283 }
284
285 @Test
Julia Reynolds437cdb12018-01-03 12:27:24 -0500286 public void testBindNotification_SettingsButtonReappearsAfterSecondBind() throws Exception {
Geoffrey Pitsch029a3fa2017-03-30 13:01:57 -0400287 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Rohan Shah63411fc2018-03-28 19:05:52 -0700288 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, false);
Geoffrey Pitsch029a3fa2017-03-30 13:01:57 -0400289 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds437cdb12018-01-03 12:27:24 -0500290 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null,
291 (View v, NotificationChannel c, int appUid) -> {
Rohan Shah63411fc2018-03-28 19:05:52 -0700292 }, null, false);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500293 final View settingsButton = mNotificationInfo.findViewById(R.id.info);
Geoffrey Pitsch029a3fa2017-03-30 13:01:57 -0400294 assertEquals(View.VISIBLE, settingsButton.getVisibility());
295 }
296
297 @Test
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500298 public void testOnClickListenerPassesNullChannelForBundle() throws Exception {
299 final CountDownLatch latch = new CountDownLatch(1);
300 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Rohan Shah63411fc2018-03-28 19:05:52 -0700301 TEST_PACKAGE_NAME, mNotificationChannel, MULTIPLE_CHANNEL_COUNT, mSbn, null,
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500302 (View v, NotificationChannel c, int appUid) -> {
Julia Reynolds3aedded2017-03-31 14:42:09 -0400303 assertEquals(null, c);
304 latch.countDown();
Rohan Shah63411fc2018-03-28 19:05:52 -0700305 }, null, true);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500306
Julia Reynolds437cdb12018-01-03 12:27:24 -0500307 mNotificationInfo.findViewById(R.id.info).performClick();
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500308 // Verify that listener was triggered.
309 assertEquals(0, latch.getCount());
310 }
311
312 @Test
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500313 @UiThreadTest
Julia Reynolds437cdb12018-01-03 12:27:24 -0500314 public void testBindNotification_ChannelNameInvisibleWhenBundleFromDifferentChannels()
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500315 throws Exception {
316 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Rohan Shah63411fc2018-03-28 19:05:52 -0700317 TEST_PACKAGE_NAME, mNotificationChannel, MULTIPLE_CHANNEL_COUNT, mSbn, null, null,
318 null, true);
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500319 final TextView channelNameView =
Julia Reynolds437cdb12018-01-03 12:27:24 -0500320 mNotificationInfo.findViewById(R.id.channel_name);
321 assertEquals(GONE, channelNameView.getVisibility());
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500322 }
323
324 @Test
325 @UiThreadTest
Julia Reynolds437cdb12018-01-03 12:27:24 -0500326 public void testStopInvisibleIfBundleFromDifferentChannels() throws Exception {
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500327 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Rohan Shah63411fc2018-03-28 19:05:52 -0700328 TEST_PACKAGE_NAME, mNotificationChannel, MULTIPLE_CHANNEL_COUNT, mSbn, null, null,
329 null, true);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500330 final TextView blockView = mNotificationInfo.findViewById(R.id.block);
331 assertEquals(GONE, blockView.getVisibility());
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500332 }
333
334 @Test
Julia Reynolds0ef7d842018-01-24 17:50:31 -0500335 public void testbindNotification_BlockingHelper() throws Exception {
336 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Rohan Shah63411fc2018-03-28 19:05:52 -0700337 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, false, false,
338 true);
Julia Reynolds0ef7d842018-01-24 17:50:31 -0500339 final TextView view = mNotificationInfo.findViewById(R.id.block_prompt);
340 assertEquals(View.VISIBLE, view.getVisibility());
341 assertEquals(mContext.getString(R.string.inline_blocking_helper), view.getText());
342 }
343
344 @Test
Geoffrey Pitschb43486a2017-06-01 13:45:59 -0400345 public void testbindNotification_UnblockableTextVisibleWhenAppUnblockable() throws Exception {
346 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Rohan Shah63411fc2018-03-28 19:05:52 -0700347 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500348 final TextView view = mNotificationInfo.findViewById(R.id.block_prompt);
349 assertEquals(View.VISIBLE, view.getVisibility());
Geoffrey Pitschb43486a2017-06-01 13:45:59 -0400350 assertEquals(mContext.getString(R.string.notification_unblockable_desc),
Julia Reynolds437cdb12018-01-03 12:27:24 -0500351 view.getText());
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500352 }
353
354 @Test
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500355 public void testBindNotification_DoesNotUpdateNotificationChannel() throws Exception {
356 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Rohan Shah63411fc2018-03-28 19:05:52 -0700357 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, false);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500358 verify(mMockINotificationManager, never()).updateNotificationChannelForPackage(
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400359 anyString(), eq(TEST_UID), any());
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500360 }
361
362 @Test
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500363 public void testDoesNotUpdateNotificationChannelAfterImportanceChanged() throws Exception {
Julia Reynolds437cdb12018-01-03 12:27:24 -0500364 mNotificationChannel.setImportance(IMPORTANCE_LOW);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500365 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Rohan Shah63411fc2018-03-28 19:05:52 -0700366 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, false);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500367
Julia Reynolds437cdb12018-01-03 12:27:24 -0500368 mNotificationInfo.findViewById(R.id.block).performClick();
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500369 verify(mMockINotificationManager, never()).updateNotificationChannelForPackage(
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400370 anyString(), eq(TEST_UID), any());
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500371 }
372
373 @Test
Julia Reynoldse0341482018-03-08 14:42:50 -0500374 public void testDoesNotUpdateNotificationChannelAfterImportanceChangedMin()
375 throws Exception {
376 mNotificationChannel.setImportance(IMPORTANCE_LOW);
377 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Rohan Shah63411fc2018-03-28 19:05:52 -0700378 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, false);
Julia Reynoldse0341482018-03-08 14:42:50 -0500379
380 mNotificationInfo.findViewById(R.id.minimize).performClick();
381 verify(mMockINotificationManager, never()).updateNotificationChannelForPackage(
382 anyString(), eq(TEST_UID), any());
383 }
384
385 @Test
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500386 public void testHandleCloseControls_DoesNotUpdateNotificationChannelIfUnchanged()
387 throws Exception {
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500388 int originalImportance = mNotificationChannel.getImportance();
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500389 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Rohan Shah63411fc2018-03-28 19:05:52 -0700390 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, false);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500391
Mady Mellorc2dbe492017-03-30 13:22:03 -0700392 mNotificationInfo.handleCloseControls(true, false);
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500393 verify(mMockINotificationManager, times(1)).updateNotificationChannelForPackage(
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400394 anyString(), eq(TEST_UID), any());
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500395 assertEquals(originalImportance, mNotificationChannel.getImportance());
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500396 }
397
398 @Test
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500399 public void testHandleCloseControls_DoesNotUpdateNotificationChannelIfUnspecified()
400 throws Exception {
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500401 mNotificationChannel.setImportance(IMPORTANCE_UNSPECIFIED);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500402 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Rohan Shah63411fc2018-03-28 19:05:52 -0700403 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, false);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500404
Mady Mellorc2dbe492017-03-30 13:22:03 -0700405 mNotificationInfo.handleCloseControls(true, false);
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500406 verify(mMockINotificationManager, times(1)).updateNotificationChannelForPackage(
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400407 anyString(), eq(TEST_UID), any());
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500408 assertEquals(IMPORTANCE_UNSPECIFIED, mNotificationChannel.getImportance());
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500409 }
410
411 @Test
Rohan Shah524cf7b2018-03-15 14:40:02 -0700412 public void testCloseControls_blockingHelperDismissedIfShown() throws Exception {
413 mNotificationInfo.bindNotification(
414 mMockPackageManager,
415 mMockINotificationManager,
416 TEST_PACKAGE_NAME,
417 mNotificationChannel,
418 1 /* numChannels */,
419 mSbn,
420 null /* checkSaveListener */,
421 null /* onSettingsClick */,
422 null /* onAppSettingsClick */,
Rohan Shah63411fc2018-03-28 19:05:52 -0700423 false /* isNonblockable */,
Rohan Shah524cf7b2018-03-15 14:40:02 -0700424 true /* isForBlockingHelper */,
425 false /* isUserSentimentNegative */);
426
427 mNotificationInfo.closeControls(mNotificationInfo);
428
429 verify(mBlockingHelperManager).dismissCurrentBlockingHelper();
430 }
431
432 @Test
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500433 public void testNonBlockableAppDoesNotBecomeBlocked() throws Exception {
Julia Reynolds437cdb12018-01-03 12:27:24 -0500434 mNotificationChannel.setImportance(IMPORTANCE_LOW);
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500435 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Rohan Shah63411fc2018-03-28 19:05:52 -0700436 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500437 mNotificationInfo.findViewById(R.id.block).performClick();
438 waitForUndoButton();
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500439 verify(mMockINotificationManager, never()).updateNotificationChannelForPackage(
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400440 anyString(), eq(TEST_UID), any());
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500441 }
442
443 @Test
Julia Reynolds437cdb12018-01-03 12:27:24 -0500444 public void testBlockChangedCallsUpdateNotificationChannel() throws Exception {
445 mNotificationChannel.setImportance(IMPORTANCE_LOW);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500446 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Rohan Shah63411fc2018-03-28 19:05:52 -0700447 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, false);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500448
Julia Reynolds437cdb12018-01-03 12:27:24 -0500449 mNotificationInfo.findViewById(R.id.block).performClick();
450 waitForUndoButton();
Mady Mellorc2dbe492017-03-30 13:22:03 -0700451 mNotificationInfo.handleCloseControls(true, false);
Julia Reynolds8ceb5792017-04-11 11:32:44 -0400452
453 ArgumentCaptor<NotificationChannel> updated =
454 ArgumentCaptor.forClass(NotificationChannel.class);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500455 verify(mMockINotificationManager, times(1)).updateNotificationChannelForPackage(
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400456 anyString(), eq(TEST_UID), updated.capture());
Julia Reynolds8ceb5792017-04-11 11:32:44 -0400457 assertTrue((updated.getValue().getUserLockedFields()
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500458 & USER_LOCKED_IMPORTANCE) != 0);
Julia Reynoldse0341482018-03-08 14:42:50 -0500459 assertEquals(IMPORTANCE_NONE, updated.getValue().getImportance());
460 }
461
462 @Test
463 public void testNonBlockableAppDoesNotBecomeMin() throws Exception {
464 mNotificationChannel.setImportance(IMPORTANCE_LOW);
465 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Rohan Shah63411fc2018-03-28 19:05:52 -0700466 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true);
Julia Reynoldse0341482018-03-08 14:42:50 -0500467 mNotificationInfo.findViewById(R.id.minimize).performClick();
468 waitForUndoButton();
469 verify(mMockINotificationManager, never()).updateNotificationChannelForPackage(
470 anyString(), eq(TEST_UID), any());
471 }
472
473 @Test
474 public void testMinChangedCallsUpdateNotificationChannel() throws Exception {
475 mNotificationChannel.setImportance(IMPORTANCE_LOW);
476 mSbn.getNotification().flags = Notification.FLAG_FOREGROUND_SERVICE;
477 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Rohan Shah63411fc2018-03-28 19:05:52 -0700478 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, false);
Julia Reynoldse0341482018-03-08 14:42:50 -0500479
480 mNotificationInfo.findViewById(R.id.minimize).performClick();
481 waitForUndoButton();
482 mNotificationInfo.handleCloseControls(true, false);
483
484 ArgumentCaptor<NotificationChannel> updated =
485 ArgumentCaptor.forClass(NotificationChannel.class);
486 verify(mMockINotificationManager, times(1)).updateNotificationChannelForPackage(
487 anyString(), eq(TEST_UID), updated.capture());
488 assertTrue((updated.getValue().getUserLockedFields()
489 & USER_LOCKED_IMPORTANCE) != 0);
490 assertEquals(IMPORTANCE_MIN, updated.getValue().getImportance());
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500491 }
492
493 @Test
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500494 public void testKeepUpdatesNotificationChannel() throws Exception {
Julia Reynolds437cdb12018-01-03 12:27:24 -0500495 mNotificationChannel.setImportance(IMPORTANCE_LOW);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500496 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Rohan Shah63411fc2018-03-28 19:05:52 -0700497 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, false);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500498
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500499 mNotificationInfo.handleCloseControls(true, false);
500
501 ArgumentCaptor<NotificationChannel> updated =
502 ArgumentCaptor.forClass(NotificationChannel.class);
503 verify(mMockINotificationManager, times(1)).updateNotificationChannelForPackage(
504 anyString(), eq(TEST_UID), updated.capture());
505 assertTrue(0 != (mNotificationChannel.getUserLockedFields() & USER_LOCKED_IMPORTANCE));
506 assertEquals(IMPORTANCE_LOW, mNotificationChannel.getImportance());
507 }
508
509 @Test
510 public void testBlockUndoDoesNotBlockNotificationChannel() throws Exception {
511 mNotificationChannel.setImportance(IMPORTANCE_LOW);
512 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Rohan Shah63411fc2018-03-28 19:05:52 -0700513 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, false);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500514
515 mNotificationInfo.findViewById(R.id.block).performClick();
516 waitForUndoButton();
517 mNotificationInfo.findViewById(R.id.undo).performClick();
518 waitForStopButton();
519 mNotificationInfo.handleCloseControls(true, false);
520
521 ArgumentCaptor<NotificationChannel> updated =
522 ArgumentCaptor.forClass(NotificationChannel.class);
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500523 verify(mMockINotificationManager, times(1)).updateNotificationChannelForPackage(
Julia Reynolds437cdb12018-01-03 12:27:24 -0500524 anyString(), eq(TEST_UID), updated.capture());
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500525 assertTrue(0 != (mNotificationChannel.getUserLockedFields() & USER_LOCKED_IMPORTANCE));
Julia Reynolds437cdb12018-01-03 12:27:24 -0500526 assertEquals(IMPORTANCE_LOW, mNotificationChannel.getImportance());
527 }
528
529 @Test
Julia Reynoldse0341482018-03-08 14:42:50 -0500530 public void testMinUndoDoesNotMinNotificationChannel() throws Exception {
531 mNotificationChannel.setImportance(IMPORTANCE_LOW);
532 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Rohan Shah63411fc2018-03-28 19:05:52 -0700533 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, false);
Julia Reynoldse0341482018-03-08 14:42:50 -0500534
535 mNotificationInfo.findViewById(R.id.minimize).performClick();
536 waitForUndoButton();
537 mNotificationInfo.findViewById(R.id.undo).performClick();
538 waitForStopButton();
539 mNotificationInfo.handleCloseControls(true, false);
540
541 ArgumentCaptor<NotificationChannel> updated =
542 ArgumentCaptor.forClass(NotificationChannel.class);
543 verify(mMockINotificationManager, times(1)).updateNotificationChannelForPackage(
544 anyString(), eq(TEST_UID), updated.capture());
545 assertTrue(0 != (mNotificationChannel.getUserLockedFields() & USER_LOCKED_IMPORTANCE));
546 assertEquals(IMPORTANCE_LOW, mNotificationChannel.getImportance());
547 }
548
549 @Test
550 public void testCloseControlsDoesNotUpdateiMinIfSaveIsFalse() throws Exception {
551 mNotificationChannel.setImportance(IMPORTANCE_LOW);
552 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Rohan Shah63411fc2018-03-28 19:05:52 -0700553 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true);
Julia Reynoldse0341482018-03-08 14:42:50 -0500554
555 mNotificationInfo.findViewById(R.id.minimize).performClick();
556 waitForUndoButton();
557 mNotificationInfo.handleCloseControls(false, false);
558 verify(mMockINotificationManager, never()).updateNotificationChannelForPackage(
559 eq(TEST_PACKAGE_NAME), eq(TEST_UID), eq(mNotificationChannel));
560 }
561
562 @Test
Julia Reynolds437cdb12018-01-03 12:27:24 -0500563 public void testCloseControlsDoesNotUpdateIfSaveIsFalse() throws Exception {
564 mNotificationChannel.setImportance(IMPORTANCE_LOW);
565 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Rohan Shah63411fc2018-03-28 19:05:52 -0700566 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500567
568 mNotificationInfo.findViewById(R.id.block).performClick();
569 waitForUndoButton();
Mady Mellorc2dbe492017-03-30 13:22:03 -0700570 mNotificationInfo.handleCloseControls(false, false);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500571 verify(mMockINotificationManager, never()).updateNotificationChannelForPackage(
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400572 eq(TEST_PACKAGE_NAME), eq(TEST_UID), eq(mNotificationChannel));
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500573 }
Geoffrey Pitsch5278d3d2017-03-29 13:39:10 -0400574
575 @Test
576 public void testCloseControlsDoesNotUpdateIfCheckSaveListenerIsNoOp() throws Exception {
Julia Reynolds437cdb12018-01-03 12:27:24 -0500577 mNotificationChannel.setImportance(IMPORTANCE_LOW);
Geoffrey Pitsch5278d3d2017-03-29 13:39:10 -0400578 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds437cdb12018-01-03 12:27:24 -0500579 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn,
Eliot Courtney47098cb2017-10-18 17:30:30 +0900580 (Runnable saveImportance, StatusBarNotification sbn) -> {
Rohan Shah63411fc2018-03-28 19:05:52 -0700581 }, null, null, true);
Geoffrey Pitsch5278d3d2017-03-29 13:39:10 -0400582
Julia Reynolds437cdb12018-01-03 12:27:24 -0500583 mNotificationInfo.findViewById(R.id.block).performClick();
584 waitForUndoButton();
Mady Mellorc2dbe492017-03-30 13:22:03 -0700585 mNotificationInfo.handleCloseControls(true, false);
Geoffrey Pitsch5278d3d2017-03-29 13:39:10 -0400586 verify(mMockINotificationManager, never()).updateNotificationChannelForPackage(
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400587 eq(TEST_PACKAGE_NAME), eq(TEST_UID), eq(mNotificationChannel));
Geoffrey Pitsch5278d3d2017-03-29 13:39:10 -0400588 }
589
590 @Test
591 public void testCloseControlsUpdatesWhenCheckSaveListenerUsesCallback() throws Exception {
Julia Reynolds437cdb12018-01-03 12:27:24 -0500592 mNotificationChannel.setImportance(IMPORTANCE_LOW);
Geoffrey Pitsch5278d3d2017-03-29 13:39:10 -0400593 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds437cdb12018-01-03 12:27:24 -0500594 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn,
Eliot Courtney47098cb2017-10-18 17:30:30 +0900595 (Runnable saveImportance, StatusBarNotification sbn) -> {
Julia Reynolds3aedded2017-03-31 14:42:09 -0400596 saveImportance.run();
Rohan Shah63411fc2018-03-28 19:05:52 -0700597 }, null, null, false);
Geoffrey Pitsch5278d3d2017-03-29 13:39:10 -0400598
Julia Reynolds437cdb12018-01-03 12:27:24 -0500599 mNotificationInfo.findViewById(R.id.block).performClick();
600 waitForUndoButton();
Mady Mellorc2dbe492017-03-30 13:22:03 -0700601 mNotificationInfo.handleCloseControls(true, false);
Geoffrey Pitsch5278d3d2017-03-29 13:39:10 -0400602 verify(mMockINotificationManager, times(1)).updateNotificationChannelForPackage(
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400603 eq(TEST_PACKAGE_NAME), eq(TEST_UID), eq(mNotificationChannel));
Geoffrey Pitsch5278d3d2017-03-29 13:39:10 -0400604 }
Julia Reynolds3aedded2017-03-31 14:42:09 -0400605
606 @Test
607 public void testDisplaySettingsLink() throws Exception {
608 final CountDownLatch latch = new CountDownLatch(1);
609 final String settingsText = "work chats";
610 final ResolveInfo ri = new ResolveInfo();
611 ri.activityInfo = new ActivityInfo();
612 ri.activityInfo.packageName = TEST_PACKAGE_NAME;
613 ri.activityInfo.name = "something";
614 List<ResolveInfo> ris = new ArrayList<>();
615 ris.add(ri);
616 when(mMockPackageManager.queryIntentActivities(any(), anyInt())).thenReturn(ris);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500617 mNotificationChannel.setImportance(IMPORTANCE_LOW);
Julia Reynolds3aedded2017-03-31 14:42:09 -0400618 Notification n = new Notification.Builder(mContext, mNotificationChannel.getId())
619 .setSettingsText(settingsText).build();
620 StatusBarNotification sbn = new StatusBarNotification(TEST_PACKAGE_NAME, TEST_PACKAGE_NAME,
621 0, null, 0, 0, n, UserHandle.CURRENT, null, 0);
622
623 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Julia Reynolds437cdb12018-01-03 12:27:24 -0500624 TEST_PACKAGE_NAME, mNotificationChannel, 1, sbn, null, null,
Julia Reynolds3aedded2017-03-31 14:42:09 -0400625 (View v, Intent intent) -> {
626 latch.countDown();
Rohan Shah63411fc2018-03-28 19:05:52 -0700627 }, false);
Julia Reynolds3aedded2017-03-31 14:42:09 -0400628 final TextView settingsLink = mNotificationInfo.findViewById(R.id.app_settings);
629 assertEquals(View.VISIBLE, settingsLink.getVisibility());
Julia Reynolds3aedded2017-03-31 14:42:09 -0400630 settingsLink.performClick();
631 assertEquals(0, latch.getCount());
632 }
633
634 @Test
635 public void testDisplaySettingsLink_multipleChannels() throws Exception {
636 final CountDownLatch latch = new CountDownLatch(1);
637 final String settingsText = "work chats";
638 final ResolveInfo ri = new ResolveInfo();
639 ri.activityInfo = new ActivityInfo();
640 ri.activityInfo.packageName = TEST_PACKAGE_NAME;
641 ri.activityInfo.name = "something";
642 List<ResolveInfo> ris = new ArrayList<>();
643 ris.add(ri);
644 when(mMockPackageManager.queryIntentActivities(any(), anyInt())).thenReturn(ris);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500645 mNotificationChannel.setImportance(IMPORTANCE_LOW);
Julia Reynolds3aedded2017-03-31 14:42:09 -0400646 Notification n = new Notification.Builder(mContext, mNotificationChannel.getId())
647 .setSettingsText(settingsText).build();
648 StatusBarNotification sbn = new StatusBarNotification(TEST_PACKAGE_NAME, TEST_PACKAGE_NAME,
649 0, null, 0, 0, n, UserHandle.CURRENT, null, 0);
650
651 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Rohan Shah63411fc2018-03-28 19:05:52 -0700652 TEST_PACKAGE_NAME, mNotificationChannel, MULTIPLE_CHANNEL_COUNT, sbn, null, null,
Julia Reynolds437cdb12018-01-03 12:27:24 -0500653 (View v, Intent intent) -> {
Julia Reynolds3aedded2017-03-31 14:42:09 -0400654 latch.countDown();
Rohan Shah63411fc2018-03-28 19:05:52 -0700655 }, false);
Julia Reynolds3aedded2017-03-31 14:42:09 -0400656 final TextView settingsLink = mNotificationInfo.findViewById(R.id.app_settings);
657 assertEquals(View.VISIBLE, settingsLink.getVisibility());
658 settingsLink.performClick();
659 assertEquals(0, latch.getCount());
660 }
661
662 @Test
663 public void testNoSettingsLink_noHandlingActivity() throws Exception {
664 final String settingsText = "work chats";
665 when(mMockPackageManager.queryIntentActivities(any(), anyInt())).thenReturn(null);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500666 mNotificationChannel.setImportance(IMPORTANCE_LOW);
Julia Reynolds3aedded2017-03-31 14:42:09 -0400667 Notification n = new Notification.Builder(mContext, mNotificationChannel.getId())
668 .setSettingsText(settingsText).build();
669 StatusBarNotification sbn = new StatusBarNotification(TEST_PACKAGE_NAME, TEST_PACKAGE_NAME,
670 0, null, 0, 0, n, UserHandle.CURRENT, null, 0);
671
672 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Rohan Shah63411fc2018-03-28 19:05:52 -0700673 TEST_PACKAGE_NAME, mNotificationChannel, MULTIPLE_CHANNEL_COUNT, sbn, null, null,
674 null, false);
Julia Reynolds3aedded2017-03-31 14:42:09 -0400675 final TextView settingsLink = mNotificationInfo.findViewById(R.id.app_settings);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500676 assertEquals(GONE, settingsLink.getVisibility());
Julia Reynolds3aedded2017-03-31 14:42:09 -0400677 }
678
679 @Test
680 public void testNoSettingsLink_noLinkText() throws Exception {
681 final ResolveInfo ri = new ResolveInfo();
682 ri.activityInfo = new ActivityInfo();
683 ri.activityInfo.packageName = TEST_PACKAGE_NAME;
684 ri.activityInfo.name = "something";
685 List<ResolveInfo> ris = new ArrayList<>();
686 ris.add(ri);
687 when(mMockPackageManager.queryIntentActivities(any(), anyInt())).thenReturn(ris);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500688 mNotificationChannel.setImportance(IMPORTANCE_LOW);
Julia Reynolds3aedded2017-03-31 14:42:09 -0400689 Notification n = new Notification.Builder(mContext, mNotificationChannel.getId()).build();
690 StatusBarNotification sbn = new StatusBarNotification(TEST_PACKAGE_NAME, TEST_PACKAGE_NAME,
691 0, null, 0, 0, n, UserHandle.CURRENT, null, 0);
692
693 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Rohan Shah63411fc2018-03-28 19:05:52 -0700694 TEST_PACKAGE_NAME, mNotificationChannel, 1, sbn, null, null, null, false);
Julia Reynolds3aedded2017-03-31 14:42:09 -0400695 final TextView settingsLink = mNotificationInfo.findViewById(R.id.app_settings);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500696 assertEquals(GONE, settingsLink.getVisibility());
Julia Reynolds3aedded2017-03-31 14:42:09 -0400697 }
698
Rohan Shah7c6b37a2018-03-30 21:07:45 +0000699 @Test
700 public void testBindHeader_noSettingsLinkWhenIsForBlockingHelper() throws Exception {
701 final String settingsText = "work chats";
702 final ResolveInfo ri = new ResolveInfo();
703 ri.activityInfo = new ActivityInfo();
704 ri.activityInfo.packageName = TEST_PACKAGE_NAME;
705 ri.activityInfo.name = "something";
706 List<ResolveInfo> ris = new ArrayList<>();
707 ris.add(ri);
708 when(mMockPackageManager.queryIntentActivities(any(), anyInt())).thenReturn(ris);
709 mNotificationChannel.setImportance(IMPORTANCE_LOW);
710 Notification n = new Notification.Builder(mContext, mNotificationChannel.getId())
711 .setSettingsText(settingsText).build();
712 StatusBarNotification sbn = new StatusBarNotification(TEST_PACKAGE_NAME, TEST_PACKAGE_NAME,
713 0, null, 0, 0, n, UserHandle.CURRENT, null, 0);
714
715 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
716 TEST_PACKAGE_NAME, mNotificationChannel, 1, sbn, null, null, null, false, true,
717 true);
718 final TextView settingsLink = mNotificationInfo.findViewById(R.id.app_settings);
719 assertEquals(GONE, settingsLink.getVisibility());
720 }
721
Geoffrey Pitsch8c8dbde2017-04-20 11:44:33 -0400722
723 @Test
724 public void testWillBeRemovedReturnsFalseBeforeBind() throws Exception {
725 assertFalse(mNotificationInfo.willBeRemoved());
726 }
Julia Reynoldse0341482018-03-08 14:42:50 -0500727
728 @Test
729 public void testUndoText_min() throws Exception {
730 mSbn.getNotification().flags = Notification.FLAG_FOREGROUND_SERVICE;
731 mNotificationChannel.setImportance(IMPORTANCE_LOW);
732 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Rohan Shah63411fc2018-03-28 19:05:52 -0700733 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true);
Julia Reynoldse0341482018-03-08 14:42:50 -0500734
735 mNotificationInfo.findViewById(R.id.minimize).performClick();
736 waitForUndoButton();
737 TextView confirmationText = mNotificationInfo.findViewById(R.id.confirmation_text);
738 assertTrue(confirmationText.getText().toString().contains("minimized"));
739 }
740
741 @Test
742 public void testUndoText_block() throws Exception {
743 mNotificationChannel.setImportance(IMPORTANCE_LOW);
744 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Rohan Shah63411fc2018-03-28 19:05:52 -0700745 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true);
Julia Reynoldse0341482018-03-08 14:42:50 -0500746
747 mNotificationInfo.findViewById(R.id.block).performClick();
748 waitForUndoButton();
749 TextView confirmationText = mNotificationInfo.findViewById(R.id.confirmation_text);
750 assertTrue(confirmationText.getText().toString().contains("won't see"));
751 }
752
753 @Test
754 public void testNoHeaderOnConfirmation() throws Exception {
755 mNotificationChannel.setImportance(IMPORTANCE_LOW);
756 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Rohan Shah63411fc2018-03-28 19:05:52 -0700757 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true);
Julia Reynoldse0341482018-03-08 14:42:50 -0500758
759 mNotificationInfo.findViewById(R.id.block).performClick();
760 waitForUndoButton();
761 assertEquals(GONE, mNotificationInfo.findViewById(R.id.header).getVisibility());
762 }
763
764 @Test
765 public void testHeaderOnUndo() throws Exception {
766 mNotificationChannel.setImportance(IMPORTANCE_LOW);
767 mNotificationInfo.bindNotification(mMockPackageManager, mMockINotificationManager,
Rohan Shah63411fc2018-03-28 19:05:52 -0700768 TEST_PACKAGE_NAME, mNotificationChannel, 1, mSbn, null, null, null, true);
Julia Reynoldse0341482018-03-08 14:42:50 -0500769
770 mNotificationInfo.findViewById(R.id.block).performClick();
771 waitForUndoButton();
772 mNotificationInfo.findViewById(R.id.undo).performClick();
773 waitForStopButton();
774 assertEquals(VISIBLE, mNotificationInfo.findViewById(R.id.header).getVisibility());
775 }
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500776}