| // Copyright (c) 2021, the R8 project authors. Please see the AUTHORS file | |
| // for details. All rights reserved. Use of this source code is governed by a | |
| // BSD-style license that can be found in the LICENSE file. | |
| package com.android.tools.r8.apimodel; | |
| import static com.android.tools.r8.apimodel.ApiModelingTestHelper.setMockApiLevelForClass; | |
| import static com.android.tools.r8.apimodel.ApiModelingTestHelper.setMockApiLevelForDefaultInstanceInitializer; | |
| import static com.android.tools.r8.apimodel.ApiModelingTestHelper.verifyThat; | |
| import static org.junit.Assume.assumeFalse; | |
| import static org.junit.Assume.assumeTrue; | |
| import com.android.tools.r8.CompilationMode; | |
| import com.android.tools.r8.SingleTestRunResult; | |
| import com.android.tools.r8.TestBase; | |
| import com.android.tools.r8.TestCompilerBuilder; | |
| import com.android.tools.r8.TestParameters; | |
| import com.android.tools.r8.TestParametersCollection; | |
| import com.android.tools.r8.ToolHelper.DexVm.Version; | |
| import com.android.tools.r8.utils.AndroidApiLevel; | |
| import com.android.tools.r8.utils.codeinspector.CodeInspector; | |
| import org.junit.Test; | |
| import org.junit.runner.RunWith; | |
| import org.junit.runners.Parameterized; | |
| import org.junit.runners.Parameterized.Parameter; | |
| import org.junit.runners.Parameterized.Parameters; | |
| @RunWith(Parameterized.class) | |
| public class ApiModelMockClassLoadingTest extends TestBase { | |
| private final AndroidApiLevel mockLevel = AndroidApiLevel.M; | |
| @Parameter() public TestParameters parameters; | |
| @Parameters(name = "{0}") | |
| public static TestParametersCollection data() { | |
| return getTestParameters().withAllRuntimesAndApiLevels().build(); | |
| } | |
| private boolean isGreaterOrEqualToMockLevel() { | |
| return parameters.isDexRuntime() && parameters.getApiLevel().isGreaterThanOrEqualTo(mockLevel); | |
| } | |
| private void setupTestBuilder(TestCompilerBuilder<?, ?, ?, ?, ?> testBuilder) { | |
| testBuilder | |
| .addProgramClasses(Main.class) | |
| .addLibraryClasses(LibraryClass.class) | |
| .addDefaultRuntimeLibrary(parameters) | |
| .setMinApi(parameters.getApiLevel()) | |
| .apply(ApiModelingTestHelper::enableStubbingOfClasses) | |
| .apply(setMockApiLevelForClass(LibraryClass.class, mockLevel)) | |
| .apply(setMockApiLevelForDefaultInstanceInitializer(LibraryClass.class, mockLevel)); | |
| } | |
| @Test | |
| public void testD8Debug() throws Exception { | |
| // TODO(b/197078995): Make this work on 12+. | |
| assumeTrue( | |
| parameters.isDexRuntime() | |
| && parameters.getDexRuntimeVersion().isOlderThan(Version.V12_0_0)); | |
| testForD8() | |
| .setMode(CompilationMode.DEBUG) | |
| .apply(this::setupTestBuilder) | |
| .compile() | |
| .inspect(ApiModelingTestHelper::assertNoSynthesizedClasses) | |
| .applyIf(isGreaterOrEqualToMockLevel(), b -> b.addBootClasspathClasses(LibraryClass.class)) | |
| .run(parameters.getRuntime(), Main.class) | |
| .apply(this::checkOutput); | |
| } | |
| @Test | |
| public void testD8Release() throws Exception { | |
| // TODO(b/197078995): Make this work on 12+. | |
| assumeFalse( | |
| parameters.isCfRuntime() | |
| || parameters.getDexRuntimeVersion().isNewerThanOrEqual(Version.V12_0_0)); | |
| testForD8() | |
| .setMode(CompilationMode.RELEASE) | |
| // TODO(b/213552119): Remove when enabled by default. | |
| .apply(ApiModelingTestHelper::enableApiCallerIdentification) | |
| .apply(this::setupTestBuilder) | |
| .compile() | |
| .inspect(this::inspect) | |
| .applyIf(isGreaterOrEqualToMockLevel(), b -> b.addBootClasspathClasses(LibraryClass.class)) | |
| .run(parameters.getRuntime(), Main.class) | |
| .apply(this::checkOutput); | |
| } | |
| @Test | |
| public void testR8() throws Exception { | |
| // TODO(b/197078995): Make this work on 12+. | |
| assumeFalse( | |
| parameters.isDexRuntime() | |
| && parameters.getDexRuntimeVersion().isNewerThanOrEqual(Version.V12_0_0)); | |
| testForR8(parameters.getBackend()) | |
| .apply(this::setupTestBuilder) | |
| .addKeepMainRule(Main.class) | |
| .compile() | |
| .inspect(this::inspect) | |
| .applyIf(isGreaterOrEqualToMockLevel(), b -> b.addBootClasspathClasses(LibraryClass.class)) | |
| .run(parameters.getRuntime(), Main.class) | |
| .apply(this::checkOutput); | |
| } | |
| private void inspect(CodeInspector inspector) { | |
| verifyThat(inspector, parameters, LibraryClass.class).stubbedUntil(mockLevel); | |
| } | |
| private void checkOutput(SingleTestRunResult<?> runResult) { | |
| runResult.assertSuccessWithOutputLinesIf(isGreaterOrEqualToMockLevel(), "Hello World"); | |
| runResult.assertFailureWithErrorThatThrowsIf( | |
| !isGreaterOrEqualToMockLevel(), NoClassDefFoundError.class); | |
| } | |
| // Only present form api level 23. | |
| public static class LibraryClass { | |
| public static void foo() { | |
| System.out.println("Hello World"); | |
| } | |
| } | |
| public static class Main { | |
| public static void main(String[] args) { | |
| LibraryClass.foo(); | |
| } | |
| } | |
| } |