1/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package com.android.storagemanager.testing;
17
18import java.util.List;
19import org.junit.runners.model.InitializationError;
20import org.robolectric.RobolectricTestRunner;
21import org.robolectric.annotation.Config;
22import org.robolectric.manifest.AndroidManifest;
23import org.robolectric.res.Fs;
24import org.robolectric.res.ResourcePath;
25
26/**
27 * Custom test runner for Robolectric UI testing. This adds additional resources needed to run.
28 */
29public class StorageManagerRobolectricTestRunner extends RobolectricTestRunner {
30
31    /**
32     * We don't actually want to change this behavior, so we just call super.
33     */
34    public StorageManagerRobolectricTestRunner(Class<?> testClass) throws InitializationError {
35        super(testClass);
36    }
37
38    /**
39     * We are going to create our own custom manifest so that we can add multiple resource
40     * paths to it. This lets us access resources in both StorageManager and SettingsLib in our
41     * tests.
42     */
43    @Override
44    protected AndroidManifest getAppManifest(Config config) {
45        // Using the manifest file's relative path, we can figure out the application directory.
46        final String appRoot = "packages/apps/StorageManager";
47        final String manifestPath = appRoot + "/AndroidManifest.xml";
48        final String resDir = appRoot + "/res";
49        final String assetsDir = appRoot + "/assets";
50
51        // By adding any resources from libraries we need to the AndroidManifest, we can access
52        // them from within the parallel universe's resource loader.
53        final AndroidManifest manifest = new AndroidManifest(Fs.fileFromPath(manifestPath),
54                Fs.fileFromPath(resDir), Fs.fileFromPath(assetsDir)) {
55            @Override
56            public List<ResourcePath> getIncludedResourcePaths() {
57                List<ResourcePath> paths = super.getIncludedResourcePaths();
58                paths.add(new ResourcePath(
59                        getPackageName(),
60                        Fs.fileFromPath("./packages/apps/StorageManager/res"),
61                        null));
62                paths.add(new ResourcePath(
63                        getPackageName(),
64                        Fs.fileFromPath("./frameworks/base/packages/SettingsLib/res"),
65                        null));
66                paths.add(new ResourcePath(
67                        getPackageName(),
68                        Fs.fileFromPath("./frameworks/base/core/res/res"),
69                        null));
70                return paths;
71            }
72        };
73
74        manifest.setPackageName("com.android.storagemanager");
75        return manifest;
76    }
77}