Convert hiddenapi tests to use test fixtures

These tests rely on files provided by javaMockFS() so have been
converted to test fixtures to allow them to remove that dependency
which will allow javaMockFS() to be removed.

Bug: 182638834
Test: m nothing
Change-Id: Ifd4069a74fcf67e555f998ddbc4de3fde26b2aae
diff --git a/java/hiddenapi_singleton_test.go b/java/hiddenapi_singleton_test.go
index fb63820..3894ed3 100644
--- a/java/hiddenapi_singleton_test.go
+++ b/java/hiddenapi_singleton_test.go
@@ -16,68 +16,48 @@
 
 import (
 	"fmt"
-	"strings"
+	"path/filepath"
 	"testing"
 
 	"android/soong/android"
-
 	"github.com/google/blueprint/proptools"
 )
 
-func testConfigWithBootJars(bp string, bootJars []string, prebuiltHiddenApiDir *string) android.Config {
-	config := testConfig(nil, bp, nil)
-	config.TestProductVariables.BootJars = android.CreateTestConfiguredJarList(bootJars)
-	config.TestProductVariables.PrebuiltHiddenApiDir = prebuiltHiddenApiDir
-	return config
+func fixtureSetBootJarsProductVariable(bootJars ...string) android.FixturePreparer {
+	return android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
+		variables.BootJars = android.CreateTestConfiguredJarList(bootJars)
+	})
 }
 
-func testContextWithHiddenAPI(config android.Config) *android.TestContext {
-	ctx := testContext(config)
-	RegisterHiddenApiSingletonComponents(ctx)
-	return ctx
+func fixtureSetPrebuiltHiddenApiDirProductVariable(prebuiltHiddenApiDir *string) android.FixturePreparer {
+	return android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
+		variables.PrebuiltHiddenApiDir = prebuiltHiddenApiDir
+	})
 }
 
-func testHiddenAPIWithConfig(t *testing.T, config android.Config) *android.TestContext {
-	t.Helper()
-
-	ctx := testContextWithHiddenAPI(config)
-
-	run(t, ctx, config)
-	return ctx
-}
-
-func testHiddenAPIBootJars(t *testing.T, bp string, bootJars []string, prebuiltHiddenApiDir *string) (*android.TestContext, android.Config) {
-	config := testConfigWithBootJars(bp, bootJars, prebuiltHiddenApiDir)
-
-	return testHiddenAPIWithConfig(t, config), config
-}
-
-func testHiddenAPIUnbundled(t *testing.T, unbundled bool) (*android.TestContext, android.Config) {
-	config := testConfig(nil, ``, nil)
-	config.TestProductVariables.Always_use_prebuilt_sdks = proptools.BoolPtr(unbundled)
-
-	return testHiddenAPIWithConfig(t, config), config
-}
+var hiddenApiFixtureFactory = javaFixtureFactory.Extend(PrepareForTestWithHiddenApiBuildComponents)
 
 func TestHiddenAPISingleton(t *testing.T) {
-	ctx, _ := testHiddenAPIBootJars(t, `
+	result := hiddenApiFixtureFactory.Extend(
+		fixtureSetBootJarsProductVariable("platform:foo"),
+	).RunTestWithBp(t, `
 		java_library {
 			name: "foo",
 			srcs: ["a.java"],
 			compile_dex: true,
 		}
-	`, []string{"platform:foo"}, nil)
+	`)
 
-	hiddenAPI := ctx.SingletonForTests("hiddenapi")
+	hiddenAPI := result.SingletonForTests("hiddenapi")
 	hiddenapiRule := hiddenAPI.Rule("hiddenapi")
 	want := "--boot-dex=" + buildDir + "/.intermediates/foo/android_common/aligned/foo.jar"
-	if !strings.Contains(hiddenapiRule.RuleParams.Command, want) {
-		t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", want, hiddenapiRule.RuleParams.Command)
-	}
+	android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, want)
 }
 
 func TestHiddenAPIIndexSingleton(t *testing.T) {
-	ctx, _ := testHiddenAPIBootJars(t, `
+	result := hiddenApiFixtureFactory.Extend(
+		fixtureSetBootJarsProductVariable("platform:foo", "platform:bar"),
+	).RunTestWithBp(t, `
 		java_library {
 			name: "foo",
 			srcs: ["a.java"],
@@ -106,9 +86,9 @@
 			srcs: ["a.java"],
 			compile_dex: true,
 		}
-	`, []string{"platform:foo", "platform:bar"}, nil)
+	`)
 
-	hiddenAPIIndex := ctx.SingletonForTests("hiddenapi_index")
+	hiddenAPIIndex := result.SingletonForTests("hiddenapi_index")
 	indexRule := hiddenAPIIndex.Rule("singleton-merged-hiddenapi-index")
 	CheckHiddenAPIRuleInputs(t, `
 .intermediates/bar/android_common/hiddenapi/index.csv
@@ -118,7 +98,7 @@
 
 	// Make sure that the foo-hiddenapi-annotations.jar is included in the inputs to the rules that
 	// creates the index.csv file.
-	foo := ctx.ModuleForTests("foo", "android_common")
+	foo := result.ModuleForTests("foo", "android_common")
 	indexParams := foo.Output("hiddenapi/index.csv")
 	CheckHiddenAPIRuleInputs(t, `
 .intermediates/foo-hiddenapi-annotations/android_common/javac/foo-hiddenapi-annotations.jar
@@ -127,7 +107,15 @@
 }
 
 func TestHiddenAPISingletonWithSourceAndPrebuiltPreferredButNoDex(t *testing.T) {
-	config := testConfigWithBootJars(`
+	expectedErrorMessage :=
+		"hiddenapi has determined that the source module \"foo\" should be ignored as it has been" +
+			" replaced by the prebuilt module \"prebuilt_foo\" but unfortunately it does not provide a" +
+			" suitable boot dex jar"
+
+	hiddenApiFixtureFactory.Extend(
+		fixtureSetBootJarsProductVariable("platform:foo"),
+	).ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(expectedErrorMessage)).
+		RunTestWithBp(t, `
 		java_library {
 			name: "foo",
 			srcs: ["a.java"],
@@ -139,35 +127,30 @@
 			jars: ["a.jar"],
 			prefer: true,
 		}
-	`, []string{"platform:foo"}, nil)
-
-	ctx := testContextWithHiddenAPI(config)
-
-	runWithErrors(t, ctx, config,
-		"hiddenapi has determined that the source module \"foo\" should be ignored as it has been"+
-			" replaced by the prebuilt module \"prebuilt_foo\" but unfortunately it does not provide a"+
-			" suitable boot dex jar")
+	`)
 }
 
 func TestHiddenAPISingletonWithPrebuilt(t *testing.T) {
-	ctx, _ := testHiddenAPIBootJars(t, `
+	result := hiddenApiFixtureFactory.Extend(
+		fixtureSetBootJarsProductVariable("platform:foo"),
+	).RunTestWithBp(t, `
 		java_import {
 			name: "foo",
 			jars: ["a.jar"],
 			compile_dex: true,
 	}
-	`, []string{"platform:foo"}, nil)
+	`)
 
-	hiddenAPI := ctx.SingletonForTests("hiddenapi")
+	hiddenAPI := result.SingletonForTests("hiddenapi")
 	hiddenapiRule := hiddenAPI.Rule("hiddenapi")
 	want := "--boot-dex=" + buildDir + "/.intermediates/foo/android_common/aligned/foo.jar"
-	if !strings.Contains(hiddenapiRule.RuleParams.Command, want) {
-		t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", want, hiddenapiRule.RuleParams.Command)
-	}
+	android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, want)
 }
 
 func TestHiddenAPISingletonWithPrebuiltUseSource(t *testing.T) {
-	ctx, _ := testHiddenAPIBootJars(t, `
+	result := hiddenApiFixtureFactory.Extend(
+		fixtureSetBootJarsProductVariable("platform:foo"),
+	).RunTestWithBp(t, `
 		java_library {
 			name: "foo",
 			srcs: ["a.java"],
@@ -180,23 +163,21 @@
 			compile_dex: true,
 			prefer: false,
 		}
-	`, []string{"platform:foo"}, nil)
+	`)
 
-	hiddenAPI := ctx.SingletonForTests("hiddenapi")
+	hiddenAPI := result.SingletonForTests("hiddenapi")
 	hiddenapiRule := hiddenAPI.Rule("hiddenapi")
 	fromSourceJarArg := "--boot-dex=" + buildDir + "/.intermediates/foo/android_common/aligned/foo.jar"
-	if !strings.Contains(hiddenapiRule.RuleParams.Command, fromSourceJarArg) {
-		t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", fromSourceJarArg, hiddenapiRule.RuleParams.Command)
-	}
+	android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, fromSourceJarArg)
 
 	prebuiltJarArg := "--boot-dex=" + buildDir + "/.intermediates/foo/android_common/dex/foo.jar"
-	if strings.Contains(hiddenapiRule.RuleParams.Command, prebuiltJarArg) {
-		t.Errorf("Did not expect %s in hiddenapi command, but it was present: %s", prebuiltJarArg, hiddenapiRule.RuleParams.Command)
-	}
+	android.AssertStringDoesNotContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, prebuiltJarArg)
 }
 
 func TestHiddenAPISingletonWithPrebuiltOverrideSource(t *testing.T) {
-	ctx, _ := testHiddenAPIBootJars(t, `
+	result := hiddenApiFixtureFactory.Extend(
+		fixtureSetBootJarsProductVariable("platform:foo"),
+	).RunTestWithBp(t, `
 		java_library {
 			name: "foo",
 			srcs: ["a.java"],
@@ -209,19 +190,15 @@
 			compile_dex: true,
 			prefer: true,
 		}
-	`, []string{"platform:foo"}, nil)
+	`)
 
-	hiddenAPI := ctx.SingletonForTests("hiddenapi")
+	hiddenAPI := result.SingletonForTests("hiddenapi")
 	hiddenapiRule := hiddenAPI.Rule("hiddenapi")
 	prebuiltJarArg := "--boot-dex=" + buildDir + "/.intermediates/prebuilt_foo/android_common/dex/foo.jar"
-	if !strings.Contains(hiddenapiRule.RuleParams.Command, prebuiltJarArg) {
-		t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", prebuiltJarArg, hiddenapiRule.RuleParams.Command)
-	}
+	android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, prebuiltJarArg)
 
 	fromSourceJarArg := "--boot-dex=" + buildDir + "/.intermediates/foo/android_common/aligned/foo.jar"
-	if strings.Contains(hiddenapiRule.RuleParams.Command, fromSourceJarArg) {
-		t.Errorf("Did not expect %s in hiddenapi command, but it was present: %s", fromSourceJarArg, hiddenapiRule.RuleParams.Command)
-	}
+	android.AssertStringDoesNotContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, fromSourceJarArg)
 }
 
 func TestHiddenAPISingletonSdks(t *testing.T) {
@@ -251,29 +228,25 @@
 	}
 	for _, tc := range testCases {
 		t.Run(tc.name, func(t *testing.T) {
-			ctx, _ := testHiddenAPIUnbundled(t, tc.unbundledBuild)
+			result := hiddenApiFixtureFactory.Extend(
+				android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
+					variables.Always_use_prebuilt_sdks = proptools.BoolPtr(tc.unbundledBuild)
+				}),
+			).RunTest(t)
 
-			hiddenAPI := ctx.SingletonForTests("hiddenapi")
+			hiddenAPI := result.SingletonForTests("hiddenapi")
 			hiddenapiRule := hiddenAPI.Rule("hiddenapi")
 			wantPublicStubs := "--public-stub-classpath=" + generateSdkDexPath(tc.publicStub, tc.unbundledBuild)
-			if !strings.Contains(hiddenapiRule.RuleParams.Command, wantPublicStubs) {
-				t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", wantPublicStubs, hiddenapiRule.RuleParams.Command)
-			}
+			android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, wantPublicStubs)
 
 			wantSystemStubs := "--system-stub-classpath=" + generateSdkDexPath(tc.systemStub, tc.unbundledBuild)
-			if !strings.Contains(hiddenapiRule.RuleParams.Command, wantSystemStubs) {
-				t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", wantSystemStubs, hiddenapiRule.RuleParams.Command)
-			}
+			android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, wantSystemStubs)
 
 			wantTestStubs := "--test-stub-classpath=" + generateSdkDexPath(tc.testStub, tc.unbundledBuild)
-			if !strings.Contains(hiddenapiRule.RuleParams.Command, wantTestStubs) {
-				t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", wantTestStubs, hiddenapiRule.RuleParams.Command)
-			}
+			android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, wantTestStubs)
 
-			wantCorePlatformStubs := "--core-platform-stub-classpath=" + generateDexPath(tc.corePlatformStub)
-			if !strings.Contains(hiddenapiRule.RuleParams.Command, wantCorePlatformStubs) {
-				t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", wantCorePlatformStubs, hiddenapiRule.RuleParams.Command)
-			}
+			wantCorePlatformStubs := "--core-platform-stub-classpath=" + generateDexPath(defaultJavaDir, tc.corePlatformStub)
+			android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, wantCorePlatformStubs)
 		})
 	}
 }
@@ -282,15 +255,15 @@
 	return fmt.Sprintf("%s/.intermediates/%s/android_common/%s/%s.jar", buildDir, subDir, dex, module)
 }
 
-func generateDexPath(module string) string {
-	return generateDexedPath(module, "dex", module)
+func generateDexPath(moduleDir string, module string) string {
+	return generateDexedPath(filepath.Join(moduleDir, module), "dex", module)
 }
 
 func generateSdkDexPath(module string, unbundled bool) string {
 	if unbundled {
 		return generateDexedPath("prebuilts/sdk/"+module, "dex", module)
 	}
-	return generateDexPath(module)
+	return generateDexPath(defaultJavaDir, module)
 }
 
 func TestHiddenAPISingletonWithPrebuiltCsvFile(t *testing.T) {
@@ -304,36 +277,33 @@
 	// Where to find the prebuilt hiddenapi files:
 	prebuiltHiddenApiDir := "path/to/prebuilt/hiddenapi"
 
-	ctx, _ := testHiddenAPIBootJars(t, `
+	result := hiddenApiFixtureFactory.Extend(
+		fixtureSetBootJarsProductVariable("platform:foo"),
+		fixtureSetPrebuiltHiddenApiDirProductVariable(&prebuiltHiddenApiDir),
+	).RunTestWithBp(t, `
 		java_import {
 			name: "foo",
 			jars: ["a.jar"],
 			compile_dex: true,
 	}
-	`, []string{"platform:foo"}, &prebuiltHiddenApiDir)
+	`)
 
 	expectedCpInput := prebuiltHiddenApiDir + "/hiddenapi-flags.csv"
 	expectedCpOutput := buildDir + "/hiddenapi/hiddenapi-flags.csv"
 	expectedFlagsCsv := buildDir + "/hiddenapi/hiddenapi-flags.csv"
 
-	foo := ctx.ModuleForTests("foo", "android_common")
+	foo := result.ModuleForTests("foo", "android_common")
 
-	hiddenAPI := ctx.SingletonForTests("hiddenapi")
+	hiddenAPI := result.SingletonForTests("hiddenapi")
 	cpRule := hiddenAPI.Rule("Cp")
 	actualCpInput := cpRule.BuildParams.Input
 	actualCpOutput := cpRule.BuildParams.Output
 	encodeDexRule := foo.Rule("hiddenAPIEncodeDex")
 	actualFlagsCsv := encodeDexRule.BuildParams.Args["flagsCsv"]
 
-	if actualCpInput.String() != expectedCpInput {
-		t.Errorf("Prebuilt hiddenapi cp rule input mismatch, actual: %s, expected: %s", actualCpInput, expectedCpInput)
-	}
+	android.AssertStringEquals(t, "hiddenapi cp rule input", expectedCpInput, actualCpInput.String())
 
-	if actualCpOutput.String() != expectedCpOutput {
-		t.Errorf("Prebuilt hiddenapi cp rule output mismatch, actual: %s, expected: %s", actualCpOutput, expectedCpOutput)
-	}
+	android.AssertStringEquals(t, "hiddenapi cp rule output", expectedCpOutput, actualCpOutput.String())
 
-	if actualFlagsCsv != expectedFlagsCsv {
-		t.Errorf("Prebuilt hiddenapi encode dex rule flags csv mismatch, actual: %s, expected: %s", actualFlagsCsv, expectedFlagsCsv)
-	}
+	android.AssertStringEquals(t, "hiddenapi encode dex rule flags csv", expectedFlagsCsv, actualFlagsCsv)
 }