Merge "Move battery unplug command to the beginning of the test" am: 7691152162

Original change: https://android-review.googlesource.com/c/platform/packages/modules/Connectivity/+/1769295

Change-Id: Idc76e3cc8268579f52ab98fbbc17aed34205ce99
This commit is contained in:
Aaron Huang
2021-07-21 06:36:01 +00:00
committed by Automerger Merge Worker

View File

@@ -87,21 +87,36 @@ public class BatteryStatsManagerTest{
@SkipPresubmit(reason = "Virtual hardware does not support wifi battery stats")
public void testReportNetworkInterfaceForTransports() throws Exception {
try {
final Network cellNetwork = mCtsNetUtils.connectToCell();
final URL url = new URL(TEST_URL);
// Simulate the device being unplugged from charging.
executeShellCommand("cmd battery unplug");
executeShellCommand("cmd battery set status " + BATTERY_STATUS_DISCHARGING);
// Reset all current stats before starting test.
executeShellCommand("dumpsys batterystats --reset");
// Do not automatically reset the stats when the devices are unplugging after the
// battery was last full or the level is 100, or have gone through a significant
// charge.
executeShellCommand("dumpsys batterystats enable no-auto-reset");
// Upon calling "cmd battery unplug" a task is scheduled on the battery
// stats worker thread. Because network battery stats are only recorded
// when the device is on battery, this test needs to wait until the
// battery status is recorded because causing traffic.
// Writing stats to disk is unnecessary, but --write waits for the worker
// thread to finish processing the enqueued tasks as a side effect. This
// side effect is the point of using --write here.
executeShellCommand("dumpsys batterystats --write");
// Make sure wifi is disabled.
mCtsNetUtils.ensureWifiDisconnected(null /* wifiNetworkToCheck */);
// Simulate the device being unplugged from charging.
executeShellCommand("dumpsys battery unplug");
executeShellCommand("dumpsys battery set status " + BATTERY_STATUS_DISCHARGING);
executeShellCommand("dumpsys batterystats enable pretend-screen-off");
final Network cellNetwork = mCtsNetUtils.connectToCell();
final URL url = new URL(TEST_URL);
// Get cellular battery stats
CellularBatteryStats cellularStatsBefore = runAsShell(UPDATE_DEVICE_STATS,
mBsm::getCellularBatteryStats);
// Generate traffic on cellular network.
Log.d(TAG, "Generate traffic on cellular network.");
generateNetworkTraffic(cellNetwork, url);
// The mobile battery stats are updated when a network stops being the default network.
@@ -119,6 +134,7 @@ public class BatteryStatsManagerTest{
mBsm::getWifiBatteryStats);
// Generate traffic on wifi network.
Log.d(TAG, "Generate traffic on wifi network.");
generateNetworkTraffic(wifiNetwork, url);
// Wifi battery stats are updated when wifi on.
mCtsNetUtils.toggleWifi();
@@ -130,8 +146,8 @@ public class BatteryStatsManagerTest{
wifiStatsAfter)));
} finally {
// Reset battery settings.
executeShellCommand("dumpsys battery reset");
executeShellCommand("dumpsys batterystats disable pretend-screen-off");
executeShellCommand("dumpsys batterystats disable no-auto-reset");
executeShellCommand("cmd battery reset");
}
}