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 | import static com.android.server.people.data.TestUtils.timestamp; |
| 20 | |
| 21 | import static org.junit.Assert.assertEquals; |
| 22 | import static org.junit.Assert.assertTrue; |
| 23 | |
| 24 | import org.junit.Before; |
| 25 | import org.junit.Test; |
| 26 | import org.junit.runner.RunWith; |
| 27 | import org.junit.runners.JUnit4; |
| 28 | |
| 29 | import java.util.List; |
| 30 | |
| 31 | @RunWith(JUnit4.class) |
| 32 | public final class AggregateEventHistoryImplTest { |
| 33 | |
| 34 | private static final long CURRENT_TIMESTAMP = timestamp("01-30 18:50"); |
| 35 | |
| 36 | private static final Event E1 = new Event(timestamp("01-06 05:26"), |
| 37 | Event.TYPE_NOTIFICATION_OPENED); |
| 38 | private static final Event E2 = new Event(timestamp("01-27 18:41"), |
| 39 | Event.TYPE_NOTIFICATION_OPENED); |
| 40 | private static final Event E3 = new Event(timestamp("01-30 03:06"), |
| 41 | Event.TYPE_SMS_OUTGOING); |
| 42 | private static final Event E4 = new Event(timestamp("01-30 18:14"), |
| 43 | Event.TYPE_SMS_INCOMING); |
| 44 | |
| 45 | private EventHistoryImpl mEventHistory1; |
| 46 | private EventHistoryImpl mEventHistory2; |
| 47 | |
| 48 | private AggregateEventHistoryImpl mAggEventHistory; |
| 49 | |
| 50 | private EventIndex.Injector mInjector = new EventIndex.Injector() { |
| 51 | @Override |
| 52 | long currentTimeMillis() { |
| 53 | return CURRENT_TIMESTAMP; |
| 54 | } |
| 55 | }; |
| 56 | |
| 57 | @Before |
| 58 | public void setUp() { |
| 59 | mAggEventHistory = new AggregateEventHistoryImpl(); |
| 60 | |
| 61 | EventHistoryImpl.Injector injector = new EventHistoryImplInjector(); |
| 62 | |
| 63 | mEventHistory1 = new EventHistoryImpl(injector); |
| 64 | mEventHistory1.addEvent(E1); |
| 65 | mEventHistory1.addEvent(E2); |
| 66 | |
| 67 | mEventHistory2 = new EventHistoryImpl(injector); |
| 68 | mEventHistory2.addEvent(E3); |
| 69 | mEventHistory2.addEvent(E4); |
| 70 | } |
| 71 | |
| 72 | @Test |
| 73 | public void testEmptyAggregateEventHistory() { |
| 74 | assertTrue(mAggEventHistory.getEventIndex(Event.TYPE_SHORTCUT_INVOCATION).isEmpty()); |
| 75 | assertTrue(mAggEventHistory.getEventIndex(Event.ALL_EVENT_TYPES).isEmpty()); |
| 76 | assertTrue(mAggEventHistory.queryEvents( |
| 77 | Event.ALL_EVENT_TYPES, 0L, Long.MAX_VALUE).isEmpty()); |
| 78 | } |
| 79 | |
| 80 | @Test |
| 81 | public void testQueryEventIndexForSingleEventType() { |
| 82 | mAggEventHistory.addEventHistory(mEventHistory1); |
| 83 | mAggEventHistory.addEventHistory(mEventHistory2); |
| 84 | |
| 85 | EventIndex eventIndex; |
| 86 | |
| 87 | eventIndex = mAggEventHistory.getEventIndex(Event.TYPE_NOTIFICATION_OPENED); |
| 88 | assertEquals(2, eventIndex.getActiveTimeSlots().size()); |
| 89 | |
| 90 | eventIndex = mAggEventHistory.getEventIndex(Event.TYPE_SMS_OUTGOING); |
| 91 | assertEquals(1, eventIndex.getActiveTimeSlots().size()); |
| 92 | |
| 93 | eventIndex = mAggEventHistory.getEventIndex(Event.TYPE_SHORTCUT_INVOCATION); |
| 94 | assertTrue(eventIndex.isEmpty()); |
| 95 | } |
| 96 | |
| 97 | @Test |
| 98 | public void testQueryEventIndexForMultipleEventTypes() { |
| 99 | mAggEventHistory.addEventHistory(mEventHistory1); |
| 100 | mAggEventHistory.addEventHistory(mEventHistory2); |
| 101 | |
| 102 | EventIndex eventIndex; |
| 103 | |
| 104 | eventIndex = mAggEventHistory.getEventIndex(Event.SMS_EVENT_TYPES); |
| 105 | assertEquals(2, eventIndex.getActiveTimeSlots().size()); |
| 106 | |
| 107 | eventIndex = mAggEventHistory.getEventIndex(Event.ALL_EVENT_TYPES); |
| 108 | assertEquals(4, eventIndex.getActiveTimeSlots().size()); |
| 109 | } |
| 110 | |
| 111 | @Test |
| 112 | public void testQueryEvents() { |
| 113 | mAggEventHistory.addEventHistory(mEventHistory1); |
| 114 | mAggEventHistory.addEventHistory(mEventHistory2); |
| 115 | |
| 116 | List<Event> events; |
| 117 | |
| 118 | events = mAggEventHistory.queryEvents(Event.NOTIFICATION_EVENT_TYPES, 0L, Long.MAX_VALUE); |
| 119 | assertEquals(2, events.size()); |
| 120 | |
| 121 | events = mAggEventHistory.queryEvents(Event.ALL_EVENT_TYPES, 0L, Long.MAX_VALUE); |
| 122 | assertEquals(4, events.size()); |
| 123 | } |
| 124 | |
| 125 | private class EventHistoryImplInjector extends EventHistoryImpl.Injector { |
| 126 | |
| 127 | EventIndex createEventIndex() { |
| 128 | return new EventIndex(mInjector); |
| 129 | } |
| 130 | } |
| 131 | } |