blob: 0800cb9c33f78e99cfb3e74038bd7c13ea160fb5 [file] [log] [blame]
Eliot Courtney21bc05f2017-10-19 17:03:34 +09001/*
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
17package com.android.systemui.statusbar;
18
Eliot Courtney21bc05f2017-10-19 17:03:34 +090019import static org.mockito.ArgumentMatchers.any;
Mady Mellorc2ff0112019-03-28 14:18:06 -070020import static org.mockito.ArgumentMatchers.anyInt;
21import static org.mockito.ArgumentMatchers.eq;
Julia Reynolds12ad7ca2019-01-28 09:29:16 -050022import static org.mockito.Mockito.mock;
Eliot Courtney21bc05f2017-10-19 17:03:34 +090023import static org.mockito.Mockito.verify;
24import static org.mockito.Mockito.when;
25
26import android.app.Notification;
Julia Reynolds12ad7ca2019-01-28 09:29:16 -050027import android.app.NotificationManager;
Eliot Courtney21bc05f2017-10-19 17:03:34 +090028import android.os.Handler;
Eliot Courtney21bc05f2017-10-19 17:03:34 +090029import android.os.UserHandle;
30import android.service.notification.NotificationListenerService;
31import android.service.notification.StatusBarNotification;
Eliot Courtney21bc05f2017-10-19 17:03:34 +090032import android.testing.AndroidTestingRunner;
33import android.testing.TestableLooper;
34
Brett Chabot84151d92019-02-27 15:37:59 -080035import androidx.test.filters.SmallTest;
36
Jason Monk297c04e2018-08-23 17:16:59 -040037import com.android.systemui.Dependency;
Eliot Courtney21bc05f2017-10-19 17:03:34 +090038import com.android.systemui.SysuiTestCase;
Rohan Shah20790b82018-07-02 17:21:04 -070039import com.android.systemui.statusbar.notification.NotificationEntryManager;
Ned Burnsf81c4c42019-01-07 14:10:43 -050040import com.android.systemui.statusbar.notification.collection.NotificationData;
41import com.android.systemui.statusbar.notification.collection.NotificationEntry;
Eliot Courtney21bc05f2017-10-19 17:03:34 +090042
43import org.junit.Before;
44import org.junit.Test;
45import org.junit.runner.RunWith;
Eliot Courtney8f56b0e2017-12-14 18:54:28 +090046import org.mockito.Mock;
47import org.mockito.MockitoAnnotations;
Eliot Courtney21bc05f2017-10-19 17:03:34 +090048
Eliot Courtney21bc05f2017-10-19 17:03:34 +090049@SmallTest
50@RunWith(AndroidTestingRunner.class)
Jason Monka716bac2018-12-05 15:48:21 -050051@TestableLooper.RunWithLooper
Eliot Courtney21bc05f2017-10-19 17:03:34 +090052public class NotificationListenerTest extends SysuiTestCase {
53 private static final String TEST_PACKAGE_NAME = "test";
54 private static final int TEST_UID = 0;
55
Eliot Courtney8f56b0e2017-12-14 18:54:28 +090056 @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 Reynolds12ad7ca2019-01-28 09:29:16 -050063 @Mock private NotificationManager mNotificationManager;
Eliot Courtney8f56b0e2017-12-14 18:54:28 +090064
Eliot Courtney21bc05f2017-10-19 17:03:34 +090065 private NotificationListener mListener;
66 private StatusBarNotification mSbn;
Eliot Courtney21bc05f2017-10-19 17:03:34 +090067
68 @Before
69 public void setUp() {
Eliot Courtney8f56b0e2017-12-14 18:54:28 +090070 MockitoAnnotations.initMocks(this);
71 mDependency.injectTestDependency(NotificationEntryManager.class, mEntryManager);
72 mDependency.injectTestDependency(NotificationRemoteInputManager.class,
73 mRemoteInputManager);
Jason Monk297c04e2018-08-23 17:16:59 -040074 mDependency.injectTestDependency(Dependency.MAIN_HANDLER,
75 new Handler(TestableLooper.get(this).getLooper()));
Julia Reynolds12ad7ca2019-01-28 09:29:16 -050076 mContext.addMockSystemService(NotificationManager.class, mNotificationManager);
Eliot Courtney8f56b0e2017-12-14 18:54:28 +090077
Eliot Courtneya6d8cf22017-10-20 13:26:58 +090078 when(mEntryManager.getNotificationData()).thenReturn(mNotificationData);
Eliot Courtney21bc05f2017-10-19 17:03:34 +090079
Eliot Courtney6c313d32017-12-14 19:57:51 +090080 mListener = new NotificationListener(mContext);
Eliot Courtney21bc05f2017-10-19 17:03:34 +090081 mSbn = new StatusBarNotification(TEST_PACKAGE_NAME, TEST_PACKAGE_NAME, 0, null, TEST_UID, 0,
82 new Notification(), UserHandle.CURRENT, null, 0);
Eliot Courtney21bc05f2017-10-19 17:03:34 +090083 }
84
85 @Test
86 public void testNotificationAddCallsAddNotification() {
87 mListener.onNotificationPosted(mSbn, mRanking);
Jason Monk6dceace2018-05-15 20:24:07 -040088 TestableLooper.get(this).processAllMessages();
Eliot Courtneya6d8cf22017-10-20 13:26:58 +090089 verify(mEntryManager).addNotification(mSbn, mRanking);
Eliot Courtney21bc05f2017-10-19 17:03:34 +090090 }
91
92 @Test
Eliot Courtney21bc05f2017-10-19 17:03:34 +090093 public void testNotificationUpdateCallsUpdateNotification() {
Ned Burnsf81c4c42019-01-07 14:10:43 -050094 when(mNotificationData.get(mSbn.getKey())).thenReturn(new NotificationEntry(mSbn));
Eliot Courtney21bc05f2017-10-19 17:03:34 +090095 mListener.onNotificationPosted(mSbn, mRanking);
Jason Monk6dceace2018-05-15 20:24:07 -040096 TestableLooper.get(this).processAllMessages();
Eliot Courtneya6d8cf22017-10-20 13:26:58 +090097 verify(mEntryManager).updateNotification(mSbn, mRanking);
Eliot Courtney21bc05f2017-10-19 17:03:34 +090098 }
99
100 @Test
101 public void testNotificationRemovalCallsRemoveNotification() {
102 mListener.onNotificationRemoved(mSbn, mRanking);
Jason Monk6dceace2018-05-15 20:24:07 -0400103 TestableLooper.get(this).processAllMessages();
Mady Mellorc2ff0112019-03-28 14:18:06 -0700104 verify(mEntryManager).removeNotification(eq(mSbn.getKey()), eq(mRanking), anyInt());
Eliot Courtney21bc05f2017-10-19 17:03:34 +0900105 }
106
107 @Test
108 public void testRankingUpdateCallsNotificationRankingUpdate() {
109 mListener.onNotificationRankingUpdate(mRanking);
Jason Monk6dceace2018-05-15 20:24:07 -0400110 TestableLooper.get(this).processAllMessages();
Eliot Courtney21bc05f2017-10-19 17:03:34 +0900111 // RankingMap may be modified by plugins.
Eliot Courtneya6d8cf22017-10-20 13:26:58 +0900112 verify(mEntryManager).updateNotificationRanking(any());
Eliot Courtney21bc05f2017-10-19 17:03:34 +0900113 }
Julia Reynolds12ad7ca2019-01-28 09:29:16 -0500114
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 Reynolds6d2b7e62019-03-22 15:15:38 -0400134 mListener.onSilentStatusBarIconsVisibilityChanged(true);
Julia Reynolds12ad7ca2019-01-28 09:29:16 -0500135
136 verify(settingsListener).onStatusBarIconsBehaviorChanged(true);
137 }
Eliot Courtney21bc05f2017-10-19 17:03:34 +0900138}