Add a test for explicit call to private lambda instance method

Bug: 179889958
Change-Id: I40b8316db04777b38793642ce5d9cf4dedafa80a
diff --git a/src/test/java/com/android/tools/r8/TestRunResult.java b/src/test/java/com/android/tools/r8/TestRunResult.java
index 4a7d5c7..34cc0b0 100644
--- a/src/test/java/com/android/tools/r8/TestRunResult.java
+++ b/src/test/java/com/android/tools/r8/TestRunResult.java
@@ -3,6 +3,7 @@
 // BSD-style license that can be found in the LICENSE file.
 package com.android.tools.r8;
 
+import static com.android.tools.r8.utils.ConsumerUtils.emptyThrowingConsumer;
 import static org.hamcrest.CoreMatchers.containsString;
 import static org.hamcrest.CoreMatchers.is;
 
@@ -44,6 +45,22 @@
     return self();
   }
 
+  public <T extends Throwable> RR applyIf(boolean condition, ThrowingConsumer<RR, T> thenConsumer)
+      throws T {
+    return applyIf(condition, thenConsumer, emptyThrowingConsumer());
+  }
+
+  public <S extends Throwable, T extends Throwable> RR applyIf(
+      boolean condition, ThrowingConsumer<RR, S> thenConsumer, ThrowingConsumer<RR, T> elseConsumer)
+      throws S, T {
+    if (condition) {
+      thenConsumer.accept(self());
+    } else {
+      elseConsumer.accept(self());
+    }
+    return self();
+  }
+
   public <S> S map(Function<RR, S> fn) {
     return fn.apply(self());
   }
diff --git a/src/test/java/com/android/tools/r8/desugar/lambdas/ExplicitCallToJavacGeneratedLambdaMethodTest.java b/src/test/java/com/android/tools/r8/desugar/lambdas/ExplicitCallToJavacGeneratedInstanceLambdaMethodTest.java
similarity index 61%
copy from src/test/java/com/android/tools/r8/desugar/lambdas/ExplicitCallToJavacGeneratedLambdaMethodTest.java
copy to src/test/java/com/android/tools/r8/desugar/lambdas/ExplicitCallToJavacGeneratedInstanceLambdaMethodTest.java
index 8a73842..b8769c3 100644
--- a/src/test/java/com/android/tools/r8/desugar/lambdas/ExplicitCallToJavacGeneratedLambdaMethodTest.java
+++ b/src/test/java/com/android/tools/r8/desugar/lambdas/ExplicitCallToJavacGeneratedInstanceLambdaMethodTest.java
@@ -11,15 +11,17 @@
 import com.android.tools.r8.TestParametersCollection;
 import com.android.tools.r8.TestRuntime.CfVm;
 import com.android.tools.r8.cf.CfVersion;
+import com.android.tools.r8.utils.codeinspector.AssertUtils;
 import java.io.IOException;
 import java.lang.reflect.Method;
 import java.util.stream.Stream;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.junit.runners.Parameterized;
+import org.objectweb.asm.Opcodes;
 
 @RunWith(Parameterized.class)
-public class ExplicitCallToJavacGeneratedLambdaMethodTest extends TestBase {
+public class ExplicitCallToJavacGeneratedInstanceLambdaMethodTest extends TestBase {
 
   private final TestParameters parameters;
 
@@ -32,29 +34,40 @@
         .build();
   }
 
-  public ExplicitCallToJavacGeneratedLambdaMethodTest(TestParameters parameters) {
+  public ExplicitCallToJavacGeneratedInstanceLambdaMethodTest(TestParameters parameters) {
     this.parameters = parameters;
   }
 
+  // TODO(b/179889958): Should succeed.
   @Test
   public void testRuntime() throws Exception {
     testForRuntime(parameters)
         .addProgramClasses(Main.class, A.class, FunctionalInterface.class)
         .addProgramClassFileData(getProgramClassFileData())
         .run(parameters.getRuntime(), Main.class)
-        .assertSuccessWithOutputLines("Hello world!", "Hello world!");
+        .applyIf(
+            parameters.isCfRuntime(),
+            result -> result.assertSuccessWithOutputLines("Hello world!", "Hello world!"),
+            result -> result.assertFailureWithErrorThatThrows(NoSuchMethodError.class));
   }
 
+  // TODO(b/179889958): Should succeed.
   @Test
   public void testR8() throws Exception {
-    testForR8(parameters.getBackend())
-        .addProgramClasses(Main.class, A.class, FunctionalInterface.class)
-        .addProgramClassFileData(getProgramClassFileData())
-        .addKeepMainRule(Main.class)
-        .setMinApi(parameters.getApiLevel())
-        .compile()
-        .run(parameters.getRuntime(), Main.class)
-        .assertSuccessWithOutputLines("Hello world!", "Hello world!");
+    AssertUtils.assertFailsCompilationIf(
+        parameters.isDexRuntime() && !parameters.canUseDefaultAndStaticInterfaceMethods(),
+        () ->
+            testForR8(parameters.getBackend())
+                .addProgramClasses(Main.class, A.class, FunctionalInterface.class)
+                .addProgramClassFileData(getProgramClassFileData())
+                .addKeepMainRule(Main.class)
+                .setMinApi(parameters.getApiLevel())
+                .compile()
+                .run(parameters.getRuntime(), Main.class)
+                .applyIf(
+                    parameters.isCfRuntime(),
+                    result -> result.assertSuccessWithOutputLines("Hello world!", "Hello world!"),
+                    result -> result.assertFailureWithErrorThatThrows(NoSuchMethodError.class)));
   }
 
   private byte[] getProgramClassFileData() throws IOException {
@@ -69,7 +82,7 @@
             (opcode, owner, name, descriptor, isInterface, visitor) -> {
               if (name.equals("lambdaMethod")) {
                 visitor.visitMethodInsn(
-                    opcode, owner, lambdaMethod.getName(), descriptor, isInterface);
+                    Opcodes.INVOKESPECIAL, owner, lambdaMethod.getName(), descriptor, isInterface);
               } else {
                 visitor.visitMethodInsn(opcode, owner, name, descriptor, isInterface);
               }
@@ -89,13 +102,17 @@
   interface I {
 
     default void test() {
-      FunctionalInterface f = () -> System.out.println("Hello world!");
+      FunctionalInterface f = () -> greet();
       f.m();
       lambdaMethod(); // Changed to lambda$test$0() by transformer.
     }
 
+    default void greet() {
+      System.out.println("Hello world!");
+    }
+
     // Removed by transformer.
-    static void lambdaMethod() {}
+    default void lambdaMethod() {}
   }
 
   static class A implements I {}
diff --git a/src/test/java/com/android/tools/r8/desugar/lambdas/ExplicitCallToJavacGeneratedLambdaMethodTest.java b/src/test/java/com/android/tools/r8/desugar/lambdas/ExplicitCallToJavacGeneratedStaticLambdaMethodTest.java
similarity index 94%
rename from src/test/java/com/android/tools/r8/desugar/lambdas/ExplicitCallToJavacGeneratedLambdaMethodTest.java
rename to src/test/java/com/android/tools/r8/desugar/lambdas/ExplicitCallToJavacGeneratedStaticLambdaMethodTest.java
index 8a73842..e14d2f3 100644
--- a/src/test/java/com/android/tools/r8/desugar/lambdas/ExplicitCallToJavacGeneratedLambdaMethodTest.java
+++ b/src/test/java/com/android/tools/r8/desugar/lambdas/ExplicitCallToJavacGeneratedStaticLambdaMethodTest.java
@@ -19,7 +19,7 @@
 import org.junit.runners.Parameterized;
 
 @RunWith(Parameterized.class)
-public class ExplicitCallToJavacGeneratedLambdaMethodTest extends TestBase {
+public class ExplicitCallToJavacGeneratedStaticLambdaMethodTest extends TestBase {
 
   private final TestParameters parameters;
 
@@ -32,7 +32,7 @@
         .build();
   }
 
-  public ExplicitCallToJavacGeneratedLambdaMethodTest(TestParameters parameters) {
+  public ExplicitCallToJavacGeneratedStaticLambdaMethodTest(TestParameters parameters) {
     this.parameters = parameters;
   }