Intents { public static boolean startUri(@NonNull Context context, @NonNull String url) { Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); if (intent.resolveActivity(context.getPackageManager()) != null) { context.startActivity(intent); return true; } return false; } private  Intents(); static boolean startUri(@NonNull Context context, @NonNull String url); static void startExternalUrl(@NonNull Activity activity, @NonNull String url);  }
@Test public void should_start_url_intent() { String url = "geo:22.5430883,114.1043205"; RobolectricPackageManager rpm = (RobolectricPackageManager) RuntimeEnvironment.application.getPackageManager(); rpm.addResolveInfoForIntent(new Intent(Intent.ACTION_VIEW, Uri.parse(url)), ShadowResolveInfo.newResolveInfo("", "", "")); boolean result = Intents.startUri(activity, url); ShadowActivity shadowActivity = Shadows.shadowOf(activity); Intent startedIntent = shadowActivity.getNextStartedActivity(); assertThat(result).isTrue(); assertThat(startedIntent.getAction()).isEqualTo(Intent.ACTION_VIEW); assertThat(startedIntent.getData().toString()).isEqualTo(url); }
@Test public void should_return_false_when_nothing_on_the_device_can_open_the_uri() { String url = "http: RobolectricPackageManager rpm = (RobolectricPackageManager) RuntimeEnvironment.application.getPackageManager(); rpm.addResolveInfoForIntent(new Intent(Intent.ACTION_VIEW, Uri.parse(url)), (ResolveInfo) null); boolean result = Intents.startUri(activity, url); ShadowActivity shadowActivity = Shadows.shadowOf(activity); Intent startedIntent = shadowActivity.getNextStartedActivity(); assertThat(result).isFalse(); assertThat(startedIntent).isNull(); }