Permalink
Browse files

Allow for setting a custom PackageManager on an Activity

[Fixes #351]
  • Loading branch information...
1 parent b733cc4 commit b9b1e6f510c355407892cf89cc70758711467c31 @herval committed Nov 1, 2012
View
2 src/main/java/com/xtremelabs/robolectric/shadows/ShadowContextWrapper.java
@@ -122,7 +122,7 @@ public void setApplicationName(String name) {
*/
@Implementation
public PackageManager getPackageManager() {
- return realContextWrapper == getApplicationContext() ? packageManager : getApplicationContext().getPackageManager();
+ return packageManager == null ? getApplicationContext().getPackageManager() : packageManager;
}
@Implementation
View
14 src/test/java/com/xtremelabs/robolectric/shadows/ContextWrapperTest.java
@@ -7,8 +7,11 @@
import android.content.ContextWrapper;
import android.content.Intent;
import android.content.IntentFilter;
+import android.content.pm.PackageManager;
+
import com.xtremelabs.robolectric.Robolectric;
import com.xtremelabs.robolectric.WithTestDefaultsRunner;
+import com.xtremelabs.robolectric.tester.android.content.pm.StubPackageManager;
import com.xtremelabs.robolectric.util.Transcript;
import org.junit.Before;
import org.junit.Test;
@@ -108,7 +111,6 @@ public void broadcasts_shouldBeLogged() {
assertEquals(broadcastIntent, broadcastIntents.get(0));
}
-
@Test
public void shouldReturnSameApplicationEveryTime() throws Exception {
Activity activity = new Activity();
@@ -125,6 +127,16 @@ public void shouldReturnSameApplicationContextEveryTime() throws Exception {
assertThat(activity.getApplicationContext(), sameInstance(new Activity().getApplicationContext()));
}
+ @Test
+ public void shouldReturnOverridenApplicationContext() throws Exception {
+ Activity activity = new Activity();
+ ShadowActivity shadow = Robolectric.shadowOf(activity);
+ PackageManager stubManager = new StubPackageManager();
+
+ shadow.setPackageManager(stubManager);
+ assertThat(stubManager, sameInstance(shadow.getPackageManager()));
+ }
+
@Test
public void shouldReturnSameContentResolverEveryTime() throws Exception {
Activity activity = new Activity();

0 comments on commit b9b1e6f

Please sign in to comment.