org.robolectric.shadows.ShadowPackageManager Java Examples

The following examples show how to use org.robolectric.shadows.ShadowPackageManager. 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: BluetoothCentralTest.java    From blessed-android with MIT License 6 votes vote down vote up
@Before
public void setUp() throws Exception {
    initMocks(this);

    application = ShadowApplication.getInstance();

    bluetoothAdapter = Shadow.extract(ShadowBluetoothLEAdapter.getDefaultAdapter());
    bluetoothAdapter.setEnabled(true);
    bluetoothAdapter.setBluetoothLeScanner(scanner);

    context = application.getApplicationContext();

    // Setup hardware features
    PackageManager packageManager = context.getPackageManager();
    ShadowPackageManager shadowPackageManager = shadowOf(packageManager);
    shadowPackageManager.setSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE, true);

    central = new BluetoothCentral(context, callback, handler);
}
 
Example #2
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 #3
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 #4
Source File: UTest.java    From Taskbar with Apache License 2.0 6 votes vote down vote up
@Test
public void testHasFreeformSupportWithFreeformEnabledAndNMR1AboveVersion() {
    PowerMockito.spy(U.class);
    when(U.canEnableFreeform()).thenReturn(true);
    assertFalse(U.hasFreeformSupport(context));
    // Case 1, system has feature freeform.
    PackageManager packageManager = context.getPackageManager();
    ShadowPackageManager shadowPackageManager = Shadows.shadowOf(packageManager);
    shadowPackageManager
            .setSystemFeature(PackageManager.FEATURE_FREEFORM_WINDOW_MANAGEMENT, true);
    assertTrue(U.hasFreeformSupport(context));
    shadowPackageManager
            .setSystemFeature(PackageManager.FEATURE_FREEFORM_WINDOW_MANAGEMENT, false);
    // Case 2, enable_freeform_support in Settings.Global is not 0
    Settings.Global.putInt(context.getContentResolver(), "enable_freeform_support", 1);
    assertTrue(U.hasFreeformSupport(context));
    Settings.Global.putInt(context.getContentResolver(), "enable_freeform_support", 0);
}
 
Example #5
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 #6
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 #7
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 #8
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 #9
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 #10
Source File: UTest.java    From Taskbar with Apache License 2.0 5 votes vote down vote up
@Test
public void testIsChromeOs() {
    PackageManager packageManager = context.getPackageManager();
    ShadowPackageManager shadowPackageManager = Shadows.shadowOf(packageManager);
    shadowPackageManager.setSystemFeature("org.chromium.arc", true);
    assertTrue(U.isChromeOs(context));
    shadowPackageManager.setSystemFeature("org.chromium.arc", false);
    assertFalse(U.isChromeOs(context));
}
 
Example #11
Source File: IconPackManagerTest.java    From Taskbar with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetAvailableIconPacks() throws PackageManager.NameNotFoundException {
    List<IconPack> iconPacks = iconPackManager.getAvailableIconPacks(context);
    assertEquals(0, iconPacks.size());

    String testPackageName = "com.test.package";
    PackageManager packageManager = context.getPackageManager();
    ShadowPackageManager shadowPackageManager = Shadows.shadowOf(packageManager);
    int testIconPackSize = 20;
    for (int i = 1; i <= testIconPackSize; i++) {
        ComponentName componentName =
                new ComponentName(testPackageName + i, testPackageName + i);
        IntentFilter intentFilter = new IntentFilter("org.adw.launcher.THEMES");
        shadowPackageManager.addActivityIfNotPresent(componentName);
        shadowPackageManager.addIntentFilterForActivity(componentName, intentFilter);
        iconPacks = iconPackManager.getAvailableIconPacks(context);
    }
    assertEquals(testIconPackSize, iconPacks.size());
    Set<String> fetchedIconPackPackages = new HashSet<>();
    iconPacks.forEach(iconPack -> fetchedIconPackPackages.add(iconPack.getPackageName()));
    assertEquals(testIconPackSize, fetchedIconPackPackages.size());
    Set<String> expectedIconPackPackages = new HashSet<>();
    for (int i = 1; i <= testIconPackSize; i++) {
        expectedIconPackPackages.add(testPackageName + i);
        shadowPackageManager.removeActivity(
                new ComponentName(testPackageName + i, testPackageName + i)
        );
    }
    assertEquals(expectedIconPackPackages, fetchedIconPackPackages);
}
 
Example #12
Source File: TestUtils.java    From Once with Apache License 2.0 5 votes vote down vote up
static void simulateAppUpdate() {
    Context applicationContext = ApplicationProvider.getApplicationContext();
    ShadowPackageManager spm = shadowOf(applicationContext.getPackageManager());
    PackageInfo packageInfo = new PackageInfo();
    packageInfo.packageName = applicationContext.getPackageName();
    packageInfo.lastUpdateTime = new Date().getTime();
    spm.installPackage(packageInfo);
    Once.initialise(applicationContext);
}
 
Example #13
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 #14
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);
}