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 | |
| 19 | import static android.test.MoreAsserts.assertNotEqual; |
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.assertEquals; |
| 22 | import static junit.framework.Assert.assertFalse; |
| 23 | import static junit.framework.Assert.assertTrue; |
Jason Monk | e978928 | 2016-11-09 08:59:56 -0500 | [diff] [blame] | 24 | |
Chris Wren | 5e6c0ff | 2017-01-05 12:57:06 -0500 | [diff] [blame] | 25 | import static org.mockito.Matchers.eq; |
Geoffrey Pitsch | 4a7931d | 2016-09-15 13:11:47 -0400 | [diff] [blame] | 26 | import static org.mockito.Mockito.any; |
Geoffrey Pitsch | 4a7931d | 2016-09-15 13:11:47 -0400 | [diff] [blame] | 27 | import static org.mockito.Mockito.anyString; |
| 28 | import static org.mockito.Mockito.mock; |
| 29 | import static org.mockito.Mockito.times; |
| 30 | import static org.mockito.Mockito.verify; |
| 31 | |
| 32 | import android.app.Notification; |
| 33 | import android.app.NotificationManager; |
Geoffrey Pitsch | 4a7931d | 2016-09-15 13:11:47 -0400 | [diff] [blame] | 34 | import android.support.test.runner.AndroidJUnit4; |
| 35 | import android.test.suitebuilder.annotation.SmallTest; |
Jason Monk | e978928 | 2016-11-09 08:59:56 -0500 | [diff] [blame] | 36 | |
Chris Wren | 5e6c0ff | 2017-01-05 12:57:06 -0500 | [diff] [blame] | 37 | import com.android.internal.messages.nano.SystemMessageProto.SystemMessage; |
Jason Monk | e978928 | 2016-11-09 08:59:56 -0500 | [diff] [blame] | 38 | import com.android.systemui.SysuiTestCase; |
Beverly | 334bc5f | 2017-07-31 10:37:17 -0400 | [diff] [blame] | 39 | import com.android.systemui.util.NotificationChannels; |
Jason Monk | e978928 | 2016-11-09 08:59:56 -0500 | [diff] [blame] | 40 | |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 41 | import java.util.concurrent.TimeUnit; |
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); |
Geoffrey Pitsch | 4a7931d | 2016-09-15 13:11:47 -0400 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | @Test |
| 64 | public void testIsInvalidChargerWarningShowing_DefaultsToFalse() { |
| 65 | assertFalse(mPowerNotificationWarnings.isInvalidChargerWarningShowing()); |
| 66 | } |
| 67 | |
| 68 | @Test |
| 69 | public void testIsInvalidChargerWarningShowing_TrueAfterShow() { |
| 70 | mPowerNotificationWarnings.showInvalidChargerWarning(); |
| 71 | assertTrue(mPowerNotificationWarnings.isInvalidChargerWarningShowing()); |
| 72 | } |
| 73 | |
| 74 | @Test |
| 75 | public void testIsInvalidChargerWarningShowing_FalseAfterDismiss() { |
| 76 | mPowerNotificationWarnings.showInvalidChargerWarning(); |
| 77 | mPowerNotificationWarnings.dismissInvalidChargerWarning(); |
| 78 | assertFalse(mPowerNotificationWarnings.isInvalidChargerWarningShowing()); |
| 79 | } |
| 80 | |
| 81 | @Test |
| 82 | public void testShowInvalidChargerNotification_NotifyAsUser() { |
| 83 | mPowerNotificationWarnings.showInvalidChargerWarning(); |
| 84 | verify(mMockNotificationManager, times(1)) |
Chris Wren | 5e6c0ff | 2017-01-05 12:57:06 -0500 | [diff] [blame] | 85 | .notifyAsUser(anyString(), eq(SystemMessage.NOTE_BAD_CHARGER), any(), any()); |
| 86 | verify(mMockNotificationManager, times(1)).cancelAsUser(anyString(), |
| 87 | eq(SystemMessage.NOTE_POWER_LOW), any()); |
Geoffrey Pitsch | 4a7931d | 2016-09-15 13:11:47 -0400 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | @Test |
| 91 | public void testDismissInvalidChargerNotification_CancelAsUser() { |
| 92 | mPowerNotificationWarnings.showInvalidChargerWarning(); |
| 93 | mPowerNotificationWarnings.dismissInvalidChargerWarning(); |
Chris Wren | 5e6c0ff | 2017-01-05 12:57:06 -0500 | [diff] [blame] | 94 | verify(mMockNotificationManager, times(1)).cancelAsUser(anyString(), |
| 95 | eq(SystemMessage.NOTE_BAD_CHARGER), any()); |
Geoffrey Pitsch | 4a7931d | 2016-09-15 13:11:47 -0400 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | @Test |
| 99 | public void testShowLowBatteryNotification_NotifyAsUser() { |
| 100 | mPowerNotificationWarnings.showLowBatteryWarning(false); |
| 101 | verify(mMockNotificationManager, times(1)) |
Chris Wren | 5e6c0ff | 2017-01-05 12:57:06 -0500 | [diff] [blame] | 102 | .notifyAsUser(anyString(), eq(SystemMessage.NOTE_POWER_LOW), any(), any()); |
| 103 | verify(mMockNotificationManager, times(1)).cancelAsUser(anyString(), |
| 104 | eq(SystemMessage.NOTE_BAD_CHARGER), any()); |
Geoffrey Pitsch | 4a7931d | 2016-09-15 13:11:47 -0400 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | @Test |
| 108 | public void testDismissLowBatteryNotification_CancelAsUser() { |
| 109 | mPowerNotificationWarnings.showLowBatteryWarning(false); |
| 110 | mPowerNotificationWarnings.dismissLowBatteryWarning(); |
Chris Wren | 5e6c0ff | 2017-01-05 12:57:06 -0500 | [diff] [blame] | 111 | verify(mMockNotificationManager, times(1)).cancelAsUser(anyString(), |
| 112 | eq(SystemMessage.NOTE_POWER_LOW), any()); |
Geoffrey Pitsch | 4a7931d | 2016-09-15 13:11:47 -0400 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | @Test |
Beverly | 334bc5f | 2017-07-31 10:37:17 -0400 | [diff] [blame] | 116 | public void testShowLowBatteryNotification_BatteryChannel() { |
Geoffrey Pitsch | 4a7931d | 2016-09-15 13:11:47 -0400 | [diff] [blame] | 117 | mPowerNotificationWarnings.showLowBatteryWarning(true); |
| 118 | ArgumentCaptor<Notification> captor = ArgumentCaptor.forClass(Notification.class); |
| 119 | verify(mMockNotificationManager) |
Chris Wren | 5e6c0ff | 2017-01-05 12:57:06 -0500 | [diff] [blame] | 120 | .notifyAsUser(anyString(), eq(SystemMessage.NOTE_POWER_LOW), |
| 121 | captor.capture(), any()); |
Beverly | 334bc5f | 2017-07-31 10:37:17 -0400 | [diff] [blame] | 122 | assertTrue(captor.getValue().getChannelId() == NotificationChannels.BATTERY); |
Geoffrey Pitsch | 4a7931d | 2016-09-15 13:11:47 -0400 | [diff] [blame] | 123 | } |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 124 | |
| 125 | @Test |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 126 | public void testShowHighTemperatureWarning_NotifyAsUser() { |
| 127 | mPowerNotificationWarnings.showHighTemperatureWarning(); |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 128 | verify(mMockNotificationManager, times(1)) |
Chris Wren | 5e6c0ff | 2017-01-05 12:57:06 -0500 | [diff] [blame] | 129 | .notifyAsUser(anyString(), eq(SystemMessage.NOTE_HIGH_TEMP), any(), any()); |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | @Test |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 133 | public void testDismissHighTemperatureWarning_CancelAsUser() { |
| 134 | mPowerNotificationWarnings.showHighTemperatureWarning(); |
| 135 | mPowerNotificationWarnings.dismissHighTemperatureWarning(); |
Chris Wren | 5e6c0ff | 2017-01-05 12:57:06 -0500 | [diff] [blame] | 136 | verify(mMockNotificationManager, times(1)).cancelAsUser(anyString(), |
| 137 | eq(SystemMessage.NOTE_HIGH_TEMP), any()); |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 138 | } |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 139 | |
| 140 | @Test |
| 141 | public void testShowThermalShutdownWarning_NotifyAsUser() { |
| 142 | mPowerNotificationWarnings.showThermalShutdownWarning(); |
| 143 | verify(mMockNotificationManager, times(1)) |
| 144 | .notifyAsUser(anyString(), eq(SystemMessage.NOTE_THERMAL_SHUTDOWN), any(), any()); |
| 145 | } |
| 146 | |
| 147 | @Test |
| 148 | public void testDismissThermalShutdownWarning_CancelAsUser() { |
| 149 | mPowerNotificationWarnings.showThermalShutdownWarning(); |
| 150 | mPowerNotificationWarnings.dismissThermalShutdownWarning(); |
| 151 | verify(mMockNotificationManager, times(1)).cancelAsUser(anyString(), |
| 152 | eq(SystemMessage.NOTE_THERMAL_SHUTDOWN), any()); |
| 153 | } |
Salvador Martinez | f9e4750 | 2018-01-04 13:45:48 -0800 | [diff] [blame] | 154 | |
| 155 | @Test |
| 156 | public void testGetTimeRemainingFormatted_roundsDownTo15() { |
| 157 | mPowerNotificationWarnings.updateEstimate( |
| 158 | new Estimate(TimeUnit.MINUTES.toMillis(57), true)); |
| 159 | String time = mPowerNotificationWarnings.getTimeRemainingFormatted(); |
| 160 | |
| 161 | assertTrue("time:" + time + ", expected: " + FORMATTED_45M, time.equals(FORMATTED_45M)); |
| 162 | } |
| 163 | |
| 164 | @Test |
| 165 | public void testGetTimeRemainingFormatted_keepsMinutesWhenZero() { |
| 166 | mPowerNotificationWarnings.updateEstimate( |
| 167 | new Estimate(TimeUnit.MINUTES.toMillis(65), true)); |
| 168 | String time = mPowerNotificationWarnings.getTimeRemainingFormatted(); |
| 169 | |
| 170 | assertTrue("time:" + time + ", expected: " + FORMATTED_HOUR, time.equals(FORMATTED_HOUR)); |
| 171 | } |
Geoffrey Pitsch | 4a7931d | 2016-09-15 13:11:47 -0400 | [diff] [blame] | 172 | } |