[ApiModel] Add test for stubbing with abstract methods and outlining

Bug: b/257680068
Change-Id: Ia4867f0018eb3f7d715eca58470c78cd2db250e9
diff --git a/src/test/java/com/android/tools/r8/apimodel/ApiModelMockAbstractMethodOnBaseToOutlineTest.java b/src/test/java/com/android/tools/r8/apimodel/ApiModelMockAbstractMethodOnBaseToOutlineTest.java
new file mode 100644
index 0000000..3382132
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/apimodel/ApiModelMockAbstractMethodOnBaseToOutlineTest.java
@@ -0,0 +1,190 @@
+// Copyright (c) 2022, 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.setMockApiLevelForMethod;
+import static com.android.tools.r8.apimodel.ApiModelingTestHelper.verifyThat;
+import static org.hamcrest.CoreMatchers.containsString;
+import static org.hamcrest.CoreMatchers.not;
+
+import com.android.tools.r8.CompilationMode;
+import com.android.tools.r8.NeverInline;
+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.testing.AndroidBuildVersion;
+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 ApiModelMockAbstractMethodOnBaseToOutlineTest extends TestBase {
+
+  private final AndroidApiLevel subMockLevel = AndroidApiLevel.M;
+
+  @Parameter public TestParameters parameters;
+
+  @Parameters(name = "{0}")
+  public static TestParametersCollection data() {
+    return getTestParameters().withAllRuntimes().withAllApiLevelsAlsoForCf().build();
+  }
+
+  private boolean isGreaterOrEqualToMockLevel() {
+    return parameters.isDexRuntime()
+        && parameters.getApiLevel().isGreaterThanOrEqualTo(subMockLevel);
+  }
+
+  private void setupTestBuilder(TestCompilerBuilder<?, ?, ?, ?, ?> testBuilder) throws Exception {
+    testBuilder
+        .addProgramClasses(Main.class)
+        .addLibraryClasses(
+            LibraryClass.class, OtherLibraryClass.class, SubLibraryClassAtLaterApiLevel.class)
+        .addDefaultRuntimeLibrary(parameters)
+        .setMinApi(parameters.getApiLevel())
+        .addAndroidBuildVersion()
+        .apply(ApiModelingTestHelper::enableStubbingOfClasses)
+        .apply(ApiModelingTestHelper::enableOutliningOfMethods)
+        .apply(setMockApiLevelForClass(LibraryClass.class, AndroidApiLevel.B))
+        .apply(setMockApiLevelForDefaultInstanceInitializer(LibraryClass.class, AndroidApiLevel.B))
+        .apply(
+            setMockApiLevelForMethod(
+                LibraryClass.class.getDeclaredMethod("foo"), AndroidApiLevel.B))
+        .apply(
+            setMockApiLevelForMethod(
+                OtherLibraryClass.class.getDeclaredMethod("baz"), subMockLevel))
+        .apply(setMockApiLevelForClass(SubLibraryClassAtLaterApiLevel.class, subMockLevel))
+        .apply(
+            setMockApiLevelForDefaultInstanceInitializer(
+                SubLibraryClassAtLaterApiLevel.class, subMockLevel))
+        .apply(
+            setMockApiLevelForMethod(
+                SubLibraryClassAtLaterApiLevel.class.getDeclaredMethod("foo"), subMockLevel));
+  }
+
+  @Test
+  public void testD8Debug() throws Exception {
+    testForD8(parameters.getBackend())
+        .setMode(CompilationMode.DEBUG)
+        .apply(this::setupTestBuilder)
+        .compile()
+        .inspect(this::inspect)
+        .addBootClasspathClasses(LibraryClass.class)
+        .applyIf(
+            isGreaterOrEqualToMockLevel(),
+            b ->
+                b.addBootClasspathClasses(
+                    OtherLibraryClass.class, SubLibraryClassAtLaterApiLevel.class))
+        .run(parameters.getRuntime(), Main.class)
+        .apply(runResult -> checkOutput(runResult, false));
+  }
+
+  @Test
+  public void testD8Release() throws Exception {
+    testForD8(parameters.getBackend())
+        .setMode(CompilationMode.RELEASE)
+        .apply(this::setupTestBuilder)
+        .compile()
+        .inspect(this::inspect)
+        .addBootClasspathClasses(LibraryClass.class)
+        .applyIf(
+            isGreaterOrEqualToMockLevel(),
+            b ->
+                b.addBootClasspathClasses(
+                    OtherLibraryClass.class, SubLibraryClassAtLaterApiLevel.class))
+        .run(parameters.getRuntime(), Main.class)
+        .apply(runResult -> checkOutput(runResult, true));
+  }
+
+  @Test
+  public void testR8() throws Exception {
+    testForR8(parameters.getBackend())
+        .apply(this::setupTestBuilder)
+        .addKeepMainRule(Main.class)
+        .enableInliningAnnotations()
+        .compile()
+        .inspect(this::inspect)
+        .addBootClasspathClasses(LibraryClass.class)
+        .applyIf(
+            isGreaterOrEqualToMockLevel(),
+            b ->
+                b.addBootClasspathClasses(
+                    OtherLibraryClass.class, SubLibraryClassAtLaterApiLevel.class))
+        .run(parameters.getRuntime(), Main.class)
+        .apply(runResult -> checkOutput(runResult, true));
+  }
+
+  private void checkOutput(SingleTestRunResult<?> runResult, boolean isRelease) {
+    if (isGreaterOrEqualToMockLevel()) {
+      runResult.assertSuccessWithOutputLines(
+          "OtherLibraryClass::foo", "SubLibraryClassAtLaterApiLevel::foo");
+    } else if (parameters.isDexRuntime()
+        && parameters.getDexRuntimeVersion().isDalvik()
+        && isRelease) {
+      runResult.assertFailureWithErrorThatThrows(VerifyError.class);
+    } else {
+      runResult.assertSuccessWithOutputLines("NoClassDefFoundError");
+    }
+    runResult.applyIf(
+        !isGreaterOrEqualToMockLevel()
+            && parameters.isDexRuntime()
+            && parameters.getDexRuntimeVersion().isNewerThanOrEqual(Version.V7_0_0),
+        result -> result.assertStderrMatches(not(containsString("This dex file is invalid"))));
+  }
+
+  private void inspect(CodeInspector inspector) {
+    verifyThat(inspector, parameters, SubLibraryClassAtLaterApiLevel.class)
+        .stubbedUntil(subMockLevel);
+  }
+
+  public abstract static class LibraryClass {
+
+    public abstract void foo();
+  }
+
+  public static class SubLibraryClassAtLaterApiLevel extends LibraryClass {
+
+    @Override
+    public void foo() {
+      System.out.println("SubLibraryClassAtLaterApiLevel::foo");
+    }
+  }
+
+  public static class OtherLibraryClass {
+
+    public static void baz() {
+      System.out.println("OtherLibraryClass::foo");
+    }
+  }
+
+  public static class Main {
+
+    public static void main(String[] args) {
+      try {
+        OtherLibraryClass.baz();
+        if (AndroidBuildVersion.VERSION >= 23) {
+          callSubFoo(new SubLibraryClassAtLaterApiLevel());
+        } else if (System.currentTimeMillis() == 0) {
+          callSubFoo(new Object());
+        }
+      } catch (NoClassDefFoundError e) {
+        System.out.println("NoClassDefFoundError");
+      }
+    }
+
+    @NeverInline
+    private static void callSubFoo(Object o) {
+      ((SubLibraryClassAtLaterApiLevel) o).foo();
+    }
+  }
+}
diff --git a/src/test/java/com/android/tools/r8/apimodel/NewInstanceToAbstractClassReferenceTest.java b/src/test/java/com/android/tools/r8/apimodel/NewInstanceToAbstractClassReferenceTest.java
new file mode 100644
index 0000000..b733845
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/apimodel/NewInstanceToAbstractClassReferenceTest.java
@@ -0,0 +1,71 @@
+// Copyright (c) 2022, 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 org.hamcrest.CoreMatchers.containsString;
+import static org.hamcrest.CoreMatchers.not;
+
+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.graph.ClassAccessFlags;
+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 NewInstanceToAbstractClassReferenceTest extends TestBase {
+
+  @Parameter() public TestParameters parameters;
+
+  @Parameters(name = "{0}")
+  public static TestParametersCollection data() {
+    return getTestParameters().withAllRuntimesAndApiLevels().build();
+  }
+
+  @Test
+  public void testRuntime() throws Exception {
+    testForRuntime(parameters)
+        .addProgramClasses(Main.class)
+        .addProgramClassFileData(
+            transformer(A.class).setAccessFlags(ClassAccessFlags::setAbstract).transform())
+        .run(parameters.getRuntime(), Main.class)
+        .assertFailureWithErrorThatThrowsIf(parameters.isCfRuntime(), InstantiationError.class)
+        .applyIf(
+            parameters.isDexRuntime(),
+            result -> {
+              if (parameters.getDexRuntimeVersion().isDalvik()) {
+                result.assertStderrMatches(
+                    containsString(
+                        "VFY: new-instance on interface or abstract class " + descriptor(A.class)));
+
+              } else if (parameters.getDexRuntimeVersion().isOlderThan(Version.V7_0_0)) {
+                result.assertStderrMatches(
+                    containsString(
+                        "Verification failed on class "
+                            + typeName(NewInstanceToAbstractClassReferenceTest.class)));
+              } else {
+                result.assertStderrMatches(not(containsString("Verification failed")));
+              }
+            });
+  }
+
+  public static class A {
+
+    public void foo() {
+      System.out.println("A::foo");
+    }
+  }
+
+  public static class Main {
+
+    public static void main(String[] args) {
+      new A().foo();
+    }
+  }
+}