Add conversion tests for hidl_interface.

Bug: 232520859
Test: Manually run the unit tests
Change-Id: I7058f6d03322fb558b7f0f7b34067f2576ad3641
diff --git a/build/Android.bp b/build/Android.bp
index 963a690..bebdd9d 100644
--- a/build/Android.bp
+++ b/build/Android.bp
@@ -28,6 +28,7 @@
         "blueprint",
         "soong",
         "soong-android",
+        "soong-bp2build",
         "soong-cc",
         "soong-genrule",
         "soong-java",
@@ -39,6 +40,9 @@
         "properties.go",
         "utils.go",
     ],
+    testSrcs: [
+        "hidl_interface_conversion_test.go",
+    ],
     pluginFor: ["soong_build"],
 }
 
diff --git a/build/go.mod b/build/go.mod
new file mode 100644
index 0000000..2d75c6e
--- /dev/null
+++ b/build/go.mod
@@ -0,0 +1,27 @@
+module android/soong/hidl
+
+require (
+	android/soong v0.0.0
+	github.com/google/blueprint v0.0.0
+)
+
+require (
+	golang.org/x/xerrors v0.0.0-20220609144429-65e65417b02f // indirect
+	google.golang.org/protobuf v0.0.0 // indirect
+)
+
+replace google.golang.org/protobuf v0.0.0 => ../../../../external/golang-protobuf
+
+replace github.com/google/blueprint v0.0.0 => ../../../../build/blueprint
+
+replace android/soong v0.0.0 => ../../../../build/soong
+
+// Indirect deps from golang-protobuf
+exclude github.com/golang/protobuf v1.5.0
+
+replace github.com/google/go-cmp v0.5.5 => ../../../../external/go-cmp
+
+// Indirect dep from go-cmp
+exclude golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543
+
+go 1.18
diff --git a/build/hidl_interface.go b/build/hidl_interface.go
index 8f47d8b..e90b275 100644
--- a/build/hidl_interface.go
+++ b/build/hidl_interface.go
@@ -64,11 +64,11 @@
 	}, "output", "options", "fqName")
 
 	zipLintRule = pctx.StaticRule("zipLintRule", blueprint.RuleParams{
-		Rspfile:     "$out.rsp",
+		Rspfile:        "$out.rsp",
 		RspfileContent: "$files",
-		Command:     "rm -f ${output} && ${soong_zip} -o ${output} -C ${intermediatesDir} -l ${out}.rsp",
-		CommandDeps: []string{"${soong_zip}"},
-		Description: "Zipping hidl-lints into ${output}",
+		Command:        "rm -f ${output} && ${soong_zip} -o ${output} -C ${intermediatesDir} -l ${out}.rsp",
+		CommandDeps:    []string{"${soong_zip}"},
+		Description:    "Zipping hidl-lints into ${output}",
 	}, "output", "files")
 
 	inheritanceHierarchyRule = pctx.StaticRule("inheritanceHierarchyRule", blueprint.RuleParams{
@@ -779,6 +779,9 @@
 			} else {
 				path = ctx.OtherModuleDir(pkg_root)
 			}
+			// The root and root_interface come from the hidl_package_root module that
+			// this module depends on, we don't convert hidl_package_root module
+			// separately since all the other properties of that module are deprecated.
 			root = pkg_root.Name()
 			if path == ctx.ModuleDir() {
 				root_interface_file = *bazel.MakeLabelAttribute(":" + "current.txt")
diff --git a/build/hidl_interface_conversion_test.go b/build/hidl_interface_conversion_test.go
new file mode 100644
index 0000000..6cc5df8
--- /dev/null
+++ b/build/hidl_interface_conversion_test.go
@@ -0,0 +1,117 @@
+// Copyright 2022 Google Inc. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package hidl
+
+import (
+	"testing"
+
+	"android/soong/android"
+	"android/soong/bp2build"
+	"android/soong/cc"
+)
+
+func runHidlInterfaceTestCase(t *testing.T, tc bp2build.Bp2buildTestCase) {
+	t.Helper()
+	bp2build.RunBp2BuildTestCase(
+		t,
+		func(ctx android.RegistrationContext) {
+			ctx.RegisterModuleType("cc_defaults", func() android.Module { return cc.DefaultsFactory() })
+			ctx.RegisterModuleType("hidl_interface", HidlInterfaceFactory)
+			ctx.RegisterModuleType("hidl_package_root", HidlPackageRootFactory)
+		},
+		tc,
+	)
+}
+
+func TestHidlInterface(t *testing.T) {
+	runHidlInterfaceTestCase(t, bp2build.Bp2buildTestCase{
+		Description: `hidl_interface with common usage of properties`,
+		Blueprint: `
+hidl_package_root {
+		name: "android.hardware",
+		use_current: true,
+}
+cc_defaults {
+		name: "hidl-module-defaults",
+}
+hidl_interface {
+		name: "android.hardware.nfc@1.0",
+		srcs: ["types.hal", "IBase.hal"],
+		root: "android.hardware",
+		gen_java: false,
+}
+hidl_interface {
+		name: "android.hardware.nfc@1.1",
+		srcs: ["types.hal", "INfc.hal"],
+		interfaces: ["android.hardware.nfc@1.0"],
+		root: "android.hardware",
+		gen_java: false,
+}`,
+		ExpectedBazelTargets: []string{
+			bp2build.MakeBazelTargetNoRestrictions("hidl_interface", "android.hardware.nfc@1.0", bp2build.AttrNameToString{
+				"min_sdk_version":     `"29"`,
+				"root":                `"android.hardware"`,
+				"root_interface_file": `":current.txt"`,
+				"srcs": `[
+        "types.hal",
+        "IBase.hal",
+    ]`,
+			}),
+			bp2build.MakeBazelTargetNoRestrictions("hidl_interface", "android.hardware.nfc@1.1", bp2build.AttrNameToString{
+				"deps":                `[":android.hardware.nfc@1.0"]`,
+				"min_sdk_version":     `"29"`,
+				"root":                `"android.hardware"`,
+				"root_interface_file": `":current.txt"`,
+				"srcs": `[
+        "types.hal",
+        "INfc.hal",
+    ]`,
+			}),
+		},
+	})
+}
+
+func TestHidlInterfacePackageRootInAnotherBp(t *testing.T) {
+	runHidlInterfaceTestCase(t, bp2build.Bp2buildTestCase{
+		Description: `hidl_interface with common usage of properties`,
+		Filesystem: map[string]string{
+			"foo/bar/Android.bp": `
+hidl_package_root {
+		name: "android.hardware",
+		use_current: true,
+}`},
+		Blueprint: `
+cc_defaults {
+		name: "hidl-module-defaults",
+}
+hidl_interface {
+		name: "android.hardware.nfc@1.0",
+		srcs: ["types.hal", "IBase.hal"],
+		root: "android.hardware",
+		gen_java: false,
+}`,
+		ExpectedBazelTargets: []string{
+			bp2build.MakeBazelTargetNoRestrictions("hidl_interface", "android.hardware.nfc@1.0", bp2build.AttrNameToString{
+				"min_sdk_version":     `"29"`,
+				"root":                `"android.hardware"`,
+				"root_interface_file": `"//foo/bar:current.txt"`,
+				"srcs": `[
+        "types.hal",
+        "IBase.hal",
+    ]`,
+			}),
+		},
+	})
+}