I'm attempting to use Robolectric (3.0 RC2) to write some offline tests for a SyncAdapter implementation in an Android app. The app will be run on SIM-less handsets in factories which will use the wifi connection. So I'd like to be able to write tests against my wifi connectivity checking code.
I've written a simple test, based on examples I found online to shadow the ConnectionManager with a view to simulate being on the wifi. However, despite setting the NetworkInfo to TYPE_WIFI, the connection manager always returns a NetworkInfo type of TYPE_MOBILE. So the final assertion of my testSimple fails. Am I missing a step?
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import test.app.BuildConfig;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricGradleTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.Shadows;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowConnectivityManager;
import org.robolectric.shadows.ShadowNetworkInfo;
import java.io.IOException;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertTrue;
@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, manifest = "/src/main/AndroidManifest.xml")
public class DownloadSyncAdapterTest {
private ConnectivityManager connectivityManager;
private ShadowConnectivityManager shadowConnectivityManager;
private ShadowNetworkInfo shadowOfActiveNetworkInfo;
@Before
public void setUp() throws IOException {
connectivityManager = getConnectivityManager();
shadowConnectivityManager = Shadows.shadowOf(connectivityManager);
shadowOfActiveNetworkInfo = Shadows.shadowOf(connectivityManager.getActiveNetworkInfo());
}
@Test
public void testSimple() {
NetworkInfo networkInfo = ShadowNetworkInfo.newInstance(NetworkInfo.DetailedState.CONNECTED, ConnectivityManager.TYPE_WIFI, 0, true, true);
shadowConnectivityManager.setNetworkInfo(ConnectivityManager.TYPE_WIFI, networkInfo);
NetworkInfo activeInfo = connectivityManager.getActiveNetworkInfo();
assertTrue(activeInfo != null && activeInfo.isConnected());
// Assertion fails: Returns TYPE_MOBILE
assertEquals(ConnectivityManager.TYPE_WIFI, activeInfo.getType());
}
private ConnectivityManager getConnectivityManager() {
return (ConnectivityManager) RuntimeEnvironment.application.getSystemService(Context.CONNECTIVITY_SERVICE);
}
}
It turns out I was making the wrong API call. To set the active network info on the ShadowConnectionManager, the correct method is setActiveNetworkInfo and not setNetworkInfo.
Here's the corrected code:
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import test.app.BuildConfig;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricGradleTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.Shadows;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowConnectivityManager;
import org.robolectric.shadows.ShadowNetworkInfo;
import java.io.IOException;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertTrue;
@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, manifest = "/src/main/AndroidManifest.xml")
public class DownloadSyncAdapterTest {
private ConnectivityManager connectivityManager;
private ShadowConnectivityManager shadowConnectivityManager;
private ShadowNetworkInfo shadowOfActiveNetworkInfo;
@Before
public void setUp() throws IOException {
connectivityManager = getConnectivityManager();
shadowConnectivityManager = Shadows.shadowOf(connectivityManager);
shadowOfActiveNetworkInfo = Shadows.shadowOf(connectivityManager.getActiveNetworkInfo());
}
@Test
public void testSimple() {
NetworkInfo networkInfo = ShadowNetworkInfo.newInstance(NetworkInfo.DetailedState.CONNECTED, ConnectivityManager.TYPE_WIFI, 0, true, true);
// Correct API call: use setActiveNetworkInfo instead of setNetworkInfo
shadowConnectivityManager.setActiveNetworkInfo(networkInfo);
NetworkInfo activeInfo = connectivityManager.getActiveNetworkInfo();
assertTrue(activeInfo != null && activeInfo.isConnected());
// Assertion now passes: Correctly returns TYPE_WIFI
assertEquals(ConnectivityManager.TYPE_WIFI, activeInfo.getType());
}
private ConnectivityManager getConnectivityManager() {
return (ConnectivityManager) RuntimeEnvironment.application.getSystemService(Context.CONNECTIVITY_SERVICE);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With