setPackageManager doesn't work for an activity. #351

Closed
nstoertz opened this Issue Oct 25, 2012 · 3 comments

Comments

Projects
None yet
4 participants

Code like this fails:
Robolectric.shadowOf(activity).setPackageManager(mockManager);
assertEquals(mockManager, activity.getPackageManager())

It looks like this is because ShadowContextWrapper#getPackageManager doesn't return the overridden package manager unless the context is the application context.

@Implementation
public PackageManager getPackageManager() {
    return realContextWrapper == getApplicationContext() ? packageManager getApplicationContext().getPackageManager();
}

d5a92b7

A workaround is to set the package manager on the application, but it seems like getPackageManager should always return the packageManager if it has been set. If that isn't possible, maybe ShadowActivity should override setPackageManager and warn that setPackageManager should be called on the application.

advait commented Oct 25, 2012

+1

herval commented Nov 1, 2012

I've fixed this here: herval/robolectric@b9b1e6f
making a pull request...

Owner

xian commented May 13, 2013

Things have changed pretty significantly with 2.0, this may no longer be relevant. Please reopen if it is, though. Thanks!

@xian xian closed this May 13, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment