Skip to content

Commit

Permalink
Fix RobolectricPackageManager.LaunchIntentForPackage() issue.
Browse files Browse the repository at this point in the history
- Fix the bug that RobolectricPackageManager.LaunchIntentForPackage() always returns non-null object, while actual PackageManager.LaunchIntentForPackage() returns null when the corresponding package is not installed.
- Add test case for RobolectricPackageManager.getLaunchIntentForPackage().
  • Loading branch information
thorikawa committed Jul 29, 2013
1 parent 83dc929 commit 5e08274
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,24 @@ public void addActivityIcon(Intent intent, Drawable d) {

@Override
public Intent getLaunchIntentForPackage(String packageName) {
Intent i = new Intent();
i.setComponent( new ComponentName(packageName, "") );
return i;
Intent intentToResolve = new Intent(Intent.ACTION_MAIN);
intentToResolve.addCategory(Intent.CATEGORY_INFO);
intentToResolve.setPackage(packageName);
List<ResolveInfo> ris = queryIntentActivities(intentToResolve, 0);

if (ris == null || ris.size() <= 0) {
intentToResolve.removeCategory(Intent.CATEGORY_INFO);
intentToResolve.addCategory(Intent.CATEGORY_LAUNCHER);
intentToResolve.setPackage(packageName);
ris = queryIntentActivities(intentToResolve, 0);
}
if (ris == null || ris.size() <= 0) {
return null;
}
Intent intent = new Intent(intentToResolve);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setClassName(ris.get(0).activityInfo.packageName, ris.get(0).activityInfo.name);
return intent;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,8 @@ public void resolveService__NoMatch() throws Exception {

@Test
public void queryActivityIcons__Match() throws Exception {
Intent i = rpm.getLaunchIntentForPackage(TEST_PACKAGE_NAME);
Intent i = new Intent();
i.setComponent(new ComponentName(TEST_PACKAGE_NAME, ""));
Drawable d = new BitmapDrawable();

rpm.addActivityIcon(i, d);
Expand Down Expand Up @@ -249,6 +250,24 @@ public void shouldAssignTheApplicationNameFromTheManifest() throws Exception {
assertThat(applicationInfo.name).isEqualTo("org.robolectric.TestApplication");
}

@Test
public void testLaunchIntentForPackage() {
Intent intent = rpm.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";
Robolectric.packageManager.addResolveInfoForIntent(launchIntent, resolveInfo);

intent = rpm.getLaunchIntentForPackage(TEST_PACKAGE_LABEL);
assertThat(intent).isNotNull();
}

/////////////////////////////

public AndroidManifest newConfigWith(String contents) throws IOException {
Expand Down

0 comments on commit 5e08274

Please sign in to comment.