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