Move some default-methods tests to Java from Smali.

Move all smali tests for default method behavior in
non-source-incompatible contexts to java. Also move some of the
simpler tests for source and binary incompatibilities into java as
well when possible.

Bug: 27310767

Change-Id: I753196f19849494825953c1bf06f15b7132f459b
diff --git a/test/961-default-iface-resolution-generated/build b/test/961-default-iface-resolution-generated/build
index 005f76c..ccebbe4 100755
--- a/test/961-default-iface-resolution-generated/build
+++ b/test/961-default-iface-resolution-generated/build
@@ -26,32 +26,19 @@
 }
 trap 'restore_ulimit' ERR
 
-mkdir -p ./smali
+if [[ $@ != *"--jvm"* ]]; then
+  # Don't do anything with jvm
+  # Hard-wired use of experimental jack.
+  # TODO: fix this temporary work-around for default-methods, see b/19467889
+  export USE_JACK=true
+fi
+
+mkdir -p ./src
 
 # Generate the smali files and expected.txt or fail
-./util-src/generate_smali.py ./smali ./expected.txt
+./util-src/generate_java.py ./src ./expected.txt
 
-# Should we compile with Java source code. By default we will use Smali.
-USES_JAVA_SOURCE="false"
-if [[ $@ == *"--jvm"* ]]; then
-  USES_JAVA_SOURCE="true"
-elif [[ $USE_JACK == "true" ]]; then
-  if "$JACK" -D jack.java.source.version=1.8 >& /dev/null; then
-    USES_JAVA_SOURCE="true"
-  else
-    echo "WARNING: Cannot use jack because it does not support JLS 1.8. Falling back to smali" >&2
-  fi
-fi
-
-if [[ "$USES_JAVA_SOURCE" == "true" ]]; then
-  # We are compiling java code, create it.
-  mkdir -p src
-  ${ANDROID_BUILD_TOP}/art/tools/extract-embedded-java ./smali ./src
-  # Ignore the smali directory.
-  EXTRA_ARGS="--no-smali"
-fi
-
-./default-build "$@" "$EXTRA_ARGS" --experimental default-methods
+./default-build "$@" --experimental default-methods
 
 # Reset the ulimit back to its initial value
 restore_ulimit
diff --git a/test/961-default-iface-resolution-generated/util-src/generate_smali.py b/test/961-default-iface-resolution-generated/util-src/generate_java.py
similarity index 71%
rename from test/961-default-iface-resolution-generated/util-src/generate_smali.py
rename to test/961-default-iface-resolution-generated/util-src/generate_java.py
index 921a096..a205cd6 100755
--- a/test/961-default-iface-resolution-generated/util-src/generate_smali.py
+++ b/test/961-default-iface-resolution-generated/util-src/generate_java.py
@@ -15,7 +15,7 @@
 # limitations under the License.
 
 """
-Generate Smali test files for test 961.
+Generate Java test files for test 961.
 """
 
 import os
@@ -43,48 +43,27 @@
 # every possible interface tree up to 5 layers deep.
 MAX_IFACE_DEPTH = 5
 
-class MainClass(mixins.DumpMixin, mixins.Named, mixins.SmaliFileMixin):
+class MainClass(mixins.DumpMixin, mixins.Named, mixins.JavaFileMixin):
   """
-  A Main.smali file containing the Main class and the main function. It will run
+  A Main.java file containing the Main class and the main function. It will run
   all the test functions we have.
   """
 
   MAIN_CLASS_TEMPLATE = """{copyright}
-
-.class public LMain;
-.super Ljava/lang/Object;
-
-# class Main {{
-
-.method public constructor <init>()V
-    .registers 1
-    invoke-direct {{p0}}, Ljava/lang/Object;-><init>()V
-    return-void
-.end method
-
+class Main {{
 {test_groups}
-
 {main_func}
-
-# }}
+}}
 """
 
   MAIN_FUNCTION_TEMPLATE = """
-#   public static void main(String[] args) {{
-.method public static main([Ljava/lang/String;)V
-    .locals 2
-    sget-object v1, Ljava/lang/System;->out:Ljava/io/PrintStream;
-
+  public static void main(String[] args) {{
     {test_group_invoke}
-
-    return-void
-.end method
-#   }}
+  }}
 """
 
   TEST_GROUP_INVOKE_TEMPLATE = """
-#     {test_name}();
-    invoke-static {{}}, {test_name}()V
+    {test_name}();
 """
 
   def __init__(self):
@@ -114,7 +93,7 @@
 
   def __str__(self):
     """
-    Print the MainClass smali code.
+    Print the MainClass java code.
     """
     all_tests = sorted(self.tests)
     test_invoke = ""
@@ -125,7 +104,7 @@
       test_invoke += self.TEST_GROUP_INVOKE_TEMPLATE.format(test_name=t.get_name())
     main_func = self.MAIN_FUNCTION_TEMPLATE.format(test_group_invoke=test_invoke)
 
-    return self.MAIN_CLASS_TEMPLATE.format(copyright = get_copyright("smali"),
+    return self.MAIN_CLASS_TEMPLATE.format(copyright = get_copyright("java"),
                                            test_groups = test_groups,
                                            main_func = main_func)
 
@@ -136,49 +115,18 @@
   """
 
   TEST_FUNCTION_TEMPLATE = """
-#   public static void {fname}() {{
-#     try {{
-#       {farg} v = new {farg}();
-#       System.out.printf("%s calls default method on %s\\n",
-#                         v.CalledClassName(),
-#                         v.CalledInterfaceName());
-#       return;
-#     }} catch (Error e) {{
-#       e.printStackTrace(System.out);
-#       return;
-#     }}
-#   }}
-.method public static {fname}()V
-    .locals 7
-    :call_{fname}_try_start
-      new-instance v6, L{farg};
-      invoke-direct {{v6}}, L{farg};-><init>()V
-
-      const/4 v0, 2
-      new-array v1,v0, [Ljava/lang/Object;
-      const/4 v0, 0
-      invoke-virtual {{v6}}, L{farg};->CalledClassName()Ljava/lang/String;
-      move-result-object v4
-      aput-object v4,v1,v0
-
-      sget-object v2, Ljava/lang/System;->out:Ljava/io/PrintStream;
-      const-string v3, "%s calls default method on %s\\n"
-
-      invoke-virtual {{v6}}, L{farg};->CalledInterfaceName()Ljava/lang/String;
-      move-result-object v4
-      const/4 v0, 1
-      aput-object v4, v1, v0
-
-      invoke-virtual {{v2,v3,v1}}, Ljava/io/PrintStream;->printf(Ljava/lang/String;[Ljava/lang/Object;)Ljava/io/PrintStream;
-      return-void
-    :call_{fname}_try_end
-    .catch Ljava/lang/Error; {{:call_{fname}_try_start .. :call_{fname}_try_end}} :error_{fname}_start
-    :error_{fname}_start
-      move-exception v3
-      sget-object v2, Ljava/lang/System;->out:Ljava/io/PrintStream;
-      invoke-virtual {{v3,v2}}, Ljava/lang/Error;->printStackTrace(Ljava/io/PrintStream;)V
-      return-void
-.end method
+  public static void {fname}() {{
+    try {{
+      {farg} v = new {farg}();
+        System.out.printf("%s calls default method on %s\\n",
+                          v.CalledClassName(),
+                          v.CalledInterfaceName());
+        return;
+    }} catch (Error e) {{
+      e.printStackTrace(System.out);
+      return;
+    }}
+  }}
 """
 
   def __init__(self, farg):
@@ -202,38 +150,21 @@
 
   def __str__(self):
     """
-    Print the smali code of this function.
+    Print the java code of this function.
     """
     return self.TEST_FUNCTION_TEMPLATE.format(fname=self.get_name(), farg=self.farg.get_name())
 
-class TestClass(mixins.DumpMixin, mixins.Named, mixins.NameComparableMixin, mixins.SmaliFileMixin):
+class TestClass(mixins.DumpMixin, mixins.Named, mixins.NameComparableMixin, mixins.JavaFileMixin):
   """
   A class that will be instantiated to test default method resolution order.
   """
 
   TEST_CLASS_TEMPLATE = """{copyright}
-
-.class public L{class_name};
-.super Ljava/lang/Object;
-.implements L{iface_name};
-
-# public class {class_name} implements {iface_name} {{
-#   public String CalledClassName() {{
-#     return "{tree}";
-#   }}
-# }}
-
-.method public constructor <init>()V
-  .registers 1
-  invoke-direct {{p0}}, Ljava/lang/Object;-><init>()V
-  return-void
-.end method
-
-.method public CalledClassName()Ljava/lang/String;
-  .locals 1
-  const-string v0, "{tree}"
-  return-object v0
-.end method
+public class {class_name} implements {iface_name} {{
+  public String CalledClassName() {{
+    return "{tree}";
+  }}
+}}
 """
 
   def __init__(self, iface):
@@ -276,46 +207,30 @@
 
   def __str__(self):
     """
-    Print the smali code of this class.
+    Print the java code of this class.
     """
-    return self.TEST_CLASS_TEMPLATE.format(copyright = get_copyright('smali'),
+    return self.TEST_CLASS_TEMPLATE.format(copyright = get_copyright('java'),
                                            iface_name = self.iface.get_name(),
                                            tree = self.get_tree(),
                                            class_name = self.class_name)
 
-class TestInterface(mixins.DumpMixin, mixins.Named, mixins.NameComparableMixin, mixins.SmaliFileMixin):
+class TestInterface(mixins.DumpMixin, mixins.Named, mixins.NameComparableMixin, mixins.JavaFileMixin):
   """
   An interface that will be used to test default method resolution order.
   """
 
   TEST_INTERFACE_TEMPLATE = """{copyright}
-.class public abstract interface L{class_name};
-.super Ljava/lang/Object;
-{implements_spec}
-
-# public interface {class_name} {extends} {ifaces} {{
-#   public String CalledClassName();
-.method public abstract CalledClassName()Ljava/lang/String;
-.end method
+public interface {class_name} {extends} {ifaces} {{
+  public String CalledClassName();
 
 {funcs}
-
-# }}
+}}
 """
 
   DEFAULT_FUNC_TEMPLATE = """
-#   public default String CalledInterfaceName() {{
-#     return "{tree}";
-#   }}
-.method public CalledInterfaceName()Ljava/lang/String;
-  .locals 1
-  const-string v0, "{tree}"
-  return-object v0
-.end method
-"""
-
-  IMPLEMENTS_TEMPLATE = """
-.implements L{iface_name};
+  public default String CalledInterfaceName() {{
+    return "{tree}";
+  }}
 """
 
   def __init__(self, ifaces, default):
@@ -357,12 +272,10 @@
 
   def __str__(self):
     """
-    Print the smali code of this interface.
+    Print the java code of this interface.
     """
-    s_ifaces = " "
     j_ifaces = " "
     for i in self.ifaces:
-      s_ifaces += self.IMPLEMENTS_TEMPLATE.format(iface_name = i.get_name())
       j_ifaces += " {},".format(i.get_name())
     j_ifaces = j_ifaces[0:-1]
     if self.default:
@@ -371,8 +284,7 @@
                                                 class_name = self.class_name)
     else:
       funcs = ""
-    return self.TEST_INTERFACE_TEMPLATE.format(copyright = get_copyright('smali'),
-                                               implements_spec = s_ifaces,
+    return self.TEST_INTERFACE_TEMPLATE.format(copyright = get_copyright('java'),
                                                extends = "extends" if len(self.ifaces) else "",
                                                ifaces = j_ifaces,
                                                funcs = funcs,
@@ -451,16 +363,16 @@
   return mc, classes
 
 def main(argv):
-  smali_dir = Path(argv[1])
-  if not smali_dir.exists() or not smali_dir.is_dir():
-    print("{} is not a valid smali dir".format(smali_dir), file=sys.stderr)
+  java_dir = Path(argv[1])
+  if not java_dir.exists() or not java_dir.is_dir():
+    print("{} is not a valid java dir".format(java_dir), file=sys.stderr)
     sys.exit(1)
   expected_txt = Path(argv[2])
   mainclass, all_files = create_all_test_files()
   with expected_txt.open('w') as out:
     print(mainclass.get_expected(), file=out)
   for f in all_files:
-    f.dump(smali_dir)
+    f.dump(java_dir)
 
 if __name__ == '__main__':
   main(sys.argv)