summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpauljensen@chromium.org <pauljensen@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-02-03 14:38:59 +0000
committerpauljensen@chromium.org <pauljensen@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-02-03 14:38:59 +0000
commitab50bf07911ff0b595695a710f10467aeacce0ff (patch)
tree37ac66c3a9bc13e170e5b61d691bcee2d22b920e
parentd354ca201f0acbd7418d88909c2a7c00582dffd0 (diff)
downloadchromium_src-ab50bf07911ff0b595695a710f10467aeacce0ff.zip
chromium_src-ab50bf07911ff0b595695a710f10467aeacce0ff.tar.gz
chromium_src-ab50bf07911ff0b595695a710f10467aeacce0ff.tar.bz2
Make NetworkChangeNotifierAndroid keep track of Wifi SSID so change
signals are produced when the Wifi SSID changes. BUG=339473 R=pliard@chromium.org Review URL: https://codereview.chromium.org/132373007 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@248491 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--net/android/java/src/org/chromium/net/NetworkChangeNotifierAutoDetect.java47
-rw-r--r--net/android/javatests/src/org/chromium/net/NetworkChangeNotifierTest.java42
2 files changed, 88 insertions, 1 deletions
diff --git a/net/android/java/src/org/chromium/net/NetworkChangeNotifierAutoDetect.java b/net/android/java/src/org/chromium/net/NetworkChangeNotifierAutoDetect.java
index 53bb1ec..981e3dc 100644
--- a/net/android/java/src/org/chromium/net/NetworkChangeNotifierAutoDetect.java
+++ b/net/android/java/src/org/chromium/net/NetworkChangeNotifierAutoDetect.java
@@ -9,6 +9,8 @@ import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
+import android.net.wifi.WifiInfo;
+import android.net.wifi.WifiManager;
import android.telephony.TelephonyManager;
import android.util.Log;
@@ -54,6 +56,29 @@ public class NetworkChangeNotifierAutoDetect extends BroadcastReceiver
}
}
+ /** Queries the WifiManager for SSID of the current Wifi connection. */
+ static class WifiManagerDelegate {
+ private final WifiManager mWifiManager;
+
+ WifiManagerDelegate(Context context) {
+ mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
+ }
+
+ // For testing.
+ WifiManagerDelegate() {
+ // All the methods below should be overridden.
+ mWifiManager = null;
+ }
+
+ String getWifiSSID() {
+ WifiInfo wifiInfo = mWifiManager.getConnectionInfo();
+ if (wifiInfo == null)
+ return "";
+ String ssid = wifiInfo.getSSID();
+ return ssid == null ? "" : ssid;
+ }
+ }
+
private static final String TAG = "NetworkChangeNotifierAutoDetect";
private final NetworkConnectivityIntentFilter mIntentFilter =
@@ -63,8 +88,10 @@ public class NetworkChangeNotifierAutoDetect extends BroadcastReceiver
private final Context mContext;
private ConnectivityManagerDelegate mConnectivityManagerDelegate;
+ private WifiManagerDelegate mWifiManagerDelegate;
private boolean mRegistered;
private int mConnectionType;
+ private String mWifiSSID;
/**
* Observer notified on the UI thread whenever a new connection type was detected.
@@ -77,7 +104,9 @@ public class NetworkChangeNotifierAutoDetect extends BroadcastReceiver
mObserver = observer;
mContext = context.getApplicationContext();
mConnectivityManagerDelegate = new ConnectivityManagerDelegate(context);
+ mWifiManagerDelegate = new WifiManagerDelegate(context);
mConnectionType = getCurrentConnectionType();
+ mWifiSSID = getCurrentWifiSSID();
ActivityStatus.registerStateListener(this);
}
@@ -88,6 +117,13 @@ public class NetworkChangeNotifierAutoDetect extends BroadcastReceiver
mConnectivityManagerDelegate = delegate;
}
+ /**
+ * Allows overriding the WifiManagerDelegate for tests.
+ */
+ void setWifiManagerDelegateForTests(WifiManagerDelegate delegate) {
+ mWifiManagerDelegate = delegate;
+ }
+
public void destroy() {
unregisterReceiver();
}
@@ -155,6 +191,12 @@ public class NetworkChangeNotifierAutoDetect extends BroadcastReceiver
}
}
+ private String getCurrentWifiSSID() {
+ if (getCurrentConnectionType() != NetworkChangeNotifier.CONNECTION_WIFI)
+ return "";
+ return mWifiManagerDelegate.getWifiSSID();
+ }
+
// BroadcastReceiver
@Override
public void onReceive(Context context, Intent intent) {
@@ -180,9 +222,12 @@ public class NetworkChangeNotifierAutoDetect extends BroadcastReceiver
private void connectionTypeChanged() {
int newConnectionType = getCurrentConnectionType();
- if (newConnectionType == mConnectionType) return;
+ String newWifiSSID = getCurrentWifiSSID();
+ if (newConnectionType == mConnectionType && newWifiSSID.equals(mWifiSSID))
+ return;
mConnectionType = newConnectionType;
+ mWifiSSID = newWifiSSID;
Log.d(TAG, "Network connectivity changed, type is: " + mConnectionType);
mObserver.onConnectionTypeChanged(newConnectionType);
}
diff --git a/net/android/javatests/src/org/chromium/net/NetworkChangeNotifierTest.java b/net/android/javatests/src/org/chromium/net/NetworkChangeNotifierTest.java
index 039013e..01c05b8 100644
--- a/net/android/javatests/src/org/chromium/net/NetworkChangeNotifierTest.java
+++ b/net/android/javatests/src/org/chromium/net/NetworkChangeNotifierTest.java
@@ -15,6 +15,9 @@ import android.test.suitebuilder.annotation.MediumTest;
import org.chromium.base.ActivityStatus;
import org.chromium.base.test.util.Feature;
+/**
+ * Tests for org.chromium.net.NetworkChangeNotifier.
+ */
public class NetworkChangeNotifierTest extends InstrumentationTestCase {
/**
* Listens for alerts fired by the NetworkChangeNotifier when network status changes.
@@ -80,6 +83,23 @@ public class NetworkChangeNotifierTest extends InstrumentationTestCase {
}
/**
+ * Mocks out calls to the WifiManager.
+ */
+ class MockWifiManagerDelegate
+ extends NetworkChangeNotifierAutoDetect.WifiManagerDelegate {
+ private String mWifiSSID;
+
+ @Override
+ String getWifiSSID() {
+ return mWifiSSID;
+ }
+
+ void setWifiSSID(String wifiSSID) {
+ mWifiSSID = wifiSSID;
+ }
+ }
+
+ /**
* Tests that when Chrome gets an intent indicating a change in network connectivity, it sends a
* notification to Java observers.
*/
@@ -102,6 +122,10 @@ public class NetworkChangeNotifierTest extends InstrumentationTestCase {
connectivityDelegate.setNetworkSubtype(TelephonyManager.NETWORK_TYPE_UNKNOWN);
receiver.setConnectivityManagerDelegateForTests(connectivityDelegate);
+ MockWifiManagerDelegate wifiDelegate = new MockWifiManagerDelegate();
+ wifiDelegate.setWifiSSID("foo");
+ receiver.setWifiManagerDelegateForTests(wifiDelegate);
+
// Initialize the NetworkChangeNotifier with a connection.
Intent connectivityIntent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
receiver.onReceive(getInstrumentation().getTargetContext(), connectivityIntent);
@@ -112,6 +136,24 @@ public class NetworkChangeNotifierTest extends InstrumentationTestCase {
receiver.onReceive(getInstrumentation().getTargetContext(), connectivityIntent);
assertFalse(observer.hasReceivedNotification());
+ // We shouldn't be notified if we're connected to non-Wifi and the Wifi SSID changes.
+ wifiDelegate.setWifiSSID("bar");
+ receiver.onReceive(getInstrumentation().getTargetContext(), connectivityIntent);
+ assertFalse(observer.hasReceivedNotification());
+ // We should be notified when we change to Wifi.
+ connectivityDelegate.setNetworkType(ConnectivityManager.TYPE_WIFI);
+ receiver.onReceive(getInstrumentation().getTargetContext(), connectivityIntent);
+ assertTrue(observer.hasReceivedNotification());
+ observer.resetHasReceivedNotification();
+ // We should be notified when the Wifi SSID changes.
+ wifiDelegate.setWifiSSID("foo");
+ receiver.onReceive(getInstrumentation().getTargetContext(), connectivityIntent);
+ assertTrue(observer.hasReceivedNotification());
+ observer.resetHasReceivedNotification();
+ // We shouldn't be re-notified if the Wifi SSID hasn't actually changed.
+ receiver.onReceive(getInstrumentation().getTargetContext(), connectivityIntent);
+ assertFalse(observer.hasReceivedNotification());
+
// Mimic that connectivity has been lost and ensure that Chrome notifies our observer.
connectivityDelegate.setActiveNetworkExists(false);
connectivityDelegate.setNetworkType(NetworkChangeNotifier.CONNECTION_NONE);