Skip to content

Commit

Permalink
Merge branch 'master' into rollback_broken_resources_fix
Browse files Browse the repository at this point in the history
  • Loading branch information
jongerrish committed Dec 9, 2016
2 parents b257fd9 + 48fc120 commit fbdddcb
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,13 @@ private void convertAndFill(AttributeResource attribute, TypedValue outValue, St
throw new Resources.NotFoundException("unknown resource " + resName);
}
outValue.type = TypedValue.TYPE_REFERENCE;
outValue.resourceId = resourceId;
if (!resolveRefs) {
// Just return the resourceId if resolveRefs is false.
outValue.data = resourceId;
return;
}

outValue.resourceId = resourceId;
TypedResource dereferencedRef = getResourceLoader(resName).getValue(resName, qualifiers);

if (dereferencedRef == null) {
Expand Down Expand Up @@ -284,8 +289,12 @@ public boolean getThemeValue(long themePtr, int ident, TypedValue outValue, bool

ThemeStyleSet themeStyleSet = getNativeTheme(themePtr).themeStyleSet;
AttributeResource attrValue = themeStyleSet.getAttrValue(resName);
while(resolveRefs && attrValue != null && attrValue.isStyleReference()) {
ResName attrResName = new ResName(attrValue.contextPackageName, "attr", attrValue.value.substring(1));
while(attrValue != null && attrValue.isStyleReference()) {
ResName attrResName = attrValue.getStyleReference();
if (attrValue.resName.equals(attrResName)) {
Logger.info("huh... circular reference for %s?", attrResName.getFullyQualifiedName());
return false;
}
attrValue = themeStyleSet.getAttrValue(attrResName);
}
if (attrValue != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.graphics.drawable.Icon;
import android.net.Uri;
import android.os.Build;
import org.robolectric.annotation.HiddenApi;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import org.robolectric.annotation.RealObject;
Expand All @@ -21,38 +22,45 @@ public class ShadowIcon {
@RealObject
private Icon realIcon;

@HiddenApi
@Implementation
public int getType() {
return directlyOn(realIcon, Icon.class, "getType");
return directlyOn(realIcon, Icon.class).getType();
}

@HiddenApi
@Implementation
public int getResId() {
return directlyOn(realIcon, Icon.class, "getResId");
return directlyOn(realIcon, Icon.class).getResId();
}

@HiddenApi
@Implementation
public Bitmap getBitmap() {
return directlyOn(realIcon, Icon.class, "getBitmap");
return directlyOn(realIcon, Icon.class).getBitmap();
}

@HiddenApi
@Implementation
public Uri getUri() {
return directlyOn(realIcon, Icon.class, "getUri");
return directlyOn(realIcon, Icon.class).getUri();
}

@HiddenApi
@Implementation
public int getDataLength() {
return directlyOn(realIcon, Icon.class, "getDataLength");
return directlyOn(realIcon, Icon.class).getDataLength();
}

@HiddenApi
@Implementation
public int getDataOffset() {
return directlyOn(realIcon, Icon.class, "getDataOffset");
return directlyOn(realIcon, Icon.class).getDataOffset();
}

@HiddenApi
@Implementation
public byte[] getDataBytes() {
return directlyOn(realIcon, Icon.class, "getDataBytes");
return directlyOn(realIcon, Icon.class).getDataBytes();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ public void themeResolveAttribute_shouldSupportNotDereferencingResource() {

theme.resolveAttribute(android.R.attr.windowBackground, out, false);
assertThat(out.type).isEqualTo(TypedValue.TYPE_REFERENCE);
assertThat(out.resourceId).isEqualTo(android.R.color.black);
assertThat(out.data).isEqualTo(android.R.color.black);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ public void setUp() throws Exception {
@Test public void shouldResolveReferencesThatStartWithAQuestionMark() throws Exception {
TestActivity activity = buildActivity(TestActivityWithAnotherTheme.class).create().get();
Button theButton = (Button) activity.findViewById(R.id.button);
assertThat(theButton.getMinWidth()).isEqualTo(42); // via AnotherTheme.Button -> logoWidth and logoHeight
// assertThat(theButton.getMinHeight()).isEqualTo(42); todo 2.0-cleanup
assertThat(theButton.getMinWidth()).isEqualTo(42);
assertThat(theButton.getMinHeight()).isEqualTo(8);
}

@Test public void shouldLookUpStylesFromStyleResId() throws Exception {
Expand Down Expand Up @@ -122,6 +122,60 @@ public void setUp() throws Exception {
assertThat(value1.coerceToString()).isEqualTo(value2.coerceToString());
}

@Test public void withResolveRefsFalse_shouldResolveValue() throws Exception {
TestActivity activity = buildActivity(TestActivityWithAnotherTheme.class).create().get();

TypedValue value = new TypedValue();
boolean resolved = activity.getTheme().resolveAttribute(R.attr.logoWidth, value, false);

assertThat(resolved).isTrue();
assertThat(value.type).isEqualTo(TypedValue.TYPE_DIMENSION);
assertThat(value.coerceToString()).isEqualTo("42.0px");
}

@Test public void withResolveRefsFalse_shouldNotResolveResource() throws Exception {
TestActivity activity = buildActivity(TestActivityWithAnotherTheme.class).create().get();

TypedValue value = new TypedValue();
boolean resolved = activity.getTheme().resolveAttribute(R.attr.logoHeight, value, false);

assertThat(resolved).isTrue();
assertThat(value.type).isEqualTo(TypedValue.TYPE_REFERENCE);
assertThat(value.data).isEqualTo(R.dimen.test_dp_dimen);
}

@Test public void withResolveRefsTrue_shouldResolveResource() throws Exception {
TestActivity activity = buildActivity(TestActivityWithAnotherTheme.class).create().get();

TypedValue value = new TypedValue();
boolean resolved = activity.getTheme().resolveAttribute(R.attr.logoHeight, value, true);

assertThat(resolved).isTrue();
assertThat(value.type).isEqualTo(TypedValue.TYPE_DIMENSION);
assertThat(value.resourceId).isEqualTo(R.dimen.test_dp_dimen);
assertThat(value.coerceToString()).isEqualTo("8.0dip");
}

@Test public void failToResolveCircularReference() throws Exception {
TestActivity activity = buildActivity(TestActivityWithAnotherTheme.class).create().get();

TypedValue value = new TypedValue();
boolean resolved = activity.getTheme().resolveAttribute(R.attr.isSugary, value, false);

assertThat(resolved).isFalse();
}

@Test public void canResolveAttrReferenceToDifferentPackage() throws Exception {
TestActivity activity = buildActivity(TestActivityWithAnotherTheme.class).create().get();

TypedValue value = new TypedValue();
boolean resolved = activity.getTheme().resolveAttribute(R.attr.styleReference, value, false);

assertThat(resolved).isTrue();
assertThat(value.type).isEqualTo(TypedValue.TYPE_REFERENCE);
assertThat(value.data).isEqualTo(R.style.Widget_AnotherTheme_Button);
}

@Test public void forStylesWithImplicitParents_shouldInheritValuesNotDefinedInChild() throws Exception {
Resources.Theme theme = resources.newTheme();
theme.applyStyle(R.style.Theme_Robolectric_ImplicitChild, true);
Expand Down
4 changes: 3 additions & 1 deletion robolectric/src/test/resources/res/values/themes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
<style name="Theme.AnotherTheme" parent="@style/Theme.Robolectric">
<item name="android:buttonStyle">@style/Widget.AnotherTheme.Button</item>
<item name="logoWidth">?attr/averageSheepWidth</item>
<item name="logoHeight">42dp</item>
<item name="logoHeight">@dimen/test_dp_dimen</item>
<item name="averageSheepWidth">42px</item>
<item name="animalStyle">@style/Gastropod</item>
<item name="isSugary">?attr/isSugary</item>
<item name="styleReference">?android:attr/buttonStyle</item>
<item name="typeface">custom_font</item>
<item name="string1">string 1 from Theme.AnotherTheme</item>
<item name="string2">string 2 from Theme.AnotherTheme</item>
Expand Down

0 comments on commit fbdddcb

Please sign in to comment.