blob: a9d49f91e44ea10d63275a580055cf22b063f932 [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;
20import static android.provider.Settings.Global.SHOW_TEMPERATURE_WARNING;
21
Salvador Martinezf9e47502018-01-04 13:45:48 -080022import static junit.framework.Assert.assertFalse;
23import static junit.framework.Assert.assertTrue;
Amin Shaikh2f6c45c2018-04-16 14:00:09 -040024
Jason Monkd819c312017-08-11 12:53:36 -040025import static org.mockito.Mockito.mock;
26import static org.mockito.Mockito.never;
Salvador Martinez926f0712018-07-03 18:07:07 -070027import static org.mockito.Mockito.times;
Jason Monkd819c312017-08-11 12:53:36 -040028import static org.mockito.Mockito.verify;
29import static org.mockito.Mockito.when;
30
31import android.content.Context;
Amin Shaikh2f6c45c2018-04-16 14:00:09 -040032import android.content.Intent;
Salvador Martinezf9e47502018-01-04 13:45:48 -080033import android.os.BatteryManager;
Jason Monkd819c312017-08-11 12:53:36 -040034import android.os.HardwarePropertiesManager;
Amin Shaikh2f6c45c2018-04-16 14:00:09 -040035import android.os.PowerManager;
Jason Monkd819c312017-08-11 12:53:36 -040036import android.provider.Settings;
37import android.testing.AndroidTestingRunner;
38import android.testing.TestableLooper.RunWithLooper;
39import android.testing.TestableResources;
40import android.test.suitebuilder.annotation.SmallTest;
41
Amin Shaikh2f6c45c2018-04-16 14:00:09 -040042import com.android.settingslib.utils.ThreadUtils;
Jason Monkd819c312017-08-11 12:53:36 -040043import com.android.systemui.R;
44import com.android.systemui.SysuiTestCase;
45import com.android.systemui.power.PowerUI.WarningsUI;
46import com.android.systemui.statusbar.phone.StatusBar;
47
Salvador Martinezbb902fc2018-01-22 19:46:55 -080048import java.time.Duration;
Amin Shaikh2f6c45c2018-04-16 14:00:09 -040049import java.util.concurrent.CountDownLatch;
Salvador Martinezf9e47502018-01-04 13:45:48 -080050import java.util.concurrent.TimeUnit;
Jason Monkd819c312017-08-11 12:53:36 -040051import org.junit.Before;
52import org.junit.Test;
53import org.junit.runner.RunWith;
Amin Shaikh2f6c45c2018-04-16 14:00:09 -040054import org.mockito.Mock;
55import org.mockito.MockitoAnnotations;
Jason Monkd819c312017-08-11 12:53:36 -040056
57@RunWith(AndroidTestingRunner.class)
58@RunWithLooper
59@SmallTest
60public class PowerUITest extends SysuiTestCase {
61
Salvador Martinezf9e47502018-01-04 13:45:48 -080062 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 Martinezbb902fc2018-01-22 19:46:55 -080065 private static final long ONE_HOUR_MILLIS = Duration.ofHours(1).toMillis();
Salvador Martinezf9e47502018-01-04 13:45:48 -080066 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 Martinezfd38aa52018-03-28 23:56:59 -070069 private static final long ABOVE_CHARGE_CYCLE_THRESHOLD = Duration.ofHours(8).toMillis();
Salvador Martinez926f0712018-07-03 18:07:07 -070070 private static final int OLD_BATTERY_LEVEL_NINE = 9;
71 private static final int OLD_BATTERY_LEVEL_10 = 10;
Jason Monkd819c312017-08-11 12:53:36 -040072 private HardwarePropertiesManager mHardProps;
73 private WarningsUI mMockWarnings;
74 private PowerUI mPowerUI;
Salvador Martinezbb902fc2018-01-22 19:46:55 -080075 private EnhancedEstimates mEnhancedEstimates;
Amin Shaikh2f6c45c2018-04-16 14:00:09 -040076 @Mock private PowerManager mPowerManager;
Jason Monkd819c312017-08-11 12:53:36 -040077
78 @Before
79 public void setup() {
Amin Shaikh2f6c45c2018-04-16 14:00:09 -040080 MockitoAnnotations.initMocks(this);
Jason Monkd819c312017-08-11 12:53:36 -040081 mMockWarnings = mDependency.injectMockDependency(WarningsUI.class);
Salvador Martinezbb902fc2018-01-22 19:46:55 -080082 mEnhancedEstimates = mDependency.injectMockDependency(EnhancedEstimates.class);
Jason Monkd819c312017-08-11 12:53:36 -040083 mHardProps = mock(HardwarePropertiesManager.class);
Salvador Martinezf9e47502018-01-04 13:45:48 -080084
Jason Monkd819c312017-08-11 12:53:36 -040085 mContext.putComponent(StatusBar.class, mock(StatusBar.class));
86 mContext.addMockSystemService(Context.HARDWARE_PROPERTIES_SERVICE, mHardProps);
Amin Shaikh2f6c45c2018-04-16 14:00:09 -040087 mContext.addMockSystemService(Context.POWER_SERVICE, mPowerManager);
Jason Monkd819c312017-08-11 12:53:36 -040088
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 Martinezf9e47502018-01-04 13:45:48 -0800160 @Test
Salvador Martinezbb902fc2018-01-22 19:46:55 -0800161 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 Martinez36307962018-02-08 14:29:08 -0800172 ABOVE_WARNING_BUCKET, BELOW_HYBRID_THRESHOLD,
Salvador Martinezbb902fc2018-01-22 19:46:55 -0800173 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 Martinez36307962018-02-08 14:29:08 -0800189 ABOVE_WARNING_BUCKET, ABOVE_HYBRID_THRESHOLD,
Salvador Martinezbb902fc2018-01-22 19:46:55 -0800190 POWER_SAVER_OFF, BatteryManager.BATTERY_HEALTH_GOOD);
191 assertTrue(shouldShow);
192 }
193
194 @Test
Salvador Martinezf9e47502018-01-04 13:45:48 -0800195 public void testShouldShowLowBatteryWarning_showHybridOnly_returnsShow() {
Salvador Martinezbb902fc2018-01-22 19:46:55 -0800196 when(mEnhancedEstimates.isHybridNotificationEnabled()).thenReturn(true);
197 when(mEnhancedEstimates.getLowWarningThreshold()).thenReturn(PowerUI.THREE_HOURS_IN_MILLIS);
198 when(mEnhancedEstimates.getSevereWarningThreshold()).thenReturn(ONE_HOUR_MILLIS);
Salvador Martinezf9e47502018-01-04 13:45:48 -0800199 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 Martinez36307962018-02-08 14:29:08 -0800205 ABOVE_WARNING_BUCKET, BELOW_HYBRID_THRESHOLD,
Salvador Martinezf9e47502018-01-04 13:45:48 -0800206 POWER_SAVER_OFF, BatteryManager.BATTERY_HEALTH_GOOD);
207 assertTrue(shouldShow);
208 }
209
210 @Test
211 public void testShouldShowLowBatteryWarning_showHybrid_showStandard_returnsShow() {
Salvador Martinezbb902fc2018-01-22 19:46:55 -0800212 when(mEnhancedEstimates.isHybridNotificationEnabled()).thenReturn(true);
213 when(mEnhancedEstimates.getLowWarningThreshold()).thenReturn(PowerUI.THREE_HOURS_IN_MILLIS);
214 when(mEnhancedEstimates.getSevereWarningThreshold()).thenReturn(ONE_HOUR_MILLIS);
Salvador Martinezfd38aa52018-03-28 23:56:59 -0700215 mPowerUI.mBatteryLevel = 10;
Salvador Martinezf9e47502018-01-04 13:45:48 -0800216 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 Martinez36307962018-02-08 14:29:08 -0800221 BELOW_WARNING_BUCKET, BELOW_HYBRID_THRESHOLD,
Salvador Martinezf9e47502018-01-04 13:45:48 -0800222 POWER_SAVER_OFF, BatteryManager.BATTERY_HEALTH_GOOD);
223 assertTrue(shouldShow);
224 }
225
226 @Test
227 public void testShouldShowLowBatteryWarning_showStandardOnly_returnsShow() {
Salvador Martinezbb902fc2018-01-22 19:46:55 -0800228 when(mEnhancedEstimates.isHybridNotificationEnabled()).thenReturn(true);
229 when(mEnhancedEstimates.getLowWarningThreshold()).thenReturn(PowerUI.THREE_HOURS_IN_MILLIS);
230 when(mEnhancedEstimates.getSevereWarningThreshold()).thenReturn(ONE_HOUR_MILLIS);
Salvador Martinezfd38aa52018-03-28 23:56:59 -0700231 mPowerUI.mBatteryLevel = 10;
Salvador Martinezf9e47502018-01-04 13:45:48 -0800232 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 Martinez36307962018-02-08 14:29:08 -0800237 BELOW_WARNING_BUCKET, ABOVE_HYBRID_THRESHOLD,
Salvador Martinezf9e47502018-01-04 13:45:48 -0800238 POWER_SAVER_OFF, BatteryManager.BATTERY_HEALTH_GOOD);
239 assertTrue(shouldShow);
240 }
241
242 @Test
243 public void testShouldShowLowBatteryWarning_deviceHighBattery_returnsNoShow() {
Salvador Martinezbb902fc2018-01-22 19:46:55 -0800244 when(mEnhancedEstimates.isHybridNotificationEnabled()).thenReturn(true);
245 when(mEnhancedEstimates.getLowWarningThreshold()).thenReturn(PowerUI.THREE_HOURS_IN_MILLIS);
246 when(mEnhancedEstimates.getSevereWarningThreshold()).thenReturn(ONE_HOUR_MILLIS);
Salvador Martinezf9e47502018-01-04 13:45:48 -0800247 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 Martinez36307962018-02-08 14:29:08 -0800252 ABOVE_WARNING_BUCKET, ABOVE_HYBRID_THRESHOLD,
Salvador Martinezf9e47502018-01-04 13:45:48 -0800253 POWER_SAVER_OFF, BatteryManager.BATTERY_HEALTH_GOOD);
254 assertFalse(shouldShow);
255 }
256
257 @Test
258 public void testShouldShowLowBatteryWarning_devicePlugged_returnsNoShow() {
Salvador Martinezbb902fc2018-01-22 19:46:55 -0800259 when(mEnhancedEstimates.isHybridNotificationEnabled()).thenReturn(true);
260 when(mEnhancedEstimates.getLowWarningThreshold()).thenReturn(PowerUI.THREE_HOURS_IN_MILLIS);
261 when(mEnhancedEstimates.getSevereWarningThreshold()).thenReturn(ONE_HOUR_MILLIS);
Salvador Martinezf9e47502018-01-04 13:45:48 -0800262 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 Martinez36307962018-02-08 14:29:08 -0800267 BELOW_WARNING_BUCKET, BELOW_HYBRID_THRESHOLD,
Salvador Martinezf9e47502018-01-04 13:45:48 -0800268 POWER_SAVER_OFF, BatteryManager.BATTERY_HEALTH_GOOD);
269 assertFalse(shouldShow);
270 }
271
272 @Test
Salvador Martinezfd38aa52018-03-28 23:56:59 -0700273 public void testShouldShowLowBatteryWarning_deviceBatteryStatusUnknown_returnsNoShow() {
Salvador Martinezbb902fc2018-01-22 19:46:55 -0800274 when(mEnhancedEstimates.isHybridNotificationEnabled()).thenReturn(true);
275 when(mEnhancedEstimates.getLowWarningThreshold()).thenReturn(PowerUI.THREE_HOURS_IN_MILLIS);
276 when(mEnhancedEstimates.getSevereWarningThreshold()).thenReturn(ONE_HOUR_MILLIS);
Salvador Martinezf9e47502018-01-04 13:45:48 -0800277 mPowerUI.start();
278
Salvador Martinezfd38aa52018-03-28 23:56:59 -0700279 // Unknown battery status device that would show the neither due to the battery status being
280 // unknown
Salvador Martinezf9e47502018-01-04 13:45:48 -0800281 boolean shouldShow =
282 mPowerUI.shouldShowLowBatteryWarning(UNPLUGGED, UNPLUGGED, ABOVE_WARNING_BUCKET,
Salvador Martinez36307962018-02-08 14:29:08 -0800283 BELOW_WARNING_BUCKET, BELOW_HYBRID_THRESHOLD,
Salvador Martinezf9e47502018-01-04 13:45:48 -0800284 !POWER_SAVER_OFF, BatteryManager.BATTERY_STATUS_UNKNOWN);
285 assertFalse(shouldShow);
286 }
287
288 @Test
289 public void testShouldShowLowBatteryWarning_batterySaverEnabled_returnsNoShow() {
Salvador Martinezbb902fc2018-01-22 19:46:55 -0800290 when(mEnhancedEstimates.isHybridNotificationEnabled()).thenReturn(true);
291 when(mEnhancedEstimates.getLowWarningThreshold()).thenReturn(PowerUI.THREE_HOURS_IN_MILLIS);
292 when(mEnhancedEstimates.getSevereWarningThreshold()).thenReturn(ONE_HOUR_MILLIS);
Salvador Martinezf9e47502018-01-04 13:45:48 -0800293 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 Martinez36307962018-02-08 14:29:08 -0800298 BELOW_WARNING_BUCKET, BELOW_HYBRID_THRESHOLD,
Salvador Martinezf9e47502018-01-04 13:45:48 -0800299 !POWER_SAVER_OFF, BatteryManager.BATTERY_HEALTH_GOOD);
300 assertFalse(shouldShow);
301 }
302
303 @Test
Salvador Martinez36307962018-02-08 14:29:08 -0800304 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 Martinez926f0712018-07-03 18:07:07 -0700313 mPowerUI.maybeShowBatteryWarning(OLD_BATTERY_LEVEL_NINE, UNPLUGGED, UNPLUGGED,
314 ABOVE_WARNING_BUCKET, ABOVE_WARNING_BUCKET);
Salvador Martinezfd38aa52018-03-28 23:56:59 -0700315
316 // reduce battery level to handle time based trigger -> level trigger interactions
317 mPowerUI.mBatteryLevel = 10;
Salvador Martinez36307962018-02-08 14:29:08 -0800318 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 Martinezc50a9bd2018-08-03 14:07:44 -0700326 public void testShouldDismissLowBatteryWarning_dismissWhenPowerSaverEnabledLegacy() {
Salvador Martinezf9e47502018-01-04 13:45:48 -0800327 mPowerUI.start();
Salvador Martinezc50a9bd2018-08-03 14:07:44 -0700328 when(mEnhancedEstimates.isHybridNotificationEnabled()).thenReturn(false);
Salvador Martinezbb902fc2018-01-22 19:46:55 -0800329 when(mEnhancedEstimates.getLowWarningThreshold()).thenReturn(PowerUI.THREE_HOURS_IN_MILLIS);
330 when(mEnhancedEstimates.getSevereWarningThreshold()).thenReturn(ONE_HOUR_MILLIS);
331
Salvador Martinezf9e47502018-01-04 13:45:48 -0800332 // 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 Martinezc50a9bd2018-08-03 14:07:44 -0700340 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 Martinezf9e47502018-01-04 13:45:48 -0800354 public void testShouldDismissLowBatteryWarning_dismissWhenPlugged() {
355 mPowerUI.start();
Salvador Martinezbb902fc2018-01-22 19:46:55 -0800356 when(mEnhancedEstimates.isHybridNotificationEnabled()).thenReturn(true);
357 when(mEnhancedEstimates.getLowWarningThreshold()).thenReturn(PowerUI.THREE_HOURS_IN_MILLIS);
358 when(mEnhancedEstimates.getSevereWarningThreshold()).thenReturn(ONE_HOUR_MILLIS);
Salvador Martinezf9e47502018-01-04 13:45:48 -0800359
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 Martinezbb902fc2018-01-22 19:46:55 -0800370 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 Martinezf9e47502018-01-04 13:45:48 -0800374 // 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 Martinezbb902fc2018-01-22 19:46:55 -0800384 when(mEnhancedEstimates.isHybridNotificationEnabled()).thenReturn(true);
385 when(mEnhancedEstimates.getLowWarningThreshold()).thenReturn(PowerUI.THREE_HOURS_IN_MILLIS);
386 when(mEnhancedEstimates.getSevereWarningThreshold()).thenReturn(ONE_HOUR_MILLIS);
Salvador Martinezf9e47502018-01-04 13:45:48 -0800387
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 Martinezbb902fc2018-01-22 19:46:55 -0800398 when(mEnhancedEstimates.isHybridNotificationEnabled()).thenReturn(true);
399 when(mEnhancedEstimates.getLowWarningThreshold()).thenReturn(PowerUI.THREE_HOURS_IN_MILLIS);
400 when(mEnhancedEstimates.getSevereWarningThreshold()).thenReturn(ONE_HOUR_MILLIS);
Salvador Martinezf9e47502018-01-04 13:45:48 -0800401
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 Martinezbb902fc2018-01-22 19:46:55 -0800412 when(mEnhancedEstimates.isHybridNotificationEnabled()).thenReturn(true);
413 when(mEnhancedEstimates.getLowWarningThreshold()).thenReturn(PowerUI.THREE_HOURS_IN_MILLIS);
414 when(mEnhancedEstimates.getSevereWarningThreshold()).thenReturn(ONE_HOUR_MILLIS);
Salvador Martinezf9e47502018-01-04 13:45:48 -0800415
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 Martinezbb902fc2018-01-22 19:46:55 -0800426 when(mEnhancedEstimates.isHybridNotificationEnabled()).thenReturn(false);
427 when(mEnhancedEstimates.getLowWarningThreshold()).thenReturn(PowerUI.THREE_HOURS_IN_MILLIS);
428 when(mEnhancedEstimates.getSevereWarningThreshold()).thenReturn(ONE_HOUR_MILLIS);
Salvador Martinezf9e47502018-01-04 13:45:48 -0800429
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 Shaikh2f6c45c2018-04-16 14:00:09 -0400437 @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 Martinez926f0712018-07-03 18:07:07 -0700469 @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 Monkd819c312017-08-11 12:53:36 -0400496 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}