1/*
2 * Copyright (C) 2017 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 */
16
17package com.android.setupwizardlib.robolectric;
18
19import org.junit.runner.notification.RunNotifier;
20import org.junit.runners.model.FrameworkMethod;
21import org.junit.runners.model.InitializationError;
22import org.robolectric.RobolectricTestRunner;
23import org.robolectric.annotation.Config;
24import org.robolectric.internal.ManifestFactory;
25
26public class SuwLibRobolectricTestRunner extends RobolectricTestRunner {
27
28    private String mModuleRootPath;
29
30    public SuwLibRobolectricTestRunner(Class<?> testClass) throws InitializationError {
31        super(testClass);
32    }
33
34    // Hack to determine the module root path in the build folder (e.g. out/gradle/setup-wizard-lib)
35    private void updateModuleRootPath(Config config) {
36        String moduleRoot = config.constants().getResource("").toString()
37                .replace("file:", "").replace("jar:", "");
38        mModuleRootPath =
39                moduleRoot.substring(0, moduleRoot.lastIndexOf("/build")) + "/setup-wizard-lib";
40    }
41
42    /**
43     * Return the default config used to run Robolectric tests.
44     */
45    @Override
46    protected Config buildGlobalConfig() {
47        Config parent = super.buildGlobalConfig();
48        updateModuleRootPath(parent);
49        return new Config.Builder(parent)
50                .setBuildDir(mModuleRootPath + "/build")
51                .build();
52    }
53
54    @Override
55    protected ManifestFactory getManifestFactory(Config config) {
56        return new PatchedGradleManifestFactory();
57    }
58
59    @Override
60    protected void runChild(FrameworkMethod method, RunNotifier notifier) {
61        System.out.println("===== Running " + method + " =====");
62        super.runChild(method, notifier);
63    }
64}
65