Search in sources :

Example 91 with Network

use of android.net.Network in project android_frameworks_base by crdroidandroid.

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 92 with Network

use of android.net.Network in project android_frameworks_base by crdroidandroid.

the class NetdEventListenerServiceTest method testConcurrentDnsBatchesAndNetworkLoss.

@SmallTest
public void testConcurrentDnsBatchesAndNetworkLoss() throws Exception {
    logDnsAsync(105, LATENCIES);
    Thread.sleep(10L);
    // call onLost() asynchronously to logDnsAsync's onDnsEvent() calls.
    mCallbackCaptor.getValue().onLost(new Network(105));
    // do not verify unpredictable batch
    verify(mLog, timeout(500).times(1)).log(any());
}
Also used : Network(android.net.Network) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Aggregations

Network (android.net.Network)92 SmallTest (android.test.suitebuilder.annotation.SmallTest)28 NetworkAgentInfo (com.android.server.connectivity.NetworkAgentInfo)25 NetworkCapabilities (android.net.NetworkCapabilities)22 NetworkRequest (android.net.NetworkRequest)18 NetworkInfo (android.net.NetworkInfo)17 LinkProperties (android.net.LinkProperties)16 ConnectivityManager (android.net.ConnectivityManager)11 NetworkCallback (android.net.ConnectivityManager.NetworkCallback)11 Test (org.junit.Test)7 InetAddress (java.net.InetAddress)6 Nullable (android.annotation.Nullable)5 NetworkMisc (android.net.NetworkMisc)5 NetworkState (android.net.NetworkState)5 WifiConfiguration (android.net.wifi.WifiConfiguration)5 WifiInfo (android.net.wifi.WifiInfo)5 ConditionVariable (android.os.ConditionVariable)5 WebSettings (android.webkit.WebSettings)5 HomeSP (com.android.hotspot2.pps.HomeSP)5 LegacyVpnInfo (com.android.internal.net.LegacyVpnInfo)5