Convert desugared library test
Bug: b:231287675
Change-Id: Ic1fcdf23e4ed46eec55999a74f8c9a5aa8d570fe
diff --git a/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/InvalidLibraryTest.java b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/InvalidLibraryTest.java
index 760fc1c..397a46f 100644
--- a/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/InvalidLibraryTest.java
+++ b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/InvalidLibraryTest.java
@@ -4,104 +4,95 @@
package com.android.tools.r8.desugar.desugaredlibrary;
+import static com.android.tools.r8.desugar.desugaredlibrary.test.CompilationSpecification.D8_L8DEBUG;
+import static com.android.tools.r8.desugar.desugaredlibrary.test.CompilationSpecification.D8_L8SHRINK;
+import static com.android.tools.r8.desugar.desugaredlibrary.test.LibraryDesugaringSpecification.JDK8;
+import static com.android.tools.r8.desugar.desugaredlibrary.test.LibraryDesugaringSpecification.getJdk8Jdk11;
import static org.hamcrest.core.StringContains.containsString;
+import com.android.tools.r8.CompilationFailedException;
import com.android.tools.r8.TestDiagnosticMessages;
import com.android.tools.r8.TestParameters;
+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.LibraryDesugaringSpecification;
import com.android.tools.r8.errors.InvalidLibrarySuperclassDiagnostic;
import com.android.tools.r8.utils.AndroidApiLevel;
-import com.android.tools.r8.utils.BooleanUtils;
import com.android.tools.r8.utils.StringUtils;
+import com.google.common.collect.ImmutableList;
+import java.io.IOException;
import java.nio.file.Path;
import java.time.Instant;
import java.util.Date;
import java.util.List;
import org.junit.Assume;
-import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameters;
@RunWith(Parameterized.class)
public class InvalidLibraryTest extends DesugaredLibraryTestBase {
- private static Path customLib;
- private static Path superclassAsClasspath;
private static final String EXPECTED_RESULT =
StringUtils.lines("1970-01-02T10:17:36.789Z", "1970-01-12T10:20:54.321123456Z");
private static final String INVALID_RESULT =
StringUtils.lines("1970-01-02T10:17:36.789Z", "1970-01-12T10:20:54.321Z");
private final TestParameters parameters;
- private final boolean shrinkDesugaredLibrary;
+ private final LibraryDesugaringSpecification libraryDesugaringSpecification;
+ private final CompilationSpecification compilationSpecification;
- @Parameterized.Parameters(name = "{1}, shrinkDesugaredLibrary: {0}")
+ @Parameters(name = "{0}, spec: {1}, {2}")
public static List<Object[]> data() {
return buildParameters(
- BooleanUtils.values(), getTestParameters().withDexRuntimes().withAllApiLevels().build());
+ getTestParameters().withDexRuntimes().withAllApiLevels().build(),
+ getJdk8Jdk11(),
+ ImmutableList.of(D8_L8DEBUG, D8_L8SHRINK));
}
- public InvalidLibraryTest(boolean shrinkDesugaredLibrary, TestParameters parameters) {
- this.shrinkDesugaredLibrary = shrinkDesugaredLibrary;
+ public InvalidLibraryTest(
+ TestParameters parameters,
+ LibraryDesugaringSpecification libraryDesugaringSpecification,
+ CompilationSpecification compilationSpecification) {
this.parameters = parameters;
+ this.libraryDesugaringSpecification = libraryDesugaringSpecification;
+ this.compilationSpecification = compilationSpecification;
}
- @BeforeClass
- public static void compileCustomLib() throws Exception {
- customLib =
- testForD8(getStaticTemp())
- .addProgramClasses(CustomLibraryClass.class)
- .setMinApi(AndroidApiLevel.B)
- .compile()
- .writeToZip();
- superclassAsClasspath =
- testForD8(getStaticTemp())
- .addProgramClasses(SuperLibraryClass.class)
- .setMinApi(AndroidApiLevel.B)
- .compile()
- .writeToZip();
+ private Path getSuperclassAsClasspath() throws CompilationFailedException, IOException {
+ return testForD8(getStaticTemp())
+ .addProgramClasses(SuperLibraryClass.class)
+ .setMinApi(AndroidApiLevel.B)
+ .compile()
+ .writeToZip();
}
@Test
public void testProgramSupertype() throws Exception {
- KeepRuleConsumer keepRuleConsumer = createKeepRuleConsumer(parameters);
- testForD8()
- .addLibraryFiles(getLibraryFile())
- .setMinApi(parameters.getApiLevel())
+ testForDesugaredLibrary(parameters, libraryDesugaringSpecification, compilationSpecification)
.addProgramClasses(
Executor.class, SuperLibraryClass.class, LocalClass.class, LocalClassOverride.class)
- .addLibraryClasses(CustomLibraryClass.class)
- .enableCoreLibraryDesugaring(parameters.getApiLevel(), keepRuleConsumer)
- .compile()
- .addDesugaredCoreLibraryRunClassPath(
- this::buildDesugaredLibrary,
- parameters.getApiLevel(),
- keepRuleConsumer.get(),
- shrinkDesugaredLibrary)
- .addRunClasspathFiles(customLib)
+ .setCustomLibrarySpecification(
+ new CustomLibrarySpecification(CustomLibraryClass.class, AndroidApiLevel.B))
+ .addKeepMainRule(Executor.class)
.run(parameters.getRuntime(), Executor.class)
.assertSuccessWithOutput(EXPECTED_RESULT);
}
@Test
public void testClasspathSupertype() throws Exception {
- Assume.assumeTrue(requiresAnyCoreLibDesugaring(parameters));
- KeepRuleConsumer keepRuleConsumer = createKeepRuleConsumer(parameters);
- testForD8()
- .addLibraryFiles(getLibraryFile())
- .setMinApi(parameters.getApiLevel())
+ Assume.assumeTrue(
+ requiresAnyCoreLibDesugaring(
+ parameters.getApiLevel(), libraryDesugaringSpecification != JDK8));
+ testForDesugaredLibrary(parameters, libraryDesugaringSpecification, compilationSpecification)
.addProgramClasses(Executor.class, LocalClass.class, LocalClassOverride.class)
.addClasspathClasses(SuperLibraryClass.class)
- .addLibraryClasses(CustomLibraryClass.class)
- .enableCoreLibraryDesugaring(parameters.getApiLevel(), keepRuleConsumer)
+ .setCustomLibrarySpecification(
+ new CustomLibrarySpecification(CustomLibraryClass.class, AndroidApiLevel.B))
+ .addKeepMainRule(Executor.class)
.compile()
- .inspectDiagnosticMessages(this::assertWarningInvalidLibrary)
- .addDesugaredCoreLibraryRunClassPath(
- this::buildDesugaredLibrary,
- parameters.getApiLevel(),
- keepRuleConsumer.get(),
- shrinkDesugaredLibrary)
- .addRunClasspathFiles(customLib, superclassAsClasspath)
+ .addRunClasspathFiles(getSuperclassAsClasspath())
.run(parameters.getRuntime(), Executor.class)
// The code requires desugaring to be run correctly, but with the classpath superclass,
// desugaring is incorrectly performed. The code therefore falls-backs to the default
@@ -112,22 +103,17 @@
@Test
public void testNullSupertype() throws Exception {
- Assume.assumeTrue(requiresAnyCoreLibDesugaring(parameters));
- KeepRuleConsumer keepRuleConsumer = createKeepRuleConsumer(parameters);
- testForD8()
- .addLibraryFiles(getLibraryFile())
- .setMinApi(parameters.getApiLevel())
+ Assume.assumeTrue(
+ requiresAnyCoreLibDesugaring(
+ parameters.getApiLevel(), libraryDesugaringSpecification != JDK8));
+ testForDesugaredLibrary(parameters, libraryDesugaringSpecification, compilationSpecification)
.addProgramClasses(Executor.class, LocalClass.class, LocalClassOverride.class)
- .addLibraryClasses(CustomLibraryClass.class)
- .enableCoreLibraryDesugaring(parameters.getApiLevel(), keepRuleConsumer)
+ .setCustomLibrarySpecification(
+ new CustomLibrarySpecification(CustomLibraryClass.class, AndroidApiLevel.B))
+ .addKeepMainRule(Executor.class)
.compile()
.inspectDiagnosticMessages(this::assertWarningInvalidLibrary)
- .addDesugaredCoreLibraryRunClassPath(
- this::buildDesugaredLibrary,
- parameters.getApiLevel(),
- keepRuleConsumer.get(),
- shrinkDesugaredLibrary)
- .addRunClasspathFiles(customLib, superclassAsClasspath)
+ .addRunClasspathFiles(getSuperclassAsClasspath())
.run(parameters.getRuntime(), Executor.class)
// The code requires desugaring to be run correctly, but with the missing supertype,
// desugaring could not be performed and the code cannot simply run (Warning was raised).
diff --git a/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/IteratorTest.java b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/IteratorTest.java
index 3664076..605854a 100644
--- a/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/IteratorTest.java
+++ b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/IteratorTest.java
@@ -4,20 +4,19 @@
package com.android.tools.r8.desugar.desugaredlibrary;
-import static com.android.tools.r8.DiagnosticsMatcher.diagnosticMessage;
+import static com.android.tools.r8.desugar.desugaredlibrary.test.CompilationSpecification.D8CF2CF_L8DEBUG;
+import static com.android.tools.r8.desugar.desugaredlibrary.test.CompilationSpecification.D8_L8DEBUG;
+import static com.android.tools.r8.desugar.desugaredlibrary.test.CompilationSpecification.D8_L8SHRINK;
+import static com.android.tools.r8.desugar.desugaredlibrary.test.LibraryDesugaringSpecification.getJdk8Jdk11;
import static junit.framework.TestCase.assertEquals;
-import static junit.framework.TestCase.fail;
-import static org.hamcrest.CoreMatchers.containsString;
-import com.android.tools.r8.CompilationFailedException;
-import com.android.tools.r8.LibraryDesugaringTestConfiguration.AbsentKeepRuleConsumer;
import com.android.tools.r8.TestParameters;
-import com.android.tools.r8.utils.AndroidApiLevel;
-import com.android.tools.r8.utils.BooleanUtils;
-import com.android.tools.r8.utils.DescriptorUtils;
+import com.android.tools.r8.desugar.desugaredlibrary.test.CompilationSpecification;
+import com.android.tools.r8.desugar.desugaredlibrary.test.LibraryDesugaringSpecification;
import com.android.tools.r8.utils.StringUtils;
-import com.android.tools.r8.utils.ZipUtils;
-import java.nio.file.Path;
+import com.android.tools.r8.utils.codeinspector.ClassSubject;
+import com.android.tools.r8.utils.codeinspector.CodeInspector;
+import com.google.common.collect.ImmutableList;
import java.util.Iterator;
import java.util.List;
import java.util.function.Consumer;
@@ -29,22 +28,28 @@
@RunWith(Parameterized.class)
public class IteratorTest extends DesugaredLibraryTestBase {
- private final TestParameters parameters;
- private final boolean shrinkDesugaredLibrary;
- private final boolean canUseDefaultAndStaticInterfaceMethods;
-
private static final String EXPECTED_OUTPUT = StringUtils.lines("1", "2", "3");
- @Parameters(name = "{1}, shrinkDesugaredLibrary: {0}")
+ private final TestParameters parameters;
+ private final LibraryDesugaringSpecification libraryDesugaringSpecification;
+ private final CompilationSpecification compilationSpecification;
+ private final boolean canUseDefaultAndStaticInterfaceMethods;
+
+ @Parameters(name = "{0}, spec: {1}, {2}")
public static List<Object[]> data() {
return buildParameters(
- BooleanUtils.values(),
- getTestParameters().withAllRuntimes().withAllApiLevelsAlsoForCf().build());
+ getTestParameters().withAllRuntimes().withAllApiLevelsAlsoForCf().build(),
+ getJdk8Jdk11(),
+ ImmutableList.of(D8_L8DEBUG, D8_L8SHRINK, D8CF2CF_L8DEBUG));
}
- public IteratorTest(boolean shrinkDesugaredLibrary, TestParameters parameters) {
- this.shrinkDesugaredLibrary = shrinkDesugaredLibrary;
+ public IteratorTest(
+ TestParameters parameters,
+ LibraryDesugaringSpecification libraryDesugaringSpecification,
+ CompilationSpecification compilationSpecification) {
this.parameters = parameters;
+ this.libraryDesugaringSpecification = libraryDesugaringSpecification;
+ this.compilationSpecification = compilationSpecification;
this.canUseDefaultAndStaticInterfaceMethods =
parameters
.getApiLevel()
@@ -52,126 +57,28 @@
}
@Test
- public void testIterator() throws Exception {
- if (parameters.isCfRuntime()) {
- testForJvm()
- .addInnerClasses(IteratorTest.class)
- .run(parameters.getRuntime(), Main.class)
- .assertSuccessWithOutput(EXPECTED_OUTPUT);
- return;
- }
- KeepRuleConsumer keepRuleConsumer = createKeepRuleConsumer(parameters);
- testForD8()
- .addLibraryFiles(getLibraryFile())
- .addInnerClasses(IteratorTest.class)
- .setMinApi(parameters.getApiLevel())
- .enableCoreLibraryDesugaring(parameters.getApiLevel(), keepRuleConsumer)
+ public void testIterator() throws Throwable {
+ testForDesugaredLibrary(parameters, libraryDesugaringSpecification, compilationSpecification)
+ .addInnerClasses(getClass())
+ .addKeepMainRule(Main.class)
.compile()
- .addDesugaredCoreLibraryRunClassPath(
- this::buildDesugaredLibrary,
- parameters.getApiLevel(),
- keepRuleConsumer.get(),
- shrinkDesugaredLibrary)
+ .inspect(this::assertInterface)
.run(parameters.getRuntime(), Main.class)
.assertSuccessWithOutput(EXPECTED_OUTPUT);
}
- @Test
- public void testD8Cf() throws Exception {
- // Use D8 to desugar with Java classfile output.
- Path firstJar =
- testForD8(Backend.CF)
- .setMinApi(parameters.getApiLevel())
- .addProgramClasses(Main.class, MyIterator.class)
- .enableCoreLibraryDesugaring(parameters.getApiLevel(), new AbsentKeepRuleConsumer())
- .compile()
- .writeToZip();
-
- ClassFileInfo info =
- extractClassFileInfo(
- ZipUtils.readSingleEntry(firstJar, ZipUtils.zipEntryNameForClass(MyIterator.class)));
- assertEquals(
- MyIterator.class.getTypeName(),
- DescriptorUtils.getJavaTypeFromBinaryName(info.getClassBinaryName()));
+ private void assertInterface(CodeInspector inspector) {
+ ClassSubject clazz = inspector.clazz(MyIterator.class);
assertEquals(
canUseDefaultAndStaticInterfaceMethods ? 0 : 1,
- info.getInterfaces().stream().filter(name -> name.equals("j$/util/Iterator")).count());
+ clazz.getDexProgramClass().getInterfaces().stream()
+ .filter(name -> name.toString().equals("j$.util.Iterator"))
+ .count());
assertEquals(
canUseDefaultAndStaticInterfaceMethods ? 1 : 2,
- info.getMethodNames().stream().filter(name -> name.equals("forEachRemaining")).count());
-
- AndroidApiLevel apiLevelNotRequiringDesugaring = AndroidApiLevel.N;
- if (parameters.getApiLevel().isLessThan(apiLevelNotRequiringDesugaring)) {
- try {
- // Use D8 to desugar with Java classfile output.
- testForD8(Backend.CF)
- .setMinApi(parameters.getApiLevel())
- .addProgramFiles(firstJar)
- .enableCoreLibraryDesugaring(parameters.getApiLevel(), new AbsentKeepRuleConsumer())
- .compileWithExpectedDiagnostics(
- diagnostics ->
- diagnostics.assertErrorsMatch(
- diagnosticMessage(
- containsString(
- "Code has already been library desugared. "
- + "Interface Lj$/util/Iterator; is already implemented by "
- + "Lcom/android/tools/r8/desugar/desugaredlibrary/"
- + "IteratorTest$MyIterator;"))));
- fail("Expected failure");
- } catch (CompilationFailedException e) {
- // Expected.
- }
- }
-
- // Use D8 to desugar with Java classfile output.
- Path secondJar =
- testForD8(Backend.CF)
- .addOptionsModification(
- options ->
- options.desugarSpecificOptions().allowAllDesugaredInput =
- parameters.getApiLevel().isLessThan(apiLevelNotRequiringDesugaring))
- .setMinApi(parameters.getApiLevel())
- .addProgramFiles(firstJar)
- .enableCoreLibraryDesugaring(parameters.getApiLevel(), new AbsentKeepRuleConsumer())
- .compile()
- .writeToZip();
-
- info =
- extractClassFileInfo(
- ZipUtils.readSingleEntry(secondJar, ZipUtils.zipEntryNameForClass(MyIterator.class)));
- assertEquals(
- MyIterator.class.getTypeName(),
- DescriptorUtils.getJavaTypeFromBinaryName(info.getClassBinaryName()));
- assertEquals(
- canUseDefaultAndStaticInterfaceMethods ? 0 : 1,
- info.getInterfaces().stream().filter(name -> name.equals("j$/util/Iterator")).count());
- assertEquals(
- canUseDefaultAndStaticInterfaceMethods ? 1 : 2,
- info.getMethodNames().stream().filter(name -> name.equals("forEachRemaining")).count());
-
- if (parameters.getRuntime().isDex()) {
- // Convert to DEX without desugaring and run.
- testForD8()
- .addProgramFiles(firstJar)
- .setMinApi(parameters.getApiLevel())
- .disableDesugaring()
- .compile()
- .addDesugaredCoreLibraryRunClassPath(
- this::buildDesugaredLibrary,
- parameters.getApiLevel(),
- collectKeepRulesWithTraceReferences(
- firstJar, buildDesugaredLibraryClassFile(parameters.getApiLevel())),
- shrinkDesugaredLibrary)
- .run(parameters.getRuntime(), Main.class)
- .assertSuccessWithOutput(EXPECTED_OUTPUT);
- } else {
- // Run on the JVM with desugared library on classpath.
- testForJvm()
- .addProgramFiles(firstJar)
- .addRunClasspathFiles(buildDesugaredLibraryClassFile(parameters.getApiLevel()))
- .run(parameters.getRuntime(), Main.class)
- .assertSuccessWithOutput(EXPECTED_OUTPUT);
- }
+ clazz.getDexProgramClass().allMethodsSorted().stream()
+ .filter(m -> m.getReference().getName().toString().equals("forEachRemaining"))
+ .count());
}
static class Main {
diff --git a/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/J$ExtensionTest.java b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/J$ExtensionTest.java
index 371443d..18d1613 100644
--- a/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/J$ExtensionTest.java
+++ b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/J$ExtensionTest.java
@@ -4,6 +4,8 @@
package com.android.tools.r8.desugar.desugaredlibrary;
+import static com.android.tools.r8.desugar.desugaredlibrary.test.CompilationSpecification.D8_L8DEBUG;
+import static com.android.tools.r8.desugar.desugaredlibrary.test.LibraryDesugaringSpecification.getJdk8Jdk11;
import static junit.framework.TestCase.assertTrue;
import com.android.tools.r8.TestParameters;
@@ -11,6 +13,9 @@
import com.android.tools.r8.TestRuntime.CfVm;
import com.android.tools.r8.ToolHelper;
import com.android.tools.r8.ToolHelper.DexVm.Version;
+import com.android.tools.r8.desugar.desugaredlibrary.test.CompilationSpecification;
+import com.android.tools.r8.desugar.desugaredlibrary.test.LibraryDesugaringSpecification;
+import com.google.common.collect.ImmutableList;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
@@ -27,8 +32,6 @@
@RunWith(Parameterized.class)
public class J$ExtensionTest extends DesugaredLibraryTestBase {
- private final TestParameters parameters;
-
private static final String MAIN_CLASS_NAME = "Main";
private static final String MAIN_CLASS =
"import java.time.LocalTimeAccess;\n"
@@ -51,13 +54,25 @@
+ "}";
private static Path[] compiledClasses = new Path[2];
- @Parameters(name = "{0}")
+ private final TestParameters parameters;
+ private final LibraryDesugaringSpecification libraryDesugaringSpecification;
+ private final CompilationSpecification compilationSpecification;
+
+ @Parameters(name = "{0}, spec: {1}, {2}")
public static List<Object[]> data() {
- return buildParameters(getTestParameters().withAllRuntimes().withAllApiLevels().build());
+ return buildParameters(
+ getTestParameters().withAllRuntimes().withAllApiLevels().build(),
+ getJdk8Jdk11(),
+ ImmutableList.of(D8_L8DEBUG));
}
- public J$ExtensionTest(TestParameters parameters) {
+ public J$ExtensionTest(
+ TestParameters parameters,
+ LibraryDesugaringSpecification libraryDesugaringSpecification,
+ CompilationSpecification compilationSpecification) {
this.parameters = parameters;
+ this.libraryDesugaringSpecification = libraryDesugaringSpecification;
+ this.compilationSpecification = compilationSpecification;
}
@BeforeClass
@@ -134,21 +149,13 @@
}
@Test
- public void testJ$ExtensionDesugaring() throws Exception {
+ public void testJ$ExtensionDesugaring() throws Throwable {
Assume.assumeFalse(parameters.isCfRuntime());
- // Above O no desugaring is required.
Assume.assumeTrue(requiresTimeDesugaring(parameters));
- KeepRuleConsumer keepRuleConsumer = createKeepRuleConsumer(parameters);
-
String stdErr =
- testForD8()
- .addLibraryFiles(getLibraryFile())
+ testForDesugaredLibrary(
+ parameters, libraryDesugaringSpecification, compilationSpecification)
.addProgramFiles(compiledClasses)
- .setMinApi(parameters.getApiLevel())
- .enableCoreLibraryDesugaring(parameters.getApiLevel(), keepRuleConsumer)
- .compile()
- .addDesugaredCoreLibraryRunClassPath(
- this::buildDesugaredLibrary, parameters.getApiLevel())
.run(parameters.getRuntime(), MAIN_CLASS_NAME)
.assertFailure()
.getStdErr();
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 c202629..3588344 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
@@ -120,6 +120,11 @@
return this;
}
+ public DesugaredLibraryTestBuilder<T> addClasspathClasses(Class<?>... clazz) throws IOException {
+ builder.addClasspathClasses(clazz);
+ return this;
+ }
+
public DesugaredLibraryTestBuilder<T> addProgramClasses(Class<?>... clazz) throws IOException {
builder.addProgramClasses(clazz);
return this;