Merge "Unit tests for new explicitlySelected behaviour." am: d1379a4a80

am: 10c1a704c0

Change-Id: Id77a5d6a58e7ab4fdaa0b7c15b824270863b83fd
This commit is contained in:
Lorenzo Colitti
2019-06-29 02:01:55 -07:00
committed by android-build-merger
3 changed files with 83 additions and 29 deletions

View File

@@ -437,15 +437,23 @@ public abstract class NetworkAgent extends Handler {
} }
/** /**
* Called by the bearer to indicate this network was manually selected by the user. * Called by the bearer to indicate whether the network was manually selected by the user.
* This should be called before the NetworkInfo is marked CONNECTED so that this * This should be called before the NetworkInfo is marked CONNECTED so that this
* Network can be given special treatment at that time. If {@code acceptUnvalidated} is * Network can be given special treatment at that time.
* {@code true}, then the system will switch to this network. If it is {@code false} and the *
* network cannot be validated, the system will ask the user whether to switch to this network. * If {@code explicitlySelected} is {@code true}, and {@code acceptUnvalidated} is {@code true},
* If the user confirms and selects "don't ask again", then the system will call * then the system will switch to this network. If {@code explicitlySelected} is {@code true}
* {@link #saveAcceptUnvalidated} to persist the user's choice. Thus, if the transport ever * and {@code acceptUnvalidated} is {@code false}, and the network cannot be validated, the
* calls this method with {@code acceptUnvalidated} set to {@code false}, it must also implement * system will ask the user whether to switch to this network. If the user confirms and selects
* {@link #saveAcceptUnvalidated} to respect the user's choice. * "don't ask again", then the system will call {@link #saveAcceptUnvalidated} to persist the
* user's choice. Thus, if the transport ever calls this method with {@code explicitlySelected}
* set to {@code true} and {@code acceptUnvalidated} set to {@code false}, it must also
* implement {@link #saveAcceptUnvalidated} to respect the user's choice.
*
* If {@code explicitlySelected} is {@code false} and {@code acceptUnvalidated} is
* {@code true}, the system will interpret this as the user having accepted partial connectivity
* on this network. Thus, the system will switch to the network and consider it validated even
* if it only provides partial connectivity, but the network is not otherwise treated specially.
*/ */
public void explicitlySelected(boolean explicitlySelected, boolean acceptUnvalidated) { public void explicitlySelected(boolean explicitlySelected, boolean acceptUnvalidated) {
queueOrSendMessage(EVENT_SET_EXPLICITLY_SELECTED, queueOrSendMessage(EVENT_SET_EXPLICITLY_SELECTED,

View File

@@ -2579,8 +2579,8 @@ public class ConnectivityService extends IConnectivityManager.Stub
if (nai.everConnected) { if (nai.everConnected) {
loge("ERROR: cannot call explicitlySelected on already-connected network"); loge("ERROR: cannot call explicitlySelected on already-connected network");
} }
nai.networkMisc.explicitlySelected = (msg.arg1 == 1); nai.networkMisc.explicitlySelected = toBool(msg.arg1);
nai.networkMisc.acceptUnvalidated = (msg.arg1 == 1) && (msg.arg2 == 1); nai.networkMisc.acceptUnvalidated = toBool(msg.arg1) && toBool(msg.arg2);
// Mark the network as temporarily accepting partial connectivity so that it // Mark the network as temporarily accepting partial connectivity so that it
// will be validated (and possibly become default) even if it only provides // will be validated (and possibly become default) even if it only provides
// partial internet access. Note that if user connects to partial connectivity // partial internet access. Note that if user connects to partial connectivity
@@ -2588,7 +2588,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
// out of wifi coverage) and if the same wifi is available again, the device // out of wifi coverage) and if the same wifi is available again, the device
// will auto connect to this wifi even though the wifi has "no internet". // will auto connect to this wifi even though the wifi has "no internet".
// TODO: Evaluate using a separate setting in IpMemoryStore. // TODO: Evaluate using a separate setting in IpMemoryStore.
nai.networkMisc.acceptPartialConnectivity = (msg.arg2 == 1); nai.networkMisc.acceptPartialConnectivity = toBool(msg.arg2);
break; break;
} }
case NetworkAgent.EVENT_SOCKET_KEEPALIVE: { case NetworkAgent.EVENT_SOCKET_KEEPALIVE: {

View File

@@ -652,8 +652,8 @@ public class ConnectivityServiceTest {
return mScore; return mScore;
} }
public void explicitlySelected(boolean acceptUnvalidated) { public void explicitlySelected(boolean explicitlySelected, boolean acceptUnvalidated) {
mNetworkAgent.explicitlySelected(acceptUnvalidated); mNetworkAgent.explicitlySelected(explicitlySelected, acceptUnvalidated);
} }
public void addCapability(int capability) { public void addCapability(int capability) {
@@ -756,6 +756,11 @@ public class ConnectivityServiceTest {
connect(false); connect(false);
} }
public void connectWithPartialValidConnectivity() {
setNetworkPartialValid();
connect(false);
}
public void suspend() { public void suspend() {
mNetworkInfo.setDetailedState(DetailedState.SUSPENDED, null, null); mNetworkInfo.setDetailedState(DetailedState.SUSPENDED, null, null);
mNetworkAgent.sendNetworkInfo(mNetworkInfo); mNetworkAgent.sendNetworkInfo(mNetworkInfo);
@@ -2389,7 +2394,7 @@ public class ConnectivityServiceTest {
// Bring up unvalidated wifi with explicitlySelected=true. // Bring up unvalidated wifi with explicitlySelected=true.
mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI); mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI);
mWiFiNetworkAgent.explicitlySelected(false); mWiFiNetworkAgent.explicitlySelected(true, false);
mWiFiNetworkAgent.connect(false); mWiFiNetworkAgent.connect(false);
callback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent); callback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
@@ -2412,7 +2417,7 @@ public class ConnectivityServiceTest {
mWiFiNetworkAgent.disconnect(); mWiFiNetworkAgent.disconnect();
callback.expectCallback(CallbackState.LOST, mWiFiNetworkAgent); callback.expectCallback(CallbackState.LOST, mWiFiNetworkAgent);
mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI); mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI);
mWiFiNetworkAgent.explicitlySelected(false); mWiFiNetworkAgent.explicitlySelected(true, false);
mWiFiNetworkAgent.connect(false); mWiFiNetworkAgent.connect(false);
callback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent); callback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
@@ -2423,7 +2428,7 @@ public class ConnectivityServiceTest {
// Reconnect, again with explicitlySelected=true, but this time validate. // Reconnect, again with explicitlySelected=true, but this time validate.
mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI); mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI);
mWiFiNetworkAgent.explicitlySelected(false); mWiFiNetworkAgent.explicitlySelected(true, false);
mWiFiNetworkAgent.connect(true); mWiFiNetworkAgent.connect(true);
callback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent); callback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
callback.expectCallback(CallbackState.LOSING, mCellNetworkAgent); callback.expectCallback(CallbackState.LOSING, mCellNetworkAgent);
@@ -2438,14 +2443,36 @@ public class ConnectivityServiceTest {
assertEquals(mEthernetNetworkAgent.getNetwork(), mCm.getActiveNetwork()); assertEquals(mEthernetNetworkAgent.getNetwork(), mCm.getActiveNetwork());
callback.assertNoCallback(); callback.assertNoCallback();
// Disconnect wifi, and then reconnect as if the user had selected "yes, don't ask again"
// (i.e., with explicitlySelected=true and acceptUnvalidated=true). Expect to switch to
// wifi immediately.
mWiFiNetworkAgent.disconnect();
callback.expectCallback(CallbackState.LOST, mWiFiNetworkAgent);
mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI);
mWiFiNetworkAgent.explicitlySelected(true, true);
mWiFiNetworkAgent.connect(false);
callback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
callback.expectCallback(CallbackState.LOSING, mEthernetNetworkAgent);
assertEquals(mWiFiNetworkAgent.getNetwork(), mCm.getActiveNetwork());
mEthernetNetworkAgent.disconnect();
callback.expectCallback(CallbackState.LOST, mEthernetNetworkAgent);
// Disconnect and reconnect with explicitlySelected=false and acceptUnvalidated=true.
// Check that the network is not scored specially and that the device prefers cell data.
mWiFiNetworkAgent.disconnect();
callback.expectCallback(CallbackState.LOST, mWiFiNetworkAgent);
mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI);
mWiFiNetworkAgent.explicitlySelected(false, true);
mWiFiNetworkAgent.connect(false);
callback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
assertEquals(mCellNetworkAgent.getNetwork(), mCm.getActiveNetwork());
// Clean up. // Clean up.
mWiFiNetworkAgent.disconnect(); mWiFiNetworkAgent.disconnect();
mCellNetworkAgent.disconnect(); mCellNetworkAgent.disconnect();
mEthernetNetworkAgent.disconnect();
callback.expectCallback(CallbackState.LOST, mWiFiNetworkAgent); callback.expectCallback(CallbackState.LOST, mWiFiNetworkAgent);
callback.expectCallback(CallbackState.LOST, mCellNetworkAgent); callback.expectCallback(CallbackState.LOST, mCellNetworkAgent);
callback.expectCallback(CallbackState.LOST, mEthernetNetworkAgent);
} }
private int[] makeIntArray(final int size, final int value) { private int[] makeIntArray(final int size, final int value) {
@@ -2690,6 +2717,7 @@ public class ConnectivityServiceTest {
// NetworkMonitor#setAcceptPartialConnectivity() should be called too. // NetworkMonitor#setAcceptPartialConnectivity() should be called too.
waitForIdle(); waitForIdle();
verify(mWiFiNetworkAgent.mNetworkMonitor, times(1)).setAcceptPartialConnectivity(); verify(mWiFiNetworkAgent.mNetworkMonitor, times(1)).setAcceptPartialConnectivity();
// Need a trigger point to let NetworkMonitor tell ConnectivityService that network is // Need a trigger point to let NetworkMonitor tell ConnectivityService that network is
// validated. // validated.
mCm.reportNetworkConnectivity(mWiFiNetworkAgent.getNetwork(), true); mCm.reportNetworkConnectivity(mWiFiNetworkAgent.getNetwork(), true);
@@ -2720,8 +2748,10 @@ public class ConnectivityServiceTest {
// NET_CAPABILITY_PARTIAL_CONNECTIVITY. // NET_CAPABILITY_PARTIAL_CONNECTIVITY.
mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI); mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI);
// acceptUnvalidated is also used as setting for accepting partial networks. // acceptUnvalidated is also used as setting for accepting partial networks.
mWiFiNetworkAgent.explicitlySelected(true /* acceptUnvalidated */); mWiFiNetworkAgent.explicitlySelected(true /* explicitlySelected */,
true /* acceptUnvalidated */);
mWiFiNetworkAgent.connect(true); mWiFiNetworkAgent.connect(true);
// If user accepted partial connectivity network before, // If user accepted partial connectivity network before,
// NetworkMonitor#setAcceptPartialConnectivity() will be called in // NetworkMonitor#setAcceptPartialConnectivity() will be called in
// ConnectivityService#updateNetworkInfo(). // ConnectivityService#updateNetworkInfo().
@@ -2731,20 +2761,18 @@ public class ConnectivityServiceTest {
callback.expectCallback(CallbackState.LOSING, mCellNetworkAgent); callback.expectCallback(CallbackState.LOSING, mCellNetworkAgent);
nc = callback.expectCapabilitiesWith(NET_CAPABILITY_VALIDATED, mWiFiNetworkAgent); nc = callback.expectCapabilitiesWith(NET_CAPABILITY_VALIDATED, mWiFiNetworkAgent);
assertFalse(nc.hasCapability(NET_CAPABILITY_PARTIAL_CONNECTIVITY)); assertFalse(nc.hasCapability(NET_CAPABILITY_PARTIAL_CONNECTIVITY));
// Wifi should be the default network. // Wifi should be the default network.
assertEquals(mWiFiNetworkAgent.getNetwork(), mCm.getActiveNetwork()); assertEquals(mWiFiNetworkAgent.getNetwork(), mCm.getActiveNetwork());
mWiFiNetworkAgent.disconnect(); mWiFiNetworkAgent.disconnect();
callback.expectCallback(CallbackState.LOST, mWiFiNetworkAgent); callback.expectCallback(CallbackState.LOST, mWiFiNetworkAgent);
// If user accepted partial connectivity before, and now the device reconnects to the // The user accepted partial connectivity and selected "don't ask again". Now the user
// partial connectivity network. The network should be valid and contain // reconnects to the partial connectivity network. Switch to wifi as soon as partial
// NET_CAPABILITY_PARTIAL_CONNECTIVITY. // connectivity is detected.
mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI); mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI);
mWiFiNetworkAgent.explicitlySelected(true /* acceptUnvalidated */); mWiFiNetworkAgent.explicitlySelected(true /* explicitlySelected */,
// Current design cannot send multi-testResult from NetworkMonitor to ConnectivityService. true /* acceptUnvalidated */);
// So, if user accepts partial connectivity, NetworkMonitor will send PARTIAL_CONNECTIVITY
// to ConnectivityService first then send VALID. Once NetworkMonitor support
// multi-testResult, this test case also need to be changed to meet the new design.
mWiFiNetworkAgent.connectWithPartialConnectivity(); mWiFiNetworkAgent.connectWithPartialConnectivity();
// If user accepted partial connectivity network before, // If user accepted partial connectivity network before,
// NetworkMonitor#setAcceptPartialConnectivity() will be called in // NetworkMonitor#setAcceptPartialConnectivity() will be called in
@@ -2753,17 +2781,35 @@ public class ConnectivityServiceTest {
verify(mWiFiNetworkAgent.mNetworkMonitor, times(1)).setAcceptPartialConnectivity(); verify(mWiFiNetworkAgent.mNetworkMonitor, times(1)).setAcceptPartialConnectivity();
callback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent); callback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
callback.expectCallback(CallbackState.LOSING, mCellNetworkAgent); callback.expectCallback(CallbackState.LOSING, mCellNetworkAgent);
// TODO: If the user accepted partial connectivity, we shouldn't switch to wifi until
// NetworkMonitor detects partial connectivity
assertEquals(mWiFiNetworkAgent.getNetwork(), mCm.getActiveNetwork()); assertEquals(mWiFiNetworkAgent.getNetwork(), mCm.getActiveNetwork());
callback.expectCapabilitiesWith(NET_CAPABILITY_PARTIAL_CONNECTIVITY, mWiFiNetworkAgent); callback.expectCapabilitiesWith(NET_CAPABILITY_PARTIAL_CONNECTIVITY, mWiFiNetworkAgent);
mWiFiNetworkAgent.setNetworkValid(); mWiFiNetworkAgent.setNetworkValid();
// Need a trigger point to let NetworkMonitor tell ConnectivityService that network is // Need a trigger point to let NetworkMonitor tell ConnectivityService that network is
// validated. // validated.
mCm.reportNetworkConnectivity(mWiFiNetworkAgent.getNetwork(), true); mCm.reportNetworkConnectivity(mWiFiNetworkAgent.getNetwork(), true);
callback.expectCapabilitiesWith(NET_CAPABILITY_VALIDATED, mWiFiNetworkAgent); callback.expectCapabilitiesWith(NET_CAPABILITY_VALIDATED, mWiFiNetworkAgent);
mWiFiNetworkAgent.disconnect(); mWiFiNetworkAgent.disconnect();
callback.expectCallback(CallbackState.LOST, mWiFiNetworkAgent); callback.expectCallback(CallbackState.LOST, mWiFiNetworkAgent);
// If the user accepted partial connectivity, and the device auto-reconnects to the partial
// connectivity network, it should contain both PARTIAL_CONNECTIVITY and VALIDATED.
mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI);
mWiFiNetworkAgent.explicitlySelected(false /* explicitlySelected */,
true /* acceptUnvalidated */);
// NetworkMonitor will immediately (once the HTTPS probe fails...) report the network as
// valid, because ConnectivityService calls setAcceptPartialConnectivity before it calls
// notifyNetworkConnected.
mWiFiNetworkAgent.connectWithPartialValidConnectivity();
waitForIdle();
verify(mWiFiNetworkAgent.mNetworkMonitor, times(1)).setAcceptPartialConnectivity();
callback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
callback.expectCallback(CallbackState.LOSING, mCellNetworkAgent);
callback.expectCapabilitiesWith(
NET_CAPABILITY_PARTIAL_CONNECTIVITY | NET_CAPABILITY_VALIDATED, mWiFiNetworkAgent);
mWiFiNetworkAgent.disconnect();
callback.expectCallback(CallbackState.LOST, mWiFiNetworkAgent);
} }
@Test @Test