Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 112aa3c5 authored by Fan Zhang's avatar Fan Zhang
Browse files

Use standard test runner for robolectric tests

Test: rerun tests
Merged-In: I776b85a10854b7324b65227842b731348e055ae3
Change-Id: I776b85a10854b7324b65227842b731348e055ae3
parent 04c08165
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -4,7 +4,7 @@
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)

LOCAL_SRC_FILES := $(call all-java-files-under, runners/android_mk src)
LOCAL_SRC_FILES := $(call all-java-files-under, src)

# Include the testing libraries (JUnit4 + Robolectric libs).
LOCAL_STATIC_JAVA_LIBRARIES := \
+0 −64
Original line number Diff line number Diff line
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.settings.intelligence;

import org.junit.runners.model.InitializationError;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.manifest.AndroidManifest;
import org.robolectric.res.Fs;

/**
 * Custom test runner for dealing with resources from multiple sources. This is needed because the
 * default behavior for robolectric is just to grab the resource directory in the target package. We
 * want to override this to add several spanning different projects/libraries.
 */
public class SettingsIntelligenceRobolectricTestRunner extends RobolectricTestRunner {

    /** We don't actually want to change this behavior, so we just call super. */
    public SettingsIntelligenceRobolectricTestRunner(Class<?> testClass)
            throws InitializationError {
        super(testClass);
    }

    /**
     * We are going to create our own custom manifest so that we can add multiple resource paths to
     * it. This lets us access resources in different projects in our tests as well as use a test
     * res directory.
     */
    @Override
    protected AndroidManifest getAppManifest(Config config) {
        // Using the manifest file's relative path, we can figure out the application directory.
        final String appRoot = "packages/apps/SettingsIntelligence";
        final String manifestPath = appRoot + "/AndroidManifest.xml";
        final String resDir = appRoot + "tests/robotests/res";
        final String assetsDir = appRoot + config.assetDir();

        // By adding any resources from libraries we need to the AndroidManifest, we can access
        // them from within the parallel universe's resource loader.
        final AndroidManifest manifest =
                new AndroidManifest(
                        Fs.fileFromPath(manifestPath),
                        Fs.fileFromPath(resDir),
                        Fs.fileFromPath(assetsDir)) {
                };

        // Set the package name to the renamed one
        manifest.setPackageName("com.android.settings.intelligence");
        return manifest;
    }
}
+0 −26
Original line number Diff line number Diff line
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.settings.intelligence;

/**
 * Constants for Robolectric config.
 */
public class TestConfig {
    public static final int SDK_VERSION = 23;
    public static final String MANIFEST_PATH =
            "packages/apps/SettingsIntelligence/Turbo/AndroidManifest.xml";
}
+0 −36
Original line number Diff line number Diff line
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.settings.intelligence;

import org.junit.runners.model.InitializationError;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;

public class SettingsIntelligenceRobolectricTestRunner extends RobolectricTestRunner {

    /**
     * Creates a runner to run {@code testClass}. Looks in your working directory for your
     * AndroidManifest.xml file
     * and res directory by default. Use the {@link Config} annotation to configure.
     *
     * @param testClass the test class to be run
     * @throws InitializationError if junit says so
     */
    public SettingsIntelligenceRobolectricTestRunner(Class<?> testClass)
            throws InitializationError {
        super(testClass);
    }
}
+5 −3
Original line number Diff line number Diff line
@@ -13,11 +13,13 @@
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.settings.intelligence;

/**
 * Constants for Robolectric config.
 */
public class TestConfig {

    public static final String MANIFEST_PATH = "--default";
    public static final int SDK_VERSION = 26;

    public static final String MANIFEST_PATH = "--default";
}
Loading