Transition BatteryInfo and BatteryUtils to BatteryUsageStats API
Bug: 173745486 Test: make RunSettingsRoboTests ROBOTEST_FILTER=com.android.settings.fuelgauge.BatteryHistoryPreferenceTest Test: make RunSettingsRoboTests ROBOTEST_FILTER=com.android.settings.fuelgauge.BatteryInfoLoaderTest Test: make RunSettingsRoboTests ROBOTEST_FILTER=com.android.settings.fuelgauge.BatteryInfoTest Test: make RunSettingsRoboTests ROBOTEST_FILTER=com.android.settings.fuelgauge.BatteryUtilsTest Test: make RunSettingsRoboTests ROBOTEST_FILTER=com.android.settings.fuelgauge.batterytip.detectors Change-Id: I469ff8b88aa3307422c02f51943df4ef1759db56
This commit is contained in:
@@ -34,9 +34,11 @@ import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.BatteryManager;
|
||||
import android.os.BatteryStats;
|
||||
import android.os.BatteryUsageStats;
|
||||
import android.os.SystemClock;
|
||||
import android.util.SparseIntArray;
|
||||
|
||||
import com.android.internal.os.BatteryStatsHistoryIterator;
|
||||
import com.android.settings.testutils.BatteryTestUtils;
|
||||
import com.android.settings.testutils.FakeFeatureFactory;
|
||||
import com.android.settings.widget.UsageView;
|
||||
@@ -46,7 +48,6 @@ import com.android.settingslib.fuelgauge.Estimate;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Answers;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
@@ -83,9 +84,8 @@ public class BatteryInfoTest {
|
||||
private Intent mChargingBatteryBroadcast;
|
||||
private Context mContext;
|
||||
private FakeFeatureFactory mFeatureFactory;
|
||||
|
||||
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
||||
private BatteryStats mBatteryStats;
|
||||
@Mock
|
||||
private BatteryUsageStats mBatteryUsageStats;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
@@ -100,9 +100,10 @@ public class BatteryInfoTest {
|
||||
|
||||
@Test
|
||||
public void testGetBatteryInfo_hasStatusLabel() {
|
||||
doReturn(REMAINING_TIME_NULL).when(mBatteryStats).computeBatteryTimeRemaining(anyLong());
|
||||
doReturn(REMAINING_TIME_NULL).when(mBatteryUsageStats).getBatteryTimeRemainingMs();
|
||||
BatteryInfo info = BatteryInfo.getBatteryInfoOld(mContext,
|
||||
mDisChargingBatteryBroadcast, mBatteryStats, SystemClock.elapsedRealtime() * 1000,
|
||||
mDisChargingBatteryBroadcast, mBatteryUsageStats,
|
||||
SystemClock.elapsedRealtime() * 1000,
|
||||
true /* shortString */);
|
||||
|
||||
assertThat(info.statusLabel).isEqualTo(STATUS_NOT_CHARGING);
|
||||
@@ -110,28 +111,28 @@ public class BatteryInfoTest {
|
||||
|
||||
@Test
|
||||
public void testGetBatteryInfo_doNotShowChargingMethod_hasRemainingTime() {
|
||||
doReturn(REMAINING_TIME).when(mBatteryStats).computeChargeTimeRemaining(anyLong());
|
||||
doReturn(REMAINING_TIME).when(mBatteryUsageStats).getChargeTimeRemainingMs();
|
||||
BatteryInfo info = BatteryInfo.getBatteryInfoOld(mContext, mChargingBatteryBroadcast,
|
||||
mBatteryStats, SystemClock.elapsedRealtime() * 1000, false /* shortString */);
|
||||
mBatteryUsageStats, SystemClock.elapsedRealtime() * 1000, false /* shortString */);
|
||||
|
||||
assertThat(info.chargeLabel.toString()).isEqualTo(STATUS_CHARGING_TIME);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetBatteryInfo_doNotShowChargingMethod_noRemainingTime() {
|
||||
doReturn(REMAINING_TIME_NULL).when(mBatteryStats).computeChargeTimeRemaining(anyLong());
|
||||
doReturn(REMAINING_TIME_NULL).when(mBatteryUsageStats).getChargeTimeRemainingMs();
|
||||
BatteryInfo info = BatteryInfo.getBatteryInfoOld(mContext, mChargingBatteryBroadcast,
|
||||
mBatteryStats, SystemClock.elapsedRealtime() * 1000, false /* shortString */);
|
||||
mBatteryUsageStats, SystemClock.elapsedRealtime() * 1000, false /* shortString */);
|
||||
|
||||
assertThat(info.chargeLabel.toString()).isEqualTo(STATUS_CHARGING_NO_TIME);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetBatteryInfo_pluggedInUsingShortString_usesCorrectData() {
|
||||
doReturn(TEST_CHARGE_TIME_REMAINING).when(mBatteryStats).computeChargeTimeRemaining(
|
||||
anyLong());
|
||||
doReturn(TEST_CHARGE_TIME_REMAINING / 1000)
|
||||
.when(mBatteryUsageStats).getChargeTimeRemainingMs();
|
||||
BatteryInfo info = BatteryInfo.getBatteryInfoOld(mContext, mChargingBatteryBroadcast,
|
||||
mBatteryStats, SystemClock.elapsedRealtime() * 1000, true /* shortString */);
|
||||
mBatteryUsageStats, SystemClock.elapsedRealtime() * 1000, true /* shortString */);
|
||||
|
||||
assertThat(info.discharging).isEqualTo(false);
|
||||
assertThat(info.chargeLabel.toString()).isEqualTo("50% - 1 min until charged");
|
||||
@@ -143,10 +144,10 @@ public class BatteryInfoTest {
|
||||
true /* isBasedOnUsage */,
|
||||
1000 /* averageDischargeTime */);
|
||||
BatteryInfo info = BatteryInfo.getBatteryInfo(mContext, mDisChargingBatteryBroadcast,
|
||||
mBatteryStats, estimate, SystemClock.elapsedRealtime() * 1000,
|
||||
mBatteryUsageStats, estimate, SystemClock.elapsedRealtime() * 1000,
|
||||
false /* shortString */);
|
||||
BatteryInfo info2 = BatteryInfo.getBatteryInfo(mContext, mDisChargingBatteryBroadcast,
|
||||
mBatteryStats, estimate, SystemClock.elapsedRealtime() * 1000,
|
||||
mBatteryUsageStats, estimate, SystemClock.elapsedRealtime() * 1000,
|
||||
true /* shortString */);
|
||||
|
||||
// We only add special mention for the long string
|
||||
@@ -163,10 +164,10 @@ public class BatteryInfoTest {
|
||||
true /* isBasedOnUsage */,
|
||||
1000 /* averageDischargeTime */);
|
||||
BatteryInfo info = BatteryInfo.getBatteryInfo(mContext, mDisChargingBatteryBroadcast,
|
||||
mBatteryStats, estimate, SystemClock.elapsedRealtime() * 1000,
|
||||
mBatteryUsageStats, estimate, SystemClock.elapsedRealtime() * 1000,
|
||||
false /* shortString */);
|
||||
BatteryInfo info2 = BatteryInfo.getBatteryInfo(mContext, mDisChargingBatteryBroadcast,
|
||||
mBatteryStats, estimate, SystemClock.elapsedRealtime() * 1000,
|
||||
mBatteryUsageStats, estimate, SystemClock.elapsedRealtime() * 1000,
|
||||
true /* shortString */);
|
||||
|
||||
// These should be identical in either case
|
||||
@@ -183,7 +184,7 @@ public class BatteryInfoTest {
|
||||
true /* isBasedOnUsage */,
|
||||
1000 /* averageDischargeTime */);
|
||||
BatteryInfo info = BatteryInfo.getBatteryInfo(mContext, mDisChargingBatteryBroadcast,
|
||||
mBatteryStats, estimate, SystemClock.elapsedRealtime() * 1000,
|
||||
mBatteryUsageStats, estimate, SystemClock.elapsedRealtime() * 1000,
|
||||
false /* shortString */);
|
||||
|
||||
assertThat(info.suggestionLabel).doesNotContain(BATTERY_RUN_OUT_PREFIX);
|
||||
@@ -196,7 +197,7 @@ public class BatteryInfoTest {
|
||||
true /* isBasedOnUsage */,
|
||||
1000 /* averageDischargeTime */);
|
||||
BatteryInfo info = BatteryInfo.getBatteryInfo(mContext, mDisChargingBatteryBroadcast,
|
||||
mBatteryStats, estimate, SystemClock.elapsedRealtime() * 1000,
|
||||
mBatteryUsageStats, estimate, SystemClock.elapsedRealtime() * 1000,
|
||||
false /* shortString */);
|
||||
|
||||
// Check that strings are showing less than 15 minutes remaining regardless of exact time.
|
||||
@@ -211,10 +212,10 @@ public class BatteryInfoTest {
|
||||
@Test
|
||||
public void testGetBatteryInfo_basedOnUsageFalse_usesDefaultString() {
|
||||
BatteryInfo info = BatteryInfo.getBatteryInfo(mContext, mDisChargingBatteryBroadcast,
|
||||
mBatteryStats, MOCK_ESTIMATE, SystemClock.elapsedRealtime() * 1000,
|
||||
mBatteryUsageStats, MOCK_ESTIMATE, SystemClock.elapsedRealtime() * 1000,
|
||||
false /* shortString */);
|
||||
BatteryInfo info2 = BatteryInfo.getBatteryInfo(mContext, mDisChargingBatteryBroadcast,
|
||||
mBatteryStats, MOCK_ESTIMATE, SystemClock.elapsedRealtime() * 1000,
|
||||
mBatteryUsageStats, MOCK_ESTIMATE, SystemClock.elapsedRealtime() * 1000,
|
||||
true /* shortString */);
|
||||
|
||||
assertThat(info.remainingLabel.toString()).doesNotContain(ENHANCED_STRING_SUFFIX);
|
||||
@@ -223,12 +224,11 @@ public class BatteryInfoTest {
|
||||
|
||||
@Test
|
||||
public void testGetBatteryInfo_charging_usesChargeTime() {
|
||||
doReturn(TEST_CHARGE_TIME_REMAINING)
|
||||
.when(mBatteryStats)
|
||||
.computeChargeTimeRemaining(anyLong());
|
||||
doReturn(TEST_CHARGE_TIME_REMAINING / 1000)
|
||||
.when(mBatteryUsageStats).getChargeTimeRemainingMs();
|
||||
|
||||
BatteryInfo info = BatteryInfo.getBatteryInfo(mContext, mChargingBatteryBroadcast,
|
||||
mBatteryStats, MOCK_ESTIMATE, SystemClock.elapsedRealtime() * 1000,
|
||||
mBatteryUsageStats, MOCK_ESTIMATE, SystemClock.elapsedRealtime() * 1000,
|
||||
false /* shortString */);
|
||||
assertThat(info.remainingTimeUs).isEqualTo(TEST_CHARGE_TIME_REMAINING);
|
||||
assertThat(info.remainingLabel.toString())
|
||||
@@ -240,7 +240,7 @@ public class BatteryInfoTest {
|
||||
mChargingBatteryBroadcast.putExtra(BatteryManager.EXTRA_LEVEL, 100);
|
||||
|
||||
BatteryInfo info = BatteryInfo.getBatteryInfo(mContext, mChargingBatteryBroadcast,
|
||||
mBatteryStats, MOCK_ESTIMATE, SystemClock.elapsedRealtime() * 1000,
|
||||
mBatteryUsageStats, MOCK_ESTIMATE, SystemClock.elapsedRealtime() * 1000,
|
||||
false /* shortString */);
|
||||
|
||||
assertThat(info.chargeLabel).isEqualTo("100%");
|
||||
@@ -249,13 +249,13 @@ public class BatteryInfoTest {
|
||||
@Test
|
||||
public void testGetBatteryInfo_chargingWithOverheated_updateChargeLabel() {
|
||||
doReturn(TEST_CHARGE_TIME_REMAINING)
|
||||
.when(mBatteryStats)
|
||||
.computeChargeTimeRemaining(anyLong());
|
||||
.when(mBatteryUsageStats)
|
||||
.getChargeTimeRemainingMs();
|
||||
mChargingBatteryBroadcast
|
||||
.putExtra(BatteryManager.EXTRA_HEALTH, BatteryManager.BATTERY_HEALTH_OVERHEAT);
|
||||
|
||||
BatteryInfo info = BatteryInfo.getBatteryInfo(mContext, mChargingBatteryBroadcast,
|
||||
mBatteryStats, MOCK_ESTIMATE, SystemClock.elapsedRealtime() * 1000,
|
||||
mBatteryUsageStats, MOCK_ESTIMATE, SystemClock.elapsedRealtime() * 1000,
|
||||
false /* shortString */);
|
||||
|
||||
assertThat(info.isOverheated).isTrue();
|
||||
@@ -264,28 +264,29 @@ public class BatteryInfoTest {
|
||||
|
||||
// Make our battery stats return a sequence of battery events.
|
||||
private void mockBatteryStatsHistory() {
|
||||
// Mock out new data every time start...Locked is called.
|
||||
// Mock out new data every time iterateBatteryStatsHistory is called.
|
||||
doAnswer(invocation -> {
|
||||
doAnswer(new Answer() {
|
||||
private int count = 0;
|
||||
private long[] times = {1000, 1500, 2000};
|
||||
private byte[] levels = {99, 98, 97};
|
||||
BatteryStatsHistoryIterator iterator = mock(BatteryStatsHistoryIterator.class);
|
||||
doAnswer(new Answer<Boolean>() {
|
||||
private int mCount = 0;
|
||||
private final long[] mTimes = {1000, 1500, 2000};
|
||||
private final byte[] mLevels = {99, 98, 97};
|
||||
|
||||
@Override
|
||||
public Object answer(InvocationOnMock invocation) throws Throwable {
|
||||
if (count == times.length) {
|
||||
public Boolean answer(InvocationOnMock invocation) throws Throwable {
|
||||
if (mCount == mTimes.length) {
|
||||
return false;
|
||||
}
|
||||
BatteryStats.HistoryItem record = invocation.getArgument(0);
|
||||
record.cmd = BatteryStats.HistoryItem.CMD_UPDATE;
|
||||
record.time = times[count];
|
||||
record.batteryLevel = levels[count];
|
||||
count++;
|
||||
record.time = mTimes[mCount];
|
||||
record.batteryLevel = mLevels[mCount];
|
||||
mCount++;
|
||||
return true;
|
||||
}
|
||||
}).when(mBatteryStats).getNextHistoryLocked(any(BatteryStats.HistoryItem.class));
|
||||
return true;
|
||||
}).when(mBatteryStats).startIteratingHistoryLocked();
|
||||
}).when(iterator).next(any(BatteryStats.HistoryItem.class));
|
||||
return iterator;
|
||||
}).when(mBatteryUsageStats).iterateBatteryStatsHistory();
|
||||
}
|
||||
|
||||
private void assertOnlyHistory(BatteryInfo info) {
|
||||
@@ -337,9 +338,9 @@ public class BatteryInfoTest {
|
||||
|
||||
private BatteryInfo getBatteryInfo(boolean charging, boolean enhanced, boolean estimate) {
|
||||
if (charging && estimate) {
|
||||
doReturn(1000L).when(mBatteryStats).computeChargeTimeRemaining(anyLong());
|
||||
doReturn(1000L).when(mBatteryUsageStats).getChargeTimeRemainingMs();
|
||||
} else {
|
||||
doReturn(0L).when(mBatteryStats).computeChargeTimeRemaining(anyLong());
|
||||
doReturn(0L).when(mBatteryUsageStats).getChargeTimeRemainingMs();
|
||||
}
|
||||
Estimate batteryEstimate = new Estimate(
|
||||
estimate ? 1000 : 0,
|
||||
@@ -347,7 +348,7 @@ public class BatteryInfoTest {
|
||||
1000 /* averageDischargeTime */);
|
||||
BatteryInfo info = BatteryInfo.getBatteryInfo(mContext,
|
||||
charging ? mChargingBatteryBroadcast : mDisChargingBatteryBroadcast,
|
||||
mBatteryStats, batteryEstimate, SystemClock.elapsedRealtime() * 1000, false);
|
||||
mBatteryUsageStats, batteryEstimate, SystemClock.elapsedRealtime() * 1000, false);
|
||||
doReturn(enhanced).when(mFeatureFactory.powerUsageFeatureProvider)
|
||||
.isEnhancedBatteryPredictionEnabled(mContext);
|
||||
return info;
|
||||
|
Reference in New Issue
Block a user