Fix UsesLibraryTest for WITH_DEXPREOPT=false

Also, fix testMissingLibrary logic and add some more docs.

Bug: 63081983
Test: cts-tradefed run cts -m CtsAppSecurityHostTestCases --test
android.appsecurity.cts.UsesLibraryHostTest

(cherry picked from commit b7dc1f0e50fb43bb050c258548f5aa2166e74f53)

Change-Id: I219a3033042685ac11f5dd6a6c921f41fb8f7b9b
diff --git a/hostsidetests/appsecurity/test-apps/UsesLibraryApp/src/com/android/cts/useslibrary/UsesLibraryTest.java b/hostsidetests/appsecurity/test-apps/UsesLibraryApp/src/com/android/cts/useslibrary/UsesLibraryTest.java
index 231bddc..b9dc639 100644
--- a/hostsidetests/appsecurity/test-apps/UsesLibraryApp/src/com/android/cts/useslibrary/UsesLibraryTest.java
+++ b/hostsidetests/appsecurity/test-apps/UsesLibraryApp/src/com/android/cts/useslibrary/UsesLibraryTest.java
@@ -34,26 +34,32 @@
 public class UsesLibraryTest extends InstrumentationTestCase {
     private static final String TAG = "UsesLibraryTest";
 
+    /**
+     * Verify that the test apk is backed by an oat file in the presence of shared libraries.
+     *
+     * This essentially verifies that dex2oat was not disabled and that hopefully
+     * the shared library check did something and was successful.
+     * Note that we cannot verify the shared library oat files (if WITH_DEXPREOPT=false
+     * we do not preopt the shared libraries).
+     */
     public void testUsesLibrary() throws Exception {
         ClassLoader loader = getClass().getClassLoader();
         if (loader instanceof BaseDexClassLoader) {
-            Object[] dexElements = getDexElementsFromClassLoader((BaseDexClassLoader) loader);
-            for (Object dexElement : dexElements) {
-                DexFile dexFile = getDexFileFromDexElement(dexElement);
-                assertTrue(isDexFileBackedByOatFile(dexFile));
-            }
+            DexFile apkDexFile = getTestDexFile((BaseDexClassLoader) loader);
+            assertTrue(isDexFileBackedByOatFile(apkDexFile));
         }
     }
 
+    /**
+     * Verify that the test apk is backed by an oat file when the shared libraries are missing
+     * from the class path.
+     */
     public void testMissingLibrary() throws Exception {
         ClassLoader loader = getClass().getClassLoader();
         if (loader instanceof BaseDexClassLoader) {
-            Object[] dexElements = getDexElementsFromClassLoader((BaseDexClassLoader) loader);
-            assertTrue(dexElements != null && dexElements.length > 1);
+            DexFile apkDexFile = getTestDexFile((BaseDexClassLoader) loader);
 
-            DexFile dexFile = getDexFileFromDexElement(dexElements[1]);
-            String testApkPath = dexFile.getName();
-            PathClassLoader testLoader = new PathClassLoader(testApkPath, null);
+            PathClassLoader testLoader = new PathClassLoader(apkDexFile.getName(), null);
             Object[] testDexElements = getDexElementsFromClassLoader(testLoader);
             assertTrue(testDexElements != null && testDexElements.length == 1);
 
@@ -62,6 +68,9 @@
         }
     }
 
+    /**
+     * Verify that we punt to run from apk if the classpath generates a class collision failure.
+     */
     public void testDuplicateLibrary() throws Exception {
         ClassLoader loader = getClass().getClassLoader();
         if (loader instanceof BaseDexClassLoader) {
@@ -108,6 +117,14 @@
         return (DexFile) dexFileField.get(dexElement);
     }
 
+    private DexFile getTestDexFile(BaseDexClassLoader loader) throws Exception {
+        Object[] dexElements = getDexElementsFromClassLoader(loader);
+        assertTrue(dexElements != null && dexElements.length > 0);
+        // First dex files are shared libraries and the cts instrumentation library.
+        // The last dex file should be the test apk file: com.android.cts.useslibrary.
+        return getDexFileFromDexElement(dexElements[dexElements.length - 1]);
+    }
+
     private boolean isDexFileBackedByOatFile(DexFile dexFile) throws Exception {
         Method isBackedByOatFileMethod = DexFile.class.getDeclaredMethod("isBackedByOatFile");
         isBackedByOatFileMethod.setAccessible(true);