FileDocCategorySizeDatePackage
ConnectivityServiceTest.javaAPI DocAndroid 5.1 API9536Thu Mar 12 22:22:42 GMT 2015com.android.server

ConnectivityServiceTest

public class ConnectivityServiceTest extends android.test.AndroidTestCase
Tests for {@link ConnectivityService}.

Fields Summary
private static final String
TAG
private static final String
MOBILE_IFACE
private static final String
WIFI_IFACE
private static final android.net.RouteInfo
MOBILE_ROUTE_V4
private static final android.net.RouteInfo
MOBILE_ROUTE_V6
private static final android.net.RouteInfo
WIFI_ROUTE_V4
private static final android.net.RouteInfo
WIFI_ROUTE_V6
private android.os.INetworkManagementService
mNetManager
private android.net.INetworkStatsService
mStatsService
private android.net.INetworkPolicyManager
mPolicyService
private BroadcastInterceptingContext
mServiceContext
private ConnectivityService
mService
Constructors Summary
Methods Summary
private static java.net.InetAddressparse(java.lang.String addr)


// TODO: rework with network factory
//    private MockNetwork mMobile;
//    private MockNetwork mWifi;
//
//    private Handler mTrackerHandler;
//
//    private static class MockNetwork {
//        public NetworkStateTracker tracker;
//        public NetworkInfo info;
//        public LinkProperties link;
//
//        public MockNetwork(int type) {
//            tracker = mock(NetworkStateTracker.class);
//            info = new NetworkInfo(type, -1, getNetworkTypeName(type), null);
//            link = new LinkProperties();
//        }
//
//        public void doReturnDefaults() {
//            // TODO: eventually CS should make defensive copies
//            doReturn(new NetworkInfo(info)).when(tracker).getNetworkInfo();
//            doReturn(new LinkProperties(link)).when(tracker).getLinkProperties();
//
//            // fallback to default TCP buffers
//            doReturn("").when(tracker).getTcpBufferSizesPropName();
//        }
//    }
//
//    @Override
//    public void setUp() throws Exception {
//        super.setUp();
//
//        mServiceContext = new BroadcastInterceptingContext(getContext());
//
//        mNetManager = mock(INetworkManagementService.class);
//        mStatsService = mock(INetworkStatsService.class);
//        mPolicyService = mock(INetworkPolicyManager.class);
//        mNetFactory = mock(ConnectivityService.NetworkFactory.class);
//
//        mMobile = new MockNetwork(TYPE_MOBILE);
//        mWifi = new MockNetwork(TYPE_WIFI);
//
//        // omit most network trackers
//        doThrow(new IllegalArgumentException("Not supported in test environment"))
//                .when(mNetFactory).createTracker(anyInt(), isA(NetworkConfig.class));
//
//        doReturn(mMobile.tracker)
//                .when(mNetFactory).createTracker(eq(TYPE_MOBILE), isA(NetworkConfig.class));
//        doReturn(mWifi.tracker)
//                .when(mNetFactory).createTracker(eq(TYPE_WIFI), isA(NetworkConfig.class));
//
//        final ArgumentCaptor<Handler> trackerHandler = ArgumentCaptor.forClass(Handler.class);
//        doNothing().when(mMobile.tracker)
//                .startMonitoring(isA(Context.class), trackerHandler.capture());
//
//        mService = new ConnectivityService(
//                mServiceContext, mNetManager, mStatsService, mPolicyService);
//        mService.systemReady();
//
//        mTrackerHandler = trackerHandler.getValue();
//        mTrackerHandler.getLooper().setMessageLogging(new LogPrinter(Log.INFO, TAG));
//    }
//
//    @Override
//    public void tearDown() throws Exception {
//        super.tearDown();
//    }
//
//    public void testMobileConnectedAddedRoutes() throws Exception {
//        Future<?> nextConnBroadcast;
//
//        // bring up mobile network
//        mMobile.info.setDetailedState(DetailedState.CONNECTED, null, null);
//        mMobile.link.setInterfaceName(MOBILE_IFACE);
//        mMobile.link.addRoute(MOBILE_ROUTE_V4);
//        mMobile.link.addRoute(MOBILE_ROUTE_V6);
//        mMobile.doReturnDefaults();
//
//        nextConnBroadcast = mServiceContext.nextBroadcastIntent(CONNECTIVITY_ACTION_IMMEDIATE);
//        mTrackerHandler.obtainMessage(EVENT_STATE_CHANGED, mMobile.info).sendToTarget();
//        nextConnBroadcast.get();
//
//        // verify that both routes were added
//        int mobileNetId = mMobile.tracker.getNetwork().netId;
//        verify(mNetManager).addRoute(eq(mobileNetId), eq(MOBILE_ROUTE_V4));
//        verify(mNetManager).addRoute(eq(mobileNetId), eq(MOBILE_ROUTE_V6));
//    }
//
//    public void testMobileWifiHandoff() throws Exception {
//        Future<?> nextConnBroadcast;
//
//        // bring up mobile network
//        mMobile.info.setDetailedState(DetailedState.CONNECTED, null, null);
//        mMobile.link.setInterfaceName(MOBILE_IFACE);
//        mMobile.link.addRoute(MOBILE_ROUTE_V4);
//        mMobile.link.addRoute(MOBILE_ROUTE_V6);
//        mMobile.doReturnDefaults();
//
//        nextConnBroadcast = mServiceContext.nextBroadcastIntent(CONNECTIVITY_ACTION_IMMEDIATE);
//        mTrackerHandler.obtainMessage(EVENT_STATE_CHANGED, mMobile.info).sendToTarget();
//        nextConnBroadcast.get();
//
//        reset(mNetManager);
//
//        // now bring up wifi network
//        mWifi.info.setDetailedState(DetailedState.CONNECTED, null, null);
//        mWifi.link.setInterfaceName(WIFI_IFACE);
//        mWifi.link.addRoute(WIFI_ROUTE_V4);
//        mWifi.link.addRoute(WIFI_ROUTE_V6);
//        mWifi.doReturnDefaults();
//
//        // expect that mobile will be torn down
//        doReturn(true).when(mMobile.tracker).teardown();
//
//        nextConnBroadcast = mServiceContext.nextBroadcastIntent(CONNECTIVITY_ACTION_IMMEDIATE);
//        mTrackerHandler.obtainMessage(EVENT_STATE_CHANGED, mWifi.info).sendToTarget();
//        nextConnBroadcast.get();
//
//        // verify that wifi routes added, and teardown requested
//        int wifiNetId = mWifi.tracker.getNetwork().netId;
//        verify(mNetManager).addRoute(eq(wifiNetId), eq(WIFI_ROUTE_V4));
//        verify(mNetManager).addRoute(eq(wifiNetId), eq(WIFI_ROUTE_V6));
//        verify(mMobile.tracker).teardown();
//
//        int mobileNetId = mMobile.tracker.getNetwork().netId;
//
//        reset(mNetManager, mMobile.tracker);
//
//        // tear down mobile network, as requested
//        mMobile.info.setDetailedState(DetailedState.DISCONNECTED, null, null);
//        mMobile.link.clear();
//        mMobile.doReturnDefaults();
//
//        nextConnBroadcast = mServiceContext.nextBroadcastIntent(CONNECTIVITY_ACTION_IMMEDIATE);
//        mTrackerHandler.obtainMessage(EVENT_STATE_CHANGED, mMobile.info).sendToTarget();
//        nextConnBroadcast.get();
//
//        verify(mNetManager).removeRoute(eq(mobileNetId), eq(MOBILE_ROUTE_V4));
//        verify(mNetManager).removeRoute(eq(mobileNetId), eq(MOBILE_ROUTE_V6));
//
//    }

         
        return InetAddress.parseNumericAddress(addr);