Ned Burns | effd216 | 2020-03-19 15:19:09 -0400 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.systemui.statusbar.notification.collection.coordinator; |
| 18 | |
| 19 | import static org.junit.Assert.assertFalse; |
| 20 | import static org.junit.Assert.assertTrue; |
| 21 | import static org.mockito.ArgumentMatchers.anyInt; |
| 22 | import static org.mockito.Mockito.verify; |
| 23 | import static org.mockito.Mockito.when; |
| 24 | |
| 25 | import android.testing.AndroidTestingRunner; |
| 26 | import android.util.SparseArray; |
| 27 | |
| 28 | import androidx.test.filters.SmallTest; |
| 29 | |
| 30 | import com.android.systemui.SysuiTestCase; |
| 31 | import com.android.systemui.statusbar.NotificationLockscreenUserManager; |
| 32 | import com.android.systemui.statusbar.NotificationLockscreenUserManager.UserChangedListener; |
| 33 | import com.android.systemui.statusbar.notification.collection.NotifPipeline; |
| 34 | import com.android.systemui.statusbar.notification.collection.NotificationEntry; |
| 35 | import com.android.systemui.statusbar.notification.collection.NotificationEntryBuilder; |
| 36 | import com.android.systemui.statusbar.notification.collection.listbuilder.pluggable.NotifFilter; |
| 37 | import com.android.systemui.statusbar.notification.collection.listbuilder.pluggable.Pluggable.PluggableListener; |
| 38 | |
| 39 | import org.junit.Before; |
| 40 | import org.junit.Test; |
| 41 | import org.junit.runner.RunWith; |
| 42 | import org.mockito.ArgumentCaptor; |
| 43 | import org.mockito.Captor; |
| 44 | import org.mockito.Mock; |
| 45 | import org.mockito.MockitoAnnotations; |
| 46 | |
| 47 | @SmallTest |
| 48 | @RunWith(AndroidTestingRunner.class) |
| 49 | public 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 | } |