Search in sources :

Example 26 with NetworkRequest

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

the class ConnectivityServiceTest method testAvoidBadWifi.

@SmallTest
public void testAvoidBadWifi() throws Exception {
    final ContentResolver cr = mServiceContext.getContentResolver();
    final WrappedAvoidBadWifiTracker tracker = mService.getAvoidBadWifiTracker();
    // Pretend we're on a carrier that restricts switching away from bad wifi.
    tracker.configRestrictsAvoidBadWifi = true;
    // File a request for cell to ensure it doesn't go down.
    final TestNetworkCallback cellNetworkCallback = new TestNetworkCallback();
    final NetworkRequest cellRequest = new NetworkRequest.Builder().addTransportType(TRANSPORT_CELLULAR).build();
    mCm.requestNetwork(cellRequest, cellNetworkCallback);
    TestNetworkCallback defaultCallback = new TestNetworkCallback();
    mCm.registerDefaultNetworkCallback(defaultCallback);
    NetworkRequest validatedWifiRequest = new NetworkRequest.Builder().addTransportType(TRANSPORT_WIFI).addCapability(NET_CAPABILITY_VALIDATED).build();
    TestNetworkCallback validatedWifiCallback = new TestNetworkCallback();
    mCm.registerNetworkCallback(validatedWifiRequest, validatedWifiCallback);
    Settings.Global.putInt(cr, Settings.Global.NETWORK_AVOID_BAD_WIFI, 0);
    tracker.reevaluate();
    // Bring up validated cell.
    mCellNetworkAgent = new MockNetworkAgent(TRANSPORT_CELLULAR);
    mCellNetworkAgent.connect(true);
    cellNetworkCallback.expectCallback(CallbackState.AVAILABLE, mCellNetworkAgent);
    defaultCallback.expectCallback(CallbackState.AVAILABLE, mCellNetworkAgent);
    Network cellNetwork = mCellNetworkAgent.getNetwork();
    // Bring up validated wifi.
    mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI);
    mWiFiNetworkAgent.connect(true);
    defaultCallback.expectCallback(CallbackState.AVAILABLE, mWiFiNetworkAgent);
    validatedWifiCallback.expectCallback(CallbackState.AVAILABLE, mWiFiNetworkAgent);
    Network wifiNetwork = mWiFiNetworkAgent.getNetwork();
    // Fail validation on wifi.
    mWiFiNetworkAgent.getWrappedNetworkMonitor().gen204ProbeResult = 599;
    mCm.reportNetworkConnectivity(wifiNetwork, false);
    validatedWifiCallback.expectCallback(CallbackState.LOST, mWiFiNetworkAgent);
    // Because avoid bad wifi is off, we don't switch to cellular.
    defaultCallback.assertNoCallback();
    assertFalse(mCm.getNetworkCapabilities(wifiNetwork).hasCapability(NET_CAPABILITY_VALIDATED));
    assertTrue(mCm.getNetworkCapabilities(cellNetwork).hasCapability(NET_CAPABILITY_VALIDATED));
    assertEquals(mCm.getActiveNetwork(), wifiNetwork);
    // Simulate switching to a carrier that does not restrict avoiding bad wifi, and expect
    // that we switch back to cell.
    tracker.configRestrictsAvoidBadWifi = false;
    tracker.reevaluate();
    defaultCallback.expectCallback(CallbackState.AVAILABLE, mCellNetworkAgent);
    assertEquals(mCm.getActiveNetwork(), cellNetwork);
    // Switch back to a restrictive carrier.
    tracker.configRestrictsAvoidBadWifi = true;
    tracker.reevaluate();
    defaultCallback.expectCallback(CallbackState.AVAILABLE, mWiFiNetworkAgent);
    assertEquals(mCm.getActiveNetwork(), wifiNetwork);
    // Simulate the user selecting "switch" on the dialog, and check that we switch to cell.
    mCm.setAvoidUnvalidated(wifiNetwork);
    defaultCallback.expectCallback(CallbackState.AVAILABLE, mCellNetworkAgent);
    assertFalse(mCm.getNetworkCapabilities(wifiNetwork).hasCapability(NET_CAPABILITY_VALIDATED));
    assertTrue(mCm.getNetworkCapabilities(cellNetwork).hasCapability(NET_CAPABILITY_VALIDATED));
    assertEquals(mCm.getActiveNetwork(), cellNetwork);
    // Disconnect and reconnect wifi to clear the one-time switch above.
    mWiFiNetworkAgent.disconnect();
    mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI);
    mWiFiNetworkAgent.connect(true);
    defaultCallback.expectCallback(CallbackState.AVAILABLE, mWiFiNetworkAgent);
    validatedWifiCallback.expectCallback(CallbackState.AVAILABLE, mWiFiNetworkAgent);
    wifiNetwork = mWiFiNetworkAgent.getNetwork();
    // Fail validation on wifi and expect the dialog to appear.
    mWiFiNetworkAgent.getWrappedNetworkMonitor().gen204ProbeResult = 599;
    mCm.reportNetworkConnectivity(wifiNetwork, false);
    validatedWifiCallback.expectCallback(CallbackState.LOST, mWiFiNetworkAgent);
    // Simulate the user selecting "switch" and checking the don't ask again checkbox.
    Settings.Global.putInt(cr, Settings.Global.NETWORK_AVOID_BAD_WIFI, 1);
    tracker.reevaluate();
    // We now switch to cell.
    defaultCallback.expectCallback(CallbackState.AVAILABLE, mCellNetworkAgent);
    assertFalse(mCm.getNetworkCapabilities(wifiNetwork).hasCapability(NET_CAPABILITY_VALIDATED));
    assertTrue(mCm.getNetworkCapabilities(cellNetwork).hasCapability(NET_CAPABILITY_VALIDATED));
    assertEquals(mCm.getActiveNetwork(), cellNetwork);
    // Simulate the user turning the cellular fallback setting off and then on.
    // We switch to wifi and then to cell.
    Settings.Global.putString(cr, Settings.Global.NETWORK_AVOID_BAD_WIFI, null);
    tracker.reevaluate();
    defaultCallback.expectCallback(CallbackState.AVAILABLE, mWiFiNetworkAgent);
    assertEquals(mCm.getActiveNetwork(), wifiNetwork);
    Settings.Global.putInt(cr, Settings.Global.NETWORK_AVOID_BAD_WIFI, 1);
    tracker.reevaluate();
    defaultCallback.expectCallback(CallbackState.AVAILABLE, mCellNetworkAgent);
    assertEquals(mCm.getActiveNetwork(), cellNetwork);
    // If cell goes down, we switch to wifi.
    mCellNetworkAgent.disconnect();
    defaultCallback.expectCallback(CallbackState.LOST, mCellNetworkAgent);
    defaultCallback.expectCallback(CallbackState.AVAILABLE, mWiFiNetworkAgent);
    validatedWifiCallback.assertNoCallback();
    mCm.unregisterNetworkCallback(cellNetworkCallback);
    mCm.unregisterNetworkCallback(validatedWifiCallback);
    mCm.unregisterNetworkCallback(defaultCallback);
}
Also used : Network(android.net.Network) NetworkRequest(android.net.NetworkRequest) ContentResolver(android.content.ContentResolver) MockContentResolver(android.test.mock.MockContentResolver) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 27 with NetworkRequest

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

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)

Example 28 with NetworkRequest

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

the class ConnectivityServiceTest method testMobileDataAlwaysOn.

@SmallTest
public void testMobileDataAlwaysOn() throws Exception {
    final TestNetworkCallback cellNetworkCallback = new TestNetworkCallback();
    final NetworkRequest cellRequest = new NetworkRequest.Builder().addTransportType(TRANSPORT_CELLULAR).build();
    mCm.registerNetworkCallback(cellRequest, cellNetworkCallback);
    final HandlerThread handlerThread = new HandlerThread("MobileDataAlwaysOnFactory");
    handlerThread.start();
    NetworkCapabilities filter = new NetworkCapabilities().addTransportType(TRANSPORT_CELLULAR).addCapability(NET_CAPABILITY_INTERNET);
    final MockNetworkFactory testFactory = new MockNetworkFactory(handlerThread.getLooper(), mServiceContext, "testFactory", filter);
    testFactory.setScoreFilter(40);
    // Register the factory and expect it to start looking for a network.
    testFactory.expectAddRequests(1);
    testFactory.register();
    testFactory.waitForNetworkRequests(1);
    assertTrue(testFactory.getMyStartRequested());
    // Bring up wifi. The factory stops looking for a network.
    mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI);
    // Because the default request changes score twice.
    testFactory.expectAddRequests(2);
    mWiFiNetworkAgent.connect(true);
    testFactory.waitForNetworkRequests(1);
    assertFalse(testFactory.getMyStartRequested());
    ContentResolver cr = mServiceContext.getContentResolver();
    // Turn on mobile data always on. The factory starts looking again.
    testFactory.expectAddRequests(1);
    setMobileDataAlwaysOn(true);
    testFactory.waitForNetworkRequests(2);
    assertTrue(testFactory.getMyStartRequested());
    // Bring up cell data and check that the factory stops looking.
    assertEquals(1, mCm.getAllNetworks().length);
    mCellNetworkAgent = new MockNetworkAgent(TRANSPORT_CELLULAR);
    // Because the cell request changes score twice.
    testFactory.expectAddRequests(2);
    mCellNetworkAgent.connect(true);
    cellNetworkCallback.expectCallback(CallbackState.AVAILABLE, mCellNetworkAgent);
    testFactory.waitForNetworkRequests(2);
    // Because the cell network outscores us.
    assertFalse(testFactory.getMyStartRequested());
    // Check that cell data stays up.
    mService.waitForIdle();
    verifyActiveNetwork(TRANSPORT_WIFI);
    assertEquals(2, mCm.getAllNetworks().length);
    // Turn off mobile data always on and expect the request to disappear...
    testFactory.expectRemoveRequests(1);
    setMobileDataAlwaysOn(false);
    testFactory.waitForNetworkRequests(1);
    // ...  and cell data to be torn down.
    cellNetworkCallback.expectCallback(CallbackState.LOST, mCellNetworkAgent);
    assertEquals(1, mCm.getAllNetworks().length);
    testFactory.unregister();
    mCm.unregisterNetworkCallback(cellNetworkCallback);
    handlerThread.quit();
}
Also used : HandlerThread(android.os.HandlerThread) NetworkRequest(android.net.NetworkRequest) NetworkCapabilities(android.net.NetworkCapabilities) ContentResolver(android.content.ContentResolver) MockContentResolver(android.test.mock.MockContentResolver) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 29 with NetworkRequest

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

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)

Example 30 with NetworkRequest

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

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)

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