diff options
author | jkarlin <jkarlin@chromium.org> | 2016-01-15 11:47:05 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2016-01-15 19:47:52 +0000 |
commit | cfb1a4c18c0170b6ec0324509ad15e372d8208bb (patch) | |
tree | e5c7e83955ba37afe05106e128afa5c81da70503 /net/android/javatests/src/org | |
parent | ab450c5ede0635194331286088d0f488f4086ba5 (diff) | |
download | chromium_src-cfb1a4c18c0170b6ec0324509ad15e372d8208bb.zip chromium_src-cfb1a4c18c0170b6ec0324509ad15e372d8208bb.tar.gz chromium_src-cfb1a4c18c0170b6ec0324509ad15e372d8208bb.tar.bz2 |
[NetworkChangeNotifier] Notify max bandwidth changed on Android when connection type changes but the max bandwidth doesn't.
Most of the CL is me going nutso with tests.
BUG=576315
Review URL: https://codereview.chromium.org/1580103002
Cr-Commit-Position: refs/heads/master@{#369816}
Diffstat (limited to 'net/android/javatests/src/org')
-rw-r--r-- | net/android/javatests/src/org/chromium/net/NetworkChangeNotifierTest.java | 71 |
1 files changed, 70 insertions, 1 deletions
diff --git a/net/android/javatests/src/org/chromium/net/NetworkChangeNotifierTest.java b/net/android/javatests/src/org/chromium/net/NetworkChangeNotifierTest.java index ae667b5..91f40df 100644 --- a/net/android/javatests/src/org/chromium/net/NetworkChangeNotifierTest.java +++ b/net/android/javatests/src/org/chromium/net/NetworkChangeNotifierTest.java @@ -58,6 +58,30 @@ public class NetworkChangeNotifierTest extends InstrumentationTestCase { } /** + * Listens for native notifications of max bandwidth change. + */ + private static class TestNetworkChangeNotifier extends NetworkChangeNotifier { + private TestNetworkChangeNotifier(Context context) { + super(context); + } + + @Override + void notifyObserversOfMaxBandwidthChange(double maxBandwidthMbps) { + mReceivedMaxBandwidthNotification = true; + } + + public boolean hasReceivedMaxBandwidthNotification() { + return mReceivedMaxBandwidthNotification; + } + + public void resetHasReceivedMaxBandwidthNotification() { + mReceivedMaxBandwidthNotification = false; + } + + private boolean mReceivedMaxBandwidthNotification = false; + } + + /** * Mocks out calls to the ConnectivityManager. */ private static class MockConnectivityManagerDelegate extends ConnectivityManagerDelegate { @@ -210,6 +234,7 @@ public class NetworkChangeNotifierTest extends InstrumentationTestCase { } // Network.Network(int netId) pointer. + private TestNetworkChangeNotifier mNotifier; private Constructor<Network> mNetworkConstructor; private NetworkChangeNotifierAutoDetect mReceiver; private MockConnectivityManagerDelegate mConnectivityDelegate; @@ -227,7 +252,8 @@ public class NetworkChangeNotifierTest extends InstrumentationTestCase { */ private void createTestNotifier(WatchForChanges watchForChanges) { Context context = getInstrumentation().getTargetContext(); - NetworkChangeNotifier.resetInstanceForTests(context); + mNotifier = new TestNetworkChangeNotifier(context); + NetworkChangeNotifier.resetInstanceForTests(mNotifier); if (watchForChanges == WatchForChanges.ALWAYS) { NetworkChangeNotifier.registerToReceiveNotificationsAlways(); } else { @@ -468,6 +494,49 @@ public class NetworkChangeNotifierTest extends InstrumentationTestCase { } /** + * Tests that when Chrome gets an intent indicating a change in max bandwidth, it sends a + * notification to Java observers. + */ + @UiThreadTest + @MediumTest + @Feature({"Android-AppBase"}) + public void testNetworkChangeNotifierMaxBandwidthNotifications() throws InterruptedException { + // Initialize the NetworkChangeNotifier with a connection. + mConnectivityDelegate.setActiveNetworkExists(true); + mConnectivityDelegate.setNetworkType(ConnectivityManager.TYPE_WIFI); + mWifiDelegate.setLinkSpeedInMbps(1); + Intent connectivityIntent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION); + mReceiver.onReceive(getInstrumentation().getTargetContext(), connectivityIntent); + assertTrue(mNotifier.hasReceivedMaxBandwidthNotification()); + mNotifier.resetHasReceivedMaxBandwidthNotification(); + + // We shouldn't be re-notified if the connection hasn't actually changed. + NetworkChangeNotifierTestObserver observer = new NetworkChangeNotifierTestObserver(); + NetworkChangeNotifier.addConnectionTypeObserver(observer); + mReceiver.onReceive(getInstrumentation().getTargetContext(), connectivityIntent); + assertFalse(mNotifier.hasReceivedMaxBandwidthNotification()); + + // We should be notified if the bandwidth changed but not the connection type. + mWifiDelegate.setLinkSpeedInMbps(2); + mReceiver.onReceive(getInstrumentation().getTargetContext(), connectivityIntent); + assertTrue(mNotifier.hasReceivedMaxBandwidthNotification()); + mNotifier.resetHasReceivedMaxBandwidthNotification(); + + // We should be notified if bandwidth and connection type changed. + mConnectivityDelegate.setNetworkType(ConnectivityManager.TYPE_ETHERNET); + mReceiver.onReceive(getInstrumentation().getTargetContext(), connectivityIntent); + assertTrue(mNotifier.hasReceivedMaxBandwidthNotification()); + mNotifier.resetHasReceivedMaxBandwidthNotification(); + + // We should be notified if the connection type changed, but not the bandwidth. + // Note that TYPE_ETHERNET and TYPE_BLUETOOTH have the same +INFINITY max bandwidth. + // This test will fail if that changes. + mConnectivityDelegate.setNetworkType(ConnectivityManager.TYPE_BLUETOOTH); + mReceiver.onReceive(getInstrumentation().getTargetContext(), connectivityIntent); + assertTrue(mNotifier.hasReceivedMaxBandwidthNotification()); + } + + /** * Tests that when setting {@code registerToReceiveNotificationsAlways()}, * a NetworkChangeNotifierAutoDetect object is successfully created. */ |