blob: 354cf2f2239e2ea5ad7036a17b3fc38d2eecfd90 [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;
26import static org.mockito.Mockito.times;
27import static org.mockito.Mockito.verify;
28import static org.mockito.Mockito.when;
Lorenzo Colitti84e6f1232016-08-29 14:03:11 +090029import static org.mockito.Mockito.reset;
Hugo Benichia43a0952016-08-30 10:01:15 +090030
Hugo Benichi4a0c5d72017-10-11 11:26:25 +090031import android.app.PendingIntent;
32import android.content.Context;
33import android.content.res.Resources;
34import android.net.ConnectivityManager;
35import android.net.Network;
36import android.net.NetworkCapabilities;
37import android.net.NetworkInfo;
38import android.net.NetworkMisc;
39import android.support.test.runner.AndroidJUnit4;
40import android.support.test.filters.SmallTest;
41import android.text.format.DateUtils;
42
43import com.android.internal.R;
44import com.android.server.ConnectivityService;
45import com.android.server.connectivity.NetworkNotificationManager;
46import com.android.server.connectivity.NetworkNotificationManager.NotificationType;
47
48import org.junit.runner.RunWith;
49import org.junit.Before;
50import org.junit.Test;
51import org.mockito.Mock;
52import org.mockito.MockitoAnnotations;
53
54@RunWith(AndroidJUnit4.class)
55@SmallTest
56public class LingerMonitorTest {
Hugo Benichia43a0952016-08-30 10:01:15 +090057 static final String CELLULAR = "CELLULAR";
58 static final String WIFI = "WIFI";
59
Lorenzo Colitti84e6f1232016-08-29 14:03:11 +090060 static final long LOW_RATE_LIMIT = DateUtils.MINUTE_IN_MILLIS;
61 static final long HIGH_RATE_LIMIT = 0;
62
63 static final int LOW_DAILY_LIMIT = 2;
64 static final int HIGH_DAILY_LIMIT = 1000;
65
Hugo Benichia43a0952016-08-30 10:01:15 +090066 LingerMonitor mMonitor;
67
68 @Mock ConnectivityService mConnService;
69 @Mock Context mCtx;
70 @Mock NetworkMisc mMisc;
71 @Mock NetworkNotificationManager mNotifier;
72 @Mock Resources mResources;
73
Hugo Benichi4a0c5d72017-10-11 11:26:25 +090074 @Before
Hugo Benichia43a0952016-08-30 10:01:15 +090075 public void setUp() {
76 MockitoAnnotations.initMocks(this);
77 when(mCtx.getResources()).thenReturn(mResources);
78 when(mCtx.getPackageName()).thenReturn("com.android.server.connectivity");
79 when(mConnService.createNetworkMonitor(any(), any(), any(), any())).thenReturn(null);
80
Lorenzo Colitti84e6f1232016-08-29 14:03:11 +090081 mMonitor = new TestableLingerMonitor(mCtx, mNotifier, HIGH_DAILY_LIMIT, HIGH_RATE_LIMIT);
Hugo Benichia43a0952016-08-30 10:01:15 +090082 }
83
Hugo Benichi4a0c5d72017-10-11 11:26:25 +090084 @Test
Hugo Benichia43a0952016-08-30 10:01:15 +090085 public void testTransitions() {
86 setNotificationSwitch(transition(WIFI, CELLULAR));
87 NetworkAgentInfo nai1 = wifiNai(100);
88 NetworkAgentInfo nai2 = cellNai(101);
89
90 assertTrue(mMonitor.isNotificationEnabled(nai1, nai2));
91 assertFalse(mMonitor.isNotificationEnabled(nai2, nai1));
92 }
93
Hugo Benichi4a0c5d72017-10-11 11:26:25 +090094 @Test
Hugo Benichia43a0952016-08-30 10:01:15 +090095 public void testNotificationOnLinger() {
96 setNotificationSwitch(transition(WIFI, CELLULAR));
97 setNotificationType(LingerMonitor.NOTIFY_TYPE_NOTIFICATION);
98 NetworkAgentInfo from = wifiNai(100);
99 NetworkAgentInfo to = cellNai(101);
100
101 mMonitor.noteLingerDefaultNetwork(from, to);
102 verifyNotification(from, to);
103 }
104
Hugo Benichi4a0c5d72017-10-11 11:26:25 +0900105 @Test
Hugo Benichia43a0952016-08-30 10:01:15 +0900106 public void testToastOnLinger() {
107 setNotificationSwitch(transition(WIFI, CELLULAR));
108 setNotificationType(LingerMonitor.NOTIFY_TYPE_TOAST);
109 NetworkAgentInfo from = wifiNai(100);
110 NetworkAgentInfo to = cellNai(101);
111
112 mMonitor.noteLingerDefaultNetwork(from, to);
113 verifyToast(from, to);
114 }
115
Hugo Benichi4a0c5d72017-10-11 11:26:25 +0900116 @Test
Hugo Benichia43a0952016-08-30 10:01:15 +0900117 public void testNotificationClearedAfterDisconnect() {
118 setNotificationSwitch(transition(WIFI, CELLULAR));
119 setNotificationType(LingerMonitor.NOTIFY_TYPE_NOTIFICATION);
120 NetworkAgentInfo from = wifiNai(100);
121 NetworkAgentInfo to = cellNai(101);
122
123 mMonitor.noteLingerDefaultNetwork(from, to);
124 verifyNotification(from, to);
125
126 mMonitor.noteDisconnect(to);
127 verify(mNotifier, times(1)).clearNotification(100);
128 }
129
Hugo Benichi4a0c5d72017-10-11 11:26:25 +0900130 @Test
Hugo Benichia43a0952016-08-30 10:01:15 +0900131 public void testNotificationClearedAfterSwitchingBack() {
132 setNotificationSwitch(transition(WIFI, CELLULAR));
133 setNotificationType(LingerMonitor.NOTIFY_TYPE_NOTIFICATION);
134 NetworkAgentInfo from = wifiNai(100);
135 NetworkAgentInfo to = cellNai(101);
136
137 mMonitor.noteLingerDefaultNetwork(from, to);
138 verifyNotification(from, to);
139
140 mMonitor.noteLingerDefaultNetwork(to, from);
141 verify(mNotifier, times(1)).clearNotification(100);
142 }
143
Hugo Benichi4a0c5d72017-10-11 11:26:25 +0900144 @Test
Hugo Benichia43a0952016-08-30 10:01:15 +0900145 public void testUniqueToast() {
146 setNotificationSwitch(transition(WIFI, CELLULAR));
147 setNotificationType(LingerMonitor.NOTIFY_TYPE_TOAST);
148 NetworkAgentInfo from = wifiNai(100);
149 NetworkAgentInfo to = cellNai(101);
150
151 mMonitor.noteLingerDefaultNetwork(from, to);
152 verifyToast(from, to);
153
154 mMonitor.noteLingerDefaultNetwork(to, from);
155 verify(mNotifier, times(1)).clearNotification(100);
156
Lorenzo Colitti84e6f1232016-08-29 14:03:11 +0900157 reset(mNotifier);
Hugo Benichia43a0952016-08-30 10:01:15 +0900158 mMonitor.noteLingerDefaultNetwork(from, to);
Lorenzo Colitti84e6f1232016-08-29 14:03:11 +0900159 verifyNoNotifications();
160 }
161
Hugo Benichi4a0c5d72017-10-11 11:26:25 +0900162 @Test
Lorenzo Colitti84e6f1232016-08-29 14:03:11 +0900163 public void testMultipleNotifications() {
164 setNotificationSwitch(transition(WIFI, CELLULAR));
165 setNotificationType(LingerMonitor.NOTIFY_TYPE_NOTIFICATION);
166 NetworkAgentInfo wifi1 = wifiNai(100);
167 NetworkAgentInfo wifi2 = wifiNai(101);
168 NetworkAgentInfo cell = cellNai(102);
169
170 mMonitor.noteLingerDefaultNetwork(wifi1, cell);
171 verifyNotification(wifi1, cell);
172
173 mMonitor.noteLingerDefaultNetwork(cell, wifi2);
174 verify(mNotifier, times(1)).clearNotification(100);
175
176 reset(mNotifier);
177 mMonitor.noteLingerDefaultNetwork(wifi2, cell);
178 verifyNotification(wifi2, cell);
179 }
180
Hugo Benichi4a0c5d72017-10-11 11:26:25 +0900181 @Test
Lorenzo Colitti84e6f1232016-08-29 14:03:11 +0900182 public void testRateLimiting() throws InterruptedException {
183 mMonitor = new TestableLingerMonitor(mCtx, mNotifier, HIGH_DAILY_LIMIT, LOW_RATE_LIMIT);
184
185 setNotificationSwitch(transition(WIFI, CELLULAR));
186 setNotificationType(LingerMonitor.NOTIFY_TYPE_NOTIFICATION);
187 NetworkAgentInfo wifi1 = wifiNai(100);
188 NetworkAgentInfo wifi2 = wifiNai(101);
189 NetworkAgentInfo wifi3 = wifiNai(102);
190 NetworkAgentInfo cell = cellNai(103);
191
192 mMonitor.noteLingerDefaultNetwork(wifi1, cell);
193 verifyNotification(wifi1, cell);
194 reset(mNotifier);
195
196 Thread.sleep(50);
197 mMonitor.noteLingerDefaultNetwork(cell, wifi2);
198 mMonitor.noteLingerDefaultNetwork(wifi2, cell);
199 verifyNoNotifications();
200
201 Thread.sleep(50);
202 mMonitor.noteLingerDefaultNetwork(cell, wifi3);
203 mMonitor.noteLingerDefaultNetwork(wifi3, cell);
204 verifyNoNotifications();
205 }
206
Hugo Benichi4a0c5d72017-10-11 11:26:25 +0900207 @Test
Lorenzo Colitti84e6f1232016-08-29 14:03:11 +0900208 public void testDailyLimiting() throws InterruptedException {
209 mMonitor = new TestableLingerMonitor(mCtx, mNotifier, LOW_DAILY_LIMIT, HIGH_RATE_LIMIT);
210
211 setNotificationSwitch(transition(WIFI, CELLULAR));
212 setNotificationType(LingerMonitor.NOTIFY_TYPE_NOTIFICATION);
213 NetworkAgentInfo wifi1 = wifiNai(100);
214 NetworkAgentInfo wifi2 = wifiNai(101);
215 NetworkAgentInfo wifi3 = wifiNai(102);
216 NetworkAgentInfo cell = cellNai(103);
217
218 mMonitor.noteLingerDefaultNetwork(wifi1, cell);
219 verifyNotification(wifi1, cell);
220 reset(mNotifier);
221
222 Thread.sleep(50);
223 mMonitor.noteLingerDefaultNetwork(cell, wifi2);
224 mMonitor.noteLingerDefaultNetwork(wifi2, cell);
225 verifyNotification(wifi2, cell);
226 reset(mNotifier);
227
228 Thread.sleep(50);
229 mMonitor.noteLingerDefaultNetwork(cell, wifi3);
230 mMonitor.noteLingerDefaultNetwork(wifi3, cell);
231 verifyNoNotifications();
Hugo Benichia43a0952016-08-30 10:01:15 +0900232 }
233
Hugo Benichi4a0c5d72017-10-11 11:26:25 +0900234 @Test
Hugo Benichia43a0952016-08-30 10:01:15 +0900235 public void testUniqueNotification() {
236 setNotificationSwitch(transition(WIFI, CELLULAR));
237 setNotificationType(LingerMonitor.NOTIFY_TYPE_NOTIFICATION);
238 NetworkAgentInfo from = wifiNai(100);
239 NetworkAgentInfo to = cellNai(101);
240
241 mMonitor.noteLingerDefaultNetwork(from, to);
242 verifyNotification(from, to);
243
244 mMonitor.noteLingerDefaultNetwork(to, from);
245 verify(mNotifier, times(1)).clearNotification(100);
246
247 mMonitor.noteLingerDefaultNetwork(from, to);
248 verifyNotification(from, to);
249 }
250
Hugo Benichi4a0c5d72017-10-11 11:26:25 +0900251 @Test
Lorenzo Colitti84e6f1232016-08-29 14:03:11 +0900252 public void testIgnoreNeverValidatedNetworks() {
Hugo Benichia43a0952016-08-30 10:01:15 +0900253 setNotificationType(LingerMonitor.NOTIFY_TYPE_TOAST);
254 setNotificationSwitch(transition(WIFI, CELLULAR));
255 NetworkAgentInfo from = wifiNai(100);
256 NetworkAgentInfo to = cellNai(101);
257 from.everValidated = false;
258
259 mMonitor.noteLingerDefaultNetwork(from, to);
260 verifyNoNotifications();
261 }
262
Hugo Benichi4a0c5d72017-10-11 11:26:25 +0900263 @Test
Lorenzo Colitti84e6f1232016-08-29 14:03:11 +0900264 public void testIgnoreCurrentlyValidatedNetworks() {
265 setNotificationType(LingerMonitor.NOTIFY_TYPE_TOAST);
266 setNotificationSwitch(transition(WIFI, CELLULAR));
267 NetworkAgentInfo from = wifiNai(100);
268 NetworkAgentInfo to = cellNai(101);
269 from.lastValidated = true;
270
271 mMonitor.noteLingerDefaultNetwork(from, to);
272 verifyNoNotifications();
273 }
274
Hugo Benichi4a0c5d72017-10-11 11:26:25 +0900275 @Test
Hugo Benichia43a0952016-08-30 10:01:15 +0900276 public void testNoNotificationType() {
277 setNotificationType(LingerMonitor.NOTIFY_TYPE_TOAST);
278 setNotificationSwitch();
279 NetworkAgentInfo from = wifiNai(100);
280 NetworkAgentInfo to = cellNai(101);
281
282 mMonitor.noteLingerDefaultNetwork(from, to);
283 verifyNoNotifications();
284 }
285
Hugo Benichi4a0c5d72017-10-11 11:26:25 +0900286 @Test
Hugo Benichia43a0952016-08-30 10:01:15 +0900287 public void testNoTransitionToNotify() {
288 setNotificationType(LingerMonitor.NOTIFY_TYPE_NONE);
289 setNotificationSwitch(transition(WIFI, CELLULAR));
290 NetworkAgentInfo from = wifiNai(100);
291 NetworkAgentInfo to = cellNai(101);
292
293 mMonitor.noteLingerDefaultNetwork(from, to);
294 verifyNoNotifications();
295 }
296
Hugo Benichi4a0c5d72017-10-11 11:26:25 +0900297 @Test
Hugo Benichia43a0952016-08-30 10:01:15 +0900298 public void testDifferentTransitionToNotify() {
299 setNotificationType(LingerMonitor.NOTIFY_TYPE_TOAST);
300 setNotificationSwitch(transition(CELLULAR, WIFI));
301 NetworkAgentInfo from = wifiNai(100);
302 NetworkAgentInfo to = cellNai(101);
303
304 mMonitor.noteLingerDefaultNetwork(from, to);
305 verifyNoNotifications();
306 }
307
308 void setNotificationSwitch(String... transitions) {
309 when(mResources.getStringArray(R.array.config_networkNotifySwitches))
310 .thenReturn(transitions);
311 }
312
313 String transition(String from, String to) {
314 return from + "-" + to;
315 }
316
317 void setNotificationType(int type) {
318 when(mResources.getInteger(R.integer.config_networkNotifySwitchType)).thenReturn(type);
319 }
320
321 void verifyNoToast() {
322 verify(mNotifier, never()).showToast(any(), any());
323 }
324
325 void verifyNoNotification() {
326 verify(mNotifier, never())
327 .showNotification(anyInt(), any(), any(), any(), any(), anyBoolean());
328 }
329
330 void verifyNoNotifications() {
331 verifyNoToast();
332 verifyNoNotification();
Hugo Benichia43a0952016-08-30 10:01:15 +0900333 }
334
335 void verifyToast(NetworkAgentInfo from, NetworkAgentInfo to) {
336 verifyNoNotification();
337 verify(mNotifier, times(1)).showToast(from, to);
338 }
339
340 void verifyNotification(NetworkAgentInfo from, NetworkAgentInfo to) {
341 verifyNoToast();
342 verify(mNotifier, times(1)).showNotification(eq(from.network.netId),
343 eq(NotificationType.NETWORK_SWITCH), eq(from), eq(to), any(), eq(true));
344 }
345
346 NetworkAgentInfo nai(int netId, int transport, int networkType, String networkTypeName) {
347 NetworkInfo info = new NetworkInfo(networkType, 0, networkTypeName, "");
348 NetworkCapabilities caps = new NetworkCapabilities();
349 caps.addCapability(0);
350 caps.addTransportType(transport);
351 NetworkAgentInfo nai = new NetworkAgentInfo(null, null, new Network(netId), info, null,
352 caps, 50, mCtx, null, mMisc, null, mConnService);
353 nai.everValidated = true;
354 return nai;
355 }
356
357 NetworkAgentInfo wifiNai(int netId) {
358 return nai(netId, NetworkCapabilities.TRANSPORT_WIFI,
359 ConnectivityManager.TYPE_WIFI, WIFI);
360 }
361
362 NetworkAgentInfo cellNai(int netId) {
363 return nai(netId, NetworkCapabilities.TRANSPORT_CELLULAR,
364 ConnectivityManager.TYPE_MOBILE, CELLULAR);
365 }
366
367 public static class TestableLingerMonitor extends LingerMonitor {
Lorenzo Colitti84e6f1232016-08-29 14:03:11 +0900368 public TestableLingerMonitor(Context c, NetworkNotificationManager n, int l, long r) {
369 super(c, n, l, r);
Hugo Benichia43a0952016-08-30 10:01:15 +0900370 }
371 @Override protected PendingIntent createNotificationIntent() {
372 return null;
373 }
374 }
375}