Add abstract optioned control class.

An optioned control is a control with a fixed list of
accepted options.

BUG: 30140438
Change-Id: Ibf82840629cb23f9082889058f7d11350beffb2a
TEST: unit tests pass
diff --git a/modules/camera/3_4/Android.mk b/modules/camera/3_4/Android.mk
index 63e7c1a..01574b9 100644
--- a/modules/camera/3_4/Android.mk
+++ b/modules/camera/3_4/Android.mk
@@ -52,6 +52,7 @@
   metadata/control_test.cpp \
   metadata/fixed_property_test.cpp \
   metadata/metadata_test.cpp \
+  metadata/optioned_control_test.cpp \
 
 # V4L2 Camera HAL.
 # ==============================================================================
@@ -60,7 +61,10 @@
 LOCAL_MODULE_RELATIVE_PATH := hw
 LOCAL_CFLAGS += $(v4l2_cflags)
 LOCAL_SHARED_LIBRARIES := $(v4l2_shared_libs)
-LOCAL_STATIC_LIBRARIES := $(v4l2_static_libs)
+LOCAL_STATIC_LIBRARIES := \
+  libgtest_prod \
+  $(v4l2_static_libs) \
+
 LOCAL_C_INCLUDES += $(v4l2_c_includes)
 LOCAL_SRC_FILES := $(v4l2_src_files)
 include $(BUILD_SHARED_LIBRARY)
diff --git a/modules/camera/3_4/metadata/optioned_control.h b/modules/camera/3_4/metadata/optioned_control.h
new file mode 100644
index 0000000..bf8ec44
--- /dev/null
+++ b/modules/camera/3_4/metadata/optioned_control.h
@@ -0,0 +1,81 @@
+/*
+ * 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.
+ */
+
+#ifndef V4L2_CAMERA_HAL_METADATA_OPTIONED_CONTROL_H_
+#define V4L2_CAMERA_HAL_METADATA_OPTIONED_CONTROL_H_
+
+#include <algorithm>
+#include <vector>
+
+#include <gtest/gtest_prod.h>
+
+#include "../common.h"
+#include "control.h"
+
+namespace v4l2_camera_hal {
+
+// An OptionedControl is a Control with a fixed list of options that can be
+// selected from.
+template <typename T>
+class OptionedControl : public Control<T> {
+ public:
+  OptionedControl(int32_t control_tag, int32_t options_tag,
+                  std::vector<T> options);
+
+  virtual int PopulateStaticFields(
+      android::CameraMetadata* metadata) const override;
+
+ protected:
+  // Simpler access to tag.
+  inline int32_t OptionsTag() const { return Control<T>::StaticTags()[0]; }
+
+  // Child classes still need to implement Get/Set from Control.
+  virtual bool IsSupported(const T& val) const override;
+
+ private:
+  const std::vector<T> options_;
+
+  FRIEND_TEST(OptionedControlTest, IsSupported);
+
+  DISALLOW_COPY_AND_ASSIGN(OptionedControl);
+};
+
+// -----------------------------------------------------------------------------
+
+template <typename T>
+OptionedControl<T>::OptionedControl(int32_t control_tag, int32_t options_tag,
+                                    std::vector<T> options)
+    : Control<T>(control_tag, {options_tag}), options_(std::move(options)) {
+  HAL_LOG_ENTER();
+}
+
+template <typename T>
+int OptionedControl<T>::PopulateStaticFields(
+    android::CameraMetadata* metadata) const {
+  HAL_LOG_ENTER();
+
+  // Populate the available options.
+  return Control<T>::UpdateMetadata(metadata, OptionsTag(), options_);
+}
+
+template <typename T>
+bool OptionedControl<T>::IsSupported(const T& value) const {
+  return (std::find(options_.begin(), options_.end(), value) != options_.end());
+}
+
+}  // namespace v4l2_camera_hal
+
+#endif  // V4L2_CAMERA_HAL_METADATA_OPTIONED_CONTROL_H_
diff --git a/modules/camera/3_4/metadata/optioned_control_test.cpp b/modules/camera/3_4/metadata/optioned_control_test.cpp
new file mode 100644
index 0000000..8fcc1cb
--- /dev/null
+++ b/modules/camera/3_4/metadata/optioned_control_test.cpp
@@ -0,0 +1,131 @@
+/*
+ * 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.
+ */
+
+#include "optioned_control.h"
+
+#include <camera/CameraMetadata.h>
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+
+using testing::AtMost;
+using testing::Return;
+using testing::ReturnRef;
+using testing::Test;
+using testing::_;
+
+namespace v4l2_camera_hal {
+
+class OptionedControlTest : public Test {
+ protected:
+  // A subclass of OptionedControl with the pure virtual methods mocked out.
+  template <typename T>
+  class MockOptionedControl : public OptionedControl<T> {
+   public:
+    MockOptionedControl(int32_t control_tag, int32_t options_tag,
+                        std::vector<T> options)
+        : OptionedControl<T>(control_tag, options_tag, options){};
+    MOCK_CONST_METHOD1_T(GetValue, int(T* value));
+    MOCK_METHOD1_T(SetValue, int(const T& value));
+  };
+
+  OptionedControlTest()
+      : options_({ANDROID_COLOR_CORRECTION_ABERRATION_MODE_OFF,
+                  ANDROID_COLOR_CORRECTION_ABERRATION_MODE_FAST,
+                  ANDROID_COLOR_CORRECTION_ABERRATION_MODE_HIGH_QUALITY}) {}
+
+  virtual void SetUp() {
+    control_.reset(
+        new MockOptionedControl<uint8_t>(control_tag_, options_tag_, options_));
+  }
+  // Check that metadata of a given tag matches expectations.
+  virtual void ExpectMetadataEq(const android::CameraMetadata& metadata,
+                                int32_t tag, const uint8_t* expected,
+                                size_t size) {
+    camera_metadata_ro_entry_t entry = metadata.find(tag);
+    ASSERT_EQ(entry.count, size);
+    for (size_t i = 0; i < size; ++i) {
+      EXPECT_EQ(entry.data.u8[i], expected[i]);
+    }
+  }
+  virtual void ExpectMetadataEq(const android::CameraMetadata& metadata,
+                                int32_t tag, const int32_t* expected,
+                                size_t size) {
+    camera_metadata_ro_entry_t entry = metadata.find(tag);
+    ASSERT_EQ(entry.count, size);
+    for (size_t i = 0; i < size; ++i) {
+      EXPECT_EQ(entry.data.i32[i], expected[i]);
+    }
+  }
+  // Single item.
+  template <typename T>
+  void ExpectMetadataEq(const android::CameraMetadata& metadata, int32_t tag,
+                        T expected) {
+    ExpectMetadataEq(metadata, tag, &expected, 1);
+  }
+  // Vector of items.
+  template <typename T>
+  void ExpectMetadataEq(const android::CameraMetadata& metadata, int32_t tag,
+                        const std::vector<T>& expected) {
+    ExpectMetadataEq(metadata, tag, expected.data(), expected.size());
+  }
+
+  std::unique_ptr<OptionedControl<uint8_t>> control_;
+
+  // Need tags that match the data type (uint8_t) being passed.
+  static constexpr int32_t options_tag_ =
+      ANDROID_COLOR_CORRECTION_AVAILABLE_ABERRATION_MODES;
+  static constexpr int32_t control_tag_ =
+      ANDROID_COLOR_CORRECTION_ABERRATION_MODE;
+
+  const std::vector<uint8_t> options_;
+};
+
+TEST_F(OptionedControlTest, Tags) {
+  // The macro doesn't like the static variables
+  // being passed directly to assertions.
+  int32_t expected_tag = options_tag_;
+  ASSERT_EQ(control_->StaticTags().size(), 1);
+  EXPECT_EQ(control_->StaticTags()[0], expected_tag);
+
+  // Controls use the same tag for getting and setting.
+  expected_tag = control_tag_;
+  ASSERT_EQ(control_->ControlTags().size(), 1);
+  EXPECT_EQ(control_->ControlTags()[0], expected_tag);
+  ASSERT_EQ(control_->DynamicTags().size(), 1);
+  EXPECT_EQ(control_->DynamicTags()[0], expected_tag);
+}
+
+TEST_F(OptionedControlTest, PopulateStatic) {
+  // Populate static fields.
+  android::CameraMetadata metadata;
+  ASSERT_EQ(control_->PopulateStaticFields(&metadata), 0);
+
+  // Check the results.
+  // Should only have added 1 entry.
+  EXPECT_EQ(metadata.entryCount(), 1);
+  // Should have added the right entry.
+  ExpectMetadataEq(metadata, options_tag_, options_);
+}
+
+TEST_F(OptionedControlTest, IsSupported) {
+  for (auto option : options_) {
+    EXPECT_TRUE(control_->IsSupported(option));
+  }
+  // And at least one unsupported.
+  EXPECT_FALSE(control_->IsSupported(99));
+}
+
+}  // namespace v4l2_camera_hal