1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.settings.fuelgauge; 17 18 import static com.google.common.truth.Truth.assertThat; 19 20 import static org.mockito.Mockito.spy; 21 import static org.mockito.Mockito.verify; 22 import static org.mockito.Mockito.when; 23 24 import android.app.settings.SettingsEnums; 25 import android.content.Context; 26 import android.os.PowerManager; 27 import android.provider.Settings; 28 import android.util.Pair; 29 30 import androidx.preference.Preference; 31 32 import com.android.settings.testutils.FakeFeatureFactory; 33 import com.android.settingslib.core.instrumentation.MetricsFeatureProvider; 34 35 import org.junit.Before; 36 import org.junit.Test; 37 import org.junit.runner.RunWith; 38 import org.mockito.Mock; 39 import org.mockito.MockitoAnnotations; 40 import org.robolectric.RobolectricTestRunner; 41 import org.robolectric.RuntimeEnvironment; 42 import org.robolectric.util.ReflectionHelpers; 43 44 @RunWith(RobolectricTestRunner.class) 45 public class BatterySaverControllerTest { 46 47 @Mock 48 private Preference mBatterySaverPref; 49 @Mock 50 private PowerManager mPowerManager; 51 52 private BatterySaverController mBatterySaverController; 53 private Context mContext; 54 private FakeFeatureFactory mFeatureFactory; 55 private MetricsFeatureProvider mMetricsFeatureProvider; 56 57 @Before setUp()58 public void setUp() { 59 MockitoAnnotations.initMocks(this); 60 61 mContext = RuntimeEnvironment.application; 62 mFeatureFactory = FakeFeatureFactory.setupForTest(); 63 mMetricsFeatureProvider = mFeatureFactory.metricsFeatureProvider; 64 mBatterySaverController = spy(new BatterySaverController(mContext)); 65 ReflectionHelpers.setField(mBatterySaverController, "mPowerManager", mPowerManager); 66 ReflectionHelpers.setField(mBatterySaverController, "mBatterySaverPref", mBatterySaverPref); 67 68 Settings.Global.putInt(mContext.getContentResolver(), 69 Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, 0); 70 } 71 72 @Test onPreferenceChange_onStart()73 public void onPreferenceChange_onStart() { 74 mBatterySaverController.onStart(); 75 verify(mBatterySaverPref).setSummary("Off"); 76 } 77 78 @Test onPreferenceChange_onPowerSaveModeChanged()79 public void onPreferenceChange_onPowerSaveModeChanged() { 80 mBatterySaverController.onPowerSaveModeChanged(); 81 verify(mBatterySaverPref).setSummary("Off"); 82 } 83 84 @Test onPreferenceChange_onPowerSaveModeChanged_manualTrigger_logsType()85 public void onPreferenceChange_onPowerSaveModeChanged_manualTrigger_logsType() { 86 when(mPowerManager.isPowerSaveMode()).thenReturn(true); 87 Settings.Global.putInt(mContext.getContentResolver(), 88 Settings.Global.AUTOMATIC_POWER_SAVE_MODE, -1); 89 90 mBatterySaverController.onPowerSaveModeChanged(); 91 verify(mMetricsFeatureProvider).action(mContext, SettingsEnums.FUELGAUGE_BATTERY_SAVER, 92 Pair.create(SettingsEnums.FIELD_BATTERY_SAVER_SCHEDULE_TYPE, 93 SettingsEnums.BATTERY_SAVER_SCHEDULE_TYPE_NO_SCHEDULE)); 94 } 95 96 @Test onPreferenceChange_onPowerSaveModeChanged_triggerPercent_logsTypeAndPercentage()97 public void onPreferenceChange_onPowerSaveModeChanged_triggerPercent_logsTypeAndPercentage() { 98 when(mPowerManager.isPowerSaveMode()).thenReturn(true); 99 Settings.Global.putInt(mContext.getContentResolver(), 100 Settings.Global.AUTOMATIC_POWER_SAVE_MODE, 101 PowerManager.POWER_SAVE_MODE_TRIGGER_PERCENTAGE); 102 final int percentageVal = 15; 103 Settings.Global.putInt(mContext.getContentResolver(), 104 Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, percentageVal); 105 106 mBatterySaverController.onPowerSaveModeChanged(); 107 verify(mMetricsFeatureProvider).action(mContext, SettingsEnums.FUELGAUGE_BATTERY_SAVER, 108 Pair.create(SettingsEnums.FIELD_BATTERY_SAVER_SCHEDULE_TYPE, 109 SettingsEnums.BATTERY_SAVER_SCHEDULE_TYPE_BASED_ON_PERCENTAGE), 110 Pair.create(SettingsEnums.FIELD_BATTERY_SAVER_PERCENTAGE_VALUE, 111 percentageVal)); 112 } 113 114 @Test onPreferenceChange_onPowerSaveModeChanged_triggerDynamic_logsType()115 public void onPreferenceChange_onPowerSaveModeChanged_triggerDynamic_logsType() { 116 when(mPowerManager.isPowerSaveMode()).thenReturn(true); 117 Settings.Global.putInt(mContext.getContentResolver(), 118 Settings.Global.AUTOMATIC_POWER_SAVE_MODE, 119 PowerManager.POWER_SAVE_MODE_TRIGGER_DYNAMIC); 120 121 mBatterySaverController.onPowerSaveModeChanged(); 122 verify(mMetricsFeatureProvider).action(mContext, SettingsEnums.FUELGAUGE_BATTERY_SAVER, 123 Pair.create(SettingsEnums.FIELD_BATTERY_SAVER_SCHEDULE_TYPE, 124 SettingsEnums.BATTERY_SAVER_SCHEDULE_TYPE_BASED_ON_ROUTINE)); 125 } 126 127 @Test getSummary_batterySaverOn_showSummaryOn()128 public void getSummary_batterySaverOn_showSummaryOn() { 129 when(mPowerManager.isPowerSaveMode()).thenReturn(true); 130 131 assertThat(mBatterySaverController.getSummary()).isEqualTo("On"); 132 } 133 134 @Test getSummary_batterySaverOffButScheduled_showSummaryScheduled()135 public void getSummary_batterySaverOffButScheduled_showSummaryScheduled() { 136 when(mPowerManager.isPowerSaveMode()).thenReturn(false); 137 Settings.Global.putInt(mContext.getContentResolver(), 138 Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, 15); 139 140 assertThat(mBatterySaverController.getSummary()).isEqualTo("Will turn on at 15%"); 141 } 142 143 @Test getSummary_batterySaverOffButScheduledZeroPercent_showSummaryOff()144 public void getSummary_batterySaverOffButScheduledZeroPercent_showSummaryOff() { 145 when(mPowerManager.isPowerSaveMode()).thenReturn(false); 146 Settings.Global.putInt(mContext.getContentResolver(), 147 Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, 0); 148 149 assertThat(mBatterySaverController.getSummary()).isEqualTo("Off"); 150 } 151 152 @Test getSummary_batterySaverOffButScheduledBasedOnRoutine_showSummaryBasedOnRoutine()153 public void getSummary_batterySaverOffButScheduledBasedOnRoutine_showSummaryBasedOnRoutine() { 154 when(mPowerManager.isPowerSaveMode()).thenReturn(false); 155 Settings.Global.putInt( 156 mContext.getContentResolver(), 157 Settings.Global.AUTOMATIC_POWER_SAVE_MODE, 158 PowerManager.POWER_SAVE_MODE_TRIGGER_DYNAMIC); 159 160 assertThat(mBatterySaverController.getSummary()). 161 isEqualTo("Will turn on based on your routine"); 162 } 163 164 @Test getSummary_batterySaverOff_showSummaryOff()165 public void getSummary_batterySaverOff_showSummaryOff() { 166 when(mPowerManager.isPowerSaveMode()).thenReturn(false); 167 168 assertThat(mBatterySaverController.getSummary()).isEqualTo("Off"); 169 } 170 } 171