Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Robolectric does not support loading resources from an Android Library project such as ActionBarSherlock of FlyInMenu, so this patch allows you to specify a library project to load resources in tests.
// In ResourceLoader.java I added the following method:
//
public void loadLibraryProjectResources(File libraryProjectRoot) throws Exception {
File systemResourceDir = getSystemResourceDir(getPathToAndroidResources());
File localValueResourceDir = getValueResourceDir(libraryProjectRoot);
File systemValueResourceDir = getValueResourceDir(systemResourceDir);
loadStringResources(localValueResourceDir, systemValueResourceDir);
loadPluralsResources(localValueResourceDir, systemValueResourceDir);
loadValueResources(localValueResourceDir, systemValueResourceDir);
loadDimenResources(localValueResourceDir, systemValueResourceDir);
loadIntegerResource(localValueResourceDir, systemValueResourceDir);
loadViewResources(systemResourceDir, libraryProjectRoot);
loadMenuResources(libraryProjectRoot);
loadDrawableResources(libraryProjectRoot);
}
// Then you need a custom test runner such as:
public class MyTestRunner extends RobolectricTestRunner {
public MyTestRunner(Class<?> testClass) throws InitializationError {
super(testClass);
}
@Override public void beforeTest(Method method) {
try {
Robolectric.getShadowApplication().getResourceLoader().loadLibraryProjectResources(new File("submodules/flyinmenu/library/res"));
} catch (Exception e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment