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