Add integration test to run odrefresh in VM

Old test case is renamed for the deprecation.

The new test currently exclude cache-info.xml since there are still
inconsistency (irrelevant APEXes and timestamp).

Bug: 205750213
Test: atest ComposHostTestCases
Change-Id: If422393faa421298184dc4c7d635dd0ebbbe376b
diff --git a/compos/tests/java/android/compos/test/ComposTestCase.java b/compos/tests/java/android/compos/test/ComposTestCase.java
index 5f4bd00..5ef6649 100644
--- a/compos/tests/java/android/compos/test/ComposTestCase.java
+++ b/compos/tests/java/android/compos/test/ComposTestCase.java
@@ -87,6 +87,56 @@
         }
 
         // Save the expected checksum for the output directory.
+        String expectedChecksumSnapshot = checksumDirectoryContentPartial(android,
+                ODREFRESH_OUTPUT_DIR);
+
+        // --check may delete the output.
+        CommandResult result = runOdrefresh(android, "--check");
+        assertThat(result.getExitCode()).isEqualTo(OKAY);
+
+        // Make sure we generate a fresh instance.
+        android.tryRun("rm", "-rf", COMPOS_TEST_ROOT);
+        // TODO: remove once composd starts to clean up the directory.
+        android.tryRun("rm", "-rf", ODREFRESH_OUTPUT_DIR);
+
+        // Expect the compilation in Compilation OS to finish successfully.
+        {
+            long start = System.currentTimeMillis();
+            result =
+                    android.runForResultWithTimeout(
+                            ODREFRESH_TIMEOUT_MS, COMPOSD_CMD_BIN, "async-odrefresh");
+            long elapsed = System.currentTimeMillis() - start;
+            assertThat(result.getExitCode()).isEqualTo(0);
+            CLog.i("Comp OS compilation took " + elapsed + "ms");
+        }
+        killVmAndReconnectAdb();
+
+        // Save the actual checksum for the output directory.
+        String actualChecksumSnapshot = checksumDirectoryContentPartial(android,
+                ODREFRESH_OUTPUT_DIR);
+
+        // Expect the output of Comp OS to be the same as compiled on Android.
+        assertThat(actualChecksumSnapshot).isEqualTo(expectedChecksumSnapshot);
+
+        // Expect extra files generated by CompOS exist.
+        android.assumeSuccess("test -f " + ODREFRESH_OUTPUT_DIR + "/compos.info");
+        android.assumeSuccess("test -f " + ODREFRESH_OUTPUT_DIR + "/compos.info.signature");
+    }
+
+    @Test
+    public void testOdrefreshDeprecated() throws Exception {
+        CommandRunner android = new CommandRunner(getDevice());
+
+        // Prepare the groundtruth. The compilation on Android should finish successfully.
+        {
+            long start = System.currentTimeMillis();
+            CommandResult result = runOdrefresh(android, "--force-compile");
+            long elapsed = System.currentTimeMillis() - start;
+            assertThat(result.getExitCode()).isEqualTo(COMPILATION_SUCCESS);
+            CLog.i("Local compilation took " + elapsed + "ms");
+        }
+
+        // Save the expected checksum for the output directory.
         String expectedChecksumSnapshot = checksumDirectoryContent(android, ODREFRESH_OUTPUT_DIR);
 
         // Let --check clean up the output.
@@ -151,4 +201,15 @@
         // Sort by filename (second column) to make comparison easier.
         return runner.run("find " + path + " -type f -exec sha256sum {} \\; | sort -k2");
     }
+
+    private String checksumDirectoryContentPartial(CommandRunner runner, String path)
+            throws Exception {
+        // Sort by filename (second column) to make comparison easier. Filter out compos.info and
+        // compos.info.signature since it's only generated by CompOS.
+        // TODO(b/210473615): Remove irrelevant APEXes (i.e. those aren't contributing to the
+        // classpaths, thus not in the VM) from cache-info.xml.
+        return runner.run("cd " + path + "; find -type f -exec sha256sum {} \\;"
+                + "| grep -v cache-info.xml | grep -v compos.info"
+                + "| sort -k2");
+    }
 }