Search in sources :

Example 1 with PerfStatsCollector

use of org.robolectric.util.PerfStatsCollector in project robolectric by robolectric.

the class SandboxTestRunner method methodBlock.

@Override
protected Statement methodBlock(final FrameworkMethod method) {
    return new Statement() {

        @Override
        public void evaluate() throws Throwable {
            PerfStatsCollector perfStatsCollector = PerfStatsCollector.getInstance();
            perfStatsCollector.reset();
            perfStatsCollector.setEnabled(!perfStatsReporters.isEmpty());
            Event initialization = perfStatsCollector.startEvent("initialization");
            final Sandbox sandbox = getSandbox(method);
            // Configure sandbox *BEFORE* setting the ClassLoader. This is necessary because
            // creating the ShadowMap loads all ShadowProviders via ServiceLoader and this is
            // not available once we install the Robolectric class loader.
            configureSandbox(sandbox, method);
            sandbox.runOnMainThread(() -> {
                ClassLoader priorContextClassLoader = Thread.currentThread().getContextClassLoader();
                Thread.currentThread().setContextClassLoader(sandbox.getRobolectricClassLoader());
                Class bootstrappedTestClass = sandbox.bootstrappedClass(getTestClass().getJavaClass());
                HelperTestRunner helperTestRunner = getHelperTestRunner(bootstrappedTestClass);
                helperTestRunner.frameworkMethod = method;
                final Method bootstrappedMethod;
                try {
                    // noinspection unchecked
                    bootstrappedMethod = bootstrappedTestClass.getMethod(method.getMethod().getName());
                } catch (NoSuchMethodException e) {
                    throw new RuntimeException(e);
                }
                try {
                    // Only invoke @BeforeClass once per class
                    invokeBeforeClass(bootstrappedTestClass, sandbox);
                    beforeTest(sandbox, method, bootstrappedMethod);
                    initialization.finished();
                    Statement statement = helperTestRunner.methodBlock(new FrameworkMethod(bootstrappedMethod));
                    // todo: this try/finally probably isn't right -- should mimic RunAfters? [xw]
                    try {
                        statement.evaluate();
                    } finally {
                        afterTest(method, bootstrappedMethod);
                    }
                } catch (Throwable throwable) {
                    throw Util.sneakyThrow(throwable);
                } finally {
                    Thread.currentThread().setContextClassLoader(priorContextClassLoader);
                    try {
                        finallyAfterTest(method);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
            reportPerfStats(perfStatsCollector);
            perfStatsCollector.reset();
        }
    };
}
Also used : Statement(org.junit.runners.model.Statement) Method(java.lang.reflect.Method) FrameworkMethod(org.junit.runners.model.FrameworkMethod) Sandbox(org.robolectric.internal.bytecode.Sandbox) Event(org.robolectric.util.PerfStatsCollector.Event) BeforeClass(org.junit.BeforeClass) TestClass(org.junit.runners.model.TestClass) AfterClass(org.junit.AfterClass) FrameworkMethod(org.junit.runners.model.FrameworkMethod) PerfStatsCollector(org.robolectric.util.PerfStatsCollector)

Example 2 with PerfStatsCollector

use of org.robolectric.util.PerfStatsCollector in project robolectric by robolectric.

the class RobolectricTestRunner method beforeTest.

@Override
protected void beforeTest(Sandbox sandbox, FrameworkMethod method, Method bootstrappedMethod) throws Throwable {
    AndroidSandbox androidSandbox = (AndroidSandbox) sandbox;
    RobolectricFrameworkMethod roboMethod = (RobolectricFrameworkMethod) method;
    PerfStatsCollector perfStatsCollector = PerfStatsCollector.getInstance();
    Sdk sdk = roboMethod.getSdk();
    perfStatsCollector.putMetadata(AndroidMetadata.class, new AndroidMetadata(ImmutableMap.of("ro.build.version.sdk", "" + sdk.getApiLevel()), roboMethod.resourcesMode.name()));
    Logger.lifecycle(roboMethod.getDeclaringClass().getName() + "." + roboMethod.getMethod().getName() + ": sdk=" + sdk.getApiLevel() + "; resources=" + roboMethod.resourcesMode);
    if (roboMethod.resourcesMode == ResourcesMode.LEGACY) {
        Logger.warn("Legacy resources mode is deprecated; see" + " http://robolectric.org/migrating/#migrating-to-40");
    }
    roboMethod.setStuff(androidSandbox, androidSandbox.getTestEnvironment());
    Class<TestLifecycle> cl = androidSandbox.bootstrappedClass(getTestLifecycleClass());
    roboMethod.testLifecycle = ReflectionHelpers.newInstance(cl);
    AndroidManifest appManifest = roboMethod.getAppManifest();
    roboMethod.getTestEnvironment().setUpApplicationState(bootstrappedMethod, roboMethod.getConfiguration(), appManifest);
    roboMethod.testLifecycle.beforeTest(bootstrappedMethod);
}
Also used : AndroidSandbox(org.robolectric.internal.AndroidSandbox) AndroidManifest(org.robolectric.manifest.AndroidManifest) Sdk(org.robolectric.pluginapi.Sdk) PerfStatsCollector(org.robolectric.util.PerfStatsCollector)

Example 3 with PerfStatsCollector

use of org.robolectric.util.PerfStatsCollector in project robolectric by robolectric.

the class ClassInstrumentor method instrument.

public byte[] instrument(ClassDetails classDetails, InstrumentationConfiguration config, ClassNodeProvider classNodeProvider) {
    PerfStatsCollector perfStats = PerfStatsCollector.getInstance();
    MutableClass mutableClass = perfStats.measure("analyze class", () -> analyzeClass(classDetails.getClassBytes(), config, classNodeProvider));
    byte[] instrumentedBytes = perfStats.measure("instrument class", () -> instrumentToBytes(mutableClass));
    recordPackageStats(perfStats, mutableClass);
    return instrumentedBytes;
}
Also used : PerfStatsCollector(org.robolectric.util.PerfStatsCollector)

Aggregations

PerfStatsCollector (org.robolectric.util.PerfStatsCollector)3 Method (java.lang.reflect.Method)1 AfterClass (org.junit.AfterClass)1 BeforeClass (org.junit.BeforeClass)1 FrameworkMethod (org.junit.runners.model.FrameworkMethod)1 Statement (org.junit.runners.model.Statement)1 TestClass (org.junit.runners.model.TestClass)1 AndroidSandbox (org.robolectric.internal.AndroidSandbox)1 Sandbox (org.robolectric.internal.bytecode.Sandbox)1 AndroidManifest (org.robolectric.manifest.AndroidManifest)1 Sdk (org.robolectric.pluginapi.Sdk)1 Event (org.robolectric.util.PerfStatsCollector.Event)1