Resource XML references should work with leading whitespace.
diff --git a/resources/src/main/java/org/robolectric/res/AttributeResource.java b/resources/src/main/java/org/robolectric/res/AttributeResource.java
index d0763e8..4d94103 100644
--- a/resources/src/main/java/org/robolectric/res/AttributeResource.java
+++ b/resources/src/main/java/org/robolectric/res/AttributeResource.java
@@ -1,5 +1,6 @@
package org.robolectric.res;
+import java.util.regex.Pattern;
import javax.annotation.Nonnull;
public class AttributeResource {
@@ -9,6 +10,7 @@
public static final String NULL_VALUE = "@null";
public static final String EMPTY_VALUE = "@empty";
+ public static final Pattern IS_RESOURCE_REFERENCE = Pattern.compile("^\\s*@");
public final @Nonnull ResName resName;
public final @Nonnull String value;
@@ -34,7 +36,7 @@
public @Nonnull ResName getResourceReference() {
if (!isResourceReference()) throw new RuntimeException("not a resource reference: " + this);
- return ResName.qualifyResName(value.substring(1).replace("+", ""), contextPackageName, "style");
+ return ResName.qualifyResName(deref(value).replace("+", ""), contextPackageName, "style");
}
public boolean isStyleReference() {
@@ -64,12 +66,16 @@
}
public static boolean isResourceReference(String value) {
- return value.startsWith("@") && !isNull(value);
+ return IS_RESOURCE_REFERENCE.matcher(value).find() && !isNull(value);
}
public static @Nonnull ResName getResourceReference(String value, String defPackage, String defType) {
if (!isResourceReference(value)) throw new IllegalArgumentException("not a resource reference: " + value);
- return ResName.qualifyResName(value.substring(1).replace("+", ""), defPackage, defType);
+ return ResName.qualifyResName(deref(value).replace("+", ""), defPackage, defType);
+ }
+
+ private static @Nonnull String deref(@Nonnull String value) {
+ return value.substring(value.indexOf('@') + 1);
}
public static boolean isStyleReference(String value) {
diff --git a/robolectric/src/test/resources/res/values/attrs.xml b/robolectric/src/test/resources/res/values/attrs.xml
index 9da14e4..c70035c 100644
--- a/robolectric/src/test/resources/res/values/attrs.xml
+++ b/robolectric/src/test/resources/res/values/attrs.xml
@@ -68,7 +68,7 @@
<declare-styleable name="Theme.AnotherTheme.Attributes">
<attr name="averageSheepWidth" format="reference"/>
- <attr name="isSugary" format="string"/>
+ <attr name="isSugary" format="reference"/>
<attr name="logoHeight" format="reference"/>
<attr name="logoWidth" format="reference"/>
<attr name="styleReference" format="reference"/>