Add more tests about metadata rewriting.

Bug: 143687784, 70169921
Change-Id: Ie94e907f1ebd81d2302eb2351cd90796cd9d50c0
diff --git a/src/test/java/com/android/tools/r8/kotlin/metadata/MetadataRenameTest.java b/src/test/java/com/android/tools/r8/kotlin/metadata/MetadataRenameTest.java
index a99b1a3..160dc3f 100644
--- a/src/test/java/com/android/tools/r8/kotlin/metadata/MetadataRenameTest.java
+++ b/src/test/java/com/android/tools/r8/kotlin/metadata/MetadataRenameTest.java
@@ -49,6 +49,7 @@
   private static final String PKG_PREFIX =
       DescriptorUtils.getBinaryNameFromJavaType(MetadataRenameTest.class.getPackage().getName());
   private static Path supertypeLibJar;
+  private static Path extLibJar;
 
   @BeforeClass
   public static void createLibJar() throws Exception {
@@ -64,6 +65,18 @@
             getKotlinFileInTest(supertypeLibFolder + "/internal", "itf")
         );
     assertEquals(0, processResult.exitCode);
+
+    String extLibFolder = PKG_PREFIX + "/extension_lib";
+    extLibJar = getStaticTemp().newFile("ext_lib.jar").toPath();
+    processResult =
+        ToolHelper.runKotlinc(
+            null,
+            null,
+            extLibJar,
+            null,
+            getKotlinFileInTest(extLibFolder, "B")
+        );
+    assertEquals(0, processResult.exitCode);
   }
 
   @Test
@@ -108,4 +121,52 @@
         kotlinTestCompileResult.stderr(),
         containsString("unresolved supertypes: " + pkg + ".supertype_lib.internal.Itf"));
   }
+
+  @Test
+  public void testMetadataInExtension() throws Exception {
+    assumeTrue(parameters.getRuntime().isCf());
+
+    R8TestCompileResult compileResult =
+        testForR8(parameters.getBackend())
+            .addProgramFiles(extLibJar)
+            // Keep the B class and its interface (which has the doStuff method).
+            .addKeepRules("-keep class **.B")
+            .addKeepRules("-keep class **.I { <methods>; }")
+            // Keep the BKt extension method which requires metadata
+            // to be called with Kotlin syntax from other kotlin code.
+            .addKeepRules("-keep class **.BKt { <methods>; }")
+            .addKeepAttributes("*Annotation*")
+            .compile();
+    String pkg = getClass().getPackage().getName();
+    final String superClassName = pkg + ".extension_lib.Super";
+    final String bClassName = pkg + ".extension_lib.B";
+    compileResult.inspect(inspector -> {
+      ClassSubject sup = inspector.clazz(superClassName);
+      assertThat(sup, not(isPresent()));
+
+      ClassSubject impl = inspector.clazz(bClassName);
+      assertThat(impl, isPresent());
+      assertThat(impl, not(isRenamed()));
+      // API entry is kept, hence the presence of Metadata.
+      DexAnnotation metadata = retrieveMetadata(impl.getDexClass());
+      assertNotNull(metadata);
+      // TODO(b/143687784): test its metadata doesn't point to shrunken Super.
+    });
+
+    Path r8ProcessedLibZip = temp.newFile("r8-lib.zip").toPath();
+    compileResult.writeToZip(r8ProcessedLibZip);
+
+    String appFolder = PKG_PREFIX + "/extension_app";
+    KotlinTestCompileResult kotlinTestCompileResult =
+        testForKotlin()
+            .addClasspathFiles(r8ProcessedLibZip)
+            .addSourceFiles(getKotlinFileInTest(appFolder, "main"))
+            .compile();
+    // TODO(b/143687784): should be able to compile!
+    assertNotEquals(0, kotlinTestCompileResult.exitCode());
+    assertThat(
+        kotlinTestCompileResult.stderr(),
+        containsString("unresolved supertypes: " + pkg + ".extension_lib.Super"));
+    assertThat(kotlinTestCompileResult.stderr(), containsString("unresolved reference: doStuff"));
+  }
 }
diff --git a/src/test/java/com/android/tools/r8/kotlin/metadata/extension_app/main.kt b/src/test/java/com/android/tools/r8/kotlin/metadata/extension_app/main.kt
new file mode 100644
index 0000000..246d3fe
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/kotlin/metadata/extension_app/main.kt
@@ -0,0 +1,12 @@
+// Copyright (c) 2019, 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.extension_app
+
+import com.android.tools.r8.kotlin.metadata.extension_lib.B
+import com.android.tools.r8.kotlin.metadata.extension_lib.extension
+
+fun main() {
+  B().doStuff()
+  B().extension()
+}
diff --git a/src/test/java/com/android/tools/r8/kotlin/metadata/extension_lib/B.kt b/src/test/java/com/android/tools/r8/kotlin/metadata/extension_lib/B.kt
new file mode 100644
index 0000000..a6fe9ab
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/kotlin/metadata/extension_lib/B.kt
@@ -0,0 +1,20 @@
+// Copyright (c) 2019, 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.extension_lib
+
+interface I {
+  fun doStuff()
+}
+
+open class Super : I {
+  override fun doStuff() {
+    println("do stuff")
+  }
+}
+
+class B : Super() { }
+
+fun B.extension() {
+  doStuff()
+}
\ No newline at end of file