Eliot Courtney | 3985ad5 | 2017-11-17 16:51:52 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 |
Rohan Shah | 20790b8 | 2018-07-02 17:21:04 -0700 | [diff] [blame] | 14 | * limitations under the License |
Eliot Courtney | 3985ad5 | 2017-11-17 16:51:52 +0900 | [diff] [blame] | 15 | */ |
| 16 | |
Rohan Shah | 20790b8 | 2018-07-02 17:21:04 -0700 | [diff] [blame] | 17 | package com.android.systemui.statusbar.notification.logging; |
Eliot Courtney | 3985ad5 | 2017-11-17 16:51:52 +0900 | [diff] [blame] | 18 | |
Chris Wren | 2e89e8d | 2018-05-17 18:55:42 -0400 | [diff] [blame] | 19 | import static org.junit.Assert.assertArrayEquals; |
Eliot Courtney | 3985ad5 | 2017-11-17 16:51:52 +0900 | [diff] [blame] | 20 | import static org.mockito.ArgumentMatchers.any; |
Chris Wren | 2e89e8d | 2018-05-17 18:55:42 -0400 | [diff] [blame] | 21 | import static org.mockito.Mockito.doAnswer; |
Jason Monk | d97204c | 2018-12-21 15:49:04 -0500 | [diff] [blame] | 22 | import static org.mockito.Mockito.mock; |
Eliot Courtney | 3985ad5 | 2017-11-17 16:51:52 +0900 | [diff] [blame] | 23 | import static org.mockito.Mockito.never; |
| 24 | import static org.mockito.Mockito.times; |
| 25 | import static org.mockito.Mockito.verify; |
| 26 | import static org.mockito.Mockito.when; |
| 27 | |
| 28 | import android.app.Notification; |
| 29 | import android.os.Handler; |
| 30 | import android.os.Looper; |
| 31 | import android.os.UserHandle; |
Eliot Courtney | 3985ad5 | 2017-11-17 16:51:52 +0900 | [diff] [blame] | 32 | import android.service.notification.StatusBarNotification; |
Eliot Courtney | 3985ad5 | 2017-11-17 16:51:52 +0900 | [diff] [blame] | 33 | import android.testing.AndroidTestingRunner; |
| 34 | import android.testing.TestableLooper; |
| 35 | |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 36 | import androidx.test.filters.SmallTest; |
| 37 | |
Eliot Courtney | 3985ad5 | 2017-11-17 16:51:52 +0900 | [diff] [blame] | 38 | import com.android.internal.statusbar.IStatusBarService; |
| 39 | import com.android.internal.statusbar.NotificationVisibility; |
Jason Monk | d97204c | 2018-12-21 15:49:04 -0500 | [diff] [blame] | 40 | import com.android.systemui.Dependency; |
Eliot Courtney | 3985ad5 | 2017-11-17 16:51:52 +0900 | [diff] [blame] | 41 | import com.android.systemui.SysuiTestCase; |
Jason Monk | d97204c | 2018-12-21 15:49:04 -0500 | [diff] [blame] | 42 | import com.android.systemui.UiOffloadThread; |
Rohan Shah | 20790b8 | 2018-07-02 17:21:04 -0700 | [diff] [blame] | 43 | import com.android.systemui.statusbar.NotificationListener; |
Beverly | 8fdb533 | 2019-02-04 14:29:49 -0500 | [diff] [blame] | 44 | import com.android.systemui.statusbar.StatusBarStateControllerImpl; |
Ned Burns | 761af0d | 2019-01-03 13:51:29 -0500 | [diff] [blame] | 45 | import com.android.systemui.statusbar.notification.NotificationEntryListener; |
Jason Monk | a716bac | 2018-12-05 15:48:21 -0500 | [diff] [blame] | 46 | import com.android.systemui.statusbar.notification.NotificationEntryManager; |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 47 | import com.android.systemui.statusbar.notification.collection.NotificationData; |
| 48 | import com.android.systemui.statusbar.notification.collection.NotificationEntry; |
Rohan Shah | 20790b8 | 2018-07-02 17:21:04 -0700 | [diff] [blame] | 49 | import com.android.systemui.statusbar.notification.row.ExpandableNotificationRow; |
| 50 | import com.android.systemui.statusbar.notification.stack.NotificationListContainer; |
Jason Monk | a716bac | 2018-12-05 15:48:21 -0500 | [diff] [blame] | 51 | |
Eliot Courtney | 3985ad5 | 2017-11-17 16:51:52 +0900 | [diff] [blame] | 52 | import com.google.android.collect.Lists; |
| 53 | |
| 54 | import org.junit.Before; |
| 55 | import org.junit.Test; |
| 56 | import org.junit.runner.RunWith; |
Ned Burns | 761af0d | 2019-01-03 13:51:29 -0500 | [diff] [blame] | 57 | import org.mockito.ArgumentCaptor; |
| 58 | import org.mockito.Captor; |
Eliot Courtney | 3985ad5 | 2017-11-17 16:51:52 +0900 | [diff] [blame] | 59 | import org.mockito.Mock; |
| 60 | import org.mockito.Mockito; |
| 61 | import org.mockito.MockitoAnnotations; |
Chris Wren | 2e89e8d | 2018-05-17 18:55:42 -0400 | [diff] [blame] | 62 | |
| 63 | import java.util.concurrent.ConcurrentLinkedQueue; |
Eliot Courtney | 3985ad5 | 2017-11-17 16:51:52 +0900 | [diff] [blame] | 64 | |
| 65 | @SmallTest |
| 66 | @RunWith(AndroidTestingRunner.class) |
Jason Monk | a716bac | 2018-12-05 15:48:21 -0500 | [diff] [blame] | 67 | @TestableLooper.RunWithLooper |
Eliot Courtney | 3985ad5 | 2017-11-17 16:51:52 +0900 | [diff] [blame] | 68 | public class NotificationLoggerTest extends SysuiTestCase { |
| 69 | private static final String TEST_PACKAGE_NAME = "test"; |
| 70 | private static final int TEST_UID = 0; |
| 71 | |
Eliot Courtney | 2b4c3a0 | 2017-11-27 13:27:46 +0900 | [diff] [blame] | 72 | @Mock private NotificationListContainer mListContainer; |
Eliot Courtney | 3985ad5 | 2017-11-17 16:51:52 +0900 | [diff] [blame] | 73 | @Mock private IStatusBarService mBarService; |
| 74 | @Mock private NotificationData mNotificationData; |
| 75 | @Mock private ExpandableNotificationRow mRow; |
Tony Mak | 202f25d | 2019-01-07 14:40:39 +0000 | [diff] [blame] | 76 | @Mock private NotificationLogger.ExpansionStateLogger mExpansionStateLogger; |
Eliot Courtney | 3985ad5 | 2017-11-17 16:51:52 +0900 | [diff] [blame] | 77 | |
Eliot Courtney | 8f56b0e | 2017-12-14 18:54:28 +0900 | [diff] [blame] | 78 | // Dependency mocks: |
| 79 | @Mock private NotificationEntryManager mEntryManager; |
| 80 | @Mock private NotificationListener mListener; |
Ned Burns | 761af0d | 2019-01-03 13:51:29 -0500 | [diff] [blame] | 81 | @Captor private ArgumentCaptor<NotificationEntryListener> mEntryListenerCaptor; |
Eliot Courtney | 8f56b0e | 2017-12-14 18:54:28 +0900 | [diff] [blame] | 82 | |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 83 | private NotificationEntry mEntry; |
Eliot Courtney | 3985ad5 | 2017-11-17 16:51:52 +0900 | [diff] [blame] | 84 | private TestableNotificationLogger mLogger; |
Ned Burns | 761af0d | 2019-01-03 13:51:29 -0500 | [diff] [blame] | 85 | private NotificationEntryListener mNotificationEntryListener; |
Chris Wren | 2e89e8d | 2018-05-17 18:55:42 -0400 | [diff] [blame] | 86 | private ConcurrentLinkedQueue<AssertionError> mErrorQueue = new ConcurrentLinkedQueue<>(); |
Eliot Courtney | 3985ad5 | 2017-11-17 16:51:52 +0900 | [diff] [blame] | 87 | |
| 88 | @Before |
Gus Prevas | ca1b6f7 | 2018-12-28 10:53:11 -0500 | [diff] [blame] | 89 | public void setUp() { |
Eliot Courtney | 3985ad5 | 2017-11-17 16:51:52 +0900 | [diff] [blame] | 90 | MockitoAnnotations.initMocks(this); |
Eliot Courtney | 8f56b0e | 2017-12-14 18:54:28 +0900 | [diff] [blame] | 91 | mDependency.injectTestDependency(NotificationEntryManager.class, mEntryManager); |
| 92 | mDependency.injectTestDependency(NotificationListener.class, mListener); |
Eliot Courtney | 3985ad5 | 2017-11-17 16:51:52 +0900 | [diff] [blame] | 93 | |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 94 | when(mEntryManager.getNotificationData()).thenReturn(mNotificationData); |
Eliot Courtney | 3985ad5 | 2017-11-17 16:51:52 +0900 | [diff] [blame] | 95 | |
Gus Prevas | ca1b6f7 | 2018-12-28 10:53:11 -0500 | [diff] [blame] | 96 | StatusBarNotification sbn = new StatusBarNotification(TEST_PACKAGE_NAME, TEST_PACKAGE_NAME, |
| 97 | 0, null, TEST_UID, |
Eliot Courtney | 3985ad5 | 2017-11-17 16:51:52 +0900 | [diff] [blame] | 98 | 0, new Notification(), UserHandle.CURRENT, null, 0); |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 99 | mEntry = new NotificationEntry(sbn); |
Evan Laird | 9449285 | 2018-10-25 13:43:01 -0400 | [diff] [blame] | 100 | mEntry.setRow(mRow); |
Eliot Courtney | 3985ad5 | 2017-11-17 16:51:52 +0900 | [diff] [blame] | 101 | |
Jason Monk | d97204c | 2018-12-21 15:49:04 -0500 | [diff] [blame] | 102 | mLogger = new TestableNotificationLogger(mListener, Dependency.get(UiOffloadThread.class), |
Beverly | 8fdb533 | 2019-02-04 14:29:49 -0500 | [diff] [blame] | 103 | mEntryManager, mock(StatusBarStateControllerImpl.class), mBarService, |
Tony Mak | 202f25d | 2019-01-07 14:40:39 +0000 | [diff] [blame] | 104 | mExpansionStateLogger); |
Jason Monk | 297c04e | 2018-08-23 17:16:59 -0400 | [diff] [blame] | 105 | mLogger.setUpWithContainer(mListContainer); |
Ned Burns | 761af0d | 2019-01-03 13:51:29 -0500 | [diff] [blame] | 106 | verify(mEntryManager).addNotificationEntryListener(mEntryListenerCaptor.capture()); |
| 107 | mNotificationEntryListener = mEntryListenerCaptor.getValue(); |
Eliot Courtney | 3985ad5 | 2017-11-17 16:51:52 +0900 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | @Test |
| 111 | public void testOnChildLocationsChangedReportsVisibilityChanged() throws Exception { |
Chris Wren | 2e89e8d | 2018-05-17 18:55:42 -0400 | [diff] [blame] | 112 | NotificationVisibility[] newlyVisibleKeys = { |
| 113 | NotificationVisibility.obtain(mEntry.key, 0, 1, true) |
| 114 | }; |
| 115 | NotificationVisibility[] noLongerVisibleKeys = {}; |
Gus Prevas | ca1b6f7 | 2018-12-28 10:53:11 -0500 | [diff] [blame] | 116 | doAnswer(invocation -> { |
Chris Wren | 2e89e8d | 2018-05-17 18:55:42 -0400 | [diff] [blame] | 117 | try { |
| 118 | assertArrayEquals(newlyVisibleKeys, |
| 119 | (NotificationVisibility[]) invocation.getArguments()[0]); |
| 120 | assertArrayEquals(noLongerVisibleKeys, |
| 121 | (NotificationVisibility[]) invocation.getArguments()[1]); |
| 122 | } catch (AssertionError error) { |
| 123 | mErrorQueue.offer(error); |
| 124 | } |
| 125 | return null; |
| 126 | } |
| 127 | ).when(mBarService).onNotificationVisibilityChanged(any(NotificationVisibility[].class), |
| 128 | any(NotificationVisibility[].class)); |
| 129 | |
Eliot Courtney | 2b4c3a0 | 2017-11-27 13:27:46 +0900 | [diff] [blame] | 130 | when(mListContainer.isInVisibleLocation(any())).thenReturn(true); |
Eliot Courtney | 3985ad5 | 2017-11-17 16:51:52 +0900 | [diff] [blame] | 131 | when(mNotificationData.getActiveNotifications()).thenReturn(Lists.newArrayList(mEntry)); |
Eliot Courtney | 2b4c3a0 | 2017-11-27 13:27:46 +0900 | [diff] [blame] | 132 | mLogger.getChildLocationsChangedListenerForTest().onChildLocationsChanged(); |
Jason Monk | 6dceace | 2018-05-15 20:24:07 -0400 | [diff] [blame] | 133 | TestableLooper.get(this).processAllMessages(); |
Eliot Courtney | 3985ad5 | 2017-11-17 16:51:52 +0900 | [diff] [blame] | 134 | waitForUiOffloadThread(); |
| 135 | |
Chris Wren | 2e89e8d | 2018-05-17 18:55:42 -0400 | [diff] [blame] | 136 | if(!mErrorQueue.isEmpty()) { |
| 137 | throw mErrorQueue.poll(); |
| 138 | } |
Eliot Courtney | 3985ad5 | 2017-11-17 16:51:52 +0900 | [diff] [blame] | 139 | |
| 140 | // |mEntry| won't change visibility, so it shouldn't be reported again: |
| 141 | Mockito.reset(mBarService); |
Eliot Courtney | 2b4c3a0 | 2017-11-27 13:27:46 +0900 | [diff] [blame] | 142 | mLogger.getChildLocationsChangedListenerForTest().onChildLocationsChanged(); |
Jason Monk | 6dceace | 2018-05-15 20:24:07 -0400 | [diff] [blame] | 143 | TestableLooper.get(this).processAllMessages(); |
Eliot Courtney | 3985ad5 | 2017-11-17 16:51:52 +0900 | [diff] [blame] | 144 | waitForUiOffloadThread(); |
| 145 | |
| 146 | verify(mBarService, never()).onNotificationVisibilityChanged(any(), any()); |
| 147 | } |
| 148 | |
| 149 | @Test |
| 150 | public void testStoppingNotificationLoggingReportsCurrentNotifications() |
| 151 | throws Exception { |
Eliot Courtney | 2b4c3a0 | 2017-11-27 13:27:46 +0900 | [diff] [blame] | 152 | when(mListContainer.isInVisibleLocation(any())).thenReturn(true); |
Eliot Courtney | 3985ad5 | 2017-11-17 16:51:52 +0900 | [diff] [blame] | 153 | when(mNotificationData.getActiveNotifications()).thenReturn(Lists.newArrayList(mEntry)); |
Eliot Courtney | 2b4c3a0 | 2017-11-27 13:27:46 +0900 | [diff] [blame] | 154 | mLogger.getChildLocationsChangedListenerForTest().onChildLocationsChanged(); |
Jason Monk | 6dceace | 2018-05-15 20:24:07 -0400 | [diff] [blame] | 155 | TestableLooper.get(this).processAllMessages(); |
Eliot Courtney | 3985ad5 | 2017-11-17 16:51:52 +0900 | [diff] [blame] | 156 | waitForUiOffloadThread(); |
| 157 | Mockito.reset(mBarService); |
| 158 | |
| 159 | mLogger.stopNotificationLogging(); |
| 160 | waitForUiOffloadThread(); |
| 161 | // The visibility objects are recycled by NotificationLogger, so we can't use specific |
| 162 | // matchers here. |
| 163 | verify(mBarService, times(1)).onNotificationVisibilityChanged(any(), any()); |
| 164 | } |
| 165 | |
| 166 | private class TestableNotificationLogger extends NotificationLogger { |
| 167 | |
Gus Prevas | ca1b6f7 | 2018-12-28 10:53:11 -0500 | [diff] [blame] | 168 | TestableNotificationLogger(NotificationListener notificationListener, |
Jason Monk | d97204c | 2018-12-21 15:49:04 -0500 | [diff] [blame] | 169 | UiOffloadThread uiOffloadThread, |
| 170 | NotificationEntryManager entryManager, |
Beverly | 8fdb533 | 2019-02-04 14:29:49 -0500 | [diff] [blame] | 171 | StatusBarStateControllerImpl statusBarStateController, |
Tony Mak | 202f25d | 2019-01-07 14:40:39 +0000 | [diff] [blame] | 172 | IStatusBarService barService, |
| 173 | ExpansionStateLogger expansionStateLogger) { |
| 174 | super(notificationListener, uiOffloadThread, entryManager, statusBarStateController, |
| 175 | expansionStateLogger); |
Eliot Courtney | 3985ad5 | 2017-11-17 16:51:52 +0900 | [diff] [blame] | 176 | mBarService = barService; |
Jason Monk | 6dceace | 2018-05-15 20:24:07 -0400 | [diff] [blame] | 177 | // Make this on the current thread so we can wait for it during tests. |
| 178 | mHandler = Handler.createAsync(Looper.myLooper()); |
Eliot Courtney | 3985ad5 | 2017-11-17 16:51:52 +0900 | [diff] [blame] | 179 | } |
| 180 | |
Gus Prevas | ca1b6f7 | 2018-12-28 10:53:11 -0500 | [diff] [blame] | 181 | OnChildLocationsChangedListener |
Eliot Courtney | 3985ad5 | 2017-11-17 16:51:52 +0900 | [diff] [blame] | 182 | getChildLocationsChangedListenerForTest() { |
| 183 | return mNotificationLocationsChangedListener; |
| 184 | } |
Eliot Courtney | 3985ad5 | 2017-11-17 16:51:52 +0900 | [diff] [blame] | 185 | } |
| 186 | } |