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 | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 19 | import static android.app.NotificationManager.EXTRA_BLOCKED_STATE; |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 20 | import static android.app.NotificationManager.IMPORTANCE_HIGH; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 21 | import static android.app.NotificationManager.IMPORTANCE_LOW; |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 22 | import static android.app.NotificationManager.IMPORTANCE_MAX; |
Julia Reynolds | 4da7970 | 2017-06-01 11:06:10 -0400 | [diff] [blame] | 23 | import static android.app.NotificationManager.IMPORTANCE_NONE; |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 24 | import static android.app.NotificationManager.IMPORTANCE_UNSPECIFIED; |
Julia Reynolds | e181641 | 2017-10-24 10:39:11 -0400 | [diff] [blame] | 25 | import static android.content.pm.PackageManager.FEATURE_WATCH; |
Julia Reynolds | 4db5955 | 2017-06-30 13:34:01 -0400 | [diff] [blame] | 26 | import static android.content.pm.PackageManager.PERMISSION_DENIED; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 27 | |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 28 | import static junit.framework.Assert.assertEquals; |
Julia Reynolds | 727a728 | 2017-04-13 10:54:01 -0400 | [diff] [blame] | 29 | import static junit.framework.Assert.assertFalse; |
Julia Reynolds | 92febc3 | 2017-10-26 11:30:31 -0400 | [diff] [blame] | 30 | import static junit.framework.Assert.assertNotNull; |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 31 | import static junit.framework.Assert.assertNull; |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 32 | import static junit.framework.Assert.assertTrue; |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 33 | import static junit.framework.Assert.fail; |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 34 | |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 35 | import static org.mockito.Matchers.anyBoolean; |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 36 | import static org.mockito.Matchers.anyLong; |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 37 | import static org.mockito.Matchers.anyString; |
| 38 | import static org.mockito.Matchers.eq; |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 39 | import static org.mockito.Mockito.any; |
| 40 | import static org.mockito.Mockito.anyInt; |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 41 | import static org.mockito.Mockito.doAnswer; |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 42 | import static org.mockito.Mockito.mock; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 43 | import static org.mockito.Mockito.never; |
| 44 | import static org.mockito.Mockito.reset; |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 45 | import static org.mockito.Mockito.spy; |
| 46 | import static org.mockito.Mockito.timeout; |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 47 | import static org.mockito.Mockito.times; |
| 48 | import static org.mockito.Mockito.verify; |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 49 | import static org.mockito.Mockito.when; |
| 50 | |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 51 | import android.app.ActivityManager; |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 52 | import android.app.INotificationManager; |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 53 | import android.app.Notification; |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 54 | import android.app.NotificationChannel; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 55 | import android.app.NotificationChannelGroup; |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 56 | import android.app.NotificationManager; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 57 | import android.companion.ICompanionDeviceManager; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 58 | import android.content.ComponentName; |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 59 | import android.content.Context; |
Beverly | d4f9649 | 2017-08-02 13:36:11 -0400 | [diff] [blame] | 60 | import android.content.Intent; |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 61 | import android.content.pm.ApplicationInfo; |
| 62 | import android.content.pm.IPackageManager; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 63 | import android.content.pm.PackageManager; |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 64 | import android.content.pm.ParceledListSlice; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 65 | import android.graphics.Color; |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 66 | import android.media.AudioManager; |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 67 | import android.os.Binder; |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 68 | import android.os.Build; |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 69 | import android.os.Bundle; |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 70 | import android.os.Process; |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 71 | import android.os.UserHandle; |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 72 | import android.provider.Settings.Secure; |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 73 | import android.service.notification.Adjustment; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 74 | import android.service.notification.NotificationListenerService; |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 75 | import android.service.notification.NotificationStats; |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 76 | import android.service.notification.StatusBarNotification; |
Geoffrey Pitsch | 8185d38 | 2017-05-19 18:41:32 -0400 | [diff] [blame] | 77 | import android.test.suitebuilder.annotation.SmallTest; |
Jason Monk | 745d0a8 | 2017-04-17 11:34:22 -0400 | [diff] [blame] | 78 | import android.testing.AndroidTestingRunner; |
Julia Reynolds | 92febc3 | 2017-10-26 11:30:31 -0400 | [diff] [blame] | 79 | import android.testing.TestableContext; |
Geoffrey Pitsch | 415e454 | 2017-04-10 13:12:58 -0400 | [diff] [blame] | 80 | import android.testing.TestableLooper; |
Jason Monk | 745d0a8 | 2017-04-17 11:34:22 -0400 | [diff] [blame] | 81 | import android.testing.TestableLooper.RunWithLooper; |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 82 | import android.util.ArrayMap; |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 83 | import android.util.AtomicFile; |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 84 | |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 85 | import com.android.internal.statusbar.NotificationVisibility; |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 86 | import com.android.server.UiServiceTestCase; |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 87 | import com.android.server.lights.Light; |
| 88 | import com.android.server.lights.LightsManager; |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 89 | import com.android.server.notification.NotificationManagerService.NotificationAssistants; |
| 90 | import com.android.server.notification.NotificationManagerService.NotificationListeners; |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 91 | |
| 92 | import org.junit.After; |
| 93 | import org.junit.Before; |
| 94 | import org.junit.Test; |
| 95 | import org.junit.runner.RunWith; |
Julia Reynolds | 40f00d7 | 2017-12-12 10:47:32 -0500 | [diff] [blame] | 96 | import org.mockito.ArgumentCaptor; |
| 97 | import org.mockito.Mock; |
| 98 | import org.mockito.MockitoAnnotations; |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 99 | import org.mockito.stubbing.Answer; |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 100 | |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 101 | import java.io.BufferedInputStream; |
| 102 | import java.io.ByteArrayInputStream; |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 103 | import java.io.File; |
| 104 | import java.io.FileInputStream; |
| 105 | import java.io.FileOutputStream; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 106 | import java.util.ArrayList; |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 107 | import java.util.Arrays; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 108 | import java.util.List; |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 109 | import java.util.Map; |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 110 | |
Geoffrey Pitsch | 8185d38 | 2017-05-19 18:41:32 -0400 | [diff] [blame] | 111 | @SmallTest |
Jason Monk | 745d0a8 | 2017-04-17 11:34:22 -0400 | [diff] [blame] | 112 | @RunWith(AndroidTestingRunner.class) |
| 113 | @RunWithLooper |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 114 | public class NotificationManagerServiceTest extends UiServiceTestCase { |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 115 | private static final String TEST_CHANNEL_ID = "NotificationManagerServiceTestChannelId"; |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 116 | private final int mUid = Binder.getCallingUid(); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 117 | private NotificationManagerService mService; |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 118 | private INotificationManager mBinderService; |
Geoffrey Pitsch | 415e454 | 2017-04-10 13:12:58 -0400 | [diff] [blame] | 119 | private NotificationManagerInternal mInternalService; |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 120 | @Mock |
| 121 | private IPackageManager mPackageManager; |
| 122 | @Mock |
| 123 | private PackageManager mPackageManagerClient; |
Julia Reynolds | 92febc3 | 2017-10-26 11:30:31 -0400 | [diff] [blame] | 124 | private TestableContext mContext = spy(getContext()); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 125 | private final String PKG = mContext.getPackageName(); |
Geoffrey Pitsch | 415e454 | 2017-04-10 13:12:58 -0400 | [diff] [blame] | 126 | private TestableLooper mTestableLooper; |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 127 | @Mock |
| 128 | private RankingHelper mRankingHelper; |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 129 | AtomicFile mPolicyFile; |
| 130 | File mFile; |
| 131 | @Mock |
Geoffrey Pitsch | d5bcf21 | 2017-06-01 15:45:35 -0400 | [diff] [blame] | 132 | private NotificationUsageStats mUsageStats; |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 133 | @Mock |
| 134 | private AudioManager mAudioManager; |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 135 | @Mock |
| 136 | ActivityManager mActivityManager; |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 137 | NotificationManagerService.WorkerHandler mHandler; |
Julia Reynolds | 3ff26d2 | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 138 | |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 139 | private NotificationChannel mTestNotificationChannel = new NotificationChannel( |
| 140 | TEST_CHANNEL_ID, TEST_CHANNEL_ID, NotificationManager.IMPORTANCE_DEFAULT); |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 141 | @Mock |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 142 | private NotificationListeners mListeners; |
| 143 | @Mock private NotificationAssistants mAssistants; |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 144 | @Mock private ConditionProviders mConditionProviders; |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 145 | private ManagedServices.ManagedServiceInfo mListener; |
| 146 | @Mock private ICompanionDeviceManager mCompanionMgr; |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 147 | @Mock SnoozeHelper mSnoozeHelper; |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 148 | @Mock GroupHelper mGroupHelper; |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 149 | |
Geoffrey Pitsch | 415e454 | 2017-04-10 13:12:58 -0400 | [diff] [blame] | 150 | // Use a Testable subclass so we can simulate calls from the system without failing. |
| 151 | private static class TestableNotificationManagerService extends NotificationManagerService { |
Amith Yamasani | 803eab69 | 2017-11-09 17:47:04 -0800 | [diff] [blame] | 152 | public TestableNotificationManagerService(Context context) { |
| 153 | super(context); |
| 154 | } |
Geoffrey Pitsch | 415e454 | 2017-04-10 13:12:58 -0400 | [diff] [blame] | 155 | |
| 156 | @Override |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 157 | protected boolean isCallingUidSystem() { |
| 158 | return true; |
| 159 | } |
| 160 | |
| 161 | @Override |
| 162 | protected boolean isCallerSystemOrPhone() { |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 163 | return true; |
| 164 | } |
Julia Reynolds | 727a728 | 2017-04-13 10:54:01 -0400 | [diff] [blame] | 165 | |
| 166 | @Override |
| 167 | protected ICompanionDeviceManager getCompanionManager() { |
| 168 | return null; |
| 169 | } |
Amith Yamasani | 803eab69 | 2017-11-09 17:47:04 -0800 | [diff] [blame] | 170 | |
| 171 | @Override |
| 172 | protected void reportSeen(NotificationRecord r) { |
| 173 | return; |
| 174 | } |
Geoffrey Pitsch | 415e454 | 2017-04-10 13:12:58 -0400 | [diff] [blame] | 175 | } |
| 176 | |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 177 | @Before |
| 178 | public void setUp() throws Exception { |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 179 | MockitoAnnotations.initMocks(this); |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 180 | |
| 181 | // most tests assume badging is enabled |
| 182 | Secure.putIntForUser(getContext().getContentResolver(), |
| 183 | Secure.NOTIFICATION_BADGING, 1, |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 184 | UserHandle.getUserHandleForUid(mUid).getIdentifier()); |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 185 | |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 186 | mService = new TestableNotificationManagerService(mContext); |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 187 | |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 188 | // Use this testable looper. |
| 189 | mTestableLooper = TestableLooper.get(this); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 190 | mHandler = mService.new WorkerHandler(mTestableLooper.getLooper()); |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 191 | // MockPackageManager - default returns ApplicationInfo with matching calling UID |
Julia Reynolds | 92febc3 | 2017-10-26 11:30:31 -0400 | [diff] [blame] | 192 | mContext.setMockPackageManager(mPackageManagerClient); |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 193 | final ApplicationInfo applicationInfo = new ApplicationInfo(); |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 194 | applicationInfo.uid = mUid; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 195 | when(mPackageManager.getApplicationInfo(anyString(), anyInt(), anyInt())) |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 196 | .thenReturn(applicationInfo); |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 197 | when(mPackageManagerClient.getApplicationInfoAsUser(anyString(), anyInt(), anyInt())) |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 198 | .thenReturn(applicationInfo); |
Julia Reynolds | 92febc3 | 2017-10-26 11:30:31 -0400 | [diff] [blame] | 199 | when(mPackageManagerClient.getPackageUidAsUser(any(), anyInt())).thenReturn(mUid); |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 200 | final LightsManager mockLightsManager = mock(LightsManager.class); |
| 201 | when(mockLightsManager.getLight(anyInt())).thenReturn(mock(Light.class)); |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 202 | when(mAudioManager.getRingerModeInternal()).thenReturn(AudioManager.RINGER_MODE_NORMAL); |
Julia Reynolds | e181641 | 2017-10-24 10:39:11 -0400 | [diff] [blame] | 203 | when(mPackageManagerClient.hasSystemFeature(FEATURE_WATCH)).thenReturn(false); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 204 | |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 205 | // write to a test file; the system file isn't readable from tests |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 206 | mFile = new File(mContext.getCacheDir(), "test.xml"); |
| 207 | mFile.createNewFile(); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 208 | final String preupgradeXml = "<notification-policy></notification-policy>"; |
| 209 | mPolicyFile = new AtomicFile(mFile); |
| 210 | FileOutputStream fos = mPolicyFile.startWrite(); |
| 211 | fos.write(preupgradeXml.getBytes()); |
| 212 | mPolicyFile.finishWrite(fos); |
| 213 | FileInputStream fStream = new FileInputStream(mFile); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 214 | |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 215 | // Setup managed services |
| 216 | mListener = mListeners.new ManagedServiceInfo( |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 217 | null, new ComponentName(PKG, "test_class"), mUid, true, null, 0); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 218 | when(mListeners.checkServiceTokenLocked(any())).thenReturn(mListener); |
| 219 | ManagedServices.Config listenerConfig = new ManagedServices.Config(); |
| 220 | listenerConfig.xmlTag = NotificationListeners.TAG_ENABLED_NOTIFICATION_LISTENERS; |
| 221 | when(mListeners.getConfig()).thenReturn(listenerConfig); |
| 222 | ManagedServices.Config assistantConfig = new ManagedServices.Config(); |
| 223 | assistantConfig.xmlTag = NotificationAssistants.TAG_ENABLED_NOTIFICATION_ASSISTANTS; |
| 224 | when(mAssistants.getConfig()).thenReturn(assistantConfig); |
| 225 | ManagedServices.Config dndConfig = new ManagedServices.Config(); |
| 226 | dndConfig.xmlTag = ConditionProviders.TAG_ENABLED_DND_APPS; |
| 227 | when(mConditionProviders.getConfig()).thenReturn(dndConfig); |
| 228 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 229 | try { |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 230 | mService.init(mTestableLooper.getLooper(), |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 231 | mPackageManager, mPackageManagerClient, mockLightsManager, |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 232 | mListeners, mAssistants, mConditionProviders, |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 233 | mCompanionMgr, mSnoozeHelper, mUsageStats, mPolicyFile, mActivityManager, |
| 234 | mGroupHelper); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 235 | } catch (SecurityException e) { |
| 236 | if (!e.getMessage().contains("Permission Denial: not allowed to send broadcast")) { |
| 237 | throw e; |
| 238 | } |
| 239 | } |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 240 | mService.setAudioManager(mAudioManager); |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 241 | |
| 242 | // Tests call directly into the Binder. |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 243 | mBinderService = mService.getBinderService(); |
| 244 | mInternalService = mService.getInternalService(); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 245 | |
| 246 | mBinderService.createNotificationChannels( |
| 247 | PKG, new ParceledListSlice(Arrays.asList(mTestNotificationChannel))); |
Julia Reynolds | 92febc3 | 2017-10-26 11:30:31 -0400 | [diff] [blame] | 248 | assertNotNull(mBinderService.getNotificationChannel(PKG, TEST_CHANNEL_ID)); |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 249 | } |
| 250 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 251 | @After |
| 252 | public void tearDown() throws Exception { |
| 253 | mFile.delete(); |
| 254 | } |
| 255 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 256 | public void waitForIdle() throws Exception { |
Geoffrey Pitsch | 415e454 | 2017-04-10 13:12:58 -0400 | [diff] [blame] | 257 | mTestableLooper.processAllMessages(); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 258 | } |
| 259 | |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 260 | private NotificationRecord generateNotificationRecord(NotificationChannel channel, int id, |
| 261 | String groupKey, boolean isSummary) { |
| 262 | Notification.Builder nb = new Notification.Builder(mContext, channel.getId()) |
| 263 | .setContentTitle("foo") |
| 264 | .setSmallIcon(android.R.drawable.sym_def_app_icon) |
| 265 | .setGroup(groupKey) |
| 266 | .setGroupSummary(isSummary); |
| 267 | |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 268 | StatusBarNotification sbn = new StatusBarNotification(PKG, PKG, id, "tag", mUid, 0, |
| 269 | nb.build(), new UserHandle(mUid), null, 0); |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 270 | return new NotificationRecord(mContext, sbn, channel); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 271 | } |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 272 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 273 | private NotificationRecord generateNotificationRecord(NotificationChannel channel) { |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 274 | return generateNotificationRecord(channel, null); |
| 275 | } |
| 276 | |
| 277 | private NotificationRecord generateNotificationRecord(NotificationChannel channel, |
| 278 | Notification.TvExtender extender) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 279 | if (channel == null) { |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 280 | channel = mTestNotificationChannel; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 281 | } |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 282 | Notification.Builder nb = new Notification.Builder(mContext, channel.getId()) |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 283 | .setContentTitle("foo") |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 284 | .setSmallIcon(android.R.drawable.sym_def_app_icon); |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 285 | if (extender != null) { |
| 286 | nb.extend(extender); |
| 287 | } |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 288 | StatusBarNotification sbn = new StatusBarNotification(PKG, PKG, 1, "tag", mUid, 0, |
| 289 | nb.build(), new UserHandle(mUid), null, 0); |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 290 | return new NotificationRecord(mContext, sbn, channel); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 291 | } |
| 292 | |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 293 | private Map<String, Answer> getSignalExtractorSideEffects() { |
| 294 | Map<String, Answer> answers = new ArrayMap<>(); |
| 295 | |
| 296 | answers.put("override group key", invocationOnMock -> { |
| 297 | ((NotificationRecord) invocationOnMock.getArguments()[0]) |
| 298 | .setOverrideGroupKey("bananas"); |
| 299 | return null; |
| 300 | }); |
| 301 | answers.put("override people", invocationOnMock -> { |
| 302 | ((NotificationRecord) invocationOnMock.getArguments()[0]) |
| 303 | .setPeopleOverride(new ArrayList<>()); |
| 304 | return null; |
| 305 | }); |
| 306 | answers.put("snooze criteria", invocationOnMock -> { |
| 307 | ((NotificationRecord) invocationOnMock.getArguments()[0]) |
| 308 | .setSnoozeCriteria(new ArrayList<>()); |
| 309 | return null; |
| 310 | }); |
| 311 | answers.put("notification channel", invocationOnMock -> { |
| 312 | ((NotificationRecord) invocationOnMock.getArguments()[0]) |
| 313 | .updateNotificationChannel(new NotificationChannel("a", "", IMPORTANCE_LOW)); |
| 314 | return null; |
| 315 | }); |
| 316 | answers.put("badging", invocationOnMock -> { |
| 317 | NotificationRecord r = (NotificationRecord) invocationOnMock.getArguments()[0]; |
| 318 | r.setShowBadge(!r.canShowBadge()); |
| 319 | return null; |
| 320 | }); |
| 321 | answers.put("package visibility", invocationOnMock -> { |
| 322 | ((NotificationRecord) invocationOnMock.getArguments()[0]).setPackageVisibilityOverride( |
| 323 | Notification.VISIBILITY_SECRET); |
| 324 | return null; |
| 325 | }); |
| 326 | |
| 327 | return answers; |
| 328 | } |
| 329 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 330 | @Test |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 331 | public void testCreateNotificationChannels_SingleChannel() throws Exception { |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 332 | final NotificationChannel channel = |
| 333 | new NotificationChannel("id", "name", NotificationManager.IMPORTANCE_DEFAULT); |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 334 | mBinderService.createNotificationChannels(PKG, |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 335 | new ParceledListSlice(Arrays.asList(channel))); |
| 336 | final NotificationChannel createdChannel = |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 337 | mBinderService.getNotificationChannel(PKG, "id"); |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 338 | assertTrue(createdChannel != null); |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 339 | } |
| 340 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 341 | @Test |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 342 | public void testCreateNotificationChannels_NullChannelThrowsException() throws Exception { |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 343 | try { |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 344 | mBinderService.createNotificationChannels(PKG, |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 345 | new ParceledListSlice(Arrays.asList(null))); |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 346 | fail("Exception should be thrown immediately."); |
| 347 | } catch (NullPointerException e) { |
| 348 | // pass |
| 349 | } |
| 350 | } |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 351 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 352 | @Test |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 353 | public void testCreateNotificationChannels_TwoChannels() throws Exception { |
| 354 | final NotificationChannel channel1 = |
| 355 | new NotificationChannel("id1", "name", NotificationManager.IMPORTANCE_DEFAULT); |
| 356 | final NotificationChannel channel2 = |
| 357 | new NotificationChannel("id2", "name", NotificationManager.IMPORTANCE_DEFAULT); |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 358 | mBinderService.createNotificationChannels(PKG, |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 359 | new ParceledListSlice(Arrays.asList(channel1, channel2))); |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 360 | assertTrue(mBinderService.getNotificationChannel(PKG, "id1") != null); |
| 361 | assertTrue(mBinderService.getNotificationChannel(PKG, "id2") != null); |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 362 | } |
| 363 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 364 | @Test |
Geoffrey Pitsch | 76a3aa0 | 2017-07-26 15:07:34 -0400 | [diff] [blame] | 365 | public void testCreateNotificationChannels_SecondCreateDoesNotChangeImportance() |
| 366 | throws Exception { |
| 367 | final NotificationChannel channel = |
| 368 | new NotificationChannel("id", "name", NotificationManager.IMPORTANCE_DEFAULT); |
| 369 | mBinderService.createNotificationChannels(PKG, |
| 370 | new ParceledListSlice(Arrays.asList(channel))); |
| 371 | |
| 372 | // Recreating the channel doesn't throw, but ignores importance. |
| 373 | final NotificationChannel dupeChannel = |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 374 | new NotificationChannel("id", "name", IMPORTANCE_HIGH); |
Geoffrey Pitsch | 76a3aa0 | 2017-07-26 15:07:34 -0400 | [diff] [blame] | 375 | mBinderService.createNotificationChannels(PKG, |
| 376 | new ParceledListSlice(Arrays.asList(dupeChannel))); |
| 377 | final NotificationChannel createdChannel = |
| 378 | mBinderService.getNotificationChannel(PKG, "id"); |
| 379 | assertEquals(NotificationManager.IMPORTANCE_DEFAULT, createdChannel.getImportance()); |
| 380 | } |
| 381 | |
| 382 | @Test |
| 383 | public void testCreateNotificationChannels_SecondCreateAllowedToDowngradeImportance() |
| 384 | throws Exception { |
| 385 | final NotificationChannel channel = |
| 386 | new NotificationChannel("id", "name", NotificationManager.IMPORTANCE_DEFAULT); |
| 387 | mBinderService.createNotificationChannels(PKG, |
| 388 | new ParceledListSlice(Arrays.asList(channel))); |
| 389 | |
| 390 | // Recreating with a lower importance is allowed to modify the channel. |
| 391 | final NotificationChannel dupeChannel = |
| 392 | new NotificationChannel("id", "name", NotificationManager.IMPORTANCE_LOW); |
| 393 | mBinderService.createNotificationChannels(PKG, |
| 394 | new ParceledListSlice(Arrays.asList(dupeChannel))); |
| 395 | final NotificationChannel createdChannel = |
| 396 | mBinderService.getNotificationChannel(PKG, "id"); |
| 397 | assertEquals(NotificationManager.IMPORTANCE_LOW, createdChannel.getImportance()); |
| 398 | } |
| 399 | |
| 400 | @Test |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 401 | public void testCreateNotificationChannels_CannotDowngradeImportanceIfAlreadyUpdated() |
| 402 | throws Exception { |
| 403 | final NotificationChannel channel = |
| 404 | new NotificationChannel("id", "name", NotificationManager.IMPORTANCE_DEFAULT); |
| 405 | mBinderService.createNotificationChannels(PKG, |
| 406 | new ParceledListSlice(Arrays.asList(channel))); |
| 407 | |
| 408 | // The user modifies importance directly, can no longer be changed by the app. |
| 409 | final NotificationChannel updatedChannel = |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 410 | new NotificationChannel("id", "name", IMPORTANCE_HIGH); |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 411 | mBinderService.updateNotificationChannelForPackage(PKG, mUid, updatedChannel); |
| 412 | |
| 413 | // Recreating with a lower importance leaves channel unchanged. |
| 414 | final NotificationChannel dupeChannel = |
| 415 | new NotificationChannel("id", "name", NotificationManager.IMPORTANCE_LOW); |
| 416 | mBinderService.createNotificationChannels(PKG, |
| 417 | new ParceledListSlice(Arrays.asList(dupeChannel))); |
| 418 | final NotificationChannel createdChannel = |
| 419 | mBinderService.getNotificationChannel(PKG, "id"); |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 420 | assertEquals(IMPORTANCE_HIGH, createdChannel.getImportance()); |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | @Test |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 424 | public void testCreateNotificationChannels_IdenticalChannelsInListIgnoresSecond() |
| 425 | throws Exception { |
| 426 | final NotificationChannel channel1 = |
| 427 | new NotificationChannel("id", "name", NotificationManager.IMPORTANCE_DEFAULT); |
| 428 | final NotificationChannel channel2 = |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 429 | new NotificationChannel("id", "name", IMPORTANCE_HIGH); |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 430 | mBinderService.createNotificationChannels(PKG, |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 431 | new ParceledListSlice(Arrays.asList(channel1, channel2))); |
| 432 | final NotificationChannel createdChannel = |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 433 | mBinderService.getNotificationChannel(PKG, "id"); |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 434 | assertEquals(NotificationManager.IMPORTANCE_DEFAULT, createdChannel.getImportance()); |
| 435 | } |
| 436 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 437 | @Test |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 438 | public void testBlockedNotifications_suspended() throws Exception { |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 439 | when(mPackageManager.isPackageSuspendedForUser(anyString(), anyInt())).thenReturn(true); |
| 440 | |
| 441 | NotificationChannel channel = new NotificationChannel("id", "name", |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 442 | IMPORTANCE_HIGH); |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 443 | NotificationRecord r = generateNotificationRecord(channel); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 444 | assertTrue(mService.isBlocked(r, mUsageStats)); |
Geoffrey Pitsch | d5bcf21 | 2017-06-01 15:45:35 -0400 | [diff] [blame] | 445 | verify(mUsageStats, times(1)).registerSuspendedByAdmin(eq(r)); |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 446 | } |
| 447 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 448 | @Test |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 449 | public void testBlockedNotifications_blockedChannel() throws Exception { |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 450 | when(mPackageManager.isPackageSuspendedForUser(anyString(), anyInt())).thenReturn(false); |
| 451 | |
| 452 | NotificationChannel channel = new NotificationChannel("id", "name", |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 453 | NotificationManager.IMPORTANCE_NONE); |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 454 | NotificationRecord r = generateNotificationRecord(channel); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 455 | assertTrue(mService.isBlocked(r, mUsageStats)); |
Geoffrey Pitsch | d5bcf21 | 2017-06-01 15:45:35 -0400 | [diff] [blame] | 456 | verify(mUsageStats, times(1)).registerBlocked(eq(r)); |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 457 | |
| 458 | mBinderService.createNotificationChannels( |
| 459 | PKG, new ParceledListSlice(Arrays.asList(channel))); |
| 460 | final StatusBarNotification sbn = generateNotificationRecord(channel).sbn; |
| 461 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", |
| 462 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
| 463 | waitForIdle(); |
| 464 | assertEquals(0, mBinderService.getActiveNotifications(sbn.getPackageName()).length); |
| 465 | } |
| 466 | |
| 467 | @Test |
| 468 | public void testEnqueuedBlockedNotifications_appBlockedChannelForegroundService() |
| 469 | throws Exception { |
| 470 | when(mPackageManager.isPackageSuspendedForUser(anyString(), anyInt())).thenReturn(false); |
| 471 | |
| 472 | NotificationChannel channel = new NotificationChannel("blocked", "name", |
| 473 | NotificationManager.IMPORTANCE_NONE); |
| 474 | mBinderService.createNotificationChannels( |
| 475 | PKG, new ParceledListSlice(Arrays.asList(channel))); |
| 476 | |
| 477 | final StatusBarNotification sbn = generateNotificationRecord(channel).sbn; |
| 478 | sbn.getNotification().flags |= Notification.FLAG_FOREGROUND_SERVICE; |
| 479 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", |
| 480 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
| 481 | waitForIdle(); |
| 482 | assertEquals(1, mBinderService.getActiveNotifications(sbn.getPackageName()).length); |
| 483 | assertEquals(IMPORTANCE_LOW, |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 484 | mService.getNotificationRecord(sbn.getKey()).getImportance()); |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 485 | assertEquals(IMPORTANCE_LOW, |
| 486 | mBinderService.getNotificationChannel(PKG, channel.getId()).getImportance()); |
| 487 | } |
| 488 | |
| 489 | @Test |
| 490 | public void testEnqueuedBlockedNotifications_userBlockedChannelForegroundService() |
| 491 | throws Exception { |
| 492 | when(mPackageManager.isPackageSuspendedForUser(anyString(), anyInt())).thenReturn(false); |
| 493 | |
| 494 | NotificationChannel channel = |
| 495 | new NotificationChannel("blockedbyuser", "name", IMPORTANCE_HIGH); |
| 496 | mBinderService.createNotificationChannels( |
| 497 | PKG, new ParceledListSlice(Arrays.asList(channel))); |
| 498 | |
| 499 | NotificationChannel update = |
| 500 | new NotificationChannel("blockedbyuser", "name", IMPORTANCE_NONE); |
| 501 | mBinderService.updateNotificationChannelForPackage(PKG, mUid, update); |
| 502 | waitForIdle(); |
| 503 | assertEquals(IMPORTANCE_NONE, |
| 504 | mBinderService.getNotificationChannel(PKG, channel.getId()).getImportance()); |
| 505 | |
| 506 | final StatusBarNotification sbn = generateNotificationRecord(channel).sbn; |
| 507 | sbn.getNotification().flags |= Notification.FLAG_FOREGROUND_SERVICE; |
| 508 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", |
| 509 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
| 510 | waitForIdle(); |
| 511 | assertEquals(0, mBinderService.getActiveNotifications(sbn.getPackageName()).length); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 512 | assertNull(mService.getNotificationRecord(sbn.getKey())); |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 513 | assertEquals(IMPORTANCE_NONE, |
| 514 | mBinderService.getNotificationChannel(PKG, channel.getId()).getImportance()); |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 515 | } |
| 516 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 517 | @Test |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 518 | public void testBlockedNotifications_blockedChannelGroup() throws Exception { |
| 519 | when(mPackageManager.isPackageSuspendedForUser(anyString(), anyInt())).thenReturn(false); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 520 | mService.setRankingHelper(mRankingHelper); |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 521 | when(mRankingHelper.isGroupBlocked(anyString(), anyInt(), anyString())).thenReturn(true); |
| 522 | |
| 523 | NotificationChannel channel = new NotificationChannel("id", "name", |
| 524 | NotificationManager.IMPORTANCE_HIGH); |
| 525 | channel.setGroup("something"); |
| 526 | NotificationRecord r = generateNotificationRecord(channel); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 527 | assertTrue(mService.isBlocked(r, mUsageStats)); |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 528 | verify(mUsageStats, times(1)).registerBlocked(eq(r)); |
| 529 | } |
| 530 | |
| 531 | @Test |
Julia Reynolds | 4da7970 | 2017-06-01 11:06:10 -0400 | [diff] [blame] | 532 | public void testEnqueuedBlockedNotifications_blockedApp() throws Exception { |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 533 | when(mPackageManager.isPackageSuspendedForUser(anyString(), anyInt())).thenReturn(false); |
| 534 | |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 535 | mBinderService.setNotificationsEnabledForPackage(PKG, mUid, false); |
Julia Reynolds | 4da7970 | 2017-06-01 11:06:10 -0400 | [diff] [blame] | 536 | |
| 537 | final StatusBarNotification sbn = generateNotificationRecord(null).sbn; |
| 538 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", |
| 539 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
| 540 | waitForIdle(); |
| 541 | assertEquals(0, mBinderService.getActiveNotifications(sbn.getPackageName()).length); |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 542 | } |
| 543 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 544 | @Test |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 545 | public void testEnqueuedBlockedNotifications_blockedAppForegroundService() throws Exception { |
| 546 | when(mPackageManager.isPackageSuspendedForUser(anyString(), anyInt())).thenReturn(false); |
| 547 | |
| 548 | mBinderService.setNotificationsEnabledForPackage(PKG, mUid, false); |
| 549 | |
| 550 | final StatusBarNotification sbn = generateNotificationRecord(null).sbn; |
| 551 | sbn.getNotification().flags |= Notification.FLAG_FOREGROUND_SERVICE; |
| 552 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", |
| 553 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
| 554 | waitForIdle(); |
| 555 | assertEquals(0, mBinderService.getActiveNotifications(sbn.getPackageName()).length); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 556 | assertNull(mService.getNotificationRecord(sbn.getKey())); |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | @Test |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 560 | public void testEnqueueNotificationWithTag_PopulatesGetActiveNotifications() throws Exception { |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 561 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", 0, |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 562 | generateNotificationRecord(null).getNotification(), 0); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 563 | waitForIdle(); |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 564 | StatusBarNotification[] notifs = mBinderService.getActiveNotifications(PKG); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 565 | assertEquals(1, notifs.length); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 566 | assertEquals(1, mService.getNotificationRecordCount()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 567 | } |
| 568 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 569 | @Test |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 570 | public void testCancelNotificationImmediatelyAfterEnqueue() throws Exception { |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 571 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", 0, |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 572 | generateNotificationRecord(null).getNotification(), 0); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 573 | mBinderService.cancelNotificationWithTag(PKG, "tag", 0, 0); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 574 | waitForIdle(); |
| 575 | StatusBarNotification[] notifs = |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 576 | mBinderService.getActiveNotifications(PKG); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 577 | assertEquals(0, notifs.length); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 578 | assertEquals(0, mService.getNotificationRecordCount()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 579 | } |
| 580 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 581 | @Test |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 582 | public void testCancelNotificationWhilePostedAndEnqueued() throws Exception { |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 583 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", 0, |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 584 | generateNotificationRecord(null).getNotification(), 0); |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 585 | waitForIdle(); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 586 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", 0, |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 587 | generateNotificationRecord(null).getNotification(), 0); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 588 | mBinderService.cancelNotificationWithTag(PKG, "tag", 0, 0); |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 589 | waitForIdle(); |
| 590 | StatusBarNotification[] notifs = |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 591 | mBinderService.getActiveNotifications(PKG); |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 592 | assertEquals(0, notifs.length); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 593 | assertEquals(0, mService.getNotificationRecordCount()); |
| 594 | ArgumentCaptor<NotificationStats> captor = ArgumentCaptor.forClass(NotificationStats.class); |
| 595 | verify(mListeners, times(1)).notifyRemovedLocked(any(), anyInt(), captor.capture()); |
| 596 | assertEquals(NotificationStats.DISMISSAL_OTHER, captor.getValue().getDismissalSurface()); |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | @Test |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 600 | public void testCancelNotificationsFromListenerImmediatelyAfterEnqueue() throws Exception { |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 601 | NotificationRecord r = generateNotificationRecord(null); |
| 602 | final StatusBarNotification sbn = r.sbn; |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 603 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 604 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 605 | mBinderService.cancelNotificationsFromListener(null, null); |
| 606 | waitForIdle(); |
| 607 | StatusBarNotification[] notifs = |
| 608 | mBinderService.getActiveNotifications(sbn.getPackageName()); |
| 609 | assertEquals(0, notifs.length); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 610 | assertEquals(0, mService.getNotificationRecordCount()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 611 | } |
| 612 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 613 | @Test |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 614 | public void testCancelAllNotificationsImmediatelyAfterEnqueue() throws Exception { |
| 615 | final StatusBarNotification sbn = generateNotificationRecord(null).sbn; |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 616 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 617 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 618 | mBinderService.cancelAllNotifications(PKG, sbn.getUserId()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 619 | waitForIdle(); |
| 620 | StatusBarNotification[] notifs = |
| 621 | mBinderService.getActiveNotifications(sbn.getPackageName()); |
| 622 | assertEquals(0, notifs.length); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 623 | assertEquals(0, mService.getNotificationRecordCount()); |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | @Test |
| 627 | public void testUserInitiatedClearAll_noLeak() throws Exception { |
| 628 | final NotificationRecord n = generateNotificationRecord( |
| 629 | mTestNotificationChannel, 1, "group", true); |
| 630 | |
| 631 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", |
| 632 | n.sbn.getId(), n.sbn.getNotification(), n.sbn.getUserId()); |
| 633 | waitForIdle(); |
| 634 | |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 635 | mService.mNotificationDelegate.onClearAll(mUid, Binder.getCallingPid(), |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 636 | n.getUserId()); |
| 637 | waitForIdle(); |
| 638 | StatusBarNotification[] notifs = |
| 639 | mBinderService.getActiveNotifications(n.sbn.getPackageName()); |
| 640 | assertEquals(0, notifs.length); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 641 | assertEquals(0, mService.getNotificationRecordCount()); |
| 642 | ArgumentCaptor<NotificationStats> captor = ArgumentCaptor.forClass(NotificationStats.class); |
| 643 | verify(mListeners, times(1)).notifyRemovedLocked(any(), anyInt(), captor.capture()); |
| 644 | assertEquals(NotificationStats.DISMISSAL_OTHER, captor.getValue().getDismissalSurface()); |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | @Test |
| 648 | public void testCancelAllNotificationsCancelsChildren() throws Exception { |
| 649 | final NotificationRecord parent = generateNotificationRecord( |
| 650 | mTestNotificationChannel, 1, "group1", true); |
| 651 | final NotificationRecord child = generateNotificationRecord( |
| 652 | mTestNotificationChannel, 2, "group1", false); |
| 653 | |
| 654 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", |
| 655 | parent.sbn.getId(), parent.sbn.getNotification(), parent.sbn.getUserId()); |
| 656 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", |
| 657 | child.sbn.getId(), child.sbn.getNotification(), child.sbn.getUserId()); |
| 658 | waitForIdle(); |
| 659 | |
| 660 | mBinderService.cancelAllNotifications(PKG, parent.sbn.getUserId()); |
| 661 | waitForIdle(); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 662 | assertEquals(0, mService.getNotificationRecordCount()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 663 | } |
| 664 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 665 | @Test |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 666 | public void testCancelAllNotificationsMultipleEnqueuedDoesNotCrash() throws Exception { |
| 667 | final StatusBarNotification sbn = generateNotificationRecord(null).sbn; |
| 668 | for (int i = 0; i < 10; i++) { |
| 669 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", |
| 670 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
| 671 | } |
| 672 | mBinderService.cancelAllNotifications(PKG, sbn.getUserId()); |
| 673 | waitForIdle(); |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 674 | |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 675 | assertEquals(0, mService.getNotificationRecordCount()); |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 676 | } |
| 677 | |
| 678 | @Test |
| 679 | public void testCancelGroupSummaryMultipleEnqueuedChildrenDoesNotCrash() throws Exception { |
| 680 | final NotificationRecord parent = generateNotificationRecord( |
| 681 | mTestNotificationChannel, 1, "group1", true); |
| 682 | final NotificationRecord parentAsChild = generateNotificationRecord( |
| 683 | mTestNotificationChannel, 1, "group1", false); |
| 684 | final NotificationRecord child = generateNotificationRecord( |
| 685 | mTestNotificationChannel, 2, "group1", false); |
| 686 | |
| 687 | // fully post parent notification |
| 688 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", |
| 689 | parent.sbn.getId(), parent.sbn.getNotification(), parent.sbn.getUserId()); |
| 690 | waitForIdle(); |
| 691 | |
| 692 | // enqueue the child several times |
| 693 | for (int i = 0; i < 10; i++) { |
| 694 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", |
| 695 | child.sbn.getId(), child.sbn.getNotification(), child.sbn.getUserId()); |
| 696 | } |
| 697 | // make the parent a child, which will cancel the child notification |
| 698 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", |
| 699 | parentAsChild.sbn.getId(), parentAsChild.sbn.getNotification(), |
| 700 | parentAsChild.sbn.getUserId()); |
| 701 | waitForIdle(); |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 702 | |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 703 | assertEquals(0, mService.getNotificationRecordCount()); |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | @Test |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 707 | public void testCancelAllNotifications_IgnoreForegroundService() throws Exception { |
| 708 | final StatusBarNotification sbn = generateNotificationRecord(null).sbn; |
| 709 | sbn.getNotification().flags |= Notification.FLAG_FOREGROUND_SERVICE; |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 710 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 711 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 712 | mBinderService.cancelAllNotifications(PKG, sbn.getUserId()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 713 | waitForIdle(); |
| 714 | StatusBarNotification[] notifs = |
| 715 | mBinderService.getActiveNotifications(sbn.getPackageName()); |
| 716 | assertEquals(1, notifs.length); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 717 | assertEquals(1, mService.getNotificationRecordCount()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 718 | } |
| 719 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 720 | @Test |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 721 | public void testCancelAllNotifications_IgnoreOtherPackages() throws Exception { |
| 722 | final StatusBarNotification sbn = generateNotificationRecord(null).sbn; |
| 723 | sbn.getNotification().flags |= Notification.FLAG_FOREGROUND_SERVICE; |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 724 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 725 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 726 | mBinderService.cancelAllNotifications("other_pkg_name", sbn.getUserId()); |
| 727 | waitForIdle(); |
| 728 | StatusBarNotification[] notifs = |
| 729 | mBinderService.getActiveNotifications(sbn.getPackageName()); |
| 730 | assertEquals(1, notifs.length); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 731 | assertEquals(1, mService.getNotificationRecordCount()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 732 | } |
| 733 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 734 | @Test |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 735 | public void testCancelAllNotifications_NullPkgRemovesAll() throws Exception { |
| 736 | final StatusBarNotification sbn = generateNotificationRecord(null).sbn; |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 737 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 738 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 739 | mBinderService.cancelAllNotifications(null, sbn.getUserId()); |
| 740 | waitForIdle(); |
| 741 | StatusBarNotification[] notifs = |
| 742 | mBinderService.getActiveNotifications(sbn.getPackageName()); |
| 743 | assertEquals(0, notifs.length); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 744 | assertEquals(0, mService.getNotificationRecordCount()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 745 | } |
| 746 | |
Geoffrey Pitsch | 1659446 | 2017-01-26 14:42:30 -0500 | [diff] [blame] | 747 | @Test |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 748 | public void testCancelAllNotifications_NullPkgIgnoresUserAllNotifications() throws Exception { |
| 749 | final StatusBarNotification sbn = generateNotificationRecord(null).sbn; |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 750 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 751 | sbn.getId(), sbn.getNotification(), UserHandle.USER_ALL); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 752 | // Null pkg is how we signal a user switch. |
| 753 | mBinderService.cancelAllNotifications(null, sbn.getUserId()); |
| 754 | waitForIdle(); |
| 755 | StatusBarNotification[] notifs = |
| 756 | mBinderService.getActiveNotifications(sbn.getPackageName()); |
| 757 | assertEquals(1, notifs.length); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 758 | assertEquals(1, mService.getNotificationRecordCount()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 759 | } |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 760 | |
| 761 | @Test |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 762 | public void testAppInitiatedCancelAllNotifications_CancelsNoClearFlag() throws Exception { |
| 763 | final StatusBarNotification sbn = generateNotificationRecord(null).sbn; |
| 764 | sbn.getNotification().flags |= Notification.FLAG_NO_CLEAR; |
| 765 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", |
| 766 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
| 767 | mBinderService.cancelAllNotifications(PKG, sbn.getUserId()); |
| 768 | waitForIdle(); |
| 769 | StatusBarNotification[] notifs = |
| 770 | mBinderService.getActiveNotifications(sbn.getPackageName()); |
| 771 | assertEquals(0, notifs.length); |
| 772 | } |
| 773 | |
| 774 | @Test |
| 775 | public void testCancelAllNotifications_CancelsNoClearFlag() throws Exception { |
| 776 | final NotificationRecord notif = generateNotificationRecord( |
| 777 | mTestNotificationChannel, 1, "group", true); |
| 778 | notif.getNotification().flags |= Notification.FLAG_NO_CLEAR; |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 779 | mService.addNotification(notif); |
| 780 | mService.cancelAllNotificationsInt(mUid, 0, PKG, null, 0, 0, true, |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 781 | notif.getUserId(), 0, null); |
| 782 | waitForIdle(); |
| 783 | StatusBarNotification[] notifs = |
| 784 | mBinderService.getActiveNotifications(notif.sbn.getPackageName()); |
| 785 | assertEquals(0, notifs.length); |
| 786 | } |
| 787 | |
| 788 | @Test |
| 789 | public void testUserInitiatedCancelAllOnClearAll_NoClearFlag() throws Exception { |
| 790 | final NotificationRecord notif = generateNotificationRecord( |
| 791 | mTestNotificationChannel, 1, "group", true); |
| 792 | notif.getNotification().flags |= Notification.FLAG_NO_CLEAR; |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 793 | mService.addNotification(notif); |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 794 | |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 795 | mService.mNotificationDelegate.onClearAll(mUid, Binder.getCallingPid(), |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 796 | notif.getUserId()); |
| 797 | waitForIdle(); |
| 798 | StatusBarNotification[] notifs = |
| 799 | mBinderService.getActiveNotifications(notif.sbn.getPackageName()); |
| 800 | assertEquals(1, notifs.length); |
| 801 | } |
| 802 | |
| 803 | @Test |
| 804 | public void testCancelAllCancelNotificationsFromListener_NoClearFlag() throws Exception { |
| 805 | final NotificationRecord parent = generateNotificationRecord( |
| 806 | mTestNotificationChannel, 1, "group", true); |
| 807 | final NotificationRecord child = generateNotificationRecord( |
| 808 | mTestNotificationChannel, 2, "group", false); |
| 809 | final NotificationRecord child2 = generateNotificationRecord( |
| 810 | mTestNotificationChannel, 3, "group", false); |
| 811 | child2.getNotification().flags |= Notification.FLAG_NO_CLEAR; |
| 812 | final NotificationRecord newGroup = generateNotificationRecord( |
| 813 | mTestNotificationChannel, 4, "group2", false); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 814 | mService.addNotification(parent); |
| 815 | mService.addNotification(child); |
| 816 | mService.addNotification(child2); |
| 817 | mService.addNotification(newGroup); |
| 818 | mService.getBinderService().cancelNotificationsFromListener(null, null); |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 819 | waitForIdle(); |
| 820 | StatusBarNotification[] notifs = |
| 821 | mBinderService.getActiveNotifications(parent.sbn.getPackageName()); |
| 822 | assertEquals(1, notifs.length); |
| 823 | } |
| 824 | |
| 825 | @Test |
| 826 | public void testUserInitiatedCancelAllWithGroup_NoClearFlag() throws Exception { |
| 827 | final NotificationRecord parent = generateNotificationRecord( |
| 828 | mTestNotificationChannel, 1, "group", true); |
| 829 | final NotificationRecord child = generateNotificationRecord( |
| 830 | mTestNotificationChannel, 2, "group", false); |
| 831 | final NotificationRecord child2 = generateNotificationRecord( |
| 832 | mTestNotificationChannel, 3, "group", false); |
| 833 | child2.getNotification().flags |= Notification.FLAG_NO_CLEAR; |
| 834 | final NotificationRecord newGroup = generateNotificationRecord( |
| 835 | mTestNotificationChannel, 4, "group2", false); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 836 | mService.addNotification(parent); |
| 837 | mService.addNotification(child); |
| 838 | mService.addNotification(child2); |
| 839 | mService.addNotification(newGroup); |
| 840 | mService.mNotificationDelegate.onClearAll(mUid, Binder.getCallingPid(), |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 841 | parent.getUserId()); |
| 842 | waitForIdle(); |
| 843 | StatusBarNotification[] notifs = |
| 844 | mBinderService.getActiveNotifications(parent.sbn.getPackageName()); |
| 845 | assertEquals(1, notifs.length); |
| 846 | } |
| 847 | |
| 848 | @Test |
Geoffrey Pitsch | 415e454 | 2017-04-10 13:12:58 -0400 | [diff] [blame] | 849 | public void testRemoveForegroundServiceFlag_ImmediatelyAfterEnqueue() throws Exception { |
| 850 | final StatusBarNotification sbn = generateNotificationRecord(null).sbn; |
| 851 | sbn.getNotification().flags |= Notification.FLAG_FOREGROUND_SERVICE; |
| 852 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", null, |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 853 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
Geoffrey Pitsch | 415e454 | 2017-04-10 13:12:58 -0400 | [diff] [blame] | 854 | mInternalService.removeForegroundServiceFlagFromNotification(PKG, sbn.getId(), |
| 855 | sbn.getUserId()); |
| 856 | waitForIdle(); |
| 857 | StatusBarNotification[] notifs = |
| 858 | mBinderService.getActiveNotifications(sbn.getPackageName()); |
| 859 | assertEquals(0, notifs[0].getNotification().flags & Notification.FLAG_FOREGROUND_SERVICE); |
| 860 | } |
| 861 | |
| 862 | @Test |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 863 | public void testCancelAfterSecondEnqueueDoesNotSpecifyForegroundFlag() throws Exception { |
| 864 | final StatusBarNotification sbn = generateNotificationRecord(null).sbn; |
| 865 | sbn.getNotification().flags = |
| 866 | Notification.FLAG_ONGOING_EVENT | Notification.FLAG_FOREGROUND_SERVICE; |
| 867 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", |
| 868 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
| 869 | sbn.getNotification().flags = Notification.FLAG_ONGOING_EVENT; |
| 870 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", |
| 871 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
| 872 | mBinderService.cancelNotificationWithTag(PKG, "tag", sbn.getId(), sbn.getUserId()); |
| 873 | waitForIdle(); |
| 874 | assertEquals(0, mBinderService.getActiveNotifications(sbn.getPackageName()).length); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 875 | assertEquals(0, mService.getNotificationRecordCount()); |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 876 | } |
| 877 | |
| 878 | @Test |
Julia Reynolds | 40f00d7 | 2017-12-12 10:47:32 -0500 | [diff] [blame] | 879 | public void testCancelAllCancelNotificationsFromListener_ForegroundServiceFlag() |
| 880 | throws Exception { |
| 881 | final NotificationRecord parent = generateNotificationRecord( |
| 882 | mTestNotificationChannel, 1, "group", true); |
| 883 | final NotificationRecord child = generateNotificationRecord( |
| 884 | mTestNotificationChannel, 2, "group", false); |
| 885 | final NotificationRecord child2 = generateNotificationRecord( |
| 886 | mTestNotificationChannel, 3, "group", false); |
| 887 | child2.getNotification().flags |= Notification.FLAG_FOREGROUND_SERVICE; |
| 888 | final NotificationRecord newGroup = generateNotificationRecord( |
| 889 | mTestNotificationChannel, 4, "group2", false); |
| 890 | mService.addNotification(parent); |
| 891 | mService.addNotification(child); |
| 892 | mService.addNotification(child2); |
| 893 | mService.addNotification(newGroup); |
| 894 | mService.getBinderService().cancelNotificationsFromListener(null, null); |
| 895 | waitForIdle(); |
| 896 | StatusBarNotification[] notifs = |
| 897 | mBinderService.getActiveNotifications(parent.sbn.getPackageName()); |
| 898 | assertEquals(0, notifs.length); |
| 899 | } |
| 900 | |
| 901 | @Test |
| 902 | public void testCancelAllCancelNotificationsFromListener_ForegroundServiceFlagWithParameter() |
| 903 | throws Exception { |
| 904 | final NotificationRecord parent = generateNotificationRecord( |
| 905 | mTestNotificationChannel, 1, "group", true); |
| 906 | final NotificationRecord child = generateNotificationRecord( |
| 907 | mTestNotificationChannel, 2, "group", false); |
| 908 | final NotificationRecord child2 = generateNotificationRecord( |
| 909 | mTestNotificationChannel, 3, "group", false); |
| 910 | child2.getNotification().flags |= Notification.FLAG_FOREGROUND_SERVICE; |
| 911 | final NotificationRecord newGroup = generateNotificationRecord( |
| 912 | mTestNotificationChannel, 4, "group2", false); |
| 913 | mService.addNotification(parent); |
| 914 | mService.addNotification(child); |
| 915 | mService.addNotification(child2); |
| 916 | mService.addNotification(newGroup); |
| 917 | String[] keys = {parent.sbn.getKey(), child.sbn.getKey(), |
| 918 | child2.sbn.getKey(), newGroup.sbn.getKey()}; |
| 919 | mService.getBinderService().cancelNotificationsFromListener(null, keys); |
| 920 | waitForIdle(); |
| 921 | StatusBarNotification[] notifs = |
| 922 | mBinderService.getActiveNotifications(parent.sbn.getPackageName()); |
| 923 | assertEquals(1, notifs.length); |
| 924 | } |
| 925 | |
| 926 | @Test |
| 927 | public void testUserInitiatedCancelAllWithGroup_ForegroundServiceFlag() throws Exception { |
| 928 | final NotificationRecord parent = generateNotificationRecord( |
| 929 | mTestNotificationChannel, 1, "group", true); |
| 930 | final NotificationRecord child = generateNotificationRecord( |
| 931 | mTestNotificationChannel, 2, "group", false); |
| 932 | final NotificationRecord child2 = generateNotificationRecord( |
| 933 | mTestNotificationChannel, 3, "group", false); |
| 934 | child2.getNotification().flags |= Notification.FLAG_FOREGROUND_SERVICE; |
| 935 | final NotificationRecord newGroup = generateNotificationRecord( |
| 936 | mTestNotificationChannel, 4, "group2", false); |
| 937 | mService.addNotification(parent); |
| 938 | mService.addNotification(child); |
| 939 | mService.addNotification(child2); |
| 940 | mService.addNotification(newGroup); |
| 941 | mService.mNotificationDelegate.onClearAll(mUid, Binder.getCallingPid(), |
| 942 | parent.getUserId()); |
| 943 | waitForIdle(); |
| 944 | StatusBarNotification[] notifs = |
| 945 | mBinderService.getActiveNotifications(parent.sbn.getPackageName()); |
| 946 | assertEquals(0, notifs.length); |
| 947 | } |
| 948 | |
| 949 | @Test |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 950 | public void testFindGroupNotificationsLocked() throws Exception { |
| 951 | // make sure the same notification can be found in both lists and returned |
| 952 | final NotificationRecord group1 = generateNotificationRecord( |
| 953 | mTestNotificationChannel, 1, "group1", true); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 954 | mService.addEnqueuedNotification(group1); |
| 955 | mService.addNotification(group1); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 956 | |
| 957 | // should not be returned |
| 958 | final NotificationRecord group2 = generateNotificationRecord( |
| 959 | mTestNotificationChannel, 2, "group2", true); |
| 960 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", null, |
| 961 | group2.sbn.getId(), group2.sbn.getNotification(), group2.sbn.getUserId()); |
| 962 | waitForIdle(); |
| 963 | |
| 964 | // should not be returned |
| 965 | final NotificationRecord nonGroup = generateNotificationRecord( |
| 966 | mTestNotificationChannel, 3, null, false); |
| 967 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", null, |
| 968 | nonGroup.sbn.getId(), nonGroup.sbn.getNotification(), nonGroup.sbn.getUserId()); |
| 969 | waitForIdle(); |
| 970 | |
| 971 | // same group, child, should be returned |
| 972 | final NotificationRecord group1Child = generateNotificationRecord( |
| 973 | mTestNotificationChannel, 4, "group1", false); |
| 974 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", null, group1Child.sbn.getId(), |
| 975 | group1Child.sbn.getNotification(), group1Child.sbn.getUserId()); |
| 976 | waitForIdle(); |
| 977 | |
| 978 | List<NotificationRecord> inGroup1 = |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 979 | mService.findGroupNotificationsLocked(PKG, group1.getGroupKey(), |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 980 | group1.sbn.getUserId()); |
| 981 | assertEquals(3, inGroup1.size()); |
| 982 | for (NotificationRecord record : inGroup1) { |
| 983 | assertTrue(record.getGroupKey().equals(group1.getGroupKey())); |
| 984 | assertTrue(record.sbn.getId() == 1 || record.sbn.getId() == 4); |
| 985 | } |
| 986 | } |
| 987 | |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 988 | @Test |
Julia Reynolds | 40f00d7 | 2017-12-12 10:47:32 -0500 | [diff] [blame] | 989 | public void testCancelAllNotifications_CancelsNoClearFlagOnGoing() throws Exception { |
| 990 | final NotificationRecord notif = generateNotificationRecord( |
| 991 | mTestNotificationChannel, 1, "group", true); |
| 992 | notif.getNotification().flags |= Notification.FLAG_NO_CLEAR; |
| 993 | mService.addNotification(notif); |
| 994 | mService.cancelAllNotificationsInt(mUid, 0, PKG, null, 0, |
| 995 | Notification.FLAG_ONGOING_EVENT, true, notif.getUserId(), 0, null); |
| 996 | waitForIdle(); |
| 997 | StatusBarNotification[] notifs = |
| 998 | mBinderService.getActiveNotifications(notif.sbn.getPackageName()); |
| 999 | assertEquals(0, notifs.length); |
| 1000 | } |
| 1001 | |
| 1002 | @Test |
| 1003 | public void testCancelAllCancelNotificationsFromListener_NoClearFlagWithParameter() |
| 1004 | throws Exception { |
| 1005 | final NotificationRecord parent = generateNotificationRecord( |
| 1006 | mTestNotificationChannel, 1, "group", true); |
| 1007 | final NotificationRecord child = generateNotificationRecord( |
| 1008 | mTestNotificationChannel, 2, "group", false); |
| 1009 | final NotificationRecord child2 = generateNotificationRecord( |
| 1010 | mTestNotificationChannel, 3, "group", false); |
| 1011 | child2.getNotification().flags |= Notification.FLAG_NO_CLEAR; |
| 1012 | final NotificationRecord newGroup = generateNotificationRecord( |
| 1013 | mTestNotificationChannel, 4, "group2", false); |
| 1014 | mService.addNotification(parent); |
| 1015 | mService.addNotification(child); |
| 1016 | mService.addNotification(child2); |
| 1017 | mService.addNotification(newGroup); |
| 1018 | String[] keys = {parent.sbn.getKey(), child.sbn.getKey(), |
| 1019 | child2.sbn.getKey(), newGroup.sbn.getKey()}; |
| 1020 | mService.getBinderService().cancelNotificationsFromListener(null, keys); |
| 1021 | waitForIdle(); |
| 1022 | StatusBarNotification[] notifs = |
| 1023 | mBinderService.getActiveNotifications(parent.sbn.getPackageName()); |
| 1024 | assertEquals(0, notifs.length); |
| 1025 | } |
| 1026 | |
| 1027 | @Test |
| 1028 | public void testAppInitiatedCancelAllNotifications_CancelsOnGoingFlag() throws Exception { |
| 1029 | final StatusBarNotification sbn = generateNotificationRecord(null).sbn; |
| 1030 | sbn.getNotification().flags |= Notification.FLAG_ONGOING_EVENT; |
| 1031 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", |
| 1032 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
| 1033 | mBinderService.cancelAllNotifications(PKG, sbn.getUserId()); |
| 1034 | waitForIdle(); |
| 1035 | StatusBarNotification[] notifs = |
| 1036 | mBinderService.getActiveNotifications(sbn.getPackageName()); |
| 1037 | assertEquals(0, notifs.length); |
| 1038 | } |
| 1039 | |
| 1040 | @Test |
| 1041 | public void testCancelAllNotifications_CancelsOnGoingFlag() throws Exception { |
| 1042 | final NotificationRecord notif = generateNotificationRecord( |
| 1043 | mTestNotificationChannel, 1, "group", true); |
| 1044 | notif.getNotification().flags |= Notification.FLAG_ONGOING_EVENT; |
| 1045 | mService.addNotification(notif); |
| 1046 | mService.cancelAllNotificationsInt(mUid, 0, PKG, null, 0, 0, true, |
| 1047 | notif.getUserId(), 0, null); |
| 1048 | waitForIdle(); |
| 1049 | StatusBarNotification[] notifs = |
| 1050 | mBinderService.getActiveNotifications(notif.sbn.getPackageName()); |
| 1051 | assertEquals(0, notifs.length); |
| 1052 | } |
| 1053 | |
| 1054 | @Test |
| 1055 | public void testUserInitiatedCancelAllOnClearAll_OnGoingFlag() throws Exception { |
| 1056 | final NotificationRecord notif = generateNotificationRecord( |
| 1057 | mTestNotificationChannel, 1, "group", true); |
| 1058 | notif.getNotification().flags |= Notification.FLAG_ONGOING_EVENT; |
| 1059 | mService.addNotification(notif); |
| 1060 | |
| 1061 | mService.mNotificationDelegate.onClearAll(mUid, Binder.getCallingPid(), |
| 1062 | notif.getUserId()); |
| 1063 | waitForIdle(); |
| 1064 | StatusBarNotification[] notifs = |
| 1065 | mBinderService.getActiveNotifications(notif.sbn.getPackageName()); |
| 1066 | assertEquals(1, notifs.length); |
| 1067 | } |
| 1068 | |
| 1069 | @Test |
| 1070 | public void testCancelAllCancelNotificationsFromListener_OnGoingFlag() throws Exception { |
| 1071 | final NotificationRecord parent = generateNotificationRecord( |
| 1072 | mTestNotificationChannel, 1, "group", true); |
| 1073 | final NotificationRecord child = generateNotificationRecord( |
| 1074 | mTestNotificationChannel, 2, "group", false); |
| 1075 | final NotificationRecord child2 = generateNotificationRecord( |
| 1076 | mTestNotificationChannel, 3, "group", false); |
| 1077 | child2.getNotification().flags |= Notification.FLAG_ONGOING_EVENT; |
| 1078 | final NotificationRecord newGroup = generateNotificationRecord( |
| 1079 | mTestNotificationChannel, 4, "group2", false); |
| 1080 | mService.addNotification(parent); |
| 1081 | mService.addNotification(child); |
| 1082 | mService.addNotification(child2); |
| 1083 | mService.addNotification(newGroup); |
| 1084 | mService.getBinderService().cancelNotificationsFromListener(null, null); |
| 1085 | waitForIdle(); |
| 1086 | StatusBarNotification[] notifs = |
| 1087 | mBinderService.getActiveNotifications(parent.sbn.getPackageName()); |
| 1088 | assertEquals(1, notifs.length); |
| 1089 | } |
| 1090 | |
| 1091 | @Test |
| 1092 | public void testCancelAllCancelNotificationsFromListener_OnGoingFlagWithParameter() |
| 1093 | throws Exception { |
| 1094 | final NotificationRecord parent = generateNotificationRecord( |
| 1095 | mTestNotificationChannel, 1, "group", true); |
| 1096 | final NotificationRecord child = generateNotificationRecord( |
| 1097 | mTestNotificationChannel, 2, "group", false); |
| 1098 | final NotificationRecord child2 = generateNotificationRecord( |
| 1099 | mTestNotificationChannel, 3, "group", false); |
| 1100 | child2.getNotification().flags |= Notification.FLAG_ONGOING_EVENT; |
| 1101 | final NotificationRecord newGroup = generateNotificationRecord( |
| 1102 | mTestNotificationChannel, 4, "group2", false); |
| 1103 | mService.addNotification(parent); |
| 1104 | mService.addNotification(child); |
| 1105 | mService.addNotification(child2); |
| 1106 | mService.addNotification(newGroup); |
| 1107 | String[] keys = {parent.sbn.getKey(), child.sbn.getKey(), |
| 1108 | child2.sbn.getKey(), newGroup.sbn.getKey()}; |
| 1109 | mService.getBinderService().cancelNotificationsFromListener(null, keys); |
| 1110 | waitForIdle(); |
| 1111 | StatusBarNotification[] notifs = |
| 1112 | mBinderService.getActiveNotifications(parent.sbn.getPackageName()); |
| 1113 | assertEquals(0, notifs.length); |
| 1114 | } |
| 1115 | |
| 1116 | @Test |
| 1117 | public void testUserInitiatedCancelAllWithGroup_OnGoingFlag() throws Exception { |
| 1118 | final NotificationRecord parent = generateNotificationRecord( |
| 1119 | mTestNotificationChannel, 1, "group", true); |
| 1120 | final NotificationRecord child = generateNotificationRecord( |
| 1121 | mTestNotificationChannel, 2, "group", false); |
| 1122 | final NotificationRecord child2 = generateNotificationRecord( |
| 1123 | mTestNotificationChannel, 3, "group", false); |
| 1124 | child2.getNotification().flags |= Notification.FLAG_ONGOING_EVENT; |
| 1125 | final NotificationRecord newGroup = generateNotificationRecord( |
| 1126 | mTestNotificationChannel, 4, "group2", false); |
| 1127 | mService.addNotification(parent); |
| 1128 | mService.addNotification(child); |
| 1129 | mService.addNotification(child2); |
| 1130 | mService.addNotification(newGroup); |
| 1131 | mService.mNotificationDelegate.onClearAll(mUid, Binder.getCallingPid(), |
| 1132 | parent.getUserId()); |
| 1133 | waitForIdle(); |
| 1134 | StatusBarNotification[] notifs = |
| 1135 | mBinderService.getActiveNotifications(parent.sbn.getPackageName()); |
| 1136 | assertEquals(1, notifs.length); |
| 1137 | } |
| 1138 | |
| 1139 | @Test |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 1140 | public void testTvExtenderChannelOverride_onTv() throws Exception { |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1141 | mService.setIsTelevision(true); |
| 1142 | mService.setRankingHelper(mRankingHelper); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 1143 | when(mRankingHelper.getNotificationChannel( |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 1144 | anyString(), anyInt(), eq("foo"), anyBoolean())).thenReturn( |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 1145 | new NotificationChannel("foo", "foo", IMPORTANCE_HIGH)); |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 1146 | |
Julia Reynolds | bad4297 | 2017-04-25 13:52:49 -0400 | [diff] [blame] | 1147 | Notification.TvExtender tv = new Notification.TvExtender().setChannelId("foo"); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 1148 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", 0, |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 1149 | generateNotificationRecord(null, tv).getNotification(), 0); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 1150 | verify(mRankingHelper, times(1)).getNotificationChannel( |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 1151 | anyString(), anyInt(), eq("foo"), anyBoolean()); |
| 1152 | } |
| 1153 | |
| 1154 | @Test |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 1155 | public void testTvExtenderChannelOverride_notOnTv() throws Exception { |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1156 | mService.setIsTelevision(false); |
| 1157 | mService.setRankingHelper(mRankingHelper); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 1158 | when(mRankingHelper.getNotificationChannel( |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 1159 | anyString(), anyInt(), anyString(), anyBoolean())).thenReturn( |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 1160 | mTestNotificationChannel); |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 1161 | |
Julia Reynolds | bad4297 | 2017-04-25 13:52:49 -0400 | [diff] [blame] | 1162 | Notification.TvExtender tv = new Notification.TvExtender().setChannelId("foo"); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 1163 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", 0, |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 1164 | generateNotificationRecord(null, tv).getNotification(), 0); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 1165 | verify(mRankingHelper, times(1)).getNotificationChannel( |
| 1166 | anyString(), anyInt(), eq(mTestNotificationChannel.getId()), anyBoolean()); |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 1167 | } |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1168 | |
| 1169 | @Test |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 1170 | public void testUpdateChannelNotifyCreatorBlock() throws Exception { |
| 1171 | mService.setRankingHelper(mRankingHelper); |
| 1172 | when(mRankingHelper.getNotificationChannel(eq(PKG), anyInt(), |
| 1173 | eq(mTestNotificationChannel.getId()), anyBoolean())) |
| 1174 | .thenReturn(mTestNotificationChannel); |
| 1175 | |
| 1176 | NotificationChannel updatedChannel = |
| 1177 | new NotificationChannel(mTestNotificationChannel.getId(), |
| 1178 | mTestNotificationChannel.getName(), IMPORTANCE_NONE); |
| 1179 | |
| 1180 | mBinderService.updateNotificationChannelForPackage(PKG, 0, updatedChannel); |
| 1181 | ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class); |
| 1182 | verify(mContext, times(1)).sendBroadcastAsUser(captor.capture(), any(), eq(null)); |
| 1183 | |
| 1184 | assertEquals(NotificationManager.ACTION_NOTIFICATION_CHANNEL_BLOCK_STATE_CHANGED, |
| 1185 | captor.getValue().getAction()); |
| 1186 | assertEquals(PKG, captor.getValue().getPackage()); |
| 1187 | assertEquals(mTestNotificationChannel.getId(), captor.getValue().getStringExtra( |
| 1188 | NotificationManager.EXTRA_BLOCK_STATE_CHANGED_ID)); |
| 1189 | assertTrue(captor.getValue().getBooleanExtra(EXTRA_BLOCKED_STATE, false)); |
| 1190 | } |
| 1191 | |
| 1192 | @Test |
| 1193 | public void testUpdateChannelNotifyCreatorUnblock() throws Exception { |
| 1194 | NotificationChannel existingChannel = |
| 1195 | new NotificationChannel(mTestNotificationChannel.getId(), |
| 1196 | mTestNotificationChannel.getName(), IMPORTANCE_NONE); |
| 1197 | mService.setRankingHelper(mRankingHelper); |
| 1198 | when(mRankingHelper.getNotificationChannel(eq(PKG), anyInt(), |
| 1199 | eq(mTestNotificationChannel.getId()), anyBoolean())) |
| 1200 | .thenReturn(existingChannel); |
| 1201 | |
| 1202 | mBinderService.updateNotificationChannelForPackage(PKG, 0, mTestNotificationChannel); |
| 1203 | ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class); |
| 1204 | verify(mContext, times(1)).sendBroadcastAsUser(captor.capture(), any(), eq(null)); |
| 1205 | |
| 1206 | assertEquals(NotificationManager.ACTION_NOTIFICATION_CHANNEL_BLOCK_STATE_CHANGED, |
| 1207 | captor.getValue().getAction()); |
| 1208 | assertEquals(PKG, captor.getValue().getPackage()); |
| 1209 | assertEquals(mTestNotificationChannel.getId(), captor.getValue().getStringExtra( |
| 1210 | NotificationManager.EXTRA_BLOCK_STATE_CHANGED_ID)); |
| 1211 | assertFalse(captor.getValue().getBooleanExtra(EXTRA_BLOCKED_STATE, false)); |
| 1212 | } |
| 1213 | |
| 1214 | @Test |
| 1215 | public void testUpdateChannelNoNotifyCreatorOtherChanges() throws Exception { |
| 1216 | NotificationChannel existingChannel = |
| 1217 | new NotificationChannel(mTestNotificationChannel.getId(), |
| 1218 | mTestNotificationChannel.getName(), IMPORTANCE_MAX); |
| 1219 | mService.setRankingHelper(mRankingHelper); |
| 1220 | when(mRankingHelper.getNotificationChannel(eq(PKG), anyInt(), |
| 1221 | eq(mTestNotificationChannel.getId()), anyBoolean())) |
| 1222 | .thenReturn(existingChannel); |
| 1223 | |
| 1224 | mBinderService.updateNotificationChannelForPackage(PKG, 0, mTestNotificationChannel); |
| 1225 | verify(mContext, never()).sendBroadcastAsUser(any(), any(), eq(null)); |
| 1226 | } |
| 1227 | |
| 1228 | @Test |
| 1229 | public void testUpdateGroupNotifyCreatorBlock() throws Exception { |
| 1230 | NotificationChannelGroup existing = new NotificationChannelGroup("id", "name"); |
| 1231 | mService.setRankingHelper(mRankingHelper); |
| 1232 | when(mRankingHelper.getNotificationChannelGroup(eq(existing.getId()), eq(PKG), anyInt())) |
| 1233 | .thenReturn(existing); |
| 1234 | |
| 1235 | NotificationChannelGroup updated = new NotificationChannelGroup("id", "name"); |
| 1236 | updated.setBlocked(true); |
| 1237 | |
| 1238 | mBinderService.updateNotificationChannelGroupForPackage(PKG, 0, updated); |
| 1239 | ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class); |
| 1240 | verify(mContext, times(1)).sendBroadcastAsUser(captor.capture(), any(), eq(null)); |
| 1241 | |
| 1242 | assertEquals(NotificationManager.ACTION_NOTIFICATION_CHANNEL_GROUP_BLOCK_STATE_CHANGED, |
| 1243 | captor.getValue().getAction()); |
| 1244 | assertEquals(PKG, captor.getValue().getPackage()); |
| 1245 | assertEquals(existing.getId(), captor.getValue().getStringExtra( |
| 1246 | NotificationManager.EXTRA_BLOCK_STATE_CHANGED_ID)); |
| 1247 | assertTrue(captor.getValue().getBooleanExtra(EXTRA_BLOCKED_STATE, false)); |
| 1248 | } |
| 1249 | |
| 1250 | @Test |
| 1251 | public void testUpdateGroupNotifyCreatorUnblock() throws Exception { |
| 1252 | NotificationChannelGroup existing = new NotificationChannelGroup("id", "name"); |
| 1253 | existing.setBlocked(true); |
| 1254 | mService.setRankingHelper(mRankingHelper); |
| 1255 | when(mRankingHelper.getNotificationChannelGroup(eq(existing.getId()), eq(PKG), anyInt())) |
| 1256 | .thenReturn(existing); |
| 1257 | |
| 1258 | mBinderService.updateNotificationChannelGroupForPackage( |
| 1259 | PKG, 0, new NotificationChannelGroup("id", "name")); |
| 1260 | ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class); |
| 1261 | verify(mContext, times(1)).sendBroadcastAsUser(captor.capture(), any(), eq(null)); |
| 1262 | |
| 1263 | assertEquals(NotificationManager.ACTION_NOTIFICATION_CHANNEL_GROUP_BLOCK_STATE_CHANGED, |
| 1264 | captor.getValue().getAction()); |
| 1265 | assertEquals(PKG, captor.getValue().getPackage()); |
| 1266 | assertEquals(existing.getId(), captor.getValue().getStringExtra( |
| 1267 | NotificationManager.EXTRA_BLOCK_STATE_CHANGED_ID)); |
| 1268 | assertFalse(captor.getValue().getBooleanExtra(EXTRA_BLOCKED_STATE, false)); |
| 1269 | } |
| 1270 | |
| 1271 | @Test |
| 1272 | public void testUpdateGroupNoNotifyCreatorOtherChanges() throws Exception { |
| 1273 | NotificationChannelGroup existing = new NotificationChannelGroup("id", "name"); |
| 1274 | mService.setRankingHelper(mRankingHelper); |
| 1275 | when(mRankingHelper.getNotificationChannelGroup(eq(existing.getId()), eq(PKG), anyInt())) |
| 1276 | .thenReturn(existing); |
| 1277 | |
| 1278 | mBinderService.updateNotificationChannelGroupForPackage( |
| 1279 | PKG, 0, new NotificationChannelGroup("id", "new name")); |
| 1280 | verify(mContext, never()).sendBroadcastAsUser(any(), any(), eq(null)); |
| 1281 | } |
| 1282 | |
| 1283 | @Test |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1284 | public void testCreateChannelNotifyListener() throws Exception { |
| 1285 | List<String> associations = new ArrayList<>(); |
| 1286 | associations.add("a"); |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 1287 | when(mCompanionMgr.getAssociations(PKG, mUid)).thenReturn(associations); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1288 | mService.setRankingHelper(mRankingHelper); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1289 | when(mRankingHelper.getNotificationChannel(eq(PKG), anyInt(), |
| 1290 | eq(mTestNotificationChannel.getId()), anyBoolean())) |
| 1291 | .thenReturn(mTestNotificationChannel); |
| 1292 | NotificationChannel channel2 = new NotificationChannel("a", "b", IMPORTANCE_LOW); |
| 1293 | when(mRankingHelper.getNotificationChannel(eq(PKG), anyInt(), |
| 1294 | eq(channel2.getId()), anyBoolean())) |
| 1295 | .thenReturn(channel2); |
| 1296 | |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1297 | reset(mListeners); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1298 | mBinderService.createNotificationChannels(PKG, |
| 1299 | new ParceledListSlice(Arrays.asList(mTestNotificationChannel, channel2))); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1300 | verify(mListeners, times(1)).notifyNotificationChannelChanged(eq(PKG), |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 1301 | eq(Process.myUserHandle()), eq(mTestNotificationChannel), |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1302 | eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_ADDED)); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1303 | verify(mListeners, times(1)).notifyNotificationChannelChanged(eq(PKG), |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 1304 | eq(Process.myUserHandle()), eq(channel2), |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1305 | eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_ADDED)); |
| 1306 | } |
| 1307 | |
| 1308 | @Test |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1309 | public void testCreateChannelGroupNotifyListener() throws Exception { |
| 1310 | List<String> associations = new ArrayList<>(); |
| 1311 | associations.add("a"); |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 1312 | when(mCompanionMgr.getAssociations(PKG, mUid)).thenReturn(associations); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1313 | mService.setRankingHelper(mRankingHelper); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1314 | NotificationChannelGroup group1 = new NotificationChannelGroup("a", "b"); |
| 1315 | NotificationChannelGroup group2 = new NotificationChannelGroup("n", "m"); |
| 1316 | |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1317 | reset(mListeners); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1318 | mBinderService.createNotificationChannelGroups(PKG, |
| 1319 | new ParceledListSlice(Arrays.asList(group1, group2))); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1320 | verify(mListeners, times(1)).notifyNotificationChannelGroupChanged(eq(PKG), |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 1321 | eq(Process.myUserHandle()), eq(group1), |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1322 | eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_ADDED)); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1323 | verify(mListeners, times(1)).notifyNotificationChannelGroupChanged(eq(PKG), |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 1324 | eq(Process.myUserHandle()), eq(group2), |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1325 | eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_ADDED)); |
| 1326 | } |
| 1327 | |
| 1328 | @Test |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1329 | public void testUpdateChannelNotifyListener() throws Exception { |
| 1330 | List<String> associations = new ArrayList<>(); |
| 1331 | associations.add("a"); |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 1332 | when(mCompanionMgr.getAssociations(PKG, mUid)).thenReturn(associations); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1333 | mService.setRankingHelper(mRankingHelper); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1334 | mTestNotificationChannel.setLightColor(Color.CYAN); |
| 1335 | when(mRankingHelper.getNotificationChannel(eq(PKG), anyInt(), |
| 1336 | eq(mTestNotificationChannel.getId()), anyBoolean())) |
| 1337 | .thenReturn(mTestNotificationChannel); |
| 1338 | |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1339 | reset(mListeners); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1340 | mBinderService.updateNotificationChannelForPackage(PKG, 0, mTestNotificationChannel); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1341 | verify(mListeners, times(1)).notifyNotificationChannelChanged(eq(PKG), |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 1342 | eq(Process.myUserHandle()), eq(mTestNotificationChannel), |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1343 | eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_UPDATED)); |
| 1344 | } |
| 1345 | |
| 1346 | @Test |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1347 | public void testDeleteChannelNotifyListener() throws Exception { |
| 1348 | List<String> associations = new ArrayList<>(); |
| 1349 | associations.add("a"); |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 1350 | when(mCompanionMgr.getAssociations(PKG, mUid)).thenReturn(associations); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1351 | mService.setRankingHelper(mRankingHelper); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1352 | when(mRankingHelper.getNotificationChannel(eq(PKG), anyInt(), |
| 1353 | eq(mTestNotificationChannel.getId()), anyBoolean())) |
| 1354 | .thenReturn(mTestNotificationChannel); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1355 | reset(mListeners); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1356 | mBinderService.deleteNotificationChannel(PKG, mTestNotificationChannel.getId()); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1357 | verify(mListeners, times(1)).notifyNotificationChannelChanged(eq(PKG), |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 1358 | eq(Process.myUserHandle()), eq(mTestNotificationChannel), |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1359 | eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_DELETED)); |
| 1360 | } |
| 1361 | |
| 1362 | @Test |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1363 | public void testDeleteChannelGroupNotifyListener() throws Exception { |
| 1364 | List<String> associations = new ArrayList<>(); |
| 1365 | associations.add("a"); |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 1366 | when(mCompanionMgr.getAssociations(PKG, mUid)).thenReturn(associations); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1367 | NotificationChannelGroup ncg = new NotificationChannelGroup("a", "b/c"); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1368 | mService.setRankingHelper(mRankingHelper); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1369 | when(mRankingHelper.getNotificationChannelGroup(eq(ncg.getId()), eq(PKG), anyInt())) |
| 1370 | .thenReturn(ncg); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1371 | reset(mListeners); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1372 | mBinderService.deleteNotificationChannelGroup(PKG, ncg.getId()); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1373 | verify(mListeners, times(1)).notifyNotificationChannelGroupChanged(eq(PKG), |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 1374 | eq(Process.myUserHandle()), eq(ncg), |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1375 | eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_DELETED)); |
| 1376 | } |
| 1377 | |
| 1378 | @Test |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1379 | public void testUpdateNotificationChannelFromPrivilegedListener_success() throws Exception { |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1380 | mService.setRankingHelper(mRankingHelper); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1381 | List<String> associations = new ArrayList<>(); |
| 1382 | associations.add("a"); |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 1383 | when(mCompanionMgr.getAssociations(PKG, mUid)).thenReturn(associations); |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 1384 | when(mRankingHelper.getNotificationChannel(eq(PKG), anyInt(), |
| 1385 | eq(mTestNotificationChannel.getId()), anyBoolean())) |
| 1386 | .thenReturn(mTestNotificationChannel); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1387 | |
| 1388 | mBinderService.updateNotificationChannelFromPrivilegedListener( |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 1389 | null, PKG, Process.myUserHandle(), mTestNotificationChannel); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1390 | |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 1391 | verify(mRankingHelper, times(1)).updateNotificationChannel( |
| 1392 | anyString(), anyInt(), any(), anyBoolean()); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1393 | |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1394 | verify(mListeners, never()).notifyNotificationChannelChanged(eq(PKG), |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 1395 | eq(Process.myUserHandle()), eq(mTestNotificationChannel), |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1396 | eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_UPDATED)); |
| 1397 | } |
| 1398 | |
| 1399 | @Test |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1400 | public void testUpdateNotificationChannelFromPrivilegedListener_noAccess() throws Exception { |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1401 | mService.setRankingHelper(mRankingHelper); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1402 | List<String> associations = new ArrayList<>(); |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 1403 | when(mCompanionMgr.getAssociations(PKG, mUid)).thenReturn(associations); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1404 | |
| 1405 | try { |
| 1406 | mBinderService.updateNotificationChannelFromPrivilegedListener( |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 1407 | null, PKG, Process.myUserHandle(), mTestNotificationChannel); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1408 | fail("listeners that don't have a companion device shouldn't be able to call this"); |
| 1409 | } catch (SecurityException e) { |
| 1410 | // pass |
| 1411 | } |
| 1412 | |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 1413 | verify(mRankingHelper, never()).updateNotificationChannel( |
| 1414 | anyString(), anyInt(), any(), anyBoolean()); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1415 | |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1416 | verify(mListeners, never()).notifyNotificationChannelChanged(eq(PKG), |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 1417 | eq(Process.myUserHandle()), eq(mTestNotificationChannel), |
| 1418 | eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_UPDATED)); |
| 1419 | } |
| 1420 | |
| 1421 | @Test |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 1422 | public void testUpdateNotificationChannelFromPrivilegedListener_badUser() throws Exception { |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1423 | mService.setRankingHelper(mRankingHelper); |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 1424 | List<String> associations = new ArrayList<>(); |
| 1425 | associations.add("a"); |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 1426 | when(mCompanionMgr.getAssociations(PKG, mUid)).thenReturn(associations); |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 1427 | mListener = mock(ManagedServices.ManagedServiceInfo.class); |
Julia Reynolds | 4da7970 | 2017-06-01 11:06:10 -0400 | [diff] [blame] | 1428 | mListener.component = new ComponentName(PKG, PKG); |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 1429 | when(mListener.enabledAndUserMatches(anyInt())).thenReturn(false); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1430 | when(mListeners.checkServiceTokenLocked(any())).thenReturn(mListener); |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 1431 | |
| 1432 | try { |
| 1433 | mBinderService.updateNotificationChannelFromPrivilegedListener( |
| 1434 | null, PKG, UserHandle.ALL, mTestNotificationChannel); |
| 1435 | fail("incorrectly allowed a change to a user listener cannot see"); |
| 1436 | } catch (SecurityException e) { |
| 1437 | // pass |
| 1438 | } |
| 1439 | |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 1440 | verify(mRankingHelper, never()).updateNotificationChannel( |
| 1441 | anyString(), anyInt(), any(), anyBoolean()); |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 1442 | |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1443 | verify(mListeners, never()).notifyNotificationChannelChanged(eq(PKG), |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 1444 | eq(Process.myUserHandle()), eq(mTestNotificationChannel), |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1445 | eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_UPDATED)); |
| 1446 | } |
| 1447 | |
| 1448 | @Test |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1449 | public void testGetNotificationChannelFromPrivilegedListener_success() throws Exception { |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1450 | mService.setRankingHelper(mRankingHelper); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1451 | List<String> associations = new ArrayList<>(); |
| 1452 | associations.add("a"); |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 1453 | when(mCompanionMgr.getAssociations(PKG, mUid)).thenReturn(associations); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1454 | |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 1455 | mBinderService.getNotificationChannelsFromPrivilegedListener( |
| 1456 | null, PKG, Process.myUserHandle()); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1457 | |
| 1458 | verify(mRankingHelper, times(1)).getNotificationChannels( |
| 1459 | anyString(), anyInt(), anyBoolean()); |
| 1460 | } |
| 1461 | |
| 1462 | @Test |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1463 | public void testGetNotificationChannelFromPrivilegedListener_noAccess() throws Exception { |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1464 | mService.setRankingHelper(mRankingHelper); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1465 | List<String> associations = new ArrayList<>(); |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 1466 | when(mCompanionMgr.getAssociations(PKG, mUid)).thenReturn(associations); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1467 | |
| 1468 | try { |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 1469 | mBinderService.getNotificationChannelsFromPrivilegedListener( |
| 1470 | null, PKG, Process.myUserHandle()); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1471 | fail("listeners that don't have a companion device shouldn't be able to call this"); |
| 1472 | } catch (SecurityException e) { |
| 1473 | // pass |
| 1474 | } |
| 1475 | |
| 1476 | verify(mRankingHelper, never()).getNotificationChannels( |
| 1477 | anyString(), anyInt(), anyBoolean()); |
| 1478 | } |
| 1479 | |
| 1480 | @Test |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 1481 | public void testGetNotificationChannelFromPrivilegedListener_badUser() throws Exception { |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1482 | mService.setRankingHelper(mRankingHelper); |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 1483 | List<String> associations = new ArrayList<>(); |
| 1484 | associations.add("a"); |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 1485 | when(mCompanionMgr.getAssociations(PKG, mUid)).thenReturn(associations); |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 1486 | mListener = mock(ManagedServices.ManagedServiceInfo.class); |
| 1487 | when(mListener.enabledAndUserMatches(anyInt())).thenReturn(false); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1488 | when(mListeners.checkServiceTokenLocked(any())).thenReturn(mListener); |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 1489 | |
| 1490 | try { |
| 1491 | mBinderService.getNotificationChannelsFromPrivilegedListener( |
| 1492 | null, PKG, Process.myUserHandle()); |
| 1493 | fail("listener getting channels from a user they cannot see"); |
| 1494 | } catch (SecurityException e) { |
| 1495 | // pass |
| 1496 | } |
| 1497 | |
| 1498 | verify(mRankingHelper, never()).getNotificationChannels( |
| 1499 | anyString(), anyInt(), anyBoolean()); |
| 1500 | } |
| 1501 | |
| 1502 | @Test |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1503 | public void testGetNotificationChannelGroupsFromPrivilegedListener_success() throws Exception { |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1504 | mService.setRankingHelper(mRankingHelper); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1505 | List<String> associations = new ArrayList<>(); |
| 1506 | associations.add("a"); |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 1507 | when(mCompanionMgr.getAssociations(PKG, mUid)).thenReturn(associations); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1508 | |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 1509 | mBinderService.getNotificationChannelGroupsFromPrivilegedListener( |
| 1510 | null, PKG, Process.myUserHandle()); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1511 | |
| 1512 | verify(mRankingHelper, times(1)).getNotificationChannelGroups(anyString(), anyInt()); |
| 1513 | } |
| 1514 | |
| 1515 | @Test |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1516 | public void testGetNotificationChannelGroupsFromPrivilegedListener_noAccess() throws Exception { |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1517 | mService.setRankingHelper(mRankingHelper); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1518 | List<String> associations = new ArrayList<>(); |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 1519 | when(mCompanionMgr.getAssociations(PKG, mUid)).thenReturn(associations); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1520 | |
| 1521 | try { |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 1522 | mBinderService.getNotificationChannelGroupsFromPrivilegedListener( |
| 1523 | null, PKG, Process.myUserHandle()); |
| 1524 | fail("listeners that don't have a companion device shouldn't be able to call this"); |
| 1525 | } catch (SecurityException e) { |
| 1526 | // pass |
| 1527 | } |
| 1528 | |
| 1529 | verify(mRankingHelper, never()).getNotificationChannelGroups(anyString(), anyInt()); |
| 1530 | } |
| 1531 | |
| 1532 | @Test |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 1533 | public void testGetNotificationChannelGroupsFromPrivilegedListener_badUser() throws Exception { |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1534 | mService.setRankingHelper(mRankingHelper); |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 1535 | List<String> associations = new ArrayList<>(); |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 1536 | when(mCompanionMgr.getAssociations(PKG, mUid)).thenReturn(associations); |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 1537 | mListener = mock(ManagedServices.ManagedServiceInfo.class); |
| 1538 | when(mListener.enabledAndUserMatches(anyInt())).thenReturn(false); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1539 | when(mListeners.checkServiceTokenLocked(any())).thenReturn(mListener); |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 1540 | |
| 1541 | try { |
| 1542 | mBinderService.getNotificationChannelGroupsFromPrivilegedListener( |
| 1543 | null, PKG, Process.myUserHandle()); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1544 | fail("listeners that don't have a companion device shouldn't be able to call this"); |
| 1545 | } catch (SecurityException e) { |
| 1546 | // pass |
| 1547 | } |
| 1548 | |
| 1549 | verify(mRankingHelper, never()).getNotificationChannelGroups(anyString(), anyInt()); |
| 1550 | } |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 1551 | |
| 1552 | @Test |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 1553 | public void testHasCompanionDevice_failure() throws Exception { |
| 1554 | when(mCompanionMgr.getAssociations(anyString(), anyInt())).thenThrow( |
| 1555 | new IllegalArgumentException()); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1556 | mService.hasCompanionDevice(mListener); |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 1557 | } |
Julia Reynolds | 727a728 | 2017-04-13 10:54:01 -0400 | [diff] [blame] | 1558 | |
| 1559 | @Test |
Julia Reynolds | 727a728 | 2017-04-13 10:54:01 -0400 | [diff] [blame] | 1560 | public void testHasCompanionDevice_noService() throws Exception { |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1561 | mService = new TestableNotificationManagerService(mContext); |
Julia Reynolds | 727a728 | 2017-04-13 10:54:01 -0400 | [diff] [blame] | 1562 | |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1563 | assertFalse(mService.hasCompanionDevice(mListener)); |
Julia Reynolds | 727a728 | 2017-04-13 10:54:01 -0400 | [diff] [blame] | 1564 | } |
| 1565 | |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 1566 | @Test |
| 1567 | public void testSnoozeRunnable_snoozeNonGrouped() throws Exception { |
| 1568 | final NotificationRecord nonGrouped = generateNotificationRecord( |
| 1569 | mTestNotificationChannel, 1, null, false); |
| 1570 | final NotificationRecord grouped = generateNotificationRecord( |
| 1571 | mTestNotificationChannel, 2, "group", false); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1572 | mService.addNotification(grouped); |
| 1573 | mService.addNotification(nonGrouped); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 1574 | |
| 1575 | NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable = |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1576 | mService.new SnoozeNotificationRunnable( |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 1577 | nonGrouped.getKey(), 100, null); |
| 1578 | snoozeNotificationRunnable.run(); |
| 1579 | |
| 1580 | // only snooze the one notification |
| 1581 | verify(mSnoozeHelper, times(1)).snooze(any(NotificationRecord.class), anyLong()); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1582 | assertTrue(nonGrouped.getStats().hasSnoozed()); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 1583 | } |
| 1584 | |
| 1585 | @Test |
| 1586 | public void testSnoozeRunnable_snoozeSummary_withChildren() throws Exception { |
| 1587 | final NotificationRecord parent = generateNotificationRecord( |
| 1588 | mTestNotificationChannel, 1, "group", true); |
| 1589 | final NotificationRecord child = generateNotificationRecord( |
| 1590 | mTestNotificationChannel, 2, "group", false); |
| 1591 | final NotificationRecord child2 = generateNotificationRecord( |
| 1592 | mTestNotificationChannel, 3, "group", false); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1593 | mService.addNotification(parent); |
| 1594 | mService.addNotification(child); |
| 1595 | mService.addNotification(child2); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 1596 | |
| 1597 | NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable = |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1598 | mService.new SnoozeNotificationRunnable( |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 1599 | parent.getKey(), 100, null); |
| 1600 | snoozeNotificationRunnable.run(); |
| 1601 | |
| 1602 | // snooze parent and children |
| 1603 | verify(mSnoozeHelper, times(3)).snooze(any(NotificationRecord.class), anyLong()); |
| 1604 | } |
| 1605 | |
| 1606 | @Test |
| 1607 | public void testSnoozeRunnable_snoozeGroupChild_fellowChildren() throws Exception { |
| 1608 | final NotificationRecord parent = generateNotificationRecord( |
| 1609 | mTestNotificationChannel, 1, "group", true); |
| 1610 | final NotificationRecord child = generateNotificationRecord( |
| 1611 | mTestNotificationChannel, 2, "group", false); |
| 1612 | final NotificationRecord child2 = generateNotificationRecord( |
| 1613 | mTestNotificationChannel, 3, "group", false); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1614 | mService.addNotification(parent); |
| 1615 | mService.addNotification(child); |
| 1616 | mService.addNotification(child2); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 1617 | |
| 1618 | NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable = |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1619 | mService.new SnoozeNotificationRunnable( |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 1620 | child2.getKey(), 100, null); |
| 1621 | snoozeNotificationRunnable.run(); |
| 1622 | |
| 1623 | // only snooze the one child |
| 1624 | verify(mSnoozeHelper, times(1)).snooze(any(NotificationRecord.class), anyLong()); |
| 1625 | } |
| 1626 | |
| 1627 | @Test |
| 1628 | public void testSnoozeRunnable_snoozeGroupChild_onlyChildOfSummary() throws Exception { |
| 1629 | final NotificationRecord parent = generateNotificationRecord( |
| 1630 | mTestNotificationChannel, 1, "group", true); |
| 1631 | assertTrue(parent.sbn.getNotification().isGroupSummary()); |
| 1632 | final NotificationRecord child = generateNotificationRecord( |
| 1633 | mTestNotificationChannel, 2, "group", false); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1634 | mService.addNotification(parent); |
| 1635 | mService.addNotification(child); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 1636 | |
| 1637 | NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable = |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1638 | mService.new SnoozeNotificationRunnable( |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 1639 | child.getKey(), 100, null); |
| 1640 | snoozeNotificationRunnable.run(); |
| 1641 | |
| 1642 | // snooze child and summary |
| 1643 | verify(mSnoozeHelper, times(2)).snooze(any(NotificationRecord.class), anyLong()); |
| 1644 | } |
| 1645 | |
| 1646 | @Test |
| 1647 | public void testSnoozeRunnable_snoozeGroupChild_noOthersInGroup() throws Exception { |
| 1648 | final NotificationRecord child = generateNotificationRecord( |
| 1649 | mTestNotificationChannel, 2, "group", false); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1650 | mService.addNotification(child); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 1651 | |
| 1652 | NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable = |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1653 | mService.new SnoozeNotificationRunnable( |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 1654 | child.getKey(), 100, null); |
| 1655 | snoozeNotificationRunnable.run(); |
| 1656 | |
| 1657 | // snooze child only |
| 1658 | verify(mSnoozeHelper, times(1)).snooze(any(NotificationRecord.class), anyLong()); |
| 1659 | } |
| 1660 | |
| 1661 | @Test |
| 1662 | public void testPostGroupChild_unsnoozeParent() throws Exception { |
| 1663 | final NotificationRecord child = generateNotificationRecord( |
| 1664 | mTestNotificationChannel, 2, "group", false); |
| 1665 | |
| 1666 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", null, |
| 1667 | child.sbn.getId(), child.sbn.getNotification(), child.sbn.getUserId()); |
| 1668 | waitForIdle(); |
| 1669 | |
| 1670 | verify(mSnoozeHelper, times(1)).repostGroupSummary( |
| 1671 | anyString(), anyInt(), eq(child.getGroupKey())); |
| 1672 | } |
| 1673 | |
| 1674 | @Test |
| 1675 | public void testPostNonGroup_noUnsnoozing() throws Exception { |
| 1676 | final NotificationRecord record = generateNotificationRecord( |
| 1677 | mTestNotificationChannel, 2, null, false); |
| 1678 | |
| 1679 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", null, |
| 1680 | record.sbn.getId(), record.sbn.getNotification(), record.sbn.getUserId()); |
| 1681 | waitForIdle(); |
| 1682 | |
| 1683 | verify(mSnoozeHelper, never()).repostGroupSummary(anyString(), anyInt(), anyString()); |
| 1684 | } |
| 1685 | |
| 1686 | @Test |
| 1687 | public void testPostGroupSummary_noUnsnoozing() throws Exception { |
| 1688 | final NotificationRecord parent = generateNotificationRecord( |
| 1689 | mTestNotificationChannel, 2, "group", true); |
| 1690 | |
| 1691 | mBinderService.enqueueNotificationWithTag(PKG, "opPkg", null, |
| 1692 | parent.sbn.getId(), parent.sbn.getNotification(), parent.sbn.getUserId()); |
| 1693 | waitForIdle(); |
| 1694 | |
| 1695 | verify(mSnoozeHelper, never()).repostGroupSummary(anyString(), anyInt(), anyString()); |
| 1696 | } |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1697 | |
| 1698 | @Test |
Julia Reynolds | 92febc3 | 2017-10-26 11:30:31 -0400 | [diff] [blame] | 1699 | public void testSetListenerAccessForUser() throws Exception { |
| 1700 | UserHandle user = UserHandle.of(10); |
| 1701 | ComponentName c = ComponentName.unflattenFromString("package/Component"); |
| 1702 | try { |
| 1703 | mBinderService.setNotificationListenerAccessGrantedForUser( |
| 1704 | c, user.getIdentifier(), true); |
| 1705 | } catch (SecurityException e) { |
| 1706 | if (!e.getMessage().contains("Permission Denial: not allowed to send broadcast")) { |
| 1707 | throw e; |
| 1708 | } |
| 1709 | } |
| 1710 | |
| 1711 | verify(mContext, times(1)).sendBroadcastAsUser(any(), eq(user), any()); |
| 1712 | verify(mListeners, times(1)).setPackageOrComponentEnabled( |
| 1713 | c.flattenToString(), user.getIdentifier(), true, true); |
| 1714 | verify(mConditionProviders, times(1)).setPackageOrComponentEnabled( |
| 1715 | c.flattenToString(), user.getIdentifier(), false, true); |
| 1716 | verify(mAssistants, never()).setPackageOrComponentEnabled( |
| 1717 | any(), anyInt(), anyBoolean(), anyBoolean()); |
| 1718 | } |
| 1719 | |
| 1720 | @Test |
| 1721 | public void testSetAssistantAccessForUser() throws Exception { |
| 1722 | UserHandle user = UserHandle.of(10); |
| 1723 | ComponentName c = ComponentName.unflattenFromString("package/Component"); |
| 1724 | try { |
| 1725 | mBinderService.setNotificationAssistantAccessGrantedForUser( |
| 1726 | c, user.getIdentifier(), true); |
| 1727 | } catch (SecurityException e) { |
| 1728 | if (!e.getMessage().contains("Permission Denial: not allowed to send broadcast")) { |
| 1729 | throw e; |
| 1730 | } |
| 1731 | } |
| 1732 | |
| 1733 | verify(mContext, times(1)).sendBroadcastAsUser(any(), eq(user), any()); |
| 1734 | verify(mAssistants, times(1)).setPackageOrComponentEnabled( |
| 1735 | c.flattenToString(), user.getIdentifier(), true, true); |
| 1736 | verify(mConditionProviders, times(1)).setPackageOrComponentEnabled( |
| 1737 | c.flattenToString(), user.getIdentifier(), false, true); |
| 1738 | verify(mListeners, never()).setPackageOrComponentEnabled( |
| 1739 | any(), anyInt(), anyBoolean(), anyBoolean()); |
| 1740 | } |
| 1741 | |
| 1742 | @Test |
| 1743 | public void testSetDndAccessForUser() throws Exception { |
| 1744 | UserHandle user = UserHandle.of(10); |
| 1745 | ComponentName c = ComponentName.unflattenFromString("package/Component"); |
| 1746 | try { |
| 1747 | mBinderService.setNotificationPolicyAccessGrantedForUser( |
| 1748 | c.getPackageName(), user.getIdentifier(), true); |
| 1749 | } catch (SecurityException e) { |
| 1750 | if (!e.getMessage().contains("Permission Denial: not allowed to send broadcast")) { |
| 1751 | throw e; |
| 1752 | } |
| 1753 | } |
| 1754 | |
| 1755 | verify(mContext, times(1)).sendBroadcastAsUser(any(), eq(user), any()); |
| 1756 | verify(mConditionProviders, times(1)).setPackageOrComponentEnabled( |
| 1757 | c.getPackageName(), user.getIdentifier(), true, true); |
| 1758 | verify(mAssistants, never()).setPackageOrComponentEnabled( |
| 1759 | any(), anyInt(), anyBoolean(), anyBoolean()); |
| 1760 | verify(mListeners, never()).setPackageOrComponentEnabled( |
| 1761 | any(), anyInt(), anyBoolean(), anyBoolean()); |
| 1762 | } |
| 1763 | |
| 1764 | @Test |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1765 | public void testSetListenerAccess() throws Exception { |
| 1766 | ComponentName c = ComponentName.unflattenFromString("package/Component"); |
| 1767 | try { |
| 1768 | mBinderService.setNotificationListenerAccessGranted(c, true); |
| 1769 | } catch (SecurityException e) { |
| 1770 | if (!e.getMessage().contains("Permission Denial: not allowed to send broadcast")) { |
| 1771 | throw e; |
| 1772 | } |
| 1773 | } |
| 1774 | |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1775 | verify(mListeners, times(1)).setPackageOrComponentEnabled( |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1776 | c.flattenToString(), 0, true, true); |
| 1777 | verify(mConditionProviders, times(1)).setPackageOrComponentEnabled( |
| 1778 | c.flattenToString(), 0, false, true); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1779 | verify(mAssistants, never()).setPackageOrComponentEnabled( |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1780 | any(), anyInt(), anyBoolean(), anyBoolean()); |
| 1781 | } |
| 1782 | |
| 1783 | @Test |
| 1784 | public void testSetAssistantAccess() throws Exception { |
| 1785 | ComponentName c = ComponentName.unflattenFromString("package/Component"); |
| 1786 | try { |
| 1787 | mBinderService.setNotificationAssistantAccessGranted(c, true); |
| 1788 | } catch (SecurityException e) { |
| 1789 | if (!e.getMessage().contains("Permission Denial: not allowed to send broadcast")) { |
| 1790 | throw e; |
| 1791 | } |
| 1792 | } |
| 1793 | |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1794 | verify(mAssistants, times(1)).setPackageOrComponentEnabled( |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1795 | c.flattenToString(), 0, true, true); |
| 1796 | verify(mConditionProviders, times(1)).setPackageOrComponentEnabled( |
| 1797 | c.flattenToString(), 0, false, true); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1798 | verify(mListeners, never()).setPackageOrComponentEnabled( |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1799 | any(), anyInt(), anyBoolean(), anyBoolean()); |
| 1800 | } |
| 1801 | |
| 1802 | @Test |
| 1803 | public void testSetDndAccess() throws Exception { |
| 1804 | ComponentName c = ComponentName.unflattenFromString("package/Component"); |
| 1805 | try { |
| 1806 | mBinderService.setNotificationPolicyAccessGranted(c.getPackageName(), true); |
| 1807 | } catch (SecurityException e) { |
| 1808 | if (!e.getMessage().contains("Permission Denial: not allowed to send broadcast")) { |
| 1809 | throw e; |
| 1810 | } |
| 1811 | } |
| 1812 | |
| 1813 | verify(mConditionProviders, times(1)).setPackageOrComponentEnabled( |
| 1814 | c.getPackageName(), 0, true, true); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1815 | verify(mAssistants, never()).setPackageOrComponentEnabled( |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1816 | any(), anyInt(), anyBoolean(), anyBoolean()); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1817 | verify(mListeners, never()).setPackageOrComponentEnabled( |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1818 | any(), anyInt(), anyBoolean(), anyBoolean()); |
| 1819 | } |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 1820 | |
| 1821 | @Test |
| 1822 | public void testSetListenerAccess_doesNothingOnLowRam() throws Exception { |
| 1823 | when(mActivityManager.isLowRamDevice()).thenReturn(true); |
| 1824 | ComponentName c = ComponentName.unflattenFromString("package/Component"); |
| 1825 | mBinderService.setNotificationListenerAccessGranted(c, true); |
| 1826 | |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1827 | verify(mListeners, never()).setPackageOrComponentEnabled( |
Julia Reynolds | e181641 | 2017-10-24 10:39:11 -0400 | [diff] [blame] | 1828 | anyString(), anyInt(), anyBoolean(), anyBoolean()); |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 1829 | verify(mConditionProviders, never()).setPackageOrComponentEnabled( |
Julia Reynolds | e181641 | 2017-10-24 10:39:11 -0400 | [diff] [blame] | 1830 | anyString(), anyInt(), anyBoolean(), anyBoolean()); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1831 | verify(mAssistants, never()).setPackageOrComponentEnabled( |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 1832 | any(), anyInt(), anyBoolean(), anyBoolean()); |
| 1833 | } |
| 1834 | |
| 1835 | @Test |
| 1836 | public void testSetAssistantAccess_doesNothingOnLowRam() throws Exception { |
| 1837 | when(mActivityManager.isLowRamDevice()).thenReturn(true); |
| 1838 | ComponentName c = ComponentName.unflattenFromString("package/Component"); |
| 1839 | mBinderService.setNotificationAssistantAccessGranted(c, true); |
| 1840 | |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1841 | verify(mListeners, never()).setPackageOrComponentEnabled( |
Julia Reynolds | e181641 | 2017-10-24 10:39:11 -0400 | [diff] [blame] | 1842 | anyString(), anyInt(), anyBoolean(), anyBoolean()); |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 1843 | verify(mConditionProviders, never()).setPackageOrComponentEnabled( |
Julia Reynolds | e181641 | 2017-10-24 10:39:11 -0400 | [diff] [blame] | 1844 | anyString(), anyInt(), anyBoolean(), anyBoolean()); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1845 | verify(mAssistants, never()).setPackageOrComponentEnabled( |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 1846 | any(), anyInt(), anyBoolean(), anyBoolean()); |
| 1847 | } |
| 1848 | |
| 1849 | @Test |
| 1850 | public void testSetDndAccess_doesNothingOnLowRam() throws Exception { |
| 1851 | when(mActivityManager.isLowRamDevice()).thenReturn(true); |
| 1852 | ComponentName c = ComponentName.unflattenFromString("package/Component"); |
| 1853 | mBinderService.setNotificationPolicyAccessGranted(c.getPackageName(), true); |
| 1854 | |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1855 | verify(mListeners, never()).setPackageOrComponentEnabled( |
Julia Reynolds | e181641 | 2017-10-24 10:39:11 -0400 | [diff] [blame] | 1856 | anyString(), anyInt(), anyBoolean(), anyBoolean()); |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 1857 | verify(mConditionProviders, never()).setPackageOrComponentEnabled( |
Julia Reynolds | e181641 | 2017-10-24 10:39:11 -0400 | [diff] [blame] | 1858 | anyString(), anyInt(), anyBoolean(), anyBoolean()); |
| 1859 | verify(mAssistants, never()).setPackageOrComponentEnabled( |
| 1860 | any(), anyInt(), anyBoolean(), anyBoolean()); |
| 1861 | } |
| 1862 | |
| 1863 | @Test |
| 1864 | public void testSetListenerAccess_doesNothingOnLowRam_exceptWatch() throws Exception { |
| 1865 | when(mPackageManagerClient.hasSystemFeature(FEATURE_WATCH)).thenReturn(true); |
| 1866 | when(mActivityManager.isLowRamDevice()).thenReturn(true); |
| 1867 | ComponentName c = ComponentName.unflattenFromString("package/Component"); |
| 1868 | try { |
| 1869 | mBinderService.setNotificationListenerAccessGranted(c, true); |
| 1870 | } catch (SecurityException e) { |
| 1871 | if (!e.getMessage().contains("Permission Denial: not allowed to send broadcast")) { |
| 1872 | throw e; |
| 1873 | } |
| 1874 | } |
| 1875 | |
| 1876 | verify(mListeners, times(1)).setPackageOrComponentEnabled( |
| 1877 | c.flattenToString(), 0, true, true); |
| 1878 | verify(mConditionProviders, times(1)).setPackageOrComponentEnabled( |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 1879 | c.flattenToString(), 0, false, true); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1880 | verify(mAssistants, never()).setPackageOrComponentEnabled( |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 1881 | any(), anyInt(), anyBoolean(), anyBoolean()); |
| 1882 | } |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 1883 | |
| 1884 | @Test |
Julia Reynolds | e181641 | 2017-10-24 10:39:11 -0400 | [diff] [blame] | 1885 | public void testSetAssistantAccess_doesNothingOnLowRam_exceptWatch() throws Exception { |
| 1886 | when(mPackageManagerClient.hasSystemFeature(FEATURE_WATCH)).thenReturn(true); |
| 1887 | when(mActivityManager.isLowRamDevice()).thenReturn(true); |
| 1888 | ComponentName c = ComponentName.unflattenFromString("package/Component"); |
| 1889 | try { |
| 1890 | mBinderService.setNotificationAssistantAccessGranted(c, true); |
| 1891 | } catch (SecurityException e) { |
| 1892 | if (!e.getMessage().contains("Permission Denial: not allowed to send broadcast")) { |
| 1893 | throw e; |
| 1894 | } |
| 1895 | } |
| 1896 | |
| 1897 | verify(mListeners, never()).setPackageOrComponentEnabled( |
| 1898 | anyString(), anyInt(), anyBoolean(), anyBoolean()); |
| 1899 | verify(mConditionProviders, times(1)).setPackageOrComponentEnabled( |
| 1900 | c.flattenToString(), 0, false, true); |
| 1901 | verify(mAssistants, times(1)).setPackageOrComponentEnabled( |
| 1902 | c.flattenToString(), 0, true, true); |
| 1903 | } |
| 1904 | |
| 1905 | @Test |
| 1906 | public void testSetDndAccess_doesNothingOnLowRam_exceptWatch() throws Exception { |
| 1907 | when(mPackageManagerClient.hasSystemFeature(FEATURE_WATCH)).thenReturn(true); |
| 1908 | when(mActivityManager.isLowRamDevice()).thenReturn(true); |
| 1909 | ComponentName c = ComponentName.unflattenFromString("package/Component"); |
| 1910 | try { |
| 1911 | mBinderService.setNotificationPolicyAccessGranted(c.getPackageName(), true); |
| 1912 | } catch (SecurityException e) { |
| 1913 | if (!e.getMessage().contains("Permission Denial: not allowed to send broadcast")) { |
| 1914 | throw e; |
| 1915 | } |
| 1916 | } |
| 1917 | |
| 1918 | verify(mListeners, never()).setPackageOrComponentEnabled( |
| 1919 | anyString(), anyInt(), anyBoolean(), anyBoolean()); |
| 1920 | verify(mConditionProviders, times(1)).setPackageOrComponentEnabled( |
| 1921 | c.getPackageName(), 0, true, true); |
| 1922 | verify(mAssistants, never()).setPackageOrComponentEnabled( |
| 1923 | any(), anyInt(), anyBoolean(), anyBoolean()); |
| 1924 | } |
| 1925 | |
| 1926 | @Test |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 1927 | public void testOnlyAutogroupIfGroupChanged_noPriorNoti_autogroups() throws Exception { |
| 1928 | NotificationRecord r = generateNotificationRecord(mTestNotificationChannel, 0, null, false); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1929 | mService.addEnqueuedNotification(r); |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 1930 | NotificationManagerService.PostNotificationRunnable runnable = |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1931 | mService.new PostNotificationRunnable(r.getKey()); |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 1932 | runnable.run(); |
| 1933 | waitForIdle(); |
| 1934 | |
Julia Reynolds | a13b3e2 | 2017-08-10 16:58:54 -0400 | [diff] [blame] | 1935 | verify(mGroupHelper, times(1)).onNotificationPosted(any(), anyBoolean()); |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 1936 | } |
| 1937 | |
| 1938 | @Test |
| 1939 | public void testOnlyAutogroupIfGroupChanged_groupChanged_autogroups() |
| 1940 | throws Exception { |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 1941 | NotificationRecord r = |
| 1942 | generateNotificationRecord(mTestNotificationChannel, 0, "group", false); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1943 | mService.addNotification(r); |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 1944 | |
| 1945 | r = generateNotificationRecord(mTestNotificationChannel, 0, null, false); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1946 | mService.addEnqueuedNotification(r); |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 1947 | NotificationManagerService.PostNotificationRunnable runnable = |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1948 | mService.new PostNotificationRunnable(r.getKey()); |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 1949 | runnable.run(); |
| 1950 | waitForIdle(); |
| 1951 | |
Julia Reynolds | a13b3e2 | 2017-08-10 16:58:54 -0400 | [diff] [blame] | 1952 | verify(mGroupHelper, times(1)).onNotificationPosted(any(), anyBoolean()); |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 1953 | } |
| 1954 | |
| 1955 | @Test |
| 1956 | public void testOnlyAutogroupIfGroupChanged_noGroupChanged_autogroups() |
| 1957 | throws Exception { |
Julia Reynolds | 4db5955 | 2017-06-30 13:34:01 -0400 | [diff] [blame] | 1958 | NotificationRecord r = generateNotificationRecord(mTestNotificationChannel, 0, "group", |
| 1959 | false); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1960 | mService.addNotification(r); |
| 1961 | mService.addEnqueuedNotification(r); |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 1962 | |
| 1963 | NotificationManagerService.PostNotificationRunnable runnable = |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1964 | mService.new PostNotificationRunnable(r.getKey()); |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 1965 | runnable.run(); |
| 1966 | waitForIdle(); |
| 1967 | |
Julia Reynolds | a13b3e2 | 2017-08-10 16:58:54 -0400 | [diff] [blame] | 1968 | verify(mGroupHelper, never()).onNotificationPosted(any(), anyBoolean()); |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 1969 | } |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 1970 | |
Julia Reynolds | 4db5955 | 2017-06-30 13:34:01 -0400 | [diff] [blame] | 1971 | @Test |
| 1972 | public void testNoFakeColorizedPermission() throws Exception { |
| 1973 | when(mPackageManagerClient.checkPermission(any(), any())).thenReturn(PERMISSION_DENIED); |
| 1974 | Notification.Builder nb = new Notification.Builder(mContext, |
| 1975 | mTestNotificationChannel.getId()) |
| 1976 | .setContentTitle("foo") |
| 1977 | .setColorized(true) |
| 1978 | .setFlag(Notification.FLAG_CAN_COLORIZE, true) |
| 1979 | .setSmallIcon(android.R.drawable.sym_def_app_icon); |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 1980 | StatusBarNotification sbn = new StatusBarNotification(PKG, PKG, 1, "tag", mUid, 0, |
| 1981 | nb.build(), new UserHandle(mUid), null, 0); |
Julia Reynolds | 4db5955 | 2017-06-30 13:34:01 -0400 | [diff] [blame] | 1982 | NotificationRecord nr = new NotificationRecord(mContext, sbn, mTestNotificationChannel); |
| 1983 | |
| 1984 | mBinderService.enqueueNotificationWithTag(PKG, PKG, null, |
| 1985 | nr.sbn.getId(), nr.sbn.getNotification(), nr.sbn.getUserId()); |
| 1986 | waitForIdle(); |
| 1987 | |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1988 | NotificationRecord posted = mService.findNotificationLocked( |
Julia Reynolds | 4db5955 | 2017-06-30 13:34:01 -0400 | [diff] [blame] | 1989 | PKG, null, nr.sbn.getId(), nr.sbn.getUserId()); |
| 1990 | |
| 1991 | assertFalse(posted.getNotification().isColorized()); |
| 1992 | } |
Julia Reynolds | 6ad0aec | 2017-07-05 08:47:03 -0400 | [diff] [blame] | 1993 | |
| 1994 | @Test |
| 1995 | public void testGetNotificationCountLocked() throws Exception { |
| 1996 | for (int i = 0; i < 20; i++) { |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 1997 | NotificationRecord r = |
| 1998 | generateNotificationRecord(mTestNotificationChannel, i, null, false); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1999 | mService.addEnqueuedNotification(r); |
Julia Reynolds | 6ad0aec | 2017-07-05 08:47:03 -0400 | [diff] [blame] | 2000 | } |
| 2001 | for (int i = 0; i < 20; i++) { |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 2002 | NotificationRecord r = |
| 2003 | generateNotificationRecord(mTestNotificationChannel, i, null, false); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2004 | mService.addNotification(r); |
Julia Reynolds | 6ad0aec | 2017-07-05 08:47:03 -0400 | [diff] [blame] | 2005 | } |
| 2006 | |
| 2007 | // another package |
| 2008 | Notification n = |
| 2009 | new Notification.Builder(mContext, mTestNotificationChannel.getId()) |
| 2010 | .setSmallIcon(android.R.drawable.sym_def_app_icon) |
| 2011 | .build(); |
| 2012 | |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 2013 | StatusBarNotification sbn = new StatusBarNotification("a", "a", 0, "tag", mUid, 0, |
| 2014 | n, new UserHandle(mUid), null, 0); |
Julia Reynolds | 6ad0aec | 2017-07-05 08:47:03 -0400 | [diff] [blame] | 2015 | NotificationRecord otherPackage = |
| 2016 | new NotificationRecord(mContext, sbn, mTestNotificationChannel); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2017 | mService.addEnqueuedNotification(otherPackage); |
| 2018 | mService.addNotification(otherPackage); |
Julia Reynolds | 6ad0aec | 2017-07-05 08:47:03 -0400 | [diff] [blame] | 2019 | |
| 2020 | // Same notifications are enqueued as posted, everything counts b/c id and tag don't match |
Geoffrey Pitsch | 07532c3 | 2017-07-18 11:44:06 -0400 | [diff] [blame] | 2021 | int userId = new UserHandle(mUid).getIdentifier(); |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 2022 | assertEquals(40, |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2023 | mService.getNotificationCountLocked(PKG, userId, 0, null)); |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 2024 | assertEquals(40, |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2025 | mService.getNotificationCountLocked(PKG, userId, 0, "tag2")); |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 2026 | assertEquals(2, |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2027 | mService.getNotificationCountLocked("a", userId, 0, "banana")); |
Julia Reynolds | 6ad0aec | 2017-07-05 08:47:03 -0400 | [diff] [blame] | 2028 | |
| 2029 | // exclude a known notification - it's excluded from only the posted list, not enqueued |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 2030 | assertEquals(39, |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2031 | mService.getNotificationCountLocked(PKG, userId, 0, "tag")); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2032 | } |
| 2033 | |
| 2034 | @Test |
Julia Reynolds | 5171071 | 2017-07-19 13:48:07 -0400 | [diff] [blame] | 2035 | public void testAddAutogroup_requestsSort() throws Exception { |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2036 | RankingHandler rh = mock(RankingHandler.class); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2037 | mService.setRankingHandler(rh); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2038 | |
| 2039 | final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2040 | mService.addNotification(r); |
| 2041 | mService.addAutogroupKeyLocked(r.getKey()); |
Julia Reynolds | 5171071 | 2017-07-19 13:48:07 -0400 | [diff] [blame] | 2042 | |
| 2043 | verify(rh, times(1)).requestSort(); |
| 2044 | } |
| 2045 | |
| 2046 | @Test |
| 2047 | public void testRemoveAutogroup_requestsSort() throws Exception { |
| 2048 | RankingHandler rh = mock(RankingHandler.class); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2049 | mService.setRankingHandler(rh); |
Julia Reynolds | 5171071 | 2017-07-19 13:48:07 -0400 | [diff] [blame] | 2050 | |
| 2051 | final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
| 2052 | r.setOverrideGroupKey("TEST"); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2053 | mService.addNotification(r); |
| 2054 | mService.removeAutogroupKeyLocked(r.getKey()); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2055 | |
Julia Reynolds | 5171071 | 2017-07-19 13:48:07 -0400 | [diff] [blame] | 2056 | verify(rh, times(1)).requestSort(); |
| 2057 | } |
| 2058 | |
| 2059 | @Test |
| 2060 | public void testReaddAutogroup_noSort() throws Exception { |
| 2061 | RankingHandler rh = mock(RankingHandler.class); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2062 | mService.setRankingHandler(rh); |
Julia Reynolds | 5171071 | 2017-07-19 13:48:07 -0400 | [diff] [blame] | 2063 | |
| 2064 | final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
| 2065 | r.setOverrideGroupKey("TEST"); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2066 | mService.addNotification(r); |
| 2067 | mService.addAutogroupKeyLocked(r.getKey()); |
Julia Reynolds | 5171071 | 2017-07-19 13:48:07 -0400 | [diff] [blame] | 2068 | |
| 2069 | verify(rh, never()).requestSort(); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2070 | } |
| 2071 | |
| 2072 | @Test |
| 2073 | public void testHandleRankingSort_sendsUpdateOnSignalExtractorChange() throws Exception { |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2074 | mService.setRankingHelper(mRankingHelper); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2075 | NotificationManagerService.WorkerHandler handler = mock( |
| 2076 | NotificationManagerService.WorkerHandler.class); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2077 | mService.setHandler(handler); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2078 | |
| 2079 | Map<String, Answer> answers = getSignalExtractorSideEffects(); |
| 2080 | for (String message : answers.keySet()) { |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2081 | mService.clearNotifications(); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2082 | final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2083 | mService.addNotification(r); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2084 | |
| 2085 | doAnswer(answers.get(message)).when(mRankingHelper).extractSignals(r); |
| 2086 | |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2087 | mService.handleRankingSort(); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2088 | } |
| 2089 | verify(handler, times(answers.size())).scheduleSendRankingUpdate(); |
| 2090 | } |
| 2091 | |
| 2092 | @Test |
| 2093 | public void testHandleRankingSort_noUpdateWhenNoSignalChange() throws Exception { |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2094 | mService.setRankingHelper(mRankingHelper); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2095 | NotificationManagerService.WorkerHandler handler = mock( |
| 2096 | NotificationManagerService.WorkerHandler.class); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2097 | mService.setHandler(handler); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2098 | |
| 2099 | final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2100 | mService.addNotification(r); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2101 | |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2102 | mService.handleRankingSort(); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2103 | verify(handler, never()).scheduleSendRankingUpdate(); |
Julia Reynolds | 6ad0aec | 2017-07-05 08:47:03 -0400 | [diff] [blame] | 2104 | } |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2105 | |
| 2106 | @Test |
| 2107 | public void testReadPolicyXml_readApprovedServicesFromXml() throws Exception { |
| 2108 | final String preupgradeXml = "<notification-policy version=\"1\">" |
| 2109 | + "<zen></zen>" |
| 2110 | + "<ranking></ranking>" |
| 2111 | + "<enabled_listeners>" |
| 2112 | + "<service_listing approved=\"test\" user=\"0\" primary=\"true\" />" |
| 2113 | + "</enabled_listeners>" |
| 2114 | + "<enabled_assistants>" |
| 2115 | + "<service_listing approved=\"test\" user=\"0\" primary=\"true\" />" |
| 2116 | + "</enabled_assistants>" |
| 2117 | + "<dnd_apps>" |
| 2118 | + "<service_listing approved=\"test\" user=\"0\" primary=\"true\" />" |
| 2119 | + "</dnd_apps>" |
| 2120 | + "</notification-policy>"; |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2121 | mService.readPolicyXml( |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2122 | new BufferedInputStream(new ByteArrayInputStream(preupgradeXml.getBytes())), false); |
| 2123 | verify(mListeners, times(1)).readXml(any()); |
| 2124 | verify(mConditionProviders, times(1)).readXml(any()); |
| 2125 | verify(mAssistants, times(1)).readXml(any()); |
| 2126 | |
| 2127 | // numbers are inflated for setup |
| 2128 | verify(mListeners, times(1)).migrateToXml(); |
| 2129 | verify(mConditionProviders, times(1)).migrateToXml(); |
| 2130 | verify(mAssistants, times(1)).migrateToXml(); |
| 2131 | } |
| 2132 | |
| 2133 | @Test |
| 2134 | public void testReadPolicyXml_readApprovedServicesFromSettings() throws Exception { |
| 2135 | final String preupgradeXml = "<notification-policy version=\"1\">" |
| 2136 | + "<zen></zen>" |
| 2137 | + "<ranking></ranking>" |
| 2138 | + "</notification-policy>"; |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2139 | mService.readPolicyXml( |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2140 | new BufferedInputStream(new ByteArrayInputStream(preupgradeXml.getBytes())), false); |
| 2141 | verify(mListeners, never()).readXml(any()); |
| 2142 | verify(mConditionProviders, never()).readXml(any()); |
| 2143 | verify(mAssistants, never()).readXml(any()); |
| 2144 | |
| 2145 | // numbers are inflated for setup |
| 2146 | verify(mListeners, times(2)).migrateToXml(); |
| 2147 | verify(mConditionProviders, times(2)).migrateToXml(); |
| 2148 | verify(mAssistants, times(2)).migrateToXml(); |
| 2149 | } |
| 2150 | |
Beverly | d4f9649 | 2017-08-02 13:36:11 -0400 | [diff] [blame] | 2151 | |
| 2152 | @Test |
| 2153 | public void testLocaleChangedCallsUpdateDefaultZenModeRules() throws Exception { |
| 2154 | ZenModeHelper mZenModeHelper = mock(ZenModeHelper.class); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2155 | mService.mZenModeHelper = mZenModeHelper; |
| 2156 | mService.mLocaleChangeReceiver.onReceive(mContext, |
Beverly | d4f9649 | 2017-08-02 13:36:11 -0400 | [diff] [blame] | 2157 | new Intent(Intent.ACTION_LOCALE_CHANGED)); |
| 2158 | |
| 2159 | verify(mZenModeHelper, times(1)).updateDefaultZenRules(); |
| 2160 | } |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 2161 | |
| 2162 | @Test |
| 2163 | public void testBumpFGImportance_noChannelChangePreOApp() throws Exception { |
| 2164 | String preOPkg = "preO"; |
| 2165 | int preOUid = 145; |
| 2166 | final ApplicationInfo legacy = new ApplicationInfo(); |
| 2167 | legacy.targetSdkVersion = Build.VERSION_CODES.N_MR1; |
| 2168 | when(mPackageManagerClient.getApplicationInfoAsUser(eq(preOPkg), anyInt(), anyInt())) |
| 2169 | .thenReturn(legacy); |
| 2170 | when(mPackageManagerClient.getPackageUidAsUser(eq(preOPkg), anyInt())).thenReturn(preOUid); |
| 2171 | getContext().setMockPackageManager(mPackageManagerClient); |
| 2172 | |
| 2173 | Notification.Builder nb = new Notification.Builder(mContext, |
| 2174 | NotificationChannel.DEFAULT_CHANNEL_ID) |
| 2175 | .setContentTitle("foo") |
| 2176 | .setSmallIcon(android.R.drawable.sym_def_app_icon) |
| 2177 | .setFlag(Notification.FLAG_FOREGROUND_SERVICE, true) |
| 2178 | .setPriority(Notification.PRIORITY_MIN); |
| 2179 | |
| 2180 | StatusBarNotification sbn = new StatusBarNotification(preOPkg, preOPkg, 9, "tag", preOUid, |
| 2181 | 0, nb.build(), new UserHandle(preOUid), null, 0); |
| 2182 | |
| 2183 | mBinderService.enqueueNotificationWithTag(preOPkg, preOPkg, "tag", |
| 2184 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
| 2185 | waitForIdle(); |
| 2186 | assertEquals(IMPORTANCE_LOW, |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2187 | mService.getNotificationRecord(sbn.getKey()).getImportance()); |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 2188 | |
| 2189 | nb = new Notification.Builder(mContext) |
| 2190 | .setContentTitle("foo") |
| 2191 | .setSmallIcon(android.R.drawable.sym_def_app_icon) |
| 2192 | .setFlag(Notification.FLAG_FOREGROUND_SERVICE, true) |
| 2193 | .setPriority(Notification.PRIORITY_MIN); |
| 2194 | |
| 2195 | sbn = new StatusBarNotification(preOPkg, preOPkg, 9, "tag", preOUid, |
| 2196 | 0, nb.build(), new UserHandle(preOUid), null, 0); |
| 2197 | |
| 2198 | mBinderService.enqueueNotificationWithTag(preOPkg, preOPkg, "tag", |
| 2199 | sbn.getId(), sbn.getNotification(), sbn.getUserId()); |
| 2200 | waitForIdle(); |
| 2201 | assertEquals(IMPORTANCE_LOW, |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2202 | mService.getNotificationRecord(sbn.getKey()).getImportance()); |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 2203 | |
| 2204 | NotificationChannel defaultChannel = mBinderService.getNotificationChannel( |
| 2205 | preOPkg, NotificationChannel.DEFAULT_CHANNEL_ID); |
| 2206 | assertEquals(IMPORTANCE_UNSPECIFIED, defaultChannel.getImportance()); |
| 2207 | } |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2208 | |
| 2209 | @Test |
| 2210 | public void testStats_updatedOnDirectReply() throws Exception { |
| 2211 | final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
| 2212 | mService.addNotification(r); |
| 2213 | |
| 2214 | mService.mNotificationDelegate.onNotificationDirectReplied(r.getKey()); |
| 2215 | assertTrue(mService.getNotificationRecord(r.getKey()).getStats().hasDirectReplied()); |
| 2216 | } |
| 2217 | |
| 2218 | @Test |
Julia Reynolds | 84dc96b | 2017-11-14 09:51:01 -0500 | [diff] [blame] | 2219 | public void testStats_updatedOnUserExpansion() throws Exception { |
| 2220 | NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2221 | mService.addNotification(r); |
| 2222 | |
| 2223 | mService.mNotificationDelegate.onNotificationExpansionChanged(r.getKey(), true, true); |
| 2224 | assertTrue(mService.getNotificationRecord(r.getKey()).getStats().hasExpanded()); |
| 2225 | mService.mNotificationDelegate.onNotificationExpansionChanged(r.getKey(), true, false); |
| 2226 | assertTrue(mService.getNotificationRecord(r.getKey()).getStats().hasExpanded()); |
| 2227 | } |
| 2228 | |
| 2229 | @Test |
Julia Reynolds | 84dc96b | 2017-11-14 09:51:01 -0500 | [diff] [blame] | 2230 | public void testStats_notUpdatedOnAutoExpansion() throws Exception { |
| 2231 | NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
| 2232 | mService.addNotification(r); |
| 2233 | |
| 2234 | mService.mNotificationDelegate.onNotificationExpansionChanged(r.getKey(), false, true); |
| 2235 | assertFalse(mService.getNotificationRecord(r.getKey()).getStats().hasExpanded()); |
| 2236 | mService.mNotificationDelegate.onNotificationExpansionChanged(r.getKey(), false, false); |
| 2237 | assertFalse(mService.getNotificationRecord(r.getKey()).getStats().hasExpanded()); |
| 2238 | } |
| 2239 | |
| 2240 | @Test |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 2241 | public void testStats_updatedOnViewSettings() throws Exception { |
| 2242 | final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
| 2243 | mService.addNotification(r); |
| 2244 | |
| 2245 | mService.mNotificationDelegate.onNotificationSettingsViewed(r.getKey()); |
| 2246 | assertTrue(mService.getNotificationRecord(r.getKey()).getStats().hasViewedSettings()); |
| 2247 | } |
| 2248 | |
| 2249 | @Test |
| 2250 | public void testStats_updatedOnVisibilityChanged() throws Exception { |
| 2251 | final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
| 2252 | mService.addNotification(r); |
| 2253 | |
| 2254 | NotificationVisibility nv = NotificationVisibility.obtain(r.getKey(), 1, true); |
| 2255 | mService.mNotificationDelegate.onNotificationVisibilityChanged( |
| 2256 | new NotificationVisibility[] {nv}, new NotificationVisibility[]{}); |
| 2257 | assertTrue(mService.getNotificationRecord(r.getKey()).getStats().hasSeen()); |
| 2258 | mService.mNotificationDelegate.onNotificationVisibilityChanged( |
| 2259 | new NotificationVisibility[] {}, new NotificationVisibility[]{nv}); |
| 2260 | assertTrue(mService.getNotificationRecord(r.getKey()).getStats().hasSeen()); |
| 2261 | } |
| 2262 | |
| 2263 | @Test |
| 2264 | public void testStats_dismissalSurface() throws Exception { |
| 2265 | final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
| 2266 | mService.addNotification(r); |
| 2267 | |
| 2268 | mService.mNotificationDelegate.onNotificationClear(mUid, 0, PKG, r.sbn.getTag(), |
| 2269 | r.sbn.getId(), r.getUserId(), r.getKey(), NotificationStats.DISMISSAL_AOD); |
| 2270 | waitForIdle(); |
| 2271 | |
| 2272 | assertEquals(NotificationStats.DISMISSAL_AOD, r.getStats().getDismissalSurface()); |
| 2273 | } |
| 2274 | |
| 2275 | @Test |
| 2276 | public void testUserSentimentChangeTriggersUpdate() throws Exception { |
| 2277 | final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel); |
| 2278 | mService.addNotification(r); |
| 2279 | NotificationManagerService.WorkerHandler handler = mock( |
| 2280 | NotificationManagerService.WorkerHandler.class); |
| 2281 | mService.setHandler(handler); |
| 2282 | |
| 2283 | Bundle signals = new Bundle(); |
| 2284 | signals.putInt(Adjustment.KEY_USER_SENTIMENT, |
| 2285 | NotificationListenerService.Ranking.USER_SENTIMENT_NEGATIVE); |
| 2286 | Adjustment adjustment = new Adjustment( |
| 2287 | r.sbn.getPackageName(), r.getKey(), signals, "", r.getUser().getIdentifier()); |
| 2288 | mBinderService.applyAdjustmentFromAssistant(null, adjustment); |
| 2289 | |
| 2290 | waitForIdle(); |
| 2291 | |
| 2292 | verify(handler, timeout(300).times(1)).scheduleSendRankingUpdate(); |
| 2293 | } |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 2294 | } |