/** * Sets extra resolve infos for an intent. * * Those entries are added to whatever might be in the manifest already. * * Note that all resolve infos will have {@link ResolveInfo#isDefault} field set to {@code * true} to allow their resolution for implicit intents. If this is not what you want, then you * still have the reference to those ResolveInfos, and you can set the field back to {@code * false}. * * @deprecated see the note on {@link #addResolveInfoForIntent(Intent, ResolveInfo)}. */ @Deprecated public void setResolveInfosForIntent(Intent intent, List<ResolveInfo> info) { resolveInfoForIntent.remove(intent); for (ResolveInfo resolveInfo : info) { addResolveInfoForIntent(intent, resolveInfo); } }
@Test public void queryIntentActivities_MatchSystemOnly() throws Exception { Intent i = new Intent(Intent.ACTION_MAIN, null); i.addCategory(Intent.CATEGORY_LAUNCHER); ResolveInfo info1 = ShadowResolveInfo.newResolveInfo(TEST_PACKAGE_LABEL, TEST_PACKAGE_NAME); ResolveInfo info2 = ShadowResolveInfo.newResolveInfo("System App", "system.launcher"); info2.activityInfo.applicationInfo.flags |= ApplicationInfo.FLAG_SYSTEM; info2.nonLocalizedLabel = "System App"; shadowPackageManager.addResolveInfoForIntent(i, info1); shadowPackageManager.addResolveInfoForIntent(i, info2); List<ResolveInfo> activities = packageManager.queryIntentActivities(i, PackageManager.MATCH_SYSTEM_ONLY); assertThat(activities).isNotNull(); assertThat(activities).hasSize(1); assertThat(activities.get(0).nonLocalizedLabel.toString()).isEqualTo("System App"); }
@Test public void queryBroadcastReceivers_Match() throws Exception { Intent i = new Intent(Intent.ACTION_MAIN, null); ResolveInfo info = new ResolveInfo(); info.nonLocalizedLabel = TEST_PACKAGE_LABEL; shadowPackageManager.addResolveInfoForIntent(i, info); List<ResolveInfo> broadCastReceivers = packageManager.queryBroadcastReceivers(i, 0); assertThat(broadCastReceivers).hasSize(1); assertThat(broadCastReceivers.get(0).nonLocalizedLabel.toString()) .isEqualTo(TEST_PACKAGE_LABEL); }
@Test @Config(minSdk = JELLY_BEAN_MR1) public void queryIntentActivitiesAsUser_Match() throws Exception { Intent i = new Intent(Intent.ACTION_MAIN, null); i.addCategory(Intent.CATEGORY_LAUNCHER); ResolveInfo info = new ResolveInfo(); info.nonLocalizedLabel = TEST_PACKAGE_LABEL; shadowPackageManager.addResolveInfoForIntent(i, info); List<ResolveInfo> activities = packageManager.queryIntentActivitiesAsUser(i, 0, 0); assertThat(activities).isNotNull(); assertThat(activities).hasSize(2); assertThat(activities.get(0).nonLocalizedLabel.toString()).isEqualTo(TEST_PACKAGE_LABEL); }
@Test public void testLaunchIntentForPackage() { Intent intent = packageManager.getLaunchIntentForPackage(TEST_PACKAGE_LABEL); assertThat(intent).isNull(); Intent launchIntent = new Intent(Intent.ACTION_MAIN); launchIntent.setPackage(TEST_PACKAGE_LABEL); launchIntent.addCategory(Intent.CATEGORY_LAUNCHER); ResolveInfo resolveInfo = new ResolveInfo(); resolveInfo.activityInfo = new ActivityInfo(); resolveInfo.activityInfo.packageName = TEST_PACKAGE_LABEL; resolveInfo.activityInfo.name = "LauncherActivity"; shadowPackageManager.addResolveInfoForIntent(launchIntent, resolveInfo); intent = packageManager.getLaunchIntentForPackage(TEST_PACKAGE_LABEL); assertThat(intent.getComponent().getClassName()).isEqualTo("LauncherActivity"); }
@Test public void queryIntentServices_Match() throws Exception { Intent i = new Intent(Intent.ACTION_MAIN, null); ResolveInfo info = new ResolveInfo(); info.serviceInfo = new ServiceInfo(); info.nonLocalizedLabel = TEST_PACKAGE_LABEL; shadowPackageManager.addResolveInfoForIntent(i, info); List<ResolveInfo> services = packageManager.queryIntentServices(i, 0); assertThat(services).hasSize(1); assertThat(services.get(0).nonLocalizedLabel.toString()).isEqualTo(TEST_PACKAGE_LABEL); }
@Test public void queryIntentActivities_Match() throws Exception { Intent i = new Intent(Intent.ACTION_MAIN, null); i.addCategory(Intent.CATEGORY_LAUNCHER); ResolveInfo info = new ResolveInfo(); info.nonLocalizedLabel = TEST_PACKAGE_LABEL; info.activityInfo = new ActivityInfo(); info.activityInfo.name = "name"; info.activityInfo.packageName = TEST_PACKAGE_NAME; shadowPackageManager.addResolveInfoForIntent(i, info); List<ResolveInfo> activities = packageManager.queryIntentActivities(i, 0); assertThat(activities).isNotNull(); assertThat(activities).hasSize(2); assertThat(activities.get(0).nonLocalizedLabel.toString()).isEqualTo(TEST_PACKAGE_LABEL); }
@Test public void queryIntentActivities_ServiceMatch() throws Exception { Intent i = new Intent("SomeStrangeAction"); ResolveInfo info = new ResolveInfo(); info.nonLocalizedLabel = TEST_PACKAGE_LABEL; info.serviceInfo = new ServiceInfo(); info.serviceInfo.name = "name"; info.serviceInfo.packageName = TEST_PACKAGE_NAME; shadowPackageManager.addResolveInfoForIntent(i, info); List<ResolveInfo> activities = packageManager.queryIntentActivities(i, 0); assertThat(activities).isNotNull(); assertThat(activities).isEmpty(); }
@Test @Config(minSdk = LOLLIPOP) public void queryIntentContentProviders_Match() throws Exception { Intent i = new Intent(DocumentsContract.PROVIDER_INTERFACE); ResolveInfo resolveInfo = new ResolveInfo(); ProviderInfo providerInfo = new ProviderInfo(); providerInfo.authority = "com.robolectric"; resolveInfo.providerInfo = providerInfo; shadowPackageManager.addResolveInfoForIntent(i, resolveInfo); List<ResolveInfo> contentProviders = packageManager.queryIntentContentProviders(i, 0); assertThat(contentProviders).hasSize(1); assertThat(contentProviders.get(0).providerInfo.authority).isEqualTo(providerInfo.authority); }
@Test public void getServiceInfo_shouldFindServiceIfAddedInResolveInfo() throws Exception { ComponentName componentName = new ComponentName("com.test", "com.test.ServiceName"); ResolveInfo resolveInfo = new ResolveInfo(); resolveInfo.serviceInfo = new ServiceInfo(); resolveInfo.serviceInfo.name = componentName.getClassName(); resolveInfo.serviceInfo.applicationInfo = new ApplicationInfo(); resolveInfo.serviceInfo.applicationInfo.packageName = componentName.getPackageName(); shadowPackageManager.addResolveInfoForIntent(new Intent("RANDOM_ACTION"), resolveInfo); ServiceInfo serviceInfo = packageManager.getServiceInfo(componentName, 0); assertThat(serviceInfo).isNotNull(); }
@Test public void removeResolveInfosForIntent_forService() throws Exception { Intent intent = new Intent(Intent.ACTION_APP_ERROR, null).addCategory(Intent.CATEGORY_APP_BROWSER); ResolveInfo info = new ResolveInfo(); info.nonLocalizedLabel = TEST_PACKAGE_LABEL; info.serviceInfo = new ServiceInfo(); info.serviceInfo.packageName = "com.org"; shadowPackageManager.addResolveInfoForIntent(intent, info); shadowPackageManager.removeResolveInfosForIntent(intent, "com.org"); assertThat(packageManager.resolveService(intent, 0)).isNull(); }
@Test public void removeResolveInfosForIntent_shouldCauseResolveActivityToReturnNull() throws Exception { Intent intent = new Intent(Intent.ACTION_APP_ERROR, null).addCategory(Intent.CATEGORY_APP_BROWSER); ResolveInfo info = new ResolveInfo(); info.nonLocalizedLabel = TEST_PACKAGE_LABEL; info.activityInfo = new ActivityInfo(); info.activityInfo.packageName = "com.org"; shadowPackageManager.addResolveInfoForIntent(intent, info); shadowPackageManager.removeResolveInfosForIntent(intent, "com.org"); assertThat(packageManager.resolveActivity(intent, 0)).isNull(); }
@Test public void resolveService_Match() throws Exception { Intent i = new Intent(Intent.ACTION_MAIN, null); ResolveInfo info = new ResolveInfo(); info.serviceInfo = new ServiceInfo(); info.serviceInfo.name = "name"; shadowPackageManager.addResolveInfoForIntent(i, info); assertThat(packageManager.resolveService(i, 0)).isNotNull(); assertThat(packageManager.resolveService(i, 0).serviceInfo.name).isEqualTo("name"); }
@Test @Config(minSdk = LOLLIPOP) public void queryIntentContentProviders_MatchSystemOnly() throws Exception { Intent i = new Intent(DocumentsContract.PROVIDER_INTERFACE); ResolveInfo info1 = new ResolveInfo(); info1.providerInfo = new ProviderInfo(); info1.providerInfo.applicationInfo = new ApplicationInfo(); ResolveInfo info2 = new ResolveInfo(); info2.providerInfo = new ProviderInfo(); info2.providerInfo.applicationInfo = new ApplicationInfo(); info2.providerInfo.applicationInfo.flags |= ApplicationInfo.FLAG_SYSTEM; info2.nonLocalizedLabel = "System App"; shadowPackageManager.addResolveInfoForIntent(i, info1); shadowPackageManager.addResolveInfoForIntent(i, info2); List<ResolveInfo> activities = packageManager.queryIntentContentProviders(i, PackageManager.MATCH_SYSTEM_ONLY); assertThat(activities).isNotNull(); assertThat(activities).hasSize(1); assertThat(activities.get(0).nonLocalizedLabel.toString()).isEqualTo("System App"); }
@Test public void testResolveDifferentIntentObjects() { Intent intent1 = new Intent(Intent.ACTION_MAIN); intent1.setPackage(TEST_PACKAGE_LABEL); intent1.addCategory(Intent.CATEGORY_APP_BROWSER); assertThat(packageManager.resolveActivity(intent1, 0)).isNull(); ResolveInfo resolveInfo = new ResolveInfo(); resolveInfo.activityInfo = new ActivityInfo(); resolveInfo.activityInfo.packageName = TEST_PACKAGE_LABEL; resolveInfo.activityInfo.name = "BrowserActivity"; shadowPackageManager.addResolveInfoForIntent(intent1, resolveInfo); // the original intent object should yield a result ResolveInfo result = packageManager.resolveActivity(intent1, 0); assertThat(result.activityInfo.name).isEqualTo("BrowserActivity"); // AND a new, functionally equivalent intent should also yield a result Intent intent2 = new Intent(Intent.ACTION_MAIN); intent2.setPackage(TEST_PACKAGE_LABEL); intent2.addCategory(Intent.CATEGORY_APP_BROWSER); result = packageManager.resolveActivity(intent2, 0); assertThat(result.activityInfo.name).isEqualTo("BrowserActivity"); }
@Test public void resolveActivity_Match() throws Exception { Intent i = new Intent(Intent.ACTION_MAIN, null).addCategory(Intent.CATEGORY_LAUNCHER); ResolveInfo info = new ResolveInfo(); info.nonLocalizedLabel = TEST_PACKAGE_LABEL; info.activityInfo = new ActivityInfo(); info.activityInfo.name = "name"; info.activityInfo.packageName = TEST_PACKAGE_NAME; shadowPackageManager.addResolveInfoForIntent(i, info); assertThat(packageManager.resolveActivity(i, 0)).isNotNull(); assertThat(packageManager.resolveActivity(i, 0).activityInfo.name).isEqualTo("name"); assertThat(packageManager.resolveActivity(i, 0).activityInfo.packageName) .isEqualTo(TEST_PACKAGE_NAME); }
public static void addResolver(Intent intent) { ShadowPackageManager packageManager = shadowOf(RuntimeEnvironment.application.getPackageManager()); packageManager.addResolveInfoForIntent( intent, ShadowResolveInfo.newResolveInfo("label", "com.android.chrome", "DefaultActivity")); }
@Test @Config(minSdk = LOLLIPOP) public void queryIntentContentProviders_MatchDisabledComponents() throws Exception { Intent i = new Intent(DocumentsContract.PROVIDER_INTERFACE); ResolveInfo resolveInfo = new ResolveInfo(); resolveInfo.providerInfo = new ProviderInfo(); resolveInfo.providerInfo.applicationInfo = new ApplicationInfo(); resolveInfo.providerInfo.applicationInfo.packageName = "org.robolectric.shadows.TestPackageName"; resolveInfo.providerInfo.name = "org.robolectric.shadows.TestProvider"; resolveInfo.providerInfo.enabled = false; shadowPackageManager.addResolveInfoForIntent(i, resolveInfo); List<ResolveInfo> resolveInfos = packageManager.queryIntentContentProviders(i, 0); assertThat(resolveInfos).isEmpty(); resolveInfos = packageManager.queryIntentContentProviders(i, PackageManager.MATCH_DISABLED_COMPONENTS); assertThat(resolveInfos).isNotNull(); assertThat(resolveInfos).hasSize(1); }
@Test public void testResolvePartiallySimilarIntents() { Intent intent1 = new Intent(Intent.ACTION_APP_ERROR); intent1.setPackage(TEST_PACKAGE_LABEL); intent1.addCategory(Intent.CATEGORY_APP_BROWSER); assertThat(packageManager.resolveActivity(intent1, 0)).isNull(); ResolveInfo resolveInfo = new ResolveInfo(); resolveInfo.activityInfo = new ActivityInfo(); resolveInfo.activityInfo.packageName = TEST_PACKAGE_LABEL; resolveInfo.activityInfo.name = "BrowserActivity"; shadowPackageManager.addResolveInfoForIntent(intent1, resolveInfo); // the original intent object should yield a result ResolveInfo result = packageManager.resolveActivity(intent1, 0); assertThat(result.activityInfo.name).isEqualTo("BrowserActivity"); // an intent with just the same action should not be considered the same Intent intent2 = new Intent(Intent.ACTION_APP_ERROR); result = packageManager.resolveActivity(intent2, 0); assertThat(result).isNull(); // an intent with just the same category should not be considered the same Intent intent3 = new Intent(); intent3.addCategory(Intent.CATEGORY_APP_BROWSER); result = packageManager.resolveActivity(intent3, 0); assertThat(result).isNull(); // an intent without the correct package restriction should not be the same Intent intent4 = new Intent(Intent.ACTION_APP_ERROR); intent4.addCategory(Intent.CATEGORY_APP_BROWSER); result = packageManager.resolveActivity(intent4, 0); assertThat(result).isNull(); }
@Test @Config(minSdk = JELLY_BEAN_MR1) public void resolveActivityAsUser_Match() throws Exception { Intent i = new Intent(Intent.ACTION_MAIN, null).addCategory(Intent.CATEGORY_LAUNCHER); ResolveInfo info = new ResolveInfo(); info.nonLocalizedLabel = TEST_PACKAGE_LABEL; info.activityInfo = new ActivityInfo(); info.activityInfo.name = "name"; info.activityInfo.packageName = TEST_PACKAGE_NAME; shadowPackageManager.addResolveInfoForIntent(i, info); ResolveInfo resolvedActivity = ReflectionHelpers.callInstanceMethod( packageManager, "resolveActivityAsUser", ClassParameter.from(Intent.class, i), ClassParameter.from(int.class, 0), ClassParameter.from(int.class, USER_ID)); assertThat(resolvedActivity).isNotNull(); assertThat(resolvedActivity.activityInfo.name).isEqualTo("name"); assertThat(resolvedActivity.activityInfo.packageName).isEqualTo(TEST_PACKAGE_NAME); }