Using multiple res folders with Robolectric

后端 未结 2 1682
滥情空心
滥情空心 2020-12-07 05:48

My current Gradle configuration has multiple (Merged) res folders:

sourceSets {
    androidTest {
        setRoot(\'src/test\')
    }
    main {
        res.         


        
相关标签:
2条回答
  • 2020-12-07 05:57

    Ok, this is the easiest way to do it, You will have to extend RobolectricTestRunner getAppManifest and createAppResourceLoader.

    In getAppManifest you will simply have to store the manifest in a field, let's say mDefaultManifest.

    In createAppResourceLoader you will have to add the right resources injected.

    /**
     * TODO: Watch OUT this is copied from RobolectricTestRunner in Robolectric-2.4 keep it up to date!
     */
    @Override
    protected ResourceLoader createAppResourceLoader(ResourceLoader systemResourceLoader, AndroidManifest appManifest) {
        List<PackageResourceLoader> appAndLibraryResourceLoaders = new ArrayList<PackageResourceLoader>();
        for (ResourcePath resourcePath : appManifest.getIncludedResourcePaths()) {
            appAndLibraryResourceLoaders.add(createResourceLoader(resourcePath));
        }
    
            /* BEGIN EDIT */
            if(mDefaultManifest != null) {
                ResourcePath rpInjected = new ResourcePath(mDefaultManifest.getRClass(), mDefaultManifest.getPackageName(), Fs.fileFromPath("../app/src/main/res/features/registration"), mDefaultManifest.getAssetsDirectory());
                appAndLibraryResourceLoaders.add(createResourceLoader(rpInjected));
                rpInjected = new ResourcePath(mDefaultManifest.getRClass(), mDefaultManifest.getPackageName(), Fs.fileFromPath("../app/src/main/res/features/login"), mDefaultManifest.getAssetsDirectory());
                appAndLibraryResourceLoaders.add(createResourceLoader(rpInjected));
            }
            /* END EDIT */
    
        OverlayResourceLoader overlayResourceLoader = new OverlayResourceLoader(appManifest.getPackageName(), appAndLibraryResourceLoaders);
    
        Map<String, ResourceLoader> resourceLoaders = new HashMap<String, ResourceLoader>();
        resourceLoaders.put("android", systemResourceLoader);
        resourceLoaders.put(appManifest.getPackageName(), overlayResourceLoader);
        return new RoutingResourceLoader(resourceLoaders);
    }
    

    Do not forget to add @RunWith(YourTestRunner.class) in your test classes.

    0 讨论(0)
  • 2020-12-07 06:01

    Another solution similar to Luca's:

    public class MyTestRunner extends RobolectricTestRunner {
        ...
        @Override
        protected AndroidManifest getAppManifest(Config config) {
            String appRoot = "./src/main/";
            String manifestPath = appRoot + "AndroidManifest.xml";
            String resDir = appRoot + "res";
            String assetsDir = appRoot + "assets";
    
            return new AndroidManifest(Fs.fileFromPath(manifestPath), Fs.fileFromPath(resDir), Fs.fileFromPath(assetsDir)) {
                @Override
                public List<ResourcePath> getIncludedResourcePaths() {
                    List<ResourcePath> paths = super.getIncludedResourcePaths();
                    paths.add(new ResourcePath(getRClass(), getPackageName(), Fs.fileFromPath("../app/src/main/res/features/registration"), getAssetsDirectory()));
                    paths.add(new ResourcePath(getRClass(), getPackageName(), Fs.fileFromPath("../app/src/main/res/features/login"), getAssetsDirectory()));
                    return paths;
                }
            };
        }
    }
    

    Don't forget to annotate your tests with @RunWith(MyTestRunner.class)

    0 讨论(0)
提交回复
热议问题