Home | History | Annotate | Download | only in res
      1 package org.robolectric.res;
      2 
      3 import static com.google.common.truth.Truth.assertThat;
      4 import static java.util.Arrays.asList;
      5 
      6 import org.junit.Test;
      7 import org.junit.runner.RunWith;
      8 import org.junit.runners.JUnit4;
      9 
     10 @RunWith(JUnit4.class)
     11 public class StyleDataTest {
     12 
     13   private final ResName androidSearchViewStyle = new ResName("android", "attr", "searchViewStyle");
     14   private final ResName myLibSearchViewStyle = new ResName("library.resource", "attr", "searchViewStyle");
     15   private final ResName myAppSearchViewStyle = new ResName("my.app", "attr", "searchViewStyle");
     16 
     17   @Test
     18   public void getAttrValue_willFindLibraryResourcesWithSameName() {
     19     StyleData styleData = new StyleData("library.resource", "Theme_MyApp", "Theme_Material", asList(
     20         new AttributeResource(myLibSearchViewStyle, "lib_value", "library.resource")
     21     ));
     22 
     23     assertThat(styleData.getAttrValue(myAppSearchViewStyle).value).isEqualTo("lib_value");
     24     assertThat(styleData.getAttrValue(myLibSearchViewStyle).value).isEqualTo("lib_value");
     25 
     26     assertThat(styleData.getAttrValue(androidSearchViewStyle)).isNull();
     27   }
     28 
     29   @Test
     30   public void getAttrValue_willNotFindFrameworkResourcesWithSameName() {
     31     StyleData styleData = new StyleData("android", "Theme_Material", "Theme", asList(
     32         new AttributeResource(androidSearchViewStyle, "android_value", "android")
     33     ));
     34 
     35     assertThat(styleData.getAttrValue(androidSearchViewStyle).value).isEqualTo("android_value");
     36 
     37     assertThat(styleData.getAttrValue(myAppSearchViewStyle)).isNull();
     38     assertThat(styleData.getAttrValue(myLibSearchViewStyle)).isNull();
     39   }
     40 
     41   @Test
     42   public void getAttrValue_willChooseBetweenAmbiguousAttributes() {
     43     StyleData styleData = new StyleData("android", "Theme_Material", "Theme", asList(
     44         new AttributeResource(myLibSearchViewStyle, "lib_value", "library.resource"),
     45         new AttributeResource(androidSearchViewStyle, "android_value", "android")
     46     ));
     47 
     48     assertThat(styleData.getAttrValue(androidSearchViewStyle).value).isEqualTo("android_value");
     49     assertThat(styleData.getAttrValue(myLibSearchViewStyle).value).isEqualTo("lib_value");
     50 
     51     // todo: any packageNames that aren't 'android' should be treated as equivalent
     52 //    assertThat(styleData.getAttrValue(myAppSearchViewStyle).value).isEqualTo("lib_value");
     53   }
     54 
     55   @Test
     56   public void getAttrValue_willReturnTrimmedAttributeValues() throws Exception {
     57     StyleData styleData = new StyleData("library.resource", "Theme_MyApp", "Theme_Material", asList(
     58             new AttributeResource(myLibSearchViewStyle, "\n lib_value ", "library.resource")
     59     ));
     60 
     61     assertThat(styleData.getAttrValue(myAppSearchViewStyle).value).isEqualTo("\n lib_value ");
     62     assertThat(styleData.getAttrValue(myLibSearchViewStyle).trimmedValue).isEqualTo("lib_value");
     63   }
     64 
     65 }
     66