blob: d37c2852bc1f1ab67a46c1855f30b56b80945f7b [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.support.test.runner.AndroidJUnit4;
36import android.test.suitebuilder.annotation.SmallTest;
Jason Monke9789282016-11-09 08:59:56 -050037
Chris Wren5e6c0ff2017-01-05 12:57:06 -050038import com.android.internal.messages.nano.SystemMessageProto.SystemMessage;
Jason Monke9789282016-11-09 08:59:56 -050039import com.android.systemui.SysuiTestCase;
Beverly334bc5f2017-07-31 10:37:17 -040040import com.android.systemui.util.NotificationChannels;
Jason Monke9789282016-11-09 08:59:56 -050041
Bill Lin32ed3d62018-10-02 18:10:09 +080042import org.junit.After;
Geoffrey Pitsch4a7931d2016-09-15 13:11:47 -040043import org.junit.Before;
44import org.junit.Test;
45import org.junit.runner.RunWith;
46import org.mockito.ArgumentCaptor;
47
48@SmallTest
49@RunWith(AndroidJUnit4.class)
Jason Monke9789282016-11-09 08:59:56 -050050public class PowerNotificationWarningsTest extends SysuiTestCase {
Salvador Martinezf9e47502018-01-04 13:45:48 -080051
52 public static final String FORMATTED_45M = "0h 45m";
53 public static final String FORMATTED_HOUR = "1h 0m";
Geoffrey Pitsch4a7931d2016-09-15 13:11:47 -040054 private final NotificationManager mMockNotificationManager = mock(NotificationManager.class);
Bill Lin32ed3d62018-10-02 18:10:09 +080055 private PowerNotificationWarnings mPowerNotificationWarnings, mSpyPowerNotificationWarnings;
Geoffrey Pitsch4a7931d2016-09-15 13:11:47 -040056
57 @Before
58 public void setUp() throws Exception {
59 // Test Instance.
Jason Monkd819c312017-08-11 12:53:36 -040060 mContext.addMockSystemService(NotificationManager.class, mMockNotificationManager);
61 mPowerNotificationWarnings = new PowerNotificationWarnings(mContext);
Bill Lin32ed3d62018-10-02 18:10:09 +080062 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 Pitsch4a7931d2016-09-15 13:11:47 -040071 }
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 Wren5e6c0ff2017-01-05 12:57:06 -050095 .notifyAsUser(anyString(), eq(SystemMessage.NOTE_BAD_CHARGER), any(), any());
96 verify(mMockNotificationManager, times(1)).cancelAsUser(anyString(),
97 eq(SystemMessage.NOTE_POWER_LOW), any());
Geoffrey Pitsch4a7931d2016-09-15 13:11:47 -040098 }
99
100 @Test
101 public void testDismissInvalidChargerNotification_CancelAsUser() {
102 mPowerNotificationWarnings.showInvalidChargerWarning();
103 mPowerNotificationWarnings.dismissInvalidChargerWarning();
Chris Wren5e6c0ff2017-01-05 12:57:06 -0500104 verify(mMockNotificationManager, times(1)).cancelAsUser(anyString(),
105 eq(SystemMessage.NOTE_BAD_CHARGER), any());
Geoffrey Pitsch4a7931d2016-09-15 13:11:47 -0400106 }
107
108 @Test
109 public void testShowLowBatteryNotification_NotifyAsUser() {
110 mPowerNotificationWarnings.showLowBatteryWarning(false);
111 verify(mMockNotificationManager, times(1))
Chris Wren5e6c0ff2017-01-05 12:57:06 -0500112 .notifyAsUser(anyString(), eq(SystemMessage.NOTE_POWER_LOW), any(), any());
113 verify(mMockNotificationManager, times(1)).cancelAsUser(anyString(),
114 eq(SystemMessage.NOTE_BAD_CHARGER), any());
Geoffrey Pitsch4a7931d2016-09-15 13:11:47 -0400115 }
116
117 @Test
118 public void testDismissLowBatteryNotification_CancelAsUser() {
119 mPowerNotificationWarnings.showLowBatteryWarning(false);
120 mPowerNotificationWarnings.dismissLowBatteryWarning();
Chris Wren5e6c0ff2017-01-05 12:57:06 -0500121 verify(mMockNotificationManager, times(1)).cancelAsUser(anyString(),
122 eq(SystemMessage.NOTE_POWER_LOW), any());
Geoffrey Pitsch4a7931d2016-09-15 13:11:47 -0400123 }
124
125 @Test
Beverly334bc5f2017-07-31 10:37:17 -0400126 public void testShowLowBatteryNotification_BatteryChannel() {
Geoffrey Pitsch4a7931d2016-09-15 13:11:47 -0400127 mPowerNotificationWarnings.showLowBatteryWarning(true);
128 ArgumentCaptor<Notification> captor = ArgumentCaptor.forClass(Notification.class);
129 verify(mMockNotificationManager)
Chris Wren5e6c0ff2017-01-05 12:57:06 -0500130 .notifyAsUser(anyString(), eq(SystemMessage.NOTE_POWER_LOW),
131 captor.capture(), any());
Beverly334bc5f2017-07-31 10:37:17 -0400132 assertTrue(captor.getValue().getChannelId() == NotificationChannels.BATTERY);
Geoffrey Pitsch4a7931d2016-09-15 13:11:47 -0400133 }
Andrew Sappersteinb7caf1d2016-12-14 15:39:20 -0800134
135 @Test
Salvador Martineza6f7b252017-04-10 10:46:15 -0700136 public void testShowHighTemperatureWarning_NotifyAsUser() {
137 mPowerNotificationWarnings.showHighTemperatureWarning();
Andrew Sappersteinb7caf1d2016-12-14 15:39:20 -0800138 verify(mMockNotificationManager, times(1))
Chris Wren5e6c0ff2017-01-05 12:57:06 -0500139 .notifyAsUser(anyString(), eq(SystemMessage.NOTE_HIGH_TEMP), any(), any());
Andrew Sappersteinb7caf1d2016-12-14 15:39:20 -0800140 }
141
142 @Test
Salvador Martineza6f7b252017-04-10 10:46:15 -0700143 public void testDismissHighTemperatureWarning_CancelAsUser() {
144 mPowerNotificationWarnings.showHighTemperatureWarning();
145 mPowerNotificationWarnings.dismissHighTemperatureWarning();
Chris Wren5e6c0ff2017-01-05 12:57:06 -0500146 verify(mMockNotificationManager, times(1)).cancelAsUser(anyString(),
147 eq(SystemMessage.NOTE_HIGH_TEMP), any());
Andrew Sappersteinb7caf1d2016-12-14 15:39:20 -0800148 }
Salvador Martineza6f7b252017-04-10 10:46:15 -0700149
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 Lin32ed3d62018-10-02 18:10:09 +0800164
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 Pitsch4a7931d2016-09-15 13:11:47 -0400306}