Fix handling of "android.test.mock" in class loader context.

Mimick the way PackageManager handles it at runtime: do add it to class
loader context for apps with targetSdkVersion < 30, but only if
"android.test.runner" is used. Previously it was not added at all.

Test: lunch aosp_cf_x86_phone-userdebug && m
Bug: 132357300
Change-Id: I4c06635277ab13e21069b9fa0b46eb6a2547dfdd
diff --git a/dexpreopt/dexpreopt.go b/dexpreopt/dexpreopt.go
index 903677f..3605d9d 100644
--- a/dexpreopt/dexpreopt.go
+++ b/dexpreopt/dexpreopt.go
@@ -312,8 +312,7 @@
 		}
 
 		// Conditional class loader context for API version < 30.
-		const testBase = "android.test.base"
-		if !classLoaderContexts.addLibs(ctx, 30, module, testBase) {
+		if !classLoaderContexts.addLibs(ctx, 30, module, OptionalCompatUsesLibs30...) {
 			return nil
 		}
 
@@ -343,6 +342,9 @@
 		for i, lib := range clc.Names {
 			if android.InList(lib, usesLibs) {
 				// skip compatibility libraries that are already included in unconditional context
+			} else if lib == AndroidTestMock && !android.InList("android.test.runner", usesLibs) {
+				// android.test.mock is only needed as a compatibility library (in conditional class
+				// loader context) if android.test.runner is used, otherwise skip it
 			} else {
 				clcMap[sdkVer].addLib(lib, clc.Host[i], clc.Target[i])
 			}
diff --git a/java/app_test.go b/java/app_test.go
index 98945da..f2e4349 100644
--- a/java/app_test.go
+++ b/java/app_test.go
@@ -2838,6 +2838,7 @@
 	}
 
 	// Test conditional context for target SDK version 30.
+	// "android.test.mock" is absent because "android.test.runner" is not used.
 	if w := `--target-classpath-for-sdk 30` +
 		` /system/framework/android.test.base.jar `; !strings.Contains(cmd, w) {
 		t.Errorf("wanted %q in %q", w, cmd)
@@ -2852,8 +2853,10 @@
 	}
 
 	// Test conditional context for target SDK version 30.
+	// "android.test.mock" is present because "android.test.runner" is used.
 	if w := `--target-classpath-for-sdk 30` +
-		` /system/framework/android.test.base.jar `; !strings.Contains(cmd, w) {
+		` /system/framework/android.test.base.jar` +
+		`:/system/framework/android.test.mock.jar `; !strings.Contains(cmd, w) {
 		t.Errorf("wanted %q in %q", w, cmd)
 	}
 }