Log battery saver disabled event and reason (1/2)
Bug: 218405966 Test: make SettingsLibRoboTests Change-Id: I3ac8829c4393bd3f75221a509364ae0007972c6e
This commit is contained in:
@@ -32,9 +32,13 @@ public final class BatterySaverLogging {
|
||||
public static final String EXTRA_POWER_SAVE_MODE_MANUAL_ENABLED_REASON =
|
||||
"extra_power_save_mode_manual_enabled_reason";
|
||||
|
||||
/** Record the event while enabling power save mode manually. */
|
||||
public static final String EXTRA_POWER_SAVE_MODE_MANUAL_ENABLED =
|
||||
"extra_power_save_mode_manual_enabled";
|
||||
|
||||
/** Broadcast action to record battery saver manual enabled reason. */
|
||||
public static final String ACTION_SAVER_MANUAL_ENABLED_REASON =
|
||||
"com.android.settingslib.fuelgauge.ACTION_SAVER_MANUAL_ENABLED_REASON";
|
||||
public static final String ACTION_SAVER_STATE_MANUAL_UPDATE =
|
||||
"com.android.settingslib.fuelgauge.ACTION_SAVER_STATE_MANUAL_UPDATE";
|
||||
|
||||
/** An interface for the battery saver manual enable reason. */
|
||||
@Retention(RetentionPolicy.SOURCE)
|
||||
|
||||
@@ -16,7 +16,8 @@
|
||||
|
||||
package com.android.settingslib.fuelgauge;
|
||||
|
||||
import static com.android.settingslib.fuelgauge.BatterySaverLogging.ACTION_SAVER_MANUAL_ENABLED_REASON;
|
||||
import static com.android.settingslib.fuelgauge.BatterySaverLogging.ACTION_SAVER_STATE_MANUAL_UPDATE;
|
||||
import static com.android.settingslib.fuelgauge.BatterySaverLogging.EXTRA_POWER_SAVE_MODE_MANUAL_ENABLED;
|
||||
import static com.android.settingslib.fuelgauge.BatterySaverLogging.EXTRA_POWER_SAVE_MODE_MANUAL_ENABLED_REASON;
|
||||
import static com.android.settingslib.fuelgauge.BatterySaverLogging.SaverManualEnabledReason;
|
||||
|
||||
@@ -152,9 +153,8 @@ public class BatterySaverUtils {
|
||||
sendSystemUiBroadcast(context, ACTION_SHOW_AUTO_SAVER_SUGGESTION,
|
||||
confirmationExtras);
|
||||
}
|
||||
recordBatterySaverEnabledReason(context, reason);
|
||||
}
|
||||
|
||||
recordBatterySaverEnabledReason(context, enable, reason);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
@@ -185,11 +185,12 @@ public class BatterySaverUtils {
|
||||
return true;
|
||||
}
|
||||
|
||||
private static void recordBatterySaverEnabledReason(Context context,
|
||||
private static void recordBatterySaverEnabledReason(Context context, boolean enable,
|
||||
@SaverManualEnabledReason int reason) {
|
||||
final Bundle enabledReasonExtras = new Bundle(1);
|
||||
final Bundle enabledReasonExtras = new Bundle(2);
|
||||
enabledReasonExtras.putInt(EXTRA_POWER_SAVE_MODE_MANUAL_ENABLED_REASON, reason);
|
||||
sendSystemUiBroadcast(context, ACTION_SAVER_MANUAL_ENABLED_REASON, enabledReasonExtras);
|
||||
enabledReasonExtras.putBoolean(EXTRA_POWER_SAVE_MODE_MANUAL_ENABLED, enable);
|
||||
sendSystemUiBroadcast(context, ACTION_SAVER_STATE_MANUAL_UPDATE, enabledReasonExtras);
|
||||
}
|
||||
|
||||
private static void sendSystemUiBroadcast(Context context, String action, Bundle extras) {
|
||||
|
||||
@@ -16,14 +16,16 @@
|
||||
|
||||
package com.android.settingslib.fuelgauge;
|
||||
|
||||
import static com.android.settingslib.fuelgauge.BatterySaverLogging.ACTION_SAVER_STATE_MANUAL_UPDATE;
|
||||
import static com.android.settingslib.fuelgauge.BatterySaverLogging.SAVER_ENABLED_UNKNOWN;
|
||||
import static com.android.settingslib.fuelgauge.BatterySaverUtils.ACTION_SHOW_AUTO_SAVER_SUGGESTION;
|
||||
import static com.android.settingslib.fuelgauge.BatterySaverUtils.ACTION_SHOW_START_SAVER_CONFIRMATION;
|
||||
import static com.android.settingslib.fuelgauge.BatterySaverUtils.KEY_NO_SCHEDULE;
|
||||
import static com.android.settingslib.fuelgauge.BatterySaverUtils.KEY_PERCENTAGE;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyBoolean;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.times;
|
||||
@@ -40,10 +42,13 @@ import android.provider.Settings.Secure;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public class BatterySaverUtilsTest {
|
||||
private static final int BATTERY_SAVER_THRESHOLD_1 = 15;
|
||||
@@ -68,7 +73,7 @@ public class BatterySaverUtilsTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetPowerSaveMode_enable_firstCall_needWarning() {
|
||||
public void testSetPowerSaveMode_enableWithWarning_firstCall_needConfirmationWarning() {
|
||||
Secure.putString(mMockResolver, Secure.LOW_POWER_WARNING_ACKNOWLEDGED, "null");
|
||||
Secure.putString(mMockResolver, Secure.EXTRA_LOW_POWER_WARNING_ACKNOWLEDGED, "null");
|
||||
Secure.putString(mMockResolver, Secure.LOW_POWER_MANUAL_ACTIVATION_COUNT, "null");
|
||||
@@ -76,9 +81,12 @@ public class BatterySaverUtilsTest {
|
||||
assertThat(BatterySaverUtils.setPowerSaveMode(mMockContext, true, true,
|
||||
SAVER_ENABLED_UNKNOWN)).isFalse();
|
||||
|
||||
verify(mMockContext, times(1)).sendBroadcast(any(Intent.class));
|
||||
ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class);
|
||||
verify(mMockContext, times(1)).sendBroadcast(intentCaptor.capture());
|
||||
verify(mMockPowerManager, times(0)).setPowerSaveModeEnabled(anyBoolean());
|
||||
|
||||
assertThat(intentCaptor.getValue().getAction()).isEqualTo(
|
||||
ACTION_SHOW_START_SAVER_CONFIRMATION);
|
||||
// They shouldn't have changed.
|
||||
assertEquals(-1, Secure.getInt(mMockResolver, Secure.LOW_POWER_WARNING_ACKNOWLEDGED, -1));
|
||||
assertEquals(-1,
|
||||
@@ -88,7 +96,7 @@ public class BatterySaverUtilsTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetPowerSaveMode_enable_secondCall_needWarning() {
|
||||
public void testSetPowerSaveMode_enableWithWarning_secondCall_expectUpdateIntent() {
|
||||
// Already acked.
|
||||
Secure.putInt(mMockResolver, Secure.LOW_POWER_WARNING_ACKNOWLEDGED, 1);
|
||||
Secure.putInt(mMockResolver, Secure.EXTRA_LOW_POWER_WARNING_ACKNOWLEDGED, 1);
|
||||
@@ -97,8 +105,12 @@ public class BatterySaverUtilsTest {
|
||||
assertThat(BatterySaverUtils.setPowerSaveMode(mMockContext, true, true,
|
||||
SAVER_ENABLED_UNKNOWN)).isTrue();
|
||||
|
||||
ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class);
|
||||
verify(mMockContext, times(1)).sendBroadcast(intentCaptor.capture());
|
||||
verify(mMockPowerManager, times(1)).setPowerSaveModeEnabled(eq(true));
|
||||
|
||||
assertThat(intentCaptor.getValue().getAction()).isEqualTo(
|
||||
ACTION_SAVER_STATE_MANUAL_UPDATE);
|
||||
assertEquals(1, Secure.getInt(mMockResolver, Secure.LOW_POWER_WARNING_ACKNOWLEDGED, -1));
|
||||
assertEquals(1,
|
||||
Secure.getInt(mMockResolver, Secure.EXTRA_LOW_POWER_WARNING_ACKNOWLEDGED, -1));
|
||||
@@ -107,7 +119,7 @@ public class BatterySaverUtilsTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetPowerSaveMode_enable_thridCall_needWarning() {
|
||||
public void testSetPowerSaveMode_enableWithWarning_thirdCall_expectUpdateIntent() {
|
||||
// Already acked.
|
||||
Secure.putInt(mMockResolver, Secure.LOW_POWER_WARNING_ACKNOWLEDGED, 1);
|
||||
Secure.putInt(mMockResolver, Secure.EXTRA_LOW_POWER_WARNING_ACKNOWLEDGED, 1);
|
||||
@@ -116,8 +128,12 @@ public class BatterySaverUtilsTest {
|
||||
assertThat(BatterySaverUtils.setPowerSaveMode(mMockContext, true, true,
|
||||
SAVER_ENABLED_UNKNOWN)).isTrue();
|
||||
|
||||
ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class);
|
||||
verify(mMockContext, times(1)).sendBroadcast(intentCaptor.capture());
|
||||
verify(mMockPowerManager, times(1)).setPowerSaveModeEnabled(eq(true));
|
||||
|
||||
assertThat(intentCaptor.getValue().getAction()).isEqualTo(
|
||||
ACTION_SAVER_STATE_MANUAL_UPDATE);
|
||||
assertEquals(1, Secure.getInt(mMockResolver, Secure.LOW_POWER_WARNING_ACKNOWLEDGED, -1));
|
||||
assertEquals(1,
|
||||
Secure.getInt(mMockResolver, Secure.EXTRA_LOW_POWER_WARNING_ACKNOWLEDGED, -1));
|
||||
@@ -126,7 +142,31 @@ public class BatterySaverUtilsTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetPowerSaveMode_enable_firstCall_noWarning() {
|
||||
public void testSetPowerSaveMode_enableWithWarning_5thCall_needAutoSuggestionWarning() {
|
||||
// Already acked.
|
||||
Secure.putInt(mMockResolver, Secure.LOW_POWER_WARNING_ACKNOWLEDGED, 1);
|
||||
Secure.putInt(mMockResolver, Secure.EXTRA_LOW_POWER_WARNING_ACKNOWLEDGED, 1);
|
||||
Secure.putInt(mMockResolver, Secure.LOW_POWER_MANUAL_ACTIVATION_COUNT, 3);
|
||||
|
||||
assertThat(BatterySaverUtils.setPowerSaveMode(mMockContext, true, true,
|
||||
SAVER_ENABLED_UNKNOWN)).isTrue();
|
||||
|
||||
ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class);
|
||||
verify(mMockContext, times(2)).sendBroadcast(intentCaptor.capture());
|
||||
verify(mMockPowerManager, times(1)).setPowerSaveModeEnabled(eq(true));
|
||||
|
||||
List<Intent> values = intentCaptor.getAllValues();
|
||||
assertThat(values.get(0).getAction()).isEqualTo(ACTION_SHOW_AUTO_SAVER_SUGGESTION);
|
||||
assertThat(values.get(1).getAction()).isEqualTo(ACTION_SAVER_STATE_MANUAL_UPDATE);
|
||||
assertEquals(1, Secure.getInt(mMockResolver, Secure.LOW_POWER_WARNING_ACKNOWLEDGED, -1));
|
||||
assertEquals(1,
|
||||
Secure.getInt(mMockResolver, Secure.EXTRA_LOW_POWER_WARNING_ACKNOWLEDGED, -1));
|
||||
assertEquals(4,
|
||||
Secure.getInt(mMockResolver, Secure.LOW_POWER_MANUAL_ACTIVATION_COUNT, -2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetPowerSaveMode_enableWithoutWarning_expectUpdateIntent() {
|
||||
Secure.putString(mMockResolver, Secure.LOW_POWER_WARNING_ACKNOWLEDGED, "null");
|
||||
Secure.putString(mMockResolver, Secure.EXTRA_LOW_POWER_WARNING_ACKNOWLEDGED, "null");
|
||||
Secure.putString(mMockResolver, Secure.LOW_POWER_MANUAL_ACTIVATION_COUNT, "null");
|
||||
@@ -134,8 +174,12 @@ public class BatterySaverUtilsTest {
|
||||
assertThat(BatterySaverUtils.setPowerSaveMode(mMockContext, true, false,
|
||||
SAVER_ENABLED_UNKNOWN)).isTrue();
|
||||
|
||||
ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class);
|
||||
verify(mMockContext, times(1)).sendBroadcast(intentCaptor.capture());
|
||||
verify(mMockPowerManager, times(1)).setPowerSaveModeEnabled(eq(true));
|
||||
|
||||
assertThat(intentCaptor.getValue().getAction()).isEqualTo(
|
||||
ACTION_SAVER_STATE_MANUAL_UPDATE);
|
||||
assertEquals(1, Secure.getInt(mMockResolver, Secure.LOW_POWER_WARNING_ACKNOWLEDGED, -1));
|
||||
assertEquals(1,
|
||||
Secure.getInt(mMockResolver, Secure.EXTRA_LOW_POWER_WARNING_ACKNOWLEDGED, -1));
|
||||
@@ -143,43 +187,13 @@ public class BatterySaverUtilsTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetPowerSaveMode_disable_firstCall_noWarning() {
|
||||
Secure.putString(mMockResolver, Secure.LOW_POWER_WARNING_ACKNOWLEDGED, "null");
|
||||
Secure.putString(mMockResolver, Secure.EXTRA_LOW_POWER_WARNING_ACKNOWLEDGED, "null");
|
||||
Secure.putString(mMockResolver, Secure.LOW_POWER_MANUAL_ACTIVATION_COUNT, "null");
|
||||
|
||||
// When disabling, needFirstTimeWarning doesn't matter.
|
||||
assertThat(BatterySaverUtils.setPowerSaveMode(mMockContext, false, false,
|
||||
SAVER_ENABLED_UNKNOWN)).isTrue();
|
||||
|
||||
verify(mMockContext, times(0)).sendBroadcast(any(Intent.class));
|
||||
verify(mMockPowerManager, times(1)).setPowerSaveModeEnabled(eq(false));
|
||||
|
||||
assertEquals(-1, Secure.getInt(mMockResolver, Secure.LOW_POWER_WARNING_ACKNOWLEDGED, -1));
|
||||
assertEquals(-1,
|
||||
Secure.getInt(mMockResolver, Secure.EXTRA_LOW_POWER_WARNING_ACKNOWLEDGED, -1));
|
||||
assertEquals(-2,
|
||||
Secure.getInt(mMockResolver, Secure.LOW_POWER_MANUAL_ACTIVATION_COUNT, -2));
|
||||
public void testSetPowerSaveMode_disableWithoutWarning_expectUpdateIntent() {
|
||||
verifyDisablePowerSaveMode(/* needFirstTimeWarning= */ false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetPowerSaveMode_disable_firstCall_needWarning() {
|
||||
Secure.putString(mMockResolver, Secure.LOW_POWER_WARNING_ACKNOWLEDGED, "null");
|
||||
Secure.putString(mMockResolver, Secure.EXTRA_LOW_POWER_WARNING_ACKNOWLEDGED, "null");
|
||||
Secure.putString(mMockResolver, Secure.LOW_POWER_MANUAL_ACTIVATION_COUNT, "null");
|
||||
|
||||
// When disabling, needFirstTimeWarning doesn't matter.
|
||||
assertThat(BatterySaverUtils.setPowerSaveMode(mMockContext, false, true,
|
||||
SAVER_ENABLED_UNKNOWN)).isTrue();
|
||||
|
||||
verify(mMockContext, times(0)).sendBroadcast(any(Intent.class));
|
||||
verify(mMockPowerManager, times(1)).setPowerSaveModeEnabled(eq(false));
|
||||
|
||||
assertEquals(-1, Secure.getInt(mMockResolver, Secure.LOW_POWER_WARNING_ACKNOWLEDGED, -1));
|
||||
assertEquals(-1,
|
||||
Secure.getInt(mMockResolver, Secure.EXTRA_LOW_POWER_WARNING_ACKNOWLEDGED, -1));
|
||||
assertEquals(-2,
|
||||
Secure.getInt(mMockResolver, Secure.LOW_POWER_MANUAL_ACTIVATION_COUNT, -2));
|
||||
public void testSetPowerSaveMode_disableWithWarning_expectUpdateIntent() {
|
||||
verifyDisablePowerSaveMode(/* needFirstTimeWarning= */ true);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -256,4 +270,26 @@ public class BatterySaverUtilsTest {
|
||||
.isEqualTo(20);
|
||||
|
||||
}
|
||||
|
||||
private void verifyDisablePowerSaveMode(boolean needFirstTimeWarning) {
|
||||
Secure.putString(mMockResolver, Secure.LOW_POWER_WARNING_ACKNOWLEDGED, "null");
|
||||
Secure.putString(mMockResolver, Secure.EXTRA_LOW_POWER_WARNING_ACKNOWLEDGED, "null");
|
||||
Secure.putString(mMockResolver, Secure.LOW_POWER_MANUAL_ACTIVATION_COUNT, "null");
|
||||
|
||||
// When disabling, needFirstTimeWarning doesn't matter.
|
||||
assertThat(BatterySaverUtils.setPowerSaveMode(mMockContext, false, needFirstTimeWarning,
|
||||
SAVER_ENABLED_UNKNOWN)).isTrue();
|
||||
|
||||
ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class);
|
||||
verify(mMockContext, times(1)).sendBroadcast(intentCaptor.capture());
|
||||
verify(mMockPowerManager, times(1)).setPowerSaveModeEnabled(eq(false));
|
||||
|
||||
assertThat(intentCaptor.getValue().getAction()).isEqualTo(
|
||||
ACTION_SAVER_STATE_MANUAL_UPDATE);
|
||||
assertEquals(-1, Secure.getInt(mMockResolver, Secure.LOW_POWER_WARNING_ACKNOWLEDGED, -1));
|
||||
assertEquals(-1,
|
||||
Secure.getInt(mMockResolver, Secure.EXTRA_LOW_POWER_WARNING_ACKNOWLEDGED, -1));
|
||||
assertEquals(-2,
|
||||
Secure.getInt(mMockResolver, Secure.LOW_POWER_MANUAL_ACTIVATION_COUNT, -2));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user