Add simple target-side VTS test for IEffectFactory.hal

Bug: 32022706
Change-Id: Id8a5a3ba8d48967beba0ef2e232624ebc185e5c6
Test: build & run test
diff --git a/audio/Android.bp b/audio/Android.bp
index 4117288..3121a36 100644
--- a/audio/Android.bp
+++ b/audio/Android.bp
@@ -3,4 +3,5 @@
     "2.0",
     "common/2.0",
     "effect/2.0",
+    "effect/2.0/vts/functional",
 ]
diff --git a/audio/effect/2.0/vts/functional/Android.bp b/audio/effect/2.0/vts/functional/Android.bp
new file mode 100644
index 0000000..fc198e5
--- /dev/null
+++ b/audio/effect/2.0/vts/functional/Android.bp
@@ -0,0 +1,36 @@
+//
+// Copyright (C) 2016 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.
+//
+
+cc_test {
+    name: "audio_effect_hidl_hal_test",
+    gtest: true,
+    srcs: ["audio_effect_hidl_hal_test.cpp"],
+    shared_libs: [
+        "libbase",
+        "liblog",
+        "libcutils",
+        "libhidl",
+        "libhwbinder",
+        "libnativehelper",
+        "libutils",
+        "android.hardware.audio.effect@2.0",
+    ],
+    static_libs: ["libgtest"],
+    cflags: [
+        "-O0",
+        "-g",
+    ],
+}
diff --git a/audio/effect/2.0/vts/functional/audio_effect_hidl_hal_test.cpp b/audio/effect/2.0/vts/functional/audio_effect_hidl_hal_test.cpp
new file mode 100644
index 0000000..ddd5eac
--- /dev/null
+++ b/audio/effect/2.0/vts/functional/audio_effect_hidl_hal_test.cpp
@@ -0,0 +1,168 @@
+/*
+ * Copyright (C) 2016 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.
+ */
+
+#define LOG_TAG "AudioEffectHidlHalTest"
+#include <android-base/logging.h>
+#include <cutils/native_handle.h>
+
+#include <android/hardware/audio/effect/2.0/IEffectsFactory.h>
+#include <android/hardware/audio/effect/2.0/types.h>
+
+#include <gtest/gtest.h>
+
+using ::android::hardware::audio::common::V2_0::Uuid;
+using ::android::hardware::audio::effect::V2_0::EffectDescriptor;
+using ::android::hardware::audio::effect::V2_0::IEffect;
+using ::android::hardware::audio::effect::V2_0::IEffectsFactory;
+using ::android::hardware::audio::effect::V2_0::Result;
+using ::android::hardware::Return;
+using ::android::hardware::Status;
+using ::android::hardware::Void;
+using ::android::hardware::hidl_vec;
+using ::android::sp;
+
+// The main test class for Audio Effect HIDL HAL.
+class AudioEffectHidlTest : public ::testing::Test {
+ public:
+  virtual void SetUp() override {
+    // currently test passthrough mode only
+    effectsFactory = IEffectsFactory::getService("audio_effects_factory", true);
+    ASSERT_NE(effectsFactory, nullptr);
+  }
+
+  virtual void TearDown() override {}
+
+  sp<IEffectsFactory> effectsFactory;
+};
+
+// A class for test environment setup (kept since this file is a template).
+class AudioEffectHidlEnvironment : public ::testing::Environment {
+ public:
+  virtual void SetUp() {}
+  virtual void TearDown() {}
+
+ private:
+};
+
+TEST_F(AudioEffectHidlTest, EnumerateEffects) {
+  Result retval = Result::NOT_INITIALIZED;
+  size_t effectCount = 0;
+  Return<void> ret = effectsFactory->getAllDescriptors(
+      [&](Result r, const hidl_vec<EffectDescriptor>& result) {
+        retval = r;
+        effectCount = result.size();
+      });
+  EXPECT_EQ(ret.getStatus().exceptionCode(), Status::EX_NONE);
+  EXPECT_EQ(retval, Result::OK);
+  EXPECT_GT(effectCount, 0u);
+}
+
+TEST_F(AudioEffectHidlTest, CreateEffect) {
+  bool gotEffect = false;
+  Uuid effectUuid;
+  Return<void> ret = effectsFactory->getAllDescriptors(
+      [&](Result r, const hidl_vec<EffectDescriptor>& result) {
+        if (r == Result::OK && result.size() > 0) {
+          gotEffect = true;
+          effectUuid = result[0].uuid;
+        }
+      });
+  ASSERT_EQ(ret.getStatus().exceptionCode(), Status::EX_NONE);
+  ASSERT_TRUE(gotEffect);
+  Result retval = Result::NOT_INITIALIZED;
+  sp<IEffect> effect;
+  ret = effectsFactory->createEffect(effectUuid, 1, 1,
+                                     [&](Result r, const sp<IEffect>& result) {
+                                       retval = r;
+                                       if (r == Result::OK) {
+                                         effect = result;
+                                       }
+                                     });
+  EXPECT_EQ(ret.getStatus().exceptionCode(), Status::EX_NONE);
+  EXPECT_EQ(retval, Result::OK);
+  EXPECT_NE(effect, nullptr);
+}
+
+// See b/32834072 -- we should have those operator== generated by hidl-gen.
+
+namespace android {
+namespace hardware {
+namespace audio {
+namespace common {
+namespace V2_0 {
+
+static bool operator==(const Uuid& lhs, const Uuid& rhs) {
+  return lhs.timeLow == rhs.timeLow && lhs.timeMid == rhs.timeMid &&
+         lhs.versionAndTimeHigh == rhs.versionAndTimeHigh &&
+         lhs.variantAndClockSeqHigh == rhs.variantAndClockSeqHigh &&
+         memcmp(lhs.node.data(), rhs.node.data(), lhs.node.size()) == 0;
+}
+
+}  // namespace V2_0
+}  // namespace common
+}  // namespace audio
+}  // namespace hardware
+}  // namespace android
+
+namespace android {
+namespace hardware {
+namespace audio {
+namespace effect {
+namespace V2_0 {
+
+static bool operator==(const EffectDescriptor& lhs,
+                       const EffectDescriptor& rhs) {
+  return lhs.type == rhs.type && lhs.uuid == rhs.uuid &&
+         lhs.flags == rhs.flags && lhs.cpuLoad == rhs.cpuLoad &&
+         lhs.memoryUsage == rhs.memoryUsage &&
+         memcmp(lhs.name.data(), rhs.name.data(), lhs.name.size()) == 0 &&
+         memcmp(lhs.implementor.data(), rhs.implementor.data(),
+                lhs.implementor.size()) == 0;
+}
+
+}  // namespace V2_0
+}  // namespace effect
+}  // namespace audio
+}  // namespace hardware
+}  // namespace android
+
+TEST_F(AudioEffectHidlTest, GetDescriptor) {
+  hidl_vec<EffectDescriptor> allDescriptors;
+  Return<void> ret = effectsFactory->getAllDescriptors(
+      [&](Result r, const hidl_vec<EffectDescriptor>& result) {
+        if (r == Result::OK) {
+          allDescriptors = result;
+        }
+      });
+  ASSERT_EQ(ret.getStatus().exceptionCode(), Status::EX_NONE);
+  ASSERT_GT(allDescriptors.size(), 0u);
+  for (size_t i = 0; i < allDescriptors.size(); ++i) {
+    ret = effectsFactory->getDescriptor(
+        allDescriptors[i].uuid, [&](Result r, const EffectDescriptor& result) {
+          EXPECT_EQ(r, Result::OK);
+          EXPECT_EQ(result, allDescriptors[i]);
+        });
+  }
+  EXPECT_EQ(ret.getStatus().exceptionCode(), Status::EX_NONE);
+}
+
+int main(int argc, char** argv) {
+  ::testing::AddGlobalTestEnvironment(new AudioEffectHidlEnvironment);
+  ::testing::InitGoogleTest(&argc, argv);
+  int status = RUN_ALL_TESTS();
+  ALOGI("Test result = %d", status);
+  return status;
+}