Add metadata helper class.

Breaks metadata down into distinct pieces (to be added later).

BUG: 29330282
TEST: unit tests pass

Change-Id: I7156e420e7cdb14b5c3155006ecefd8ff2a49500
diff --git a/modules/camera/3_4/Android.mk b/modules/camera/3_4/Android.mk
index e785015..1ced4a5 100644
--- a/modules/camera/3_4/Android.mk
+++ b/modules/camera/3_4/Android.mk
@@ -20,43 +20,64 @@
 # requesting to use it.
 ifeq ($(USE_CAMERA_V4L2_HAL), true)
 
-include $(CLEAR_VARS)
+v4l2_shared_libs := \
+  libbase \
+  libcamera_client \
+  libcamera_metadata \
+  libcutils \
+  libhardware \
+  liblog \
+  libnativehelper \
+  libsync \
+  libutils \
 
-LOCAL_MODULE := camera.v4l2
-LOCAL_MODULE_RELATIVE_PATH := hw
+v4l2_static_libs :=
 
-LOCAL_CFLAGS += -fno-short-enums
+v4l2_cflags := -fno-short-enums -Wall -Wextra -fvisibility=hidden
 
-# Note: see V4L2 HALv1 implementation when adding YUV support,
-#   some various unexpected variables had to be set.
+v4l2_c_includes := $(call include-path-for, camera)
 
-LOCAL_SHARED_LIBRARIES := \
-    libbase \
-    libcamera_client \
-    libcamera_metadata \
-    libcutils \
-    libhardware \
-    liblog \
-    libnativehelper \
-    libsync \
-    libutils \
-
-LOCAL_STATIC_LIBRARIES :=
-
-LOCAL_C_INCLUDES += \
-    $(call include-path-for, camera)
-
-LOCAL_SRC_FILES := \
+v4l2_src_files := \
   camera.cpp \
   stream.cpp \
   stream_format.cpp \
   v4l2_camera.cpp \
   v4l2_camera_hal.cpp \
   v4l2_gralloc.cpp \
+  v4l2_metadata.cpp \
   v4l2_wrapper.cpp \
 
-LOCAL_CFLAGS += -Wall -Wextra -fvisibility=hidden
+v4l2_test_files := \
+  v4l2_metadata_test.cpp \
 
+# V4L2 Camera HAL.
+# ==============================================================================
+include $(CLEAR_VARS)
+LOCAL_MODULE := camera.v4l2
+LOCAL_MODULE_RELATIVE_PATH := hw
+LOCAL_CFLAGS += $(v4l2_cflags)
+LOCAL_SHARED_LIBRARIES := $(v4l2_shared_libs)
+LOCAL_STATIC_LIBRARIES := $(v4l2_static_libs)
+LOCAL_C_INCLUDES += $(v4l2_c_includes)
+LOCAL_SRC_FILES := $(v4l2_src_files)
 include $(BUILD_SHARED_LIBRARY)
 
+# Unit tests for V4L2 Camera HAL.
+# ==============================================================================
+include $(CLEAR_VARS)
+LOCAL_MODULE := camera.v4l2_test
+LOCAL_CFLAGS += $(v4l2_cflags)
+LOCAL_SHARED_LIBRARIES := $(v4l2_shared_libs)
+LOCAL_STATIC_LIBRARIES := \
+  libBionicGtestMain \
+  libgmock \
+  $(v4l2_static_libs) \
+
+LOCAL_C_INCLUDES += $(v4l2_c_includes)
+LOCAL_SRC_FILES := \
+  $(v4l2_src_files) \
+  $(v4l2_test_files) \
+
+include $(BUILD_NATIVE_TEST)
+
 endif # USE_CAMERA_V4L2_HAL
diff --git a/modules/camera/3_4/metadata/partial_metadata_interface.h b/modules/camera/3_4/metadata/partial_metadata_interface.h
new file mode 100644
index 0000000..78da42c
--- /dev/null
+++ b/modules/camera/3_4/metadata/partial_metadata_interface.h
@@ -0,0 +1,53 @@
+/*
+ * 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_PARTIAL_METADATA_INTERFACE_H_
+#define V4L2_CAMERA_HAL_METADATA_PARTIAL_METADATA_INTERFACE_H_
+
+#include <vector>
+
+#include <hardware/camera3.h>
+
+namespace v4l2_camera_hal {
+
+// A subset of metadata.
+class PartialMetadataInterface {
+ public:
+  virtual ~PartialMetadataInterface(){};
+
+  // The metadata tags this partial metadata is responsible for.
+  // See system/media/camera/docs/docs.html for descriptions of each tag.
+  virtual const std::vector<int32_t>& StaticTags() = 0;
+  virtual const std::vector<int32_t>& ControlTags() = 0;
+  virtual const std::vector<int32_t>& DynamicTags() = 0;
+
+  // Add all the static properties this partial metadata
+  // is responsible for to |metadata|.
+  virtual int PopulateStaticFields(camera_metadata_t** metadata) = 0;
+  // Add all the dynamic states this partial metadata
+  // is responsible for to |metadata|.
+  virtual int PopulateDynamicFields(camera_metadata_t** metadata) = 0;
+  // Check if the requested control values from |metadata| (for controls
+  // this partial metadata owns) are supported.
+  virtual bool SupportsRequestValues(const camera_metadata_t* metadata) = 0;
+  // Set all the controls this partial metadata
+  // is responsible for from |metadata|.
+  virtual int SetRequestValues(const camera_metadata_t* metadata) = 0;
+};
+
+}  // namespace v4l2_camera_hal
+
+#endif  // V4L2_CAMERA_HAL_METADATA_PARTIAL_METADATA_INTERFACE_H_
diff --git a/modules/camera/3_4/metadata/partial_metadata_interface_mock.h b/modules/camera/3_4/metadata/partial_metadata_interface_mock.h
new file mode 100644
index 0000000..a8795a3
--- /dev/null
+++ b/modules/camera/3_4/metadata/partial_metadata_interface_mock.h
@@ -0,0 +1,42 @@
+/*
+ * 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.
+ */
+
+// Mock for partial metadata interfaces.
+
+#ifndef V4L2_CAMERA_HAL_PARTIAL_METADATA_INTERFACE_MOCK_H_
+#define V4L2_CAMERA_HAL_PARTIAL_METADATA_INTERFACE_MOCK_H_
+
+#include <gmock/gmock.h>
+
+#include "partial_metadata_interface.h"
+
+namespace v4l2_camera_hal {
+
+class PartialMetadataInterfaceMock : public PartialMetadataInterface {
+ public:
+  PartialMetadataInterfaceMock() : PartialMetadataInterface(){};
+  MOCK_METHOD0(StaticTags, const std::vector<int32_t>&());
+  MOCK_METHOD0(ControlTags, const std::vector<int32_t>&());
+  MOCK_METHOD0(DynamicTags, const std::vector<int32_t>&());
+  MOCK_METHOD1(PopulateStaticFields, int(camera_metadata_t** metadata));
+  MOCK_METHOD1(PopulateDynamicFields, int(camera_metadata_t** metadata));
+  MOCK_METHOD1(SupportsRequestValues, bool(const camera_metadata_t* metadata));
+  MOCK_METHOD1(SetRequestValues, int(const camera_metadata_t* metadata));
+};
+
+}  // namespace v4l2_camera_hal
+
+#endif  // V4L2_CAMERA_HAL_PARTIAL_METADATA_INTERFACE_MOCK_H_
diff --git a/modules/camera/3_4/v4l2_metadata.cpp b/modules/camera/3_4/v4l2_metadata.cpp
new file mode 100644
index 0000000..54d2809
--- /dev/null
+++ b/modules/camera/3_4/v4l2_metadata.cpp
@@ -0,0 +1,143 @@
+/*
+ * Copyright 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 "v4l2_metadata.h"
+
+#include <camera/CameraMetadata.h>
+
+#include "common.h"
+
+namespace v4l2_camera_hal {
+
+V4L2Metadata::V4L2Metadata(V4L2Wrapper* device) : device_(device) {
+  HAL_LOG_ENTER();
+
+  PopulateComponents();
+}
+
+V4L2Metadata::~V4L2Metadata() { HAL_LOG_ENTER(); }
+
+void V4L2Metadata::PopulateComponents() {
+  HAL_LOG_ENTER();
+
+  // TODO(arihc): Add all default components.
+}
+
+void V4L2Metadata::AddComponent(
+    std::unique_ptr<PartialMetadataInterface> component) {
+  HAL_LOG_ENTER();
+
+  components_.push_back(std::move(component));
+}
+
+int V4L2Metadata::FillStaticMetadata(camera_metadata_t** metadata) {
+  HAL_LOG_ENTER();
+
+  std::vector<int32_t> static_tags;
+  std::vector<int32_t> control_tags;
+  std::vector<int32_t> dynamic_tags;
+  int res = 0;
+
+  for (auto& component : components_) {
+    // Populate the fields.
+    res = component->PopulateStaticFields(metadata);
+    if (res) {
+      // Exit on error.
+      HAL_LOGE("Failed to get all static properties.");
+      return res;
+    }
+
+    // Note what tags the component adds.
+    const std::vector<int32_t>* tags = &component->StaticTags();
+    static_tags.insert(static_tags.end(), tags->begin(), tags->end());
+    tags = &component->ControlTags();
+    control_tags.insert(control_tags.end(), tags->begin(), tags->end());
+    tags = &component->DynamicTags();
+    dynamic_tags.insert(dynamic_tags.end(), tags->begin(), tags->end());
+  }
+
+  // Populate the meta fields.
+  android::CameraMetadata metadata_wrapper(*metadata);
+  static_tags.push_back(ANDROID_REQUEST_AVAILABLE_REQUEST_KEYS);
+  res = metadata_wrapper.update(ANDROID_REQUEST_AVAILABLE_REQUEST_KEYS,
+                                control_tags.data(), control_tags.size());
+  if (res != android::OK) {
+    HAL_LOGE("Failed to add request keys meta key.");
+    return -ENODEV;
+  }
+  static_tags.push_back(ANDROID_REQUEST_AVAILABLE_RESULT_KEYS);
+  res = metadata_wrapper.update(ANDROID_REQUEST_AVAILABLE_RESULT_KEYS,
+                                dynamic_tags.data(), dynamic_tags.size());
+  if (res != android::OK) {
+    HAL_LOGE("Failed to add result keys meta key.");
+    return -ENODEV;
+  }
+  static_tags.push_back(ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS);
+  res = metadata_wrapper.update(ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS,
+                                static_tags.data(), static_tags.size());
+  if (res != android::OK) {
+    HAL_LOGE("Failed to add characteristics keys meta key.");
+    return -ENODEV;
+  }
+
+  *metadata = metadata_wrapper.release();
+  return 0;
+}
+
+bool V4L2Metadata::IsValidRequest(const camera_metadata_t* metadata) {
+  HAL_LOG_ENTER();
+
+  for (auto& component : components_) {
+    // Check that all components support the values requested of them.
+    bool valid_request = component->SupportsRequestValues(metadata);
+    if (!valid_request) {
+      // Exit early if possible.
+      return false;
+    }
+  }
+
+  return true;
+}
+
+int V4L2Metadata::SetRequestSettings(const camera_metadata_t* metadata) {
+  HAL_LOG_ENTER();
+
+  for (auto& component : components_) {
+    int res = component->SetRequestValues(metadata);
+    if (res) {
+      // Exit early if possible.
+      HAL_LOGE("Failed to set all requested settings.");
+      return res;
+    }
+  }
+
+  return 0;
+}
+
+int V4L2Metadata::FillResultMetadata(camera_metadata_t** metadata) {
+  for (auto& component : components_) {
+    int res = component->PopulateDynamicFields(metadata);
+    if (res) {
+      // Exit early if possible.
+      HAL_LOGE("Failed to get all dynamic result fields.");
+      return res;
+    }
+  }
+
+  return 0;
+}
+
+}  // namespace v4l2_camera_hal
diff --git a/modules/camera/3_4/v4l2_metadata.h b/modules/camera/3_4/v4l2_metadata.h
new file mode 100644
index 0000000..573154a
--- /dev/null
+++ b/modules/camera/3_4/v4l2_metadata.h
@@ -0,0 +1,59 @@
+/*
+ * 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_V4L2_METADATA_H_
+#define V4L2_CAMERA_HAL_V4L2_METADATA_H_
+
+#include <hardware/camera3.h>
+
+#include "common.h"
+#include "metadata/partial_metadata_interface.h"
+#include "v4l2_wrapper.h"
+
+namespace v4l2_camera_hal {
+class V4L2Metadata {
+ public:
+  V4L2Metadata(V4L2Wrapper* device);
+  virtual ~V4L2Metadata();
+
+  int FillStaticMetadata(camera_metadata_t** metadata);
+  bool IsValidRequest(const camera_metadata_t* metadata);
+  int SetRequestSettings(const camera_metadata_t* metadata);
+  int FillResultMetadata(camera_metadata_t** metadata);
+
+ protected:
+  // Helper for the constructor to fill in metadata components.
+  // Virtual/protected so tests can subclass and override populating with mocks.
+  virtual void PopulateComponents();
+
+ private:
+  // Helper for the constructor to fill in metadata components.
+  void AddComponent(std::unique_ptr<PartialMetadataInterface> component);
+
+  // Access to the device.
+  V4L2Wrapper* device_;
+  // The overall metadata is broken down into several distinct pieces.
+  // Note: it is undefined behavior if multiple components share tags.
+  std::vector<std::unique_ptr<PartialMetadataInterface>> components_;
+
+  friend class V4L2MetadataTest;
+
+  DISALLOW_COPY_AND_ASSIGN(V4L2Metadata);
+};
+
+}  // namespace v4l2_camera_hal
+
+#endif  // V4L2_CAMERA_HAL_V4L2_METADATA_H_
diff --git a/modules/camera/3_4/v4l2_metadata_test.cpp b/modules/camera/3_4/v4l2_metadata_test.cpp
new file mode 100644
index 0000000..651d48c
--- /dev/null
+++ b/modules/camera/3_4/v4l2_metadata_test.cpp
@@ -0,0 +1,255 @@
+/*
+ * 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 "v4l2_metadata.h"
+
+#include <memory>
+#include <set>
+#include <vector>
+
+#include <camera/CameraMetadata.h>
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+
+#include "metadata/partial_metadata_interface_mock.h"
+#include "v4l2_wrapper_mock.h"
+
+using testing::AtMost;
+using testing::Return;
+using testing::ReturnRef;
+using testing::Test;
+using testing::_;
+
+namespace v4l2_camera_hal {
+
+class V4L2MetadataTest : public Test {
+ protected:
+  // Create a test version of V4L2Metadata with no default components.
+  class TestV4L2Metadata : public V4L2Metadata {
+   public:
+    TestV4L2Metadata(V4L2Wrapper* device) : V4L2Metadata(device){};
+
+   private:
+    void PopulateComponents() override{};
+  };
+
+  virtual void SetUp() {
+    device_.reset(new V4L2WrapperMock());
+    dut_.reset(new TestV4L2Metadata(device_.get()));
+
+    component1_.reset(new PartialMetadataInterfaceMock);
+    component2_.reset(new PartialMetadataInterfaceMock);
+  }
+
+  // Once the component mocks have had expectations set,
+  // add them to the device under test.
+  virtual void AddComponents() {
+    // Don't mind moving; Gmock/Gtest fails on leaked mocks unless disabled by
+    // runtime flags.
+    dut_->AddComponent(std::move(component1_));
+    dut_->AddComponent(std::move(component2_));
+  }
+
+  virtual void CompareTags(const std::set<int32_t>& expected,
+                           const camera_metadata_entry_t& actual) {
+    EXPECT_EQ(expected.size(), actual.count);
+    for (size_t i = 0; i < actual.count; ++i) {
+      EXPECT_NE(expected.find(actual.data.i32[i]), expected.end());
+    }
+  }
+
+  // Device under test.
+  std::unique_ptr<V4L2Metadata> dut_;
+  // Mocks.
+  std::unique_ptr<V4L2WrapperMock> device_;
+  std::unique_ptr<PartialMetadataInterfaceMock> component1_;
+  std::unique_ptr<PartialMetadataInterfaceMock> component2_;
+  // An empty vector to use as necessary.
+  std::vector<int32_t> empty_tags_;
+};
+
+TEST_F(V4L2MetadataTest, FillStaticSuccess) {
+  android::CameraMetadata metadata(1);
+  camera_metadata_t* metadata_raw = metadata.release();
+
+  // Should populate all the component static pieces.
+  EXPECT_CALL(*component1_, PopulateStaticFields(_)).WillOnce(Return(0));
+  EXPECT_CALL(*component2_, PopulateStaticFields(_)).WillOnce(Return(0));
+
+  // Should populate the meta keys, by polling each component's keys.
+  std::vector<int32_t> static_tags_1({1, 2});
+  std::vector<int32_t> static_tags_2({3, 4});
+  std::vector<int32_t> control_tags_1({5, 6});
+  std::vector<int32_t> control_tags_2({7, 8});
+  std::vector<int32_t> dynamic_tags_1({9, 10});
+  std::vector<int32_t> dynamic_tags_2({11, 12});
+  EXPECT_CALL(*component1_, StaticTags()).WillOnce(ReturnRef(static_tags_1));
+  EXPECT_CALL(*component1_, ControlTags()).WillOnce(ReturnRef(control_tags_1));
+  EXPECT_CALL(*component1_, DynamicTags()).WillOnce(ReturnRef(dynamic_tags_1));
+  EXPECT_CALL(*component2_, StaticTags()).WillOnce(ReturnRef(static_tags_2));
+  EXPECT_CALL(*component2_, ControlTags()).WillOnce(ReturnRef(control_tags_2));
+  EXPECT_CALL(*component2_, DynamicTags()).WillOnce(ReturnRef(dynamic_tags_2));
+
+  AddComponents();
+  // Should succeed. If it didn't, no reason to continue checking output.
+  ASSERT_EQ(dut_->FillStaticMetadata(&metadata_raw), 0);
+
+  // Meta keys should be filled correctly.
+  // Note: sets are used here, but it is undefined behavior if
+  // the class has multiple componenets reporting overlapping tags.
+
+  // Get the expected tags = combined tags of all components.
+  std::set<int32_t> static_tags(static_tags_1.begin(), static_tags_1.end());
+  static_tags.insert(static_tags_2.begin(), static_tags_2.end());
+  std::set<int32_t> control_tags(control_tags_1.begin(), control_tags_1.end());
+  control_tags.insert(control_tags_2.begin(), control_tags_2.end());
+  std::set<int32_t> dynamic_tags(dynamic_tags_1.begin(), dynamic_tags_1.end());
+  dynamic_tags.insert(dynamic_tags_2.begin(), dynamic_tags_2.end());
+
+  // Static tags includes not only all component static tags, but also
+  // the meta AVAILABLE_*_KEYS (* = [REQUEST, RESULT, CHARACTERISTICS]).
+  static_tags.emplace(ANDROID_REQUEST_AVAILABLE_REQUEST_KEYS);
+  static_tags.emplace(ANDROID_REQUEST_AVAILABLE_RESULT_KEYS);
+  static_tags.emplace(ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS);
+
+  // Check against what was filled in in the metadata.
+  metadata.acquire(metadata_raw);
+  CompareTags(static_tags,
+              metadata.find(ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS));
+  CompareTags(control_tags,
+              metadata.find(ANDROID_REQUEST_AVAILABLE_REQUEST_KEYS));
+  CompareTags(dynamic_tags,
+              metadata.find(ANDROID_REQUEST_AVAILABLE_RESULT_KEYS));
+}
+
+TEST_F(V4L2MetadataTest, FillStaticFail) {
+  camera_metadata_t* metadata = nullptr;
+  int err = -99;
+  // Order undefined, and may or may not exit early; use AtMost.
+  EXPECT_CALL(*component1_, PopulateStaticFields(_))
+      .Times(AtMost(1))
+      .WillOnce(Return(0));
+  EXPECT_CALL(*component2_, PopulateStaticFields(_)).WillOnce(Return(err));
+
+  // May or may not exit early, may still try to populate meta tags.
+  EXPECT_CALL(*component1_, StaticTags())
+      .Times(AtMost(1))
+      .WillOnce(ReturnRef(empty_tags_));
+  EXPECT_CALL(*component1_, ControlTags())
+      .Times(AtMost(1))
+      .WillOnce(ReturnRef(empty_tags_));
+  EXPECT_CALL(*component1_, DynamicTags())
+      .Times(AtMost(1))
+      .WillOnce(ReturnRef(empty_tags_));
+  EXPECT_CALL(*component2_, StaticTags())
+      .Times(AtMost(1))
+      .WillOnce(ReturnRef(empty_tags_));
+  EXPECT_CALL(*component2_, ControlTags())
+      .Times(AtMost(1))
+      .WillOnce(ReturnRef(empty_tags_));
+  EXPECT_CALL(*component2_, DynamicTags())
+      .Times(AtMost(1))
+      .WillOnce(ReturnRef(empty_tags_));
+
+  AddComponents();
+  // If any component errors, error should be returned
+  EXPECT_EQ(dut_->FillStaticMetadata(&metadata), err);
+}
+
+TEST_F(V4L2MetadataTest, IsValidSuccess) {
+  camera_metadata_t* metadata = nullptr;
+
+  // Should check if all the component request values are valid.
+  EXPECT_CALL(*component1_, SupportsRequestValues(_)).WillOnce(Return(true));
+  EXPECT_CALL(*component2_, SupportsRequestValues(_)).WillOnce(Return(true));
+
+  AddComponents();
+  // Should succeed.
+  EXPECT_TRUE(dut_->IsValidRequest(metadata));
+}
+
+TEST_F(V4L2MetadataTest, IsValidFail) {
+  camera_metadata_t* metadata = nullptr;
+
+  // Should check if all the component request values are valid.
+  // Order undefined, and may or may not exit early; use AtMost.
+  EXPECT_CALL(*component1_, SupportsRequestValues(_))
+      .Times(AtMost(1))
+      .WillOnce(Return(true));
+  EXPECT_CALL(*component2_, SupportsRequestValues(_)).WillOnce(Return(false));
+
+  AddComponents();
+  // Should fail since one of the components failed.
+  EXPECT_FALSE(dut_->IsValidRequest(metadata));
+}
+
+TEST_F(V4L2MetadataTest, SetSettingsSuccess) {
+  camera_metadata_t* metadata = nullptr;
+
+  // Should check if all the components set successfully.
+  EXPECT_CALL(*component1_, SetRequestValues(_)).WillOnce(Return(0));
+  EXPECT_CALL(*component2_, SetRequestValues(_)).WillOnce(Return(0));
+
+  AddComponents();
+  // Should succeed.
+  EXPECT_EQ(dut_->SetRequestSettings(metadata), 0);
+}
+
+TEST_F(V4L2MetadataTest, SetSettingsFail) {
+  camera_metadata_t* metadata = nullptr;
+  int err = -99;
+
+  // Should check if all the components set successfully.
+  // Order undefined, and may or may not exit early; use AtMost.
+  EXPECT_CALL(*component1_, SetRequestValues(_))
+      .Times(AtMost(1))
+      .WillOnce(Return(0));
+  EXPECT_CALL(*component2_, SetRequestValues(_)).WillOnce(Return(err));
+
+  AddComponents();
+  // Should fail since one of the components failed.
+  EXPECT_EQ(dut_->SetRequestSettings(metadata), err);
+}
+
+TEST_F(V4L2MetadataTest, FillResultSuccess) {
+  camera_metadata_t* metadata = nullptr;
+
+  // Should check if all the components fill results successfully.
+  EXPECT_CALL(*component1_, PopulateDynamicFields(_)).WillOnce(Return(0));
+  EXPECT_CALL(*component2_, PopulateDynamicFields(_)).WillOnce(Return(0));
+
+  AddComponents();
+  // Should succeed.
+  EXPECT_EQ(dut_->FillResultMetadata(&metadata), 0);
+}
+
+TEST_F(V4L2MetadataTest, FillResultFail) {
+  camera_metadata_t* metadata = nullptr;
+  int err = -99;
+
+  // Should check if all the components fill results successfully.
+  // Order undefined, and may or may not exit early; use AtMost.
+  EXPECT_CALL(*component1_, PopulateDynamicFields(_))
+      .Times(AtMost(1))
+      .WillOnce(Return(0));
+  EXPECT_CALL(*component2_, PopulateDynamicFields(_)).WillOnce(Return(err));
+
+  AddComponents();
+  // Should fail since one of the components failed.
+  EXPECT_EQ(dut_->FillResultMetadata(&metadata), err);
+}
+
+}  // namespace v4l2_camera_hal
diff --git a/modules/camera/3_4/v4l2_wrapper.h b/modules/camera/3_4/v4l2_wrapper.h
index fd4ac34..d67b36e 100644
--- a/modules/camera/3_4/v4l2_wrapper.h
+++ b/modules/camera/3_4/v4l2_wrapper.h
@@ -82,6 +82,8 @@
   // Lock protecting use of the device.
   std::mutex device_lock_;
 
+  friend class V4L2WrapperMock;
+
   DISALLOW_COPY_AND_ASSIGN(V4L2Wrapper);
 };
 
diff --git a/modules/camera/3_4/v4l2_wrapper_mock.h b/modules/camera/3_4/v4l2_wrapper_mock.h
new file mode 100644
index 0000000..d82ed33
--- /dev/null
+++ b/modules/camera/3_4/v4l2_wrapper_mock.h
@@ -0,0 +1,49 @@
+/*
+ * 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.
+ */
+
+// Mock for wrapper class used to communicate with V4L2 devices.
+
+#ifndef V4L2_CAMERA_HAL_V4L2_WRAPPER_MOCK_H_
+#define V4L2_CAMERA_HAL_V4L2_WRAPPER_MOCK_H_
+
+#include <gmock/gmock.h>
+
+#include "v4l2_wrapper.h"
+
+namespace v4l2_camera_hal {
+
+class V4L2WrapperMock : public V4L2Wrapper {
+ public:
+  V4L2WrapperMock() : V4L2Wrapper("", nullptr){};
+  MOCK_METHOD0(Connect, int());
+  MOCK_METHOD0(Disconnect, void());
+  MOCK_METHOD0(StreamOn, int());
+  MOCK_METHOD0(StreamOff, int());
+  MOCK_METHOD2(QueryControl,
+               int(uint32_t control_id, v4l2_query_ext_ctrl* result));
+  MOCK_METHOD2(GetControl, int(uint32_t control_id, int32_t* value));
+  MOCK_METHOD3(SetControl,
+               int(uint32_t control_id, int32_t desired, int32_t* result));
+  MOCK_METHOD2(SetFormat, int(const default_camera_hal::Stream& stream,
+                              uint32_t* result_max_buffers));
+  MOCK_METHOD1(EnqueueBuffer,
+               int(const camera3_stream_buffer_t* camera_buffer));
+  MOCK_METHOD1(DequeueBuffer, int(v4l2_buffer* buffer));
+};
+
+}  // namespace v4l2_camera_hal
+
+#endif  // V4L2_CAMERA_HAL_V4L2_WRAPPER_MOCK_H_