Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(241)

Side by Side Diff: net/android/javatests/src/org/chromium/net/NetworkChangeNotifierTest.java

Issue 2160263002: Use supported Android network handles in Android M and newer. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: comment parseInt Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2012 The Chromium Authors. All rights reserved. 1 // Copyright 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 package org.chromium.net; 5 package org.chromium.net;
6 6
7 import static android.net.NetworkCapabilities.NET_CAPABILITY_INTERNET; 7 import static android.net.NetworkCapabilities.NET_CAPABILITY_INTERNET;
8 import static android.net.NetworkCapabilities.TRANSPORT_CELLULAR; 8 import static android.net.NetworkCapabilities.TRANSPORT_CELLULAR;
9 import static android.net.NetworkCapabilities.TRANSPORT_VPN; 9 import static android.net.NetworkCapabilities.TRANSPORT_VPN;
10 import static android.net.NetworkCapabilities.TRANSPORT_WIFI; 10 import static android.net.NetworkCapabilities.TRANSPORT_WIFI;
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
142 private int mNetworkSubtype; 142 private int mNetworkSubtype;
143 private NetworkCallback mLastRegisteredNetworkCallback; 143 private NetworkCallback mLastRegisteredNetworkCallback;
144 144
145 @Override 145 @Override
146 public NetworkState getNetworkState() { 146 public NetworkState getNetworkState() {
147 return new NetworkState(mActiveNetworkExists, mNetworkType, mNetwork Subtype); 147 return new NetworkState(mActiveNetworkExists, mNetworkType, mNetwork Subtype);
148 } 148 }
149 149
150 @Override 150 @Override
151 protected NetworkCapabilities getNetworkCapabilities(Network network) { 151 protected NetworkCapabilities getNetworkCapabilities(Network network) {
152 int netId = NetworkChangeNotifierAutoDetect.networkToNetId(network); 152 long netId = NetworkChangeNotifierAutoDetect.networkToNetId(network) ;
153 for (MockNetwork mockNetwork : mMockNetworks) { 153 for (MockNetwork mockNetwork : mMockNetworks) {
154 if (netId == mockNetwork.mNetId) { 154 if (netId == mockNetwork.mNetId) {
155 return mockNetwork.getCapabilities(); 155 return mockNetwork.getCapabilities();
156 } 156 }
157 } 157 }
158 return null; 158 return null;
159 } 159 }
160 160
161 @Override 161 @Override
162 protected boolean vpnAccessible(Network network) { 162 protected boolean vpnAccessible(Network network) {
163 int netId = NetworkChangeNotifierAutoDetect.networkToNetId(network); 163 long netId = NetworkChangeNotifierAutoDetect.networkToNetId(network) ;
164 for (MockNetwork mockNetwork : mMockNetworks) { 164 for (MockNetwork mockNetwork : mMockNetworks) {
165 if (netId == mockNetwork.mNetId) { 165 if (netId == mockNetwork.mNetId) {
166 return mockNetwork.mVpnAccessible; 166 return mockNetwork.mVpnAccessible;
167 } 167 }
168 } 168 }
169 return false; 169 return false;
170 } 170 }
171 171
172 @Override 172 @Override
173 protected Network[] getAllNetworksUnfiltered() { 173 protected Network[] getAllNetworksUnfiltered() {
174 Network[] networks = new Network[mMockNetworks.size()]; 174 Network[] networks = new Network[mMockNetworks.size()];
175 for (int i = 0; i < networks.length; i++) { 175 for (int i = 0; i < networks.length; i++) {
176 networks[i] = netIdToNetwork(mMockNetworks.get(i).mNetId); 176 networks[i] = netIdToNetwork(mMockNetworks.get(i).mNetId);
177 } 177 }
178 return networks; 178 return networks;
179 } 179 }
180 180
181 // Dummy implementations to avoid NullPointerExceptions in default imple mentations: 181 // Dummy implementations to avoid NullPointerExceptions in default imple mentations:
182 182
183 @Override 183 @Override
184 public int getDefaultNetId() { 184 public long getDefaultNetId() {
185 return NetId.INVALID; 185 return NetId.INVALID;
186 } 186 }
187 187
188 @Override 188 @Override
189 public NetworkState getNetworkState(Network network) { 189 public NetworkState getNetworkState(Network network) {
190 return new NetworkState(false, -1, -1); 190 return new NetworkState(false, -1, -1);
191 } 191 }
192 192
193 @Override 193 @Override
194 public void unregisterNetworkCallback(NetworkCallback networkCallback) { } 194 public void unregisterNetworkCallback(NetworkCallback networkCallback) { }
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
271 271
272 // Types of network changes. Each is associated with a NetworkChangeNotifier AutoDetect.Observer 272 // Types of network changes. Each is associated with a NetworkChangeNotifier AutoDetect.Observer
273 // callback, and NONE is provided to indicate no callback observed. 273 // callback, and NONE is provided to indicate no callback observed.
274 private static enum ChangeType { NONE, CONNECT, SOON_TO_DISCONNECT, DISCONNE CT, PURGE_LIST } 274 private static enum ChangeType { NONE, CONNECT, SOON_TO_DISCONNECT, DISCONNE CT, PURGE_LIST }
275 275
276 // Recorded information about a network change that took place. 276 // Recorded information about a network change that took place.
277 private static class ChangeInfo { 277 private static class ChangeInfo {
278 // The type of change. 278 // The type of change.
279 final ChangeType mChangeType; 279 final ChangeType mChangeType;
280 // The network identifier of the network changing. 280 // The network identifier of the network changing.
281 final int mNetId; 281 final long mNetId;
282 282
283 /** 283 /**
284 * @param changeType the type of change. 284 * @param changeType the type of change.
285 * @param netId the network identifier of the network changing. 285 * @param netId the network identifier of the network changing.
286 */ 286 */
287 ChangeInfo(ChangeType changeType, int netId) { 287 ChangeInfo(ChangeType changeType, long netId) {
288 mChangeType = changeType; 288 mChangeType = changeType;
289 mNetId = netId; 289 mNetId = netId;
290 } 290 }
291 } 291 }
292 292
293 // NetworkChangeNotifierAutoDetect.Observer used to verify proper notificati ons are sent out. 293 // NetworkChangeNotifierAutoDetect.Observer used to verify proper notificati ons are sent out.
294 // Notifications come back on UI thread. assertLastChange() called on test t hread. 294 // Notifications come back on UI thread. assertLastChange() called on test t hread.
295 private static class TestNetworkChangeNotifierAutoDetectObserver 295 private static class TestNetworkChangeNotifierAutoDetectObserver
296 implements NetworkChangeNotifierAutoDetect.Observer { 296 implements NetworkChangeNotifierAutoDetect.Observer {
297 // The list of network changes that have been witnessed. 297 // The list of network changes that have been witnessed.
298 final ArrayList<ChangeInfo> mChanges = new ArrayList<ChangeInfo>(); 298 final ArrayList<ChangeInfo> mChanges = new ArrayList<ChangeInfo>();
299 299
300 @Override 300 @Override
301 public void onConnectionTypeChanged(int newConnectionType) {} 301 public void onConnectionTypeChanged(int newConnectionType) {}
302 @Override 302 @Override
303 public void onMaxBandwidthChanged(double maxBandwidthMbps) {} 303 public void onMaxBandwidthChanged(double maxBandwidthMbps) {}
304 304
305 @Override 305 @Override
306 public void onNetworkConnect(int netId, int connectionType) { 306 public void onNetworkConnect(long netId, int connectionType) {
307 ThreadUtils.assertOnUiThread(); 307 ThreadUtils.assertOnUiThread();
308 mChanges.add(new ChangeInfo(ChangeType.CONNECT, netId)); 308 mChanges.add(new ChangeInfo(ChangeType.CONNECT, netId));
309 } 309 }
310 310
311 @Override 311 @Override
312 public void onNetworkSoonToDisconnect(int netId) { 312 public void onNetworkSoonToDisconnect(long netId) {
313 ThreadUtils.assertOnUiThread(); 313 ThreadUtils.assertOnUiThread();
314 mChanges.add(new ChangeInfo(ChangeType.SOON_TO_DISCONNECT, netId)); 314 mChanges.add(new ChangeInfo(ChangeType.SOON_TO_DISCONNECT, netId));
315 } 315 }
316 316
317 @Override 317 @Override
318 public void onNetworkDisconnect(int netId) { 318 public void onNetworkDisconnect(long netId) {
319 ThreadUtils.assertOnUiThread(); 319 ThreadUtils.assertOnUiThread();
320 mChanges.add(new ChangeInfo(ChangeType.DISCONNECT, netId)); 320 mChanges.add(new ChangeInfo(ChangeType.DISCONNECT, netId));
321 } 321 }
322 322
323 @Override 323 @Override
324 public void purgeActiveNetworkList(int[] activeNetIds) { 324 public void purgeActiveNetworkList(long[] activeNetIds) {
325 ThreadUtils.assertOnUiThread(); 325 ThreadUtils.assertOnUiThread();
326 if (activeNetIds.length == 1) { 326 if (activeNetIds.length == 1) {
327 mChanges.add(new ChangeInfo(ChangeType.PURGE_LIST, activeNetIds[ 0])); 327 mChanges.add(new ChangeInfo(ChangeType.PURGE_LIST, activeNetIds[ 0]));
328 } else { 328 } else {
329 mChanges.add(new ChangeInfo(ChangeType.PURGE_LIST, NetId.INVALID )); 329 mChanges.add(new ChangeInfo(ChangeType.PURGE_LIST, NetId.INVALID ));
330 } 330 }
331 } 331 }
332 332
333 // Verify last notification was the expected one. 333 // Verify last notification was the expected one.
334 public void assertLastChange(ChangeType type, int netId) throws Exceptio n { 334 public void assertLastChange(ChangeType type, int netId) throws Exceptio n {
(...skipping 424 matching lines...) Expand 10 before | Expand all | Expand 10 after
759 ncn.setConnectivityManagerDelegateForTests(new ConnectivityManagerDelega te() { 759 ncn.setConnectivityManagerDelegateForTests(new ConnectivityManagerDelega te() {
760 public final Network[] mNetworks = 760 public final Network[] mNetworks =
761 new Network[] {netIdToNetwork(111), netIdToNetwork(333)}; 761 new Network[] {netIdToNetwork(111), netIdToNetwork(333)};
762 762
763 @Override 763 @Override
764 protected Network[] getAllNetworksUnfiltered() { 764 protected Network[] getAllNetworksUnfiltered() {
765 return mNetworks; 765 return mNetworks;
766 } 766 }
767 767
768 @Override 768 @Override
769 int getDefaultNetId() { 769 long getDefaultNetId() {
770 return Integer.parseInt(mNetworks[1].toString()); 770 return Integer.parseInt(mNetworks[1].toString());
771 } 771 }
772 772
773 @Override 773 @Override
774 protected NetworkCapabilities getNetworkCapabilities(Network network ) { 774 protected NetworkCapabilities getNetworkCapabilities(Network network ) {
775 return getCapabilities(TRANSPORT_WIFI); 775 return getCapabilities(TRANSPORT_WIFI);
776 } 776 }
777 777
778 @Override 778 @Override
779 public NetworkState getNetworkState(Network network) { 779 public NetworkState getNetworkState(Network network) {
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after
927 mConnectivityDelegate.setActiveNetworkExists(true); 927 mConnectivityDelegate.setActiveNetworkExists(true);
928 mConnectivityDelegate.setNetworkType(i); 928 mConnectivityDelegate.setNetworkType(i);
929 mReceiver.onReceive(getInstrumentation().getTargetContext(), intent) ; 929 mReceiver.onReceive(getInstrumentation().getTargetContext(), intent) ;
930 assertTrue(NetworkChangeNotifier.isOnline()); 930 assertTrue(NetworkChangeNotifier.isOnline());
931 } 931 }
932 mConnectivityDelegate.setActiveNetworkExists(false); 932 mConnectivityDelegate.setActiveNetworkExists(false);
933 mReceiver.onReceive(getInstrumentation().getTargetContext(), intent); 933 mReceiver.onReceive(getInstrumentation().getTargetContext(), intent);
934 assertFalse(NetworkChangeNotifier.isOnline()); 934 assertFalse(NetworkChangeNotifier.isOnline());
935 } 935 }
936 } 936 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698