blob: fc3d36a303dcc4cdfc052996c66ce36e030460c0 [file] [log] [blame]
Geoffrey Pitsch4a7931d2016-09-15 13:11:47 -04001/*
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
17package com.android.systemui.power;
18
Bill Lin32ed3d62018-10-02 18:10:09 +080019import static com.google.common.truth.Truth.assertThat;
Jason Monke9789282016-11-09 08:59:56 -050020
Geoffrey Pitsch4a7931d2016-09-15 13:11:47 -040021import static junit.framework.Assert.assertFalse;
22import static junit.framework.Assert.assertTrue;
Jason Monke9789282016-11-09 08:59:56 -050023
Chris Wren5e6c0ff2017-01-05 12:57:06 -050024import static org.mockito.Matchers.eq;
Geoffrey Pitsch4a7931d2016-09-15 13:11:47 -040025import static org.mockito.Mockito.any;
Geoffrey Pitsch4a7931d2016-09-15 13:11:47 -040026import static org.mockito.Mockito.anyString;
27import static org.mockito.Mockito.mock;
Bill Lin32ed3d62018-10-02 18:10:09 +080028import static org.mockito.Mockito.never;
29import static org.mockito.Mockito.spy;
Geoffrey Pitsch4a7931d2016-09-15 13:11:47 -040030import static org.mockito.Mockito.times;
31import static org.mockito.Mockito.verify;
32
33import android.app.Notification;
34import android.app.NotificationManager;
Geoffrey Pitsch4a7931d2016-09-15 13:11:47 -040035import android.test.suitebuilder.annotation.SmallTest;
Jason Monke9789282016-11-09 08:59:56 -050036
Brett Chabot8091d9e2019-02-26 14:52:33 -080037import androidx.test.runner.AndroidJUnit4;
38
Chris Wren5e6c0ff2017-01-05 12:57:06 -050039import com.android.internal.messages.nano.SystemMessageProto.SystemMessage;
Jason Monke9789282016-11-09 08:59:56 -050040import com.android.systemui.SysuiTestCase;
Beverly334bc5f2017-07-31 10:37:17 -040041import com.android.systemui.util.NotificationChannels;
Jason Monke9789282016-11-09 08:59:56 -050042
Bill Lin32ed3d62018-10-02 18:10:09 +080043import org.junit.After;
Geoffrey Pitsch4a7931d2016-09-15 13:11:47 -040044import org.junit.Before;
45import org.junit.Test;
46import org.junit.runner.RunWith;
47import org.mockito.ArgumentCaptor;
48
49@SmallTest
50@RunWith(AndroidJUnit4.class)
Jason Monke9789282016-11-09 08:59:56 -050051public class PowerNotificationWarningsTest extends SysuiTestCase {
Salvador Martinezf9e47502018-01-04 13:45:48 -080052
53 public static final String FORMATTED_45M = "0h 45m";
54 public static final String FORMATTED_HOUR = "1h 0m";
Geoffrey Pitsch4a7931d2016-09-15 13:11:47 -040055 private final NotificationManager mMockNotificationManager = mock(NotificationManager.class);
Bill Lin32ed3d62018-10-02 18:10:09 +080056 private PowerNotificationWarnings mPowerNotificationWarnings, mSpyPowerNotificationWarnings;
Geoffrey Pitsch4a7931d2016-09-15 13:11:47 -040057
58 @Before
59 public void setUp() throws Exception {
60 // Test Instance.
Jason Monkd819c312017-08-11 12:53:36 -040061 mContext.addMockSystemService(NotificationManager.class, mMockNotificationManager);
62 mPowerNotificationWarnings = new PowerNotificationWarnings(mContext);
Bill Lin32ed3d62018-10-02 18:10:09 +080063 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 Pitsch4a7931d2016-09-15 13:11:47 -040072 }
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 Wren5e6c0ff2017-01-05 12:57:06 -050096 .notifyAsUser(anyString(), eq(SystemMessage.NOTE_BAD_CHARGER), any(), any());
97 verify(mMockNotificationManager, times(1)).cancelAsUser(anyString(),
98 eq(SystemMessage.NOTE_POWER_LOW), any());
Geoffrey Pitsch4a7931d2016-09-15 13:11:47 -040099 }
100
101 @Test
102 public void testDismissInvalidChargerNotification_CancelAsUser() {
103 mPowerNotificationWarnings.showInvalidChargerWarning();
104 mPowerNotificationWarnings.dismissInvalidChargerWarning();
Chris Wren5e6c0ff2017-01-05 12:57:06 -0500105 verify(mMockNotificationManager, times(1)).cancelAsUser(anyString(),
106 eq(SystemMessage.NOTE_BAD_CHARGER), any());
Geoffrey Pitsch4a7931d2016-09-15 13:11:47 -0400107 }
108
109 @Test
110 public void testShowLowBatteryNotification_NotifyAsUser() {
111 mPowerNotificationWarnings.showLowBatteryWarning(false);
112 verify(mMockNotificationManager, times(1))
Chris Wren5e6c0ff2017-01-05 12:57:06 -0500113 .notifyAsUser(anyString(), eq(SystemMessage.NOTE_POWER_LOW), any(), any());
114 verify(mMockNotificationManager, times(1)).cancelAsUser(anyString(),
115 eq(SystemMessage.NOTE_BAD_CHARGER), any());
Geoffrey Pitsch4a7931d2016-09-15 13:11:47 -0400116 }
117
118 @Test
119 public void testDismissLowBatteryNotification_CancelAsUser() {
120 mPowerNotificationWarnings.showLowBatteryWarning(false);
121 mPowerNotificationWarnings.dismissLowBatteryWarning();
Chris Wren5e6c0ff2017-01-05 12:57:06 -0500122 verify(mMockNotificationManager, times(1)).cancelAsUser(anyString(),
123 eq(SystemMessage.NOTE_POWER_LOW), any());
Geoffrey Pitsch4a7931d2016-09-15 13:11:47 -0400124 }
125
126 @Test
Beverly334bc5f2017-07-31 10:37:17 -0400127 public void testShowLowBatteryNotification_BatteryChannel() {
Geoffrey Pitsch4a7931d2016-09-15 13:11:47 -0400128 mPowerNotificationWarnings.showLowBatteryWarning(true);
129 ArgumentCaptor<Notification> captor = ArgumentCaptor.forClass(Notification.class);
130 verify(mMockNotificationManager)
Chris Wren5e6c0ff2017-01-05 12:57:06 -0500131 .notifyAsUser(anyString(), eq(SystemMessage.NOTE_POWER_LOW),
132 captor.capture(), any());
Beverly334bc5f2017-07-31 10:37:17 -0400133 assertTrue(captor.getValue().getChannelId() == NotificationChannels.BATTERY);
Geoffrey Pitsch4a7931d2016-09-15 13:11:47 -0400134 }
Andrew Sappersteinb7caf1d2016-12-14 15:39:20 -0800135
136 @Test
Salvador Martineza6f7b252017-04-10 10:46:15 -0700137 public void testShowHighTemperatureWarning_NotifyAsUser() {
138 mPowerNotificationWarnings.showHighTemperatureWarning();
Andrew Sappersteinb7caf1d2016-12-14 15:39:20 -0800139 verify(mMockNotificationManager, times(1))
Chris Wren5e6c0ff2017-01-05 12:57:06 -0500140 .notifyAsUser(anyString(), eq(SystemMessage.NOTE_HIGH_TEMP), any(), any());
Andrew Sappersteinb7caf1d2016-12-14 15:39:20 -0800141 }
142
143 @Test
Salvador Martineza6f7b252017-04-10 10:46:15 -0700144 public void testDismissHighTemperatureWarning_CancelAsUser() {
145 mPowerNotificationWarnings.showHighTemperatureWarning();
146 mPowerNotificationWarnings.dismissHighTemperatureWarning();
Chris Wren5e6c0ff2017-01-05 12:57:06 -0500147 verify(mMockNotificationManager, times(1)).cancelAsUser(anyString(),
148 eq(SystemMessage.NOTE_HIGH_TEMP), any());
Andrew Sappersteinb7caf1d2016-12-14 15:39:20 -0800149 }
Salvador Martineza6f7b252017-04-10 10:46:15 -0700150
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 Lin32ed3d62018-10-02 18:10:09 +0800165
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 Pitsch4a7931d2016-09-15 13:11:47 -0400307}