blob: 6de4aa1be1eabf2e9e8e5df0028049db7e8c57ba [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;
Lorenzo Colitti9307ca22019-01-12 01:54:23 +090041import android.os.INetworkManagementService;
Hugo Benichi4a0c5d72017-10-11 11:26:25 +090042import android.text.format.DateUtils;
43
Brett Chabot1ae2aa62019-03-04 14:14:56 -080044import androidx.test.filters.SmallTest;
45import androidx.test.runner.AndroidJUnit4;
46
Hugo Benichi4a0c5d72017-10-11 11:26:25 +090047import 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;
78
Hugo Benichi4a0c5d72017-10-11 11:26:25 +090079 @Before
Hugo Benichia43a0952016-08-30 10:01:15 +090080 public void setUp() {
81 MockitoAnnotations.initMocks(this);
82 when(mCtx.getResources()).thenReturn(mResources);
83 when(mCtx.getPackageName()).thenReturn("com.android.server.connectivity");
Hugo Benichia43a0952016-08-30 10:01:15 +090084
Lorenzo Colitti84e6f1232016-08-29 14:03:11 +090085 mMonitor = new TestableLingerMonitor(mCtx, mNotifier, HIGH_DAILY_LIMIT, HIGH_RATE_LIMIT);
Hugo Benichia43a0952016-08-30 10:01:15 +090086 }
87
Hugo Benichi4a0c5d72017-10-11 11:26:25 +090088 @Test
Hugo Benichia43a0952016-08-30 10:01:15 +090089 public void testTransitions() {
90 setNotificationSwitch(transition(WIFI, CELLULAR));
91 NetworkAgentInfo nai1 = wifiNai(100);
92 NetworkAgentInfo nai2 = cellNai(101);
93
94 assertTrue(mMonitor.isNotificationEnabled(nai1, nai2));
95 assertFalse(mMonitor.isNotificationEnabled(nai2, nai1));
96 }
97
Hugo Benichi4a0c5d72017-10-11 11:26:25 +090098 @Test
Hugo Benichia43a0952016-08-30 10:01:15 +090099 public void testNotificationOnLinger() {
100 setNotificationSwitch(transition(WIFI, CELLULAR));
101 setNotificationType(LingerMonitor.NOTIFY_TYPE_NOTIFICATION);
102 NetworkAgentInfo from = wifiNai(100);
103 NetworkAgentInfo to = cellNai(101);
104
105 mMonitor.noteLingerDefaultNetwork(from, to);
106 verifyNotification(from, to);
107 }
108
Hugo Benichi4a0c5d72017-10-11 11:26:25 +0900109 @Test
Hugo Benichia43a0952016-08-30 10:01:15 +0900110 public void testToastOnLinger() {
111 setNotificationSwitch(transition(WIFI, CELLULAR));
112 setNotificationType(LingerMonitor.NOTIFY_TYPE_TOAST);
113 NetworkAgentInfo from = wifiNai(100);
114 NetworkAgentInfo to = cellNai(101);
115
116 mMonitor.noteLingerDefaultNetwork(from, to);
117 verifyToast(from, to);
118 }
119
Hugo Benichi4a0c5d72017-10-11 11:26:25 +0900120 @Test
Hugo Benichia43a0952016-08-30 10:01:15 +0900121 public void testNotificationClearedAfterDisconnect() {
122 setNotificationSwitch(transition(WIFI, CELLULAR));
123 setNotificationType(LingerMonitor.NOTIFY_TYPE_NOTIFICATION);
124 NetworkAgentInfo from = wifiNai(100);
125 NetworkAgentInfo to = cellNai(101);
126
127 mMonitor.noteLingerDefaultNetwork(from, to);
128 verifyNotification(from, to);
129
130 mMonitor.noteDisconnect(to);
131 verify(mNotifier, times(1)).clearNotification(100);
132 }
133
Hugo Benichi4a0c5d72017-10-11 11:26:25 +0900134 @Test
Hugo Benichia43a0952016-08-30 10:01:15 +0900135 public void testNotificationClearedAfterSwitchingBack() {
136 setNotificationSwitch(transition(WIFI, CELLULAR));
137 setNotificationType(LingerMonitor.NOTIFY_TYPE_NOTIFICATION);
138 NetworkAgentInfo from = wifiNai(100);
139 NetworkAgentInfo to = cellNai(101);
140
141 mMonitor.noteLingerDefaultNetwork(from, to);
142 verifyNotification(from, to);
143
144 mMonitor.noteLingerDefaultNetwork(to, from);
145 verify(mNotifier, times(1)).clearNotification(100);
146 }
147
Hugo Benichi4a0c5d72017-10-11 11:26:25 +0900148 @Test
Hugo Benichia43a0952016-08-30 10:01:15 +0900149 public void testUniqueToast() {
150 setNotificationSwitch(transition(WIFI, CELLULAR));
151 setNotificationType(LingerMonitor.NOTIFY_TYPE_TOAST);
152 NetworkAgentInfo from = wifiNai(100);
153 NetworkAgentInfo to = cellNai(101);
154
155 mMonitor.noteLingerDefaultNetwork(from, to);
156 verifyToast(from, to);
157
158 mMonitor.noteLingerDefaultNetwork(to, from);
159 verify(mNotifier, times(1)).clearNotification(100);
160
Lorenzo Colitti84e6f1232016-08-29 14:03:11 +0900161 reset(mNotifier);
Hugo Benichia43a0952016-08-30 10:01:15 +0900162 mMonitor.noteLingerDefaultNetwork(from, to);
Lorenzo Colitti84e6f1232016-08-29 14:03:11 +0900163 verifyNoNotifications();
164 }
165
Hugo Benichi4a0c5d72017-10-11 11:26:25 +0900166 @Test
Lorenzo Colitti84e6f1232016-08-29 14:03:11 +0900167 public void testMultipleNotifications() {
168 setNotificationSwitch(transition(WIFI, CELLULAR));
169 setNotificationType(LingerMonitor.NOTIFY_TYPE_NOTIFICATION);
170 NetworkAgentInfo wifi1 = wifiNai(100);
171 NetworkAgentInfo wifi2 = wifiNai(101);
172 NetworkAgentInfo cell = cellNai(102);
173
174 mMonitor.noteLingerDefaultNetwork(wifi1, cell);
175 verifyNotification(wifi1, cell);
176
177 mMonitor.noteLingerDefaultNetwork(cell, wifi2);
178 verify(mNotifier, times(1)).clearNotification(100);
179
180 reset(mNotifier);
181 mMonitor.noteLingerDefaultNetwork(wifi2, cell);
182 verifyNotification(wifi2, cell);
183 }
184
Hugo Benichi4a0c5d72017-10-11 11:26:25 +0900185 @Test
Lorenzo Colitti84e6f1232016-08-29 14:03:11 +0900186 public void testRateLimiting() throws InterruptedException {
187 mMonitor = new TestableLingerMonitor(mCtx, mNotifier, HIGH_DAILY_LIMIT, LOW_RATE_LIMIT);
188
189 setNotificationSwitch(transition(WIFI, CELLULAR));
190 setNotificationType(LingerMonitor.NOTIFY_TYPE_NOTIFICATION);
191 NetworkAgentInfo wifi1 = wifiNai(100);
192 NetworkAgentInfo wifi2 = wifiNai(101);
193 NetworkAgentInfo wifi3 = wifiNai(102);
194 NetworkAgentInfo cell = cellNai(103);
195
196 mMonitor.noteLingerDefaultNetwork(wifi1, cell);
197 verifyNotification(wifi1, cell);
198 reset(mNotifier);
199
200 Thread.sleep(50);
201 mMonitor.noteLingerDefaultNetwork(cell, wifi2);
202 mMonitor.noteLingerDefaultNetwork(wifi2, cell);
203 verifyNoNotifications();
204
205 Thread.sleep(50);
206 mMonitor.noteLingerDefaultNetwork(cell, wifi3);
207 mMonitor.noteLingerDefaultNetwork(wifi3, cell);
208 verifyNoNotifications();
209 }
210
Hugo Benichi4a0c5d72017-10-11 11:26:25 +0900211 @Test
Lorenzo Colitti84e6f1232016-08-29 14:03:11 +0900212 public void testDailyLimiting() throws InterruptedException {
213 mMonitor = new TestableLingerMonitor(mCtx, mNotifier, LOW_DAILY_LIMIT, HIGH_RATE_LIMIT);
214
215 setNotificationSwitch(transition(WIFI, CELLULAR));
216 setNotificationType(LingerMonitor.NOTIFY_TYPE_NOTIFICATION);
217 NetworkAgentInfo wifi1 = wifiNai(100);
218 NetworkAgentInfo wifi2 = wifiNai(101);
219 NetworkAgentInfo wifi3 = wifiNai(102);
220 NetworkAgentInfo cell = cellNai(103);
221
222 mMonitor.noteLingerDefaultNetwork(wifi1, cell);
223 verifyNotification(wifi1, cell);
224 reset(mNotifier);
225
226 Thread.sleep(50);
227 mMonitor.noteLingerDefaultNetwork(cell, wifi2);
228 mMonitor.noteLingerDefaultNetwork(wifi2, cell);
229 verifyNotification(wifi2, cell);
230 reset(mNotifier);
231
232 Thread.sleep(50);
233 mMonitor.noteLingerDefaultNetwork(cell, wifi3);
234 mMonitor.noteLingerDefaultNetwork(wifi3, cell);
235 verifyNoNotifications();
Hugo Benichia43a0952016-08-30 10:01:15 +0900236 }
237
Hugo Benichi4a0c5d72017-10-11 11:26:25 +0900238 @Test
Hugo Benichia43a0952016-08-30 10:01:15 +0900239 public void testUniqueNotification() {
240 setNotificationSwitch(transition(WIFI, CELLULAR));
241 setNotificationType(LingerMonitor.NOTIFY_TYPE_NOTIFICATION);
242 NetworkAgentInfo from = wifiNai(100);
243 NetworkAgentInfo to = cellNai(101);
244
245 mMonitor.noteLingerDefaultNetwork(from, to);
246 verifyNotification(from, to);
247
248 mMonitor.noteLingerDefaultNetwork(to, from);
249 verify(mNotifier, times(1)).clearNotification(100);
250
251 mMonitor.noteLingerDefaultNetwork(from, to);
252 verifyNotification(from, to);
253 }
254
Hugo Benichi4a0c5d72017-10-11 11:26:25 +0900255 @Test
Lorenzo Colitti84e6f1232016-08-29 14:03:11 +0900256 public void testIgnoreNeverValidatedNetworks() {
Hugo Benichia43a0952016-08-30 10:01:15 +0900257 setNotificationType(LingerMonitor.NOTIFY_TYPE_TOAST);
258 setNotificationSwitch(transition(WIFI, CELLULAR));
259 NetworkAgentInfo from = wifiNai(100);
260 NetworkAgentInfo to = cellNai(101);
261 from.everValidated = false;
262
263 mMonitor.noteLingerDefaultNetwork(from, to);
264 verifyNoNotifications();
265 }
266
Hugo Benichi4a0c5d72017-10-11 11:26:25 +0900267 @Test
Lorenzo Colitti84e6f1232016-08-29 14:03:11 +0900268 public void testIgnoreCurrentlyValidatedNetworks() {
269 setNotificationType(LingerMonitor.NOTIFY_TYPE_TOAST);
270 setNotificationSwitch(transition(WIFI, CELLULAR));
271 NetworkAgentInfo from = wifiNai(100);
272 NetworkAgentInfo to = cellNai(101);
273 from.lastValidated = true;
274
275 mMonitor.noteLingerDefaultNetwork(from, to);
276 verifyNoNotifications();
277 }
278
Hugo Benichi4a0c5d72017-10-11 11:26:25 +0900279 @Test
Hugo Benichia43a0952016-08-30 10:01:15 +0900280 public void testNoNotificationType() {
281 setNotificationType(LingerMonitor.NOTIFY_TYPE_TOAST);
282 setNotificationSwitch();
283 NetworkAgentInfo from = wifiNai(100);
284 NetworkAgentInfo to = cellNai(101);
285
286 mMonitor.noteLingerDefaultNetwork(from, to);
287 verifyNoNotifications();
288 }
289
Hugo Benichi4a0c5d72017-10-11 11:26:25 +0900290 @Test
Hugo Benichia43a0952016-08-30 10:01:15 +0900291 public void testNoTransitionToNotify() {
292 setNotificationType(LingerMonitor.NOTIFY_TYPE_NONE);
293 setNotificationSwitch(transition(WIFI, CELLULAR));
294 NetworkAgentInfo from = wifiNai(100);
295 NetworkAgentInfo to = cellNai(101);
296
297 mMonitor.noteLingerDefaultNetwork(from, to);
298 verifyNoNotifications();
299 }
300
Hugo Benichi4a0c5d72017-10-11 11:26:25 +0900301 @Test
Hugo Benichia43a0952016-08-30 10:01:15 +0900302 public void testDifferentTransitionToNotify() {
303 setNotificationType(LingerMonitor.NOTIFY_TYPE_TOAST);
304 setNotificationSwitch(transition(CELLULAR, WIFI));
305 NetworkAgentInfo from = wifiNai(100);
306 NetworkAgentInfo to = cellNai(101);
307
308 mMonitor.noteLingerDefaultNetwork(from, to);
309 verifyNoNotifications();
310 }
311
312 void setNotificationSwitch(String... transitions) {
313 when(mResources.getStringArray(R.array.config_networkNotifySwitches))
314 .thenReturn(transitions);
315 }
316
317 String transition(String from, String to) {
318 return from + "-" + to;
319 }
320
321 void setNotificationType(int type) {
322 when(mResources.getInteger(R.integer.config_networkNotifySwitchType)).thenReturn(type);
323 }
324
325 void verifyNoToast() {
326 verify(mNotifier, never()).showToast(any(), any());
327 }
328
329 void verifyNoNotification() {
330 verify(mNotifier, never())
331 .showNotification(anyInt(), any(), any(), any(), any(), anyBoolean());
332 }
333
334 void verifyNoNotifications() {
335 verifyNoToast();
336 verifyNoNotification();
Hugo Benichia43a0952016-08-30 10:01:15 +0900337 }
338
339 void verifyToast(NetworkAgentInfo from, NetworkAgentInfo to) {
340 verifyNoNotification();
341 verify(mNotifier, times(1)).showToast(from, to);
342 }
343
344 void verifyNotification(NetworkAgentInfo from, NetworkAgentInfo to) {
345 verifyNoToast();
346 verify(mNotifier, times(1)).showNotification(eq(from.network.netId),
347 eq(NotificationType.NETWORK_SWITCH), eq(from), eq(to), any(), eq(true));
348 }
349
350 NetworkAgentInfo nai(int netId, int transport, int networkType, String networkTypeName) {
351 NetworkInfo info = new NetworkInfo(networkType, 0, networkTypeName, "");
352 NetworkCapabilities caps = new NetworkCapabilities();
353 caps.addCapability(0);
354 caps.addTransportType(transport);
355 NetworkAgentInfo nai = new NetworkAgentInfo(null, null, new Network(netId), info, null,
Chalard Jean08577fc2018-05-02 21:14:54 +0900356 caps, 50, mCtx, null, mMisc, mConnService, mNetd, mNMS,
357 NetworkFactory.SerialNumber.NONE);
Hugo Benichia43a0952016-08-30 10:01:15 +0900358 nai.everValidated = true;
359 return nai;
360 }
361
362 NetworkAgentInfo wifiNai(int netId) {
363 return nai(netId, NetworkCapabilities.TRANSPORT_WIFI,
364 ConnectivityManager.TYPE_WIFI, WIFI);
365 }
366
367 NetworkAgentInfo cellNai(int netId) {
368 return nai(netId, NetworkCapabilities.TRANSPORT_CELLULAR,
369 ConnectivityManager.TYPE_MOBILE, CELLULAR);
370 }
371
372 public static class TestableLingerMonitor extends LingerMonitor {
Lorenzo Colitti84e6f1232016-08-29 14:03:11 +0900373 public TestableLingerMonitor(Context c, NetworkNotificationManager n, int l, long r) {
374 super(c, n, l, r);
Hugo Benichia43a0952016-08-30 10:01:15 +0900375 }
376 @Override protected PendingIntent createNotificationIntent() {
377 return null;
378 }
379 }
380}