Convert desugared library test

introduce custom library inspector

Bug: b:231287675
Change-Id: I032ec0e85147eafb9443777e0bd1c31a7d99c747
diff --git a/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/conversiontests/FunctionConversionTest.java b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/conversiontests/FunctionConversionTest.java
index d07a09c..7d81e76 100644
--- a/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/conversiontests/FunctionConversionTest.java
+++ b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/conversiontests/FunctionConversionTest.java
@@ -59,7 +59,7 @@
   }
 
   @Test
-  public void testClock() throws Throwable {
+  public void testFunction() throws Throwable {
     testForDesugaredLibrary(parameters, libraryDesugaringSpecification, compilationSpecification)
         .addProgramClasses(
             Executor.class, Executor.Object1.class, Executor.Object2.class, Executor.Object3.class)
diff --git a/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/conversiontests/MoreFunctionConversionTest.java b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/conversiontests/MoreFunctionConversionTest.java
index 4800500..70b7b73 100644
--- a/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/conversiontests/MoreFunctionConversionTest.java
+++ b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/conversiontests/MoreFunctionConversionTest.java
@@ -4,22 +4,24 @@
 
 package com.android.tools.r8.desugar.desugaredlibrary.conversiontests;
 
+import static com.android.tools.r8.desugar.desugaredlibrary.test.CompilationSpecification.DEFAULT_SPECIFICATIONS;
+import static com.android.tools.r8.desugar.desugaredlibrary.test.LibraryDesugaringSpecification.getJdk8Jdk11;
 import static org.junit.Assert.assertEquals;
 
-import com.android.tools.r8.D8TestCompileResult;
 import com.android.tools.r8.TestParameters;
 import com.android.tools.r8.desugar.desugaredlibrary.DesugaredLibraryTestBase;
+import com.android.tools.r8.desugar.desugaredlibrary.test.CompilationSpecification;
+import com.android.tools.r8.desugar.desugaredlibrary.test.CustomLibrarySpecification;
+import com.android.tools.r8.desugar.desugaredlibrary.test.DesugaredLibraryTestCompileResult;
+import com.android.tools.r8.desugar.desugaredlibrary.test.LibraryDesugaringSpecification;
 import com.android.tools.r8.utils.AndroidApiLevel;
-import com.android.tools.r8.utils.BooleanUtils;
 import com.android.tools.r8.utils.StringUtils;
 import com.android.tools.r8.utils.codeinspector.CodeInspector;
 import com.android.tools.r8.utils.codeinspector.FoundClassSubject;
 import com.google.common.collect.Sets;
-import java.nio.file.Path;
 import java.util.List;
 import java.util.Set;
 import java.util.function.Function;
-import org.junit.BeforeClass;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.junit.runners.Parameterized;
@@ -28,74 +30,40 @@
 @RunWith(Parameterized.class)
 public class MoreFunctionConversionTest extends DesugaredLibraryTestBase {
 
+
   private final TestParameters parameters;
-  private final boolean shrinkDesugaredLibrary;
+  private final LibraryDesugaringSpecification libraryDesugaringSpecification;
+  private final CompilationSpecification compilationSpecification;
+
   private static final AndroidApiLevel MIN_SUPPORTED = AndroidApiLevel.N;
   private static final String EXPECTED_RESULT = StringUtils.lines("6", "6", "6", "6", "6");
-  private static Path CUSTOM_LIB;
 
-  @Parameters(name = "{0}, shrinkDesugaredLibrary: {1}")
+  @Parameters(name = "{0}, spec: {1}, {2}")
   public static List<Object[]> data() {
     return buildParameters(
-        getConversionParametersUpToExcluding(MIN_SUPPORTED), BooleanUtils.values());
+        getConversionParametersUpToExcluding(MIN_SUPPORTED),
+        getJdk8Jdk11(),
+        DEFAULT_SPECIFICATIONS);
   }
 
-  public MoreFunctionConversionTest(TestParameters parameters, boolean shrinkDesugaredLibrary) {
-    this.shrinkDesugaredLibrary = shrinkDesugaredLibrary;
+  public MoreFunctionConversionTest(
+      TestParameters parameters,
+      LibraryDesugaringSpecification libraryDesugaringSpecification,
+      CompilationSpecification compilationSpecification) {
     this.parameters = parameters;
-  }
-
-  @BeforeClass
-  public static void compileCustomLib() throws Exception {
-    CUSTOM_LIB =
-        testForD8(getStaticTemp())
-            .addProgramClasses(CustomLibClass.class)
-            .setMinApi(MIN_SUPPORTED)
-            .compile()
-            .writeToZip();
+    this.libraryDesugaringSpecification = libraryDesugaringSpecification;
+    this.compilationSpecification = compilationSpecification;
   }
 
   @Test
-  public void testFunctionD8() throws Exception {
-    KeepRuleConsumer keepRuleConsumer = createKeepRuleConsumer(parameters);
-    D8TestCompileResult compileResult =
-        testForD8()
-            .addLibraryFiles(getLibraryFile())
-            .setMinApi(parameters.getApiLevel())
-            .addProgramClasses(Executor.class)
-            .addLibraryClasses(CustomLibClass.class)
-            .enableCoreLibraryDesugaring(parameters.getApiLevel(), keepRuleConsumer)
-            .compile();
-    Path program = compileResult.writeToZip();
-    assertNoDuplicateLambdas(program, CUSTOM_LIB);
-    compileResult
-        .addDesugaredCoreLibraryRunClassPath(
-            this::buildDesugaredLibrary,
-            parameters.getApiLevel(),
-            keepRuleConsumer.get(),
-            shrinkDesugaredLibrary)
-        .addRunClasspathFiles(CUSTOM_LIB)
-        .run(parameters.getRuntime(), Executor.class)
-        .assertSuccessWithOutput(EXPECTED_RESULT);
-  }
-
-  @Test
-  public void testFunctionR8() throws Exception {
-    KeepRuleConsumer keepRuleConsumer = createKeepRuleConsumer(parameters);
-    testForR8(parameters.getBackend())
-        .addLibraryFiles(getLibraryFile())
-        .setMinApi(parameters.getApiLevel())
+  public void testFunction() throws Throwable {
+    testForDesugaredLibrary(parameters, libraryDesugaringSpecification, compilationSpecification)
         .addProgramClasses(Executor.class)
+        .setCustomLibrarySpecification(
+            new CustomLibrarySpecification(CustomLibClass.class, MIN_SUPPORTED))
         .addKeepMainRule(Executor.class)
-        .addLibraryClasses(CustomLibClass.class)
-        .enableCoreLibraryDesugaring(parameters.getApiLevel(), keepRuleConsumer)
         .compile()
-        .addDesugaredCoreLibraryRunClassPath(
-            this::buildDesugaredLibrary,
-            parameters.getApiLevel(),
-            keepRuleConsumer.get(),
-            shrinkDesugaredLibrary)
-        .addRunClasspathFiles(CUSTOM_LIB)
+        .apply(this::assertNoDuplicateLambdas)
         .run(parameters.getRuntime(), Executor.class)
         .assertSuccessWithOutput(EXPECTED_RESULT);
   }
@@ -103,9 +71,10 @@
   // If we have the exact same lambda in both, but one implements j$..Function and the other
   // java..Function, ART is obviously very confused.
   // This happens for instance when String::length function is used both in CustomLib and Executor.
-  private void assertNoDuplicateLambdas(Path program, Path customLib) throws Exception {
-    CodeInspector programInspector = new CodeInspector(program);
-    CodeInspector customLibInspector = new CodeInspector(customLib);
+  private void assertNoDuplicateLambdas(DesugaredLibraryTestCompileResult<?> builder)
+      throws Throwable {
+    CodeInspector programInspector = builder.inspector();
+    CodeInspector customLibInspector = builder.customLibInspector();
     int programSize = programInspector.allClasses().size();
     int customLibSize = customLibInspector.allClasses().size();
     Set<String> foundClassSubjects = Sets.newHashSet();
diff --git a/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/test/DesugaredLibraryTestBuilder.java b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/test/DesugaredLibraryTestBuilder.java
index 35bb41e..089b9b3 100644
--- a/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/test/DesugaredLibraryTestBuilder.java
+++ b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/test/DesugaredLibraryTestBuilder.java
@@ -5,6 +5,7 @@
 package com.android.tools.r8.desugar.desugaredlibrary.test;
 
 import com.android.tools.r8.CompilationFailedException;
+import com.android.tools.r8.D8TestCompileResult;
 import com.android.tools.r8.L8TestBuilder;
 import com.android.tools.r8.L8TestCompileResult;
 import com.android.tools.r8.LibraryDesugaringTestConfiguration;
@@ -142,16 +143,27 @@
     TestCompileResult<?, ?> compile = builder.compile();
     String keepRule = keepRuleConsumer == null ? null : keepRuleConsumer.get();
     L8TestCompileResult l8Compile = compileDesugaredLibrary(keepRule);
+    D8TestCompileResult customLibCompile = compileCustomLib();
     return new DesugaredLibraryTestCompileResult<>(
         test,
         compile,
         parameters,
         libraryDesugaringSpecification,
         compilationSpecification,
-        customLibrarySpecification,
+        customLibCompile,
         l8Compile);
   }
 
+  private D8TestCompileResult compileCustomLib() throws CompilationFailedException {
+    if (compilationSpecification == null) {
+      return null;
+    }
+    return test.testForD8()
+        .addProgramClasses(customLibrarySpecification.getClasses())
+        .setMinApi(customLibrarySpecification.getMinApi())
+        .compile();
+  }
+
   private L8TestCompileResult compileDesugaredLibrary(String keepRule)
       throws IOException, CompilationFailedException, ExecutionException {
     return test.testForL8(parameters.getApiLevel(), parameters.getBackend())
diff --git a/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/test/DesugaredLibraryTestCompileResult.java b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/test/DesugaredLibraryTestCompileResult.java
index c45315e..78b9fb1 100644
--- a/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/test/DesugaredLibraryTestCompileResult.java
+++ b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/test/DesugaredLibraryTestCompileResult.java
@@ -4,6 +4,7 @@
 
 package com.android.tools.r8.desugar.desugaredlibrary.test;
 
+import com.android.tools.r8.D8TestCompileResult;
 import com.android.tools.r8.L8TestCompileResult;
 import com.android.tools.r8.TestCompileResult;
 import com.android.tools.r8.TestDiagnosticMessages;
@@ -23,7 +24,7 @@
   private final TestParameters parameters;
   private final LibraryDesugaringSpecification libraryDesugaringSpecification;
   private final CompilationSpecification compilationSpecification;
-  private final CustomLibrarySpecification customLibrarySpecification;
+  private final D8TestCompileResult customLibCompile;
   private final L8TestCompileResult l8Compile;
 
   public DesugaredLibraryTestCompileResult(
@@ -32,17 +33,24 @@
       TestParameters parameters,
       LibraryDesugaringSpecification libraryDesugaringSpecification,
       CompilationSpecification compilationSpecification,
-      CustomLibrarySpecification customLibrarySpecification,
+      D8TestCompileResult customLibCompile,
       L8TestCompileResult l8Compile) {
     this.test = test;
     this.compileResult = compileResult;
     this.parameters = parameters;
     this.libraryDesugaringSpecification = libraryDesugaringSpecification;
     this.compilationSpecification = compilationSpecification;
-    this.customLibrarySpecification = customLibrarySpecification;
+    this.customLibCompile = customLibCompile;
     this.l8Compile = l8Compile;
   }
 
+  public <E extends Throwable> DesugaredLibraryTestCompileResult<T> inspectCustomLib(
+      ThrowingConsumer<CodeInspector, E> consumer) throws Throwable {
+    assert customLibCompile != null;
+    customLibCompile.inspect(consumer);
+    return this;
+  }
+
   public <E extends Throwable> DesugaredLibraryTestCompileResult<T> inspectL8(
       ThrowingConsumer<CodeInspector, E> consumer) throws Throwable {
     l8Compile.inspect(consumer);
@@ -55,6 +63,25 @@
     return this;
   }
 
+  public <E extends Throwable> DesugaredLibraryTestCompileResult<T> apply(
+      ThrowingConsumer<DesugaredLibraryTestCompileResult<T>, E> consumer) throws Throwable {
+    consumer.accept(this);
+    return this;
+  }
+
+  public CodeInspector customLibInspector() throws Throwable {
+    assert customLibCompile != null;
+    return customLibCompile.inspector();
+  }
+
+  public CodeInspector l8Inspector() throws Throwable {
+    return l8Compile.inspector();
+  }
+
+  public CodeInspector inspector() throws Throwable {
+    return compileResult.inspector();
+  }
+
   public DesugaredLibraryTestCompileResult<T> inspectDiagnosticMessages(
       Consumer<TestDiagnosticMessages> consumer) {
     compileResult.inspectDiagnosticMessages(consumer);
@@ -79,14 +106,8 @@
     TestCompileResult<?, ?> actualCompileResult =
         compilationSpecification.isCfToCf() ? convertCompileResultToDex() : compileResult;
 
-    if (customLibrarySpecification != null) {
-      Path customLib =
-          test.testForD8()
-              .addProgramClasses(customLibrarySpecification.getClasses())
-              .setMinApi(customLibrarySpecification.getMinApi())
-              .compile()
-              .writeToZip();
-      actualCompileResult.addRunClasspathFiles(customLib);
+    if (customLibCompile != null) {
+      actualCompileResult.addRunClasspathFiles(customLibCompile.writeToZip());
     }
 
     actualCompileResult.addRunClasspathFiles(desugaredLibrary);