Merge remote-tracking branch 'upstream/master' into robo-staging
diff --git a/src/main/java/com/xtremelabs/robolectric/bytecode/AndroidTranslator.java b/src/main/java/com/xtremelabs/robolectric/bytecode/AndroidTranslator.java
index c0cf7e7..e55f76f 100644
--- a/src/main/java/com/xtremelabs/robolectric/bytecode/AndroidTranslator.java
+++ b/src/main/java/com/xtremelabs/robolectric/bytecode/AndroidTranslator.java
@@ -34,7 +34,7 @@
instrumentingList.add("org.apache.http.impl.client.DefaultRequestDirector");
instrumentingExcludeList.add("android.support.v4.app.NotificationCompat");
- instrumentingExcludeList.add("android.support.v4.content.");
+ instrumentingExcludeList.add("android.support.v4.content.LocalBroadcastManager");
instrumentingExcludeList.add("android.support.v4.util.LruCache");
}
diff --git a/src/main/java/com/xtremelabs/robolectric/res/RobolectricPackageManager.java b/src/main/java/com/xtremelabs/robolectric/res/RobolectricPackageManager.java
index a4e3bbf..b15e89d 100644
--- a/src/main/java/com/xtremelabs/robolectric/res/RobolectricPackageManager.java
+++ b/src/main/java/com/xtremelabs/robolectric/res/RobolectricPackageManager.java
@@ -17,7 +17,7 @@
import com.xtremelabs.robolectric.tester.android.content.pm.StubPackageManager;
public class RobolectricPackageManager extends StubPackageManager {
-
+
private Map<String, PackageInfo> packageList;
private Map<Intent, List<ResolveInfo>> resolveList = new HashMap<Intent, List<ResolveInfo>>();
private Map<ComponentName, ComponentState> componentList = new HashMap<ComponentName,ComponentState>();
@@ -39,7 +39,7 @@
if (packageList.containsKey(packageName)) {
return packageList.get(packageName);
}
-
+
throw new NameNotFoundException();
}
@@ -57,7 +57,7 @@
}
return applicationInfo;
}
-
+
PackageInfo info;
if ((info = packageList.get(packageName)) != null) {
return info.applicationInfo;
@@ -71,115 +71,129 @@
return new ArrayList<PackageInfo>(packageList.values());
}
- @Override
+ @Override
public List<ResolveInfo> queryIntentActivities( Intent intent, int flags ) {
List<ResolveInfo> result = resolveList.get( intent );
return (result == null) ? new ArrayList<ResolveInfo>() : result;
}
-
+
@Override
public ResolveInfo resolveActivity(Intent intent, int flags) {
List<ResolveInfo> candidates = queryIntentActivities(intent, flags);
return candidates.isEmpty() ? null : candidates.get(0);
}
-
- public void addResolveInfoForIntent( Intent intent, List<ResolveInfo> info ) {
- resolveList.put( intent, info );
+
+ @Override
+ public ResolveInfo resolveService(Intent intent, int flags) {
+ return resolveActivity(intent, flags);
}
-
+
+ public void addResolveInfoForIntent( Intent intent, List<ResolveInfo> info ) {
+ resolveList.put(intent, info);
+ }
+
+ public void addResolveInfoForIntent(Intent intent, ResolveInfo info) {
+ List<ResolveInfo> l = resolveList.get(intent);
+ if (l == null) {
+ l = new ArrayList<ResolveInfo>();
+ resolveList.put(intent, l);
+ }
+ l.add(info);
+ }
+
@Override
public Drawable getActivityIcon(Intent intent) {
return drawableList.get(intent.getComponent());
}
-
+
@Override
public Drawable getActivityIcon(ComponentName componentName) {
return drawableList.get(componentName);
}
-
+
public void addActivityIcon( ComponentName component, Drawable d ) {
drawableList.put( component, d);
}
-
+
public void addActivityIcon( Intent intent, Drawable d ) {
drawableList.put( intent.getComponent(), d);
}
-
+
@Override
public Intent getLaunchIntentForPackage(String packageName) {
Intent i = new Intent();
i.setComponent( new ComponentName(packageName, "") );
return i;
}
-
+
@Override
public CharSequence getApplicationLabel(ApplicationInfo info) {
return info.name;
}
-
+
@Override
public void setComponentEnabledSetting(ComponentName componentName, int newState, int flags) {
componentList.put(componentName, new ComponentState(newState, flags));
}
-
+
/**
* Non-Android accessor. Use to make assertions on values passed to
* setComponentEnabledSetting.
- *
+ *
* @param componentName
* @return
*/
public RobolectricPackageManager.ComponentState getComponentState(ComponentName componentName) {
return componentList.get(componentName);
}
-
+
/**
* Non-Android accessor. Used to add a package to the list of those
* already 'installed' on system.
- *
+ *
* @param packageInfo
*/
public void addPackage( PackageInfo packageInfo ) {
packageList.put(packageInfo.packageName, packageInfo);
}
-
+
public void addPackage( String packageName ) {
PackageInfo info = new PackageInfo();
info.packageName = packageName;
addPackage( info );
- }
-
+ }
+
@Override
public boolean hasSystemFeature(String name) {
return systemFeatureList.containsKey(name) ? systemFeatureList.get(name) : false;
}
-
+
/**
* Non-Android accessor. Used to declare a system feature is
* or is not supported.
- *
+ *
* @param name
* @param supported
*/
public void setSystemFeature(String name, boolean supported) {
systemFeatureList.put(name, supported);
}
-
+
private void initializePackageInfo() {
if (packageList != null) { return; }
PackageInfo packageInfo = new PackageInfo();
packageInfo.packageName = contextWrapper.getPackageName();
packageInfo.versionName = "1.0";
-
+
packageList = new HashMap<String, PackageInfo>();
addPackage( packageInfo );
}
-
+
public class ComponentState {
public int newState;
public int flags;
-
+
public ComponentState(int newState, int flags) {
this.newState = newState;
this.flags = flags;
diff --git a/src/test/java/com/xtremelabs/robolectric/res/RobolectricPackageManagerTest.java b/src/test/java/com/xtremelabs/robolectric/res/RobolectricPackageManagerTest.java
index 7c8f009..ed4d873 100644
--- a/src/test/java/com/xtremelabs/robolectric/res/RobolectricPackageManagerTest.java
+++ b/src/test/java/com/xtremelabs/robolectric/res/RobolectricPackageManagerTest.java
@@ -1,16 +1,15 @@
package com.xtremelabs.robolectric.res;
-import java.util.ArrayList;
-import java.util.List;
+import static org.hamcrest.CoreMatchers.*;
+import static org.junit.Assert.assertThat;
+import com.xtremelabs.robolectric.Robolectric;
+import com.xtremelabs.robolectric.WithTestDefaultsRunner;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
-import com.xtremelabs.robolectric.Robolectric;
-import com.xtremelabs.robolectric.WithTestDefaultsRunner;
-
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
@@ -20,121 +19,132 @@
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
-import static org.hamcrest.CoreMatchers.*;
-import static org.junit.Assert.assertThat;
+import java.util.List;
@RunWith(WithTestDefaultsRunner.class)
public class RobolectricPackageManagerTest {
-
- private static final String TEST_PACKAGE_NAME = "com.some.other.package";
- private static final String TEST_PACKAGE_LABEL = "My Little App";
-
- RobolectricPackageManager rpm;
- @Before
- public void setUp() throws Exception {
- rpm = (RobolectricPackageManager) Robolectric.application.getPackageManager();
- }
+ private static final String TEST_PACKAGE_NAME = "com.some.other.package";
+ private static final String TEST_PACKAGE_LABEL = "My Little App";
- @After
- public void tearDown() throws Exception {
- }
+ RobolectricPackageManager rpm;
- @Test
- public void getApplicationInfo__ThisApplication() throws Exception {
- ApplicationInfo info = rpm.getApplicationInfo(Robolectric.application.getPackageName(), 0);
- assertThat( info, notNullValue() );
- assertThat( info.packageName, equalTo(Robolectric.application.getPackageName()));
- }
-
- @Test
- public void getApplicationInfo__OtherApplication() throws Exception {
- PackageInfo packageInfo = new PackageInfo();
- packageInfo.packageName = TEST_PACKAGE_NAME;
- packageInfo.applicationInfo = new ApplicationInfo();
- packageInfo.applicationInfo.packageName = TEST_PACKAGE_NAME;
- packageInfo.applicationInfo.name = TEST_PACKAGE_LABEL;
- rpm.addPackage( packageInfo );
-
- ApplicationInfo info = rpm.getApplicationInfo(TEST_PACKAGE_NAME, 0);
- assertThat(info, notNullValue() );
- assertThat(info.packageName, equalTo(TEST_PACKAGE_NAME));
- assertThat(rpm.getApplicationLabel(info).toString(), equalTo(TEST_PACKAGE_LABEL));
- }
-
- @Test
- public void queryIntentActivities__EmptyResult() throws Exception {
- Intent i = new Intent(Intent.ACTION_MAIN, null);
- i.addCategory(Intent.CATEGORY_LAUNCHER);
-
- List<ResolveInfo> activities = rpm.queryIntentActivities( i, 0 );
- assertThat(activities, notNullValue()); // empty list, not null
- assertThat(activities.size(), equalTo(0));
- }
-
- @Test
- public void queryIntentActivities__Match() throws Exception {
- Intent i = new Intent(Intent.ACTION_MAIN, null);
- i.addCategory(Intent.CATEGORY_LAUNCHER);
-
- List<ResolveInfo> resolved = new ArrayList<ResolveInfo>();
- ResolveInfo info = new ResolveInfo();
- info.nonLocalizedLabel = TEST_PACKAGE_LABEL;
- resolved.add(info);
-
- rpm.addResolveInfoForIntent(i, resolved);
-
- List<ResolveInfo> activities = rpm.queryIntentActivities( i, 0 );
- assertThat(activities, notNullValue());
- assertThat(activities.size(), equalTo(1));
- assertThat(activities.get(0).nonLocalizedLabel.toString(), equalTo(TEST_PACKAGE_LABEL));
- }
-
- @Test
- public void resolveActivity__Match() throws Exception {
- Intent i = new Intent(Intent.ACTION_MAIN, null);
- i.addCategory(Intent.CATEGORY_LAUNCHER);
-
- List<ResolveInfo> resolved = new ArrayList<ResolveInfo>();
- ResolveInfo info = new ResolveInfo();
- info.nonLocalizedLabel = TEST_PACKAGE_LABEL;
- resolved.add(info);
-
- rpm.addResolveInfoForIntent(i, resolved);
-
- assertThat( rpm.resolveActivity(i, 0), sameInstance(info) );
- }
-
- @Test
- public void resolveActivity__NoMatch() throws Exception {
- Intent i = new Intent();
- i.setComponent( new ComponentName("foo.bar", "No Activity") );
- assertThat( rpm.resolveActivity(i, 0), nullValue() );
- }
-
- @Test
- public void queryActivityIcons__Match() throws Exception {
- Intent i = rpm.getLaunchIntentForPackage( TEST_PACKAGE_NAME );
- Drawable d = new BitmapDrawable();
-
- rpm.addActivityIcon(i, d);
-
- assertThat( rpm.getActivityIcon( i ), sameInstance(d) );
- assertThat( rpm.getActivityIcon( i.getComponent() ), sameInstance(d) );
- }
-
- @Test
- public void hasSystemFeature() throws Exception {
- // uninitialized
- assertThat(rpm.hasSystemFeature( PackageManager.FEATURE_CAMERA), equalTo(false) );
-
- // positive
- rpm.setSystemFeature(PackageManager.FEATURE_CAMERA, true);
- assertThat(rpm.hasSystemFeature( PackageManager.FEATURE_CAMERA), equalTo(true) );
+ @Before
+ public void setUp() throws Exception {
+ rpm = (RobolectricPackageManager) Robolectric.application.getPackageManager();
+ }
- // negative
- rpm.setSystemFeature(PackageManager.FEATURE_CAMERA, false);
- assertThat(rpm.hasSystemFeature( PackageManager.FEATURE_CAMERA), equalTo(false) );
- }
+ @After
+ public void tearDown() throws Exception {
+ }
+
+ @Test
+ public void getApplicationInfo__ThisApplication() throws Exception {
+ ApplicationInfo info = rpm.getApplicationInfo(Robolectric.application.getPackageName(), 0);
+ assertThat(info, notNullValue());
+ assertThat(info.packageName, equalTo(Robolectric.application.getPackageName()));
+ }
+
+ @Test
+ public void getApplicationInfo__OtherApplication() throws Exception {
+ PackageInfo packageInfo = new PackageInfo();
+ packageInfo.packageName = TEST_PACKAGE_NAME;
+ packageInfo.applicationInfo = new ApplicationInfo();
+ packageInfo.applicationInfo.packageName = TEST_PACKAGE_NAME;
+ packageInfo.applicationInfo.name = TEST_PACKAGE_LABEL;
+ rpm.addPackage(packageInfo);
+
+ ApplicationInfo info = rpm.getApplicationInfo(TEST_PACKAGE_NAME, 0);
+ assertThat(info, notNullValue());
+ assertThat(info.packageName, equalTo(TEST_PACKAGE_NAME));
+ assertThat(rpm.getApplicationLabel(info).toString(), equalTo(TEST_PACKAGE_LABEL));
+ }
+
+ @Test
+ public void queryIntentActivities__EmptyResult() throws Exception {
+ Intent i = new Intent(Intent.ACTION_MAIN, null);
+ i.addCategory(Intent.CATEGORY_LAUNCHER);
+
+ List<ResolveInfo> activities = rpm.queryIntentActivities(i, 0);
+ assertThat(activities, notNullValue()); // empty list, not null
+ assertThat(activities.size(), equalTo(0));
+ }
+
+ @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;
+
+ rpm.addResolveInfoForIntent(i, info);
+
+ List<ResolveInfo> activities = rpm.queryIntentActivities(i, 0);
+ assertThat(activities, notNullValue());
+ assertThat(activities.size(), equalTo(1));
+ assertThat(activities.get(0).nonLocalizedLabel.toString(), equalTo(TEST_PACKAGE_LABEL));
+ }
+
+ @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;
+ rpm.addResolveInfoForIntent(i, info);
+
+ assertThat(rpm.resolveActivity(i, 0), sameInstance(info));
+ }
+
+ @Test
+ public void resolveActivity__NoMatch() throws Exception {
+ Intent i = new Intent();
+ i.setComponent(new ComponentName("foo.bar", "No Activity"));
+ assertThat(rpm.resolveActivity(i, 0), nullValue());
+ }
+
+ @Test
+ public void resolveService__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;
+ rpm.addResolveInfoForIntent(i, info);
+
+ assertThat(rpm.resolveService(i, 0), sameInstance(info));
+ }
+
+ @Test
+ public void resolveService__NoMatch() throws Exception {
+ Intent i = new Intent();
+ i.setComponent(new ComponentName("foo.bar", "No Activity"));
+ assertThat(rpm.resolveService(i, 0), nullValue());
+ }
+
+ @Test
+ public void queryActivityIcons__Match() throws Exception {
+ Intent i = rpm.getLaunchIntentForPackage(TEST_PACKAGE_NAME);
+ Drawable d = new BitmapDrawable();
+
+ rpm.addActivityIcon(i, d);
+
+ assertThat(rpm.getActivityIcon(i), sameInstance(d));
+ assertThat(rpm.getActivityIcon(i.getComponent()), sameInstance(d));
+ }
+
+ @Test
+ public void hasSystemFeature() throws Exception {
+ // uninitialized
+ assertThat(rpm.hasSystemFeature(PackageManager.FEATURE_CAMERA), equalTo(false));
+
+ // positive
+ rpm.setSystemFeature(PackageManager.FEATURE_CAMERA, true);
+ assertThat(rpm.hasSystemFeature(PackageManager.FEATURE_CAMERA), equalTo(true));
+
+ // negative
+ rpm.setSystemFeature(PackageManager.FEATURE_CAMERA, false);
+ assertThat(rpm.hasSystemFeature(PackageManager.FEATURE_CAMERA), equalTo(false));
+ }
}