Add test for removing values for enum in annotation
Bug: 174826328
Change-Id: Ifdc8227f4153c6ead369b615331574f734c9a3ca
diff --git a/src/test/java/com/android/tools/r8/SingleTestRunResult.java b/src/test/java/com/android/tools/r8/SingleTestRunResult.java
index 7f04357..875e50d 100644
--- a/src/test/java/com/android/tools/r8/SingleTestRunResult.java
+++ b/src/test/java/com/android/tools/r8/SingleTestRunResult.java
@@ -182,6 +182,14 @@
return self();
}
+ public RR forCfRuntime(Consumer<RR> action) {
+ if (runtime.isCf()) {
+ action.accept(self());
+ executedSatisfyingRuntime = true;
+ }
+ return self();
+ }
+
public RR otherwise(Consumer<RR> action) {
if (!executedSatisfyingRuntime) {
action.accept(self());
diff --git a/src/test/java/com/android/tools/r8/shaking/enums/EnumInAnnotationTest.java b/src/test/java/com/android/tools/r8/shaking/enums/EnumInAnnotationTest.java
new file mode 100644
index 0000000..a3b3b86
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/shaking/enums/EnumInAnnotationTest.java
@@ -0,0 +1,74 @@
+// 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.shaking.enums;
+
+import com.android.tools.r8.TestBase;
+import com.android.tools.r8.TestParameters;
+import com.android.tools.r8.TestParametersCollection;
+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;
+import org.junit.runners.Parameterized.Parameters;
+
+@RunWith(Parameterized.class)
+public class EnumInAnnotationTest extends TestBase {
+
+ private final TestParameters parameters;
+
+ @Parameters(name = "{0}")
+ public static TestParametersCollection data() {
+ return getTestParameters().withAllRuntimesAndApiLevels().build();
+ }
+
+ public EnumInAnnotationTest(TestParameters parameters) {
+ this.parameters = parameters;
+ }
+
+ @Test
+ public void testRuntime() throws Exception {
+ testForRuntime(parameters)
+ .addInnerClasses(EnumInAnnotationTest.class)
+ .run(parameters.getRuntime(), Main.class)
+ .assertSuccessWithOutputLines("TEST_ONE");
+ }
+
+ @Test
+ public void testR8() throws Exception {
+ testForR8(parameters.getBackend())
+ .addInnerClasses(EnumInAnnotationTest.class)
+ .addKeepMainRule(Main.class)
+ .setMinApi(parameters.getApiLevel())
+ .addKeepRuntimeVisibleAnnotations()
+ .run(parameters.getRuntime(), Main.class)
+ .forCfRuntime(
+ result ->
+ result.assertFailureWithErrorThatThrows(EnumConstantNotPresentException.class))
+ .otherwise(result -> result.assertSuccessWithOutputLines("TEST_ONE"));
+ }
+
+ public enum Enum {
+ TEST_ONE,
+ TEST_TWO
+ }
+
+ @Retention(RetentionPolicy.RUNTIME)
+ @Target({ElementType.TYPE})
+ public @interface MyAnnotation {
+
+ Enum value();
+ }
+
+ @MyAnnotation(value = Enum.TEST_ONE)
+ public static class Main {
+
+ public static void main(String[] args) {
+ System.out.println(Main.class.getAnnotation(MyAnnotation.class).value());
+ }
+ }
+}