1 package org.robolectric; 2 3 import static org.assertj.core.api.Assertions.assertThat; 4 import static org.robolectric.res.AttributeResource.ANDROID_RES_NS_PREFIX; 5 6 import android.util.AttributeSet; 7 import org.junit.Rule; 8 import org.junit.Test; 9 import org.junit.rules.ExpectedException; 10 import org.junit.runner.RunWith; 11 import org.robolectric.res.AttributeResource; 12 13 /** 14 * Tests for {@link Robolectric#buildAttributeSet()} 15 */ 16 @RunWith(RobolectricTestRunner.class) 17 public class AttributeSetBuilderTest { 18 19 private static final String ANDROID_NS = "http://schemas.android.com/apk/res/android"; 20 21 @Rule 22 public ExpectedException thrown = ExpectedException.none(); 23 24 @Test getAttributeResourceValue_shouldReturnTheResourceValue()25 public void getAttributeResourceValue_shouldReturnTheResourceValue() throws Exception { 26 AttributeSet roboAttributeSet = Robolectric.buildAttributeSet() 27 .addAttribute(android.R.attr.text, "@android:string/ok") 28 .build(); 29 30 assertThat(roboAttributeSet.getAttributeResourceValue(ANDROID_NS, "text", 0)).isEqualTo(android.R.string.ok); 31 } 32 33 @Test getAttributeResourceValueWithLeadingWhitespace_shouldReturnTheResourceValue()34 public void getAttributeResourceValueWithLeadingWhitespace_shouldReturnTheResourceValue() throws Exception { 35 AttributeSet roboAttributeSet = Robolectric.buildAttributeSet() 36 .addAttribute(android.R.attr.text, " @android:string/ok") 37 .build(); 38 39 assertThat(roboAttributeSet.getAttributeResourceValue(ANDROID_NS, "text", 0)).isEqualTo(android.R.string.ok); 40 } 41 42 @Test getSystemAttributeResourceValue_shouldReturnDefaultValueForNullResourceId()43 public void getSystemAttributeResourceValue_shouldReturnDefaultValueForNullResourceId() throws Exception { 44 AttributeSet roboAttributeSet = Robolectric.buildAttributeSet() 45 .addAttribute(android.R.attr.text, AttributeResource.NULL_VALUE) 46 .build(); 47 48 assertThat(roboAttributeSet.getAttributeResourceValue(ANDROID_RES_NS_PREFIX + "com.some.namespace", "text", 0)).isEqualTo(0); 49 } 50 51 @Test getSystemAttributeResourceValue_shouldReturnDefaultValueForNonMatchingNamespaceId()52 public void getSystemAttributeResourceValue_shouldReturnDefaultValueForNonMatchingNamespaceId() throws Exception { 53 AttributeSet roboAttributeSet = Robolectric.buildAttributeSet() 54 .addAttribute(android.R.attr.id, "@+id/text1") 55 .build(); 56 57 assertThat(roboAttributeSet.getAttributeResourceValue(ANDROID_RES_NS_PREFIX + "com.some.other.namespace", "id", 0)).isEqualTo(0); 58 } 59 60 @Test shouldCopeWithDefiningLocalIds()61 public void shouldCopeWithDefiningLocalIds() throws Exception { 62 AttributeSet roboAttributeSet = Robolectric.buildAttributeSet() 63 .addAttribute(android.R.attr.id, "@+id/text1") 64 .build(); 65 66 assertThat(roboAttributeSet.getAttributeResourceValue(ANDROID_NS, "id", 0)).isEqualTo(R.id.text1); 67 } 68 69 @Test getAttributeResourceValue_withNamespace_shouldReturnTheResourceValue()70 public void getAttributeResourceValue_withNamespace_shouldReturnTheResourceValue() throws Exception { 71 AttributeSet roboAttributeSet = Robolectric.buildAttributeSet() 72 .addAttribute(R.attr.message, "@string/howdy") 73 .build(); 74 75 assertThat(roboAttributeSet.getAttributeResourceValue(ANDROID_RES_NS_PREFIX + R.class.getPackage().getName(), "message", 0)).isEqualTo(R.string.howdy); 76 } 77 78 @Test getAttributeResourceValue_shouldReturnDefaultValueWhenAttributeIsNull()79 public void getAttributeResourceValue_shouldReturnDefaultValueWhenAttributeIsNull() throws Exception { 80 AttributeSet roboAttributeSet = Robolectric.buildAttributeSet() 81 .addAttribute(android.R.attr.text, AttributeResource.NULL_VALUE) 82 .build(); 83 84 assertThat(roboAttributeSet.getAttributeResourceValue(ANDROID_RES_NS_PREFIX + R.class.getPackage().getName(), "message", -1)).isEqualTo(-1); 85 } 86 87 @Test getAttributeResourceValue_shouldReturnDefaultValueWhenNotInAttributeSet()88 public void getAttributeResourceValue_shouldReturnDefaultValueWhenNotInAttributeSet() throws Exception { 89 AttributeSet roboAttributeSet = Robolectric.buildAttributeSet() 90 .build(); 91 92 assertThat(roboAttributeSet.getAttributeResourceValue(ANDROID_RES_NS_PREFIX + R.class.getPackage().getName(), "message", -1)).isEqualTo(-1); 93 } 94 95 @Test getAttributeBooleanValue_shouldGetBooleanValuesFromAttributes()96 public void getAttributeBooleanValue_shouldGetBooleanValuesFromAttributes() throws Exception { 97 AttributeSet roboAttributeSet = Robolectric.buildAttributeSet() 98 .addAttribute(R.attr.isSugary, "true") 99 .build(); 100 101 assertThat(roboAttributeSet.getAttributeBooleanValue(ANDROID_RES_NS_PREFIX + R.class.getPackage().getName(), "isSugary", false)).isTrue(); 102 } 103 104 @Test getAttributeBooleanValue_withNamespace_shouldGetBooleanValuesFromAttributes()105 public void getAttributeBooleanValue_withNamespace_shouldGetBooleanValuesFromAttributes() throws Exception { 106 // org.robolectric.lib1.R values should be reconciled to match org.robolectric.R values. 107 AttributeSet roboAttributeSet = Robolectric.buildAttributeSet() 108 .addAttribute(R.attr.isSugary, "true") 109 .build(); 110 111 assertThat(roboAttributeSet.getAttributeBooleanValue(ANDROID_RES_NS_PREFIX + R.class.getPackage().getName(), "isSugary", false)).isTrue(); 112 } 113 114 @Test getAttributeBooleanValue_shouldReturnDefaultBooleanValueWhenNotInAttributeSet()115 public void getAttributeBooleanValue_shouldReturnDefaultBooleanValueWhenNotInAttributeSet() throws Exception { 116 AttributeSet roboAttributeSet = Robolectric.buildAttributeSet() 117 .build(); 118 119 assertThat(roboAttributeSet.getAttributeBooleanValue(ANDROID_RES_NS_PREFIX + "com.some.namespace", "isSugary", true)).isTrue(); 120 } 121 122 @Test getAttributeValue_byName_shouldReturnValueFromAttribute()123 public void getAttributeValue_byName_shouldReturnValueFromAttribute() throws Exception { 124 AttributeSet roboAttributeSet = Robolectric.buildAttributeSet() 125 .addAttribute(R.attr.isSugary, "oh heck yeah") 126 .build(); 127 128 assertThat(roboAttributeSet.getAttributeValue(ANDROID_RES_NS_PREFIX + R.class.getPackage().getName(), "isSugary")).isEqualTo("oh heck yeah"); 129 } 130 131 @Test getAttributeValue_byNameWithReference_shouldReturnFullyQualifiedValueFromAttribute()132 public void getAttributeValue_byNameWithReference_shouldReturnFullyQualifiedValueFromAttribute() throws Exception { 133 AttributeSet roboAttributeSet = Robolectric.buildAttributeSet() 134 .addAttribute(R.attr.isSugary, "@string/ok") 135 .build(); 136 137 assertThat(roboAttributeSet.getAttributeValue(ANDROID_RES_NS_PREFIX + R.class.getPackage().getName(), "isSugary")).isEqualTo("@org.robolectric:string/ok"); 138 } 139 140 @Test getAttributeValue_byId_shouldReturnValueFromAttribute()141 public void getAttributeValue_byId_shouldReturnValueFromAttribute() throws Exception { 142 AttributeSet roboAttributeSet = Robolectric.buildAttributeSet() 143 .addAttribute(R.attr.isSugary, "oh heck yeah") 144 .build(); 145 146 assertThat(roboAttributeSet.getAttributeValue(0)).isEqualTo("oh heck yeah"); 147 } 148 149 @Test getAttributeValue_byIdWithReference_shouldReturnValueFromAttribute()150 public void getAttributeValue_byIdWithReference_shouldReturnValueFromAttribute() throws Exception { 151 AttributeSet roboAttributeSet = Robolectric.buildAttributeSet() 152 .addAttribute(R.attr.isSugary, "@string/ok") 153 .build(); 154 155 assertThat(roboAttributeSet.getAttributeValue(0)).isEqualTo("@org.robolectric:string/ok"); 156 } 157 158 @Test getAttributeIntValue_shouldReturnValueFromAttribute()159 public void getAttributeIntValue_shouldReturnValueFromAttribute() throws Exception { 160 AttributeSet roboAttributeSet = Robolectric.buildAttributeSet() 161 .addAttribute(R.attr.sugarinessPercent, "100") 162 .build(); 163 164 assertThat(roboAttributeSet.getAttributeIntValue(ANDROID_RES_NS_PREFIX + R.class.getPackage().getName(), "sugarinessPercent", 0)).isEqualTo(100); 165 } 166 167 @Test getAttributeIntValue_shouldReturnHexValueFromAttribute()168 public void getAttributeIntValue_shouldReturnHexValueFromAttribute() throws Exception { 169 AttributeSet roboAttributeSet = Robolectric.buildAttributeSet() 170 .addAttribute(R.attr.sugarinessPercent, "0x10") 171 .build(); 172 173 assertThat(roboAttributeSet.getAttributeIntValue(ANDROID_RES_NS_PREFIX + R.class.getPackage().getName(), "sugarinessPercent", 0)).isEqualTo(16); 174 } 175 176 @Test getAttributeIntValue_whenTypeAllowsIntOrEnum_withInt_shouldReturnInt()177 public void getAttributeIntValue_whenTypeAllowsIntOrEnum_withInt_shouldReturnInt() throws Exception { 178 AttributeSet roboAttributeSet = Robolectric.buildAttributeSet() 179 .addAttribute(R.attr.numColumns, "3") 180 .build(); 181 182 assertThat(roboAttributeSet.getAttributeIntValue(ANDROID_RES_NS_PREFIX + R.class.getPackage().getName(), "numColumns", 0)).isEqualTo(3); 183 } 184 185 @Test getAttributeIntValue_shouldReturnValueFromAttributeWhenNotInAttributeSet()186 public void getAttributeIntValue_shouldReturnValueFromAttributeWhenNotInAttributeSet() throws Exception { 187 AttributeSet roboAttributeSet = Robolectric.buildAttributeSet() 188 .build(); 189 190 assertThat(roboAttributeSet.getAttributeIntValue(ANDROID_RES_NS_PREFIX + R.class.getPackage().getName(), "sugarinessPercent", 42)).isEqualTo(42); 191 } 192 193 @Test getAttributeIntValue_shouldReturnEnumValuesForEnumAttributesWhenNotInAttributeSet()194 public void getAttributeIntValue_shouldReturnEnumValuesForEnumAttributesWhenNotInAttributeSet() throws Exception { 195 AttributeSet roboAttributeSet = Robolectric.buildAttributeSet() 196 .build(); 197 198 assertThat(roboAttributeSet.getAttributeIntValue(ANDROID_RES_NS_PREFIX + R.class.getPackage().getName(), "itemType", 24)).isEqualTo(24); 199 } 200 201 @Test getAttributeFloatValue_shouldGetFloatValuesFromAttributes()202 public void getAttributeFloatValue_shouldGetFloatValuesFromAttributes() throws Exception { 203 AttributeSet roboAttributeSet = Robolectric.buildAttributeSet() 204 .addAttribute(R.attr.isSugary, "1234.456") 205 .build(); 206 207 assertThat(roboAttributeSet.getAttributeFloatValue(ANDROID_RES_NS_PREFIX + R.class.getPackage().getName(), "isSugary", 78.9f)).isEqualTo(1234.456f); 208 } 209 210 @Test getAttributeFloatValue_shouldReturnDefaultFloatValueWhenNotInAttributeSet()211 public void getAttributeFloatValue_shouldReturnDefaultFloatValueWhenNotInAttributeSet() throws Exception { 212 AttributeSet roboAttributeSet = Robolectric.buildAttributeSet() 213 .build(); 214 215 assertThat(roboAttributeSet.getAttributeFloatValue(ANDROID_RES_NS_PREFIX + R.class.getPackage().getName(), "isSugary", 78.9f)).isEqualTo(78.9f); 216 } 217 218 @Test getStyleAttribute_doesNotThrowException()219 public void getStyleAttribute_doesNotThrowException() throws Exception { 220 AttributeSet roboAttributeSet = Robolectric.buildAttributeSet() 221 .build(); 222 223 roboAttributeSet.getStyleAttribute(); 224 } 225 226 @Test getStyleAttribute_returnsZeroWhenNoStyle()227 public void getStyleAttribute_returnsZeroWhenNoStyle() throws Exception { 228 AttributeSet roboAttributeSet = Robolectric.buildAttributeSet() 229 .build(); 230 231 assertThat(roboAttributeSet.getStyleAttribute()).isEqualTo(0); 232 } 233 234 @Test getStyleAttribute_returnsCorrectValue()235 public void getStyleAttribute_returnsCorrectValue() throws Exception { 236 AttributeSet roboAttributeSet = Robolectric.buildAttributeSet() 237 .setStyleAttribute("@style/Gastropod") 238 .build(); 239 240 assertThat(roboAttributeSet.getStyleAttribute()).isEqualTo(R.style.Gastropod); 241 } 242 243 @Test getStyleAttribute_whenStyleIsBogus()244 public void getStyleAttribute_whenStyleIsBogus() throws Exception { 245 AttributeSet roboAttributeSet = Robolectric.buildAttributeSet() 246 .setStyleAttribute("@style/non_existent_style") 247 .build(); 248 249 assertThat(roboAttributeSet.getStyleAttribute()).isEqualTo(0); 250 } 251 252 @Test getAttributeNameResource()253 public void getAttributeNameResource() throws Exception { 254 AttributeSet roboAttributeSet = Robolectric.buildAttributeSet() 255 .addAttribute(R.attr.isSugary, "1") 256 .build(); 257 258 assertThat(roboAttributeSet.getAttributeNameResource(0)).isEqualTo(R.attr.isSugary); 259 } 260 } 261