blob: 669b98e1b279497568f3185ed365e9f4aa400e54 [file] [log] [blame]
Rohan Shah524cf7b2018-03-15 14:40:02 -07001/*
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
Rohan Shah20790b82018-07-02 17:21:04 -070017package com.android.systemui.statusbar.notification.row;
Rohan Shah524cf7b2018-03-15 14:40:02 -070018
Rohan Shah524cf7b2018-03-15 14:40:02 -070019import static android.service.notification.NotificationListenerService.Ranking.USER_SENTIMENT_NEGATIVE;
20import static android.service.notification.NotificationListenerService.Ranking.USER_SENTIMENT_NEUTRAL;
21import static android.service.notification.NotificationListenerService.Ranking.USER_SENTIMENT_POSITIVE;
Jason Monka716bac2018-12-05 15:48:21 -050022
Rohan Shah524cf7b2018-03-15 14:40:02 -070023import static org.junit.Assert.assertFalse;
24import static org.junit.Assert.assertTrue;
25import static org.mockito.ArgumentMatchers.any;
26import static org.mockito.ArgumentMatchers.anyInt;
27import static org.mockito.Mockito.spy;
28import static org.mockito.Mockito.times;
29import static org.mockito.Mockito.verify;
30import static org.mockito.Mockito.when;
31
Jason Monka716bac2018-12-05 15:48:21 -050032import android.content.Context;
33import android.support.test.filters.FlakyTest;
34import android.support.test.filters.SmallTest;
35import android.testing.AndroidTestingRunner;
36import android.testing.TestableLooper;
37import android.view.View;
38
39import com.android.systemui.SysuiTestCase;
40import com.android.systemui.plugins.statusbar.NotificationMenuRowPlugin;
41import com.android.systemui.statusbar.NotificationTestHelper;
42import com.android.systemui.statusbar.notification.NotificationEntryManager;
43import com.android.systemui.util.Assert;
44
45import org.junit.Before;
46import org.junit.Test;
47import org.mockito.Mock;
48import org.mockito.MockitoAnnotations;
49
Rohan Shah524cf7b2018-03-15 14:40:02 -070050/**
51 * Tests for {@link NotificationBlockingHelperManager}.
52 */
53@SmallTest
Rohan Shahae5fdf02018-05-09 18:59:44 -070054@FlakyTest
Rohan Shah524cf7b2018-03-15 14:40:02 -070055@org.junit.runner.RunWith(AndroidTestingRunner.class)
Jason Monka716bac2018-12-05 15:48:21 -050056@TestableLooper.RunWithLooper
Rohan Shah524cf7b2018-03-15 14:40:02 -070057public class NotificationBlockingHelperManagerTest extends SysuiTestCase {
58
59 private NotificationBlockingHelperManager mBlockingHelperManager;
60
61 private NotificationTestHelper mHelper;
62
Rohan Shah524cf7b2018-03-15 14:40:02 -070063 @Mock private NotificationGutsManager mGutsManager;
64 @Mock private NotificationEntryManager mEntryManager;
65 @Mock private NotificationMenuRow mMenuRow;
66 @Mock private NotificationMenuRowPlugin.MenuItem mMenuItem;
67
68 @Before
69 public void setUp() {
Jason Monka716bac2018-12-05 15:48:21 -050070 Assert.sMainLooper = TestableLooper.get(this).getLooper();
Rohan Shahae5fdf02018-05-09 18:59:44 -070071 MockitoAnnotations.initMocks(this);
Rohan Shah524cf7b2018-03-15 14:40:02 -070072 when(mGutsManager.openGuts(
73 any(View.class),
74 anyInt(),
75 anyInt(),
76 any(NotificationMenuRowPlugin.MenuItem.class)))
77 .thenReturn(true);
Rohan Shahae5fdf02018-05-09 18:59:44 -070078 when(mMenuRow.getLongpressMenuItem(any(Context.class))).thenReturn(mMenuItem);
Rohan Shah524cf7b2018-03-15 14:40:02 -070079 mDependency.injectTestDependency(NotificationGutsManager.class, mGutsManager);
80 mDependency.injectTestDependency(NotificationEntryManager.class, mEntryManager);
Rohan Shahae5fdf02018-05-09 18:59:44 -070081
82 mHelper = new NotificationTestHelper(mContext);
83
84 mBlockingHelperManager = new NotificationBlockingHelperManager(mContext);
85 // By default, have the shade visible/expanded.
86 mBlockingHelperManager.setNotificationShadeExpanded(1f);
Rohan Shah524cf7b2018-03-15 14:40:02 -070087 }
88
89 @Test
90 public void testDismissCurrentBlockingHelper_nullBlockingHelperRow() {
91 // By default, this shouldn't dismiss (no pointers/vars set up!)
92 assertFalse(mBlockingHelperManager.dismissCurrentBlockingHelper());
93 assertTrue(mBlockingHelperManager.isBlockingHelperRowNull());
94 }
95
96 @Test
97 public void testDismissCurrentBlockingHelper_withDetachedBlockingHelperRow() throws Exception {
Rohan Shahae5fdf02018-05-09 18:59:44 -070098 ExpandableNotificationRow row = createBlockableRowSpy();
Rohan Shah524cf7b2018-03-15 14:40:02 -070099 row.setBlockingHelperShowing(true);
100 when(row.isAttachedToWindow()).thenReturn(false);
101 mBlockingHelperManager.setBlockingHelperRowForTest(row);
102
103 assertTrue(mBlockingHelperManager.dismissCurrentBlockingHelper());
104 assertTrue(mBlockingHelperManager.isBlockingHelperRowNull());
105
106 verify(mEntryManager, times(0)).updateNotifications();
107 }
108
109 @Test
110 public void testDismissCurrentBlockingHelper_withAttachedBlockingHelperRow() throws Exception {
Rohan Shahae5fdf02018-05-09 18:59:44 -0700111 ExpandableNotificationRow row = createBlockableRowSpy();
Rohan Shah524cf7b2018-03-15 14:40:02 -0700112 row.setBlockingHelperShowing(true);
113 when(row.isAttachedToWindow()).thenReturn(true);
114 mBlockingHelperManager.setBlockingHelperRowForTest(row);
115
116 assertTrue(mBlockingHelperManager.dismissCurrentBlockingHelper());
117 assertTrue(mBlockingHelperManager.isBlockingHelperRowNull());
118
119 verify(mEntryManager).updateNotifications();
120 }
121
122 @Test
123 public void testPerhapsShowBlockingHelper_shown() throws Exception {
Rohan Shah63411fc2018-03-28 19:05:52 -0700124 ExpandableNotificationRow row = createBlockableRowSpy();
Rohan Shah524cf7b2018-03-15 14:40:02 -0700125 row.getEntry().userSentiment = USER_SENTIMENT_NEGATIVE;
Rohan Shah524cf7b2018-03-15 14:40:02 -0700126
127 assertTrue(mBlockingHelperManager.perhapsShowBlockingHelper(row, mMenuRow));
128
129 verify(mGutsManager).openGuts(row, 0, 0, mMenuItem);
130 }
131
132
133 @Test
134 public void testPerhapsShowBlockingHelper_shownForLargeGroup() throws Exception {
Rohan Shah63411fc2018-03-28 19:05:52 -0700135 ExpandableNotificationRow groupRow = createBlockableGroupRowSpy(10);
Rohan Shah524cf7b2018-03-15 14:40:02 -0700136 groupRow.getEntry().userSentiment = USER_SENTIMENT_NEGATIVE;
Rohan Shah524cf7b2018-03-15 14:40:02 -0700137
138 assertTrue(mBlockingHelperManager.perhapsShowBlockingHelper(groupRow, mMenuRow));
139
140 verify(mGutsManager).openGuts(groupRow, 0, 0, mMenuItem);
141 }
142
143 @Test
144 public void testPerhapsShowBlockingHelper_shownForOnlyChildNotification()
145 throws Exception {
Rohan Shah63411fc2018-03-28 19:05:52 -0700146 ExpandableNotificationRow groupRow = createBlockableGroupRowSpy(1);
Rohan Shah524cf7b2018-03-15 14:40:02 -0700147 // Explicitly get the children container & call getViewAtPosition on it instead of the row
148 // as other factors such as view expansion may cause us to get the parent row back instead
149 // of the child row.
150 ExpandableNotificationRow childRow = groupRow.getChildrenContainer().getViewAtPosition(0);
151 childRow.getEntry().userSentiment = USER_SENTIMENT_NEGATIVE;
Julia Reynolds0abae112018-06-08 10:36:57 -0400152 assertFalse(childRow.getIsNonblockable());
Rohan Shah524cf7b2018-03-15 14:40:02 -0700153
154 assertTrue(mBlockingHelperManager.perhapsShowBlockingHelper(childRow, mMenuRow));
155
156 verify(mGutsManager).openGuts(childRow, 0, 0, mMenuItem);
157 }
158
159 @Test
160 public void testPerhapsShowBlockingHelper_notShownDueToNeutralUserSentiment() throws Exception {
Rohan Shah63411fc2018-03-28 19:05:52 -0700161 ExpandableNotificationRow row = createBlockableRowSpy();
Rohan Shah524cf7b2018-03-15 14:40:02 -0700162 row.getEntry().userSentiment = USER_SENTIMENT_NEUTRAL;
Rohan Shah524cf7b2018-03-15 14:40:02 -0700163
164 assertFalse(mBlockingHelperManager.perhapsShowBlockingHelper(row, mMenuRow));
165 }
166
167 @Test
168 public void testPerhapsShowBlockingHelper_notShownDueToPositiveUserSentiment()
169 throws Exception {
Rohan Shah63411fc2018-03-28 19:05:52 -0700170 ExpandableNotificationRow row = createBlockableRowSpy();
Rohan Shah524cf7b2018-03-15 14:40:02 -0700171 row.getEntry().userSentiment = USER_SENTIMENT_POSITIVE;
Rohan Shah524cf7b2018-03-15 14:40:02 -0700172
173 assertFalse(mBlockingHelperManager.perhapsShowBlockingHelper(row, mMenuRow));
174 }
175
176 @Test
177 public void testPerhapsShowBlockingHelper_notShownDueToShadeVisibility() throws Exception {
Rohan Shah63411fc2018-03-28 19:05:52 -0700178 ExpandableNotificationRow row = createBlockableRowSpy();
Rohan Shah524cf7b2018-03-15 14:40:02 -0700179 row.getEntry().userSentiment = USER_SENTIMENT_NEGATIVE;
180 // Hide the shade
181 mBlockingHelperManager.setNotificationShadeExpanded(0f);
182
183 assertFalse(mBlockingHelperManager.perhapsShowBlockingHelper(row, mMenuRow));
184 }
185
186 @Test
Rohan Shah63411fc2018-03-28 19:05:52 -0700187 public void testPerhapsShowBlockingHelper_notShownDueToNonblockability() throws Exception {
188 ExpandableNotificationRow row = createBlockableRowSpy();
189 when(row.getIsNonblockable()).thenReturn(true);
190 row.getEntry().userSentiment = USER_SENTIMENT_NEGATIVE;
Rohan Shah63411fc2018-03-28 19:05:52 -0700191
192 assertFalse(mBlockingHelperManager.perhapsShowBlockingHelper(row, mMenuRow));
193 }
194
195 @Test
Rohan Shah524cf7b2018-03-15 14:40:02 -0700196 public void testPerhapsShowBlockingHelper_notShownAsNotificationIsInMultipleChildGroup()
197 throws Exception {
Rohan Shah63411fc2018-03-28 19:05:52 -0700198 ExpandableNotificationRow groupRow = createBlockableGroupRowSpy(2);
Rohan Shah524cf7b2018-03-15 14:40:02 -0700199 // Explicitly get the children container & call getViewAtPosition on it instead of the row
200 // as other factors such as view expansion may cause us to get the parent row back instead
201 // of the child row.
202 ExpandableNotificationRow childRow = groupRow.getChildrenContainer().getViewAtPosition(0);
203 childRow.getEntry().userSentiment = USER_SENTIMENT_NEGATIVE;
Rohan Shah524cf7b2018-03-15 14:40:02 -0700204
205 assertFalse(mBlockingHelperManager.perhapsShowBlockingHelper(childRow, mMenuRow));
206 }
207
208 @Test
209 public void testBlockingHelperShowAndDismiss() throws Exception{
Rohan Shahae5fdf02018-05-09 18:59:44 -0700210 ExpandableNotificationRow row = createBlockableRowSpy();
Rohan Shah524cf7b2018-03-15 14:40:02 -0700211 row.getEntry().userSentiment = USER_SENTIMENT_NEGATIVE;
212 when(row.isAttachedToWindow()).thenReturn(true);
Rohan Shah524cf7b2018-03-15 14:40:02 -0700213
214 // Show check
215 assertTrue(mBlockingHelperManager.perhapsShowBlockingHelper(row, mMenuRow));
216
217 verify(mGutsManager).openGuts(row, 0, 0, mMenuItem);
218
219 // Dismiss check
220 assertTrue(mBlockingHelperManager.dismissCurrentBlockingHelper());
221 assertTrue(mBlockingHelperManager.isBlockingHelperRowNull());
222
223 verify(mEntryManager).updateNotifications();
224 }
Rohan Shah63411fc2018-03-28 19:05:52 -0700225
Julia Reynolds0abae112018-06-08 10:36:57 -0400226 @Test
227 public void testNonBlockable_package() {
228 mBlockingHelperManager.setNonBlockablePkgs(new String[] {"banana", "strawberry:pie"});
229
230 assertFalse(mBlockingHelperManager.isNonblockable("orange", "pie"));
231
232 assertTrue(mBlockingHelperManager.isNonblockable("banana", "pie"));
233 }
234
235 @Test
236 public void testNonBlockable_channel() {
237 mBlockingHelperManager.setNonBlockablePkgs(new String[] {"banana", "strawberry:pie"});
238
239 assertFalse(mBlockingHelperManager.isNonblockable("strawberry", "shortcake"));
240
241 assertTrue(mBlockingHelperManager.isNonblockable("strawberry", "pie"));
242 }
243
Rohan Shah63411fc2018-03-28 19:05:52 -0700244 private ExpandableNotificationRow createBlockableRowSpy() throws Exception {
245 ExpandableNotificationRow row = spy(mHelper.createRow());
246 when(row.getIsNonblockable()).thenReturn(false);
247 return row;
248 }
249
250 private ExpandableNotificationRow createBlockableGroupRowSpy(int numChildren) throws Exception {
251 ExpandableNotificationRow row = spy(mHelper.createGroup(numChildren));
252 when(row.getIsNonblockable()).thenReturn(false);
253 return row;
254 }
Rohan Shah524cf7b2018-03-15 14:40:02 -0700255}