blob: e877a8f7e6c119d8399f7362989abb869ddc82e6 [file] [log] [blame]
Hugo Benichia43a0952016-08-30 10:01:15 +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
17package com.android.server.connectivity;
18
Hugo Benichi4a0c5d72017-10-11 11:26:25 +090019import static org.junit.Assert.assertFalse;
20import static org.junit.Assert.assertTrue;
Hugo Benichia43a0952016-08-30 10:01:15 +090021import static org.mockito.Mockito.any;
22import static org.mockito.Mockito.anyBoolean;
23import static org.mockito.Mockito.anyInt;
24import static org.mockito.Mockito.eq;
25import static org.mockito.Mockito.never;
Remi NGUYEN VANe67b0c32018-12-27 16:43:56 +090026import static org.mockito.Mockito.reset;
Hugo Benichia43a0952016-08-30 10:01:15 +090027import static org.mockito.Mockito.times;
28import static org.mockito.Mockito.verify;
29import static org.mockito.Mockito.when;
30
Hugo Benichi4a0c5d72017-10-11 11:26:25 +090031import android.app.PendingIntent;
32import android.content.Context;
33import android.content.res.Resources;
34import android.net.ConnectivityManager;
Lorenzo Colitti9307ca22019-01-12 01:54:23 +090035import android.net.INetd;
Hugo Benichi4a0c5d72017-10-11 11:26:25 +090036import android.net.Network;
37import android.net.NetworkCapabilities;
Chalard Jean08577fc2018-05-02 21:14:54 +090038import android.net.NetworkFactory;
Hugo Benichi4a0c5d72017-10-11 11:26:25 +090039import android.net.NetworkInfo;
40import android.net.NetworkMisc;
Remi NGUYEN VANe67b0c32018-12-27 16:43:56 +090041import android.net.NetworkStack;
Lorenzo Colitti9307ca22019-01-12 01:54:23 +090042import android.os.INetworkManagementService;
Hugo Benichi4a0c5d72017-10-11 11:26:25 +090043import android.support.test.filters.SmallTest;
Remi NGUYEN VANe67b0c32018-12-27 16:43:56 +090044import android.support.test.runner.AndroidJUnit4;
Hugo Benichi4a0c5d72017-10-11 11:26:25 +090045import android.text.format.DateUtils;
46
47import com.android.internal.R;
48import com.android.server.ConnectivityService;
Hugo Benichi4a0c5d72017-10-11 11:26:25 +090049import com.android.server.connectivity.NetworkNotificationManager.NotificationType;
50
Hugo Benichi4a0c5d72017-10-11 11:26:25 +090051import org.junit.Before;
52import org.junit.Test;
Remi NGUYEN VANe67b0c32018-12-27 16:43:56 +090053import org.junit.runner.RunWith;
Hugo Benichi4a0c5d72017-10-11 11:26:25 +090054import org.mockito.Mock;
55import org.mockito.MockitoAnnotations;
56
57@RunWith(AndroidJUnit4.class)
58@SmallTest
59public class LingerMonitorTest {
Hugo Benichia43a0952016-08-30 10:01:15 +090060 static final String CELLULAR = "CELLULAR";
61 static final String WIFI = "WIFI";
62
Lorenzo Colitti84e6f1232016-08-29 14:03:11 +090063 static final long LOW_RATE_LIMIT = DateUtils.MINUTE_IN_MILLIS;
64 static final long HIGH_RATE_LIMIT = 0;
65
66 static final int LOW_DAILY_LIMIT = 2;
67 static final int HIGH_DAILY_LIMIT = 1000;
68
Hugo Benichia43a0952016-08-30 10:01:15 +090069 LingerMonitor mMonitor;
70
71 @Mock ConnectivityService mConnService;
Lorenzo Colitti9307ca22019-01-12 01:54:23 +090072 @Mock INetd mNetd;
73 @Mock INetworkManagementService mNMS;
Hugo Benichia43a0952016-08-30 10:01:15 +090074 @Mock Context mCtx;
75 @Mock NetworkMisc mMisc;
76 @Mock NetworkNotificationManager mNotifier;
77 @Mock Resources mResources;
Remi NGUYEN VANe67b0c32018-12-27 16:43:56 +090078 @Mock NetworkStack mNetworkStack;
Hugo Benichia43a0952016-08-30 10:01:15 +090079
Hugo Benichi4a0c5d72017-10-11 11:26:25 +090080 @Before
Hugo Benichia43a0952016-08-30 10:01:15 +090081 public void setUp() {
82 MockitoAnnotations.initMocks(this);
83 when(mCtx.getResources()).thenReturn(mResources);
84 when(mCtx.getPackageName()).thenReturn("com.android.server.connectivity");
Remi NGUYEN VANe67b0c32018-12-27 16:43:56 +090085 when(mCtx.getSystemServiceName(NetworkStack.class))
86 .thenReturn(Context.NETWORK_STACK_SERVICE);
87 when(mCtx.getSystemService(Context.NETWORK_STACK_SERVICE)).thenReturn(mNetworkStack);
Hugo Benichia43a0952016-08-30 10:01:15 +090088
Lorenzo Colitti84e6f1232016-08-29 14:03:11 +090089 mMonitor = new TestableLingerMonitor(mCtx, mNotifier, HIGH_DAILY_LIMIT, HIGH_RATE_LIMIT);
Hugo Benichia43a0952016-08-30 10:01:15 +090090 }
91
Hugo Benichi4a0c5d72017-10-11 11:26:25 +090092 @Test
Hugo Benichia43a0952016-08-30 10:01:15 +090093 public void testTransitions() {
94 setNotificationSwitch(transition(WIFI, CELLULAR));
95 NetworkAgentInfo nai1 = wifiNai(100);
96 NetworkAgentInfo nai2 = cellNai(101);
97
98 assertTrue(mMonitor.isNotificationEnabled(nai1, nai2));
99 assertFalse(mMonitor.isNotificationEnabled(nai2, nai1));
100 }
101
Hugo Benichi4a0c5d72017-10-11 11:26:25 +0900102 @Test
Hugo Benichia43a0952016-08-30 10:01:15 +0900103 public void testNotificationOnLinger() {
104 setNotificationSwitch(transition(WIFI, CELLULAR));
105 setNotificationType(LingerMonitor.NOTIFY_TYPE_NOTIFICATION);
106 NetworkAgentInfo from = wifiNai(100);
107 NetworkAgentInfo to = cellNai(101);
108
109 mMonitor.noteLingerDefaultNetwork(from, to);
110 verifyNotification(from, to);
111 }
112
Hugo Benichi4a0c5d72017-10-11 11:26:25 +0900113 @Test
Hugo Benichia43a0952016-08-30 10:01:15 +0900114 public void testToastOnLinger() {
115 setNotificationSwitch(transition(WIFI, CELLULAR));
116 setNotificationType(LingerMonitor.NOTIFY_TYPE_TOAST);
117 NetworkAgentInfo from = wifiNai(100);
118 NetworkAgentInfo to = cellNai(101);
119
120 mMonitor.noteLingerDefaultNetwork(from, to);
121 verifyToast(from, to);
122 }
123
Hugo Benichi4a0c5d72017-10-11 11:26:25 +0900124 @Test
Hugo Benichia43a0952016-08-30 10:01:15 +0900125 public void testNotificationClearedAfterDisconnect() {
126 setNotificationSwitch(transition(WIFI, CELLULAR));
127 setNotificationType(LingerMonitor.NOTIFY_TYPE_NOTIFICATION);
128 NetworkAgentInfo from = wifiNai(100);
129 NetworkAgentInfo to = cellNai(101);
130
131 mMonitor.noteLingerDefaultNetwork(from, to);
132 verifyNotification(from, to);
133
134 mMonitor.noteDisconnect(to);
135 verify(mNotifier, times(1)).clearNotification(100);
136 }
137
Hugo Benichi4a0c5d72017-10-11 11:26:25 +0900138 @Test
Hugo Benichia43a0952016-08-30 10:01:15 +0900139 public void testNotificationClearedAfterSwitchingBack() {
140 setNotificationSwitch(transition(WIFI, CELLULAR));
141 setNotificationType(LingerMonitor.NOTIFY_TYPE_NOTIFICATION);
142 NetworkAgentInfo from = wifiNai(100);
143 NetworkAgentInfo to = cellNai(101);
144
145 mMonitor.noteLingerDefaultNetwork(from, to);
146 verifyNotification(from, to);
147
148 mMonitor.noteLingerDefaultNetwork(to, from);
149 verify(mNotifier, times(1)).clearNotification(100);
150 }
151
Hugo Benichi4a0c5d72017-10-11 11:26:25 +0900152 @Test
Hugo Benichia43a0952016-08-30 10:01:15 +0900153 public void testUniqueToast() {
154 setNotificationSwitch(transition(WIFI, CELLULAR));
155 setNotificationType(LingerMonitor.NOTIFY_TYPE_TOAST);
156 NetworkAgentInfo from = wifiNai(100);
157 NetworkAgentInfo to = cellNai(101);
158
159 mMonitor.noteLingerDefaultNetwork(from, to);
160 verifyToast(from, to);
161
162 mMonitor.noteLingerDefaultNetwork(to, from);
163 verify(mNotifier, times(1)).clearNotification(100);
164
Lorenzo Colitti84e6f1232016-08-29 14:03:11 +0900165 reset(mNotifier);
Hugo Benichia43a0952016-08-30 10:01:15 +0900166 mMonitor.noteLingerDefaultNetwork(from, to);
Lorenzo Colitti84e6f1232016-08-29 14:03:11 +0900167 verifyNoNotifications();
168 }
169
Hugo Benichi4a0c5d72017-10-11 11:26:25 +0900170 @Test
Lorenzo Colitti84e6f1232016-08-29 14:03:11 +0900171 public void testMultipleNotifications() {
172 setNotificationSwitch(transition(WIFI, CELLULAR));
173 setNotificationType(LingerMonitor.NOTIFY_TYPE_NOTIFICATION);
174 NetworkAgentInfo wifi1 = wifiNai(100);
175 NetworkAgentInfo wifi2 = wifiNai(101);
176 NetworkAgentInfo cell = cellNai(102);
177
178 mMonitor.noteLingerDefaultNetwork(wifi1, cell);
179 verifyNotification(wifi1, cell);
180
181 mMonitor.noteLingerDefaultNetwork(cell, wifi2);
182 verify(mNotifier, times(1)).clearNotification(100);
183
184 reset(mNotifier);
185 mMonitor.noteLingerDefaultNetwork(wifi2, cell);
186 verifyNotification(wifi2, cell);
187 }
188
Hugo Benichi4a0c5d72017-10-11 11:26:25 +0900189 @Test
Lorenzo Colitti84e6f1232016-08-29 14:03:11 +0900190 public void testRateLimiting() throws InterruptedException {
191 mMonitor = new TestableLingerMonitor(mCtx, mNotifier, HIGH_DAILY_LIMIT, LOW_RATE_LIMIT);
192
193 setNotificationSwitch(transition(WIFI, CELLULAR));
194 setNotificationType(LingerMonitor.NOTIFY_TYPE_NOTIFICATION);
195 NetworkAgentInfo wifi1 = wifiNai(100);
196 NetworkAgentInfo wifi2 = wifiNai(101);
197 NetworkAgentInfo wifi3 = wifiNai(102);
198 NetworkAgentInfo cell = cellNai(103);
199
200 mMonitor.noteLingerDefaultNetwork(wifi1, cell);
201 verifyNotification(wifi1, cell);
202 reset(mNotifier);
203
204 Thread.sleep(50);
205 mMonitor.noteLingerDefaultNetwork(cell, wifi2);
206 mMonitor.noteLingerDefaultNetwork(wifi2, cell);
207 verifyNoNotifications();
208
209 Thread.sleep(50);
210 mMonitor.noteLingerDefaultNetwork(cell, wifi3);
211 mMonitor.noteLingerDefaultNetwork(wifi3, cell);
212 verifyNoNotifications();
213 }
214
Hugo Benichi4a0c5d72017-10-11 11:26:25 +0900215 @Test
Lorenzo Colitti84e6f1232016-08-29 14:03:11 +0900216 public void testDailyLimiting() throws InterruptedException {
217 mMonitor = new TestableLingerMonitor(mCtx, mNotifier, LOW_DAILY_LIMIT, HIGH_RATE_LIMIT);
218
219 setNotificationSwitch(transition(WIFI, CELLULAR));
220 setNotificationType(LingerMonitor.NOTIFY_TYPE_NOTIFICATION);
221 NetworkAgentInfo wifi1 = wifiNai(100);
222 NetworkAgentInfo wifi2 = wifiNai(101);
223 NetworkAgentInfo wifi3 = wifiNai(102);
224 NetworkAgentInfo cell = cellNai(103);
225
226 mMonitor.noteLingerDefaultNetwork(wifi1, cell);
227 verifyNotification(wifi1, cell);
228 reset(mNotifier);
229
230 Thread.sleep(50);
231 mMonitor.noteLingerDefaultNetwork(cell, wifi2);
232 mMonitor.noteLingerDefaultNetwork(wifi2, cell);
233 verifyNotification(wifi2, cell);
234 reset(mNotifier);
235
236 Thread.sleep(50);
237 mMonitor.noteLingerDefaultNetwork(cell, wifi3);
238 mMonitor.noteLingerDefaultNetwork(wifi3, cell);
239 verifyNoNotifications();
Hugo Benichia43a0952016-08-30 10:01:15 +0900240 }
241
Hugo Benichi4a0c5d72017-10-11 11:26:25 +0900242 @Test
Hugo Benichia43a0952016-08-30 10:01:15 +0900243 public void testUniqueNotification() {
244 setNotificationSwitch(transition(WIFI, CELLULAR));
245 setNotificationType(LingerMonitor.NOTIFY_TYPE_NOTIFICATION);
246 NetworkAgentInfo from = wifiNai(100);
247 NetworkAgentInfo to = cellNai(101);
248
249 mMonitor.noteLingerDefaultNetwork(from, to);
250 verifyNotification(from, to);
251
252 mMonitor.noteLingerDefaultNetwork(to, from);
253 verify(mNotifier, times(1)).clearNotification(100);
254
255 mMonitor.noteLingerDefaultNetwork(from, to);
256 verifyNotification(from, to);
257 }
258
Hugo Benichi4a0c5d72017-10-11 11:26:25 +0900259 @Test
Lorenzo Colitti84e6f1232016-08-29 14:03:11 +0900260 public void testIgnoreNeverValidatedNetworks() {
Hugo Benichia43a0952016-08-30 10:01:15 +0900261 setNotificationType(LingerMonitor.NOTIFY_TYPE_TOAST);
262 setNotificationSwitch(transition(WIFI, CELLULAR));
263 NetworkAgentInfo from = wifiNai(100);
264 NetworkAgentInfo to = cellNai(101);
265 from.everValidated = false;
266
267 mMonitor.noteLingerDefaultNetwork(from, to);
268 verifyNoNotifications();
269 }
270
Hugo Benichi4a0c5d72017-10-11 11:26:25 +0900271 @Test
Lorenzo Colitti84e6f1232016-08-29 14:03:11 +0900272 public void testIgnoreCurrentlyValidatedNetworks() {
273 setNotificationType(LingerMonitor.NOTIFY_TYPE_TOAST);
274 setNotificationSwitch(transition(WIFI, CELLULAR));
275 NetworkAgentInfo from = wifiNai(100);
276 NetworkAgentInfo to = cellNai(101);
277 from.lastValidated = true;
278
279 mMonitor.noteLingerDefaultNetwork(from, to);
280 verifyNoNotifications();
281 }
282
Hugo Benichi4a0c5d72017-10-11 11:26:25 +0900283 @Test
Hugo Benichia43a0952016-08-30 10:01:15 +0900284 public void testNoNotificationType() {
285 setNotificationType(LingerMonitor.NOTIFY_TYPE_TOAST);
286 setNotificationSwitch();
287 NetworkAgentInfo from = wifiNai(100);
288 NetworkAgentInfo to = cellNai(101);
289
290 mMonitor.noteLingerDefaultNetwork(from, to);
291 verifyNoNotifications();
292 }
293
Hugo Benichi4a0c5d72017-10-11 11:26:25 +0900294 @Test
Hugo Benichia43a0952016-08-30 10:01:15 +0900295 public void testNoTransitionToNotify() {
296 setNotificationType(LingerMonitor.NOTIFY_TYPE_NONE);
297 setNotificationSwitch(transition(WIFI, CELLULAR));
298 NetworkAgentInfo from = wifiNai(100);
299 NetworkAgentInfo to = cellNai(101);
300
301 mMonitor.noteLingerDefaultNetwork(from, to);
302 verifyNoNotifications();
303 }
304
Hugo Benichi4a0c5d72017-10-11 11:26:25 +0900305 @Test
Hugo Benichia43a0952016-08-30 10:01:15 +0900306 public void testDifferentTransitionToNotify() {
307 setNotificationType(LingerMonitor.NOTIFY_TYPE_TOAST);
308 setNotificationSwitch(transition(CELLULAR, WIFI));
309 NetworkAgentInfo from = wifiNai(100);
310 NetworkAgentInfo to = cellNai(101);
311
312 mMonitor.noteLingerDefaultNetwork(from, to);
313 verifyNoNotifications();
314 }
315
316 void setNotificationSwitch(String... transitions) {
317 when(mResources.getStringArray(R.array.config_networkNotifySwitches))
318 .thenReturn(transitions);
319 }
320
321 String transition(String from, String to) {
322 return from + "-" + to;
323 }
324
325 void setNotificationType(int type) {
326 when(mResources.getInteger(R.integer.config_networkNotifySwitchType)).thenReturn(type);
327 }
328
329 void verifyNoToast() {
330 verify(mNotifier, never()).showToast(any(), any());
331 }
332
333 void verifyNoNotification() {
334 verify(mNotifier, never())
335 .showNotification(anyInt(), any(), any(), any(), any(), anyBoolean());
336 }
337
338 void verifyNoNotifications() {
339 verifyNoToast();
340 verifyNoNotification();
Hugo Benichia43a0952016-08-30 10:01:15 +0900341 }
342
343 void verifyToast(NetworkAgentInfo from, NetworkAgentInfo to) {
344 verifyNoNotification();
345 verify(mNotifier, times(1)).showToast(from, to);
346 }
347
348 void verifyNotification(NetworkAgentInfo from, NetworkAgentInfo to) {
349 verifyNoToast();
350 verify(mNotifier, times(1)).showNotification(eq(from.network.netId),
351 eq(NotificationType.NETWORK_SWITCH), eq(from), eq(to), any(), eq(true));
352 }
353
354 NetworkAgentInfo nai(int netId, int transport, int networkType, String networkTypeName) {
355 NetworkInfo info = new NetworkInfo(networkType, 0, networkTypeName, "");
356 NetworkCapabilities caps = new NetworkCapabilities();
357 caps.addCapability(0);
358 caps.addTransportType(transport);
359 NetworkAgentInfo nai = new NetworkAgentInfo(null, null, new Network(netId), info, null,
Chalard Jean08577fc2018-05-02 21:14:54 +0900360 caps, 50, mCtx, null, mMisc, mConnService, mNetd, mNMS,
361 NetworkFactory.SerialNumber.NONE);
Hugo Benichia43a0952016-08-30 10:01:15 +0900362 nai.everValidated = true;
363 return nai;
364 }
365
366 NetworkAgentInfo wifiNai(int netId) {
367 return nai(netId, NetworkCapabilities.TRANSPORT_WIFI,
368 ConnectivityManager.TYPE_WIFI, WIFI);
369 }
370
371 NetworkAgentInfo cellNai(int netId) {
372 return nai(netId, NetworkCapabilities.TRANSPORT_CELLULAR,
373 ConnectivityManager.TYPE_MOBILE, CELLULAR);
374 }
375
376 public static class TestableLingerMonitor extends LingerMonitor {
Lorenzo Colitti84e6f1232016-08-29 14:03:11 +0900377 public TestableLingerMonitor(Context c, NetworkNotificationManager n, int l, long r) {
378 super(c, n, l, r);
Hugo Benichia43a0952016-08-30 10:01:15 +0900379 }
380 @Override protected PendingIntent createNotificationIntent() {
381 return null;
382 }
383 }
384}