Add test for shared parent reserved field from different interfaces

Change-Id: I0bc0e21bd2ae7cd5fc0be2daad261d3dc077941c
diff --git a/src/main/java/com/android/tools/r8/naming/InterfaceMethodNameMinifier.java b/src/main/java/com/android/tools/r8/naming/InterfaceMethodNameMinifier.java
index 3ba2467..8067817 100644
--- a/src/main/java/com/android/tools/r8/naming/InterfaceMethodNameMinifier.java
+++ b/src/main/java/com/android/tools/r8/naming/InterfaceMethodNameMinifier.java
@@ -654,9 +654,8 @@
                             DexType frontierType = minifierState.getFrontier(clazz.type);
                             iState.addReservationType(frontierType);
                             // The reservation state should already be added, but if a class is
-                            // extending
-                            // an interface, we will not visit the class during the sub-type
-                            // traversel
+                            // extending an interface, we will not visit the class during the
+                            // sub-type traversel.
                             if (minifierState.getReservationState(clazz.type) == null) {
                               minifierState.allocateReservationStateAndReserve(
                                   clazz.type, frontierType);
diff --git a/src/test/java/com/android/tools/r8/naming/FieldSharedParentMinificationTest.java b/src/test/java/com/android/tools/r8/naming/FieldSharedParentMinificationTest.java
new file mode 100644
index 0000000..e3a8047
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/naming/FieldSharedParentMinificationTest.java
@@ -0,0 +1,83 @@
+// 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.naming;
+
+import static com.android.tools.r8.utils.codeinspector.Matchers.isPresentAndRenamed;
+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.utils.codeinspector.FieldSubject;
+import com.google.common.collect.ImmutableSet;
+import java.util.Set;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameter;
+import org.junit.runners.Parameterized.Parameters;
+
+@RunWith(Parameterized.class)
+public class FieldSharedParentMinificationTest extends TestBase {
+
+  @Parameter() public TestParameters parameters;
+
+  @Parameters(name = "{0}")
+  public static TestParametersCollection data() {
+    return getTestParameters().withAllRuntimesAndApiLevels().build();
+  }
+
+  @Test
+  public void testR8() throws Exception {
+    testForR8(parameters.getBackend())
+        .addInnerClasses(getClass())
+        .setMinApi(parameters.getApiLevel())
+        .addKeepMainRule(Main.class)
+        .addKeepClassAndMembersRulesWithAllowObfuscation(
+            I.class, J.class, A.class, B.class, C.class)
+        .run(parameters.getRuntime(), Main.class)
+        .assertSuccessWithOutputLines("true", "42", "Hello World!")
+        .inspect(
+            inspector -> {
+              FieldSubject foo = inspector.clazz(I.class).uniqueFieldWithName("foo");
+              FieldSubject bar = inspector.clazz(J.class).uniqueFieldWithName("bar");
+              FieldSubject baz = inspector.clazz(A.class).uniqueFieldWithName("baz");
+              assertThat(foo, isPresentAndRenamed());
+              assertThat(bar, isPresentAndRenamed());
+              assertThat(baz, isPresentAndRenamed());
+              Set<String> seenNames =
+                  ImmutableSet.of(foo.getFinalName(), bar.getFinalName(), baz.getFinalName());
+              assertEquals(ImmutableSet.of("a", "b", "c"), seenNames);
+            });
+  }
+
+  public interface I {
+
+    int foo = 42;
+  }
+
+  public interface J {
+    String bar = "Hello World!";
+  }
+
+  public static class A {
+
+    boolean baz = System.currentTimeMillis() > 0;
+  }
+
+  public static class B extends A implements I {}
+
+  public static class C extends A implements J {}
+
+  public static class Main {
+
+    public static void main(String[] args) {
+      System.out.println(new A().baz);
+      System.out.println(new B().foo);
+      System.out.println(new C().bar);
+    }
+  }
+}
diff --git a/src/test/java/com/android/tools/r8/naming/InterfaceFieldMinificationTest.java b/src/test/java/com/android/tools/r8/naming/InterfaceFieldMinificationTest.java
index ca294fa..f72ffa2 100644
--- a/src/test/java/com/android/tools/r8/naming/InterfaceFieldMinificationTest.java
+++ b/src/test/java/com/android/tools/r8/naming/InterfaceFieldMinificationTest.java
@@ -8,13 +8,27 @@
 import com.android.tools.r8.NeverInline;
 import com.android.tools.r8.NoVerticalClassMerging;
 import com.android.tools.r8.TestBase;
+import com.android.tools.r8.TestParameters;
+import com.android.tools.r8.TestParametersCollection;
 import com.android.tools.r8.naming.testclasses.Greeting;
 import com.android.tools.r8.utils.StringUtils;
 import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameter;
+import org.junit.runners.Parameterized.Parameters;
 
 /** Regression test for b/128600647. */
+@RunWith(Parameterized.class)
 public class InterfaceFieldMinificationTest extends TestBase {
 
+  @Parameter() public TestParameters parameters;
+
+  @Parameters(name = "{0}")
+  public static TestParametersCollection data() {
+    return getTestParameters().withDexRuntimes().withAllApiLevels().build();
+  }
+
   @Test
   public void test() throws Exception {
     String expectedOutput = StringUtils.lines("Greeter: Hello world!");
@@ -26,7 +40,8 @@
         .enableNeverClassInliningAnnotations()
         .enableInliningAnnotations()
         .enableNoVerticalClassMergingAnnotations()
-        .run(TestClass.class)
+        .setMinApi(parameters.getApiLevel())
+        .run(parameters.getRuntime(), TestClass.class)
         .assertSuccessWithOutput(expectedOutput);
   }