| OLD | NEW |
| 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 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 * @param vpnAccessible Is this VPN accessible to the current user? | 137 * @param vpnAccessible Is this VPN accessible to the current user? |
| 138 */ | 138 */ |
| 139 MockNetwork(int netId, int transport, boolean vpnAccessible) { | 139 MockNetwork(int netId, int transport, boolean vpnAccessible) { |
| 140 mNetId = netId; | 140 mNetId = netId; |
| 141 mTransport = transport; | 141 mTransport = transport; |
| 142 mVpnAccessible = vpnAccessible; | 142 mVpnAccessible = vpnAccessible; |
| 143 } | 143 } |
| 144 } | 144 } |
| 145 | 145 |
| 146 // List of networks we're pretending are currently connected. | 146 // List of networks we're pretending are currently connected. |
| 147 private final ArrayList<MockNetwork> mMockNetworks = new ArrayList<MockN
etwork>(); | 147 private final ArrayList<MockNetwork> mMockNetworks = new ArrayList<>(); |
| 148 | 148 |
| 149 private boolean mActiveNetworkExists; | 149 private boolean mActiveNetworkExists; |
| 150 private int mNetworkType; | 150 private int mNetworkType; |
| 151 private int mNetworkSubtype; | 151 private int mNetworkSubtype; |
| 152 private NetworkCallback mLastRegisteredNetworkCallback; | 152 private NetworkCallback mLastRegisteredNetworkCallback; |
| 153 | 153 |
| 154 @Override | 154 @Override |
| 155 public NetworkState getNetworkState() { | 155 public NetworkState getNetworkState() { |
| 156 return new NetworkState(mActiveNetworkExists, mNetworkType, mNetwork
Subtype); | 156 return new NetworkState(mActiveNetworkExists, mNetworkType, mNetwork
Subtype); |
| 157 } | 157 } |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 297 mChangeType = changeType; | 297 mChangeType = changeType; |
| 298 mNetId = demungeNetId(netId); | 298 mNetId = demungeNetId(netId); |
| 299 } | 299 } |
| 300 } | 300 } |
| 301 | 301 |
| 302 // NetworkChangeNotifierAutoDetect.Observer used to verify proper notificati
ons are sent out. | 302 // NetworkChangeNotifierAutoDetect.Observer used to verify proper notificati
ons are sent out. |
| 303 // Notifications come back on UI thread. assertLastChange() called on test t
hread. | 303 // Notifications come back on UI thread. assertLastChange() called on test t
hread. |
| 304 private static class TestNetworkChangeNotifierAutoDetectObserver | 304 private static class TestNetworkChangeNotifierAutoDetectObserver |
| 305 implements NetworkChangeNotifierAutoDetect.Observer { | 305 implements NetworkChangeNotifierAutoDetect.Observer { |
| 306 // The list of network changes that have been witnessed. | 306 // The list of network changes that have been witnessed. |
| 307 final ArrayList<ChangeInfo> mChanges = new ArrayList<ChangeInfo>(); | 307 final ArrayList<ChangeInfo> mChanges = new ArrayList<>(); |
| 308 | 308 |
| 309 @Override | 309 @Override |
| 310 public void onConnectionTypeChanged(int newConnectionType) {} | 310 public void onConnectionTypeChanged(int newConnectionType) {} |
| 311 @Override | 311 @Override |
| 312 public void onMaxBandwidthChanged(double maxBandwidthMbps) {} | 312 public void onMaxBandwidthChanged(double maxBandwidthMbps) {} |
| 313 | 313 |
| 314 @Override | 314 @Override |
| 315 public void onNetworkConnect(long netId, int connectionType) { | 315 public void onNetworkConnect(long netId, int connectionType) { |
| 316 ThreadUtils.assertOnUiThread(); | 316 ThreadUtils.assertOnUiThread(); |
| 317 mChanges.add(new ChangeInfo(ChangeType.CONNECT, netId)); | 317 mChanges.add(new ChangeInfo(ChangeType.CONNECT, netId)); |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 406 try { | 406 try { |
| 407 return mNetworkConstructor.newInstance(netId); | 407 return mNetworkConstructor.newInstance(netId); |
| 408 } catch (InstantiationException | InvocationTargetException | IllegalAcc
essException e) { | 408 } catch (InstantiationException | InvocationTargetException | IllegalAcc
essException e) { |
| 409 throw new IllegalStateException("Trying to create Network when not a
llowed"); | 409 throw new IllegalStateException("Trying to create Network when not a
llowed"); |
| 410 } | 410 } |
| 411 } | 411 } |
| 412 | 412 |
| 413 @Override | 413 @Override |
| 414 protected void setUp() throws Exception { | 414 protected void setUp() throws Exception { |
| 415 super.setUp(); | 415 super.setUp(); |
| 416 LibraryLoader.get(LibraryProcessType.PROCESS_BROWSER) | 416 LibraryLoader.get(LibraryProcessType.PROCESS_BROWSER).ensureInitialized(
); |
| 417 .ensureInitialized(getInstrumentation().getTargetContext()); | |
| 418 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { | 417 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { |
| 419 // Find Network.Network(int netId) using reflection. | 418 // Find Network.Network(int netId) using reflection. |
| 420 mNetworkConstructor = Network.class.getConstructor(Integer.TYPE); | 419 mNetworkConstructor = Network.class.getConstructor(Integer.TYPE); |
| 421 } | 420 } |
| 422 ThreadUtils.postOnUiThread(new Runnable() { | 421 ThreadUtils.postOnUiThread(new Runnable() { |
| 422 @Override |
| 423 public void run() { | 423 public void run() { |
| 424 createTestNotifier(WatchForChanges.ONLY_WHEN_APP_IN_FOREGROUND); | 424 createTestNotifier(WatchForChanges.ONLY_WHEN_APP_IN_FOREGROUND); |
| 425 } | 425 } |
| 426 }); | 426 }); |
| 427 } | 427 } |
| 428 | 428 |
| 429 /** | 429 /** |
| 430 * Tests that the receiver registers for connectivity | 430 * Tests that the receiver registers for connectivity |
| 431 * broadcasts during construction when the registration policy dictates. | 431 * broadcasts during construction when the registration policy dictates. |
| 432 */ | 432 */ |
| (...skipping 339 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 772 public void testNetworkCallbacks() throws Exception { | 772 public void testNetworkCallbacks() throws Exception { |
| 773 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { | 773 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { |
| 774 return; | 774 return; |
| 775 } | 775 } |
| 776 // Setup NetworkChangeNotifierAutoDetect | 776 // Setup NetworkChangeNotifierAutoDetect |
| 777 final Context context = getInstrumentation().getTargetContext(); | 777 final Context context = getInstrumentation().getTargetContext(); |
| 778 final TestNetworkChangeNotifierAutoDetectObserver observer = | 778 final TestNetworkChangeNotifierAutoDetectObserver observer = |
| 779 new TestNetworkChangeNotifierAutoDetectObserver(); | 779 new TestNetworkChangeNotifierAutoDetectObserver(); |
| 780 Callable<NetworkChangeNotifierAutoDetect> callable = | 780 Callable<NetworkChangeNotifierAutoDetect> callable = |
| 781 new Callable<NetworkChangeNotifierAutoDetect>() { | 781 new Callable<NetworkChangeNotifierAutoDetect>() { |
| 782 @Override |
| 782 public NetworkChangeNotifierAutoDetect call() { | 783 public NetworkChangeNotifierAutoDetect call() { |
| 783 return new NetworkChangeNotifierAutoDetect( | 784 return new NetworkChangeNotifierAutoDetect( |
| 784 observer, context, new RegistrationPolicyApplica
tionStatus() { | 785 observer, context, new RegistrationPolicyApplica
tionStatus() { |
| 785 // This override prevents NetworkChangeNotif
ierAutoDetect from | 786 // This override prevents NetworkChangeNotif
ierAutoDetect from |
| 786 // registering for events right off the bat.
We'll delay this | 787 // registering for events right off the bat.
We'll delay this |
| 787 // until our MockConnectivityManagerDelegate
is first installed | 788 // until our MockConnectivityManagerDelegate
is first installed |
| 788 // to prevent inadvertent communication with
the real | 789 // to prevent inadvertent communication with
the real |
| 789 // ConnectivityManager. | 790 // ConnectivityManager. |
| 790 @Override | 791 @Override |
| 791 int getApplicationState() { | 792 int getApplicationState() { |
| 792 return ApplicationState.HAS_PAUSED_ACTIV
ITIES; | 793 return ApplicationState.HAS_PAUSED_ACTIV
ITIES; |
| 793 } | 794 } |
| 794 }); | 795 }); |
| 795 } | 796 } |
| 796 }; | 797 }; |
| 797 FutureTask<NetworkChangeNotifierAutoDetect> task = | 798 FutureTask<NetworkChangeNotifierAutoDetect> task = new FutureTask<>(call
able); |
| 798 new FutureTask<NetworkChangeNotifierAutoDetect>(callable); | |
| 799 ThreadUtils.postOnUiThread(task); | 799 ThreadUtils.postOnUiThread(task); |
| 800 NetworkChangeNotifierAutoDetect ncn = task.get(); | 800 NetworkChangeNotifierAutoDetect ncn = task.get(); |
| 801 | 801 |
| 802 // Insert mock ConnectivityDelegate | 802 // Insert mock ConnectivityDelegate |
| 803 mConnectivityDelegate = new MockConnectivityManagerDelegate(); | 803 mConnectivityDelegate = new MockConnectivityManagerDelegate(); |
| 804 ncn.setConnectivityManagerDelegateForTests(mConnectivityDelegate); | 804 ncn.setConnectivityManagerDelegateForTests(mConnectivityDelegate); |
| 805 // Now that mock ConnectivityDelegate is inserted, pretend app is foregr
ounded | 805 // Now that mock ConnectivityDelegate is inserted, pretend app is foregr
ounded |
| 806 // so NetworkChangeNotifierAutoDetect will register its NetworkCallback. | 806 // so NetworkChangeNotifierAutoDetect will register its NetworkCallback. |
| 807 assertFalse(ncn.isReceiverRegisteredForTesting()); | 807 assertFalse(ncn.isReceiverRegisteredForTesting()); |
| 808 | 808 |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 895 mConnectivityDelegate.setActiveNetworkExists(true); | 895 mConnectivityDelegate.setActiveNetworkExists(true); |
| 896 mConnectivityDelegate.setNetworkType(i); | 896 mConnectivityDelegate.setNetworkType(i); |
| 897 mReceiver.onReceive(getInstrumentation().getTargetContext(), intent)
; | 897 mReceiver.onReceive(getInstrumentation().getTargetContext(), intent)
; |
| 898 assertTrue(NetworkChangeNotifier.isOnline()); | 898 assertTrue(NetworkChangeNotifier.isOnline()); |
| 899 } | 899 } |
| 900 mConnectivityDelegate.setActiveNetworkExists(false); | 900 mConnectivityDelegate.setActiveNetworkExists(false); |
| 901 mReceiver.onReceive(getInstrumentation().getTargetContext(), intent); | 901 mReceiver.onReceive(getInstrumentation().getTargetContext(), intent); |
| 902 assertFalse(NetworkChangeNotifier.isOnline()); | 902 assertFalse(NetworkChangeNotifier.isOnline()); |
| 903 } | 903 } |
| 904 } | 904 } |
| OLD | NEW |