Search in sources :

Example 21 with NetworkCallback

use of android.net.ConnectivityManager.NetworkCallback in project android_frameworks_base by DirtyUnicorns.

the class ConnectivityServiceTest method testMultipleLingering.

@SmallTest
public void testMultipleLingering() {
    NetworkRequest request = new NetworkRequest.Builder().clearCapabilities().addCapability(NET_CAPABILITY_NOT_METERED).build();
    TestNetworkCallback callback = new TestNetworkCallback();
    mCm.registerNetworkCallback(request, callback);
    TestNetworkCallback defaultCallback = new TestNetworkCallback();
    mCm.registerDefaultNetworkCallback(defaultCallback);
    mCellNetworkAgent = new MockNetworkAgent(TRANSPORT_CELLULAR);
    mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI);
    mEthernetNetworkAgent = new MockNetworkAgent(TRANSPORT_ETHERNET);
    mCellNetworkAgent.addCapability(NET_CAPABILITY_NOT_METERED);
    mWiFiNetworkAgent.addCapability(NET_CAPABILITY_NOT_METERED);
    mEthernetNetworkAgent.addCapability(NET_CAPABILITY_NOT_METERED);
    mCellNetworkAgent.connect(true);
    callback.expectCallback(CallbackState.AVAILABLE, mCellNetworkAgent);
    defaultCallback.expectCallback(CallbackState.AVAILABLE, mCellNetworkAgent);
    assertEquals(mCellNetworkAgent.getNetwork(), mCm.getActiveNetwork());
    mWiFiNetworkAgent.connect(true);
    // We get AVAILABLE on wifi when wifi connects and satisfies our unmetered request.
    // We then get LOSING when wifi validates and cell is outscored.
    callback.expectCallback(CallbackState.AVAILABLE, mWiFiNetworkAgent);
    callback.expectCallback(CallbackState.LOSING, mCellNetworkAgent);
    defaultCallback.expectCallback(CallbackState.AVAILABLE, mWiFiNetworkAgent);
    assertEquals(mWiFiNetworkAgent.getNetwork(), mCm.getActiveNetwork());
    mEthernetNetworkAgent.connect(true);
    callback.expectCallback(CallbackState.AVAILABLE, mEthernetNetworkAgent);
    callback.expectCallback(CallbackState.LOSING, mWiFiNetworkAgent);
    defaultCallback.expectCallback(CallbackState.AVAILABLE, mEthernetNetworkAgent);
    assertEquals(mEthernetNetworkAgent.getNetwork(), mCm.getActiveNetwork());
    mEthernetNetworkAgent.disconnect();
    callback.expectCallback(CallbackState.LOST, mEthernetNetworkAgent);
    defaultCallback.expectCallback(CallbackState.LOST, mEthernetNetworkAgent);
    defaultCallback.expectCallback(CallbackState.AVAILABLE, mWiFiNetworkAgent);
    for (int i = 0; i < 4; i++) {
        MockNetworkAgent oldNetwork, newNetwork;
        if (i % 2 == 0) {
            mWiFiNetworkAgent.adjustScore(-15);
            oldNetwork = mWiFiNetworkAgent;
            newNetwork = mCellNetworkAgent;
        } else {
            mWiFiNetworkAgent.adjustScore(15);
            oldNetwork = mCellNetworkAgent;
            newNetwork = mWiFiNetworkAgent;
        }
        callback.expectCallback(CallbackState.LOSING, oldNetwork);
        // TODO: should we send an AVAILABLE callback to newNetwork, to indicate that it is no
        // longer lingering?
        defaultCallback.expectCallback(CallbackState.AVAILABLE, newNetwork);
        assertEquals(newNetwork.getNetwork(), mCm.getActiveNetwork());
    }
    assertEquals(mWiFiNetworkAgent.getNetwork(), mCm.getActiveNetwork());
    // Verify that if a network no longer satisfies a request, we send LOST and not LOSING, even
    // if the network is still up.
    mWiFiNetworkAgent.removeCapability(NET_CAPABILITY_NOT_METERED);
    callback.expectCallback(CallbackState.LOST, mWiFiNetworkAgent);
    // Wifi no longer satisfies our listen, which is for an unmetered network.
    // But because its score is 55, it's still up (and the default network).
    defaultCallback.assertNoCallback();
    assertEquals(mWiFiNetworkAgent.getNetwork(), mCm.getActiveNetwork());
    // Disconnect our test networks.
    mWiFiNetworkAgent.disconnect();
    defaultCallback.expectCallback(CallbackState.LOST, mWiFiNetworkAgent);
    defaultCallback.expectCallback(CallbackState.AVAILABLE, mCellNetworkAgent);
    mCellNetworkAgent.disconnect();
    defaultCallback.expectCallback(CallbackState.LOST, mCellNetworkAgent);
    mCm.unregisterNetworkCallback(callback);
    mService.waitForIdle();
    // Check that a network is only lingered or torn down if it would not satisfy a request even
    // if it validated.
    request = new NetworkRequest.Builder().clearCapabilities().build();
    callback = new TestNetworkCallback();
    mCm.registerNetworkCallback(request, callback);
    mCellNetworkAgent = new MockNetworkAgent(TRANSPORT_CELLULAR);
    // Score: 10
    mCellNetworkAgent.connect(false);
    callback.expectCallback(CallbackState.AVAILABLE, mCellNetworkAgent);
    defaultCallback.expectCallback(CallbackState.AVAILABLE, mCellNetworkAgent);
    assertEquals(mCellNetworkAgent.getNetwork(), mCm.getActiveNetwork());
    // Bring up wifi with a score of 20.
    // Cell stays up because it would satisfy the default request if it validated.
    mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI);
    // Score: 20
    mWiFiNetworkAgent.connect(false);
    callback.expectCallback(CallbackState.AVAILABLE, mWiFiNetworkAgent);
    defaultCallback.expectCallback(CallbackState.AVAILABLE, mWiFiNetworkAgent);
    assertEquals(mWiFiNetworkAgent.getNetwork(), mCm.getActiveNetwork());
    mWiFiNetworkAgent.disconnect();
    callback.expectCallback(CallbackState.LOST, mWiFiNetworkAgent);
    defaultCallback.expectCallback(CallbackState.LOST, mWiFiNetworkAgent);
    defaultCallback.expectCallback(CallbackState.AVAILABLE, mCellNetworkAgent);
    assertEquals(mCellNetworkAgent.getNetwork(), mCm.getActiveNetwork());
    // Bring up wifi with a score of 70.
    // Cell is lingered because it would not satisfy any request, even if it validated.
    mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI);
    mWiFiNetworkAgent.adjustScore(50);
    // Score: 70
    mWiFiNetworkAgent.connect(false);
    callback.expectCallback(CallbackState.AVAILABLE, mWiFiNetworkAgent);
    callback.expectCallback(CallbackState.LOSING, mCellNetworkAgent);
    defaultCallback.expectCallback(CallbackState.AVAILABLE, mWiFiNetworkAgent);
    assertEquals(mWiFiNetworkAgent.getNetwork(), mCm.getActiveNetwork());
    // Tear down wifi.
    mWiFiNetworkAgent.disconnect();
    callback.expectCallback(CallbackState.LOST, mWiFiNetworkAgent);
    defaultCallback.expectCallback(CallbackState.LOST, mWiFiNetworkAgent);
    defaultCallback.expectCallback(CallbackState.AVAILABLE, mCellNetworkAgent);
    assertEquals(mCellNetworkAgent.getNetwork(), mCm.getActiveNetwork());
    // Bring up wifi, then validate it. Previous versions would immediately tear down cell, but
    // it's arguably correct to linger it, since it was the default network before it validated.
    mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI);
    mWiFiNetworkAgent.connect(true);
    callback.expectCallback(CallbackState.AVAILABLE, mWiFiNetworkAgent);
    callback.expectCallback(CallbackState.LOSING, mCellNetworkAgent);
    defaultCallback.expectCallback(CallbackState.AVAILABLE, mWiFiNetworkAgent);
    assertEquals(mWiFiNetworkAgent.getNetwork(), mCm.getActiveNetwork());
    mWiFiNetworkAgent.disconnect();
    callback.expectCallback(CallbackState.LOST, mWiFiNetworkAgent);
    defaultCallback.expectCallback(CallbackState.LOST, mWiFiNetworkAgent);
    defaultCallback.expectCallback(CallbackState.AVAILABLE, mCellNetworkAgent);
    mCellNetworkAgent.disconnect();
    callback.expectCallback(CallbackState.LOST, mCellNetworkAgent);
    defaultCallback.expectCallback(CallbackState.LOST, mCellNetworkAgent);
    // If a network is lingering, and we add and remove a request from it, resume lingering.
    mCellNetworkAgent = new MockNetworkAgent(TRANSPORT_CELLULAR);
    mCellNetworkAgent.connect(true);
    callback.expectCallback(CallbackState.AVAILABLE, mCellNetworkAgent);
    defaultCallback.expectCallback(CallbackState.AVAILABLE, mCellNetworkAgent);
    mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI);
    mWiFiNetworkAgent.connect(true);
    callback.expectCallback(CallbackState.AVAILABLE, mWiFiNetworkAgent);
    defaultCallback.expectCallback(CallbackState.AVAILABLE, mWiFiNetworkAgent);
    callback.expectCallback(CallbackState.LOSING, mCellNetworkAgent);
    NetworkRequest cellRequest = new NetworkRequest.Builder().addTransportType(TRANSPORT_CELLULAR).build();
    NetworkCallback noopCallback = new NetworkCallback();
    mCm.requestNetwork(cellRequest, noopCallback);
    // TODO: should this cause an AVAILABLE callback, to indicate that the network is no longer
    // lingering?
    mCm.unregisterNetworkCallback(noopCallback);
    callback.expectCallback(CallbackState.LOSING, mCellNetworkAgent);
    // Similar to the above: lingering can start even after the lingered request is removed.
    // Disconnect wifi and switch to cell.
    mWiFiNetworkAgent.disconnect();
    callback.expectCallback(CallbackState.LOST, mWiFiNetworkAgent);
    defaultCallback.expectCallback(CallbackState.LOST, mWiFiNetworkAgent);
    defaultCallback.expectCallback(CallbackState.AVAILABLE, mCellNetworkAgent);
    // Cell is now the default network. Pin it with a cell-specific request.
    // Can't reuse NetworkCallbacks. http://b/20701525
    noopCallback = new NetworkCallback();
    mCm.requestNetwork(cellRequest, noopCallback);
    // Now connect wifi, and expect it to become the default network.
    mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI);
    mWiFiNetworkAgent.connect(true);
    callback.expectCallback(CallbackState.AVAILABLE, mWiFiNetworkAgent);
    defaultCallback.expectCallback(CallbackState.AVAILABLE, mWiFiNetworkAgent);
    // The default request is lingering on cell, but nothing happens to cell, and we send no
    // callbacks for it, because it's kept up by cellRequest.
    callback.assertNoCallback();
    // Now unregister cellRequest and expect cell to start lingering.
    mCm.unregisterNetworkCallback(noopCallback);
    callback.expectCallback(CallbackState.LOSING, mCellNetworkAgent);
    // Let linger run its course.
    callback.assertNoCallback();
    callback.expectCallback(CallbackState.LOST, mCellNetworkAgent, TEST_LINGER_DELAY_MS);
    // Clean up.
    mWiFiNetworkAgent.disconnect();
    callback.expectCallback(CallbackState.LOST, mWiFiNetworkAgent);
    defaultCallback.expectCallback(CallbackState.LOST, mWiFiNetworkAgent);
    mCm.unregisterNetworkCallback(callback);
    mCm.unregisterNetworkCallback(defaultCallback);
}
Also used : NetworkRequest(android.net.NetworkRequest) NetworkCallback(android.net.ConnectivityManager.NetworkCallback) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 22 with NetworkCallback

use of android.net.ConnectivityManager.NetworkCallback in project android_frameworks_base by ResurrectionRemix.

the class ConnectivityServiceTest method testMultipleLingering.

@SmallTest
public void testMultipleLingering() {
    NetworkRequest request = new NetworkRequest.Builder().clearCapabilities().addCapability(NET_CAPABILITY_NOT_METERED).build();
    TestNetworkCallback callback = new TestNetworkCallback();
    mCm.registerNetworkCallback(request, callback);
    TestNetworkCallback defaultCallback = new TestNetworkCallback();
    mCm.registerDefaultNetworkCallback(defaultCallback);
    mCellNetworkAgent = new MockNetworkAgent(TRANSPORT_CELLULAR);
    mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI);
    mEthernetNetworkAgent = new MockNetworkAgent(TRANSPORT_ETHERNET);
    mCellNetworkAgent.addCapability(NET_CAPABILITY_NOT_METERED);
    mWiFiNetworkAgent.addCapability(NET_CAPABILITY_NOT_METERED);
    mEthernetNetworkAgent.addCapability(NET_CAPABILITY_NOT_METERED);
    mCellNetworkAgent.connect(true);
    callback.expectCallback(CallbackState.AVAILABLE, mCellNetworkAgent);
    defaultCallback.expectCallback(CallbackState.AVAILABLE, mCellNetworkAgent);
    assertEquals(mCellNetworkAgent.getNetwork(), mCm.getActiveNetwork());
    mWiFiNetworkAgent.connect(true);
    // We get AVAILABLE on wifi when wifi connects and satisfies our unmetered request.
    // We then get LOSING when wifi validates and cell is outscored.
    callback.expectCallback(CallbackState.AVAILABLE, mWiFiNetworkAgent);
    callback.expectCallback(CallbackState.LOSING, mCellNetworkAgent);
    defaultCallback.expectCallback(CallbackState.AVAILABLE, mWiFiNetworkAgent);
    assertEquals(mWiFiNetworkAgent.getNetwork(), mCm.getActiveNetwork());
    mEthernetNetworkAgent.connect(true);
    callback.expectCallback(CallbackState.AVAILABLE, mEthernetNetworkAgent);
    callback.expectCallback(CallbackState.LOSING, mWiFiNetworkAgent);
    defaultCallback.expectCallback(CallbackState.AVAILABLE, mEthernetNetworkAgent);
    assertEquals(mEthernetNetworkAgent.getNetwork(), mCm.getActiveNetwork());
    mEthernetNetworkAgent.disconnect();
    callback.expectCallback(CallbackState.LOST, mEthernetNetworkAgent);
    defaultCallback.expectCallback(CallbackState.LOST, mEthernetNetworkAgent);
    defaultCallback.expectCallback(CallbackState.AVAILABLE, mWiFiNetworkAgent);
    for (int i = 0; i < 4; i++) {
        MockNetworkAgent oldNetwork, newNetwork;
        if (i % 2 == 0) {
            mWiFiNetworkAgent.adjustScore(-15);
            oldNetwork = mWiFiNetworkAgent;
            newNetwork = mCellNetworkAgent;
        } else {
            mWiFiNetworkAgent.adjustScore(15);
            oldNetwork = mCellNetworkAgent;
            newNetwork = mWiFiNetworkAgent;
        }
        callback.expectCallback(CallbackState.LOSING, oldNetwork);
        // TODO: should we send an AVAILABLE callback to newNetwork, to indicate that it is no
        // longer lingering?
        defaultCallback.expectCallback(CallbackState.AVAILABLE, newNetwork);
        assertEquals(newNetwork.getNetwork(), mCm.getActiveNetwork());
    }
    assertEquals(mWiFiNetworkAgent.getNetwork(), mCm.getActiveNetwork());
    // Verify that if a network no longer satisfies a request, we send LOST and not LOSING, even
    // if the network is still up.
    mWiFiNetworkAgent.removeCapability(NET_CAPABILITY_NOT_METERED);
    callback.expectCallback(CallbackState.LOST, mWiFiNetworkAgent);
    // Wifi no longer satisfies our listen, which is for an unmetered network.
    // But because its score is 55, it's still up (and the default network).
    defaultCallback.assertNoCallback();
    assertEquals(mWiFiNetworkAgent.getNetwork(), mCm.getActiveNetwork());
    // Disconnect our test networks.
    mWiFiNetworkAgent.disconnect();
    defaultCallback.expectCallback(CallbackState.LOST, mWiFiNetworkAgent);
    defaultCallback.expectCallback(CallbackState.AVAILABLE, mCellNetworkAgent);
    mCellNetworkAgent.disconnect();
    defaultCallback.expectCallback(CallbackState.LOST, mCellNetworkAgent);
    mCm.unregisterNetworkCallback(callback);
    mService.waitForIdle();
    // Check that a network is only lingered or torn down if it would not satisfy a request even
    // if it validated.
    request = new NetworkRequest.Builder().clearCapabilities().build();
    callback = new TestNetworkCallback();
    mCm.registerNetworkCallback(request, callback);
    mCellNetworkAgent = new MockNetworkAgent(TRANSPORT_CELLULAR);
    // Score: 10
    mCellNetworkAgent.connect(false);
    callback.expectCallback(CallbackState.AVAILABLE, mCellNetworkAgent);
    defaultCallback.expectCallback(CallbackState.AVAILABLE, mCellNetworkAgent);
    assertEquals(mCellNetworkAgent.getNetwork(), mCm.getActiveNetwork());
    // Bring up wifi with a score of 20.
    // Cell stays up because it would satisfy the default request if it validated.
    mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI);
    // Score: 20
    mWiFiNetworkAgent.connect(false);
    callback.expectCallback(CallbackState.AVAILABLE, mWiFiNetworkAgent);
    defaultCallback.expectCallback(CallbackState.AVAILABLE, mWiFiNetworkAgent);
    assertEquals(mWiFiNetworkAgent.getNetwork(), mCm.getActiveNetwork());
    mWiFiNetworkAgent.disconnect();
    callback.expectCallback(CallbackState.LOST, mWiFiNetworkAgent);
    defaultCallback.expectCallback(CallbackState.LOST, mWiFiNetworkAgent);
    defaultCallback.expectCallback(CallbackState.AVAILABLE, mCellNetworkAgent);
    assertEquals(mCellNetworkAgent.getNetwork(), mCm.getActiveNetwork());
    // Bring up wifi with a score of 70.
    // Cell is lingered because it would not satisfy any request, even if it validated.
    mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI);
    mWiFiNetworkAgent.adjustScore(50);
    // Score: 70
    mWiFiNetworkAgent.connect(false);
    callback.expectCallback(CallbackState.AVAILABLE, mWiFiNetworkAgent);
    callback.expectCallback(CallbackState.LOSING, mCellNetworkAgent);
    defaultCallback.expectCallback(CallbackState.AVAILABLE, mWiFiNetworkAgent);
    assertEquals(mWiFiNetworkAgent.getNetwork(), mCm.getActiveNetwork());
    // Tear down wifi.
    mWiFiNetworkAgent.disconnect();
    callback.expectCallback(CallbackState.LOST, mWiFiNetworkAgent);
    defaultCallback.expectCallback(CallbackState.LOST, mWiFiNetworkAgent);
    defaultCallback.expectCallback(CallbackState.AVAILABLE, mCellNetworkAgent);
    assertEquals(mCellNetworkAgent.getNetwork(), mCm.getActiveNetwork());
    // Bring up wifi, then validate it. Previous versions would immediately tear down cell, but
    // it's arguably correct to linger it, since it was the default network before it validated.
    mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI);
    mWiFiNetworkAgent.connect(true);
    callback.expectCallback(CallbackState.AVAILABLE, mWiFiNetworkAgent);
    callback.expectCallback(CallbackState.LOSING, mCellNetworkAgent);
    defaultCallback.expectCallback(CallbackState.AVAILABLE, mWiFiNetworkAgent);
    assertEquals(mWiFiNetworkAgent.getNetwork(), mCm.getActiveNetwork());
    mWiFiNetworkAgent.disconnect();
    callback.expectCallback(CallbackState.LOST, mWiFiNetworkAgent);
    defaultCallback.expectCallback(CallbackState.LOST, mWiFiNetworkAgent);
    defaultCallback.expectCallback(CallbackState.AVAILABLE, mCellNetworkAgent);
    mCellNetworkAgent.disconnect();
    callback.expectCallback(CallbackState.LOST, mCellNetworkAgent);
    defaultCallback.expectCallback(CallbackState.LOST, mCellNetworkAgent);
    // If a network is lingering, and we add and remove a request from it, resume lingering.
    mCellNetworkAgent = new MockNetworkAgent(TRANSPORT_CELLULAR);
    mCellNetworkAgent.connect(true);
    callback.expectCallback(CallbackState.AVAILABLE, mCellNetworkAgent);
    defaultCallback.expectCallback(CallbackState.AVAILABLE, mCellNetworkAgent);
    mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI);
    mWiFiNetworkAgent.connect(true);
    callback.expectCallback(CallbackState.AVAILABLE, mWiFiNetworkAgent);
    defaultCallback.expectCallback(CallbackState.AVAILABLE, mWiFiNetworkAgent);
    callback.expectCallback(CallbackState.LOSING, mCellNetworkAgent);
    NetworkRequest cellRequest = new NetworkRequest.Builder().addTransportType(TRANSPORT_CELLULAR).build();
    NetworkCallback noopCallback = new NetworkCallback();
    mCm.requestNetwork(cellRequest, noopCallback);
    // TODO: should this cause an AVAILABLE callback, to indicate that the network is no longer
    // lingering?
    mCm.unregisterNetworkCallback(noopCallback);
    callback.expectCallback(CallbackState.LOSING, mCellNetworkAgent);
    // Similar to the above: lingering can start even after the lingered request is removed.
    // Disconnect wifi and switch to cell.
    mWiFiNetworkAgent.disconnect();
    callback.expectCallback(CallbackState.LOST, mWiFiNetworkAgent);
    defaultCallback.expectCallback(CallbackState.LOST, mWiFiNetworkAgent);
    defaultCallback.expectCallback(CallbackState.AVAILABLE, mCellNetworkAgent);
    // Cell is now the default network. Pin it with a cell-specific request.
    // Can't reuse NetworkCallbacks. http://b/20701525
    noopCallback = new NetworkCallback();
    mCm.requestNetwork(cellRequest, noopCallback);
    // Now connect wifi, and expect it to become the default network.
    mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI);
    mWiFiNetworkAgent.connect(true);
    callback.expectCallback(CallbackState.AVAILABLE, mWiFiNetworkAgent);
    defaultCallback.expectCallback(CallbackState.AVAILABLE, mWiFiNetworkAgent);
    // The default request is lingering on cell, but nothing happens to cell, and we send no
    // callbacks for it, because it's kept up by cellRequest.
    callback.assertNoCallback();
    // Now unregister cellRequest and expect cell to start lingering.
    mCm.unregisterNetworkCallback(noopCallback);
    callback.expectCallback(CallbackState.LOSING, mCellNetworkAgent);
    // Let linger run its course.
    callback.assertNoCallback();
    callback.expectCallback(CallbackState.LOST, mCellNetworkAgent, TEST_LINGER_DELAY_MS);
    // Clean up.
    mWiFiNetworkAgent.disconnect();
    callback.expectCallback(CallbackState.LOST, mWiFiNetworkAgent);
    defaultCallback.expectCallback(CallbackState.LOST, mWiFiNetworkAgent);
    mCm.unregisterNetworkCallback(callback);
    mCm.unregisterNetworkCallback(defaultCallback);
}
Also used : NetworkRequest(android.net.NetworkRequest) NetworkCallback(android.net.ConnectivityManager.NetworkCallback) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 23 with NetworkCallback

use of android.net.ConnectivityManager.NetworkCallback in project android_frameworks_base by ResurrectionRemix.

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 24 with NetworkCallback

use of android.net.ConnectivityManager.NetworkCallback in project android_frameworks_base by ResurrectionRemix.

the class ConnectivityServiceTest method tryNetworkFactoryRequests.

private void tryNetworkFactoryRequests(int capability) throws Exception {
    // Verify NOT_RESTRICTED is set appropriately
    final NetworkCapabilities nc = new NetworkRequest.Builder().addCapability(capability).build().networkCapabilities;
    if (capability == NET_CAPABILITY_CBS || capability == NET_CAPABILITY_DUN || capability == NET_CAPABILITY_EIMS || capability == NET_CAPABILITY_FOTA || capability == NET_CAPABILITY_IA || capability == NET_CAPABILITY_IMS || capability == NET_CAPABILITY_RCS || capability == NET_CAPABILITY_XCAP) {
        assertFalse(nc.hasCapability(NET_CAPABILITY_NOT_RESTRICTED));
    } else {
        assertTrue(nc.hasCapability(NET_CAPABILITY_NOT_RESTRICTED));
    }
    NetworkCapabilities filter = new NetworkCapabilities();
    filter.addCapability(capability);
    final HandlerThread handlerThread = new HandlerThread("testNetworkFactoryRequests");
    handlerThread.start();
    final MockNetworkFactory testFactory = new MockNetworkFactory(handlerThread.getLooper(), mServiceContext, "testFactory", filter);
    testFactory.setScoreFilter(40);
    ConditionVariable cv = testFactory.getNetworkStartedCV();
    testFactory.expectAddRequests(1);
    testFactory.register();
    testFactory.waitForNetworkRequests(1);
    int expectedRequestCount = 1;
    NetworkCallback networkCallback = null;
    // add one.
    if (capability != NET_CAPABILITY_INTERNET) {
        assertFalse(testFactory.getMyStartRequested());
        NetworkRequest request = new NetworkRequest.Builder().addCapability(capability).build();
        networkCallback = new NetworkCallback();
        testFactory.expectAddRequests(1);
        mCm.requestNetwork(request, networkCallback);
        expectedRequestCount++;
        testFactory.waitForNetworkRequests(expectedRequestCount);
    }
    waitFor(cv);
    assertEquals(expectedRequestCount, testFactory.getMyRequestCount());
    assertTrue(testFactory.getMyStartRequested());
    // Now bring in a higher scored network.
    MockNetworkAgent testAgent = new MockNetworkAgent(TRANSPORT_CELLULAR);
    // Rather than create a validated network which complicates things by registering it's
    // own NetworkRequest during startup, just bump up the score to cancel out the
    // unvalidated penalty.
    testAgent.adjustScore(40);
    cv = testFactory.getNetworkStoppedCV();
    // When testAgent connects, ConnectivityService will re-send us all current requests with
    // the new score. There are expectedRequestCount such requests, and we must wait for all of
    // them.
    testFactory.expectAddRequests(expectedRequestCount);
    testAgent.connect(false);
    testAgent.addCapability(capability);
    waitFor(cv);
    testFactory.waitForNetworkRequests(expectedRequestCount);
    assertFalse(testFactory.getMyStartRequested());
    // Bring in a bunch of requests.
    testFactory.expectAddRequests(10);
    assertEquals(expectedRequestCount, testFactory.getMyRequestCount());
    ConnectivityManager.NetworkCallback[] networkCallbacks = new ConnectivityManager.NetworkCallback[10];
    for (int i = 0; i < networkCallbacks.length; i++) {
        networkCallbacks[i] = new ConnectivityManager.NetworkCallback();
        NetworkRequest.Builder builder = new NetworkRequest.Builder();
        builder.addCapability(capability);
        mCm.requestNetwork(builder.build(), networkCallbacks[i]);
    }
    testFactory.waitForNetworkRequests(10 + expectedRequestCount);
    assertFalse(testFactory.getMyStartRequested());
    // Remove the requests.
    testFactory.expectRemoveRequests(10);
    for (int i = 0; i < networkCallbacks.length; i++) {
        mCm.unregisterNetworkCallback(networkCallbacks[i]);
    }
    testFactory.waitForNetworkRequests(expectedRequestCount);
    assertFalse(testFactory.getMyStartRequested());
    // Drop the higher scored network.
    cv = testFactory.getNetworkStartedCV();
    testAgent.disconnect();
    waitFor(cv);
    assertEquals(expectedRequestCount, testFactory.getMyRequestCount());
    assertTrue(testFactory.getMyStartRequested());
    testFactory.unregister();
    if (networkCallback != null)
        mCm.unregisterNetworkCallback(networkCallback);
    handlerThread.quit();
}
Also used : ConnectivityManager(android.net.ConnectivityManager) NetworkRequest(android.net.NetworkRequest) NetworkCallback(android.net.ConnectivityManager.NetworkCallback) NetworkCapabilities(android.net.NetworkCapabilities) NetworkCallback(android.net.ConnectivityManager.NetworkCallback) ConditionVariable(android.os.ConditionVariable) HandlerThread(android.os.HandlerThread)

Example 25 with NetworkCallback

use of android.net.ConnectivityManager.NetworkCallback in project android_frameworks_base by ResurrectionRemix.

the class ConnectivityServiceTest method testNetworkRequestMaximum.

@SmallTest
public void testNetworkRequestMaximum() {
    final int MAX_REQUESTS = 100;
    // Test that the limit is enforced when MAX_REQUESTS simultaneous requests are added.
    NetworkRequest networkRequest = new NetworkRequest.Builder().build();
    ArrayList<NetworkCallback> networkCallbacks = new ArrayList<NetworkCallback>();
    try {
        for (int i = 0; i < MAX_REQUESTS; i++) {
            NetworkCallback networkCallback = new NetworkCallback();
            mCm.requestNetwork(networkRequest, networkCallback);
            networkCallbacks.add(networkCallback);
        }
        fail("Registering " + MAX_REQUESTS + " NetworkRequests did not throw exception");
    } catch (IllegalArgumentException expected) {
    }
    for (NetworkCallback networkCallback : networkCallbacks) {
        mCm.unregisterNetworkCallback(networkCallback);
    }
    networkCallbacks.clear();
    try {
        for (int i = 0; i < MAX_REQUESTS; i++) {
            NetworkCallback networkCallback = new NetworkCallback();
            mCm.registerNetworkCallback(networkRequest, networkCallback);
            networkCallbacks.add(networkCallback);
        }
        fail("Registering " + MAX_REQUESTS + " NetworkCallbacks did not throw exception");
    } catch (IllegalArgumentException expected) {
    }
    for (NetworkCallback networkCallback : networkCallbacks) {
        mCm.unregisterNetworkCallback(networkCallback);
    }
    networkCallbacks.clear();
    ArrayList<PendingIntent> pendingIntents = new ArrayList<PendingIntent>();
    try {
        for (int i = 0; i < MAX_REQUESTS + 1; i++) {
            PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 0, new Intent("a" + i), 0);
            mCm.requestNetwork(networkRequest, pendingIntent);
            pendingIntents.add(pendingIntent);
        }
        fail("Registering " + MAX_REQUESTS + " PendingIntent NetworkRequests did not throw exception");
    } catch (IllegalArgumentException expected) {
    }
    for (PendingIntent pendingIntent : pendingIntents) {
        mCm.unregisterNetworkCallback(pendingIntent);
    }
    pendingIntents.clear();
    try {
        for (int i = 0; i < MAX_REQUESTS + 1; i++) {
            PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 0, new Intent("a" + i), 0);
            mCm.registerNetworkCallback(networkRequest, pendingIntent);
            pendingIntents.add(pendingIntent);
        }
        fail("Registering " + MAX_REQUESTS + " PendingIntent NetworkCallbacks did not throw exception");
    } catch (IllegalArgumentException expected) {
    }
    for (PendingIntent pendingIntent : pendingIntents) {
        mCm.unregisterNetworkCallback(pendingIntent);
    }
    pendingIntents.clear();
    mService.waitForIdle(5000);
    // Test that the limit is not hit when MAX_REQUESTS requests are added and removed.
    for (int i = 0; i < MAX_REQUESTS; i++) {
        NetworkCallback networkCallback = new NetworkCallback();
        mCm.requestNetwork(networkRequest, networkCallback);
        mCm.unregisterNetworkCallback(networkCallback);
    }
    mService.waitForIdle();
    for (int i = 0; i < MAX_REQUESTS; i++) {
        NetworkCallback networkCallback = new NetworkCallback();
        mCm.registerNetworkCallback(networkRequest, networkCallback);
        mCm.unregisterNetworkCallback(networkCallback);
    }
    mService.waitForIdle();
    for (int i = 0; i < MAX_REQUESTS; i++) {
        PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 0, new Intent("b" + i), 0);
        mCm.requestNetwork(networkRequest, pendingIntent);
        mCm.unregisterNetworkCallback(pendingIntent);
    }
    mService.waitForIdle();
    for (int i = 0; i < MAX_REQUESTS; i++) {
        PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 0, new Intent("c" + i), 0);
        mCm.registerNetworkCallback(networkRequest, pendingIntent);
        mCm.unregisterNetworkCallback(pendingIntent);
    }
}
Also used : NetworkRequest(android.net.NetworkRequest) ArrayList(java.util.ArrayList) PendingIntent(android.app.PendingIntent) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) NetworkCallback(android.net.ConnectivityManager.NetworkCallback) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Aggregations

NetworkCallback (android.net.ConnectivityManager.NetworkCallback)35 NetworkRequest (android.net.NetworkRequest)32 Network (android.net.Network)18 Intent (android.content.Intent)17 NetworkCapabilities (android.net.NetworkCapabilities)16 SmallTest (android.test.suitebuilder.annotation.SmallTest)13 PendingIntent (android.app.PendingIntent)8 NetworkInfo (android.net.NetworkInfo)7 WebSettings (android.webkit.WebSettings)5 WebView (android.webkit.WebView)5 ConnectivityManager (android.net.ConnectivityManager)4 ConditionVariable (android.os.ConditionVariable)4 HandlerThread (android.os.HandlerThread)4 ArrayList (java.util.ArrayList)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 LargeTest (android.test.suitebuilder.annotation.LargeTest)3 Test (org.junit.Test)2