Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christopherperry/3541673 to your computer and use it in GitHub Desktop.
Save christopherperry/3541673 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();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment