Revert "Change test_module_config from copying files to symlink ..."

Revert submission 3060229-ron-tmc-symlinks

Reason for revert: Droidmonitor created revert due to b/344045516

Reverted changes: /q/submissionid:3060229-ron-tmc-symlinks

Change-Id: If5045366677163560cdae95c0ab74256b4b49b9a
diff --git a/java/app.go b/java/app.go
index a24099c..d2f2d0b 100644
--- a/java/app.go
+++ b/java/app.go
@@ -1380,8 +1380,6 @@
 		HostRequiredModuleNames: a.HostRequiredModuleNames(),
 		TestSuites:              a.testProperties.Test_suites,
 		IsHost:                  false,
-		LocalCertificate:        a.certificate.AndroidMkString(),
-		IsUnitTest:              Bool(a.testProperties.Test_options.Unit_test),
 	})
 	android.SetProvider(ctx, android.TestOnlyProviderKey, android.TestModuleInformation{
 		TestOnly:       true,
diff --git a/java/java.go b/java/java.go
index ccccbac..9fa6175 100644
--- a/java/java.go
+++ b/java/java.go
@@ -1504,8 +1504,6 @@
 		RequiredModuleNames: j.RequiredModuleNames(),
 		TestSuites:          j.testProperties.Test_suites,
 		IsHost:              true,
-		LocalSdkVersion:     j.sdkVersion.String(),
-		IsUnitTest:          Bool(j.testProperties.Test_options.Unit_test),
 	})
 }
 
diff --git a/tradefed/providers.go b/tradefed/providers.go
index 0abac12..66cb625 100644
--- a/tradefed/providers.go
+++ b/tradefed/providers.go
@@ -6,8 +6,7 @@
 	"github.com/google/blueprint"
 )
 
-// Data that test_module_config[_host] modules types will need from
-// their dependencies to write out build rules and AndroidMkEntries.
+// Output files we need from a base test that we derive from.
 type BaseTestProviderData struct {
 	// data files and apps for android_test
 	InstalledFiles android.Paths
@@ -20,14 +19,8 @@
 	RequiredModuleNames     []string
 	// List of test suites base uses.
 	TestSuites []string
-	// True indicates the base modules is built for Host.
+	// Used for bases that are Host
 	IsHost bool
-	// Base's sdk version for AndroidMkEntries, generally only used for Host modules.
-	LocalSdkVersion string
-	// Base's certificate for AndroidMkEntries, generally only used for device modules.
-	LocalCertificate string
-	// Indicates if the base module was a unit test.
-	IsUnitTest bool
 }
 
 var BaseTestProviderKey = blueprint.NewProvider[BaseTestProviderData]()
diff --git a/tradefed_modules/test_module_config.go b/tradefed_modules/test_module_config.go
index f8518f7..b2d5631 100644
--- a/tradefed_modules/test_module_config.go
+++ b/tradefed_modules/test_module_config.go
@@ -5,8 +5,6 @@
 	"android/soong/tradefed"
 	"encoding/json"
 	"fmt"
-	"io"
-	"strings"
 
 	"github.com/google/blueprint"
 	"github.com/google/blueprint/proptools"
@@ -25,17 +23,14 @@
 type testModuleConfigModule struct {
 	android.ModuleBase
 	android.DefaultableModuleBase
+	base android.Module
 
 	tradefedProperties
 
 	// Our updated testConfig.
 	testConfig android.OutputPath
-	manifest   android.OutputPath
+	manifest   android.InstallPath
 	provider   tradefed.BaseTestProviderData
-
-	supportFiles android.InstallPaths
-
-	isHost bool
 }
 
 // Host is mostly the same as non-host, just some diffs for AddDependency and
@@ -99,6 +94,7 @@
 
 // Takes base's Tradefed Config xml file and generates a new one with the test properties
 // appeneded from this module.
+// Rewrite the name of the apk in "test-file-name" to be our module's name, rather than the original one.
 func (m *testModuleConfigModule) fixTestConfig(ctx android.ModuleContext, baseTestConfig android.Path) android.OutputPath {
 	// Test safe to do when no test_runner_options, but check for that earlier?
 	fixedConfig := android.PathForModuleOut(ctx, "test_config_fixer", ctx.ModuleName()+".config")
@@ -110,8 +106,9 @@
 	}
 	xmlTestModuleConfigSnippet, _ := json.Marshal(options)
 	escaped := proptools.NinjaAndShellEscape(string(xmlTestModuleConfigSnippet))
-	command.FlagWithArg("--test-runner-options=", escaped)
-
+	command.FlagWithArg("--test-file-name=", ctx.ModuleName()+".apk").
+		FlagWithArg("--orig-test-file-name=", *m.tradefedProperties.Base+".apk").
+		FlagWithArg("--test-runner-options=", escaped)
 	rule.Build("fix_test_config", "fix test config")
 	return fixedConfig.OutputPath
 }
@@ -193,7 +190,6 @@
 	module.AddProperties(&module.tradefedProperties)
 	android.InitAndroidMultiTargetsArchModule(module, android.HostSupported, android.MultilibCommon)
 	android.InitDefaultableModule(module)
-	module.isHost = true
 
 	return module
 }
@@ -202,66 +198,39 @@
 var _ android.AndroidMkEntriesProvider = (*testModuleConfigModule)(nil)
 
 func (m *testModuleConfigModule) AndroidMkEntries() []android.AndroidMkEntries {
-	appClass := "APPS"
-	include := "$(BUILD_SYSTEM)/soong_app_prebuilt.mk"
-	if m.isHost {
-		appClass = "JAVA_LIBRARIES"
-		include = "$(BUILD_SYSTEM)/soong_java_prebuilt.mk"
-	}
-	return []android.AndroidMkEntries{{
-		Class:      appClass,
-		OutputFile: android.OptionalPathForPath(m.manifest),
-		Include:    include,
-		Required:   []string{*m.Base},
-		ExtraEntries: []android.AndroidMkExtraEntriesFunc{
-			func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
-				entries.SetPath("LOCAL_FULL_TEST_CONFIG", m.testConfig)
-				entries.SetString("LOCAL_MODULE_TAGS", "tests")
-				entries.SetString("LOCAL_TEST_MODULE_CONFIG_BASE", *m.Base)
-				if m.provider.LocalSdkVersion != "" {
-					entries.SetString("LOCAL_SDK_VERSION", m.provider.LocalSdkVersion)
-				}
-				if m.provider.LocalCertificate != "" {
-					entries.SetString("LOCAL_CERTIFICATE", m.provider.LocalCertificate)
-				}
+	// We rely on base writing LOCAL_COMPATIBILITY_SUPPORT_FILES for its data files
+	entriesList := m.base.(android.AndroidMkEntriesProvider).AndroidMkEntries()
+	entries := &entriesList[0]
+	entries.OutputFile = android.OptionalPathForPath(m.provider.OutputFile)
+	entries.ExtraEntries = append(entries.ExtraEntries, func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
+		entries.SetString("LOCAL_MODULE", m.Name()) //  out module name, not base's
 
-				entries.SetBoolIfTrue("LOCAL_IS_UNIT_TEST", m.provider.IsUnitTest)
-				entries.AddCompatibilityTestSuites(m.tradefedProperties.Test_suites...)
+		// Out update config file with extra options.
+		entries.SetPath("LOCAL_FULL_TEST_CONFIG", m.testConfig)
+		entries.SetString("LOCAL_MODULE_TAGS", "tests")
 
-				// The app_prebuilt_internal.mk files try create a copy of the OutputFile as an .apk.
-				// Normally, this copies the "package.apk" from the intermediate directory here.
-				// To prevent the copy of the large apk and to prevent confusion with the real .apk we
-				// link to, we set the STEM here to a bogus name and we set OutputFile to a small file (our manifest).
-				// We do this so we don't have to add more conditionals to base_rules.mk
-				// soong_java_prebult has the same issue for .jars so use this in both module types.
-				entries.SetString("LOCAL_MODULE_STEM", fmt.Sprintf("UNUSED-%s", *m.Base))
+		// Don't append to base's test-suites, only use the ones we define, so clear it before
+		// appending to it.
+		entries.SetString("LOCAL_COMPATIBILITY_SUITE", "")
+		entries.AddCompatibilityTestSuites(m.tradefedProperties.Test_suites...)
 
-				// In normal java/app modules, the module writes LOCAL_COMPATIBILITY_SUPPORT_FILES
-				// and then base_rules.mk ends up copying each of those dependencies from .intermediates to the install directory.
-				// tasks/general-tests.mk, tasks/devices-tests.mk also use these to figure out
-				// which testcase files to put in a zip for running tests on another machine.
-				//
-				// We need our files to end up in the zip, but we don't want \.mk files to
-				// `install` files for us.
-				// So we create a new make variable to indicate these should be in the zip
-				// but not installed.
-				entries.AddStrings("LOCAL_SOONG_INSTALLED_COMPATIBILITY_SUPPORT_FILES", m.supportFiles.Strings()...)
-			},
-		},
-		// Ensure each of our supportFiles depends on the installed file in base so that our symlinks will always
-		// resolve.  The provider gives us the .intermediate path for the support file in base, we change it to
-		// the installed path with a string substitution.
-		ExtraFooters: []android.AndroidMkExtraFootersFunc{
-			func(w io.Writer, name, prefix, moduleDir string) {
-				for _, f := range m.supportFiles.Strings() {
-					// convert out/.../testcases/FrameworksServicesTests_contentprotection/file1.apk
-					// to      out/.../testcases/FrameworksServicesTests/file1.apk
-					basePath := strings.Replace(f, "/"+m.Name()+"/", "/"+*m.Base+"/", 1)
-					fmt.Fprintf(w, "%s: %s\n", f, basePath)
-				}
-			},
-		},
-	}}
+		if len(m.provider.HostRequiredModuleNames) > 0 {
+			entries.AddStrings("LOCAL_HOST_REQUIRED_MODULES", m.provider.HostRequiredModuleNames...)
+		}
+		if len(m.provider.RequiredModuleNames) > 0 {
+			entries.AddStrings("LOCAL_REQUIRED_MODULES", m.provider.RequiredModuleNames...)
+		}
+
+		if m.provider.IsHost == false {
+			// Not needed for jar_host_test
+			//
+			// Clear the JNI symbols because they belong to base not us. Either transform the names in the string
+			// or clear the variable because we don't need it, we are copying bases libraries not generating
+			// new ones.
+			entries.SetString("LOCAL_SOONG_JNI_LIBS_SYMBOLS", "")
+		}
+	})
+	return entriesList
 }
 
 func (m *testModuleConfigHostModule) DepsMutator(ctx android.BottomUpMutatorContext) {
@@ -279,7 +248,7 @@
 //   - written via soong_java_prebuilt.mk
 //
 // 4) out/host/linux-x86/testcases/derived-module/* # data dependencies from base.
-//   - written via our InstallSymlink
+//   - written via soong_java_prebuilt.mk
 func (m *testModuleConfigHostModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
 	m.validateBase(ctx, &testModuleConfigHostTag, "java_test_host", true)
 	m.generateManifestAndConfig(ctx)
@@ -291,6 +260,7 @@
 	ctx.VisitDirectDepsWithTag(*depTag, func(dep android.Module) {
 		if provider, ok := android.OtherModuleProvider(ctx, dep, tradefed.BaseTestProviderKey); ok {
 			if baseShouldBeHost == provider.IsHost {
+				m.base = dep
 				m.provider = provider
 			} else {
 				if baseShouldBeHost {
@@ -307,9 +277,7 @@
 
 // Actions to write:
 //  1. manifest file to testcases dir
-//  2. Symlink to base.apk under base's arch dir
-//  3. Symlink to all data dependencies
-//  4. New Module.config / AndroidTest.xml file with our options.
+//  2. New Module.config / AndroidTest.xml file with our options.
 func (m *testModuleConfigModule) generateManifestAndConfig(ctx android.ModuleContext) {
 	// Keep before early returns.
 	android.SetProvider(ctx, android.TestOnlyProviderKey, android.TestModuleInformation{
@@ -324,6 +292,7 @@
 	if m.provider.TestConfig == nil {
 		return
 	}
+
 	// 1) A manifest file listing the base, write text to a tiny file.
 	installDir := android.PathForModuleInstall(ctx, ctx.ModuleName())
 	manifest := android.PathForModuleOut(ctx, "test_module_config.manifest")
@@ -332,53 +301,9 @@
 	//    Assume the primary install file is last
 	// so we need to Install our file last.
 	ctx.InstallFile(installDir, manifest.Base(), manifest)
-	m.manifest = manifest.OutputPath
 
-	// 2) Symlink to base.apk
-	baseApk := m.provider.OutputFile
-
-	// Typically looks like this for baseApk
-	// FrameworksServicesTests
-	// └── x86_64
-	//    └── FrameworksServicesTests.apk
-	symlinkName := fmt.Sprintf("%s/%s", ctx.DeviceConfig().DeviceArch(), baseApk.Base())
-	// Only android_test, not java_host_test puts the output in the DeviceArch dir.
-	if m.provider.IsHost {
-		// testcases/CtsDevicePolicyManagerTestCases
-		// ├── CtsDevicePolicyManagerTestCases.jar
-		symlinkName = baseApk.Base()
-	}
-	target := installedBaseRelativeToHere(symlinkName, *m.tradefedProperties.Base)
-	installedApk := ctx.InstallAbsoluteSymlink(installDir, symlinkName, target)
-	m.supportFiles = append(m.supportFiles, installedApk)
-
-	// 3) Symlink for all data deps
-	// And like this for data files and required modules
-	// FrameworksServicesTests
-	// ├── data
-	// │   └── broken_shortcut.xml
-	// ├── JobTestApp.apk
-	for _, f := range m.provider.InstalledFiles {
-		symlinkName := f.Rel()
-		target := installedBaseRelativeToHere(symlinkName, *m.tradefedProperties.Base)
-		installedPath := ctx.InstallAbsoluteSymlink(installDir, symlinkName, target)
-		m.supportFiles = append(m.supportFiles, installedPath)
-	}
-
-	// 4) Module.config / AndroidTest.xml
+	// 2) Module.config / AndroidTest.xml
 	m.testConfig = m.fixTestConfig(ctx, m.provider.TestConfig)
 }
 
 var _ android.AndroidMkEntriesProvider = (*testModuleConfigHostModule)(nil)
-
-// Given a relative path to a file in the current directory or a subdirectory,
-// return a relative path under our sibling directory named `base`.
-// There should be one "../" for each subdir we descend plus one to backup to "base".
-//
-//	 ThisDir/file1
-//	 ThisDir/subdir/file2
-//	would return "../base/file1" or "../../subdir/file2"
-func installedBaseRelativeToHere(targetFileName string, base string) string {
-	backup := strings.Repeat("../", strings.Count(targetFileName, "/")+1)
-	return fmt.Sprintf("%s%s/%s", backup, base, targetFileName)
-}
diff --git a/tradefed_modules/test_module_config_test.go b/tradefed_modules/test_module_config_test.go
index 97179f5..b2049b1 100644
--- a/tradefed_modules/test_module_config_test.go
+++ b/tradefed_modules/test_module_config_test.go
@@ -16,7 +16,6 @@
 import (
 	"android/soong/android"
 	"android/soong/java"
-	"fmt"
 	"strconv"
 	"strings"
 	"testing"
@@ -70,36 +69,15 @@
 	entries := android.AndroidMkEntriesForTest(t, ctx.TestContext, derived.Module())[0]
 
 	// Ensure some entries from base are there, specifically support files for data and helper apps.
-	// Do not use LOCAL_COMPATIBILITY_SUPPORT_FILES, but instead use LOCAL_SOONG_INSTALLED_COMPATIBILITY_SUPPORT_FILES
-	android.AssertStringPathsRelativeToTopEquals(t, "support-files", ctx.Config,
-		[]string{"out/soong/target/product/test_device/testcases/derived_test/arm64/base.apk",
-			"out/soong/target/product/test_device/testcases/derived_test/HelperApp.apk",
-			"out/soong/target/product/test_device/testcases/derived_test/data/testfile"},
-		entries.EntryMap["LOCAL_SOONG_INSTALLED_COMPATIBILITY_SUPPORT_FILES"])
-	android.AssertArrayString(t, "", entries.EntryMap["LOCAL_COMPATIBILITY_SUPPORT_FILES"], []string{})
-
-	android.AssertArrayString(t, "", entries.EntryMap["LOCAL_REQUIRED_MODULES"], []string{"base"})
-	android.AssertArrayString(t, "", entries.EntryMap["LOCAL_CERTIFICATE"], []string{"build/make/target/product/security/testkey.x509.pem"})
-	android.AssertStringEquals(t, "", entries.Class, "APPS")
+	assertEntryPairValues(t, entries.EntryMap["LOCAL_COMPATIBILITY_SUPPORT_FILES"], []string{"HelperApp.apk", "data/testfile"})
 
 	// And some new derived entries are there.
 	android.AssertArrayString(t, "", entries.EntryMap["LOCAL_MODULE_TAGS"], []string{"tests"})
 
-	android.AssertStringMatches(t, "", entries.EntryMap["LOCAL_FULL_TEST_CONFIG"][0], "derived_test/android_common/test_config_fixer/derived_test.config")
+	// And ones we override
+	android.AssertArrayString(t, "", entries.EntryMap["LOCAL_SOONG_JNI_LIBS_SYMBOLS"], []string{""})
 
-	// Check the footer lines.  Our support files should depend on base's support files.
-	convertedActual := make([]string, 5)
-	for i, e := range entries.FooterLinesForTests() {
-		// AssertStringPathsRelativeToTop doesn't replace both instances
-		convertedActual[i] = strings.Replace(e, ctx.Config.SoongOutDir(), "", 2)
-	}
-	android.AssertArrayString(t, fmt.Sprintf("%s", ctx.Config.SoongOutDir()), convertedActual, []string{
-		"include $(BUILD_SYSTEM)/soong_app_prebuilt.mk",
-		"/target/product/test_device/testcases/derived_test/arm64/base.apk: /target/product/test_device/testcases/base/arm64/base.apk",
-		"/target/product/test_device/testcases/derived_test/HelperApp.apk: /target/product/test_device/testcases/base/HelperApp.apk",
-		"/target/product/test_device/testcases/derived_test/data/testfile: /target/product/test_device/testcases/base/data/testfile",
-		"",
-	})
+	android.AssertStringMatches(t, "", entries.EntryMap["LOCAL_FULL_TEST_CONFIG"][0], "derived_test/android_common/test_config_fixer/derived_test.config")
 }
 
 // Make sure we call test-config-fixer with the right args.
@@ -114,7 +92,7 @@
 	derived := ctx.ModuleForTests("derived_test", "android_common")
 	rule_cmd := derived.Rule("fix_test_config").RuleParams.Command
 	android.AssertStringDoesContain(t, "Bad FixConfig rule inputs", rule_cmd,
-		`--test-runner-options='[{"Name":"exclude-filter","Key":"","Value":"android.test.example.devcodelab.DevCodelabTest#testHelloFail"},{"Name":"include-annotation","Key":"","Value":"android.platform.test.annotations.LargeTest"}]'`)
+		`--test-file-name=derived_test.apk --orig-test-file-name=base.apk --test-runner-options='[{"Name":"exclude-filter","Key":"","Value":"android.test.example.devcodelab.DevCodelabTest#testHelloFail"},{"Name":"include-annotation","Key":"","Value":"android.platform.test.annotations.LargeTest"}]'`)
 }
 
 // Ensure we error for a base we don't support.
@@ -217,14 +195,8 @@
 			name: "base",
 			sdk_version: "current",
                         srcs: ["a.java"],
-                        data: [":HelperApp", "data/testfile"],
 		}
 
-                android_test_helper_app {
-                        name: "HelperApp",
-                        srcs: ["helper.java"],
-                }
-
                 test_module_config {
                         name: "derived_test",
                         base: "base",
@@ -248,12 +220,8 @@
 		derived := ctx.ModuleForTests("derived_test", "android_common")
 		entries := android.AndroidMkEntriesForTest(t, ctx.TestContext, derived.Module())[0]
 		// All these should be the same in both derived tests
-		android.AssertStringPathsRelativeToTopEquals(t, "support-files", ctx.Config,
-			[]string{"out/soong/target/product/test_device/testcases/derived_test/arm64/base.apk",
-				"out/soong/target/product/test_device/testcases/derived_test/HelperApp.apk",
-				"out/soong/target/product/test_device/testcases/derived_test/data/testfile"},
-			entries.EntryMap["LOCAL_SOONG_INSTALLED_COMPATIBILITY_SUPPORT_FILES"])
-
+		assertEntryPairValues(t, entries.EntryMap["LOCAL_COMPATIBILITY_SUPPORT_FILES"], []string{"HelperApp.apk", "data/testfile"})
+		android.AssertArrayString(t, "", entries.EntryMap["LOCAL_SOONG_JNI_LIBS_SYMBOLS"], []string{""})
 		// Except this one, which points to the updated tradefed xml file.
 		android.AssertStringMatches(t, "", entries.EntryMap["LOCAL_FULL_TEST_CONFIG"][0], "derived_test/android_common/test_config_fixer/derived_test.config")
 		// And this one, the module name.
@@ -264,11 +232,8 @@
 		derived := ctx.ModuleForTests("another_derived_test", "android_common")
 		entries := android.AndroidMkEntriesForTest(t, ctx.TestContext, derived.Module())[0]
 		// All these should be the same in both derived tests
-		android.AssertStringPathsRelativeToTopEquals(t, "support-files", ctx.Config,
-			[]string{"out/soong/target/product/test_device/testcases/another_derived_test/arm64/base.apk",
-				"out/soong/target/product/test_device/testcases/another_derived_test/HelperApp.apk",
-				"out/soong/target/product/test_device/testcases/another_derived_test/data/testfile"},
-			entries.EntryMap["LOCAL_SOONG_INSTALLED_COMPATIBILITY_SUPPORT_FILES"])
+		assertEntryPairValues(t, entries.EntryMap["LOCAL_COMPATIBILITY_SUPPORT_FILES"], []string{"HelperApp.apk", "data/testfile"})
+		android.AssertArrayString(t, "", entries.EntryMap["LOCAL_SOONG_JNI_LIBS_SYMBOLS"], []string{""})
 		// Except this one, which points to the updated tradefed xml file.
 		android.AssertStringMatches(t, "", entries.EntryMap["LOCAL_FULL_TEST_CONFIG"][0], "another_derived_test/android_common/test_config_fixer/another_derived_test.config")
 		// And this one, the module name.
@@ -304,8 +269,6 @@
 	allEntries := android.AndroidMkEntriesForTest(t, ctx.TestContext, mod)
 	entries := allEntries[0]
 	android.AssertArrayString(t, "", entries.EntryMap["LOCAL_MODULE"], []string{"derived_test"})
-	android.AssertArrayString(t, "", entries.EntryMap["LOCAL_SDK_VERSION"], []string{"private_current"})
-	android.AssertStringEquals(t, "", entries.Class, "JAVA_LIBRARIES")
 
 	if !mod.Host() {
 		t.Errorf("host bit is not set for a java_test_host module.")
@@ -422,3 +385,16 @@
 		t.Errorf("test-only: Expected but not found: %v, Found but not expected: %v", left, right)
 	}
 }
+
+// Use for situations where the entries map contains pairs:  [srcPath:installedPath1, srcPath2:installedPath2]
+// and we want to compare the RHS of the pairs, i.e. installedPath1, installedPath2
+func assertEntryPairValues(t *testing.T, actual []string, expected []string) {
+	for i, e := range actual {
+		parts := strings.Split(e, ":")
+		if len(parts) != 2 {
+			t.Errorf("Expected entry to have a value delimited by :, received: %s", e)
+			return
+		}
+		android.AssertStringEquals(t, "", parts[1], expected[i])
+	}
+}