Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 1b82e6a1 authored by Paul Duffin's avatar Paul Duffin
Browse files

Add support for java_test in sdk

Adds java_test_import module type for use by the sdk snapshot and
adds java_tests property to the sdk and sdk_snapshot module type.

This is needed for the conscrypt test sdk.

Bug: 143678475
Test: m nothing
Change-Id: Ied4c56c978dac2f92a9b3bc34b3235d7eeba2fd3
parent e6029182
Loading
Loading
Loading
Loading
+107 −6
Original line number Diff line number Diff line
@@ -53,6 +53,12 @@ func init() {
			},
		},
	})

	android.RegisterSdkMemberType(&testSdkMemberType{
		SdkMemberTypeBase: android.SdkMemberTypeBase{
			PropertyName: "java_tests",
		},
	})
}

func RegisterJavaBuildComponents(ctx android.RegistrationContext) {
@@ -66,6 +72,7 @@ func RegisterJavaBuildComponents(ctx android.RegistrationContext) {
	ctx.RegisterModuleType("java_test", TestFactory)
	ctx.RegisterModuleType("java_test_helper_library", TestHelperLibraryFactory)
	ctx.RegisterModuleType("java_test_host", TestHostFactory)
	ctx.RegisterModuleType("java_test_import", JavaTestImportFactory)
	ctx.RegisterModuleType("java_import", ImportFactory)
	ctx.RegisterModuleType("java_import_host", ImportFactoryHost)
	ctx.RegisterModuleType("java_device_for_host", DeviceForHostFactory)
@@ -1768,11 +1775,16 @@ const (
	aidlIncludeDir   = "aidl"
	javaDir          = "java"
	jarFileSuffix    = ".jar"
	testConfigSuffix = "-AndroidTest.xml"
)

// path to the jar file of a java library. Relative to <sdk_root>/<api_dir>
func (j *Library) sdkSnapshotFilePathForJar() string {
	return filepath.Join(javaDir, j.Name()+jarFileSuffix)
func sdkSnapshotFilePathForJar(member android.SdkMember) string {
	return sdkSnapshotFilePathForMember(member, jarFileSuffix)
}

func sdkSnapshotFilePathForMember(member android.SdkMember, suffix string) string {
	return filepath.Join(javaDir, member.Name()+suffix)
}

type librarySdkMemberType struct {
@@ -1805,7 +1817,7 @@ func (mt *librarySdkMemberType) buildSnapshot(
	j := variant.(*Library)

	exportedJar := jarToExportGetter(j)
	snapshotRelativeJavaLibPath := j.sdkSnapshotFilePathForJar()
	snapshotRelativeJavaLibPath := sdkSnapshotFilePathForJar(member)
	builder.CopyToSnapshot(exportedJar, snapshotRelativeJavaLibPath)

	for _, dir := range j.AidlIncludeDirs() {
@@ -1932,6 +1944,16 @@ type testHelperLibraryProperties struct {
	Test_suites []string `android:"arch_variant"`
}

type prebuiltTestProperties struct {
	// list of compatibility suites (for example "cts", "vts") that the module should be
	// installed into.
	Test_suites []string `android:"arch_variant"`

	// the name of the test configuration (for example "AndroidTest.xml") that should be
	// installed with the module.
	Test_config *string `android:"path,arch_variant"`
}

type Test struct {
	Library

@@ -1947,6 +1969,14 @@ type TestHelperLibrary struct {
	testHelperLibraryProperties testHelperLibraryProperties
}

type JavaTestImport struct {
	Import

	prebuiltTestProperties prebuiltTestProperties

	testConfig android.Path
}

func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
	j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.testProperties.Test_config, j.testProperties.Test_config_template,
		j.testProperties.Test_suites, j.testProperties.Auto_gen_config)
@@ -1959,6 +1989,53 @@ func (j *TestHelperLibrary) GenerateAndroidBuildActions(ctx android.ModuleContex
	j.Library.GenerateAndroidBuildActions(ctx)
}

func (j *JavaTestImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
	j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.prebuiltTestProperties.Test_config, nil,
		j.prebuiltTestProperties.Test_suites, nil)

	j.Import.GenerateAndroidBuildActions(ctx)
}

type testSdkMemberType struct {
	android.SdkMemberTypeBase
}

func (mt *testSdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
	mctx.AddVariationDependencies(nil, dependencyTag, names...)
}

func (mt *testSdkMemberType) IsInstance(module android.Module) bool {
	_, ok := module.(*Test)
	return ok
}

func (mt *testSdkMemberType) BuildSnapshot(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, member android.SdkMember) {
	variants := member.Variants()
	if len(variants) != 1 {
		sdkModuleContext.ModuleErrorf("sdk contains %d variants of member %q but only one is allowed", len(variants), member.Name())
		for _, variant := range variants {
			sdkModuleContext.ModuleErrorf("    %q", variant)
		}
	}
	variant := variants[0]
	j := variant.(*Test)

	implementationJars := j.ImplementationJars()
	if len(implementationJars) != 1 {
		panic(fmt.Errorf("there must be only one implementation jar from %q", j.Name()))
	}

	snapshotRelativeJavaLibPath := sdkSnapshotFilePathForJar(member)
	builder.CopyToSnapshot(implementationJars[0], snapshotRelativeJavaLibPath)

	snapshotRelativeTestConfigPath := sdkSnapshotFilePathForMember(member, testConfigSuffix)
	builder.CopyToSnapshot(j.testConfig, snapshotRelativeTestConfigPath)

	module := builder.AddPrebuiltModule(member, "java_test_import")
	module.AddProperty("jars", []string{snapshotRelativeJavaLibPath})
	module.AddProperty("test_config", snapshotRelativeTestConfigPath)
}

// java_test builds a and links sources into a `.jar` file for the device, and possibly for the host as well, and
// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
//
@@ -2002,6 +2079,30 @@ func TestHelperLibraryFactory() android.Module {
	return module
}

// java_test_import imports one or more `.jar` files into the build graph as if they were built by a java_test module
// and makes sure that it is added to the appropriate test suite.
//
// By default, a java_test_import has a single variant that expects a `.jar` file containing `.class` files that were
// compiled against an Android classpath.
//
// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
// for host modules.
func JavaTestImportFactory() android.Module {
	module := &JavaTestImport{}

	module.AddProperties(
		&module.Import.properties,
		&module.prebuiltTestProperties)

	module.Import.properties.Installable = proptools.BoolPtr(true)

	android.InitPrebuiltModule(module, &module.properties.Jars)
	android.InitApexModule(module)
	android.InitSdkAwareModule(module)
	InitJavaModule(module, android.HostAndDeviceSupported)
	return module
}

// java_test_host builds a and links sources into a `.jar` file for the host, and creates an `AndroidTest.xml` file to
// allow running the test with `atest` or a `TEST_MAPPING` file.
//
+7 −0
Original line number Diff line number Diff line
@@ -486,6 +486,13 @@ func TestPrebuilts(t *testing.T) {
			name: "stubs-source",
			srcs: ["stubs/sources"],
		}

		java_test_import {
			name: "test",
			jars: ["a.jar"],
			test_suites: ["cts"],
			test_config: "AndroidTest.xml",
		}
		`)

	fooModule := ctx.ModuleForTests("foo", "android_common")
+1 −0
Original line number Diff line number Diff line
@@ -34,6 +34,7 @@ func TestConfig(buildDir string, env map[string]string, bp string, fs map[string
		"GENRULE_NOTICE":         nil,
		"LIB_NOTICE":             nil,
		"TOOL_NOTICE":            nil,
		"AndroidTest.xml":        nil,
		"java-res/a/a":           nil,
		"java-res/b/b":           nil,
		"java-res2/a":            nil,
+106 −0
Original line number Diff line number Diff line
@@ -321,6 +321,112 @@ aidl/foo/bar/Test.aidl -> aidl/aidl/foo/bar/Test.aidl
	)
}

func TestSnapshotWithJavaTest(t *testing.T) {
	result := testSdkWithJava(t, `
		module_exports {
			name: "myexports",
			java_tests: ["myjavatests"],
		}

		java_test {
			name: "myjavatests",
			srcs: ["Test.java"],
			system_modules: "none",
			sdk_version: "none",
			compile_dex: true,
			host_supported: true,
		}
	`)

	result.CheckSnapshot("myexports", "android_common", "",
		checkAndroidBpContents(`
// This is auto-generated. DO NOT EDIT.

java_test_import {
    name: "myexports_myjavatests@current",
    sdk_member_name: "myjavatests",
    jars: ["java/myjavatests.jar"],
    test_config: "java/myjavatests-AndroidTest.xml",
}

java_test_import {
    name: "myjavatests",
    prefer: false,
    jars: ["java/myjavatests.jar"],
    test_config: "java/myjavatests-AndroidTest.xml",
}

module_exports_snapshot {
    name: "myexports@current",
    java_tests: ["myexports_myjavatests@current"],
}
`),
		checkAllCopyRules(`
.intermediates/myjavatests/android_common/javac/myjavatests.jar -> java/myjavatests.jar
.intermediates/myjavatests/android_common/myjavatests.config -> java/myjavatests-AndroidTest.xml
`),
	)
}

func TestHostSnapshotWithJavaTest(t *testing.T) {
	// b/145598135 - Generating host snapshots for anything other than linux is not supported.
	SkipIfNotLinux(t)

	result := testSdkWithJava(t, `
		module_exports {
			name: "myexports",
			device_supported: false,
			host_supported: true,
			java_tests: ["myjavatests"],
		}

		java_test {
			name: "myjavatests",
			device_supported: false,
			host_supported: true,
			srcs: ["Test.java"],
			system_modules: "none",
			sdk_version: "none",
			compile_dex: true,
		}
	`)

	result.CheckSnapshot("myexports", "linux_glibc_common", "",
		checkAndroidBpContents(`
// This is auto-generated. DO NOT EDIT.

java_test_import {
    name: "myexports_myjavatests@current",
    sdk_member_name: "myjavatests",
    device_supported: false,
    host_supported: true,
    jars: ["java/myjavatests.jar"],
    test_config: "java/myjavatests-AndroidTest.xml",
}

java_test_import {
    name: "myjavatests",
    prefer: false,
    device_supported: false,
    host_supported: true,
    jars: ["java/myjavatests.jar"],
    test_config: "java/myjavatests-AndroidTest.xml",
}

module_exports_snapshot {
    name: "myexports@current",
    device_supported: false,
    host_supported: true,
    java_tests: ["myexports_myjavatests@current"],
}
`),
		checkAllCopyRules(`
.intermediates/myjavatests/linux_glibc_common/javac/myjavatests.jar -> java/myjavatests.jar
.intermediates/myjavatests/linux_glibc_common/myjavatests.config -> java/myjavatests-AndroidTest.xml
`),
	)
}

func testSdkWithDroidstubs(t *testing.T, bp string) *testSdkResult {
	t.Helper()