Add a test for minimal startup dex
Change-Id: Idc0f322ea6298ee29b06e5aa178b6a610fee2f9b
diff --git a/src/main/java/com/android/tools/r8/experimental/startup/StartupOptions.java b/src/main/java/com/android/tools/r8/experimental/startup/StartupOptions.java
index 9bab1cd..a8b4476 100644
--- a/src/main/java/com/android/tools/r8/experimental/startup/StartupOptions.java
+++ b/src/main/java/com/android/tools/r8/experimental/startup/StartupOptions.java
@@ -15,14 +15,20 @@
return enableMinimalStartupDex;
}
- public void setEnableMinimalStartupDex() {
+ public StartupOptions setEnableMinimalStartupDex() {
enableMinimalStartupDex = true;
+ return this;
}
public boolean isStartupCompletenessCheckForTesting() {
return enableStartupCompletenessCheckForTesting;
}
+ public StartupOptions setEnableStartupCompletenessCheckForTesting() {
+ enableStartupCompletenessCheckForTesting = true;
+ return this;
+ }
+
public boolean hasStartupConfiguration() {
return startupConfiguration != null;
}
diff --git a/src/test/java/com/android/tools/r8/R8TestCompileResult.java b/src/test/java/com/android/tools/r8/R8TestCompileResult.java
index b96cef4..fe500f8 100644
--- a/src/test/java/com/android/tools/r8/R8TestCompileResult.java
+++ b/src/test/java/com/android/tools/r8/R8TestCompileResult.java
@@ -111,6 +111,19 @@
return self();
}
+ @SafeVarargs
+ public final <E extends Throwable> R8TestCompileResult inspectMultiDex(
+ ThrowingConsumer<CodeInspector, E>... consumers) throws IOException, E {
+ Path out = state.getNewTempFolder();
+ getApp().writeToDirectory(out, OutputMode.DexIndexed);
+ consumers[0].accept(new CodeInspector(out.resolve("classes.dex"), getProguardMap()));
+ for (int i = 1; i < consumers.length; i++) {
+ consumers[i].accept(
+ new CodeInspector(out.resolve("classes" + (i + 1) + ".dex"), getProguardMap()));
+ }
+ return self();
+ }
+
public final <E extends Throwable> R8TestCompileResult inspectGraph(
ThrowingConsumer<GraphInspector, E> consumer) throws IOException, E {
consumer.accept(graphInspector());
diff --git a/src/test/java/com/android/tools/r8/startup/MinimalStartupDexTest.java b/src/test/java/com/android/tools/r8/startup/MinimalStartupDexTest.java
new file mode 100644
index 0000000..a6dbbb7
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/startup/MinimalStartupDexTest.java
@@ -0,0 +1,126 @@
+// 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.startup;
+
+import static com.android.tools.r8.utils.codeinspector.Matchers.isPresent;
+import static junit.framework.TestCase.assertTrue;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+import com.android.tools.r8.NeverInline;
+import com.android.tools.r8.TestBase;
+import com.android.tools.r8.TestParameters;
+import com.android.tools.r8.TestParametersCollection;
+import com.android.tools.r8.experimental.startup.StartupConfiguration;
+import com.android.tools.r8.utils.AndroidApiLevel;
+import com.android.tools.r8.utils.codeinspector.ClassSubject;
+import com.android.tools.r8.utils.codeinspector.InstructionSubject;
+import com.android.tools.r8.utils.codeinspector.MethodSubject;
+import com.google.common.collect.Lists;
+import java.util.Collections;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameter;
+import org.junit.runners.Parameterized.Parameters;
+
+@RunWith(Parameterized.class)
+public class MinimalStartupDexTest extends TestBase {
+
+ @Parameter(0)
+ public TestParameters parameters;
+
+ @Parameters(name = "{0}")
+ public static TestParametersCollection data() {
+ return getTestParameters()
+ .withDexRuntimes()
+ .withApiLevelsStartingAtIncluding(AndroidApiLevel.L)
+ .build();
+ }
+
+ @Test
+ public void test() throws Exception {
+ testForR8(parameters.getBackend())
+ .addInnerClasses(getClass())
+ .addKeepClassAndMembersRules(Main.class)
+ .addOptionsModification(
+ options ->
+ options
+ .getStartupOptions()
+ .setEnableMinimalStartupDex()
+ .setEnableStartupCompletenessCheckForTesting()
+ .setStartupConfiguration(
+ new StartupConfiguration(
+ Lists.newArrayList(
+ toDexType(Main.class, options.dexItemFactory()),
+ toDexType(StartupClass.class, options.dexItemFactory())),
+ Collections.emptyList())))
+ .enableInliningAnnotations()
+ .setMinApi(parameters.getApiLevel())
+ .compile()
+ .inspectMultiDex(
+ primaryDexInspector -> {
+ // StartupClass should be in the primary dex.
+ ClassSubject startupClassSubject = primaryDexInspector.clazz(StartupClass.class);
+ assertThat(startupClassSubject, isPresent());
+
+ MethodSubject startupMethodSubject = startupClassSubject.uniqueMethodWithName("foo");
+ assertThat(startupMethodSubject, isPresent());
+ assertTrue(
+ startupMethodSubject.streamInstructions().noneMatch(InstructionSubject::isThrow));
+ },
+ secondaryDexInspector -> {
+ // NonStartupClass should be in the secondary dex and should be transformed such that
+ // all methods throw null.
+ ClassSubject nonStartupClassSubject =
+ secondaryDexInspector.clazz(NonStartupClass.class);
+ assertThat(nonStartupClassSubject, isPresent());
+
+ MethodSubject nonStartupClinitSubject = nonStartupClassSubject.clinit();
+ assertThat(nonStartupClinitSubject, isPresent());
+ assertTrue(
+ nonStartupClinitSubject
+ .streamInstructions()
+ .anyMatch(InstructionSubject::isThrow));
+
+ MethodSubject nonStartupMethodSubject =
+ nonStartupClassSubject.uniqueMethodWithName("bar");
+ assertThat(nonStartupMethodSubject, isPresent());
+ assertTrue(
+ nonStartupMethodSubject
+ .streamInstructions()
+ .anyMatch(InstructionSubject::isThrow));
+ })
+ .run(parameters.getRuntime(), Main.class)
+ .assertSuccessWithOutputLines("foo");
+ }
+
+ static class Main {
+
+ public static void main(String[] args) {
+ StartupClass.foo();
+ }
+
+ // @Keep
+ public void onClick() {
+ NonStartupClass.bar();
+ }
+ }
+
+ static class StartupClass {
+
+ @NeverInline
+ static void foo() {
+ System.out.println("foo");
+ }
+ }
+
+ static class NonStartupClass {
+
+ @NeverInline
+ static void bar() {
+ System.out.println("bar");
+ }
+ }
+}