Add test for not removing generic signatures with -dontoptimize

Bug: 221404266
Change-Id: If06dd75c75c559de54f42497e7762fa9f4de1d59
diff --git a/src/test/java/com/android/tools/r8/graph/genericsignature/GenericSignatureDontOptimizeTest.java b/src/test/java/com/android/tools/r8/graph/genericsignature/GenericSignatureDontOptimizeTest.java
new file mode 100644
index 0000000..7c55472
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/graph/genericsignature/GenericSignatureDontOptimizeTest.java
@@ -0,0 +1,82 @@
+// 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.graph.genericsignature;
+
+import static com.android.tools.r8.utils.codeinspector.Matchers.isPresent;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.Assert.assertEquals;
+
+import com.android.tools.r8.TestBase;
+import com.android.tools.r8.TestParameters;
+import com.android.tools.r8.TestParametersCollection;
+import com.android.tools.r8.shaking.ProguardKeepAttributes;
+import com.android.tools.r8.utils.codeinspector.ClassSubject;
+import com.android.tools.r8.utils.codeinspector.CodeInspector;
+import java.lang.reflect.ParameterizedType;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameters;
+
+@RunWith(Parameterized.class)
+// This is a reproduction of b/221404266.
+public class GenericSignatureDontOptimizeTest extends TestBase {
+
+  private final TestParameters parameters;
+  private final String[] EXPECTED = new String[] {"class java.lang.String"};
+
+  @Parameters(name = "{0}")
+  public static TestParametersCollection data() {
+    return getTestParameters().withAllRuntimesAndApiLevels().build();
+  }
+
+  public GenericSignatureDontOptimizeTest(TestParameters parameters) {
+    this.parameters = parameters;
+  }
+
+  @Test
+  public void testR8() throws Exception {
+    testForR8(parameters.getBackend())
+        .addInnerClasses(getClass())
+        .setMinApi(parameters.getApiLevel())
+        .addKeepAttributes(ProguardKeepAttributes.SIGNATURE)
+        .addKeepClassRules(Foo.class)
+        .addKeepMainRule(Main.class)
+        .addDontOptimize()
+        .run(parameters.getRuntime(), Main.class)
+        .inspect(this::inspect)
+        // TODO(b/221404266): We should fail with an exception since the parameterized generic type
+        //   should be removed.
+        .assertSuccessWithOutputLines(EXPECTED);
+  }
+
+  private void inspect(CodeInspector inspector) {
+    ClassSubject foo = inspector.clazz(Foo.class);
+    assertThat(foo, isPresent());
+    assertEquals("<T:Ljava/lang/Object;>Ljava/lang/Object;", foo.getFinalSignatureAttribute());
+
+    // TODO(b/221404266): We should not keep generic signatures when having -dontoptimize
+    ClassSubject main$1 = inspector.clazz(Main.class.getTypeName() + "$1");
+    assertThat(main$1, isPresent());
+    assertEquals(
+        "L" + binaryName(Foo.class) + "<Ljava/lang/String;>;", main$1.getFinalSignatureAttribute());
+  }
+
+  public static class Foo<T> {
+
+    public void print() {
+      ParameterizedType genericSuperclass =
+          (ParameterizedType) this.getClass().getGenericSuperclass();
+      System.out.println(genericSuperclass.getActualTypeArguments()[0].toString());
+    }
+  }
+
+  public static class Main {
+
+    public static void main(String[] args) {
+      (new Foo<String>() {}).print();
+    }
+  }
+}