Danning Chen | a1bf86d | 2020-01-14 16:09:24 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 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.people.data; |
| 18 | |
Danning Chen | 44725cd | 2020-02-06 12:40:22 -0800 | [diff] [blame] | 19 | import static android.service.notification.NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_ADDED; |
| 20 | import static android.service.notification.NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_DELETED; |
| 21 | import static android.service.notification.NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_UPDATED; |
| 22 | |
Danning Chen | e84ee5f | 2020-01-29 15:11:03 -0800 | [diff] [blame] | 23 | import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation; |
| 24 | |
Danning Chen | a1bf86d | 2020-01-14 16:09:24 -0800 | [diff] [blame] | 25 | import static org.junit.Assert.assertEquals; |
| 26 | import static org.junit.Assert.assertFalse; |
Danning Chen | 44725cd | 2020-02-06 12:40:22 -0800 | [diff] [blame] | 27 | import static org.junit.Assert.assertNull; |
Danning Chen | a1bf86d | 2020-01-14 16:09:24 -0800 | [diff] [blame] | 28 | import static org.junit.Assert.assertTrue; |
| 29 | import static org.mockito.ArgumentMatchers.any; |
Danning Chen | a1bf86d | 2020-01-14 16:09:24 -0800 | [diff] [blame] | 30 | import static org.mockito.ArgumentMatchers.anyInt; |
| 31 | import static org.mockito.ArgumentMatchers.anyLong; |
| 32 | import static org.mockito.ArgumentMatchers.anyString; |
| 33 | import static org.mockito.Mockito.eq; |
| 34 | import static org.mockito.Mockito.mock; |
| 35 | import static org.mockito.Mockito.verify; |
| 36 | import static org.mockito.Mockito.when; |
| 37 | |
| 38 | import android.annotation.NonNull; |
| 39 | import android.annotation.Nullable; |
| 40 | import android.app.Notification; |
Danning Chen | 44725cd | 2020-02-06 12:40:22 -0800 | [diff] [blame] | 41 | import android.app.NotificationChannel; |
| 42 | import android.app.NotificationManager; |
Danning Chen | a1bf86d | 2020-01-14 16:09:24 -0800 | [diff] [blame] | 43 | import android.app.Person; |
| 44 | import android.app.prediction.AppTarget; |
| 45 | import android.app.prediction.AppTargetEvent; |
| 46 | import android.app.prediction.AppTargetId; |
Danning Chen | a1bf86d | 2020-01-14 16:09:24 -0800 | [diff] [blame] | 47 | import android.app.usage.UsageStatsManagerInternal; |
Danning Chen | a1bf86d | 2020-01-14 16:09:24 -0800 | [diff] [blame] | 48 | import android.content.ContentResolver; |
| 49 | import android.content.Context; |
| 50 | import android.content.Intent; |
| 51 | import android.content.IntentFilter; |
| 52 | import android.content.pm.ShortcutInfo; |
| 53 | import android.content.pm.ShortcutManager; |
Danning Chen | a1bf86d | 2020-01-14 16:09:24 -0800 | [diff] [blame] | 54 | import android.content.pm.ShortcutServiceInternal; |
| 55 | import android.content.pm.UserInfo; |
| 56 | import android.database.ContentObserver; |
| 57 | import android.net.Uri; |
| 58 | import android.os.Looper; |
| 59 | import android.os.UserHandle; |
| 60 | import android.os.UserManager; |
| 61 | import android.provider.ContactsContract; |
| 62 | import android.service.notification.NotificationListenerService; |
| 63 | import android.service.notification.StatusBarNotification; |
Danning Chen | e84ee5f | 2020-01-29 15:11:03 -0800 | [diff] [blame] | 64 | import android.telecom.TelecomManager; |
Danning Chen | a1bf86d | 2020-01-14 16:09:24 -0800 | [diff] [blame] | 65 | import android.telephony.TelephonyManager; |
| 66 | import android.util.Range; |
| 67 | |
| 68 | import com.android.internal.app.ChooserActivity; |
| 69 | import com.android.server.LocalServices; |
| 70 | |
| 71 | import org.junit.After; |
| 72 | import org.junit.Before; |
| 73 | import org.junit.Test; |
| 74 | import org.junit.runner.RunWith; |
| 75 | import org.junit.runners.JUnit4; |
| 76 | import org.mockito.Mock; |
| 77 | import org.mockito.MockitoAnnotations; |
| 78 | |
| 79 | import java.util.ArrayList; |
| 80 | import java.util.Arrays; |
| 81 | import java.util.Collections; |
| 82 | import java.util.List; |
| 83 | import java.util.concurrent.ScheduledExecutorService; |
| 84 | import java.util.concurrent.ScheduledFuture; |
| 85 | import java.util.concurrent.TimeUnit; |
Danning Chen | e84ee5f | 2020-01-29 15:11:03 -0800 | [diff] [blame] | 86 | import java.util.function.BiConsumer; |
Danning Chen | a1bf86d | 2020-01-14 16:09:24 -0800 | [diff] [blame] | 87 | |
| 88 | @RunWith(JUnit4.class) |
| 89 | public final class DataManagerTest { |
| 90 | |
| 91 | private static final int USER_ID_PRIMARY = 0; |
| 92 | private static final int USER_ID_PRIMARY_MANAGED = 10; |
| 93 | private static final int USER_ID_SECONDARY = 11; |
| 94 | private static final String TEST_PKG_NAME = "pkg"; |
| 95 | private static final String TEST_SHORTCUT_ID = "sc"; |
| 96 | private static final String CONTACT_URI = "content://com.android.contacts/contacts/lookup/123"; |
| 97 | private static final String PHONE_NUMBER = "+1234567890"; |
Danning Chen | 44725cd | 2020-02-06 12:40:22 -0800 | [diff] [blame] | 98 | private static final String NOTIFICATION_CHANNEL_ID = "test : sc"; |
Danning Chen | e84ee5f | 2020-01-29 15:11:03 -0800 | [diff] [blame] | 99 | private static final long MILLIS_PER_MINUTE = 1000L * 60L; |
Danning Chen | a1bf86d | 2020-01-14 16:09:24 -0800 | [diff] [blame] | 100 | |
| 101 | @Mock private Context mContext; |
| 102 | @Mock private ShortcutServiceInternal mShortcutServiceInternal; |
| 103 | @Mock private UsageStatsManagerInternal mUsageStatsManagerInternal; |
| 104 | @Mock private ShortcutManager mShortcutManager; |
| 105 | @Mock private UserManager mUserManager; |
| 106 | @Mock private TelephonyManager mTelephonyManager; |
Danning Chen | e84ee5f | 2020-01-29 15:11:03 -0800 | [diff] [blame] | 107 | @Mock private TelecomManager mTelecomManager; |
Danning Chen | a1bf86d | 2020-01-14 16:09:24 -0800 | [diff] [blame] | 108 | @Mock private ContentResolver mContentResolver; |
| 109 | @Mock private ScheduledExecutorService mExecutorService; |
| 110 | @Mock private ScheduledFuture mScheduledFuture; |
| 111 | @Mock private StatusBarNotification mStatusBarNotification; |
| 112 | @Mock private Notification mNotification; |
| 113 | |
Danning Chen | 44725cd | 2020-02-06 12:40:22 -0800 | [diff] [blame] | 114 | private NotificationChannel mNotificationChannel; |
Danning Chen | a1bf86d | 2020-01-14 16:09:24 -0800 | [diff] [blame] | 115 | private DataManager mDataManager; |
| 116 | private int mCallingUserId; |
| 117 | private TestInjector mInjector; |
| 118 | |
| 119 | @Before |
| 120 | public void setUp() { |
| 121 | MockitoAnnotations.initMocks(this); |
| 122 | |
| 123 | addLocalServiceMock(ShortcutServiceInternal.class, mShortcutServiceInternal); |
| 124 | |
| 125 | addLocalServiceMock(UsageStatsManagerInternal.class, mUsageStatsManagerInternal); |
| 126 | |
| 127 | when(mContext.getMainLooper()).thenReturn(Looper.getMainLooper()); |
| 128 | |
Danning Chen | e84ee5f | 2020-01-29 15:11:03 -0800 | [diff] [blame] | 129 | Context originalContext = getInstrumentation().getTargetContext(); |
| 130 | when(mContext.getApplicationInfo()).thenReturn(originalContext.getApplicationInfo()); |
| 131 | |
Danning Chen | a1bf86d | 2020-01-14 16:09:24 -0800 | [diff] [blame] | 132 | when(mContext.getSystemService(Context.SHORTCUT_SERVICE)).thenReturn(mShortcutManager); |
| 133 | when(mContext.getSystemServiceName(ShortcutManager.class)).thenReturn( |
| 134 | Context.SHORTCUT_SERVICE); |
| 135 | |
| 136 | when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager); |
| 137 | when(mContext.getSystemServiceName(UserManager.class)).thenReturn( |
| 138 | Context.USER_SERVICE); |
| 139 | |
| 140 | when(mContext.getSystemService(Context.TELEPHONY_SERVICE)).thenReturn(mTelephonyManager); |
| 141 | |
Danning Chen | e84ee5f | 2020-01-29 15:11:03 -0800 | [diff] [blame] | 142 | when(mContext.getSystemService(Context.TELECOM_SERVICE)).thenReturn(mTelecomManager); |
| 143 | when(mContext.getSystemServiceName(TelecomManager.class)).thenReturn( |
| 144 | Context.TELECOM_SERVICE); |
| 145 | when(mTelecomManager.getDefaultDialerPackage(anyInt())).thenReturn(TEST_PKG_NAME); |
| 146 | |
Danning Chen | a1bf86d | 2020-01-14 16:09:24 -0800 | [diff] [blame] | 147 | when(mExecutorService.scheduleAtFixedRate(any(Runnable.class), anyLong(), anyLong(), any( |
| 148 | TimeUnit.class))).thenReturn(mScheduledFuture); |
| 149 | |
| 150 | when(mUserManager.getEnabledProfiles(USER_ID_PRIMARY)) |
| 151 | .thenReturn(Arrays.asList( |
| 152 | buildUserInfo(USER_ID_PRIMARY), |
| 153 | buildUserInfo(USER_ID_PRIMARY_MANAGED))); |
| 154 | when(mUserManager.getEnabledProfiles(USER_ID_SECONDARY)) |
| 155 | .thenReturn(Collections.singletonList(buildUserInfo(USER_ID_SECONDARY))); |
| 156 | |
| 157 | when(mContext.getContentResolver()).thenReturn(mContentResolver); |
| 158 | |
| 159 | when(mStatusBarNotification.getNotification()).thenReturn(mNotification); |
| 160 | when(mStatusBarNotification.getPackageName()).thenReturn(TEST_PKG_NAME); |
| 161 | when(mStatusBarNotification.getUser()).thenReturn(UserHandle.of(USER_ID_PRIMARY)); |
| 162 | when(mNotification.getShortcutId()).thenReturn(TEST_SHORTCUT_ID); |
| 163 | |
Danning Chen | 44725cd | 2020-02-06 12:40:22 -0800 | [diff] [blame] | 164 | mNotificationChannel = new NotificationChannel( |
| 165 | NOTIFICATION_CHANNEL_ID, "test channel", NotificationManager.IMPORTANCE_DEFAULT); |
| 166 | mNotificationChannel.setConversationId("test", TEST_SHORTCUT_ID); |
| 167 | |
Danning Chen | a1bf86d | 2020-01-14 16:09:24 -0800 | [diff] [blame] | 168 | mCallingUserId = USER_ID_PRIMARY; |
| 169 | |
| 170 | mInjector = new TestInjector(); |
| 171 | mDataManager = new DataManager(mContext, mInjector); |
| 172 | mDataManager.initialize(); |
| 173 | } |
| 174 | |
| 175 | @After |
| 176 | public void tearDown() { |
| 177 | LocalServices.removeServiceForTest(ShortcutServiceInternal.class); |
| 178 | LocalServices.removeServiceForTest(UsageStatsManagerInternal.class); |
| 179 | } |
| 180 | |
| 181 | @Test |
| 182 | public void testAccessConversationFromTheSameProfileGroup() { |
| 183 | mDataManager.onUserUnlocked(USER_ID_PRIMARY); |
| 184 | mDataManager.onUserUnlocked(USER_ID_PRIMARY_MANAGED); |
| 185 | mDataManager.onUserUnlocked(USER_ID_SECONDARY); |
| 186 | |
| 187 | mDataManager.onShortcutAddedOrUpdated( |
| 188 | buildShortcutInfo("pkg_1", USER_ID_PRIMARY, "sc_1", |
| 189 | buildPerson(true, false))); |
| 190 | mDataManager.onShortcutAddedOrUpdated( |
| 191 | buildShortcutInfo("pkg_2", USER_ID_PRIMARY_MANAGED, "sc_2", |
| 192 | buildPerson(false, true))); |
| 193 | mDataManager.onShortcutAddedOrUpdated( |
| 194 | buildShortcutInfo("pkg_3", USER_ID_SECONDARY, "sc_3", buildPerson())); |
| 195 | |
| 196 | List<ConversationInfo> conversations = new ArrayList<>(); |
| 197 | mDataManager.forAllPackages( |
| 198 | packageData -> packageData.forAllConversations(conversations::add)); |
| 199 | |
| 200 | // USER_ID_SECONDARY is not in the same profile group as USER_ID_PRIMARY. |
| 201 | assertEquals(2, conversations.size()); |
| 202 | |
| 203 | assertEquals("sc_1", conversations.get(0).getShortcutId()); |
| 204 | assertTrue(conversations.get(0).isPersonImportant()); |
| 205 | assertFalse(conversations.get(0).isPersonBot()); |
| 206 | assertFalse(conversations.get(0).isContactStarred()); |
| 207 | assertEquals(PHONE_NUMBER, conversations.get(0).getContactPhoneNumber()); |
| 208 | |
| 209 | assertEquals("sc_2", conversations.get(1).getShortcutId()); |
| 210 | assertFalse(conversations.get(1).isPersonImportant()); |
| 211 | assertTrue(conversations.get(1).isPersonBot()); |
| 212 | assertFalse(conversations.get(0).isContactStarred()); |
| 213 | assertEquals(PHONE_NUMBER, conversations.get(0).getContactPhoneNumber()); |
| 214 | } |
| 215 | |
| 216 | @Test |
| 217 | public void testAccessConversationForUnlockedUsersOnly() { |
| 218 | mDataManager.onUserUnlocked(USER_ID_PRIMARY); |
| 219 | mDataManager.onShortcutAddedOrUpdated( |
| 220 | buildShortcutInfo("pkg_1", USER_ID_PRIMARY, "sc_1", buildPerson())); |
| 221 | mDataManager.onShortcutAddedOrUpdated( |
| 222 | buildShortcutInfo("pkg_2", USER_ID_PRIMARY_MANAGED, "sc_2", buildPerson())); |
| 223 | |
| 224 | List<ConversationInfo> conversations = new ArrayList<>(); |
| 225 | mDataManager.forAllPackages( |
| 226 | packageData -> packageData.forAllConversations(conversations::add)); |
| 227 | |
| 228 | // USER_ID_PRIMARY_MANAGED is not locked, so only USER_ID_PRIMARY's conversation is stored. |
| 229 | assertEquals(1, conversations.size()); |
| 230 | assertEquals("sc_1", conversations.get(0).getShortcutId()); |
| 231 | |
| 232 | mDataManager.onUserStopped(USER_ID_PRIMARY); |
| 233 | conversations.clear(); |
| 234 | mDataManager.forAllPackages( |
| 235 | packageData -> packageData.forAllConversations(conversations::add)); |
| 236 | assertTrue(conversations.isEmpty()); |
| 237 | } |
| 238 | |
| 239 | @Test |
| 240 | public void testGetShortcut() { |
| 241 | mDataManager.getShortcut(TEST_PKG_NAME, USER_ID_PRIMARY, TEST_SHORTCUT_ID); |
| 242 | verify(mShortcutServiceInternal).getShortcuts(anyInt(), anyString(), anyLong(), |
| 243 | eq(TEST_PKG_NAME), eq(Collections.singletonList(TEST_SHORTCUT_ID)), |
Mehdi Alizadeh | ae808ff | 2020-01-21 13:39:53 -0800 | [diff] [blame] | 244 | eq(null), eq(null), anyInt(), eq(USER_ID_PRIMARY), anyInt(), anyInt()); |
Danning Chen | a1bf86d | 2020-01-14 16:09:24 -0800 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | @Test |
Danning Chen | a1bf86d | 2020-01-14 16:09:24 -0800 | [diff] [blame] | 248 | public void testReportAppTargetEvent() throws IntentFilter.MalformedMimeTypeException { |
| 249 | mDataManager.onUserUnlocked(USER_ID_PRIMARY); |
| 250 | ShortcutInfo shortcut = buildShortcutInfo(TEST_PKG_NAME, USER_ID_PRIMARY, TEST_SHORTCUT_ID, |
| 251 | buildPerson()); |
| 252 | mDataManager.onShortcutAddedOrUpdated(shortcut); |
| 253 | |
| 254 | AppTarget appTarget = new AppTarget.Builder(new AppTargetId(TEST_SHORTCUT_ID), shortcut) |
| 255 | .build(); |
| 256 | AppTargetEvent appTargetEvent = |
| 257 | new AppTargetEvent.Builder(appTarget, AppTargetEvent.ACTION_LAUNCH) |
| 258 | .setLaunchLocation(ChooserActivity.LAUNCH_LOCATON_DIRECT_SHARE) |
| 259 | .build(); |
| 260 | IntentFilter intentFilter = new IntentFilter(Intent.ACTION_SEND, "image/jpg"); |
| 261 | mDataManager.reportAppTargetEvent(appTargetEvent, intentFilter); |
| 262 | |
| 263 | List<Range<Long>> activeShareTimeSlots = new ArrayList<>(); |
| 264 | mDataManager.forAllPackages(packageData -> |
| 265 | activeShareTimeSlots.addAll( |
| 266 | packageData.getEventHistory(TEST_SHORTCUT_ID) |
| 267 | .getEventIndex(Event.TYPE_SHARE_IMAGE) |
| 268 | .getActiveTimeSlots())); |
| 269 | assertEquals(1, activeShareTimeSlots.size()); |
| 270 | } |
| 271 | |
| 272 | @Test |
| 273 | public void testContactsChanged() { |
| 274 | mDataManager.onUserUnlocked(USER_ID_PRIMARY); |
Danning Chen | b3f8521 | 2020-02-05 11:52:46 -0800 | [diff] [blame] | 275 | mDataManager.onUserUnlocked(USER_ID_SECONDARY); |
Danning Chen | a1bf86d | 2020-01-14 16:09:24 -0800 | [diff] [blame] | 276 | |
| 277 | ShortcutInfo shortcut = buildShortcutInfo(TEST_PKG_NAME, USER_ID_PRIMARY, TEST_SHORTCUT_ID, |
| 278 | buildPerson()); |
| 279 | mDataManager.onShortcutAddedOrUpdated(shortcut); |
| 280 | |
| 281 | final String newPhoneNumber = "+1000000000"; |
| 282 | mInjector.mContactsQueryHelper.mIsStarred = true; |
| 283 | mInjector.mContactsQueryHelper.mPhoneNumber = newPhoneNumber; |
| 284 | |
| 285 | ContentObserver contentObserver = mDataManager.getContactsContentObserverForTesting( |
| 286 | USER_ID_PRIMARY); |
| 287 | contentObserver.onChange(false, ContactsContract.Contacts.CONTENT_URI, USER_ID_PRIMARY); |
| 288 | |
| 289 | List<ConversationInfo> conversations = new ArrayList<>(); |
| 290 | mDataManager.forAllPackages( |
| 291 | packageData -> packageData.forAllConversations(conversations::add)); |
| 292 | assertEquals(1, conversations.size()); |
| 293 | |
| 294 | assertEquals(TEST_SHORTCUT_ID, conversations.get(0).getShortcutId()); |
| 295 | assertTrue(conversations.get(0).isContactStarred()); |
| 296 | assertEquals(newPhoneNumber, conversations.get(0).getContactPhoneNumber()); |
| 297 | } |
| 298 | |
| 299 | @Test |
Danning Chen | 44725cd | 2020-02-06 12:40:22 -0800 | [diff] [blame] | 300 | public void testNotificationOpened() { |
Danning Chen | a1bf86d | 2020-01-14 16:09:24 -0800 | [diff] [blame] | 301 | mDataManager.onUserUnlocked(USER_ID_PRIMARY); |
| 302 | |
| 303 | ShortcutInfo shortcut = buildShortcutInfo(TEST_PKG_NAME, USER_ID_PRIMARY, TEST_SHORTCUT_ID, |
| 304 | buildPerson()); |
| 305 | mDataManager.onShortcutAddedOrUpdated(shortcut); |
| 306 | |
| 307 | NotificationListenerService listenerService = |
| 308 | mDataManager.getNotificationListenerServiceForTesting(USER_ID_PRIMARY); |
| 309 | |
| 310 | listenerService.onNotificationRemoved(mStatusBarNotification, null, |
| 311 | NotificationListenerService.REASON_CLICK); |
| 312 | |
| 313 | List<Range<Long>> activeNotificationOpenTimeSlots = new ArrayList<>(); |
| 314 | mDataManager.forAllPackages(packageData -> |
| 315 | activeNotificationOpenTimeSlots.addAll( |
| 316 | packageData.getEventHistory(TEST_SHORTCUT_ID) |
| 317 | .getEventIndex(Event.TYPE_NOTIFICATION_OPENED) |
| 318 | .getActiveTimeSlots())); |
| 319 | assertEquals(1, activeNotificationOpenTimeSlots.size()); |
| 320 | } |
| 321 | |
| 322 | @Test |
Danning Chen | 44725cd | 2020-02-06 12:40:22 -0800 | [diff] [blame] | 323 | public void testNotificationChannelCreated() { |
| 324 | mDataManager.onUserUnlocked(USER_ID_PRIMARY); |
| 325 | mDataManager.onUserUnlocked(USER_ID_SECONDARY); |
| 326 | |
| 327 | ShortcutInfo shortcut = buildShortcutInfo(TEST_PKG_NAME, USER_ID_PRIMARY, TEST_SHORTCUT_ID, |
| 328 | buildPerson()); |
| 329 | mDataManager.onShortcutAddedOrUpdated(shortcut); |
| 330 | |
| 331 | NotificationListenerService listenerService = |
| 332 | mDataManager.getNotificationListenerServiceForTesting(USER_ID_PRIMARY); |
| 333 | listenerService.onNotificationChannelModified(TEST_PKG_NAME, UserHandle.of(USER_ID_PRIMARY), |
| 334 | mNotificationChannel, NOTIFICATION_CHANNEL_OR_GROUP_ADDED); |
| 335 | |
| 336 | ConversationInfo conversationInfo = mDataManager.getPackage(TEST_PKG_NAME, USER_ID_PRIMARY) |
| 337 | .getConversationStore() |
| 338 | .getConversation(TEST_SHORTCUT_ID); |
| 339 | assertEquals(NOTIFICATION_CHANNEL_ID, conversationInfo.getNotificationChannelId()); |
| 340 | assertFalse(conversationInfo.isImportant()); |
| 341 | assertFalse(conversationInfo.isNotificationSilenced()); |
| 342 | assertFalse(conversationInfo.isDemoted()); |
| 343 | } |
| 344 | |
| 345 | @Test |
| 346 | public void testNotificationChannelModified() { |
| 347 | mNotificationChannel.setImportantConversation(true); |
| 348 | |
| 349 | mDataManager.onUserUnlocked(USER_ID_PRIMARY); |
| 350 | mDataManager.onUserUnlocked(USER_ID_SECONDARY); |
| 351 | |
| 352 | ShortcutInfo shortcut = buildShortcutInfo(TEST_PKG_NAME, USER_ID_PRIMARY, TEST_SHORTCUT_ID, |
| 353 | buildPerson()); |
| 354 | mDataManager.onShortcutAddedOrUpdated(shortcut); |
| 355 | |
| 356 | NotificationListenerService listenerService = |
| 357 | mDataManager.getNotificationListenerServiceForTesting(USER_ID_PRIMARY); |
| 358 | listenerService.onNotificationChannelModified(TEST_PKG_NAME, UserHandle.of(USER_ID_PRIMARY), |
| 359 | mNotificationChannel, NOTIFICATION_CHANNEL_OR_GROUP_UPDATED); |
| 360 | |
| 361 | ConversationInfo conversationInfo = mDataManager.getPackage(TEST_PKG_NAME, USER_ID_PRIMARY) |
| 362 | .getConversationStore() |
| 363 | .getConversation(TEST_SHORTCUT_ID); |
| 364 | assertEquals(NOTIFICATION_CHANNEL_ID, conversationInfo.getNotificationChannelId()); |
| 365 | assertTrue(conversationInfo.isImportant()); |
| 366 | assertFalse(conversationInfo.isNotificationSilenced()); |
| 367 | assertFalse(conversationInfo.isDemoted()); |
| 368 | } |
| 369 | |
| 370 | @Test |
| 371 | public void testNotificationChannelDeleted() { |
| 372 | mDataManager.onUserUnlocked(USER_ID_PRIMARY); |
| 373 | mDataManager.onUserUnlocked(USER_ID_SECONDARY); |
| 374 | |
| 375 | ShortcutInfo shortcut = buildShortcutInfo(TEST_PKG_NAME, USER_ID_PRIMARY, TEST_SHORTCUT_ID, |
| 376 | buildPerson()); |
| 377 | mDataManager.onShortcutAddedOrUpdated(shortcut); |
| 378 | |
| 379 | NotificationListenerService listenerService = |
| 380 | mDataManager.getNotificationListenerServiceForTesting(USER_ID_PRIMARY); |
| 381 | listenerService.onNotificationChannelModified(TEST_PKG_NAME, UserHandle.of(USER_ID_PRIMARY), |
| 382 | mNotificationChannel, NOTIFICATION_CHANNEL_OR_GROUP_ADDED); |
| 383 | ConversationInfo conversationInfo = mDataManager.getPackage(TEST_PKG_NAME, USER_ID_PRIMARY) |
| 384 | .getConversationStore() |
| 385 | .getConversation(TEST_SHORTCUT_ID); |
| 386 | assertEquals(NOTIFICATION_CHANNEL_ID, conversationInfo.getNotificationChannelId()); |
| 387 | |
| 388 | listenerService.onNotificationChannelModified(TEST_PKG_NAME, UserHandle.of(USER_ID_PRIMARY), |
| 389 | mNotificationChannel, NOTIFICATION_CHANNEL_OR_GROUP_DELETED); |
| 390 | conversationInfo = mDataManager.getPackage(TEST_PKG_NAME, USER_ID_PRIMARY) |
| 391 | .getConversationStore() |
| 392 | .getConversation(TEST_SHORTCUT_ID); |
| 393 | assertNull(conversationInfo.getNotificationChannelId()); |
| 394 | assertFalse(conversationInfo.isImportant()); |
| 395 | assertFalse(conversationInfo.isNotificationSilenced()); |
| 396 | assertFalse(conversationInfo.isDemoted()); |
| 397 | } |
| 398 | |
| 399 | @Test |
Danning Chen | e84ee5f | 2020-01-29 15:11:03 -0800 | [diff] [blame] | 400 | public void testCallLogContentObserver() { |
| 401 | mDataManager.onUserUnlocked(USER_ID_PRIMARY); |
Danning Chen | b3f8521 | 2020-02-05 11:52:46 -0800 | [diff] [blame] | 402 | mDataManager.onUserUnlocked(USER_ID_SECONDARY); |
Danning Chen | e84ee5f | 2020-01-29 15:11:03 -0800 | [diff] [blame] | 403 | |
| 404 | ShortcutInfo shortcut = buildShortcutInfo(TEST_PKG_NAME, USER_ID_PRIMARY, TEST_SHORTCUT_ID, |
| 405 | buildPerson()); |
| 406 | mDataManager.onShortcutAddedOrUpdated(shortcut); |
| 407 | |
| 408 | ContentObserver contentObserver = mDataManager.getCallLogContentObserverForTesting(); |
| 409 | contentObserver.onChange(false); |
| 410 | long currentTimestamp = System.currentTimeMillis(); |
| 411 | mInjector.mCallLogQueryHelper.mEventConsumer.accept(PHONE_NUMBER, |
| 412 | new Event(currentTimestamp - MILLIS_PER_MINUTE * 15L, Event.TYPE_CALL_OUTGOING)); |
| 413 | mInjector.mCallLogQueryHelper.mEventConsumer.accept(PHONE_NUMBER, |
| 414 | new Event(currentTimestamp - MILLIS_PER_MINUTE * 10L, Event.TYPE_CALL_INCOMING)); |
| 415 | mInjector.mCallLogQueryHelper.mEventConsumer.accept(PHONE_NUMBER, |
| 416 | new Event(currentTimestamp - MILLIS_PER_MINUTE * 5L, Event.TYPE_CALL_MISSED)); |
| 417 | |
| 418 | List<Range<Long>> activeTimeSlots = new ArrayList<>(); |
| 419 | mDataManager.forAllPackages(packageData -> |
| 420 | activeTimeSlots.addAll( |
| 421 | packageData.getEventHistory(TEST_SHORTCUT_ID) |
| 422 | .getEventIndex(Event.CALL_EVENT_TYPES) |
| 423 | .getActiveTimeSlots())); |
| 424 | assertEquals(3, activeTimeSlots.size()); |
| 425 | } |
| 426 | |
| 427 | @Test |
| 428 | public void testMmsSmsContentObserver() { |
| 429 | mDataManager.onUserUnlocked(USER_ID_PRIMARY); |
Danning Chen | b3f8521 | 2020-02-05 11:52:46 -0800 | [diff] [blame] | 430 | mDataManager.onUserUnlocked(USER_ID_SECONDARY); |
Danning Chen | e84ee5f | 2020-01-29 15:11:03 -0800 | [diff] [blame] | 431 | |
| 432 | ShortcutInfo shortcut = buildShortcutInfo(TEST_PKG_NAME, USER_ID_PRIMARY, TEST_SHORTCUT_ID, |
| 433 | buildPerson()); |
| 434 | mDataManager.onShortcutAddedOrUpdated(shortcut); |
| 435 | mDataManager.getUserDataForTesting(USER_ID_PRIMARY).setDefaultSmsApp(TEST_PKG_NAME); |
| 436 | |
| 437 | ContentObserver contentObserver = mDataManager.getMmsSmsContentObserverForTesting(); |
| 438 | contentObserver.onChange(false); |
| 439 | long currentTimestamp = System.currentTimeMillis(); |
| 440 | Event outgoingSmsEvent = |
| 441 | new Event(currentTimestamp - MILLIS_PER_MINUTE * 10L, Event.TYPE_SMS_OUTGOING); |
| 442 | Event incomingSmsEvent = |
| 443 | new Event(currentTimestamp - MILLIS_PER_MINUTE * 5L, Event.TYPE_SMS_INCOMING); |
| 444 | mInjector.mMmsQueryHelper.mEventConsumer.accept(PHONE_NUMBER, outgoingSmsEvent); |
| 445 | mInjector.mSmsQueryHelper.mEventConsumer.accept(PHONE_NUMBER, incomingSmsEvent); |
| 446 | |
| 447 | List<Range<Long>> activeTimeSlots = new ArrayList<>(); |
| 448 | mDataManager.forAllPackages(packageData -> |
| 449 | activeTimeSlots.addAll( |
| 450 | packageData.getEventHistory(TEST_SHORTCUT_ID) |
| 451 | .getEventIndex(Event.SMS_EVENT_TYPES) |
| 452 | .getActiveTimeSlots())); |
| 453 | assertEquals(2, activeTimeSlots.size()); |
| 454 | } |
| 455 | |
Danning Chen | a1bf86d | 2020-01-14 16:09:24 -0800 | [diff] [blame] | 456 | private static <T> void addLocalServiceMock(Class<T> clazz, T mock) { |
| 457 | LocalServices.removeServiceForTest(clazz); |
| 458 | LocalServices.addService(clazz, mock); |
| 459 | } |
| 460 | |
| 461 | private ShortcutInfo buildShortcutInfo(String packageName, int userId, String id, |
| 462 | @Nullable Person person) { |
| 463 | Context mockContext = mock(Context.class); |
| 464 | when(mockContext.getPackageName()).thenReturn(packageName); |
| 465 | when(mockContext.getUserId()).thenReturn(userId); |
| 466 | when(mockContext.getUser()).thenReturn(UserHandle.of(userId)); |
| 467 | ShortcutInfo.Builder builder = new ShortcutInfo.Builder(mockContext, id) |
| 468 | .setShortLabel(id) |
| 469 | .setIntent(new Intent("TestIntent")); |
| 470 | if (person != null) { |
| 471 | builder.setPersons(new Person[] {person}); |
| 472 | } |
| 473 | return builder.build(); |
| 474 | } |
| 475 | |
| 476 | private Person buildPerson() { |
| 477 | return buildPerson(true, false); |
| 478 | } |
| 479 | |
| 480 | private Person buildPerson(boolean isImportant, boolean isBot) { |
| 481 | return new Person.Builder() |
| 482 | .setImportant(isImportant) |
| 483 | .setBot(isBot) |
| 484 | .setUri(CONTACT_URI) |
| 485 | .build(); |
| 486 | } |
| 487 | |
| 488 | private UserInfo buildUserInfo(int userId) { |
| 489 | return new UserInfo(userId, "", 0); |
| 490 | } |
| 491 | |
| 492 | private class TestContactsQueryHelper extends ContactsQueryHelper { |
| 493 | |
| 494 | private Uri mContactUri; |
| 495 | private boolean mIsStarred; |
| 496 | private String mPhoneNumber; |
| 497 | |
| 498 | TestContactsQueryHelper(Context context) { |
| 499 | super(context); |
| 500 | mContactUri = Uri.parse(CONTACT_URI); |
| 501 | mIsStarred = false; |
| 502 | mPhoneNumber = PHONE_NUMBER; |
| 503 | } |
| 504 | |
| 505 | @Override |
| 506 | boolean query(@NonNull String contactUri) { |
| 507 | return true; |
| 508 | } |
| 509 | |
| 510 | @Override |
| 511 | boolean querySince(long sinceTime) { |
| 512 | return true; |
| 513 | } |
| 514 | |
| 515 | @Override |
| 516 | @Nullable |
| 517 | Uri getContactUri() { |
| 518 | return mContactUri; |
| 519 | } |
| 520 | |
| 521 | @Override |
| 522 | boolean isStarred() { |
| 523 | return mIsStarred; |
| 524 | } |
| 525 | |
| 526 | @Override |
| 527 | @Nullable |
| 528 | String getPhoneNumber() { |
| 529 | return mPhoneNumber; |
| 530 | } |
| 531 | } |
| 532 | |
Danning Chen | e84ee5f | 2020-01-29 15:11:03 -0800 | [diff] [blame] | 533 | private class TestCallLogQueryHelper extends CallLogQueryHelper { |
| 534 | |
| 535 | private final BiConsumer<String, Event> mEventConsumer; |
| 536 | |
| 537 | TestCallLogQueryHelper(Context context, BiConsumer<String, Event> eventConsumer) { |
| 538 | super(context, eventConsumer); |
| 539 | mEventConsumer = eventConsumer; |
| 540 | } |
| 541 | |
| 542 | @Override |
| 543 | boolean querySince(long sinceTime) { |
| 544 | return true; |
| 545 | } |
| 546 | |
| 547 | @Override |
| 548 | long getLastCallTimestamp() { |
| 549 | return 100L; |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | private class TestSmsQueryHelper extends SmsQueryHelper { |
| 554 | |
| 555 | private final BiConsumer<String, Event> mEventConsumer; |
| 556 | |
| 557 | TestSmsQueryHelper(Context context, BiConsumer<String, Event> eventConsumer) { |
| 558 | super(context, eventConsumer); |
| 559 | mEventConsumer = eventConsumer; |
| 560 | } |
| 561 | |
| 562 | @Override |
| 563 | boolean querySince(long sinceTime) { |
| 564 | return true; |
| 565 | } |
| 566 | |
| 567 | @Override |
| 568 | long getLastMessageTimestamp() { |
| 569 | return 100L; |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | private class TestMmsQueryHelper extends MmsQueryHelper { |
| 574 | |
| 575 | private final BiConsumer<String, Event> mEventConsumer; |
| 576 | |
| 577 | TestMmsQueryHelper(Context context, BiConsumer<String, Event> eventConsumer) { |
| 578 | super(context, eventConsumer); |
| 579 | mEventConsumer = eventConsumer; |
| 580 | } |
| 581 | |
| 582 | @Override |
| 583 | boolean querySince(long sinceTime) { |
| 584 | return true; |
| 585 | } |
| 586 | |
| 587 | @Override |
| 588 | long getLastMessageTimestamp() { |
| 589 | return 100L; |
| 590 | } |
| 591 | } |
| 592 | |
Danning Chen | a1bf86d | 2020-01-14 16:09:24 -0800 | [diff] [blame] | 593 | private class TestInjector extends DataManager.Injector { |
| 594 | |
| 595 | private final TestContactsQueryHelper mContactsQueryHelper = |
| 596 | new TestContactsQueryHelper(mContext); |
Danning Chen | e84ee5f | 2020-01-29 15:11:03 -0800 | [diff] [blame] | 597 | private TestCallLogQueryHelper mCallLogQueryHelper; |
| 598 | private TestMmsQueryHelper mMmsQueryHelper; |
| 599 | private TestSmsQueryHelper mSmsQueryHelper; |
Danning Chen | a1bf86d | 2020-01-14 16:09:24 -0800 | [diff] [blame] | 600 | |
| 601 | @Override |
| 602 | ScheduledExecutorService createScheduledExecutor() { |
| 603 | return mExecutorService; |
| 604 | } |
| 605 | |
| 606 | @Override |
| 607 | ContactsQueryHelper createContactsQueryHelper(Context context) { |
| 608 | return mContactsQueryHelper; |
| 609 | } |
| 610 | |
| 611 | @Override |
Danning Chen | e84ee5f | 2020-01-29 15:11:03 -0800 | [diff] [blame] | 612 | CallLogQueryHelper createCallLogQueryHelper(Context context, |
| 613 | BiConsumer<String, Event> eventConsumer) { |
| 614 | mCallLogQueryHelper = new TestCallLogQueryHelper(context, eventConsumer); |
| 615 | return mCallLogQueryHelper; |
| 616 | } |
| 617 | |
| 618 | @Override |
| 619 | MmsQueryHelper createMmsQueryHelper(Context context, |
| 620 | BiConsumer<String, Event> eventConsumer) { |
| 621 | mMmsQueryHelper = new TestMmsQueryHelper(context, eventConsumer); |
| 622 | return mMmsQueryHelper; |
| 623 | } |
| 624 | |
| 625 | @Override |
| 626 | SmsQueryHelper createSmsQueryHelper(Context context, |
| 627 | BiConsumer<String, Event> eventConsumer) { |
| 628 | mSmsQueryHelper = new TestSmsQueryHelper(context, eventConsumer); |
| 629 | return mSmsQueryHelper; |
| 630 | } |
| 631 | |
| 632 | @Override |
Danning Chen | a1bf86d | 2020-01-14 16:09:24 -0800 | [diff] [blame] | 633 | int getCallingUserId() { |
| 634 | return mCallingUserId; |
| 635 | } |
| 636 | } |
| 637 | } |