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 | |
Sherry Huang | ce02ed3 | 2019-01-17 20:37:29 +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; |
| 28 | import static org.mockito.Mockito.times; |
| 29 | import static org.mockito.Mockito.verify; |
| 30 | |
| 31 | import android.app.Notification; |
| 32 | import android.app.NotificationManager; |
Salvador Martinez | 4387bd5 | 2019-02-21 16:16:28 -0800 | [diff] [blame] | 33 | import android.os.BatteryManager; |
Geoffrey Pitsch | 4a7931d | 2016-09-15 13:11:47 -0400 | [diff] [blame] | 34 | import android.test.suitebuilder.annotation.SmallTest; |
Jason Monk | e978928 | 2016-11-09 08:59:56 -0500 | [diff] [blame] | 35 | |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 36 | import androidx.test.runner.AndroidJUnit4; |
| 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 | |
Geoffrey Pitsch | 4a7931d | 2016-09-15 13:11:47 -0400 | [diff] [blame] | 42 | import org.junit.Before; |
| 43 | import org.junit.Test; |
| 44 | import org.junit.runner.RunWith; |
| 45 | import org.mockito.ArgumentCaptor; |
| 46 | |
| 47 | @SmallTest |
| 48 | @RunWith(AndroidJUnit4.class) |
Jason Monk | e978928 | 2016-11-09 08:59:56 -0500 | [diff] [blame] | 49 | public class PowerNotificationWarningsTest extends SysuiTestCase { |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 50 | |
| 51 | public static final String FORMATTED_45M = "0h 45m"; |
| 52 | public static final String FORMATTED_HOUR = "1h 0m"; |
Geoffrey Pitsch | 4a7931d | 2016-09-15 13:11:47 -0400 | [diff] [blame] | 53 | private final NotificationManager mMockNotificationManager = mock(NotificationManager.class); |
| 54 | private PowerNotificationWarnings mPowerNotificationWarnings; |
| 55 | |
| 56 | @Before |
| 57 | public void setUp() throws Exception { |
| 58 | // Test Instance. |
Jason Monk | d819c31 | 2017-08-11 12:53:36 -0400 | [diff] [blame] | 59 | mContext.addMockSystemService(NotificationManager.class, mMockNotificationManager); |
| 60 | mPowerNotificationWarnings = new PowerNotificationWarnings(mContext); |
Salvador Martinez | 4387bd5 | 2019-02-21 16:16:28 -0800 | [diff] [blame] | 61 | BatteryStateSnapshot snapshot = new BatteryStateSnapshot(100, false, false, 1, |
| 62 | BatteryManager.BATTERY_HEALTH_GOOD, 5, 15); |
| 63 | mPowerNotificationWarnings.updateSnapshot(snapshot); |
Geoffrey Pitsch | 4a7931d | 2016-09-15 13:11:47 -0400 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | @Test |
| 67 | public void testIsInvalidChargerWarningShowing_DefaultsToFalse() { |
| 68 | assertFalse(mPowerNotificationWarnings.isInvalidChargerWarningShowing()); |
| 69 | } |
| 70 | |
| 71 | @Test |
| 72 | public void testIsInvalidChargerWarningShowing_TrueAfterShow() { |
| 73 | mPowerNotificationWarnings.showInvalidChargerWarning(); |
| 74 | assertTrue(mPowerNotificationWarnings.isInvalidChargerWarningShowing()); |
| 75 | } |
| 76 | |
| 77 | @Test |
| 78 | public void testIsInvalidChargerWarningShowing_FalseAfterDismiss() { |
| 79 | mPowerNotificationWarnings.showInvalidChargerWarning(); |
| 80 | mPowerNotificationWarnings.dismissInvalidChargerWarning(); |
| 81 | assertFalse(mPowerNotificationWarnings.isInvalidChargerWarningShowing()); |
| 82 | } |
| 83 | |
| 84 | @Test |
| 85 | public void testShowInvalidChargerNotification_NotifyAsUser() { |
| 86 | mPowerNotificationWarnings.showInvalidChargerWarning(); |
| 87 | verify(mMockNotificationManager, times(1)) |
Chris Wren | 5e6c0ff | 2017-01-05 12:57:06 -0500 | [diff] [blame] | 88 | .notifyAsUser(anyString(), eq(SystemMessage.NOTE_BAD_CHARGER), any(), any()); |
| 89 | verify(mMockNotificationManager, times(1)).cancelAsUser(anyString(), |
| 90 | eq(SystemMessage.NOTE_POWER_LOW), any()); |
Geoffrey Pitsch | 4a7931d | 2016-09-15 13:11:47 -0400 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | @Test |
| 94 | public void testDismissInvalidChargerNotification_CancelAsUser() { |
| 95 | mPowerNotificationWarnings.showInvalidChargerWarning(); |
| 96 | mPowerNotificationWarnings.dismissInvalidChargerWarning(); |
Chris Wren | 5e6c0ff | 2017-01-05 12:57:06 -0500 | [diff] [blame] | 97 | verify(mMockNotificationManager, times(1)).cancelAsUser(anyString(), |
| 98 | eq(SystemMessage.NOTE_BAD_CHARGER), any()); |
Geoffrey Pitsch | 4a7931d | 2016-09-15 13:11:47 -0400 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | @Test |
| 102 | public void testShowLowBatteryNotification_NotifyAsUser() { |
| 103 | mPowerNotificationWarnings.showLowBatteryWarning(false); |
| 104 | verify(mMockNotificationManager, times(1)) |
Chris Wren | 5e6c0ff | 2017-01-05 12:57:06 -0500 | [diff] [blame] | 105 | .notifyAsUser(anyString(), eq(SystemMessage.NOTE_POWER_LOW), any(), any()); |
| 106 | verify(mMockNotificationManager, times(1)).cancelAsUser(anyString(), |
| 107 | eq(SystemMessage.NOTE_BAD_CHARGER), any()); |
Geoffrey Pitsch | 4a7931d | 2016-09-15 13:11:47 -0400 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | @Test |
| 111 | public void testDismissLowBatteryNotification_CancelAsUser() { |
| 112 | mPowerNotificationWarnings.showLowBatteryWarning(false); |
| 113 | mPowerNotificationWarnings.dismissLowBatteryWarning(); |
Chris Wren | 5e6c0ff | 2017-01-05 12:57:06 -0500 | [diff] [blame] | 114 | verify(mMockNotificationManager, times(1)).cancelAsUser(anyString(), |
| 115 | eq(SystemMessage.NOTE_POWER_LOW), any()); |
Geoffrey Pitsch | 4a7931d | 2016-09-15 13:11:47 -0400 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | @Test |
Beverly | 334bc5f | 2017-07-31 10:37:17 -0400 | [diff] [blame] | 119 | public void testShowLowBatteryNotification_BatteryChannel() { |
Geoffrey Pitsch | 4a7931d | 2016-09-15 13:11:47 -0400 | [diff] [blame] | 120 | mPowerNotificationWarnings.showLowBatteryWarning(true); |
| 121 | ArgumentCaptor<Notification> captor = ArgumentCaptor.forClass(Notification.class); |
| 122 | verify(mMockNotificationManager) |
Chris Wren | 5e6c0ff | 2017-01-05 12:57:06 -0500 | [diff] [blame] | 123 | .notifyAsUser(anyString(), eq(SystemMessage.NOTE_POWER_LOW), |
| 124 | captor.capture(), any()); |
Beverly | 334bc5f | 2017-07-31 10:37:17 -0400 | [diff] [blame] | 125 | assertTrue(captor.getValue().getChannelId() == NotificationChannels.BATTERY); |
Geoffrey Pitsch | 4a7931d | 2016-09-15 13:11:47 -0400 | [diff] [blame] | 126 | } |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 127 | |
| 128 | @Test |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 129 | public void testShowHighTemperatureWarning_NotifyAsUser() { |
| 130 | mPowerNotificationWarnings.showHighTemperatureWarning(); |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 131 | verify(mMockNotificationManager, times(1)) |
Chris Wren | 5e6c0ff | 2017-01-05 12:57:06 -0500 | [diff] [blame] | 132 | .notifyAsUser(anyString(), eq(SystemMessage.NOTE_HIGH_TEMP), any(), any()); |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | @Test |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 136 | public void testDismissHighTemperatureWarning_CancelAsUser() { |
| 137 | mPowerNotificationWarnings.showHighTemperatureWarning(); |
| 138 | mPowerNotificationWarnings.dismissHighTemperatureWarning(); |
Chris Wren | 5e6c0ff | 2017-01-05 12:57:06 -0500 | [diff] [blame] | 139 | verify(mMockNotificationManager, times(1)).cancelAsUser(anyString(), |
| 140 | eq(SystemMessage.NOTE_HIGH_TEMP), any()); |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 141 | } |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 142 | |
| 143 | @Test |
| 144 | public void testShowThermalShutdownWarning_NotifyAsUser() { |
| 145 | mPowerNotificationWarnings.showThermalShutdownWarning(); |
| 146 | verify(mMockNotificationManager, times(1)) |
| 147 | .notifyAsUser(anyString(), eq(SystemMessage.NOTE_THERMAL_SHUTDOWN), any(), any()); |
| 148 | } |
| 149 | |
| 150 | @Test |
| 151 | public void testDismissThermalShutdownWarning_CancelAsUser() { |
| 152 | mPowerNotificationWarnings.showThermalShutdownWarning(); |
| 153 | mPowerNotificationWarnings.dismissThermalShutdownWarning(); |
| 154 | verify(mMockNotificationManager, times(1)).cancelAsUser(anyString(), |
| 155 | eq(SystemMessage.NOTE_THERMAL_SHUTDOWN), any()); |
| 156 | } |
Sherry Huang | ce02ed3 | 2019-01-17 20:37:29 +0800 | [diff] [blame] | 157 | |
| 158 | @Test |
| 159 | public void testShowUsbHighTemperatureAlarm() { |
| 160 | mPowerNotificationWarnings.showUsbHighTemperatureAlarm(); |
| 161 | waitForIdleSync(mContext.getMainThreadHandler()); |
| 162 | assertThat(mPowerNotificationWarnings.mUsbHighTempDialog).isNotNull(); |
| 163 | |
| 164 | mPowerNotificationWarnings.mUsbHighTempDialog.dismiss(); |
| 165 | } |
Geoffrey Pitsch | 4a7931d | 2016-09-15 13:11:47 -0400 | [diff] [blame] | 166 | } |