From 5bb0a63ba9a32ad9a83e8bbab4915123fafbf0be Mon Sep 17 00:00:00 2001 From: Salvador Martinez Date: Fri, 7 Dec 2018 13:12:28 -0800 Subject: [PATCH] Add Battery Saver Schedule screen logic This CL creates the controllers for the screen and makes it so that the appropriate APIs are called when the user makes selections. Test: battery saver comes on when state changed via adb, robotests Bug: 111450127 Change-Id: I1dbcadd4b91276b700947df5a9444aabfec63aa3 --- res/xml/battery_saver_settings.xml | 12 -- .../fuelgauge/BatterySaverController.java | 21 ++- .../AutoBatterySaverPreferenceController.java | 70 -------- ...utoBatterySeekBarPreferenceController.java | 166 ------------------ ...terySaverSchedulePreferenceController.java | 7 + ...rySaverScheduleRadioButtonsController.java | 91 ++++++++++ ...BatterySaverScheduleSeekBarController.java | 100 +++++++++++ .../BatterySaverScheduleSettings.java | 69 +++++--- .../batterysaver/BatterySaverSettings.java | 19 -- ...lid_base_preference_controller_constructor | 1 - ...referenceControllerSignatureInspector.java | 4 +- ...oBatterySaverPreferenceControllerTest.java | 89 ---------- ...atterySeekBarPreferenceControllerTest.java | 117 ------------ ...verScheduleRadioButtonsControllerTest.java | 57 ++++++ ...erySaverScheduleSeekBarControllerTest.java | 65 +++++++ 15 files changed, 386 insertions(+), 502 deletions(-) delete mode 100644 src/com/android/settings/fuelgauge/batterysaver/AutoBatterySaverPreferenceController.java delete mode 100644 src/com/android/settings/fuelgauge/batterysaver/AutoBatterySeekBarPreferenceController.java create mode 100644 src/com/android/settings/fuelgauge/batterysaver/BatterySaverScheduleRadioButtonsController.java create mode 100644 src/com/android/settings/fuelgauge/batterysaver/BatterySaverScheduleSeekBarController.java delete mode 100644 tests/robotests/src/com/android/settings/fuelgauge/batterysaver/AutoBatterySaverPreferenceControllerTest.java delete mode 100644 tests/robotests/src/com/android/settings/fuelgauge/batterysaver/AutoBatterySeekBarPreferenceControllerTest.java create mode 100644 tests/robotests/src/com/android/settings/fuelgauge/batterysaver/BatterySaverScheduleRadioButtonsControllerTest.java create mode 100644 tests/robotests/src/com/android/settings/fuelgauge/batterysaver/BatterySaverScheduleSeekBarControllerTest.java diff --git a/res/xml/battery_saver_settings.xml b/res/xml/battery_saver_settings.xml index a0860182f41..89f7eab2ba7 100644 --- a/res/xml/battery_saver_settings.xml +++ b/res/xml/battery_saver_settings.xml @@ -26,18 +26,6 @@ android:title="@string/battery_saver_schedule_settings_title" settings:controller="com.android.settings.fuelgauge.batterysaver.BatterySaverSchedulePreferenceController"/> - - - - - 0) { - if (!(preference instanceof SeekBarPreference)) { - Log.e(TAG, "Unexpected preference class: " + preference.getClass()); - } else { - final SeekBarPreference seekBarPreference = (SeekBarPreference) preference; - if (maxLevel < seekBarPreference.getMin()) { - Log.e(TAG, "LOW_POWER_MODE_TRIGGER_LEVEL_MAX too low; ignored."); - } else { - seekBarPreference.setMax(maxLevel); - } - } - } - - // Set the current value. - final int level = Settings.Global.getInt(contentResolver, - Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, - AutoBatterySaverPreferenceController.DEFAULT_TRIGGER_LEVEL); - if (level == 0) { - preference.setVisible(false); - } else { - preference.setVisible(true); - preference.setTitle(mContext.getString(R.string.battery_saver_seekbar_title, - Utils.formatPercentage(level))); - SeekBarPreference seekBarPreference = (SeekBarPreference) preference; - seekBarPreference.setProgress(level / INTERVAL); - seekBarPreference.setSeekBarContentDescription( - mContext.getString(R.string.battery_saver_turn_on_automatically_title)); - } - } - - /** - * Observer that listens to change from {@link Settings.Global#LOW_POWER_MODE_TRIGGER_LEVEL} - */ - private final class AutoBatterySaverSettingObserver extends ContentObserver { - private final Uri mUri = Settings.Global.getUriFor( - Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL); - private final ContentResolver mContentResolver; - - public AutoBatterySaverSettingObserver(Handler handler) { - super(handler); - mContentResolver = mContext.getContentResolver(); - } - - public void registerContentObserver() { - mContentResolver.registerContentObserver(mUri, false, this); - } - - public void unRegisterContentObserver() { - mContentResolver.unregisterContentObserver(this); - } - - @Override - public void onChange(boolean selfChange, Uri uri, int userId) { - if (mUri.equals(uri)) { - updatePreference(mPreference); - } - } - } -} diff --git a/src/com/android/settings/fuelgauge/batterysaver/BatterySaverSchedulePreferenceController.java b/src/com/android/settings/fuelgauge/batterysaver/BatterySaverSchedulePreferenceController.java index cc6aa00487d..868c73f8f12 100644 --- a/src/com/android/settings/fuelgauge/batterysaver/BatterySaverSchedulePreferenceController.java +++ b/src/com/android/settings/fuelgauge/batterysaver/BatterySaverSchedulePreferenceController.java @@ -26,7 +26,13 @@ import androidx.preference.PreferenceScreen; import com.android.settings.R; import com.android.settings.Utils; import com.android.settings.core.BasePreferenceController; +import com.android.settingslib.fuelgauge.BatterySaverUtils; +/** + * Simple controller to navigate users to the scheduling page from + * "Settings > Battery > Battery Saver". Also updates the summary for preference based on + * the currently selected settings. + */ public class BatterySaverSchedulePreferenceController extends BasePreferenceController { @VisibleForTesting @@ -36,6 +42,7 @@ public class BatterySaverSchedulePreferenceController extends BasePreferenceCont public BatterySaverSchedulePreferenceController(Context context) { super(context, KEY_BATTERY_SAVER_SCHEDULE); + BatterySaverUtils.revertScheduleToNoneIfNeeded(context); } @Override diff --git a/src/com/android/settings/fuelgauge/batterysaver/BatterySaverScheduleRadioButtonsController.java b/src/com/android/settings/fuelgauge/batterysaver/BatterySaverScheduleRadioButtonsController.java new file mode 100644 index 00000000000..d49eb0a86f9 --- /dev/null +++ b/src/com/android/settings/fuelgauge/batterysaver/BatterySaverScheduleRadioButtonsController.java @@ -0,0 +1,91 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.android.settings.fuelgauge.batterysaver; + +import android.content.ContentResolver; +import android.content.Context; +import android.os.PowerManager; +import android.provider.Settings; +import android.provider.Settings.Global; + +/** + * Responds to user actions in the Settings > Battery > Set a Schedule Screen + * + * Note that this is not a preference controller since that screen does not inherit from + * DashboardFragment. + * + * Will call the appropriate power manager APIs and modify the correct settings to enable + * users to control their automatic battery saver toggling preferences. + * See {@link Settings.Global#AUTOMATIC_POWER_SAVER_MODE} for more details. + */ +public class BatterySaverScheduleRadioButtonsController { + + public static final String KEY_NO_SCHEDULE = "key_battery_saver_no_schedule"; + public static final String KEY_ROUTINE = "key_battery_saver_routine"; + public static final String KEY_PERCENTAGE = "key_battery_saver_percentage"; + + private Context mContext; + private BatterySaverScheduleSeekBarController mSeekBarController; + + public BatterySaverScheduleRadioButtonsController(Context context, + BatterySaverScheduleSeekBarController seekbar) { + mContext = context; + mSeekBarController = seekbar; + } + + public String getDefaultKey() { + final ContentResolver resolver = mContext.getContentResolver(); + // Note: this can also be obtained via PowerManager.getPowerSaveMode() + final int mode = Settings.Global.getInt(resolver, Global.AUTOMATIC_POWER_SAVER_MODE, + PowerManager.POWER_SAVER_MODE_PERCENTAGE); + // if mode is "dynamic" we are in routine mode, percentage with non-zero threshold is + // percentage mode, otherwise it is no schedule mode + if (mode == PowerManager.POWER_SAVER_MODE_PERCENTAGE) { + final int threshold = + Settings.Global.getInt(resolver, Global.LOW_POWER_MODE_TRIGGER_LEVEL, 0); + if (threshold <= 0) { + return KEY_NO_SCHEDULE; + } + return KEY_PERCENTAGE; + } + return KEY_ROUTINE; + } + + public boolean setDefaultKey(String key) { + final ContentResolver resolver = mContext.getContentResolver(); + switch(key) { + case KEY_NO_SCHEDULE: + Settings.Global.putInt(resolver, Global.AUTOMATIC_POWER_SAVER_MODE, + PowerManager.POWER_SAVER_MODE_PERCENTAGE); + Settings.Global.putInt(resolver, Global.LOW_POWER_MODE_TRIGGER_LEVEL, 0); + break; + case KEY_PERCENTAGE: + Settings.Global.putInt(resolver, Global.AUTOMATIC_POWER_SAVER_MODE, + PowerManager.POWER_SAVER_MODE_PERCENTAGE); + Settings.Global.putInt(resolver, Global.LOW_POWER_MODE_TRIGGER_LEVEL, 5); + break; + case KEY_ROUTINE: + Settings.Global.putInt(resolver, Global.AUTOMATIC_POWER_SAVER_MODE, + PowerManager.POWER_SAVER_MODE_DYNAMIC); + break; + default: + throw new IllegalStateException( + "Not a valid key for " + this.getClass().getSimpleName()); + } + mSeekBarController.updateSeekBar(); + return true; + } +} diff --git a/src/com/android/settings/fuelgauge/batterysaver/BatterySaverScheduleSeekBarController.java b/src/com/android/settings/fuelgauge/batterysaver/BatterySaverScheduleSeekBarController.java new file mode 100644 index 00000000000..215f40021f9 --- /dev/null +++ b/src/com/android/settings/fuelgauge/batterysaver/BatterySaverScheduleSeekBarController.java @@ -0,0 +1,100 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.android.settings.fuelgauge.batterysaver; + +import android.content.ContentResolver; +import android.content.Context; +import android.os.PowerManager; +import android.provider.Settings; +import android.provider.Settings.Global; +import androidx.preference.Preference; +import androidx.preference.Preference.OnPreferenceChangeListener; +import androidx.preference.PreferenceScreen; +import com.android.internal.annotations.VisibleForTesting; +import com.android.settings.R; +import com.android.settings.Utils; +import com.android.settings.widget.SeekBarPreference; + +/** + * Responds to user actions in the Settings > Battery > Set a Schedule Screen for the seekbar. + * Note that this seekbar is only visible when the radio button selected is "Percentage". + * + * Note that this is not a preference controller since that screen does not inherit from + * DashboardFragment. + * + * Will call the appropriate power manager APIs and modify the correct settings to enable + * users to control their automatic battery saver toggling preferences. + * See {@link Settings.Global#AUTOMATIC_POWER_SAVER_MODE} for more details. + */ +public class BatterySaverScheduleSeekBarController implements + OnPreferenceChangeListener { + + public static final int MAX_SEEKBAR_VALUE = 15; + public static final int MIN_SEEKBAR_VALUE = 1; + public static final String KEY_BATTERY_SAVER_SEEK_BAR = "battery_saver_seek_bar"; + + @VisibleForTesting + public SeekBarPreference mSeekBarPreference; + private Context mContext; + + public BatterySaverScheduleSeekBarController(Context context) { + mContext = context; + mSeekBarPreference = new SeekBarPreference(context); + mSeekBarPreference.setOnPreferenceChangeListener(this); + mSeekBarPreference.setMax(BatterySaverScheduleSeekBarController.MAX_SEEKBAR_VALUE); + mSeekBarPreference.setMin(BatterySaverScheduleSeekBarController.MIN_SEEKBAR_VALUE); + mSeekBarPreference.setKey(KEY_BATTERY_SAVER_SEEK_BAR); + } + + @Override + public boolean onPreferenceChange(Preference preference, Object newValue) { + // The nits are in intervals of 5% + final int percentage = ((Integer) newValue) * 5; + Settings.Global.putInt(mContext.getContentResolver(), Global.LOW_POWER_MODE_TRIGGER_LEVEL, + percentage); + preference.setTitle(mContext.getString( + R.string.battery_saver_seekbar_title, Utils.formatPercentage(percentage))); + return true; + } + + public void updateSeekBar() { + final ContentResolver resolver = mContext.getContentResolver(); + // Note: this can also be obtained via PowerManager.getPowerSaveMode() + final int mode = Settings.Global.getInt(resolver, Global.AUTOMATIC_POWER_SAVER_MODE, + PowerManager.POWER_SAVER_MODE_PERCENTAGE); + // if mode is "dynamic" we are in routine mode, percentage with non-zero threshold is + // percentage mode, otherwise it is no schedule mode + if (mode == PowerManager.POWER_SAVER_MODE_PERCENTAGE) { + final int threshold = + Settings.Global.getInt(resolver, Global.LOW_POWER_MODE_TRIGGER_LEVEL, 0); + if (threshold <= 0) { + mSeekBarPreference.setVisible(false); + } else { + mSeekBarPreference.setVisible(true); + mSeekBarPreference.setProgress(MIN_SEEKBAR_VALUE); + mSeekBarPreference.setTitle(mContext.getString( + R.string.battery_saver_seekbar_title, + Utils.formatPercentage(MIN_SEEKBAR_VALUE * 5))); + } + } else { + mSeekBarPreference.setVisible(false); + } + } + + public void addToScreen(PreferenceScreen screen) { + screen.addPreference(mSeekBarPreference); + } +} diff --git a/src/com/android/settings/fuelgauge/batterysaver/BatterySaverScheduleSettings.java b/src/com/android/settings/fuelgauge/batterysaver/BatterySaverScheduleSettings.java index fad9f31ac6f..6e9c7115fe7 100644 --- a/src/com/android/settings/fuelgauge/batterysaver/BatterySaverScheduleSettings.java +++ b/src/com/android/settings/fuelgauge/batterysaver/BatterySaverScheduleSettings.java @@ -18,48 +18,77 @@ package com.android.settings.fuelgauge.batterysaver; import android.content.Context; import android.graphics.drawable.Drawable; +import android.os.Bundle; +import android.text.TextUtils; import android.view.View; import androidx.preference.PreferenceScreen; import com.android.settings.widget.RadioButtonPickerFragment; import com.android.settings.R; import com.android.settings.widget.RadioButtonPreference; -import com.android.settings.widget.SeekBarPreference; +import com.android.settingslib.fuelgauge.BatterySaverUtils; import com.android.settingslib.widget.CandidateInfo; import com.google.common.collect.Lists; import java.util.List; +/** + * Fragment that allows users to customize their automatic battery saver mode settings. + * + * Location: Settings > Battery > Battery Saver > Set a Schedule + * See {@link BatterySaverSchedulePreferenceController} for the controller that manages navigation + * to this screen from "Settings > Battery > Battery Saver" and the summary. + * See {@link BatterySaverScheduleRadioButtonsController} & + * {@link BatterySaverScheduleSeekBarController} for the controller that manages user + * interactions in this screen. + */ public class BatterySaverScheduleSettings extends RadioButtonPickerFragment { - private static final String KEY_NO_SCHEDULE = "key_battery_saver_no_schedule"; - private static final String KEY_ROUTINE = "key_battery_saver_routine"; - private static final String KEY_PERCENTAGE = "key_battery_saver_percentage"; - public static final int MAX_SEEKBAR_VALUE = 15; - public static final int MIN_SEEKBAR_VALUE = 1; - public static final String KEY_BATTERY_SAVER_SEEK_BAR = "battery_saver_seek_bar"; + public BatterySaverScheduleRadioButtonsController mRadioButtonController; + private BatterySaverScheduleSeekBarController mSeekBarController; @Override protected int getPreferenceScreenResId() { return R.xml.battery_saver_schedule_settings; } + @Override + public void onAttach(Context context) { + super.onAttach(context); + mSeekBarController = new BatterySaverScheduleSeekBarController(context); + mRadioButtonController = new BatterySaverScheduleRadioButtonsController( + context, mSeekBarController); + } + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + } + @Override protected List getCandidates() { Context context = getContext(); List candidates = Lists.newArrayList(); + String routineProviderApp = getContext().getResources() + .getString(com.android.internal.R.string.config_batterySaverScheduleProvider); candidates.add(new BatterySaverScheduleCandidateInfo( context.getText(R.string.battery_saver_auto_no_schedule), /* summary */ null, - KEY_NO_SCHEDULE, - /* enabled */ true)); - candidates.add(new BatterySaverScheduleCandidateInfo( - context.getText(R.string.battery_saver_auto_routine), - context.getText(R.string.battery_saver_auto_routine_summary), - KEY_ROUTINE, + BatterySaverScheduleRadioButtonsController.KEY_NO_SCHEDULE, /* enabled */ true)); + // only add routine option if an app has been specified + if (!TextUtils.isEmpty(routineProviderApp)) { + candidates.add(new BatterySaverScheduleCandidateInfo( + context.getText(R.string.battery_saver_auto_routine), + context.getText(R.string.battery_saver_auto_routine_summary), + BatterySaverScheduleRadioButtonsController.KEY_ROUTINE, + /* enabled */ true)); + } else { + // Make sure routine is not selected if no provider app is configured + BatterySaverUtils.revertScheduleToNoneIfNeeded(context); + } candidates.add(new BatterySaverScheduleCandidateInfo( context.getText(R.string.battery_saver_auto_percentage), /* summary */ null, - KEY_PERCENTAGE, + BatterySaverScheduleRadioButtonsController.KEY_PERCENTAGE, /* enabled */ true)); return candidates; @@ -79,22 +108,18 @@ public class BatterySaverScheduleSettings extends RadioButtonPickerFragment { @Override protected void addStaticPreferences(PreferenceScreen screen) { - SeekBarPreference seekbar = new SeekBarPreference(getContext()); - seekbar.setMax(MAX_SEEKBAR_VALUE); - seekbar.setMin(MIN_SEEKBAR_VALUE); - seekbar.setTitle(R.string.battery_saver_seekbar_title_placeholder); - seekbar.setKey(KEY_BATTERY_SAVER_SEEK_BAR); - screen.addPreference(seekbar); + mSeekBarController.updateSeekBar(); + mSeekBarController.addToScreen(screen); } @Override protected String getDefaultKey() { - return null; + return mRadioButtonController.getDefaultKey(); } @Override protected boolean setDefaultKey(String key) { - return false; + return mRadioButtonController.setDefaultKey(key); } @Override diff --git a/src/com/android/settings/fuelgauge/batterysaver/BatterySaverSettings.java b/src/com/android/settings/fuelgauge/batterysaver/BatterySaverSettings.java index 8b0409cc92a..6aef9b57e72 100644 --- a/src/com/android/settings/fuelgauge/batterysaver/BatterySaverSettings.java +++ b/src/com/android/settings/fuelgauge/batterysaver/BatterySaverSettings.java @@ -60,24 +60,11 @@ public class BatterySaverSettings extends DashboardFragment { return TAG; } - @Override - protected List createPreferenceControllers(Context context) { - return buildPreferenceControllers(context, getSettingsLifecycle()); - } - @Override public int getHelpResource() { return R.string.help_url_battery_saver_settings; } - private static List buildPreferenceControllers( - Context context, Lifecycle lifecycle) { - final List controllers = new ArrayList<>(); - controllers.add(new AutoBatterySaverPreferenceController(context)); - controllers.add(new AutoBatterySeekBarPreferenceController(context, lifecycle)); - return controllers; - } - /** * For Search. */ @@ -90,11 +77,5 @@ public class BatterySaverSettings extends DashboardFragment { sir.xmlResId = R.xml.battery_saver_settings; return Arrays.asList(sir); } - - @Override - public List createPreferenceControllers( - Context context) { - return buildPreferenceControllers(context, null); - } }; } diff --git a/tests/robotests/assets/grandfather_invalid_base_preference_controller_constructor b/tests/robotests/assets/grandfather_invalid_base_preference_controller_constructor index befd8804103..ce03d955549 100644 --- a/tests/robotests/assets/grandfather_invalid_base_preference_controller_constructor +++ b/tests/robotests/assets/grandfather_invalid_base_preference_controller_constructor @@ -7,7 +7,6 @@ com.android.settings.bluetooth.BluetoothDeviceNamePreferenceController com.android.settings.bluetooth.BluetoothDeviceRenamePreferenceController com.android.settings.datausage.DataUsageSummaryPreferenceController com.android.settings.fuelgauge.RestrictAppPreferenceController -com.android.settings.fuelgauge.batterysaver.AutoBatterySeekBarPreferenceController com.android.settings.fuelgauge.batterysaver.BatterySaverButtonPreferenceController com.android.settings.fuelgauge.batterytip.BatteryTipPreferenceController com.android.settings.security.VisiblePatternProfilePreferenceController diff --git a/tests/robotests/src/com/android/settings/core/BasePreferenceControllerSignatureInspector.java b/tests/robotests/src/com/android/settings/core/BasePreferenceControllerSignatureInspector.java index ce438b06bb0..28e394f94cb 100644 --- a/tests/robotests/src/com/android/settings/core/BasePreferenceControllerSignatureInspector.java +++ b/tests/robotests/src/com/android/settings/core/BasePreferenceControllerSignatureInspector.java @@ -71,7 +71,9 @@ public class BasePreferenceControllerSignatureInspector extends CodeInspector { .that(badClasses.toString()) .isEmpty(); - assertWithMessage("Something in the grandfather list is no longer relevant. Please remove") + assertWithMessage("Something in the grandfather list is no longer relevant. Please remove" + + "it from packages/apps/Settings/tests/robotests/assets/" + + "grandfather_invalid_base_preference_controller_constructor") .that(grandfather) .isEmpty(); } diff --git a/tests/robotests/src/com/android/settings/fuelgauge/batterysaver/AutoBatterySaverPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/fuelgauge/batterysaver/AutoBatterySaverPreferenceControllerTest.java deleted file mode 100644 index df1d9094e2e..00000000000 --- a/tests/robotests/src/com/android/settings/fuelgauge/batterysaver/AutoBatterySaverPreferenceControllerTest.java +++ /dev/null @@ -1,89 +0,0 @@ -/* - * Copyright (C) 2017 The Android Open Source Project - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * - */ - -package com.android.settings.fuelgauge.batterysaver; - -import static com.google.common.truth.Truth.assertThat; - -import android.content.Context; -import android.provider.Settings; - -import androidx.preference.SwitchPreference; - - -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.MockitoAnnotations; -import org.robolectric.RobolectricTestRunner; -import org.robolectric.RuntimeEnvironment; - -@RunWith(RobolectricTestRunner.class) -public class AutoBatterySaverPreferenceControllerTest { - - private AutoBatterySaverPreferenceController mController; - private Context mContext; - private SwitchPreference mPreference; - - @Before - public void setUp() { - MockitoAnnotations.initMocks(this); - - mContext = RuntimeEnvironment.application; - mPreference = new SwitchPreference(mContext); - mController = new AutoBatterySaverPreferenceController(mContext); - } - - @Test - public void testUpdateState_lowPowerLevelZero_preferenceNotChecked() { - Settings.Global.putInt(mContext.getContentResolver(), - Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, 0); - mController.updateState(mPreference); - - assertThat(mPreference.isChecked()).isFalse(); - } - - @Test - public void testUpdateState_lowPowerLevelZero_preferenceChecked() { - Settings.Global.putInt(mContext.getContentResolver(), - Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, 15); - mController.updateState(mPreference); - - assertThat(mPreference.isChecked()).isTrue(); - } - - @Test - public void testOnPreferenceChange_turnOn_setValueNotZero() { - mController.onPreferenceChange(mPreference, true); - - assertThat(Settings.Global.getInt(mContext.getContentResolver(), - Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, 0)).isNotEqualTo(0); - } - - @Test - public void testOnPreferenceChange_turnOff_setValueZero() { - mController.onPreferenceChange(mPreference, false); - - assertThat(Settings.Global.getInt(mContext.getContentResolver(), - Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, 0)).isEqualTo(0); - } - - @Test - public void testIsChecked_useDefaultValue_returnFalse() { - assertThat(mController.isChecked()).isFalse(); - } -} diff --git a/tests/robotests/src/com/android/settings/fuelgauge/batterysaver/AutoBatterySeekBarPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/fuelgauge/batterysaver/AutoBatterySeekBarPreferenceControllerTest.java deleted file mode 100644 index c13b9139994..00000000000 --- a/tests/robotests/src/com/android/settings/fuelgauge/batterysaver/AutoBatterySeekBarPreferenceControllerTest.java +++ /dev/null @@ -1,117 +0,0 @@ -/* - * Copyright (C) 2018 The Android Open Source Project - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.settings.fuelgauge.batterysaver; - -import static com.google.common.truth.Truth.assertThat; - -import android.content.Context; -import android.provider.Settings; - -import androidx.lifecycle.LifecycleOwner; - -import com.android.settings.widget.SeekBarPreference; -import com.android.settingslib.core.lifecycle.Lifecycle; - -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.MockitoAnnotations; -import org.robolectric.RobolectricTestRunner; -import org.robolectric.RuntimeEnvironment; - -@RunWith(RobolectricTestRunner.class) -public class AutoBatterySeekBarPreferenceControllerTest { - - private static final int TRIGGER_LEVEL = 20; - private static final int INTERVAL = 5; - - private AutoBatterySeekBarPreferenceController mController; - private Context mContext; - private SeekBarPreference mPreference; - private Lifecycle mLifecycle; - private LifecycleOwner mLifecycleOwner; - - @Before - public void setUp() { - MockitoAnnotations.initMocks(this); - mLifecycleOwner = () -> mLifecycle; - mLifecycle = new Lifecycle(mLifecycleOwner); - - mContext = RuntimeEnvironment.application; - mPreference = new SeekBarPreference(mContext); - mPreference.setMax(100); - mController = new AutoBatterySeekBarPreferenceController(mContext, mLifecycle); - } - - @Test - public void testPreference_lowPowerLevelZero_preferenceInvisible() { - Settings.Global.putInt(mContext.getContentResolver(), - Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, 0); - - mController.updateState(mPreference); - - assertThat(mPreference.isVisible()).isFalse(); - } - - @Test - public void testPreference_defaultValue_preferenceNotVisible() { - mController.updateState(mPreference); - - assertThat(mPreference.isVisible()).isFalse(); - } - - @Test - public void testPreference_lowPowerLevelNotZero_updatePreference() { - Settings.Global.putInt(mContext.getContentResolver(), - Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, TRIGGER_LEVEL); - mController.updateState(mPreference); - - assertThat(mPreference.isVisible()).isTrue(); - assertThat(mPreference.getTitle()).isEqualTo("20%"); - assertThat(mPreference.getProgress()).isEqualTo(TRIGGER_LEVEL / INTERVAL); - } - - @Test - public void testOnPreferenceChange_updateValue() { - Settings.Global.putInt(mContext.getContentResolver(), - Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, 0); - - mController.onPreferenceChange(mPreference, TRIGGER_LEVEL / INTERVAL); - - assertThat(Settings.Global.getInt(mContext.getContentResolver(), - Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, 0)).isEqualTo(TRIGGER_LEVEL); - } - - @Test - public void testOnPreferenceChange_changeMax() { - Settings.Global.putInt(mContext.getContentResolver(), - Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL_MAX, 50); - - mController.updateState(mPreference); - - assertThat(mPreference.getMax()).isEqualTo(50 / INTERVAL); - } - - @Test - public void testOnPreferenceChange_noChangeMax() { - Settings.Global.putInt(mContext.getContentResolver(), - Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL_MAX, 0); - - mController.updateState(mPreference); - - assertThat(mPreference.getMax()).isEqualTo(100); - } -} diff --git a/tests/robotests/src/com/android/settings/fuelgauge/batterysaver/BatterySaverScheduleRadioButtonsControllerTest.java b/tests/robotests/src/com/android/settings/fuelgauge/batterysaver/BatterySaverScheduleRadioButtonsControllerTest.java new file mode 100644 index 00000000000..6d412ee95ed --- /dev/null +++ b/tests/robotests/src/com/android/settings/fuelgauge/batterysaver/BatterySaverScheduleRadioButtonsControllerTest.java @@ -0,0 +1,57 @@ +package com.android.settings.fuelgauge.batterysaver; + +import static com.google.common.truth.Truth.assertThat; + +import android.content.ContentResolver; +import android.content.Context; +import android.os.PowerManager; +import android.provider.Settings; +import android.provider.Settings.Global; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.RuntimeEnvironment; + +@RunWith(RobolectricTestRunner.class) +public class BatterySaverScheduleRadioButtonsControllerTest { + private Context mContext; + private ContentResolver mResolver; + private BatterySaverScheduleRadioButtonsController mController; + private BatterySaverScheduleSeekBarController mSeekBarController; + + @Before + public void setUp() { + mContext = RuntimeEnvironment.application; + mSeekBarController = new BatterySaverScheduleSeekBarController(mContext); + mController = new BatterySaverScheduleRadioButtonsController( + mContext, mSeekBarController); + mResolver = mContext.getContentResolver(); + } + + @Test + public void getDefaultKey_routine_returnsCorrectValue() { + Settings.Global.putInt(mResolver, Global.AUTOMATIC_POWER_SAVER_MODE, + PowerManager.POWER_SAVER_MODE_DYNAMIC); + assertThat(mController.getDefaultKey()) + .isEqualTo(BatterySaverScheduleRadioButtonsController.KEY_ROUTINE); + } + + @Test + public void getDefaultKey_automatic_returnsCorrectValue() { + Settings.Global.putInt(mResolver, Global.AUTOMATIC_POWER_SAVER_MODE, + PowerManager.POWER_SAVER_MODE_PERCENTAGE); + Settings.Global.putInt(mResolver, Global.LOW_POWER_MODE_TRIGGER_LEVEL, 5); + assertThat(mController.getDefaultKey()) + .isEqualTo(BatterySaverScheduleRadioButtonsController.KEY_PERCENTAGE); + } + + @Test + public void getDefaultKey_none_returnsCorrectValue() { + Settings.Global.putInt(mResolver, Global.AUTOMATIC_POWER_SAVER_MODE, + PowerManager.POWER_SAVER_MODE_PERCENTAGE); + Settings.Global.putInt(mResolver, Global.LOW_POWER_MODE_TRIGGER_LEVEL, 0); + assertThat(mController.getDefaultKey()) + .isEqualTo(BatterySaverScheduleRadioButtonsController.KEY_NO_SCHEDULE); + } +} diff --git a/tests/robotests/src/com/android/settings/fuelgauge/batterysaver/BatterySaverScheduleSeekBarControllerTest.java b/tests/robotests/src/com/android/settings/fuelgauge/batterysaver/BatterySaverScheduleSeekBarControllerTest.java new file mode 100644 index 00000000000..8aac9d816ef --- /dev/null +++ b/tests/robotests/src/com/android/settings/fuelgauge/batterysaver/BatterySaverScheduleSeekBarControllerTest.java @@ -0,0 +1,65 @@ +package com.android.settings.fuelgauge.batterysaver; + +import static com.google.common.truth.Truth.assertThat; + +import android.content.ContentResolver; +import android.content.Context; +import android.os.PowerManager; +import android.provider.Settings; +import android.provider.Settings.Global; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.RuntimeEnvironment; + +@RunWith(RobolectricTestRunner.class) +public class BatterySaverScheduleSeekBarControllerTest { + + private Context mContext; + private ContentResolver mResolver; + private BatterySaverScheduleSeekBarController mController; + + @Before + public void setUp() { + mContext = RuntimeEnvironment.application; + mController = new BatterySaverScheduleSeekBarController(mContext); + mResolver = mContext.getContentResolver(); + } + + @Test + public void onPreferenceChange_updatesSettingsGlobal() { + Settings.Global.putInt(mResolver, Global.LOW_POWER_MODE_TRIGGER_LEVEL, 5); + mController.onPreferenceChange(mController.mSeekBarPreference, 10); + assertThat(Settings.Global.getInt(mResolver, Global.LOW_POWER_MODE_TRIGGER_LEVEL, -1)) + .isEqualTo(50); + + assertThat(mController.mSeekBarPreference.getTitle()).isEqualTo("50%"); + } + + @Test + public void updateSeekBar_routineMode_hasCorrectProperties() { + Settings.Global.putInt(mResolver, Global.AUTOMATIC_POWER_SAVER_MODE, + PowerManager.POWER_SAVER_MODE_DYNAMIC); + mController.updateSeekBar(); + assertThat(mController.mSeekBarPreference.isVisible()).isFalse(); + } + + @Test + public void updateSeekBar_percentageMode_hasCorrectProperties() { + Settings.Global.putInt(mResolver, Global.AUTOMATIC_POWER_SAVER_MODE, + PowerManager.POWER_SAVER_MODE_PERCENTAGE); + Settings.Global.putInt(mResolver, Global.LOW_POWER_MODE_TRIGGER_LEVEL, 5); + mController.updateSeekBar(); + assertThat(mController.mSeekBarPreference.isVisible()).isTrue(); + } + + @Test + public void updateSeekBar_noneMode_hasCorrectProperties() { + Settings.Global.putInt(mResolver, Global.AUTOMATIC_POWER_SAVER_MODE, + PowerManager.POWER_SAVER_MODE_PERCENTAGE); + Settings.Global.putInt(mResolver, Global.LOW_POWER_MODE_TRIGGER_LEVEL, 0); + mController.updateSeekBar(); + assertThat(mController.mSeekBarPreference.isVisible()).isFalse(); + } +}