Julia Reynolds | a614c27 | 2019-11-15 16:43:29 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 | package com.android.server.notification; |
| 17 | |
Julia Reynolds | e43fac3 | 2019-11-20 13:36:24 -0500 | [diff] [blame] | 18 | import static android.os.UserHandle.MIN_SECONDARY_USER_ID; |
Julia Reynolds | a614c27 | 2019-11-15 16:43:29 -0500 | [diff] [blame] | 19 | import static android.os.UserHandle.USER_SYSTEM; |
| 20 | |
| 21 | import static com.google.common.truth.Truth.assertThat; |
| 22 | |
Julia Reynolds | e43fac3 | 2019-11-20 13:36:24 -0500 | [diff] [blame] | 23 | import static org.mockito.ArgumentMatchers.any; |
Julia Reynolds | a614c27 | 2019-11-15 16:43:29 -0500 | [diff] [blame] | 24 | import static org.mockito.Mockito.mock; |
| 25 | import static org.mockito.Mockito.never; |
| 26 | import static org.mockito.Mockito.times; |
| 27 | import static org.mockito.Mockito.verify; |
| 28 | import static org.mockito.Mockito.when; |
| 29 | |
| 30 | import android.app.NotificationHistory; |
| 31 | import android.app.NotificationHistory.HistoricalNotification; |
Julia Reynolds | e43fac3 | 2019-11-20 13:36:24 -0500 | [diff] [blame] | 32 | import android.content.pm.UserInfo; |
Julia Reynolds | a614c27 | 2019-11-15 16:43:29 -0500 | [diff] [blame] | 33 | import android.graphics.drawable.Icon; |
Julia Reynolds | e43fac3 | 2019-11-20 13:36:24 -0500 | [diff] [blame] | 34 | import android.os.Handler; |
Julia Reynolds | a614c27 | 2019-11-15 16:43:29 -0500 | [diff] [blame] | 35 | import android.os.UserManager; |
Julia Reynolds | e43fac3 | 2019-11-20 13:36:24 -0500 | [diff] [blame] | 36 | import android.provider.Settings; |
Julia Reynolds | a614c27 | 2019-11-15 16:43:29 -0500 | [diff] [blame] | 37 | |
| 38 | import androidx.test.InstrumentationRegistry; |
| 39 | import androidx.test.runner.AndroidJUnit4; |
| 40 | |
| 41 | import com.android.server.UiServiceTestCase; |
| 42 | |
| 43 | import org.junit.Before; |
Colin Cross | 5ea7fb7 | 2019-12-18 17:16:36 -0800 | [diff] [blame] | 44 | import org.junit.Ignore; |
Julia Reynolds | a614c27 | 2019-11-15 16:43:29 -0500 | [diff] [blame] | 45 | import org.junit.Test; |
| 46 | import org.junit.runner.RunWith; |
| 47 | import org.mockito.Mock; |
| 48 | import org.mockito.MockitoAnnotations; |
| 49 | |
| 50 | import java.util.ArrayList; |
Julia Reynolds | e43fac3 | 2019-11-20 13:36:24 -0500 | [diff] [blame] | 51 | import java.util.List; |
| 52 | |
Julia Reynolds | a614c27 | 2019-11-15 16:43:29 -0500 | [diff] [blame] | 53 | |
| 54 | @RunWith(AndroidJUnit4.class) |
| 55 | public class NotificationHistoryManagerTest extends UiServiceTestCase { |
| 56 | |
| 57 | @Mock |
Julia Reynolds | a614c27 | 2019-11-15 16:43:29 -0500 | [diff] [blame] | 58 | UserManager mUserManager; |
| 59 | @Mock |
| 60 | NotificationHistoryDatabase mDb; |
Julia Reynolds | e43fac3 | 2019-11-20 13:36:24 -0500 | [diff] [blame] | 61 | @Mock |
| 62 | Handler mHandler; |
| 63 | List<UserInfo> mUsers; |
Julia Reynolds | a614c27 | 2019-11-15 16:43:29 -0500 | [diff] [blame] | 64 | |
| 65 | NotificationHistoryManager mHistoryManager; |
| 66 | |
Julia Reynolds | a614c27 | 2019-11-15 16:43:29 -0500 | [diff] [blame] | 67 | private HistoricalNotification getHistoricalNotification(String packageName, int index) { |
| 68 | String expectedChannelName = "channelName" + index; |
| 69 | String expectedChannelId = "channelId" + index; |
| 70 | int expectedUid = 1123456 + index; |
| 71 | int expectedUserId = index; |
| 72 | long expectedPostTime = 987654321 + index; |
| 73 | String expectedTitle = "title" + index; |
| 74 | String expectedText = "text" + index; |
| 75 | Icon expectedIcon = Icon.createWithResource(InstrumentationRegistry.getContext(), |
| 76 | index); |
| 77 | |
| 78 | return new HistoricalNotification.Builder() |
| 79 | .setPackage(packageName) |
| 80 | .setChannelName(expectedChannelName) |
| 81 | .setChannelId(expectedChannelId) |
| 82 | .setUid(expectedUid) |
| 83 | .setUserId(expectedUserId) |
| 84 | .setPostedTimeMs(expectedPostTime) |
| 85 | .setTitle(expectedTitle) |
| 86 | .setText(expectedText) |
| 87 | .setIcon(expectedIcon) |
| 88 | .build(); |
| 89 | } |
| 90 | |
| 91 | @Before |
| 92 | public void setUp() { |
| 93 | MockitoAnnotations.initMocks(this); |
Julia Reynolds | e43fac3 | 2019-11-20 13:36:24 -0500 | [diff] [blame] | 94 | |
| 95 | getContext().addMockSystemService(UserManager.class, mUserManager); |
| 96 | |
| 97 | mUsers = new ArrayList<>(); |
| 98 | UserInfo userSystem = new UserInfo(); |
| 99 | userSystem.id = USER_SYSTEM; |
| 100 | mUsers.add(userSystem); |
| 101 | UserInfo userAll = new UserInfo(); |
| 102 | userAll.id = MIN_SECONDARY_USER_ID; |
| 103 | mUsers.add(userAll); |
| 104 | mUsers.add(userAll); |
| 105 | when(mUserManager.getUsers()).thenReturn(mUsers); |
| 106 | |
| 107 | for (UserInfo info : mUsers) { |
| 108 | Settings.Secure.putIntForUser(getContext().getContentResolver(), |
| 109 | Settings.Secure.NOTIFICATION_HISTORY_ENABLED, 1, info.id); |
| 110 | } |
Julia Reynolds | a614c27 | 2019-11-15 16:43:29 -0500 | [diff] [blame] | 111 | |
| 112 | NotificationHistoryDatabaseFactory.setTestingNotificationHistoryDatabase(mDb); |
| 113 | |
Julia Reynolds | e43fac3 | 2019-11-20 13:36:24 -0500 | [diff] [blame] | 114 | mHistoryManager = new NotificationHistoryManager(getContext(), mHandler); |
| 115 | mHistoryManager.onBootPhaseAppsCanStart(); |
Julia Reynolds | a614c27 | 2019-11-15 16:43:29 -0500 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | @Test |
| 119 | public void testOnUserUnlocked() { |
| 120 | assertThat(mHistoryManager.doesHistoryExistForUser(USER_SYSTEM)).isFalse(); |
| 121 | assertThat(mHistoryManager.isUserUnlocked(USER_SYSTEM)).isFalse(); |
| 122 | mHistoryManager.onUserUnlocked(USER_SYSTEM); |
| 123 | assertThat(mHistoryManager.doesHistoryExistForUser(USER_SYSTEM)).isTrue(); |
| 124 | assertThat(mHistoryManager.isUserUnlocked(USER_SYSTEM)).isTrue(); |
| 125 | verify(mDb, times(1)).init(); |
| 126 | } |
| 127 | |
Colin Cross | 5ea7fb7 | 2019-12-18 17:16:36 -0800 | [diff] [blame] | 128 | @Test |
| 129 | @Ignore("b/147012298") |
Julia Reynolds | e43fac3 | 2019-11-20 13:36:24 -0500 | [diff] [blame] | 130 | public void testOnUserUnlocked_historyDisabled() { |
| 131 | Settings.Secure.putIntForUser(getContext().getContentResolver(), |
| 132 | Settings.Secure.NOTIFICATION_HISTORY_ENABLED, 0, USER_SYSTEM); |
| 133 | mHistoryManager.mSettingsObserver.update(null, USER_SYSTEM); |
| 134 | assertThat(mHistoryManager.doesHistoryExistForUser(USER_SYSTEM)).isFalse(); |
| 135 | assertThat(mHistoryManager.isUserUnlocked(USER_SYSTEM)).isFalse(); |
| 136 | |
| 137 | mHistoryManager.onUserUnlocked(USER_SYSTEM); |
| 138 | |
| 139 | assertThat(mHistoryManager.doesHistoryExistForUser(USER_SYSTEM)).isFalse(); |
| 140 | assertThat(mHistoryManager.isUserUnlocked(USER_SYSTEM)).isFalse(); |
| 141 | verify(mDb, times(1)).disableHistory(); |
| 142 | } |
| 143 | |
Julia Reynolds | a614c27 | 2019-11-15 16:43:29 -0500 | [diff] [blame] | 144 | @Test |
| 145 | public void testOnUserUnlocked_cleansUpRemovedPackages() { |
| 146 | String pkg = "pkg"; |
| 147 | mHistoryManager.onPackageRemoved(USER_SYSTEM, pkg); |
| 148 | assertThat(mHistoryManager.doesHistoryExistForUser(USER_SYSTEM)).isFalse(); |
| 149 | |
| 150 | mHistoryManager.onUserUnlocked(USER_SYSTEM); |
| 151 | assertThat(mHistoryManager.doesHistoryExistForUser(USER_SYSTEM)).isTrue(); |
| 152 | assertThat(mHistoryManager.isUserUnlocked(USER_SYSTEM)).isTrue(); |
| 153 | |
| 154 | verify(mDb, times(1)).onPackageRemoved(pkg); |
| 155 | } |
| 156 | |
| 157 | @Test |
| 158 | public void testOnUserStopped_userExists() { |
| 159 | mHistoryManager.onUserUnlocked(USER_SYSTEM); |
| 160 | mHistoryManager.onUserStopped(USER_SYSTEM); |
| 161 | |
| 162 | assertThat(mHistoryManager.doesHistoryExistForUser(USER_SYSTEM)).isFalse(); |
| 163 | assertThat(mHistoryManager.isUserUnlocked(USER_SYSTEM)).isFalse(); |
| 164 | } |
| 165 | |
| 166 | @Test |
| 167 | public void testOnUserStopped_userDoesNotExist() { |
| 168 | mHistoryManager.onUserStopped(USER_SYSTEM); |
| 169 | // no crash |
| 170 | assertThat(mHistoryManager.doesHistoryExistForUser(USER_SYSTEM)).isFalse(); |
| 171 | assertThat(mHistoryManager.isUserUnlocked(USER_SYSTEM)).isFalse(); |
| 172 | } |
| 173 | |
| 174 | @Test |
| 175 | public void testOnUserRemoved_userExists() { |
| 176 | mHistoryManager.onUserUnlocked(USER_SYSTEM); |
| 177 | mHistoryManager.onUserRemoved(USER_SYSTEM); |
| 178 | |
| 179 | assertThat(mHistoryManager.doesHistoryExistForUser(USER_SYSTEM)).isFalse(); |
| 180 | assertThat(mHistoryManager.isUserUnlocked(USER_SYSTEM)).isFalse(); |
Julia Reynolds | e43fac3 | 2019-11-20 13:36:24 -0500 | [diff] [blame] | 181 | assertThat(mHistoryManager.isHistoryEnabled(USER_SYSTEM)).isFalse(); |
Julia Reynolds | a614c27 | 2019-11-15 16:43:29 -0500 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | @Test |
| 185 | public void testOnUserRemoved_userDoesNotExist() { |
| 186 | mHistoryManager.onUserRemoved(USER_SYSTEM); |
| 187 | // no crash |
| 188 | assertThat(mHistoryManager.doesHistoryExistForUser(USER_SYSTEM)).isFalse(); |
| 189 | assertThat(mHistoryManager.isUserUnlocked(USER_SYSTEM)).isFalse(); |
| 190 | } |
| 191 | |
| 192 | @Test |
| 193 | public void testOnUserRemoved_cleanupPendingPackages() { |
| 194 | mHistoryManager.onUserUnlocked(USER_SYSTEM); |
| 195 | mHistoryManager.onUserStopped(USER_SYSTEM); |
| 196 | String pkg = "pkg"; |
| 197 | mHistoryManager.onPackageRemoved(USER_SYSTEM, pkg); |
| 198 | mHistoryManager.onUserRemoved(USER_SYSTEM); |
| 199 | |
| 200 | assertThat(mHistoryManager.getPendingPackageRemovalsForUser(USER_SYSTEM)).isNull(); |
| 201 | } |
| 202 | |
| 203 | @Test |
| 204 | public void testOnPackageRemoved_userUnlocked() { |
| 205 | String pkg = "pkg"; |
| 206 | NotificationHistoryDatabase userHistory = mock(NotificationHistoryDatabase.class); |
| 207 | |
| 208 | mHistoryManager.onUserUnlocked(USER_SYSTEM); |
| 209 | mHistoryManager.replaceNotificationHistoryDatabase(USER_SYSTEM, userHistory); |
| 210 | |
| 211 | mHistoryManager.onPackageRemoved(USER_SYSTEM, pkg); |
| 212 | |
| 213 | verify(userHistory, times(1)).onPackageRemoved(pkg); |
| 214 | } |
| 215 | |
| 216 | @Test |
| 217 | public void testOnPackageRemoved_userLocked() { |
| 218 | String pkg = "pkg"; |
| 219 | mHistoryManager.onPackageRemoved(USER_SYSTEM, pkg); |
| 220 | |
| 221 | assertThat(mHistoryManager.getPendingPackageRemovalsForUser(USER_SYSTEM)).contains(pkg); |
| 222 | } |
| 223 | |
| 224 | @Test |
Julia Reynolds | e43fac3 | 2019-11-20 13:36:24 -0500 | [diff] [blame] | 225 | public void testOnPackageRemoved_historyDisabled() { |
| 226 | Settings.Secure.putIntForUser(getContext().getContentResolver(), |
| 227 | Settings.Secure.NOTIFICATION_HISTORY_ENABLED, 0, USER_SYSTEM); |
| 228 | mHistoryManager.mSettingsObserver.update(null, USER_SYSTEM); |
| 229 | String pkg = "pkg"; |
| 230 | mHistoryManager.onPackageRemoved(USER_SYSTEM, pkg); |
| 231 | |
| 232 | assertThat(mHistoryManager.getPendingPackageRemovalsForUser(USER_SYSTEM)) |
| 233 | .isNull(); |
| 234 | } |
| 235 | |
| 236 | @Test |
Julia Reynolds | a614c27 | 2019-11-15 16:43:29 -0500 | [diff] [blame] | 237 | public void testOnPackageRemoved_multiUser() { |
| 238 | String pkg = "pkg"; |
| 239 | NotificationHistoryDatabase userHistorySystem = mock(NotificationHistoryDatabase.class); |
| 240 | NotificationHistoryDatabase userHistoryAll = mock(NotificationHistoryDatabase.class); |
| 241 | |
| 242 | mHistoryManager.onUserUnlocked(USER_SYSTEM); |
| 243 | mHistoryManager.replaceNotificationHistoryDatabase(USER_SYSTEM, userHistorySystem); |
| 244 | |
Julia Reynolds | e43fac3 | 2019-11-20 13:36:24 -0500 | [diff] [blame] | 245 | mHistoryManager.onUserUnlocked(MIN_SECONDARY_USER_ID); |
| 246 | mHistoryManager.replaceNotificationHistoryDatabase(MIN_SECONDARY_USER_ID, userHistoryAll); |
Julia Reynolds | a614c27 | 2019-11-15 16:43:29 -0500 | [diff] [blame] | 247 | |
| 248 | mHistoryManager.onPackageRemoved(USER_SYSTEM, pkg); |
| 249 | |
| 250 | verify(userHistorySystem, times(1)).onPackageRemoved(pkg); |
| 251 | verify(userHistoryAll, never()).onPackageRemoved(pkg); |
| 252 | } |
| 253 | |
| 254 | @Test |
| 255 | public void testTriggerWriteToDisk() { |
| 256 | NotificationHistoryDatabase userHistorySystem = mock(NotificationHistoryDatabase.class); |
| 257 | NotificationHistoryDatabase userHistoryAll = mock(NotificationHistoryDatabase.class); |
| 258 | |
| 259 | mHistoryManager.onUserUnlocked(USER_SYSTEM); |
| 260 | mHistoryManager.replaceNotificationHistoryDatabase(USER_SYSTEM, userHistorySystem); |
| 261 | |
Julia Reynolds | e43fac3 | 2019-11-20 13:36:24 -0500 | [diff] [blame] | 262 | mHistoryManager.onUserUnlocked(MIN_SECONDARY_USER_ID); |
| 263 | mHistoryManager.replaceNotificationHistoryDatabase(MIN_SECONDARY_USER_ID, userHistoryAll); |
Julia Reynolds | a614c27 | 2019-11-15 16:43:29 -0500 | [diff] [blame] | 264 | |
| 265 | mHistoryManager.triggerWriteToDisk(); |
| 266 | |
| 267 | verify(userHistorySystem, times(1)).forceWriteToDisk(); |
| 268 | verify(userHistoryAll, times(1)).forceWriteToDisk(); |
| 269 | } |
| 270 | |
| 271 | @Test |
| 272 | public void testTriggerWriteToDisk_onlyUnlockedUsers() { |
| 273 | NotificationHistoryDatabase userHistorySystem = mock(NotificationHistoryDatabase.class); |
| 274 | NotificationHistoryDatabase userHistoryAll = mock(NotificationHistoryDatabase.class); |
| 275 | |
| 276 | mHistoryManager.onUserUnlocked(USER_SYSTEM); |
| 277 | mHistoryManager.replaceNotificationHistoryDatabase(USER_SYSTEM, userHistorySystem); |
| 278 | |
Julia Reynolds | e43fac3 | 2019-11-20 13:36:24 -0500 | [diff] [blame] | 279 | mHistoryManager.onUserUnlocked(MIN_SECONDARY_USER_ID); |
| 280 | mHistoryManager.replaceNotificationHistoryDatabase(MIN_SECONDARY_USER_ID, userHistoryAll); |
| 281 | mHistoryManager.onUserStopped(MIN_SECONDARY_USER_ID); |
Julia Reynolds | a614c27 | 2019-11-15 16:43:29 -0500 | [diff] [blame] | 282 | |
| 283 | mHistoryManager.triggerWriteToDisk(); |
| 284 | |
| 285 | verify(userHistorySystem, times(1)).forceWriteToDisk(); |
| 286 | verify(userHistoryAll, never()).forceWriteToDisk(); |
| 287 | } |
| 288 | |
| 289 | @Test |
Julia Reynolds | e43fac3 | 2019-11-20 13:36:24 -0500 | [diff] [blame] | 290 | public void testTriggerWriteToDisk_historyDisabled() { |
| 291 | Settings.Secure.putIntForUser(getContext().getContentResolver(), |
| 292 | Settings.Secure.NOTIFICATION_HISTORY_ENABLED, 0, USER_SYSTEM); |
| 293 | mHistoryManager.mSettingsObserver.update(null, USER_SYSTEM); |
| 294 | NotificationHistoryDatabase userHistorySystem = mock(NotificationHistoryDatabase.class); |
| 295 | |
| 296 | mHistoryManager.onUserUnlocked(USER_SYSTEM); |
| 297 | mHistoryManager.replaceNotificationHistoryDatabase(USER_SYSTEM, userHistorySystem); |
| 298 | |
| 299 | mHistoryManager.triggerWriteToDisk(); |
| 300 | |
| 301 | verify(userHistorySystem, never()).forceWriteToDisk(); |
| 302 | } |
| 303 | |
| 304 | @Test |
Julia Reynolds | a614c27 | 2019-11-15 16:43:29 -0500 | [diff] [blame] | 305 | public void testAddNotification_userLocked_noCrash() { |
| 306 | HistoricalNotification hn = getHistoricalNotification("pkg", 1); |
| 307 | |
| 308 | mHistoryManager.addNotification(hn); |
| 309 | } |
| 310 | |
| 311 | @Test |
Julia Reynolds | e43fac3 | 2019-11-20 13:36:24 -0500 | [diff] [blame] | 312 | public void testAddNotification_historyDisabled() { |
| 313 | HistoricalNotification hn = getHistoricalNotification("pkg", 1); |
| 314 | |
| 315 | Settings.Secure.putIntForUser(getContext().getContentResolver(), |
| 316 | Settings.Secure.NOTIFICATION_HISTORY_ENABLED, 0, hn.getUserId()); |
| 317 | mHistoryManager.mSettingsObserver.update(null, USER_SYSTEM); |
| 318 | |
| 319 | mHistoryManager.onUserUnlocked(hn.getUserId()); |
| 320 | mHistoryManager.addNotification(hn); |
| 321 | |
| 322 | verify(mDb, never()).addNotification(any()); |
| 323 | } |
| 324 | |
| 325 | @Test |
Julia Reynolds | a614c27 | 2019-11-15 16:43:29 -0500 | [diff] [blame] | 326 | public void testAddNotification() { |
| 327 | HistoricalNotification hnSystem = getHistoricalNotification("pkg", USER_SYSTEM); |
Julia Reynolds | e43fac3 | 2019-11-20 13:36:24 -0500 | [diff] [blame] | 328 | HistoricalNotification hnAll = getHistoricalNotification("pkg", MIN_SECONDARY_USER_ID); |
Julia Reynolds | a614c27 | 2019-11-15 16:43:29 -0500 | [diff] [blame] | 329 | |
| 330 | NotificationHistoryDatabase userHistorySystem = mock(NotificationHistoryDatabase.class); |
| 331 | NotificationHistoryDatabase userHistoryAll = mock(NotificationHistoryDatabase.class); |
| 332 | |
| 333 | mHistoryManager.onUserUnlocked(USER_SYSTEM); |
| 334 | mHistoryManager.replaceNotificationHistoryDatabase(USER_SYSTEM, userHistorySystem); |
| 335 | |
Julia Reynolds | e43fac3 | 2019-11-20 13:36:24 -0500 | [diff] [blame] | 336 | mHistoryManager.onUserUnlocked(MIN_SECONDARY_USER_ID); |
| 337 | mHistoryManager.replaceNotificationHistoryDatabase(MIN_SECONDARY_USER_ID, userHistoryAll); |
Julia Reynolds | a614c27 | 2019-11-15 16:43:29 -0500 | [diff] [blame] | 338 | |
| 339 | mHistoryManager.addNotification(hnSystem); |
| 340 | mHistoryManager.addNotification(hnAll); |
| 341 | |
| 342 | verify(userHistorySystem, times(1)).addNotification(hnSystem); |
| 343 | verify(userHistoryAll, times(1)).addNotification(hnAll); |
| 344 | } |
| 345 | |
| 346 | @Test |
| 347 | public void testReadNotificationHistory() { |
| 348 | HistoricalNotification hnSystem = getHistoricalNotification("pkg", USER_SYSTEM); |
Julia Reynolds | e43fac3 | 2019-11-20 13:36:24 -0500 | [diff] [blame] | 349 | HistoricalNotification hnAll = getHistoricalNotification("pkg", MIN_SECONDARY_USER_ID); |
Julia Reynolds | a614c27 | 2019-11-15 16:43:29 -0500 | [diff] [blame] | 350 | |
| 351 | NotificationHistoryDatabase userHistorySystem = mock(NotificationHistoryDatabase.class); |
| 352 | NotificationHistoryDatabase userHistoryAll = mock(NotificationHistoryDatabase.class); |
| 353 | |
| 354 | mHistoryManager.onUserUnlocked(USER_SYSTEM); |
| 355 | mHistoryManager.replaceNotificationHistoryDatabase(USER_SYSTEM, userHistorySystem); |
| 356 | NotificationHistory nhSystem = mock(NotificationHistory.class); |
| 357 | ArrayList<HistoricalNotification> nhSystemList = new ArrayList<>(); |
| 358 | nhSystemList.add(hnSystem); |
| 359 | when(nhSystem.getNotificationsToWrite()).thenReturn(nhSystemList); |
| 360 | when(userHistorySystem.readNotificationHistory()).thenReturn(nhSystem); |
| 361 | |
Julia Reynolds | e43fac3 | 2019-11-20 13:36:24 -0500 | [diff] [blame] | 362 | mHistoryManager.onUserUnlocked(MIN_SECONDARY_USER_ID); |
| 363 | mHistoryManager.replaceNotificationHistoryDatabase(MIN_SECONDARY_USER_ID, userHistoryAll); |
Julia Reynolds | a614c27 | 2019-11-15 16:43:29 -0500 | [diff] [blame] | 364 | NotificationHistory nhAll = mock(NotificationHistory.class); |
| 365 | ArrayList<HistoricalNotification> nhAllList = new ArrayList<>(); |
| 366 | nhAllList.add(hnAll); |
| 367 | when(nhAll.getNotificationsToWrite()).thenReturn(nhAllList); |
| 368 | when(userHistoryAll.readNotificationHistory()).thenReturn(nhAll); |
| 369 | |
| 370 | // ensure read history returns both historical notifs |
| 371 | NotificationHistory nh = mHistoryManager.readNotificationHistory( |
Julia Reynolds | e43fac3 | 2019-11-20 13:36:24 -0500 | [diff] [blame] | 372 | new int[] {USER_SYSTEM, MIN_SECONDARY_USER_ID}); |
Julia Reynolds | a614c27 | 2019-11-15 16:43:29 -0500 | [diff] [blame] | 373 | assertThat(nh.getNotificationsToWrite()).contains(hnSystem); |
| 374 | assertThat(nh.getNotificationsToWrite()).contains(hnAll); |
| 375 | } |
| 376 | |
| 377 | @Test |
Julia Reynolds | e43fac3 | 2019-11-20 13:36:24 -0500 | [diff] [blame] | 378 | public void testReadNotificationHistory_historyDisabled() { |
| 379 | HistoricalNotification hnSystem = getHistoricalNotification("pkg", USER_SYSTEM); |
| 380 | |
| 381 | mHistoryManager.onUserUnlocked(USER_SYSTEM); |
| 382 | NotificationHistory nhSystem = mock(NotificationHistory.class); |
| 383 | ArrayList<HistoricalNotification> nhSystemList = new ArrayList<>(); |
| 384 | nhSystemList.add(hnSystem); |
| 385 | when(nhSystem.getNotificationsToWrite()).thenReturn(nhSystemList); |
| 386 | when(mDb.readNotificationHistory()).thenReturn(nhSystem); |
| 387 | |
| 388 | mHistoryManager.onUserUnlocked(USER_SYSTEM); |
| 389 | |
| 390 | Settings.Secure.putIntForUser(getContext().getContentResolver(), |
| 391 | Settings.Secure.NOTIFICATION_HISTORY_ENABLED, 0, USER_SYSTEM); |
| 392 | mHistoryManager.mSettingsObserver.update(null, USER_SYSTEM); |
| 393 | |
| 394 | NotificationHistory nh = |
| 395 | mHistoryManager.readNotificationHistory(new int[] {USER_SYSTEM,}); |
| 396 | assertThat(nh.getNotificationsToWrite()).isEmpty(); |
| 397 | } |
| 398 | |
| 399 | @Test |
| 400 | public void testReadFilteredNotificationHistory_userLocked() { |
| 401 | NotificationHistory nh = |
| 402 | mHistoryManager.readFilteredNotificationHistory(USER_SYSTEM, "", "", 1000); |
| 403 | assertThat(nh.getNotificationsToWrite()).isEmpty(); |
| 404 | } |
| 405 | |
| 406 | @Test |
| 407 | public void testReadFilteredNotificationHistory_historyDisabled() { |
| 408 | Settings.Secure.putIntForUser(getContext().getContentResolver(), |
| 409 | Settings.Secure.NOTIFICATION_HISTORY_ENABLED, 0, USER_SYSTEM); |
| 410 | mHistoryManager.mSettingsObserver.update(null, USER_SYSTEM); |
| 411 | |
| 412 | mHistoryManager.onUserUnlocked(USER_SYSTEM); |
Julia Reynolds | a614c27 | 2019-11-15 16:43:29 -0500 | [diff] [blame] | 413 | NotificationHistory nh = |
| 414 | mHistoryManager.readFilteredNotificationHistory(USER_SYSTEM, "", "", 1000); |
| 415 | assertThat(nh.getNotificationsToWrite()).isEmpty(); |
| 416 | } |
| 417 | |
| 418 | @Test |
| 419 | public void readFilteredNotificationHistory() { |
| 420 | mHistoryManager.onUserUnlocked(USER_SYSTEM); |
| 421 | |
| 422 | mHistoryManager.readFilteredNotificationHistory(USER_SYSTEM, "pkg", "chn", 1000); |
| 423 | verify(mDb, times(1)).readNotificationHistory("pkg", "chn", 1000); |
| 424 | } |
Julia Reynolds | e43fac3 | 2019-11-20 13:36:24 -0500 | [diff] [blame] | 425 | |
| 426 | @Test |
| 427 | public void testIsHistoryEnabled() { |
| 428 | assertThat(mHistoryManager.isHistoryEnabled(USER_SYSTEM)).isTrue(); |
| 429 | |
| 430 | Settings.Secure.putIntForUser(getContext().getContentResolver(), |
| 431 | Settings.Secure.NOTIFICATION_HISTORY_ENABLED, 0, USER_SYSTEM); |
| 432 | mHistoryManager.mSettingsObserver.update(null, USER_SYSTEM); |
| 433 | |
| 434 | assertThat(mHistoryManager.isHistoryEnabled(USER_SYSTEM)).isFalse(); |
| 435 | } |
Julia Reynolds | a614c27 | 2019-11-15 16:43:29 -0500 | [diff] [blame] | 436 | } |