blob: 054bdee9fd97dc4bb12cbe3c86e9ee02b3a03101 [file] [log] [blame]
Jason Monkd819c312017-08-11 12:53:36 -04001/*
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
15package com.android.systemui.power;
16
17import static android.os.HardwarePropertiesManager.DEVICE_TEMPERATURE_SKIN;
18import static android.os.HardwarePropertiesManager.TEMPERATURE_CURRENT;
19import static android.os.HardwarePropertiesManager.TEMPERATURE_SHUTDOWN;
Bill Lin32ed3d62018-10-02 18:10:09 +080020import static android.os.HardwarePropertiesManager.TEMPERATURE_THROTTLING;
Jason Monkd819c312017-08-11 12:53:36 -040021import static android.provider.Settings.Global.SHOW_TEMPERATURE_WARNING;
22
Salvador Martinezf9e47502018-01-04 13:45:48 -080023import static junit.framework.Assert.assertFalse;
24import static junit.framework.Assert.assertTrue;
Amin Shaikh2f6c45c2018-04-16 14:00:09 -040025
Jason Monkd819c312017-08-11 12:53:36 -040026import static org.mockito.Mockito.mock;
27import static org.mockito.Mockito.never;
Salvador Martinez926f0712018-07-03 18:07:07 -070028import static org.mockito.Mockito.times;
Jason Monkd819c312017-08-11 12:53:36 -040029import static org.mockito.Mockito.verify;
30import static org.mockito.Mockito.when;
31
32import android.content.Context;
Amin Shaikh2f6c45c2018-04-16 14:00:09 -040033import android.content.Intent;
Salvador Martinezf9e47502018-01-04 13:45:48 -080034import android.os.BatteryManager;
Jason Monkd819c312017-08-11 12:53:36 -040035import android.os.HardwarePropertiesManager;
Amin Shaikh2f6c45c2018-04-16 14:00:09 -040036import android.os.PowerManager;
Jason Monkd819c312017-08-11 12:53:36 -040037import android.provider.Settings;
38import android.testing.AndroidTestingRunner;
39import android.testing.TestableLooper.RunWithLooper;
40import android.testing.TestableResources;
41import android.test.suitebuilder.annotation.SmallTest;
42
Amin Shaikh2f6c45c2018-04-16 14:00:09 -040043import com.android.settingslib.utils.ThreadUtils;
Jason Monkd819c312017-08-11 12:53:36 -040044import com.android.systemui.R;
45import com.android.systemui.SysuiTestCase;
46import com.android.systemui.power.PowerUI.WarningsUI;
47import com.android.systemui.statusbar.phone.StatusBar;
48
Salvador Martinezbb902fc2018-01-22 19:46:55 -080049import java.time.Duration;
Amin Shaikh2f6c45c2018-04-16 14:00:09 -040050import java.util.concurrent.CountDownLatch;
Salvador Martinezf9e47502018-01-04 13:45:48 -080051import java.util.concurrent.TimeUnit;
Jason Monkd819c312017-08-11 12:53:36 -040052import org.junit.Before;
53import org.junit.Test;
54import org.junit.runner.RunWith;
Amin Shaikh2f6c45c2018-04-16 14:00:09 -040055import org.mockito.Mock;
56import org.mockito.MockitoAnnotations;
Jason Monkd819c312017-08-11 12:53:36 -040057
58@RunWith(AndroidTestingRunner.class)
59@RunWithLooper
60@SmallTest
61public class PowerUITest extends SysuiTestCase {
62
Salvador Martinezf9e47502018-01-04 13:45:48 -080063 private static final boolean UNPLUGGED = false;
64 private static final boolean POWER_SAVER_OFF = false;
65 private static final int ABOVE_WARNING_BUCKET = 1;
Salvador Martinezbb902fc2018-01-22 19:46:55 -080066 private static final long ONE_HOUR_MILLIS = Duration.ofHours(1).toMillis();
Salvador Martinezf9e47502018-01-04 13:45:48 -080067 public static final int BELOW_WARNING_BUCKET = -1;
68 public static final long BELOW_HYBRID_THRESHOLD = TimeUnit.HOURS.toMillis(2);
69 public static final long ABOVE_HYBRID_THRESHOLD = TimeUnit.HOURS.toMillis(4);
Salvador Martinezfd38aa52018-03-28 23:56:59 -070070 private static final long ABOVE_CHARGE_CYCLE_THRESHOLD = Duration.ofHours(8).toMillis();
Salvador Martinez926f0712018-07-03 18:07:07 -070071 private static final int OLD_BATTERY_LEVEL_NINE = 9;
72 private static final int OLD_BATTERY_LEVEL_10 = 10;
Bill Lin32ed3d62018-10-02 18:10:09 +080073 private static final int DEFAULT_OVERHEAT_ALARM_THRESHOLD = 58;
Jason Monkd819c312017-08-11 12:53:36 -040074 private HardwarePropertiesManager mHardProps;
75 private WarningsUI mMockWarnings;
76 private PowerUI mPowerUI;
Salvador Martinezbb902fc2018-01-22 19:46:55 -080077 private EnhancedEstimates mEnhancedEstimates;
Amin Shaikh2f6c45c2018-04-16 14:00:09 -040078 @Mock private PowerManager mPowerManager;
Jason Monkd819c312017-08-11 12:53:36 -040079
80 @Before
81 public void setup() {
Amin Shaikh2f6c45c2018-04-16 14:00:09 -040082 MockitoAnnotations.initMocks(this);
Jason Monkd819c312017-08-11 12:53:36 -040083 mMockWarnings = mDependency.injectMockDependency(WarningsUI.class);
Salvador Martinezbb902fc2018-01-22 19:46:55 -080084 mEnhancedEstimates = mDependency.injectMockDependency(EnhancedEstimates.class);
Jason Monkd819c312017-08-11 12:53:36 -040085 mHardProps = mock(HardwarePropertiesManager.class);
Salvador Martinezf9e47502018-01-04 13:45:48 -080086
Jason Monkd819c312017-08-11 12:53:36 -040087 mContext.putComponent(StatusBar.class, mock(StatusBar.class));
88 mContext.addMockSystemService(Context.HARDWARE_PROPERTIES_SERVICE, mHardProps);
Amin Shaikh2f6c45c2018-04-16 14:00:09 -040089 mContext.addMockSystemService(Context.POWER_SERVICE, mPowerManager);
Jason Monkd819c312017-08-11 12:53:36 -040090
Bill Lin32ed3d62018-10-02 18:10:09 +080091 setUnderThreshold();
Jason Monkd819c312017-08-11 12:53:36 -040092 createPowerUi();
93 }
94
95 @Test
96 public void testNoConfig_NoWarnings() {
97 setOverThreshold();
98 Settings.Global.putString(mContext.getContentResolver(), SHOW_TEMPERATURE_WARNING, null);
99 TestableResources resources = mContext.getOrCreateTestableResources();
100 resources.addOverride(R.integer.config_showTemperatureWarning, 0);
101 resources.addOverride(R.integer.config_warningTemperature, 55);
102
103 mPowerUI.start();
104 verify(mMockWarnings, never()).showHighTemperatureWarning();
105 }
106
107 @Test
108 public void testConfig_NoWarnings() {
109 setUnderThreshold();
110 Settings.Global.putString(mContext.getContentResolver(), SHOW_TEMPERATURE_WARNING, null);
111 TestableResources resources = mContext.getOrCreateTestableResources();
112 resources.addOverride(R.integer.config_showTemperatureWarning, 1);
113 resources.addOverride(R.integer.config_warningTemperature, 55);
114
115 mPowerUI.start();
116 verify(mMockWarnings, never()).showHighTemperatureWarning();
117 }
118
119 @Test
120 public void testConfig_Warnings() {
121 setOverThreshold();
122 Settings.Global.putString(mContext.getContentResolver(), SHOW_TEMPERATURE_WARNING, null);
123 TestableResources resources = mContext.getOrCreateTestableResources();
124 resources.addOverride(R.integer.config_showTemperatureWarning, 1);
125 resources.addOverride(R.integer.config_warningTemperature, 55);
126
127 mPowerUI.start();
128 verify(mMockWarnings).showHighTemperatureWarning();
129 }
130
131 @Test
132 public void testSettingOverrideConfig() {
133 setOverThreshold();
134 Settings.Global.putInt(mContext.getContentResolver(), SHOW_TEMPERATURE_WARNING, 1);
135 TestableResources resources = mContext.getOrCreateTestableResources();
136 resources.addOverride(R.integer.config_showTemperatureWarning, 0);
137 resources.addOverride(R.integer.config_warningTemperature, 55);
138
139 mPowerUI.start();
140 verify(mMockWarnings).showHighTemperatureWarning();
141 }
142
143 @Test
144 public void testShutdownBasedThreshold() {
145 int tolerance = 2;
146 Settings.Global.putString(mContext.getContentResolver(), SHOW_TEMPERATURE_WARNING, null);
147 TestableResources resources = mContext.getOrCreateTestableResources();
148 resources.addOverride(R.integer.config_showTemperatureWarning, 1);
149 resources.addOverride(R.integer.config_warningTemperature, -1);
150 resources.addOverride(R.integer.config_warningTemperatureTolerance, tolerance);
151 when(mHardProps.getDeviceTemperatures(DEVICE_TEMPERATURE_SKIN, TEMPERATURE_SHUTDOWN))
152 .thenReturn(new float[] { 55 + tolerance });
153
154 setCurrentTemp(54); // Below threshold.
155 mPowerUI.start();
156 verify(mMockWarnings, never()).showHighTemperatureWarning();
157
158 setCurrentTemp(56); // Above threshold.
Bill Lin32ed3d62018-10-02 18:10:09 +0800159 mPowerUI.updateTemperature();
Jason Monkd819c312017-08-11 12:53:36 -0400160 verify(mMockWarnings).showHighTemperatureWarning();
161 }
162
Salvador Martinezf9e47502018-01-04 13:45:48 -0800163 @Test
Bill Lin32ed3d62018-10-02 18:10:09 +0800164 public void testNoConfig_noAlarms() {
165 setOverThreshold();
166 final Boolean overheat = false;
167 final Boolean shouldBeepSound = false;
168 TestableResources resources = mContext.getOrCreateTestableResources();
169 resources.addOverride(R.integer.config_showTemperatureWarning, 0);
170 resources.addOverride(R.integer.config_alarmTemperature, 55);
171 resources.addOverride(R.bool.config_alarmTemperatureBeepSound, shouldBeepSound);
172
173 mPowerUI.start();
174 verify(mMockWarnings, never()).notifyHighTemperatureAlarm(overheat, shouldBeepSound);
175 }
176
177 @Test
178 public void testConfig_noAlarms() {
179 setUnderThreshold();
180 final Boolean overheat = false;
181 final Boolean shouldBeepSound = false;
182 TestableResources resources = mContext.getOrCreateTestableResources();
183 resources.addOverride(R.integer.config_showTemperatureAlarm, 1);
184 resources.addOverride(R.integer.config_alarmTemperature, 58);
185 resources.addOverride(R.bool.config_alarmTemperatureBeepSound, shouldBeepSound);
186
187 mPowerUI.start();
188 verify(mMockWarnings, never()).notifyHighTemperatureAlarm(overheat, shouldBeepSound);
189 }
190
191 @Test
192 public void testConfig_alarms() {
193 setOverThreshold();
194 final Boolean overheat = true;
195 final Boolean shouldBeepSound = false;
196 TestableResources resources = mContext.getOrCreateTestableResources();
197 resources.addOverride(R.integer.config_showTemperatureAlarm, 1);
198 resources.addOverride(R.integer.config_alarmTemperature, 58);
199 resources.addOverride(R.bool.config_alarmTemperatureBeepSound, shouldBeepSound);
200
201 mPowerUI.start();
202 verify(mMockWarnings).notifyHighTemperatureAlarm(overheat, shouldBeepSound);
203 }
204
205 @Test
206 public void testConfig_alarmsWithBeepSound() {
207 setOverThreshold();
208 final Boolean overheat = true;
209 final Boolean shouldBeepSound = true;
210 TestableResources resources = mContext.getOrCreateTestableResources();
211 resources.addOverride(R.integer.config_showTemperatureAlarm, 1);
212 resources.addOverride(R.integer.config_alarmTemperature, 58);
213 resources.addOverride(R.bool.config_alarmTemperatureBeepSound, shouldBeepSound);
214
215 mPowerUI.start();
216 verify(mMockWarnings).notifyHighTemperatureAlarm(overheat, shouldBeepSound);
217 }
218
219 @Test
220 public void testHardPropsThrottlingThreshold_alarms() {
221 setThrottlingThreshold(DEFAULT_OVERHEAT_ALARM_THRESHOLD);
222 setOverThreshold();
223 final Boolean overheat = true;
224 final Boolean shouldBeepSound = false;
225 TestableResources resources = mContext.getOrCreateTestableResources();
226 resources.addOverride(R.integer.config_showTemperatureAlarm, 1);
227 resources.addOverride(R.bool.config_alarmTemperatureBeepSound, shouldBeepSound);
228
229 mPowerUI.start();
230 verify(mMockWarnings).notifyHighTemperatureAlarm(overheat, shouldBeepSound);
231 }
232
233 @Test
234 public void testHardPropsThrottlingThreshold_noAlarms() {
235 setThrottlingThreshold(DEFAULT_OVERHEAT_ALARM_THRESHOLD);
236 setUnderThreshold();
237 final Boolean overheat = false;
238 final Boolean shouldBeepSound = false;
239 TestableResources resources = mContext.getOrCreateTestableResources();
240 resources.addOverride(R.integer.config_showTemperatureAlarm, 1);
241 resources.addOverride(R.bool.config_alarmTemperatureBeepSound, shouldBeepSound);
242
243 mPowerUI.start();
244 verify(mMockWarnings, never()).notifyHighTemperatureAlarm(overheat, shouldBeepSound);
245 }
246
247 @Test
Salvador Martinezbb902fc2018-01-22 19:46:55 -0800248 public void testShouldShowLowBatteryWarning_showHybridOnly_overrideThresholdHigh_returnsNoShow() {
249 when(mEnhancedEstimates.isHybridNotificationEnabled()).thenReturn(true);
250 when(mEnhancedEstimates.getLowWarningThreshold())
251 .thenReturn(Duration.ofHours(1).toMillis());
252 when(mEnhancedEstimates.getSevereWarningThreshold()).thenReturn(ONE_HOUR_MILLIS);
253 mPowerUI.start();
254
255 // unplugged device that would not show the non-hybrid notification but would show the
256 // hybrid but the threshold has been overriden to be too low
257 boolean shouldShow =
258 mPowerUI.shouldShowLowBatteryWarning(UNPLUGGED, UNPLUGGED, ABOVE_WARNING_BUCKET,
Salvador Martinez36307962018-02-08 14:29:08 -0800259 ABOVE_WARNING_BUCKET, BELOW_HYBRID_THRESHOLD,
Salvador Martinezbb902fc2018-01-22 19:46:55 -0800260 POWER_SAVER_OFF, BatteryManager.BATTERY_HEALTH_GOOD);
261 assertFalse(shouldShow);
262 }
263
264 @Test
265 public void testShouldShowLowBatteryWarning_showHybridOnly_overrideThresholdHigh_returnsShow() {
266 when(mEnhancedEstimates.isHybridNotificationEnabled()).thenReturn(true);
267 when(mEnhancedEstimates.getLowWarningThreshold())
268 .thenReturn(Duration.ofHours(5).toMillis());
269 when(mEnhancedEstimates.getSevereWarningThreshold()).thenReturn(ONE_HOUR_MILLIS);
270 mPowerUI.start();
271
272 // unplugged device that would not show the non-hybrid notification but would show the
273 // hybrid since the threshold has been overriden to be much higher
274 boolean shouldShow =
275 mPowerUI.shouldShowLowBatteryWarning(UNPLUGGED, UNPLUGGED, ABOVE_WARNING_BUCKET,
Salvador Martinez36307962018-02-08 14:29:08 -0800276 ABOVE_WARNING_BUCKET, ABOVE_HYBRID_THRESHOLD,
Salvador Martinezbb902fc2018-01-22 19:46:55 -0800277 POWER_SAVER_OFF, BatteryManager.BATTERY_HEALTH_GOOD);
278 assertTrue(shouldShow);
279 }
280
281 @Test
Salvador Martinezf9e47502018-01-04 13:45:48 -0800282 public void testShouldShowLowBatteryWarning_showHybridOnly_returnsShow() {
Salvador Martinezbb902fc2018-01-22 19:46:55 -0800283 when(mEnhancedEstimates.isHybridNotificationEnabled()).thenReturn(true);
284 when(mEnhancedEstimates.getLowWarningThreshold()).thenReturn(PowerUI.THREE_HOURS_IN_MILLIS);
285 when(mEnhancedEstimates.getSevereWarningThreshold()).thenReturn(ONE_HOUR_MILLIS);
Salvador Martinezf9e47502018-01-04 13:45:48 -0800286 mPowerUI.start();
287
288 // unplugged device that would not show the non-hybrid notification but would show the
289 // hybrid
290 boolean shouldShow =
291 mPowerUI.shouldShowLowBatteryWarning(UNPLUGGED, UNPLUGGED, ABOVE_WARNING_BUCKET,
Salvador Martinez36307962018-02-08 14:29:08 -0800292 ABOVE_WARNING_BUCKET, BELOW_HYBRID_THRESHOLD,
Salvador Martinezf9e47502018-01-04 13:45:48 -0800293 POWER_SAVER_OFF, BatteryManager.BATTERY_HEALTH_GOOD);
294 assertTrue(shouldShow);
295 }
296
297 @Test
298 public void testShouldShowLowBatteryWarning_showHybrid_showStandard_returnsShow() {
Salvador Martinezbb902fc2018-01-22 19:46:55 -0800299 when(mEnhancedEstimates.isHybridNotificationEnabled()).thenReturn(true);
300 when(mEnhancedEstimates.getLowWarningThreshold()).thenReturn(PowerUI.THREE_HOURS_IN_MILLIS);
301 when(mEnhancedEstimates.getSevereWarningThreshold()).thenReturn(ONE_HOUR_MILLIS);
Salvador Martinezfd38aa52018-03-28 23:56:59 -0700302 mPowerUI.mBatteryLevel = 10;
Salvador Martinezf9e47502018-01-04 13:45:48 -0800303 mPowerUI.start();
304
305 // unplugged device that would show the non-hybrid notification and the hybrid
306 boolean shouldShow =
307 mPowerUI.shouldShowLowBatteryWarning(UNPLUGGED, UNPLUGGED, ABOVE_WARNING_BUCKET,
Salvador Martinez36307962018-02-08 14:29:08 -0800308 BELOW_WARNING_BUCKET, BELOW_HYBRID_THRESHOLD,
Salvador Martinezf9e47502018-01-04 13:45:48 -0800309 POWER_SAVER_OFF, BatteryManager.BATTERY_HEALTH_GOOD);
310 assertTrue(shouldShow);
311 }
312
313 @Test
314 public void testShouldShowLowBatteryWarning_showStandardOnly_returnsShow() {
Salvador Martinezbb902fc2018-01-22 19:46:55 -0800315 when(mEnhancedEstimates.isHybridNotificationEnabled()).thenReturn(true);
316 when(mEnhancedEstimates.getLowWarningThreshold()).thenReturn(PowerUI.THREE_HOURS_IN_MILLIS);
317 when(mEnhancedEstimates.getSevereWarningThreshold()).thenReturn(ONE_HOUR_MILLIS);
Salvador Martinezfd38aa52018-03-28 23:56:59 -0700318 mPowerUI.mBatteryLevel = 10;
Salvador Martinezf9e47502018-01-04 13:45:48 -0800319 mPowerUI.start();
320
321 // unplugged device that would show the non-hybrid but not the hybrid
322 boolean shouldShow =
323 mPowerUI.shouldShowLowBatteryWarning(UNPLUGGED, UNPLUGGED, ABOVE_WARNING_BUCKET,
Salvador Martinez36307962018-02-08 14:29:08 -0800324 BELOW_WARNING_BUCKET, ABOVE_HYBRID_THRESHOLD,
Salvador Martinezf9e47502018-01-04 13:45:48 -0800325 POWER_SAVER_OFF, BatteryManager.BATTERY_HEALTH_GOOD);
326 assertTrue(shouldShow);
327 }
328
329 @Test
330 public void testShouldShowLowBatteryWarning_deviceHighBattery_returnsNoShow() {
Salvador Martinezbb902fc2018-01-22 19:46:55 -0800331 when(mEnhancedEstimates.isHybridNotificationEnabled()).thenReturn(true);
332 when(mEnhancedEstimates.getLowWarningThreshold()).thenReturn(PowerUI.THREE_HOURS_IN_MILLIS);
333 when(mEnhancedEstimates.getSevereWarningThreshold()).thenReturn(ONE_HOUR_MILLIS);
Salvador Martinezf9e47502018-01-04 13:45:48 -0800334 mPowerUI.start();
335
336 // unplugged device that would show the neither due to battery level being good
337 boolean shouldShow =
338 mPowerUI.shouldShowLowBatteryWarning(UNPLUGGED, UNPLUGGED, ABOVE_WARNING_BUCKET,
Salvador Martinez36307962018-02-08 14:29:08 -0800339 ABOVE_WARNING_BUCKET, ABOVE_HYBRID_THRESHOLD,
Salvador Martinezf9e47502018-01-04 13:45:48 -0800340 POWER_SAVER_OFF, BatteryManager.BATTERY_HEALTH_GOOD);
341 assertFalse(shouldShow);
342 }
343
344 @Test
345 public void testShouldShowLowBatteryWarning_devicePlugged_returnsNoShow() {
Salvador Martinezbb902fc2018-01-22 19:46:55 -0800346 when(mEnhancedEstimates.isHybridNotificationEnabled()).thenReturn(true);
347 when(mEnhancedEstimates.getLowWarningThreshold()).thenReturn(PowerUI.THREE_HOURS_IN_MILLIS);
348 when(mEnhancedEstimates.getSevereWarningThreshold()).thenReturn(ONE_HOUR_MILLIS);
Salvador Martinezf9e47502018-01-04 13:45:48 -0800349 mPowerUI.start();
350
351 // plugged device that would show the neither due to being plugged
352 boolean shouldShow =
353 mPowerUI.shouldShowLowBatteryWarning(!UNPLUGGED, UNPLUGGED, ABOVE_WARNING_BUCKET,
Salvador Martinez36307962018-02-08 14:29:08 -0800354 BELOW_WARNING_BUCKET, BELOW_HYBRID_THRESHOLD,
Salvador Martinezf9e47502018-01-04 13:45:48 -0800355 POWER_SAVER_OFF, BatteryManager.BATTERY_HEALTH_GOOD);
356 assertFalse(shouldShow);
357 }
358
359 @Test
Salvador Martinezfd38aa52018-03-28 23:56:59 -0700360 public void testShouldShowLowBatteryWarning_deviceBatteryStatusUnknown_returnsNoShow() {
Salvador Martinezbb902fc2018-01-22 19:46:55 -0800361 when(mEnhancedEstimates.isHybridNotificationEnabled()).thenReturn(true);
362 when(mEnhancedEstimates.getLowWarningThreshold()).thenReturn(PowerUI.THREE_HOURS_IN_MILLIS);
363 when(mEnhancedEstimates.getSevereWarningThreshold()).thenReturn(ONE_HOUR_MILLIS);
Salvador Martinezf9e47502018-01-04 13:45:48 -0800364 mPowerUI.start();
365
Salvador Martinezfd38aa52018-03-28 23:56:59 -0700366 // Unknown battery status device that would show the neither due to the battery status being
367 // unknown
Salvador Martinezf9e47502018-01-04 13:45:48 -0800368 boolean shouldShow =
369 mPowerUI.shouldShowLowBatteryWarning(UNPLUGGED, UNPLUGGED, ABOVE_WARNING_BUCKET,
Salvador Martinez36307962018-02-08 14:29:08 -0800370 BELOW_WARNING_BUCKET, BELOW_HYBRID_THRESHOLD,
Salvador Martinezf9e47502018-01-04 13:45:48 -0800371 !POWER_SAVER_OFF, BatteryManager.BATTERY_STATUS_UNKNOWN);
372 assertFalse(shouldShow);
373 }
374
375 @Test
376 public void testShouldShowLowBatteryWarning_batterySaverEnabled_returnsNoShow() {
Salvador Martinezbb902fc2018-01-22 19:46:55 -0800377 when(mEnhancedEstimates.isHybridNotificationEnabled()).thenReturn(true);
378 when(mEnhancedEstimates.getLowWarningThreshold()).thenReturn(PowerUI.THREE_HOURS_IN_MILLIS);
379 when(mEnhancedEstimates.getSevereWarningThreshold()).thenReturn(ONE_HOUR_MILLIS);
Salvador Martinezf9e47502018-01-04 13:45:48 -0800380 mPowerUI.start();
381
382 // BatterySaverEnabled device that would show the neither due to battery saver
383 boolean shouldShow =
384 mPowerUI.shouldShowLowBatteryWarning(UNPLUGGED, UNPLUGGED, ABOVE_WARNING_BUCKET,
Salvador Martinez36307962018-02-08 14:29:08 -0800385 BELOW_WARNING_BUCKET, BELOW_HYBRID_THRESHOLD,
Salvador Martinezf9e47502018-01-04 13:45:48 -0800386 !POWER_SAVER_OFF, BatteryManager.BATTERY_HEALTH_GOOD);
387 assertFalse(shouldShow);
388 }
389
390 @Test
Salvador Martinez36307962018-02-08 14:29:08 -0800391 public void testShouldShowLowBatteryWarning_onlyShowsOncePerChargeCycle() {
392 mPowerUI.start();
393 when(mEnhancedEstimates.isHybridNotificationEnabled()).thenReturn(true);
394 when(mEnhancedEstimates.getLowWarningThreshold()).thenReturn(PowerUI.THREE_HOURS_IN_MILLIS);
395 when(mEnhancedEstimates.getSevereWarningThreshold()).thenReturn(ONE_HOUR_MILLIS);
396 when(mEnhancedEstimates.getEstimate())
397 .thenReturn(new Estimate(BELOW_HYBRID_THRESHOLD, true));
398 mPowerUI.mBatteryStatus = BatteryManager.BATTERY_HEALTH_GOOD;
399
Salvador Martinez926f0712018-07-03 18:07:07 -0700400 mPowerUI.maybeShowBatteryWarning(OLD_BATTERY_LEVEL_NINE, UNPLUGGED, UNPLUGGED,
401 ABOVE_WARNING_BUCKET, ABOVE_WARNING_BUCKET);
Salvador Martinezfd38aa52018-03-28 23:56:59 -0700402
403 // reduce battery level to handle time based trigger -> level trigger interactions
404 mPowerUI.mBatteryLevel = 10;
Salvador Martinez36307962018-02-08 14:29:08 -0800405 boolean shouldShow =
406 mPowerUI.shouldShowLowBatteryWarning(UNPLUGGED, UNPLUGGED, ABOVE_WARNING_BUCKET,
407 ABOVE_WARNING_BUCKET, BELOW_HYBRID_THRESHOLD,
408 POWER_SAVER_OFF, BatteryManager.BATTERY_HEALTH_GOOD);
409 assertFalse(shouldShow);
410 }
411
412 @Test
Salvador Martinezc50a9bd2018-08-03 14:07:44 -0700413 public void testShouldDismissLowBatteryWarning_dismissWhenPowerSaverEnabledLegacy() {
Salvador Martinezf9e47502018-01-04 13:45:48 -0800414 mPowerUI.start();
Salvador Martinezc50a9bd2018-08-03 14:07:44 -0700415 when(mEnhancedEstimates.isHybridNotificationEnabled()).thenReturn(false);
Salvador Martinezbb902fc2018-01-22 19:46:55 -0800416 when(mEnhancedEstimates.getLowWarningThreshold()).thenReturn(PowerUI.THREE_HOURS_IN_MILLIS);
417 when(mEnhancedEstimates.getSevereWarningThreshold()).thenReturn(ONE_HOUR_MILLIS);
418
Salvador Martinezf9e47502018-01-04 13:45:48 -0800419 // device that gets power saver turned on should dismiss
420 boolean shouldDismiss =
421 mPowerUI.shouldDismissLowBatteryWarning(UNPLUGGED, BELOW_WARNING_BUCKET,
422 BELOW_WARNING_BUCKET, ABOVE_HYBRID_THRESHOLD, !POWER_SAVER_OFF);
423 assertTrue(shouldDismiss);
424 }
425
426 @Test
Salvador Martinezc50a9bd2018-08-03 14:07:44 -0700427 public void testShouldNotDismissLowBatteryWarning_dismissWhenPowerSaverEnabledHybrid() {
428 mPowerUI.start();
429 when(mEnhancedEstimates.isHybridNotificationEnabled()).thenReturn(true);
430 when(mEnhancedEstimates.getLowWarningThreshold()).thenReturn(PowerUI.THREE_HOURS_IN_MILLIS);
431 when(mEnhancedEstimates.getSevereWarningThreshold()).thenReturn(ONE_HOUR_MILLIS);
432
433 // device that gets power saver turned on should dismiss
434 boolean shouldDismiss =
435 mPowerUI.shouldDismissLowBatteryWarning(UNPLUGGED, BELOW_WARNING_BUCKET,
436 BELOW_WARNING_BUCKET, ABOVE_HYBRID_THRESHOLD, !POWER_SAVER_OFF);
437 assertFalse(shouldDismiss);
438 }
439
440 @Test
Salvador Martinezf9e47502018-01-04 13:45:48 -0800441 public void testShouldDismissLowBatteryWarning_dismissWhenPlugged() {
442 mPowerUI.start();
Salvador Martinezbb902fc2018-01-22 19:46:55 -0800443 when(mEnhancedEstimates.isHybridNotificationEnabled()).thenReturn(true);
444 when(mEnhancedEstimates.getLowWarningThreshold()).thenReturn(PowerUI.THREE_HOURS_IN_MILLIS);
445 when(mEnhancedEstimates.getSevereWarningThreshold()).thenReturn(ONE_HOUR_MILLIS);
Salvador Martinezf9e47502018-01-04 13:45:48 -0800446
447 // device that gets plugged in should dismiss
448 boolean shouldDismiss =
449 mPowerUI.shouldDismissLowBatteryWarning(!UNPLUGGED, BELOW_WARNING_BUCKET,
450 BELOW_WARNING_BUCKET, ABOVE_HYBRID_THRESHOLD, POWER_SAVER_OFF);
451 assertTrue(shouldDismiss);
452 }
453
454 @Test
455 public void testShouldDismissLowBatteryWarning_dismissHybridSignal_showStandardSignal_shouldShow() {
456 mPowerUI.start();
Salvador Martinezbb902fc2018-01-22 19:46:55 -0800457 when(mEnhancedEstimates.isHybridNotificationEnabled()).thenReturn(true);
458 when(mEnhancedEstimates.getLowWarningThreshold()).thenReturn(PowerUI.THREE_HOURS_IN_MILLIS);
459 when(mEnhancedEstimates.getSevereWarningThreshold()).thenReturn(ONE_HOUR_MILLIS);
460
Salvador Martinezf9e47502018-01-04 13:45:48 -0800461 // would dismiss hybrid but not non-hybrid should not dismiss
462 boolean shouldDismiss =
463 mPowerUI.shouldDismissLowBatteryWarning(UNPLUGGED, BELOW_WARNING_BUCKET,
464 BELOW_WARNING_BUCKET, ABOVE_HYBRID_THRESHOLD, POWER_SAVER_OFF);
465 assertFalse(shouldDismiss);
466 }
467
468 @Test
469 public void testShouldDismissLowBatteryWarning_showHybridSignal_dismissStandardSignal_shouldShow() {
470 mPowerUI.start();
Salvador Martinezbb902fc2018-01-22 19:46:55 -0800471 when(mEnhancedEstimates.isHybridNotificationEnabled()).thenReturn(true);
472 when(mEnhancedEstimates.getLowWarningThreshold()).thenReturn(PowerUI.THREE_HOURS_IN_MILLIS);
473 when(mEnhancedEstimates.getSevereWarningThreshold()).thenReturn(ONE_HOUR_MILLIS);
Salvador Martinezf9e47502018-01-04 13:45:48 -0800474
475 // would dismiss non-hybrid but not hybrid should not dismiss
476 boolean shouldDismiss =
477 mPowerUI.shouldDismissLowBatteryWarning(UNPLUGGED, BELOW_WARNING_BUCKET,
478 ABOVE_WARNING_BUCKET, BELOW_HYBRID_THRESHOLD, POWER_SAVER_OFF);
479 assertFalse(shouldDismiss);
480 }
481
482 @Test
483 public void testShouldDismissLowBatteryWarning_showBothSignal_shouldShow() {
484 mPowerUI.start();
Salvador Martinezbb902fc2018-01-22 19:46:55 -0800485 when(mEnhancedEstimates.isHybridNotificationEnabled()).thenReturn(true);
486 when(mEnhancedEstimates.getLowWarningThreshold()).thenReturn(PowerUI.THREE_HOURS_IN_MILLIS);
487 when(mEnhancedEstimates.getSevereWarningThreshold()).thenReturn(ONE_HOUR_MILLIS);
Salvador Martinezf9e47502018-01-04 13:45:48 -0800488
489 // should not dismiss when both would not dismiss
490 boolean shouldDismiss =
491 mPowerUI.shouldDismissLowBatteryWarning(UNPLUGGED, BELOW_WARNING_BUCKET,
492 BELOW_WARNING_BUCKET, BELOW_HYBRID_THRESHOLD, POWER_SAVER_OFF);
493 assertFalse(shouldDismiss);
494 }
495
496 @Test
497 public void testShouldDismissLowBatteryWarning_dismissBothSignal_shouldDismiss() {
498 mPowerUI.start();
Salvador Martinezbb902fc2018-01-22 19:46:55 -0800499 when(mEnhancedEstimates.isHybridNotificationEnabled()).thenReturn(true);
500 when(mEnhancedEstimates.getLowWarningThreshold()).thenReturn(PowerUI.THREE_HOURS_IN_MILLIS);
501 when(mEnhancedEstimates.getSevereWarningThreshold()).thenReturn(ONE_HOUR_MILLIS);
Salvador Martinezf9e47502018-01-04 13:45:48 -0800502
503 //should dismiss if both would dismiss
504 boolean shouldDismiss =
505 mPowerUI.shouldDismissLowBatteryWarning(UNPLUGGED, BELOW_WARNING_BUCKET,
506 ABOVE_WARNING_BUCKET, ABOVE_HYBRID_THRESHOLD, POWER_SAVER_OFF);
507 assertTrue(shouldDismiss);
508 }
509
510 @Test
511 public void testShouldDismissLowBatteryWarning_dismissStandardSignal_hybridDisabled_shouldDismiss() {
512 mPowerUI.start();
Salvador Martinezbb902fc2018-01-22 19:46:55 -0800513 when(mEnhancedEstimates.isHybridNotificationEnabled()).thenReturn(false);
514 when(mEnhancedEstimates.getLowWarningThreshold()).thenReturn(PowerUI.THREE_HOURS_IN_MILLIS);
515 when(mEnhancedEstimates.getSevereWarningThreshold()).thenReturn(ONE_HOUR_MILLIS);
Salvador Martinezf9e47502018-01-04 13:45:48 -0800516
517 // would dismiss non-hybrid with hybrid disabled should dismiss
518 boolean shouldDismiss =
519 mPowerUI.shouldDismissLowBatteryWarning(UNPLUGGED, BELOW_WARNING_BUCKET,
520 ABOVE_WARNING_BUCKET, ABOVE_HYBRID_THRESHOLD, POWER_SAVER_OFF);
521 assertTrue(shouldDismiss);
522 }
523
Amin Shaikh2f6c45c2018-04-16 14:00:09 -0400524 @Test
525 public void testShouldDismissLowBatteryWarning_powerSaverModeEnabled()
526 throws InterruptedException {
527 when(mPowerManager.isPowerSaveMode()).thenReturn(true);
528
529 mPowerUI.start();
530 mPowerUI.mReceiver.onReceive(mContext,
531 new Intent(PowerManager.ACTION_POWER_SAVE_MODE_CHANGED));
532
533 CountDownLatch latch = new CountDownLatch(1);
534 ThreadUtils.postOnBackgroundThread(() -> latch.countDown());
535 latch.await(5, TimeUnit.SECONDS);
536
537 verify(mMockWarnings).dismissLowBatteryWarning();
538 }
539
540 @Test
541 public void testShouldNotDismissLowBatteryWarning_powerSaverModeDisabled()
542 throws InterruptedException {
543 when(mPowerManager.isPowerSaveMode()).thenReturn(false);
544
545 mPowerUI.start();
546 mPowerUI.mReceiver.onReceive(mContext,
547 new Intent(PowerManager.ACTION_POWER_SAVE_MODE_CHANGED));
548
549 CountDownLatch latch = new CountDownLatch(1);
550 ThreadUtils.postOnBackgroundThread(() -> latch.countDown());
551 latch.await(5, TimeUnit.SECONDS);
552
553 verify(mMockWarnings, never()).dismissLowBatteryWarning();
554 }
555
Salvador Martinez926f0712018-07-03 18:07:07 -0700556 @Test
557 public void testMaybeShowBatteryWarning_onlyQueriesEstimateOnBatteryLevelChangeOrNull() {
558 mPowerUI.start();
559 Estimate estimate = new Estimate(BELOW_HYBRID_THRESHOLD, true);
560 when(mEnhancedEstimates.isHybridNotificationEnabled()).thenReturn(true);
561 when(mEnhancedEstimates.getLowWarningThreshold()).thenReturn(PowerUI.THREE_HOURS_IN_MILLIS);
562 when(mEnhancedEstimates.getSevereWarningThreshold()).thenReturn(ONE_HOUR_MILLIS);
563 when(mEnhancedEstimates.getEstimate()).thenReturn(estimate);
564 mPowerUI.mBatteryStatus = BatteryManager.BATTERY_HEALTH_GOOD;
565
566 // we expect that the first time it will query even if the level is the same
567 mPowerUI.mBatteryLevel = 9;
568 mPowerUI.maybeShowBatteryWarning(OLD_BATTERY_LEVEL_NINE, UNPLUGGED, UNPLUGGED,
569 ABOVE_WARNING_BUCKET, ABOVE_WARNING_BUCKET);
570 verify(mEnhancedEstimates, times(1)).getEstimate();
571
572 // We should NOT query again if the battery level hasn't changed
573 mPowerUI.maybeShowBatteryWarning(OLD_BATTERY_LEVEL_NINE, UNPLUGGED, UNPLUGGED,
574 ABOVE_WARNING_BUCKET, ABOVE_WARNING_BUCKET);
575 verify(mEnhancedEstimates, times(1)).getEstimate();
576
577 // Battery level has changed, so we should query again
578 mPowerUI.maybeShowBatteryWarning(OLD_BATTERY_LEVEL_10, UNPLUGGED, UNPLUGGED,
579 ABOVE_WARNING_BUCKET, ABOVE_WARNING_BUCKET);
580 verify(mEnhancedEstimates, times(2)).getEstimate();
581 }
582
Jason Monkd819c312017-08-11 12:53:36 -0400583 private void setCurrentTemp(float temp) {
584 when(mHardProps.getDeviceTemperatures(DEVICE_TEMPERATURE_SKIN, TEMPERATURE_CURRENT))
Bill Lin32ed3d62018-10-02 18:10:09 +0800585 .thenReturn(new float[] { temp, temp });
586 }
587
588 private void setThrottlingThreshold(float temp) {
589 when(mHardProps.getDeviceTemperatures(DEVICE_TEMPERATURE_SKIN, TEMPERATURE_THROTTLING))
590 .thenReturn(new float[] { temp, temp });
Jason Monkd819c312017-08-11 12:53:36 -0400591 }
592
593 private void setOverThreshold() {
594 setCurrentTemp(50000);
595 }
596
597 private void setUnderThreshold() {
598 setCurrentTemp(5);
599 }
600
601 private void createPowerUi() {
602 mPowerUI = new PowerUI();
603 mPowerUI.mContext = mContext;
604 mPowerUI.mComponents = mContext.getComponents();
605 }
606}