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