diff options
| author | 2019-05-15 11:23:19 -0700 | |
|---|---|---|
| committer | 2019-05-15 11:23:19 -0700 | |
| commit | 5bcc89f165286bad2995c19e9a22e8376a837faf (patch) | |
| tree | 490023e3f44ba3ee0a23c5869d2fee7f2061f0d7 | |
| parent | b3196e53d7bb17745526ab3c6821a57784d2bf7c (diff) | |
Fix BatterySaverStateMachineTest.
1. Disable actually posting a notification so that the test doesn't deal
with a SecurityException.
2. Verify that the notification is only posted when expected.
3. Put the state change from PENDING_STICKY_ON to OFF at the
beginning of the block. setStickyActive() calls putGlobalSetting(),
which ends up calling setSettingsLocked() which then goes back to
updateStateLocked(). Since this is all in the same thread, the test was
seeing the state machine post the notification twice. Putting the
state change at the beginning prevents the "second" notification.
Bug: 132620259
Test: atest com.android.server.power.batterysaver.BatterySaverStateMachineTest
Test: atest CtsBatterySavingTestCases
Change-Id: I1ceecc150bc57a37002d7de4ece09b503ec583c8
2 files changed, 31 insertions, 3 deletions
diff --git a/services/core/java/com/android/server/power/batterysaver/BatterySaverStateMachine.java b/services/core/java/com/android/server/power/batterysaver/BatterySaverStateMachine.java index fe0b9a6acc85..b7e18c35829e 100644 --- a/services/core/java/com/android/server/power/batterysaver/BatterySaverStateMachine.java +++ b/services/core/java/com/android/server/power/batterysaver/BatterySaverStateMachine.java @@ -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 @@ -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, @@ -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); diff --git a/services/tests/mockingservicestests/src/com/android/server/power/batterysaver/BatterySaverStateMachineTest.java b/services/tests/mockingservicestests/src/com/android/server/power/batterysaver/BatterySaverStateMachineTest.java index 212d2a845254..a8faa54fe9d6 100644 --- a/services/tests/mockingservicestests/src/com/android/server/power/batterysaver/BatterySaverStateMachineTest.java +++ b/services/tests/mockingservicestests/src/com/android/server/power/batterysaver/BatterySaverStateMachineTest.java @@ -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; @@ -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; @@ -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; @@ -201,6 +208,8 @@ public class BatterySaverStateMachineTest { mDevice = new Device(); mTarget = new TestableBatterySaverStateMachine(); + spyOn(mTarget); + doNothing().when(mTarget).triggerStickyDisabledNotification(); mDevice.pushBatteryStatus(); mTarget.onBootCompleted(); @@ -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); @@ -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); @@ -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. @@ -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. @@ -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. @@ -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. @@ -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. @@ -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. @@ -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. @@ -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. @@ -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 @@ -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 @@ -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 |