blob: 87fc02062ad4d788e3290e7f78428312b48d4fd6 [file] [log] [blame]
Ned Burnseffd2162020-03-19 15:19:09 -04001/*
2 * Copyright (C) 2020 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.collection.coordinator;
18
19import static org.junit.Assert.assertFalse;
20import static org.junit.Assert.assertTrue;
21import static org.mockito.ArgumentMatchers.anyInt;
22import static org.mockito.Mockito.verify;
23import static org.mockito.Mockito.when;
24
25import android.testing.AndroidTestingRunner;
26import android.util.SparseArray;
27
28import androidx.test.filters.SmallTest;
29
30import com.android.systemui.SysuiTestCase;
31import com.android.systemui.statusbar.NotificationLockscreenUserManager;
32import com.android.systemui.statusbar.NotificationLockscreenUserManager.UserChangedListener;
33import com.android.systemui.statusbar.notification.collection.NotifPipeline;
34import com.android.systemui.statusbar.notification.collection.NotificationEntry;
35import com.android.systemui.statusbar.notification.collection.NotificationEntryBuilder;
36import com.android.systemui.statusbar.notification.collection.listbuilder.pluggable.NotifFilter;
37import com.android.systemui.statusbar.notification.collection.listbuilder.pluggable.Pluggable.PluggableListener;
38
39import org.junit.Before;
40import org.junit.Test;
41import org.junit.runner.RunWith;
42import org.mockito.ArgumentCaptor;
43import org.mockito.Captor;
44import org.mockito.Mock;
45import org.mockito.MockitoAnnotations;
46
47@SmallTest
48@RunWith(AndroidTestingRunner.class)
49public class HideNotifsForOtherUsersCoordinatorTest extends SysuiTestCase {
50
51 @Mock private NotificationLockscreenUserManager mLockscreenUserManager;
52 @Mock private NotifPipeline mNotifPipeline;
53 @Mock private PluggableListener<NotifFilter> mInvalidationListener;
54
55 @Captor private ArgumentCaptor<UserChangedListener> mUserChangedListenerCaptor;
56 @Captor private ArgumentCaptor<NotifFilter> mNotifFilterCaptor;
57
58 private UserChangedListener mCapturedUserChangeListener;
59 private NotifFilter mCapturedNotifFilter;
60
61 private NotificationEntry mEntry = new NotificationEntryBuilder().build();
62
63 @Before
64 public void setUp() {
65 MockitoAnnotations.initMocks(this);
66
67 HideNotifsForOtherUsersCoordinator coordinator =
68 new HideNotifsForOtherUsersCoordinator(mLockscreenUserManager);
69 coordinator.attach(mNotifPipeline);
70
71 verify(mLockscreenUserManager).addUserChangedListener(mUserChangedListenerCaptor.capture());
72 verify(mNotifPipeline).addPreGroupFilter(mNotifFilterCaptor.capture());
73
74 mCapturedUserChangeListener = mUserChangedListenerCaptor.getValue();
75 mCapturedNotifFilter = mNotifFilterCaptor.getValue();
76
77 mCapturedNotifFilter.setInvalidationListener(mInvalidationListener);
78 }
79
80 @Test
81 public void testFilterOutNotifsFromOtherProfiles() {
82 // GIVEN that all notifs are NOT for the current user
83 when(mLockscreenUserManager.isCurrentProfile(anyInt())).thenReturn(false);
84
85 // THEN they should all be filtered out
86 assertTrue(mCapturedNotifFilter.shouldFilterOut(mEntry, 0));
87 }
88
89 @Test
90 public void testPreserveNotifsFromThisProfile() {
91 // GIVEN that all notifs ARE for the current user
92 when(mLockscreenUserManager.isCurrentProfile(anyInt())).thenReturn(true);
93
94 // THEN none should be filtered out
95 assertFalse(mCapturedNotifFilter.shouldFilterOut(mEntry, 0));
96 }
97
98 @Test
99 public void testFilterIsInvalidatedWhenProfilesChange() {
100 // WHEN the current user profiles change
101 mCapturedUserChangeListener.onCurrentProfilesChanged(new SparseArray<>());
102
103 // THEN the filter is invalidated
104 verify(mInvalidationListener).onPluggableInvalidated(mCapturedNotifFilter);
105 }
106}