1 package org.robolectric.res; 2 3 import static com.google.common.truth.Truth.assertThat; 4 import static org.junit.Assert.fail; 5 6 import org.junit.Test; 7 import org.junit.runner.RunWith; 8 import org.junit.runners.JUnit4; 9 import org.robolectric.res.android.ResTable_config; 10 11 @RunWith(JUnit4.class) 12 public class QualifiersTest { 13 @Test testQualifiers()14 public void testQualifiers() throws Exception { 15 assertThat(configFrom("values-land-finger")).isEqualTo("land-finger"); 16 } 17 18 @Test testWhenQualifiersFailToParse()19 public void testWhenQualifiersFailToParse() throws Exception { 20 try { 21 configFrom("values-unknown-v23"); 22 fail("Expected exception"); 23 } catch(IllegalArgumentException expected) { 24 assertThat(expected.getMessage()).contains("failed to parse qualifiers 'unknown-v23"); 25 } 26 } 27 configFrom(String path)28 private String configFrom(String path) { 29 FsFile xmlFile = Fs.newFile(path + "/whatever.xml"); 30 Qualifiers qualifiers = Qualifiers.fromParentDir(xmlFile.getParent()); 31 32 ResTable_config config = new XmlContext("package", xmlFile, qualifiers) 33 .getConfig(); 34 return config.toString(); 35 } 36 37 ///////// deprecated stuff... 38 addPlatformVersion()39 @Test public void addPlatformVersion() throws Exception { 40 assertThat(Qualifiers.addPlatformVersion("", 21)).isEqualTo("v21"); 41 assertThat(Qualifiers.addPlatformVersion("v23", 21)).isEqualTo("v23"); 42 assertThat(Qualifiers.addPlatformVersion("foo-v14", 21)).isEqualTo("foo-v14"); 43 } 44 addSmallestScreenWidth()45 @Test public void addSmallestScreenWidth() throws Exception { 46 assertThat(Qualifiers.addSmallestScreenWidth("", 320)).isEqualTo("sw320dp"); 47 assertThat(Qualifiers.addSmallestScreenWidth("sw160dp", 320)).isEqualTo("sw160dp"); 48 assertThat(Qualifiers.addSmallestScreenWidth("sw480dp", 320)).isEqualTo("sw480dp"); 49 assertThat(Qualifiers.addSmallestScreenWidth("en-v23", 320)).isEqualTo("en-v23-sw320dp"); // todo: order is wrong here 50 assertThat(Qualifiers.addSmallestScreenWidth("en-sw160dp-v23", 320)).isEqualTo("en-sw160dp-v23"); 51 assertThat(Qualifiers.addSmallestScreenWidth("en-sw480dp-v23", 320)).isEqualTo("en-sw480dp-v23"); 52 } 53 addScreenWidth()54 @Test public void addScreenWidth() throws Exception { 55 assertThat(Qualifiers.addScreenWidth("", 320)).isEqualTo("w320dp"); 56 assertThat(Qualifiers.addScreenWidth("w160dp", 320)).isEqualTo("w160dp"); 57 assertThat(Qualifiers.addScreenWidth("w480dp", 320)).isEqualTo("w480dp"); 58 assertThat(Qualifiers.addScreenWidth("en-v23", 320)).isEqualTo("en-v23-w320dp"); // todo: order is wrong here 59 assertThat(Qualifiers.addScreenWidth("en-w160dp-v23", 320)).isEqualTo("en-w160dp-v23"); 60 assertThat(Qualifiers.addScreenWidth("en-w480dp-v23", 320)).isEqualTo("en-w480dp-v23"); 61 } 62 getSmallestScreenWidth()63 @Test public void getSmallestScreenWidth() { 64 assertThat(Qualifiers.getSmallestScreenWidth("sw320dp")).isEqualTo(320); 65 assertThat(Qualifiers.getSmallestScreenWidth("sw320dp-v7")).isEqualTo(320); 66 assertThat(Qualifiers.getSmallestScreenWidth("en-rUS-sw320dp")).isEqualTo(320); 67 assertThat(Qualifiers.getSmallestScreenWidth("en-rUS-sw320dp-v7")).isEqualTo(320); 68 assertThat(Qualifiers.getSmallestScreenWidth("en-rUS-v7")).isEqualTo(-1); 69 assertThat(Qualifiers.getSmallestScreenWidth("en-rUS-w320dp-v7")).isEqualTo(-1); 70 } 71 getAddSmallestScreenWidth()72 @Test public void getAddSmallestScreenWidth() { 73 assertThat(Qualifiers.addSmallestScreenWidth("v7", 320)).isEqualTo("v7-sw320dp"); 74 assertThat(Qualifiers.addSmallestScreenWidth("sw320dp-v7", 480)).isEqualTo("sw320dp-v7"); 75 } 76 getScreenWidth()77 @Test public void getScreenWidth() { 78 assertThat(Qualifiers.getScreenWidth("w320dp")).isEqualTo(320); 79 assertThat(Qualifiers.getScreenWidth("w320dp-v7")).isEqualTo(320); 80 assertThat(Qualifiers.getScreenWidth("en-rUS-w320dp")).isEqualTo(320); 81 assertThat(Qualifiers.getScreenWidth("en-rUS-w320dp-v7")).isEqualTo(320); 82 assertThat(Qualifiers.getScreenWidth("en-rUS-v7")).isEqualTo(-1); 83 assertThat(Qualifiers.getScreenWidth("de-v23-sw320dp-w1024dp")).isEqualTo(1024); 84 assertThat(Qualifiers.getScreenWidth("en-rUS-sw320dp-v7")).isEqualTo(-1); 85 } 86 getAddScreenWidth()87 @Test public void getAddScreenWidth() { 88 assertThat(Qualifiers.addScreenWidth("v7", 320)).isEqualTo("v7-w320dp"); 89 assertThat(Qualifiers.addScreenWidth("w320dp-v7", 480)).isEqualTo("w320dp-v7"); 90 } 91 getOrientation()92 @Test public void getOrientation() { 93 assertThat(Qualifiers.getOrientation("land")).isEqualTo("land"); 94 assertThat(Qualifiers.getOrientation("en-rUs-land")).isEqualTo("land"); 95 assertThat(Qualifiers.getOrientation("port")).isEqualTo("port"); 96 assertThat(Qualifiers.getOrientation("port-v7")).isEqualTo("port"); 97 } 98 } 99