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 | |
| 19 | |
| 20 | import static org.junit.Assert.assertEquals; |
| 21 | import static org.junit.Assert.assertFalse; |
| 22 | import static org.junit.Assert.assertTrue; |
| 23 | |
| 24 | import android.content.LocusId; |
| 25 | import android.content.pm.ShortcutInfo; |
| 26 | import android.net.Uri; |
| 27 | |
| 28 | import org.junit.Before; |
| 29 | import org.junit.Test; |
| 30 | import org.junit.runner.RunWith; |
| 31 | import org.junit.runners.JUnit4; |
| 32 | |
| 33 | import java.util.List; |
| 34 | |
| 35 | @RunWith(JUnit4.class) |
| 36 | public final class PackageDataTest { |
| 37 | |
| 38 | private static final String PACKAGE_NAME = "com.google.test"; |
| 39 | private static final int USER_ID = 0; |
| 40 | private static final String SHORTCUT_ID = "abc"; |
| 41 | private static final LocusId LOCUS_ID = new LocusId("def"); |
| 42 | private static final Uri CONTACT_URI = Uri.parse("tel:+1234567890"); |
| 43 | private static final String PHONE_NUMBER = "+1234567890"; |
| 44 | |
| 45 | private Event mE1; |
| 46 | private Event mE2; |
| 47 | private Event mE3; |
| 48 | private Event mE4; |
| 49 | |
| 50 | private PackageData mPackageData; |
| 51 | |
| 52 | @Before |
| 53 | public void setUp() { |
| 54 | mPackageData = new PackageData(PACKAGE_NAME, USER_ID); |
| 55 | ConversationInfo conversationInfo = new ConversationInfo.Builder() |
| 56 | .setShortcutId(SHORTCUT_ID) |
| 57 | .setLocusId(LOCUS_ID) |
| 58 | .setContactUri(CONTACT_URI) |
| 59 | .setContactPhoneNumber(PHONE_NUMBER) |
| 60 | .setShortcutFlags(ShortcutInfo.FLAG_LONG_LIVED) |
| 61 | .build(); |
| 62 | mPackageData.getConversationStore().addOrUpdate(conversationInfo); |
| 63 | |
| 64 | long currentTimestamp = System.currentTimeMillis(); |
| 65 | mE1 = new Event(currentTimestamp - 800L, Event.TYPE_SHORTCUT_INVOCATION); |
| 66 | mE2 = new Event(currentTimestamp - 700L, Event.TYPE_NOTIFICATION_OPENED); |
| 67 | mE3 = new Event(currentTimestamp - 600L, Event.TYPE_CALL_INCOMING); |
| 68 | mE4 = new Event(currentTimestamp - 500L, Event.TYPE_SMS_OUTGOING); |
| 69 | } |
| 70 | |
| 71 | @Test |
| 72 | public void testGetEventHistory() { |
| 73 | EventStore eventStore = mPackageData.getEventStore(); |
| 74 | eventStore.getOrCreateShortcutEventHistory(SHORTCUT_ID).addEvent(mE1); |
| 75 | eventStore.getOrCreateLocusEventHistory(LOCUS_ID).addEvent(mE2); |
| 76 | |
| 77 | EventHistory eventHistory = mPackageData.getEventHistory(SHORTCUT_ID); |
| 78 | List<Event> events = eventHistory.queryEvents(Event.ALL_EVENT_TYPES, 0L, Long.MAX_VALUE); |
| 79 | assertEquals(2, events.size()); |
| 80 | assertEventEquals(mE1, events.get(0)); |
| 81 | assertEventEquals(mE2, events.get(1)); |
| 82 | } |
| 83 | |
| 84 | @Test |
| 85 | public void testGetEventHistoryDefaultDialerAndSmsApp() { |
| 86 | mPackageData.setIsDefaultDialer(true); |
| 87 | mPackageData.setIsDefaultSmsApp(true); |
| 88 | EventStore eventStore = mPackageData.getEventStore(); |
| 89 | eventStore.getOrCreateShortcutEventHistory(SHORTCUT_ID).addEvent(mE1); |
| 90 | eventStore.getOrCreateCallEventHistory(PHONE_NUMBER).addEvent(mE3); |
| 91 | eventStore.getOrCreateSmsEventHistory(PHONE_NUMBER).addEvent(mE4); |
| 92 | |
| 93 | assertTrue(mPackageData.isDefaultDialer()); |
| 94 | assertTrue(mPackageData.isDefaultSmsApp()); |
| 95 | EventHistory eventHistory = mPackageData.getEventHistory(SHORTCUT_ID); |
| 96 | List<Event> events = eventHistory.queryEvents(Event.ALL_EVENT_TYPES, 0L, Long.MAX_VALUE); |
| 97 | assertEquals(3, events.size()); |
| 98 | assertEventEquals(mE1, events.get(0)); |
| 99 | assertEventEquals(mE3, events.get(1)); |
| 100 | assertEventEquals(mE4, events.get(2)); |
| 101 | } |
| 102 | |
| 103 | @Test |
| 104 | public void testGetEventHistoryNotDefaultDialerOrSmsApp() { |
| 105 | EventStore eventStore = mPackageData.getEventStore(); |
| 106 | eventStore.getOrCreateShortcutEventHistory(SHORTCUT_ID).addEvent(mE1); |
| 107 | eventStore.getOrCreateCallEventHistory(PHONE_NUMBER).addEvent(mE3); |
| 108 | eventStore.getOrCreateSmsEventHistory(PHONE_NUMBER).addEvent(mE4); |
| 109 | |
| 110 | assertFalse(mPackageData.isDefaultDialer()); |
| 111 | assertFalse(mPackageData.isDefaultSmsApp()); |
| 112 | EventHistory eventHistory = mPackageData.getEventHistory(SHORTCUT_ID); |
| 113 | List<Event> events = eventHistory.queryEvents(Event.ALL_EVENT_TYPES, 0L, Long.MAX_VALUE); |
| 114 | assertEquals(1, events.size()); |
| 115 | assertEventEquals(mE1, events.get(0)); |
| 116 | } |
| 117 | |
| 118 | private void assertEventEquals(Event expected, Event actual) { |
| 119 | assertEquals(expected.getTimestamp(), actual.getTimestamp()); |
| 120 | assertEquals(expected.getType(), actual.getType()); |
| 121 | } |
| 122 | } |