Eliot Courtney | 21bc05f | 2017-10-19 17:03:34 +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 |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.systemui.statusbar; |
| 18 | |
Eliot Courtney | 21bc05f | 2017-10-19 17:03:34 +0900 | [diff] [blame] | 19 | import static org.mockito.ArgumentMatchers.any; |
Mady Mellor | c2ff011 | 2019-03-28 14:18:06 -0700 | [diff] [blame] | 20 | import static org.mockito.ArgumentMatchers.anyInt; |
| 21 | import static org.mockito.ArgumentMatchers.eq; |
Julia Reynolds | 12ad7ca | 2019-01-28 09:29:16 -0500 | [diff] [blame] | 22 | import static org.mockito.Mockito.mock; |
Eliot Courtney | 21bc05f | 2017-10-19 17:03:34 +0900 | [diff] [blame] | 23 | import static org.mockito.Mockito.verify; |
| 24 | import static org.mockito.Mockito.when; |
| 25 | |
| 26 | import android.app.Notification; |
Julia Reynolds | 12ad7ca | 2019-01-28 09:29:16 -0500 | [diff] [blame] | 27 | import android.app.NotificationManager; |
Eliot Courtney | 21bc05f | 2017-10-19 17:03:34 +0900 | [diff] [blame] | 28 | import android.os.Handler; |
Eliot Courtney | 21bc05f | 2017-10-19 17:03:34 +0900 | [diff] [blame] | 29 | import android.os.UserHandle; |
| 30 | import android.service.notification.NotificationListenerService; |
| 31 | import android.service.notification.StatusBarNotification; |
Eliot Courtney | 21bc05f | 2017-10-19 17:03:34 +0900 | [diff] [blame] | 32 | import android.testing.AndroidTestingRunner; |
| 33 | import android.testing.TestableLooper; |
| 34 | |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 35 | import androidx.test.filters.SmallTest; |
| 36 | |
Jason Monk | 297c04e | 2018-08-23 17:16:59 -0400 | [diff] [blame] | 37 | import com.android.systemui.Dependency; |
Eliot Courtney | 21bc05f | 2017-10-19 17:03:34 +0900 | [diff] [blame] | 38 | import com.android.systemui.SysuiTestCase; |
Rohan Shah | 20790b8 | 2018-07-02 17:21:04 -0700 | [diff] [blame] | 39 | import com.android.systemui.statusbar.notification.NotificationEntryManager; |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 40 | import com.android.systemui.statusbar.notification.collection.NotificationData; |
| 41 | import com.android.systemui.statusbar.notification.collection.NotificationEntry; |
Eliot Courtney | 21bc05f | 2017-10-19 17:03:34 +0900 | [diff] [blame] | 42 | |
| 43 | import org.junit.Before; |
| 44 | import org.junit.Test; |
| 45 | import org.junit.runner.RunWith; |
Eliot Courtney | 8f56b0e | 2017-12-14 18:54:28 +0900 | [diff] [blame] | 46 | import org.mockito.Mock; |
| 47 | import org.mockito.MockitoAnnotations; |
Eliot Courtney | 21bc05f | 2017-10-19 17:03:34 +0900 | [diff] [blame] | 48 | |
Eliot Courtney | 21bc05f | 2017-10-19 17:03:34 +0900 | [diff] [blame] | 49 | @SmallTest |
| 50 | @RunWith(AndroidTestingRunner.class) |
Jason Monk | a716bac | 2018-12-05 15:48:21 -0500 | [diff] [blame] | 51 | @TestableLooper.RunWithLooper |
Eliot Courtney | 21bc05f | 2017-10-19 17:03:34 +0900 | [diff] [blame] | 52 | public class NotificationListenerTest extends SysuiTestCase { |
| 53 | private static final String TEST_PACKAGE_NAME = "test"; |
| 54 | private static final int TEST_UID = 0; |
| 55 | |
Eliot Courtney | 8f56b0e | 2017-12-14 18:54:28 +0900 | [diff] [blame] | 56 | @Mock private NotificationPresenter mPresenter; |
| 57 | @Mock private NotificationListenerService.RankingMap mRanking; |
| 58 | @Mock private NotificationData mNotificationData; |
| 59 | |
| 60 | // Dependency mocks: |
| 61 | @Mock private NotificationEntryManager mEntryManager; |
| 62 | @Mock private NotificationRemoteInputManager mRemoteInputManager; |
Julia Reynolds | 12ad7ca | 2019-01-28 09:29:16 -0500 | [diff] [blame] | 63 | @Mock private NotificationManager mNotificationManager; |
Eliot Courtney | 8f56b0e | 2017-12-14 18:54:28 +0900 | [diff] [blame] | 64 | |
Eliot Courtney | 21bc05f | 2017-10-19 17:03:34 +0900 | [diff] [blame] | 65 | private NotificationListener mListener; |
| 66 | private StatusBarNotification mSbn; |
Eliot Courtney | 21bc05f | 2017-10-19 17:03:34 +0900 | [diff] [blame] | 67 | |
| 68 | @Before |
| 69 | public void setUp() { |
Eliot Courtney | 8f56b0e | 2017-12-14 18:54:28 +0900 | [diff] [blame] | 70 | MockitoAnnotations.initMocks(this); |
| 71 | mDependency.injectTestDependency(NotificationEntryManager.class, mEntryManager); |
| 72 | mDependency.injectTestDependency(NotificationRemoteInputManager.class, |
| 73 | mRemoteInputManager); |
Jason Monk | 297c04e | 2018-08-23 17:16:59 -0400 | [diff] [blame] | 74 | mDependency.injectTestDependency(Dependency.MAIN_HANDLER, |
| 75 | new Handler(TestableLooper.get(this).getLooper())); |
Julia Reynolds | 12ad7ca | 2019-01-28 09:29:16 -0500 | [diff] [blame] | 76 | mContext.addMockSystemService(NotificationManager.class, mNotificationManager); |
Eliot Courtney | 8f56b0e | 2017-12-14 18:54:28 +0900 | [diff] [blame] | 77 | |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 78 | when(mEntryManager.getNotificationData()).thenReturn(mNotificationData); |
Eliot Courtney | 21bc05f | 2017-10-19 17:03:34 +0900 | [diff] [blame] | 79 | |
Eliot Courtney | 6c313d3 | 2017-12-14 19:57:51 +0900 | [diff] [blame] | 80 | mListener = new NotificationListener(mContext); |
Eliot Courtney | 21bc05f | 2017-10-19 17:03:34 +0900 | [diff] [blame] | 81 | mSbn = new StatusBarNotification(TEST_PACKAGE_NAME, TEST_PACKAGE_NAME, 0, null, TEST_UID, 0, |
| 82 | new Notification(), UserHandle.CURRENT, null, 0); |
Eliot Courtney | 21bc05f | 2017-10-19 17:03:34 +0900 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | @Test |
| 86 | public void testNotificationAddCallsAddNotification() { |
| 87 | mListener.onNotificationPosted(mSbn, mRanking); |
Jason Monk | 6dceace | 2018-05-15 20:24:07 -0400 | [diff] [blame] | 88 | TestableLooper.get(this).processAllMessages(); |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 89 | verify(mEntryManager).addNotification(mSbn, mRanking); |
Eliot Courtney | 21bc05f | 2017-10-19 17:03:34 +0900 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | @Test |
Eliot Courtney | 21bc05f | 2017-10-19 17:03:34 +0900 | [diff] [blame] | 93 | public void testNotificationUpdateCallsUpdateNotification() { |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 94 | when(mNotificationData.get(mSbn.getKey())).thenReturn(new NotificationEntry(mSbn)); |
Eliot Courtney | 21bc05f | 2017-10-19 17:03:34 +0900 | [diff] [blame] | 95 | mListener.onNotificationPosted(mSbn, mRanking); |
Jason Monk | 6dceace | 2018-05-15 20:24:07 -0400 | [diff] [blame] | 96 | TestableLooper.get(this).processAllMessages(); |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 97 | verify(mEntryManager).updateNotification(mSbn, mRanking); |
Eliot Courtney | 21bc05f | 2017-10-19 17:03:34 +0900 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | @Test |
| 101 | public void testNotificationRemovalCallsRemoveNotification() { |
| 102 | mListener.onNotificationRemoved(mSbn, mRanking); |
Jason Monk | 6dceace | 2018-05-15 20:24:07 -0400 | [diff] [blame] | 103 | TestableLooper.get(this).processAllMessages(); |
Mady Mellor | c2ff011 | 2019-03-28 14:18:06 -0700 | [diff] [blame] | 104 | verify(mEntryManager).removeNotification(eq(mSbn.getKey()), eq(mRanking), anyInt()); |
Eliot Courtney | 21bc05f | 2017-10-19 17:03:34 +0900 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | @Test |
| 108 | public void testRankingUpdateCallsNotificationRankingUpdate() { |
| 109 | mListener.onNotificationRankingUpdate(mRanking); |
Jason Monk | 6dceace | 2018-05-15 20:24:07 -0400 | [diff] [blame] | 110 | TestableLooper.get(this).processAllMessages(); |
Eliot Courtney | 21bc05f | 2017-10-19 17:03:34 +0900 | [diff] [blame] | 111 | // RankingMap may be modified by plugins. |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 112 | verify(mEntryManager).updateNotificationRanking(any()); |
Eliot Courtney | 21bc05f | 2017-10-19 17:03:34 +0900 | [diff] [blame] | 113 | } |
Julia Reynolds | 12ad7ca | 2019-01-28 09:29:16 -0500 | [diff] [blame] | 114 | |
| 115 | @Test |
| 116 | public void testOnConnectReadStatusBarSetting() { |
| 117 | NotificationListener.NotificationSettingsListener settingsListener = |
| 118 | mock(NotificationListener.NotificationSettingsListener.class); |
| 119 | mListener.addNotificationSettingsListener(settingsListener); |
| 120 | |
| 121 | when(mNotificationManager.shouldHideSilentStatusBarIcons()).thenReturn(true); |
| 122 | |
| 123 | mListener.onListenerConnected(); |
| 124 | |
| 125 | verify(settingsListener).onStatusBarIconsBehaviorChanged(true); |
| 126 | } |
| 127 | |
| 128 | @Test |
| 129 | public void testOnStatusBarIconsBehaviorChanged() { |
| 130 | NotificationListener.NotificationSettingsListener settingsListener = |
| 131 | mock(NotificationListener.NotificationSettingsListener.class); |
| 132 | mListener.addNotificationSettingsListener(settingsListener); |
| 133 | |
Julia Reynolds | 6d2b7e6 | 2019-03-22 15:15:38 -0400 | [diff] [blame] | 134 | mListener.onSilentStatusBarIconsVisibilityChanged(true); |
Julia Reynolds | 12ad7ca | 2019-01-28 09:29:16 -0500 | [diff] [blame] | 135 | |
| 136 | verify(settingsListener).onStatusBarIconsBehaviorChanged(true); |
| 137 | } |
Eliot Courtney | 21bc05f | 2017-10-19 17:03:34 +0900 | [diff] [blame] | 138 | } |