1 package org.robolectric.res; 2 3 import static com.google.common.truth.Truth.assertThat; 4 import static org.mockito.Mockito.mock; 5 import static org.mockito.Mockito.when; 6 7 import java.io.File; 8 import java.lang.reflect.Field; 9 import org.junit.After; 10 import org.junit.Before; 11 import org.junit.Test; 12 import org.junit.runner.RunWith; 13 import org.junit.runners.JUnit4; 14 import org.robolectric.res.android.ResTable_config; 15 import org.robolectric.util.ReflectionHelpers; 16 17 @RunWith(JUnit4.class) 18 public class DrawableResourceLoaderNoRunnerTest { 19 private static final String JAR_SEPARATOR = "/"; 20 private static final String UNIX_SEPARATOR = "/"; 21 private static final String WINDOWS_SEPARATOR = "\\"; 22 private static final String JAR_SCHEME = "jar:"; 23 private static final String DRAWABLE_DIR = "drawable"; 24 private static final String FILE_PATH_ON_UNIX = UNIX_SEPARATOR + "foo" 25 + UNIX_SEPARATOR + DRAWABLE_DIR; 26 private static final String FILE_PATH_ON_WINDOWS = "C:" 27 + WINDOWS_SEPARATOR + "foo" 28 + WINDOWS_SEPARATOR + DRAWABLE_DIR; 29 private static final String JAR_PATH_ON_UNIX = JAR_SCHEME + UNIX_SEPARATOR + "foo" 30 + UNIX_SEPARATOR + "bar.jar!" 31 + JAR_SEPARATOR + DRAWABLE_DIR; 32 private static final String JAR_PATH_ON_WINDOWS = JAR_SCHEME + "C:" 33 + WINDOWS_SEPARATOR + "foo" 34 + WINDOWS_SEPARATOR + "bar.jar!" 35 + JAR_SEPARATOR + DRAWABLE_DIR; 36 37 private String originalSeparator; 38 private PackageResourceTable resourceTable; 39 40 @Before 41 public void setUp() { 42 resourceTable = new ResourceTableFactory().newResourceTable("org.robolectric"); 43 } 44 45 @After 46 public void tearDown() throws Exception { 47 if (originalSeparator != null) { 48 Field field = File.class.getDeclaredField("separator"); 49 ReflectionHelpers.setStaticField(field, originalSeparator); 50 originalSeparator = null; 51 } 52 } 53 54 @Test 55 public void shouldFindDrawableResourcesWorkWithUnixJarFilePath() throws Exception { 56 setFileSeparator(UNIX_SEPARATOR); 57 58 Fs.JarFs.JarFsFile mockTestFile = mock(Fs.JarFs.JarFsFile.class); 59 when(mockTestFile.getName()).thenReturn("foo.png"); 60 when(mockTestFile.getBaseName()).thenReturn("foo"); 61 62 Fs.JarFs.JarFsFile mockTestDir = mock(Fs.JarFs.JarFsFile.class); 63 when(mockTestDir.toString()).thenReturn(JAR_PATH_ON_UNIX); 64 when(mockTestDir.getName()).thenReturn(DRAWABLE_DIR); 65 when(mockTestDir.listFiles()).thenReturn(new FsFile[]{mockTestFile}); 66 when(mockTestDir.isDirectory()).thenReturn(true); 67 FsFile mockTestBaseDir = mock(FsFile.class); 68 when(mockTestBaseDir.listFiles()).thenReturn(new FsFile[]{mockTestDir}); 69 ResourcePath resourcePath = new ResourcePath(null, mockTestBaseDir, null); 70 71 DrawableResourceLoader testLoader = new DrawableResourceLoader(resourceTable); 72 testLoader.findDrawableResources(resourcePath); 73 74 assertThat(resourceTable.getValue(new ResName("org.robolectric", "drawable", "foo"), new ResTable_config()).isFile()).isTrue(); 75 } 76 77 @Test 78 public void shouldFindDrawableResourcesWorkWithUnixFilePath() throws Exception { 79 setFileSeparator(UNIX_SEPARATOR); 80 81 FileFsFile mockTestFile = mock(FileFsFile.class); 82 when(mockTestFile.getName()).thenReturn("foo.png"); 83 when(mockTestFile.getBaseName()).thenReturn("foo"); 84 85 FileFsFile mockTestDir = mock(FileFsFile.class); 86 when(mockTestDir.toString()).thenReturn(FILE_PATH_ON_UNIX); 87 when(mockTestDir.getName()).thenReturn(DRAWABLE_DIR); 88 when(mockTestDir.listFiles()).thenReturn(new FsFile[]{mockTestFile}); 89 when(mockTestDir.isDirectory()).thenReturn(true); 90 FsFile mockTestBaseDir = mock(FsFile.class); 91 when(mockTestBaseDir.listFiles()).thenReturn(new FsFile[]{mockTestDir}); 92 ResourcePath resourcePath = new ResourcePath(null, mockTestBaseDir, null); 93 94 DrawableResourceLoader testLoader = new DrawableResourceLoader(resourceTable); 95 testLoader.findDrawableResources(resourcePath); 96 97 assertThat(resourceTable.getValue(new ResName("org.robolectric", "drawable", "foo"), new ResTable_config()).isFile()).isTrue(); 98 } 99 100 @Test 101 public void shouldFindDrawableResourcesWorkWithWindowsJarFilePath() throws Exception { 102 setFileSeparator(WINDOWS_SEPARATOR); 103 104 Fs.JarFs.JarFsFile mockTestFile = mock(Fs.JarFs.JarFsFile.class); 105 when(mockTestFile.getName()).thenReturn("foo.png"); 106 when(mockTestFile.getBaseName()).thenReturn("foo"); 107 108 Fs.JarFs.JarFsFile mockTestDir = mock(Fs.JarFs.JarFsFile.class); 109 when(mockTestDir.toString()).thenReturn(JAR_PATH_ON_WINDOWS); 110 when(mockTestDir.getName()).thenReturn(DRAWABLE_DIR); 111 when(mockTestDir.listFiles()).thenReturn(new FsFile[]{mockTestFile}); 112 when(mockTestDir.isDirectory()).thenReturn(true); 113 FsFile mockTestBaseDir = mock(FsFile.class); 114 when(mockTestBaseDir.listFiles()).thenReturn(new FsFile[]{mockTestDir}); 115 ResourcePath resourcePath = new ResourcePath(null, mockTestBaseDir, null); 116 117 DrawableResourceLoader testLoader = new DrawableResourceLoader(resourceTable); 118 testLoader.findDrawableResources(resourcePath); 119 120 assertThat(resourceTable.getValue(new ResName("org.robolectric", "drawable", "foo"), new ResTable_config()).isFile()).isTrue(); 121 } 122 123 @Test 124 public void shouldFindDrawableResourcesWorkWithWindowsFilePath() throws Exception { 125 setFileSeparator(WINDOWS_SEPARATOR); 126 127 FileFsFile mockTestFile = mock(FileFsFile.class); 128 when(mockTestFile.getName()).thenReturn("foo.png"); 129 when(mockTestFile.getBaseName()).thenReturn("foo"); 130 131 FileFsFile mockTestDir = mock(FileFsFile.class); 132 when(mockTestDir.toString()).thenReturn(FILE_PATH_ON_WINDOWS); 133 when(mockTestDir.getName()).thenReturn(DRAWABLE_DIR); 134 when(mockTestDir.listFiles()).thenReturn(new FsFile[]{mockTestFile}); 135 when(mockTestDir.isDirectory()).thenReturn(true); 136 FsFile mockTestBaseDir = mock(FsFile.class); 137 when(mockTestBaseDir.listFiles()).thenReturn(new FsFile[]{mockTestDir}); 138 ResourcePath resourcePath = new ResourcePath(null, mockTestBaseDir, null); 139 140 DrawableResourceLoader testLoader = new DrawableResourceLoader(resourceTable); 141 testLoader.findDrawableResources(resourcePath); 142 143 assertThat(resourceTable.getValue(new ResName("org.robolectric", "drawable", "foo"), new ResTable_config()).isFile()).isTrue(); 144 } 145 146 private void setFileSeparator(String separator) throws Exception { 147 Field field = File.class.getDeclaredField("separator"); 148 originalSeparator = ReflectionHelpers.getStaticField(field); 149 ReflectionHelpers.setStaticField(field, separator); 150 } 151 } 152