Merge "Fix ConnectivityDiagnosticsManagerTest for MTS"

This commit is contained in:
Treehugger Robot
2021-08-16 10:51:19 +00:00
committed by Gerrit Code Review

View File

@@ -73,6 +73,7 @@ import android.platform.test.annotations.AppModeFull;
import android.telephony.CarrierConfigManager; import android.telephony.CarrierConfigManager;
import android.telephony.SubscriptionManager; import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager; import android.telephony.TelephonyManager;
import android.util.ArraySet;
import android.util.Pair; import android.util.Pair;
import androidx.test.InstrumentationRegistry; import androidx.test.InstrumentationRegistry;
@@ -92,7 +93,9 @@ import org.junit.runner.RunWith;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Set;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@@ -293,7 +296,7 @@ public class ConnectivityDiagnosticsManagerTest {
final String interfaceName = final String interfaceName =
mConnectivityManager.getLinkProperties(network).getInterfaceName(); mConnectivityManager.getLinkProperties(network).getInterfaceName();
connDiagsCallback.expectOnConnectivityReportAvailable( connDiagsCallback.maybeVerifyConnectivityReportAvailable(
network, interfaceName, TRANSPORT_CELLULAR, NETWORK_VALIDATION_RESULT_VALID); network, interfaceName, TRANSPORT_CELLULAR, NETWORK_VALIDATION_RESULT_VALID);
connDiagsCallback.assertNoCallback(); connDiagsCallback.assertNoCallback();
} }
@@ -426,10 +429,15 @@ public class ConnectivityDiagnosticsManagerTest {
cb.expectOnNetworkConnectivityReported(mTestNetwork, hasConnectivity); cb.expectOnNetworkConnectivityReported(mTestNetwork, hasConnectivity);
// All calls to #onNetworkConnectivityReported are expected to be accompanied by a call to // All calls to #onNetworkConnectivityReported are expected to be accompanied by a call to
// #onConnectivityReportAvailable for S+ (for R, ConnectivityReports were only sent when the // #onConnectivityReportAvailable for T+ (for R, ConnectivityReports were only sent when the
// Network was re-validated - when reported connectivity != known connectivity). // Network was re-validated - when reported connectivity != known connectivity). On S,
if (SdkLevel.isAtLeastS() || !hasConnectivity) { // recent module versions will have the callback, but not the earliest ones.
if (!hasConnectivity) {
cb.expectOnConnectivityReportAvailable(mTestNetwork, interfaceName); cb.expectOnConnectivityReportAvailable(mTestNetwork, interfaceName);
} else if (SdkLevel.isAtLeastS()) {
cb.maybeVerifyConnectivityReportAvailable(mTestNetwork, interfaceName, TRANSPORT_TEST,
getPossibleDiagnosticsValidationResults(),
SdkLevel.isAtLeastT() /* requireCallbackFired */);
} }
cb.assertNoCallback(); cb.assertNoCallback();
@@ -485,18 +493,25 @@ public class ConnectivityDiagnosticsManagerTest {
// Test Networks both do not require validation and are not tested for validation. This // Test Networks both do not require validation and are not tested for validation. This
// results in the validation result being reported as SKIPPED for S+ (for R, the // results in the validation result being reported as SKIPPED for S+ (for R, the
// platform marked these Networks as VALID). // platform marked these Networks as VALID).
final int expectedNetworkValidationResult =
SdkLevel.isAtLeastS() maybeVerifyConnectivityReportAvailable(network, interfaceName, TRANSPORT_TEST,
? NETWORK_VALIDATION_RESULT_SKIPPED getPossibleDiagnosticsValidationResults(), true);
: NETWORK_VALIDATION_RESULT_VALID;
expectOnConnectivityReportAvailable(
network, interfaceName, TRANSPORT_TEST, expectedNetworkValidationResult);
} }
public void expectOnConnectivityReportAvailable(@NonNull Network network, public void maybeVerifyConnectivityReportAvailable(@NonNull Network network,
@NonNull String interfaceName, int transportType, int expectedValidationResult) { @NonNull String interfaceName, int transportType, int expectedValidationResult) {
maybeVerifyConnectivityReportAvailable(network, interfaceName, transportType,
new ArraySet<>(Collections.singletonList(expectedValidationResult)), true);
}
public void maybeVerifyConnectivityReportAvailable(@NonNull Network network,
@NonNull String interfaceName, int transportType,
Set<Integer> possibleValidationResults, boolean requireCallbackFired) {
final ConnectivityReport result = final ConnectivityReport result =
(ConnectivityReport) mHistory.poll(CALLBACK_TIMEOUT_MILLIS, x -> true); (ConnectivityReport) mHistory.poll(CALLBACK_TIMEOUT_MILLIS, x -> true);
if (!requireCallbackFired && result == null) {
return;
}
assertEquals(network, result.getNetwork()); assertEquals(network, result.getNetwork());
final NetworkCapabilities nc = result.getNetworkCapabilities(); final NetworkCapabilities nc = result.getNetworkCapabilities();
@@ -508,8 +523,8 @@ public class ConnectivityDiagnosticsManagerTest {
final PersistableBundle extras = result.getAdditionalInfo(); final PersistableBundle extras = result.getAdditionalInfo();
assertTrue(extras.containsKey(KEY_NETWORK_VALIDATION_RESULT)); assertTrue(extras.containsKey(KEY_NETWORK_VALIDATION_RESULT));
final int actualValidationResult = extras.getInt(KEY_NETWORK_VALIDATION_RESULT); final int actualValidationResult = extras.getInt(KEY_NETWORK_VALIDATION_RESULT);
assertEquals("Network validation result is incorrect", assertTrue("Network validation result is incorrect: " + actualValidationResult,
expectedValidationResult, actualValidationResult); possibleValidationResults.contains(actualValidationResult));
assertTrue(extras.containsKey(KEY_NETWORK_PROBES_SUCCEEDED_BITMASK)); assertTrue(extras.containsKey(KEY_NETWORK_PROBES_SUCCEEDED_BITMASK));
final int probesSucceeded = extras.getInt(KEY_NETWORK_VALIDATION_RESULT); final int probesSucceeded = extras.getInt(KEY_NETWORK_VALIDATION_RESULT);
@@ -556,6 +571,19 @@ public class ConnectivityDiagnosticsManagerTest {
} }
} }
private static Set<Integer> getPossibleDiagnosticsValidationResults() {
final Set<Integer> possibleValidationResults = new ArraySet<>();
possibleValidationResults.add(NETWORK_VALIDATION_RESULT_SKIPPED);
// In S, some early module versions will return NETWORK_VALIDATION_RESULT_VALID.
// Starting from T, all module versions should only return SKIPPED. For platform < T,
// accept both values.
if (!SdkLevel.isAtLeastT()) {
possibleValidationResults.add(NETWORK_VALIDATION_RESULT_VALID);
}
return possibleValidationResults;
}
private class CarrierConfigReceiver extends BroadcastReceiver { private class CarrierConfigReceiver extends BroadcastReceiver {
// CountDownLatch used to wait for this BroadcastReceiver to be notified of a CarrierConfig // CountDownLatch used to wait for this BroadcastReceiver to be notified of a CarrierConfig
// change. This latch will be counted down if a broadcast indicates this package has carrier // change. This latch will be counted down if a broadcast indicates this package has carrier