使用robolectric v3.0测试WiFi连接时,ShadowNetworkInfo始终为TYPE_MOBILE

时间:2015-06-03 16:11:00

标签: android robolectric

我正在尝试使用Robolectric(3.0 RC2)为Android应用中的SyncAdapter实现编写一些离线测试。该应用程序将在将使用wifi连接的工厂中的无SIM卡手机上运行。所以我希望能够针对我的wifi连接检查代码编写测试。

我已经编写了一个简单的测试,基于我在网上找到的示例来影响ConnectionManager,以模拟在wifi上。但是,尽管将NetworkInfo设置为TYPE_WIFI,但连接管理器始终返回TYPE_MOBILE的NetworkInfo类型。所以我的testSimple的最终断言失败了。我错过了一步吗?

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

}

1 个答案:

答案 0 :(得分:9)

事实证明我正在进行错误的API调用。要在ShadowConnectionManager上设置活动网络信息,正确的方法是setActiveNetworkInfo而不是setNetworkInfo。

以下是更正后的代码:

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