Brad Ebinger | 0d2c1e6 | 2016-10-13 15:21:11 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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.telecom.tests; |
| 18 | |
| 19 | import android.telecom.Logging.EventManager; |
| 20 | import android.test.suitebuilder.annotation.SmallTest; |
| 21 | |
Hall Liu | c8a396b | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 22 | import org.junit.After; |
| 23 | import org.junit.Before; |
| 24 | import org.junit.Test; |
| 25 | import org.junit.runner.RunWith; |
| 26 | import org.junit.runners.JUnit4; |
| 27 | |
Brad Ebinger | 0d2c1e6 | 2016-10-13 15:21:11 -0700 | [diff] [blame] | 28 | import java.util.List; |
| 29 | import java.util.concurrent.LinkedBlockingQueue; |
| 30 | import java.util.stream.Collectors; |
| 31 | |
Hall Liu | c8a396b | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 32 | import static org.junit.Assert.assertEquals; |
| 33 | import static org.junit.Assert.assertFalse; |
| 34 | import static org.junit.Assert.assertNotNull; |
| 35 | import static org.junit.Assert.assertTrue; |
| 36 | |
Brad Ebinger | 0d2c1e6 | 2016-10-13 15:21:11 -0700 | [diff] [blame] | 37 | /** |
| 38 | * Unit tests for android.telecom.Logging.EventManager. |
| 39 | */ |
Hall Liu | c8a396b | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 40 | @RunWith(JUnit4.class) |
Brad Ebinger | 0d2c1e6 | 2016-10-13 15:21:11 -0700 | [diff] [blame] | 41 | public class EventManagerTest extends TelecomTestCase { |
| 42 | |
| 43 | private EventManager mTestEventManager; |
| 44 | // A reference to the recently added event record, populated from the eventRecordAdded callback |
| 45 | private EventManager.EventRecord mAddedEventRecord; |
| 46 | |
| 47 | private static final String TEST_EVENT = "testEvent"; |
| 48 | private static final String TEST_START_EVENT = "testStartEvent"; |
| 49 | private static final String TEST_END_EVENT = "testEndEvent"; |
| 50 | private static final String TEST_TIMED_EVENT = "TimedEvent"; |
| 51 | private static final int TEST_DELAY_TIME = 100; // ms |
| 52 | |
| 53 | private class TestRecord implements EventManager.Loggable { |
| 54 | private String mId; |
| 55 | private String mDescription; |
| 56 | |
| 57 | TestRecord(String id, String description) { |
| 58 | mId = id; |
| 59 | mDescription = description; |
| 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public String getId() { |
| 64 | return mId; |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public String getDescription() { |
| 69 | return mDescription; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | @Override |
Hall Liu | c8a396b | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 74 | @Before |
Brad Ebinger | 0d2c1e6 | 2016-10-13 15:21:11 -0700 | [diff] [blame] | 75 | public void setUp() throws Exception { |
| 76 | super.setUp(); |
| 77 | mTestEventManager = new EventManager(() -> ""); |
| 78 | mTestEventManager.registerEventListener((e) -> mAddedEventRecord = e); |
| 79 | } |
| 80 | |
| 81 | @Override |
Hall Liu | c8a396b | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 82 | @After |
Brad Ebinger | 0d2c1e6 | 2016-10-13 15:21:11 -0700 | [diff] [blame] | 83 | public void tearDown() throws Exception { |
| 84 | mTestEventManager = null; |
| 85 | mAddedEventRecord = null; |
| 86 | super.tearDown(); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Tests EventManager#addEventRecord to make sure that new events are added properly and that |
| 91 | * the eventRecordAdded callback is working. |
| 92 | */ |
| 93 | @SmallTest |
Hall Liu | c8a396b | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 94 | @Test |
Brad Ebinger | 0d2c1e6 | 2016-10-13 15:21:11 -0700 | [diff] [blame] | 95 | public void testAddEventRecord() throws Exception { |
| 96 | TestRecord testRecord = new TestRecord("testId", "testDescription"); |
| 97 | mTestEventManager.event(testRecord, TEST_EVENT, null); |
| 98 | |
| 99 | assertNotNull(mAddedEventRecord); |
| 100 | assertEquals(testRecord, mAddedEventRecord.getRecordEntry()); |
| 101 | assertTrue(mTestEventManager.getEventRecords().contains(mAddedEventRecord)); |
| 102 | assertTrue(mTestEventManager.getCallEventRecordMap().containsKey( |
| 103 | mAddedEventRecord.getRecordEntry())); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Tests EventManager#addEventRecord for the case when we overflow the cached record entries and |
| 108 | * the oldest entry is dropped. |
| 109 | */ |
| 110 | @SmallTest |
Hall Liu | c8a396b | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 111 | @Test |
Brad Ebinger | 0d2c1e6 | 2016-10-13 15:21:11 -0700 | [diff] [blame] | 112 | public void testAddEventRecordOverflowMaxEvents() throws Exception { |
| 113 | TestRecord oldestRecordEntry = new TestRecord("id0", "desc0"); |
| 114 | // Add the oldest record separately so that we can verify it is dropped later |
| 115 | mTestEventManager.event(oldestRecordEntry, TEST_EVENT, null); |
| 116 | // Record the EventRecord created by the oldest event |
| 117 | assertNotNull(mAddedEventRecord); |
| 118 | EventManager.EventRecord oldestRecord = mAddedEventRecord; |
| 119 | for (int i = 1; i < EventManager.DEFAULT_EVENTS_TO_CACHE; i++) { |
| 120 | mTestEventManager.event(new TestRecord("id" + i, "desc" + i), TEST_EVENT, null); |
| 121 | } |
| 122 | |
| 123 | // Add a new event that overflows the cache |
| 124 | TestRecord overflowRecord = new TestRecord("newestId", "newestDesc"); |
| 125 | // Add the oldest record separately so that we can verify it is dropped later |
| 126 | mTestEventManager.event(overflowRecord, TEST_EVENT, null); |
| 127 | |
| 128 | assertFalse(mTestEventManager.getEventRecords().contains(oldestRecord)); |
| 129 | assertTrue(mTestEventManager.getEventRecords().contains(mAddedEventRecord)); |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Tests the restructuring of the record entry queue when it is changed (usually in debugging). |
| 134 | * If the queue is resized to be smaller, the oldest records are dropped. |
| 135 | */ |
| 136 | @SmallTest |
Hall Liu | c8a396b | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 137 | @Test |
Brad Ebinger | 0d2c1e6 | 2016-10-13 15:21:11 -0700 | [diff] [blame] | 138 | public void testChangeQueueSize() throws Exception { |
| 139 | TestRecord oldestRecordEntry = new TestRecord("id0", "desc0"); |
| 140 | // Add the oldest record separately so that we can verify it is dropped later |
| 141 | mTestEventManager.event(oldestRecordEntry, TEST_EVENT, null); |
| 142 | // Record the EventRecord created by the oldest event |
| 143 | assertNotNull(mAddedEventRecord); |
| 144 | EventManager.EventRecord oldestRecord = mAddedEventRecord; |
| 145 | for (int i = 1; i < EventManager.DEFAULT_EVENTS_TO_CACHE; i++) { |
| 146 | mTestEventManager.event(new TestRecord("id" + i, "desc" + i), TEST_EVENT, null); |
| 147 | } |
| 148 | |
| 149 | mTestEventManager.changeEventCacheSize(EventManager.DEFAULT_EVENTS_TO_CACHE - 1); |
| 150 | |
| 151 | assertFalse(mTestEventManager.getEventRecords().contains(oldestRecord)); |
| 152 | // Check to make sure the other event records are there (id1-9) |
| 153 | LinkedBlockingQueue<EventManager.EventRecord> eventRecords = |
| 154 | mTestEventManager.getEventRecords(); |
| 155 | for (int i = 1; i < EventManager.DEFAULT_EVENTS_TO_CACHE; i++) { |
| 156 | final int index = i; |
| 157 | List<EventManager.EventRecord> filteredEvent = eventRecords.stream() |
| 158 | .filter(e -> e.getRecordEntry().getId().equals("id" + index)) |
| 159 | .collect(Collectors.toList()); |
| 160 | assertEquals(1, filteredEvent.size()); |
| 161 | assertEquals("desc" + index, filteredEvent.get(0).getRecordEntry().getDescription()); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Tests adding TimedEventPairs and generating the paired events as well as verifies that the |
| 167 | * timing response is correct. |
| 168 | */ |
| 169 | @SmallTest |
Hall Liu | c8a396b | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 170 | @Test |
Brad Ebinger | 0d2c1e6 | 2016-10-13 15:21:11 -0700 | [diff] [blame] | 171 | public void testExtractEventTimings() throws Exception { |
| 172 | TestRecord testRecord = new TestRecord("testId", "testDesc"); |
| 173 | // Add unassociated event |
| 174 | mTestEventManager.event(testRecord, TEST_EVENT, null); |
| 175 | mTestEventManager.addRequestResponsePair(new EventManager.TimedEventPair(TEST_START_EVENT, |
| 176 | TEST_END_EVENT, TEST_TIMED_EVENT)); |
| 177 | |
| 178 | // Add Start/End Event |
| 179 | mTestEventManager.event(testRecord, TEST_START_EVENT, null); |
| 180 | try { |
| 181 | Thread.sleep(TEST_DELAY_TIME); |
| 182 | } catch (InterruptedException ignored) { } |
| 183 | mTestEventManager.event(testRecord, TEST_END_EVENT, null); |
| 184 | |
| 185 | // Verify that the events were captured and that the timing is correct. |
| 186 | List<EventManager.EventRecord.EventTiming> timings = |
| 187 | mAddedEventRecord.extractEventTimings(); |
| 188 | assertEquals(1, timings.size()); |
| 189 | assertEquals(TEST_TIMED_EVENT, timings.get(0).name); |
| 190 | // Verify that the timing is correct with a +-10 ms buffer |
| 191 | assertTrue(timings.get(0).time >= TEST_DELAY_TIME - 10); |
| 192 | assertTrue(timings.get(0).time <= TEST_DELAY_TIME + 10); |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Verify that adding events to different records does not create a valid TimedEventPair |
| 197 | */ |
| 198 | @SmallTest |
Hall Liu | c8a396b | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 199 | @Test |
Brad Ebinger | 0d2c1e6 | 2016-10-13 15:21:11 -0700 | [diff] [blame] | 200 | public void testExtractEventTimingsDifferentRecords() throws Exception { |
| 201 | TestRecord testRecord = new TestRecord("testId", "testDesc"); |
| 202 | TestRecord testRecord2 = new TestRecord("testId2", "testDesc2"); |
| 203 | mTestEventManager.addRequestResponsePair(new EventManager.TimedEventPair(TEST_START_EVENT, |
| 204 | TEST_END_EVENT, TEST_TIMED_EVENT)); |
| 205 | |
| 206 | // Add Start event for two separate records |
| 207 | mTestEventManager.event(testRecord, TEST_START_EVENT, null); |
| 208 | EventManager.EventRecord eventRecord1 = mAddedEventRecord; |
| 209 | mTestEventManager.event(testRecord2, TEST_END_EVENT, null); |
| 210 | EventManager.EventRecord eventRecord2 = mAddedEventRecord; |
| 211 | |
| 212 | // Verify that the events were captured and that the timing is correct. |
| 213 | List<EventManager.EventRecord.EventTiming> timings1 = |
| 214 | eventRecord1.extractEventTimings(); |
| 215 | List<EventManager.EventRecord.EventTiming> timings2 = |
| 216 | eventRecord2.extractEventTimings(); |
| 217 | assertEquals(0, timings1.size()); |
| 218 | assertEquals(0, timings2.size()); |
| 219 | } |
| 220 | } |