[ApiModel] Add test for throwing exception when referencing stub
Bug: 209803983
Change-Id: Ic92f6a746ff6423595685fe13f5addb5f321a21b
diff --git a/src/test/java/com/android/tools/r8/apimodel/ApiModelMockClassLoadingTest.java b/src/test/java/com/android/tools/r8/apimodel/ApiModelMockClassLoadingTest.java
new file mode 100644
index 0000000..4db820e
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/apimodel/ApiModelMockClassLoadingTest.java
@@ -0,0 +1,76 @@
+// 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 org.junit.Assume.assumeFalse;
+
+import com.android.tools.r8.TestBase;
+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 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();
+ }
+
+ @Test
+ public void testR8() throws Exception {
+ // TODO(b/197078995): Make this work on 12.
+ assumeFalse(
+ parameters.isDexRuntime() && parameters.getDexRuntimeVersion().isEqualTo(Version.V12_0_0));
+ boolean isLibraryOnBootClassPath =
+ parameters.isDexRuntime()
+ && parameters.getRuntime().maxSupportedApiLevel().isGreaterThanOrEqualTo(mockLevel);
+ testForR8(parameters.getBackend())
+ .addProgramClasses(Main.class)
+ .addLibraryClasses(LibraryClass.class)
+ .addDefaultRuntimeLibrary(parameters)
+ .setMinApi(parameters.getApiLevel())
+ .addKeepMainRule(Main.class)
+ .apply(ApiModelingTestHelper::enableStubbingOfClasses)
+ .apply(setMockApiLevelForClass(LibraryClass.class, mockLevel))
+ .apply(setMockApiLevelForDefaultInstanceInitializer(LibraryClass.class, mockLevel))
+ .compile()
+ .applyIf(isLibraryOnBootClassPath, b -> b.addBootClasspathClasses(LibraryClass.class))
+ .run(parameters.getRuntime(), Main.class)
+ .assertFailureWithErrorThatThrowsIf(parameters.isCfRuntime(), NoClassDefFoundError.class)
+ // TODO(b/209803983): Should fail with a NoClassDefFoundError unless the api level of the
+ // runtime >= 23.
+ .assertFailureWithErrorThatThrowsIf(
+ parameters.isDexRuntime() && !isLibraryOnBootClassPath, UnsatisfiedLinkError.class)
+ .assertSuccessWithOutputLinesIf(isLibraryOnBootClassPath, "Hello World");
+ }
+
+ // 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();
+ }
+ }
+}