Hugo Benichi | 3bba249 | 2016-05-30 14:42:29 +0900 | [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 | |
Hugo Benichi | b1af5e5 | 2016-09-09 15:09:23 +0900 | [diff] [blame] | 17 | package android.net; |
Hugo Benichi | 3bba249 | 2016-05-30 14:42:29 +0900 | [diff] [blame] | 18 | |
Hugo Benichi | cfddd68 | 2016-05-31 16:28:06 +0900 | [diff] [blame] | 19 | import android.os.Bundle; |
Hugo Benichi | 3bba249 | 2016-05-30 14:42:29 +0900 | [diff] [blame] | 20 | import android.os.Parcel; |
Hugo Benichi | b1af5e5 | 2016-09-09 15:09:23 +0900 | [diff] [blame] | 21 | import java.util.List; |
Hugo Benichi | 3bba249 | 2016-05-30 14:42:29 +0900 | [diff] [blame] | 22 | import junit.framework.TestCase; |
Hugo Benichi | 3bba249 | 2016-05-30 14:42:29 +0900 | [diff] [blame] | 23 | import org.mockito.ArgumentCaptor; |
| 24 | import org.mockito.Mock; |
| 25 | import org.mockito.MockitoAnnotations; |
Hugo Benichi | b1af5e5 | 2016-09-09 15:09:23 +0900 | [diff] [blame] | 26 | |
Hugo Benichi | 3bba249 | 2016-05-30 14:42:29 +0900 | [diff] [blame] | 27 | import static org.mockito.Mockito.any; |
Hugo Benichi | 3bba249 | 2016-05-30 14:42:29 +0900 | [diff] [blame] | 28 | import static org.mockito.Mockito.times; |
| 29 | import static org.mockito.Mockito.verify; |
| 30 | import static org.mockito.Mockito.when; |
| 31 | |
Hugo Benichi | b1af5e5 | 2016-09-09 15:09:23 +0900 | [diff] [blame] | 32 | public class ConnectivityMetricsLoggerTest extends TestCase { |
Hugo Benichi | 3bba249 | 2016-05-30 14:42:29 +0900 | [diff] [blame] | 33 | |
Hugo Benichi | cfddd68 | 2016-05-31 16:28:06 +0900 | [diff] [blame] | 34 | // use same Parcel object everywhere for pointer equality |
| 35 | static final Bundle FAKE_EV = new Bundle(); |
Hugo Benichi | b1af5e5 | 2016-09-09 15:09:23 +0900 | [diff] [blame] | 36 | static final int FAKE_COMPONENT = 1; |
| 37 | static final int FAKE_EVENT = 2; |
Hugo Benichi | 3bba249 | 2016-05-30 14:42:29 +0900 | [diff] [blame] | 38 | |
| 39 | @Mock IConnectivityMetricsLogger mService; |
| 40 | ArgumentCaptor<ConnectivityMetricsEvent> evCaptor; |
Hugo Benichi | b1af5e5 | 2016-09-09 15:09:23 +0900 | [diff] [blame] | 41 | ArgumentCaptor<ConnectivityMetricsEvent[]> evArrayCaptor; |
Hugo Benichi | 3bba249 | 2016-05-30 14:42:29 +0900 | [diff] [blame] | 42 | |
Hugo Benichi | b1af5e5 | 2016-09-09 15:09:23 +0900 | [diff] [blame] | 43 | ConnectivityMetricsLogger mLog; |
Hugo Benichi | 3bba249 | 2016-05-30 14:42:29 +0900 | [diff] [blame] | 44 | |
| 45 | public void setUp() { |
| 46 | MockitoAnnotations.initMocks(this); |
| 47 | evCaptor = ArgumentCaptor.forClass(ConnectivityMetricsEvent.class); |
Hugo Benichi | b1af5e5 | 2016-09-09 15:09:23 +0900 | [diff] [blame] | 48 | evArrayCaptor = ArgumentCaptor.forClass(ConnectivityMetricsEvent[].class); |
| 49 | mLog = new ConnectivityMetricsLogger(mService); |
Hugo Benichi | 3bba249 | 2016-05-30 14:42:29 +0900 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | public void testLogEvents() throws Exception { |
Hugo Benichi | b1af5e5 | 2016-09-09 15:09:23 +0900 | [diff] [blame] | 53 | mLog.logEvent(1, FAKE_COMPONENT, FAKE_EVENT, FAKE_EV); |
| 54 | mLog.logEvent(2, FAKE_COMPONENT, FAKE_EVENT, FAKE_EV); |
| 55 | mLog.logEvent(3, FAKE_COMPONENT, FAKE_EVENT, FAKE_EV); |
Hugo Benichi | 3bba249 | 2016-05-30 14:42:29 +0900 | [diff] [blame] | 56 | |
| 57 | List<ConnectivityMetricsEvent> gotEvents = verifyEvents(3); |
| 58 | assertEventsEqual(expectedEvent(1), gotEvents.get(0)); |
| 59 | assertEventsEqual(expectedEvent(2), gotEvents.get(1)); |
| 60 | assertEventsEqual(expectedEvent(3), gotEvents.get(2)); |
| 61 | } |
| 62 | |
| 63 | public void testLogEventTriggerThrottling() throws Exception { |
| 64 | when(mService.logEvent(any())).thenReturn(1234L); |
| 65 | |
Hugo Benichi | b1af5e5 | 2016-09-09 15:09:23 +0900 | [diff] [blame] | 66 | mLog.logEvent(1, FAKE_COMPONENT, FAKE_EVENT, FAKE_EV); |
| 67 | mLog.logEvent(2, FAKE_COMPONENT, FAKE_EVENT, FAKE_EV); |
| 68 | |
| 69 | List<ConnectivityMetricsEvent> gotEvents = verifyEvents(1); |
| 70 | assertEventsEqual(expectedEvent(1), gotEvents.get(0)); |
Hugo Benichi | 3bba249 | 2016-05-30 14:42:29 +0900 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | public void testLogEventFails() throws Exception { |
| 74 | when(mService.logEvent(any())).thenReturn(-1L); // Error. |
| 75 | |
Hugo Benichi | b1af5e5 | 2016-09-09 15:09:23 +0900 | [diff] [blame] | 76 | mLog.logEvent(1, FAKE_COMPONENT, FAKE_EVENT, FAKE_EV); |
| 77 | mLog.logEvent(2, FAKE_COMPONENT, FAKE_EVENT, FAKE_EV); |
| 78 | |
| 79 | List<ConnectivityMetricsEvent> gotEvents = verifyEvents(1); |
| 80 | assertEventsEqual(expectedEvent(1), gotEvents.get(0)); |
Hugo Benichi | 3bba249 | 2016-05-30 14:42:29 +0900 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | public void testLogEventWhenThrottling() throws Exception { |
| 84 | when(mService.logEvent(any())).thenReturn(Long.MAX_VALUE); // Throttled |
| 85 | |
| 86 | // No events are logged. The service is only called once |
| 87 | // After that, throttling state is maintained locally. |
Hugo Benichi | b1af5e5 | 2016-09-09 15:09:23 +0900 | [diff] [blame] | 88 | mLog.logEvent(1, FAKE_COMPONENT, FAKE_EVENT, FAKE_EV); |
| 89 | mLog.logEvent(2, FAKE_COMPONENT, FAKE_EVENT, FAKE_EV); |
Hugo Benichi | 3bba249 | 2016-05-30 14:42:29 +0900 | [diff] [blame] | 90 | |
| 91 | List<ConnectivityMetricsEvent> gotEvents = verifyEvents(1); |
| 92 | assertEventsEqual(expectedEvent(1), gotEvents.get(0)); |
| 93 | } |
| 94 | |
| 95 | public void testLogEventRecoverFromThrottling() throws Exception { |
Hugo Benichi | b1af5e5 | 2016-09-09 15:09:23 +0900 | [diff] [blame] | 96 | final long throttleTimeout = System.currentTimeMillis() + 10; |
Hugo Benichi | 3bba249 | 2016-05-30 14:42:29 +0900 | [diff] [blame] | 97 | when(mService.logEvent(any())).thenReturn(throttleTimeout, 0L); |
| 98 | |
Hugo Benichi | b1af5e5 | 2016-09-09 15:09:23 +0900 | [diff] [blame] | 99 | mLog.logEvent(1, FAKE_COMPONENT, FAKE_EVENT, FAKE_EV); |
| 100 | mLog.logEvent(2, FAKE_COMPONENT, FAKE_EVENT, FAKE_EV); |
| 101 | mLog.logEvent(3, FAKE_COMPONENT, FAKE_EVENT, FAKE_EV); |
| 102 | Thread.sleep(100); |
| 103 | mLog.logEvent(53, FAKE_COMPONENT, FAKE_EVENT, FAKE_EV); |
Hugo Benichi | 3bba249 | 2016-05-30 14:42:29 +0900 | [diff] [blame] | 104 | |
Hugo Benichi | b1af5e5 | 2016-09-09 15:09:23 +0900 | [diff] [blame] | 105 | List<ConnectivityMetricsEvent> gotEvents = verifyEvents(1); |
Hugo Benichi | 3bba249 | 2016-05-30 14:42:29 +0900 | [diff] [blame] | 106 | assertEventsEqual(expectedEvent(1), gotEvents.get(0)); |
Hugo Benichi | 3bba249 | 2016-05-30 14:42:29 +0900 | [diff] [blame] | 107 | |
Hugo Benichi | b1af5e5 | 2016-09-09 15:09:23 +0900 | [diff] [blame] | 108 | verify(mService, times(1)).logEvents(evArrayCaptor.capture()); |
| 109 | ConnectivityMetricsEvent[] gotOtherEvents = evArrayCaptor.getAllValues().get(0); |
| 110 | assertEquals(ConnectivityMetricsLogger.TAG_SKIPPED_EVENTS, gotOtherEvents[0].eventTag); |
| 111 | assertEventsEqual(expectedEvent(53), gotOtherEvents[1]); |
Hugo Benichi | 3bba249 | 2016-05-30 14:42:29 +0900 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | List<ConnectivityMetricsEvent> verifyEvents(int n) throws Exception { |
| 115 | verify(mService, times(n)).logEvent(evCaptor.capture()); |
| 116 | return evCaptor.getAllValues(); |
| 117 | } |
| 118 | |
Hugo Benichi | 3bba249 | 2016-05-30 14:42:29 +0900 | [diff] [blame] | 119 | static ConnectivityMetricsEvent expectedEvent(int timestamp) { |
Hugo Benichi | b1af5e5 | 2016-09-09 15:09:23 +0900 | [diff] [blame] | 120 | return new ConnectivityMetricsEvent((long)timestamp, FAKE_COMPONENT, FAKE_EVENT, FAKE_EV); |
Hugo Benichi | 3bba249 | 2016-05-30 14:42:29 +0900 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | /** Outer equality for ConnectivityMetricsEvent to avoid overriding equals() and hashCode(). */ |
| 124 | static void assertEventsEqual(ConnectivityMetricsEvent expected, ConnectivityMetricsEvent got) { |
| 125 | assertEquals(expected.timestamp, got.timestamp); |
| 126 | assertEquals(expected.componentTag, got.componentTag); |
| 127 | assertEquals(expected.eventTag, got.eventTag); |
| 128 | assertEquals(expected.data, got.data); |
| 129 | } |
| 130 | } |