Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ShadowNetworkInfo is always TYPE_MOBILE when testing WiFi connectivity with robolectric v3.0

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);
    }

}
like image 575
Andrew Heckford Avatar asked Dec 25 '22 18:12

Andrew Heckford


1 Answers

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);
    }
like image 59
Andrew Heckford Avatar answered Feb 06 '23 10:02

Andrew Heckford