blob: 50fb9b425652234d2846eb18ec884fc9b8d88da7 [file] [log] [blame]
Mady Mellor56515c42020-02-18 17:58:36 -08001/*
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.server.notification;
18
19import static org.mockito.ArgumentMatchers.any;
20import static org.mockito.Mockito.mock;
21import static org.mockito.Mockito.times;
22import static org.mockito.Mockito.verify;
23import static org.mockito.Mockito.when;
24
25import android.app.Notification;
26import android.content.pm.LauncherApps;
27import android.content.pm.ShortcutInfo;
28import android.os.UserHandle;
29import android.service.notification.StatusBarNotification;
30import android.test.suitebuilder.annotation.SmallTest;
31import android.testing.TestableLooper;
32
33import androidx.test.runner.AndroidJUnit4;
34
35import com.android.server.UiServiceTestCase;
36
37import org.junit.Before;
38import org.junit.Test;
39import org.junit.runner.RunWith;
40import org.mockito.ArgumentCaptor;
41import org.mockito.Mock;
42import org.mockito.MockitoAnnotations;
43
44import java.util.ArrayList;
45import java.util.List;
46
47@SmallTest
48@RunWith(AndroidJUnit4.class)
49@TestableLooper.RunWithLooper
50public 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}