Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge branch 'master' of github.com:pivotal/robolectric

  • Loading branch information...
commit 0a7ac38b1e4db868e90fec572d19dd1244d7b3db 2 parents 8f06fa1 + 2f4cde0
Ryan Richard and Sarah Chandler authored
Showing with 499 additions and 60 deletions.
  1. +12 −10 build.xml
  2. +16 −2 src/main/java/com/xtremelabs/robolectric/Robolectric.java
  3. +3 −2 src/main/java/com/xtremelabs/robolectric/RobolectricConfig.java
  4. +88 −3 src/main/java/com/xtremelabs/robolectric/RobolectricTestRunner.java
  5. +15 −0 src/main/java/com/xtremelabs/robolectric/annotation/WithConstantInt.java
  6. +15 −0 src/main/java/com/xtremelabs/robolectric/annotation/WithConstantString.java
  7. +49 −5 src/main/java/com/xtremelabs/robolectric/res/RobolectricPackageManager.java
  8. +20 −3 src/main/java/com/xtremelabs/robolectric/shadows/ShadowBitmapFactory.java
  9. +9 −1 src/main/java/com/xtremelabs/robolectric/shadows/ShadowBluetoothAdapter.java
  10. +65 −5 src/main/java/com/xtremelabs/robolectric/shadows/ShadowIntentFilter.java
  11. +14 −0 src/main/java/com/xtremelabs/robolectric/shadows/ShadowParcel.java
  12. +1 −2  src/main/java/com/xtremelabs/robolectric/{ → shadows}/ShadowScrollView.java
  13. +27 −0 src/test/java/com/xtremelabs/robolectric/annotation/WithConstantIntTest.java
  14. +28 −0 src/test/java/com/xtremelabs/robolectric/annotation/WithConstantStringTest.java
  15. +1 −1  src/test/java/com/xtremelabs/robolectric/res/DrawableResourceLoaderTest.java
  16. +10 −11 src/test/java/com/xtremelabs/robolectric/res/MenuResourceLoaderTest.java
  17. +7 −6 src/test/java/com/xtremelabs/robolectric/res/ResourceLoaderTest.java
  18. +42 −5 src/test/java/com/xtremelabs/robolectric/res/RobolectricPackageManagerTest.java
  19. +40 −1 src/test/java/com/xtremelabs/robolectric/shadows/BitmapFactoryTest.java
  20. +19 −0 src/test/java/com/xtremelabs/robolectric/shadows/BluetoothAdapterTest.java
  21. +13 −0 src/test/java/com/xtremelabs/robolectric/shadows/ParcelTest.java
  22. +2 −1  src/test/java/com/xtremelabs/robolectric/shadows/ViewGroupTest.java
  23. +2 −1  src/test/java/com/xtremelabs/robolectric/util/TestUtil.java
  24. +1 −1  src/test/resources/TestAndroidManifest.xml
View
22 build.xml
@@ -21,8 +21,10 @@
should be checked in in Version Control Systems.
-->
- <property file="build.properties"/>
+ <!-- this should match whatever is defined in Robolectic.DEFAULT_SDK_VERSION -->
+ <property name="default.sdk.version" value="16"/>
+ <property file="build.properties"/>
<!-- The default.properties file is created and updated by the 'android' tool, as well
as ADT.
This file is an integral part of the build system for your application and
@@ -52,10 +54,10 @@
</path>
<path id="android.target.classpath">
- <pathelement path="${sdk.dir}/platforms/android-16/android.jar"/>
- <pathelement path="${sdk.dir}/add-ons/addon_google_apis_google_inc_16/libs/maps.jar"/>
- <pathelement path="${sdk.dir}/add-ons/addon-google_apis-google_inc_-16/libs/maps.jar"/>
- <pathelement path="${sdk.dir}/add-ons/addon-google_apis-google-16/libs/maps.jar"/>
+ <pathelement path="${sdk.dir}/platforms/android-${default.sdk.version}/android.jar"/>
+ <pathelement path="${sdk.dir}/add-ons/addon_google_apis_google_inc_${default.sdk.version}/libs/maps.jar"/>
+ <pathelement path="${sdk.dir}/add-ons/addon-google_apis-google_inc_-${default.sdk.version}/libs/maps.jar"/>
+ <pathelement path="${sdk.dir}/add-ons/addon-google_apis-google-${default.sdk.version}/libs/maps.jar"/>
<pathelement path="${sdk.dir}/extras/android/support/v4/android-support-v4.jar"/>
<pathelement path="${sdk.dir}/android-compatibility/v4/android-support-v4.jar"/>
</path>
@@ -217,13 +219,13 @@
<target name="maven-install-jars" description="Install the jar files that Maven can't find for itself">
<exec executable="mvn">
- <arg line="install:install-file -DgroupId=com.google.android.maps -DartifactId=maps -Dversion=10_r2 -Dpackaging=jar -Dfile=${sdk.dir}/add-ons/addon_google_apis_google_inc_10/libs/maps.jar" />
+ <arg line="install:install-file -DgroupId=com.google.android.maps -DartifactId=maps -Dversion=10_r2 -Dpackaging=jar -Dfile=${sdk.dir}/add-ons/addon_google_apis_google_inc_${default.sdk.version}/libs/maps.jar" />
</exec>
<exec executable="mvn">
- <arg line="install:install-file -DgroupId=com.google.android.maps -DartifactId=maps -Dversion=10_r2 -Dpackaging=jar -Dfile=${sdk.dir}/add-ons/addon-google_apis-google_inc_-10/libs/maps.jar" />
+ <arg line="install:install-file -DgroupId=com.google.android.maps -DartifactId=maps -Dversion=10_r2 -Dpackaging=jar -Dfile=${sdk.dir}/add-ons/addon-google_apis-google_inc_-${default.sdk.version}/libs/maps.jar" />
</exec>
<exec executable="mvn">
- <arg line="install:install-file -DgroupId=com.google.android.maps -DartifactId=maps -Dversion=10_r2 -Dpackaging=jar -Dfile=${sdk.dir}/add-ons/addon-google_apis-google-10/libs/maps.jar" />
+ <arg line="install:install-file -DgroupId=com.google.android.maps -DartifactId=maps -Dversion=10_r2 -Dpackaging=jar -Dfile=${sdk.dir}/add-ons/addon-google_apis-google-${default.sdk.version}/libs/maps.jar" />
</exec>
</target>
@@ -236,7 +238,7 @@
ANDROID="tmp/sdk/android-sdk-linux/tools/android"
chmod +x $ANDROID
SDKS=$($ANDROID list sdk --all | awk \
- '/SDK Platform.* API 10/ || /Google APIs, Android API 10, revision 2/ \
+ '/SDK Platform.* API ${default.sdk.version}/ || /Google APIs, Android API ${default.sdk.version}, revision 2/ \
{ s=s "," int($1) } \
END { print substr(s, 2, length(s)) }' \
)
@@ -248,7 +250,7 @@
<entry key="sdk.dir" value="${basedir}/tmp/sdk/android-sdk-linux/"/>
</propertyfile>
<exec executable="mvn">
- <arg line="install:install-file -DgroupId=com.google.android.maps -DartifactId=maps -Dversion=10_r2 -Dpackaging=jar -Dfile=tmp/sdk/android-sdk-linux/add-ons/addon-google_apis-google-10/libs/maps.jar"/>
+ <arg line="install:install-file -DgroupId=com.google.android.maps -DartifactId=maps -Dversion=10_r2 -Dpackaging=jar -Dfile=tmp/sdk/android-sdk-linux/add-ons/addon-google_apis-google-${default.sdk.version}/libs/maps.jar"/>
</exec>
</target>
</project>
View
18 src/main/java/com/xtremelabs/robolectric/Robolectric.java
@@ -74,6 +74,7 @@
public class Robolectric {
public static Application application;
+ public static final int DEFAULT_SDK_VERSION = 16;
public static <T> T newInstanceOf(Class<T> clazz) {
return RobolectricInternals.newInstanceOf(clazz);
@@ -1276,18 +1277,31 @@ public static Object newInstanceOf(String className) {
public static void setFinalStaticField(Class classWhichContainsField, String fieldName, Object newValue) {
try {
Field field = classWhichContainsField.getDeclaredField(fieldName);
- field.setAccessible(true);
+ setFinalStaticField(field, newValue);
+ } catch (NoSuchFieldException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ public static Object setFinalStaticField(Field field, Object newValue) {
+ Object oldValue = null;
+
+ try {
+ field.setAccessible(true);
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
- field.set(null, newValue);
+ oldValue = field.get(null);
+ field.set(null, newValue);
} catch (NoSuchFieldException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
+
+ return oldValue;
}
}
View
5 src/main/java/com/xtremelabs/robolectric/RobolectricConfig.java
@@ -14,6 +14,7 @@
import java.util.List;
import static android.content.pm.ApplicationInfo.*;
+import static com.xtremelabs.robolectric.Robolectric.DEFAULT_SDK_VERSION;
public class RobolectricConfig {
private final File androidManifestFile;
@@ -92,13 +93,13 @@ private void parseAndroidManifest() {
Integer minSdkVer = getTagAttributeIntValue(manifestDocument, "uses-sdk", "android:minSdkVersion");
Integer sdkVer = getTagAttributeIntValue(manifestDocument, "uses-sdk", "android:targetSdkVersion");
if (minSdkVer == null) {
- minSdkVersion = 10;
+ minSdkVersion = DEFAULT_SDK_VERSION;
minSdkVersionSpecified = false;
} else {
minSdkVersion = minSdkVer;
}
if (sdkVer == null) {
- sdkVersion = 10;
+ sdkVersion = DEFAULT_SDK_VERSION;
sdkVersionSpecified = false;
} else {
sdkVersion = sdkVer;
View
91 src/main/java/com/xtremelabs/robolectric/RobolectricTestRunner.java
@@ -6,7 +6,9 @@
import java.io.PrintStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
+import java.lang.reflect.Field;
import java.lang.reflect.Method;
+import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
@@ -26,6 +28,7 @@
import android.app.Application;
import android.net.Uri__FromAndroid;
+import com.xtremelabs.robolectric.annotation.WithConstantString;
import com.xtremelabs.robolectric.bytecode.ClassHandler;
import com.xtremelabs.robolectric.bytecode.RobolectricClassLoader;
import com.xtremelabs.robolectric.bytecode.ShadowWrangler;
@@ -283,9 +286,20 @@ protected void delegateLoadingOf(final String className) {
final Statement statement = super.methodBlock(method);
return new Statement() {
@Override public void evaluate() throws Throwable {
- // todo: this try/finally probably isn't right -- should mimic RunAfters? [xw]
+ HashMap<Field,Object> withConstantAnnos = getWithConstantAnnotations(method.getMethod());
+
+ // todo: this try/finally probably isn't right -- should mimic RunAfters? [xw]
try {
- statement.evaluate();
+ if (withConstantAnnos.isEmpty()) {
+ statement.evaluate();
+ }
+ else {
+ synchronized(this) {
+ setupConstants(withConstantAnnos);
+ statement.evaluate();
+ setupConstants(withConstantAnnos);
+ }
+ }
} finally {
delegate.internalAfterTest(method.getMethod());
if (classHandler != null) {
@@ -409,7 +423,7 @@ private void setupI18nStrictState(Method method, RobolectricConfig robolectricCo
robolectricConfig.setStrictI18n(strictI18n);
}
-
+
/**
* Default implementation of global switch for i18n-strict mode.
* To enable i18n-strict mode globally, set the system property
@@ -469,6 +483,77 @@ private void lookForLocaleAnnotation( Method method, RobolectricConfig robolectr
robolectricConfig.setLocale( locale );
}
+
+ /**
+ * Find all the class and method annotations and pass them to
+ * addConstantFromAnnotation() for evaluation.
+ *
+ * TODO: Add compound annotations to suport defining more than one int and string at a time
+ * TODO: See http://stackoverflow.com/questions/1554112/multiple-annotations-of-the-same-type-on-one-element
+ *
+ * @param method
+ * @return
+ */
+ private HashMap<Field,Object> getWithConstantAnnotations(Method method) {
+ HashMap<Field,Object> constants = new HashMap<Field,Object>();
+
+ for(Annotation anno:method.getDeclaringClass().getAnnotations()) {
+ addConstantFromAnnotation(constants, anno);
+ }
+
+ for(Annotation anno:method.getAnnotations()) {
+ addConstantFromAnnotation(constants, anno);
+ }
+
+ return constants;
+ }
+
+ /**
+ * If the annotation is a constant redefinition, add it to the provided hash
+ *
+ * @param constants
+ * @param anno
+ */
+ private void addConstantFromAnnotation(HashMap<Field,Object> constants, Annotation anno) {
+ try {
+ String name = anno.annotationType().getName();
+ Object newValue = null;
+
+ if (name.equals( "com.xtremelabs.robolectric.annotation.WithConstantString" )) {
+ newValue = (String) anno.annotationType().getMethod("newValue").invoke(anno);
+ }
+ else if (name.equals( "com.xtremelabs.robolectric.annotation.WithConstantInt" )) {
+ newValue = (Integer) anno.annotationType().getMethod("newValue").invoke(anno);
+ }
+ else {
+ return;
+ }
+
+ @SuppressWarnings("rawtypes")
+ Class classWithField = (Class) anno.annotationType().getMethod("classWithField").invoke(anno);
+ String fieldName = (String) anno.annotationType().getMethod("fieldName").invoke(anno);
+ Field field = classWithField.getDeclaredField(fieldName);
+ constants.put(field, newValue);
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ /**
+ * Defines static finals from the provided hash and stores the old values back
+ * into the hash.
+ *
+ * Call it twice with the same hash, and it puts everything back the way it was originally.
+ *
+ * @param constants
+ */
+ private void setupConstants(HashMap<Field,Object> constants) {
+ for(Field field:constants.keySet()) {
+ Object newValue = constants.get(field);
+ Object oldValue = Robolectric.Reflection.setFinalStaticField(field, newValue);
+ constants.put(field,oldValue);
+ }
+ }
private void setupLogging() {
String logging = System.getProperty("robolectric.logging");
View
15 src/main/java/com/xtremelabs/robolectric/annotation/WithConstantInt.java
@@ -0,0 +1,15 @@
+package com.xtremelabs.robolectric.annotation;
+
+/**
+ * Annotation to run test with setFinalStaticField() defined in a synchronized
+ * block with automatic reversion to the original value.
+ */
+@java.lang.annotation.Documented
+@java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
+@java.lang.annotation.Target({java.lang.annotation.ElementType.TYPE,java.lang.annotation.ElementType.METHOD})
+public @interface WithConstantInt {
+ @SuppressWarnings("rawtypes")
+ Class classWithField();
+ String fieldName();
+ int newValue();
+}
View
15 src/main/java/com/xtremelabs/robolectric/annotation/WithConstantString.java
@@ -0,0 +1,15 @@
+package com.xtremelabs.robolectric.annotation;
+
+/**
+ * Annotation to run test with setFinalStaticField() defined in a synchronized
+ * block with automatic reversion to the original value.
+ */
+@java.lang.annotation.Documented
+@java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
+@java.lang.annotation.Target({java.lang.annotation.ElementType.TYPE,java.lang.annotation.ElementType.METHOD})
+public @interface WithConstantString {
+ @SuppressWarnings("rawtypes")
+ Class classWithField();
+ String fieldName();
+ String newValue();
+}
View
54 src/main/java/com/xtremelabs/robolectric/res/RobolectricPackageManager.java
@@ -1,13 +1,12 @@
package com.xtremelabs.robolectric.res;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
+import java.util.Map.Entry;
import android.content.ComponentName;
import android.content.ContextWrapper;
import android.content.Intent;
+import android.content.IntentFilter;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.ResolveInfo;
@@ -23,7 +22,8 @@
private Map<ComponentName, ComponentState> componentList = new HashMap<ComponentName,ComponentState>();
private Map<ComponentName, Drawable> drawableList = new HashMap<ComponentName, Drawable>();
private Map<String, Boolean> systemFeatureList = new HashMap<String, Boolean>();
-
+ private Map<IntentFilter, ComponentName > preferredActivities = new HashMap<IntentFilter, ComponentName>();
+
private ContextWrapper contextWrapper;
private RobolectricConfig config;
private ApplicationInfo applicationInfo;
@@ -135,6 +135,50 @@ public CharSequence getApplicationLabel(ApplicationInfo info) {
public void setComponentEnabledSetting(ComponentName componentName, int newState, int flags) {
componentList.put(componentName, new ComponentState(newState, flags));
}
+
+ public void addPreferredActivity(IntentFilter filter, int match, ComponentName[] set, ComponentName activity) {
+ preferredActivities.put(filter, activity);
+ }
+
+ @Override
+ public int getPreferredActivities(List<IntentFilter> outFilters, List<ComponentName> outActivities, String packageName) {
+ if( outFilters == null ){ return 0; }
+
+ Set< IntentFilter> filters = preferredActivities.keySet();
+ for( IntentFilter filter: outFilters ){
+ step:
+ for ( IntentFilter testFilter : filters ) {
+ ComponentName name = preferredActivities.get( testFilter );
+ // filter out based on the given packageName;
+ if( packageName != null && !name.getPackageName().equals( packageName ) ){
+ continue step;
+ }
+
+ // Check actions
+ Iterator< String > iterator = filter.actionsIterator();
+ while ( iterator.hasNext() ) {
+ if ( !testFilter.matchAction( iterator.next() ) ) {
+ continue step;
+ }
+ }
+
+ iterator = filter.categoriesIterator();
+ while ( iterator.hasNext() ) {
+ if ( !filter.hasCategory( iterator.next() ) ) {
+ continue step;
+ }
+ }
+
+ if( outActivities == null ){
+ outActivities = new ArrayList<ComponentName>();
+ }
+
+ outActivities.add( name );
+ }
+ }
+
+ return 0;
+ }
/**
* Non-Android accessor. Use to make assertions on values passed to
View
23 src/main/java/com/xtremelabs/robolectric/shadows/ShadowBitmapFactory.java
@@ -16,6 +16,8 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import java.util.zip.CRC32;
+import java.util.zip.Checksum;
import static com.xtremelabs.robolectric.Robolectric.shadowOf;
@@ -37,7 +39,7 @@ public static Bitmap decodeResource(Resources res, int id, BitmapFactory.Options
shadowOf(bitmap).setLoadedFromResourceId(id);
return bitmap;
}
-
+
private static String getResourceName(int id) {
return shadowOf(Robolectric.application).getResourceLoader().getNameForId(id);
}
@@ -61,7 +63,19 @@ public static Bitmap decodeStream(InputStream is) {
public static Bitmap decodeStream(InputStream is, Rect outPadding, BitmapFactory.Options opts) {
return create(is.toString().replaceFirst("stream for ", ""), opts);
}
+
+ @Implementation
+ public static Bitmap decodeByteArray(byte[] data, int offset, int length) {
+ return decodeByteArray( data, offset, length, new BitmapFactory.Options() );
+ }
+ @Implementation
+ public static Bitmap decodeByteArray(byte[] data, int offset, int length, BitmapFactory.Options opts) {
+ Checksum checksumEngine = new CRC32();
+ checksumEngine.update(data, 0, data.length);
+ return create("byte array, checksum:" + checksumEngine.getValue() + " offset: " + offset + " length: " + data.length, opts );
+ }
+
static Bitmap create(String name) {
return create(name, new BitmapFactory.Options());
}
@@ -84,8 +98,10 @@ public static Bitmap create(String name, BitmapFactory.Options options) {
shadowBitmap.setWidth(widthAndHeight.x);
shadowBitmap.setHeight(widthAndHeight.y);
- options.outWidth = widthAndHeight.x;
- options.outHeight = widthAndHeight.y;
+ if (options != null) {
+ options.outWidth = widthAndHeight.x;
+ options.outHeight = widthAndHeight.y;
+ }
return bitmap;
}
@@ -102,6 +118,7 @@ public static void provideWidthAndHeightHints(String file, int width, int height
}
private static String stringify(BitmapFactory.Options options) {
+ if (options == null) return "";
List<String> opts = new ArrayList<String>();
if (options.inJustDecodeBounds) opts.add("inJustDecodeBounds");
View
10 src/main/java/com/xtremelabs/robolectric/shadows/ShadowBluetoothAdapter.java
@@ -15,9 +15,9 @@
@SuppressWarnings({"UnusedDeclaration"})
@Implements(BluetoothAdapter.class)
public class ShadowBluetoothAdapter {
-
private Set<BluetoothDevice> bondedDevices = new HashSet<BluetoothDevice>();
private boolean isDiscovering;
+ private String address;
@Implementation
public static BluetoothAdapter getDefaultAdapter() {
@@ -50,4 +50,12 @@ public boolean isDiscovering() {
return isDiscovering;
}
+ @Implementation
+ public String getAddress() {
+ return this.address;
+ }
+
+ public void setAddress(String address) {
+ this.address = address;
+ }
}
View
70 src/main/java/com/xtremelabs/robolectric/shadows/ShadowIntentFilter.java
@@ -1,12 +1,16 @@
package com.xtremelabs.robolectric.shadows;
-import android.content.IntentFilter;
-import com.xtremelabs.robolectric.internal.Implementation;
-import com.xtremelabs.robolectric.internal.Implements;
+import static com.xtremelabs.robolectric.Robolectric.shadowOf_;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
+import java.util.Set;
+
+import android.content.IntentFilter;
+
+import com.xtremelabs.robolectric.internal.Implementation;
+import com.xtremelabs.robolectric.internal.Implements;
/**
* Shadow of {@code IntentFilter} implemented with a {@link java.util.List}
@@ -14,10 +18,12 @@
@SuppressWarnings({"UnusedDeclaration"})
@Implements(IntentFilter.class)
public class ShadowIntentFilter {
- List<String> actions = new ArrayList<String>();
+
+ List<String> actions = new ArrayList<String>();
List<String> schemes = new ArrayList<String>();
List<IntentFilter.AuthorityEntry> authoritites = new ArrayList<IntentFilter.AuthorityEntry>();
-
+ List<String> categories = new ArrayList<String>();
+
public void __constructor__(String action) {
actions.add(action);
}
@@ -66,4 +72,58 @@ public void addDataScheme(String scheme) {
public String getDataScheme(int index) {
return schemes.get(index);
}
+
+ @Implementation
+ public void addCategory( String category ) {
+ categories.add( category );
+ }
+
+ @Implementation
+ public boolean hasCategory( String category ) {
+ return categories.contains( category );
+ }
+
+ @Implementation
+ public Iterator<String> categoriesIterator() {
+ return categories.iterator();
+ }
+
+ @Implementation
+ public String getCategory( int index ) {
+ return categories.get( index );
+ }
+
+ @Implementation
+ public boolean matchCategories(Set<String> categories){
+ for( String category: this.categories ){
+ if( !categories.contains( category ) ){
+ return false;
+ }
+ }
+ return true;
+ }
+
+ @Override @Implementation
+ public boolean equals(Object o) {
+ if (o == null) return false;
+ o = shadowOf_(o);
+ if (o == null) return false;
+ if (this == o) return true;
+ if (getClass() != o.getClass()) return false;
+
+ ShadowIntentFilter that = (ShadowIntentFilter) o;
+
+ return actions.equals( that.actions ) && categories.equals( that.categories )
+ && schemes.equals( that.schemes ) && authoritites.equals( that.authoritites );
+ }
+
+ @Override @Implementation
+ public int hashCode() {
+ int result = 13;
+ result = 31 * result + actions.hashCode();
+ result = 31 * result + categories.hashCode();
+ result = 31 * result + schemes.hashCode();
+ result = 31 * result + authoritites.hashCode();
+ return result;
+ }
}
View
14 src/main/java/com/xtremelabs/robolectric/shadows/ShadowParcel.java
@@ -1,5 +1,6 @@
package com.xtremelabs.robolectric.shadows;
+import android.os.IBinder;
import android.os.Parcel;
import android.os.Parcelable;
import android.os.Bundle;
@@ -60,6 +61,14 @@ public void writeByte(byte b) {
}
@Implementation
+ public void writeStrongBinder(IBinder binder) {
+ if (binder == null) {
+ return;
+ }
+ parcelData.add(binder);
+ }
+
+ @Implementation
public String readString() {
return index < parcelData.size() ? (String) parcelData.get(index++) : null;
}
@@ -95,6 +104,11 @@ public Bundle readBundle() {
}
@Implementation
+ public IBinder readStrongBinder() {
+ return index < parcelData.size() ? (IBinder) parcelData.get(index++) : null;
+ }
+
+ @Implementation
public Bundle readBundle(ClassLoader loader) {
return readBundle();
}
View
3  .../com/xtremelabs/robolectric/ShadowScrollView.java → ...emelabs/robolectric/shadows/ShadowScrollView.java
@@ -1,9 +1,8 @@
-package com.xtremelabs.robolectric;
+package com.xtremelabs.robolectric.shadows;
import android.widget.ScrollView;
import com.xtremelabs.robolectric.internal.Implementation;
import com.xtremelabs.robolectric.internal.Implements;
-import com.xtremelabs.robolectric.shadows.ShadowFrameLayout;
@Implements(ScrollView.class)
public class ShadowScrollView extends ShadowFrameLayout {
View
27 src/test/java/com/xtremelabs/robolectric/annotation/WithConstantIntTest.java
@@ -0,0 +1,27 @@
+package com.xtremelabs.robolectric.annotation;
+
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.junit.Assert.*;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import com.xtremelabs.robolectric.WithTestDefaultsRunner;
+
+@RunWith(WithTestDefaultsRunner.class)
+public class WithConstantIntTest {
+
+ private static final int NEW_VALUE = 9;
+
+ @Test
+ @WithConstantInt(classWithField=android.os.Build.VERSION.class, fieldName="SDK_INT", newValue=NEW_VALUE )
+ public void testWithConstantInt() {
+ assertThat(android.os.Build.VERSION.SDK_INT, equalTo(NEW_VALUE));
+ }
+
+ @Test
+ public void testWithoutConstantInt() {
+ assertThat(android.os.Build.VERSION.SDK_INT, equalTo(0));
+ }
+
+}
View
28 src/test/java/com/xtremelabs/robolectric/annotation/WithConstantStringTest.java
@@ -0,0 +1,28 @@
+package com.xtremelabs.robolectric.annotation;
+
+import static org.junit.Assert.*;
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.hamcrest.CoreMatchers.nullValue;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import com.xtremelabs.robolectric.WithTestDefaultsRunner;
+
+@RunWith(WithTestDefaultsRunner.class)
+public class WithConstantStringTest {
+
+ private static final String NEW_VALUE = "HTC";
+
+ @Test
+ @WithConstantString(classWithField=android.os.Build.class, fieldName="MANUFACTURER", newValue=NEW_VALUE)
+ public void testWithConstantString() {
+ assertThat(android.os.Build.MANUFACTURER, equalTo(NEW_VALUE));
+ }
+
+ @Test
+ public void testWithoutConstantString() {
+ assertThat(android.os.Build.MANUFACTURER, nullValue());
+ }
+
+}
View
2  src/test/java/com/xtremelabs/robolectric/res/DrawableResourceLoaderTest.java
@@ -44,7 +44,7 @@ public void setup() throws Exception {
@Test
public void testProcessResourceXml() throws Exception {
assertTrue("drawable/rainbow", resourceLoader.documents.containsKey("drawable/rainbow"));
- assertEquals("documents.size", 116, resourceLoader.documents.size());
+ assertEquals("documents.size", 218, resourceLoader.documents.size());
}
@Test
View
21 src/test/java/com/xtremelabs/robolectric/res/MenuResourceLoaderTest.java
@@ -1,26 +1,25 @@
package com.xtremelabs.robolectric.res;
-import static com.xtremelabs.robolectric.util.TestUtil.resourceFile;
-import static org.hamcrest.CoreMatchers.equalTo;
-import static org.junit.Assert.*;
-
-import org.junit.Test;
-import org.junit.runner.RunWith;
-
import android.view.MenuItem;
-
import com.xtremelabs.robolectric.R;
import com.xtremelabs.robolectric.Robolectric;
import com.xtremelabs.robolectric.WithTestDefaultsRunner;
import com.xtremelabs.robolectric.tester.android.view.TestMenu;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import static com.xtremelabs.robolectric.Robolectric.DEFAULT_SDK_VERSION;
+import static com.xtremelabs.robolectric.util.TestUtil.resourceFile;
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
@RunWith(WithTestDefaultsRunner.class)
public class MenuResourceLoaderTest {
-
@Test
public void shouldInflateComplexMenu() throws Exception {
- ResourceLoader resourceLoader = new ResourceLoader(10, R.class, resourceFile("res"), resourceFile("menu"));
+ ResourceLoader resourceLoader = new ResourceLoader(DEFAULT_SDK_VERSION, R.class, resourceFile("res"), resourceFile("menu"));
TestMenu testMenu = new TestMenu();
resourceLoader.inflateMenu(Robolectric.application, R.menu.test_withchilds, testMenu);
assertThat(testMenu.size(), equalTo(4));
@@ -28,7 +27,7 @@ public void shouldInflateComplexMenu() throws Exception {
@Test
public void shouldParseSubItemCorrectly() throws Exception {
- ResourceLoader resourceLoader = new ResourceLoader(10, R.class, resourceFile("res"), resourceFile("menu"));
+ ResourceLoader resourceLoader = new ResourceLoader(DEFAULT_SDK_VERSION, R.class, resourceFile("res"), resourceFile("menu"));
TestMenu testMenu = new TestMenu();
resourceLoader.inflateMenu(Robolectric.application, R.menu.test_withchilds, testMenu);
MenuItem mi = testMenu.findItem(R.id.test_submenu_1);
View
13 src/test/java/com/xtremelabs/robolectric/res/ResourceLoaderTest.java
@@ -13,6 +13,7 @@
import java.io.File;
+import static com.xtremelabs.robolectric.Robolectric.DEFAULT_SDK_VERSION;
import static com.xtremelabs.robolectric.util.TestUtil.resourceFile;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.*;
@@ -26,7 +27,7 @@ public void shouldUseFileSystemSeparatorWhenEvaluatingLayoutDirectories() throws
@Test
public void shouldLoadSystemResources() throws Exception {
- ResourceLoader resourceLoader = new ResourceLoader(10, R.class, resourceFile("res"), resourceFile("assets"));
+ ResourceLoader resourceLoader = new ResourceLoader(DEFAULT_SDK_VERSION, R.class, resourceFile("res"), resourceFile("assets"));
String stringValue = resourceLoader.getStringValue(android.R.string.copy);
assertEquals("Copy", stringValue);
@@ -36,14 +37,14 @@ public void shouldLoadSystemResources() throws Exception {
@Test
public void shouldLoadLocalResources() throws Exception {
- ResourceLoader resourceLoader = new ResourceLoader(10, R.class, resourceFile("res"), resourceFile("assets"));
+ ResourceLoader resourceLoader = new ResourceLoader(DEFAULT_SDK_VERSION, R.class, resourceFile("res"), resourceFile("assets"));
String stringValue = resourceLoader.getStringValue(R.string.copy);
assertEquals("Local Copy", stringValue);
}
@Test(expected=I18nException.class)
public void shouldThrowExceptionOnI18nStrictModeInflateView() throws Exception {
- ResourceLoader resourceLoader = new ResourceLoader(10, R.class, resourceFile("res"), resourceFile("layout"));
+ ResourceLoader resourceLoader = new ResourceLoader(DEFAULT_SDK_VERSION, R.class, resourceFile("res"), resourceFile("layout"));
resourceLoader.setStrictI18n(true);
ViewGroup vg = new FrameLayout(Robolectric.application);
resourceLoader.inflateView(Robolectric.application, R.layout.text_views, vg);
@@ -51,21 +52,21 @@ public void shouldThrowExceptionOnI18nStrictModeInflateView() throws Exception {
@Test(expected=I18nException.class)
public void shouldThrowExceptionOnI18nStrictModeInflateMenu() throws Exception {
- ResourceLoader resourceLoader = new ResourceLoader(10, R.class, resourceFile("res"), resourceFile("menu"));
+ ResourceLoader resourceLoader = new ResourceLoader(DEFAULT_SDK_VERSION, R.class, resourceFile("res"), resourceFile("menu"));
resourceLoader.setStrictI18n(true);
resourceLoader.inflateMenu(Robolectric.application, R.menu.test, null);
}
@Test(expected=I18nException.class)
public void shouldThrowExceptionOnI18nStrictModeInflatePreferences() throws Exception {
- ResourceLoader resourceLoader = new ResourceLoader(10, R.class, resourceFile("res"), resourceFile("xml"));
+ ResourceLoader resourceLoader = new ResourceLoader(DEFAULT_SDK_VERSION, R.class, resourceFile("res"), resourceFile("xml"));
resourceLoader.setStrictI18n(true);
resourceLoader.inflatePreferences(Robolectric.application, R.xml.preferences);
}
@Test
public void testChoosesLayoutBasedOnSearchPath_respectsOrderOfPath() throws Exception {
- ResourceLoader resourceLoader = new ResourceLoader(10, R.class, resourceFile("res"), resourceFile("layout"));
+ ResourceLoader resourceLoader = new ResourceLoader(DEFAULT_SDK_VERSION, R.class, resourceFile("res"), resourceFile("layout"));
resourceLoader.setLayoutQualifierSearchPath("does-not-exist", "land", "xlarge");
ViewGroup viewGroup = new FrameLayout(Robolectric.application);
ViewGroup view = (ViewGroup) resourceLoader.inflateView(Robolectric.application, R.layout.different_screen_sizes, viewGroup);
View
47 src/test/java/com/xtremelabs/robolectric/res/RobolectricPackageManagerTest.java
@@ -1,10 +1,14 @@
package com.xtremelabs.robolectric.res;
-import static org.hamcrest.CoreMatchers.*;
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.hamcrest.CoreMatchers.notNullValue;
+import static org.hamcrest.CoreMatchers.nullValue;
+import static org.hamcrest.CoreMatchers.sameInstance;
import static org.junit.Assert.assertThat;
-import com.xtremelabs.robolectric.Robolectric;
-import com.xtremelabs.robolectric.WithTestDefaultsRunner;
+import java.util.ArrayList;
+import java.util.List;
+
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
@@ -12,6 +16,7 @@
import android.content.ComponentName;
import android.content.Intent;
+import android.content.IntentFilter;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
@@ -19,7 +24,8 @@
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
-import java.util.List;
+import com.xtremelabs.robolectric.Robolectric;
+import com.xtremelabs.robolectric.WithTestDefaultsRunner;
@RunWith(WithTestDefaultsRunner.class)
public class RobolectricPackageManagerTest {
@@ -146,5 +152,36 @@ public void hasSystemFeature() throws Exception {
rpm.setSystemFeature(PackageManager.FEATURE_CAMERA, false);
assertThat(rpm.hasSystemFeature(PackageManager.FEATURE_CAMERA), equalTo(false));
}
-
+
+ @Test
+ public void testGetPreferredActivities() throws Exception {
+ // Setup an intentfilter and add to packagemanager
+ IntentFilter filter = new IntentFilter(Intent.ACTION_MAIN);
+ filter.addCategory(Intent.CATEGORY_HOME);
+ final String packageName = "com.example.dummy";
+ ComponentName name = new ComponentName( packageName, "LauncherAcitivity" );
+ rpm.addPreferredActivity( filter, 0, null, name);
+
+ // Test match
+ List<IntentFilter> filters = new ArrayList<IntentFilter>();
+ filters.add( filter );
+
+ List<ComponentName> activities = new ArrayList<ComponentName>();
+ rpm.getPreferredActivities( filters, activities, null );
+
+ assertThat( activities.size(), equalTo(1) );
+ assertThat( activities.get( 0 ).getPackageName(), equalTo(packageName));
+
+ // Test not match
+ IntentFilter filter1 = new IntentFilter(Intent.ACTION_VIEW );
+ filters.add( filter1 );
+ filters.clear();
+ activities.clear();
+ filters.add( filter1 );
+
+ rpm.getPreferredActivities( filters, activities, null );
+
+ assertThat( activities.size(), equalTo(0) );
+ }
+
}
View
41 src/test/java/com/xtremelabs/robolectric/shadows/BitmapFactoryTest.java
@@ -14,6 +14,12 @@
import static com.xtremelabs.robolectric.Robolectric.shadowOf;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThat;
+
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.hamcrest.CoreMatchers.sameInstance;
+import static org.hamcrest.CoreMatchers.nullValue;
+import static org.hamcrest.CoreMatchers.notNullValue;
@RunWith(WithTestDefaultsRunner.class)
public class BitmapFactoryTest {
@@ -43,6 +49,15 @@ public void decodeStream_shouldSetDescription() throws Exception {
}
@Test
+ public void decodeStream_shouldSetDescriptionWithNullOptions() throws Exception {
+ InputStream inputStream = Robolectric.application.getContentResolver().openInputStream(Uri.parse("content:/path"));
+ Bitmap bitmap = BitmapFactory.decodeStream(inputStream, null, null);
+ assertEquals("Bitmap for content:/path", shadowOf(bitmap).getDescription());
+ assertEquals(100, bitmap.getWidth());
+ assertEquals(100, bitmap.getHeight());
+ }
+
+ @Test
public void decodeResource_shouldGetWidthAndHeightFromHints() throws Exception {
ShadowBitmapFactory.provideWidthAndHeightHints(R.drawable.an_image, 123, 456);
@@ -59,7 +74,7 @@ public void decodeResource_canTakeOptions() throws Exception {
Bitmap bitmap = BitmapFactory.decodeResource(Robolectric.application.getResources(), R.drawable.an_image, options);
assertEquals(true, shadowOf(bitmap).getDescription().contains("inSampleSize=100"));
}
-
+
@Test
public void decodeFile_shouldGetWidthAndHeightFromHints() throws Exception {
ShadowBitmapFactory.provideWidthAndHeightHints("/some/file.jpg", 123, 456);
@@ -100,4 +115,28 @@ public void decodeStream_shouldGetWidthAndHeightFromHints() throws Exception {
assertEquals(123, bitmap.getWidth());
assertEquals(456, bitmap.getHeight());
}
+
+ @Test
+ public void decodeByteArray_shouldSetDataChecksum() throws Exception {
+ byte[] data = { 23, 100, 23, 52, 23, 18, 76, 43 };
+
+ Bitmap bitmap = ShadowBitmapFactory.decodeByteArray(data, 0, data.length);
+ assertThat( bitmap, notNullValue() );
+ assertThat( shadowOf(bitmap).getDescription(), equalTo( "Bitmap for byte array, checksum:80429753 offset: 0 length: 8" ) );
+ assertThat( bitmap.getWidth(), equalTo(100) );
+ assertThat( bitmap.getHeight(), equalTo(100) );
+
+ }
+
+ @Test
+ public void decodeByteArray_withOptionsShouldSetDataChecksum() throws Exception {
+ byte[] data = { 23, 100, 23, 52, 23, 18, 76, 43 };
+
+ BitmapFactory.Options options = new BitmapFactory.Options();
+ options.inSampleSize = 4;
+ Bitmap bitmap = ShadowBitmapFactory.decodeByteArray(data, 0, data.length, options);
+ assertThat( shadowOf(bitmap).getDescription(), equalTo( "Bitmap for byte array, checksum:80429753 offset: 0 length: 8 with options inSampleSize=4" ) );
+ assertThat( bitmap.getWidth(), equalTo(100) );
+ assertThat( bitmap.getHeight(), equalTo(100) );
+ }
}
View
19 src/test/java/com/xtremelabs/robolectric/shadows/BluetoothAdapterTest.java
@@ -0,0 +1,19 @@
+package com.xtremelabs.robolectric.shadows;
+
+import android.bluetooth.BluetoothAdapter;
+import com.xtremelabs.robolectric.WithTestDefaultsRunner;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import static com.xtremelabs.robolectric.Robolectric.shadowOf;
+import static junit.framework.Assert.assertEquals;
+
+@RunWith(WithTestDefaultsRunner.class)
+public class BluetoothAdapterTest {
+ @Test
+ public void canGetAndSetAddress() throws Exception {
+ BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
+ shadowOf(adapter).setAddress("expected");
+ assertEquals("expected", adapter.getAddress());
+ }
+}
View
13 src/test/java/com/xtremelabs/robolectric/shadows/ParcelTest.java
@@ -1,6 +1,7 @@
package com.xtremelabs.robolectric.shadows;
import android.content.Intent;
+import android.os.Binder;
import android.os.Bundle;
import android.os.Parcel;
import com.xtremelabs.robolectric.Robolectric;
@@ -55,6 +56,11 @@ public void testReadStringWhenEmpty() {
}
@Test
+ public void testReadStrongBinderWhenEmpty() {
+ assertThat(parcel.readStrongBinder(), nullValue());
+ }
+
+ @Test
public void testReadWriteSingleString() {
String val = "test";
parcel.writeString(val);
@@ -310,4 +316,11 @@ public void testReadAndWriteStringList() throws Exception {
assertEquals("str1", rehydrated.get(0));
assertEquals("str2", rehydrated.get(1));
}
+
+ @Test
+ public void testReadWriteStrongBinder() throws Exception {
+ Binder expected = new Binder();
+ parcel.writeStrongBinder(expected);
+ assertEquals(expected, parcel.readStrongBinder());
+ }
}
View
3  src/test/java/com/xtremelabs/robolectric/shadows/ViewGroupTest.java
@@ -19,6 +19,7 @@
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
+import static com.xtremelabs.robolectric.Robolectric.DEFAULT_SDK_VERSION;
import static com.xtremelabs.robolectric.Robolectric.shadowOf;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.sameInstance;
@@ -40,7 +41,7 @@
@Before
public void setUp() throws Exception {
context = new Application();
- ShadowApplication.bind(context, new ResourceLoader(10, R.class, null, null));
+ ShadowApplication.bind(context, new ResourceLoader(DEFAULT_SDK_VERSION, R.class, null, null));
root = new FrameLayout(context);
View
3  src/test/java/com/xtremelabs/robolectric/util/TestUtil.java
@@ -8,6 +8,7 @@
import java.util.Map;
import java.util.Properties;
+import static com.xtremelabs.robolectric.Robolectric.DEFAULT_SDK_VERSION;
import static org.junit.Assert.assertTrue;
public abstract class TestUtil {
@@ -90,6 +91,6 @@ public static File getSystemResourceDir(String... paths) throws Exception {
sdkDir = localProperties.getProperty("sdk.dir");
}
- return file(new File(sdkDir, "platforms/android-10/data/res/"), paths);
+ return file(new File(sdkDir, "platforms/android-" + DEFAULT_SDK_VERSION + "/data/res/"), paths);
}
}
View
2  src/test/resources/TestAndroidManifest.xml
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xtremelabs.robolectric">
- <uses-sdk android:targetSdkVersion="10"/>
+ <uses-sdk android:targetSdkVersion="16"/>
</manifest>
Please sign in to comment.
Something went wrong with that request. Please try again.