1 package com.xtremelabs.robolectric.shadows; 2 3 import android.content.res.AssetManager; 4 import com.xtremelabs.robolectric.internal.Implementation; 5 import com.xtremelabs.robolectric.internal.Implements; 6 import com.xtremelabs.robolectric.res.ResourceLoader; 7 8 import java.io.File; 9 import java.io.FileInputStream; 10 import java.io.IOException; 11 import java.io.InputStream; 12 13 import static com.xtremelabs.robolectric.Robolectric.shadowOf; 14 15 @SuppressWarnings({"UnusedDeclaration"}) 16 @Implements(AssetManager.class) 17 public final class ShadowAssetManager { 18 static AssetManager bind(AssetManager assetManager, ResourceLoader resourceLoader) { 19 ShadowAssetManager shadowAssetManager = shadowOf(assetManager); 20 if (shadowAssetManager.resourceLoader != null) throw new RuntimeException("ResourceLoader already set!"); 21 shadowAssetManager.resourceLoader = resourceLoader; 22 return assetManager; 23 } 24 25 private ResourceLoader resourceLoader; 26 27 @Implementation 28 public final String[] list(String path) throws IOException { 29 File file = new File(resourceLoader.getAssetsBase(), path); 30 if (file.isDirectory()) { 31 return file.list(); 32 } 33 return new String[0]; 34 } 35 36 @Implementation 37 public final InputStream open(String fileName) throws IOException { 38 return new FileInputStream(new File(resourceLoader.getAssetsBase(), fileName)); 39 } 40 41 } 42