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 org.junit.Assert.assertEquals; |
| 20 | import static org.junit.Assert.assertTrue; |
| 21 | |
| 22 | import com.google.android.collect.Lists; |
| 23 | import com.google.android.collect.Sets; |
| 24 | |
| 25 | import org.junit.Before; |
| 26 | import org.junit.Test; |
| 27 | import org.junit.runner.RunWith; |
| 28 | import org.junit.runners.JUnit4; |
| 29 | |
| 30 | import java.util.List; |
| 31 | |
| 32 | @RunWith(JUnit4.class) |
| 33 | public final class EventListTest { |
| 34 | |
| 35 | private static final Event E1 = new Event(101L, Event.TYPE_NOTIFICATION_OPENED); |
| 36 | private static final Event E2 = new Event(103L, Event.TYPE_NOTIFICATION_OPENED); |
| 37 | private static final Event E3 = new Event(107L, Event.TYPE_SHARE_IMAGE); |
| 38 | private static final Event E4 = new Event(109L, Event.TYPE_SMS_INCOMING); |
| 39 | |
| 40 | private EventList mEventList; |
| 41 | |
| 42 | @Before |
| 43 | public void setUp() { |
| 44 | mEventList = new EventList(); |
| 45 | } |
| 46 | |
| 47 | @Test |
| 48 | public void testQueryEmptyEventList() { |
| 49 | List<Event> events = mEventList.queryEvents(Event.ALL_EVENT_TYPES, 0L, 999L); |
| 50 | assertTrue(events.isEmpty()); |
| 51 | } |
| 52 | |
| 53 | @Test |
| 54 | public void testAddAndQueryEvents() { |
| 55 | List<Event> in = Lists.newArrayList(E1, E2, E3, E4); |
| 56 | for (Event e : in) { |
| 57 | mEventList.add(e); |
| 58 | } |
| 59 | |
| 60 | List<Event> out = mEventList.queryEvents(Event.ALL_EVENT_TYPES, 0L, 999L); |
| 61 | assertEventListEquals(in, out); |
| 62 | } |
| 63 | |
| 64 | @Test |
| 65 | public void testAddEventsNotInOrder() { |
| 66 | mEventList.add(E3); |
| 67 | mEventList.add(E1); |
| 68 | mEventList.add(E4); |
| 69 | mEventList.add(E2); |
| 70 | |
| 71 | List<Event> out = mEventList.queryEvents(Event.ALL_EVENT_TYPES, 0L, 999L); |
| 72 | List<Event> expected = Lists.newArrayList(E1, E2, E3, E4); |
| 73 | assertEventListEquals(expected, out); |
| 74 | } |
| 75 | |
| 76 | @Test |
| 77 | public void testQueryEventsByType() { |
| 78 | mEventList.add(E1); |
| 79 | mEventList.add(E2); |
| 80 | mEventList.add(E3); |
| 81 | mEventList.add(E4); |
| 82 | |
| 83 | List<Event> out = mEventList.queryEvents( |
| 84 | Sets.newArraySet(Event.TYPE_NOTIFICATION_OPENED), 0L, 999L); |
| 85 | assertEventListEquals(Lists.newArrayList(E1, E2), out); |
| 86 | } |
| 87 | |
| 88 | @Test |
| 89 | public void testQueryEventsByTimeRange() { |
| 90 | mEventList.add(E1); |
| 91 | mEventList.add(E2); |
| 92 | mEventList.add(E3); |
| 93 | mEventList.add(E4); |
| 94 | |
| 95 | List<Event> out = mEventList.queryEvents(Event.ALL_EVENT_TYPES, 103L, 109L); |
| 96 | // Only E2 and E3 are in the time range [103L, 109L). |
| 97 | assertEventListEquals(Lists.newArrayList(E2, E3), out); |
| 98 | } |
| 99 | |
| 100 | @Test |
| 101 | public void testQueryEventsOutOfRange() { |
| 102 | mEventList.add(E1); |
| 103 | mEventList.add(E2); |
| 104 | mEventList.add(E3); |
| 105 | mEventList.add(E4); |
| 106 | |
| 107 | List<Event> out = mEventList.queryEvents(Event.ALL_EVENT_TYPES, 900L, 900L); |
| 108 | assertTrue(out.isEmpty()); |
| 109 | } |
| 110 | |
| 111 | @Test |
| 112 | public void testAddDuplicateEvents() { |
| 113 | mEventList.add(E1); |
| 114 | mEventList.add(E2); |
| 115 | mEventList.add(E2); |
| 116 | mEventList.add(E3); |
| 117 | mEventList.add(E2); |
| 118 | mEventList.add(E3); |
| 119 | mEventList.add(E3); |
| 120 | mEventList.add(E4); |
| 121 | mEventList.add(E1); |
| 122 | mEventList.add(E3); |
| 123 | mEventList.add(E2); |
| 124 | |
| 125 | List<Event> out = mEventList.queryEvents(Event.ALL_EVENT_TYPES, 0L, 999L); |
| 126 | List<Event> expected = Lists.newArrayList(E1, E2, E3, E4); |
| 127 | assertEventListEquals(expected, out); |
| 128 | } |
| 129 | |
| 130 | private static void assertEventListEquals(List<Event> expected, List<Event> actual) { |
| 131 | assertEquals(expected.size(), actual.size()); |
| 132 | for (int i = 0; i < expected.size(); i++) { |
| 133 | assertEquals(expected.get(i).getTimestamp(), actual.get(i).getTimestamp()); |
| 134 | assertEquals(expected.get(i).getType(), actual.get(i).getType()); |
| 135 | } |
| 136 | } |
| 137 | } |