Skip to content

Instantly share code, notes, and snippets.

@pplante
Created August 28, 2012 20:23
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pplante/3503788 to your computer and use it in GitHub Desktop.
Save pplante/3503788 to your computer and use it in GitHub Desktop.
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();
}
}
}
@martijndebruijn
Copy link

Where does the getSystemResourceDir comes from? Could you please provide the complete java file including the imports?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment