Skip to content

Commit

Permalink
fix failing test
Browse files Browse the repository at this point in the history
Signed-off-by: Chidera Olibie <[email protected]>
  • Loading branch information
colibie committed Oct 17, 2022
1 parent 8e0a504 commit af5a8e1
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ public class AndroidNetworkMonitor extends BroadcastReceiver {
private ConnectivityManager connectivityManager;
private NetworkCallback networkCallback;
private NetworkRequest networkRequest;
private boolean isOnline = false;

public static void load(Context context, EnvoyEngine envoyEngine) {
if (instance != null) {
Expand Down Expand Up @@ -106,19 +105,19 @@ public void onLost(final Network network) {
}
}

public static AndroidNetworkMonitor getInstance() { return instance; }
/** @returns The singleton instance of {@link AndroidNetworkMonitor}. */
public static AndroidNetworkMonitor getInstance() {
assert instance != null;
return instance;
}

@Override
public void onReceive(Context context, Intent intent) {
handleNetworkChange();
}

public boolean isOnline() { return isOnline; }

@VisibleForTesting
public void setConnectivityManagerForTesting(ConnectivityManager testConnectivityManager) {
connectivityManager = testConnectivityManager;
}
/** @returns True if there is connectivity */
public boolean isOnline() { return previousNetworkType != -1; }

private void handleNetworkChange() {
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
Expand All @@ -127,7 +126,6 @@ private void handleNetworkChange() {
return;
}
previousNetworkType = networkType;
isOnline = networkType == -1 ? false : true;

switch (networkType) {
case ConnectivityManager.TYPE_MOBILE:
Expand All @@ -140,4 +138,10 @@ private void handleNetworkChange() {
envoyEngine.setPreferredNetwork(EnvoyNetworkType.ENVOY_NETWORK_TYPE_GENERIC);
}
}

/** Expose connectivityManager only for testing */
@VisibleForTesting
public ConnectivityManager getConnectivityManager() {
return connectivityManager;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

import static org.robolectric.Shadows.shadowOf;

import android.content.Intent;
import android.content.Context;
import android.content.Intent;
import android.Manifest;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import androidx.test.filters.MediumTest;
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.rule.GrantPermissionRule;
Expand Down Expand Up @@ -39,8 +40,7 @@ public void setUp() {
context = InstrumentationRegistry.getInstrumentation().getTargetContext();
AndroidNetworkMonitor.load(context, new MockEnvoyEngine());
androidNetworkMonitor = AndroidNetworkMonitor.getInstance();
connectivityManager =
shadowOf((ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE));
connectivityManager = shadowOf(androidNetworkMonitor.getConnectivityManager());
}

/**
Expand All @@ -55,9 +55,14 @@ public void testAndroidNetworkMonitorIsOnline() {
androidNetworkMonitor.onReceive(context, intent);
Assert.assertTrue(androidNetworkMonitor.isOnline());

// Simulate a no network scenerio
// Save old networkInfo and simulate a no network scenerio
NetworkInfo networkInfo = androidNetworkMonitor.getConnectivityManager().getActiveNetworkInfo();
connectivityManager.setActiveNetworkInfo(null);
androidNetworkMonitor.onReceive(context, intent);
Assert.assertFalse(androidNetworkMonitor.isOnline());

// Bring back online since the AndroidNetworkMonitor class is a singleton
connectivityManager.setActiveNetworkInfo(networkInfo);
androidNetworkMonitor.onReceive(context, intent);
}
}
19 changes: 15 additions & 4 deletions test/java/org/chromium/net/CronetUrlRequestTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@
import static org.junit.Assert.fail;
import static org.robolectric.Shadows.shadowOf;

import androidx.test.annotation.UiThreadTest;
import android.content.Context;
import android.content.Intent;
import android.Manifest;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.os.ConditionVariable;
import android.os.StrictMode;
Expand Down Expand Up @@ -2090,17 +2093,25 @@ public void testErrorCodes() throws Exception {
@Feature({"Cronet"})
@OnlyRunNativeCronet // Java impl doesn't support MockUrlRequestJobFactory
public void testInternetDisconnectedError() throws Exception {
AndroidNetworkMonitor androidNetworkMonitor = AndroidNetworkMonitor.getInstance();
Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
// save old networkInfo before overriding
NetworkInfo networkInfo = androidNetworkMonitor.getConnectivityManager().getActiveNetworkInfo();

// simulate no network
ShadowConnectivityManager connectivityManager = shadowOf(
(ConnectivityManager)getContext().getSystemService(getContext().CONNECTIVITY_SERVICE));
ShadowConnectivityManager connectivityManager =
shadowOf(androidNetworkMonitor.getConnectivityManager());
connectivityManager.setActiveNetworkInfo(null);
AndroidNetworkMonitor.getInstance().onReceive(
getContext(), new Intent(ConnectivityManager.CONNECTIVITY_ACTION));
androidNetworkMonitor.onReceive(getContext(), intent);

// send request and confirm errorcode
checkSpecificErrorCode(
EnvoyMobileError.DNS_RESOLUTION_FAILED, NetError.ERR_INTERNET_DISCONNECTED,
NetworkException.ERROR_INTERNET_DISCONNECTED, "INTERNET_DISCONNECTED", false);

// bring back online since the AndroidNetworkMonitor class is a singleton
connectivityManager.setActiveNetworkInfo(networkInfo);
androidNetworkMonitor.onReceive(getContext(), intent);
}

/*
Expand Down

0 comments on commit af5a8e1

Please sign in to comment.