1package com.xtremelabs.robolectric.res; 2 3 4import android.view.ViewGroup; 5import android.widget.FrameLayout; 6import android.widget.TextView; 7import com.xtremelabs.robolectric.R; 8import com.xtremelabs.robolectric.Robolectric; 9import com.xtremelabs.robolectric.WithTestDefaultsRunner; 10import com.xtremelabs.robolectric.util.I18nException; 11import org.junit.Test; 12import org.junit.runner.RunWith; 13 14import java.io.File; 15 16import static com.xtremelabs.robolectric.util.TestUtil.resourceFile; 17import static org.hamcrest.CoreMatchers.equalTo; 18import static org.junit.Assert.*; 19 20@RunWith(WithTestDefaultsRunner.class) 21public class ResourceLoaderTest { 22 @Test 23 public void shouldUseFileSystemSeparatorWhenEvaluatingLayoutDirectories() throws Exception { 24 assertTrue(ResourceLoader.isLayoutDirectory(File.separator + "layout")); 25 } 26 27 @Test 28 public void shouldLoadSystemResources() throws Exception { 29 ResourceLoader resourceLoader = new ResourceLoader(10, R.class, resourceFile("res"), resourceFile("assets")); 30 String stringValue = resourceLoader.getStringValue(android.R.string.copy); 31 assertEquals("Copy", stringValue); 32 33 ViewLoader.ViewNode node = resourceLoader.getLayoutViewNode("android:layout/simple_spinner_item"); 34 assertNotNull(node); 35 } 36 37 @Test 38 public void shouldLoadLocalResources() throws Exception { 39 ResourceLoader resourceLoader = new ResourceLoader(10, R.class, resourceFile("res"), resourceFile("assets")); 40 String stringValue = resourceLoader.getStringValue(R.string.copy); 41 assertEquals("Local Copy", stringValue); 42 } 43 44 @Test(expected=I18nException.class) 45 public void shouldThrowExceptionOnI18nStrictModeInflateView() throws Exception { 46 ResourceLoader resourceLoader = new ResourceLoader(10, R.class, resourceFile("res"), resourceFile("layout")); 47 resourceLoader.setStrictI18n(true); 48 ViewGroup vg = new FrameLayout(Robolectric.application); 49 resourceLoader.inflateView(Robolectric.application, R.layout.text_views, vg); 50 } 51 52 @Test(expected=I18nException.class) 53 public void shouldThrowExceptionOnI18nStrictModeInflateMenu() throws Exception { 54 ResourceLoader resourceLoader = new ResourceLoader(10, R.class, resourceFile("res"), resourceFile("menu")); 55 resourceLoader.setStrictI18n(true); 56 resourceLoader.inflateMenu(Robolectric.application, R.menu.test, null); 57 } 58 59 @Test(expected=I18nException.class) 60 public void shouldThrowExceptionOnI18nStrictModeInflatePreferences() throws Exception { 61 ResourceLoader resourceLoader = new ResourceLoader(10, R.class, resourceFile("res"), resourceFile("xml")); 62 resourceLoader.setStrictI18n(true); 63 resourceLoader.inflatePreferences(Robolectric.application, R.xml.preferences); 64 } 65 66 @Test 67 public void testChoosesLayoutBasedOnSearchPath_respectsOrderOfPath() throws Exception { 68 ResourceLoader resourceLoader = new ResourceLoader(10, R.class, resourceFile("res"), resourceFile("layout")); 69 resourceLoader.setLayoutQualifierSearchPath("does-not-exist", "land", "xlarge"); 70 ViewGroup viewGroup = new FrameLayout(Robolectric.application); 71 ViewGroup view = (ViewGroup) resourceLoader.inflateView(Robolectric.application, R.layout.different_screen_sizes, viewGroup); 72 TextView textView = (TextView) view.findViewById(android.R.id.text1); 73 assertThat(textView.getText().toString(), equalTo("land")); 74 } 75 76 @Test 77 public void checkForPollution1() throws Exception { 78 checkForPollutionHelper(); 79 } 80 81 @Test 82 public void checkForPollution2() throws Exception { 83 checkForPollutionHelper(); 84 } 85 86 private void checkForPollutionHelper() { 87 ResourceLoader resourceLoader = Robolectric.getShadowApplication().getResourceLoader(); 88 ViewGroup viewGroup = new FrameLayout(Robolectric.application); 89 ViewGroup view = (ViewGroup) resourceLoader.inflateView(Robolectric.application, R.layout.different_screen_sizes, viewGroup); 90 TextView textView = (TextView) view.findViewById(android.R.id.text1); 91 assertThat(textView.getText().toString(), equalTo("default")); 92 resourceLoader.setLayoutQualifierSearchPath("land"); // testing if this pollutes the other test 93 } 94 95 @Test 96 public void shouldIdentifyNinePatchDrawables() { 97 ResourceLoader resourceLoader = Robolectric.getShadowApplication().getResourceLoader(); 98 99 assertThat(resourceLoader.isNinePatchDrawable(R.drawable.nine_patch_drawable), equalTo(true)); 100 assertThat(resourceLoader.isNinePatchDrawable(R.drawable.l2_yellow), equalTo(false)); 101 assertThat(resourceLoader.isNinePatchDrawable(R.drawable.state_drawable), equalTo(false)); 102 assertThat(resourceLoader.isNinePatchDrawable(R.drawable.animation_list), equalTo(false)); 103 assertThat(resourceLoader.isNinePatchDrawable(0), equalTo(false)); 104 assertThat(resourceLoader.isNinePatchDrawable(-1), equalTo(false)); 105 } 106} 107