Skip to content

Commit

Permalink
Merge branch 'master' of github.com:zoodles/robolectric
Browse files Browse the repository at this point in the history
  • Loading branch information
Wenhui Yao committed Sep 28, 2012
2 parents 37d35ce + 7139ff6 commit aa04a45
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 22 deletions.
57 changes: 37 additions & 20 deletions src/main/java/com/xtremelabs/robolectric/RobolectricTestRunner.java
Expand Up @@ -484,6 +484,16 @@ 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>();

Expand All @@ -498,38 +508,45 @@ private HashMap<Field,Object> getWithConstantAnnotations(Method method) {
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" )) {
Class classWithField = (Class) anno.annotationType().getMethod("classWithField").invoke(anno);
String fieldName = (String) anno.annotationType().getMethod("fieldName").invoke(anno);
String newValue = (String) anno.annotationType().getMethod("newValue").invoke(anno);
addConstant(constants, classWithField, fieldName, newValue);
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);
}

if (name.equals( "com.xtremelabs.robolectric.annotation.WithConstantInt" )) {
Class classWithField = (Class) anno.annotationType().getMethod("classWithField").invoke(anno);
String fieldName = (String) anno.annotationType().getMethod("fieldName").invoke(anno);
Integer newValue = (Integer) anno.annotationType().getMethod("newValue").invoke(anno);
addConstant(constants, classWithField, fieldName, newValue);
else {
return;
}

} catch (Exception e) {
throw new RuntimeException(e);
}
}

private void addConstant(HashMap<Field,Object> constants, Class classWithField, String fieldName, Object newValue) {
try {

@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 (NoSuchFieldException e) {
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);
Expand Down
Expand Up @@ -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;

Expand Down Expand Up @@ -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());
}
Expand Down
@@ -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 {
Expand Down
Expand Up @@ -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 {
Expand Down Expand Up @@ -100,4 +106,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) );
}
}

0 comments on commit aa04a45

Please sign in to comment.