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