blob: 6d42cce2e5027815b6a3bfeaaf4cd647283e9d9a [file] [log] [blame]
Hugo Benichi3bba2492016-05-30 14:42:29 +09001/*
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 Benichib1af5e52016-09-09 15:09:23 +090017package android.net;
Hugo Benichi3bba2492016-05-30 14:42:29 +090018
Hugo Benichicfddd682016-05-31 16:28:06 +090019import android.os.Bundle;
Hugo Benichi3bba2492016-05-30 14:42:29 +090020import android.os.Parcel;
Hugo Benichib1af5e52016-09-09 15:09:23 +090021import java.util.List;
Hugo Benichi3bba2492016-05-30 14:42:29 +090022import junit.framework.TestCase;
Hugo Benichi3bba2492016-05-30 14:42:29 +090023import org.mockito.ArgumentCaptor;
24import org.mockito.Mock;
25import org.mockito.MockitoAnnotations;
Hugo Benichib1af5e52016-09-09 15:09:23 +090026
Hugo Benichi3bba2492016-05-30 14:42:29 +090027import static org.mockito.Mockito.any;
Hugo Benichi3bba2492016-05-30 14:42:29 +090028import static org.mockito.Mockito.times;
29import static org.mockito.Mockito.verify;
30import static org.mockito.Mockito.when;
31
Hugo Benichib1af5e52016-09-09 15:09:23 +090032public class ConnectivityMetricsLoggerTest extends TestCase {
Hugo Benichi3bba2492016-05-30 14:42:29 +090033
Hugo Benichicfddd682016-05-31 16:28:06 +090034 // use same Parcel object everywhere for pointer equality
35 static final Bundle FAKE_EV = new Bundle();
Hugo Benichib1af5e52016-09-09 15:09:23 +090036 static final int FAKE_COMPONENT = 1;
37 static final int FAKE_EVENT = 2;
Hugo Benichi3bba2492016-05-30 14:42:29 +090038
39 @Mock IConnectivityMetricsLogger mService;
40 ArgumentCaptor<ConnectivityMetricsEvent> evCaptor;
Hugo Benichib1af5e52016-09-09 15:09:23 +090041 ArgumentCaptor<ConnectivityMetricsEvent[]> evArrayCaptor;
Hugo Benichi3bba2492016-05-30 14:42:29 +090042
Hugo Benichib1af5e52016-09-09 15:09:23 +090043 ConnectivityMetricsLogger mLog;
Hugo Benichi3bba2492016-05-30 14:42:29 +090044
45 public void setUp() {
46 MockitoAnnotations.initMocks(this);
47 evCaptor = ArgumentCaptor.forClass(ConnectivityMetricsEvent.class);
Hugo Benichib1af5e52016-09-09 15:09:23 +090048 evArrayCaptor = ArgumentCaptor.forClass(ConnectivityMetricsEvent[].class);
49 mLog = new ConnectivityMetricsLogger(mService);
Hugo Benichi3bba2492016-05-30 14:42:29 +090050 }
51
52 public void testLogEvents() throws Exception {
Hugo Benichib1af5e52016-09-09 15:09:23 +090053 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 Benichi3bba2492016-05-30 14:42:29 +090056
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 Benichib1af5e52016-09-09 15:09:23 +090066 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 Benichi3bba2492016-05-30 14:42:29 +090071 }
72
73 public void testLogEventFails() throws Exception {
74 when(mService.logEvent(any())).thenReturn(-1L); // Error.
75
Hugo Benichib1af5e52016-09-09 15:09:23 +090076 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 Benichi3bba2492016-05-30 14:42:29 +090081 }
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 Benichib1af5e52016-09-09 15:09:23 +090088 mLog.logEvent(1, FAKE_COMPONENT, FAKE_EVENT, FAKE_EV);
89 mLog.logEvent(2, FAKE_COMPONENT, FAKE_EVENT, FAKE_EV);
Hugo Benichi3bba2492016-05-30 14:42:29 +090090
91 List<ConnectivityMetricsEvent> gotEvents = verifyEvents(1);
92 assertEventsEqual(expectedEvent(1), gotEvents.get(0));
93 }
94
95 public void testLogEventRecoverFromThrottling() throws Exception {
Hugo Benichib1af5e52016-09-09 15:09:23 +090096 final long throttleTimeout = System.currentTimeMillis() + 10;
Hugo Benichi3bba2492016-05-30 14:42:29 +090097 when(mService.logEvent(any())).thenReturn(throttleTimeout, 0L);
98
Hugo Benichib1af5e52016-09-09 15:09:23 +090099 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 Benichi3bba2492016-05-30 14:42:29 +0900104
Hugo Benichib1af5e52016-09-09 15:09:23 +0900105 List<ConnectivityMetricsEvent> gotEvents = verifyEvents(1);
Hugo Benichi3bba2492016-05-30 14:42:29 +0900106 assertEventsEqual(expectedEvent(1), gotEvents.get(0));
Hugo Benichi3bba2492016-05-30 14:42:29 +0900107
Hugo Benichib1af5e52016-09-09 15:09:23 +0900108 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 Benichi3bba2492016-05-30 14:42:29 +0900112 }
113
114 List<ConnectivityMetricsEvent> verifyEvents(int n) throws Exception {
115 verify(mService, times(n)).logEvent(evCaptor.capture());
116 return evCaptor.getAllValues();
117 }
118
Hugo Benichi3bba2492016-05-30 14:42:29 +0900119 static ConnectivityMetricsEvent expectedEvent(int timestamp) {
Hugo Benichib1af5e52016-09-09 15:09:23 +0900120 return new ConnectivityMetricsEvent((long)timestamp, FAKE_COMPONENT, FAKE_EVENT, FAKE_EV);
Hugo Benichi3bba2492016-05-30 14:42:29 +0900121 }
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}