Adding HIDL interface type to safeunion test HAL

This change adds a new HIDL safe union, InterfaceTypeSafeUnion
(containing an interface+array) to the safeunion test HAL.

Bug: 79878527
Bug: 110269925
Test: Existing hidl_test suite passes successfully. Added more test-
cases to hidl_test_client.cpp to exercise InterfaceTypeSafeUnion
(included in a separate CL).

Change-Id: I04ffeeeabccc1dc2651aac9371569c36d37f4f87
diff --git a/tests/safeunion/1.0/Android.bp b/tests/safeunion/1.0/Android.bp
index 5082f47..4c6631e 100644
--- a/tests/safeunion/1.0/Android.bp
+++ b/tests/safeunion/1.0/Android.bp
@@ -4,18 +4,12 @@
     name: "android.hardware.tests.safeunion@1.0",
     root: "android.hardware",
     srcs: [
-        "types.hal",
+        "IOtherInterface.hal",
         "ISafeUnion.hal",
     ],
     interfaces: [
         "android.hidl.base@1.0",
     ],
-    types: [
-        "EmptySafeUnion",
-        "SmallSafeUnion",
-        "LargeSafeUnion",
-        "MiscTypesSafeUnion",
-    ],
     gen_java: false,
 }
 
diff --git a/tests/safeunion/1.0/IOtherInterface.hal b/tests/safeunion/1.0/IOtherInterface.hal
new file mode 100644
index 0000000..cdaf847
--- /dev/null
+++ b/tests/safeunion/1.0/IOtherInterface.hal
@@ -0,0 +1,21 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ *
+ * 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 android.hardware.tests.safeunion@1.0;
+
+interface IOtherInterface {
+    concatTwoStrings(string a, string b) generates (string result);
+};
diff --git a/tests/safeunion/1.0/ISafeUnion.hal b/tests/safeunion/1.0/ISafeUnion.hal
index 91d8b8c..153d6f4 100644
--- a/tests/safeunion/1.0/ISafeUnion.hal
+++ b/tests/safeunion/1.0/ISafeUnion.hal
@@ -16,7 +16,66 @@
 
 package android.hardware.tests.safeunion@1.0;
 
+import IOtherInterface;
+
 interface ISafeUnion {
+
+    enum BitField : uint8_t {
+        V0 = 1 << 0,
+        V1 = 1 << 1,
+        V2 = 1 << 2,
+        V3 = 1 << 3,
+    };
+
+    struct J {
+        vec<uint32_t> j1;
+        uint8_t[65] j2;
+        string j3;
+    };
+
+    safe_union EmptySafeUnion {
+    };
+
+    safe_union SmallSafeUnion {
+        uint8_t a;
+    };
+
+    safe_union LargeSafeUnion {
+        int8_t a;
+        uint16_t b;
+        int32_t c;
+        uint64_t d;
+
+        int8_t[13] e;
+        int64_t[5] f;
+
+        string g;
+        vec<bool> h;
+        vec<uint64_t> i;
+
+        J j;
+        struct K {
+            uint8_t k1;
+            uint64_t k2;
+        } k;
+
+        SmallSafeUnion l;
+    };
+
+    // TODO(b/110269925): Test more HIDL types. Missing:
+    // death_recipient, fmq_{sync,unsync}, pointer, ref.
+    safe_union MiscTypesSafeUnion {
+        memory a;
+        handle b;
+        bitfield<BitField> c;
+    };
+
+    safe_union InterfaceTypeSafeUnion {
+        uint32_t a;
+        int8_t[7] b;
+        IOtherInterface c;
+    };
+
     newLargeSafeUnion() generates (LargeSafeUnion myUnion);
     setA(LargeSafeUnion myUnion, int8_t a) generates (LargeSafeUnion myUnion);
     setB(LargeSafeUnion myUnion, uint16_t b) generates (LargeSafeUnion myUnion);
@@ -28,11 +87,16 @@
     setH(LargeSafeUnion myUnion, vec<bool> h) generates (LargeSafeUnion myUnion);
     setI(LargeSafeUnion myUnion, vec<uint64_t> i) generates (LargeSafeUnion myUnion);
     setJ(LargeSafeUnion myUnion, J j) generates (LargeSafeUnion myUnion);
-    setK(LargeSafeUnion myUnion, K k) generates (LargeSafeUnion myUnion);
+    setK(LargeSafeUnion myUnion, LargeSafeUnion.K k) generates (LargeSafeUnion myUnion);
     setL(LargeSafeUnion myUnion, SmallSafeUnion l) generates (LargeSafeUnion myUnion);
 
     newMiscTypesSafeUnion() generates (MiscTypesSafeUnion myUnion);
     setMiscA(MiscTypesSafeUnion myUnion, memory a) generates (MiscTypesSafeUnion myUnion);
     setMiscB(MiscTypesSafeUnion myUnion, handle b) generates (MiscTypesSafeUnion myUnion);
     setMiscC(MiscTypesSafeUnion myUnion, bitfield<BitField> c) generates (MiscTypesSafeUnion myUnion);
+
+    newInterfaceTypeSafeUnion() generates (InterfaceTypeSafeUnion myUnion);
+    setInterfaceA(InterfaceTypeSafeUnion myUnion, uint32_t a) generates (InterfaceTypeSafeUnion myUnion);
+    setInterfaceB(InterfaceTypeSafeUnion myUnion, int8_t[7] b) generates (InterfaceTypeSafeUnion myUnion);
+    setInterfaceC(InterfaceTypeSafeUnion myUnion, IOtherInterface c) generates (InterfaceTypeSafeUnion myUnion);
 };
diff --git a/tests/safeunion/1.0/default/SafeUnion.cpp b/tests/safeunion/1.0/default/SafeUnion.cpp
index d968987..55d20f8 100644
--- a/tests/safeunion/1.0/default/SafeUnion.cpp
+++ b/tests/safeunion/1.0/default/SafeUnion.cpp
@@ -73,7 +73,7 @@
     return Void();
 }
 
-Return<void> SafeUnion::setE(const ::android::hardware::tests::safeunion::V1_0::LargeSafeUnion& myUnion, const hidl_array<int8_t, 13>& e, setE_cb _hidl_cb) {
+Return<void> SafeUnion::setE(const LargeSafeUnion& myUnion, const hidl_array<int8_t, 13>& e, setE_cb _hidl_cb) {
     LOG(INFO) << "SERVER(SafeUnion) setE(myUnion, " << toString(e) << ")";
 
     LargeSafeUnion myNewUnion = myUnion;
@@ -83,7 +83,7 @@
     return Void();
 }
 
-Return<void> SafeUnion::setF(const ::android::hardware::tests::safeunion::V1_0::LargeSafeUnion& myUnion, const hidl_array<int64_t, 5>& f, setF_cb _hidl_cb) {
+Return<void> SafeUnion::setF(const LargeSafeUnion& myUnion, const hidl_array<int64_t, 5>& f, setF_cb _hidl_cb) {
     LOG(INFO) << "SERVER(SafeUnion) setF(myUnion, " << toString(f) << ")";
 
     LargeSafeUnion myNewUnion = myUnion;
@@ -93,7 +93,7 @@
     return Void();
 }
 
-Return<void> SafeUnion::setG(const ::android::hardware::tests::safeunion::V1_0::LargeSafeUnion& myUnion, const hidl_string& g, setG_cb _hidl_cb) {
+Return<void> SafeUnion::setG(const LargeSafeUnion& myUnion, const hidl_string& g, setG_cb _hidl_cb) {
     LOG(INFO) << "SERVER(SafeUnion) setG(myUnion, " << toString(g) << ")";
 
     LargeSafeUnion myNewUnion = myUnion;
@@ -103,7 +103,7 @@
     return Void();
 }
 
-Return<void> SafeUnion::setH(const ::android::hardware::tests::safeunion::V1_0::LargeSafeUnion& myUnion, const hidl_vec<bool>& h, setH_cb _hidl_cb) {
+Return<void> SafeUnion::setH(const LargeSafeUnion& myUnion, const hidl_vec<bool>& h, setH_cb _hidl_cb) {
     LOG(INFO) << "SERVER(SafeUnion) setH(myUnion, " << toString(h) << ")";
 
     LargeSafeUnion myNewUnion = myUnion;
@@ -113,7 +113,7 @@
     return Void();
 }
 
-Return<void> SafeUnion::setI(const ::android::hardware::tests::safeunion::V1_0::LargeSafeUnion& myUnion, const hidl_vec<uint64_t>& i, setI_cb _hidl_cb) {
+Return<void> SafeUnion::setI(const LargeSafeUnion& myUnion, const hidl_vec<uint64_t>& i, setI_cb _hidl_cb) {
     LOG(INFO) << "SERVER(SafeUnion) setI(myUnion, " << toString(i) << ")";
 
     LargeSafeUnion myNewUnion = myUnion;
@@ -123,7 +123,7 @@
     return Void();
 }
 
-Return<void> SafeUnion::setJ(const ::android::hardware::tests::safeunion::V1_0::LargeSafeUnion& myUnion, const J& j, setJ_cb _hidl_cb) {
+Return<void> SafeUnion::setJ(const LargeSafeUnion& myUnion, const J& j, setJ_cb _hidl_cb) {
     LOG(INFO) << "SERVER(SafeUnion) setJ(myUnion, " << toString(j) << ")";
 
     LargeSafeUnion myNewUnion = myUnion;
@@ -133,7 +133,7 @@
     return Void();
 }
 
-Return<void> SafeUnion::setK(const ::android::hardware::tests::safeunion::V1_0::LargeSafeUnion& myUnion, const LargeSafeUnion::K& k, setK_cb _hidl_cb) {
+Return<void> SafeUnion::setK(const LargeSafeUnion& myUnion, const LargeSafeUnion::K& k, setK_cb _hidl_cb) {
     LOG(INFO) << "SERVER(SafeUnion) setK(myUnion, " << toString(k) << ")";
 
     LargeSafeUnion myNewUnion = myUnion;
@@ -143,7 +143,7 @@
     return Void();
 }
 
-Return<void> SafeUnion::setL(const ::android::hardware::tests::safeunion::V1_0::LargeSafeUnion& myUnion, const ::android::hardware::tests::safeunion::V1_0::SmallSafeUnion& l, setL_cb _hidl_cb) {
+Return<void> SafeUnion::setL(const LargeSafeUnion& myUnion, const SmallSafeUnion& l, setL_cb _hidl_cb) {
     LOG(INFO) << "SERVER(SafeUnion) setL(myUnion, " << toString(l) << ")";
 
     LargeSafeUnion myNewUnion = myUnion;
@@ -161,7 +161,7 @@
     return Void();
 }
 
-Return<void> SafeUnion::setMiscA(const ::android::hardware::tests::safeunion::V1_0::MiscTypesSafeUnion& myUnion, const hidl_memory& a, setMiscA_cb _hidl_cb) {
+Return<void> SafeUnion::setMiscA(const MiscTypesSafeUnion& myUnion, const hidl_memory& a, setMiscA_cb _hidl_cb) {
     LOG(INFO) << "SERVER(SafeUnion) setMiscA(myUnion, " << toString(a) << ")";
 
     MiscTypesSafeUnion myNewUnion = myUnion;
@@ -171,7 +171,7 @@
     return Void();
 }
 
-Return<void> SafeUnion::setMiscB(const ::android::hardware::tests::safeunion::V1_0::MiscTypesSafeUnion& myUnion, const hidl_handle& b, setMiscB_cb _hidl_cb) {
+Return<void> SafeUnion::setMiscB(const MiscTypesSafeUnion& myUnion, const hidl_handle& b, setMiscB_cb _hidl_cb) {
     LOG(INFO) << "SERVER(SafeUnion) setMiscB(myUnion, " << toString(b) << ")";
 
     MiscTypesSafeUnion myNewUnion = myUnion;
@@ -181,7 +181,7 @@
     return Void();
 }
 
-Return<void> SafeUnion::setMiscC(const ::android::hardware::tests::safeunion::V1_0::MiscTypesSafeUnion& myUnion, hidl_bitfield<BitField> c, setMiscC_cb _hidl_cb) {
+Return<void> SafeUnion::setMiscC(const MiscTypesSafeUnion& myUnion, hidl_bitfield<BitField> c, setMiscC_cb _hidl_cb) {
     LOG(INFO) << "SERVER(SafeUnion) setMiscC(myUnion, " << c << ")";
 
     MiscTypesSafeUnion myNewUnion = myUnion;
@@ -191,6 +191,43 @@
     return Void();
 }
 
+Return<void> SafeUnion::newInterfaceTypeSafeUnion(newInterfaceTypeSafeUnion_cb _hidl_cb) {
+    LOG(INFO) << "SERVER(SafeUnion) newInterfaceTypeSafeUnion()";
+
+    InterfaceTypeSafeUnion ret;
+    _hidl_cb(ret);
+    return Void();
+}
+
+Return<void> SafeUnion::setInterfaceA(const InterfaceTypeSafeUnion& myUnion, uint32_t a, setInterfaceA_cb _hidl_cb) {
+    LOG(INFO) << "SERVER(SafeUnion) setInterfaceA(myUnion, " << a << ")";
+
+    InterfaceTypeSafeUnion myNewUnion = myUnion;
+    myNewUnion.a(a);
+
+    _hidl_cb(myNewUnion);
+    return Void();
+}
+
+Return<void> SafeUnion::setInterfaceB(const InterfaceTypeSafeUnion& myUnion, const hidl_array<int8_t, 7>& b, setInterfaceB_cb _hidl_cb) {
+    LOG(INFO) << "SERVER(SafeUnion) setInterfaceB(myUnion, " << toString(b) << ")";
+
+    InterfaceTypeSafeUnion myNewUnion = myUnion;
+    myNewUnion.b(b);
+
+    _hidl_cb(myNewUnion);
+    return Void();
+}
+
+Return<void> SafeUnion::setInterfaceC(const InterfaceTypeSafeUnion& myUnion, const sp<::android::hardware::tests::safeunion::V1_0::IOtherInterface>& c, setInterfaceC_cb _hidl_cb) {
+    LOG(INFO) << "SERVER(SafeUnion) setInterfaceC(myUnion, " << toString(c) << ")";
+
+    InterfaceTypeSafeUnion myNewUnion = myUnion;
+    myNewUnion.c(c);
+
+    _hidl_cb(myNewUnion);
+    return Void();
+}
 
 ISafeUnion* HIDL_FETCH_ISafeUnion(const char* /* name */) {
     return new SafeUnion();
diff --git a/tests/safeunion/1.0/default/SafeUnion.h b/tests/safeunion/1.0/default/SafeUnion.h
index 6b9997a..e3550b8 100644
--- a/tests/safeunion/1.0/default/SafeUnion.h
+++ b/tests/safeunion/1.0/default/SafeUnion.h
@@ -30,9 +30,7 @@
 
 using ::android::hardware::Return;
 using ::android::hardware::Void;
-using ::android::hardware::tests::safeunion::V1_0::SmallSafeUnion;
-using ::android::hardware::tests::safeunion::V1_0::LargeSafeUnion;
-using ::android::hardware::tests::safeunion::V1_0::MiscTypesSafeUnion;
+using ::android::hardware::tests::safeunion::V1_0::ISafeUnion;
 
 struct SafeUnion : public ISafeUnion {
     // Methods from ::android::hardware::tests::safeunion::V1_0::ISafeUnion follow.
@@ -51,9 +49,14 @@
     Return<void> setL(const LargeSafeUnion& myUnion, const SmallSafeUnion& l, setL_cb _hidl_cb) override;
 
     Return<void> newMiscTypesSafeUnion(newMiscTypesSafeUnion_cb _hidl_cb) override;
-    Return<void> setMiscA(const ::android::hardware::tests::safeunion::V1_0::MiscTypesSafeUnion& myUnion, const hidl_memory& a, setMiscA_cb _hidl_cb) override;
-    Return<void> setMiscB(const ::android::hardware::tests::safeunion::V1_0::MiscTypesSafeUnion& myUnion, const hidl_handle& b, setMiscB_cb _hidl_cb) override;
-    Return<void> setMiscC(const ::android::hardware::tests::safeunion::V1_0::MiscTypesSafeUnion& myUnion, hidl_bitfield<BitField> c, setMiscC_cb _hidl_cb) override;
+    Return<void> setMiscA(const MiscTypesSafeUnion& myUnion, const hidl_memory& a, setMiscA_cb _hidl_cb) override;
+    Return<void> setMiscB(const MiscTypesSafeUnion& myUnion, const hidl_handle& b, setMiscB_cb _hidl_cb) override;
+    Return<void> setMiscC(const MiscTypesSafeUnion& myUnion, hidl_bitfield<BitField> c, setMiscC_cb _hidl_cb) override;
+
+    Return<void> newInterfaceTypeSafeUnion(newInterfaceTypeSafeUnion_cb _hidl_cb) override;
+    Return<void> setInterfaceA(const InterfaceTypeSafeUnion& myUnion, uint32_t a, setInterfaceA_cb _hidl_cb) override;
+    Return<void> setInterfaceB(const InterfaceTypeSafeUnion& myUnion, const hidl_array<int8_t, 7>& b, setInterfaceB_cb _hidl_cb) override;
+    Return<void> setInterfaceC(const InterfaceTypeSafeUnion& myUnion, const sp<::android::hardware::tests::safeunion::V1_0::IOtherInterface>& c, setInterfaceC_cb _hidl_cb) override;
 };
 
 extern "C" ISafeUnion* HIDL_FETCH_ISafeUnion(const char* name);
diff --git a/tests/safeunion/1.0/types.hal b/tests/safeunion/1.0/types.hal
deleted file mode 100644
index a70079d..0000000
--- a/tests/safeunion/1.0/types.hal
+++ /dev/null
@@ -1,67 +0,0 @@
-/*
- * Copyright (C) 2018 The Android Open Source Project
- *
- * 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 android.hardware.tests.safeunion@1.0;
-
-enum BitField : uint8_t {
-    V0 = 1 << 0,
-    V1 = 1 << 1,
-    V2 = 1 << 2,
-    V3 = 1 << 3,
-};
-
-struct J {
-    vec<uint32_t> j1;
-    uint8_t[65] j2;
-    string j3;
-};
-
-safe_union EmptySafeUnion {
-};
-
-safe_union SmallSafeUnion {
-    uint8_t a;
-};
-
-safe_union LargeSafeUnion {
-    int8_t a;
-    uint16_t b;
-    int32_t c;
-    uint64_t d;
-
-    int8_t[13] e;
-    int64_t[5] f;
-
-    string g;
-    vec<bool> h;
-    vec<uint64_t> i;
-
-    J j;
-    struct K {
-        uint8_t k1;
-        uint64_t k2;
-    } k;
-
-    SmallSafeUnion l;
-};
-
-// TODO(b/110269925): Test more HIDL types. Missing:
-// death_recipient, fmq_{sync,unsync}, pointer, ref.
-safe_union MiscTypesSafeUnion {
-    memory a;
-    handle b;
-    bitfield<BitField> c;
-};