Test Record R8 cf to cf Merge

Bug: 200259877
Change-Id: I750631199e8ea768a771960dff50a9db9578338c
diff --git a/src/test/examplesJava17/records/RecordLib.java b/src/test/examplesJava17/records/RecordLib.java
new file mode 100644
index 0000000..7f1b149
--- /dev/null
+++ b/src/test/examplesJava17/records/RecordLib.java
@@ -0,0 +1,14 @@
+// Copyright (c) 2021, 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 records;
+
+public class RecordLib {
+
+  record LibRecord(String data) {}
+
+  public static Object getRecord() {
+    return new LibRecord("data");
+  }
+}
diff --git a/src/test/examplesJava17/records/RecordMain.java b/src/test/examplesJava17/records/RecordMain.java
new file mode 100644
index 0000000..dcca88e
--- /dev/null
+++ b/src/test/examplesJava17/records/RecordMain.java
@@ -0,0 +1,16 @@
+// Copyright (c) 2021, 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 records;
+
+public class RecordMain {
+
+  record MainRecord(String data) {}
+  ;
+
+  public static void main(String[] args) {
+    System.out.println(new MainRecord("main") instanceof java.lang.Record);
+    System.out.println(RecordLib.getRecord() instanceof java.lang.Record);
+  }
+}
diff --git a/src/test/java/com/android/tools/r8/desugar/records/RecordLibMergeTest.java b/src/test/java/com/android/tools/r8/desugar/records/RecordLibMergeTest.java
new file mode 100644
index 0000000..7c32b98
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/desugar/records/RecordLibMergeTest.java
@@ -0,0 +1,79 @@
+// Copyright (c) 2021, 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.desugar.records;
+
+import com.android.tools.r8.R8FullTestBuilder;
+import com.android.tools.r8.TestBase;
+import com.android.tools.r8.TestParameters;
+import com.android.tools.r8.TestRuntime.CfRuntime;
+import com.android.tools.r8.utils.InternalOptions.TestingOptions;
+import com.android.tools.r8.utils.StringUtils;
+import java.nio.file.Path;
+import java.util.List;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+@RunWith(Parameterized.class)
+public class RecordLibMergeTest extends TestBase {
+
+  private static final String RECORD_LIB = "RecordLib";
+  private static final String RECORD_MAIN = "RecordMain";
+  private static final byte[][] PROGRAM_DATA_LIB = RecordTestUtils.getProgramData(RECORD_LIB);
+  private static final byte[][] PROGRAM_DATA_MAIN = RecordTestUtils.getProgramData(RECORD_MAIN);
+  private static final String MAIN_TYPE = RecordTestUtils.getMainType(RECORD_MAIN);
+  private static final String EXPECTED_RESULT = StringUtils.lines("true", "true");
+
+  private final TestParameters parameters;
+
+  public RecordLibMergeTest(TestParameters parameters) {
+    this.parameters = parameters;
+  }
+
+  @Parameterized.Parameters(name = "{0}")
+  public static List<Object[]> data() {
+    // TODO(b/174431251): This should be replaced with .withCfRuntimes(start = jdk17).
+    return buildParameters(
+        getTestParameters()
+            .withCustomRuntime(CfRuntime.getCheckedInJdk17())
+            .withDexRuntimes()
+            .withAllApiLevelsAlsoForCf()
+            .build());
+  }
+
+  @Test
+  public void testR8Merge() throws Exception {
+    Path lib =
+        testForR8(Backend.CF)
+            .addProgramClassFileData(PROGRAM_DATA_LIB)
+            .setMinApi(parameters.getApiLevel())
+            .addKeepRules(
+                "-keep class records.RecordLib { public static java.lang.Object getRecord(); }")
+            .addKeepRules("-keep class records.RecordLib$LibRecord")
+            .addLibraryFiles(RecordTestUtils.getJdk15LibraryFiles(temp))
+            .addOptionsModification(TestingOptions::allowExperimentClassFileVersion)
+            .compile()
+            .writeToZip();
+    R8FullTestBuilder builder =
+        testForR8(parameters.getBackend())
+            .addProgramFiles(lib)
+            .addProgramClassFileData(PROGRAM_DATA_MAIN)
+            .setMinApi(parameters.getApiLevel())
+            .addKeepMainRule(MAIN_TYPE)
+            .addKeepRules("-keep class records.RecordLib$LibRecord")
+            .addKeepRules("-keep class records.RecordMain$MainRecord")
+            .addOptionsModification(TestingOptions::allowExperimentClassFileVersion);
+    if (parameters.isCfRuntime()) {
+      builder
+          .addLibraryFiles(RecordTestUtils.getJdk15LibraryFiles(temp))
+          .compile()
+          .inspect(RecordTestUtils::assertRecordsAreRecords)
+          .run(parameters.getRuntime(), MAIN_TYPE)
+          .assertSuccessWithOutput(EXPECTED_RESULT);
+      return;
+    }
+    builder.run(parameters.getRuntime(), MAIN_TYPE).assertSuccessWithOutput(EXPECTED_RESULT);
+  }
+}