Loading services/core/java/com/android/server/power/batterysaver/BatterySaverStateMachine.java +4 −2 Original line number Diff line number Diff line Loading @@ -537,6 +537,7 @@ public class BatterySaverStateMachine { Slog.d(TAG, "doAutoBatterySaverLocked: mBootCompleted=" + mBootCompleted + " mSettingsLoaded=" + mSettingsLoaded + " mBatteryStatusSet=" + mBatteryStatusSet + " mState=" + mState + " mIsBatteryLevelLow=" + mIsBatteryLevelLow + " mIsPowered=" + mIsPowered + " mSettingAutomaticBatterySaver=" + mSettingAutomaticBatterySaver Loading Loading @@ -689,9 +690,9 @@ public class BatterySaverStateMachine { final boolean isStickyDisabled = mBatterySaverStickyBehaviourDisabled || !mSettingBatterySaverEnabledSticky; if (isStickyDisabled || shouldTurnOffSticky) { mState = STATE_OFF; setStickyActive(false); triggerStickyDisabledNotification(); mState = STATE_OFF; } else if (!mIsPowered) { // Re-enable BS. enableBatterySaverLocked(/*enable*/ true, /*manual*/ true, Loading Loading @@ -797,7 +798,8 @@ public class BatterySaverStateMachine { Intent.ACTION_POWER_USAGE_SUMMARY)); } private void triggerStickyDisabledNotification() { @VisibleForTesting void triggerStickyDisabledNotification() { NotificationManager manager = mContext.getSystemService(NotificationManager.class); ensureNotificationChannelExists(manager, BATTERY_SAVER_NOTIF_CHANNEL_ID, R.string.battery_saver_notification_channel_name); Loading services/tests/mockingservicestests/src/com/android/server/power/batterysaver/BatterySaverStateMachineTest.java +27 −1 Original line number Diff line number Diff line Loading @@ -15,6 +15,10 @@ */ package com.android.server.power.batterysaver; import static com.android.dx.mockito.inline.extended.ExtendedMockito.doNothing; import static com.android.dx.mockito.inline.extended.ExtendedMockito.inOrder; import static com.android.dx.mockito.inline.extended.ExtendedMockito.spyOn; import static org.junit.Assert.assertEquals; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyBoolean; Loading @@ -22,6 +26,8 @@ import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import android.app.NotificationManager; Loading @@ -37,6 +43,7 @@ import androidx.test.runner.AndroidJUnit4; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.InOrder; import java.util.HashMap; import java.util.Objects; Loading Loading @@ -201,6 +208,8 @@ public class BatterySaverStateMachineTest { mDevice = new Device(); mTarget = new TestableBatterySaverStateMachine(); spyOn(mTarget); doNothing().when(mTarget).triggerStickyDisabledNotification(); mDevice.pushBatteryStatus(); mTarget.onBootCompleted(); Loading Loading @@ -423,7 +432,7 @@ public class BatterySaverStateMachineTest { assertEquals(70, mPersistedState.batteryLevel); assertEquals(false, mPersistedState.batteryLow); // Bump ump the threshold. // Bump up the threshold. mDevice.putGlobalSetting(Global.LOW_POWER_MODE_TRIGGER_LEVEL, 70); mDevice.setBatteryLevel(mPersistedState.batteryLevel); Loading Loading @@ -545,6 +554,8 @@ public class BatterySaverStateMachineTest { @Test public void testAutoBatterySaver_withSticky_withAutoOffEnabled() { InOrder inOrder = inOrder(mTarget); mDevice.putGlobalSetting(Global.LOW_POWER_MODE_TRIGGER_LEVEL, 50); mDevice.putGlobalSetting(Global.LOW_POWER_MODE_STICKY_AUTO_DISABLE_ENABLED, 1); mDevice.putGlobalSetting(Global.LOW_POWER_MODE_STICKY_AUTO_DISABLE_LEVEL, 90); Loading @@ -569,6 +580,7 @@ public class BatterySaverStateMachineTest { assertEquals(true, mDevice.batterySaverEnabled); // Stays on. assertEquals(95, mPersistedState.batteryLevel); assertEquals(false, mPersistedState.batteryLow); inOrder.verify(mTarget, never()).triggerStickyDisabledNotification(); // Scenario 2: User turns BS on manually above the threshold then charges device. BS // shouldn't turn back on. Loading @@ -584,6 +596,7 @@ public class BatterySaverStateMachineTest { assertEquals(false, mDevice.batterySaverEnabled); // Sticky BS no longer enabled. assertEquals(97, mPersistedState.batteryLevel); assertEquals(false, mPersistedState.batteryLow); inOrder.verify(mTarget).triggerStickyDisabledNotification(); // Scenario 3: User turns BS on manually above the threshold. Device drains below // threshold and then charged to below threshold. Sticky BS should activate. Loading Loading @@ -612,6 +625,7 @@ public class BatterySaverStateMachineTest { assertEquals(true, mDevice.batterySaverEnabled); assertEquals(30, mPersistedState.batteryLevel); assertEquals(true, mPersistedState.batteryLow); inOrder.verify(mTarget, never()).triggerStickyDisabledNotification(); // Scenario 4: User turns BS on manually above the threshold. Device drains below // threshold and is eventually charged to above threshold. Sticky BS should turn off. Loading @@ -627,6 +641,7 @@ public class BatterySaverStateMachineTest { assertEquals(false, mDevice.batterySaverEnabled); // Sticky BS no longer enabled. assertEquals(90, mPersistedState.batteryLevel); assertEquals(false, mPersistedState.batteryLow); inOrder.verify(mTarget).triggerStickyDisabledNotification(); // Scenario 5: User turns BS on manually below threshold and charges to below threshold. // Sticky BS should activate. Loading Loading @@ -654,6 +669,7 @@ public class BatterySaverStateMachineTest { assertEquals(true, mDevice.batterySaverEnabled); // Sticky BS still on. assertEquals(80, mPersistedState.batteryLevel); assertEquals(false, mPersistedState.batteryLow); inOrder.verify(mTarget, never()).triggerStickyDisabledNotification(); // Scenario 6: User turns BS on manually below threshold and eventually charges to above // threshold. Sticky BS should turn off. Loading @@ -665,6 +681,7 @@ public class BatterySaverStateMachineTest { assertEquals(false, mDevice.batterySaverEnabled); assertEquals(95, mPersistedState.batteryLevel); assertEquals(false, mPersistedState.batteryLow); inOrder.verify(mTarget).triggerStickyDisabledNotification(); // Scenario 7: User turns BS on above threshold and then reboots device. Sticky BS // shouldn't activate. Loading @@ -676,6 +693,8 @@ public class BatterySaverStateMachineTest { assertEquals(false, mDevice.batterySaverEnabled); assertEquals(93, mPersistedState.batteryLevel); assertEquals(false, mPersistedState.batteryLow); // initDevice() changes the mTarget reference, so inOrder is invalid here. verify(mTarget).triggerStickyDisabledNotification(); // Scenario 8: User turns BS on below threshold and then reboots device without charging. // Sticky BS should activate. Loading @@ -690,6 +709,8 @@ public class BatterySaverStateMachineTest { assertEquals(true, mDevice.batterySaverEnabled); assertEquals(75, mPersistedState.batteryLevel); assertEquals(false, mPersistedState.batteryLow); // initDevice() changes the mTarget reference, so inOrder is invalid here. verify(mTarget, never()).triggerStickyDisabledNotification(); // Scenario 9: User turns BS on below threshold and then reboots device after charging // above threshold. Sticky BS shouldn't activate. Loading @@ -702,6 +723,8 @@ public class BatterySaverStateMachineTest { assertEquals(false, mDevice.batterySaverEnabled); assertEquals(100, mPersistedState.batteryLevel); assertEquals(false, mPersistedState.batteryLow); // initDevice() changes the mTarget reference, so inOrder is invalid here. verify(mTarget).triggerStickyDisabledNotification(); // Scenario 10: Somehow autoDisableLevel is set to a value below lowPowerModeTriggerLevel // and then user enables manually above both thresholds, discharges below Loading Loading @@ -738,6 +761,8 @@ public class BatterySaverStateMachineTest { assertEquals(true, mDevice.batterySaverEnabled); assertEquals(65, mPersistedState.batteryLevel); assertEquals(true, mPersistedState.batteryLow); // initDevice() changes the mTarget reference, so inOrder is invalid here. verify(mTarget, never()).triggerStickyDisabledNotification(); } @Test Loading Loading @@ -780,6 +805,7 @@ public class BatterySaverStateMachineTest { assertEquals(false, mDevice.batterySaverEnabled); // Sticky BS no longer enabled. assertEquals(95, mPersistedState.batteryLevel); assertEquals(false, mPersistedState.batteryLow); verify(mTarget).triggerStickyDisabledNotification(); } @Test Loading Loading
services/core/java/com/android/server/power/batterysaver/BatterySaverStateMachine.java +4 −2 Original line number Diff line number Diff line Loading @@ -537,6 +537,7 @@ public class BatterySaverStateMachine { Slog.d(TAG, "doAutoBatterySaverLocked: mBootCompleted=" + mBootCompleted + " mSettingsLoaded=" + mSettingsLoaded + " mBatteryStatusSet=" + mBatteryStatusSet + " mState=" + mState + " mIsBatteryLevelLow=" + mIsBatteryLevelLow + " mIsPowered=" + mIsPowered + " mSettingAutomaticBatterySaver=" + mSettingAutomaticBatterySaver Loading Loading @@ -689,9 +690,9 @@ public class BatterySaverStateMachine { final boolean isStickyDisabled = mBatterySaverStickyBehaviourDisabled || !mSettingBatterySaverEnabledSticky; if (isStickyDisabled || shouldTurnOffSticky) { mState = STATE_OFF; setStickyActive(false); triggerStickyDisabledNotification(); mState = STATE_OFF; } else if (!mIsPowered) { // Re-enable BS. enableBatterySaverLocked(/*enable*/ true, /*manual*/ true, Loading Loading @@ -797,7 +798,8 @@ public class BatterySaverStateMachine { Intent.ACTION_POWER_USAGE_SUMMARY)); } private void triggerStickyDisabledNotification() { @VisibleForTesting void triggerStickyDisabledNotification() { NotificationManager manager = mContext.getSystemService(NotificationManager.class); ensureNotificationChannelExists(manager, BATTERY_SAVER_NOTIF_CHANNEL_ID, R.string.battery_saver_notification_channel_name); Loading
services/tests/mockingservicestests/src/com/android/server/power/batterysaver/BatterySaverStateMachineTest.java +27 −1 Original line number Diff line number Diff line Loading @@ -15,6 +15,10 @@ */ package com.android.server.power.batterysaver; import static com.android.dx.mockito.inline.extended.ExtendedMockito.doNothing; import static com.android.dx.mockito.inline.extended.ExtendedMockito.inOrder; import static com.android.dx.mockito.inline.extended.ExtendedMockito.spyOn; import static org.junit.Assert.assertEquals; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyBoolean; Loading @@ -22,6 +26,8 @@ import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import android.app.NotificationManager; Loading @@ -37,6 +43,7 @@ import androidx.test.runner.AndroidJUnit4; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.InOrder; import java.util.HashMap; import java.util.Objects; Loading Loading @@ -201,6 +208,8 @@ public class BatterySaverStateMachineTest { mDevice = new Device(); mTarget = new TestableBatterySaverStateMachine(); spyOn(mTarget); doNothing().when(mTarget).triggerStickyDisabledNotification(); mDevice.pushBatteryStatus(); mTarget.onBootCompleted(); Loading Loading @@ -423,7 +432,7 @@ public class BatterySaverStateMachineTest { assertEquals(70, mPersistedState.batteryLevel); assertEquals(false, mPersistedState.batteryLow); // Bump ump the threshold. // Bump up the threshold. mDevice.putGlobalSetting(Global.LOW_POWER_MODE_TRIGGER_LEVEL, 70); mDevice.setBatteryLevel(mPersistedState.batteryLevel); Loading Loading @@ -545,6 +554,8 @@ public class BatterySaverStateMachineTest { @Test public void testAutoBatterySaver_withSticky_withAutoOffEnabled() { InOrder inOrder = inOrder(mTarget); mDevice.putGlobalSetting(Global.LOW_POWER_MODE_TRIGGER_LEVEL, 50); mDevice.putGlobalSetting(Global.LOW_POWER_MODE_STICKY_AUTO_DISABLE_ENABLED, 1); mDevice.putGlobalSetting(Global.LOW_POWER_MODE_STICKY_AUTO_DISABLE_LEVEL, 90); Loading @@ -569,6 +580,7 @@ public class BatterySaverStateMachineTest { assertEquals(true, mDevice.batterySaverEnabled); // Stays on. assertEquals(95, mPersistedState.batteryLevel); assertEquals(false, mPersistedState.batteryLow); inOrder.verify(mTarget, never()).triggerStickyDisabledNotification(); // Scenario 2: User turns BS on manually above the threshold then charges device. BS // shouldn't turn back on. Loading @@ -584,6 +596,7 @@ public class BatterySaverStateMachineTest { assertEquals(false, mDevice.batterySaverEnabled); // Sticky BS no longer enabled. assertEquals(97, mPersistedState.batteryLevel); assertEquals(false, mPersistedState.batteryLow); inOrder.verify(mTarget).triggerStickyDisabledNotification(); // Scenario 3: User turns BS on manually above the threshold. Device drains below // threshold and then charged to below threshold. Sticky BS should activate. Loading Loading @@ -612,6 +625,7 @@ public class BatterySaverStateMachineTest { assertEquals(true, mDevice.batterySaverEnabled); assertEquals(30, mPersistedState.batteryLevel); assertEquals(true, mPersistedState.batteryLow); inOrder.verify(mTarget, never()).triggerStickyDisabledNotification(); // Scenario 4: User turns BS on manually above the threshold. Device drains below // threshold and is eventually charged to above threshold. Sticky BS should turn off. Loading @@ -627,6 +641,7 @@ public class BatterySaverStateMachineTest { assertEquals(false, mDevice.batterySaverEnabled); // Sticky BS no longer enabled. assertEquals(90, mPersistedState.batteryLevel); assertEquals(false, mPersistedState.batteryLow); inOrder.verify(mTarget).triggerStickyDisabledNotification(); // Scenario 5: User turns BS on manually below threshold and charges to below threshold. // Sticky BS should activate. Loading Loading @@ -654,6 +669,7 @@ public class BatterySaverStateMachineTest { assertEquals(true, mDevice.batterySaverEnabled); // Sticky BS still on. assertEquals(80, mPersistedState.batteryLevel); assertEquals(false, mPersistedState.batteryLow); inOrder.verify(mTarget, never()).triggerStickyDisabledNotification(); // Scenario 6: User turns BS on manually below threshold and eventually charges to above // threshold. Sticky BS should turn off. Loading @@ -665,6 +681,7 @@ public class BatterySaverStateMachineTest { assertEquals(false, mDevice.batterySaverEnabled); assertEquals(95, mPersistedState.batteryLevel); assertEquals(false, mPersistedState.batteryLow); inOrder.verify(mTarget).triggerStickyDisabledNotification(); // Scenario 7: User turns BS on above threshold and then reboots device. Sticky BS // shouldn't activate. Loading @@ -676,6 +693,8 @@ public class BatterySaverStateMachineTest { assertEquals(false, mDevice.batterySaverEnabled); assertEquals(93, mPersistedState.batteryLevel); assertEquals(false, mPersistedState.batteryLow); // initDevice() changes the mTarget reference, so inOrder is invalid here. verify(mTarget).triggerStickyDisabledNotification(); // Scenario 8: User turns BS on below threshold and then reboots device without charging. // Sticky BS should activate. Loading @@ -690,6 +709,8 @@ public class BatterySaverStateMachineTest { assertEquals(true, mDevice.batterySaverEnabled); assertEquals(75, mPersistedState.batteryLevel); assertEquals(false, mPersistedState.batteryLow); // initDevice() changes the mTarget reference, so inOrder is invalid here. verify(mTarget, never()).triggerStickyDisabledNotification(); // Scenario 9: User turns BS on below threshold and then reboots device after charging // above threshold. Sticky BS shouldn't activate. Loading @@ -702,6 +723,8 @@ public class BatterySaverStateMachineTest { assertEquals(false, mDevice.batterySaverEnabled); assertEquals(100, mPersistedState.batteryLevel); assertEquals(false, mPersistedState.batteryLow); // initDevice() changes the mTarget reference, so inOrder is invalid here. verify(mTarget).triggerStickyDisabledNotification(); // Scenario 10: Somehow autoDisableLevel is set to a value below lowPowerModeTriggerLevel // and then user enables manually above both thresholds, discharges below Loading Loading @@ -738,6 +761,8 @@ public class BatterySaverStateMachineTest { assertEquals(true, mDevice.batterySaverEnabled); assertEquals(65, mPersistedState.batteryLevel); assertEquals(true, mPersistedState.batteryLow); // initDevice() changes the mTarget reference, so inOrder is invalid here. verify(mTarget, never()).triggerStickyDisabledNotification(); } @Test Loading Loading @@ -780,6 +805,7 @@ public class BatterySaverStateMachineTest { assertEquals(false, mDevice.batterySaverEnabled); // Sticky BS no longer enabled. assertEquals(95, mPersistedState.batteryLevel); assertEquals(false, mPersistedState.batteryLow); verify(mTarget).triggerStickyDisabledNotification(); } @Test Loading