@Override public URL getLocalArtifactUrl(DependencyJar dependency) { String depShortName = dependency.getShortName(); String path = properties.getProperty(depShortName); if (path != null) { File pathFile = new File(path); if (!pathFile.isAbsolute()) { pathFile = new File(baseDir.getPath(), path); } try { return pathFile.toURI().toURL(); } catch (MalformedURLException e) { throw new RuntimeException(e); } } else { if (delegate != null) { return delegate.getLocalArtifactUrl(dependency); } } throw new RuntimeException("no artifacts found for " + dependency); } }
private static Properties getProperties(FsFile propertiesFile) { Properties properties = new Properties(); // return an empty Properties object if the propertiesFile does not exist if (!propertiesFile.exists()) return properties; InputStream stream; try { stream = propertiesFile.getInputStream(); } catch (IOException e) { throw new RuntimeException(e); } try { try { properties.load(stream); } finally { stream.close(); } } catch (IOException e) { throw new RuntimeException(e); } return properties; }
FsFile baseDir = resDirectory.getParent(); final Properties properties = getProperties(baseDir.join("project.properties")); Properties overrideProperties = getProperties(baseDir.join("test-project.properties")); properties.putAll(overrideProperties); String lib; while ((lib = properties.getProperty("android.library.reference." + libRef)) != null) { FsFile libraryDir = baseDir.join(lib); if (libraryDir.isDirectory()) { FsFile[] libraryBaseDirFiles = libraryDir.listFiles(); if (libraryBaseDirFiles != null && libraryBaseDirFiles.length > 0) { List<ManifestIdentifier> libraries = findLibraries(libraryDir.join(Config.DEFAULT_RES_FOLDER)); libraryBaseDirs.add(new ManifestIdentifier( null, libraryDir.join(Config.DEFAULT_MANIFEST_NAME), libraryDir.join(Config.DEFAULT_RES_FOLDER), libraryDir.join(Config.DEFAULT_ASSET_FOLDER), libraries));
manifestFile = Fs.newFile(relativePath.toString()); } else { manifestFile = getBaseDir().join(manifestPath); final FsFile baseDir = manifestFile.getParent(); final FsFile resDir = baseDir.join(config.resourceDir()); final FsFile assetDir = baseDir.join(config.assetDir()); libraries = new ArrayList<>(); for (String libraryDirName : config.libraries()) { FsFile libDir = baseDir.join(libraryDirName); libraries.add(new ManifestIdentifier( null, libDir.join(Config.DEFAULT_MANIFEST_NAME), libDir.join(Config.DEFAULT_RES_FOLDER), libDir.join(Config.DEFAULT_ASSET_FOLDER), null));
public File getNativeLibraryPath() { String tempPath = System.getProperty("java.io.tmpdir"); if (tempPath == null) { throw new IllegalStateException("Java temporary directory is not defined (java.io.tmpdir)"); } return new File(Fs.fileFromPath(tempPath).join("robolectric-libs", getLibName()).getPath()); }
@Implementation protected final String[] list(String path) throws IOException { List<String> assetFiles = new ArrayList<>(); for (FsFile assetsDir : getAllAssetDirs()) { FsFile file; if (path.isEmpty()) { file = assetsDir; } else { file = assetsDir.join(path); } if (file.isDirectory()) { Collections.addAll(assetFiles, file.listFileNames()); } } return assetFiles.toArray(new String[assetFiles.size()]); }
@Implementation public final InputStream open(String fileName) throws IOException { return ShadowApplication.getInstance().getAppManifest().getAssetsDirectory().join(fileName).getInputStream(); }
protected AndroidManifest createAppManifest(FsFile manifestFile, FsFile resDir, FsFile assetDir, String packageName) { if (!manifestFile.exists()) { System.out.print("WARNING: No manifest file found at " + manifestFile.getPath() + "."); System.out.println("Falling back to the Android OS resources only."); System.out.println("To remove this warning, annotate your test class with @Config(manifest=Config.NONE)."); return null; } Logger.debug("Robolectric assets directory: " + assetDir.getPath()); Logger.debug(" Robolectric res directory: " + resDir.getPath()); Logger.debug(" Robolectric manifest path: " + manifestFile.getPath()); Logger.debug(" Robolectric package name: " + packageName); return new AndroidManifest(manifestFile, resDir, assetDir, packageName); }
/** * Returns a collection of resource IDs for all nine-patch drawables in the project. * * @param resourcePath Resource path. */ void findDrawableResources(ResourcePath resourcePath) { FsFile[] files = resourcePath.getResourceBase().listFiles(); if (files != null) { for (FsFile f : files) { if (f.isDirectory() && f.getName().startsWith("drawable")) { listDrawableResources(f, "drawable"); } else if (f.isDirectory() && f.getName().startsWith("mipmap")) { listDrawableResources(f, "mipmap"); } } } }
@Override public boolean accept(FsFile file) { return file.getName().startsWith(folderBaseName); } }
@HiddenApi @Implementation public final InputStream openNonAsset(int cookie, String fileName, int accessMode) throws IOException { final ResName resName = qualifyFromNonAssetFileName(fileName); final FileTypedResource typedResource = (FileTypedResource) resourceTable.getValue(resName, RuntimeEnvironment.getQualifiers()); if (typedResource == null) { throw new IOException("Unable to find resource for " + fileName); } if (accessMode == AssetManager.ACCESS_STREAMING) { return typedResource.getFsFile().getInputStream(); } else { return new ByteArrayInputStream(typedResource.getFsFile().getBytes()); } }
@Implementation protected static Typeface createFromAsset(AssetManager mgr, String path) { ShadowAssetManager shadowAssetManager = Shadow.extract(mgr); Collection<FsFile> assetDirs = shadowAssetManager.getAllAssetDirs(); for (FsFile assetDir : assetDirs) { FsFile[] files = assetDir.listFiles(new StartsWith(path)); FsFile assetFile = assetDir.join(path); if (assetFile.exists() || files.length != 0) { return createUnderlyingTypeface(path, Typeface.NORMAL); } } throw new RuntimeException("Font asset not found " + path); }
@Override protected AndroidManifest createAppManifest(FsFile manifestFile) { if (!manifestFile.exists()) { manifestFile = Fs.fileFromPath("pathToMy/AndroidManifest.xml"); } return super.createAppManifest(manifestFile); }
private void loadFile(FsFile dir) { if (!dir.exists()) { throw new RuntimeException("no such directory " + dir); } if (!dir.isDirectory()) { return; } Qualifiers qualifiers; try { qualifiers = Qualifiers.fromParentDir(dir); } catch (IllegalArgumentException e) { Logger.warn(dir + ": " + e.getMessage()); return; } for (FsFile file : dir.listFiles(ENDS_WITH_XML)) { loadResourceXmlFile(new XmlContext(packageName, file, qualifiers)); } }
@Override Collection<FsFile> getAllAssetDirs() { ArrayList<FsFile> fsFiles = new ArrayList<>(); for (AssetPath assetPath : cppAssetManager.getAssetPaths()) { if (assetPath.file.isFile()) { fsFiles.add(Fs.newJarFile(new File(assetPath.file.getPath())).join("assets")); } else { fsFiles.add(assetPath.file); } } return fsFiles; }
private Properties loadProperties(FsFile propertiesFile) throws IOException { final Properties properties = new Properties(); InputStream stream = propertiesFile.getInputStream(); properties.load(stream); stream.close(); return properties; }
private void listDrawableResources(FsFile dir, String type) { FsFile[] files = dir.listFiles(); if (files != null) { for (FsFile f : files) { String name = f.getName(); if (name.startsWith(".")) continue; String shortName; boolean isNinePatch; if (name.endsWith(".xml")) { // already handled, do nothing... continue; } else if (name.endsWith(".9.png")) { String[] tokens = name.split("\\.9\\.png$"); shortName = tokens[0]; isNinePatch = true; } else { shortName = f.getBaseName(); isNinePatch = false; } XmlContext fakeXmlContext = new XmlContext(resourceTable.getPackageName(), f); resourceTable.addResource(type, shortName, new FileTypedResource.Image(f, isNinePatch, fakeXmlContext)); } } } }