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; |
Julia Reynolds | 4da7970 | 2017-06-01 11:06:10 -0400 | [diff] [blame] | 20 | import static android.app.NotificationManager.IMPORTANCE_NONE; |
Julia Reynolds | 4db5955 | 2017-06-30 13:34:01 -0400 | [diff] [blame] | 21 | import static android.content.pm.PackageManager.PERMISSION_DENIED; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 22 | |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 23 | import static com.android.server.notification.NotificationManagerService |
| 24 | .MESSAGE_SEND_RANKING_UPDATE; |
| 25 | |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 26 | import static junit.framework.Assert.assertEquals; |
Julia Reynolds | 727a728 | 2017-04-13 10:54:01 -0400 | [diff] [blame] | 27 | import static junit.framework.Assert.assertFalse; |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 28 | import static junit.framework.Assert.assertTrue; |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 29 | import static junit.framework.Assert.fail; |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 30 | |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 31 | import static org.mockito.Matchers.anyBoolean; |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 32 | import static org.mockito.Matchers.anyLong; |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 33 | import static org.mockito.Matchers.anyString; |
| 34 | import static org.mockito.Matchers.eq; |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 35 | import static org.mockito.Mockito.any; |
| 36 | import static org.mockito.Mockito.anyInt; |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 37 | import static org.mockito.Mockito.atLeastOnce; |
| 38 | import static org.mockito.Mockito.doAnswer; |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 39 | import static org.mockito.Mockito.mock; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 40 | import static org.mockito.Mockito.never; |
| 41 | import static org.mockito.Mockito.reset; |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 42 | import static org.mockito.Mockito.spy; |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 43 | import static org.mockito.Mockito.times; |
| 44 | import static org.mockito.Mockito.verify; |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 45 | import static org.mockito.Mockito.when; |
| 46 | |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 47 | import android.app.ActivityManager; |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 48 | import android.app.INotificationManager; |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 49 | import android.app.Notification; |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 50 | import android.app.NotificationChannel; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 51 | import android.app.NotificationChannelGroup; |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 52 | import android.app.NotificationManager; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 53 | import android.companion.ICompanionDeviceManager; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 54 | import android.content.ComponentName; |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 55 | import android.content.Context; |
| 56 | import android.content.pm.ApplicationInfo; |
| 57 | import android.content.pm.IPackageManager; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 58 | import android.content.pm.PackageManager; |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 59 | import android.content.pm.ParceledListSlice; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 60 | import android.graphics.Color; |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 61 | import android.media.AudioManager; |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 62 | import android.os.Binder; |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 63 | import android.os.Handler; |
| 64 | import android.os.Looper; |
| 65 | import android.os.Message; |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 66 | import android.os.Process; |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 67 | import android.os.UserHandle; |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 68 | import android.provider.Settings.Secure; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 69 | import android.service.notification.NotificationListenerService; |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 70 | import android.service.notification.StatusBarNotification; |
Geoffrey Pitsch | 8185d38 | 2017-05-19 18:41:32 -0400 | [diff] [blame] | 71 | import android.test.suitebuilder.annotation.SmallTest; |
Jason Monk | 745d0a8 | 2017-04-17 11:34:22 -0400 | [diff] [blame] | 72 | import android.testing.AndroidTestingRunner; |
Geoffrey Pitsch | 415e454 | 2017-04-10 13:12:58 -0400 | [diff] [blame] | 73 | import android.testing.TestableLooper; |
Jason Monk | 745d0a8 | 2017-04-17 11:34:22 -0400 | [diff] [blame] | 74 | import android.testing.TestableLooper.RunWithLooper; |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 75 | import android.util.ArrayMap; |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 76 | import android.util.AtomicFile; |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 77 | import android.util.Log; |
| 78 | import android.util.Slog; |
| 79 | |
| 80 | import com.android.server.lights.Light; |
| 81 | import com.android.server.lights.LightsManager; |
| 82 | |
| 83 | import org.junit.After; |
| 84 | import org.junit.Before; |
| 85 | import org.junit.Test; |
| 86 | import org.junit.runner.RunWith; |
| 87 | import org.mockito.ArgumentCaptor; |
| 88 | import org.mockito.Mock; |
| 89 | import org.mockito.MockitoAnnotations; |
| 90 | import org.mockito.stubbing.Answer; |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 91 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 92 | import java.io.File; |
| 93 | import java.io.FileInputStream; |
| 94 | import java.io.FileOutputStream; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 95 | import java.util.ArrayList; |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 96 | import java.util.Arrays; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 97 | import java.util.List; |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 98 | import java.util.Map; |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 99 | |
Geoffrey Pitsch | 8185d38 | 2017-05-19 18:41:32 -0400 | [diff] [blame] | 100 | @SmallTest |
Jason Monk | 745d0a8 | 2017-04-17 11:34:22 -0400 | [diff] [blame] | 101 | @RunWith(AndroidTestingRunner.class) |
| 102 | @RunWithLooper |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 103 | public class NotificationManagerServiceTest extends NotificationTestCase { |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 104 | private static final String TEST_CHANNEL_ID = "NotificationManagerServiceTestChannelId"; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 105 | private final int uid = Binder.getCallingUid(); |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 106 | private NotificationManagerService mNotificationManagerService; |
| 107 | private INotificationManager mBinderService; |
Geoffrey Pitsch | 415e454 | 2017-04-10 13:12:58 -0400 | [diff] [blame] | 108 | private NotificationManagerInternal mInternalService; |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 109 | @Mock |
| 110 | private IPackageManager mPackageManager; |
| 111 | @Mock |
| 112 | private PackageManager mPackageManagerClient; |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 113 | private Context mContext = getContext(); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 114 | private final String PKG = mContext.getPackageName(); |
Geoffrey Pitsch | 415e454 | 2017-04-10 13:12:58 -0400 | [diff] [blame] | 115 | private TestableLooper mTestableLooper; |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 116 | @Mock |
| 117 | private RankingHelper mRankingHelper; |
Geoffrey Pitsch | d5bcf21 | 2017-06-01 15:45:35 -0400 | [diff] [blame] | 118 | @Mock |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 119 | AtomicFile mPolicyFile; |
| 120 | File mFile; |
| 121 | @Mock |
Geoffrey Pitsch | d5bcf21 | 2017-06-01 15:45:35 -0400 | [diff] [blame] | 122 | private NotificationUsageStats mUsageStats; |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 123 | @Mock |
| 124 | private AudioManager mAudioManager; |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 125 | @Mock |
| 126 | ActivityManager mActivityManager; |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 127 | NotificationManagerService.WorkerHandler mHandler; |
Julia Reynolds | 3ff26d2 | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 128 | |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 129 | private NotificationChannel mTestNotificationChannel = new NotificationChannel( |
| 130 | TEST_CHANNEL_ID, TEST_CHANNEL_ID, NotificationManager.IMPORTANCE_DEFAULT); |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 131 | @Mock |
| 132 | private NotificationManagerService.NotificationListeners mNotificationListeners; |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 133 | @Mock private NotificationManagerService.NotificationAssistants mNotificationAssistants; |
| 134 | @Mock private ConditionProviders mConditionProviders; |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 135 | private ManagedServices.ManagedServiceInfo mListener; |
| 136 | @Mock private ICompanionDeviceManager mCompanionMgr; |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 137 | @Mock SnoozeHelper mSnoozeHelper; |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 138 | @Mock GroupHelper mGroupHelper; |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 139 | |
Geoffrey Pitsch | 415e454 | 2017-04-10 13:12:58 -0400 | [diff] [blame] | 140 | // Use a Testable subclass so we can simulate calls from the system without failing. |
| 141 | private static class TestableNotificationManagerService extends NotificationManagerService { |
| 142 | public TestableNotificationManagerService(Context context) { super(context); } |
| 143 | |
| 144 | @Override |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 145 | protected boolean isCallingUidSystem() { |
| 146 | return true; |
| 147 | } |
| 148 | |
| 149 | @Override |
| 150 | protected boolean isCallerSystemOrPhone() { |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 151 | return true; |
| 152 | } |
Julia Reynolds | 727a728 | 2017-04-13 10:54:01 -0400 | [diff] [blame] | 153 | |
| 154 | @Override |
| 155 | protected ICompanionDeviceManager getCompanionManager() { |
| 156 | return null; |
| 157 | } |
Geoffrey Pitsch | 415e454 | 2017-04-10 13:12:58 -0400 | [diff] [blame] | 158 | } |
| 159 | |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 160 | @Before |
| 161 | public void setUp() throws Exception { |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 162 | MockitoAnnotations.initMocks(this); |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 163 | |
| 164 | // most tests assume badging is enabled |
| 165 | Secure.putIntForUser(getContext().getContentResolver(), |
| 166 | Secure.NOTIFICATION_BADGING, 1, |
| 167 | UserHandle.getUserHandleForUid(uid).getIdentifier()); |
| 168 | |
Geoffrey Pitsch | 415e454 | 2017-04-10 13:12:58 -0400 | [diff] [blame] | 169 | mNotificationManagerService = new TestableNotificationManagerService(mContext); |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 170 | |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 171 | // Use this testable looper. |
| 172 | mTestableLooper = TestableLooper.get(this); |
| 173 | mHandler = mNotificationManagerService.new WorkerHandler(mTestableLooper.getLooper()); |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 174 | // MockPackageManager - default returns ApplicationInfo with matching calling UID |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 175 | final ApplicationInfo applicationInfo = new ApplicationInfo(); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 176 | applicationInfo.uid = uid; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 177 | when(mPackageManager.getApplicationInfo(anyString(), anyInt(), anyInt())) |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 178 | .thenReturn(applicationInfo); |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 179 | when(mPackageManagerClient.getApplicationInfoAsUser(anyString(), anyInt(), anyInt())) |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 180 | .thenReturn(applicationInfo); |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 181 | final LightsManager mockLightsManager = mock(LightsManager.class); |
| 182 | when(mockLightsManager.getLight(anyInt())).thenReturn(mock(Light.class)); |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 183 | when(mAudioManager.getRingerModeInternal()).thenReturn(AudioManager.RINGER_MODE_NORMAL); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 184 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 185 | mFile = new File(mContext.getCacheDir(), "test.xml"); |
| 186 | mFile.createNewFile(); |
| 187 | when(mPolicyFile.openRead()).thenReturn(new FileInputStream(mFile)); |
| 188 | when(mPolicyFile.startWrite()).thenReturn(new FileOutputStream(mFile)); |
| 189 | |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 190 | mListener = mNotificationListeners.new ManagedServiceInfo( |
| 191 | null, new ComponentName(PKG, "test_class"), uid, true, null, 0); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 192 | when(mNotificationListeners.checkServiceTokenLocked(any())).thenReturn(mListener); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 193 | try { |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 194 | mNotificationManagerService.init(mTestableLooper.getLooper(), |
| 195 | mPackageManager, mPackageManagerClient, mockLightsManager, |
| 196 | mNotificationListeners, mNotificationAssistants, mConditionProviders, |
| 197 | mCompanionMgr, mSnoozeHelper, mUsageStats, mPolicyFile, mActivityManager, |
| 198 | mGroupHelper); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 199 | } catch (SecurityException e) { |
| 200 | if (!e.getMessage().contains("Permission Denial: not allowed to send broadcast")) { |
| 201 | throw e; |
| 202 | } |
| 203 | } |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 204 | mNotificationManagerService.setAudioManager(mAudioManager); |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 205 | |
| 206 | // Tests call directly into the Binder. |
| 207 | mBinderService = mNotificationManagerService.getBinderService(); |
Geoffrey Pitsch | 415e454 | 2017-04-10 13:12:58 -0400 | [diff] [blame] | 208 | mInternalService = mNotificationManagerService.getInternalService(); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 209 | |
| 210 | mBinderService.createNotificationChannels( |
| 211 | PKG, new ParceledListSlice(Arrays.asList(mTestNotificationChannel))); |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 212 | } |
| 213 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 214 | @After |
| 215 | public void tearDown() throws Exception { |
| 216 | mFile.delete(); |
| 217 | } |
| 218 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 219 | public void waitForIdle() throws Exception { |
Geoffrey Pitsch | 415e454 | 2017-04-10 13:12:58 -0400 | [diff] [blame] | 220 | mTestableLooper.processAllMessages(); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 221 | } |
| 222 | |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 223 | private NotificationRecord generateNotificationRecord(NotificationChannel channel, int id, |
| 224 | String groupKey, boolean isSummary) { |
| 225 | Notification.Builder nb = new Notification.Builder(mContext, channel.getId()) |
| 226 | .setContentTitle("foo") |
| 227 | .setSmallIcon(android.R.drawable.sym_def_app_icon) |
| 228 | .setGroup(groupKey) |
| 229 | .setGroupSummary(isSummary); |
| 230 | |
| 231 | StatusBarNotification sbn = new StatusBarNotification(PKG, PKG, id, "tag", uid, 0, |
| 232 | nb.build(), new UserHandle(uid), null, 0); |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 233 | return new NotificationRecord(mContext, sbn, channel); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 234 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 235 | private NotificationRecord generateNotificationRecord(NotificationChannel channel) { |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 236 | return generateNotificationRecord(channel, null); |
| 237 | } |
| 238 | |
| 239 | private NotificationRecord generateNotificationRecord(NotificationChannel channel, |
| 240 | Notification.TvExtender extender) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 241 | if (channel == null) { |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 242 | channel = mTestNotificationChannel; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 243 | } |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 244 | Notification.Builder nb = new Notification.Builder(mContext, channel.getId()) |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 245 | .setContentTitle("foo") |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 246 | .setSmallIcon(android.R.drawable.sym_def_app_icon); |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 247 | if (extender != null) { |
| 248 | nb.extend(extender); |
| 249 | } |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 250 | StatusBarNotification sbn = new StatusBarNotification(PKG, PKG, 1, "tag", uid, 0, |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 251 | nb.build(), new UserHandle(uid), null, 0); |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 252 | return new NotificationRecord(mContext, sbn, channel); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 253 | } |
| 254 | |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 255 | private Map<String, Answer> getSignalExtractorSideEffects() { |
| 256 | Map<String, Answer> answers = new ArrayMap<>(); |
| 257 | |
| 258 | answers.put("override group key", invocationOnMock -> { |
| 259 | ((NotificationRecord) invocationOnMock.getArguments()[0]) |
| 260 | .setOverrideGroupKey("bananas"); |
| 261 | return null; |
| 262 | }); |
| 263 | answers.put("override people", invocationOnMock -> { |
| 264 | ((NotificationRecord) invocationOnMock.getArguments()[0]) |
| 265 | .setPeopleOverride(new ArrayList<>()); |
| 266 | return null; |
| 267 | }); |
| 268 | answers.put("snooze criteria", invocationOnMock -> { |
| 269 | ((NotificationRecord) invocationOnMock.getArguments()[0]) |
| 270 | .setSnoozeCriteria(new ArrayList<>()); |
| 271 | return null; |
| 272 | }); |
| 273 | answers.put("notification channel", invocationOnMock -> { |
| 274 | ((NotificationRecord) invocationOnMock.getArguments()[0]) |
| 275 | .updateNotificationChannel(new NotificationChannel("a", "", IMPORTANCE_LOW)); |
| 276 | return null; |
| 277 | }); |
| 278 | answers.put("badging", invocationOnMock -> { |
| 279 | NotificationRecord r = (NotificationRecord) invocationOnMock.getArguments()[0]; |
| 280 | r.setShowBadge(!r.canShowBadge()); |
| 281 | return null; |
| 282 | }); |
| 283 | answers.put("package visibility", invocationOnMock -> { |
| 284 | ((NotificationRecord) invocationOnMock.getArguments()[0]).setPackageVisibilityOverride( |
| 285 | Notification.VISIBILITY_SECRET); |
| 286 | return null; |
| 287 | }); |
| 288 | |
| 289 | return answers; |
| 290 | } |
| 291 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 292 | @Test |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 293 | public void testCreateNotificationChannels_SingleChannel() throws Exception { |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 294 | final NotificationChannel channel = |
| 295 | new NotificationChannel("id", "name", NotificationManager.IMPORTANCE_DEFAULT); |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 296 | mBinderService.createNotificationChannels("test_pkg", |
| 297 | new ParceledListSlice(Arrays.asList(channel))); |
| 298 | final NotificationChannel createdChannel = |
| 299 | mBinderService.getNotificationChannel("test_pkg", "id"); |
| 300 | assertTrue(createdChannel != null); |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 301 | } |
| 302 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 303 | @Test |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 304 | public void testCreateNotificationChannels_NullChannelThrowsException() throws Exception { |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 305 | try { |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 306 | mBinderService.createNotificationChannels("test_pkg", |
| 307 | new ParceledListSlice(Arrays.asList(null))); |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 308 | fail("Exception should be thrown immediately."); |
| 309 | } catch (NullPointerException e) { |
| 310 | // pass |
| 311 | } |
| 312 | } |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 313 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 314 | @Test |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 315 | public void testCreateNotificationChannels_TwoChannels() throws Exception { |
| 316 | final NotificationChannel channel1 = |
| 317 | new NotificationChannel("id1", "name", NotificationManager.IMPORTANCE_DEFAULT); |
| 318 | final NotificationChannel channel2 = |
| 319 | new NotificationChannel("id2", "name", NotificationManager.IMPORTANCE_DEFAULT); |
| 320 | mBinderService.createNotificationChannels("test_pkg", |
| 321 | new ParceledListSlice(Arrays.asList(channel1, channel2))); |
| 322 | assertTrue(mBinderService.getNotificationChannel("test_pkg", "id1") != null); |
| 323 | assertTrue(mBinderService.getNotificationChannel("test_pkg", "id2") != null); |
| 324 | } |
| 325 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 326 | @Test |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 327 | public void testCreateNotificationChannels_SecondCreateDoesNotChangeImportance() |
| 328 | throws Exception { |
| 329 | final NotificationChannel channel = |
| 330 | new NotificationChannel("id", "name", NotificationManager.IMPORTANCE_DEFAULT); |
| 331 | mBinderService.createNotificationChannels("test_pkg", |
| 332 | new ParceledListSlice(Arrays.asList(channel))); |
| 333 | |
| 334 | // Recreating the channel doesn't throw, but ignores importance. |
| 335 | final NotificationChannel dupeChannel = |
| 336 | new NotificationChannel("id", "name", NotificationManager.IMPORTANCE_HIGH); |
| 337 | mBinderService.createNotificationChannels("test_pkg", |
| 338 | new ParceledListSlice(Arrays.asList(dupeChannel))); |
| 339 | final NotificationChannel createdChannel = |
| 340 | mBinderService.getNotificationChannel("test_pkg", "id"); |
| 341 | assertEquals(NotificationManager.IMPORTANCE_DEFAULT, createdChannel.getImportance()); |
| 342 | } |
| 343 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 344 | @Test |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 345 | public void testCreateNotificationChannels_IdenticalChannelsInListIgnoresSecond() |
| 346 | throws Exception { |
| 347 | final NotificationChannel channel1 = |
| 348 | new NotificationChannel("id", "name", NotificationManager.IMPORTANCE_DEFAULT); |
| 349 | final NotificationChannel channel2 = |
| 350 | new NotificationChannel("id", "name", NotificationManager.IMPORTANCE_HIGH); |
| 351 | mBinderService.createNotificationChannels("test_pkg", |
| 352 | new ParceledListSlice(Arrays.asList(channel1, channel2))); |
| 353 | final NotificationChannel createdChannel = |
| 354 | mBinderService.getNotificationChannel("test_pkg", "id"); |
| 355 | assertEquals(NotificationManager.IMPORTANCE_DEFAULT, createdChannel.getImportance()); |
| 356 | } |
| 357 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 358 | @Test |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 359 | public void testBlockedNotifications_suspended() throws Exception { |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 360 | when(mPackageManager.isPackageSuspendedForUser(anyString(), anyInt())).thenReturn(true); |
| 361 | |
| 362 | NotificationChannel channel = new NotificationChannel("id", "name", |
| 363 | NotificationManager.IMPORTANCE_HIGH); |
| 364 | NotificationRecord r = generateNotificationRecord(channel); |
Geoffrey Pitsch | d5bcf21 | 2017-06-01 15:45:35 -0400 | [diff] [blame] | 365 | assertTrue(mNotificationManagerService.isBlocked(r, mUsageStats)); |
| 366 | verify(mUsageStats, times(1)).registerSuspendedByAdmin(eq(r)); |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 367 | } |
| 368 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 369 | @Test |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 370 | public void testBlockedNotifications_blockedChannel() throws Exception { |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 371 | when(mPackageManager.isPackageSuspendedForUser(anyString(), anyInt())).thenReturn(false); |
| 372 | |
| 373 | NotificationChannel channel = new NotificationChannel("id", "name", |
| 374 | NotificationManager.IMPORTANCE_HIGH); |
Julia Reynolds | 4db5955 | 2017-06-30 13:34:01 -0400 | [diff] [blame] | 375 | channel.setImportance(IMPORTANCE_NONE); |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 376 | NotificationRecord r = generateNotificationRecord(channel); |
Geoffrey Pitsch | d5bcf21 | 2017-06-01 15:45:35 -0400 | [diff] [blame] | 377 | assertTrue(mNotificationManagerService.isBlocked(r, mUsageStats)); |
| 378 | verify(mUsageStats, times(1)).registerBlocked(eq(r)); |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 379 | } |
| 380 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 381 | @Test |
Julia Reynolds | 4da7970 | 2017-06-01 11:06:10 -0400 | [diff] [blame] | 382 | public void testEnqueuedBlockedNotifications_blockedApp() throws Exception { |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 383 | when(mPackageManager.isPackageSuspendedForUser(anyString(), anyInt())).thenReturn(false); |
| 384 | |
Julia Reynolds | 4da7970 | 2017-06-01 11:06:10 -0400 | [diff] [blame] | 385 | mBinderService.setNotificationsEnabledForPackage(PKG, uid, false); |
| 386 | |
| 387 | final StatusBarNotification sbn = generateNotificationRecord(null).sbn; |
| 388 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", |
| 389 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
| 390 | waitForIdle(); |
| 391 | assertEquals(0, mBinderService.getActiveNotifications(sbn.getPackageName()).length); |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 392 | } |
| 393 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 394 | @Test |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 395 | public void testEnqueueNotificationWithTag_PopulatesGetActiveNotifications() throws Exception { |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 396 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", 0, |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 397 | generateNotificationRecord(null).getNotification(), 0); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 398 | waitForIdle(); |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 399 | StatusBarNotification[] notifs = mBinderService.getActiveNotifications(PKG); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 400 | assertEquals(1, notifs.length); |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 401 | assertEquals(1, mNotificationManagerService.getNotificationRecordCount()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 402 | } |
| 403 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 404 | @Test |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 405 | public void testCancelNotificationImmediatelyAfterEnqueue() throws Exception { |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 406 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", 0, |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 407 | generateNotificationRecord(null).getNotification(), 0); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 408 | mBinderService.cancelNotificationWithTag(PKG, "tag", 0, 0); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 409 | waitForIdle(); |
| 410 | StatusBarNotification[] notifs = |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 411 | mBinderService.getActiveNotifications(PKG); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 412 | assertEquals(0, notifs.length); |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 413 | assertEquals(0, mNotificationManagerService.getNotificationRecordCount()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 414 | } |
| 415 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 416 | @Test |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 417 | public void testCancelNotificationWhilePostedAndEnqueued() throws Exception { |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 418 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", 0, |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 419 | generateNotificationRecord(null).getNotification(), 0); |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 420 | waitForIdle(); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 421 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", 0, |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 422 | generateNotificationRecord(null).getNotification(), 0); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 423 | mBinderService.cancelNotificationWithTag(PKG, "tag", 0, 0); |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 424 | waitForIdle(); |
| 425 | StatusBarNotification[] notifs = |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 426 | mBinderService.getActiveNotifications(PKG); |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 427 | assertEquals(0, notifs.length); |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 428 | assertEquals(0, mNotificationManagerService.getNotificationRecordCount()); |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | @Test |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 432 | public void testCancelNotificationsFromListenerImmediatelyAfterEnqueue() throws Exception { |
| 433 | final StatusBarNotification sbn = generateNotificationRecord(null).sbn; |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 434 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 435 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 436 | mBinderService.cancelNotificationsFromListener(null, null); |
| 437 | waitForIdle(); |
| 438 | StatusBarNotification[] notifs = |
| 439 | mBinderService.getActiveNotifications(sbn.getPackageName()); |
| 440 | assertEquals(0, notifs.length); |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 441 | assertEquals(0, mNotificationManagerService.getNotificationRecordCount()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 442 | } |
| 443 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 444 | @Test |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 445 | public void testCancelAllNotificationsImmediatelyAfterEnqueue() throws Exception { |
| 446 | final StatusBarNotification sbn = generateNotificationRecord(null).sbn; |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 447 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 448 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 449 | mBinderService.cancelAllNotifications(PKG, sbn.getUserId()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 450 | waitForIdle(); |
| 451 | StatusBarNotification[] notifs = |
| 452 | mBinderService.getActiveNotifications(sbn.getPackageName()); |
| 453 | assertEquals(0, notifs.length); |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 454 | assertEquals(0, mNotificationManagerService.getNotificationRecordCount()); |
| 455 | } |
| 456 | |
| 457 | @Test |
| 458 | public void testUserInitiatedClearAll_noLeak() throws Exception { |
| 459 | final NotificationRecord n = generateNotificationRecord( |
| 460 | mTestNotificationChannel, 1, "group", true); |
| 461 | |
| 462 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", |
| 463 | n.sbn.getId(), n.sbn.getNotification(), n.sbn.getUserId()); |
| 464 | waitForIdle(); |
| 465 | |
| 466 | mNotificationManagerService.mNotificationDelegate.onClearAll(uid, Binder.getCallingPid(), |
| 467 | n.getUserId()); |
| 468 | waitForIdle(); |
| 469 | StatusBarNotification[] notifs = |
| 470 | mBinderService.getActiveNotifications(n.sbn.getPackageName()); |
| 471 | assertEquals(0, notifs.length); |
| 472 | assertEquals(0, mNotificationManagerService.getNotificationRecordCount()); |
| 473 | } |
| 474 | |
| 475 | @Test |
| 476 | public void testCancelAllNotificationsCancelsChildren() throws Exception { |
| 477 | final NotificationRecord parent = generateNotificationRecord( |
| 478 | mTestNotificationChannel, 1, "group1", true); |
| 479 | final NotificationRecord child = generateNotificationRecord( |
| 480 | mTestNotificationChannel, 2, "group1", false); |
| 481 | |
| 482 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", |
| 483 | parent.sbn.getId(), parent.sbn.getNotification(), parent.sbn.getUserId()); |
| 484 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", |
| 485 | child.sbn.getId(), child.sbn.getNotification(), child.sbn.getUserId()); |
| 486 | waitForIdle(); |
| 487 | |
| 488 | mBinderService.cancelAllNotifications(PKG, parent.sbn.getUserId()); |
| 489 | waitForIdle(); |
| 490 | assertEquals(0, mNotificationManagerService.getNotificationRecordCount()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 491 | } |
| 492 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 493 | @Test |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 494 | public void testCancelAllNotificationsMultipleEnqueuedDoesNotCrash() throws Exception { |
| 495 | final StatusBarNotification sbn = generateNotificationRecord(null).sbn; |
| 496 | for (int i = 0; i < 10; i++) { |
| 497 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", |
| 498 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
| 499 | } |
| 500 | mBinderService.cancelAllNotifications(PKG, sbn.getUserId()); |
| 501 | waitForIdle(); |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 502 | |
| 503 | assertEquals(0, mNotificationManagerService.getNotificationRecordCount()); |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | @Test |
| 507 | public void testCancelGroupSummaryMultipleEnqueuedChildrenDoesNotCrash() throws Exception { |
| 508 | final NotificationRecord parent = generateNotificationRecord( |
| 509 | mTestNotificationChannel, 1, "group1", true); |
| 510 | final NotificationRecord parentAsChild = generateNotificationRecord( |
| 511 | mTestNotificationChannel, 1, "group1", false); |
| 512 | final NotificationRecord child = generateNotificationRecord( |
| 513 | mTestNotificationChannel, 2, "group1", false); |
| 514 | |
| 515 | // fully post parent notification |
| 516 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", |
| 517 | parent.sbn.getId(), parent.sbn.getNotification(), parent.sbn.getUserId()); |
| 518 | waitForIdle(); |
| 519 | |
| 520 | // enqueue the child several times |
| 521 | for (int i = 0; i < 10; i++) { |
| 522 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", |
| 523 | child.sbn.getId(), child.sbn.getNotification(), child.sbn.getUserId()); |
| 524 | } |
| 525 | // make the parent a child, which will cancel the child notification |
| 526 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", |
| 527 | parentAsChild.sbn.getId(), parentAsChild.sbn.getNotification(), |
| 528 | parentAsChild.sbn.getUserId()); |
| 529 | waitForIdle(); |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 530 | |
| 531 | assertEquals(0, mNotificationManagerService.getNotificationRecordCount()); |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | @Test |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 535 | public void testCancelAllNotifications_IgnoreForegroundService() throws Exception { |
| 536 | final StatusBarNotification sbn = generateNotificationRecord(null).sbn; |
| 537 | sbn.getNotification().flags |= Notification.FLAG_FOREGROUND_SERVICE; |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 538 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 539 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 540 | mBinderService.cancelAllNotifications(PKG, sbn.getUserId()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 541 | waitForIdle(); |
| 542 | StatusBarNotification[] notifs = |
| 543 | mBinderService.getActiveNotifications(sbn.getPackageName()); |
| 544 | assertEquals(1, notifs.length); |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 545 | assertEquals(1, mNotificationManagerService.getNotificationRecordCount()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 546 | } |
| 547 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 548 | @Test |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 549 | public void testCancelAllNotifications_IgnoreOtherPackages() throws Exception { |
| 550 | final StatusBarNotification sbn = generateNotificationRecord(null).sbn; |
| 551 | sbn.getNotification().flags |= Notification.FLAG_FOREGROUND_SERVICE; |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 552 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 553 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 554 | mBinderService.cancelAllNotifications("other_pkg_name", sbn.getUserId()); |
| 555 | waitForIdle(); |
| 556 | StatusBarNotification[] notifs = |
| 557 | mBinderService.getActiveNotifications(sbn.getPackageName()); |
| 558 | assertEquals(1, notifs.length); |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 559 | assertEquals(1, mNotificationManagerService.getNotificationRecordCount()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 560 | } |
| 561 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 562 | @Test |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 563 | public void testCancelAllNotifications_NullPkgRemovesAll() throws Exception { |
| 564 | final StatusBarNotification sbn = generateNotificationRecord(null).sbn; |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 565 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 566 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 567 | mBinderService.cancelAllNotifications(null, sbn.getUserId()); |
| 568 | waitForIdle(); |
| 569 | StatusBarNotification[] notifs = |
| 570 | mBinderService.getActiveNotifications(sbn.getPackageName()); |
| 571 | assertEquals(0, notifs.length); |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 572 | assertEquals(0, mNotificationManagerService.getNotificationRecordCount()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 573 | } |
| 574 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 575 | @Test |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 576 | public void testCancelAllNotifications_NullPkgIgnoresUserAllNotifications() throws Exception { |
| 577 | final StatusBarNotification sbn = generateNotificationRecord(null).sbn; |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 578 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 579 | sbn.getId(), sbn.getNotification(), UserHandle.USER_ALL); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 580 | // Null pkg is how we signal a user switch. |
| 581 | mBinderService.cancelAllNotifications(null, sbn.getUserId()); |
| 582 | waitForIdle(); |
| 583 | StatusBarNotification[] notifs = |
| 584 | mBinderService.getActiveNotifications(sbn.getPackageName()); |
| 585 | assertEquals(1, notifs.length); |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 586 | assertEquals(1, mNotificationManagerService.getNotificationRecordCount()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 587 | } |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 588 | |
| 589 | @Test |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 590 | public void testAppInitiatedCancelAllNotifications_CancelsNoClearFlag() throws Exception { |
| 591 | final StatusBarNotification sbn = generateNotificationRecord(null).sbn; |
| 592 | sbn.getNotification().flags |= Notification.FLAG_NO_CLEAR; |
| 593 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", |
| 594 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
| 595 | mBinderService.cancelAllNotifications(PKG, sbn.getUserId()); |
| 596 | waitForIdle(); |
| 597 | StatusBarNotification[] notifs = |
| 598 | mBinderService.getActiveNotifications(sbn.getPackageName()); |
| 599 | assertEquals(0, notifs.length); |
| 600 | } |
| 601 | |
| 602 | @Test |
| 603 | public void testCancelAllNotifications_CancelsNoClearFlag() throws Exception { |
| 604 | final NotificationRecord notif = generateNotificationRecord( |
| 605 | mTestNotificationChannel, 1, "group", true); |
| 606 | notif.getNotification().flags |= Notification.FLAG_NO_CLEAR; |
| 607 | mNotificationManagerService.addNotification(notif); |
| 608 | mNotificationManagerService.cancelAllNotificationsInt(uid, 0, PKG, null, 0, 0, true, |
| 609 | notif.getUserId(), 0, null); |
| 610 | waitForIdle(); |
| 611 | StatusBarNotification[] notifs = |
| 612 | mBinderService.getActiveNotifications(notif.sbn.getPackageName()); |
| 613 | assertEquals(0, notifs.length); |
| 614 | } |
| 615 | |
| 616 | @Test |
| 617 | public void testUserInitiatedCancelAllOnClearAll_NoClearFlag() throws Exception { |
| 618 | final NotificationRecord notif = generateNotificationRecord( |
| 619 | mTestNotificationChannel, 1, "group", true); |
| 620 | notif.getNotification().flags |= Notification.FLAG_NO_CLEAR; |
| 621 | mNotificationManagerService.addNotification(notif); |
| 622 | |
| 623 | mNotificationManagerService.mNotificationDelegate.onClearAll(uid, Binder.getCallingPid(), |
| 624 | notif.getUserId()); |
| 625 | waitForIdle(); |
| 626 | StatusBarNotification[] notifs = |
| 627 | mBinderService.getActiveNotifications(notif.sbn.getPackageName()); |
| 628 | assertEquals(1, notifs.length); |
| 629 | } |
| 630 | |
| 631 | @Test |
| 632 | public void testCancelAllCancelNotificationsFromListener_NoClearFlag() throws Exception { |
| 633 | final NotificationRecord parent = generateNotificationRecord( |
| 634 | mTestNotificationChannel, 1, "group", true); |
| 635 | final NotificationRecord child = generateNotificationRecord( |
| 636 | mTestNotificationChannel, 2, "group", false); |
| 637 | final NotificationRecord child2 = generateNotificationRecord( |
| 638 | mTestNotificationChannel, 3, "group", false); |
| 639 | child2.getNotification().flags |= Notification.FLAG_NO_CLEAR; |
| 640 | final NotificationRecord newGroup = generateNotificationRecord( |
| 641 | mTestNotificationChannel, 4, "group2", false); |
| 642 | mNotificationManagerService.addNotification(parent); |
| 643 | mNotificationManagerService.addNotification(child); |
| 644 | mNotificationManagerService.addNotification(child2); |
| 645 | mNotificationManagerService.addNotification(newGroup); |
| 646 | mNotificationManagerService.getBinderService().cancelNotificationsFromListener(null, null); |
| 647 | waitForIdle(); |
| 648 | StatusBarNotification[] notifs = |
| 649 | mBinderService.getActiveNotifications(parent.sbn.getPackageName()); |
| 650 | assertEquals(1, notifs.length); |
| 651 | } |
| 652 | |
| 653 | @Test |
| 654 | public void testUserInitiatedCancelAllWithGroup_NoClearFlag() throws Exception { |
| 655 | final NotificationRecord parent = generateNotificationRecord( |
| 656 | mTestNotificationChannel, 1, "group", true); |
| 657 | final NotificationRecord child = generateNotificationRecord( |
| 658 | mTestNotificationChannel, 2, "group", false); |
| 659 | final NotificationRecord child2 = generateNotificationRecord( |
| 660 | mTestNotificationChannel, 3, "group", false); |
| 661 | child2.getNotification().flags |= Notification.FLAG_NO_CLEAR; |
| 662 | final NotificationRecord newGroup = generateNotificationRecord( |
| 663 | mTestNotificationChannel, 4, "group2", false); |
| 664 | mNotificationManagerService.addNotification(parent); |
| 665 | mNotificationManagerService.addNotification(child); |
| 666 | mNotificationManagerService.addNotification(child2); |
| 667 | mNotificationManagerService.addNotification(newGroup); |
| 668 | mNotificationManagerService.mNotificationDelegate.onClearAll(uid, Binder.getCallingPid(), |
| 669 | parent.getUserId()); |
| 670 | waitForIdle(); |
| 671 | StatusBarNotification[] notifs = |
| 672 | mBinderService.getActiveNotifications(parent.sbn.getPackageName()); |
| 673 | assertEquals(1, notifs.length); |
| 674 | } |
| 675 | |
| 676 | @Test |
Geoffrey Pitsch | 415e454 | 2017-04-10 13:12:58 -0400 | [diff] [blame] | 677 | public void testRemoveForegroundServiceFlag_ImmediatelyAfterEnqueue() throws Exception { |
| 678 | final StatusBarNotification sbn = generateNotificationRecord(null).sbn; |
| 679 | sbn.getNotification().flags |= Notification.FLAG_FOREGROUND_SERVICE; |
| 680 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", null, |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 681 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
Geoffrey Pitsch | 415e454 | 2017-04-10 13:12:58 -0400 | [diff] [blame] | 682 | mInternalService.removeForegroundServiceFlagFromNotification(PKG, sbn.getId(), |
| 683 | sbn.getUserId()); |
| 684 | waitForIdle(); |
| 685 | StatusBarNotification[] notifs = |
| 686 | mBinderService.getActiveNotifications(sbn.getPackageName()); |
| 687 | assertEquals(0, notifs[0].getNotification().flags & Notification.FLAG_FOREGROUND_SERVICE); |
| 688 | } |
| 689 | |
| 690 | @Test |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 691 | public void testCancelAfterSecondEnqueueDoesNotSpecifyForegroundFlag() throws Exception { |
| 692 | final StatusBarNotification sbn = generateNotificationRecord(null).sbn; |
| 693 | sbn.getNotification().flags = |
| 694 | Notification.FLAG_ONGOING_EVENT | Notification.FLAG_FOREGROUND_SERVICE; |
| 695 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", |
| 696 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
| 697 | sbn.getNotification().flags = Notification.FLAG_ONGOING_EVENT; |
| 698 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", |
| 699 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
| 700 | mBinderService.cancelNotificationWithTag(PKG, "tag", sbn.getId(), sbn.getUserId()); |
| 701 | waitForIdle(); |
| 702 | assertEquals(0, mBinderService.getActiveNotifications(sbn.getPackageName()).length); |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 703 | assertEquals(0, mNotificationManagerService.getNotificationRecordCount()); |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | @Test |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 707 | public void testFindGroupNotificationsLocked() throws Exception { |
| 708 | // make sure the same notification can be found in both lists and returned |
| 709 | final NotificationRecord group1 = generateNotificationRecord( |
| 710 | mTestNotificationChannel, 1, "group1", true); |
| 711 | mNotificationManagerService.addEnqueuedNotification(group1); |
| 712 | mNotificationManagerService.addNotification(group1); |
| 713 | |
| 714 | // should not be returned |
| 715 | final NotificationRecord group2 = generateNotificationRecord( |
| 716 | mTestNotificationChannel, 2, "group2", true); |
| 717 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", null, |
| 718 | group2.sbn.getId(), group2.sbn.getNotification(), group2.sbn.getUserId()); |
| 719 | waitForIdle(); |
| 720 | |
| 721 | // should not be returned |
| 722 | final NotificationRecord nonGroup = generateNotificationRecord( |
| 723 | mTestNotificationChannel, 3, null, false); |
| 724 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", null, |
| 725 | nonGroup.sbn.getId(), nonGroup.sbn.getNotification(), nonGroup.sbn.getUserId()); |
| 726 | waitForIdle(); |
| 727 | |
| 728 | // same group, child, should be returned |
| 729 | final NotificationRecord group1Child = generateNotificationRecord( |
| 730 | mTestNotificationChannel, 4, "group1", false); |
| 731 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", null, group1Child.sbn.getId(), |
| 732 | group1Child.sbn.getNotification(), group1Child.sbn.getUserId()); |
| 733 | waitForIdle(); |
| 734 | |
| 735 | List<NotificationRecord> inGroup1 = |
| 736 | mNotificationManagerService.findGroupNotificationsLocked(PKG, group1.getGroupKey(), |
| 737 | group1.sbn.getUserId()); |
| 738 | assertEquals(3, inGroup1.size()); |
| 739 | for (NotificationRecord record : inGroup1) { |
| 740 | assertTrue(record.getGroupKey().equals(group1.getGroupKey())); |
| 741 | assertTrue(record.sbn.getId() == 1 || record.sbn.getId() == 4); |
| 742 | } |
| 743 | } |
| 744 | |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 745 | @Test |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 746 | public void testTvExtenderChannelOverride_onTv() throws Exception { |
| 747 | mNotificationManagerService.setIsTelevision(true); |
| 748 | mNotificationManagerService.setRankingHelper(mRankingHelper); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 749 | when(mRankingHelper.getNotificationChannel( |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 750 | anyString(), anyInt(), eq("foo"), anyBoolean())).thenReturn( |
| 751 | new NotificationChannel("foo", "foo", NotificationManager.IMPORTANCE_HIGH)); |
| 752 | |
Julia Reynolds | bad4297 | 2017-04-25 13:52:49 -0400 | [diff] [blame] | 753 | Notification.TvExtender tv = new Notification.TvExtender().setChannelId("foo"); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 754 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", 0, |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 755 | generateNotificationRecord(null, tv).getNotification(), 0); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 756 | verify(mRankingHelper, times(1)).getNotificationChannel( |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 757 | anyString(), anyInt(), eq("foo"), anyBoolean()); |
| 758 | } |
| 759 | |
| 760 | @Test |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 761 | public void testTvExtenderChannelOverride_notOnTv() throws Exception { |
| 762 | mNotificationManagerService.setIsTelevision(false); |
| 763 | mNotificationManagerService.setRankingHelper(mRankingHelper); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 764 | when(mRankingHelper.getNotificationChannel( |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 765 | anyString(), anyInt(), anyString(), anyBoolean())).thenReturn( |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 766 | mTestNotificationChannel); |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 767 | |
Julia Reynolds | bad4297 | 2017-04-25 13:52:49 -0400 | [diff] [blame] | 768 | Notification.TvExtender tv = new Notification.TvExtender().setChannelId("foo"); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 769 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", 0, |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 770 | generateNotificationRecord(null, tv).getNotification(), 0); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 771 | verify(mRankingHelper, times(1)).getNotificationChannel( |
| 772 | anyString(), anyInt(), eq(mTestNotificationChannel.getId()), anyBoolean()); |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 773 | } |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 774 | |
| 775 | @Test |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 776 | public void testCreateChannelNotifyListener() throws Exception { |
| 777 | List<String> associations = new ArrayList<>(); |
| 778 | associations.add("a"); |
| 779 | when(mCompanionMgr.getAssociations(PKG, uid)).thenReturn(associations); |
| 780 | mNotificationManagerService.setRankingHelper(mRankingHelper); |
| 781 | when(mRankingHelper.getNotificationChannel(eq(PKG), anyInt(), |
| 782 | eq(mTestNotificationChannel.getId()), anyBoolean())) |
| 783 | .thenReturn(mTestNotificationChannel); |
| 784 | NotificationChannel channel2 = new NotificationChannel("a", "b", IMPORTANCE_LOW); |
| 785 | when(mRankingHelper.getNotificationChannel(eq(PKG), anyInt(), |
| 786 | eq(channel2.getId()), anyBoolean())) |
| 787 | .thenReturn(channel2); |
| 788 | |
| 789 | reset(mNotificationListeners); |
| 790 | mBinderService.createNotificationChannels(PKG, |
| 791 | new ParceledListSlice(Arrays.asList(mTestNotificationChannel, channel2))); |
| 792 | verify(mNotificationListeners, times(1)).notifyNotificationChannelChanged(eq(PKG), |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 793 | eq(Process.myUserHandle()), eq(mTestNotificationChannel), |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 794 | eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_ADDED)); |
| 795 | verify(mNotificationListeners, times(1)).notifyNotificationChannelChanged(eq(PKG), |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 796 | eq(Process.myUserHandle()), eq(channel2), |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 797 | eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_ADDED)); |
| 798 | } |
| 799 | |
| 800 | @Test |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 801 | public void testCreateChannelGroupNotifyListener() throws Exception { |
| 802 | List<String> associations = new ArrayList<>(); |
| 803 | associations.add("a"); |
| 804 | when(mCompanionMgr.getAssociations(PKG, uid)).thenReturn(associations); |
| 805 | mNotificationManagerService.setRankingHelper(mRankingHelper); |
| 806 | NotificationChannelGroup group1 = new NotificationChannelGroup("a", "b"); |
| 807 | NotificationChannelGroup group2 = new NotificationChannelGroup("n", "m"); |
| 808 | |
| 809 | reset(mNotificationListeners); |
| 810 | mBinderService.createNotificationChannelGroups(PKG, |
| 811 | new ParceledListSlice(Arrays.asList(group1, group2))); |
| 812 | verify(mNotificationListeners, times(1)).notifyNotificationChannelGroupChanged(eq(PKG), |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 813 | eq(Process.myUserHandle()), eq(group1), |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 814 | eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_ADDED)); |
| 815 | verify(mNotificationListeners, times(1)).notifyNotificationChannelGroupChanged(eq(PKG), |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 816 | eq(Process.myUserHandle()), eq(group2), |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 817 | eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_ADDED)); |
| 818 | } |
| 819 | |
| 820 | @Test |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 821 | public void testUpdateChannelNotifyListener() throws Exception { |
| 822 | List<String> associations = new ArrayList<>(); |
| 823 | associations.add("a"); |
| 824 | when(mCompanionMgr.getAssociations(PKG, uid)).thenReturn(associations); |
| 825 | mNotificationManagerService.setRankingHelper(mRankingHelper); |
| 826 | mTestNotificationChannel.setLightColor(Color.CYAN); |
| 827 | when(mRankingHelper.getNotificationChannel(eq(PKG), anyInt(), |
| 828 | eq(mTestNotificationChannel.getId()), anyBoolean())) |
| 829 | .thenReturn(mTestNotificationChannel); |
| 830 | |
| 831 | reset(mNotificationListeners); |
| 832 | mBinderService.updateNotificationChannelForPackage(PKG, 0, mTestNotificationChannel); |
| 833 | verify(mNotificationListeners, times(1)).notifyNotificationChannelChanged(eq(PKG), |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 834 | eq(Process.myUserHandle()), eq(mTestNotificationChannel), |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 835 | eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_UPDATED)); |
| 836 | } |
| 837 | |
| 838 | @Test |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 839 | public void testDeleteChannelNotifyListener() throws Exception { |
| 840 | List<String> associations = new ArrayList<>(); |
| 841 | associations.add("a"); |
| 842 | when(mCompanionMgr.getAssociations(PKG, uid)).thenReturn(associations); |
| 843 | mNotificationManagerService.setRankingHelper(mRankingHelper); |
| 844 | when(mRankingHelper.getNotificationChannel(eq(PKG), anyInt(), |
| 845 | eq(mTestNotificationChannel.getId()), anyBoolean())) |
| 846 | .thenReturn(mTestNotificationChannel); |
| 847 | reset(mNotificationListeners); |
| 848 | mBinderService.deleteNotificationChannel(PKG, mTestNotificationChannel.getId()); |
| 849 | verify(mNotificationListeners, times(1)).notifyNotificationChannelChanged(eq(PKG), |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 850 | eq(Process.myUserHandle()), eq(mTestNotificationChannel), |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 851 | eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_DELETED)); |
| 852 | } |
| 853 | |
| 854 | @Test |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 855 | public void testDeleteChannelGroupNotifyListener() throws Exception { |
| 856 | List<String> associations = new ArrayList<>(); |
| 857 | associations.add("a"); |
| 858 | when(mCompanionMgr.getAssociations(PKG, uid)).thenReturn(associations); |
| 859 | NotificationChannelGroup ncg = new NotificationChannelGroup("a", "b/c"); |
| 860 | mNotificationManagerService.setRankingHelper(mRankingHelper); |
| 861 | when(mRankingHelper.getNotificationChannelGroup(eq(ncg.getId()), eq(PKG), anyInt())) |
| 862 | .thenReturn(ncg); |
| 863 | reset(mNotificationListeners); |
| 864 | mBinderService.deleteNotificationChannelGroup(PKG, ncg.getId()); |
| 865 | verify(mNotificationListeners, times(1)).notifyNotificationChannelGroupChanged(eq(PKG), |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 866 | eq(Process.myUserHandle()), eq(ncg), |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 867 | eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_DELETED)); |
| 868 | } |
| 869 | |
| 870 | @Test |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 871 | public void testUpdateNotificationChannelFromPrivilegedListener_success() throws Exception { |
| 872 | mNotificationManagerService.setRankingHelper(mRankingHelper); |
| 873 | List<String> associations = new ArrayList<>(); |
| 874 | associations.add("a"); |
| 875 | when(mCompanionMgr.getAssociations(PKG, uid)).thenReturn(associations); |
| 876 | |
| 877 | mBinderService.updateNotificationChannelFromPrivilegedListener( |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 878 | null, PKG, Process.myUserHandle(), mTestNotificationChannel); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 879 | |
| 880 | verify(mRankingHelper, times(1)).updateNotificationChannel(anyString(), anyInt(), any()); |
| 881 | |
| 882 | verify(mNotificationListeners, never()).notifyNotificationChannelChanged(eq(PKG), |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 883 | eq(Process.myUserHandle()), eq(mTestNotificationChannel), |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 884 | eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_UPDATED)); |
| 885 | } |
| 886 | |
| 887 | @Test |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 888 | public void testUpdateNotificationChannelFromPrivilegedListener_noAccess() throws Exception { |
| 889 | mNotificationManagerService.setRankingHelper(mRankingHelper); |
| 890 | List<String> associations = new ArrayList<>(); |
| 891 | when(mCompanionMgr.getAssociations(PKG, uid)).thenReturn(associations); |
| 892 | |
| 893 | try { |
| 894 | mBinderService.updateNotificationChannelFromPrivilegedListener( |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 895 | null, PKG, Process.myUserHandle(), mTestNotificationChannel); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 896 | fail("listeners that don't have a companion device shouldn't be able to call this"); |
| 897 | } catch (SecurityException e) { |
| 898 | // pass |
| 899 | } |
| 900 | |
| 901 | verify(mRankingHelper, never()).updateNotificationChannel(anyString(), anyInt(), any()); |
| 902 | |
| 903 | verify(mNotificationListeners, never()).notifyNotificationChannelChanged(eq(PKG), |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 904 | eq(Process.myUserHandle()), eq(mTestNotificationChannel), |
| 905 | eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_UPDATED)); |
| 906 | } |
| 907 | |
| 908 | @Test |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 909 | public void testUpdateNotificationChannelFromPrivilegedListener_badUser() throws Exception { |
| 910 | mNotificationManagerService.setRankingHelper(mRankingHelper); |
| 911 | List<String> associations = new ArrayList<>(); |
| 912 | associations.add("a"); |
| 913 | when(mCompanionMgr.getAssociations(PKG, uid)).thenReturn(associations); |
| 914 | mListener = mock(ManagedServices.ManagedServiceInfo.class); |
Julia Reynolds | 4da7970 | 2017-06-01 11:06:10 -0400 | [diff] [blame] | 915 | mListener.component = new ComponentName(PKG, PKG); |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 916 | when(mListener.enabledAndUserMatches(anyInt())).thenReturn(false); |
| 917 | when(mNotificationListeners.checkServiceTokenLocked(any())).thenReturn(mListener); |
| 918 | |
| 919 | try { |
| 920 | mBinderService.updateNotificationChannelFromPrivilegedListener( |
| 921 | null, PKG, UserHandle.ALL, mTestNotificationChannel); |
| 922 | fail("incorrectly allowed a change to a user listener cannot see"); |
| 923 | } catch (SecurityException e) { |
| 924 | // pass |
| 925 | } |
| 926 | |
| 927 | verify(mRankingHelper, never()).updateNotificationChannel(anyString(), anyInt(), any()); |
| 928 | |
| 929 | verify(mNotificationListeners, never()).notifyNotificationChannelChanged(eq(PKG), |
| 930 | eq(Process.myUserHandle()), eq(mTestNotificationChannel), |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 931 | eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_UPDATED)); |
| 932 | } |
| 933 | |
| 934 | @Test |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 935 | public void testGetNotificationChannelFromPrivilegedListener_success() throws Exception { |
| 936 | mNotificationManagerService.setRankingHelper(mRankingHelper); |
| 937 | List<String> associations = new ArrayList<>(); |
| 938 | associations.add("a"); |
| 939 | when(mCompanionMgr.getAssociations(PKG, uid)).thenReturn(associations); |
| 940 | |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 941 | mBinderService.getNotificationChannelsFromPrivilegedListener( |
| 942 | null, PKG, Process.myUserHandle()); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 943 | |
| 944 | verify(mRankingHelper, times(1)).getNotificationChannels( |
| 945 | anyString(), anyInt(), anyBoolean()); |
| 946 | } |
| 947 | |
| 948 | @Test |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 949 | public void testGetNotificationChannelFromPrivilegedListener_noAccess() throws Exception { |
| 950 | mNotificationManagerService.setRankingHelper(mRankingHelper); |
| 951 | List<String> associations = new ArrayList<>(); |
| 952 | when(mCompanionMgr.getAssociations(PKG, uid)).thenReturn(associations); |
| 953 | |
| 954 | try { |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 955 | mBinderService.getNotificationChannelsFromPrivilegedListener( |
| 956 | null, PKG, Process.myUserHandle()); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 957 | fail("listeners that don't have a companion device shouldn't be able to call this"); |
| 958 | } catch (SecurityException e) { |
| 959 | // pass |
| 960 | } |
| 961 | |
| 962 | verify(mRankingHelper, never()).getNotificationChannels( |
| 963 | anyString(), anyInt(), anyBoolean()); |
| 964 | } |
| 965 | |
| 966 | @Test |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 967 | public void testGetNotificationChannelFromPrivilegedListener_badUser() throws Exception { |
| 968 | mNotificationManagerService.setRankingHelper(mRankingHelper); |
| 969 | List<String> associations = new ArrayList<>(); |
| 970 | associations.add("a"); |
| 971 | when(mCompanionMgr.getAssociations(PKG, uid)).thenReturn(associations); |
| 972 | mListener = mock(ManagedServices.ManagedServiceInfo.class); |
| 973 | when(mListener.enabledAndUserMatches(anyInt())).thenReturn(false); |
| 974 | when(mNotificationListeners.checkServiceTokenLocked(any())).thenReturn(mListener); |
| 975 | |
| 976 | try { |
| 977 | mBinderService.getNotificationChannelsFromPrivilegedListener( |
| 978 | null, PKG, Process.myUserHandle()); |
| 979 | fail("listener getting channels from a user they cannot see"); |
| 980 | } catch (SecurityException e) { |
| 981 | // pass |
| 982 | } |
| 983 | |
| 984 | verify(mRankingHelper, never()).getNotificationChannels( |
| 985 | anyString(), anyInt(), anyBoolean()); |
| 986 | } |
| 987 | |
| 988 | @Test |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 989 | public void testGetNotificationChannelGroupsFromPrivilegedListener_success() throws Exception { |
| 990 | mNotificationManagerService.setRankingHelper(mRankingHelper); |
| 991 | List<String> associations = new ArrayList<>(); |
| 992 | associations.add("a"); |
| 993 | when(mCompanionMgr.getAssociations(PKG, uid)).thenReturn(associations); |
| 994 | |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 995 | mBinderService.getNotificationChannelGroupsFromPrivilegedListener( |
| 996 | null, PKG, Process.myUserHandle()); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 997 | |
| 998 | verify(mRankingHelper, times(1)).getNotificationChannelGroups(anyString(), anyInt()); |
| 999 | } |
| 1000 | |
| 1001 | @Test |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1002 | public void testGetNotificationChannelGroupsFromPrivilegedListener_noAccess() throws Exception { |
| 1003 | mNotificationManagerService.setRankingHelper(mRankingHelper); |
| 1004 | List<String> associations = new ArrayList<>(); |
| 1005 | when(mCompanionMgr.getAssociations(PKG, uid)).thenReturn(associations); |
| 1006 | |
| 1007 | try { |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 1008 | mBinderService.getNotificationChannelGroupsFromPrivilegedListener( |
| 1009 | null, PKG, Process.myUserHandle()); |
| 1010 | fail("listeners that don't have a companion device shouldn't be able to call this"); |
| 1011 | } catch (SecurityException e) { |
| 1012 | // pass |
| 1013 | } |
| 1014 | |
| 1015 | verify(mRankingHelper, never()).getNotificationChannelGroups(anyString(), anyInt()); |
| 1016 | } |
| 1017 | |
| 1018 | @Test |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 1019 | public void testGetNotificationChannelGroupsFromPrivilegedListener_badUser() throws Exception { |
| 1020 | mNotificationManagerService.setRankingHelper(mRankingHelper); |
| 1021 | List<String> associations = new ArrayList<>(); |
| 1022 | when(mCompanionMgr.getAssociations(PKG, uid)).thenReturn(associations); |
| 1023 | mListener = mock(ManagedServices.ManagedServiceInfo.class); |
| 1024 | when(mListener.enabledAndUserMatches(anyInt())).thenReturn(false); |
| 1025 | when(mNotificationListeners.checkServiceTokenLocked(any())).thenReturn(mListener); |
| 1026 | |
| 1027 | try { |
| 1028 | mBinderService.getNotificationChannelGroupsFromPrivilegedListener( |
| 1029 | null, PKG, Process.myUserHandle()); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1030 | fail("listeners that don't have a companion device shouldn't be able to call this"); |
| 1031 | } catch (SecurityException e) { |
| 1032 | // pass |
| 1033 | } |
| 1034 | |
| 1035 | verify(mRankingHelper, never()).getNotificationChannelGroups(anyString(), anyInt()); |
| 1036 | } |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 1037 | |
| 1038 | @Test |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 1039 | public void testHasCompanionDevice_failure() throws Exception { |
| 1040 | when(mCompanionMgr.getAssociations(anyString(), anyInt())).thenThrow( |
| 1041 | new IllegalArgumentException()); |
| 1042 | mNotificationManagerService.hasCompanionDevice(mListener); |
| 1043 | } |
Julia Reynolds | 727a728 | 2017-04-13 10:54:01 -0400 | [diff] [blame] | 1044 | |
| 1045 | @Test |
Julia Reynolds | 727a728 | 2017-04-13 10:54:01 -0400 | [diff] [blame] | 1046 | public void testHasCompanionDevice_noService() throws Exception { |
| 1047 | mNotificationManagerService = new TestableNotificationManagerService(mContext); |
| 1048 | |
| 1049 | assertFalse(mNotificationManagerService.hasCompanionDevice(mListener)); |
| 1050 | } |
| 1051 | |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 1052 | @Test |
| 1053 | public void testSnoozeRunnable_snoozeNonGrouped() throws Exception { |
| 1054 | final NotificationRecord nonGrouped = generateNotificationRecord( |
| 1055 | mTestNotificationChannel, 1, null, false); |
| 1056 | final NotificationRecord grouped = generateNotificationRecord( |
| 1057 | mTestNotificationChannel, 2, "group", false); |
| 1058 | mNotificationManagerService.addNotification(grouped); |
| 1059 | mNotificationManagerService.addNotification(nonGrouped); |
| 1060 | |
| 1061 | NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable = |
| 1062 | mNotificationManagerService.new SnoozeNotificationRunnable( |
| 1063 | nonGrouped.getKey(), 100, null); |
| 1064 | snoozeNotificationRunnable.run(); |
| 1065 | |
| 1066 | // only snooze the one notification |
| 1067 | verify(mSnoozeHelper, times(1)).snooze(any(NotificationRecord.class), anyLong()); |
| 1068 | } |
| 1069 | |
| 1070 | @Test |
| 1071 | public void testSnoozeRunnable_snoozeSummary_withChildren() throws Exception { |
| 1072 | final NotificationRecord parent = generateNotificationRecord( |
| 1073 | mTestNotificationChannel, 1, "group", true); |
| 1074 | final NotificationRecord child = generateNotificationRecord( |
| 1075 | mTestNotificationChannel, 2, "group", false); |
| 1076 | final NotificationRecord child2 = generateNotificationRecord( |
| 1077 | mTestNotificationChannel, 3, "group", false); |
| 1078 | mNotificationManagerService.addNotification(parent); |
| 1079 | mNotificationManagerService.addNotification(child); |
| 1080 | mNotificationManagerService.addNotification(child2); |
| 1081 | |
| 1082 | NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable = |
| 1083 | mNotificationManagerService.new SnoozeNotificationRunnable( |
| 1084 | parent.getKey(), 100, null); |
| 1085 | snoozeNotificationRunnable.run(); |
| 1086 | |
| 1087 | // snooze parent and children |
| 1088 | verify(mSnoozeHelper, times(3)).snooze(any(NotificationRecord.class), anyLong()); |
| 1089 | } |
| 1090 | |
| 1091 | @Test |
| 1092 | public void testSnoozeRunnable_snoozeGroupChild_fellowChildren() throws Exception { |
| 1093 | final NotificationRecord parent = generateNotificationRecord( |
| 1094 | mTestNotificationChannel, 1, "group", true); |
| 1095 | final NotificationRecord child = generateNotificationRecord( |
| 1096 | mTestNotificationChannel, 2, "group", false); |
| 1097 | final NotificationRecord child2 = generateNotificationRecord( |
| 1098 | mTestNotificationChannel, 3, "group", false); |
| 1099 | mNotificationManagerService.addNotification(parent); |
| 1100 | mNotificationManagerService.addNotification(child); |
| 1101 | mNotificationManagerService.addNotification(child2); |
| 1102 | |
| 1103 | NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable = |
| 1104 | mNotificationManagerService.new SnoozeNotificationRunnable( |
| 1105 | child2.getKey(), 100, null); |
| 1106 | snoozeNotificationRunnable.run(); |
| 1107 | |
| 1108 | // only snooze the one child |
| 1109 | verify(mSnoozeHelper, times(1)).snooze(any(NotificationRecord.class), anyLong()); |
| 1110 | } |
| 1111 | |
| 1112 | @Test |
| 1113 | public void testSnoozeRunnable_snoozeGroupChild_onlyChildOfSummary() throws Exception { |
| 1114 | final NotificationRecord parent = generateNotificationRecord( |
| 1115 | mTestNotificationChannel, 1, "group", true); |
| 1116 | assertTrue(parent.sbn.getNotification().isGroupSummary()); |
| 1117 | final NotificationRecord child = generateNotificationRecord( |
| 1118 | mTestNotificationChannel, 2, "group", false); |
| 1119 | mNotificationManagerService.addNotification(parent); |
| 1120 | mNotificationManagerService.addNotification(child); |
| 1121 | |
| 1122 | NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable = |
| 1123 | mNotificationManagerService.new SnoozeNotificationRunnable( |
| 1124 | child.getKey(), 100, null); |
| 1125 | snoozeNotificationRunnable.run(); |
| 1126 | |
| 1127 | // snooze child and summary |
| 1128 | verify(mSnoozeHelper, times(2)).snooze(any(NotificationRecord.class), anyLong()); |
| 1129 | } |
| 1130 | |
| 1131 | @Test |
| 1132 | public void testSnoozeRunnable_snoozeGroupChild_noOthersInGroup() throws Exception { |
| 1133 | final NotificationRecord child = generateNotificationRecord( |
| 1134 | mTestNotificationChannel, 2, "group", false); |
| 1135 | mNotificationManagerService.addNotification(child); |
| 1136 | |
| 1137 | NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable = |
| 1138 | mNotificationManagerService.new SnoozeNotificationRunnable( |
| 1139 | child.getKey(), 100, null); |
| 1140 | snoozeNotificationRunnable.run(); |
| 1141 | |
| 1142 | // snooze child only |
| 1143 | verify(mSnoozeHelper, times(1)).snooze(any(NotificationRecord.class), anyLong()); |
| 1144 | } |
| 1145 | |
| 1146 | @Test |
| 1147 | public void testPostGroupChild_unsnoozeParent() throws Exception { |
| 1148 | final NotificationRecord child = generateNotificationRecord( |
| 1149 | mTestNotificationChannel, 2, "group", false); |
| 1150 | |
| 1151 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", null, |
| 1152 | child.sbn.getId(), child.sbn.getNotification(), child.sbn.getUserId()); |
| 1153 | waitForIdle(); |
| 1154 | |
| 1155 | verify(mSnoozeHelper, times(1)).repostGroupSummary( |
| 1156 | anyString(), anyInt(), eq(child.getGroupKey())); |
| 1157 | } |
| 1158 | |
| 1159 | @Test |
| 1160 | public void testPostNonGroup_noUnsnoozing() throws Exception { |
| 1161 | final NotificationRecord record = generateNotificationRecord( |
| 1162 | mTestNotificationChannel, 2, null, false); |
| 1163 | |
| 1164 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", null, |
| 1165 | record.sbn.getId(), record.sbn.getNotification(), record.sbn.getUserId()); |
| 1166 | waitForIdle(); |
| 1167 | |
| 1168 | verify(mSnoozeHelper, never()).repostGroupSummary(anyString(), anyInt(), anyString()); |
| 1169 | } |
| 1170 | |
| 1171 | @Test |
| 1172 | public void testPostGroupSummary_noUnsnoozing() throws Exception { |
| 1173 | final NotificationRecord parent = generateNotificationRecord( |
| 1174 | mTestNotificationChannel, 2, "group", true); |
| 1175 | |
| 1176 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", null, |
| 1177 | parent.sbn.getId(), parent.sbn.getNotification(), parent.sbn.getUserId()); |
| 1178 | waitForIdle(); |
| 1179 | |
| 1180 | verify(mSnoozeHelper, never()).repostGroupSummary(anyString(), anyInt(), anyString()); |
| 1181 | } |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1182 | |
| 1183 | @Test |
| 1184 | public void testSetListenerAccess() throws Exception { |
| 1185 | ComponentName c = ComponentName.unflattenFromString("package/Component"); |
| 1186 | try { |
| 1187 | mBinderService.setNotificationListenerAccessGranted(c, true); |
| 1188 | } catch (SecurityException e) { |
| 1189 | if (!e.getMessage().contains("Permission Denial: not allowed to send broadcast")) { |
| 1190 | throw e; |
| 1191 | } |
| 1192 | } |
| 1193 | |
| 1194 | verify(mNotificationListeners, times(1)).setPackageOrComponentEnabled( |
| 1195 | c.flattenToString(), 0, true, true); |
| 1196 | verify(mConditionProviders, times(1)).setPackageOrComponentEnabled( |
| 1197 | c.flattenToString(), 0, false, true); |
| 1198 | verify(mNotificationAssistants, never()).setPackageOrComponentEnabled( |
| 1199 | any(), anyInt(), anyBoolean(), anyBoolean()); |
| 1200 | } |
| 1201 | |
| 1202 | @Test |
| 1203 | public void testSetAssistantAccess() throws Exception { |
| 1204 | ComponentName c = ComponentName.unflattenFromString("package/Component"); |
| 1205 | try { |
| 1206 | mBinderService.setNotificationAssistantAccessGranted(c, true); |
| 1207 | } catch (SecurityException e) { |
| 1208 | if (!e.getMessage().contains("Permission Denial: not allowed to send broadcast")) { |
| 1209 | throw e; |
| 1210 | } |
| 1211 | } |
| 1212 | |
| 1213 | verify(mNotificationAssistants, times(1)).setPackageOrComponentEnabled( |
| 1214 | c.flattenToString(), 0, true, true); |
| 1215 | verify(mConditionProviders, times(1)).setPackageOrComponentEnabled( |
| 1216 | c.flattenToString(), 0, false, true); |
| 1217 | verify(mNotificationListeners, never()).setPackageOrComponentEnabled( |
| 1218 | any(), anyInt(), anyBoolean(), anyBoolean()); |
| 1219 | } |
| 1220 | |
| 1221 | @Test |
| 1222 | public void testSetDndAccess() throws Exception { |
| 1223 | ComponentName c = ComponentName.unflattenFromString("package/Component"); |
| 1224 | try { |
| 1225 | mBinderService.setNotificationPolicyAccessGranted(c.getPackageName(), true); |
| 1226 | } catch (SecurityException e) { |
| 1227 | if (!e.getMessage().contains("Permission Denial: not allowed to send broadcast")) { |
| 1228 | throw e; |
| 1229 | } |
| 1230 | } |
| 1231 | |
| 1232 | verify(mConditionProviders, times(1)).setPackageOrComponentEnabled( |
| 1233 | c.getPackageName(), 0, true, true); |
| 1234 | verify(mNotificationAssistants, never()).setPackageOrComponentEnabled( |
| 1235 | any(), anyInt(), anyBoolean(), anyBoolean()); |
| 1236 | verify(mNotificationListeners, never()).setPackageOrComponentEnabled( |
| 1237 | any(), anyInt(), anyBoolean(), anyBoolean()); |
| 1238 | } |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 1239 | |
| 1240 | @Test |
| 1241 | public void testSetListenerAccess_doesNothingOnLowRam() throws Exception { |
| 1242 | when(mActivityManager.isLowRamDevice()).thenReturn(true); |
| 1243 | ComponentName c = ComponentName.unflattenFromString("package/Component"); |
| 1244 | mBinderService.setNotificationListenerAccessGranted(c, true); |
| 1245 | |
| 1246 | verify(mNotificationListeners, never()).setPackageOrComponentEnabled( |
| 1247 | c.flattenToString(), 0, true, true); |
| 1248 | verify(mConditionProviders, never()).setPackageOrComponentEnabled( |
| 1249 | c.flattenToString(), 0, false, true); |
| 1250 | verify(mNotificationAssistants, never()).setPackageOrComponentEnabled( |
| 1251 | any(), anyInt(), anyBoolean(), anyBoolean()); |
| 1252 | } |
| 1253 | |
| 1254 | @Test |
| 1255 | public void testSetAssistantAccess_doesNothingOnLowRam() throws Exception { |
| 1256 | when(mActivityManager.isLowRamDevice()).thenReturn(true); |
| 1257 | ComponentName c = ComponentName.unflattenFromString("package/Component"); |
| 1258 | mBinderService.setNotificationAssistantAccessGranted(c, true); |
| 1259 | |
| 1260 | |
| 1261 | verify(mNotificationListeners, never()).setPackageOrComponentEnabled( |
| 1262 | c.flattenToString(), 0, true, true); |
| 1263 | verify(mConditionProviders, never()).setPackageOrComponentEnabled( |
| 1264 | c.flattenToString(), 0, false, true); |
| 1265 | verify(mNotificationAssistants, never()).setPackageOrComponentEnabled( |
| 1266 | any(), anyInt(), anyBoolean(), anyBoolean()); |
| 1267 | } |
| 1268 | |
| 1269 | @Test |
| 1270 | public void testSetDndAccess_doesNothingOnLowRam() throws Exception { |
| 1271 | when(mActivityManager.isLowRamDevice()).thenReturn(true); |
| 1272 | ComponentName c = ComponentName.unflattenFromString("package/Component"); |
| 1273 | mBinderService.setNotificationPolicyAccessGranted(c.getPackageName(), true); |
| 1274 | |
| 1275 | verify(mNotificationListeners, never()).setPackageOrComponentEnabled( |
| 1276 | c.flattenToString(), 0, true, true); |
| 1277 | verify(mConditionProviders, never()).setPackageOrComponentEnabled( |
| 1278 | c.flattenToString(), 0, false, true); |
| 1279 | verify(mNotificationAssistants, never()).setPackageOrComponentEnabled( |
| 1280 | any(), anyInt(), anyBoolean(), anyBoolean()); |
| 1281 | } |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 1282 | |
| 1283 | @Test |
| 1284 | public void testOnlyAutogroupIfGroupChanged_noPriorNoti_autogroups() throws Exception { |
| 1285 | NotificationRecord r = generateNotificationRecord(mTestNotificationChannel, 0, null, false); |
| 1286 | mNotificationManagerService.addEnqueuedNotification(r); |
| 1287 | NotificationManagerService.PostNotificationRunnable runnable = |
| 1288 | mNotificationManagerService.new PostNotificationRunnable(r.getKey()); |
| 1289 | runnable.run(); |
| 1290 | waitForIdle(); |
| 1291 | |
| 1292 | verify(mGroupHelper, times(1)).onNotificationPosted(any()); |
| 1293 | } |
| 1294 | |
| 1295 | @Test |
| 1296 | public void testOnlyAutogroupIfGroupChanged_groupChanged_autogroups() |
| 1297 | throws Exception { |
| 1298 | NotificationRecord r = generateNotificationRecord(mTestNotificationChannel, 0, "group", false); |
| 1299 | mNotificationManagerService.addNotification(r); |
| 1300 | |
| 1301 | r = generateNotificationRecord(mTestNotificationChannel, 0, null, false); |
| 1302 | mNotificationManagerService.addEnqueuedNotification(r); |
| 1303 | NotificationManagerService.PostNotificationRunnable runnable = |
| 1304 | mNotificationManagerService.new PostNotificationRunnable(r.getKey()); |
| 1305 | runnable.run(); |
| 1306 | waitForIdle(); |
| 1307 | |
| 1308 | verify(mGroupHelper, times(1)).onNotificationPosted(any()); |
| 1309 | } |
| 1310 | |
| 1311 | @Test |
| 1312 | public void testOnlyAutogroupIfGroupChanged_noGroupChanged_autogroups() |
| 1313 | throws Exception { |
Julia Reynolds | 4db5955 | 2017-06-30 13:34:01 -0400 | [diff] [blame] | 1314 | NotificationRecord r = generateNotificationRecord(mTestNotificationChannel, 0, "group", |
| 1315 | false); |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 1316 | mNotificationManagerService.addNotification(r); |
| 1317 | mNotificationManagerService.addEnqueuedNotification(r); |
| 1318 | |
| 1319 | NotificationManagerService.PostNotificationRunnable runnable = |
| 1320 | mNotificationManagerService.new PostNotificationRunnable(r.getKey()); |
| 1321 | runnable.run(); |
| 1322 | waitForIdle(); |
| 1323 | |
| 1324 | verify(mGroupHelper, never()).onNotificationPosted(any()); |
| 1325 | } |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 1326 | |
Julia Reynolds | 4db5955 | 2017-06-30 13:34:01 -0400 | [diff] [blame] | 1327 | @Test |
| 1328 | public void testNoFakeColorizedPermission() throws Exception { |
| 1329 | when(mPackageManagerClient.checkPermission(any(), any())).thenReturn(PERMISSION_DENIED); |
| 1330 | Notification.Builder nb = new Notification.Builder(mContext, |
| 1331 | mTestNotificationChannel.getId()) |
| 1332 | .setContentTitle("foo") |
| 1333 | .setColorized(true) |
| 1334 | .setFlag(Notification.FLAG_CAN_COLORIZE, true) |
| 1335 | .setSmallIcon(android.R.drawable.sym_def_app_icon); |
| 1336 | StatusBarNotification sbn = new StatusBarNotification(PKG, PKG, 1, "tag", uid, 0, |
| 1337 | nb.build(), new UserHandle(uid), null, 0); |
| 1338 | NotificationRecord nr = new NotificationRecord(mContext, sbn, mTestNotificationChannel); |
| 1339 | |
| 1340 | mBinderService.enqueueNotificationWithTag(PKG, PKG, null, |
| 1341 | nr.sbn.getId(), nr.sbn.getNotification(), nr.sbn.getUserId()); |
| 1342 | waitForIdle(); |
| 1343 | |
| 1344 | NotificationRecord posted = mNotificationManagerService.findNotificationLocked( |
| 1345 | PKG, null, nr.sbn.getId(), nr.sbn.getUserId()); |
| 1346 | |
| 1347 | assertFalse(posted.getNotification().isColorized()); |
| 1348 | } |
Julia Reynolds | 6ad0aec | 2017-07-05 08:47:03 -0400 | [diff] [blame] | 1349 | |
| 1350 | @Test |
| 1351 | public void testGetNotificationCountLocked() throws Exception { |
| 1352 | for (int i = 0; i < 20; i++) { |
| 1353 | NotificationRecord r = generateNotificationRecord(mTestNotificationChannel, i, null, false); |
| 1354 | mNotificationManagerService.addEnqueuedNotification(r); |
| 1355 | } |
| 1356 | for (int i = 0; i < 20; i++) { |
| 1357 | NotificationRecord r = generateNotificationRecord(mTestNotificationChannel, i, null, false); |
| 1358 | mNotificationManagerService.addNotification(r); |
| 1359 | } |
| 1360 | |
| 1361 | // another package |
| 1362 | Notification n = |
| 1363 | new Notification.Builder(mContext, mTestNotificationChannel.getId()) |
| 1364 | .setSmallIcon(android.R.drawable.sym_def_app_icon) |
| 1365 | .build(); |
| 1366 | |
| 1367 | StatusBarNotification sbn = new StatusBarNotification("a", "a", 0, "tag", uid, 0, |
| 1368 | n, new UserHandle(uid), null, 0); |
| 1369 | NotificationRecord otherPackage = |
| 1370 | new NotificationRecord(mContext, sbn, mTestNotificationChannel); |
| 1371 | mNotificationManagerService.addEnqueuedNotification(otherPackage); |
| 1372 | mNotificationManagerService.addNotification(otherPackage); |
| 1373 | |
| 1374 | // Same notifications are enqueued as posted, everything counts b/c id and tag don't match |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1375 | int userId = new UserHandle(uid).getIdentifier(); |
| 1376 | assertEquals(40, mNotificationManagerService.getNotificationCountLocked(PKG, userId, 0, null)); |
| 1377 | assertEquals(40, mNotificationManagerService.getNotificationCountLocked(PKG, userId, 0, "tag2")); |
| 1378 | assertEquals(2, mNotificationManagerService.getNotificationCountLocked("a", userId, 0, "banana")); |
Julia Reynolds | 6ad0aec | 2017-07-05 08:47:03 -0400 | [diff] [blame] | 1379 | |
| 1380 | // exclude a known notification - it's excluded from only the posted list, not enqueued |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1381 | assertEquals(39, mNotificationManagerService.getNotificationCountLocked(PKG, userId, 0, "tag")); |
| 1382 | } |
| 1383 | |
| 1384 | @Test |
Julia Reynolds | 5171071 | 2017-07-19 13:48:07 -0400 | [diff] [blame^] | 1385 | public void testAddAutogroup_requestsSort() throws Exception { |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1386 | RankingHandler rh = mock(RankingHandler.class); |
| 1387 | mNotificationManagerService.setRankingHandler(rh); |
| 1388 | |
| 1389 | final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
| 1390 | mNotificationManagerService.addNotification(r); |
| 1391 | mNotificationManagerService.addAutogroupKeyLocked(r.getKey()); |
Julia Reynolds | 5171071 | 2017-07-19 13:48:07 -0400 | [diff] [blame^] | 1392 | |
| 1393 | verify(rh, times(1)).requestSort(); |
| 1394 | } |
| 1395 | |
| 1396 | @Test |
| 1397 | public void testRemoveAutogroup_requestsSort() throws Exception { |
| 1398 | RankingHandler rh = mock(RankingHandler.class); |
| 1399 | mNotificationManagerService.setRankingHandler(rh); |
| 1400 | |
| 1401 | final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
| 1402 | r.setOverrideGroupKey("TEST"); |
| 1403 | mNotificationManagerService.addNotification(r); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1404 | mNotificationManagerService.removeAutogroupKeyLocked(r.getKey()); |
| 1405 | |
Julia Reynolds | 5171071 | 2017-07-19 13:48:07 -0400 | [diff] [blame^] | 1406 | verify(rh, times(1)).requestSort(); |
| 1407 | } |
| 1408 | |
| 1409 | @Test |
| 1410 | public void testReaddAutogroup_noSort() throws Exception { |
| 1411 | RankingHandler rh = mock(RankingHandler.class); |
| 1412 | mNotificationManagerService.setRankingHandler(rh); |
| 1413 | |
| 1414 | final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
| 1415 | r.setOverrideGroupKey("TEST"); |
| 1416 | mNotificationManagerService.addNotification(r); |
| 1417 | mNotificationManagerService.addAutogroupKeyLocked(r.getKey()); |
| 1418 | |
| 1419 | verify(rh, never()).requestSort(); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1420 | } |
| 1421 | |
| 1422 | @Test |
| 1423 | public void testHandleRankingSort_sendsUpdateOnSignalExtractorChange() throws Exception { |
| 1424 | mNotificationManagerService.setRankingHelper(mRankingHelper); |
| 1425 | NotificationManagerService.WorkerHandler handler = mock( |
| 1426 | NotificationManagerService.WorkerHandler.class); |
| 1427 | mNotificationManagerService.setHandler(handler); |
| 1428 | |
| 1429 | Map<String, Answer> answers = getSignalExtractorSideEffects(); |
| 1430 | for (String message : answers.keySet()) { |
| 1431 | mNotificationManagerService.clearNotifications(); |
| 1432 | final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
| 1433 | mNotificationManagerService.addNotification(r); |
| 1434 | |
| 1435 | doAnswer(answers.get(message)).when(mRankingHelper).extractSignals(r); |
| 1436 | |
| 1437 | mNotificationManagerService.handleRankingSort(); |
| 1438 | } |
| 1439 | verify(handler, times(answers.size())).scheduleSendRankingUpdate(); |
| 1440 | } |
| 1441 | |
| 1442 | @Test |
| 1443 | public void testHandleRankingSort_noUpdateWhenNoSignalChange() throws Exception { |
| 1444 | mNotificationManagerService.setRankingHelper(mRankingHelper); |
| 1445 | NotificationManagerService.WorkerHandler handler = mock( |
| 1446 | NotificationManagerService.WorkerHandler.class); |
| 1447 | mNotificationManagerService.setHandler(handler); |
| 1448 | |
| 1449 | final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
| 1450 | mNotificationManagerService.addNotification(r); |
| 1451 | |
| 1452 | mNotificationManagerService.handleRankingSort(); |
| 1453 | verify(handler, never()).scheduleSendRankingUpdate(); |
Julia Reynolds | 6ad0aec | 2017-07-05 08:47:03 -0400 | [diff] [blame] | 1454 | } |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 1455 | } |