IntentUtils { public static boolean safeStartActivity(Context context, Intent intent) { if (isActivityAvailable(context, intent)) { context.startActivity(intent); return true; } return false; }  static boolean isActivityAvailable(Context context, Intent intent); static boolean safeStartActivity(Context context, Intent intent);  }
@Test public void testSafeStartActivity() { when(pm.queryIntentActivities(intent, 0)) .thenReturn(Collections.EMPTY_LIST); when(context.getPackageManager()).thenReturn(pm); final Intent intent = new Intent("com.twitter.is.awesome"); assertFalse(IntentUtils.safeStartActivity(context, intent)); verify(context).getPackageManager(); verify(pm).queryIntentActivities(intent, 0); }
@Test @SuppressWarnings("WrongConstant") public void testSafeStartActivity_noActivitiesAvailable() { when(pm.queryIntentActivities(intent, 0)) .thenReturn(Collections.EMPTY_LIST); when(context.getPackageManager()).thenReturn(pm); IntentUtils.safeStartActivity(context, intent); verify(context).getPackageManager(); verifyNoMoreInteractions(context); }
@Test public void testSafeStartActivity_activityAvailable() { final List<ResolveInfo> activities = new ArrayList<>(); activities.add(mock(ResolveInfo.class)); when(pm.queryIntentActivities(intent, 0)).thenReturn(activities); when(context.getPackageManager()).thenReturn(pm); IntentUtils.safeStartActivity(context, intent); verify(context).startActivity(intent); }