Update kept-graph test and improve setup of graph inspector.

Change-Id: I4951d444c2ecd247b1eaf049abb4928ac43fc6e9
diff --git a/src/test/java/com/android/tools/r8/R8TestBuilder.java b/src/test/java/com/android/tools/r8/R8TestBuilder.java
index 2c72ec7..1dc88f2 100644
--- a/src/test/java/com/android/tools/r8/R8TestBuilder.java
+++ b/src/test/java/com/android/tools/r8/R8TestBuilder.java
@@ -8,6 +8,7 @@
 import com.android.tools.r8.TestBase.R8Mode;
 import com.android.tools.r8.experimental.graphinfo.GraphConsumer;
 import com.android.tools.r8.origin.Origin;
+import com.android.tools.r8.shaking.CollectingGraphConsumer;
 import com.android.tools.r8.utils.AndroidApp;
 import com.android.tools.r8.utils.InternalOptions;
 import java.nio.file.Path;
@@ -38,6 +39,7 @@
   private boolean enableInliningAnnotations = false;
   private boolean enableClassInliningAnnotations = false;
   private boolean enableMergeAnnotations = false;
+  private CollectingGraphConsumer graphConsumer = null;
 
   @Override
   R8TestBuilder self() {
@@ -54,7 +56,8 @@
     StringBuilder proguardMapBuilder = new StringBuilder();
     builder.setProguardMapConsumer((string, ignore) -> proguardMapBuilder.append(string));
     ToolHelper.runR8WithoutResult(builder.build(), optionsConsumer);
-    return new R8TestCompileResult(getState(), backend, app.get(), proguardMapBuilder.toString());
+    return new R8TestCompileResult(
+        getState(), backend, app.get(), proguardMapBuilder.toString(), graphConsumer);
   }
 
   @Override
@@ -140,7 +143,15 @@
     return self();
   }
 
+  public R8TestBuilder enableGraphInspector() {
+    CollectingGraphConsumer consumer = new CollectingGraphConsumer(null);
+    setKeptGraphConsumer(consumer);
+    graphConsumer = consumer;
+    return self();
+  }
+
   public R8TestBuilder setKeptGraphConsumer(GraphConsumer graphConsumer) {
+    assert this.graphConsumer == null;
     builder.setKeptGraphConsumer(graphConsumer);
     return self();
   }
diff --git a/src/test/java/com/android/tools/r8/R8TestCompileResult.java b/src/test/java/com/android/tools/r8/R8TestCompileResult.java
index d27a4a0..ef5bfb1 100644
--- a/src/test/java/com/android/tools/r8/R8TestCompileResult.java
+++ b/src/test/java/com/android/tools/r8/R8TestCompileResult.java
@@ -5,8 +5,10 @@
 
 import com.android.tools.r8.TestBase.Backend;
 import com.android.tools.r8.ToolHelper.ProcessResult;
+import com.android.tools.r8.shaking.CollectingGraphConsumer;
 import com.android.tools.r8.utils.AndroidApp;
 import com.android.tools.r8.utils.codeinspector.CodeInspector;
+import com.android.tools.r8.utils.graphinspector.GraphInspector;
 import java.io.IOException;
 import java.util.concurrent.ExecutionException;
 
@@ -14,11 +16,18 @@
 
   private final Backend backend;
   private final String proguardMap;
+  private final CollectingGraphConsumer graphConsumer;
 
-  R8TestCompileResult(TestState state, Backend backend, AndroidApp app, String proguardMap) {
+  R8TestCompileResult(
+      TestState state,
+      Backend backend,
+      AndroidApp app,
+      String proguardMap,
+      CollectingGraphConsumer graphConsumer) {
     super(state, app);
     this.backend = backend;
     this.proguardMap = proguardMap;
+    this.graphConsumer = graphConsumer;
   }
 
   @Override
@@ -36,8 +45,13 @@
     return new CodeInspector(app, proguardMap);
   }
 
+  public GraphInspector graphInspector() throws IOException, ExecutionException {
+    assert graphConsumer != null;
+    return new GraphInspector(graphConsumer, inspector());
+  }
+
   @Override
   public R8TestRunResult createRunResult(AndroidApp app, ProcessResult result) {
-    return new R8TestRunResult(app, result, proguardMap);
+    return new R8TestRunResult(app, result, proguardMap, this::graphInspector);
   }
 }
diff --git a/src/test/java/com/android/tools/r8/R8TestRunResult.java b/src/test/java/com/android/tools/r8/R8TestRunResult.java
index ddea062..42870fa 100644
--- a/src/test/java/com/android/tools/r8/R8TestRunResult.java
+++ b/src/test/java/com/android/tools/r8/R8TestRunResult.java
@@ -9,16 +9,27 @@
 import com.android.tools.r8.ToolHelper.ProcessResult;
 import com.android.tools.r8.utils.AndroidApp;
 import com.android.tools.r8.utils.codeinspector.CodeInspector;
+import com.android.tools.r8.utils.graphinspector.GraphInspector;
 import java.io.IOException;
 import java.util.concurrent.ExecutionException;
 
 public class R8TestRunResult extends TestRunResult<R8TestRunResult> {
 
-  private final String proguardMap;
+  public interface GraphInspectorSupplier {
+    GraphInspector get() throws IOException, ExecutionException;
+  }
 
-  public R8TestRunResult(AndroidApp app, ProcessResult result, String proguardMap) {
+  private final String proguardMap;
+  private final GraphInspectorSupplier graphInspector;
+
+  public R8TestRunResult(
+      AndroidApp app,
+      ProcessResult result,
+      String proguardMap,
+      GraphInspectorSupplier graphInspector) {
     super(app, result);
     this.proguardMap = proguardMap;
+    this.graphInspector = graphInspector;
   }
 
   @Override
@@ -34,6 +45,11 @@
     return new CodeInspector(app, proguardMap);
   }
 
+  public GraphInspector graphInspector() throws IOException, ExecutionException {
+    assertSuccess();
+    return graphInspector.get();
+  }
+
   public String proguardMap() {
     return proguardMap;
   }
diff --git a/src/test/java/com/android/tools/r8/shaking/keptgraph/KeptByAnnotatedClassTestRunner.java b/src/test/java/com/android/tools/r8/shaking/keptgraph/KeptByAnnotatedClassTestRunner.java
index 676118b..ea61c9a 100644
--- a/src/test/java/com/android/tools/r8/shaking/keptgraph/KeptByAnnotatedClassTestRunner.java
+++ b/src/test/java/com/android/tools/r8/shaking/keptgraph/KeptByAnnotatedClassTestRunner.java
@@ -8,9 +8,7 @@
 
 import com.android.tools.r8.TestBase;
 import com.android.tools.r8.references.MethodReference;
-import com.android.tools.r8.shaking.CollectingGraphConsumer;
 import com.android.tools.r8.utils.StringUtils;
-import com.android.tools.r8.utils.codeinspector.CodeInspector;
 import com.android.tools.r8.utils.graphinspector.GraphInspector;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -40,27 +38,24 @@
     MethodReference barMethod = methodFromMethod(CLASS.getDeclaredMethod("bar"));
     MethodReference bazMethod = methodFromMethod(CLASS.getDeclaredMethod("baz"));
 
-    CollectingGraphConsumer graphConsumer = new CollectingGraphConsumer(null);
-    CodeInspector codeInspector =
+    GraphInspector inspector =
         testForR8(backend)
-            .setKeptGraphConsumer(graphConsumer)
+            .enableGraphInspector()
             .enableInliningAnnotations()
             .addProgramClasses(CLASS)
             .addKeepRules("-keep @com.android.tools.r8.Keep class * { public *; }")
             .run(CLASS)
             .assertSuccessWithOutput(EXPECTED)
-            .inspector();
-
-    GraphInspector graphInspector = new GraphInspector(graphConsumer, codeInspector);
+            .graphInspector();
 
     // The only root should be the keep annotation rule.
-    assertEquals(1, graphInspector.getRoots().size());
+    assertEquals(1, inspector.getRoots().size());
 
     // Check that the call chain goes from root -> main(unchanged) -> bar(renamed).
-    graphInspector.method(barMethod).assertRenamed().assertInvokedFrom(mainMethod);
-    graphInspector.method(mainMethod).assertNotRenamed().assertKeptByRootRule();
+    inspector.method(barMethod).assertRenamed().assertInvokedFrom(mainMethod);
+    inspector.method(mainMethod).assertNotRenamed().assertKeptByRootRule();
 
     // Check baz is removed.
-    graphInspector.method(bazMethod).assertAbsent();
+    inspector.method(bazMethod).assertAbsent();
   }
 }
diff --git a/src/test/java/com/android/tools/r8/shaking/keptgraph/KeptMethodTest.java b/src/test/java/com/android/tools/r8/shaking/keptgraph/KeptMethodTest.java
index 35083c7..fc9baa4 100644
--- a/src/test/java/com/android/tools/r8/shaking/keptgraph/KeptMethodTest.java
+++ b/src/test/java/com/android/tools/r8/shaking/keptgraph/KeptMethodTest.java
@@ -1,31 +1,13 @@
-// Copyright (c) 2018, the R8 project authors. Please see the AUTHORS file
+// 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.shaking.keptgraph;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertTrue;
-
-import com.android.tools.r8.CompilationFailedException;
 import com.android.tools.r8.NeverInline;
-import com.android.tools.r8.R8TestBuilder;
-import com.android.tools.r8.TestBase;
-import com.android.tools.r8.references.MethodReference;
-import com.android.tools.r8.references.Reference;
-import com.android.tools.r8.shaking.CollectingGraphConsumer;
-import com.android.tools.r8.utils.codeinspector.CodeInspector;
-import com.android.tools.r8.utils.graphinspector.GraphInspector;
-import java.io.IOException;
-import java.util.concurrent.ExecutionException;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.junit.runners.Parameterized;
-import org.junit.runners.Parameterized.Parameters;
 
-class Main {
+public class KeptMethodTest {
 
-  public static void foo() {
+  public static void main(String[] args) {
     bar();
   }
 
@@ -39,43 +21,3 @@
     System.out.println("called baz");
   }
 }
-
-@RunWith(Parameterized.class)
-public class KeptMethodTest extends TestBase {
-
-  private final Backend backend;
-
-  @Parameters(name = "{0}")
-  public static Backend[] data() {
-    return Backend.values();
-  }
-
-  public KeptMethodTest(Backend backend) {
-    this.backend = backend;
-  }
-
-  @Test
-  public void testKeptMethod()
-      throws NoSuchMethodException, CompilationFailedException, IOException, ExecutionException {
-    MethodReference fooMethod = Reference.methodFromMethod(Main.class.getMethod("foo"));
-    MethodReference barMethod = Reference.methodFromMethod(Main.class.getMethod("bar"));
-    MethodReference bazMethod = Reference.methodFromMethod(Main.class.getMethod("baz"));
-
-    CollectingGraphConsumer graphConsumer = new CollectingGraphConsumer(null);
-    R8TestBuilder builder =
-        testForR8(backend)
-            .setKeptGraphConsumer(graphConsumer)
-            .enableInliningAnnotations()
-            .addProgramClasses(Main.class)
-            .addKeepMethodRules(fooMethod);
-
-    CodeInspector codeInspector = builder.compile().inspector();
-    GraphInspector graphInspector = new GraphInspector(graphConsumer, codeInspector);
-
-    // The only root should be the method rule.
-    assertEquals(1, graphInspector.getRoots().size());
-    assertTrue(graphInspector.isPresent(fooMethod));
-    assertTrue(graphInspector.isRenamed(barMethod));
-    assertFalse(graphInspector.isPresent(bazMethod));
-  }
-}
diff --git a/src/test/java/com/android/tools/r8/shaking/keptgraph/KeptMethodTestRunner.java b/src/test/java/com/android/tools/r8/shaking/keptgraph/KeptMethodTestRunner.java
new file mode 100644
index 0000000..8ac68f9
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/shaking/keptgraph/KeptMethodTestRunner.java
@@ -0,0 +1,61 @@
+// 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.shaking.keptgraph;
+
+import static com.android.tools.r8.references.Reference.methodFromMethod;
+import static org.junit.Assert.assertEquals;
+
+import com.android.tools.r8.TestBase;
+import com.android.tools.r8.references.MethodReference;
+import com.android.tools.r8.utils.StringUtils;
+import com.android.tools.r8.utils.graphinspector.GraphInspector;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameters;
+
+@RunWith(Parameterized.class)
+public class KeptMethodTestRunner extends TestBase {
+
+  private static final Class<KeptMethodTest> CLASS = KeptMethodTest.class;
+  private static final String EXPECTED = StringUtils.lines("called bar");
+
+  private final Backend backend;
+
+  @Parameters(name = "{0}")
+  public static Backend[] data() {
+    return Backend.values();
+  }
+
+  public KeptMethodTestRunner(Backend backend) {
+    this.backend = backend;
+  }
+
+  @Test
+  public void testKeptMethod() throws Exception {
+    MethodReference mainMethod = methodFromMethod(CLASS.getDeclaredMethod("main", String[].class));
+    MethodReference barMethod = methodFromMethod(CLASS.getDeclaredMethod("bar"));
+    MethodReference bazMethod = methodFromMethod(CLASS.getDeclaredMethod("baz"));
+
+    GraphInspector inspector =
+        testForR8(backend)
+            .enableGraphInspector()
+            .enableInliningAnnotations()
+            .addProgramClasses(CLASS)
+            .addKeepMethodRules(mainMethod)
+            .run(CLASS)
+            .assertSuccessWithOutput(EXPECTED)
+            .graphInspector();
+
+    // The only root should be the keep main-method rule.
+    assertEquals(1, inspector.getRoots().size());
+
+    // Check that the call chain goes from root -> main(unchanged) -> bar(renamed).
+    inspector.method(barMethod).assertRenamed().assertInvokedFrom(mainMethod);
+    inspector.method(mainMethod).assertNotRenamed().assertKeptByRootRule();
+
+    // Check baz is removed.
+    inspector.method(bazMethod).assertAbsent();
+  }
+}
diff --git a/src/test/java/com/android/tools/r8/utils/graphinspector/GraphInspector.java b/src/test/java/com/android/tools/r8/utils/graphinspector/GraphInspector.java
index 65cf1af..b1a5e33 100644
--- a/src/test/java/com/android/tools/r8/utils/graphinspector/GraphInspector.java
+++ b/src/test/java/com/android/tools/r8/utils/graphinspector/GraphInspector.java
@@ -252,19 +252,6 @@
     }
   }
 
-  public boolean isRenamed(MethodReference method) {
-    assert isPresent(method);
-    return inspector.method(method).isRenamed();
-  }
-
-  public boolean isPresent(MethodReference method) {
-    if (methods.containsKey(method)) {
-      assert inspector.method(method).isPresent();
-      return true;
-    }
-    return false;
-  }
-
   public Set<GraphNode> getRoots() {
     return Collections.unmodifiableSet(roots);
   }