Add reproduction for not rewriting enums in annotations

Bug: 174742877
Change-Id: Ie189db7954db17456e5080bde8608ea509227f54
diff --git a/src/test/java/com/android/tools/r8/SingleTestRunResult.java b/src/test/java/com/android/tools/r8/SingleTestRunResult.java
index c03c8e05..7f04357 100644
--- a/src/test/java/com/android/tools/r8/SingleTestRunResult.java
+++ b/src/test/java/com/android/tools/r8/SingleTestRunResult.java
@@ -8,6 +8,7 @@
 import static org.junit.Assert.assertNotEquals;
 import static org.junit.Assert.assertNotNull;
 
+import com.android.tools.r8.ToolHelper.DexVm;
 import com.android.tools.r8.ToolHelper.ProcessResult;
 import com.android.tools.r8.naming.retrace.StackTrace;
 import com.android.tools.r8.utils.AndroidApp;
@@ -16,6 +17,8 @@
 import java.io.IOException;
 import java.io.PrintStream;
 import java.util.concurrent.ExecutionException;
+import java.util.function.Consumer;
+import java.util.function.Predicate;
 import org.hamcrest.Matcher;
 
 public abstract class SingleTestRunResult<RR extends SingleTestRunResult<RR>>
@@ -23,6 +26,7 @@
   protected final AndroidApp app;
   private final TestRuntime runtime;
   private final ProcessResult result;
+  private boolean executedSatisfyingRuntime = false;
 
   public SingleTestRunResult(AndroidApp app, TestRuntime runtime, ProcessResult result) {
     this.app = app;
@@ -169,4 +173,19 @@
     ps.println(sb.toString());
     return self();
   }
+
+  public RR forDexRuntimeSatisfying(Predicate<DexVm.Version> predicate, Consumer<RR> action) {
+    if (runtime.isDex() && predicate.test(runtime.asDex().getVm().getVersion())) {
+      action.accept(self());
+      executedSatisfyingRuntime = true;
+    }
+    return self();
+  }
+
+  public RR otherwise(Consumer<RR> action) {
+    if (!executedSatisfyingRuntime) {
+      action.accept(self());
+    }
+    return self();
+  }
 }
diff --git a/src/test/java/com/android/tools/r8/TestRunResult.java b/src/test/java/com/android/tools/r8/TestRunResult.java
index 849e7d7..c775215 100644
--- a/src/test/java/com/android/tools/r8/TestRunResult.java
+++ b/src/test/java/com/android/tools/r8/TestRunResult.java
@@ -81,13 +81,6 @@
     return assertFailure();
   }
 
-  public RR assertFailureWithErrorThatMatchesIf(boolean condition, Matcher<String> matcher) {
-    if (condition) {
-      return assertFailureWithErrorThatMatches(matcher);
-    }
-    return self();
-  }
-
   public RR assertFailureWithOutput(String expected) {
     assertStdoutMatches(is(expected));
     return assertFailure();
diff --git a/src/test/java/com/android/tools/r8/repackage/EnumAndIdentifierBasedStringInAnnotationTest.java b/src/test/java/com/android/tools/r8/repackage/EnumAndIdentifierBasedStringInAnnotationTest.java
new file mode 100644
index 0000000..4f22f5c
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/repackage/EnumAndIdentifierBasedStringInAnnotationTest.java
@@ -0,0 +1,79 @@
+// Copyright (c) 2020, 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.repackage;
+
+import static com.android.tools.r8.utils.codeinspector.Matchers.isPresentAndRenamed;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+import com.android.tools.r8.TestParameters;
+import com.android.tools.r8.ToolHelper.DexVm.Version;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+@RunWith(Parameterized.class)
+public class EnumAndIdentifierBasedStringInAnnotationTest extends RepackageTestBase {
+
+  public EnumAndIdentifierBasedStringInAnnotationTest(
+      String flattenPackageHierarchyOrRepackageClasses, TestParameters parameters) {
+    super(flattenPackageHierarchyOrRepackageClasses, parameters);
+  }
+
+  @Test
+  public void testRuntime() throws Exception {
+    testForRuntime(parameters)
+        .addInnerClasses(EnumAndIdentifierBasedStringInAnnotationTest.class)
+        .run(parameters.getRuntime(), Main.class)
+        .assertSuccessWithOutputLines("TEST_ONE");
+  }
+
+  @Test
+  public void testR8() throws Exception {
+    testForR8(parameters.getBackend())
+        .addInnerClasses(EnumAndIdentifierBasedStringInAnnotationTest.class)
+        .addKeepMainRule(Main.class)
+        .addKeepClassAndMembersRules(MyAnnotation.class)
+        .addKeepClassAndMembersRulesWithAllowObfuscation(Enum.class)
+        .addKeepRules("-keepclassmembers,allowshrinking class ** { *; }")
+        .setMinApi(parameters.getApiLevel())
+        .addKeepRuntimeVisibleAnnotations()
+        .apply(this::configureRepackaging)
+        .compile()
+        .inspect(inspector -> assertThat(inspector.clazz(Enum.class), isPresentAndRenamed()))
+        .run(parameters.getRuntime(), Main.class)
+        // It is truly amazing that these two ART VMs can find the value with incorrect names :)
+        .forDexRuntimeSatisfying(
+            version ->
+                version.isNewerThanOrEqual(Version.V5_1_1)
+                    && version.isOlderThanOrEqual(Version.V6_0_1),
+            result -> result.assertSuccessWithOutputLines("TEST_ONE"))
+        // TODO(b/174742877): We should not throw an error.
+        .otherwise(result -> result.assertFailureWithErrorThatThrows(ClassNotFoundException.class));
+  }
+
+  public enum Enum {
+    TEST_ONE,
+    TEST_TWO
+  }
+
+  @Retention(RetentionPolicy.RUNTIME)
+  @Target({ElementType.TYPE})
+  public @interface MyAnnotation {
+
+    Enum value() default Enum.TEST_TWO;
+  }
+
+  @MyAnnotation(value = Enum.TEST_ONE)
+  public static class Main {
+
+    public static void main(String[] args) {
+      System.out.println(Main.class.getAnnotation(MyAnnotation.class).value());
+    }
+  }
+}