@Test public void should_start_url_intent() { // Given 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("", "", "")); // When boolean result = Intents.startUri(activity, url); // Then 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_start_external_uri() { // Given String url = "http://www.google.com"; RobolectricPackageManager rpm = (RobolectricPackageManager) RuntimeEnvironment.application.getPackageManager(); rpm.addResolveInfoForIntent(new Intent(Intent.ACTION_VIEW, Uri.parse(url)), ShadowResolveInfo.newResolveInfo("", "", "")); // When Intents.startExternalUrl(activity, url); // Then ShadowActivity shadowActivity = Shadows.shadowOf(activity); Intent startedIntent = shadowActivity.getNextStartedActivity(); assertThat(startedIntent.getAction()).isEqualTo(Intent.ACTION_VIEW); assertThat(startedIntent.getData().toString()).isEqualTo(url); }
@Test public void testSelectItemOpenExternal() { RobolectricPackageManager packageManager = (RobolectricPackageManager) RuntimeEnvironment.application.getPackageManager(); packageManager.addResolveInfoForIntent( new Intent(Intent.ACTION_VIEW, Uri.parse("http://example.com")), ShadowResolveInfo.newResolveInfo("label", "com.android.chrome", "DefaultActivity")); PreferenceManager.getDefaultSharedPreferences(activity) .edit() .putBoolean(activity.getString(R.string.pref_external), true) .commit(); controller.pause().resume(); activity.onItemSelected(new TestWebItem() { @Override public String getUrl() { return "http://example.com"; } }); assertThat(shadowOf(activity).getNextStartedActivity()).hasAction(Intent.ACTION_VIEW); }
@Test public void testSelectItemStartActionView() { RobolectricPackageManager packageManager = (RobolectricPackageManager) RuntimeEnvironment.application.getPackageManager(); packageManager.addResolveInfoForIntent( new Intent(Intent.ACTION_VIEW, Uri.parse("http://example.com")), ShadowResolveInfo.newResolveInfo("label", "com.android.chrome", "DefaultActivity")); packageManager.addResolveInfoForIntent( new Intent(Intent.ACTION_VIEW, Uri.parse("http://example.com")), ShadowResolveInfo.newResolveInfo("label", "com.android.browser", "DefaultActivity")); PreferenceManager.getDefaultSharedPreferences(activity) .edit() .putBoolean(activity.getString(R.string.pref_external), true) .commit(); controller.pause().resume(); activity.onItemSelected(new TestWebItem() { @Override public String getUrl() { return "http://example.com"; } }); assertThat(shadowOf(activity).getNextStartedActivity()).hasAction(Intent.ACTION_VIEW); }
@Test public void testSelectItemOpenChooser() { RobolectricPackageManager packageManager = (RobolectricPackageManager) RuntimeEnvironment.application.getPackageManager(); packageManager.addResolveInfoForIntent( new Intent(Intent.ACTION_VIEW, Uri.parse("http://news.ycombinator.com/item?id=1")), ShadowResolveInfo.newResolveInfo("label", "com.android.chrome", "DefaultActivity")); packageManager.addResolveInfoForIntent( new Intent(Intent.ACTION_VIEW, Uri.parse("http://news.ycombinator.com/item?id=1")), ShadowResolveInfo.newResolveInfo("label", "com.android.browser", "DefaultActivity")); PreferenceManager.getDefaultSharedPreferences(activity) .edit() .putBoolean(activity.getString(R.string.pref_external), true) .commit(); controller.pause().resume(); activity.onItemSelected(new TestWebItem() { @Override public String getUrl() { return "http://news.ycombinator.com/item?id=1"; } }); assertThat(shadowOf(activity).getNextStartedActivity()).hasAction(Intent.ACTION_CHOOSER); }
@Test public void testBindService() throws RemoteException { // no chrome installed should not bind service delegate.bindCustomTabsService(activity); assertThat(ShadowApplication.getInstance().getBoundServiceConnections()).isEmpty(); // bind service should create connection RuntimeEnvironment.getRobolectricPackageManager().addResolveInfoForIntent( new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.example.com")), ShadowResolveInfo.newResolveInfo("label", "com.android.chrome", "DefaultActivity")); RuntimeEnvironment.getRobolectricPackageManager().addResolveInfoForIntent( new Intent("android.support.customtabs.action.CustomTabsService") .setPackage("com.android.chrome"), ShadowResolveInfo.newResolveInfo("label", "com.android.chrome", "DefaultActivity")); delegate.bindCustomTabsService(activity); List<ServiceConnection> connections = ShadowApplication.getInstance() .getBoundServiceConnections(); assertThat(connections).isNotEmpty(); // on service connected should create session and warm up client verify(service).warmup(anyLong()); assertNotNull(delegate.getSession()); verify(service).newSession(any(ICustomTabsCallback.class)); // may launch url should success when(service.mayLaunchUrl(any(), any(), any(), any())).thenReturn(true); assertTrue(delegate.mayLaunchUrl(Uri.parse("http://www.example.com"), null, null)); // on service disconnected should clear session delegate.unbindCustomTabsService(activity); assertNull(delegate.getSession()); }
public static void addResolver(Intent intent) { RobolectricPackageManager packageManager = (RobolectricPackageManager) RuntimeEnvironment.application.getPackageManager(); packageManager.addResolveInfoForIntent( intent, ShadowResolveInfo.newResolveInfo("label", "com.android.chrome", "DefaultActivity")); }
@SuppressLint("NewApi") @Test public void testOptionExternal() { RobolectricPackageManager packageManager = (RobolectricPackageManager) RuntimeEnvironment.application.getPackageManager(); packageManager.addResolveInfoForIntent( new Intent(Intent.ACTION_VIEW, Uri.parse("http://example.com")), ShadowResolveInfo.newResolveInfo("label", "com.android.chrome", "DefaultActivity")); packageManager.addResolveInfoForIntent( new Intent(Intent.ACTION_VIEW, Uri.parse(String.format(HackerNewsClient.WEB_ITEM_PATH, "1"))), ShadowResolveInfo.newResolveInfo("label", "com.android.chrome", "DefaultActivity")); Intent intent = new Intent(); intent.putExtra(ItemActivity.EXTRA_ITEM, new TestItem() { @NonNull @Override public String getType() { return STORY_TYPE; } @Override public String getUrl() { return "http://example.com"; } @Override public boolean isStoryType() { return true; } @Override public String getId() { return "1"; } }); controller.withIntent(intent).create().start().resume(); // inflate menu, see https://github.com/robolectric/robolectric/issues/1326 ShadowLooper.pauseMainLooper(); controller.visible(); ShadowApplication.getInstance().getForegroundThreadScheduler().advanceToLastPostedRunnable(); // open article shadowOf(activity).clickMenuItem(R.id.menu_external); shadowOf(ShadowPopupMenu.getLatestPopupMenu()) .getOnMenuItemClickListener() .onMenuItemClick(new RoboMenuItem(R.id.menu_article)); ShadowApplication.getInstance().getForegroundThreadScheduler().advanceToLastPostedRunnable(); assertThat(shadowOf(activity).getNextStartedActivity()).hasAction(Intent.ACTION_VIEW); // open item shadowOf(activity).clickMenuItem(R.id.menu_external); shadowOf(ShadowPopupMenu.getLatestPopupMenu()) .getOnMenuItemClickListener() .onMenuItemClick(new RoboMenuItem(R.id.menu_comments)); ShadowApplication.getInstance().getForegroundThreadScheduler().advanceToLastPostedRunnable(); assertThat(shadowOf(activity).getNextStartedActivity()).hasAction(Intent.ACTION_VIEW); }
public static ShadowResolveInfo shadowOf(ResolveInfo instance) { return (ShadowResolveInfo) shadowOf_(instance); }