Search in sources :

Example 96 with ConditionVariable

use of android.os.ConditionVariable 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 97 with ConditionVariable

use of android.os.ConditionVariable in project android_frameworks_base by ResurrectionRemix.

the class ConnectivityServiceTest method testLingering.

@LargeTest
public void testLingering() throws Exception {
    verifyNoNetwork();
    mCellNetworkAgent = new MockNetworkAgent(TRANSPORT_CELLULAR);
    mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI);
    assertNull(mCm.getActiveNetworkInfo());
    assertNull(mCm.getActiveNetwork());
    // Test bringing up validated cellular.
    ConditionVariable cv = waitForConnectivityBroadcasts(1);
    mCellNetworkAgent.connect(true);
    waitFor(cv);
    verifyActiveNetwork(TRANSPORT_CELLULAR);
    assertEquals(2, mCm.getAllNetworks().length);
    assertTrue(mCm.getAllNetworks()[0].equals(mCm.getActiveNetwork()) || mCm.getAllNetworks()[1].equals(mCm.getActiveNetwork()));
    assertTrue(mCm.getAllNetworks()[0].equals(mWiFiNetworkAgent.getNetwork()) || mCm.getAllNetworks()[1].equals(mWiFiNetworkAgent.getNetwork()));
    // Test bringing up validated WiFi.
    cv = waitForConnectivityBroadcasts(2);
    mWiFiNetworkAgent.connect(true);
    waitFor(cv);
    verifyActiveNetwork(TRANSPORT_WIFI);
    assertEquals(2, mCm.getAllNetworks().length);
    assertTrue(mCm.getAllNetworks()[0].equals(mCm.getActiveNetwork()) || mCm.getAllNetworks()[1].equals(mCm.getActiveNetwork()));
    assertTrue(mCm.getAllNetworks()[0].equals(mCellNetworkAgent.getNetwork()) || mCm.getAllNetworks()[1].equals(mCellNetworkAgent.getNetwork()));
    // Test cellular linger timeout.
    waitFor(new Criteria() {

        public boolean get() {
            return mCm.getAllNetworks().length == 1;
        }
    });
    verifyActiveNetwork(TRANSPORT_WIFI);
    assertEquals(1, mCm.getAllNetworks().length);
    assertEquals(mCm.getAllNetworks()[0], mCm.getActiveNetwork());
    // Test WiFi disconnect.
    cv = waitForConnectivityBroadcasts(1);
    mWiFiNetworkAgent.disconnect();
    waitFor(cv);
    verifyNoNetwork();
}
Also used : ConditionVariable(android.os.ConditionVariable) LargeTest(android.test.suitebuilder.annotation.LargeTest)

Example 98 with ConditionVariable

use of android.os.ConditionVariable in project android_frameworks_base by ResurrectionRemix.

the class ConnectivityServiceTest method testNetworkPinner.

@SmallTest
public void testNetworkPinner() {
    NetworkRequest wifiRequest = new NetworkRequest.Builder().addTransportType(TRANSPORT_WIFI).build();
    assertNull(mCm.getBoundNetworkForProcess());
    TestNetworkPinner.pin(mServiceContext, wifiRequest);
    assertNull(mCm.getBoundNetworkForProcess());
    mCellNetworkAgent = new MockNetworkAgent(TRANSPORT_CELLULAR);
    mCellNetworkAgent.connect(true);
    mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI);
    mWiFiNetworkAgent.connect(false);
    // When wi-fi connects, expect to be pinned.
    assertTrue(TestNetworkPinner.awaitPin(100));
    assertPinnedToWifiWithCellDefault();
    // Disconnect and expect the pin to drop.
    mWiFiNetworkAgent.disconnect();
    assertTrue(TestNetworkPinner.awaitUnpin(100));
    assertNotPinnedToWifi();
    // Reconnecting does not cause the pin to come back.
    mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI);
    mWiFiNetworkAgent.connect(false);
    assertFalse(TestNetworkPinner.awaitPin(100));
    assertNotPinnedToWifi();
    // Pinning while connected causes the pin to take effect immediately.
    TestNetworkPinner.pin(mServiceContext, wifiRequest);
    assertTrue(TestNetworkPinner.awaitPin(100));
    assertPinnedToWifiWithCellDefault();
    // Explicitly unpin and expect to use the default network again.
    TestNetworkPinner.unpin();
    assertNotPinnedToWifi();
    // Disconnect cell and wifi.
    // cell down, wifi up, wifi down.
    ConditionVariable cv = waitForConnectivityBroadcasts(3);
    mCellNetworkAgent.disconnect();
    mWiFiNetworkAgent.disconnect();
    waitFor(cv);
    // Pinning takes effect even if the pinned network is the default when the pin is set...
    TestNetworkPinner.pin(mServiceContext, wifiRequest);
    mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI);
    mWiFiNetworkAgent.connect(false);
    assertTrue(TestNetworkPinner.awaitPin(100));
    assertPinnedToWifiWithWifiDefault();
    // ... and is maintained even when that network is no longer the default.
    cv = waitForConnectivityBroadcasts(1);
    mCellNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI);
    mCellNetworkAgent.connect(true);
    waitFor(cv);
    assertPinnedToWifiWithCellDefault();
}
Also used : ConditionVariable(android.os.ConditionVariable) NetworkRequest(android.net.NetworkRequest) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 99 with ConditionVariable

use of android.os.ConditionVariable in project android_frameworks_base by ResurrectionRemix.

the class ConnectivityServiceTest method connectKeepaliveNetwork.

private Network connectKeepaliveNetwork(LinkProperties lp) {
    // Ensure the network is disconnected before we do anything.
    if (mWiFiNetworkAgent != null) {
        assertNull(mCm.getNetworkCapabilities(mWiFiNetworkAgent.getNetwork()));
    }
    mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI);
    ConditionVariable cv = waitForConnectivityBroadcasts(1);
    mWiFiNetworkAgent.connect(true);
    waitFor(cv);
    verifyActiveNetwork(TRANSPORT_WIFI);
    mWiFiNetworkAgent.sendLinkProperties(lp);
    mService.waitForIdle();
    return mWiFiNetworkAgent.getNetwork();
}
Also used : ConditionVariable(android.os.ConditionVariable)

Example 100 with ConditionVariable

use of android.os.ConditionVariable in project android_frameworks_base by ResurrectionRemix.

the class ConnectivityServiceTest method testWiFiFallback.

@LargeTest
public void testWiFiFallback() throws Exception {
    // Test bringing up unvalidated WiFi.
    mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI);
    ConditionVariable cv = waitForConnectivityBroadcasts(1);
    mWiFiNetworkAgent.connect(false);
    waitFor(cv);
    verifyActiveNetwork(TRANSPORT_WIFI);
    // Test bringing up validated cellular.
    mCellNetworkAgent = new MockNetworkAgent(TRANSPORT_CELLULAR);
    cv = waitForConnectivityBroadcasts(2);
    mCellNetworkAgent.connect(true);
    waitFor(cv);
    verifyActiveNetwork(TRANSPORT_CELLULAR);
    // Reevaluate cellular (it'll instantly fail DNS).
    cv = waitForConnectivityBroadcasts(2);
    assertTrue(mCm.getNetworkCapabilities(mCellNetworkAgent.getNetwork()).hasCapability(NET_CAPABILITY_VALIDATED));
    mCm.reportBadNetwork(mCellNetworkAgent.getNetwork());
    // Should quickly fall back to WiFi.
    waitFor(cv);
    assertFalse(mCm.getNetworkCapabilities(mCellNetworkAgent.getNetwork()).hasCapability(NET_CAPABILITY_VALIDATED));
    verifyActiveNetwork(TRANSPORT_WIFI);
}
Also used : ConditionVariable(android.os.ConditionVariable) LargeTest(android.test.suitebuilder.annotation.LargeTest)

Aggregations

ConditionVariable (android.os.ConditionVariable)122 LargeTest (android.test.suitebuilder.annotation.LargeTest)36 NetworkRequest (android.net.NetworkRequest)24 SmallTest (android.test.suitebuilder.annotation.SmallTest)17 Handler (android.os.Handler)16 Messenger (android.os.Messenger)8 Test (org.junit.Test)6 Network (android.net.Network)5 Message (android.os.Message)5 PendingIntent (android.app.PendingIntent)4 BroadcastReceiver (android.content.BroadcastReceiver)4 Context (android.content.Context)4 Intent (android.content.Intent)4 IntentFilter (android.content.IntentFilter)4 ConnectivityManager (android.net.ConnectivityManager)4 NetworkCallback (android.net.ConnectivityManager.NetworkCallback)4 DataUsageRequest (android.net.DataUsageRequest)4 NetworkCapabilities (android.net.NetworkCapabilities)4 NetworkStats (android.net.NetworkStats)4 HandlerThread (android.os.HandlerThread)4