15d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)package com.xtremelabs.robolectric.res; 25d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 35d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)import android.view.View; 45d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)import com.xtremelabs.robolectric.R; 55d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)import com.xtremelabs.robolectric.WithTestDefaultsRunner; 65d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)import com.xtremelabs.robolectric.tester.android.util.TestAttributeSet; 75d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)import com.xtremelabs.robolectric.util.CustomView; 85d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)import org.junit.Before; 95d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)import org.junit.Test; 105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)import org.junit.runner.RunWith; 115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)import java.util.HashMap; 135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)import static com.xtremelabs.robolectric.util.TestUtil.resourceFile; 155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)import static junit.framework.Assert.assertEquals; 165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)import static org.hamcrest.CoreMatchers.equalTo; 175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)import static org.hamcrest.MatcherAssert.assertThat; 185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)@RunWith(WithTestDefaultsRunner.class) 205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)public class TestAttributeSetTest { 215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) private HashMap<String, String> attributes; 225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) private ResourceExtractor resourceExtractor; 235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) @Before 255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) public void setUp() throws Exception { 265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) attributes = new HashMap<String, String>(); 275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) resourceExtractor = new ResourceExtractor(); 295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) resourceExtractor.addLocalRClass(R.class); 305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) resourceExtractor.addSystemRClass(android.R.class); 315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) } 325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) @Test 345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) public void getSystemAttributeResourceValue_shouldReturnTheResourceValue() throws Exception { 355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) attributes.put("android:id", "@android:id/text1"); 365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) TestAttributeSet testAttributeSet = new TestAttributeSet(attributes, resourceExtractor, null, null, false); 375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) assertThat(testAttributeSet.getAttributeResourceValue("android", "id", 0), equalTo(android.R.id.text1)); 385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) } 395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) @Test 41 public void getSystemAttributeResourceValue_shouldNotReturnTheResourceValueIfNameSpaceDoesNotMatch() throws Exception { 42 attributes.put("id", "@id/text1"); 43 TestAttributeSet testAttributeSet = new TestAttributeSet(attributes, resourceExtractor, null, null, false); 44 assertEquals(0, testAttributeSet.getAttributeResourceValue("android", "id", 0)); 45 } 46 47 @Test 48 public void getSystemAttributeResourceValue_shouldReturnDefaultValueForNullResourceId() throws Exception { 49 attributes.put("id", "@null"); 50 TestAttributeSet testAttributeSet = new TestAttributeSet(attributes, resourceExtractor, null, null, false); 51 assertEquals(0, testAttributeSet.getAttributeResourceValue("com.some.namespace", "id", 0)); 52 } 53 54 @Test 55 public void shouldCopeWithDefiningSystemIds() throws Exception { 56 attributes.put("android:id", "@+id/text1"); 57 58 TestAttributeSet testAttributeSet = new TestAttributeSet(attributes, resourceExtractor, null, null, true); 59 assertThat(testAttributeSet.getAttributeResourceValue("android", "id", 0), equalTo(android.R.id.text1)); 60 } 61 62 @Test 63 public void shouldCopeWithDefiningLocalIds() throws Exception { 64 attributes.put("android:id", "@+id/text1"); 65 66 TestAttributeSet testAttributeSet = new TestAttributeSet(attributes, resourceExtractor, null, null, false); 67 assertThat(testAttributeSet.getAttributeResourceValue("android", "id", 0), equalTo(R.id.text1)); 68 } 69 70 @Test 71 public void getAttributeResourceValue_shouldReturnTheResourceValue() throws Exception { 72 attributes.put("message", "@string/howdy"); 73 74 TestAttributeSet testAttributeSet = new TestAttributeSet(attributes, resourceExtractor, null, null, false); 75 assertThat(testAttributeSet.getAttributeResourceValue("com.some.namespace", "message", 0), equalTo(R.string.howdy)); 76 } 77 78 @Test 79 public void getAttributeResourceValue_withNamespace_shouldReturnTheResourceValue() throws Exception { 80 attributes.put("message", "@string/howdy"); 81 82 TestAttributeSet testAttributeSet = new TestAttributeSet(attributes, resourceExtractor, null, null, false); 83 assertThat(testAttributeSet.getAttributeResourceValue("com.some.namespace", "message", 0), equalTo(R.string.howdy)); 84 } 85 86 @Test 87 public void getAttributeResourceValue_shouldReturnDefaultValueWhenNotInAttributeSet() throws Exception { 88 TestAttributeSet testAttributeSet = new TestAttributeSet(attributes, resourceExtractor, null, null, false); 89 assertThat(testAttributeSet.getAttributeResourceValue("com.some.namespace", "message", -1), equalTo(-1)); 90 } 91 92 @Test 93 public void getAttributeBooleanValue_shouldGetBooleanValuesFromAttributes() throws Exception { 94 attributes.put("isSugary", "true"); 95 96 TestAttributeSet testAttributeSet = new TestAttributeSet(attributes, null, null, null, false); 97 assertThat(testAttributeSet.getAttributeBooleanValue("com.some.namespace", "isSugary", false), equalTo(true)); 98 } 99 100 @Test 101 public void getAttributeBooleanValue_withNamespace_shouldGetBooleanValuesFromAttributes() throws Exception { 102 attributes.put("xxx:isSugary", "true"); 103 104 TestAttributeSet testAttributeSet = new TestAttributeSet(attributes, null, null, null, false); 105 assertThat(testAttributeSet.getAttributeBooleanValue("com.some.namespace", "isSugary", false), equalTo(true)); 106 } 107 108 @Test 109 public void getAttributeBooleanValue_shouldReturnDefaultBooleanValueWhenNotInAttributeSet() throws Exception { 110 TestAttributeSet testAttributeSet = new TestAttributeSet(attributes, null, null, null, false); 111 assertThat(testAttributeSet.getAttributeBooleanValue("com.some.namespace", "isSugary", true), equalTo(true)); 112 } 113 114 @Test 115 public void getAttributeValue_shouldReturnValueFromAttribute() throws Exception { 116 attributes.put("isSugary", "oh heck yeah"); 117 118 TestAttributeSet testAttributeSet = new TestAttributeSet(attributes, null, null, null, false); 119 assertThat(testAttributeSet.getAttributeValue("com.some.namespace", "isSugary"), equalTo("oh heck yeah")); 120 } 121 122 @Test 123 public void getAttributeIntValue_shouldReturnValueFromAttribute() throws Exception { 124 attributes.put("sugarinessPercent", "100"); 125 126 AttrResourceLoader resourceLoader = new AttrResourceLoader(resourceExtractor); 127 TestAttributeSet testAttributeSet = new TestAttributeSet(attributes, null, resourceLoader, View.class, false); 128 assertThat(testAttributeSet.getAttributeIntValue("some namespace", "sugarinessPercent", 0), equalTo(100)); 129 } 130 131 @Test 132 public void getAttributeIntValue_shouldReturnEnumValuesForEnumAttributes() throws Exception { 133 attributes.put("itemType", "string"); 134 135 AttrResourceLoader attrResourceLoader = new AttrResourceLoader(resourceExtractor); 136 new DocumentLoader(attrResourceLoader).loadResourceXmlDir(resourceFile("res", "values")); 137 TestAttributeSet testAttributeSet = new TestAttributeSet(attributes, null, attrResourceLoader, CustomView.class, false); 138 assertThat(testAttributeSet.getAttributeIntValue("some namespace", "itemType", 0), equalTo(1)); 139 } 140 141 @Test 142 public void getAttributeIntValue_defaultConstructor() throws Exception { 143 TestAttributeSet testAttributeSet = new TestAttributeSet(); 144 testAttributeSet.put("sugarinessPercent", "100"); 145 146 assertThat(testAttributeSet.getAttributeIntValue("some namespace", "sugarinessPercent", 0), equalTo(100)); 147 } 148} 149