Geoffrey Pitsch | 4a7931d | 2016-09-15 13:11:47 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.systemui.power; |
| 18 | |
Bill Lin | 32ed3d6 | 2018-10-02 18:10:09 +0800 | [diff] [blame] | 19 | import static com.google.common.truth.Truth.assertThat; |
Jason Monk | e978928 | 2016-11-09 08:59:56 -0500 | [diff] [blame] | 20 | |
Geoffrey Pitsch | 4a7931d | 2016-09-15 13:11:47 -0400 | [diff] [blame] | 21 | import static junit.framework.Assert.assertFalse; |
| 22 | import static junit.framework.Assert.assertTrue; |
Jason Monk | e978928 | 2016-11-09 08:59:56 -0500 | [diff] [blame] | 23 | |
Chris Wren | 5e6c0ff | 2017-01-05 12:57:06 -0500 | [diff] [blame] | 24 | import static org.mockito.Matchers.eq; |
Geoffrey Pitsch | 4a7931d | 2016-09-15 13:11:47 -0400 | [diff] [blame] | 25 | import static org.mockito.Mockito.any; |
Geoffrey Pitsch | 4a7931d | 2016-09-15 13:11:47 -0400 | [diff] [blame] | 26 | import static org.mockito.Mockito.anyString; |
| 27 | import static org.mockito.Mockito.mock; |
Bill Lin | 32ed3d6 | 2018-10-02 18:10:09 +0800 | [diff] [blame] | 28 | import static org.mockito.Mockito.never; |
| 29 | import static org.mockito.Mockito.spy; |
Geoffrey Pitsch | 4a7931d | 2016-09-15 13:11:47 -0400 | [diff] [blame] | 30 | import static org.mockito.Mockito.times; |
| 31 | import static org.mockito.Mockito.verify; |
| 32 | |
| 33 | import android.app.Notification; |
| 34 | import android.app.NotificationManager; |
Geoffrey Pitsch | 4a7931d | 2016-09-15 13:11:47 -0400 | [diff] [blame] | 35 | import android.test.suitebuilder.annotation.SmallTest; |
Jason Monk | e978928 | 2016-11-09 08:59:56 -0500 | [diff] [blame] | 36 | |
Brett Chabot | 8091d9e | 2019-02-26 14:52:33 -0800 | [diff] [blame] | 37 | import androidx.test.runner.AndroidJUnit4; |
| 38 | |
Chris Wren | 5e6c0ff | 2017-01-05 12:57:06 -0500 | [diff] [blame] | 39 | import com.android.internal.messages.nano.SystemMessageProto.SystemMessage; |
Jason Monk | e978928 | 2016-11-09 08:59:56 -0500 | [diff] [blame] | 40 | import com.android.systemui.SysuiTestCase; |
Beverly | 334bc5f | 2017-07-31 10:37:17 -0400 | [diff] [blame] | 41 | import com.android.systemui.util.NotificationChannels; |
Jason Monk | e978928 | 2016-11-09 08:59:56 -0500 | [diff] [blame] | 42 | |
Bill Lin | 32ed3d6 | 2018-10-02 18:10:09 +0800 | [diff] [blame] | 43 | import org.junit.After; |
Geoffrey Pitsch | 4a7931d | 2016-09-15 13:11:47 -0400 | [diff] [blame] | 44 | import org.junit.Before; |
| 45 | import org.junit.Test; |
| 46 | import org.junit.runner.RunWith; |
| 47 | import org.mockito.ArgumentCaptor; |
| 48 | |
| 49 | @SmallTest |
| 50 | @RunWith(AndroidJUnit4.class) |
Jason Monk | e978928 | 2016-11-09 08:59:56 -0500 | [diff] [blame] | 51 | public class PowerNotificationWarningsTest extends SysuiTestCase { |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 52 | |
| 53 | public static final String FORMATTED_45M = "0h 45m"; |
| 54 | public static final String FORMATTED_HOUR = "1h 0m"; |
Geoffrey Pitsch | 4a7931d | 2016-09-15 13:11:47 -0400 | [diff] [blame] | 55 | private final NotificationManager mMockNotificationManager = mock(NotificationManager.class); |
Bill Lin | 32ed3d6 | 2018-10-02 18:10:09 +0800 | [diff] [blame] | 56 | private PowerNotificationWarnings mPowerNotificationWarnings, mSpyPowerNotificationWarnings; |
Geoffrey Pitsch | 4a7931d | 2016-09-15 13:11:47 -0400 | [diff] [blame] | 57 | |
| 58 | @Before |
| 59 | public void setUp() throws Exception { |
| 60 | // Test Instance. |
Jason Monk | d819c31 | 2017-08-11 12:53:36 -0400 | [diff] [blame] | 61 | mContext.addMockSystemService(NotificationManager.class, mMockNotificationManager); |
| 62 | mPowerNotificationWarnings = new PowerNotificationWarnings(mContext); |
Bill Lin | 32ed3d6 | 2018-10-02 18:10:09 +0800 | [diff] [blame] | 63 | mSpyPowerNotificationWarnings = spy(mPowerNotificationWarnings); |
| 64 | } |
| 65 | |
| 66 | @After |
| 67 | public void tearDown() throws Exception { |
| 68 | if (mSpyPowerNotificationWarnings.mOverheatAlarmDialog != null) { |
| 69 | mSpyPowerNotificationWarnings.mOverheatAlarmDialog.dismiss(); |
| 70 | mSpyPowerNotificationWarnings.mOverheatAlarmDialog = null; |
| 71 | } |
Geoffrey Pitsch | 4a7931d | 2016-09-15 13:11:47 -0400 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | @Test |
| 75 | public void testIsInvalidChargerWarningShowing_DefaultsToFalse() { |
| 76 | assertFalse(mPowerNotificationWarnings.isInvalidChargerWarningShowing()); |
| 77 | } |
| 78 | |
| 79 | @Test |
| 80 | public void testIsInvalidChargerWarningShowing_TrueAfterShow() { |
| 81 | mPowerNotificationWarnings.showInvalidChargerWarning(); |
| 82 | assertTrue(mPowerNotificationWarnings.isInvalidChargerWarningShowing()); |
| 83 | } |
| 84 | |
| 85 | @Test |
| 86 | public void testIsInvalidChargerWarningShowing_FalseAfterDismiss() { |
| 87 | mPowerNotificationWarnings.showInvalidChargerWarning(); |
| 88 | mPowerNotificationWarnings.dismissInvalidChargerWarning(); |
| 89 | assertFalse(mPowerNotificationWarnings.isInvalidChargerWarningShowing()); |
| 90 | } |
| 91 | |
| 92 | @Test |
| 93 | public void testShowInvalidChargerNotification_NotifyAsUser() { |
| 94 | mPowerNotificationWarnings.showInvalidChargerWarning(); |
| 95 | verify(mMockNotificationManager, times(1)) |
Chris Wren | 5e6c0ff | 2017-01-05 12:57:06 -0500 | [diff] [blame] | 96 | .notifyAsUser(anyString(), eq(SystemMessage.NOTE_BAD_CHARGER), any(), any()); |
| 97 | verify(mMockNotificationManager, times(1)).cancelAsUser(anyString(), |
| 98 | eq(SystemMessage.NOTE_POWER_LOW), any()); |
Geoffrey Pitsch | 4a7931d | 2016-09-15 13:11:47 -0400 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | @Test |
| 102 | public void testDismissInvalidChargerNotification_CancelAsUser() { |
| 103 | mPowerNotificationWarnings.showInvalidChargerWarning(); |
| 104 | mPowerNotificationWarnings.dismissInvalidChargerWarning(); |
Chris Wren | 5e6c0ff | 2017-01-05 12:57:06 -0500 | [diff] [blame] | 105 | verify(mMockNotificationManager, times(1)).cancelAsUser(anyString(), |
| 106 | eq(SystemMessage.NOTE_BAD_CHARGER), any()); |
Geoffrey Pitsch | 4a7931d | 2016-09-15 13:11:47 -0400 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | @Test |
| 110 | public void testShowLowBatteryNotification_NotifyAsUser() { |
| 111 | mPowerNotificationWarnings.showLowBatteryWarning(false); |
| 112 | verify(mMockNotificationManager, times(1)) |
Chris Wren | 5e6c0ff | 2017-01-05 12:57:06 -0500 | [diff] [blame] | 113 | .notifyAsUser(anyString(), eq(SystemMessage.NOTE_POWER_LOW), any(), any()); |
| 114 | verify(mMockNotificationManager, times(1)).cancelAsUser(anyString(), |
| 115 | eq(SystemMessage.NOTE_BAD_CHARGER), any()); |
Geoffrey Pitsch | 4a7931d | 2016-09-15 13:11:47 -0400 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | @Test |
| 119 | public void testDismissLowBatteryNotification_CancelAsUser() { |
| 120 | mPowerNotificationWarnings.showLowBatteryWarning(false); |
| 121 | mPowerNotificationWarnings.dismissLowBatteryWarning(); |
Chris Wren | 5e6c0ff | 2017-01-05 12:57:06 -0500 | [diff] [blame] | 122 | verify(mMockNotificationManager, times(1)).cancelAsUser(anyString(), |
| 123 | eq(SystemMessage.NOTE_POWER_LOW), any()); |
Geoffrey Pitsch | 4a7931d | 2016-09-15 13:11:47 -0400 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | @Test |
Beverly | 334bc5f | 2017-07-31 10:37:17 -0400 | [diff] [blame] | 127 | public void testShowLowBatteryNotification_BatteryChannel() { |
Geoffrey Pitsch | 4a7931d | 2016-09-15 13:11:47 -0400 | [diff] [blame] | 128 | mPowerNotificationWarnings.showLowBatteryWarning(true); |
| 129 | ArgumentCaptor<Notification> captor = ArgumentCaptor.forClass(Notification.class); |
| 130 | verify(mMockNotificationManager) |
Chris Wren | 5e6c0ff | 2017-01-05 12:57:06 -0500 | [diff] [blame] | 131 | .notifyAsUser(anyString(), eq(SystemMessage.NOTE_POWER_LOW), |
| 132 | captor.capture(), any()); |
Beverly | 334bc5f | 2017-07-31 10:37:17 -0400 | [diff] [blame] | 133 | assertTrue(captor.getValue().getChannelId() == NotificationChannels.BATTERY); |
Geoffrey Pitsch | 4a7931d | 2016-09-15 13:11:47 -0400 | [diff] [blame] | 134 | } |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 135 | |
| 136 | @Test |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 137 | public void testShowHighTemperatureWarning_NotifyAsUser() { |
| 138 | mPowerNotificationWarnings.showHighTemperatureWarning(); |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 139 | verify(mMockNotificationManager, times(1)) |
Chris Wren | 5e6c0ff | 2017-01-05 12:57:06 -0500 | [diff] [blame] | 140 | .notifyAsUser(anyString(), eq(SystemMessage.NOTE_HIGH_TEMP), any(), any()); |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | @Test |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 144 | public void testDismissHighTemperatureWarning_CancelAsUser() { |
| 145 | mPowerNotificationWarnings.showHighTemperatureWarning(); |
| 146 | mPowerNotificationWarnings.dismissHighTemperatureWarning(); |
Chris Wren | 5e6c0ff | 2017-01-05 12:57:06 -0500 | [diff] [blame] | 147 | verify(mMockNotificationManager, times(1)).cancelAsUser(anyString(), |
| 148 | eq(SystemMessage.NOTE_HIGH_TEMP), any()); |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 149 | } |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 150 | |
| 151 | @Test |
| 152 | public void testShowThermalShutdownWarning_NotifyAsUser() { |
| 153 | mPowerNotificationWarnings.showThermalShutdownWarning(); |
| 154 | verify(mMockNotificationManager, times(1)) |
| 155 | .notifyAsUser(anyString(), eq(SystemMessage.NOTE_THERMAL_SHUTDOWN), any(), any()); |
| 156 | } |
| 157 | |
| 158 | @Test |
| 159 | public void testDismissThermalShutdownWarning_CancelAsUser() { |
| 160 | mPowerNotificationWarnings.showThermalShutdownWarning(); |
| 161 | mPowerNotificationWarnings.dismissThermalShutdownWarning(); |
| 162 | verify(mMockNotificationManager, times(1)).cancelAsUser(anyString(), |
| 163 | eq(SystemMessage.NOTE_THERMAL_SHUTDOWN), any()); |
| 164 | } |
Bill Lin | 32ed3d6 | 2018-10-02 18:10:09 +0800 | [diff] [blame] | 165 | |
| 166 | @Test |
| 167 | public void testSetOverheatAlarmDialog_Overheat_ShouldShowing() { |
| 168 | final boolean overheat = true; |
| 169 | final boolean shouldBeepSound = false; |
| 170 | mContext.getMainThreadHandler().post( |
| 171 | () -> mSpyPowerNotificationWarnings.notifyHighTemperatureAlarm(overheat, |
| 172 | shouldBeepSound)); |
| 173 | waitForIdleSync(); |
| 174 | verify(mSpyPowerNotificationWarnings, times(1)).setOverheatAlarmDialogShowing(overheat); |
| 175 | verify(mSpyPowerNotificationWarnings, times(1)).setAlarmShouldSound(shouldBeepSound); |
| 176 | } |
| 177 | |
| 178 | @Test |
| 179 | public void testSetOverheatAlarmDialog_Overheat_ShouldShowingWithBeepSound() { |
| 180 | final boolean overheat = true; |
| 181 | final boolean shouldBeepSound = true; |
| 182 | mContext.getMainThreadHandler().post( |
| 183 | () -> mSpyPowerNotificationWarnings.notifyHighTemperatureAlarm(overheat, |
| 184 | shouldBeepSound)); |
| 185 | waitForIdleSync(); |
| 186 | verify(mSpyPowerNotificationWarnings, times(1)).setOverheatAlarmDialogShowing(overheat); |
| 187 | verify(mSpyPowerNotificationWarnings, times(1)).setAlarmShouldSound(shouldBeepSound); |
| 188 | } |
| 189 | |
| 190 | @Test |
| 191 | public void testSetOverheatAlarmDialog_NotOverheat_ShouldNotShowing() { |
| 192 | final boolean overheat = false; |
| 193 | final boolean shouldBeepSound = false; |
| 194 | mContext.getMainThreadHandler().post( |
| 195 | () -> mSpyPowerNotificationWarnings.notifyHighTemperatureAlarm(overheat, |
| 196 | shouldBeepSound)); |
| 197 | waitForIdleSync(); |
| 198 | verify(mSpyPowerNotificationWarnings, never()).setOverheatAlarmDialogShowing(overheat); |
| 199 | verify(mSpyPowerNotificationWarnings, never()).setAlarmShouldSound(shouldBeepSound); |
| 200 | } |
| 201 | |
| 202 | @Test |
| 203 | public void testSetOverheatAlarmDialog_NotOverheat_ShouldNotAlarmBeepSound() { |
| 204 | final boolean overheat = false; |
| 205 | final boolean configBeepSound = true; |
| 206 | mContext.getMainThreadHandler().post( |
| 207 | () -> mSpyPowerNotificationWarnings.notifyHighTemperatureAlarm(overheat, |
| 208 | configBeepSound)); |
| 209 | waitForIdleSync(); |
| 210 | verify(mSpyPowerNotificationWarnings, never()).setOverheatAlarmDialogShowing(overheat); |
| 211 | verify(mSpyPowerNotificationWarnings, never()).setAlarmShouldSound(configBeepSound); |
| 212 | } |
| 213 | |
| 214 | @Test |
| 215 | public void testSetAlarmShouldSound_OverheatDrop_ShouldNotSound() { |
| 216 | final boolean overheat = true; |
| 217 | final boolean shouldBeepSound = true; |
| 218 | mContext.getMainThreadHandler().post( |
| 219 | () -> mSpyPowerNotificationWarnings.notifyHighTemperatureAlarm(overheat, |
| 220 | shouldBeepSound)); |
| 221 | waitForIdleSync(); |
| 222 | // First time overheat, show overheat alarm dialog with alarm beep sound |
| 223 | verify(mSpyPowerNotificationWarnings, times(1)).setOverheatAlarmDialogShowing(overheat); |
| 224 | verify(mSpyPowerNotificationWarnings, times(1)).setAlarmShouldSound(shouldBeepSound); |
| 225 | |
| 226 | // After disconnected cable or temperature drop |
| 227 | mContext.getMainThreadHandler().post( |
| 228 | () -> mSpyPowerNotificationWarnings.notifyHighTemperatureAlarm(!overheat, |
| 229 | !shouldBeepSound)); |
| 230 | waitForIdleSync(); |
| 231 | verify(mSpyPowerNotificationWarnings, times(1)).setOverheatAlarmDialogShowing(!overheat); |
| 232 | verify(mSpyPowerNotificationWarnings, times(1)).setAlarmShouldSound(!shouldBeepSound); |
| 233 | } |
| 234 | |
| 235 | @Test |
| 236 | public void testSetAlarmShouldSound_Overheat_Twice_ShouldShowOverheatDialogAgain() { |
| 237 | final boolean overheat = true; |
| 238 | final boolean shouldBeepSound = true; |
| 239 | // First time overheat, show mAlarmDialog and alarm beep sound |
| 240 | mContext.getMainThreadHandler().post( |
| 241 | () -> mSpyPowerNotificationWarnings.notifyHighTemperatureAlarm(overheat, |
| 242 | shouldBeepSound)); |
| 243 | waitForIdleSync(); |
| 244 | verify(mSpyPowerNotificationWarnings, times(1)).setOverheatAlarmDialogShowing(overheat); |
| 245 | verify(mSpyPowerNotificationWarnings, times(1)).setAlarmShouldSound(shouldBeepSound); |
| 246 | |
| 247 | // After disconnected cable or temperature drop, stop beep sound |
| 248 | mContext.getMainThreadHandler().post( |
| 249 | () -> mSpyPowerNotificationWarnings.notifyHighTemperatureAlarm(!overheat, |
| 250 | !shouldBeepSound)); |
| 251 | waitForIdleSync(); |
| 252 | verify(mSpyPowerNotificationWarnings, times(1)).setOverheatAlarmDialogShowing(!overheat); |
| 253 | verify(mSpyPowerNotificationWarnings, times(1)).setAlarmShouldSound(!shouldBeepSound); |
| 254 | |
| 255 | // Overheat again, ensure the previous dialog do not auto-dismiss |
| 256 | mContext.getMainThreadHandler().post( |
| 257 | () -> mSpyPowerNotificationWarnings.notifyHighTemperatureAlarm(overheat, |
| 258 | shouldBeepSound)); |
| 259 | waitForIdleSync(); |
| 260 | verify(mSpyPowerNotificationWarnings, times(1)).setOverheatAlarmDialogShowing(overheat); |
| 261 | verify(mSpyPowerNotificationWarnings, times(1)).setAlarmShouldSound(shouldBeepSound); |
| 262 | } |
| 263 | |
| 264 | @Test |
| 265 | public void testOverheatAlarmDialogShowing() { |
| 266 | final boolean overheat = true; |
| 267 | final boolean shouldBeepSound = false; |
| 268 | mContext.getMainThreadHandler().post( |
| 269 | () -> mSpyPowerNotificationWarnings.notifyHighTemperatureAlarm(overheat, |
| 270 | shouldBeepSound)); |
| 271 | waitForIdleSync(); |
| 272 | assertThat(mSpyPowerNotificationWarnings.mOverheatAlarmDialog).isNotNull(); |
| 273 | } |
| 274 | |
| 275 | @Test |
| 276 | public void testOverheatAlarmDialogShowingWithBeepSound() { |
| 277 | final boolean overheat = true; |
| 278 | final boolean shouldBeepSound = true; |
| 279 | mContext.getMainThreadHandler().post( |
| 280 | () -> mSpyPowerNotificationWarnings.notifyHighTemperatureAlarm(overheat, |
| 281 | shouldBeepSound)); |
| 282 | waitForIdleSync(); |
| 283 | assertThat(mSpyPowerNotificationWarnings.mOverheatAlarmDialog).isNotNull(); |
| 284 | } |
| 285 | |
| 286 | @Test |
| 287 | public void testOverheatAlarmDialogNotShowing() { |
| 288 | final boolean overheat = false; |
| 289 | final boolean shouldBeepSound = false; |
| 290 | mContext.getMainThreadHandler().post( |
| 291 | () -> mSpyPowerNotificationWarnings.notifyHighTemperatureAlarm(overheat, |
| 292 | shouldBeepSound)); |
| 293 | waitForIdleSync(); |
| 294 | assertThat(mSpyPowerNotificationWarnings.mOverheatAlarmDialog).isNull(); |
| 295 | } |
| 296 | |
| 297 | @Test |
| 298 | public void testOverheatAlarmDialogNotShowingWithBeepSound() { |
| 299 | final boolean overheat = false; |
| 300 | final boolean shouldBeepSound = true; |
| 301 | mContext.getMainThreadHandler().post( |
| 302 | () -> mSpyPowerNotificationWarnings.notifyHighTemperatureAlarm(overheat, |
| 303 | shouldBeepSound)); |
| 304 | waitForIdleSync(); |
| 305 | assertThat(mSpyPowerNotificationWarnings.mOverheatAlarmDialog).isNull(); |
| 306 | } |
Geoffrey Pitsch | 4a7931d | 2016-09-15 13:11:47 -0400 | [diff] [blame] | 307 | } |