2 * Copyright (C) 2018 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.systemui.statusbar.phone;
19 import static junit.framework.Assert.assertFalse;
20 import static junit.framework.Assert.assertTrue;
22 import static org.mockito.ArgumentMatchers.anyInt;
23 import static org.mockito.Mockito.when;
25 import android.content.Context;
26 import android.testing.AndroidTestingRunner;
27 import android.testing.TestableLooper;
28 import android.view.View;
30 import androidx.test.filters.SmallTest;
32 import com.android.systemui.plugins.statusbar.StatusBarStateController;
33 import com.android.systemui.statusbar.AlertingNotificationManager;
34 import com.android.systemui.statusbar.AlertingNotificationManagerTest;
35 import com.android.systemui.statusbar.notification.VisualStabilityManager;
36 import com.android.systemui.statusbar.notification.collection.NotificationEntry;
37 import com.android.systemui.statusbar.policy.AccessibilityManagerWrapper;
39 import org.junit.Before;
40 import org.junit.Rule;
41 import org.junit.Test;
42 import org.junit.runner.RunWith;
43 import org.mockito.Mock;
44 import org.mockito.junit.MockitoJUnit;
45 import org.mockito.junit.MockitoRule;
48 @RunWith(AndroidTestingRunner.class)
49 @TestableLooper.RunWithLooper
50 public class HeadsUpManagerPhoneTest extends AlertingNotificationManagerTest {
51 @Rule public MockitoRule rule = MockitoJUnit.rule();
53 private HeadsUpManagerPhone mHeadsUpManager;
55 @Mock private NotificationGroupManager mGroupManager;
56 @Mock private View mStatusBarWindowView;
57 @Mock private VisualStabilityManager mVSManager;
58 @Mock private StatusBar mBar;
59 @Mock private StatusBarStateController mStatusBarStateController;
60 @Mock private KeyguardBypassController mBypassController;
61 private boolean mLivesPastNormalTime;
63 private final class TestableHeadsUpManagerPhone extends HeadsUpManagerPhone {
64 TestableHeadsUpManagerPhone(Context context, View statusBarWindowView,
65 NotificationGroupManager groupManager, StatusBar bar,
66 VisualStabilityManager vsManager,
67 StatusBarStateController statusBarStateController,
68 KeyguardBypassController keyguardBypassController) {
69 super(context, statusBarStateController, keyguardBypassController);
70 setUp(statusBarWindowView, groupManager, bar, vsManager);
71 mMinimumDisplayTime = TEST_MINIMUM_DISPLAY_TIME;
72 mAutoDismissNotificationDecay = TEST_AUTO_DISMISS_TIME;
76 protected AlertingNotificationManager createAlertingNotificationManager() {
77 return mHeadsUpManager;
82 AccessibilityManagerWrapper accessibilityMgr =
83 mDependency.injectMockDependency(AccessibilityManagerWrapper.class);
84 when(accessibilityMgr.getRecommendedTimeoutMillis(anyInt(), anyInt()))
85 .thenReturn(TEST_AUTO_DISMISS_TIME);
86 when(mVSManager.isReorderingAllowed()).thenReturn(true);
87 mHeadsUpManager = new TestableHeadsUpManagerPhone(mContext, mStatusBarWindowView,
88 mGroupManager, mBar, mVSManager, mStatusBarStateController, mBypassController);
90 mHeadsUpManager.mHandler = mTestHandler;
94 public void testSnooze() {
95 mHeadsUpManager.showNotification(mEntry);
97 mHeadsUpManager.snooze();
99 assertTrue(mHeadsUpManager.isSnoozed(mEntry.notification.getPackageName()));
103 public void testSwipedOutNotification() {
104 mHeadsUpManager.showNotification(mEntry);
105 mHeadsUpManager.addSwipedOutNotification(mEntry.key);
107 // Remove should succeed because the notification is swiped out
108 mHeadsUpManager.removeNotification(mEntry.key, false /* releaseImmediately */);
110 assertFalse(mHeadsUpManager.isAlerting(mEntry.key));
114 public void testCanRemoveImmediately_swipedOut() {
115 mHeadsUpManager.showNotification(mEntry);
116 mHeadsUpManager.addSwipedOutNotification(mEntry.key);
118 // Notification is swiped so it can be immediately removed.
119 assertTrue(mHeadsUpManager.canRemoveImmediately(mEntry.key));
123 public void testCanRemoveImmediately_notTopEntry() {
124 NotificationEntry laterEntry = new NotificationEntry(createNewNotification(1));
125 laterEntry.setRow(mRow);
126 mHeadsUpManager.showNotification(mEntry);
127 mHeadsUpManager.showNotification(laterEntry);
129 // Notification is "behind" a higher priority notification so we can remove it immediately.
130 assertTrue(mHeadsUpManager.canRemoveImmediately(mEntry.key));
135 public void testExtendHeadsUp() {
136 mHeadsUpManager.showNotification(mEntry);
137 Runnable pastNormalTimeRunnable =
138 () -> mLivesPastNormalTime = mHeadsUpManager.isAlerting(mEntry.key);
139 mTestHandler.postDelayed(pastNormalTimeRunnable,
140 TEST_AUTO_DISMISS_TIME + mHeadsUpManager.mExtensionTime / 2);
141 mTestHandler.postDelayed(TEST_TIMEOUT_RUNNABLE, TEST_TIMEOUT_TIME);
143 mHeadsUpManager.extendHeadsUp();
145 // Wait for normal time runnable and extended remove runnable and process them on arrival.
146 TestableLooper.get(this).processMessages(2);
148 assertFalse("Test timed out", mTimedOut);
149 assertTrue("Pulse was not extended", mLivesPastNormalTime);
150 assertFalse(mHeadsUpManager.isAlerting(mEntry.key));