OSDN Git Service

Merge "docs: Add documentation for equals() method" into qt-dev am: 732a127636
[android-x86/frameworks-base.git] / packages / SystemUI / tests / src / com / android / systemui / statusbar / phone / HeadsUpManagerPhoneTest.java
1 /*
2  * Copyright (C) 2018 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
17 package com.android.systemui.statusbar.phone;
18
19 import static junit.framework.Assert.assertFalse;
20 import static junit.framework.Assert.assertTrue;
21
22 import static org.mockito.ArgumentMatchers.anyInt;
23 import static org.mockito.Mockito.when;
24
25 import android.content.Context;
26 import android.testing.AndroidTestingRunner;
27 import android.testing.TestableLooper;
28 import android.view.View;
29
30 import androidx.test.filters.SmallTest;
31
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;
38
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;
46
47 @SmallTest
48 @RunWith(AndroidTestingRunner.class)
49 @TestableLooper.RunWithLooper
50 public class HeadsUpManagerPhoneTest extends AlertingNotificationManagerTest {
51     @Rule public MockitoRule rule = MockitoJUnit.rule();
52
53     private HeadsUpManagerPhone mHeadsUpManager;
54
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;
62
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;
73         }
74     }
75
76     protected AlertingNotificationManager createAlertingNotificationManager() {
77         return mHeadsUpManager;
78     }
79
80     @Before
81     public void setUp() {
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);
89         super.setUp();
90         mHeadsUpManager.mHandler = mTestHandler;
91     }
92
93     @Test
94     public void testSnooze() {
95         mHeadsUpManager.showNotification(mEntry);
96
97         mHeadsUpManager.snooze();
98
99         assertTrue(mHeadsUpManager.isSnoozed(mEntry.notification.getPackageName()));
100     }
101
102     @Test
103     public void testSwipedOutNotification() {
104         mHeadsUpManager.showNotification(mEntry);
105         mHeadsUpManager.addSwipedOutNotification(mEntry.key);
106
107         // Remove should succeed because the notification is swiped out
108         mHeadsUpManager.removeNotification(mEntry.key, false /* releaseImmediately */);
109
110         assertFalse(mHeadsUpManager.isAlerting(mEntry.key));
111     }
112
113     @Test
114     public void testCanRemoveImmediately_swipedOut() {
115         mHeadsUpManager.showNotification(mEntry);
116         mHeadsUpManager.addSwipedOutNotification(mEntry.key);
117
118         // Notification is swiped so it can be immediately removed.
119         assertTrue(mHeadsUpManager.canRemoveImmediately(mEntry.key));
120     }
121
122     @Test
123     public void testCanRemoveImmediately_notTopEntry() {
124         NotificationEntry laterEntry = new NotificationEntry(createNewNotification(1));
125         laterEntry.setRow(mRow);
126         mHeadsUpManager.showNotification(mEntry);
127         mHeadsUpManager.showNotification(laterEntry);
128
129         // Notification is "behind" a higher priority notification so we can remove it immediately.
130         assertTrue(mHeadsUpManager.canRemoveImmediately(mEntry.key));
131     }
132
133
134     @Test
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);
142
143         mHeadsUpManager.extendHeadsUp();
144
145         // Wait for normal time runnable and extended remove runnable and process them on arrival.
146         TestableLooper.get(this).processMessages(2);
147
148         assertFalse("Test timed out", mTimedOut);
149         assertTrue("Pulse was not extended", mLivesPastNormalTime);
150         assertFalse(mHeadsUpManager.isAlerting(mEntry.key));
151     }
152 }