• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.xtremelabs.robolectric.res;
2 
3 import android.view.View;
4 import com.xtremelabs.robolectric.R;
5 import com.xtremelabs.robolectric.WithTestDefaultsRunner;
6 import com.xtremelabs.robolectric.tester.android.util.TestAttributeSet;
7 import com.xtremelabs.robolectric.util.CustomView;
8 import org.junit.Before;
9 import org.junit.Test;
10 import org.junit.runner.RunWith;
11 
12 import java.util.HashMap;
13 
14 import static com.xtremelabs.robolectric.util.TestUtil.resourceFile;
15 import static junit.framework.Assert.assertEquals;
16 import static org.hamcrest.CoreMatchers.equalTo;
17 import static org.hamcrest.MatcherAssert.assertThat;
18 
19 @RunWith(WithTestDefaultsRunner.class)
20 public class TestAttributeSetTest {
21     private HashMap<String, String> attributes;
22     private ResourceExtractor resourceExtractor;
23 
24     @Before
setUp()25     public void setUp() throws Exception {
26         attributes = new HashMap<String, String>();
27 
28         resourceExtractor = new ResourceExtractor();
29         resourceExtractor.addLocalRClass(R.class);
30         resourceExtractor.addSystemRClass(android.R.class);
31     }
32 
33     @Test
getSystemAttributeResourceValue_shouldReturnTheResourceValue()34     public void getSystemAttributeResourceValue_shouldReturnTheResourceValue() throws Exception {
35         attributes.put("android:id", "@android:id/text1");
36         TestAttributeSet testAttributeSet = new TestAttributeSet(attributes, resourceExtractor, null, null, false);
37         assertThat(testAttributeSet.getAttributeResourceValue("android", "id", 0), equalTo(android.R.id.text1));
38     }
39 
40     @Test
getSystemAttributeResourceValue_shouldNotReturnTheResourceValueIfNameSpaceDoesNotMatch()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
getSystemAttributeResourceValue_shouldReturnDefaultValueForNullResourceId()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
shouldCopeWithDefiningSystemIds()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
shouldCopeWithDefiningLocalIds()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
getAttributeResourceValue_shouldReturnTheResourceValue()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
getAttributeResourceValue_withNamespace_shouldReturnTheResourceValue()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
getAttributeResourceValue_shouldReturnDefaultValueWhenNotInAttributeSet()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
getAttributeBooleanValue_shouldGetBooleanValuesFromAttributes()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
getAttributeBooleanValue_withNamespace_shouldGetBooleanValuesFromAttributes()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
getAttributeBooleanValue_shouldReturnDefaultBooleanValueWhenNotInAttributeSet()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
getAttributeValue_shouldReturnValueFromAttribute()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
getAttributeIntValue_shouldReturnValueFromAttribute()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
getAttributeIntValue_shouldReturnEnumValuesForEnumAttributes()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
getAttributeIntValue_defaultConstructor()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