Move profile test to text format

Create the profile from primary.prof.txt as a text based format.
This makes it not necessary to update the test when the profile
format changes.

Test: root and non root
Test: cts-tradefed run cts-dev -m CtsCompilationTestCases

Bug: 62040831

Change-Id: Ice77c846aa9065f76cc330e4661bbfe94530cfc1
diff --git a/hostsidetests/compilation/assets/primary.prof b/hostsidetests/compilation/assets/primary.prof
deleted file mode 100644
index 404089d..0000000
--- a/hostsidetests/compilation/assets/primary.prof
+++ /dev/null
Binary files differ
diff --git a/hostsidetests/compilation/assets/primary.prof.txt b/hostsidetests/compilation/assets/primary.prof.txt
new file mode 100644
index 0000000..e74ec47
--- /dev/null
+++ b/hostsidetests/compilation/assets/primary.prof.txt
@@ -0,0 +1,18 @@
+Landroid/cts/compilation/CompilationTargetActivity;
+HLandroid/cts/compilation/CompilationTargetActivity;->m11()I
+HLandroid/cts/compilation/CompilationTargetActivity;->m12()I
+HLandroid/cts/compilation/CompilationTargetActivity;->m13()I
+HLandroid/cts/compilation/CompilationTargetActivity;->m14()I
+HLandroid/cts/compilation/CompilationTargetActivity;->m15()I
+HLandroid/cts/compilation/CompilationTargetActivity;->m16()I
+HLandroid/cts/compilation/CompilationTargetActivity;->m17()I
+HLandroid/cts/compilation/CompilationTargetActivity;->m18()I
+HLandroid/cts/compilation/CompilationTargetActivity;->m19()I
+HLandroid/cts/compilation/CompilationTargetActivity;->m20()I
+HLandroid/cts/compilation/CompilationTargetActivity;->m21()I
+HLandroid/cts/compilation/CompilationTargetActivity;->m22()I
+HLandroid/cts/compilation/CompilationTargetActivity;->m23()I
+HLandroid/cts/compilation/CompilationTargetActivity;->m24()I
+SLandroid/cts/compilation/CompilationTargetActivity;->onResume()V
+SPLandroid/cts/compilation/CompilationTargetActivity;->onPause()V
+
diff --git a/hostsidetests/compilation/src/android/cts/compilation/AdbRootDependentCompilationTest.java b/hostsidetests/compilation/src/android/cts/compilation/AdbRootDependentCompilationTest.java
index 2fc3e1e..1f5d669 100644
--- a/hostsidetests/compilation/src/android/cts/compilation/AdbRootDependentCompilationTest.java
+++ b/hostsidetests/compilation/src/android/cts/compilation/AdbRootDependentCompilationTest.java
@@ -72,8 +72,8 @@
     }
 
     private ITestDevice mDevice;
-    private byte[] profileBytes;
-    private File localProfileFile;
+    private File textProfileFile;
+    private byte[] initialOdexFileContents;
     private File apkFile;
     private boolean mCanEnableDeviceRootAccess;
 
@@ -97,18 +97,19 @@
         mDevice.uninstallPackage(APPLICATION_PACKAGE); // in case it's still installed
         mDevice.installPackage(apkFile, false);
 
-        // Load snapshot of file contents from {@link ProfileLocation#CUR} after
-        // manually running the target application manually for a few minutes.
-        profileBytes = ByteStreams.toByteArray(getClass().getResourceAsStream("/primary.prof"));
-        localProfileFile = File.createTempFile("compilationtest", "prof");
-        Files.write(profileBytes, localProfileFile);
+        // Write the text profile to a temporary file so that we can run profman on it to create a
+        // real profile.
+        byte[] profileBytes = ByteStreams.toByteArray(
+                getClass().getResourceAsStream("/primary.prof.txt"));
         assertTrue("empty profile", profileBytes.length > 0); // sanity check
+        textProfileFile = File.createTempFile("compilationtest", "prof.txt");
+        Files.write(profileBytes, textProfileFile);
     }
 
     @Override
     protected void tearDown() throws Exception {
         FileUtil.deleteFile(apkFile);
-        FileUtil.deleteFile(localProfileFile);
+        FileUtil.deleteFile(textProfileFile);
         mDevice.uninstallPackage(APPLICATION_PACKAGE);
         super.tearDown();
     }
@@ -188,7 +189,7 @@
     }
 
     /**
-     * Places {@link #profileBytes} in the specified locations, recompiles (without -f)
+     * Places the profile in the specified locations, recompiles (without -f)
      * and checks the compiler-filter in the odex file.
      *
      * @return whether the test ran (as opposed to early exit)
@@ -248,7 +249,8 @@
     }
 
     /**
-     * Copies {@link #localProfileFile} to the specified location on the client device.
+     * Copies {@link #textProfileFile} to the device and convert it to a binary profile on the
+     * client device.
      */
     private void writeProfile(ProfileLocation location) throws Exception {
         String targetPath = location.getPath();
@@ -263,12 +265,34 @@
         while (owner.startsWith(" ")) {
             owner = owner.substring(1);
         }
-        executePush(localProfileFile.getAbsolutePath(), targetPath, targetDir);
+
+        String targetPathTemp = targetPath + ".tmp";
+        executePush(textProfileFile.getAbsolutePath(), targetPathTemp, targetDir);
+        assertTrue("Failed to push text profile", doesFileExist(targetPathTemp));
+
+        String targetPathApk = targetPath + ".apk";
+        executePush(apkFile.getAbsolutePath(), targetPathApk, targetDir);
+        assertTrue("Failed to push APK from ", doesFileExist(targetPathApk));
+        // Run profman to create the real profile on device.
+        try {
+            String pathSpec = executeSuShellAdbCommand(1, "pm", "path", APPLICATION_PACKAGE)[0];
+            pathSpec = pathSpec.replace("package:", "");
+            assertTrue("Failed find APK " + pathSpec, doesFileExist(pathSpec));
+            executeSuShellAdbCommand(
+                "profman",
+                "--create-profile-from=" + targetPathTemp,
+                "--apk=" + pathSpec,
+                "--dex-location=" + pathSpec,
+                "--reference-profile-file=" + targetPath);
+        } catch (Exception e) {
+            assertEquals("", e.toString());
+        }
         executeSuShellAdbCommand(0, "chown", owner, targetPath);
         // Verify that the file was written successfully
         assertTrue("failed to create profile file", doesFileExist(targetPath));
-        assertEquals(Integer.toString(profileBytes.length),
-                executeSuShellAdbCommand(1, "stat", "-c", "%s", targetPath)[0]);
+        String[] result = executeSuShellAdbCommand(1, "stat", "-c", "%s", targetPath);
+        assertTrue("profile " + targetPath + " is " + Integer.parseInt(result[0]) + " bytes",
+                   Integer.parseInt(result[0]) > 0);
     }
 
     /**