diff --git a/java/app.go b/java/app.go index 7828a8045..e9941f2d2 100755 --- a/java/app.go +++ b/java/app.go @@ -644,22 +644,40 @@ func (a *AndroidTest) GenerateAndroidBuildActions(ctx android.ModuleContext) { } a.generateAndroidBuildActions(ctx) - a.testConfig = tradefed.AutoGenInstrumentationTestConfig(ctx, a.testProperties.Test_config, + testConfig := tradefed.AutoGenInstrumentationTestConfig(ctx, a.testProperties.Test_config, a.testProperties.Test_config_template, a.manifestPath, a.testProperties.Test_suites, a.testProperties.Auto_gen_config) - if a.overridableAppProperties.Package_name != nil { - fixedConfig := android.PathForModuleOut(ctx, "test_config_fixer", "AndroidTest.xml") - rule := android.NewRuleBuilder() - rule.Command().BuiltTool(ctx, "test_config_fixer"). - FlagWithInput("--manifest ", a.manifestPath). - FlagWithArg("--package-name ", *a.overridableAppProperties.Package_name). - Input(a.testConfig). - Output(fixedConfig) - rule.Build(pctx, ctx, "fix_test_config", "fix test config") - a.testConfig = fixedConfig - } + a.testConfig = a.FixTestConfig(ctx, testConfig) a.data = android.PathsForModuleSrc(ctx, a.testProperties.Data) } +func (a *AndroidTest) FixTestConfig(ctx android.ModuleContext, testConfig android.Path) android.Path { + if testConfig == nil { + return nil + } + + fixedConfig := android.PathForModuleOut(ctx, "test_config_fixer", "AndroidTest.xml") + rule := android.NewRuleBuilder() + command := rule.Command().BuiltTool(ctx, "test_config_fixer").Input(testConfig).Output(fixedConfig) + fixNeeded := false + + if ctx.ModuleName() != a.installApkName { + fixNeeded = true + command.FlagWithArg("--test-file-name ", a.installApkName+".apk") + } + + if a.overridableAppProperties.Package_name != nil { + fixNeeded = true + command.FlagWithInput("--manifest ", a.manifestPath). + FlagWithArg("--package-name ", *a.overridableAppProperties.Package_name) + } + + if fixNeeded { + rule.Build(pctx, ctx, "fix_test_config", "fix test config") + return fixedConfig + } + return testConfig +} + func (a *AndroidTest) DepsMutator(ctx android.BottomUpMutatorContext) { a.AndroidApp.DepsMutator(ctx) } diff --git a/java/app_test.go b/java/app_test.go index 862af774a..6f89da410 100644 --- a/java/app_test.go +++ b/java/app_test.go @@ -1305,6 +1305,87 @@ func TestOverrideAndroidTest(t *testing.T) { } } +func TestAndroidTest_FixTestConfig(t *testing.T) { + ctx, _ := testJava(t, ` + android_app { + name: "foo", + srcs: ["a.java"], + package_name: "com.android.foo", + sdk_version: "current", + } + + android_test { + name: "foo_test", + srcs: ["b.java"], + instrumentation_for: "foo", + } + + android_test { + name: "bar_test", + srcs: ["b.java"], + package_name: "com.android.bar.test", + instrumentation_for: "foo", + } + + override_android_test { + name: "baz_test", + base: "foo_test", + package_name: "com.android.baz.test", + } + `) + + testCases := []struct { + moduleName string + variantName string + expectedFlags []string + }{ + { + moduleName: "foo_test", + variantName: "android_common", + }, + { + moduleName: "bar_test", + variantName: "android_common", + expectedFlags: []string{ + "--manifest " + buildDir + "/.intermediates/bar_test/android_common/manifest_fixer/AndroidManifest.xml", + "--package-name com.android.bar.test", + }, + }, + { + moduleName: "foo_test", + variantName: "android_common_baz_test", + expectedFlags: []string{ + "--manifest " + buildDir + + "/.intermediates/foo_test/android_common_baz_test/manifest_fixer/AndroidManifest.xml", + "--package-name com.android.baz.test", + "--test-file-name baz_test.apk", + }, + }, + } + + for _, test := range testCases { + variant := ctx.ModuleForTests(test.moduleName, test.variantName) + params := variant.MaybeOutput("test_config_fixer/AndroidTest.xml") + + if len(test.expectedFlags) > 0 { + if params.Rule == nil { + t.Errorf("test_config_fixer was expected to run, but didn't") + } else { + for _, flag := range test.expectedFlags { + if !strings.Contains(params.RuleParams.Command, flag) { + t.Errorf("Flag %q was not found in command: %q", flag, params.RuleParams.Command) + } + } + } + } else { + if params.Rule != nil { + t.Errorf("test_config_fixer was not expected to run, but did: %q", params.RuleParams.Command) + } + } + + } +} + func TestAndroidAppImport(t *testing.T) { ctx, _ := testJava(t, ` android_app_import { diff --git a/scripts/test_config_fixer.py b/scripts/test_config_fixer.py index 7bb4b52a3..32d5b175e 100644 --- a/scripts/test_config_fixer.py +++ b/scripts/test_config_fixer.py @@ -37,6 +37,8 @@ def parse_args(): help=('AndroidManifest.xml that contains the original package name')) parser.add_argument('--package-name', default='', dest='package_name', help=('overwrite package fields in the test config')) + parser.add_argument('--test-file-name', default='', dest='test_file_name', + help=('overwrite test file name in the test config')) parser.add_argument('input', help='input test config file') parser.add_argument('output', help='output test config file') return parser.parse_args() @@ -46,7 +48,6 @@ def overwrite_package_name(test_config_doc, manifest_doc, package_name): manifest = parse_manifest(manifest_doc) original_package = manifest.getAttribute('package') - print('package: ' + original_package) test_config = parse_test_config(test_config_doc) tests = get_children_with_tag(test_config, 'test') @@ -57,6 +58,18 @@ def overwrite_package_name(test_config_doc, manifest_doc, package_name): if option.getAttribute('name') == "package" and option.getAttribute('value') == original_package: option.setAttribute('value', package_name) +def overwrite_test_file_name(test_config_doc, test_file_name): + + test_config = parse_test_config(test_config_doc) + tests = get_children_with_tag(test_config, 'target_preparer') + + for test in tests: + if test.getAttribute('class') == "com.android.tradefed.targetprep.TestAppInstallSetup": + options = get_children_with_tag(test, 'option') + for option in options: + if option.getAttribute('name') == "test-file-name": + option.setAttribute('value', test_file_name) + def main(): """Program entry point.""" try: @@ -70,6 +83,9 @@ def main(): manifest_doc = minidom.parse(args.manifest) overwrite_package_name(doc, manifest_doc, args.package_name) + if args.test_file_name: + overwrite_test_file_name(doc, args.test_file_name) + with open(args.output, 'wb') as f: write_xml(f, doc) diff --git a/scripts/test_config_fixer_test.py b/scripts/test_config_fixer_test.py index b90582ed9..1272c6b33 100644 --- a/scripts/test_config_fixer_test.py +++ b/scripts/test_config_fixer_test.py @@ -67,5 +67,32 @@ class OverwritePackageNameTest(unittest.TestCase): self.assertEqual(expected, output.getvalue()) +class OverwriteTestFileNameTest(unittest.TestCase): + """ Unit tests for overwrite_test_file_name function """ + + test_config = ( + '\n' + '\n' + ' \n' + ' \n' + ' \n' + ' \n' + '\n') + + def test_all(self): + doc = minidom.parseString(self.test_config % ("foo.apk")) + + test_config_fixer.overwrite_test_file_name(doc, "bar.apk") + output = StringIO.StringIO() + test_config_fixer.write_xml(output, doc) + + # Only the matching package name in a test node should be updated. + expected = self.test_config % ("bar.apk") + self.assertEqual(expected, output.getvalue()) + + if __name__ == '__main__': unittest.main(verbosity=2)