Jason Monk | d819c31 | 2017-08-11 12:53:36 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file |
| 5 | * except in compliance with the License. You may obtain a copy of the License at |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software distributed under the |
| 10 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 11 | * KIND, either express or implied. See the License for the specific language governing |
| 12 | * permissions and limitations under the License. |
| 13 | */ |
| 14 | |
| 15 | package com.android.systemui.power; |
| 16 | |
| 17 | import static android.os.HardwarePropertiesManager.DEVICE_TEMPERATURE_SKIN; |
| 18 | import static android.os.HardwarePropertiesManager.TEMPERATURE_CURRENT; |
| 19 | import static android.os.HardwarePropertiesManager.TEMPERATURE_SHUTDOWN; |
| 20 | import static android.provider.Settings.Global.SHOW_TEMPERATURE_WARNING; |
| 21 | |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 22 | import static junit.framework.Assert.assertFalse; |
| 23 | import static junit.framework.Assert.assertTrue; |
Amin Shaikh | 2f6c45c | 2018-04-16 14:00:09 -0400 | [diff] [blame] | 24 | |
Jason Monk | d819c31 | 2017-08-11 12:53:36 -0400 | [diff] [blame] | 25 | import static org.mockito.Mockito.mock; |
| 26 | import static org.mockito.Mockito.never; |
Salvador Martinez | 926f071 | 2018-07-03 18:07:07 -0700 | [diff] [blame] | 27 | import static org.mockito.Mockito.times; |
Jason Monk | d819c31 | 2017-08-11 12:53:36 -0400 | [diff] [blame] | 28 | import static org.mockito.Mockito.verify; |
| 29 | import static org.mockito.Mockito.when; |
| 30 | |
| 31 | import android.content.Context; |
Amin Shaikh | 2f6c45c | 2018-04-16 14:00:09 -0400 | [diff] [blame] | 32 | import android.content.Intent; |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 33 | import android.os.BatteryManager; |
Jason Monk | d819c31 | 2017-08-11 12:53:36 -0400 | [diff] [blame] | 34 | import android.os.HardwarePropertiesManager; |
Amin Shaikh | 2f6c45c | 2018-04-16 14:00:09 -0400 | [diff] [blame] | 35 | import android.os.PowerManager; |
Jason Monk | d819c31 | 2017-08-11 12:53:36 -0400 | [diff] [blame] | 36 | import android.provider.Settings; |
| 37 | import android.testing.AndroidTestingRunner; |
| 38 | import android.testing.TestableLooper.RunWithLooper; |
| 39 | import android.testing.TestableResources; |
| 40 | import android.test.suitebuilder.annotation.SmallTest; |
| 41 | |
Amin Shaikh | 2f6c45c | 2018-04-16 14:00:09 -0400 | [diff] [blame] | 42 | import com.android.settingslib.utils.ThreadUtils; |
Jason Monk | d819c31 | 2017-08-11 12:53:36 -0400 | [diff] [blame] | 43 | import com.android.systemui.R; |
| 44 | import com.android.systemui.SysuiTestCase; |
| 45 | import com.android.systemui.power.PowerUI.WarningsUI; |
| 46 | import com.android.systemui.statusbar.phone.StatusBar; |
| 47 | |
Salvador Martinez | bb902fc | 2018-01-22 19:46:55 -0800 | [diff] [blame] | 48 | import java.time.Duration; |
Amin Shaikh | 2f6c45c | 2018-04-16 14:00:09 -0400 | [diff] [blame] | 49 | import java.util.concurrent.CountDownLatch; |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 50 | import java.util.concurrent.TimeUnit; |
Jason Monk | d819c31 | 2017-08-11 12:53:36 -0400 | [diff] [blame] | 51 | import org.junit.Before; |
| 52 | import org.junit.Test; |
| 53 | import org.junit.runner.RunWith; |
Amin Shaikh | 2f6c45c | 2018-04-16 14:00:09 -0400 | [diff] [blame] | 54 | import org.mockito.Mock; |
| 55 | import org.mockito.MockitoAnnotations; |
Jason Monk | d819c31 | 2017-08-11 12:53:36 -0400 | [diff] [blame] | 56 | |
| 57 | @RunWith(AndroidTestingRunner.class) |
| 58 | @RunWithLooper |
| 59 | @SmallTest |
| 60 | public class PowerUITest extends SysuiTestCase { |
| 61 | |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 62 | private static final boolean UNPLUGGED = false; |
| 63 | private static final boolean POWER_SAVER_OFF = false; |
| 64 | private static final int ABOVE_WARNING_BUCKET = 1; |
Salvador Martinez | bb902fc | 2018-01-22 19:46:55 -0800 | [diff] [blame] | 65 | private static final long ONE_HOUR_MILLIS = Duration.ofHours(1).toMillis(); |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 66 | public static final int BELOW_WARNING_BUCKET = -1; |
| 67 | public static final long BELOW_HYBRID_THRESHOLD = TimeUnit.HOURS.toMillis(2); |
| 68 | public static final long ABOVE_HYBRID_THRESHOLD = TimeUnit.HOURS.toMillis(4); |
Salvador Martinez | fd38aa5 | 2018-03-28 23:56:59 -0700 | [diff] [blame] | 69 | private static final long ABOVE_CHARGE_CYCLE_THRESHOLD = Duration.ofHours(8).toMillis(); |
Salvador Martinez | 926f071 | 2018-07-03 18:07:07 -0700 | [diff] [blame] | 70 | private static final int OLD_BATTERY_LEVEL_NINE = 9; |
| 71 | private static final int OLD_BATTERY_LEVEL_10 = 10; |
Jason Monk | d819c31 | 2017-08-11 12:53:36 -0400 | [diff] [blame] | 72 | private HardwarePropertiesManager mHardProps; |
| 73 | private WarningsUI mMockWarnings; |
| 74 | private PowerUI mPowerUI; |
Salvador Martinez | bb902fc | 2018-01-22 19:46:55 -0800 | [diff] [blame] | 75 | private EnhancedEstimates mEnhancedEstimates; |
Amin Shaikh | 2f6c45c | 2018-04-16 14:00:09 -0400 | [diff] [blame] | 76 | @Mock private PowerManager mPowerManager; |
Jason Monk | d819c31 | 2017-08-11 12:53:36 -0400 | [diff] [blame] | 77 | |
| 78 | @Before |
| 79 | public void setup() { |
Amin Shaikh | 2f6c45c | 2018-04-16 14:00:09 -0400 | [diff] [blame] | 80 | MockitoAnnotations.initMocks(this); |
Jason Monk | d819c31 | 2017-08-11 12:53:36 -0400 | [diff] [blame] | 81 | mMockWarnings = mDependency.injectMockDependency(WarningsUI.class); |
Salvador Martinez | bb902fc | 2018-01-22 19:46:55 -0800 | [diff] [blame] | 82 | mEnhancedEstimates = mDependency.injectMockDependency(EnhancedEstimates.class); |
Jason Monk | d819c31 | 2017-08-11 12:53:36 -0400 | [diff] [blame] | 83 | mHardProps = mock(HardwarePropertiesManager.class); |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 84 | |
Jason Monk | d819c31 | 2017-08-11 12:53:36 -0400 | [diff] [blame] | 85 | mContext.putComponent(StatusBar.class, mock(StatusBar.class)); |
| 86 | mContext.addMockSystemService(Context.HARDWARE_PROPERTIES_SERVICE, mHardProps); |
Amin Shaikh | 2f6c45c | 2018-04-16 14:00:09 -0400 | [diff] [blame] | 87 | mContext.addMockSystemService(Context.POWER_SERVICE, mPowerManager); |
Jason Monk | d819c31 | 2017-08-11 12:53:36 -0400 | [diff] [blame] | 88 | |
| 89 | createPowerUi(); |
| 90 | } |
| 91 | |
| 92 | @Test |
| 93 | public void testNoConfig_NoWarnings() { |
| 94 | setOverThreshold(); |
| 95 | Settings.Global.putString(mContext.getContentResolver(), SHOW_TEMPERATURE_WARNING, null); |
| 96 | TestableResources resources = mContext.getOrCreateTestableResources(); |
| 97 | resources.addOverride(R.integer.config_showTemperatureWarning, 0); |
| 98 | resources.addOverride(R.integer.config_warningTemperature, 55); |
| 99 | |
| 100 | mPowerUI.start(); |
| 101 | verify(mMockWarnings, never()).showHighTemperatureWarning(); |
| 102 | } |
| 103 | |
| 104 | @Test |
| 105 | public void testConfig_NoWarnings() { |
| 106 | setUnderThreshold(); |
| 107 | Settings.Global.putString(mContext.getContentResolver(), SHOW_TEMPERATURE_WARNING, null); |
| 108 | TestableResources resources = mContext.getOrCreateTestableResources(); |
| 109 | resources.addOverride(R.integer.config_showTemperatureWarning, 1); |
| 110 | resources.addOverride(R.integer.config_warningTemperature, 55); |
| 111 | |
| 112 | mPowerUI.start(); |
| 113 | verify(mMockWarnings, never()).showHighTemperatureWarning(); |
| 114 | } |
| 115 | |
| 116 | @Test |
| 117 | public void testConfig_Warnings() { |
| 118 | setOverThreshold(); |
| 119 | Settings.Global.putString(mContext.getContentResolver(), SHOW_TEMPERATURE_WARNING, null); |
| 120 | TestableResources resources = mContext.getOrCreateTestableResources(); |
| 121 | resources.addOverride(R.integer.config_showTemperatureWarning, 1); |
| 122 | resources.addOverride(R.integer.config_warningTemperature, 55); |
| 123 | |
| 124 | mPowerUI.start(); |
| 125 | verify(mMockWarnings).showHighTemperatureWarning(); |
| 126 | } |
| 127 | |
| 128 | @Test |
| 129 | public void testSettingOverrideConfig() { |
| 130 | setOverThreshold(); |
| 131 | Settings.Global.putInt(mContext.getContentResolver(), SHOW_TEMPERATURE_WARNING, 1); |
| 132 | TestableResources resources = mContext.getOrCreateTestableResources(); |
| 133 | resources.addOverride(R.integer.config_showTemperatureWarning, 0); |
| 134 | resources.addOverride(R.integer.config_warningTemperature, 55); |
| 135 | |
| 136 | mPowerUI.start(); |
| 137 | verify(mMockWarnings).showHighTemperatureWarning(); |
| 138 | } |
| 139 | |
| 140 | @Test |
| 141 | public void testShutdownBasedThreshold() { |
| 142 | int tolerance = 2; |
| 143 | Settings.Global.putString(mContext.getContentResolver(), SHOW_TEMPERATURE_WARNING, null); |
| 144 | TestableResources resources = mContext.getOrCreateTestableResources(); |
| 145 | resources.addOverride(R.integer.config_showTemperatureWarning, 1); |
| 146 | resources.addOverride(R.integer.config_warningTemperature, -1); |
| 147 | resources.addOverride(R.integer.config_warningTemperatureTolerance, tolerance); |
| 148 | when(mHardProps.getDeviceTemperatures(DEVICE_TEMPERATURE_SKIN, TEMPERATURE_SHUTDOWN)) |
| 149 | .thenReturn(new float[] { 55 + tolerance }); |
| 150 | |
| 151 | setCurrentTemp(54); // Below threshold. |
| 152 | mPowerUI.start(); |
| 153 | verify(mMockWarnings, never()).showHighTemperatureWarning(); |
| 154 | |
| 155 | setCurrentTemp(56); // Above threshold. |
| 156 | mPowerUI.updateTemperatureWarning(); |
| 157 | verify(mMockWarnings).showHighTemperatureWarning(); |
| 158 | } |
| 159 | |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 160 | @Test |
Salvador Martinez | bb902fc | 2018-01-22 19:46:55 -0800 | [diff] [blame] | 161 | public void testShouldShowLowBatteryWarning_showHybridOnly_overrideThresholdHigh_returnsNoShow() { |
| 162 | when(mEnhancedEstimates.isHybridNotificationEnabled()).thenReturn(true); |
| 163 | when(mEnhancedEstimates.getLowWarningThreshold()) |
| 164 | .thenReturn(Duration.ofHours(1).toMillis()); |
| 165 | when(mEnhancedEstimates.getSevereWarningThreshold()).thenReturn(ONE_HOUR_MILLIS); |
| 166 | mPowerUI.start(); |
| 167 | |
| 168 | // unplugged device that would not show the non-hybrid notification but would show the |
| 169 | // hybrid but the threshold has been overriden to be too low |
| 170 | boolean shouldShow = |
| 171 | mPowerUI.shouldShowLowBatteryWarning(UNPLUGGED, UNPLUGGED, ABOVE_WARNING_BUCKET, |
Salvador Martinez | 3630796 | 2018-02-08 14:29:08 -0800 | [diff] [blame] | 172 | ABOVE_WARNING_BUCKET, BELOW_HYBRID_THRESHOLD, |
Salvador Martinez | bb902fc | 2018-01-22 19:46:55 -0800 | [diff] [blame] | 173 | POWER_SAVER_OFF, BatteryManager.BATTERY_HEALTH_GOOD); |
| 174 | assertFalse(shouldShow); |
| 175 | } |
| 176 | |
| 177 | @Test |
| 178 | public void testShouldShowLowBatteryWarning_showHybridOnly_overrideThresholdHigh_returnsShow() { |
| 179 | when(mEnhancedEstimates.isHybridNotificationEnabled()).thenReturn(true); |
| 180 | when(mEnhancedEstimates.getLowWarningThreshold()) |
| 181 | .thenReturn(Duration.ofHours(5).toMillis()); |
| 182 | when(mEnhancedEstimates.getSevereWarningThreshold()).thenReturn(ONE_HOUR_MILLIS); |
| 183 | mPowerUI.start(); |
| 184 | |
| 185 | // unplugged device that would not show the non-hybrid notification but would show the |
| 186 | // hybrid since the threshold has been overriden to be much higher |
| 187 | boolean shouldShow = |
| 188 | mPowerUI.shouldShowLowBatteryWarning(UNPLUGGED, UNPLUGGED, ABOVE_WARNING_BUCKET, |
Salvador Martinez | 3630796 | 2018-02-08 14:29:08 -0800 | [diff] [blame] | 189 | ABOVE_WARNING_BUCKET, ABOVE_HYBRID_THRESHOLD, |
Salvador Martinez | bb902fc | 2018-01-22 19:46:55 -0800 | [diff] [blame] | 190 | POWER_SAVER_OFF, BatteryManager.BATTERY_HEALTH_GOOD); |
| 191 | assertTrue(shouldShow); |
| 192 | } |
| 193 | |
| 194 | @Test |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 195 | public void testShouldShowLowBatteryWarning_showHybridOnly_returnsShow() { |
Salvador Martinez | bb902fc | 2018-01-22 19:46:55 -0800 | [diff] [blame] | 196 | when(mEnhancedEstimates.isHybridNotificationEnabled()).thenReturn(true); |
| 197 | when(mEnhancedEstimates.getLowWarningThreshold()).thenReturn(PowerUI.THREE_HOURS_IN_MILLIS); |
| 198 | when(mEnhancedEstimates.getSevereWarningThreshold()).thenReturn(ONE_HOUR_MILLIS); |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 199 | mPowerUI.start(); |
| 200 | |
| 201 | // unplugged device that would not show the non-hybrid notification but would show the |
| 202 | // hybrid |
| 203 | boolean shouldShow = |
| 204 | mPowerUI.shouldShowLowBatteryWarning(UNPLUGGED, UNPLUGGED, ABOVE_WARNING_BUCKET, |
Salvador Martinez | 3630796 | 2018-02-08 14:29:08 -0800 | [diff] [blame] | 205 | ABOVE_WARNING_BUCKET, BELOW_HYBRID_THRESHOLD, |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 206 | POWER_SAVER_OFF, BatteryManager.BATTERY_HEALTH_GOOD); |
| 207 | assertTrue(shouldShow); |
| 208 | } |
| 209 | |
| 210 | @Test |
| 211 | public void testShouldShowLowBatteryWarning_showHybrid_showStandard_returnsShow() { |
Salvador Martinez | bb902fc | 2018-01-22 19:46:55 -0800 | [diff] [blame] | 212 | when(mEnhancedEstimates.isHybridNotificationEnabled()).thenReturn(true); |
| 213 | when(mEnhancedEstimates.getLowWarningThreshold()).thenReturn(PowerUI.THREE_HOURS_IN_MILLIS); |
| 214 | when(mEnhancedEstimates.getSevereWarningThreshold()).thenReturn(ONE_HOUR_MILLIS); |
Salvador Martinez | fd38aa5 | 2018-03-28 23:56:59 -0700 | [diff] [blame] | 215 | mPowerUI.mBatteryLevel = 10; |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 216 | mPowerUI.start(); |
| 217 | |
| 218 | // unplugged device that would show the non-hybrid notification and the hybrid |
| 219 | boolean shouldShow = |
| 220 | mPowerUI.shouldShowLowBatteryWarning(UNPLUGGED, UNPLUGGED, ABOVE_WARNING_BUCKET, |
Salvador Martinez | 3630796 | 2018-02-08 14:29:08 -0800 | [diff] [blame] | 221 | BELOW_WARNING_BUCKET, BELOW_HYBRID_THRESHOLD, |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 222 | POWER_SAVER_OFF, BatteryManager.BATTERY_HEALTH_GOOD); |
| 223 | assertTrue(shouldShow); |
| 224 | } |
| 225 | |
| 226 | @Test |
| 227 | public void testShouldShowLowBatteryWarning_showStandardOnly_returnsShow() { |
Salvador Martinez | bb902fc | 2018-01-22 19:46:55 -0800 | [diff] [blame] | 228 | when(mEnhancedEstimates.isHybridNotificationEnabled()).thenReturn(true); |
| 229 | when(mEnhancedEstimates.getLowWarningThreshold()).thenReturn(PowerUI.THREE_HOURS_IN_MILLIS); |
| 230 | when(mEnhancedEstimates.getSevereWarningThreshold()).thenReturn(ONE_HOUR_MILLIS); |
Salvador Martinez | fd38aa5 | 2018-03-28 23:56:59 -0700 | [diff] [blame] | 231 | mPowerUI.mBatteryLevel = 10; |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 232 | mPowerUI.start(); |
| 233 | |
| 234 | // unplugged device that would show the non-hybrid but not the hybrid |
| 235 | boolean shouldShow = |
| 236 | mPowerUI.shouldShowLowBatteryWarning(UNPLUGGED, UNPLUGGED, ABOVE_WARNING_BUCKET, |
Salvador Martinez | 3630796 | 2018-02-08 14:29:08 -0800 | [diff] [blame] | 237 | BELOW_WARNING_BUCKET, ABOVE_HYBRID_THRESHOLD, |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 238 | POWER_SAVER_OFF, BatteryManager.BATTERY_HEALTH_GOOD); |
| 239 | assertTrue(shouldShow); |
| 240 | } |
| 241 | |
| 242 | @Test |
| 243 | public void testShouldShowLowBatteryWarning_deviceHighBattery_returnsNoShow() { |
Salvador Martinez | bb902fc | 2018-01-22 19:46:55 -0800 | [diff] [blame] | 244 | when(mEnhancedEstimates.isHybridNotificationEnabled()).thenReturn(true); |
| 245 | when(mEnhancedEstimates.getLowWarningThreshold()).thenReturn(PowerUI.THREE_HOURS_IN_MILLIS); |
| 246 | when(mEnhancedEstimates.getSevereWarningThreshold()).thenReturn(ONE_HOUR_MILLIS); |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 247 | mPowerUI.start(); |
| 248 | |
| 249 | // unplugged device that would show the neither due to battery level being good |
| 250 | boolean shouldShow = |
| 251 | mPowerUI.shouldShowLowBatteryWarning(UNPLUGGED, UNPLUGGED, ABOVE_WARNING_BUCKET, |
Salvador Martinez | 3630796 | 2018-02-08 14:29:08 -0800 | [diff] [blame] | 252 | ABOVE_WARNING_BUCKET, ABOVE_HYBRID_THRESHOLD, |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 253 | POWER_SAVER_OFF, BatteryManager.BATTERY_HEALTH_GOOD); |
| 254 | assertFalse(shouldShow); |
| 255 | } |
| 256 | |
| 257 | @Test |
| 258 | public void testShouldShowLowBatteryWarning_devicePlugged_returnsNoShow() { |
Salvador Martinez | bb902fc | 2018-01-22 19:46:55 -0800 | [diff] [blame] | 259 | when(mEnhancedEstimates.isHybridNotificationEnabled()).thenReturn(true); |
| 260 | when(mEnhancedEstimates.getLowWarningThreshold()).thenReturn(PowerUI.THREE_HOURS_IN_MILLIS); |
| 261 | when(mEnhancedEstimates.getSevereWarningThreshold()).thenReturn(ONE_HOUR_MILLIS); |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 262 | mPowerUI.start(); |
| 263 | |
| 264 | // plugged device that would show the neither due to being plugged |
| 265 | boolean shouldShow = |
| 266 | mPowerUI.shouldShowLowBatteryWarning(!UNPLUGGED, UNPLUGGED, ABOVE_WARNING_BUCKET, |
Salvador Martinez | 3630796 | 2018-02-08 14:29:08 -0800 | [diff] [blame] | 267 | BELOW_WARNING_BUCKET, BELOW_HYBRID_THRESHOLD, |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 268 | POWER_SAVER_OFF, BatteryManager.BATTERY_HEALTH_GOOD); |
| 269 | assertFalse(shouldShow); |
| 270 | } |
| 271 | |
| 272 | @Test |
Salvador Martinez | fd38aa5 | 2018-03-28 23:56:59 -0700 | [diff] [blame] | 273 | public void testShouldShowLowBatteryWarning_deviceBatteryStatusUnknown_returnsNoShow() { |
Salvador Martinez | bb902fc | 2018-01-22 19:46:55 -0800 | [diff] [blame] | 274 | when(mEnhancedEstimates.isHybridNotificationEnabled()).thenReturn(true); |
| 275 | when(mEnhancedEstimates.getLowWarningThreshold()).thenReturn(PowerUI.THREE_HOURS_IN_MILLIS); |
| 276 | when(mEnhancedEstimates.getSevereWarningThreshold()).thenReturn(ONE_HOUR_MILLIS); |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 277 | mPowerUI.start(); |
| 278 | |
Salvador Martinez | fd38aa5 | 2018-03-28 23:56:59 -0700 | [diff] [blame] | 279 | // Unknown battery status device that would show the neither due to the battery status being |
| 280 | // unknown |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 281 | boolean shouldShow = |
| 282 | mPowerUI.shouldShowLowBatteryWarning(UNPLUGGED, UNPLUGGED, ABOVE_WARNING_BUCKET, |
Salvador Martinez | 3630796 | 2018-02-08 14:29:08 -0800 | [diff] [blame] | 283 | BELOW_WARNING_BUCKET, BELOW_HYBRID_THRESHOLD, |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 284 | !POWER_SAVER_OFF, BatteryManager.BATTERY_STATUS_UNKNOWN); |
| 285 | assertFalse(shouldShow); |
| 286 | } |
| 287 | |
| 288 | @Test |
| 289 | public void testShouldShowLowBatteryWarning_batterySaverEnabled_returnsNoShow() { |
Salvador Martinez | bb902fc | 2018-01-22 19:46:55 -0800 | [diff] [blame] | 290 | when(mEnhancedEstimates.isHybridNotificationEnabled()).thenReturn(true); |
| 291 | when(mEnhancedEstimates.getLowWarningThreshold()).thenReturn(PowerUI.THREE_HOURS_IN_MILLIS); |
| 292 | when(mEnhancedEstimates.getSevereWarningThreshold()).thenReturn(ONE_HOUR_MILLIS); |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 293 | mPowerUI.start(); |
| 294 | |
| 295 | // BatterySaverEnabled device that would show the neither due to battery saver |
| 296 | boolean shouldShow = |
| 297 | mPowerUI.shouldShowLowBatteryWarning(UNPLUGGED, UNPLUGGED, ABOVE_WARNING_BUCKET, |
Salvador Martinez | 3630796 | 2018-02-08 14:29:08 -0800 | [diff] [blame] | 298 | BELOW_WARNING_BUCKET, BELOW_HYBRID_THRESHOLD, |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 299 | !POWER_SAVER_OFF, BatteryManager.BATTERY_HEALTH_GOOD); |
| 300 | assertFalse(shouldShow); |
| 301 | } |
| 302 | |
| 303 | @Test |
Salvador Martinez | 3630796 | 2018-02-08 14:29:08 -0800 | [diff] [blame] | 304 | public void testShouldShowLowBatteryWarning_onlyShowsOncePerChargeCycle() { |
| 305 | mPowerUI.start(); |
| 306 | when(mEnhancedEstimates.isHybridNotificationEnabled()).thenReturn(true); |
| 307 | when(mEnhancedEstimates.getLowWarningThreshold()).thenReturn(PowerUI.THREE_HOURS_IN_MILLIS); |
| 308 | when(mEnhancedEstimates.getSevereWarningThreshold()).thenReturn(ONE_HOUR_MILLIS); |
| 309 | when(mEnhancedEstimates.getEstimate()) |
| 310 | .thenReturn(new Estimate(BELOW_HYBRID_THRESHOLD, true)); |
| 311 | mPowerUI.mBatteryStatus = BatteryManager.BATTERY_HEALTH_GOOD; |
| 312 | |
Salvador Martinez | 926f071 | 2018-07-03 18:07:07 -0700 | [diff] [blame] | 313 | mPowerUI.maybeShowBatteryWarning(OLD_BATTERY_LEVEL_NINE, UNPLUGGED, UNPLUGGED, |
| 314 | ABOVE_WARNING_BUCKET, ABOVE_WARNING_BUCKET); |
Salvador Martinez | fd38aa5 | 2018-03-28 23:56:59 -0700 | [diff] [blame] | 315 | |
| 316 | // reduce battery level to handle time based trigger -> level trigger interactions |
| 317 | mPowerUI.mBatteryLevel = 10; |
Salvador Martinez | 3630796 | 2018-02-08 14:29:08 -0800 | [diff] [blame] | 318 | boolean shouldShow = |
| 319 | mPowerUI.shouldShowLowBatteryWarning(UNPLUGGED, UNPLUGGED, ABOVE_WARNING_BUCKET, |
| 320 | ABOVE_WARNING_BUCKET, BELOW_HYBRID_THRESHOLD, |
| 321 | POWER_SAVER_OFF, BatteryManager.BATTERY_HEALTH_GOOD); |
| 322 | assertFalse(shouldShow); |
| 323 | } |
| 324 | |
| 325 | @Test |
Salvador Martinez | c50a9bd | 2018-08-03 14:07:44 -0700 | [diff] [blame] | 326 | public void testShouldDismissLowBatteryWarning_dismissWhenPowerSaverEnabledLegacy() { |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 327 | mPowerUI.start(); |
Salvador Martinez | c50a9bd | 2018-08-03 14:07:44 -0700 | [diff] [blame] | 328 | when(mEnhancedEstimates.isHybridNotificationEnabled()).thenReturn(false); |
Salvador Martinez | bb902fc | 2018-01-22 19:46:55 -0800 | [diff] [blame] | 329 | when(mEnhancedEstimates.getLowWarningThreshold()).thenReturn(PowerUI.THREE_HOURS_IN_MILLIS); |
| 330 | when(mEnhancedEstimates.getSevereWarningThreshold()).thenReturn(ONE_HOUR_MILLIS); |
| 331 | |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 332 | // device that gets power saver turned on should dismiss |
| 333 | boolean shouldDismiss = |
| 334 | mPowerUI.shouldDismissLowBatteryWarning(UNPLUGGED, BELOW_WARNING_BUCKET, |
| 335 | BELOW_WARNING_BUCKET, ABOVE_HYBRID_THRESHOLD, !POWER_SAVER_OFF); |
| 336 | assertTrue(shouldDismiss); |
| 337 | } |
| 338 | |
| 339 | @Test |
Salvador Martinez | c50a9bd | 2018-08-03 14:07:44 -0700 | [diff] [blame] | 340 | public void testShouldNotDismissLowBatteryWarning_dismissWhenPowerSaverEnabledHybrid() { |
| 341 | mPowerUI.start(); |
| 342 | when(mEnhancedEstimates.isHybridNotificationEnabled()).thenReturn(true); |
| 343 | when(mEnhancedEstimates.getLowWarningThreshold()).thenReturn(PowerUI.THREE_HOURS_IN_MILLIS); |
| 344 | when(mEnhancedEstimates.getSevereWarningThreshold()).thenReturn(ONE_HOUR_MILLIS); |
| 345 | |
| 346 | // device that gets power saver turned on should dismiss |
| 347 | boolean shouldDismiss = |
| 348 | mPowerUI.shouldDismissLowBatteryWarning(UNPLUGGED, BELOW_WARNING_BUCKET, |
| 349 | BELOW_WARNING_BUCKET, ABOVE_HYBRID_THRESHOLD, !POWER_SAVER_OFF); |
| 350 | assertFalse(shouldDismiss); |
| 351 | } |
| 352 | |
| 353 | @Test |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 354 | public void testShouldDismissLowBatteryWarning_dismissWhenPlugged() { |
| 355 | mPowerUI.start(); |
Salvador Martinez | bb902fc | 2018-01-22 19:46:55 -0800 | [diff] [blame] | 356 | when(mEnhancedEstimates.isHybridNotificationEnabled()).thenReturn(true); |
| 357 | when(mEnhancedEstimates.getLowWarningThreshold()).thenReturn(PowerUI.THREE_HOURS_IN_MILLIS); |
| 358 | when(mEnhancedEstimates.getSevereWarningThreshold()).thenReturn(ONE_HOUR_MILLIS); |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 359 | |
| 360 | // device that gets plugged in should dismiss |
| 361 | boolean shouldDismiss = |
| 362 | mPowerUI.shouldDismissLowBatteryWarning(!UNPLUGGED, BELOW_WARNING_BUCKET, |
| 363 | BELOW_WARNING_BUCKET, ABOVE_HYBRID_THRESHOLD, POWER_SAVER_OFF); |
| 364 | assertTrue(shouldDismiss); |
| 365 | } |
| 366 | |
| 367 | @Test |
| 368 | public void testShouldDismissLowBatteryWarning_dismissHybridSignal_showStandardSignal_shouldShow() { |
| 369 | mPowerUI.start(); |
Salvador Martinez | bb902fc | 2018-01-22 19:46:55 -0800 | [diff] [blame] | 370 | when(mEnhancedEstimates.isHybridNotificationEnabled()).thenReturn(true); |
| 371 | when(mEnhancedEstimates.getLowWarningThreshold()).thenReturn(PowerUI.THREE_HOURS_IN_MILLIS); |
| 372 | when(mEnhancedEstimates.getSevereWarningThreshold()).thenReturn(ONE_HOUR_MILLIS); |
| 373 | |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 374 | // would dismiss hybrid but not non-hybrid should not dismiss |
| 375 | boolean shouldDismiss = |
| 376 | mPowerUI.shouldDismissLowBatteryWarning(UNPLUGGED, BELOW_WARNING_BUCKET, |
| 377 | BELOW_WARNING_BUCKET, ABOVE_HYBRID_THRESHOLD, POWER_SAVER_OFF); |
| 378 | assertFalse(shouldDismiss); |
| 379 | } |
| 380 | |
| 381 | @Test |
| 382 | public void testShouldDismissLowBatteryWarning_showHybridSignal_dismissStandardSignal_shouldShow() { |
| 383 | mPowerUI.start(); |
Salvador Martinez | bb902fc | 2018-01-22 19:46:55 -0800 | [diff] [blame] | 384 | when(mEnhancedEstimates.isHybridNotificationEnabled()).thenReturn(true); |
| 385 | when(mEnhancedEstimates.getLowWarningThreshold()).thenReturn(PowerUI.THREE_HOURS_IN_MILLIS); |
| 386 | when(mEnhancedEstimates.getSevereWarningThreshold()).thenReturn(ONE_HOUR_MILLIS); |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 387 | |
| 388 | // would dismiss non-hybrid but not hybrid should not dismiss |
| 389 | boolean shouldDismiss = |
| 390 | mPowerUI.shouldDismissLowBatteryWarning(UNPLUGGED, BELOW_WARNING_BUCKET, |
| 391 | ABOVE_WARNING_BUCKET, BELOW_HYBRID_THRESHOLD, POWER_SAVER_OFF); |
| 392 | assertFalse(shouldDismiss); |
| 393 | } |
| 394 | |
| 395 | @Test |
| 396 | public void testShouldDismissLowBatteryWarning_showBothSignal_shouldShow() { |
| 397 | mPowerUI.start(); |
Salvador Martinez | bb902fc | 2018-01-22 19:46:55 -0800 | [diff] [blame] | 398 | when(mEnhancedEstimates.isHybridNotificationEnabled()).thenReturn(true); |
| 399 | when(mEnhancedEstimates.getLowWarningThreshold()).thenReturn(PowerUI.THREE_HOURS_IN_MILLIS); |
| 400 | when(mEnhancedEstimates.getSevereWarningThreshold()).thenReturn(ONE_HOUR_MILLIS); |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 401 | |
| 402 | // should not dismiss when both would not dismiss |
| 403 | boolean shouldDismiss = |
| 404 | mPowerUI.shouldDismissLowBatteryWarning(UNPLUGGED, BELOW_WARNING_BUCKET, |
| 405 | BELOW_WARNING_BUCKET, BELOW_HYBRID_THRESHOLD, POWER_SAVER_OFF); |
| 406 | assertFalse(shouldDismiss); |
| 407 | } |
| 408 | |
| 409 | @Test |
| 410 | public void testShouldDismissLowBatteryWarning_dismissBothSignal_shouldDismiss() { |
| 411 | mPowerUI.start(); |
Salvador Martinez | bb902fc | 2018-01-22 19:46:55 -0800 | [diff] [blame] | 412 | when(mEnhancedEstimates.isHybridNotificationEnabled()).thenReturn(true); |
| 413 | when(mEnhancedEstimates.getLowWarningThreshold()).thenReturn(PowerUI.THREE_HOURS_IN_MILLIS); |
| 414 | when(mEnhancedEstimates.getSevereWarningThreshold()).thenReturn(ONE_HOUR_MILLIS); |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 415 | |
| 416 | //should dismiss if both would dismiss |
| 417 | boolean shouldDismiss = |
| 418 | mPowerUI.shouldDismissLowBatteryWarning(UNPLUGGED, BELOW_WARNING_BUCKET, |
| 419 | ABOVE_WARNING_BUCKET, ABOVE_HYBRID_THRESHOLD, POWER_SAVER_OFF); |
| 420 | assertTrue(shouldDismiss); |
| 421 | } |
| 422 | |
| 423 | @Test |
| 424 | public void testShouldDismissLowBatteryWarning_dismissStandardSignal_hybridDisabled_shouldDismiss() { |
| 425 | mPowerUI.start(); |
Salvador Martinez | bb902fc | 2018-01-22 19:46:55 -0800 | [diff] [blame] | 426 | when(mEnhancedEstimates.isHybridNotificationEnabled()).thenReturn(false); |
| 427 | when(mEnhancedEstimates.getLowWarningThreshold()).thenReturn(PowerUI.THREE_HOURS_IN_MILLIS); |
| 428 | when(mEnhancedEstimates.getSevereWarningThreshold()).thenReturn(ONE_HOUR_MILLIS); |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 429 | |
| 430 | // would dismiss non-hybrid with hybrid disabled should dismiss |
| 431 | boolean shouldDismiss = |
| 432 | mPowerUI.shouldDismissLowBatteryWarning(UNPLUGGED, BELOW_WARNING_BUCKET, |
| 433 | ABOVE_WARNING_BUCKET, ABOVE_HYBRID_THRESHOLD, POWER_SAVER_OFF); |
| 434 | assertTrue(shouldDismiss); |
| 435 | } |
| 436 | |
Amin Shaikh | 2f6c45c | 2018-04-16 14:00:09 -0400 | [diff] [blame] | 437 | @Test |
| 438 | public void testShouldDismissLowBatteryWarning_powerSaverModeEnabled() |
| 439 | throws InterruptedException { |
| 440 | when(mPowerManager.isPowerSaveMode()).thenReturn(true); |
| 441 | |
| 442 | mPowerUI.start(); |
| 443 | mPowerUI.mReceiver.onReceive(mContext, |
| 444 | new Intent(PowerManager.ACTION_POWER_SAVE_MODE_CHANGED)); |
| 445 | |
| 446 | CountDownLatch latch = new CountDownLatch(1); |
| 447 | ThreadUtils.postOnBackgroundThread(() -> latch.countDown()); |
| 448 | latch.await(5, TimeUnit.SECONDS); |
| 449 | |
| 450 | verify(mMockWarnings).dismissLowBatteryWarning(); |
| 451 | } |
| 452 | |
| 453 | @Test |
| 454 | public void testShouldNotDismissLowBatteryWarning_powerSaverModeDisabled() |
| 455 | throws InterruptedException { |
| 456 | when(mPowerManager.isPowerSaveMode()).thenReturn(false); |
| 457 | |
| 458 | mPowerUI.start(); |
| 459 | mPowerUI.mReceiver.onReceive(mContext, |
| 460 | new Intent(PowerManager.ACTION_POWER_SAVE_MODE_CHANGED)); |
| 461 | |
| 462 | CountDownLatch latch = new CountDownLatch(1); |
| 463 | ThreadUtils.postOnBackgroundThread(() -> latch.countDown()); |
| 464 | latch.await(5, TimeUnit.SECONDS); |
| 465 | |
| 466 | verify(mMockWarnings, never()).dismissLowBatteryWarning(); |
| 467 | } |
| 468 | |
Salvador Martinez | 926f071 | 2018-07-03 18:07:07 -0700 | [diff] [blame] | 469 | @Test |
| 470 | public void testMaybeShowBatteryWarning_onlyQueriesEstimateOnBatteryLevelChangeOrNull() { |
| 471 | mPowerUI.start(); |
| 472 | Estimate estimate = new Estimate(BELOW_HYBRID_THRESHOLD, true); |
| 473 | when(mEnhancedEstimates.isHybridNotificationEnabled()).thenReturn(true); |
| 474 | when(mEnhancedEstimates.getLowWarningThreshold()).thenReturn(PowerUI.THREE_HOURS_IN_MILLIS); |
| 475 | when(mEnhancedEstimates.getSevereWarningThreshold()).thenReturn(ONE_HOUR_MILLIS); |
| 476 | when(mEnhancedEstimates.getEstimate()).thenReturn(estimate); |
| 477 | mPowerUI.mBatteryStatus = BatteryManager.BATTERY_HEALTH_GOOD; |
| 478 | |
| 479 | // we expect that the first time it will query even if the level is the same |
| 480 | mPowerUI.mBatteryLevel = 9; |
| 481 | mPowerUI.maybeShowBatteryWarning(OLD_BATTERY_LEVEL_NINE, UNPLUGGED, UNPLUGGED, |
| 482 | ABOVE_WARNING_BUCKET, ABOVE_WARNING_BUCKET); |
| 483 | verify(mEnhancedEstimates, times(1)).getEstimate(); |
| 484 | |
| 485 | // We should NOT query again if the battery level hasn't changed |
| 486 | mPowerUI.maybeShowBatteryWarning(OLD_BATTERY_LEVEL_NINE, UNPLUGGED, UNPLUGGED, |
| 487 | ABOVE_WARNING_BUCKET, ABOVE_WARNING_BUCKET); |
| 488 | verify(mEnhancedEstimates, times(1)).getEstimate(); |
| 489 | |
| 490 | // Battery level has changed, so we should query again |
| 491 | mPowerUI.maybeShowBatteryWarning(OLD_BATTERY_LEVEL_10, UNPLUGGED, UNPLUGGED, |
| 492 | ABOVE_WARNING_BUCKET, ABOVE_WARNING_BUCKET); |
| 493 | verify(mEnhancedEstimates, times(2)).getEstimate(); |
| 494 | } |
| 495 | |
Jason Monk | d819c31 | 2017-08-11 12:53:36 -0400 | [diff] [blame] | 496 | private void setCurrentTemp(float temp) { |
| 497 | when(mHardProps.getDeviceTemperatures(DEVICE_TEMPERATURE_SKIN, TEMPERATURE_CURRENT)) |
| 498 | .thenReturn(new float[] { temp }); |
| 499 | } |
| 500 | |
| 501 | private void setOverThreshold() { |
| 502 | setCurrentTemp(50000); |
| 503 | } |
| 504 | |
| 505 | private void setUnderThreshold() { |
| 506 | setCurrentTemp(5); |
| 507 | } |
| 508 | |
| 509 | private void createPowerUi() { |
| 510 | mPowerUI = new PowerUI(); |
| 511 | mPowerUI.mContext = mContext; |
| 512 | mPowerUI.mComponents = mContext.getComponents(); |
| 513 | } |
| 514 | } |