Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server.notification; |
| 18 | |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 19 | import static android.app.NotificationManager.IMPORTANCE_LOW; |
| 20 | |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 21 | import static junit.framework.Assert.assertEquals; |
Julia Reynolds | 727a728 | 2017-04-13 10:54:01 -0400 | [diff] [blame] | 22 | import static junit.framework.Assert.assertFalse; |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 23 | import static junit.framework.Assert.assertTrue; |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 24 | import static junit.framework.Assert.fail; |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 25 | |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 26 | import static org.mockito.Matchers.anyBoolean; |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 27 | import static org.mockito.Matchers.anyLong; |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 28 | import static org.mockito.Matchers.anyString; |
| 29 | import static org.mockito.Matchers.eq; |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 30 | import static org.mockito.Mockito.any; |
| 31 | import static org.mockito.Mockito.anyInt; |
| 32 | import static org.mockito.Mockito.mock; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 33 | import static org.mockito.Mockito.never; |
| 34 | import static org.mockito.Mockito.reset; |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 35 | import static org.mockito.Mockito.times; |
| 36 | import static org.mockito.Mockito.verify; |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 37 | import static org.mockito.Mockito.when; |
| 38 | |
| 39 | import android.app.INotificationManager; |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 40 | import android.app.Notification; |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 41 | import android.app.NotificationChannel; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 42 | import android.app.NotificationChannelGroup; |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 43 | import android.app.NotificationManager; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 44 | import android.companion.ICompanionDeviceManager; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 45 | import android.content.ComponentName; |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 46 | import android.content.Context; |
| 47 | import android.content.pm.ApplicationInfo; |
| 48 | import android.content.pm.IPackageManager; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 49 | import android.content.pm.PackageManager; |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 50 | import android.content.pm.ParceledListSlice; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 51 | import android.graphics.Color; |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 52 | import android.os.Binder; |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 53 | import android.os.Process; |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 54 | import android.os.UserHandle; |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame^] | 55 | import android.provider.Settings.Secure; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 56 | import android.service.notification.NotificationListenerService; |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 57 | import android.service.notification.StatusBarNotification; |
Jason Monk | 745d0a8 | 2017-04-17 11:34:22 -0400 | [diff] [blame] | 58 | import android.testing.AndroidTestingRunner; |
Geoffrey Pitsch | 415e454 | 2017-04-10 13:12:58 -0400 | [diff] [blame] | 59 | import android.testing.TestableLooper; |
Jason Monk | 745d0a8 | 2017-04-17 11:34:22 -0400 | [diff] [blame] | 60 | import android.testing.TestableLooper.RunWithLooper; |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 61 | |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 62 | import java.util.ArrayList; |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 63 | import java.util.Arrays; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 64 | import java.util.List; |
| 65 | |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 66 | import org.junit.Before; |
| 67 | import org.junit.Test; |
Jason Monk | 745d0a8 | 2017-04-17 11:34:22 -0400 | [diff] [blame] | 68 | import org.junit.runner.RunWith; |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 69 | import org.mockito.Mock; |
| 70 | import org.mockito.Mockito; |
| 71 | import org.mockito.MockitoAnnotations; |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 72 | |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 73 | import com.android.server.lights.Light; |
| 74 | import com.android.server.lights.LightsManager; |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 75 | |
Jason Monk | 745d0a8 | 2017-04-17 11:34:22 -0400 | [diff] [blame] | 76 | @RunWith(AndroidTestingRunner.class) |
| 77 | @RunWithLooper |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame^] | 78 | public class NotificationManagerServiceTest extends NotificationTestCase { |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 79 | private static final long WAIT_FOR_IDLE_TIMEOUT = 2; |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 80 | private static final String TEST_CHANNEL_ID = "NotificationManagerServiceTestChannelId"; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 81 | private final int uid = Binder.getCallingUid(); |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 82 | private NotificationManagerService mNotificationManagerService; |
| 83 | private INotificationManager mBinderService; |
Geoffrey Pitsch | 415e454 | 2017-04-10 13:12:58 -0400 | [diff] [blame] | 84 | private NotificationManagerInternal mInternalService; |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 85 | @Mock |
| 86 | private IPackageManager mPackageManager; |
| 87 | @Mock |
| 88 | private PackageManager mPackageManagerClient; |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame^] | 89 | private Context mContext = getContext(); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 90 | private final String PKG = mContext.getPackageName(); |
Geoffrey Pitsch | 415e454 | 2017-04-10 13:12:58 -0400 | [diff] [blame] | 91 | private TestableLooper mTestableLooper; |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 92 | @Mock |
| 93 | private RankingHelper mRankingHelper; |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 94 | private NotificationChannel mTestNotificationChannel = new NotificationChannel( |
| 95 | TEST_CHANNEL_ID, TEST_CHANNEL_ID, NotificationManager.IMPORTANCE_DEFAULT); |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 96 | @Mock |
| 97 | private NotificationManagerService.NotificationListeners mNotificationListeners; |
| 98 | private ManagedServices.ManagedServiceInfo mListener; |
| 99 | @Mock private ICompanionDeviceManager mCompanionMgr; |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 100 | @Mock SnoozeHelper mSnoozeHelper; |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 101 | |
Geoffrey Pitsch | 415e454 | 2017-04-10 13:12:58 -0400 | [diff] [blame] | 102 | // Use a Testable subclass so we can simulate calls from the system without failing. |
| 103 | private static class TestableNotificationManagerService extends NotificationManagerService { |
| 104 | public TestableNotificationManagerService(Context context) { super(context); } |
| 105 | |
| 106 | @Override |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 107 | protected boolean isCallingUidSystem() { |
| 108 | return true; |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | protected boolean isCallerSystemOrPhone() { |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 113 | return true; |
| 114 | } |
Julia Reynolds | 727a728 | 2017-04-13 10:54:01 -0400 | [diff] [blame] | 115 | |
| 116 | @Override |
| 117 | protected ICompanionDeviceManager getCompanionManager() { |
| 118 | return null; |
| 119 | } |
Geoffrey Pitsch | 415e454 | 2017-04-10 13:12:58 -0400 | [diff] [blame] | 120 | } |
| 121 | |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 122 | @Before |
| 123 | public void setUp() throws Exception { |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 124 | MockitoAnnotations.initMocks(this); |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame^] | 125 | |
| 126 | // most tests assume badging is enabled |
| 127 | Secure.putIntForUser(getContext().getContentResolver(), |
| 128 | Secure.NOTIFICATION_BADGING, 1, |
| 129 | UserHandle.getUserHandleForUid(uid).getIdentifier()); |
| 130 | |
Geoffrey Pitsch | 415e454 | 2017-04-10 13:12:58 -0400 | [diff] [blame] | 131 | mNotificationManagerService = new TestableNotificationManagerService(mContext); |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 132 | |
| 133 | // MockPackageManager - default returns ApplicationInfo with matching calling UID |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 134 | final ApplicationInfo applicationInfo = new ApplicationInfo(); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 135 | applicationInfo.uid = uid; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 136 | when(mPackageManager.getApplicationInfo(anyString(), anyInt(), anyInt())) |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 137 | .thenReturn(applicationInfo); |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 138 | when(mPackageManagerClient.getApplicationInfoAsUser(anyString(), anyInt(), anyInt())) |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 139 | .thenReturn(applicationInfo); |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 140 | final LightsManager mockLightsManager = mock(LightsManager.class); |
| 141 | when(mockLightsManager.getLight(anyInt())).thenReturn(mock(Light.class)); |
Geoffrey Pitsch | 415e454 | 2017-04-10 13:12:58 -0400 | [diff] [blame] | 142 | // Use this testable looper. |
Jason Monk | 745d0a8 | 2017-04-17 11:34:22 -0400 | [diff] [blame] | 143 | mTestableLooper = TestableLooper.get(this); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 144 | |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 145 | mListener = mNotificationListeners.new ManagedServiceInfo( |
| 146 | null, new ComponentName(PKG, "test_class"), uid, true, null, 0); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 147 | when(mNotificationListeners.checkServiceTokenLocked(any())).thenReturn(mListener); |
Geoffrey Pitsch | 415e454 | 2017-04-10 13:12:58 -0400 | [diff] [blame] | 148 | mNotificationManagerService.init(mTestableLooper.getLooper(), mPackageManager, |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 149 | mPackageManagerClient, mockLightsManager, mNotificationListeners, mCompanionMgr, |
| 150 | mSnoozeHelper); |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 151 | |
| 152 | // Tests call directly into the Binder. |
| 153 | mBinderService = mNotificationManagerService.getBinderService(); |
Geoffrey Pitsch | 415e454 | 2017-04-10 13:12:58 -0400 | [diff] [blame] | 154 | mInternalService = mNotificationManagerService.getInternalService(); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 155 | |
| 156 | mBinderService.createNotificationChannels( |
| 157 | PKG, new ParceledListSlice(Arrays.asList(mTestNotificationChannel))); |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 158 | } |
| 159 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 160 | public void waitForIdle() throws Exception { |
Geoffrey Pitsch | 415e454 | 2017-04-10 13:12:58 -0400 | [diff] [blame] | 161 | mTestableLooper.processAllMessages(); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 162 | } |
| 163 | |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 164 | private NotificationRecord generateNotificationRecord(NotificationChannel channel, int id, |
| 165 | String groupKey, boolean isSummary) { |
| 166 | Notification.Builder nb = new Notification.Builder(mContext, channel.getId()) |
| 167 | .setContentTitle("foo") |
| 168 | .setSmallIcon(android.R.drawable.sym_def_app_icon) |
| 169 | .setGroup(groupKey) |
| 170 | .setGroupSummary(isSummary); |
| 171 | |
| 172 | StatusBarNotification sbn = new StatusBarNotification(PKG, PKG, id, "tag", uid, 0, |
| 173 | nb.build(), new UserHandle(uid), null, 0); |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 174 | return new NotificationRecord(mContext, sbn, channel); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 175 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 176 | private NotificationRecord generateNotificationRecord(NotificationChannel channel) { |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 177 | return generateNotificationRecord(channel, null); |
| 178 | } |
| 179 | |
| 180 | private NotificationRecord generateNotificationRecord(NotificationChannel channel, |
| 181 | Notification.TvExtender extender) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 182 | if (channel == null) { |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 183 | channel = mTestNotificationChannel; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 184 | } |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 185 | Notification.Builder nb = new Notification.Builder(mContext, channel.getId()) |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 186 | .setContentTitle("foo") |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 187 | .setSmallIcon(android.R.drawable.sym_def_app_icon); |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 188 | if (extender != null) { |
| 189 | nb.extend(extender); |
| 190 | } |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 191 | StatusBarNotification sbn = new StatusBarNotification(PKG, PKG, 1, "tag", uid, 0, |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 192 | nb.build(), new UserHandle(uid), null, 0); |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 193 | return new NotificationRecord(mContext, sbn, channel); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 194 | } |
| 195 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 196 | @Test |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 197 | public void testCreateNotificationChannels_SingleChannel() throws Exception { |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 198 | final NotificationChannel channel = |
| 199 | new NotificationChannel("id", "name", NotificationManager.IMPORTANCE_DEFAULT); |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 200 | mBinderService.createNotificationChannels("test_pkg", |
| 201 | new ParceledListSlice(Arrays.asList(channel))); |
| 202 | final NotificationChannel createdChannel = |
| 203 | mBinderService.getNotificationChannel("test_pkg", "id"); |
| 204 | assertTrue(createdChannel != null); |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 205 | } |
| 206 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 207 | @Test |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 208 | public void testCreateNotificationChannels_NullChannelThrowsException() throws Exception { |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 209 | try { |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 210 | mBinderService.createNotificationChannels("test_pkg", |
| 211 | new ParceledListSlice(Arrays.asList(null))); |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 212 | fail("Exception should be thrown immediately."); |
| 213 | } catch (NullPointerException e) { |
| 214 | // pass |
| 215 | } |
| 216 | } |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 217 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 218 | @Test |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 219 | public void testCreateNotificationChannels_TwoChannels() throws Exception { |
| 220 | final NotificationChannel channel1 = |
| 221 | new NotificationChannel("id1", "name", NotificationManager.IMPORTANCE_DEFAULT); |
| 222 | final NotificationChannel channel2 = |
| 223 | new NotificationChannel("id2", "name", NotificationManager.IMPORTANCE_DEFAULT); |
| 224 | mBinderService.createNotificationChannels("test_pkg", |
| 225 | new ParceledListSlice(Arrays.asList(channel1, channel2))); |
| 226 | assertTrue(mBinderService.getNotificationChannel("test_pkg", "id1") != null); |
| 227 | assertTrue(mBinderService.getNotificationChannel("test_pkg", "id2") != null); |
| 228 | } |
| 229 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 230 | @Test |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 231 | public void testCreateNotificationChannels_SecondCreateDoesNotChangeImportance() |
| 232 | throws Exception { |
| 233 | final NotificationChannel channel = |
| 234 | new NotificationChannel("id", "name", NotificationManager.IMPORTANCE_DEFAULT); |
| 235 | mBinderService.createNotificationChannels("test_pkg", |
| 236 | new ParceledListSlice(Arrays.asList(channel))); |
| 237 | |
| 238 | // Recreating the channel doesn't throw, but ignores importance. |
| 239 | final NotificationChannel dupeChannel = |
| 240 | new NotificationChannel("id", "name", NotificationManager.IMPORTANCE_HIGH); |
| 241 | mBinderService.createNotificationChannels("test_pkg", |
| 242 | new ParceledListSlice(Arrays.asList(dupeChannel))); |
| 243 | final NotificationChannel createdChannel = |
| 244 | mBinderService.getNotificationChannel("test_pkg", "id"); |
| 245 | assertEquals(NotificationManager.IMPORTANCE_DEFAULT, createdChannel.getImportance()); |
| 246 | } |
| 247 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 248 | @Test |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 249 | public void testCreateNotificationChannels_IdenticalChannelsInListIgnoresSecond() |
| 250 | throws Exception { |
| 251 | final NotificationChannel channel1 = |
| 252 | new NotificationChannel("id", "name", NotificationManager.IMPORTANCE_DEFAULT); |
| 253 | final NotificationChannel channel2 = |
| 254 | new NotificationChannel("id", "name", NotificationManager.IMPORTANCE_HIGH); |
| 255 | mBinderService.createNotificationChannels("test_pkg", |
| 256 | new ParceledListSlice(Arrays.asList(channel1, channel2))); |
| 257 | final NotificationChannel createdChannel = |
| 258 | mBinderService.getNotificationChannel("test_pkg", "id"); |
| 259 | assertEquals(NotificationManager.IMPORTANCE_DEFAULT, createdChannel.getImportance()); |
| 260 | } |
| 261 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 262 | @Test |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 263 | public void testBlockedNotifications_suspended() throws Exception { |
| 264 | NotificationUsageStats usageStats = mock(NotificationUsageStats.class); |
| 265 | when(mPackageManager.isPackageSuspendedForUser(anyString(), anyInt())).thenReturn(true); |
| 266 | |
| 267 | NotificationChannel channel = new NotificationChannel("id", "name", |
| 268 | NotificationManager.IMPORTANCE_HIGH); |
| 269 | NotificationRecord r = generateNotificationRecord(channel); |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 270 | assertTrue(mNotificationManagerService.isBlocked(r, usageStats)); |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 271 | verify(usageStats, times(1)).registerSuspendedByAdmin(eq(r)); |
| 272 | } |
| 273 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 274 | @Test |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 275 | public void testBlockedNotifications_blockedChannel() throws Exception { |
| 276 | NotificationUsageStats usageStats = mock(NotificationUsageStats.class); |
| 277 | when(mPackageManager.isPackageSuspendedForUser(anyString(), anyInt())).thenReturn(false); |
| 278 | |
| 279 | NotificationChannel channel = new NotificationChannel("id", "name", |
| 280 | NotificationManager.IMPORTANCE_HIGH); |
Julia Reynolds | aebfc4e | 2017-01-06 11:44:03 -0500 | [diff] [blame] | 281 | channel.setImportance(NotificationManager.IMPORTANCE_NONE); |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 282 | NotificationRecord r = generateNotificationRecord(channel); |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 283 | assertTrue(mNotificationManagerService.isBlocked(r, usageStats)); |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 284 | verify(usageStats, times(1)).registerBlocked(eq(r)); |
| 285 | } |
| 286 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 287 | @Test |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 288 | public void testBlockedNotifications_blockedApp() throws Exception { |
| 289 | NotificationUsageStats usageStats = mock(NotificationUsageStats.class); |
| 290 | when(mPackageManager.isPackageSuspendedForUser(anyString(), anyInt())).thenReturn(false); |
| 291 | |
| 292 | NotificationChannel channel = new NotificationChannel("id", "name", |
| 293 | NotificationManager.IMPORTANCE_HIGH); |
| 294 | NotificationRecord r = generateNotificationRecord(channel); |
| 295 | r.setUserImportance(NotificationManager.IMPORTANCE_NONE); |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 296 | assertTrue(mNotificationManagerService.isBlocked(r, usageStats)); |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 297 | verify(usageStats, times(1)).registerBlocked(eq(r)); |
| 298 | } |
| 299 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 300 | @Test |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 301 | public void testEnqueueNotificationWithTag_PopulatesGetActiveNotifications() throws Exception { |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 302 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", 0, |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 303 | generateNotificationRecord(null).getNotification(), 0); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 304 | waitForIdle(); |
| 305 | StatusBarNotification[] notifs = |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 306 | mBinderService.getActiveNotifications(PKG); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 307 | assertEquals(1, notifs.length); |
| 308 | } |
| 309 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 310 | @Test |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 311 | public void testCancelNotificationImmediatelyAfterEnqueue() throws Exception { |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 312 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", 0, |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 313 | generateNotificationRecord(null).getNotification(), 0); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 314 | mBinderService.cancelNotificationWithTag(PKG, "tag", 0, 0); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 315 | waitForIdle(); |
| 316 | StatusBarNotification[] notifs = |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 317 | mBinderService.getActiveNotifications(PKG); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 318 | assertEquals(0, notifs.length); |
| 319 | } |
| 320 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 321 | @Test |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 322 | public void testCancelNotificationWhilePostedAndEnqueued() throws Exception { |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 323 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", 0, |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 324 | generateNotificationRecord(null).getNotification(), 0); |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 325 | waitForIdle(); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 326 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", 0, |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 327 | generateNotificationRecord(null).getNotification(), 0); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 328 | mBinderService.cancelNotificationWithTag(PKG, "tag", 0, 0); |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 329 | waitForIdle(); |
| 330 | StatusBarNotification[] notifs = |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 331 | mBinderService.getActiveNotifications(PKG); |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 332 | assertEquals(0, notifs.length); |
| 333 | } |
| 334 | |
| 335 | @Test |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 336 | public void testCancelNotificationsFromListenerImmediatelyAfterEnqueue() throws Exception { |
| 337 | final StatusBarNotification sbn = generateNotificationRecord(null).sbn; |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 338 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 339 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 340 | mBinderService.cancelNotificationsFromListener(null, null); |
| 341 | waitForIdle(); |
| 342 | StatusBarNotification[] notifs = |
| 343 | mBinderService.getActiveNotifications(sbn.getPackageName()); |
| 344 | assertEquals(0, notifs.length); |
| 345 | } |
| 346 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 347 | @Test |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 348 | public void testCancelAllNotificationsImmediatelyAfterEnqueue() throws Exception { |
| 349 | final StatusBarNotification sbn = generateNotificationRecord(null).sbn; |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 350 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 351 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 352 | mBinderService.cancelAllNotifications(PKG, sbn.getUserId()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 353 | waitForIdle(); |
| 354 | StatusBarNotification[] notifs = |
| 355 | mBinderService.getActiveNotifications(sbn.getPackageName()); |
| 356 | assertEquals(0, notifs.length); |
| 357 | } |
| 358 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 359 | @Test |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 360 | public void testCancelAllNotifications_IgnoreForegroundService() throws Exception { |
| 361 | final StatusBarNotification sbn = generateNotificationRecord(null).sbn; |
| 362 | sbn.getNotification().flags |= Notification.FLAG_FOREGROUND_SERVICE; |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 363 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 364 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 365 | mBinderService.cancelAllNotifications(PKG, sbn.getUserId()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 366 | waitForIdle(); |
| 367 | StatusBarNotification[] notifs = |
| 368 | mBinderService.getActiveNotifications(sbn.getPackageName()); |
| 369 | assertEquals(1, notifs.length); |
| 370 | } |
| 371 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 372 | @Test |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 373 | public void testCancelAllNotifications_IgnoreOtherPackages() throws Exception { |
| 374 | final StatusBarNotification sbn = generateNotificationRecord(null).sbn; |
| 375 | sbn.getNotification().flags |= Notification.FLAG_FOREGROUND_SERVICE; |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 376 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 377 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 378 | mBinderService.cancelAllNotifications("other_pkg_name", sbn.getUserId()); |
| 379 | waitForIdle(); |
| 380 | StatusBarNotification[] notifs = |
| 381 | mBinderService.getActiveNotifications(sbn.getPackageName()); |
| 382 | assertEquals(1, notifs.length); |
| 383 | } |
| 384 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 385 | @Test |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 386 | public void testCancelAllNotifications_NullPkgRemovesAll() throws Exception { |
| 387 | final StatusBarNotification sbn = generateNotificationRecord(null).sbn; |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 388 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 389 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 390 | mBinderService.cancelAllNotifications(null, sbn.getUserId()); |
| 391 | waitForIdle(); |
| 392 | StatusBarNotification[] notifs = |
| 393 | mBinderService.getActiveNotifications(sbn.getPackageName()); |
| 394 | assertEquals(0, notifs.length); |
| 395 | } |
| 396 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 397 | @Test |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 398 | public void testCancelAllNotifications_NullPkgIgnoresUserAllNotifications() throws Exception { |
| 399 | final StatusBarNotification sbn = generateNotificationRecord(null).sbn; |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 400 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 401 | sbn.getId(), sbn.getNotification(), UserHandle.USER_ALL); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 402 | // Null pkg is how we signal a user switch. |
| 403 | mBinderService.cancelAllNotifications(null, sbn.getUserId()); |
| 404 | waitForIdle(); |
| 405 | StatusBarNotification[] notifs = |
| 406 | mBinderService.getActiveNotifications(sbn.getPackageName()); |
| 407 | assertEquals(1, notifs.length); |
| 408 | } |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 409 | |
| 410 | @Test |
Geoffrey Pitsch | 415e454 | 2017-04-10 13:12:58 -0400 | [diff] [blame] | 411 | public void testRemoveForegroundServiceFlag_ImmediatelyAfterEnqueue() throws Exception { |
| 412 | final StatusBarNotification sbn = generateNotificationRecord(null).sbn; |
| 413 | sbn.getNotification().flags |= Notification.FLAG_FOREGROUND_SERVICE; |
| 414 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", null, |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 415 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
Geoffrey Pitsch | 415e454 | 2017-04-10 13:12:58 -0400 | [diff] [blame] | 416 | mInternalService.removeForegroundServiceFlagFromNotification(PKG, sbn.getId(), |
| 417 | sbn.getUserId()); |
| 418 | waitForIdle(); |
| 419 | StatusBarNotification[] notifs = |
| 420 | mBinderService.getActiveNotifications(sbn.getPackageName()); |
| 421 | assertEquals(0, notifs[0].getNotification().flags & Notification.FLAG_FOREGROUND_SERVICE); |
| 422 | } |
| 423 | |
| 424 | @Test |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 425 | public void testCancelAfterSecondEnqueueDoesNotSpecifyForegroundFlag() throws Exception { |
| 426 | final StatusBarNotification sbn = generateNotificationRecord(null).sbn; |
| 427 | sbn.getNotification().flags = |
| 428 | Notification.FLAG_ONGOING_EVENT | Notification.FLAG_FOREGROUND_SERVICE; |
| 429 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", |
| 430 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
| 431 | sbn.getNotification().flags = Notification.FLAG_ONGOING_EVENT; |
| 432 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", |
| 433 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
| 434 | mBinderService.cancelNotificationWithTag(PKG, "tag", sbn.getId(), sbn.getUserId()); |
| 435 | waitForIdle(); |
| 436 | assertEquals(0, mBinderService.getActiveNotifications(sbn.getPackageName()).length); |
| 437 | } |
| 438 | |
| 439 | @Test |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 440 | public void testFindGroupNotificationsLocked() throws Exception { |
| 441 | // make sure the same notification can be found in both lists and returned |
| 442 | final NotificationRecord group1 = generateNotificationRecord( |
| 443 | mTestNotificationChannel, 1, "group1", true); |
| 444 | mNotificationManagerService.addEnqueuedNotification(group1); |
| 445 | mNotificationManagerService.addNotification(group1); |
| 446 | |
| 447 | // should not be returned |
| 448 | final NotificationRecord group2 = generateNotificationRecord( |
| 449 | mTestNotificationChannel, 2, "group2", true); |
| 450 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", null, |
| 451 | group2.sbn.getId(), group2.sbn.getNotification(), group2.sbn.getUserId()); |
| 452 | waitForIdle(); |
| 453 | |
| 454 | // should not be returned |
| 455 | final NotificationRecord nonGroup = generateNotificationRecord( |
| 456 | mTestNotificationChannel, 3, null, false); |
| 457 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", null, |
| 458 | nonGroup.sbn.getId(), nonGroup.sbn.getNotification(), nonGroup.sbn.getUserId()); |
| 459 | waitForIdle(); |
| 460 | |
| 461 | // same group, child, should be returned |
| 462 | final NotificationRecord group1Child = generateNotificationRecord( |
| 463 | mTestNotificationChannel, 4, "group1", false); |
| 464 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", null, group1Child.sbn.getId(), |
| 465 | group1Child.sbn.getNotification(), group1Child.sbn.getUserId()); |
| 466 | waitForIdle(); |
| 467 | |
| 468 | List<NotificationRecord> inGroup1 = |
| 469 | mNotificationManagerService.findGroupNotificationsLocked(PKG, group1.getGroupKey(), |
| 470 | group1.sbn.getUserId()); |
| 471 | assertEquals(3, inGroup1.size()); |
| 472 | for (NotificationRecord record : inGroup1) { |
| 473 | assertTrue(record.getGroupKey().equals(group1.getGroupKey())); |
| 474 | assertTrue(record.sbn.getId() == 1 || record.sbn.getId() == 4); |
| 475 | } |
| 476 | } |
| 477 | |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 478 | @Test |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 479 | public void testTvExtenderChannelOverride_onTv() throws Exception { |
| 480 | mNotificationManagerService.setIsTelevision(true); |
| 481 | mNotificationManagerService.setRankingHelper(mRankingHelper); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 482 | when(mRankingHelper.getNotificationChannel( |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 483 | anyString(), anyInt(), eq("foo"), anyBoolean())).thenReturn( |
| 484 | new NotificationChannel("foo", "foo", NotificationManager.IMPORTANCE_HIGH)); |
| 485 | |
Julia Reynolds | bad4297 | 2017-04-25 13:52:49 -0400 | [diff] [blame] | 486 | Notification.TvExtender tv = new Notification.TvExtender().setChannelId("foo"); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 487 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", 0, |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 488 | generateNotificationRecord(null, tv).getNotification(), 0); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 489 | verify(mRankingHelper, times(1)).getNotificationChannel( |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 490 | anyString(), anyInt(), eq("foo"), anyBoolean()); |
| 491 | } |
| 492 | |
| 493 | @Test |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 494 | public void testTvExtenderChannelOverride_notOnTv() throws Exception { |
| 495 | mNotificationManagerService.setIsTelevision(false); |
| 496 | mNotificationManagerService.setRankingHelper(mRankingHelper); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 497 | when(mRankingHelper.getNotificationChannel( |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 498 | anyString(), anyInt(), anyString(), anyBoolean())).thenReturn( |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 499 | mTestNotificationChannel); |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 500 | |
Julia Reynolds | bad4297 | 2017-04-25 13:52:49 -0400 | [diff] [blame] | 501 | Notification.TvExtender tv = new Notification.TvExtender().setChannelId("foo"); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 502 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", 0, |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 503 | generateNotificationRecord(null, tv).getNotification(), 0); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 504 | verify(mRankingHelper, times(1)).getNotificationChannel( |
| 505 | anyString(), anyInt(), eq(mTestNotificationChannel.getId()), anyBoolean()); |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 506 | } |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 507 | |
| 508 | @Test |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 509 | public void testCreateChannelNotifyListener() throws Exception { |
| 510 | List<String> associations = new ArrayList<>(); |
| 511 | associations.add("a"); |
| 512 | when(mCompanionMgr.getAssociations(PKG, uid)).thenReturn(associations); |
| 513 | mNotificationManagerService.setRankingHelper(mRankingHelper); |
| 514 | when(mRankingHelper.getNotificationChannel(eq(PKG), anyInt(), |
| 515 | eq(mTestNotificationChannel.getId()), anyBoolean())) |
| 516 | .thenReturn(mTestNotificationChannel); |
| 517 | NotificationChannel channel2 = new NotificationChannel("a", "b", IMPORTANCE_LOW); |
| 518 | when(mRankingHelper.getNotificationChannel(eq(PKG), anyInt(), |
| 519 | eq(channel2.getId()), anyBoolean())) |
| 520 | .thenReturn(channel2); |
| 521 | |
| 522 | reset(mNotificationListeners); |
| 523 | mBinderService.createNotificationChannels(PKG, |
| 524 | new ParceledListSlice(Arrays.asList(mTestNotificationChannel, channel2))); |
| 525 | verify(mNotificationListeners, times(1)).notifyNotificationChannelChanged(eq(PKG), |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 526 | eq(Process.myUserHandle()), eq(mTestNotificationChannel), |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 527 | eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_ADDED)); |
| 528 | verify(mNotificationListeners, times(1)).notifyNotificationChannelChanged(eq(PKG), |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 529 | eq(Process.myUserHandle()), eq(channel2), |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 530 | eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_ADDED)); |
| 531 | } |
| 532 | |
| 533 | @Test |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 534 | public void testCreateChannelGroupNotifyListener() throws Exception { |
| 535 | List<String> associations = new ArrayList<>(); |
| 536 | associations.add("a"); |
| 537 | when(mCompanionMgr.getAssociations(PKG, uid)).thenReturn(associations); |
| 538 | mNotificationManagerService.setRankingHelper(mRankingHelper); |
| 539 | NotificationChannelGroup group1 = new NotificationChannelGroup("a", "b"); |
| 540 | NotificationChannelGroup group2 = new NotificationChannelGroup("n", "m"); |
| 541 | |
| 542 | reset(mNotificationListeners); |
| 543 | mBinderService.createNotificationChannelGroups(PKG, |
| 544 | new ParceledListSlice(Arrays.asList(group1, group2))); |
| 545 | verify(mNotificationListeners, times(1)).notifyNotificationChannelGroupChanged(eq(PKG), |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 546 | eq(Process.myUserHandle()), eq(group1), |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 547 | eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_ADDED)); |
| 548 | verify(mNotificationListeners, times(1)).notifyNotificationChannelGroupChanged(eq(PKG), |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 549 | eq(Process.myUserHandle()), eq(group2), |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 550 | eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_ADDED)); |
| 551 | } |
| 552 | |
| 553 | @Test |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 554 | public void testUpdateChannelNotifyListener() throws Exception { |
| 555 | List<String> associations = new ArrayList<>(); |
| 556 | associations.add("a"); |
| 557 | when(mCompanionMgr.getAssociations(PKG, uid)).thenReturn(associations); |
| 558 | mNotificationManagerService.setRankingHelper(mRankingHelper); |
| 559 | mTestNotificationChannel.setLightColor(Color.CYAN); |
| 560 | when(mRankingHelper.getNotificationChannel(eq(PKG), anyInt(), |
| 561 | eq(mTestNotificationChannel.getId()), anyBoolean())) |
| 562 | .thenReturn(mTestNotificationChannel); |
| 563 | |
| 564 | reset(mNotificationListeners); |
| 565 | mBinderService.updateNotificationChannelForPackage(PKG, 0, mTestNotificationChannel); |
| 566 | verify(mNotificationListeners, times(1)).notifyNotificationChannelChanged(eq(PKG), |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 567 | eq(Process.myUserHandle()), eq(mTestNotificationChannel), |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 568 | eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_UPDATED)); |
| 569 | } |
| 570 | |
| 571 | @Test |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 572 | public void testDeleteChannelNotifyListener() throws Exception { |
| 573 | List<String> associations = new ArrayList<>(); |
| 574 | associations.add("a"); |
| 575 | when(mCompanionMgr.getAssociations(PKG, uid)).thenReturn(associations); |
| 576 | mNotificationManagerService.setRankingHelper(mRankingHelper); |
| 577 | when(mRankingHelper.getNotificationChannel(eq(PKG), anyInt(), |
| 578 | eq(mTestNotificationChannel.getId()), anyBoolean())) |
| 579 | .thenReturn(mTestNotificationChannel); |
| 580 | reset(mNotificationListeners); |
| 581 | mBinderService.deleteNotificationChannel(PKG, mTestNotificationChannel.getId()); |
| 582 | verify(mNotificationListeners, times(1)).notifyNotificationChannelChanged(eq(PKG), |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 583 | eq(Process.myUserHandle()), eq(mTestNotificationChannel), |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 584 | eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_DELETED)); |
| 585 | } |
| 586 | |
| 587 | @Test |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 588 | public void testDeleteChannelGroupNotifyListener() throws Exception { |
| 589 | List<String> associations = new ArrayList<>(); |
| 590 | associations.add("a"); |
| 591 | when(mCompanionMgr.getAssociations(PKG, uid)).thenReturn(associations); |
| 592 | NotificationChannelGroup ncg = new NotificationChannelGroup("a", "b/c"); |
| 593 | mNotificationManagerService.setRankingHelper(mRankingHelper); |
| 594 | when(mRankingHelper.getNotificationChannelGroup(eq(ncg.getId()), eq(PKG), anyInt())) |
| 595 | .thenReturn(ncg); |
| 596 | reset(mNotificationListeners); |
| 597 | mBinderService.deleteNotificationChannelGroup(PKG, ncg.getId()); |
| 598 | verify(mNotificationListeners, times(1)).notifyNotificationChannelGroupChanged(eq(PKG), |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 599 | eq(Process.myUserHandle()), eq(ncg), |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 600 | eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_DELETED)); |
| 601 | } |
| 602 | |
| 603 | @Test |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 604 | public void testUpdateNotificationChannelFromPrivilegedListener_success() throws Exception { |
| 605 | mNotificationManagerService.setRankingHelper(mRankingHelper); |
| 606 | List<String> associations = new ArrayList<>(); |
| 607 | associations.add("a"); |
| 608 | when(mCompanionMgr.getAssociations(PKG, uid)).thenReturn(associations); |
| 609 | |
| 610 | mBinderService.updateNotificationChannelFromPrivilegedListener( |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 611 | null, PKG, Process.myUserHandle(), mTestNotificationChannel); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 612 | |
| 613 | verify(mRankingHelper, times(1)).updateNotificationChannel(anyString(), anyInt(), any()); |
| 614 | |
| 615 | verify(mNotificationListeners, never()).notifyNotificationChannelChanged(eq(PKG), |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 616 | eq(Process.myUserHandle()), eq(mTestNotificationChannel), |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 617 | eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_UPDATED)); |
| 618 | } |
| 619 | |
| 620 | @Test |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 621 | public void testUpdateNotificationChannelFromPrivilegedListener_noAccess() throws Exception { |
| 622 | mNotificationManagerService.setRankingHelper(mRankingHelper); |
| 623 | List<String> associations = new ArrayList<>(); |
| 624 | when(mCompanionMgr.getAssociations(PKG, uid)).thenReturn(associations); |
| 625 | |
| 626 | try { |
| 627 | mBinderService.updateNotificationChannelFromPrivilegedListener( |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 628 | null, PKG, Process.myUserHandle(), mTestNotificationChannel); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 629 | fail("listeners that don't have a companion device shouldn't be able to call this"); |
| 630 | } catch (SecurityException e) { |
| 631 | // pass |
| 632 | } |
| 633 | |
| 634 | verify(mRankingHelper, never()).updateNotificationChannel(anyString(), anyInt(), any()); |
| 635 | |
| 636 | verify(mNotificationListeners, never()).notifyNotificationChannelChanged(eq(PKG), |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 637 | eq(Process.myUserHandle()), eq(mTestNotificationChannel), |
| 638 | eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_UPDATED)); |
| 639 | } |
| 640 | |
| 641 | @Test |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 642 | public void testUpdateNotificationChannelFromPrivilegedListener_badUser() throws Exception { |
| 643 | mNotificationManagerService.setRankingHelper(mRankingHelper); |
| 644 | List<String> associations = new ArrayList<>(); |
| 645 | associations.add("a"); |
| 646 | when(mCompanionMgr.getAssociations(PKG, uid)).thenReturn(associations); |
| 647 | mListener = mock(ManagedServices.ManagedServiceInfo.class); |
| 648 | when(mListener.enabledAndUserMatches(anyInt())).thenReturn(false); |
| 649 | when(mNotificationListeners.checkServiceTokenLocked(any())).thenReturn(mListener); |
| 650 | |
| 651 | try { |
| 652 | mBinderService.updateNotificationChannelFromPrivilegedListener( |
| 653 | null, PKG, UserHandle.ALL, mTestNotificationChannel); |
| 654 | fail("incorrectly allowed a change to a user listener cannot see"); |
| 655 | } catch (SecurityException e) { |
| 656 | // pass |
| 657 | } |
| 658 | |
| 659 | verify(mRankingHelper, never()).updateNotificationChannel(anyString(), anyInt(), any()); |
| 660 | |
| 661 | verify(mNotificationListeners, never()).notifyNotificationChannelChanged(eq(PKG), |
| 662 | eq(Process.myUserHandle()), eq(mTestNotificationChannel), |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 663 | eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_UPDATED)); |
| 664 | } |
| 665 | |
| 666 | @Test |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 667 | public void testGetNotificationChannelFromPrivilegedListener_success() throws Exception { |
| 668 | mNotificationManagerService.setRankingHelper(mRankingHelper); |
| 669 | List<String> associations = new ArrayList<>(); |
| 670 | associations.add("a"); |
| 671 | when(mCompanionMgr.getAssociations(PKG, uid)).thenReturn(associations); |
| 672 | |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 673 | mBinderService.getNotificationChannelsFromPrivilegedListener( |
| 674 | null, PKG, Process.myUserHandle()); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 675 | |
| 676 | verify(mRankingHelper, times(1)).getNotificationChannels( |
| 677 | anyString(), anyInt(), anyBoolean()); |
| 678 | } |
| 679 | |
| 680 | @Test |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 681 | public void testGetNotificationChannelFromPrivilegedListener_noAccess() throws Exception { |
| 682 | mNotificationManagerService.setRankingHelper(mRankingHelper); |
| 683 | List<String> associations = new ArrayList<>(); |
| 684 | when(mCompanionMgr.getAssociations(PKG, uid)).thenReturn(associations); |
| 685 | |
| 686 | try { |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 687 | mBinderService.getNotificationChannelsFromPrivilegedListener( |
| 688 | null, PKG, Process.myUserHandle()); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 689 | fail("listeners that don't have a companion device shouldn't be able to call this"); |
| 690 | } catch (SecurityException e) { |
| 691 | // pass |
| 692 | } |
| 693 | |
| 694 | verify(mRankingHelper, never()).getNotificationChannels( |
| 695 | anyString(), anyInt(), anyBoolean()); |
| 696 | } |
| 697 | |
| 698 | @Test |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 699 | public void testGetNotificationChannelFromPrivilegedListener_badUser() throws Exception { |
| 700 | mNotificationManagerService.setRankingHelper(mRankingHelper); |
| 701 | List<String> associations = new ArrayList<>(); |
| 702 | associations.add("a"); |
| 703 | when(mCompanionMgr.getAssociations(PKG, uid)).thenReturn(associations); |
| 704 | mListener = mock(ManagedServices.ManagedServiceInfo.class); |
| 705 | when(mListener.enabledAndUserMatches(anyInt())).thenReturn(false); |
| 706 | when(mNotificationListeners.checkServiceTokenLocked(any())).thenReturn(mListener); |
| 707 | |
| 708 | try { |
| 709 | mBinderService.getNotificationChannelsFromPrivilegedListener( |
| 710 | null, PKG, Process.myUserHandle()); |
| 711 | fail("listener getting channels from a user they cannot see"); |
| 712 | } catch (SecurityException e) { |
| 713 | // pass |
| 714 | } |
| 715 | |
| 716 | verify(mRankingHelper, never()).getNotificationChannels( |
| 717 | anyString(), anyInt(), anyBoolean()); |
| 718 | } |
| 719 | |
| 720 | @Test |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 721 | public void testGetNotificationChannelGroupsFromPrivilegedListener_success() throws Exception { |
| 722 | mNotificationManagerService.setRankingHelper(mRankingHelper); |
| 723 | List<String> associations = new ArrayList<>(); |
| 724 | associations.add("a"); |
| 725 | when(mCompanionMgr.getAssociations(PKG, uid)).thenReturn(associations); |
| 726 | |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 727 | mBinderService.getNotificationChannelGroupsFromPrivilegedListener( |
| 728 | null, PKG, Process.myUserHandle()); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 729 | |
| 730 | verify(mRankingHelper, times(1)).getNotificationChannelGroups(anyString(), anyInt()); |
| 731 | } |
| 732 | |
| 733 | @Test |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 734 | public void testGetNotificationChannelGroupsFromPrivilegedListener_noAccess() throws Exception { |
| 735 | mNotificationManagerService.setRankingHelper(mRankingHelper); |
| 736 | List<String> associations = new ArrayList<>(); |
| 737 | when(mCompanionMgr.getAssociations(PKG, uid)).thenReturn(associations); |
| 738 | |
| 739 | try { |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 740 | mBinderService.getNotificationChannelGroupsFromPrivilegedListener( |
| 741 | null, PKG, Process.myUserHandle()); |
| 742 | fail("listeners that don't have a companion device shouldn't be able to call this"); |
| 743 | } catch (SecurityException e) { |
| 744 | // pass |
| 745 | } |
| 746 | |
| 747 | verify(mRankingHelper, never()).getNotificationChannelGroups(anyString(), anyInt()); |
| 748 | } |
| 749 | |
| 750 | @Test |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 751 | public void testGetNotificationChannelGroupsFromPrivilegedListener_badUser() throws Exception { |
| 752 | mNotificationManagerService.setRankingHelper(mRankingHelper); |
| 753 | List<String> associations = new ArrayList<>(); |
| 754 | when(mCompanionMgr.getAssociations(PKG, uid)).thenReturn(associations); |
| 755 | mListener = mock(ManagedServices.ManagedServiceInfo.class); |
| 756 | when(mListener.enabledAndUserMatches(anyInt())).thenReturn(false); |
| 757 | when(mNotificationListeners.checkServiceTokenLocked(any())).thenReturn(mListener); |
| 758 | |
| 759 | try { |
| 760 | mBinderService.getNotificationChannelGroupsFromPrivilegedListener( |
| 761 | null, PKG, Process.myUserHandle()); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 762 | fail("listeners that don't have a companion device shouldn't be able to call this"); |
| 763 | } catch (SecurityException e) { |
| 764 | // pass |
| 765 | } |
| 766 | |
| 767 | verify(mRankingHelper, never()).getNotificationChannelGroups(anyString(), anyInt()); |
| 768 | } |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 769 | |
| 770 | @Test |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 771 | public void testHasCompanionDevice_failure() throws Exception { |
| 772 | when(mCompanionMgr.getAssociations(anyString(), anyInt())).thenThrow( |
| 773 | new IllegalArgumentException()); |
| 774 | mNotificationManagerService.hasCompanionDevice(mListener); |
| 775 | } |
Julia Reynolds | 727a728 | 2017-04-13 10:54:01 -0400 | [diff] [blame] | 776 | |
| 777 | @Test |
Julia Reynolds | 727a728 | 2017-04-13 10:54:01 -0400 | [diff] [blame] | 778 | public void testHasCompanionDevice_noService() throws Exception { |
| 779 | mNotificationManagerService = new TestableNotificationManagerService(mContext); |
| 780 | |
| 781 | assertFalse(mNotificationManagerService.hasCompanionDevice(mListener)); |
| 782 | } |
| 783 | |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 784 | @Test |
| 785 | public void testSnoozeRunnable_snoozeNonGrouped() throws Exception { |
| 786 | final NotificationRecord nonGrouped = generateNotificationRecord( |
| 787 | mTestNotificationChannel, 1, null, false); |
| 788 | final NotificationRecord grouped = generateNotificationRecord( |
| 789 | mTestNotificationChannel, 2, "group", false); |
| 790 | mNotificationManagerService.addNotification(grouped); |
| 791 | mNotificationManagerService.addNotification(nonGrouped); |
| 792 | |
| 793 | NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable = |
| 794 | mNotificationManagerService.new SnoozeNotificationRunnable( |
| 795 | nonGrouped.getKey(), 100, null); |
| 796 | snoozeNotificationRunnable.run(); |
| 797 | |
| 798 | // only snooze the one notification |
| 799 | verify(mSnoozeHelper, times(1)).snooze(any(NotificationRecord.class), anyLong()); |
| 800 | } |
| 801 | |
| 802 | @Test |
| 803 | public void testSnoozeRunnable_snoozeSummary_withChildren() throws Exception { |
| 804 | final NotificationRecord parent = generateNotificationRecord( |
| 805 | mTestNotificationChannel, 1, "group", true); |
| 806 | final NotificationRecord child = generateNotificationRecord( |
| 807 | mTestNotificationChannel, 2, "group", false); |
| 808 | final NotificationRecord child2 = generateNotificationRecord( |
| 809 | mTestNotificationChannel, 3, "group", false); |
| 810 | mNotificationManagerService.addNotification(parent); |
| 811 | mNotificationManagerService.addNotification(child); |
| 812 | mNotificationManagerService.addNotification(child2); |
| 813 | |
| 814 | NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable = |
| 815 | mNotificationManagerService.new SnoozeNotificationRunnable( |
| 816 | parent.getKey(), 100, null); |
| 817 | snoozeNotificationRunnable.run(); |
| 818 | |
| 819 | // snooze parent and children |
| 820 | verify(mSnoozeHelper, times(3)).snooze(any(NotificationRecord.class), anyLong()); |
| 821 | } |
| 822 | |
| 823 | @Test |
| 824 | public void testSnoozeRunnable_snoozeGroupChild_fellowChildren() throws Exception { |
| 825 | final NotificationRecord parent = generateNotificationRecord( |
| 826 | mTestNotificationChannel, 1, "group", true); |
| 827 | final NotificationRecord child = generateNotificationRecord( |
| 828 | mTestNotificationChannel, 2, "group", false); |
| 829 | final NotificationRecord child2 = generateNotificationRecord( |
| 830 | mTestNotificationChannel, 3, "group", false); |
| 831 | mNotificationManagerService.addNotification(parent); |
| 832 | mNotificationManagerService.addNotification(child); |
| 833 | mNotificationManagerService.addNotification(child2); |
| 834 | |
| 835 | NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable = |
| 836 | mNotificationManagerService.new SnoozeNotificationRunnable( |
| 837 | child2.getKey(), 100, null); |
| 838 | snoozeNotificationRunnable.run(); |
| 839 | |
| 840 | // only snooze the one child |
| 841 | verify(mSnoozeHelper, times(1)).snooze(any(NotificationRecord.class), anyLong()); |
| 842 | } |
| 843 | |
| 844 | @Test |
| 845 | public void testSnoozeRunnable_snoozeGroupChild_onlyChildOfSummary() throws Exception { |
| 846 | final NotificationRecord parent = generateNotificationRecord( |
| 847 | mTestNotificationChannel, 1, "group", true); |
| 848 | assertTrue(parent.sbn.getNotification().isGroupSummary()); |
| 849 | final NotificationRecord child = generateNotificationRecord( |
| 850 | mTestNotificationChannel, 2, "group", false); |
| 851 | mNotificationManagerService.addNotification(parent); |
| 852 | mNotificationManagerService.addNotification(child); |
| 853 | |
| 854 | NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable = |
| 855 | mNotificationManagerService.new SnoozeNotificationRunnable( |
| 856 | child.getKey(), 100, null); |
| 857 | snoozeNotificationRunnable.run(); |
| 858 | |
| 859 | // snooze child and summary |
| 860 | verify(mSnoozeHelper, times(2)).snooze(any(NotificationRecord.class), anyLong()); |
| 861 | } |
| 862 | |
| 863 | @Test |
| 864 | public void testSnoozeRunnable_snoozeGroupChild_noOthersInGroup() throws Exception { |
| 865 | final NotificationRecord child = generateNotificationRecord( |
| 866 | mTestNotificationChannel, 2, "group", false); |
| 867 | mNotificationManagerService.addNotification(child); |
| 868 | |
| 869 | NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable = |
| 870 | mNotificationManagerService.new SnoozeNotificationRunnable( |
| 871 | child.getKey(), 100, null); |
| 872 | snoozeNotificationRunnable.run(); |
| 873 | |
| 874 | // snooze child only |
| 875 | verify(mSnoozeHelper, times(1)).snooze(any(NotificationRecord.class), anyLong()); |
| 876 | } |
| 877 | |
| 878 | @Test |
| 879 | public void testPostGroupChild_unsnoozeParent() throws Exception { |
| 880 | final NotificationRecord child = generateNotificationRecord( |
| 881 | mTestNotificationChannel, 2, "group", false); |
| 882 | |
| 883 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", null, |
| 884 | child.sbn.getId(), child.sbn.getNotification(), child.sbn.getUserId()); |
| 885 | waitForIdle(); |
| 886 | |
| 887 | verify(mSnoozeHelper, times(1)).repostGroupSummary( |
| 888 | anyString(), anyInt(), eq(child.getGroupKey())); |
| 889 | } |
| 890 | |
| 891 | @Test |
| 892 | public void testPostNonGroup_noUnsnoozing() throws Exception { |
| 893 | final NotificationRecord record = generateNotificationRecord( |
| 894 | mTestNotificationChannel, 2, null, false); |
| 895 | |
| 896 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", null, |
| 897 | record.sbn.getId(), record.sbn.getNotification(), record.sbn.getUserId()); |
| 898 | waitForIdle(); |
| 899 | |
| 900 | verify(mSnoozeHelper, never()).repostGroupSummary(anyString(), anyInt(), anyString()); |
| 901 | } |
| 902 | |
| 903 | @Test |
| 904 | public void testPostGroupSummary_noUnsnoozing() throws Exception { |
| 905 | final NotificationRecord parent = generateNotificationRecord( |
| 906 | mTestNotificationChannel, 2, "group", true); |
| 907 | |
| 908 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", null, |
| 909 | parent.sbn.getId(), parent.sbn.getNotification(), parent.sbn.getUserId()); |
| 910 | waitForIdle(); |
| 911 | |
| 912 | verify(mSnoozeHelper, never()).repostGroupSummary(anyString(), anyInt(), anyString()); |
| 913 | } |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 914 | } |