Move battery unplug command to the beginning of the test
The test failed because the test generated traffic
before the unplug command really done.
The unplug command will eventually have onBattery changed
to true. If the traffic is generated before onBattery changed
to true, then the cellular stats won't be updated. And then,
when BatteryStatsService schedules a sync task to update the
stats which will get no delta because the stats on cellular
has already added to mLastModemNetworkStats. Thus, the test
will finally get failed.
Currently, the battery unplug command is executed after connecting
to cellular network and making sure wifi is disconnected which may
trigger battery stats service schedules sync tasks. If there are
too many tasks need to run, then change onBattery to true would
be later. Thus, move the unplug command to the beginning of the
test and add "dumpsys batterystats --write" which will wait for the
worker to finish the task before continuing. Thus, it can make sure
the batterystats are synced.
Also, the stats will be reset when the devices are unplugging after
the battery was last full or the level is 100, or have gone through
a significant charge. So, enable no-auto-reset to avoid the stats
getting reset during the test.
Bug: 192808200
Test: atest CtsNetTestCases:BatteryStatsManagerTest
Original-Change: https://android-review.googlesource.com/1769295
Merged-In: I08b38d9955c8cbb2d680e268c29368a4c0a7d144
Change-Id: I08b38d9955c8cbb2d680e268c29368a4c0a7d144
diff --git a/tests/cts/net/src/android/net/cts/BatteryStatsManagerTest.java b/tests/cts/net/src/android/net/cts/BatteryStatsManagerTest.java
index 86642ea..dfb03c7 100644
--- a/tests/cts/net/src/android/net/cts/BatteryStatsManagerTest.java
+++ b/tests/cts/net/src/android/net/cts/BatteryStatsManagerTest.java
@@ -85,21 +85,36 @@
@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.
@@ -117,6 +132,7 @@
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();
@@ -128,8 +144,8 @@
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");
}
}