Add test checking R8 compiled programs can be debugged.
This checks that compiling in debug or compiling no-optimize no-obfuscate will
yield a result that has maintained the input line numbers.
Change-Id: I29c3a3046f270752d893a183b963bfb9e7093e3b
diff --git a/src/test/java/com/android/tools/r8/debug/R8DebugNonMinifiedProgramTest.java b/src/test/java/com/android/tools/r8/debug/R8DebugNonMinifiedProgramTest.java
new file mode 100644
index 0000000..cb32247
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/debug/R8DebugNonMinifiedProgramTest.java
@@ -0,0 +1,32 @@
+// 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.debug;
+
+import com.android.tools.r8.NeverClassInline;
+import com.android.tools.r8.NeverInline;
+
+public class R8DebugNonMinifiedProgramTest {
+
+ public static void main(String[] args) {
+ System.out.println("Hello, world: " + new A().foo());
+ }
+
+ @NeverClassInline
+ public static class A {
+
+ @NeverInline
+ String foo() {
+ return "Class A";
+ }
+ }
+
+ @NeverClassInline
+ public static class B {
+
+ @NeverInline
+ String bar() {
+ return "Class B";
+ }
+ }
+}
diff --git a/src/test/java/com/android/tools/r8/debug/R8DebugNonMinifiedProgramTestRunner.java b/src/test/java/com/android/tools/r8/debug/R8DebugNonMinifiedProgramTestRunner.java
new file mode 100644
index 0000000..ecfc8c1
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/debug/R8DebugNonMinifiedProgramTestRunner.java
@@ -0,0 +1,93 @@
+// 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.debug;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+import com.android.tools.r8.R8FullTestBuilder;
+import com.android.tools.r8.R8TestCompileResult;
+import com.android.tools.r8.TestParameters;
+import com.android.tools.r8.TestParametersCollection;
+import com.android.tools.r8.utils.AndroidApiLevel;
+import java.util.function.BiFunction;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+@RunWith(Parameterized.class)
+public class R8DebugNonMinifiedProgramTestRunner extends DebugTestBase {
+
+ private static final Class<?> CLASS = R8DebugNonMinifiedProgramTest.class;
+
+ @Parameterized.Parameters(name = "{0}")
+ public static TestParametersCollection data() {
+ return getTestParameters().withAllRuntimes().withAllApiLevels().build();
+ }
+
+ private final TestParameters parameters;
+
+ public R8DebugNonMinifiedProgramTestRunner(TestParameters parameters) {
+ this.parameters = parameters;
+ }
+
+ private static BiFunction<Backend, AndroidApiLevel, R8TestCompileResult> compiledDebug
+ = memoizeBiFunction(R8DebugNonMinifiedProgramTestRunner::compileDebug);
+
+ private static BiFunction<Backend, AndroidApiLevel, R8TestCompileResult> compiledNoOptNoMinify
+ = memoizeBiFunction(R8DebugNonMinifiedProgramTestRunner::compileNoOptNoMinify);
+
+ private static R8TestCompileResult compileDebug(Backend backend, AndroidApiLevel apiLevel)
+ throws Exception {
+ return compile(testForR8(getStaticTemp(), backend).debug(), apiLevel);
+ }
+
+ private static R8TestCompileResult compileNoOptNoMinify(Backend backend, AndroidApiLevel apiLevel)
+ throws Exception {
+ return compile(
+ testForR8(getStaticTemp(), backend)
+ .addKeepRules("-dontoptimize", "-dontobfuscate", "-keepattributes LineNumberTable"),
+ apiLevel);
+ }
+
+ private static R8TestCompileResult compile(R8FullTestBuilder builder, AndroidApiLevel apiLevel)
+ throws Exception {
+ return builder.enableInliningAnnotations()
+ .enableClassInliningAnnotations()
+ .addProgramClassesAndInnerClasses(CLASS)
+ .addKeepMainRule(CLASS)
+ .setMinApi(apiLevel)
+ .compile()
+ .inspect(inspector -> {
+ // Check that tree shaking is running (e.g., B is removed).
+ assertTrue(inspector.clazz(R8DebugNonMinifiedProgramTest.A.class).isPresent());
+ assertFalse(inspector.clazz(R8DebugNonMinifiedProgramTest.B.class).isPresent());
+ });
+ }
+
+ @Test
+ public void testDebugMode() throws Throwable {
+ runTest(compiledDebug.apply(parameters.getBackend(), parameters.getApiLevel()));
+ }
+
+ @Test
+ public void testNoOptimizationAndNoMinification() throws Throwable {
+ runTest(compiledNoOptNoMinify.apply(parameters.getBackend(), parameters.getApiLevel()));
+ }
+
+ private void runTest(R8TestCompileResult compileResult) throws Throwable {
+ compileResult
+ .run(parameters.getRuntime(), CLASS)
+ .assertSuccessWithOutputLines("Hello, world: Class A");
+
+ DebugTestConfig debugTestConfig = compileResult.debugConfig();
+ assertNull("For this test the map file must not be present!", debugTestConfig.getProguardMap());
+ runDebugTest(debugTestConfig, CLASS,
+ breakpoint(CLASS.getTypeName(), "main"),
+ run(),
+ checkLine(12),
+ run());
+ }
+}
\ No newline at end of file