Merge "Include suspended network when getAllNetworkStateSnapshots"

This commit is contained in:
Junyu Lai
2021-09-09 09:24:04 +00:00
committed by Gerrit Code Review
2 changed files with 19 additions and 16 deletions

View File

@@ -2311,9 +2311,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
final ArrayList<NetworkStateSnapshot> result = new ArrayList<>();
for (Network network : getAllNetworks()) {
final NetworkAgentInfo nai = getNetworkAgentInfoForNetwork(network);
// TODO: Consider include SUSPENDED networks, which should be considered as
// temporary shortage of connectivity of a connected network.
if (nai != null && nai.networkInfo.isConnected()) {
if (nai != null && nai.everConnected) {
// TODO (b/73321673) : NetworkStateSnapshot contains a copy of the
// NetworkCapabilities, which may contain UIDs of apps to which the
// network applies. Should the UIDs be cleared so as not to leak or

View File

@@ -6237,16 +6237,16 @@ public class ConnectivityServiceTest {
mCm.unregisterNetworkCallback(networkCallback);
}
private void expectNotifyNetworkStatus(List<Network> networks, String defaultIface,
private void expectNotifyNetworkStatus(List<Network> defaultNetworks, String defaultIface,
Integer vpnUid, String vpnIfname, List<String> underlyingIfaces) throws Exception {
ArgumentCaptor<List<Network>> networksCaptor = ArgumentCaptor.forClass(List.class);
ArgumentCaptor<List<Network>> defaultNetworksCaptor = ArgumentCaptor.forClass(List.class);
ArgumentCaptor<List<UnderlyingNetworkInfo>> vpnInfosCaptor =
ArgumentCaptor.forClass(List.class);
verify(mStatsManager, atLeastOnce()).notifyNetworkStatus(networksCaptor.capture(),
verify(mStatsManager, atLeastOnce()).notifyNetworkStatus(defaultNetworksCaptor.capture(),
any(List.class), eq(defaultIface), vpnInfosCaptor.capture());
assertSameElements(networks, networksCaptor.getValue());
assertSameElements(defaultNetworks, defaultNetworksCaptor.getValue());
List<UnderlyingNetworkInfo> infos = vpnInfosCaptor.getValue();
if (vpnUid != null) {
@@ -6262,8 +6262,8 @@ public class ConnectivityServiceTest {
}
private void expectNotifyNetworkStatus(
List<Network> networks, String defaultIface) throws Exception {
expectNotifyNetworkStatus(networks, defaultIface, null, null, List.of());
List<Network> defaultNetworks, String defaultIface) throws Exception {
expectNotifyNetworkStatus(defaultNetworks, defaultIface, null, null, List.of());
}
@Test
@@ -12971,21 +12971,26 @@ public class ConnectivityServiceTest {
assertLength(2, snapshots);
assertContainsAll(snapshots, cellSnapshot, wifiSnapshot);
// Set cellular as suspended, verify the snapshots will not contain suspended networks.
// TODO: Consider include SUSPENDED networks, which should be considered as
// temporary shortage of connectivity of a connected network.
// Set cellular as suspended, verify the snapshots will contain suspended networks.
mCellNetworkAgent.suspend();
waitForIdle();
final NetworkCapabilities cellSuspendedNc =
mCm.getNetworkCapabilities(mCellNetworkAgent.getNetwork());
assertFalse(cellSuspendedNc.hasCapability(NET_CAPABILITY_NOT_SUSPENDED));
final NetworkStateSnapshot cellSuspendedSnapshot = new NetworkStateSnapshot(
mCellNetworkAgent.getNetwork(), cellSuspendedNc, cellLp,
null, ConnectivityManager.TYPE_MOBILE);
snapshots = mCm.getAllNetworkStateSnapshots();
assertLength(1, snapshots);
assertEquals(wifiSnapshot, snapshots.get(0));
assertLength(2, snapshots);
assertContainsAll(snapshots, cellSuspendedSnapshot, wifiSnapshot);
// Disconnect wifi, verify the snapshots contain nothing.
// Disconnect wifi, verify the snapshots contain only cellular.
mWiFiNetworkAgent.disconnect();
waitForIdle();
snapshots = mCm.getAllNetworkStateSnapshots();
assertEquals(mCellNetworkAgent.getNetwork(), mCm.getActiveNetwork());
assertLength(0, snapshots);
assertLength(1, snapshots);
assertEquals(cellSuspendedSnapshot, snapshots.get(0));
mCellNetworkAgent.resume();
waitForIdle();