Add a test for not keeping all of kotlin.Metadata
Bug: 156447059
Change-Id: I4bbaad476051f6d33764039c81d332b65f88d05a
diff --git a/src/test/java/com/android/tools/r8/kotlin/metadata/MetadataRewriteKeepTest.java b/src/test/java/com/android/tools/r8/kotlin/metadata/MetadataRewriteKeepTest.java
index 608f914..e6f238d 100644
--- a/src/test/java/com/android/tools/r8/kotlin/metadata/MetadataRewriteKeepTest.java
+++ b/src/test/java/com/android/tools/r8/kotlin/metadata/MetadataRewriteKeepTest.java
@@ -4,19 +4,20 @@
package com.android.tools.r8.kotlin.metadata;
+import static com.android.tools.r8.utils.codeinspector.Matchers.isPresent;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
-import com.android.tools.r8.CompilationFailedException;
import com.android.tools.r8.TestParameters;
import com.android.tools.r8.ToolHelper;
import com.android.tools.r8.ToolHelper.KotlinTargetVersion;
import com.android.tools.r8.shaking.ProguardKeepAttributes;
+import com.android.tools.r8.utils.codeinspector.ClassSubject;
import com.android.tools.r8.utils.codeinspector.CodeInspector;
import com.android.tools.r8.utils.codeinspector.FoundClassSubject;
-import java.io.IOException;
import java.util.Collection;
-import java.util.concurrent.ExecutionException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
@@ -38,7 +39,7 @@
}
@Test
- public void testR8() throws CompilationFailedException, IOException, ExecutionException {
+ public void testR8() throws Exception {
testForR8(parameters.getBackend())
.addProgramFiles(ToolHelper.getKotlinStdlibJar())
.setMinApi(parameters.getApiLevel())
@@ -49,12 +50,53 @@
.inspect(this::inspect);
}
+ @Test
+ public void testR8KeepPartial() throws Exception {
+ // This test is a bit weird, since it shows that we can remove params from the kotlin.Metadata
+ // class, but still be able to fully read the kotlin.Metadata.
+ testForR8(parameters.getBackend())
+ .addProgramFiles(ToolHelper.getKotlinStdlibJar())
+ .setMinApi(parameters.getApiLevel())
+ .addKeepRules("-keep class kotlin.Metadata { *** d1(); }")
+ .addKeepAttributes(ProguardKeepAttributes.RUNTIME_VISIBLE_ANNOTATIONS)
+ .compile()
+ .inspect(
+ inspector -> {
+ inspect(inspector);
+ ClassSubject kotlinMetadataClass = inspector.clazz("kotlin.Metadata");
+ assertThat(kotlinMetadataClass, isPresent());
+ assertEquals(1, kotlinMetadataClass.allMethods().size());
+ assertNotNull(kotlinMetadataClass.getKmClass().getName());
+ });
+ }
+
+ @Test
+ public void testR8KeepPartialCooking() throws Exception {
+ // This test is a bit weird, since it shows that we can remove params from the kotlin.Metadata
+ // class, but still be able to fully read the kotlin.Metadata externally.
+ testForR8(parameters.getBackend())
+ .addProgramFiles(ToolHelper.getKotlinStdlibJar())
+ .setMinApi(parameters.getApiLevel())
+ .addKeepRules("-keep class kotlin.Metadata { *** d1(); }")
+ .addKeepAttributes(ProguardKeepAttributes.RUNTIME_VISIBLE_ANNOTATIONS)
+ .compile()
+ .inspect(
+ inspector -> {
+ inspect(inspector);
+ ClassSubject kotlinMetadataClass = inspector.clazz("kotlin.Metadata");
+ assertThat(kotlinMetadataClass, isPresent());
+ assertEquals(1, kotlinMetadataClass.allMethods().size());
+ assertNotNull(kotlinMetadataClass.getKmClass().getName());
+ });
+ }
+
private void inspect(CodeInspector inspector) {
// All kept classes should have their kotlin metadata.
for (FoundClassSubject clazz : inspector.allClasses()) {
if (clazz.getFinalName().startsWith("kotlin.io")
|| clazz.getFinalName().equals("kotlin.Metadata")) {
assertNotNull(clazz.getKotlinClassMetadata());
+ assertNotNull(clazz.getKotlinClassMetadata().getHeader().getData2());
} else {
assertNull(clazz.getKotlinClassMetadata());
}