IntentUtils { public static boolean isActivityAvailable(Context context, Intent intent) { final PackageManager packageManager = context.getPackageManager(); final List<ResolveInfo> activities = packageManager.queryIntentActivities(intent, 0); return !activities.isEmpty(); }  static boolean isActivityAvailable(Context context, Intent intent); static boolean safeStartActivity(Context context, Intent intent);  }
@Test public void testIsActivityAvailable_activitiesAvailable() { final List<ResolveInfo> activities = new ArrayList<>(); activities.add(mock(ResolveInfo.class)); when(pm.queryIntentActivities(intent, 0)).thenReturn(activities); when(context.getPackageManager()).thenReturn(pm); assertTrue(IntentUtils.isActivityAvailable(context, intent)); }
@Test public void testIsActivityAvailable_noActivitiesAvailable() { when(pm.queryIntentActivities(intent, 0)) .thenReturn(Collections.EMPTY_LIST); when(context.getPackageManager()).thenReturn(pm); assertFalse(IntentUtils.isActivityAvailable(context, intent)); verify(context).getPackageManager(); verify(pm).queryIntentActivities(intent, 0); }
@Test public void testIsActivityAvailable_activityAvailable() { final List<ResolveInfo> activities = new ArrayList<>(); activities.add(mock(ResolveInfo.class)); when(pm.queryIntentActivities(intent, 0)).thenReturn(activities); when(context.getPackageManager()).thenReturn(pm); assertTrue(IntentUtils.isActivityAvailable(context, intent)); verify(context).getPackageManager(); verify(pm).queryIntentActivities(intent, 0); }