Mady Mellor | 56515c4 | 2020-02-18 17:58:36 -0800 | [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.server.notification; |
| 18 | |
| 19 | import static org.mockito.ArgumentMatchers.any; |
| 20 | import static org.mockito.Mockito.mock; |
| 21 | import static org.mockito.Mockito.times; |
| 22 | import static org.mockito.Mockito.verify; |
| 23 | import static org.mockito.Mockito.when; |
| 24 | |
| 25 | import android.app.Notification; |
| 26 | import android.content.pm.LauncherApps; |
| 27 | import android.content.pm.ShortcutInfo; |
| 28 | import android.os.UserHandle; |
| 29 | import android.service.notification.StatusBarNotification; |
| 30 | import android.test.suitebuilder.annotation.SmallTest; |
| 31 | import android.testing.TestableLooper; |
| 32 | |
| 33 | import androidx.test.runner.AndroidJUnit4; |
| 34 | |
| 35 | import com.android.server.UiServiceTestCase; |
| 36 | |
| 37 | import org.junit.Before; |
| 38 | import org.junit.Test; |
| 39 | import org.junit.runner.RunWith; |
| 40 | import org.mockito.ArgumentCaptor; |
| 41 | import org.mockito.Mock; |
| 42 | import org.mockito.MockitoAnnotations; |
| 43 | |
| 44 | import java.util.ArrayList; |
| 45 | import java.util.List; |
| 46 | |
| 47 | @SmallTest |
| 48 | @RunWith(AndroidJUnit4.class) |
| 49 | @TestableLooper.RunWithLooper |
| 50 | public class ShortcutHelperTest extends UiServiceTestCase { |
| 51 | |
| 52 | private static final String SHORTCUT_ID = "shortcut"; |
| 53 | private static final String PKG = "pkg"; |
| 54 | private static final String KEY = "key"; |
| 55 | |
| 56 | @Mock |
| 57 | LauncherApps mLauncherApps; |
| 58 | @Mock |
| 59 | ShortcutHelper.ShortcutListener mShortcutListener; |
| 60 | @Mock |
| 61 | NotificationRecord mNr; |
| 62 | @Mock |
| 63 | Notification mNotif; |
| 64 | @Mock |
| 65 | StatusBarNotification mSbn; |
| 66 | @Mock |
| 67 | Notification.BubbleMetadata mBubbleMetadata; |
| 68 | |
| 69 | ShortcutHelper mShortcutHelper; |
| 70 | |
| 71 | @Before |
| 72 | public void setUp() { |
| 73 | MockitoAnnotations.initMocks(this); |
| 74 | |
| 75 | mShortcutHelper = new ShortcutHelper(mLauncherApps, mShortcutListener); |
| 76 | when(mNr.getKey()).thenReturn(KEY); |
| 77 | when(mNr.getSbn()).thenReturn(mSbn); |
| 78 | when(mSbn.getPackageName()).thenReturn(PKG); |
| 79 | when(mNr.getNotification()).thenReturn(mNotif); |
| 80 | when(mNotif.getBubbleMetadata()).thenReturn(mBubbleMetadata); |
| 81 | when(mBubbleMetadata.getShortcutId()).thenReturn(SHORTCUT_ID); |
| 82 | } |
| 83 | |
| 84 | private LauncherApps.Callback addShortcutBubbleAndVerifyListener() { |
| 85 | when(mNotif.isBubbleNotification()).thenReturn(true); |
| 86 | |
| 87 | mShortcutHelper.maybeListenForShortcutChangesForBubbles(mNr, |
| 88 | false /* removed */, |
| 89 | null /* handler */); |
| 90 | |
| 91 | ArgumentCaptor<LauncherApps.Callback> launcherAppsCallback = |
| 92 | ArgumentCaptor.forClass(LauncherApps.Callback.class); |
| 93 | |
| 94 | verify(mLauncherApps, times(1)).registerCallback( |
| 95 | launcherAppsCallback.capture(), any()); |
| 96 | return launcherAppsCallback.getValue(); |
| 97 | } |
| 98 | |
| 99 | @Test |
| 100 | public void testBubbleAdded_listenedAdded() { |
| 101 | addShortcutBubbleAndVerifyListener(); |
| 102 | } |
| 103 | |
| 104 | @Test |
| 105 | public void testBubbleRemoved_listenerRemoved() { |
| 106 | // First set it up to listen |
| 107 | addShortcutBubbleAndVerifyListener(); |
| 108 | |
| 109 | // Then remove the notif |
| 110 | mShortcutHelper.maybeListenForShortcutChangesForBubbles(mNr, |
| 111 | true /* removed */, |
| 112 | null /* handler */); |
| 113 | |
| 114 | verify(mLauncherApps, times(1)).unregisterCallback(any()); |
| 115 | } |
| 116 | |
| 117 | @Test |
| 118 | public void testBubbleNoLongerBubble_listenerRemoved() { |
| 119 | // First set it up to listen |
| 120 | addShortcutBubbleAndVerifyListener(); |
| 121 | |
| 122 | // Then make it not a bubble |
| 123 | when(mNotif.isBubbleNotification()).thenReturn(false); |
| 124 | mShortcutHelper.maybeListenForShortcutChangesForBubbles(mNr, |
| 125 | false /* removed */, |
| 126 | null /* handler */); |
| 127 | |
| 128 | verify(mLauncherApps, times(1)).unregisterCallback(any()); |
| 129 | } |
| 130 | |
| 131 | @Test |
| 132 | public void testListenerNotifiedOnShortcutRemoved() { |
| 133 | LauncherApps.Callback callback = addShortcutBubbleAndVerifyListener(); |
| 134 | |
| 135 | List<ShortcutInfo> shortcutInfos = new ArrayList<>(); |
| 136 | when(mLauncherApps.getShortcuts(any(), any())).thenReturn(shortcutInfos); |
| 137 | |
| 138 | callback.onShortcutsChanged(PKG, shortcutInfos, mock(UserHandle.class)); |
| 139 | verify(mShortcutListener).onShortcutRemoved(mNr.getKey()); |
| 140 | } |
| 141 | } |