Hugo Benichi | a43a095 | 2016-08-30 10:01:15 +0900 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server.connectivity; |
| 18 | |
Hugo Benichi | 4a0c5d7 | 2017-10-11 11:26:25 +0900 | [diff] [blame] | 19 | import static org.junit.Assert.assertFalse; |
| 20 | import static org.junit.Assert.assertTrue; |
Hugo Benichi | a43a095 | 2016-08-30 10:01:15 +0900 | [diff] [blame] | 21 | import static org.mockito.Mockito.any; |
| 22 | import static org.mockito.Mockito.anyBoolean; |
| 23 | import static org.mockito.Mockito.anyInt; |
| 24 | import static org.mockito.Mockito.eq; |
| 25 | import static org.mockito.Mockito.never; |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 26 | import static org.mockito.Mockito.reset; |
Hugo Benichi | a43a095 | 2016-08-30 10:01:15 +0900 | [diff] [blame] | 27 | import static org.mockito.Mockito.times; |
| 28 | import static org.mockito.Mockito.verify; |
| 29 | import static org.mockito.Mockito.when; |
| 30 | |
Hugo Benichi | 4a0c5d7 | 2017-10-11 11:26:25 +0900 | [diff] [blame] | 31 | import android.app.PendingIntent; |
| 32 | import android.content.Context; |
| 33 | import android.content.res.Resources; |
| 34 | import android.net.ConnectivityManager; |
Lorenzo Colitti | 9307ca2 | 2019-01-12 01:54:23 +0900 | [diff] [blame] | 35 | import android.net.INetd; |
Hugo Benichi | 4a0c5d7 | 2017-10-11 11:26:25 +0900 | [diff] [blame] | 36 | import android.net.Network; |
| 37 | import android.net.NetworkCapabilities; |
Chalard Jean | 08577fc | 2018-05-02 21:14:54 +0900 | [diff] [blame] | 38 | import android.net.NetworkFactory; |
Hugo Benichi | 4a0c5d7 | 2017-10-11 11:26:25 +0900 | [diff] [blame] | 39 | import android.net.NetworkInfo; |
| 40 | import android.net.NetworkMisc; |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 41 | import android.net.NetworkStack; |
Lorenzo Colitti | 9307ca2 | 2019-01-12 01:54:23 +0900 | [diff] [blame] | 42 | import android.os.INetworkManagementService; |
Hugo Benichi | 4a0c5d7 | 2017-10-11 11:26:25 +0900 | [diff] [blame] | 43 | import android.support.test.filters.SmallTest; |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 44 | import android.support.test.runner.AndroidJUnit4; |
Hugo Benichi | 4a0c5d7 | 2017-10-11 11:26:25 +0900 | [diff] [blame] | 45 | import android.text.format.DateUtils; |
| 46 | |
| 47 | import com.android.internal.R; |
| 48 | import com.android.server.ConnectivityService; |
Hugo Benichi | 4a0c5d7 | 2017-10-11 11:26:25 +0900 | [diff] [blame] | 49 | import com.android.server.connectivity.NetworkNotificationManager.NotificationType; |
| 50 | |
Hugo Benichi | 4a0c5d7 | 2017-10-11 11:26:25 +0900 | [diff] [blame] | 51 | import org.junit.Before; |
| 52 | import org.junit.Test; |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 53 | import org.junit.runner.RunWith; |
Hugo Benichi | 4a0c5d7 | 2017-10-11 11:26:25 +0900 | [diff] [blame] | 54 | import org.mockito.Mock; |
| 55 | import org.mockito.MockitoAnnotations; |
| 56 | |
| 57 | @RunWith(AndroidJUnit4.class) |
| 58 | @SmallTest |
| 59 | public class LingerMonitorTest { |
Hugo Benichi | a43a095 | 2016-08-30 10:01:15 +0900 | [diff] [blame] | 60 | static final String CELLULAR = "CELLULAR"; |
| 61 | static final String WIFI = "WIFI"; |
| 62 | |
Lorenzo Colitti | 84e6f123 | 2016-08-29 14:03:11 +0900 | [diff] [blame] | 63 | 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 Benichi | a43a095 | 2016-08-30 10:01:15 +0900 | [diff] [blame] | 69 | LingerMonitor mMonitor; |
| 70 | |
| 71 | @Mock ConnectivityService mConnService; |
Lorenzo Colitti | 9307ca2 | 2019-01-12 01:54:23 +0900 | [diff] [blame] | 72 | @Mock INetd mNetd; |
| 73 | @Mock INetworkManagementService mNMS; |
Hugo Benichi | a43a095 | 2016-08-30 10:01:15 +0900 | [diff] [blame] | 74 | @Mock Context mCtx; |
| 75 | @Mock NetworkMisc mMisc; |
| 76 | @Mock NetworkNotificationManager mNotifier; |
| 77 | @Mock Resources mResources; |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 78 | @Mock NetworkStack mNetworkStack; |
Hugo Benichi | a43a095 | 2016-08-30 10:01:15 +0900 | [diff] [blame] | 79 | |
Hugo Benichi | 4a0c5d7 | 2017-10-11 11:26:25 +0900 | [diff] [blame] | 80 | @Before |
Hugo Benichi | a43a095 | 2016-08-30 10:01:15 +0900 | [diff] [blame] | 81 | public void setUp() { |
| 82 | MockitoAnnotations.initMocks(this); |
| 83 | when(mCtx.getResources()).thenReturn(mResources); |
| 84 | when(mCtx.getPackageName()).thenReturn("com.android.server.connectivity"); |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 85 | when(mCtx.getSystemServiceName(NetworkStack.class)) |
| 86 | .thenReturn(Context.NETWORK_STACK_SERVICE); |
| 87 | when(mCtx.getSystemService(Context.NETWORK_STACK_SERVICE)).thenReturn(mNetworkStack); |
Hugo Benichi | a43a095 | 2016-08-30 10:01:15 +0900 | [diff] [blame] | 88 | |
Lorenzo Colitti | 84e6f123 | 2016-08-29 14:03:11 +0900 | [diff] [blame] | 89 | mMonitor = new TestableLingerMonitor(mCtx, mNotifier, HIGH_DAILY_LIMIT, HIGH_RATE_LIMIT); |
Hugo Benichi | a43a095 | 2016-08-30 10:01:15 +0900 | [diff] [blame] | 90 | } |
| 91 | |
Hugo Benichi | 4a0c5d7 | 2017-10-11 11:26:25 +0900 | [diff] [blame] | 92 | @Test |
Hugo Benichi | a43a095 | 2016-08-30 10:01:15 +0900 | [diff] [blame] | 93 | 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 Benichi | 4a0c5d7 | 2017-10-11 11:26:25 +0900 | [diff] [blame] | 102 | @Test |
Hugo Benichi | a43a095 | 2016-08-30 10:01:15 +0900 | [diff] [blame] | 103 | 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 Benichi | 4a0c5d7 | 2017-10-11 11:26:25 +0900 | [diff] [blame] | 113 | @Test |
Hugo Benichi | a43a095 | 2016-08-30 10:01:15 +0900 | [diff] [blame] | 114 | 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 Benichi | 4a0c5d7 | 2017-10-11 11:26:25 +0900 | [diff] [blame] | 124 | @Test |
Hugo Benichi | a43a095 | 2016-08-30 10:01:15 +0900 | [diff] [blame] | 125 | 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 Benichi | 4a0c5d7 | 2017-10-11 11:26:25 +0900 | [diff] [blame] | 138 | @Test |
Hugo Benichi | a43a095 | 2016-08-30 10:01:15 +0900 | [diff] [blame] | 139 | 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 Benichi | 4a0c5d7 | 2017-10-11 11:26:25 +0900 | [diff] [blame] | 152 | @Test |
Hugo Benichi | a43a095 | 2016-08-30 10:01:15 +0900 | [diff] [blame] | 153 | 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 Colitti | 84e6f123 | 2016-08-29 14:03:11 +0900 | [diff] [blame] | 165 | reset(mNotifier); |
Hugo Benichi | a43a095 | 2016-08-30 10:01:15 +0900 | [diff] [blame] | 166 | mMonitor.noteLingerDefaultNetwork(from, to); |
Lorenzo Colitti | 84e6f123 | 2016-08-29 14:03:11 +0900 | [diff] [blame] | 167 | verifyNoNotifications(); |
| 168 | } |
| 169 | |
Hugo Benichi | 4a0c5d7 | 2017-10-11 11:26:25 +0900 | [diff] [blame] | 170 | @Test |
Lorenzo Colitti | 84e6f123 | 2016-08-29 14:03:11 +0900 | [diff] [blame] | 171 | 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 Benichi | 4a0c5d7 | 2017-10-11 11:26:25 +0900 | [diff] [blame] | 189 | @Test |
Lorenzo Colitti | 84e6f123 | 2016-08-29 14:03:11 +0900 | [diff] [blame] | 190 | 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 Benichi | 4a0c5d7 | 2017-10-11 11:26:25 +0900 | [diff] [blame] | 215 | @Test |
Lorenzo Colitti | 84e6f123 | 2016-08-29 14:03:11 +0900 | [diff] [blame] | 216 | 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 Benichi | a43a095 | 2016-08-30 10:01:15 +0900 | [diff] [blame] | 240 | } |
| 241 | |
Hugo Benichi | 4a0c5d7 | 2017-10-11 11:26:25 +0900 | [diff] [blame] | 242 | @Test |
Hugo Benichi | a43a095 | 2016-08-30 10:01:15 +0900 | [diff] [blame] | 243 | 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 Benichi | 4a0c5d7 | 2017-10-11 11:26:25 +0900 | [diff] [blame] | 259 | @Test |
Lorenzo Colitti | 84e6f123 | 2016-08-29 14:03:11 +0900 | [diff] [blame] | 260 | public void testIgnoreNeverValidatedNetworks() { |
Hugo Benichi | a43a095 | 2016-08-30 10:01:15 +0900 | [diff] [blame] | 261 | 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 Benichi | 4a0c5d7 | 2017-10-11 11:26:25 +0900 | [diff] [blame] | 271 | @Test |
Lorenzo Colitti | 84e6f123 | 2016-08-29 14:03:11 +0900 | [diff] [blame] | 272 | 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 Benichi | 4a0c5d7 | 2017-10-11 11:26:25 +0900 | [diff] [blame] | 283 | @Test |
Hugo Benichi | a43a095 | 2016-08-30 10:01:15 +0900 | [diff] [blame] | 284 | 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 Benichi | 4a0c5d7 | 2017-10-11 11:26:25 +0900 | [diff] [blame] | 294 | @Test |
Hugo Benichi | a43a095 | 2016-08-30 10:01:15 +0900 | [diff] [blame] | 295 | 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 Benichi | 4a0c5d7 | 2017-10-11 11:26:25 +0900 | [diff] [blame] | 305 | @Test |
Hugo Benichi | a43a095 | 2016-08-30 10:01:15 +0900 | [diff] [blame] | 306 | 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 Benichi | a43a095 | 2016-08-30 10:01:15 +0900 | [diff] [blame] | 341 | } |
| 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 Jean | 08577fc | 2018-05-02 21:14:54 +0900 | [diff] [blame] | 360 | caps, 50, mCtx, null, mMisc, mConnService, mNetd, mNMS, |
| 361 | NetworkFactory.SerialNumber.NONE); |
Hugo Benichi | a43a095 | 2016-08-30 10:01:15 +0900 | [diff] [blame] | 362 | 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 Colitti | 84e6f123 | 2016-08-29 14:03:11 +0900 | [diff] [blame] | 377 | public TestableLingerMonitor(Context c, NetworkNotificationManager n, int l, long r) { |
| 378 | super(c, n, l, r); |
Hugo Benichi | a43a095 | 2016-08-30 10:01:15 +0900 | [diff] [blame] | 379 | } |
| 380 | @Override protected PendingIntent createNotificationIntent() { |
| 381 | return null; |
| 382 | } |
| 383 | } |
| 384 | } |