Add tests for pruning interfaces without changing generic signature
Bug: 187885797
Change-Id: If43149c65024375f9c772702ff3f5ad2a57129a4
diff --git a/src/test/java/com/android/tools/r8/graph/genericsignature/GenericSignaturePrunedInterfacesKeepTest.java b/src/test/java/com/android/tools/r8/graph/genericsignature/GenericSignaturePrunedInterfacesKeepTest.java
new file mode 100644
index 0000000..3595ede
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/graph/genericsignature/GenericSignaturePrunedInterfacesKeepTest.java
@@ -0,0 +1,75 @@
+// 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.graph.genericsignature;
+
+import com.android.tools.r8.TestBase;
+import com.android.tools.r8.TestParameters;
+import com.android.tools.r8.TestParametersCollection;
+import java.lang.reflect.Type;
+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 GenericSignaturePrunedInterfacesKeepTest extends TestBase {
+
+ private final TestParameters parameters;
+ private final String[] EXPECTED =
+ new String[] {
+ "interface com.android.tools.r8.graph.genericsignature"
+ + ".GenericSignaturePrunedInterfacesKeepTest$J",
+ "interface com.android.tools.r8.graph.genericsignature"
+ + ".GenericSignaturePrunedInterfacesKeepTest$I",
+ "com.android.tools.r8.graph.genericsignature"
+ + ".GenericSignaturePrunedInterfacesKeepTest$J<java.lang.Object>"
+ };
+
+ @Parameters(name = "{0}")
+ public static TestParametersCollection data() {
+ return getTestParameters().withAllRuntimesAndApiLevels().build();
+ }
+
+ public GenericSignaturePrunedInterfacesKeepTest(TestParameters parameters) {
+ this.parameters = parameters;
+ }
+
+ @Test
+ public void testR8() throws Exception {
+ testForR8(parameters.getBackend())
+ .addInnerClasses(getClass())
+ .setMinApi(parameters.getApiLevel())
+ .addKeepAllClassesRule()
+ .addKeepAttributeSignature()
+ .addKeepAttributeInnerClassesAndEnclosingMethod()
+ .run(parameters.getRuntime(), Main.class)
+ .assertSuccessWithOutputLines(EXPECTED);
+ }
+
+ public interface I {}
+
+ public interface J<T> {}
+
+ public static class A implements I {}
+
+ public static class B extends A implements I, J<Object> {
+
+ public static void foo() {
+ for (Type genericInterface : B.class.getInterfaces()) {
+ System.out.println(genericInterface);
+ }
+ for (Type genericInterface : B.class.getGenericInterfaces()) {
+ System.out.println(genericInterface);
+ }
+ }
+ }
+
+ public static class Main {
+
+ public static void main(String[] args) {
+ B.foo();
+ }
+ }
+}
diff --git a/src/test/java/com/android/tools/r8/graph/genericsignature/GenericSignaturePrunedInterfacesTest.java b/src/test/java/com/android/tools/r8/graph/genericsignature/GenericSignaturePrunedInterfacesTest.java
new file mode 100644
index 0000000..41cc438
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/graph/genericsignature/GenericSignaturePrunedInterfacesTest.java
@@ -0,0 +1,79 @@
+// 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.graph.genericsignature;
+
+import com.android.tools.r8.NeverInline;
+import com.android.tools.r8.TestBase;
+import com.android.tools.r8.TestParameters;
+import com.android.tools.r8.TestParametersCollection;
+import java.lang.reflect.Type;
+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 GenericSignaturePrunedInterfacesTest extends TestBase {
+
+ private final TestParameters parameters;
+ private final String[] EXPECTED =
+ new String[] {
+ "interface com.android.tools.r8.graph.genericsignature"
+ + ".GenericSignaturePrunedInterfacesTest$J",
+ "interface com.android.tools.r8.graph.genericsignature"
+ + ".GenericSignaturePrunedInterfacesTest$I",
+ "com.android.tools.r8.graph.genericsignature"
+ + ".GenericSignaturePrunedInterfacesTest$J<java.lang.Object>"
+ };
+
+ @Parameters(name = "{0}")
+ public static TestParametersCollection data() {
+ return getTestParameters().withAllRuntimesAndApiLevels().build();
+ }
+
+ public GenericSignaturePrunedInterfacesTest(TestParameters parameters) {
+ this.parameters = parameters;
+ }
+
+ @Test
+ public void testR8() throws Exception {
+ testForR8Compat(parameters.getBackend())
+ .addInnerClasses(getClass())
+ .setMinApi(parameters.getApiLevel())
+ .addKeepMainRule(Main.class)
+ .addKeepClassRules(I.class, J.class, A.class)
+ .addKeepAttributeSignature()
+ .addKeepAttributeInnerClassesAndEnclosingMethod()
+ .enableInliningAnnotations()
+ .run(parameters.getRuntime(), Main.class)
+ .assertSuccessWithOutputLines(EXPECTED);
+ }
+
+ public interface I {}
+
+ public interface J<T> {}
+
+ public static class A implements I {}
+
+ public static class B extends A implements I, J<Object> {
+
+ @NeverInline
+ public static void foo() {
+ for (Type genericInterface : B.class.getInterfaces()) {
+ System.out.println(genericInterface);
+ }
+ for (Type genericInterface : B.class.getGenericInterfaces()) {
+ System.out.println(genericInterface);
+ }
+ }
+ }
+
+ public static class Main {
+
+ public static void main(String[] args) {
+ B.foo();
+ }
+ }
+}