Search in sources :

Example 11 with ConditionVariable

use of android.os.ConditionVariable in project platform_frameworks_base by android.

the class ConnectivityServiceTest method testAvoidOrIgnoreCaptivePortals.

@SmallTest
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.expectAvailableCallbacks(mWiFiNetworkAgent);
    // But there should be no CaptivePortal callback.
    captivePortalCallback.assertNoCallback();
}
Also used : ConditionVariable(android.os.ConditionVariable) NetworkRequest(android.net.NetworkRequest) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 12 with ConditionVariable

use of android.os.ConditionVariable in project platform_frameworks_base by android.

the class ConnectivityServiceTest method testStateChangeNetworkCallbacks.

@SmallTest
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.expectAvailableCallbacks(mCellNetworkAgent);
    cellNetworkCallback.expectAvailableCallbacks(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.expectAvailableCallbacks(mWiFiNetworkAgent);
    wifiNetworkCallback.expectAvailableCallbacks(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.expectAvailableAndValidatedCallbacks(mCellNetworkAgent);
    cellNetworkCallback.expectAvailableAndValidatedCallbacks(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.expectAvailableCallbacks(mWiFiNetworkAgent);
    genericNetworkCallback.expectCallback(CallbackState.LOSING, mCellNetworkAgent);
    genericNetworkCallback.expectCapabilitiesWith(NET_CAPABILITY_VALIDATED, mWiFiNetworkAgent);
    wifiNetworkCallback.expectAvailableAndValidatedCallbacks(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) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 13 with ConditionVariable

use of android.os.ConditionVariable in project VirtualApp by asLody.

the class StubContentProvider method initProcess.

private Bundle initProcess(Bundle extras) {
    ConditionVariable lock = VirtualCore.get().getInitLock();
    if (lock != null) {
        lock.block();
    }
    IBinder token = BundleCompat.getBinder(extras, "_VA_|_binder_");
    int vuid = extras.getInt("_VA_|_vuid_");
    VClientImpl client = VClientImpl.get();
    client.initProcess(token, vuid);
    Bundle res = new Bundle();
    BundleCompat.putBinder(res, "_VA_|_client_", client.asBinder());
    res.putInt("_VA_|_pid_", Process.myPid());
    return res;
}
Also used : ConditionVariable(android.os.ConditionVariable) IBinder(android.os.IBinder) Bundle(android.os.Bundle) VClientImpl(com.lody.virtual.client.VClientImpl)

Example 14 with ConditionVariable

use of android.os.ConditionVariable in project facebook-android-sdk by facebook.

the class FacebookSdkPowerMockTest method testGetExecutor.

@Test
public void testGetExecutor() {
    final ConditionVariable condition = new ConditionVariable();
    FacebookSdk.getExecutor().execute(new Runnable() {

        @Override
        public void run() {
            condition.open();
        }
    });
    boolean success = condition.block(5000);
    assertTrue(success);
}
Also used : ConditionVariable(android.os.ConditionVariable) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 15 with ConditionVariable

use of android.os.ConditionVariable in project facebook-android-sdk by facebook.

the class FacebookSdkPowerMockTest method testSetExecutor.

@Test
public void testSetExecutor() {
    final ConditionVariable condition = new ConditionVariable();
    final Runnable runnable = new Runnable() {

        @Override
        public void run() {
        }
    };
    final Executor executor = new Executor() {

        @Override
        public void execute(Runnable command) {
            assertEquals(runnable, command);
            command.run();
            condition.open();
        }
    };
    Executor original = FacebookSdk.getExecutor();
    try {
        FacebookSdk.setExecutor(executor);
        FacebookSdk.getExecutor().execute(runnable);
        boolean success = condition.block(5000);
        assertTrue(success);
    } finally {
        FacebookSdk.setExecutor(original);
    }
}
Also used : ConditionVariable(android.os.ConditionVariable) Executor(java.util.concurrent.Executor) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

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