Add NPE check for screen and cellular update.
When battery is full of charge, usageList in BatteryStatsHelper will be empty. As a result, we cannot find BatterySipper with type screen and cell, which creates the null pointer error. In this cl, I add NPE check for sipper. When sipper is null, all data will be set to default value. I also add check for totalPower so we won't have divided by zero error. Bug: 35757789 Test: RunSettingsRoboTests Change-Id: I80f3c0c542e0a50868e7c314a8d9b3c17999d8c6
This commit is contained in:
@@ -40,6 +40,7 @@ import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Answers;
|
||||
import org.mockito.Matchers;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
@@ -180,6 +181,9 @@ public class PowerUsageSummaryTest {
|
||||
|
||||
mPowerUsageSummary.mStatsHelper = mBatteryHelper;
|
||||
when(mBatteryHelper.getUsageList()).thenReturn(mUsageList);
|
||||
mPowerUsageSummary.mScreenUsagePref = mScreenUsagePref;
|
||||
mPowerUsageSummary.mScreenConsumptionPref = mScreenConsumptionPref;
|
||||
mPowerUsageSummary.mCellularNetworkPref = mCellularNetworkPref;
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -384,7 +388,6 @@ public class PowerUsageSummaryTest {
|
||||
|
||||
@Test
|
||||
public void testUpdateCellularPreference_ShowCorrectSummary() {
|
||||
mPowerUsageSummary.mCellularNetworkPref = mCellularNetworkPref;
|
||||
final double percent = POWER_MAH / TOTAL_POWER * DISCHARGE_AMOUNT;
|
||||
final String expectedSummary = mRealContext.getString(R.string.battery_overall_usage,
|
||||
Utils.formatPercentage((int) percent));
|
||||
@@ -397,8 +400,6 @@ public class PowerUsageSummaryTest {
|
||||
|
||||
@Test
|
||||
public void testUpdateScreenPreference_ShowCorrectSummary() {
|
||||
mPowerUsageSummary.mScreenUsagePref = mScreenUsagePref;
|
||||
mPowerUsageSummary.mScreenConsumptionPref = mScreenConsumptionPref;
|
||||
final String expectedUsedTime = mRealContext.getString(R.string.battery_used_for,
|
||||
Utils.formatElapsedTime(mRealContext, USAGE_TIME_MS, false));
|
||||
final double percent = BATTERY_SCREEN_USAGE / TOTAL_POWER * DISCHARGE_AMOUNT;
|
||||
@@ -415,6 +416,16 @@ public class PowerUsageSummaryTest {
|
||||
verify(mScreenConsumptionPref).setSummary(expectedOverallUsage);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdatePreference_UsageListEmpty_ShouldNotCrash() {
|
||||
when(mBatteryHelper.getUsageList()).thenReturn(new ArrayList<BatterySipper>());
|
||||
doReturn("").when(mPowerUsageSummary).getString(anyInt(), Matchers.anyObject());
|
||||
|
||||
// Should not crash when update
|
||||
mPowerUsageSummary.updateScreenPreference(DISCHARGE_AMOUNT);
|
||||
mPowerUsageSummary.updateCellularPreference(DISCHARGE_AMOUNT);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCalculatePercentage() {
|
||||
final double percent = mPowerUsageSummary.calculatePercentage(POWER_MAH, DISCHARGE_AMOUNT);
|
||||
|
Reference in New Issue
Block a user