Merge "Skip tests in CtsNetTestCases module if feature not supported" into main

This commit is contained in:
Treehugger Robot
2023-08-25 13:37:03 +00:00
committed by Gerrit Code Review
3 changed files with 87 additions and 24 deletions

View File

@@ -18,6 +18,7 @@ package android.net.cts;
import static android.Manifest.permission.UPDATE_DEVICE_STATS;
import static android.content.pm.PackageManager.FEATURE_TELEPHONY;
import static android.content.pm.PackageManager.FEATURE_WIFI;
import static androidx.test.InstrumentationRegistry.getContext;
@@ -118,8 +119,10 @@ public class BatteryStatsManagerTest{
// side effect is the point of using --write here.
executeShellCommand("dumpsys batterystats --write");
// Make sure wifi is disabled.
mCtsNetUtils.ensureWifiDisconnected(null /* wifiNetworkToCheck */);
if (mPm.hasSystemFeature(FEATURE_WIFI)) {
// Make sure wifi is disabled.
mCtsNetUtils.ensureWifiDisconnected(null /* wifiNetworkToCheck */);
}
verifyGetCellBatteryStats();
verifyGetWifiBatteryStats();
@@ -128,6 +131,9 @@ public class BatteryStatsManagerTest{
// Reset battery settings.
executeShellCommand("dumpsys batterystats disable no-auto-reset");
executeShellCommand("cmd battery reset");
if (mPm.hasSystemFeature(FEATURE_WIFI)) {
mCtsNetUtils.ensureWifiConnected();
}
}
}
@@ -153,23 +159,31 @@ public class BatteryStatsManagerTest{
// The mobile battery stats are updated when a network stops being the default network.
// ConnectivityService will call BatteryStatsManager.reportMobileRadioPowerState when
// removing data activity tracking.
mCtsNetUtils.ensureWifiConnected();
try {
mCtsNetUtils.setMobileDataEnabled(false);
// There's rate limit to update mobile battery so if ConnectivityService calls
// BatteryStatsManager.reportMobileRadioPowerState when default network changed,
// the mobile stats might not be updated. But if the mobile update due to other
// reasons (plug/unplug, battery level change, etc) will be unaffected. Thus here
// dumps the battery stats to trigger a full sync of data.
executeShellCommand("dumpsys batterystats");
// There's rate limit to update mobile battery so if ConnectivityService calls
// BatteryStatsManager.reportMobileRadioPowerState when default network changed,
// the mobile stats might not be updated. But if the mobile update due to other
// reasons (plug/unplug, battery level change, etc) will be unaffected. Thus here
// dumps the battery stats to trigger a full sync of data.
executeShellCommand("dumpsys batterystats");
// Check cellular battery stats are updated.
runAsShell(UPDATE_DEVICE_STATS,
() -> assertStatsEventually(mBsm::getCellularBatteryStats,
cellularStatsAfter -> cellularBatteryStatsIncreased(
cellularStatsBefore, cellularStatsAfter)));
// Check cellular battery stats are updated.
runAsShell(UPDATE_DEVICE_STATS,
() -> assertStatsEventually(mBsm::getCellularBatteryStats,
cellularStatsAfter -> cellularBatteryStatsIncreased(
cellularStatsBefore, cellularStatsAfter)));
} finally {
mCtsNetUtils.setMobileDataEnabled(true);
}
}
private void verifyGetWifiBatteryStats() throws Exception {
if (!mPm.hasSystemFeature(FEATURE_WIFI)) {
return;
}
final Network wifiNetwork = mCtsNetUtils.ensureWifiConnected();
final URL url = new URL(TEST_URL);
@@ -199,9 +213,9 @@ public class BatteryStatsManagerTest{
@Test
public void testReportNetworkInterfaceForTransports_throwsSecurityException()
throws Exception {
Network wifiNetwork = mCtsNetUtils.ensureWifiConnected();
final String iface = mCm.getLinkProperties(wifiNetwork).getInterfaceName();
final int[] transportType = mCm.getNetworkCapabilities(wifiNetwork).getTransportTypes();
final Network network = mCm.getActiveNetwork();
final String iface = mCm.getLinkProperties(network).getInterfaceName();
final int[] transportType = mCm.getNetworkCapabilities(network).getTransportTypes();
assertThrows(SecurityException.class,
() -> mBsm.reportNetworkInterfaceForTransports(iface, transportType));
}

View File

@@ -1025,11 +1025,13 @@ public class ConnectivityManagerTest {
final String goodPrivateDnsServer = "dns.google";
mCtsNetUtils.storePrivateDnsSetting();
final TestableNetworkCallback cb = new TestableNetworkCallback();
registerNetworkCallback(makeWifiNetworkRequest(), cb);
final NetworkRequest networkRequest = new NetworkRequest.Builder()
.addCapability(NET_CAPABILITY_INTERNET).build();
registerNetworkCallback(networkRequest, cb);
final Network networkForPrivateDns = mCm.getActiveNetwork();
try {
// Verifying the good private DNS sever
mCtsNetUtils.setPrivateDnsStrictMode(goodPrivateDnsServer);
final Network networkForPrivateDns = mCtsNetUtils.ensureWifiConnected();
cb.eventuallyExpect(CallbackEntry.NETWORK_CAPS_UPDATED, NETWORK_CALLBACK_TIMEOUT_MS,
entry -> hasPrivateDnsValidated(entry, networkForPrivateDns));
@@ -1040,8 +1042,11 @@ public class ConnectivityManagerTest {
.isPrivateDnsBroken()) && networkForPrivateDns.equals(entry.getNetwork()));
} finally {
mCtsNetUtils.restorePrivateDnsSetting();
// Toggle wifi to make sure it is re-validated
reconnectWifi();
// Toggle network to make sure it is re-validated
mCm.reportNetworkConnectivity(networkForPrivateDns, true);
cb.eventuallyExpect(CallbackEntry.NETWORK_CAPS_UPDATED, NETWORK_CALLBACK_TIMEOUT_MS,
entry -> !(((CallbackEntry.CapabilitiesChanged) entry).getCaps()
.isPrivateDnsBroken()) && networkForPrivateDns.equals(entry.getNetwork()));
}
}
@@ -1306,9 +1311,12 @@ public class ConnectivityManagerTest {
@AppModeFull(reason = "Cannot get WifiManager in instant app mode")
@Test
public void testRequestNetworkCallback_onUnavailable() {
final boolean previousWifiEnabledState = mWifiManager.isWifiEnabled();
if (previousWifiEnabledState) {
mCtsNetUtils.ensureWifiDisconnected(null);
boolean previousWifiEnabledState = false;
if (mPackageManager.hasSystemFeature(FEATURE_WIFI)) {
previousWifiEnabledState = mWifiManager.isWifiEnabled();
if (previousWifiEnabledState) {
mCtsNetUtils.ensureWifiDisconnected(null);
}
}
final TestNetworkCallback callback = new TestNetworkCallback();
@@ -1344,6 +1352,8 @@ public class ConnectivityManagerTest {
@AppModeFull(reason = "Cannot get WifiManager in instant app mode")
@Test
public void testToggleWifiConnectivityAction() throws Exception {
assumeTrue(mPackageManager.hasSystemFeature(FEATURE_WIFI));
// toggleWifi calls connectToWifi and disconnectFromWifi, which both wait for
// CONNECTIVITY_ACTION broadcasts.
mCtsNetUtils.toggleWifi();

View File

@@ -16,6 +16,7 @@
package android.net.cts.util;
import static android.Manifest.permission.MODIFY_PHONE_STATE;
import static android.Manifest.permission.NETWORK_SETTINGS;
import static android.net.NetworkCapabilities.NET_CAPABILITY_INTERNET;
import static android.net.NetworkCapabilities.TRANSPORT_CELLULAR;
@@ -54,6 +55,8 @@ import android.os.ConditionVariable;
import android.os.IBinder;
import android.system.Os;
import android.system.OsConstants;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;
@@ -566,6 +569,42 @@ public final class CtsNetUtils {
return testableNetworks.toArray(new Network[0]);
}
/**
* Enables or disables the mobile data and waits for the state to change.
*
* @param enabled - true to enable, false to disable the mobile data.
*/
public void setMobileDataEnabled(boolean enabled) throws InterruptedException {
final TelephonyManager tm = mContext.getSystemService(TelephonyManager.class)
.createForSubscriptionId(SubscriptionManager.getDefaultDataSubscriptionId());
final NetworkRequest request = new NetworkRequest.Builder()
.addTransportType(TRANSPORT_CELLULAR)
.addCapability(NET_CAPABILITY_INTERNET)
.build();
final TestNetworkCallback callback = new TestNetworkCallback();
mCm.requestNetwork(request, callback);
try {
if (!enabled) {
assertNotNull("Cannot disable mobile data unless mobile data is connected",
callback.waitForAvailable());
}
runAsShell(MODIFY_PHONE_STATE, () -> tm.setDataEnabledForReason(
TelephonyManager.DATA_ENABLED_REASON_USER, enabled));
if (enabled) {
assertNotNull("Enabling mobile data did not connect mobile data",
callback.waitForAvailable());
} else {
assertNotNull("Disabling mobile data did not disconnect mobile data",
callback.waitForLost());
}
} finally {
mCm.unregisterNetworkCallback(callback);
}
}
/**
* Receiver that captures the last connectivity change's network type and state. Recognizes
* both {@code CONNECTIVITY_ACTION} and {@code NETWORK_CALLBACK_ACTION} intents.