[Metadata] Add tests for not keeping class with includescriptorclasses

Bug: 208209210
Change-Id: I107ceb6aaaeb406e962da0d067b0ace6f08c46de
diff --git a/src/test/java/com/android/tools/r8/kotlin/metadata/MetadataRewriteInlineClassIncludeDescriptorClassesTest.java b/src/test/java/com/android/tools/r8/kotlin/metadata/MetadataRewriteInlineClassIncludeDescriptorClassesTest.java
new file mode 100644
index 0000000..f2274fa
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/kotlin/metadata/MetadataRewriteInlineClassIncludeDescriptorClassesTest.java
@@ -0,0 +1,109 @@
+// 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.kotlin.metadata;
+
+import static org.hamcrest.CoreMatchers.containsString;
+import static org.hamcrest.CoreMatchers.not;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.Assert.assertEquals;
+
+import com.android.tools.r8.KotlinCompilerTool.KotlinCompilerVersion;
+import com.android.tools.r8.KotlinTestParameters;
+import com.android.tools.r8.TestParameters;
+import com.android.tools.r8.ToolHelper.ProcessResult;
+import com.android.tools.r8.shaking.ProguardKeepAttributes;
+import com.android.tools.r8.utils.StringUtils;
+import com.android.tools.r8.utils.codeinspector.Matchers;
+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 MetadataRewriteInlineClassIncludeDescriptorClassesTest extends KotlinMetadataTestBase {
+
+  private final String EXPECTED = StringUtils.lines("Hello World!");
+
+  @Parameterized.Parameters(name = "{0}, {1}")
+  public static Collection<Object[]> data() {
+    return buildParameters(
+        getTestParameters().withCfRuntimes().build(),
+        getKotlinTestParameters()
+            .withCompilersStartingFromIncluding(KotlinCompilerVersion.KOTLINC_1_6_0)
+            .withAllTargetVersions()
+            .build());
+  }
+
+  public MetadataRewriteInlineClassIncludeDescriptorClassesTest(
+      TestParameters parameters, KotlinTestParameters kotlinParameters) {
+    super(kotlinParameters);
+    this.parameters = parameters;
+  }
+
+  private static final KotlinCompileMemoizer libJars =
+      getCompileMemoizer(
+          getKotlinFileInTest(PKG_PREFIX + "/inline_class_fun_descriptor_classes_lib", "lib"),
+          getKotlinFileInTest(
+              PKG_PREFIX + "/inline_class_fun_descriptor_classes_lib", "keepForApi"));
+  private final TestParameters parameters;
+
+  @Test
+  public void smokeTest() throws Exception {
+    Path libJar = libJars.getForConfiguration(kotlinc, targetVersion);
+    Path output =
+        kotlinc(parameters.getRuntime().asCf(), kotlinc, targetVersion)
+            .addClasspathFiles(libJar)
+            .addSourceFiles(
+                getKotlinFileInTest(
+                    PKG_PREFIX + "/inline_class_fun_descriptor_classes_app", "main"))
+            .setOutputPath(temp.newFolder().toPath())
+            .compile();
+    testForJvm()
+        .addRunClasspathFiles(kotlinc.getKotlinStdlibJar(), libJar)
+        .addClasspath(output)
+        .run(parameters.getRuntime(), PKG + ".inline_class_fun_descriptor_classes_app.MainKt")
+        .assertSuccessWithOutput(EXPECTED);
+  }
+
+  @Test
+  public void testMetadataForLib() throws Exception {
+    Path libJar =
+        testForR8(parameters.getBackend())
+            .addClasspathFiles(kotlinc.getKotlinStdlibJar(), kotlinc.getKotlinAnnotationJar())
+            .addProgramFiles(libJars.getForConfiguration(kotlinc, targetVersion))
+            .addKeepAttributes(ProguardKeepAttributes.RUNTIME_VISIBLE_ANNOTATIONS)
+            // kotlinc will generate a method for the unboxed type on the form login-XXXXX(String).
+            // We define an annotation that specify we keep the method and descriptor classes.
+            .addKeepRules(
+                "-keepclasseswithmembers class * { @"
+                    + PKG
+                    + ".inline_class_fun_descriptor_classes_lib.KeepForApi *; }")
+            .addKeepRules(
+                "-keepclassmembers,includedescriptorclasses class * { @"
+                    + PKG
+                    + ".inline_class_fun_descriptor_classes_lib.KeepForApi *; }")
+            .compile()
+            .inspect(
+                inspector -> {
+                  // TODO(b/208209210): Perhaps this should be kept.
+                  assertThat(
+                      inspector.clazz(
+                          PKG + ".inline_class_fun_descriptor_classes_lib.KeepForApi.Password"),
+                      not(Matchers.isPresent()));
+                })
+            .writeToZip();
+    ProcessResult kotlinCompileAppResult =
+        kotlinc(parameters.getRuntime().asCf(), kotlinc, targetVersion)
+            .addClasspathFiles(libJar)
+            .addSourceFiles(
+                getKotlinFileInTest(
+                    PKG_PREFIX + "/inline_class_fun_descriptor_classes_app", "main"))
+            .setOutputPath(temp.newFolder().toPath())
+            .compileRaw();
+    assertEquals(1, kotlinCompileAppResult.exitCode);
+    assertThat(kotlinCompileAppResult.stderr, containsString("unresolved reference: Password"));
+  }
+}
diff --git a/src/test/java/com/android/tools/r8/kotlin/metadata/inline_class_fun_descriptor_classes_app/main.kt b/src/test/java/com/android/tools/r8/kotlin/metadata/inline_class_fun_descriptor_classes_app/main.kt
new file mode 100644
index 0000000..a9d5023
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/kotlin/metadata/inline_class_fun_descriptor_classes_app/main.kt
@@ -0,0 +1,13 @@
+// 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.kotlin.metadata.inline_class_fun_descriptor_classes_app
+
+import com.android.tools.r8.kotlin.metadata.inline_class_fun_descriptor_classes_lib.create
+import com.android.tools.r8.kotlin.metadata.inline_class_fun_descriptor_classes_lib.Password
+import com.android.tools.r8.kotlin.metadata.inline_class_fun_descriptor_classes_lib.login
+
+fun main() {
+  login(create("Hello World!"))
+}
\ No newline at end of file
diff --git a/src/test/java/com/android/tools/r8/kotlin/metadata/inline_class_fun_descriptor_classes_lib/keepForApi.kt b/src/test/java/com/android/tools/r8/kotlin/metadata/inline_class_fun_descriptor_classes_lib/keepForApi.kt
new file mode 100644
index 0000000..1e0ec5a
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/kotlin/metadata/inline_class_fun_descriptor_classes_lib/keepForApi.kt
@@ -0,0 +1,11 @@
+// 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.kotlin.metadata.inline_class_fun_descriptor_classes_lib
+
+@Target(AnnotationTarget.FUNCTION)
+@Retention(AnnotationRetention.RUNTIME)
+annotation class KeepForApi {
+
+}
\ No newline at end of file
diff --git a/src/test/java/com/android/tools/r8/kotlin/metadata/inline_class_fun_descriptor_classes_lib/lib.kt b/src/test/java/com/android/tools/r8/kotlin/metadata/inline_class_fun_descriptor_classes_lib/lib.kt
new file mode 100644
index 0000000..8c902e7
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/kotlin/metadata/inline_class_fun_descriptor_classes_lib/lib.kt
@@ -0,0 +1,18 @@
+// 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.kotlin.metadata.inline_class_fun_descriptor_classes_lib
+
+@JvmInline
+value class Password(val s: String)
+
+@KeepForApi
+fun create(pw : String) : Password {
+  return Password(pw)
+}
+
+@KeepForApi
+fun login(pw : Password) {
+  println(pw.s)
+}