Merge "Bypass cellular battery stats test if telephony is not supported"

This commit is contained in:
Aaron Huang
2021-08-19 05:37:59 +00:00
committed by Gerrit Code Review

View File

@@ -17,6 +17,7 @@
package android.net.cts;
import static android.Manifest.permission.UPDATE_DEVICE_STATS;
import static android.content.pm.PackageManager.FEATURE_TELEPHONY;
import static androidx.test.InstrumentationRegistry.getContext;
@@ -28,6 +29,7 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import android.content.Context;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.cts.util.CtsNetUtils;
@@ -74,6 +76,7 @@ public class BatteryStatsManagerTest{
private BatteryStatsManager mBsm;
private ConnectivityManager mCm;
private WifiManager mWm;
private PackageManager mPm;
private CtsNetUtils mCtsNetUtils;
@Before
@@ -82,9 +85,13 @@ public class BatteryStatsManagerTest{
mBsm = mContext.getSystemService(BatteryStatsManager.class);
mCm = mContext.getSystemService(ConnectivityManager.class);
mWm = mContext.getSystemService(WifiManager.class);
mPm = mContext.getPackageManager();
mCtsNetUtils = new CtsNetUtils(mContext);
}
// reportNetworkInterfaceForTransports classifies one network interface as wifi or mobile, so
// check that the interface is classified properly by checking the data usage is reported
// properly.
@Test
@AppModeFull(reason = "Cannot get CHANGE_NETWORK_STATE to request wifi/cell in instant mode")
@SkipPresubmit(reason = "Virtual hardware does not support wifi battery stats")
@@ -111,6 +118,24 @@ public class BatteryStatsManagerTest{
// Make sure wifi is disabled.
mCtsNetUtils.ensureWifiDisconnected(null /* wifiNetworkToCheck */);
verifyGetCellBatteryStats();
verifyGetWifiBatteryStats();
} finally {
// Reset battery settings.
executeShellCommand("dumpsys batterystats disable no-auto-reset");
executeShellCommand("cmd battery reset");
}
}
private void verifyGetCellBatteryStats() throws Exception {
final boolean isTelephonySupported = mPm.hasSystemFeature(FEATURE_TELEPHONY);
if (!isTelephonySupported) {
Log.d(TAG, "Skip cell battery stats test because device does not support telephony.");
return;
}
final Network cellNetwork = mCtsNetUtils.connectToCell();
final URL url = new URL(TEST_URL);
@@ -125,13 +150,18 @@ 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.
final Network wifiNetwork = mCtsNetUtils.ensureWifiConnected();
mCtsNetUtils.ensureWifiConnected();
// Check cellular battery stats are updated.
runAsShell(UPDATE_DEVICE_STATS,
() -> assertStatsEventually(mBsm::getCellularBatteryStats,
cellularStatsAfter -> cellularBatteryStatsIncreased(
cellularStatsBefore, cellularStatsAfter)));
}
private void verifyGetWifiBatteryStats() throws Exception {
final Network wifiNetwork = mCtsNetUtils.ensureWifiConnected();
final URL url = new URL(TEST_URL);
if (!mWm.isEnhancedPowerReportingSupported()) {
Log.d(TAG, "Skip wifi stats test because wifi does not support link layer stats.");
@@ -152,11 +182,6 @@ public class BatteryStatsManagerTest{
() -> assertStatsEventually(mBsm::getWifiBatteryStats,
wifiStatsAfter -> wifiBatteryStatsIncreased(wifiStatsBefore,
wifiStatsAfter)));
} finally {
// Reset battery settings.
executeShellCommand("dumpsys batterystats disable no-auto-reset");
executeShellCommand("cmd battery reset");
}
}
@DevSdkIgnoreRule.IgnoreUpTo(Build.VERSION_CODES.R)