Search in sources :

Example 41 with NetworkRequest

use of android.net.NetworkRequest in project android_frameworks_base by DirtyUnicorns.

the class ConnectivityServiceTest method testRequestBenchmark.

@SmallTest
public void testRequestBenchmark() throws Exception {
    // Benchmarks connecting and switching performance in the presence of a large number of
    // NetworkRequests.
    // 1. File NUM_REQUESTS requests.
    // 2. Have a network connect. Wait for NUM_REQUESTS onAvailable callbacks to fire.
    // 3. Have a new network connect and outscore the previous. Wait for NUM_REQUESTS onLosing
    //    and NUM_REQUESTS onAvailable callbacks to fire.
    // See how long it took.
    final int NUM_REQUESTS = 90;
    final NetworkRequest request = new NetworkRequest.Builder().clearCapabilities().build();
    final NetworkCallback[] callbacks = new NetworkCallback[NUM_REQUESTS];
    final CountDownLatch availableLatch = new CountDownLatch(NUM_REQUESTS);
    final CountDownLatch losingLatch = new CountDownLatch(NUM_REQUESTS);
    final int REGISTER_TIME_LIMIT_MS = 100;
    long startTime = System.currentTimeMillis();
    for (int i = 0; i < NUM_REQUESTS; i++) {
        callbacks[i] = new NetworkCallback() {

            @Override
            public void onAvailable(Network n) {
                availableLatch.countDown();
            }

            @Override
            public void onLosing(Network n, int t) {
                losingLatch.countDown();
            }
        };
        mCm.registerNetworkCallback(request, callbacks[i]);
    }
    long timeTaken = System.currentTimeMillis() - startTime;
    String msg = String.format("Register %d callbacks: %dms, acceptable %dms", NUM_REQUESTS, timeTaken, REGISTER_TIME_LIMIT_MS);
    Log.d(TAG, msg);
    assertTrue(msg, timeTaken < REGISTER_TIME_LIMIT_MS);
    final int CONNECT_TIME_LIMIT_MS = 30;
    mCellNetworkAgent = new MockNetworkAgent(TRANSPORT_CELLULAR);
    // Don't request that the network validate, because otherwise connect() will block until
    // the network gets NET_CAPABILITY_VALIDATED, after all the callbacks below have fired,
    // and we won't actually measure anything.
    mCellNetworkAgent.connect(false);
    startTime = System.currentTimeMillis();
    if (!availableLatch.await(CONNECT_TIME_LIMIT_MS, TimeUnit.MILLISECONDS)) {
        fail(String.format("Only dispatched %d/%d onAvailable callbacks in %dms", NUM_REQUESTS - availableLatch.getCount(), NUM_REQUESTS, CONNECT_TIME_LIMIT_MS));
    }
    timeTaken = System.currentTimeMillis() - startTime;
    Log.d(TAG, String.format("Connect, %d callbacks: %dms, acceptable %dms", NUM_REQUESTS, timeTaken, CONNECT_TIME_LIMIT_MS));
    final int SWITCH_TIME_LIMIT_MS = 30;
    mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI);
    // Give wifi a high enough score that we'll linger cell when wifi comes up.
    mWiFiNetworkAgent.adjustScore(40);
    mWiFiNetworkAgent.connect(false);
    startTime = System.currentTimeMillis();
    if (!losingLatch.await(SWITCH_TIME_LIMIT_MS, TimeUnit.MILLISECONDS)) {
        fail(String.format("Only dispatched %d/%d onLosing callbacks in %dms", NUM_REQUESTS - losingLatch.getCount(), NUM_REQUESTS, SWITCH_TIME_LIMIT_MS));
    }
    timeTaken = System.currentTimeMillis() - startTime;
    Log.d(TAG, String.format("Linger, %d callbacks: %dms, acceptable %dms", NUM_REQUESTS, timeTaken, SWITCH_TIME_LIMIT_MS));
    final int UNREGISTER_TIME_LIMIT_MS = 10;
    startTime = System.currentTimeMillis();
    for (int i = 0; i < NUM_REQUESTS; i++) {
        mCm.unregisterNetworkCallback(callbacks[i]);
    }
    timeTaken = System.currentTimeMillis() - startTime;
    msg = String.format("Unregister %d callbacks: %dms, acceptable %dms", NUM_REQUESTS, timeTaken, UNREGISTER_TIME_LIMIT_MS);
    Log.d(TAG, msg);
    assertTrue(msg, timeTaken < UNREGISTER_TIME_LIMIT_MS);
}
Also used : Network(android.net.Network) NetworkRequest(android.net.NetworkRequest) CountDownLatch(java.util.concurrent.CountDownLatch) NetworkCallback(android.net.ConnectivityManager.NetworkCallback) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 42 with NetworkRequest

use of android.net.NetworkRequest in project android_frameworks_base by DirtyUnicorns.

the class ConnectivityServiceTest method testCaptivePortal.

@LargeTest
public void testCaptivePortal() {
    final TestNetworkCallback captivePortalCallback = new TestNetworkCallback();
    final NetworkRequest captivePortalRequest = new NetworkRequest.Builder().addCapability(NET_CAPABILITY_CAPTIVE_PORTAL).build();
    mCm.registerNetworkCallback(captivePortalRequest, captivePortalCallback);
    final TestNetworkCallback validatedCallback = new TestNetworkCallback();
    final NetworkRequest validatedRequest = new NetworkRequest.Builder().addCapability(NET_CAPABILITY_VALIDATED).build();
    mCm.registerNetworkCallback(validatedRequest, validatedCallback);
    // Bring up a network with a captive portal.
    // Expect onAvailable callback of listen for NET_CAPABILITY_CAPTIVE_PORTAL.
    mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI);
    String firstRedirectUrl = "http://example.com/firstPath";
    mWiFiNetworkAgent.connectWithCaptivePortal(firstRedirectUrl);
    captivePortalCallback.expectCallback(CallbackState.AVAILABLE, mWiFiNetworkAgent);
    assertEquals(mWiFiNetworkAgent.waitForRedirectUrl(), firstRedirectUrl);
    // Take down network.
    // Expect onLost callback.
    mWiFiNetworkAgent.disconnect();
    captivePortalCallback.expectCallback(CallbackState.LOST, mWiFiNetworkAgent);
    // Bring up a network with a captive portal.
    // Expect onAvailable callback of listen for NET_CAPABILITY_CAPTIVE_PORTAL.
    mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI);
    String secondRedirectUrl = "http://example.com/secondPath";
    mWiFiNetworkAgent.connectWithCaptivePortal(secondRedirectUrl);
    captivePortalCallback.expectCallback(CallbackState.AVAILABLE, mWiFiNetworkAgent);
    assertEquals(mWiFiNetworkAgent.waitForRedirectUrl(), secondRedirectUrl);
    // Make captive portal disappear then revalidate.
    // Expect onLost callback because network no longer provides NET_CAPABILITY_CAPTIVE_PORTAL.
    mWiFiNetworkAgent.getWrappedNetworkMonitor().gen204ProbeResult = 204;
    mCm.reportNetworkConnectivity(mWiFiNetworkAgent.getNetwork(), true);
    captivePortalCallback.expectCallback(CallbackState.LOST, mWiFiNetworkAgent);
    // Expect NET_CAPABILITY_VALIDATED onAvailable callback.
    validatedCallback.expectCallback(CallbackState.AVAILABLE, mWiFiNetworkAgent);
    // Break network connectivity.
    // Expect NET_CAPABILITY_VALIDATED onLost callback.
    mWiFiNetworkAgent.getWrappedNetworkMonitor().gen204ProbeResult = 500;
    mCm.reportNetworkConnectivity(mWiFiNetworkAgent.getNetwork(), false);
    validatedCallback.expectCallback(CallbackState.LOST, mWiFiNetworkAgent);
}
Also used : NetworkRequest(android.net.NetworkRequest) LargeTest(android.test.suitebuilder.annotation.LargeTest)

Example 43 with NetworkRequest

use of android.net.NetworkRequest in project android_frameworks_base by DirtyUnicorns.

the class ConnectivityServiceTest method testMMSonWiFi.

@LargeTest
public void testMMSonWiFi() throws Exception {
    // Test bringing up cellular without MMS NetworkRequest gets reaped
    mCellNetworkAgent = new MockNetworkAgent(TRANSPORT_CELLULAR);
    mCellNetworkAgent.addCapability(NET_CAPABILITY_MMS);
    ConditionVariable cv = mCellNetworkAgent.getDisconnectedCV();
    mCellNetworkAgent.connectWithoutInternet();
    waitFor(cv);
    waitFor(new Criteria() {

        public boolean get() {
            return mCm.getAllNetworks().length == 0;
        }
    });
    verifyNoNetwork();
    // Test bringing up validated WiFi.
    mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI);
    cv = waitForConnectivityBroadcasts(1);
    mWiFiNetworkAgent.connect(true);
    waitFor(cv);
    verifyActiveNetwork(TRANSPORT_WIFI);
    // 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 unvalidated cellular with MMS
    mCellNetworkAgent = new MockNetworkAgent(TRANSPORT_CELLULAR);
    mCellNetworkAgent.addCapability(NET_CAPABILITY_MMS);
    mCellNetworkAgent.connectWithoutInternet();
    networkCallback.expectCallback(CallbackState.AVAILABLE, mCellNetworkAgent);
    verifyActiveNetwork(TRANSPORT_WIFI);
    // Test releasing NetworkRequest disconnects cellular with MMS
    cv = mCellNetworkAgent.getDisconnectedCV();
    mCm.unregisterNetworkCallback(networkCallback);
    waitFor(cv);
    verifyActiveNetwork(TRANSPORT_WIFI);
}
Also used : ConditionVariable(android.os.ConditionVariable) NetworkRequest(android.net.NetworkRequest) LargeTest(android.test.suitebuilder.annotation.LargeTest)

Example 44 with NetworkRequest

use of android.net.NetworkRequest in project android_frameworks_base by DirtyUnicorns.

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.expectCallback(CallbackState.AVAILABLE, mCellNetworkAgent);
    fgCallback.expectCallback(CallbackState.AVAILABLE, mCellNetworkAgent);
    assertTrue(isForegroundNetwork(mCellNetworkAgent));
    mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI);
    mWiFiNetworkAgent.connect(true);
    // When wifi connects, cell lingers.
    callback.expectCallback(CallbackState.AVAILABLE, mWiFiNetworkAgent);
    fgCallback.expectCallback(CallbackState.AVAILABLE, mWiFiNetworkAgent);
    callback.expectCallback(CallbackState.LOSING, mCellNetworkAgent);
    fgCallback.expectCallback(CallbackState.LOSING, mCellNetworkAgent);
    assertTrue(isForegroundNetwork(mCellNetworkAgent));
    assertTrue(isForegroundNetwork(mWiFiNetworkAgent));
    // When lingering is complete, cell is still there but is now in the background.
    fgCallback.expectCallback(CallbackState.LOST, mCellNetworkAgent, TEST_LINGER_DELAY_MS);
    callback.assertNoCallback();
    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);
    cellCallback.expectCallback(CallbackState.AVAILABLE, mCellNetworkAgent);
    fgCallback.expectCallback(CallbackState.AVAILABLE, mCellNetworkAgent);
    // Because the network is already up.
    callback.assertNoCallback();
    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);
    callback.assertNoCallback();
    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.expectCallback(CallbackState.AVAILABLE, mCellNetworkAgent);
    assertTrue(isForegroundNetwork(mCellNetworkAgent));
    mCm.unregisterNetworkCallback(callback);
    mCm.unregisterNetworkCallback(fgCallback);
}
Also used : NetworkRequest(android.net.NetworkRequest) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 45 with NetworkRequest

use of android.net.NetworkRequest in project android_frameworks_base by DirtyUnicorns.

the class ConnectivityServiceTest method testAvoidOrIgnoreCaptivePortals.

@LargeTest
public void testAvoidOrIgnoreCaptivePortals() {
    final TestNetworkCallback captivePortalCallback = new TestNetworkCallback();
    final NetworkRequest captivePortalRequest = new NetworkRequest.Builder().addCapability(NET_CAPABILITY_CAPTIVE_PORTAL).build();
    mCm.registerNetworkCallback(captivePortalRequest, captivePortalCallback);
    final TestNetworkCallback validatedCallback = new TestNetworkCallback();
    final NetworkRequest validatedRequest = new NetworkRequest.Builder().addCapability(NET_CAPABILITY_VALIDATED).build();
    mCm.registerNetworkCallback(validatedRequest, validatedCallback);
    setCaptivePortalMode(Settings.Global.CAPTIVE_PORTAL_MODE_AVOID);
    // Bring up a network with a captive portal.
    // Expect it to fail to connect and not result in any callbacks.
    mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI);
    String firstRedirectUrl = "http://example.com/firstPath";
    ConditionVariable disconnectCv = mWiFiNetworkAgent.getDisconnectedCV();
    ConditionVariable avoidCv = mWiFiNetworkAgent.getPreventReconnectReceived();
    mWiFiNetworkAgent.connectWithCaptivePortal(firstRedirectUrl);
    waitFor(disconnectCv);
    waitFor(avoidCv);
    assertNoCallbacks(captivePortalCallback, validatedCallback);
    // Now test ignore mode.
    setCaptivePortalMode(Settings.Global.CAPTIVE_PORTAL_MODE_IGNORE);
    // Bring up a network with a captive portal.
    // Since we're ignoring captive portals, the network will validate.
    mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI);
    String secondRedirectUrl = "http://example.com/secondPath";
    mWiFiNetworkAgent.connectWithCaptivePortal(secondRedirectUrl);
    // Expect NET_CAPABILITY_VALIDATED onAvailable callback.
    validatedCallback.expectCallback(CallbackState.AVAILABLE, mWiFiNetworkAgent);
    // But there should be no CaptivePortal callback.
    captivePortalCallback.assertNoCallback();
}
Also used : ConditionVariable(android.os.ConditionVariable) NetworkRequest(android.net.NetworkRequest) LargeTest(android.test.suitebuilder.annotation.LargeTest)

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