Search in sources :

Example 81 with ConditionVariable

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

the class ConnectivityServiceTest method testStateChangeNetworkCallbacks.

@LargeTest
public void testStateChangeNetworkCallbacks() throws Exception {
    final TestNetworkCallback genericNetworkCallback = new TestNetworkCallback();
    final TestNetworkCallback wifiNetworkCallback = new TestNetworkCallback();
    final TestNetworkCallback cellNetworkCallback = new TestNetworkCallback();
    final NetworkRequest genericRequest = new NetworkRequest.Builder().clearCapabilities().build();
    final NetworkRequest wifiRequest = new NetworkRequest.Builder().addTransportType(TRANSPORT_WIFI).build();
    final NetworkRequest cellRequest = new NetworkRequest.Builder().addTransportType(TRANSPORT_CELLULAR).build();
    mCm.registerNetworkCallback(genericRequest, genericNetworkCallback);
    mCm.registerNetworkCallback(wifiRequest, wifiNetworkCallback);
    mCm.registerNetworkCallback(cellRequest, cellNetworkCallback);
    // Test unvalidated networks
    ConditionVariable cv = waitForConnectivityBroadcasts(1);
    mCellNetworkAgent = new MockNetworkAgent(TRANSPORT_CELLULAR);
    mCellNetworkAgent.connect(false);
    genericNetworkCallback.expectCallback(CallbackState.AVAILABLE, mCellNetworkAgent);
    cellNetworkCallback.expectCallback(CallbackState.AVAILABLE, mCellNetworkAgent);
    assertEquals(mCellNetworkAgent.getNetwork(), mCm.getActiveNetwork());
    waitFor(cv);
    assertNoCallbacks(genericNetworkCallback, wifiNetworkCallback, cellNetworkCallback);
    // This should not trigger spurious onAvailable() callbacks, b/21762680.
    mCellNetworkAgent.adjustScore(-1);
    mService.waitForIdle();
    assertNoCallbacks(genericNetworkCallback, wifiNetworkCallback, cellNetworkCallback);
    assertEquals(mCellNetworkAgent.getNetwork(), mCm.getActiveNetwork());
    cv = waitForConnectivityBroadcasts(2);
    mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI);
    mWiFiNetworkAgent.connect(false);
    genericNetworkCallback.expectCallback(CallbackState.AVAILABLE, mWiFiNetworkAgent);
    wifiNetworkCallback.expectCallback(CallbackState.AVAILABLE, mWiFiNetworkAgent);
    assertEquals(mWiFiNetworkAgent.getNetwork(), mCm.getActiveNetwork());
    waitFor(cv);
    assertNoCallbacks(genericNetworkCallback, wifiNetworkCallback, cellNetworkCallback);
    cv = waitForConnectivityBroadcasts(2);
    mWiFiNetworkAgent.disconnect();
    genericNetworkCallback.expectCallback(CallbackState.LOST, mWiFiNetworkAgent);
    wifiNetworkCallback.expectCallback(CallbackState.LOST, mWiFiNetworkAgent);
    cellNetworkCallback.assertNoCallback();
    waitFor(cv);
    assertNoCallbacks(genericNetworkCallback, wifiNetworkCallback, cellNetworkCallback);
    cv = waitForConnectivityBroadcasts(1);
    mCellNetworkAgent.disconnect();
    genericNetworkCallback.expectCallback(CallbackState.LOST, mCellNetworkAgent);
    cellNetworkCallback.expectCallback(CallbackState.LOST, mCellNetworkAgent);
    waitFor(cv);
    assertNoCallbacks(genericNetworkCallback, wifiNetworkCallback, cellNetworkCallback);
    // Test validated networks
    mCellNetworkAgent = new MockNetworkAgent(TRANSPORT_CELLULAR);
    mCellNetworkAgent.connect(true);
    genericNetworkCallback.expectCallback(CallbackState.AVAILABLE, mCellNetworkAgent);
    cellNetworkCallback.expectCallback(CallbackState.AVAILABLE, mCellNetworkAgent);
    assertEquals(mCellNetworkAgent.getNetwork(), mCm.getActiveNetwork());
    assertNoCallbacks(genericNetworkCallback, wifiNetworkCallback, cellNetworkCallback);
    // This should not trigger spurious onAvailable() callbacks, b/21762680.
    mCellNetworkAgent.adjustScore(-1);
    mService.waitForIdle();
    assertNoCallbacks(genericNetworkCallback, wifiNetworkCallback, cellNetworkCallback);
    assertEquals(mCellNetworkAgent.getNetwork(), mCm.getActiveNetwork());
    mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI);
    mWiFiNetworkAgent.connect(true);
    genericNetworkCallback.expectCallback(CallbackState.AVAILABLE, mWiFiNetworkAgent);
    genericNetworkCallback.expectCallback(CallbackState.LOSING, mCellNetworkAgent);
    wifiNetworkCallback.expectCallback(CallbackState.AVAILABLE, mWiFiNetworkAgent);
    cellNetworkCallback.expectCallback(CallbackState.LOSING, mCellNetworkAgent);
    assertEquals(mWiFiNetworkAgent.getNetwork(), mCm.getActiveNetwork());
    assertNoCallbacks(genericNetworkCallback, wifiNetworkCallback, cellNetworkCallback);
    mWiFiNetworkAgent.disconnect();
    genericNetworkCallback.expectCallback(CallbackState.LOST, mWiFiNetworkAgent);
    wifiNetworkCallback.expectCallback(CallbackState.LOST, mWiFiNetworkAgent);
    assertNoCallbacks(genericNetworkCallback, wifiNetworkCallback, cellNetworkCallback);
    mCellNetworkAgent.disconnect();
    genericNetworkCallback.expectCallback(CallbackState.LOST, mCellNetworkAgent);
    cellNetworkCallback.expectCallback(CallbackState.LOST, mCellNetworkAgent);
    assertNoCallbacks(genericNetworkCallback, wifiNetworkCallback, cellNetworkCallback);
}
Also used : ConditionVariable(android.os.ConditionVariable) NetworkRequest(android.net.NetworkRequest) LargeTest(android.test.suitebuilder.annotation.LargeTest)

Example 82 with ConditionVariable

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

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 83 with ConditionVariable

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

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 84 with ConditionVariable

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

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)

Example 85 with ConditionVariable

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

the class ConnectivityServiceTest method testMMSonCell.

@LargeTest
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.expectCallback(CallbackState.AVAILABLE, 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) 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