Search in sources :

Example 81 with NetworkRequest

use of android.net.NetworkRequest in project platform_frameworks_base by android.

the class ConnectivityServiceTest method testTimedoutNetworkRequest.

/**
     * Validate that when a time-out is specified for a network request the onUnavailable()
     * callback is called when time-out expires. Then validate that if network request is
     * (somehow) satisfied - the callback isn't called later.
     */
@SmallTest
public void testTimedoutNetworkRequest() {
    NetworkRequest nr = new NetworkRequest.Builder().addTransportType(NetworkCapabilities.TRANSPORT_WIFI).build();
    final TestNetworkCallback networkCallback = new TestNetworkCallback();
    final int timeoutMs = 10;
    mCm.requestNetwork(nr, networkCallback, timeoutMs);
    // pass timeout and validate that UNAVAILABLE is called
    networkCallback.expectCallback(CallbackState.UNAVAILABLE, null);
    // create a network satisfying request - validate that request not triggered
    mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI);
    mWiFiNetworkAgent.connect(false);
    networkCallback.assertNoCallback();
}
Also used : NetworkRequest(android.net.NetworkRequest) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 82 with NetworkRequest

use of android.net.NetworkRequest in project platform_frameworks_base by android.

the class ConnectivityServiceTest method testBackgroundNetworks.

@SmallTest
public void testBackgroundNetworks() throws Exception {
    // Create a background request. We can't do this ourselves because ConnectivityService
    // doesn't have an API for it. So just turn on mobile data always on.
    setMobileDataAlwaysOn(true);
    final NetworkRequest request = new NetworkRequest.Builder().build();
    final NetworkRequest fgRequest = new NetworkRequest.Builder().addCapability(NET_CAPABILITY_FOREGROUND).build();
    final TestNetworkCallback callback = new TestNetworkCallback();
    final TestNetworkCallback fgCallback = new TestNetworkCallback();
    mCm.registerNetworkCallback(request, callback);
    mCm.registerNetworkCallback(fgRequest, fgCallback);
    mCellNetworkAgent = new MockNetworkAgent(TRANSPORT_CELLULAR);
    mCellNetworkAgent.connect(true);
    callback.expectAvailableAndValidatedCallbacks(mCellNetworkAgent);
    fgCallback.expectAvailableAndValidatedCallbacks(mCellNetworkAgent);
    assertTrue(isForegroundNetwork(mCellNetworkAgent));
    mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI);
    mWiFiNetworkAgent.connect(true);
    // When wifi connects, cell lingers.
    callback.expectAvailableCallbacks(mWiFiNetworkAgent);
    callback.expectCallback(CallbackState.LOSING, mCellNetworkAgent);
    callback.expectCapabilitiesWith(NET_CAPABILITY_VALIDATED, mWiFiNetworkAgent);
    fgCallback.expectAvailableCallbacks(mWiFiNetworkAgent);
    fgCallback.expectCallback(CallbackState.LOSING, mCellNetworkAgent);
    fgCallback.expectCapabilitiesWith(NET_CAPABILITY_VALIDATED, mWiFiNetworkAgent);
    assertTrue(isForegroundNetwork(mCellNetworkAgent));
    assertTrue(isForegroundNetwork(mWiFiNetworkAgent));
    // When lingering is complete, cell is still there but is now in the background.
    mService.waitForIdle();
    int timeoutMs = TEST_LINGER_DELAY_MS + TEST_LINGER_DELAY_MS / 4;
    fgCallback.expectCallback(CallbackState.LOST, mCellNetworkAgent, timeoutMs);
    // Expect a network capabilities update sans FOREGROUND.
    callback.expectCapabilitiesWithout(NET_CAPABILITY_FOREGROUND, mCellNetworkAgent);
    assertFalse(isForegroundNetwork(mCellNetworkAgent));
    assertTrue(isForegroundNetwork(mWiFiNetworkAgent));
    // File a cell request and check that cell comes into the foreground.
    final NetworkRequest cellRequest = new NetworkRequest.Builder().addTransportType(TRANSPORT_CELLULAR).build();
    final TestNetworkCallback cellCallback = new TestNetworkCallback();
    mCm.requestNetwork(cellRequest, cellCallback);
    // NOTE: This request causes the network's capabilities to change. This
    // is currently delivered before the onAvailable() callbacks.
    // TODO: Fix this.
    cellCallback.expectCapabilitiesWith(NET_CAPABILITY_FOREGROUND, mCellNetworkAgent);
    cellCallback.expectAvailableCallbacks(mCellNetworkAgent);
    fgCallback.expectAvailableCallbacks(mCellNetworkAgent);
    // Expect a network capabilities update with FOREGROUND, because the most recent
    // request causes its state to change.
    callback.expectCapabilitiesWith(NET_CAPABILITY_FOREGROUND, mCellNetworkAgent);
    assertTrue(isForegroundNetwork(mCellNetworkAgent));
    assertTrue(isForegroundNetwork(mWiFiNetworkAgent));
    // Release the request. The network immediately goes into the background, since it was not
    // lingering.
    mCm.unregisterNetworkCallback(cellCallback);
    fgCallback.expectCallback(CallbackState.LOST, mCellNetworkAgent);
    // Expect a network capabilities update sans FOREGROUND.
    callback.expectCapabilitiesWithout(NET_CAPABILITY_FOREGROUND, mCellNetworkAgent);
    assertFalse(isForegroundNetwork(mCellNetworkAgent));
    assertTrue(isForegroundNetwork(mWiFiNetworkAgent));
    // Disconnect wifi and check that cell is foreground again.
    mWiFiNetworkAgent.disconnect();
    callback.expectCallback(CallbackState.LOST, mWiFiNetworkAgent);
    fgCallback.expectCallback(CallbackState.LOST, mWiFiNetworkAgent);
    fgCallback.expectAvailableCallbacks(mCellNetworkAgent);
    assertTrue(isForegroundNetwork(mCellNetworkAgent));
    mCm.unregisterNetworkCallback(callback);
    mCm.unregisterNetworkCallback(fgCallback);
}
Also used : NetworkRequest(android.net.NetworkRequest) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 83 with NetworkRequest

use of android.net.NetworkRequest in project platform_frameworks_base by android.

the class ConnectivityServiceTest method testAdditionalStateCallbacks.

@SmallTest
public void testAdditionalStateCallbacks() throws Exception {
    // File a network request for mobile.
    final TestNetworkCallback cellNetworkCallback = new TestNetworkCallback();
    final NetworkRequest cellRequest = new NetworkRequest.Builder().addTransportType(TRANSPORT_CELLULAR).build();
    mCm.requestNetwork(cellRequest, cellNetworkCallback);
    // Bring up the mobile network.
    mCellNetworkAgent = new MockNetworkAgent(TRANSPORT_CELLULAR);
    mCellNetworkAgent.connect(true);
    // We should get onAvailable(), onCapabilitiesChanged(), and
    // onLinkPropertiesChanged() in rapid succession. Additionally, we
    // should get onCapabilitiesChanged() when the mobile network validates.
    cellNetworkCallback.expectAvailableAndValidatedCallbacks(mCellNetworkAgent);
    cellNetworkCallback.assertNoCallback();
    // Update LinkProperties.
    final LinkProperties lp = new LinkProperties();
    lp.setInterfaceName("foonet_data0");
    mCellNetworkAgent.sendLinkProperties(lp);
    // We should get onLinkPropertiesChanged().
    cellNetworkCallback.expectCallback(CallbackState.LINK_PROPERTIES, mCellNetworkAgent);
    cellNetworkCallback.assertNoCallback();
    // Suspend the network.
    mCellNetworkAgent.suspend();
    cellNetworkCallback.expectCallback(CallbackState.SUSPENDED, mCellNetworkAgent);
    cellNetworkCallback.assertNoCallback();
    // Register a garden variety default network request.
    final TestNetworkCallback dfltNetworkCallback = new TestNetworkCallback();
    mCm.registerDefaultNetworkCallback(dfltNetworkCallback);
    // We should get onAvailable(), onCapabilitiesChanged(), onLinkPropertiesChanged(),
    // as well as onNetworkSuspended() in rapid succession.
    dfltNetworkCallback.expectAvailableAndSuspendedCallbacks(mCellNetworkAgent);
    dfltNetworkCallback.assertNoCallback();
    mCm.unregisterNetworkCallback(dfltNetworkCallback);
    mCm.unregisterNetworkCallback(cellNetworkCallback);
}
Also used : NetworkRequest(android.net.NetworkRequest) LinkProperties(android.net.LinkProperties) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 84 with NetworkRequest

use of android.net.NetworkRequest in project platform_frameworks_base by android.

the class ConnectivityServiceTest method testInvalidNetworkSpecifier.

@SmallTest
public void testInvalidNetworkSpecifier() {
    boolean execptionCalled = true;
    try {
        NetworkRequest.Builder builder = new NetworkRequest.Builder();
        builder.setNetworkSpecifier(MATCH_ALL_REQUESTS_NETWORK_SPECIFIER);
        execptionCalled = false;
    } catch (IllegalArgumentException e) {
    // do nothing - should get here
    }
    assertTrue("NetworkRequest builder with MATCH_ALL_REQUESTS_NETWORK_SPECIFIER", execptionCalled);
    try {
        NetworkCapabilities networkCapabilities = new NetworkCapabilities();
        networkCapabilities.addTransportType(TRANSPORT_WIFI).setNetworkSpecifier(NetworkCapabilities.MATCH_ALL_REQUESTS_NETWORK_SPECIFIER);
        mService.requestNetwork(networkCapabilities, null, 0, null, ConnectivityManager.TYPE_WIFI);
        execptionCalled = false;
    } catch (IllegalArgumentException e) {
    // do nothing - should get here
    }
    assertTrue("ConnectivityService requestNetwork with MATCH_ALL_REQUESTS_NETWORK_SPECIFIER", execptionCalled);
}
Also used : NetworkRequest(android.net.NetworkRequest) NetworkCapabilities(android.net.NetworkCapabilities) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 85 with NetworkRequest

use of android.net.NetworkRequest in project platform_frameworks_base by android.

the class ConnectivityServiceTest method testMMSonCell.

@SmallTest
public void testMMSonCell() throws Exception {
    // Test bringing up cellular without MMS
    mCellNetworkAgent = new MockNetworkAgent(TRANSPORT_CELLULAR);
    ConditionVariable cv = waitForConnectivityBroadcasts(1);
    mCellNetworkAgent.connect(false);
    waitFor(cv);
    verifyActiveNetwork(TRANSPORT_CELLULAR);
    // Register MMS NetworkRequest
    NetworkRequest.Builder builder = new NetworkRequest.Builder();
    builder.addCapability(NetworkCapabilities.NET_CAPABILITY_MMS);
    final TestNetworkCallback networkCallback = new TestNetworkCallback();
    mCm.requestNetwork(builder.build(), networkCallback);
    // Test bringing up MMS cellular network
    MockNetworkAgent mmsNetworkAgent = new MockNetworkAgent(TRANSPORT_CELLULAR);
    mmsNetworkAgent.addCapability(NET_CAPABILITY_MMS);
    mmsNetworkAgent.connectWithoutInternet();
    networkCallback.expectAvailableCallbacks(mmsNetworkAgent);
    verifyActiveNetwork(TRANSPORT_CELLULAR);
    // Test releasing MMS NetworkRequest does not disconnect main cellular NetworkAgent
    cv = mmsNetworkAgent.getDisconnectedCV();
    mCm.unregisterNetworkCallback(networkCallback);
    waitFor(cv);
    verifyActiveNetwork(TRANSPORT_CELLULAR);
}
Also used : ConditionVariable(android.os.ConditionVariable) NetworkRequest(android.net.NetworkRequest) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Aggregations

NetworkRequest (android.net.NetworkRequest)158 NetworkCapabilities (android.net.NetworkCapabilities)48 SmallTest (android.test.suitebuilder.annotation.SmallTest)43 ConditionVariable (android.os.ConditionVariable)24 LargeTest (android.test.suitebuilder.annotation.LargeTest)21 RemoteException (android.os.RemoteException)20 NetworkCallback (android.net.ConnectivityManager.NetworkCallback)18 Network (android.net.Network)16 NetworkAgentInfo (com.android.server.connectivity.NetworkAgentInfo)15 LinkProperties (android.net.LinkProperties)14 ArrayList (java.util.ArrayList)9 ContentResolver (android.content.ContentResolver)8 HandlerThread (android.os.HandlerThread)8 MockContentResolver (android.test.mock.MockContentResolver)8 ConnectivityManager (android.net.ConnectivityManager)6 NetworkPolicyManager.uidRulesToString (android.net.NetworkPolicyManager.uidRulesToString)6 Intent (android.content.Intent)5 Bundle (android.os.Bundle)5 Message (android.os.Message)5 IBatteryStats (com.android.internal.app.IBatteryStats)5