blob: 58c931190c83c41250ded4f5cfcec8daec9494c4 [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
Sherry Huangce02ed32019-01-17 20:37:29 +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;
28import static org.mockito.Mockito.times;
29import static org.mockito.Mockito.verify;
30
31import android.app.Notification;
32import android.app.NotificationManager;
Salvador Martinez4387bd52019-02-21 16:16:28 -080033import android.os.BatteryManager;
Geoffrey Pitsch4a7931d2016-09-15 13:11:47 -040034import android.test.suitebuilder.annotation.SmallTest;
Jason Monke9789282016-11-09 08:59:56 -050035
Brett Chabot84151d92019-02-27 15:37:59 -080036import androidx.test.runner.AndroidJUnit4;
37
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
Geoffrey Pitsch4a7931d2016-09-15 13:11:47 -040042import org.junit.Before;
43import org.junit.Test;
44import org.junit.runner.RunWith;
45import org.mockito.ArgumentCaptor;
46
47@SmallTest
48@RunWith(AndroidJUnit4.class)
Jason Monke9789282016-11-09 08:59:56 -050049public class PowerNotificationWarningsTest extends SysuiTestCase {
Salvador Martinezf9e47502018-01-04 13:45:48 -080050
51 public static final String FORMATTED_45M = "0h 45m";
52 public static final String FORMATTED_HOUR = "1h 0m";
Geoffrey Pitsch4a7931d2016-09-15 13:11:47 -040053 private final NotificationManager mMockNotificationManager = mock(NotificationManager.class);
54 private PowerNotificationWarnings mPowerNotificationWarnings;
55
56 @Before
57 public void setUp() throws Exception {
58 // Test Instance.
Jason Monkd819c312017-08-11 12:53:36 -040059 mContext.addMockSystemService(NotificationManager.class, mMockNotificationManager);
60 mPowerNotificationWarnings = new PowerNotificationWarnings(mContext);
Salvador Martinez4387bd52019-02-21 16:16:28 -080061 BatteryStateSnapshot snapshot = new BatteryStateSnapshot(100, false, false, 1,
62 BatteryManager.BATTERY_HEALTH_GOOD, 5, 15);
63 mPowerNotificationWarnings.updateSnapshot(snapshot);
Geoffrey Pitsch4a7931d2016-09-15 13:11:47 -040064 }
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 Wren5e6c0ff2017-01-05 12:57:06 -050088 .notifyAsUser(anyString(), eq(SystemMessage.NOTE_BAD_CHARGER), any(), any());
89 verify(mMockNotificationManager, times(1)).cancelAsUser(anyString(),
90 eq(SystemMessage.NOTE_POWER_LOW), any());
Geoffrey Pitsch4a7931d2016-09-15 13:11:47 -040091 }
92
93 @Test
94 public void testDismissInvalidChargerNotification_CancelAsUser() {
95 mPowerNotificationWarnings.showInvalidChargerWarning();
96 mPowerNotificationWarnings.dismissInvalidChargerWarning();
Chris Wren5e6c0ff2017-01-05 12:57:06 -050097 verify(mMockNotificationManager, times(1)).cancelAsUser(anyString(),
98 eq(SystemMessage.NOTE_BAD_CHARGER), any());
Geoffrey Pitsch4a7931d2016-09-15 13:11:47 -040099 }
100
101 @Test
102 public void testShowLowBatteryNotification_NotifyAsUser() {
103 mPowerNotificationWarnings.showLowBatteryWarning(false);
104 verify(mMockNotificationManager, times(1))
Chris Wren5e6c0ff2017-01-05 12:57:06 -0500105 .notifyAsUser(anyString(), eq(SystemMessage.NOTE_POWER_LOW), any(), any());
106 verify(mMockNotificationManager, times(1)).cancelAsUser(anyString(),
107 eq(SystemMessage.NOTE_BAD_CHARGER), any());
Geoffrey Pitsch4a7931d2016-09-15 13:11:47 -0400108 }
109
110 @Test
111 public void testDismissLowBatteryNotification_CancelAsUser() {
112 mPowerNotificationWarnings.showLowBatteryWarning(false);
113 mPowerNotificationWarnings.dismissLowBatteryWarning();
Chris Wren5e6c0ff2017-01-05 12:57:06 -0500114 verify(mMockNotificationManager, times(1)).cancelAsUser(anyString(),
115 eq(SystemMessage.NOTE_POWER_LOW), any());
Geoffrey Pitsch4a7931d2016-09-15 13:11:47 -0400116 }
117
118 @Test
Beverly334bc5f2017-07-31 10:37:17 -0400119 public void testShowLowBatteryNotification_BatteryChannel() {
Geoffrey Pitsch4a7931d2016-09-15 13:11:47 -0400120 mPowerNotificationWarnings.showLowBatteryWarning(true);
121 ArgumentCaptor<Notification> captor = ArgumentCaptor.forClass(Notification.class);
122 verify(mMockNotificationManager)
Chris Wren5e6c0ff2017-01-05 12:57:06 -0500123 .notifyAsUser(anyString(), eq(SystemMessage.NOTE_POWER_LOW),
124 captor.capture(), any());
Beverly334bc5f2017-07-31 10:37:17 -0400125 assertTrue(captor.getValue().getChannelId() == NotificationChannels.BATTERY);
Geoffrey Pitsch4a7931d2016-09-15 13:11:47 -0400126 }
Andrew Sappersteinb7caf1d2016-12-14 15:39:20 -0800127
128 @Test
Salvador Martineza6f7b252017-04-10 10:46:15 -0700129 public void testShowHighTemperatureWarning_NotifyAsUser() {
130 mPowerNotificationWarnings.showHighTemperatureWarning();
Andrew Sappersteinb7caf1d2016-12-14 15:39:20 -0800131 verify(mMockNotificationManager, times(1))
Chris Wren5e6c0ff2017-01-05 12:57:06 -0500132 .notifyAsUser(anyString(), eq(SystemMessage.NOTE_HIGH_TEMP), any(), any());
Andrew Sappersteinb7caf1d2016-12-14 15:39:20 -0800133 }
134
135 @Test
Salvador Martineza6f7b252017-04-10 10:46:15 -0700136 public void testDismissHighTemperatureWarning_CancelAsUser() {
137 mPowerNotificationWarnings.showHighTemperatureWarning();
138 mPowerNotificationWarnings.dismissHighTemperatureWarning();
Chris Wren5e6c0ff2017-01-05 12:57:06 -0500139 verify(mMockNotificationManager, times(1)).cancelAsUser(anyString(),
140 eq(SystemMessage.NOTE_HIGH_TEMP), any());
Andrew Sappersteinb7caf1d2016-12-14 15:39:20 -0800141 }
Salvador Martineza6f7b252017-04-10 10:46:15 -0700142
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 Huangce02ed32019-01-17 20:37:29 +0800157
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 Pitsch4a7931d2016-09-15 13:11:47 -0400166}