This source file includes following definitions.
- onConnectionTypeChanged
- hasReceivedNotification
- resetHasReceivedNotification
- activeNetworkExists
- isConnected
- setActiveNetworkExists
- getNetworkType
- setNetworkType
- getNetworkSubtype
- setNetworkSubtype
- getWifiSSID
- setWifiSSID
- Feature
- testNetworkChangeNotifierJavaObservers
package org.chromium.net;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.telephony.TelephonyManager;
import android.test.InstrumentationTestCase;
import android.test.UiThreadTest;
import android.test.suitebuilder.annotation.MediumTest;
import org.chromium.base.ApplicationState;
import org.chromium.base.test.util.Feature;
public class NetworkChangeNotifierTest extends InstrumentationTestCase {
private static class NetworkChangeNotifierTestObserver
implements NetworkChangeNotifier.ConnectionTypeObserver {
private boolean mReceivedNotification = false;
@Override
public void onConnectionTypeChanged(int connectionType) {
mReceivedNotification = true;
}
public boolean hasReceivedNotification() {
return mReceivedNotification;
}
public void resetHasReceivedNotification() {
mReceivedNotification = false;
}
}
class MockConnectivityManagerDelegate
extends NetworkChangeNotifierAutoDetect.ConnectivityManagerDelegate {
private boolean mActiveNetworkExists;
private int mNetworkType;
private int mNetworkSubtype;
@Override
boolean activeNetworkExists() {
return mActiveNetworkExists;
}
@Override
boolean isConnected() {
return getNetworkType() != NetworkChangeNotifier.CONNECTION_NONE;
}
void setActiveNetworkExists(boolean networkExists) {
mActiveNetworkExists = networkExists;
}
@Override
int getNetworkType() {
return mNetworkType;
}
void setNetworkType(int networkType) {
mNetworkType = networkType;
}
@Override
int getNetworkSubtype() {
return mNetworkSubtype;
}
void setNetworkSubtype(int networkSubtype) {
mNetworkSubtype = networkSubtype;
}
}
class MockWifiManagerDelegate
extends NetworkChangeNotifierAutoDetect.WifiManagerDelegate {
private String mWifiSSID;
@Override
String getWifiSSID() {
return mWifiSSID;
}
void setWifiSSID(String wifiSSID) {
mWifiSSID = wifiSSID;
}
}
@UiThreadTest
@MediumTest
@Feature({"Android-AppBase"})
public void testNetworkChangeNotifierJavaObservers() throws InterruptedException {
Context context = getInstrumentation().getTargetContext();
NetworkChangeNotifier.resetInstanceForTests(context);
NetworkChangeNotifier.setAutoDetectConnectivityState(true);
NetworkChangeNotifierAutoDetect receiver = NetworkChangeNotifier.getAutoDetectorForTest();
assertTrue(receiver != null);
MockConnectivityManagerDelegate connectivityDelegate =
new MockConnectivityManagerDelegate();
connectivityDelegate.setActiveNetworkExists(true);
connectivityDelegate.setNetworkType(NetworkChangeNotifier.CONNECTION_UNKNOWN);
connectivityDelegate.setNetworkSubtype(TelephonyManager.NETWORK_TYPE_UNKNOWN);
receiver.setConnectivityManagerDelegateForTests(connectivityDelegate);
MockWifiManagerDelegate wifiDelegate = new MockWifiManagerDelegate();
wifiDelegate.setWifiSSID("foo");
receiver.setWifiManagerDelegateForTests(wifiDelegate);
Intent connectivityIntent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
receiver.onReceive(getInstrumentation().getTargetContext(), connectivityIntent);
NetworkChangeNotifierTestObserver observer = new NetworkChangeNotifierTestObserver();
NetworkChangeNotifier.addConnectionTypeObserver(observer);
receiver.onReceive(getInstrumentation().getTargetContext(), connectivityIntent);
assertFalse(observer.hasReceivedNotification());
wifiDelegate.setWifiSSID("bar");
receiver.onReceive(getInstrumentation().getTargetContext(), connectivityIntent);
assertFalse(observer.hasReceivedNotification());
connectivityDelegate.setNetworkType(ConnectivityManager.TYPE_WIFI);
receiver.onReceive(getInstrumentation().getTargetContext(), connectivityIntent);
assertTrue(observer.hasReceivedNotification());
observer.resetHasReceivedNotification();
wifiDelegate.setWifiSSID("foo");
receiver.onReceive(getInstrumentation().getTargetContext(), connectivityIntent);
assertTrue(observer.hasReceivedNotification());
observer.resetHasReceivedNotification();
receiver.onReceive(getInstrumentation().getTargetContext(), connectivityIntent);
assertFalse(observer.hasReceivedNotification());
connectivityDelegate.setActiveNetworkExists(false);
connectivityDelegate.setNetworkType(NetworkChangeNotifier.CONNECTION_NONE);
Intent noConnectivityIntent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
receiver.onReceive(getInstrumentation().getTargetContext(), noConnectivityIntent);
assertTrue(observer.hasReceivedNotification());
observer.resetHasReceivedNotification();
receiver.onApplicationStateChange(ApplicationState.HAS_PAUSED_ACTIVITIES);
connectivityDelegate.setActiveNetworkExists(true);
connectivityDelegate.setNetworkType(NetworkChangeNotifier.CONNECTION_WIFI);
receiver.onApplicationStateChange(ApplicationState.HAS_RUNNING_ACTIVITIES);
assertTrue(observer.hasReceivedNotification());
}
}