[Metadata] Add test for not recording a change in companion changing

Bug: b/248450861
Change-Id: I1d2b902f16c96b4299df38184404372f8e12d352
diff --git a/src/test/java/com/android/tools/r8/kotlin/metadata/MetadataRewriteRemovedCompanionTest.java b/src/test/java/com/android/tools/r8/kotlin/metadata/MetadataRewriteRemovedCompanionTest.java
new file mode 100644
index 0000000..2f27bf0
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/kotlin/metadata/MetadataRewriteRemovedCompanionTest.java
@@ -0,0 +1,111 @@
+// 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.kotlin.metadata;
+
+import static com.android.tools.r8.KotlinCompilerTool.KotlinCompilerVersion.MIN_SUPPORTED_VERSION;
+import static org.hamcrest.CoreMatchers.containsString;
+import static org.junit.Assert.assertThrows;
+
+import com.android.tools.r8.CompilationFailedException;
+import com.android.tools.r8.KotlinTestParameters;
+import com.android.tools.r8.TestParameters;
+import com.android.tools.r8.utils.StringUtils;
+import java.nio.file.Path;
+import java.util.Collection;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+@RunWith(Parameterized.class)
+public class MetadataRewriteRemovedCompanionTest extends KotlinMetadataTestBase {
+  private static final String EXPECTED = StringUtils.lines("Hello World!");
+
+  private final TestParameters parameters;
+
+  @Parameterized.Parameters(name = "{0}, {1}")
+  public static Collection<Object[]> data() {
+    return buildParameters(
+        getTestParameters().withCfRuntimes().build(),
+        getKotlinTestParameters()
+            .withCompilersStartingFromIncluding(MIN_SUPPORTED_VERSION)
+            .withAllTargetVersions()
+            .build());
+  }
+
+  public MetadataRewriteRemovedCompanionTest(
+      TestParameters parameters, KotlinTestParameters kotlinParameters) {
+    super(kotlinParameters);
+    this.parameters = parameters;
+  }
+
+  private static final KotlinCompileMemoizer companionRemoveJarMap =
+      getCompileMemoizer(getKotlinFileInTest(PKG_PREFIX + "/companion_remove_lib", "lib"));
+
+  @Test
+  public void smokeTest() throws Exception {
+    Path libJar = companionRemoveJarMap.getForConfiguration(kotlinc, targetVersion);
+
+    Path output =
+        kotlinc(parameters.getRuntime().asCf(), kotlinc, targetVersion)
+            .addClasspathFiles(libJar)
+            .addSourceFiles(getKotlinFileInTest(PKG_PREFIX + "/companion_remove_app", "main"))
+            .setOutputPath(temp.newFolder().toPath())
+            .compile();
+
+    testForJvm()
+        .addRunClasspathFiles(kotlinc.getKotlinStdlibJar(), libJar)
+        .addClasspath(output)
+        .run(parameters.getRuntime(), PKG + ".companion_remove_app.MainKt")
+        .assertSuccessWithOutput(EXPECTED);
+  }
+
+  @Test
+  public void testMetadataInCompanion_kept() throws Exception {
+    Path libJar =
+        testForR8(parameters.getBackend())
+            .addClasspathFiles(kotlinc.getKotlinStdlibJar(), kotlinc.getKotlinAnnotationJar())
+            .addProgramFiles(companionRemoveJarMap.getForConfiguration(kotlinc, targetVersion))
+            // Keep everything
+            .addKeepRules("-keep class **.companion_remove_lib.** { *; }")
+            .addKeepKotlinMetadata()
+            // To keep ...$Companion structure
+            .addKeepAttributeInnerClassesAndEnclosingMethod()
+            .compile()
+            .writeToZip();
+
+    Path output =
+        kotlinc(parameters.getRuntime().asCf(), kotlinc, targetVersion)
+            .addClasspathFiles(libJar)
+            .addSourceFiles(getKotlinFileInTest(PKG_PREFIX + "/companion_remove_app", "main"))
+            .setOutputPath(temp.newFolder().toPath())
+            .compile();
+
+    testForJvm()
+        .addRunClasspathFiles(kotlinc.getKotlinStdlibJar(), libJar)
+        .addClasspath(output)
+        .run(parameters.getRuntime(), PKG + ".companion_remove_app.MainKt")
+        .assertSuccessWithOutput(EXPECTED);
+  }
+
+  @Test
+  public void testMetadataInCompanion_removedField() {
+    // TODO(b/248450861): Should not throw.
+    assertThrows(
+        CompilationFailedException.class,
+        () ->
+            testForR8(parameters.getBackend())
+                .addClasspathFiles(kotlinc.getKotlinStdlibJar(), kotlinc.getKotlinAnnotationJar())
+                .addProgramFiles(companionRemoveJarMap.getForConfiguration(kotlinc, targetVersion))
+                // Keep the B class and its interface (which has the doStuff method).
+                .addKeepKotlinMetadata()
+                .addKeepRules(
+                    "-keep class **.ClassWithCompanion { void <init>(); void doStuff(); }")
+                .addKeepRules("-keep class **.ClassWithCompanion$Companion")
+                .compileWithExpectedDiagnostics(
+                    diagnostics -> {
+                      diagnostics.assertErrorMessageThatMatches(
+                          containsString("The metadata should be equivalent"));
+                    }));
+  }
+}
diff --git a/src/test/java/com/android/tools/r8/kotlin/metadata/companion_remove_app/main.kt b/src/test/java/com/android/tools/r8/kotlin/metadata/companion_remove_app/main.kt
new file mode 100644
index 0000000..d08a2d4
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/kotlin/metadata/companion_remove_app/main.kt
@@ -0,0 +1,10 @@
+// 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.kotlin.metadata.companion_remove_app
+
+import com.android.tools.r8.kotlin.metadata.companion_remove_lib.ClassWithCompanion
+
+fun main() {
+  ClassWithCompanion().doStuff()
+}
diff --git a/src/test/java/com/android/tools/r8/kotlin/metadata/companion_remove_lib/lib.kt b/src/test/java/com/android/tools/r8/kotlin/metadata/companion_remove_lib/lib.kt
new file mode 100644
index 0000000..a16595e
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/kotlin/metadata/companion_remove_lib/lib.kt
@@ -0,0 +1,14 @@
+// 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.kotlin.metadata.companion_remove_lib
+
+class ClassWithCompanion {
+  fun doStuff() {
+    println(foo)
+  }
+  companion object {
+    val foo: String
+      get() = "Hello World!"
+  }
+}