Java Code Examples for org.robolectric.shadows.ShadowPackageManager#addResolveInfoForIntent()

The following examples show how to use org.robolectric.shadows.ShadowPackageManager#addResolveInfoForIntent() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RobolectricUtils.java    From android-browser-helper with Apache License 2.0 6 votes vote down vote up
/**
 * Ensures intents with {@link CustomTabsService#ACTION_CUSTOM_TABS_CONNECTION} resolve to a
 * Service with given categories
 */
public static void installCustomTabsService(String providerPackage, List<String> categories) {
    Intent intent = new Intent()
            .setAction(CustomTabsService.ACTION_CUSTOM_TABS_CONNECTION)
            .setPackage(providerPackage);

    IntentFilter filter = new IntentFilter();
    for (String category : categories) {
        filter.addCategory(category);
    }
    ResolveInfo resolveInfo = new ResolveInfo();
    resolveInfo.filter = filter;

    ShadowPackageManager manager = Shadows.shadowOf(RuntimeEnvironment.application
            .getPackageManager());
    manager.addResolveInfoForIntent(intent, resolveInfo);
}
 
Example 2
Source File: OAuthClientConfigurationTest.java    From okta-sdk-appauth-android with Apache License 2.0 6 votes vote down vote up
private void addResolveInfoForRedirectUri() {
    Intent redirectIntent = new Intent();
    redirectIntent.setPackage(mContext.getPackageName());
    redirectIntent.setAction(Intent.ACTION_VIEW);
    redirectIntent.addCategory(Intent.CATEGORY_BROWSABLE);
    redirectIntent.setData(Uri.parse("com.okta.appauth.android.test:/oauth2redirect"));

    ShadowPackageManager packageManager = shadowOf(mContext.getPackageManager());
    ResolveInfo info = new ResolveInfo();
    info.isDefault = true;

    ApplicationInfo applicationInfo = new ApplicationInfo();
    applicationInfo.packageName = "com.example";
    info.activityInfo = new ActivityInfo();
    info.activityInfo.applicationInfo = applicationInfo;
    info.activityInfo.name = "Example";

    packageManager.addResolveInfoForIntent(redirectIntent, info);
}
 
Example 3
Source File: TransactionViewActivityTest.java    From budget-watch with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Register a handler in the package manager for a image capture intent
 */
private void registerMediaStoreIntentHandler()
{
    // Add something that will 'handle' the media capture intent
    ShadowPackageManager shadowPackageManager = shadowOf(RuntimeEnvironment.application.getPackageManager());

    ResolveInfo info = new ResolveInfo();
    info.isDefault = true;

    ApplicationInfo applicationInfo = new ApplicationInfo();
    applicationInfo.packageName = "does.not.matter";
    info.activityInfo = new ActivityInfo();
    info.activityInfo.applicationInfo = applicationInfo;
    info.activityInfo.name = "DoesNotMatter";

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    shadowPackageManager.addResolveInfoForIntent(intent, info);
}
 
Example 4
Source File: ImportExportActivityTest.java    From budget-watch with GNU General Public License v3.0 6 votes vote down vote up
private void registerIntentHandler(String handler)
{
    // Add something that will 'handle' the given intent type
    ShadowPackageManager shadowPackageManager = shadowOf(RuntimeEnvironment.application.getPackageManager());

    ResolveInfo info = new ResolveInfo();
    info.isDefault = true;

    ApplicationInfo applicationInfo = new ApplicationInfo();
    applicationInfo.packageName = "does.not.matter";
    info.activityInfo = new ActivityInfo();
    info.activityInfo.applicationInfo = applicationInfo;
    info.activityInfo.name = "DoesNotMatter";
    info.activityInfo.exported = true;

    Intent intent = new Intent(handler);

    if(handler.equals(Intent.ACTION_GET_CONTENT))
    {
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        intent.setType("*/*");
    }

    shadowPackageManager.addResolveInfoForIntent(intent, info);
}
 
Example 5
Source File: WebFragmentTest.java    From materialistic with Apache License 2.0 6 votes vote down vote up
@Test
public void testDownloadContent() {
    ResolveInfo resolverInfo = new ResolveInfo();
    resolverInfo.activityInfo = new ActivityInfo();
    resolverInfo.activityInfo.applicationInfo = new ApplicationInfo();
    resolverInfo.activityInfo.applicationInfo.packageName =
            ListActivity.class.getPackage().getName();
    resolverInfo.activityInfo.name = ListActivity.class.getName();
    ShadowPackageManager rpm = shadowOf(RuntimeEnvironment.application.getPackageManager());
    final String url = "http://example.com/file.doc";
    rpm.addResolveInfoForIntent(new Intent(Intent.ACTION_VIEW, Uri.parse(url)), resolverInfo);

    WebView webView = activity.findViewById(R.id.web_view);
    ShadowWebView shadowWebView = Shadow.extract(webView);
    when(item.getUrl()).thenReturn(url);
    shadowWebView.getDownloadListener().onDownloadStart(url, "", "", "", 0L);
    assertThat((View) activity.findViewById(R.id.empty)).isVisible();
    activity.findViewById(R.id.download_button).performClick();
    assertNotNull(shadowOf(activity).getNextStartedActivity());
}
 
Example 6
Source File: WebFragmentTest.java    From materialistic with Apache License 2.0 6 votes vote down vote up
@Test
public void testDownloadPdf() {
    ResolveInfo resolverInfo = new ResolveInfo();
    resolverInfo.activityInfo = new ActivityInfo();
    resolverInfo.activityInfo.applicationInfo = new ApplicationInfo();
    resolverInfo.activityInfo.applicationInfo.packageName = ListActivity.class.getPackage().getName();
    resolverInfo.activityInfo.name = ListActivity.class.getName();
    ShadowPackageManager rpm = shadowOf(RuntimeEnvironment.application.getPackageManager());
    when(item.getUrl()).thenReturn("http://example.com/file.pdf");
    rpm.addResolveInfoForIntent(new Intent(Intent.ACTION_VIEW, Uri.parse(item.getUrl())), resolverInfo);

    WebView webView = activity.findViewById(R.id.web_view);
    ShadowWebView shadowWebView = Shadow.extract(webView);
    WebFragment fragment = (WebFragment) activity.getSupportFragmentManager()
            .findFragmentByTag(WebFragment.class.getName());
    shadowWebView.getDownloadListener().onDownloadStart(item.getUrl(), "", "", "application/pdf", 0L);
    shadowWebView.getWebViewClient().onPageFinished(webView, PDF_LOADER_URL);
    verify(fragment.mFileDownloader).downloadFile(
        eq(item.getUrl()),
        eq("application/pdf"),
        any(FileDownloader.FileDownloaderCallback.class));
}
 
Example 7
Source File: RobolectricUtils.java    From custom-tabs-client with Apache License 2.0 6 votes vote down vote up
/**
 * Ensures intents with {@link CustomTabsService#ACTION_CUSTOM_TABS_CONNECTION} resolve to a
 * Service with given categories
 */
public static void installCustomTabsService(String providerPackage, List<String> categories) {
    Intent intent = new Intent()
            .setAction(CustomTabsService.ACTION_CUSTOM_TABS_CONNECTION)
            .setPackage(providerPackage);

    IntentFilter filter = new IntentFilter();
    for (String category : categories) {
        filter.addCategory(category);
    }
    ResolveInfo resolveInfo = new ResolveInfo();
    resolveInfo.filter = filter;

    ShadowPackageManager manager = Shadows.shadowOf(RuntimeEnvironment.application
            .getPackageManager());
    manager.addResolveInfoForIntent(intent, resolveInfo);
}
 
Example 8
Source File: TestApplication.java    From materialistic with Apache License 2.0 4 votes vote down vote up
public static void addResolver(Intent intent) {
    ShadowPackageManager packageManager = shadowOf(RuntimeEnvironment.application.getPackageManager());
    packageManager.addResolveInfoForIntent( intent,
            ShadowResolveInfo.newResolveInfo("label", "com.android.chrome", "DefaultActivity"));
}
 
Example 9
Source File: ItemActivityTest.java    From materialistic with Apache License 2.0 4 votes vote down vote up
@SuppressLint("NewApi")
@Test
public void testOptionExternal() {
    ShadowPackageManager packageManager = shadowOf(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 = Robolectric.buildActivity(ItemActivity.class, intent);
    controller.create().start().resume();
    activity = controller.get();

    // 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);
}