blob: bce5787ed9a5facd02e5b1d5a6aee6c7f800183a [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
19import android.app.PendingIntent;
20import android.content.Context;
21import android.content.res.Resources;
22import android.net.ConnectivityManager;
23import android.net.Network;
24import android.net.NetworkCapabilities;
25import android.net.NetworkInfo;
26import android.net.NetworkMisc;
Lorenzo Colitti84e6f1232016-08-29 14:03:11 +090027import android.text.format.DateUtils;
Hugo Benichia43a0952016-08-30 10:01:15 +090028import com.android.internal.R;
29import com.android.server.ConnectivityService;
30import com.android.server.connectivity.NetworkNotificationManager;
31import com.android.server.connectivity.NetworkNotificationManager.NotificationType;
32import junit.framework.TestCase;
33import org.mockito.Mock;
34import org.mockito.MockitoAnnotations;
35
36import static org.mockito.Mockito.any;
37import static org.mockito.Mockito.anyBoolean;
38import static org.mockito.Mockito.anyInt;
39import static org.mockito.Mockito.eq;
40import static org.mockito.Mockito.never;
41import static org.mockito.Mockito.times;
42import static org.mockito.Mockito.verify;
43import static org.mockito.Mockito.when;
Lorenzo Colitti84e6f1232016-08-29 14:03:11 +090044import static org.mockito.Mockito.reset;
Hugo Benichia43a0952016-08-30 10:01:15 +090045
46public class LingerMonitorTest extends TestCase {
47 static final String CELLULAR = "CELLULAR";
48 static final String WIFI = "WIFI";
49
Lorenzo Colitti84e6f1232016-08-29 14:03:11 +090050 static final long LOW_RATE_LIMIT = DateUtils.MINUTE_IN_MILLIS;
51 static final long HIGH_RATE_LIMIT = 0;
52
53 static final int LOW_DAILY_LIMIT = 2;
54 static final int HIGH_DAILY_LIMIT = 1000;
55
Hugo Benichia43a0952016-08-30 10:01:15 +090056 LingerMonitor mMonitor;
57
58 @Mock ConnectivityService mConnService;
59 @Mock Context mCtx;
60 @Mock NetworkMisc mMisc;
61 @Mock NetworkNotificationManager mNotifier;
62 @Mock Resources mResources;
63
64 public void setUp() {
65 MockitoAnnotations.initMocks(this);
66 when(mCtx.getResources()).thenReturn(mResources);
67 when(mCtx.getPackageName()).thenReturn("com.android.server.connectivity");
68 when(mConnService.createNetworkMonitor(any(), any(), any(), any())).thenReturn(null);
69
Lorenzo Colitti84e6f1232016-08-29 14:03:11 +090070 mMonitor = new TestableLingerMonitor(mCtx, mNotifier, HIGH_DAILY_LIMIT, HIGH_RATE_LIMIT);
Hugo Benichia43a0952016-08-30 10:01:15 +090071 }
72
73 public void testTransitions() {
74 setNotificationSwitch(transition(WIFI, CELLULAR));
75 NetworkAgentInfo nai1 = wifiNai(100);
76 NetworkAgentInfo nai2 = cellNai(101);
77
78 assertTrue(mMonitor.isNotificationEnabled(nai1, nai2));
79 assertFalse(mMonitor.isNotificationEnabled(nai2, nai1));
80 }
81
82 public void testNotificationOnLinger() {
83 setNotificationSwitch(transition(WIFI, CELLULAR));
84 setNotificationType(LingerMonitor.NOTIFY_TYPE_NOTIFICATION);
85 NetworkAgentInfo from = wifiNai(100);
86 NetworkAgentInfo to = cellNai(101);
87
88 mMonitor.noteLingerDefaultNetwork(from, to);
89 verifyNotification(from, to);
90 }
91
92 public void testToastOnLinger() {
93 setNotificationSwitch(transition(WIFI, CELLULAR));
94 setNotificationType(LingerMonitor.NOTIFY_TYPE_TOAST);
95 NetworkAgentInfo from = wifiNai(100);
96 NetworkAgentInfo to = cellNai(101);
97
98 mMonitor.noteLingerDefaultNetwork(from, to);
99 verifyToast(from, to);
100 }
101
102 public void testNotificationClearedAfterDisconnect() {
103 setNotificationSwitch(transition(WIFI, CELLULAR));
104 setNotificationType(LingerMonitor.NOTIFY_TYPE_NOTIFICATION);
105 NetworkAgentInfo from = wifiNai(100);
106 NetworkAgentInfo to = cellNai(101);
107
108 mMonitor.noteLingerDefaultNetwork(from, to);
109 verifyNotification(from, to);
110
111 mMonitor.noteDisconnect(to);
112 verify(mNotifier, times(1)).clearNotification(100);
113 }
114
115 public void testNotificationClearedAfterSwitchingBack() {
116 setNotificationSwitch(transition(WIFI, CELLULAR));
117 setNotificationType(LingerMonitor.NOTIFY_TYPE_NOTIFICATION);
118 NetworkAgentInfo from = wifiNai(100);
119 NetworkAgentInfo to = cellNai(101);
120
121 mMonitor.noteLingerDefaultNetwork(from, to);
122 verifyNotification(from, to);
123
124 mMonitor.noteLingerDefaultNetwork(to, from);
125 verify(mNotifier, times(1)).clearNotification(100);
126 }
127
128 public void testUniqueToast() {
129 setNotificationSwitch(transition(WIFI, CELLULAR));
130 setNotificationType(LingerMonitor.NOTIFY_TYPE_TOAST);
131 NetworkAgentInfo from = wifiNai(100);
132 NetworkAgentInfo to = cellNai(101);
133
134 mMonitor.noteLingerDefaultNetwork(from, to);
135 verifyToast(from, to);
136
137 mMonitor.noteLingerDefaultNetwork(to, from);
138 verify(mNotifier, times(1)).clearNotification(100);
139
Lorenzo Colitti84e6f1232016-08-29 14:03:11 +0900140 reset(mNotifier);
Hugo Benichia43a0952016-08-30 10:01:15 +0900141 mMonitor.noteLingerDefaultNetwork(from, to);
Lorenzo Colitti84e6f1232016-08-29 14:03:11 +0900142 verifyNoNotifications();
143 }
144
145 public void testMultipleNotifications() {
146 setNotificationSwitch(transition(WIFI, CELLULAR));
147 setNotificationType(LingerMonitor.NOTIFY_TYPE_NOTIFICATION);
148 NetworkAgentInfo wifi1 = wifiNai(100);
149 NetworkAgentInfo wifi2 = wifiNai(101);
150 NetworkAgentInfo cell = cellNai(102);
151
152 mMonitor.noteLingerDefaultNetwork(wifi1, cell);
153 verifyNotification(wifi1, cell);
154
155 mMonitor.noteLingerDefaultNetwork(cell, wifi2);
156 verify(mNotifier, times(1)).clearNotification(100);
157
158 reset(mNotifier);
159 mMonitor.noteLingerDefaultNetwork(wifi2, cell);
160 verifyNotification(wifi2, cell);
161 }
162
163 public void testRateLimiting() throws InterruptedException {
164 mMonitor = new TestableLingerMonitor(mCtx, mNotifier, HIGH_DAILY_LIMIT, LOW_RATE_LIMIT);
165
166 setNotificationSwitch(transition(WIFI, CELLULAR));
167 setNotificationType(LingerMonitor.NOTIFY_TYPE_NOTIFICATION);
168 NetworkAgentInfo wifi1 = wifiNai(100);
169 NetworkAgentInfo wifi2 = wifiNai(101);
170 NetworkAgentInfo wifi3 = wifiNai(102);
171 NetworkAgentInfo cell = cellNai(103);
172
173 mMonitor.noteLingerDefaultNetwork(wifi1, cell);
174 verifyNotification(wifi1, cell);
175 reset(mNotifier);
176
177 Thread.sleep(50);
178 mMonitor.noteLingerDefaultNetwork(cell, wifi2);
179 mMonitor.noteLingerDefaultNetwork(wifi2, cell);
180 verifyNoNotifications();
181
182 Thread.sleep(50);
183 mMonitor.noteLingerDefaultNetwork(cell, wifi3);
184 mMonitor.noteLingerDefaultNetwork(wifi3, cell);
185 verifyNoNotifications();
186 }
187
188 public void testDailyLimiting() throws InterruptedException {
189 mMonitor = new TestableLingerMonitor(mCtx, mNotifier, LOW_DAILY_LIMIT, HIGH_RATE_LIMIT);
190
191 setNotificationSwitch(transition(WIFI, CELLULAR));
192 setNotificationType(LingerMonitor.NOTIFY_TYPE_NOTIFICATION);
193 NetworkAgentInfo wifi1 = wifiNai(100);
194 NetworkAgentInfo wifi2 = wifiNai(101);
195 NetworkAgentInfo wifi3 = wifiNai(102);
196 NetworkAgentInfo cell = cellNai(103);
197
198 mMonitor.noteLingerDefaultNetwork(wifi1, cell);
199 verifyNotification(wifi1, cell);
200 reset(mNotifier);
201
202 Thread.sleep(50);
203 mMonitor.noteLingerDefaultNetwork(cell, wifi2);
204 mMonitor.noteLingerDefaultNetwork(wifi2, cell);
205 verifyNotification(wifi2, cell);
206 reset(mNotifier);
207
208 Thread.sleep(50);
209 mMonitor.noteLingerDefaultNetwork(cell, wifi3);
210 mMonitor.noteLingerDefaultNetwork(wifi3, cell);
211 verifyNoNotifications();
Hugo Benichia43a0952016-08-30 10:01:15 +0900212 }
213
214 public void testUniqueNotification() {
215 setNotificationSwitch(transition(WIFI, CELLULAR));
216 setNotificationType(LingerMonitor.NOTIFY_TYPE_NOTIFICATION);
217 NetworkAgentInfo from = wifiNai(100);
218 NetworkAgentInfo to = cellNai(101);
219
220 mMonitor.noteLingerDefaultNetwork(from, to);
221 verifyNotification(from, to);
222
223 mMonitor.noteLingerDefaultNetwork(to, from);
224 verify(mNotifier, times(1)).clearNotification(100);
225
226 mMonitor.noteLingerDefaultNetwork(from, to);
227 verifyNotification(from, to);
228 }
229
Lorenzo Colitti84e6f1232016-08-29 14:03:11 +0900230 public void testIgnoreNeverValidatedNetworks() {
Hugo Benichia43a0952016-08-30 10:01:15 +0900231 setNotificationType(LingerMonitor.NOTIFY_TYPE_TOAST);
232 setNotificationSwitch(transition(WIFI, CELLULAR));
233 NetworkAgentInfo from = wifiNai(100);
234 NetworkAgentInfo to = cellNai(101);
235 from.everValidated = false;
236
237 mMonitor.noteLingerDefaultNetwork(from, to);
238 verifyNoNotifications();
239 }
240
Lorenzo Colitti84e6f1232016-08-29 14:03:11 +0900241 public void testIgnoreCurrentlyValidatedNetworks() {
242 setNotificationType(LingerMonitor.NOTIFY_TYPE_TOAST);
243 setNotificationSwitch(transition(WIFI, CELLULAR));
244 NetworkAgentInfo from = wifiNai(100);
245 NetworkAgentInfo to = cellNai(101);
246 from.lastValidated = true;
247
248 mMonitor.noteLingerDefaultNetwork(from, to);
249 verifyNoNotifications();
250 }
251
Hugo Benichia43a0952016-08-30 10:01:15 +0900252 public void testNoNotificationType() {
253 setNotificationType(LingerMonitor.NOTIFY_TYPE_TOAST);
254 setNotificationSwitch();
255 NetworkAgentInfo from = wifiNai(100);
256 NetworkAgentInfo to = cellNai(101);
257
258 mMonitor.noteLingerDefaultNetwork(from, to);
259 verifyNoNotifications();
260 }
261
262 public void testNoTransitionToNotify() {
263 setNotificationType(LingerMonitor.NOTIFY_TYPE_NONE);
264 setNotificationSwitch(transition(WIFI, CELLULAR));
265 NetworkAgentInfo from = wifiNai(100);
266 NetworkAgentInfo to = cellNai(101);
267
268 mMonitor.noteLingerDefaultNetwork(from, to);
269 verifyNoNotifications();
270 }
271
272 public void testDifferentTransitionToNotify() {
273 setNotificationType(LingerMonitor.NOTIFY_TYPE_TOAST);
274 setNotificationSwitch(transition(CELLULAR, WIFI));
275 NetworkAgentInfo from = wifiNai(100);
276 NetworkAgentInfo to = cellNai(101);
277
278 mMonitor.noteLingerDefaultNetwork(from, to);
279 verifyNoNotifications();
280 }
281
282 void setNotificationSwitch(String... transitions) {
283 when(mResources.getStringArray(R.array.config_networkNotifySwitches))
284 .thenReturn(transitions);
285 }
286
287 String transition(String from, String to) {
288 return from + "-" + to;
289 }
290
291 void setNotificationType(int type) {
292 when(mResources.getInteger(R.integer.config_networkNotifySwitchType)).thenReturn(type);
293 }
294
295 void verifyNoToast() {
296 verify(mNotifier, never()).showToast(any(), any());
297 }
298
299 void verifyNoNotification() {
300 verify(mNotifier, never())
301 .showNotification(anyInt(), any(), any(), any(), any(), anyBoolean());
302 }
303
304 void verifyNoNotifications() {
305 verifyNoToast();
306 verifyNoNotification();
Hugo Benichia43a0952016-08-30 10:01:15 +0900307 }
308
309 void verifyToast(NetworkAgentInfo from, NetworkAgentInfo to) {
310 verifyNoNotification();
311 verify(mNotifier, times(1)).showToast(from, to);
312 }
313
314 void verifyNotification(NetworkAgentInfo from, NetworkAgentInfo to) {
315 verifyNoToast();
316 verify(mNotifier, times(1)).showNotification(eq(from.network.netId),
317 eq(NotificationType.NETWORK_SWITCH), eq(from), eq(to), any(), eq(true));
318 }
319
320 NetworkAgentInfo nai(int netId, int transport, int networkType, String networkTypeName) {
321 NetworkInfo info = new NetworkInfo(networkType, 0, networkTypeName, "");
322 NetworkCapabilities caps = new NetworkCapabilities();
323 caps.addCapability(0);
324 caps.addTransportType(transport);
325 NetworkAgentInfo nai = new NetworkAgentInfo(null, null, new Network(netId), info, null,
326 caps, 50, mCtx, null, mMisc, null, mConnService);
327 nai.everValidated = true;
328 return nai;
329 }
330
331 NetworkAgentInfo wifiNai(int netId) {
332 return nai(netId, NetworkCapabilities.TRANSPORT_WIFI,
333 ConnectivityManager.TYPE_WIFI, WIFI);
334 }
335
336 NetworkAgentInfo cellNai(int netId) {
337 return nai(netId, NetworkCapabilities.TRANSPORT_CELLULAR,
338 ConnectivityManager.TYPE_MOBILE, CELLULAR);
339 }
340
341 public static class TestableLingerMonitor extends LingerMonitor {
Lorenzo Colitti84e6f1232016-08-29 14:03:11 +0900342 public TestableLingerMonitor(Context c, NetworkNotificationManager n, int l, long r) {
343 super(c, n, l, r);
Hugo Benichia43a0952016-08-30 10:01:15 +0900344 }
345 @Override protected PendingIntent createNotificationIntent() {
346 return null;
347 }
348 }
349}