blob: 311b81c2858e1e9aab754d2b6213914eefeb4064 [file] [log] [blame]
Ned Burnsf36c6252019-01-09 19:12:33 -05001/*
2 * Copyright (C) 2019 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.notification;
18
19import static junit.framework.Assert.assertEquals;
20import static junit.framework.Assert.assertTrue;
21
22import static org.mockito.ArgumentMatchers.anyInt;
23import static org.mockito.ArgumentMatchers.anyString;
24import static org.mockito.Mockito.never;
25import static org.mockito.Mockito.times;
26import static org.mockito.Mockito.verify;
27import static org.mockito.Mockito.when;
28
29import android.app.ActivityManager;
30import android.app.AppOpsManager;
31import android.app.Notification;
32import android.os.UserHandle;
33import android.service.notification.StatusBarNotification;
Ned Burnsf36c6252019-01-09 19:12:33 -050034import android.testing.AndroidTestingRunner;
35import android.testing.TestableLooper;
36import android.util.ArraySet;
37
Brett Chabot84151d92019-02-27 15:37:59 -080038import androidx.test.filters.SmallTest;
39
Ned Burnsf36c6252019-01-09 19:12:33 -050040import com.android.internal.statusbar.NotificationVisibility;
41import com.android.systemui.ForegroundServiceController;
42import com.android.systemui.R;
43import com.android.systemui.SysuiTestCase;
44import com.android.systemui.statusbar.notification.collection.NotificationData;
45import com.android.systemui.statusbar.notification.collection.NotificationEntry;
46import com.android.systemui.statusbar.notification.stack.NotificationListContainer;
47import com.android.systemui.statusbar.policy.DeviceProvisionedController;
48import com.android.systemui.statusbar.policy.DeviceProvisionedController.DeviceProvisionedListener;
49
50import org.junit.Before;
51import org.junit.Test;
52import org.junit.runner.RunWith;
53import org.mockito.ArgumentCaptor;
54import org.mockito.Captor;
55import org.mockito.Mock;
56import org.mockito.MockitoAnnotations;
57
58@SmallTest
59@RunWith(AndroidTestingRunner.class)
60@TestableLooper.RunWithLooper
61public class NotificationListControllerTest extends SysuiTestCase {
62 private NotificationListController mController;
63
64 @Mock private NotificationEntryManager mEntryManager;
65 @Mock private NotificationListContainer mListContainer;
66 @Mock private ForegroundServiceController mForegroundServiceController;
67 @Mock private DeviceProvisionedController mDeviceProvisionedController;
68
69 @Captor private ArgumentCaptor<NotificationEntryListener> mEntryListenerCaptor;
70 @Captor private ArgumentCaptor<DeviceProvisionedListener> mProvisionedCaptor;
71
72 private NotificationEntryListener mEntryListener;
73 private DeviceProvisionedListener mProvisionedListener;
74
75 // TODO: Remove this once EntryManager no longer needs to be mocked
76 private NotificationData mNotificationData = new NotificationData();
77
78 private int mNextNotifId = 0;
79
80 @Before
81 public void setUp() {
82 MockitoAnnotations.initMocks(this);
83
84 when(mEntryManager.getNotificationData()).thenReturn(mNotificationData);
85
86 mController = new NotificationListController(
87 mEntryManager,
88 mListContainer,
89 mForegroundServiceController,
90 mDeviceProvisionedController);
91 mController.bind();
92
93 // Capture callbacks passed to mocks
94 verify(mEntryManager).addNotificationEntryListener(mEntryListenerCaptor.capture());
95 mEntryListener = mEntryListenerCaptor.getValue();
96 verify(mDeviceProvisionedController).addCallback(mProvisionedCaptor.capture());
97 mProvisionedListener = mProvisionedCaptor.getValue();
98 }
99
100 @Test
101 public void testCleanUpViewStateOnEntryRemoved() {
102 final NotificationEntry entry = buildEntry();
103 mEntryListener.onEntryRemoved(
104 entry,
105 NotificationVisibility.obtain(entry.key, 0, 0, true),
106 false);
107 verify(mListContainer).cleanUpViewStateForEntry(entry);
108 }
109
110 @Test
111 public void testCallUpdateNotificationsOnDeviceProvisionedChange() {
112 mProvisionedListener.onDeviceProvisionedChanged();
113 verify(mEntryManager).updateNotifications();
114 }
115
116 @Test
117 public void testAppOps_appOpAddedToForegroundNotif() {
118 // GIVEN a notification associated with a foreground service
119 final NotificationEntry entry = buildEntry();
120 mNotificationData.add(entry);
121 when(mForegroundServiceController.getStandardLayoutKey(anyInt(), anyString()))
122 .thenReturn(entry.key);
123
124 // WHEN we are notified of a new app op
125 mController.updateNotificationsForAppOp(
126 AppOpsManager.OP_CAMERA,
127 entry.notification.getUid(),
128 entry.notification.getPackageName(),
129 true);
130
131 // THEN the app op is added to the entry
132 assertTrue(entry.mActiveAppOps.contains(AppOpsManager.OP_CAMERA));
133 // THEN updateNotifications() is called
134 verify(mEntryManager, times(1)).updateNotifications();
135 }
136
137 @Test
138 public void testAppOps_appOpAddedToUnrelatedNotif() {
139 // GIVEN No current foreground notifs
140 when(mForegroundServiceController.getStandardLayoutKey(anyInt(), anyString()))
141 .thenReturn(null);
142
143 // WHEN An unrelated notification gets a new app op
144 mController.updateNotificationsForAppOp(AppOpsManager.OP_CAMERA, 1000, "pkg", true);
145
146 // THEN We never call updateNotifications()
147 verify(mEntryManager, never()).updateNotifications();
148 }
149
150 @Test
151 public void testAppOps_addNotificationWithExistingAppOps() {
152 // GIVEN a notification with three associated app ops that is associated with a foreground
153 // service
154 final NotificationEntry entry = buildEntry();
155 mNotificationData.add(entry);
156 ArraySet<Integer> expected = new ArraySet<>();
157 expected.add(3);
158 expected.add(235);
159 expected.add(1);
160 when(mForegroundServiceController.getStandardLayoutKey(
161 entry.notification.getUserId(),
162 entry.notification.getPackageName())).thenReturn(entry.key);
163 when(mForegroundServiceController.getAppOps(entry.notification.getUserId(),
164 entry.notification.getPackageName())).thenReturn(expected);
165
166 // WHEN the notification is added
167 mEntryListener.onBeforeNotificationAdded(entry);
168
169 // THEN the entry is tagged with all three app ops
170 assertEquals(expected.size(), entry.mActiveAppOps.size());
171 for (int op : expected) {
172 assertTrue("Entry missing op " + op, entry.mActiveAppOps.contains(op));
173 }
174 }
175
176 @Test
177 public void testAdd_addNotificationWithNoExistingAppOps() {
178 // GIVEN a notification with NO associated app ops
179 final NotificationEntry entry = buildEntry();
180
181 mNotificationData.add(entry);
182 when(mForegroundServiceController.getStandardLayoutKey(
183 entry.notification.getUserId(),
184 entry.notification.getPackageName())).thenReturn(entry.key);
185 when(mForegroundServiceController.getAppOps(entry.notification.getUserId(),
186 entry.notification.getPackageName())).thenReturn(null);
187
188 // WHEN the notification is added
189 mEntryListener.onBeforeNotificationAdded(entry);
190
191 // THEN the entry doesn't have any app ops associated with it
192 assertEquals(0, entry.mActiveAppOps.size());
193 }
194
195 @Test
196 public void testAdd_addNonForegroundNotificationWithExistingAppOps() {
197 // GIVEN a notification with app ops that isn't associated with a foreground service
198 final NotificationEntry entry = buildEntry();
199 mNotificationData.add(entry);
200 ArraySet<Integer> ops = new ArraySet<>();
201 ops.add(3);
202 ops.add(235);
203 ops.add(1);
204 when(mForegroundServiceController.getAppOps(entry.notification.getUserId(),
205 entry.notification.getPackageName())).thenReturn(ops);
206 when(mForegroundServiceController.getStandardLayoutKey(
207 entry.notification.getUserId(),
208 entry.notification.getPackageName())).thenReturn("something else");
209
210 // WHEN the notification is added
211 mEntryListener.onBeforeNotificationAdded(entry);
212
213 // THEN the entry doesn't have any app ops associated with it
214 assertEquals(0, entry.mActiveAppOps.size());
215 }
216
217 private NotificationEntry buildEntry() {
218 mNextNotifId++;
219
220 Notification.Builder n = new Notification.Builder(mContext, "")
221 .setSmallIcon(R.drawable.ic_person)
222 .setContentTitle("Title")
223 .setContentText("Text");
224
225 StatusBarNotification notification =
226 new StatusBarNotification(
227 TEST_PACKAGE_NAME,
228 TEST_PACKAGE_NAME,
229 mNextNotifId,
230 null,
231 TEST_UID,
232 0,
233 n.build(),
234 new UserHandle(ActivityManager.getCurrentUser()),
235 null,
236 0);
237 return new NotificationEntry(notification);
238 }
239
240 private static final String TEST_PACKAGE_NAME = "test";
241 private static final int TEST_UID = 0;
242}