Erik Kline | 885a909 | 2017-01-16 16:27:22 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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.tethering; |
| 18 | |
Erik Kline | dd8e891 | 2017-01-18 16:08:06 +0900 | [diff] [blame] | 19 | import static android.net.ConnectivityManager.TYPE_MOBILE_DUN; |
| 20 | import static android.net.ConnectivityManager.TYPE_MOBILE_HIPRI; |
Erik Kline | 885a909 | 2017-01-16 16:27:22 +0900 | [diff] [blame] | 21 | import static android.net.NetworkCapabilities.NET_CAPABILITY_DUN; |
| 22 | import static org.junit.Assert.assertEquals; |
| 23 | import static org.junit.Assert.assertFalse; |
| 24 | import static org.junit.Assert.assertTrue; |
Erik Kline | 35eee13 | 2017-01-17 15:06:35 +0900 | [diff] [blame] | 25 | import static org.junit.Assert.fail; |
Erik Kline | 7462f46 | 2017-01-23 19:05:28 +0900 | [diff] [blame] | 26 | import static org.mockito.Mockito.any; |
| 27 | import static org.mockito.Mockito.anyInt; |
Erik Kline | 292b65a | 2017-05-12 16:52:48 +0900 | [diff] [blame] | 28 | import static org.mockito.Mockito.anyString; |
Erik Kline | 885a909 | 2017-01-16 16:27:22 +0900 | [diff] [blame] | 29 | import static org.mockito.Mockito.reset; |
Erik Kline | 7462f46 | 2017-01-23 19:05:28 +0900 | [diff] [blame] | 30 | import static org.mockito.Mockito.spy; |
| 31 | import static org.mockito.Mockito.times; |
| 32 | import static org.mockito.Mockito.verify; |
| 33 | import static org.mockito.Mockito.verifyNoMoreInteractions; |
Erik Kline | 292b65a | 2017-05-12 16:52:48 +0900 | [diff] [blame] | 34 | import static org.mockito.Mockito.when; |
Erik Kline | 885a909 | 2017-01-16 16:27:22 +0900 | [diff] [blame] | 35 | |
| 36 | import android.content.Context; |
Erik Kline | b583b03 | 2017-02-22 12:58:24 +0900 | [diff] [blame] | 37 | import android.os.Handler; |
| 38 | import android.os.Message; |
Erik Kline | 885a909 | 2017-01-16 16:27:22 +0900 | [diff] [blame] | 39 | import android.net.ConnectivityManager; |
| 40 | import android.net.ConnectivityManager.NetworkCallback; |
| 41 | import android.net.IConnectivityManager; |
| 42 | import android.net.Network; |
| 43 | import android.net.NetworkCapabilities; |
| 44 | import android.net.NetworkRequest; |
Erik Kline | 292b65a | 2017-05-12 16:52:48 +0900 | [diff] [blame] | 45 | import android.net.util.SharedLog; |
Erik Kline | 885a909 | 2017-01-16 16:27:22 +0900 | [diff] [blame] | 46 | |
| 47 | import android.support.test.filters.SmallTest; |
| 48 | import android.support.test.runner.AndroidJUnit4; |
| 49 | |
Erik Kline | b583b03 | 2017-02-22 12:58:24 +0900 | [diff] [blame] | 50 | import com.android.internal.util.State; |
| 51 | import com.android.internal.util.StateMachine; |
| 52 | |
| 53 | import org.junit.After; |
Erik Kline | 885a909 | 2017-01-16 16:27:22 +0900 | [diff] [blame] | 54 | import org.junit.Before; |
| 55 | import org.junit.runner.RunWith; |
| 56 | import org.junit.Test; |
| 57 | import org.mockito.Mock; |
Erik Kline | 7462f46 | 2017-01-23 19:05:28 +0900 | [diff] [blame] | 58 | import org.mockito.Mockito; |
Erik Kline | 885a909 | 2017-01-16 16:27:22 +0900 | [diff] [blame] | 59 | import org.mockito.MockitoAnnotations; |
| 60 | |
Erik Kline | b583b03 | 2017-02-22 12:58:24 +0900 | [diff] [blame] | 61 | import java.util.ArrayList; |
Erik Kline | 885a909 | 2017-01-16 16:27:22 +0900 | [diff] [blame] | 62 | import java.util.HashMap; |
| 63 | import java.util.HashSet; |
| 64 | import java.util.Map; |
| 65 | import java.util.Set; |
| 66 | |
| 67 | |
| 68 | @RunWith(AndroidJUnit4.class) |
| 69 | @SmallTest |
| 70 | public class UpstreamNetworkMonitorTest { |
| 71 | private static final int EVENT_UNM_UPDATE = 1; |
| 72 | |
| 73 | @Mock private Context mContext; |
| 74 | @Mock private IConnectivityManager mCS; |
Erik Kline | 292b65a | 2017-05-12 16:52:48 +0900 | [diff] [blame] | 75 | @Mock private SharedLog mLog; |
Erik Kline | 885a909 | 2017-01-16 16:27:22 +0900 | [diff] [blame] | 76 | |
Erik Kline | b583b03 | 2017-02-22 12:58:24 +0900 | [diff] [blame] | 77 | private TestStateMachine mSM; |
Erik Kline | 885a909 | 2017-01-16 16:27:22 +0900 | [diff] [blame] | 78 | private TestConnectivityManager mCM; |
| 79 | private UpstreamNetworkMonitor mUNM; |
| 80 | |
| 81 | @Before public void setUp() throws Exception { |
| 82 | MockitoAnnotations.initMocks(this); |
| 83 | reset(mContext); |
| 84 | reset(mCS); |
Erik Kline | 292b65a | 2017-05-12 16:52:48 +0900 | [diff] [blame] | 85 | reset(mLog); |
| 86 | when(mLog.forSubComponent(anyString())).thenReturn(mLog); |
Erik Kline | 885a909 | 2017-01-16 16:27:22 +0900 | [diff] [blame] | 87 | |
Erik Kline | 7462f46 | 2017-01-23 19:05:28 +0900 | [diff] [blame] | 88 | mCM = spy(new TestConnectivityManager(mContext, mCS)); |
Erik Kline | b583b03 | 2017-02-22 12:58:24 +0900 | [diff] [blame] | 89 | mSM = new TestStateMachine(); |
Erik Kline | 292b65a | 2017-05-12 16:52:48 +0900 | [diff] [blame] | 90 | mUNM = new UpstreamNetworkMonitor(mSM, EVENT_UNM_UPDATE, (ConnectivityManager) mCM, mLog); |
Erik Kline | b583b03 | 2017-02-22 12:58:24 +0900 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | @After public void tearDown() throws Exception { |
| 94 | if (mSM != null) { |
| 95 | mSM.quit(); |
| 96 | mSM = null; |
| 97 | } |
Erik Kline | 885a909 | 2017-01-16 16:27:22 +0900 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | @Test |
| 101 | public void testDoesNothingBeforeStarted() { |
Erik Kline | 35eee13 | 2017-01-17 15:06:35 +0900 | [diff] [blame] | 102 | assertTrue(mCM.hasNoCallbacks()); |
| 103 | assertFalse(mUNM.mobileNetworkRequested()); |
| 104 | |
| 105 | mUNM.updateMobileRequiresDun(true); |
| 106 | assertTrue(mCM.hasNoCallbacks()); |
| 107 | mUNM.updateMobileRequiresDun(false); |
| 108 | assertTrue(mCM.hasNoCallbacks()); |
Erik Kline | 885a909 | 2017-01-16 16:27:22 +0900 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | @Test |
| 112 | public void testDefaultNetworkIsTracked() throws Exception { |
| 113 | assertEquals(0, mCM.trackingDefault.size()); |
| 114 | |
| 115 | mUNM.start(); |
| 116 | assertEquals(1, mCM.trackingDefault.size()); |
| 117 | |
| 118 | mUNM.stop(); |
Erik Kline | 35eee13 | 2017-01-17 15:06:35 +0900 | [diff] [blame] | 119 | assertTrue(mCM.hasNoCallbacks()); |
Erik Kline | 885a909 | 2017-01-16 16:27:22 +0900 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | @Test |
Erik Kline | d2ec391 | 2017-01-25 00:53:04 +0900 | [diff] [blame] | 123 | public void testListensForAllNetworks() throws Exception { |
Erik Kline | 885a909 | 2017-01-16 16:27:22 +0900 | [diff] [blame] | 124 | assertTrue(mCM.listening.isEmpty()); |
| 125 | |
| 126 | mUNM.start(); |
| 127 | assertFalse(mCM.listening.isEmpty()); |
Erik Kline | d2ec391 | 2017-01-25 00:53:04 +0900 | [diff] [blame] | 128 | assertTrue(mCM.isListeningForAll()); |
Erik Kline | 885a909 | 2017-01-16 16:27:22 +0900 | [diff] [blame] | 129 | |
| 130 | mUNM.stop(); |
Erik Kline | 35eee13 | 2017-01-17 15:06:35 +0900 | [diff] [blame] | 131 | assertTrue(mCM.hasNoCallbacks()); |
Erik Kline | 885a909 | 2017-01-16 16:27:22 +0900 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | @Test |
Erik Kline | 35eee13 | 2017-01-17 15:06:35 +0900 | [diff] [blame] | 135 | public void testRequestsMobileNetwork() throws Exception { |
Erik Kline | 885a909 | 2017-01-16 16:27:22 +0900 | [diff] [blame] | 136 | assertFalse(mUNM.mobileNetworkRequested()); |
| 137 | assertEquals(0, mCM.requested.size()); |
| 138 | |
| 139 | mUNM.start(); |
| 140 | assertFalse(mUNM.mobileNetworkRequested()); |
| 141 | assertEquals(0, mCM.requested.size()); |
| 142 | |
Erik Kline | efdd3f4c | 2017-01-20 16:31:29 +0900 | [diff] [blame] | 143 | mUNM.updateMobileRequiresDun(false); |
Erik Kline | 885a909 | 2017-01-16 16:27:22 +0900 | [diff] [blame] | 144 | assertFalse(mUNM.mobileNetworkRequested()); |
| 145 | assertEquals(0, mCM.requested.size()); |
| 146 | |
| 147 | mUNM.registerMobileNetworkRequest(); |
| 148 | assertTrue(mUNM.mobileNetworkRequested()); |
Erik Kline | 973c7df | 2017-01-23 15:55:16 +0900 | [diff] [blame] | 149 | assertUpstreamTypeRequested(TYPE_MOBILE_HIPRI); |
Erik Kline | 885a909 | 2017-01-16 16:27:22 +0900 | [diff] [blame] | 150 | assertFalse(mCM.isDunRequested()); |
| 151 | |
| 152 | mUNM.stop(); |
| 153 | assertFalse(mUNM.mobileNetworkRequested()); |
Erik Kline | 35eee13 | 2017-01-17 15:06:35 +0900 | [diff] [blame] | 154 | assertTrue(mCM.hasNoCallbacks()); |
Erik Kline | 885a909 | 2017-01-16 16:27:22 +0900 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | @Test |
Erik Kline | 7462f46 | 2017-01-23 19:05:28 +0900 | [diff] [blame] | 158 | public void testDuplicateMobileRequestsIgnored() throws Exception { |
| 159 | assertFalse(mUNM.mobileNetworkRequested()); |
| 160 | assertEquals(0, mCM.requested.size()); |
| 161 | |
| 162 | mUNM.start(); |
| 163 | verify(mCM, Mockito.times(1)).registerNetworkCallback( |
Erik Kline | b583b03 | 2017-02-22 12:58:24 +0900 | [diff] [blame] | 164 | any(NetworkRequest.class), any(NetworkCallback.class), any(Handler.class)); |
| 165 | verify(mCM, Mockito.times(1)).registerDefaultNetworkCallback( |
| 166 | any(NetworkCallback.class), any(Handler.class)); |
Erik Kline | 7462f46 | 2017-01-23 19:05:28 +0900 | [diff] [blame] | 167 | assertFalse(mUNM.mobileNetworkRequested()); |
| 168 | assertEquals(0, mCM.requested.size()); |
| 169 | |
| 170 | mUNM.updateMobileRequiresDun(true); |
| 171 | mUNM.registerMobileNetworkRequest(); |
| 172 | verify(mCM, Mockito.times(1)).requestNetwork( |
Erik Kline | b583b03 | 2017-02-22 12:58:24 +0900 | [diff] [blame] | 173 | any(NetworkRequest.class), any(NetworkCallback.class), anyInt(), anyInt(), |
| 174 | any(Handler.class)); |
Erik Kline | 7462f46 | 2017-01-23 19:05:28 +0900 | [diff] [blame] | 175 | |
| 176 | assertTrue(mUNM.mobileNetworkRequested()); |
| 177 | assertUpstreamTypeRequested(TYPE_MOBILE_DUN); |
| 178 | assertTrue(mCM.isDunRequested()); |
| 179 | |
| 180 | // Try a few things that must not result in any state change. |
| 181 | mUNM.registerMobileNetworkRequest(); |
| 182 | mUNM.updateMobileRequiresDun(true); |
| 183 | mUNM.registerMobileNetworkRequest(); |
| 184 | |
| 185 | assertTrue(mUNM.mobileNetworkRequested()); |
| 186 | assertUpstreamTypeRequested(TYPE_MOBILE_DUN); |
| 187 | assertTrue(mCM.isDunRequested()); |
| 188 | |
| 189 | mUNM.stop(); |
| 190 | verify(mCM, times(3)).unregisterNetworkCallback(any(NetworkCallback.class)); |
| 191 | |
| 192 | verifyNoMoreInteractions(mCM); |
| 193 | } |
| 194 | |
| 195 | @Test |
Erik Kline | 35eee13 | 2017-01-17 15:06:35 +0900 | [diff] [blame] | 196 | public void testRequestsDunNetwork() throws Exception { |
Erik Kline | 885a909 | 2017-01-16 16:27:22 +0900 | [diff] [blame] | 197 | assertFalse(mUNM.mobileNetworkRequested()); |
| 198 | assertEquals(0, mCM.requested.size()); |
| 199 | |
| 200 | mUNM.start(); |
| 201 | assertFalse(mUNM.mobileNetworkRequested()); |
| 202 | assertEquals(0, mCM.requested.size()); |
| 203 | |
Erik Kline | efdd3f4c | 2017-01-20 16:31:29 +0900 | [diff] [blame] | 204 | mUNM.updateMobileRequiresDun(true); |
Erik Kline | 885a909 | 2017-01-16 16:27:22 +0900 | [diff] [blame] | 205 | assertFalse(mUNM.mobileNetworkRequested()); |
| 206 | assertEquals(0, mCM.requested.size()); |
| 207 | |
| 208 | mUNM.registerMobileNetworkRequest(); |
| 209 | assertTrue(mUNM.mobileNetworkRequested()); |
Erik Kline | 973c7df | 2017-01-23 15:55:16 +0900 | [diff] [blame] | 210 | assertUpstreamTypeRequested(TYPE_MOBILE_DUN); |
Erik Kline | 885a909 | 2017-01-16 16:27:22 +0900 | [diff] [blame] | 211 | assertTrue(mCM.isDunRequested()); |
| 212 | |
| 213 | mUNM.stop(); |
| 214 | assertFalse(mUNM.mobileNetworkRequested()); |
Erik Kline | 35eee13 | 2017-01-17 15:06:35 +0900 | [diff] [blame] | 215 | assertTrue(mCM.hasNoCallbacks()); |
Erik Kline | 885a909 | 2017-01-16 16:27:22 +0900 | [diff] [blame] | 216 | } |
| 217 | |
Erik Kline | 973c7df | 2017-01-23 15:55:16 +0900 | [diff] [blame] | 218 | @Test |
Erik Kline | 7462f46 | 2017-01-23 19:05:28 +0900 | [diff] [blame] | 219 | public void testUpdateMobileRequiresDun() throws Exception { |
Erik Kline | 973c7df | 2017-01-23 15:55:16 +0900 | [diff] [blame] | 220 | mUNM.start(); |
| 221 | |
| 222 | // Test going from no-DUN to DUN correctly re-registers callbacks. |
| 223 | mUNM.updateMobileRequiresDun(false); |
| 224 | mUNM.registerMobileNetworkRequest(); |
| 225 | assertTrue(mUNM.mobileNetworkRequested()); |
| 226 | assertUpstreamTypeRequested(TYPE_MOBILE_HIPRI); |
| 227 | assertFalse(mCM.isDunRequested()); |
| 228 | mUNM.updateMobileRequiresDun(true); |
| 229 | assertTrue(mUNM.mobileNetworkRequested()); |
| 230 | assertUpstreamTypeRequested(TYPE_MOBILE_DUN); |
| 231 | assertTrue(mCM.isDunRequested()); |
| 232 | |
| 233 | // Test going from DUN to no-DUN correctly re-registers callbacks. |
| 234 | mUNM.updateMobileRequiresDun(false); |
| 235 | assertTrue(mUNM.mobileNetworkRequested()); |
| 236 | assertUpstreamTypeRequested(TYPE_MOBILE_HIPRI); |
| 237 | assertFalse(mCM.isDunRequested()); |
| 238 | |
| 239 | mUNM.stop(); |
| 240 | assertFalse(mUNM.mobileNetworkRequested()); |
| 241 | } |
| 242 | |
| 243 | private void assertUpstreamTypeRequested(int upstreamType) throws Exception { |
| 244 | assertEquals(1, mCM.requested.size()); |
| 245 | assertEquals(1, mCM.legacyTypeMap.size()); |
| 246 | assertEquals(Integer.valueOf(upstreamType), |
| 247 | mCM.legacyTypeMap.values().iterator().next()); |
| 248 | } |
| 249 | |
Erik Kline | 7462f46 | 2017-01-23 19:05:28 +0900 | [diff] [blame] | 250 | public static class TestConnectivityManager extends ConnectivityManager { |
Erik Kline | b583b03 | 2017-02-22 12:58:24 +0900 | [diff] [blame] | 251 | public Map<NetworkCallback, Handler> allCallbacks = new HashMap<>(); |
Erik Kline | 885a909 | 2017-01-16 16:27:22 +0900 | [diff] [blame] | 252 | public Set<NetworkCallback> trackingDefault = new HashSet<>(); |
| 253 | public Map<NetworkCallback, NetworkRequest> listening = new HashMap<>(); |
| 254 | public Map<NetworkCallback, NetworkRequest> requested = new HashMap<>(); |
Erik Kline | dd8e891 | 2017-01-18 16:08:06 +0900 | [diff] [blame] | 255 | public Map<NetworkCallback, Integer> legacyTypeMap = new HashMap<>(); |
Erik Kline | 885a909 | 2017-01-16 16:27:22 +0900 | [diff] [blame] | 256 | |
| 257 | public TestConnectivityManager(Context ctx, IConnectivityManager svc) { |
| 258 | super(ctx, svc); |
| 259 | } |
| 260 | |
Erik Kline | 35eee13 | 2017-01-17 15:06:35 +0900 | [diff] [blame] | 261 | boolean hasNoCallbacks() { |
Erik Kline | b583b03 | 2017-02-22 12:58:24 +0900 | [diff] [blame] | 262 | return allCallbacks.isEmpty() && |
| 263 | trackingDefault.isEmpty() && |
Erik Kline | 885a909 | 2017-01-16 16:27:22 +0900 | [diff] [blame] | 264 | listening.isEmpty() && |
Erik Kline | dd8e891 | 2017-01-18 16:08:06 +0900 | [diff] [blame] | 265 | requested.isEmpty() && |
| 266 | legacyTypeMap.isEmpty(); |
Erik Kline | 885a909 | 2017-01-16 16:27:22 +0900 | [diff] [blame] | 267 | } |
| 268 | |
Erik Kline | d2ec391 | 2017-01-25 00:53:04 +0900 | [diff] [blame] | 269 | boolean isListeningForAll() { |
| 270 | final NetworkCapabilities empty = new NetworkCapabilities(); |
| 271 | empty.clearAll(); |
| 272 | |
Erik Kline | 885a909 | 2017-01-16 16:27:22 +0900 | [diff] [blame] | 273 | for (NetworkRequest req : listening.values()) { |
Erik Kline | d2ec391 | 2017-01-25 00:53:04 +0900 | [diff] [blame] | 274 | if (req.networkCapabilities.equalRequestableCapabilities(empty)) { |
Erik Kline | 885a909 | 2017-01-16 16:27:22 +0900 | [diff] [blame] | 275 | return true; |
| 276 | } |
| 277 | } |
| 278 | return false; |
| 279 | } |
| 280 | |
| 281 | boolean isDunRequested() { |
| 282 | for (NetworkRequest req : requested.values()) { |
| 283 | if (req.networkCapabilities.hasCapability(NET_CAPABILITY_DUN)) { |
| 284 | return true; |
| 285 | } |
| 286 | } |
| 287 | return false; |
| 288 | } |
| 289 | |
| 290 | @Override |
Erik Kline | b583b03 | 2017-02-22 12:58:24 +0900 | [diff] [blame] | 291 | public void requestNetwork(NetworkRequest req, NetworkCallback cb, Handler h) { |
| 292 | assertFalse(allCallbacks.containsKey(cb)); |
| 293 | allCallbacks.put(cb, h); |
Erik Kline | 885a909 | 2017-01-16 16:27:22 +0900 | [diff] [blame] | 294 | assertFalse(requested.containsKey(cb)); |
| 295 | requested.put(cb, req); |
| 296 | } |
| 297 | |
| 298 | @Override |
Erik Kline | b583b03 | 2017-02-22 12:58:24 +0900 | [diff] [blame] | 299 | public void requestNetwork(NetworkRequest req, NetworkCallback cb) { |
| 300 | fail("Should never be called."); |
| 301 | } |
| 302 | |
| 303 | @Override |
Erik Kline | dd8e891 | 2017-01-18 16:08:06 +0900 | [diff] [blame] | 304 | public void requestNetwork(NetworkRequest req, NetworkCallback cb, |
Erik Kline | b583b03 | 2017-02-22 12:58:24 +0900 | [diff] [blame] | 305 | int timeoutMs, int legacyType, Handler h) { |
| 306 | assertFalse(allCallbacks.containsKey(cb)); |
| 307 | allCallbacks.put(cb, h); |
Erik Kline | dd8e891 | 2017-01-18 16:08:06 +0900 | [diff] [blame] | 308 | assertFalse(requested.containsKey(cb)); |
| 309 | requested.put(cb, req); |
| 310 | assertFalse(legacyTypeMap.containsKey(cb)); |
| 311 | if (legacyType != ConnectivityManager.TYPE_NONE) { |
| 312 | legacyTypeMap.put(cb, legacyType); |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | @Override |
Erik Kline | b583b03 | 2017-02-22 12:58:24 +0900 | [diff] [blame] | 317 | public void registerNetworkCallback(NetworkRequest req, NetworkCallback cb, Handler h) { |
| 318 | assertFalse(allCallbacks.containsKey(cb)); |
| 319 | allCallbacks.put(cb, h); |
Erik Kline | 885a909 | 2017-01-16 16:27:22 +0900 | [diff] [blame] | 320 | assertFalse(listening.containsKey(cb)); |
| 321 | listening.put(cb, req); |
| 322 | } |
| 323 | |
| 324 | @Override |
Erik Kline | b583b03 | 2017-02-22 12:58:24 +0900 | [diff] [blame] | 325 | public void registerNetworkCallback(NetworkRequest req, NetworkCallback cb) { |
| 326 | fail("Should never be called."); |
| 327 | } |
| 328 | |
| 329 | @Override |
| 330 | public void registerDefaultNetworkCallback(NetworkCallback cb, Handler h) { |
| 331 | assertFalse(allCallbacks.containsKey(cb)); |
| 332 | allCallbacks.put(cb, h); |
Erik Kline | 885a909 | 2017-01-16 16:27:22 +0900 | [diff] [blame] | 333 | assertFalse(trackingDefault.contains(cb)); |
| 334 | trackingDefault.add(cb); |
| 335 | } |
| 336 | |
| 337 | @Override |
Erik Kline | b583b03 | 2017-02-22 12:58:24 +0900 | [diff] [blame] | 338 | public void registerDefaultNetworkCallback(NetworkCallback cb) { |
| 339 | fail("Should never be called."); |
| 340 | } |
| 341 | |
| 342 | @Override |
Erik Kline | 885a909 | 2017-01-16 16:27:22 +0900 | [diff] [blame] | 343 | public void unregisterNetworkCallback(NetworkCallback cb) { |
| 344 | if (trackingDefault.contains(cb)) { |
| 345 | trackingDefault.remove(cb); |
| 346 | } else if (listening.containsKey(cb)) { |
| 347 | listening.remove(cb); |
| 348 | } else if (requested.containsKey(cb)) { |
| 349 | requested.remove(cb); |
Erik Kline | dd8e891 | 2017-01-18 16:08:06 +0900 | [diff] [blame] | 350 | legacyTypeMap.remove(cb); |
Erik Kline | 35eee13 | 2017-01-17 15:06:35 +0900 | [diff] [blame] | 351 | } else { |
| 352 | fail("Unexpected callback removed"); |
Erik Kline | 885a909 | 2017-01-16 16:27:22 +0900 | [diff] [blame] | 353 | } |
Erik Kline | b583b03 | 2017-02-22 12:58:24 +0900 | [diff] [blame] | 354 | allCallbacks.remove(cb); |
Erik Kline | 885a909 | 2017-01-16 16:27:22 +0900 | [diff] [blame] | 355 | |
Erik Kline | b583b03 | 2017-02-22 12:58:24 +0900 | [diff] [blame] | 356 | assertFalse(allCallbacks.containsKey(cb)); |
Erik Kline | 885a909 | 2017-01-16 16:27:22 +0900 | [diff] [blame] | 357 | assertFalse(trackingDefault.contains(cb)); |
| 358 | assertFalse(listening.containsKey(cb)); |
| 359 | assertFalse(requested.containsKey(cb)); |
| 360 | } |
| 361 | } |
Erik Kline | b583b03 | 2017-02-22 12:58:24 +0900 | [diff] [blame] | 362 | |
| 363 | public static class TestStateMachine extends StateMachine { |
| 364 | public final ArrayList<Message> messages = new ArrayList<>(); |
| 365 | private final State mLoggingState = new LoggingState(); |
| 366 | |
| 367 | class LoggingState extends State { |
| 368 | @Override public void enter() { messages.clear(); } |
| 369 | |
| 370 | @Override public void exit() { messages.clear(); } |
| 371 | |
| 372 | @Override public boolean processMessage(Message msg) { |
| 373 | messages.add(msg); |
| 374 | return true; |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | public TestStateMachine() { |
| 379 | super("UpstreamNetworkMonitor.TestStateMachine"); |
| 380 | addState(mLoggingState); |
| 381 | setInitialState(mLoggingState); |
| 382 | super.start(); |
| 383 | } |
| 384 | } |
Erik Kline | 885a909 | 2017-01-16 16:27:22 +0900 | [diff] [blame] | 385 | } |