Update battery states text

- Update progress bar bottom text when battery states changed(charging,
 discharging, power saver and low battery)

Bug: 183689347
Bug: 177407113
Test: make SettingsRoboTests
Change-Id: I0397eb1dea7fcb14357750f84d1d5b1a7dc7d19a
This commit is contained in:
Wesley.CW Wang
2021-04-06 15:14:59 +08:00
committed by Wesley Wang
parent 1f7c6f03b9
commit 11d74e7231
5 changed files with 145 additions and 5 deletions

View File

@@ -27,6 +27,7 @@ import static org.mockito.ArgumentMatchers.nullable;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;
import android.app.Activity;
@@ -44,6 +45,9 @@ import androidx.recyclerview.widget.RecyclerView;
import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;
import com.android.settings.fuelgauge.batterytip.tips.BatteryTip;
import com.android.settings.fuelgauge.batterytip.tips.LowBatteryTip;
import com.android.settings.fuelgauge.batterytip.tips.SmartBatteryTip;
import com.android.settings.testutils.shadow.ShadowEntityHeaderController;
import com.android.settings.testutils.shadow.ShadowUtils;
import com.android.settings.widget.EntityHeaderController;
@@ -58,7 +62,9 @@ import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.Shadows;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowPowerManager;
@RunWith(RobolectricTestRunner.class)
@Config(shadows = {ShadowEntityHeaderController.class, ShadowUtils.class})
@@ -84,7 +90,7 @@ public class BatteryHeaderPreferenceControllerTest {
private UsageProgressBarPreference mBatteryUsageProgressBarPref;
private BatteryHeaderPreferenceController mController;
private Context mContext;
private PowerManager mPowerManager;
private ShadowPowerManager mShadowPowerManager;
private Intent mBatteryIntent;
private LifecycleOwner mLifecycleOwner;
private Lifecycle mLifecycle;
@@ -109,7 +115,7 @@ public class BatteryHeaderPreferenceControllerTest {
mBatteryInfo.batteryLevel = BATTERY_LEVEL;
mPowerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
mShadowPowerManager = Shadows.shadowOf(mContext.getSystemService(PowerManager.class));
mController = spy(new BatteryHeaderPreferenceController(mContext, PREF_KEY));
mLifecycle.addObserver(mController);
@@ -144,9 +150,7 @@ public class BatteryHeaderPreferenceControllerTest {
@Test
public void updatePreference_updateBatteryInfo() {
mBatteryInfo.remainingLabel = TIME_LEFT;
mBatteryInfo.batteryLevel = BATTERY_LEVEL;
mBatteryInfo.discharging = true;
setChargingState(/* isDischarging */ true);
mController.updateHeaderPreference(mBatteryInfo);
@@ -165,6 +169,68 @@ public class BatteryHeaderPreferenceControllerTest {
verify(mBatteryUsageProgressBarPref).setBottomSummary(BATTERY_STATUS);
}
@Test
public void updatePreference_charging_showFullText() {
setChargingState(/* isDischarging */ false);
mController.updateHeaderPreference(mBatteryInfo);
final String expectedResult = BATTERY_STATUS + "" + TIME_LEFT;
verify(mBatteryUsageProgressBarPref).setBottomSummary(expectedResult);
}
@Test
public void updatePreference_powerSaverOn_showPowerSaverOn() {
setChargingState(/* isDischarging */ true);
mShadowPowerManager.setIsPowerSaveMode(true);
mController.updateHeaderPreference(mBatteryInfo);
final String expectedResult = "Battery Saver is on • " + TIME_LEFT;
verify(mBatteryUsageProgressBarPref).setBottomSummary(expectedResult);
}
@Test
public void updateHeaderByBatteryTips_lowBatteryTip_showLowBattery() {
setChargingState(/* isDischarging */ true);
BatteryTip lowBatteryTip = new LowBatteryTip(
BatteryTip.StateType.NEW, /* powerSaveModeOn */false);
mController.updateHeaderByBatteryTips(lowBatteryTip, mBatteryInfo);
final String expectedResult = "Low battery • " + TIME_LEFT;
verify(mBatteryUsageProgressBarPref).setBottomSummary(expectedResult);
}
@Test
public void updateHeaderByBatteryTips_notLowBatteryTip_showRemainingLabel() {
setChargingState(/* isDischarging */ true);
BatteryTip lowBatteryTip = new SmartBatteryTip(BatteryTip.StateType.NEW);
mController.updateHeaderByBatteryTips(lowBatteryTip, mBatteryInfo);
verify(mBatteryUsageProgressBarPref).setBottomSummary(mBatteryInfo.remainingLabel);
}
@Test
public void updateHeaderByBatteryTips_noTip_noAction() {
setChargingState(/* isDischarging */ true);
mController.updateHeaderByBatteryTips(null, mBatteryInfo);
verifyZeroInteractions(mBatteryUsageProgressBarPref);
}
@Test
public void updateHeaderByBatteryTips_noBatteryInfo_noAction() {
BatteryTip lowBatteryTip = new LowBatteryTip(
BatteryTip.StateType.NEW, /* powerSaveModeOn */false);
mController.updateHeaderByBatteryTips(lowBatteryTip, null);
verifyZeroInteractions(mBatteryUsageProgressBarPref);
}
@Test
public void updatePreference_isOverheat_showEmptyText() {
mBatteryInfo.isOverheated = true;
@@ -203,4 +269,10 @@ public class BatteryHeaderPreferenceControllerTest {
return TextUtils.expandTemplate(mContext.getText(R.string.battery_header_title_alternate),
NumberFormat.getIntegerInstance().format(BATTERY_LEVEL));
}
private void setChargingState(boolean isDischarging) {
mBatteryInfo.remainingLabel = TIME_LEFT;
mBatteryInfo.statusLabel = BATTERY_STATUS;
mBatteryInfo.discharging = isDischarging;
}
}