Rohan Shah | 524cf7b | 2018-03-15 14:40:02 -0700 | [diff] [blame] | 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 | |
Rohan Shah | 20790b8 | 2018-07-02 17:21:04 -0700 | [diff] [blame] | 17 | package com.android.systemui.statusbar.notification.row; |
Rohan Shah | 524cf7b | 2018-03-15 14:40:02 -0700 | [diff] [blame] | 18 | |
Rohan Shah | 524cf7b | 2018-03-15 14:40:02 -0700 | [diff] [blame] | 19 | import static android.service.notification.NotificationListenerService.Ranking.USER_SENTIMENT_NEGATIVE; |
| 20 | import static android.service.notification.NotificationListenerService.Ranking.USER_SENTIMENT_NEUTRAL; |
| 21 | import static android.service.notification.NotificationListenerService.Ranking.USER_SENTIMENT_POSITIVE; |
Jason Monk | a716bac | 2018-12-05 15:48:21 -0500 | [diff] [blame] | 22 | |
Rohan Shah | 524cf7b | 2018-03-15 14:40:02 -0700 | [diff] [blame] | 23 | import static org.junit.Assert.assertFalse; |
| 24 | import static org.junit.Assert.assertTrue; |
| 25 | import static org.mockito.ArgumentMatchers.any; |
| 26 | import static org.mockito.ArgumentMatchers.anyInt; |
| 27 | import static org.mockito.Mockito.spy; |
| 28 | import static org.mockito.Mockito.times; |
| 29 | import static org.mockito.Mockito.verify; |
| 30 | import static org.mockito.Mockito.when; |
| 31 | |
Jason Monk | a716bac | 2018-12-05 15:48:21 -0500 | [diff] [blame] | 32 | import android.content.Context; |
| 33 | import android.support.test.filters.FlakyTest; |
| 34 | import android.support.test.filters.SmallTest; |
| 35 | import android.testing.AndroidTestingRunner; |
| 36 | import android.testing.TestableLooper; |
| 37 | import android.view.View; |
| 38 | |
| 39 | import com.android.systemui.SysuiTestCase; |
| 40 | import com.android.systemui.plugins.statusbar.NotificationMenuRowPlugin; |
| 41 | import com.android.systemui.statusbar.NotificationTestHelper; |
| 42 | import com.android.systemui.statusbar.notification.NotificationEntryManager; |
| 43 | import com.android.systemui.util.Assert; |
| 44 | |
| 45 | import org.junit.Before; |
| 46 | import org.junit.Test; |
| 47 | import org.mockito.Mock; |
| 48 | import org.mockito.MockitoAnnotations; |
| 49 | |
Rohan Shah | 524cf7b | 2018-03-15 14:40:02 -0700 | [diff] [blame] | 50 | /** |
| 51 | * Tests for {@link NotificationBlockingHelperManager}. |
| 52 | */ |
| 53 | @SmallTest |
Rohan Shah | ae5fdf0 | 2018-05-09 18:59:44 -0700 | [diff] [blame] | 54 | @FlakyTest |
Rohan Shah | 524cf7b | 2018-03-15 14:40:02 -0700 | [diff] [blame] | 55 | @org.junit.runner.RunWith(AndroidTestingRunner.class) |
Jason Monk | a716bac | 2018-12-05 15:48:21 -0500 | [diff] [blame] | 56 | @TestableLooper.RunWithLooper |
Rohan Shah | 524cf7b | 2018-03-15 14:40:02 -0700 | [diff] [blame] | 57 | public class NotificationBlockingHelperManagerTest extends SysuiTestCase { |
| 58 | |
| 59 | private NotificationBlockingHelperManager mBlockingHelperManager; |
| 60 | |
| 61 | private NotificationTestHelper mHelper; |
| 62 | |
Rohan Shah | 524cf7b | 2018-03-15 14:40:02 -0700 | [diff] [blame] | 63 | @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 Monk | a716bac | 2018-12-05 15:48:21 -0500 | [diff] [blame] | 70 | Assert.sMainLooper = TestableLooper.get(this).getLooper(); |
Rohan Shah | ae5fdf0 | 2018-05-09 18:59:44 -0700 | [diff] [blame] | 71 | MockitoAnnotations.initMocks(this); |
Rohan Shah | 524cf7b | 2018-03-15 14:40:02 -0700 | [diff] [blame] | 72 | when(mGutsManager.openGuts( |
| 73 | any(View.class), |
| 74 | anyInt(), |
| 75 | anyInt(), |
| 76 | any(NotificationMenuRowPlugin.MenuItem.class))) |
| 77 | .thenReturn(true); |
Rohan Shah | ae5fdf0 | 2018-05-09 18:59:44 -0700 | [diff] [blame] | 78 | when(mMenuRow.getLongpressMenuItem(any(Context.class))).thenReturn(mMenuItem); |
Rohan Shah | 524cf7b | 2018-03-15 14:40:02 -0700 | [diff] [blame] | 79 | mDependency.injectTestDependency(NotificationGutsManager.class, mGutsManager); |
| 80 | mDependency.injectTestDependency(NotificationEntryManager.class, mEntryManager); |
Rohan Shah | ae5fdf0 | 2018-05-09 18:59:44 -0700 | [diff] [blame] | 81 | |
| 82 | mHelper = new NotificationTestHelper(mContext); |
| 83 | |
| 84 | mBlockingHelperManager = new NotificationBlockingHelperManager(mContext); |
| 85 | // By default, have the shade visible/expanded. |
| 86 | mBlockingHelperManager.setNotificationShadeExpanded(1f); |
Rohan Shah | 524cf7b | 2018-03-15 14:40:02 -0700 | [diff] [blame] | 87 | } |
| 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 Shah | ae5fdf0 | 2018-05-09 18:59:44 -0700 | [diff] [blame] | 98 | ExpandableNotificationRow row = createBlockableRowSpy(); |
Rohan Shah | 524cf7b | 2018-03-15 14:40:02 -0700 | [diff] [blame] | 99 | 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 Shah | ae5fdf0 | 2018-05-09 18:59:44 -0700 | [diff] [blame] | 111 | ExpandableNotificationRow row = createBlockableRowSpy(); |
Rohan Shah | 524cf7b | 2018-03-15 14:40:02 -0700 | [diff] [blame] | 112 | 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 Shah | 63411fc | 2018-03-28 19:05:52 -0700 | [diff] [blame] | 124 | ExpandableNotificationRow row = createBlockableRowSpy(); |
Rohan Shah | 524cf7b | 2018-03-15 14:40:02 -0700 | [diff] [blame] | 125 | row.getEntry().userSentiment = USER_SENTIMENT_NEGATIVE; |
Rohan Shah | 524cf7b | 2018-03-15 14:40:02 -0700 | [diff] [blame] | 126 | |
| 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 Shah | 63411fc | 2018-03-28 19:05:52 -0700 | [diff] [blame] | 135 | ExpandableNotificationRow groupRow = createBlockableGroupRowSpy(10); |
Rohan Shah | 524cf7b | 2018-03-15 14:40:02 -0700 | [diff] [blame] | 136 | groupRow.getEntry().userSentiment = USER_SENTIMENT_NEGATIVE; |
Rohan Shah | 524cf7b | 2018-03-15 14:40:02 -0700 | [diff] [blame] | 137 | |
| 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 Shah | 63411fc | 2018-03-28 19:05:52 -0700 | [diff] [blame] | 146 | ExpandableNotificationRow groupRow = createBlockableGroupRowSpy(1); |
Rohan Shah | 524cf7b | 2018-03-15 14:40:02 -0700 | [diff] [blame] | 147 | // 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 Reynolds | 0abae11 | 2018-06-08 10:36:57 -0400 | [diff] [blame] | 152 | assertFalse(childRow.getIsNonblockable()); |
Rohan Shah | 524cf7b | 2018-03-15 14:40:02 -0700 | [diff] [blame] | 153 | |
| 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 Shah | 63411fc | 2018-03-28 19:05:52 -0700 | [diff] [blame] | 161 | ExpandableNotificationRow row = createBlockableRowSpy(); |
Rohan Shah | 524cf7b | 2018-03-15 14:40:02 -0700 | [diff] [blame] | 162 | row.getEntry().userSentiment = USER_SENTIMENT_NEUTRAL; |
Rohan Shah | 524cf7b | 2018-03-15 14:40:02 -0700 | [diff] [blame] | 163 | |
| 164 | assertFalse(mBlockingHelperManager.perhapsShowBlockingHelper(row, mMenuRow)); |
| 165 | } |
| 166 | |
| 167 | @Test |
| 168 | public void testPerhapsShowBlockingHelper_notShownDueToPositiveUserSentiment() |
| 169 | throws Exception { |
Rohan Shah | 63411fc | 2018-03-28 19:05:52 -0700 | [diff] [blame] | 170 | ExpandableNotificationRow row = createBlockableRowSpy(); |
Rohan Shah | 524cf7b | 2018-03-15 14:40:02 -0700 | [diff] [blame] | 171 | row.getEntry().userSentiment = USER_SENTIMENT_POSITIVE; |
Rohan Shah | 524cf7b | 2018-03-15 14:40:02 -0700 | [diff] [blame] | 172 | |
| 173 | assertFalse(mBlockingHelperManager.perhapsShowBlockingHelper(row, mMenuRow)); |
| 174 | } |
| 175 | |
| 176 | @Test |
| 177 | public void testPerhapsShowBlockingHelper_notShownDueToShadeVisibility() throws Exception { |
Rohan Shah | 63411fc | 2018-03-28 19:05:52 -0700 | [diff] [blame] | 178 | ExpandableNotificationRow row = createBlockableRowSpy(); |
Rohan Shah | 524cf7b | 2018-03-15 14:40:02 -0700 | [diff] [blame] | 179 | 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 Shah | 63411fc | 2018-03-28 19:05:52 -0700 | [diff] [blame] | 187 | public void testPerhapsShowBlockingHelper_notShownDueToNonblockability() throws Exception { |
| 188 | ExpandableNotificationRow row = createBlockableRowSpy(); |
| 189 | when(row.getIsNonblockable()).thenReturn(true); |
| 190 | row.getEntry().userSentiment = USER_SENTIMENT_NEGATIVE; |
Rohan Shah | 63411fc | 2018-03-28 19:05:52 -0700 | [diff] [blame] | 191 | |
| 192 | assertFalse(mBlockingHelperManager.perhapsShowBlockingHelper(row, mMenuRow)); |
| 193 | } |
| 194 | |
| 195 | @Test |
Rohan Shah | 524cf7b | 2018-03-15 14:40:02 -0700 | [diff] [blame] | 196 | public void testPerhapsShowBlockingHelper_notShownAsNotificationIsInMultipleChildGroup() |
| 197 | throws Exception { |
Rohan Shah | 63411fc | 2018-03-28 19:05:52 -0700 | [diff] [blame] | 198 | ExpandableNotificationRow groupRow = createBlockableGroupRowSpy(2); |
Rohan Shah | 524cf7b | 2018-03-15 14:40:02 -0700 | [diff] [blame] | 199 | // 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 Shah | 524cf7b | 2018-03-15 14:40:02 -0700 | [diff] [blame] | 204 | |
| 205 | assertFalse(mBlockingHelperManager.perhapsShowBlockingHelper(childRow, mMenuRow)); |
| 206 | } |
| 207 | |
| 208 | @Test |
| 209 | public void testBlockingHelperShowAndDismiss() throws Exception{ |
Rohan Shah | ae5fdf0 | 2018-05-09 18:59:44 -0700 | [diff] [blame] | 210 | ExpandableNotificationRow row = createBlockableRowSpy(); |
Rohan Shah | 524cf7b | 2018-03-15 14:40:02 -0700 | [diff] [blame] | 211 | row.getEntry().userSentiment = USER_SENTIMENT_NEGATIVE; |
| 212 | when(row.isAttachedToWindow()).thenReturn(true); |
Rohan Shah | 524cf7b | 2018-03-15 14:40:02 -0700 | [diff] [blame] | 213 | |
| 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 Shah | 63411fc | 2018-03-28 19:05:52 -0700 | [diff] [blame] | 225 | |
Julia Reynolds | 0abae11 | 2018-06-08 10:36:57 -0400 | [diff] [blame] | 226 | @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 Shah | 63411fc | 2018-03-28 19:05:52 -0700 | [diff] [blame] | 244 | 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 Shah | 524cf7b | 2018-03-15 14:40:02 -0700 | [diff] [blame] | 255 | } |