@Test public void shouldMatchPermissions() { // Initialize: configure test for current app configuration FsFile mergedManifestFile; Object[] expectedPermissions; if (BuildConfig.DEBUG) { mergedManifestFile = Fs.fileFromPath(MERGED_DEBUG_MANIFEST_FILE); expectedPermissions = EXPECTED_DEBUG_PERMISSIONS; } else { mergedManifestFile = Fs.fileFromPath(MERGED_RELEASE_MANIFEST_FILE); expectedPermissions = EXPECTED_RELEASE_PERMISSIONS; } // Run: Creates a Robolectric configuration using specified manifest file AndroidManifest manifest = new AndroidManifest(mergedManifestFile, null, null); // Check: manifest file should contain only expected permissions assertThat(manifest.getUsedPermissions()) .containsExactly(expectedPermissions); }
@Override protected Properties getConfigProperties() { Properties configProperties = super.getConfigProperties(); if (configProperties == null) { configProperties = new Properties(); } String manifestPath = "src/test/AndroidManifest.xml"; FsFile manifestFile = Fs.newFile(new File(manifestPath)); if (manifestFile.exists()) { configProperties.put("manifest", manifestPath); } else { configProperties.put("manifest", "LikeOrmExample/" + manifestPath); } configProperties.put("emulateSdk", "16"); return configProperties; }
@Override protected AndroidManifest createAppManifest(FsFile manifestFile, FsFile resDir, FsFile assetsDir) { return new AndroidManifest( Fs.fileFromPath("src/main/AndroidManifest.xml"), Fs.fileFromPath("src/main/res"), Fs.fileFromPath("src/main/assets")) { @Override public int getTargetSdkVersion() { return 18; } @Override public String getThemeRef(Class<? extends Activity> activityClass) { return "@android:style/Theme.Holo.Light.NoActionBar"; } }; }
TypedResource getAndResolve(@NotNull ResName resName, String qualifiers, boolean resolveRefs) { TypedResource value = resourceLoader.getValue(resName, qualifiers); if (resolveRefs) { value = resolve(value, qualifiers, resName); } // todo: make the drawable loader put stuff into the normal spot... if (value == null && DrawableResourceLoader.isStillHandledHere(resName)) { DrawableNode drawableNode = resourceLoader.getDrawableNode(resName, qualifiers); return new TypedResource<FsFile>(drawableNode.getFsFile(), ResType.FILE); } // todo: gross. this is so resources.getString(R.layout.foo) works for ABS. if (value == null && "layout".equals(resName.type)) { throw new UnsupportedOperationException("ugh, this doesn't work still?"); } return value; }
@Test public void whenNoAppManifest_shouldRunThingsInTheRightOrder() throws Exception { StateHolder.transcript = new Transcript(); assertNoFailures(run(new Runner(SimpleTest.class) { @Override protected AndroidManifest createAppManifest(FsFile manifestFile, FsFile resDir, FsFile assetsDir) { return null; } })); StateHolder.transcript.assertEventsSoFar( "configureShadows", "createApplication", "application.onCreate", "beforeTest", "application.beforeTest", "prepareTest", "application.prepareTest", "TEST!", "application.onTerminate", "afterTest", "application.afterTest" ); }
protected List<FsFile> findLibraries() { FsFile baseDir = getBaseDir(); List<FsFile> libraryBaseDirs = new ArrayList<FsFile>(); Properties properties = getProperties(baseDir.join("project.properties")); // get the project.properties overrides and apply them (if any) Properties overrideProperties = getProperties(baseDir.join("test-project.properties")); if (overrideProperties!=null) properties.putAll(overrideProperties); if (properties != null) { int libRef = 1; String lib; while ((lib = properties.getProperty("android.library.reference." + libRef)) != null) { FsFile libraryBaseDir = baseDir.join(lib); if (libraryBaseDir.isDirectory()) { // Ignore directories without any files FsFile[] libraryBaseDirFiles = libraryBaseDir.listFiles(); if (libraryBaseDirFiles != null && libraryBaseDirFiles.length > 0) { libraryBaseDirs.add(libraryBaseDir); } } libRef++; } } return libraryBaseDirs; }
@Override protected AndroidManifest getAppManifest(Config config) { AndroidManifest appManifest = super.getAppManifest(config); FsFile androidManifestFile = appManifest.getAndroidManifestFile(); if (androidManifestFile.exists()) { return appManifest; } else { androidManifestFile = FileFsFile.from(appManifest.getAndroidManifestFile().getPath() .replace("manifests/full", "manifests/aapt")); return new AndroidManifest(androidManifestFile, appManifest.getResDirectory(), appManifest.getAssetsDirectory()); } }
protected AndroidManifest getAppManifest(Config config) { AndroidManifest appManifest = super.getAppManifest(config); FsFile androidManifestFile = appManifest.getAndroidManifestFile(); if (androidManifestFile.exists()) { return appManifest; } else { String moduleRoot = getModuleRootPath(config); androidManifestFile = FileFsFile.from(moduleRoot, appManifest.getAndroidManifestFile().getPath().replace("bundles", "manifests/full")); FsFile resDirectory = FileFsFile.from(moduleRoot, appManifest.getResDirectory().getPath()); FsFile assetsDirectory = FileFsFile.from(moduleRoot, appManifest.getAssetsDirectory().getPath()); return new AndroidManifest(androidManifestFile, resDirectory, assetsDirectory); } }
@Override protected AndroidManifest createAppManifest(final FsFile manifestFile, final FsFile resDir, final FsFile assetsDir) { FsFile moduleManifestFile = fixModuleDirectoryPathIfNeeded(manifestFile); FsFile moduleResDir = fixModuleDirectoryPathIfNeeded(resDir); FsFile moduleAssetsDir = fixModuleDirectoryPathIfNeeded(assetsDir); return super.createAppManifest(moduleManifestFile, moduleResDir, moduleAssetsDir); }
protected AndroidManifest getAppManifest(Config config) { AndroidManifest appManifest = super.getAppManifest(config); FsFile androidManifestFile = appManifest.getAndroidManifestFile(); if (androidManifestFile.exists()) { return appManifest; } else { String moduleRoot = getModuleRootPath(config); androidManifestFile = FileFsFile.from(moduleRoot, appManifest.getAndroidManifestFile().getPath().replace("bundles", "manifests/full")); FsFile resDirectory = FileFsFile.from(moduleRoot, appManifest.getResDirectory().getPath().replace("/res", "").replace("bundles", "res")); FsFile assetsDirectory = FileFsFile.from(moduleRoot, appManifest.getAssetsDirectory().getPath().replace("/assets", "").replace("bundles", "assets")); return new AndroidManifest(androidManifestFile, resDirectory, assetsDirectory); } }
@Override protected AndroidManifest getAppManifest(Config config) { String buckManifest = System.getProperty(ROBOLECTRIC_MANIFEST); String buckResourcesProperty = System.getProperty(ROBOLECTRIC_RESOURCE_DIRECTORIES); if (buckManifest != null && buckResourcesProperty != null) { final List<String> buckResources = Arrays.asList(buckResourcesProperty.split(File.pathSeparator)); final FsFile res = Fs.fileFromPath(buckResources.get(buckResources.size() - 1)); final FsFile assets = Fs.fileFromPath(buckResources.get(buckResources.size() - 1)); final FsFile manifest = Fs.fileFromPath(buckManifest); return new AndroidManifest(manifest, res, assets, config.packageName()) { @Override public List<ResourcePath> getIncludedResourcePaths() { Collection<ResourcePath> resourcePaths = new LinkedHashSet<>(); resourcePaths.add(super.getResourcePath()); ListIterator<String> it = buckResources.listIterator(buckResources.size()); while (it.hasPrevious()) { resourcePaths.add(new ResourcePath( getRClass(), getPackageName(), Fs.fileFromPath(it.previous()), getAssetsDirectory())); } return new ArrayList<>(resourcePaths); } }; } else { return null; } }
protected AndroidManifest createAppManifest(FsFile manifestFile, FsFile resDir, FsFile assetsDir) { 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; } AndroidManifest manifest = new AndroidManifest(manifestFile, resDir, assetsDir); String packageName = System.getProperty("android.package"); manifest.setPackageName(packageName); return manifest; }
protected AndroidManifest getAppManifest(Config config) { if (config.manifest().equals(Config.NONE)) { return null; } String manifestProperty = System.getProperty("android.manifest"); String resourcesProperty = System.getProperty("android.resources"); String assetsProperty = System.getProperty("android.assets"); FsFile fsFile = getBaseDir(); FsFile manifestFile; FsFile resDir; FsFile assetsDir; boolean defaultManifest = config.manifest().equals(Config.DEFAULT); if (defaultManifest && manifestProperty != null) { manifestFile = Fs.fileFromPath(manifestProperty); resDir = Fs.fileFromPath(resourcesProperty); assetsDir = Fs.fileFromPath(assetsProperty); } else { manifestFile = fsFile.join(defaultManifest ? "AndroidManifest.xml" : config.manifest()); resDir = manifestFile.getParent().join(config.resourceDir()); assetsDir = manifestFile.getParent().join("assets"); } synchronized (envHolder) { AndroidManifest appManifest; appManifest = envHolder.appManifestsByFile.get(manifestFile); if (appManifest == null) { long startTime = System.currentTimeMillis(); appManifest = createAppManifest(manifestFile, resDir, assetsDir); if (DocumentLoader.DEBUG_PERF) System.out.println(String.format("%4dms spent in %s", System.currentTimeMillis() - startTime, manifestFile)); envHolder.appManifestsByFile.put(manifestFile, appManifest); } return appManifest; } }
@Implementation public final String[] list(String path) throws IOException { FsFile file = appManifest.getAssetsDirectory().join(path); if (file.isDirectory()) { return file.listFileNames(); } return new String[0]; }
public static XmlResourceParser getXmlResourceParser(String file, String packageName, ResourceIndex resourceIndex) { FsFile fsFile = Fs.fileFromPath(file); Document document = new XmlFileLoader(null, "xml").parse(fsFile); if (document == null) { throw new Resources.NotFoundException("couldn't find resource " + fsFile.getPath()); } return new XmlFileBuilder().getXml(document, fsFile.getPath(), packageName, resourceIndex); }
protected AndroidManifest getAppManifest(Config config) { AndroidManifest appManifest = super.getAppManifest(config); FsFile androidManifestFile = appManifest.getAndroidManifestFile(); if (androidManifestFile.exists()) { return appManifest; } else { androidManifestFile = FileFsFile.from(MODULE_ROOT, "src/main/AndroidManifest.xml"); FsFile resDirectory = FileFsFile.from(MODULE_ROOT, "src/main/res"); FsFile assetsDirectory = FileFsFile.from(MODULE_ROOT, "src/main/assets"); return new AndroidManifest(androidManifestFile, resDirectory, assetsDirectory); } }
protected AndroidManifest getAppManifest(Config config) { AndroidManifest appManifest = super.getAppManifest(config); FsFile androidManifestFile = appManifest.getAndroidManifestFile(); if (androidManifestFile.exists()) { return appManifest; } else { String moduleRoot = getModuleRootPath(config); androidManifestFile = FileFsFile.from(moduleRoot, appManifest.getAndroidManifestFile().getPath()); FsFile resDirectory = FileFsFile.from(moduleRoot, appManifest.getAndroidManifestFile().getPath().replace("AndroidManifest.xml", "res")); FsFile assetsDirectory = FileFsFile.from(moduleRoot, appManifest.getAndroidManifestFile().getPath().replace("AndroidManifest.xml", "assets")); return new AndroidManifest(androidManifestFile, resDirectory, assetsDirectory); } }
/** * Creates a Robolectric configuration using specified locations. * * @param androidManifestFile location of the AndroidManifest.xml file * @param resDirectory location of the res directory * @param assetsDirectory location of the assets directory */ public MapzenAndroidManifest(FsFile androidManifestFile, FsFile resDirectory, FsFile assetsDirectory) { super(androidManifestFile, resDirectory, assetsDirectory); this.androidManifestFile = androidManifestFile; this.resDirectory = resDirectory; this.assetsDirectory = assetsDirectory; }
protected void createLibraryManifests() { libraryManifests = new ArrayList<AndroidManifest>(); if (libraryDirectories == null) { libraryDirectories = findLibraries(); } for (FsFile libraryBaseDir : libraryDirectories) { MapzenAndroidManifest libraryManifest = createLibraryAndroidManifest(libraryBaseDir); libraryManifest.createLibraryManifests(); libraryManifests.add(libraryManifest); } }
/** * Uses custom manifest as workaround to maintain backward compatibility with library projects * that do not yet include the <code><application/></code> tag in AndroidManifest.xml. * <p /> * See https://github.com/robolectric/robolectric/pull/1309 for more info. */ @Override protected AndroidManifest createAppManifest(FsFile manifestFile, FsFile resDir, FsFile assetsDir) { AndroidManifest manifest = new MapzenAndroidManifest(manifestFile, resDir, assetsDir); String packageName = System.getProperty("android.package"); manifest.setPackageName(packageName); return manifest; }
protected AndroidManifest getAppManifest(Config config) { AndroidManifest appManifest = super.getAppManifest(config); FsFile androidManifestFile = appManifest.getAndroidManifestFile(); FsFile resDirectory; FsFile assetsDirectory; String moduleRoot = getModuleRootPath(config); androidManifestFile = FileFsFile.from(moduleRoot, appManifest.getAndroidManifestFile().getPath().replace("bundles", "manifests/aapt")); if(appManifest.getResDirectory().getPath().contains("release")) { resDirectory = FileFsFile.from(moduleRoot, appManifest.getResDirectory().getPath().replace("release", "default")); assetsDirectory = FileFsFile.from(moduleRoot, appManifest.getAssetsDirectory().getPath().replace("release", "default")); }else{ resDirectory = FileFsFile.from(moduleRoot, appManifest.getResDirectory().getPath().replace("bundles/debug/res", "res/merged/debug")); assetsDirectory = FileFsFile.from(moduleRoot, appManifest.getAssetsDirectory().getPath()); } System.out.print(androidManifestFile.getPath() + '\n'); System.out.print(resDirectory.getPath() + '\n'); System.out.print(assetsDirectory.getPath() + '\n'); return new AndroidManifest(androidManifestFile, resDirectory, assetsDirectory); }
protected AndroidManifest getAppManifest(Config config) { AndroidManifest appManifest = super.getAppManifest(config); FsFile androidManifestFile = appManifest.getAndroidManifestFile(); FsFile resDirectory; FsFile assetsDirectory; String moduleRoot = getModuleRootPath(config); androidManifestFile = FileFsFile.from(moduleRoot, appManifest.getAndroidManifestFile().getPath().replace("bundles", "manifests/aapt")); if(appManifest.getResDirectory().getPath().contains("release")) { resDirectory = FileFsFile.from(moduleRoot, appManifest.getResDirectory().getPath().replace("release", "default")); assetsDirectory = FileFsFile.from(moduleRoot, appManifest.getAssetsDirectory().getPath().replace("release", "default")); }else{ resDirectory = FileFsFile.from(moduleRoot, appManifest.getResDirectory().getPath()); assetsDirectory = FileFsFile.from(moduleRoot, appManifest.getAssetsDirectory().getPath()); } System.out.print(androidManifestFile.getPath() + '\n'); System.out.print(resDirectory.getPath() + '\n'); System.out.print(assetsDirectory.getPath() + '\n'); return new AndroidManifest(androidManifestFile, resDirectory, assetsDirectory); }
private FsFile fixModuleDirectoryPath(final FsFile fsFile) { return Fs.newFile(new File(fsFile.getPath().replace("./", "./" + MODULE_NAME + "/"))); }
private FsFile fixModuleDirectoryPathIfNeeded(final FsFile fsFile) { if (!directoryPathsNeedFixing()) { return fsFile; } return fixModuleDirectoryPath(fsFile); }
protected FsFile getBaseDir() { return Fs.currentDirectory(); }
@Override public void fillTypedValue(FsFile data, TypedValue typedValue) { typedValue.type = TypedValue.TYPE_STRING; typedValue.data = 0; typedValue.string = data.getPath(); typedValue.assetCookie = getNextStringCookie(); }
public FsFile getAssetsDirectory() { return appManifest.getAssetsDirectory(); }
@Override protected AndroidManifest createAppManifest(FsFile manifestFile, FsFile resDir, FsFile assetsDir) { return new AndroidManifest(resourceFile("TestAndroidManifest.xml"), resourceFile("res"), resourceFile("assets")); }
@Override protected AndroidManifest createAppManifest(FsFile manifestFile, FsFile resDir, FsFile assetsDir) { return null; }
public static FsFile resourcesBaseDir() { return Fs.newFile(resourcesBaseDirFile()); }
public static FsFile resourceFile(String... pathParts) { return resourcesBaseDir().join(pathParts); }
@Override protected AndroidManifest getAppManifest(Config config) { FsFile baseDir = Fs.newFile(new File(getClass().getResource("/").getPath())); return new AndroidManifest(baseDir.join("AndroidManifest.xml"), baseDir.join("/res"),baseDir.join("asserts")); }
public MapzenAndroidManifest(final FsFile androidManifestFile, final FsFile resDirectory) { this(androidManifestFile, resDirectory, resDirectory.getParent().join(DEFAULT_ASSETS_FOLDER)); }
/** * @deprecated Use {@link #MapzenAndroidManifest(org.robolectric.res.FsFile, org.robolectric.res.FsFile, org.robolectric.res.FsFile)} instead.} */ public MapzenAndroidManifest(final FsFile baseDir) { this(baseDir.join(DEFAULT_MANIFEST_NAME), baseDir.join(DEFAULT_RES_FOLDER), baseDir.join(DEFAULT_ASSETS_FOLDER)); }
public void setLibraryDirectories(List<FsFile> libraryDirectories) { this.libraryDirectories = libraryDirectories; }
protected FsFile getBaseDir() { return getResDirectory().getParent(); }