private static ResolveInfo buildResolveInfo(ComponentInfo componentInfo) { ResolveInfo resolveInfo = new ResolveInfo(); resolveInfo.resolvePackageName = componentInfo.applicationInfo.packageName; resolveInfo.labelRes = componentInfo.labelRes; resolveInfo.icon = componentInfo.icon; resolveInfo.nonLocalizedLabel = componentInfo.nonLocalizedLabel; return resolveInfo; }
/** * Creates a {@link ResolveInfo}. * * @param displayName Display name. * @param packageName Package name. * @param activityName Activity name. * @return Resolve info instance. */ public static ResolveInfo newResolveInfo(String displayName, String packageName, String activityName) { ResolveInfo resInfo = new ResolveInfo(); ActivityInfo actInfo = new ActivityInfo(); actInfo.applicationInfo = new ApplicationInfo(); actInfo.packageName = packageName; actInfo.applicationInfo.packageName = packageName; actInfo.name = activityName; resInfo.activityInfo = actInfo; resInfo.nonLocalizedLabel = displayName; return resInfo; }
@Test public void resolveInfoComparator() { ResolveInfo priority = new ResolveInfo(); priority.priority = 100; ResolveInfo preferredOrder = new ResolveInfo(); preferredOrder.preferredOrder = 100; ResolveInfo defaultResolveInfo = new ResolveInfo(); ResolveInfo[] array = new ResolveInfo[] {priority, preferredOrder, defaultResolveInfo}; Arrays.sort(array, new ResolveInfoComparator()); assertThat(array) .asList() .containsExactly(preferredOrder, priority, defaultResolveInfo) .inOrder(); }
private ArgumentCaptor<Intent> mockPackageManagerInfo() { PackageManager packageManager = mock(PackageManager.class); when(mMockContext.getPackageManager()).thenReturn(packageManager); ArgumentCaptor<Intent> intentArgCaptor = ArgumentCaptor.forClass(Intent.class); ResolveInfo info = new ResolveInfo(); info.serviceInfo = new ServiceInfo(); info.serviceInfo.enabled = true; // noinspection WrongConstant when(packageManager.queryIntentServices(intentArgCaptor.capture(), eq(0))) .thenReturn(Arrays.asList(info)); return intentArgCaptor; }
@Test public void verifyCreateSuccessful() { PackageManager packageManager = mock(PackageManager.class); when(packageManager.queryIntentServices(any(Intent.class), anyInt())).thenReturn(Collections.singletonList(new ResolveInfo())); when(packageManager.queryBroadcastReceivers(any(Intent.class), anyInt())).thenReturn(Collections.singletonList(new ResolveInfo())); Context context = mockContext(); when(context.getPackageManager()).thenReturn(packageManager); JobManager.create(context); }
@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 validateService_noActiveService() { when(mockContext.getPackageManager()).thenReturn(packageManagerMock); ResolveInfo resolveInfo = new ResolveInfo(); resolveInfo.serviceInfo = new ServiceInfo(); resolveInfo.serviceInfo.enabled = false; when(packageManagerMock.queryIntentServices(any(Intent.class), eq(0))) .thenReturn(Arrays.asList(resolveInfo)); when(mockContext.getPackageName()).thenReturn("package"); List<String> errors = validator.validateService("service"); assertTrue(errors.contains("service is disabled.")); }
@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 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 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_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 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); }
@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); }