• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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 android.appenumeration.cts;
18 
19 import static android.appenumeration.cts.Constants.ACTION_AWAIT_LAUNCHER_APPS_CALLBACK;
20 import static android.appenumeration.cts.Constants.ACTION_AWAIT_LAUNCHER_APPS_SESSION_CALLBACK;
21 import static android.appenumeration.cts.Constants.ACTION_GET_ALL_PACKAGE_INSTALLER_SESSIONS;
22 import static android.appenumeration.cts.Constants.ACTION_LAUNCHER_APPS_GET_SUSPENDED_PACKAGE_LAUNCHER_EXTRAS;
23 import static android.appenumeration.cts.Constants.ACTION_LAUNCHER_APPS_IS_ACTIVITY_ENABLED;
24 import static android.appenumeration.cts.Constants.ACTION_LAUNCHER_APPS_SHOULD_HIDE_FROM_SUGGESTIONS;
25 import static android.appenumeration.cts.Constants.ACTIVITY_CLASS_DUMMY_ACTIVITY;
26 import static android.appenumeration.cts.Constants.CALLBACK_EVENT_INVALID;
27 import static android.appenumeration.cts.Constants.CALLBACK_EVENT_PACKAGES_SUSPENDED;
28 import static android.appenumeration.cts.Constants.CALLBACK_EVENT_PACKAGES_UNSUSPENDED;
29 import static android.appenumeration.cts.Constants.CALLBACK_EVENT_PACKAGE_ADDED;
30 import static android.appenumeration.cts.Constants.CALLBACK_EVENT_PACKAGE_CHANGED;
31 import static android.appenumeration.cts.Constants.CALLBACK_EVENT_PACKAGE_REMOVED;
32 import static android.appenumeration.cts.Constants.EXTRA_FLAGS;
33 import static android.appenumeration.cts.Constants.EXTRA_ID;
34 import static android.appenumeration.cts.Constants.QUERIES_ACTIVITY_ACTION;
35 import static android.appenumeration.cts.Constants.QUERIES_NOTHING;
36 import static android.appenumeration.cts.Constants.QUERIES_NOTHING_PERM;
37 import static android.appenumeration.cts.Constants.QUERIES_NOTHING_Q;
38 import static android.appenumeration.cts.Constants.QUERIES_PACKAGE;
39 import static android.appenumeration.cts.Constants.TARGET_FILTERS;
40 import static android.appenumeration.cts.Constants.TARGET_FILTERS_APK;
41 import static android.appenumeration.cts.Constants.TARGET_NO_API;
42 import static android.appenumeration.cts.Constants.TARGET_STUB;
43 import static android.appenumeration.cts.Constants.TARGET_STUB_APK;
44 import static android.appenumeration.cts.Utils.Result;
45 import static android.appenumeration.cts.Utils.adoptShellPermissions;
46 import static android.appenumeration.cts.Utils.cleanUpMySessions;
47 import static android.appenumeration.cts.Utils.commitSession;
48 import static android.appenumeration.cts.Utils.dropShellPermissions;
49 import static android.appenumeration.cts.Utils.ensurePackageIsInstalled;
50 import static android.appenumeration.cts.Utils.ensurePackageIsNotInstalled;
51 import static android.appenumeration.cts.Utils.installPackage;
52 import static android.appenumeration.cts.Utils.suspendPackages;
53 import static android.appenumeration.cts.Utils.suspendPackagesForUser;
54 import static android.appenumeration.cts.Utils.uninstallPackage;
55 import static android.content.Intent.EXTRA_PACKAGES;
56 import static android.os.Process.INVALID_UID;
57 
58 import static org.hamcrest.MatcherAssert.assertThat;
59 import static org.hamcrest.Matchers.arrayContainingInAnyOrder;
60 import static org.hamcrest.Matchers.emptyArray;
61 import static org.hamcrest.Matchers.equalTo;
62 import static org.hamcrest.Matchers.hasItemInArray;
63 import static org.hamcrest.Matchers.not;
64 import static org.hamcrest.core.Is.is;
65 import static org.junit.Assert.assertTrue;
66 
67 import android.Manifest;
68 import android.content.ComponentName;
69 import android.content.Intent;
70 import android.content.pm.LauncherApps;
71 import android.content.pm.PackageInstaller;
72 import android.content.pm.PackageManager;
73 import android.os.Bundle;
74 import android.os.Process;
75 
76 import androidx.test.ext.junit.runners.AndroidJUnit4;
77 import androidx.test.filters.LargeTest;
78 
79 import com.android.bedstead.nene.users.UserReference;
80 import com.android.compatibility.common.util.SystemUtil;
81 import com.android.cts.install.lib.Install;
82 import com.android.cts.install.lib.TestApp;
83 
84 import org.junit.Assert;
85 import org.junit.Test;
86 import org.junit.runner.RunWith;
87 
88 import java.util.Arrays;
89 import java.util.concurrent.CountDownLatch;
90 import java.util.concurrent.TimeUnit;
91 
92 @RunWith(AndroidJUnit4.class)
93 public class LauncherAppsEnumerationTests extends AppEnumerationTestsBase {
94 
95     @LargeTest
96     @Test
callback_added_notVisibleNotReceives()97     public void callback_added_notVisibleNotReceives() throws Exception {
98         ensurePackageIsNotInstalled(TARGET_STUB);
99         final Result result = sendCommandAndWaitForLauncherAppsCallback(QUERIES_NOTHING,
100                 CALLBACK_EVENT_PACKAGE_ADDED, new String[]{TARGET_STUB});
101 
102         installPackage(TARGET_STUB_APK);
103         final Bundle response = result.await();
104 
105         assertThat(response.getInt(EXTRA_FLAGS), equalTo(CALLBACK_EVENT_INVALID));
106         assertThat(response.getStringArray(EXTRA_PACKAGES), emptyArray());
107     }
108 
109     @Test
callback_added_visibleReceives()110     public void callback_added_visibleReceives() throws Exception {
111         ensurePackageIsNotInstalled(TARGET_STUB);
112         final Result result = sendCommandAndWaitForLauncherAppsCallback(QUERIES_NOTHING_PERM,
113                 CALLBACK_EVENT_PACKAGE_ADDED, new String[]{TARGET_STUB});
114 
115         installPackage(TARGET_STUB_APK);
116         final Bundle response = result.await();
117 
118         assertThat(response.getInt(EXTRA_FLAGS), equalTo(CALLBACK_EVENT_PACKAGE_ADDED));
119         assertThat(response.getStringArray(EXTRA_PACKAGES),
120                 arrayContainingInAnyOrder(new String[]{TARGET_STUB}));
121     }
122 
123     @LargeTest
124     @Test
callback_removed_notVisibleNotReceives()125     public void callback_removed_notVisibleNotReceives() throws Exception {
126         ensurePackageIsInstalled(TARGET_STUB, TARGET_STUB_APK);
127         final Result result = sendCommandAndWaitForLauncherAppsCallback(QUERIES_NOTHING,
128                 CALLBACK_EVENT_PACKAGE_REMOVED, new String[]{TARGET_STUB});
129 
130         uninstallPackage(TARGET_STUB);
131         final Bundle response = result.await();
132 
133         assertThat(response.getInt(EXTRA_FLAGS), equalTo(CALLBACK_EVENT_INVALID));
134         assertThat(response.getStringArray(EXTRA_PACKAGES), emptyArray());
135     }
136 
137     @Test
callback_removed_visibleReceives()138     public void callback_removed_visibleReceives() throws Exception {
139         ensurePackageIsInstalled(TARGET_STUB, TARGET_STUB_APK);
140         final Result result = sendCommandAndWaitForLauncherAppsCallback(QUERIES_NOTHING_PERM,
141                 CALLBACK_EVENT_PACKAGE_REMOVED, new String[]{TARGET_STUB});
142 
143         uninstallPackage(TARGET_STUB);
144         final Bundle response = result.await();
145 
146         assertThat(response.getInt(EXTRA_FLAGS), equalTo(CALLBACK_EVENT_PACKAGE_REMOVED));
147         assertThat(response.getStringArray(EXTRA_PACKAGES),
148                 arrayContainingInAnyOrder(new String[]{TARGET_STUB}));
149     }
150 
151     @LargeTest
152     @Test
callback_changed_notVisibleNotReceives()153     public void callback_changed_notVisibleNotReceives() throws Exception {
154         final Result result = sendCommandAndWaitForLauncherAppsCallback(QUERIES_NOTHING,
155                 CALLBACK_EVENT_PACKAGE_CHANGED, new String[]{TARGET_FILTERS});
156 
157         installPackage(TARGET_FILTERS_APK);
158         final Bundle response = result.await();
159 
160         assertThat(response.getInt(EXTRA_FLAGS), equalTo(CALLBACK_EVENT_INVALID));
161         assertThat(response.getStringArray(EXTRA_PACKAGES), emptyArray());
162     }
163 
164     @Test
callback_changed_visibleReceives()165     public void callback_changed_visibleReceives() throws Exception {
166         final Result result = sendCommandAndWaitForLauncherAppsCallback(QUERIES_NOTHING_PERM,
167                 CALLBACK_EVENT_PACKAGE_CHANGED, new String[]{TARGET_FILTERS});
168 
169         installPackage(TARGET_FILTERS_APK);
170         final Bundle response = result.await();
171 
172         assertThat(response.getInt(EXTRA_FLAGS), equalTo(CALLBACK_EVENT_PACKAGE_CHANGED));
173         assertThat(response.getStringArray(EXTRA_PACKAGES),
174                 arrayContainingInAnyOrder(new String[]{TARGET_FILTERS}));
175     }
176 
177     @LargeTest
178     @Test
callback_suspended_notVisibleNotReceives()179     public void callback_suspended_notVisibleNotReceives() throws Exception {
180         final Result result = sendCommandAndWaitForLauncherAppsCallback(QUERIES_NOTHING,
181                 CALLBACK_EVENT_PACKAGES_SUSPENDED, new String[]{TARGET_FILTERS});
182 
183         try {
184             suspendPackages(true /* suspend */, Arrays.asList(TARGET_NO_API, TARGET_FILTERS));
185             final Bundle response = result.await();
186 
187             assertThat(response.getInt(EXTRA_FLAGS), equalTo(CALLBACK_EVENT_INVALID));
188             assertThat(response.getStringArray(EXTRA_PACKAGES), emptyArray());
189         } finally {
190             suspendPackages(false /* suspend */, Arrays.asList(TARGET_NO_API, TARGET_FILTERS));
191         }
192     }
193 
194     @Test
callback_suspended_visibleReceives()195     public void callback_suspended_visibleReceives() throws Exception {
196         final Result result = sendCommandAndWaitForLauncherAppsCallback(QUERIES_ACTIVITY_ACTION,
197                 CALLBACK_EVENT_PACKAGES_SUSPENDED, new String[]{TARGET_FILTERS});
198 
199         try {
200             suspendPackages(true /* suspend */, Arrays.asList(TARGET_NO_API, TARGET_FILTERS));
201             final Bundle response = result.await();
202 
203             assertThat(response.getInt(EXTRA_FLAGS), equalTo(CALLBACK_EVENT_PACKAGES_SUSPENDED));
204             assertThat(response.getStringArray(EXTRA_PACKAGES),
205                     arrayContainingInAnyOrder(new String[]{TARGET_FILTERS}));
206         } finally {
207             suspendPackages(false /* suspend */, Arrays.asList(TARGET_NO_API, TARGET_FILTERS));
208         }
209     }
210 
211     @LargeTest
212     @Test
callback_unsuspended_notVisibleNotReceives()213     public void callback_unsuspended_notVisibleNotReceives() throws Exception {
214         final Result result = sendCommandAndWaitForLauncherAppsCallback(QUERIES_NOTHING,
215                 CALLBACK_EVENT_PACKAGES_UNSUSPENDED, new String[]{TARGET_FILTERS});
216 
217         suspendPackages(false /* suspend */, Arrays.asList(TARGET_NO_API, TARGET_FILTERS));
218         final Bundle response = result.await();
219 
220         assertThat(response.getInt(EXTRA_FLAGS), equalTo(CALLBACK_EVENT_INVALID));
221         assertThat(response.getStringArray(EXTRA_PACKAGES), emptyArray());
222     }
223 
224     @Test
callback_unsuspended_visibleReceives()225     public void callback_unsuspended_visibleReceives() throws Exception {
226         suspendPackages(true /* suspend */, Arrays.asList(TARGET_NO_API, TARGET_FILTERS));
227 
228         final Result result = sendCommandAndWaitForLauncherAppsCallback(QUERIES_ACTIVITY_ACTION,
229                 CALLBACK_EVENT_PACKAGES_UNSUSPENDED, new String[]{TARGET_FILTERS});
230 
231         suspendPackages(false /* suspend */, Arrays.asList(TARGET_NO_API, TARGET_FILTERS));
232         final Bundle response = result.await();
233 
234         assertThat(response.getInt(EXTRA_FLAGS), equalTo(CALLBACK_EVENT_PACKAGES_UNSUSPENDED));
235         assertThat(response.getStringArray(EXTRA_PACKAGES),
236                 arrayContainingInAnyOrder(new String[]{TARGET_FILTERS}));
237     }
238 
239     @LargeTest
240     @Test
sessionCallback_queriesNothing_cannotSeeSession()241     public void sessionCallback_queriesNothing_cannotSeeSession() throws Exception {
242         try {
243             adoptShellPermissions(Manifest.permission.INSTALL_PACKAGES);
244             final int sessionId = Install.single(TestApp.A1).setPackageName(TestApp.A)
245                     .createSession();
246             final Result result = sendCommandAndWaitForLauncherAppsSessionCallback(
247                     QUERIES_NOTHING, sessionId);
248             commitSession(sessionId);
249             final Bundle response = result.await();
250             assertThat(response.getInt(EXTRA_ID), equalTo(PackageInstaller.SessionInfo.INVALID_ID));
251         } finally {
252             uninstallPackage(TestApp.A);
253             dropShellPermissions();
254         }
255     }
256 
257     @Test
sessionCallback_queriesNothingHasPermission_canSeeSession()258     public void sessionCallback_queriesNothingHasPermission_canSeeSession()
259             throws Exception {
260         try {
261             adoptShellPermissions(Manifest.permission.INSTALL_PACKAGES);
262             final int sessionId = Install.single(TestApp.A1).setPackageName(TestApp.A)
263                     .createSession();
264             final Result result = sendCommandAndWaitForLauncherAppsSessionCallback(
265                     QUERIES_NOTHING_PERM, sessionId);
266             commitSession(sessionId);
267             final Bundle response = result.await();
268             assertThat(response.getInt(EXTRA_ID), equalTo(sessionId));
269         } finally {
270             uninstallPackage(TestApp.A);
271             dropShellPermissions();
272         }
273     }
274 
275     @Test
sessionCallback_queriesPackage_canSeeSession()276     public void sessionCallback_queriesPackage_canSeeSession()
277             throws Exception {
278         try {
279             adoptShellPermissions(Manifest.permission.INSTALL_PACKAGES);
280             final int sessionId = Install.single(TestApp.A1).setPackageName(TestApp.A)
281                     .createSession();
282             final Result result = sendCommandAndWaitForLauncherAppsSessionCallback(
283                     QUERIES_PACKAGE, sessionId);
284             commitSession(sessionId);
285             final Bundle response = result.await();
286             assertThat(response.getInt(EXTRA_ID), equalTo(sessionId));
287         } finally {
288             uninstallPackage(TestApp.A);
289             dropShellPermissions();
290         }
291     }
292 
293     @Test
sessionCallback_queriesNothingTargetsQ_canSeeSession()294     public void sessionCallback_queriesNothingTargetsQ_canSeeSession()
295             throws Exception {
296         try {
297             adoptShellPermissions(Manifest.permission.INSTALL_PACKAGES);
298             final int sessionId = Install.single(TestApp.A1).setPackageName(TestApp.A)
299                     .createSession();
300             final Result result = sendCommandAndWaitForLauncherAppsSessionCallback(
301                     QUERIES_NOTHING_Q, sessionId);
302             commitSession(sessionId);
303             final Bundle response = result.await();
304             assertThat(response.getInt(EXTRA_ID), equalTo(sessionId));
305         } finally {
306             uninstallPackage(TestApp.A);
307             dropShellPermissions();
308         }
309     }
310 
311     @Test
sessionCallback_sessionOwner_canSeeSession()312     public void sessionCallback_sessionOwner_canSeeSession() throws Exception {
313         try {
314             adoptShellPermissions(Manifest.permission.INSTALL_PACKAGES);
315             final CountDownLatch countDownLatch = new CountDownLatch(1);
316             final int expectedSessionId = Install.single(TestApp.A1).setPackageName(TestApp.A)
317                     .createSession();
318             final LauncherApps launcherApps = sContext.getSystemService(LauncherApps.class);
319             final PackageInstaller.SessionCallback
320                     sessionCallback = new PackageInstaller.SessionCallback() {
321 
322                         @Override
323                         public void onCreated(int sessionId) {
324                             // No-op
325                         }
326 
327                         @Override
328                         public void onBadgingChanged(int sessionId) {
329                             // No-op
330                         }
331 
332                         @Override
333                         public void onActiveChanged(int sessionId, boolean active) {
334                             // No-op
335                         }
336 
337                         @Override
338                         public void onProgressChanged(int sessionId, float progress) {
339                             // No-op
340                         }
341 
342                         @Override
343                         public void onFinished(int sessionId, boolean success) {
344                             if (sessionId != expectedSessionId) {
345                                 return;
346                             }
347 
348                             launcherApps.unregisterPackageInstallerSessionCallback(this);
349                             countDownLatch.countDown();
350                         }
351                     };
352 
353             launcherApps.registerPackageInstallerSessionCallback(sContext.getMainExecutor(),
354                     sessionCallback);
355 
356             commitSession(expectedSessionId);
357             assertTrue(countDownLatch.await(5, TimeUnit.SECONDS));
358         } finally {
359             uninstallPackage(TestApp.A);
360             dropShellPermissions();
361         }
362     }
363 
364     @Test
getAllPkgInstallerSessions_queriesNothing_cannotSeeSessions()365     public void getAllPkgInstallerSessions_queriesNothing_cannotSeeSessions() throws Exception {
366         try {
367             adoptShellPermissions(Manifest.permission.INSTALL_PACKAGES);
368             final int sessionId = Install.single(TestApp.A1).setPackageName(TestApp.A)
369                     .createSession();
370             final Integer[] sessionIds = getSessionInfos(ACTION_GET_ALL_PACKAGE_INSTALLER_SESSIONS,
371                     QUERIES_NOTHING, PackageInstaller.SessionInfo.INVALID_ID);
372             assertThat(sessionIds, not(hasItemInArray(sessionId)));
373         } finally {
374             cleanUpMySessions();
375             dropShellPermissions();
376         }
377     }
378 
379     @Test
getAllPkgInstallerSessions_queriesNothingHasPermission_canSeeSessions()380     public void getAllPkgInstallerSessions_queriesNothingHasPermission_canSeeSessions()
381             throws Exception {
382         try {
383             adoptShellPermissions(Manifest.permission.INSTALL_PACKAGES);
384             final int sessionId = Install.single(TestApp.A1).setPackageName(TestApp.A)
385                     .createSession();
386             final Integer[] sessionIds = getSessionInfos(ACTION_GET_ALL_PACKAGE_INSTALLER_SESSIONS,
387                     QUERIES_NOTHING_PERM, PackageInstaller.SessionInfo.INVALID_ID);
388             assertThat(sessionIds, hasItemInArray(sessionId));
389         } finally {
390             cleanUpMySessions();
391             dropShellPermissions();
392         }
393     }
394 
395     @Test
getAllPkgInstallerSessions_queriesPackage_canSeeSessions()396     public void getAllPkgInstallerSessions_queriesPackage_canSeeSessions() throws Exception {
397         try {
398             adoptShellPermissions(Manifest.permission.INSTALL_PACKAGES);
399             final int sessionId = Install.single(TestApp.A1).setPackageName(TestApp.A)
400                     .createSession();
401             final Integer[] sessionIds = getSessionInfos(ACTION_GET_ALL_PACKAGE_INSTALLER_SESSIONS,
402                     QUERIES_PACKAGE, PackageInstaller.SessionInfo.INVALID_ID);
403             assertThat(sessionIds, hasItemInArray(sessionId));
404         } finally {
405             cleanUpMySessions();
406             dropShellPermissions();
407         }
408     }
409 
410     @Test
getAllPkgInstallerSessions_queriesNothingTargetsQ_canSeeSessions()411     public void getAllPkgInstallerSessions_queriesNothingTargetsQ_canSeeSessions()
412             throws Exception {
413         try {
414             adoptShellPermissions(Manifest.permission.INSTALL_PACKAGES);
415             final int sessionId = Install.single(TestApp.A1).setPackageName(TestApp.A)
416                     .createSession();
417             final Integer[] sessionIds = getSessionInfos(ACTION_GET_ALL_PACKAGE_INSTALLER_SESSIONS,
418                     QUERIES_NOTHING_Q, PackageInstaller.SessionInfo.INVALID_ID);
419             assertThat(sessionIds, hasItemInArray(sessionId));
420         } finally {
421             cleanUpMySessions();
422             dropShellPermissions();
423         }
424     }
425 
426     @Test
getAllPkgInstallerSessions_sessionOwner_canSeeSessions()427     public void getAllPkgInstallerSessions_sessionOwner_canSeeSessions() throws Exception {
428         try {
429             adoptShellPermissions(Manifest.permission.INSTALL_PACKAGES);
430             final int sessionId = Install.single(TestApp.A1).setPackageName(TestApp.A)
431                     .createSession();
432             final LauncherApps launcherApps = sContext.getSystemService(LauncherApps.class);
433             final Integer[] sessionIds = launcherApps.getAllPackageInstallerSessions().stream()
434                     .map(i -> i.getSessionId())
435                     .distinct()
436                     .toArray(Integer[]::new);
437             assertThat(sessionIds, hasItemInArray(sessionId));
438         } finally {
439             cleanUpMySessions();
440             dropShellPermissions();
441         }
442     }
443 
444     @Test
isActivityEnabled_queriesActivityAction_canSeeActivity()445     public void isActivityEnabled_queriesActivityAction_canSeeActivity() throws Exception {
446         final ComponentName targetFilters = ComponentName.createRelative(TARGET_FILTERS,
447                 ACTIVITY_CLASS_DUMMY_ACTIVITY);
448         assertThat(QUERIES_ACTIVITY_ACTION + " should be able to see " + targetFilters,
449                 isActivityEnabled(QUERIES_ACTIVITY_ACTION, targetFilters),
450                 is(true));
451     }
452 
453     @Test
isActivityEnabled_queriesNothing_cannotSeeActivity()454     public void isActivityEnabled_queriesNothing_cannotSeeActivity() throws Exception {
455         final ComponentName targetFilters = ComponentName.createRelative(TARGET_FILTERS,
456                 ACTIVITY_CLASS_DUMMY_ACTIVITY);
457         assertThat(QUERIES_ACTIVITY_ACTION + " should not be able to see " + targetFilters,
458                 isActivityEnabled(QUERIES_NOTHING, targetFilters),
459                 is(false));
460     }
461 
462     @Test
getSuspendedPackageLauncherExtras_queriesNothingHasPerm_canGetExtras()463     public void getSuspendedPackageLauncherExtras_queriesNothingHasPerm_canGetExtras()
464             throws Exception {
465         try {
466             suspendPackagesForUser(true /* suspend */, Arrays.asList(TARGET_NO_API),
467                     UserReference.of(sContext.getUser()), true /* extraPersistableBundle */);
468             Assert.assertNotNull(getSuspendedPackageLauncherExtras(QUERIES_NOTHING_PERM,
469                     TARGET_NO_API));
470         } finally {
471             suspendPackages(false /* suspend */, Arrays.asList(TARGET_NO_API));
472         }
473     }
474 
475     @Test
getSuspendedPackageLauncherExtras_queriesNothing_cannotGetExtras()476     public void getSuspendedPackageLauncherExtras_queriesNothing_cannotGetExtras()
477             throws Exception {
478         try {
479             suspendPackagesForUser(true /* suspend */, Arrays.asList(TARGET_NO_API),
480                     UserReference.of(sContext.getUser()), true /* extraPersistableBundle */);
481             Assert.assertNull(getSuspendedPackageLauncherExtras(QUERIES_NOTHING,
482                     TARGET_NO_API));
483         } finally {
484             suspendPackages(false /* suspend */, Arrays.asList(TARGET_NO_API));
485         }
486     }
487 
488     @Test
shouldHideFromSuggestions_queriesPackage_canSeeNoApi()489     public void shouldHideFromSuggestions_queriesPackage_canSeeNoApi() throws Exception {
490         setDistractingPackageRestrictions(new String[]{TARGET_NO_API},
491                 PackageManager.RESTRICTION_HIDE_FROM_SUGGESTIONS);
492 
493         try {
494             final boolean hideFromSuggestions = shouldHideFromSuggestions(
495                     QUERIES_PACKAGE, TARGET_NO_API);
496             assertThat(hideFromSuggestions, is(true));
497         } finally {
498             setDistractingPackageRestrictions(new String[]{TARGET_NO_API},
499                     PackageManager.RESTRICTION_NONE);
500         }
501     }
502 
503     @Test
shouldHideFromSuggestions_queriesNothing_cannotSeeNoApi()504     public void shouldHideFromSuggestions_queriesNothing_cannotSeeNoApi() throws Exception {
505         setDistractingPackageRestrictions(new String[]{TARGET_NO_API},
506                 PackageManager.RESTRICTION_HIDE_FROM_SUGGESTIONS);
507 
508         try {
509             final boolean hideFromSuggestions = shouldHideFromSuggestions(
510                     QUERIES_NOTHING, TARGET_NO_API);
511             assertThat(hideFromSuggestions, is(false));
512         } finally {
513             setDistractingPackageRestrictions(new String[]{TARGET_NO_API},
514                     PackageManager.RESTRICTION_NONE);
515         }
516     }
517 
sendCommandAndWaitForLauncherAppsCallback(String sourcePackageName, int expectedEventCode, String[] expectedPackages)518     private Result sendCommandAndWaitForLauncherAppsCallback(String sourcePackageName,
519             int expectedEventCode, String[] expectedPackages) throws Exception {
520         final Bundle extra = new Bundle();
521         extra.putInt(EXTRA_FLAGS, expectedEventCode);
522         extra.putStringArray(EXTRA_PACKAGES, expectedPackages);
523         final Result result = sendCommand(sourcePackageName, null /* targetPackageName */,
524                 INVALID_UID /* targetUid */, extra, ACTION_AWAIT_LAUNCHER_APPS_CALLBACK,
525                 true /* waitForReady */);
526         return result;
527     }
528 
sendCommandAndWaitForLauncherAppsSessionCallback(String sourcePackageName, int expectedSessionId)529     private Result sendCommandAndWaitForLauncherAppsSessionCallback(String sourcePackageName,
530             int expectedSessionId) throws Exception {
531         final Bundle extra = new Bundle();
532         extra.putInt(EXTRA_ID, expectedSessionId);
533         final Result result = sendCommand(sourcePackageName, null /* targetPackageName */,
534                 INVALID_UID /* targetUid */, extra, ACTION_AWAIT_LAUNCHER_APPS_SESSION_CALLBACK,
535                 true /* waitForReady */);
536         return result;
537     }
538 
isActivityEnabled(String sourcePackageName, ComponentName componentName)539     private boolean isActivityEnabled(String sourcePackageName, ComponentName componentName)
540             throws Exception {
541         final Bundle extraData = new Bundle();
542         extraData.putString(Intent.EXTRA_COMPONENT_NAME, componentName.flattenToString());
543         final Bundle response = sendCommandBlocking(sourcePackageName, null /* targetPackageName */,
544                 extraData, ACTION_LAUNCHER_APPS_IS_ACTIVITY_ENABLED);
545         return response.getBoolean(Intent.EXTRA_RETURN_RESULT);
546     }
547 
getSuspendedPackageLauncherExtras(String sourcePackageName, String targetPackageName)548     private Bundle getSuspendedPackageLauncherExtras(String sourcePackageName,
549             String targetPackageName) throws Exception {
550         final Bundle response = sendCommandBlocking(sourcePackageName, targetPackageName,
551                 null /* extraData */, ACTION_LAUNCHER_APPS_GET_SUSPENDED_PACKAGE_LAUNCHER_EXTRAS);
552         return response.getBundle(Intent.EXTRA_RETURN_RESULT);
553     }
554 
setDistractingPackageRestrictions(String[] packagesToRestrict, int distractionFlags)555     private void setDistractingPackageRestrictions(String[] packagesToRestrict,
556             int distractionFlags) throws Exception {
557         final String[] failed = SystemUtil.callWithShellPermissionIdentity(
558                 () -> sPm.setDistractingPackageRestrictions(packagesToRestrict, distractionFlags));
559         assertThat(failed, emptyArray());
560     }
561 
shouldHideFromSuggestions(String sourcePackageName, String targetPackageName)562     private boolean shouldHideFromSuggestions(String sourcePackageName, String targetPackageName)
563             throws Exception {
564         final Bundle extraData = new Bundle();
565         extraData.putInt(Intent.EXTRA_USER, Process.myUserHandle().getIdentifier());
566         final Bundle response = sendCommandBlocking(sourcePackageName, targetPackageName, extraData,
567                 ACTION_LAUNCHER_APPS_SHOULD_HIDE_FROM_SUGGESTIONS);
568         return response.getBoolean(Intent.EXTRA_RETURN_RESULT);
569     }
570 }
571