blob: bf6cc53aa5e900643e14083cec171414fe36fe9d [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
19import static android.test.MoreAsserts.assertNotEqual;
Jason Monke9789282016-11-09 08:59:56 -050020
Geoffrey Pitsch4a7931d2016-09-15 13:11:47 -040021import static junit.framework.Assert.assertEquals;
22import static junit.framework.Assert.assertFalse;
23import static junit.framework.Assert.assertTrue;
Jason Monke9789282016-11-09 08:59:56 -050024
Chris Wren5e6c0ff2017-01-05 12:57:06 -050025import static org.mockito.Matchers.eq;
Geoffrey Pitsch4a7931d2016-09-15 13:11:47 -040026import static org.mockito.Mockito.any;
Geoffrey Pitsch4a7931d2016-09-15 13:11:47 -040027import static org.mockito.Mockito.anyString;
28import static org.mockito.Mockito.mock;
29import static org.mockito.Mockito.times;
30import static org.mockito.Mockito.verify;
31
32import android.app.Notification;
33import android.app.NotificationManager;
Geoffrey Pitsch4a7931d2016-09-15 13:11:47 -040034import android.support.test.runner.AndroidJUnit4;
35import android.test.suitebuilder.annotation.SmallTest;
Jason Monke9789282016-11-09 08:59:56 -050036
Chris Wren5e6c0ff2017-01-05 12:57:06 -050037import com.android.internal.messages.nano.SystemMessageProto.SystemMessage;
Jason Monke9789282016-11-09 08:59:56 -050038import com.android.systemui.SysuiTestCase;
Beverly334bc5f2017-07-31 10:37:17 -040039import com.android.systemui.util.NotificationChannels;
Jason Monke9789282016-11-09 08:59:56 -050040
Salvador Martinezf9e47502018-01-04 13:45:48 -080041import java.util.concurrent.TimeUnit;
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);
Geoffrey Pitsch4a7931d2016-09-15 13:11:47 -040061 }
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 Wren5e6c0ff2017-01-05 12:57:06 -050085 .notifyAsUser(anyString(), eq(SystemMessage.NOTE_BAD_CHARGER), any(), any());
86 verify(mMockNotificationManager, times(1)).cancelAsUser(anyString(),
87 eq(SystemMessage.NOTE_POWER_LOW), any());
Geoffrey Pitsch4a7931d2016-09-15 13:11:47 -040088 }
89
90 @Test
91 public void testDismissInvalidChargerNotification_CancelAsUser() {
92 mPowerNotificationWarnings.showInvalidChargerWarning();
93 mPowerNotificationWarnings.dismissInvalidChargerWarning();
Chris Wren5e6c0ff2017-01-05 12:57:06 -050094 verify(mMockNotificationManager, times(1)).cancelAsUser(anyString(),
95 eq(SystemMessage.NOTE_BAD_CHARGER), any());
Geoffrey Pitsch4a7931d2016-09-15 13:11:47 -040096 }
97
98 @Test
99 public void testShowLowBatteryNotification_NotifyAsUser() {
100 mPowerNotificationWarnings.showLowBatteryWarning(false);
101 verify(mMockNotificationManager, times(1))
Chris Wren5e6c0ff2017-01-05 12:57:06 -0500102 .notifyAsUser(anyString(), eq(SystemMessage.NOTE_POWER_LOW), any(), any());
103 verify(mMockNotificationManager, times(1)).cancelAsUser(anyString(),
104 eq(SystemMessage.NOTE_BAD_CHARGER), any());
Geoffrey Pitsch4a7931d2016-09-15 13:11:47 -0400105 }
106
107 @Test
108 public void testDismissLowBatteryNotification_CancelAsUser() {
109 mPowerNotificationWarnings.showLowBatteryWarning(false);
110 mPowerNotificationWarnings.dismissLowBatteryWarning();
Chris Wren5e6c0ff2017-01-05 12:57:06 -0500111 verify(mMockNotificationManager, times(1)).cancelAsUser(anyString(),
112 eq(SystemMessage.NOTE_POWER_LOW), any());
Geoffrey Pitsch4a7931d2016-09-15 13:11:47 -0400113 }
114
115 @Test
Beverly334bc5f2017-07-31 10:37:17 -0400116 public void testShowLowBatteryNotification_BatteryChannel() {
Geoffrey Pitsch4a7931d2016-09-15 13:11:47 -0400117 mPowerNotificationWarnings.showLowBatteryWarning(true);
118 ArgumentCaptor<Notification> captor = ArgumentCaptor.forClass(Notification.class);
119 verify(mMockNotificationManager)
Chris Wren5e6c0ff2017-01-05 12:57:06 -0500120 .notifyAsUser(anyString(), eq(SystemMessage.NOTE_POWER_LOW),
121 captor.capture(), any());
Beverly334bc5f2017-07-31 10:37:17 -0400122 assertTrue(captor.getValue().getChannelId() == NotificationChannels.BATTERY);
Geoffrey Pitsch4a7931d2016-09-15 13:11:47 -0400123 }
Andrew Sappersteinb7caf1d2016-12-14 15:39:20 -0800124
125 @Test
Salvador Martineza6f7b252017-04-10 10:46:15 -0700126 public void testShowHighTemperatureWarning_NotifyAsUser() {
127 mPowerNotificationWarnings.showHighTemperatureWarning();
Andrew Sappersteinb7caf1d2016-12-14 15:39:20 -0800128 verify(mMockNotificationManager, times(1))
Chris Wren5e6c0ff2017-01-05 12:57:06 -0500129 .notifyAsUser(anyString(), eq(SystemMessage.NOTE_HIGH_TEMP), any(), any());
Andrew Sappersteinb7caf1d2016-12-14 15:39:20 -0800130 }
131
132 @Test
Salvador Martineza6f7b252017-04-10 10:46:15 -0700133 public void testDismissHighTemperatureWarning_CancelAsUser() {
134 mPowerNotificationWarnings.showHighTemperatureWarning();
135 mPowerNotificationWarnings.dismissHighTemperatureWarning();
Chris Wren5e6c0ff2017-01-05 12:57:06 -0500136 verify(mMockNotificationManager, times(1)).cancelAsUser(anyString(),
137 eq(SystemMessage.NOTE_HIGH_TEMP), any());
Andrew Sappersteinb7caf1d2016-12-14 15:39:20 -0800138 }
Salvador Martineza6f7b252017-04-10 10:46:15 -0700139
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 }
Geoffrey Pitsch4a7931d2016-09-15 13:11:47 -0400154}