Testing for default and native methods in vertical class merger

Change-Id: I8508ca29835325e11fdd4bb4680f98021aea485b
diff --git a/src/test/examples/classmerging/ClassWithNativeMethodTest.java b/src/test/examples/classmerging/ClassWithNativeMethodTest.java
new file mode 100644
index 0000000..b28433d
--- /dev/null
+++ b/src/test/examples/classmerging/ClassWithNativeMethodTest.java
@@ -0,0 +1,22 @@
+// Copyright (c) 2018, 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 classmerging;
+
+public class ClassWithNativeMethodTest {
+
+  public static void main(String[] args) {
+    B obj = new B();
+
+    // Make sure that A.method is not removed by tree shaking.
+    if (args.length == 42) {
+      obj.method();
+    }
+  }
+
+  public static class A {
+    public native void method();
+  }
+
+  public static class B extends A {}
+}
diff --git a/src/test/examples/classmerging/keep-rules.txt b/src/test/examples/classmerging/keep-rules.txt
index 516e39d..b3e0ad1 100644
--- a/src/test/examples/classmerging/keep-rules.txt
+++ b/src/test/examples/classmerging/keep-rules.txt
@@ -7,6 +7,9 @@
 -keep public class classmerging.Test {
   public static void main(...);
 }
+-keep public class classmerging.ClassWithNativeMethodTest {
+  public static void main(...);
+}
 -keep public class classmerging.ConflictInGeneratedNameTest {
   public static void main(...);
 }
diff --git a/src/test/examplesAndroidO/classmerging/MergeDefaultMethodIntoClassTest.java b/src/test/examplesAndroidO/classmerging/MergeDefaultMethodIntoClassTest.java
new file mode 100644
index 0000000..10f995d
--- /dev/null
+++ b/src/test/examplesAndroidO/classmerging/MergeDefaultMethodIntoClassTest.java
@@ -0,0 +1,23 @@
+// Copyright (c) 2018, 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 classmerging;
+
+public class MergeDefaultMethodIntoClassTest {
+
+  public static void main(String[] args) {
+    // Note: Important that the static type of [obj] is A, such that the call to f becomes an
+    // invoke-interface instruction and not invoke-virtual instruction.
+    A obj = new B();
+    obj.f();
+  }
+
+  public interface A {
+    default void f() {
+      System.out.println("In A.f");
+    }
+  }
+
+  public static class B implements A {}
+}
diff --git a/src/test/examplesAndroidO/classmerging/keep-rules.txt b/src/test/examplesAndroidO/classmerging/keep-rules.txt
index 9868dcf..fc91808 100644
--- a/src/test/examplesAndroidO/classmerging/keep-rules.txt
+++ b/src/test/examplesAndroidO/classmerging/keep-rules.txt
@@ -7,6 +7,9 @@
 -keep public class classmerging.LambdaRewritingTest {
   public static void main(...);
 }
+-keep public class classmerging.MergeDefaultMethodIntoClassTest {
+  public static void main(...);
+}
 
 # TODO(herhut): Consider supporting merging of inner-class attributes.
 # -keepattributes *
\ No newline at end of file
diff --git a/src/test/java/com/android/tools/r8/classmerging/ClassMergingTest.java b/src/test/java/com/android/tools/r8/classmerging/ClassMergingTest.java
index 59accb3..c2df29a 100644
--- a/src/test/java/com/android/tools/r8/classmerging/ClassMergingTest.java
+++ b/src/test/java/com/android/tools/r8/classmerging/ClassMergingTest.java
@@ -5,6 +5,7 @@
 
 import static com.android.tools.r8.smali.SmaliBuilder.buildCode;
 import static com.android.tools.r8.utils.DexInspectorMatchers.isPresent;
+import static org.hamcrest.CoreMatchers.containsString;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertThat;
@@ -24,6 +25,7 @@
 import com.android.tools.r8.ir.optimize.Inliner.Reason;
 import com.android.tools.r8.origin.Origin;
 import com.android.tools.r8.smali.SmaliBuilder;
+import com.android.tools.r8.utils.AndroidApiLevel;
 import com.android.tools.r8.utils.AndroidApp;
 import com.android.tools.r8.utils.DexInspector;
 import com.android.tools.r8.utils.DexInspector.ClassSubject;
@@ -496,6 +498,58 @@
   }
 
   @Test
+  public void testMergeDefaultMethodIntoClass() throws Exception {
+    String main = "classmerging.MergeDefaultMethodIntoClassTest";
+    Path[] programFiles =
+        new Path[] {
+          JAVA8_CF_DIR.resolve("MergeDefaultMethodIntoClassTest.class"),
+          JAVA8_CF_DIR.resolve("MergeDefaultMethodIntoClassTest$A.class"),
+          JAVA8_CF_DIR.resolve("MergeDefaultMethodIntoClassTest$B.class")
+        };
+    ImmutableSet<String> preservedClassNames =
+        ImmutableSet.of(
+            "classmerging.MergeDefaultMethodIntoClassTest",
+            "classmerging.MergeDefaultMethodIntoClassTest$B");
+    AndroidApp app =
+        AndroidApp.builder()
+            .addProgramFiles(programFiles)
+            .addLibraryFile(ToolHelper.getAndroidJar(AndroidApiLevel.O))
+            .build();
+
+    // Sanity check that there is actually an invoke-interface instruction in the input. We need
+    // to make sure that this invoke-interface instruction is translated to invoke-virtual after
+    // the classes A and B are merged.
+    DexInspector inputInspector = new DexInspector(app);
+    ClassSubject clazz = inputInspector.clazz("classmerging.MergeDefaultMethodIntoClassTest");
+    assertThat(clazz, isPresent());
+    MethodSubject method = clazz.method("void", "main", ImmutableList.of("java.lang.String[]"));
+    assertThat(method, isPresent());
+    assertThat(
+        method.getMethod().getCode().asJarCode().toString(),
+        containsString("INVOKEINTERFACE classmerging/MergeDefaultMethodIntoClassTest$A.f"));
+
+    runTestOnInput(main, app, preservedClassNames::contains, getProguardConfig(JAVA8_EXAMPLE_KEEP));
+  }
+
+  @Test
+  public void testNativeMethod() throws Exception {
+    String main = "classmerging.ClassWithNativeMethodTest";
+    Path[] programFiles =
+        new Path[] {
+          CF_DIR.resolve("ClassWithNativeMethodTest.class"),
+          CF_DIR.resolve("ClassWithNativeMethodTest$A.class"),
+          CF_DIR.resolve("ClassWithNativeMethodTest$B.class")
+        };
+    // Ensures that the class A with a native method has not been merged into its subclass B.
+    ImmutableSet<String> preservedClassNames =
+        ImmutableSet.of(
+            "classmerging.ClassWithNativeMethodTest",
+            "classmerging.ClassWithNativeMethodTest$A",
+            "classmerging.ClassWithNativeMethodTest$B");
+    runTest(main, programFiles, preservedClassNames::contains);
+  }
+
+  @Test
   public void testNoIllegalClassAccess() throws Exception {
     String main = "classmerging.SimpleInterfaceAccessTest";
     Path[] programFiles =