Verify and reset invalid battery setting in the data restore stage
Verify and reset invalid battery setting configuration in the data restore stage to avoid resoring invalid configuration from other devices Bug: 258243197 Test: make RunSettingsRoboTests Change-Id: I1a0febd2ad527147e4e9e9c77734c4b51963263a
This commit is contained in:
@@ -88,6 +88,7 @@ public final class BatteryBackupHelper implements BackupHelper {
|
||||
|
||||
@Override
|
||||
public void restoreEntity(BackupDataInputStream data) {
|
||||
BatterySettingsMigrateChecker.verifyConfiguration(mContext);
|
||||
if (!isOwner() || data == null || data.size() == 0) {
|
||||
Log.w(TAG, "ignore restoreEntity() for non-owner or empty data");
|
||||
return;
|
||||
|
@@ -31,7 +31,14 @@ public final class BatterySettingsMigrateChecker extends BroadcastReceiver {
|
||||
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
verifySaverConfiguration(context.getApplicationContext());
|
||||
if (intent != null && Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
|
||||
verifyConfiguration(context);
|
||||
}
|
||||
}
|
||||
|
||||
static void verifyConfiguration(Context context) {
|
||||
context = context.getApplicationContext();
|
||||
verifySaverConfiguration(context);
|
||||
}
|
||||
|
||||
private static void verifySaverConfiguration(Context context) {
|
||||
|
@@ -16,6 +16,10 @@
|
||||
|
||||
package com.android.settings;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.provider.Settings;
|
||||
|
||||
/**
|
||||
* Convenience methods and constants for testing.
|
||||
*/
|
||||
@@ -23,4 +27,15 @@ public class TestUtils {
|
||||
public static final long KILOBYTE = 1024L; // TODO: Change to 1000 in O Robolectric.
|
||||
public static final long MEGABYTE = KILOBYTE * KILOBYTE;
|
||||
public static final long GIGABYTE = KILOBYTE * MEGABYTE;
|
||||
|
||||
public static void setScheduledLevel(Context context, int scheduledLevel) {
|
||||
Settings.Global.putInt(context.getContentResolver(),
|
||||
Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, scheduledLevel);
|
||||
}
|
||||
|
||||
public static int getScheduledLevel(Context context) {
|
||||
return Settings.Global.getInt(context.getContentResolver(),
|
||||
Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, /*defaultValue*/ 0);
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -51,6 +51,7 @@ import android.os.UserHandle;
|
||||
import android.os.UserManager;
|
||||
import android.util.ArraySet;
|
||||
|
||||
import com.android.settings.TestUtils;
|
||||
import com.android.settings.fuelgauge.BatteryOptimizeHistoricalLogEntry.Action;
|
||||
import com.android.settingslib.fuelgauge.PowerAllowlistBackend;
|
||||
|
||||
@@ -290,6 +291,16 @@ public final class BatteryBackupHelperTest {
|
||||
assertThat(captor.getValue().length).isEqualTo(dataSize);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void restoreEntity_verifyConfiguration() {
|
||||
final int invalidScheduledLevel = 5;
|
||||
TestUtils.setScheduledLevel(mContext, invalidScheduledLevel);
|
||||
|
||||
mBatteryBackupHelper.restoreEntity(mBackupDataInputStream);
|
||||
|
||||
assertThat(TestUtils.getScheduledLevel(mContext)).isNotEqualTo(invalidScheduledLevel);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void restoreOptimizationMode_nullBytesData_skipRestore() throws Exception {
|
||||
mBatteryBackupHelper.restoreOptimizationMode(new byte[0]);
|
||||
|
@@ -20,8 +20,8 @@ import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.provider.Settings;
|
||||
|
||||
import com.android.settings.TestUtils;
|
||||
import com.android.settings.fuelgauge.batterysaver.BatterySaverScheduleRadioButtonsController;
|
||||
|
||||
import org.junit.Before;
|
||||
@@ -34,6 +34,9 @@ import org.robolectric.RuntimeEnvironment;
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public final class BatterySettingsMigrateCheckerTest {
|
||||
|
||||
private static final Intent BOOT_COMPLETED_INTENT =
|
||||
new Intent(Intent.ACTION_BOOT_COMPLETED);
|
||||
|
||||
private Context mContext;
|
||||
private BatterySettingsMigrateChecker mBatterySettingsMigrateChecker;
|
||||
|
||||
@@ -49,7 +52,7 @@ public final class BatterySettingsMigrateCheckerTest {
|
||||
final int invalidScheduledLevel = 5;
|
||||
setScheduledLevel(invalidScheduledLevel);
|
||||
|
||||
mBatterySettingsMigrateChecker.onReceive(mContext, new Intent());
|
||||
mBatterySettingsMigrateChecker.onReceive(mContext, BOOT_COMPLETED_INTENT);
|
||||
|
||||
assertThat(getScheduledLevel())
|
||||
.isEqualTo(BatterySaverScheduleRadioButtonsController.TRIGGER_LEVEL_MIN);
|
||||
@@ -60,7 +63,7 @@ public final class BatterySettingsMigrateCheckerTest {
|
||||
final int validScheduledLevel = 12;
|
||||
setScheduledLevel(validScheduledLevel);
|
||||
|
||||
mBatterySettingsMigrateChecker.onReceive(mContext, new Intent());
|
||||
mBatterySettingsMigrateChecker.onReceive(mContext, BOOT_COMPLETED_INTENT);
|
||||
|
||||
assertThat(getScheduledLevel()).isEqualTo(validScheduledLevel);
|
||||
}
|
||||
@@ -70,18 +73,36 @@ public final class BatterySettingsMigrateCheckerTest {
|
||||
final int validScheduledLevel = 0;
|
||||
setScheduledLevel(validScheduledLevel);
|
||||
|
||||
mBatterySettingsMigrateChecker.onReceive(mContext, new Intent());
|
||||
mBatterySettingsMigrateChecker.onReceive(mContext, BOOT_COMPLETED_INTENT);
|
||||
|
||||
assertThat(getScheduledLevel()).isEqualTo(validScheduledLevel);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onReceive_nullIntnt_noAction() {
|
||||
final int invalidScheduledLevel = 5;
|
||||
setScheduledLevel(invalidScheduledLevel);
|
||||
|
||||
mBatterySettingsMigrateChecker.onReceive(mContext, null);
|
||||
|
||||
assertThat(getScheduledLevel()).isEqualTo(invalidScheduledLevel);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onReceive_invalidIntent_noAction() {
|
||||
final int invalidScheduledLevel = 5;
|
||||
setScheduledLevel(invalidScheduledLevel);
|
||||
|
||||
mBatterySettingsMigrateChecker.onReceive(mContext, new Intent());
|
||||
|
||||
assertThat(getScheduledLevel()).isEqualTo(invalidScheduledLevel);
|
||||
}
|
||||
|
||||
private void setScheduledLevel(int scheduledLevel) {
|
||||
Settings.Global.putInt(mContext.getContentResolver(),
|
||||
Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, scheduledLevel);
|
||||
TestUtils.setScheduledLevel(mContext, scheduledLevel);
|
||||
}
|
||||
|
||||
private int getScheduledLevel() {
|
||||
return Settings.Global.getInt(mContext.getContentResolver(),
|
||||
Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, /*defaultValue*/ 0);
|
||||
return TestUtils.getScheduledLevel(mContext);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user