blob: f7304bd0075beee0e39cb4c84910336543e8839e [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
Julia Reynoldsfa273072020-04-14 15:31:21 -040019import static com.google.common.truth.Truth.assertThat;
20
Mady Mellor56515c42020-02-18 17:58:36 -080021import static org.mockito.ArgumentMatchers.any;
Julia Reynoldsfa273072020-04-14 15:31:21 -040022import static org.mockito.ArgumentMatchers.anyInt;
23import static org.mockito.ArgumentMatchers.anyString;
Mady Mellor56515c42020-02-18 17:58:36 -080024import static org.mockito.Mockito.mock;
25import static org.mockito.Mockito.times;
26import static org.mockito.Mockito.verify;
27import static org.mockito.Mockito.when;
28
29import android.app.Notification;
30import android.content.pm.LauncherApps;
31import android.content.pm.ShortcutInfo;
Julia Reynoldsfa273072020-04-14 15:31:21 -040032import android.content.pm.ShortcutServiceInternal;
Mady Mellor56515c42020-02-18 17:58:36 -080033import android.os.UserHandle;
34import android.service.notification.StatusBarNotification;
35import android.test.suitebuilder.annotation.SmallTest;
36import android.testing.TestableLooper;
37
38import androidx.test.runner.AndroidJUnit4;
39
40import com.android.server.UiServiceTestCase;
41
42import org.junit.Before;
43import org.junit.Test;
44import org.junit.runner.RunWith;
45import org.mockito.ArgumentCaptor;
46import org.mockito.Mock;
47import org.mockito.MockitoAnnotations;
48
49import java.util.ArrayList;
50import java.util.List;
51
52@SmallTest
53@RunWith(AndroidJUnit4.class)
54@TestableLooper.RunWithLooper
55public class ShortcutHelperTest extends UiServiceTestCase {
56
57 private static final String SHORTCUT_ID = "shortcut";
58 private static final String PKG = "pkg";
59 private static final String KEY = "key";
60
61 @Mock
62 LauncherApps mLauncherApps;
63 @Mock
64 ShortcutHelper.ShortcutListener mShortcutListener;
65 @Mock
Julia Reynoldsfa273072020-04-14 15:31:21 -040066 ShortcutServiceInternal mShortcutServiceInternal;
67 @Mock
Mady Mellor56515c42020-02-18 17:58:36 -080068 NotificationRecord mNr;
69 @Mock
70 Notification mNotif;
71 @Mock
72 StatusBarNotification mSbn;
73 @Mock
74 Notification.BubbleMetadata mBubbleMetadata;
75
76 ShortcutHelper mShortcutHelper;
77
78 @Before
79 public void setUp() {
80 MockitoAnnotations.initMocks(this);
81
Julia Reynoldsfa273072020-04-14 15:31:21 -040082 mShortcutHelper = new ShortcutHelper(
83 mLauncherApps, mShortcutListener, mShortcutServiceInternal);
Mady Mellor56515c42020-02-18 17:58:36 -080084 when(mNr.getKey()).thenReturn(KEY);
85 when(mNr.getSbn()).thenReturn(mSbn);
86 when(mSbn.getPackageName()).thenReturn(PKG);
87 when(mNr.getNotification()).thenReturn(mNotif);
88 when(mNotif.getBubbleMetadata()).thenReturn(mBubbleMetadata);
89 when(mBubbleMetadata.getShortcutId()).thenReturn(SHORTCUT_ID);
90 }
91
92 private LauncherApps.Callback addShortcutBubbleAndVerifyListener() {
93 when(mNotif.isBubbleNotification()).thenReturn(true);
94
95 mShortcutHelper.maybeListenForShortcutChangesForBubbles(mNr,
96 false /* removed */,
97 null /* handler */);
98
99 ArgumentCaptor<LauncherApps.Callback> launcherAppsCallback =
100 ArgumentCaptor.forClass(LauncherApps.Callback.class);
101
102 verify(mLauncherApps, times(1)).registerCallback(
103 launcherAppsCallback.capture(), any());
104 return launcherAppsCallback.getValue();
105 }
106
107 @Test
108 public void testBubbleAdded_listenedAdded() {
109 addShortcutBubbleAndVerifyListener();
110 }
111
112 @Test
113 public void testBubbleRemoved_listenerRemoved() {
114 // First set it up to listen
115 addShortcutBubbleAndVerifyListener();
116
117 // Then remove the notif
118 mShortcutHelper.maybeListenForShortcutChangesForBubbles(mNr,
119 true /* removed */,
120 null /* handler */);
121
122 verify(mLauncherApps, times(1)).unregisterCallback(any());
123 }
124
125 @Test
126 public void testBubbleNoLongerBubble_listenerRemoved() {
127 // First set it up to listen
128 addShortcutBubbleAndVerifyListener();
129
130 // Then make it not a bubble
131 when(mNotif.isBubbleNotification()).thenReturn(false);
132 mShortcutHelper.maybeListenForShortcutChangesForBubbles(mNr,
133 false /* removed */,
134 null /* handler */);
135
136 verify(mLauncherApps, times(1)).unregisterCallback(any());
137 }
138
139 @Test
140 public void testListenerNotifiedOnShortcutRemoved() {
141 LauncherApps.Callback callback = addShortcutBubbleAndVerifyListener();
142
143 List<ShortcutInfo> shortcutInfos = new ArrayList<>();
144 when(mLauncherApps.getShortcuts(any(), any())).thenReturn(shortcutInfos);
145
146 callback.onShortcutsChanged(PKG, shortcutInfos, mock(UserHandle.class));
147 verify(mShortcutListener).onShortcutRemoved(mNr.getKey());
148 }
Julia Reynoldsfa273072020-04-14 15:31:21 -0400149
150 @Test
151 public void testGetValidShortcutInfo_noMatchingShortcut() {
152 when(mLauncherApps.getShortcuts(any(), any())).thenReturn(null);
153 when(mShortcutServiceInternal.isSharingShortcut(anyInt(), anyString(), anyString(),
154 anyString(), anyInt(), any())).thenReturn(true);
155
156 assertThat(mShortcutHelper.getValidShortcutInfo("a", "p", UserHandle.SYSTEM)).isNull();
157 }
158
159 @Test
160 public void testGetValidShortcutInfo_nullShortcut() {
161 ArrayList<ShortcutInfo> shortcuts = new ArrayList<>();
162 shortcuts.add(null);
163 when(mLauncherApps.getShortcuts(any(), any())).thenReturn(shortcuts);
164 when(mShortcutServiceInternal.isSharingShortcut(anyInt(), anyString(), anyString(),
165 anyString(), anyInt(), any())).thenReturn(true);
166
167 assertThat(mShortcutHelper.getValidShortcutInfo("a", "p", UserHandle.SYSTEM)).isNull();
168 }
169
170 @Test
171 public void testGetValidShortcutInfo_notLongLived() {
172 ShortcutInfo si = mock(ShortcutInfo.class);
173 when(si.isLongLived()).thenReturn(false);
174 when(si.isEnabled()).thenReturn(true);
175 ArrayList<ShortcutInfo> shortcuts = new ArrayList<>();
176 shortcuts.add(si);
177 when(mLauncherApps.getShortcuts(any(), any())).thenReturn(shortcuts);
178 when(mShortcutServiceInternal.isSharingShortcut(anyInt(), anyString(), anyString(),
179 anyString(), anyInt(), any())).thenReturn(true);
180
181 assertThat(mShortcutHelper.getValidShortcutInfo("a", "p", UserHandle.SYSTEM)).isNull();
182 }
183
184 @Test
185 public void testGetValidShortcutInfo_notSharingShortcut() {
186 ShortcutInfo si = mock(ShortcutInfo.class);
187 when(si.isLongLived()).thenReturn(true);
188 when(si.isEnabled()).thenReturn(true);
189 ArrayList<ShortcutInfo> shortcuts = new ArrayList<>();
190 shortcuts.add(si);
191 when(mLauncherApps.getShortcuts(any(), any())).thenReturn(shortcuts);
192 when(mShortcutServiceInternal.isSharingShortcut(anyInt(), anyString(), anyString(),
193 anyString(), anyInt(), any())).thenReturn(false);
194
195 assertThat(mShortcutHelper.getValidShortcutInfo("a", "p", UserHandle.SYSTEM)).isNull();
196 }
197
198 @Test
199 public void testGetValidShortcutInfo_notEnabled() {
200 ShortcutInfo si = mock(ShortcutInfo.class);
201 when(si.isLongLived()).thenReturn(true);
202 when(si.isEnabled()).thenReturn(false);
203 ArrayList<ShortcutInfo> shortcuts = new ArrayList<>();
204 shortcuts.add(si);
205 when(mLauncherApps.getShortcuts(any(), any())).thenReturn(shortcuts);
206 when(mShortcutServiceInternal.isSharingShortcut(anyInt(), anyString(), anyString(),
207 anyString(), anyInt(), any())).thenReturn(true);
208
209 assertThat(mShortcutHelper.getValidShortcutInfo("a", "p", UserHandle.SYSTEM)).isNull();
210 }
211
212 @Test
213 public void testGetValidShortcutInfo_isValid() {
214 ShortcutInfo si = mock(ShortcutInfo.class);
215 when(si.isLongLived()).thenReturn(true);
216 when(si.isEnabled()).thenReturn(true);
217 ArrayList<ShortcutInfo> shortcuts = new ArrayList<>();
218 shortcuts.add(si);
219 when(mLauncherApps.getShortcuts(any(), any())).thenReturn(shortcuts);
220 when(mShortcutServiceInternal.isSharingShortcut(anyInt(), anyString(), anyString(),
221 anyString(), anyInt(), any())).thenReturn(true);
222
223 assertThat(mShortcutHelper.getValidShortcutInfo("a", "p", UserHandle.SYSTEM)).isSameAs(si);
224 }
Mady Mellor56515c42020-02-18 17:58:36 -0800225}