Change def value for LOW_POWER_MODE_TRIGGER_LEVEL

Fixes: 73668682
Test: RunSettingsRoboTests
Change-Id: I2122523c958f12bbdd658ae53d63b00769db3520
This commit is contained in:
jackqdyulei
2018-02-20 18:15:24 -08:00
parent 49c8080a50
commit da7ac699fb
4 changed files with 35 additions and 8 deletions

View File

@@ -29,13 +29,15 @@ import com.android.settings.core.TogglePreferenceController;
*/ */
public class AutoBatterySaverPreferenceController extends TogglePreferenceController implements public class AutoBatterySaverPreferenceController extends TogglePreferenceController implements
Preference.OnPreferenceChangeListener { Preference.OnPreferenceChangeListener {
private static final int LOW_POWER_MODE_TRIGGER_THRESHOLD = 15; private final int mDefWarnLevel;
@VisibleForTesting @VisibleForTesting
static final String KEY_AUTO_BATTERY_SAVER = "auto_battery_saver"; static final String KEY_AUTO_BATTERY_SAVER = "auto_battery_saver";
public AutoBatterySaverPreferenceController(Context context) { public AutoBatterySaverPreferenceController(Context context) {
super(context, KEY_AUTO_BATTERY_SAVER); super(context, KEY_AUTO_BATTERY_SAVER);
mDefWarnLevel = mContext.getResources().getInteger(
com.android.internal.R.integer.config_lowBatteryWarningLevel);
} }
@Override @Override
@@ -46,7 +48,7 @@ public class AutoBatterySaverPreferenceController extends TogglePreferenceContro
@Override @Override
public boolean isChecked() { public boolean isChecked() {
return Settings.Global.getInt(mContext.getContentResolver(), return Settings.Global.getInt(mContext.getContentResolver(),
Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, 0) != 0; Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, mDefWarnLevel) != 0;
} }
@Override @Override
@@ -54,7 +56,7 @@ public class AutoBatterySaverPreferenceController extends TogglePreferenceContro
Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.putInt(mContext.getContentResolver(),
Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL,
isChecked isChecked
? LOW_POWER_MODE_TRIGGER_THRESHOLD ? mDefWarnLevel
: 0); : 0);
return true; return true;
} }

View File

@@ -42,12 +42,15 @@ public class AutoBatterySeekBarPreferenceController extends BasePreferenceContro
LifecycleObserver, OnStart, OnStop, SeekBarPreference.OnPreferenceChangeListener { LifecycleObserver, OnStart, OnStop, SeekBarPreference.OnPreferenceChangeListener {
@VisibleForTesting @VisibleForTesting
static final String KEY_AUTO_BATTERY_SEEK_BAR = "battery_saver_seek_bar"; static final String KEY_AUTO_BATTERY_SEEK_BAR = "battery_saver_seek_bar";
private final int mDefWarnLevel;
private SeekBarPreference mPreference; private SeekBarPreference mPreference;
private AutoBatterySaverSettingObserver mContentObserver; private AutoBatterySaverSettingObserver mContentObserver;
public AutoBatterySeekBarPreferenceController(Context context, Lifecycle lifecycle) { public AutoBatterySeekBarPreferenceController(Context context, Lifecycle lifecycle) {
super(context, KEY_AUTO_BATTERY_SEEK_BAR); super(context, KEY_AUTO_BATTERY_SEEK_BAR);
mContentObserver = new AutoBatterySaverSettingObserver(new Handler(Looper.getMainLooper())); mContentObserver = new AutoBatterySaverSettingObserver(new Handler(Looper.getMainLooper()));
mDefWarnLevel = mContext.getResources().getInteger(
com.android.internal.R.integer.config_lowBatteryWarningLevel);
if (lifecycle != null) { if (lifecycle != null) {
lifecycle.addObserver(this); lifecycle.addObserver(this);
} }
@@ -94,7 +97,7 @@ public class AutoBatterySeekBarPreferenceController extends BasePreferenceContro
void updatePreference(Preference preference) { void updatePreference(Preference preference) {
final ContentResolver contentResolver = mContext.getContentResolver(); final ContentResolver contentResolver = mContext.getContentResolver();
final int level = Settings.Global.getInt(contentResolver, final int level = Settings.Global.getInt(contentResolver,
Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, 0); Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, mDefWarnLevel);
if (level == 0) { if (level == 0) {
preference.setVisible(false); preference.setVisible(false);
} else { } else {

View File

@@ -25,6 +25,7 @@ import android.support.v14.preference.SwitchPreference;
import com.android.settings.TestConfig; import com.android.settings.TestConfig;
import com.android.settings.testutils.SettingsRobolectricTestRunner; import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settings.testutils.shadow.SettingsShadowResources;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@@ -34,7 +35,8 @@ import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config; import org.robolectric.annotation.Config;
@RunWith(SettingsRobolectricTestRunner.class) @RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION, shadows =
SettingsShadowResources.class)
public class AutoBatterySaverPreferenceControllerTest { public class AutoBatterySaverPreferenceControllerTest {
private AutoBatterySaverPreferenceController mController; private AutoBatterySaverPreferenceController mController;
@@ -45,6 +47,8 @@ public class AutoBatterySaverPreferenceControllerTest {
public void setUp() { public void setUp() {
MockitoAnnotations.initMocks(this); MockitoAnnotations.initMocks(this);
SettingsShadowResources.overrideResource(
com.android.internal.R.integer.config_lowBatteryWarningLevel, 15);
mContext = RuntimeEnvironment.application; mContext = RuntimeEnvironment.application;
mPreference = new SwitchPreference(mContext); mPreference = new SwitchPreference(mContext);
mController = new AutoBatterySaverPreferenceController(mContext); mController = new AutoBatterySaverPreferenceController(mContext);
@@ -84,4 +88,9 @@ public class AutoBatterySaverPreferenceControllerTest {
Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, 0)).isEqualTo(0); Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, 0)).isEqualTo(0);
} }
@Test
public void testIsChecked_useDefaultValue_returnTrue() {
assertThat(mController.isChecked()).isTrue();
}
} }

View File

@@ -24,6 +24,7 @@ import android.support.v14.preference.SwitchPreference;
import com.android.settings.TestConfig; import com.android.settings.TestConfig;
import com.android.settings.testutils.SettingsRobolectricTestRunner; import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settings.testutils.shadow.SettingsShadowResources;
import com.android.settings.widget.SeekBarPreference; import com.android.settings.widget.SeekBarPreference;
import com.android.settingslib.core.lifecycle.Lifecycle; import com.android.settingslib.core.lifecycle.Lifecycle;
@@ -35,9 +36,11 @@ import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config; import org.robolectric.annotation.Config;
@RunWith(SettingsRobolectricTestRunner.class) @RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION, shadows =
SettingsShadowResources.class)
public class AutoBatterySeekBarPreferenceControllerTest { public class AutoBatterySeekBarPreferenceControllerTest {
private static final int TRIGGER_LEVEL = 15; private static final int TRIGGER_LEVEL = 20;
private static final int DEFAULT_LEVEL = 15;
private AutoBatterySeekBarPreferenceController mController; private AutoBatterySeekBarPreferenceController mController;
private Context mContext; private Context mContext;
@@ -51,6 +54,8 @@ public class AutoBatterySeekBarPreferenceControllerTest {
mLifecycleOwner = () -> mLifecycle; mLifecycleOwner = () -> mLifecycle;
mLifecycle = new Lifecycle(mLifecycleOwner); mLifecycle = new Lifecycle(mLifecycleOwner);
SettingsShadowResources.overrideResource(
com.android.internal.R.integer.config_lowBatteryWarningLevel, DEFAULT_LEVEL);
mContext = RuntimeEnvironment.application; mContext = RuntimeEnvironment.application;
mPreference = new SeekBarPreference(mContext); mPreference = new SeekBarPreference(mContext);
mPreference.setMax(100); mPreference.setMax(100);
@@ -67,6 +72,14 @@ public class AutoBatterySeekBarPreferenceControllerTest {
assertThat(mPreference.isVisible()).isFalse(); assertThat(mPreference.isVisible()).isFalse();
} }
@Test
public void testPreference_defaultValue_preferenceVisible() {
mController.updateState(mPreference);
assertThat(mPreference.isVisible()).isTrue();
assertThat(mPreference.getProgress()).isEqualTo(DEFAULT_LEVEL);
}
@Test @Test
public void testPreference_lowPowerLevelNotZero_updatePreference() { public void testPreference_lowPowerLevelNotZero_updatePreference() {
Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.putInt(mContext.getContentResolver(),
@@ -74,7 +87,7 @@ public class AutoBatterySeekBarPreferenceControllerTest {
mController.updateState(mPreference); mController.updateState(mPreference);
assertThat(mPreference.isVisible()).isTrue(); assertThat(mPreference.isVisible()).isTrue();
assertThat(mPreference.getTitle()).isEqualTo("Turn on automatically at 15%"); assertThat(mPreference.getTitle()).isEqualTo("Turn on automatically at 20%");
assertThat(mPreference.getProgress()).isEqualTo(TRIGGER_LEVEL); assertThat(mPreference.getProgress()).isEqualTo(TRIGGER_LEVEL);
} }