1 /*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #ifndef V4L2_CAMERA_HAL_METADATA_TEST_COMMON_H_
18 #define V4L2_CAMERA_HAL_METADATA_TEST_COMMON_H_
19
20 #include <array>
21 #include <vector>
22
23 #include <camera/CameraMetadata.h>
24 #include <gtest/gtest.h>
25
26 #include "array_vector.h"
27 #include "metadata_common.h"
28
29 namespace v4l2_camera_hal {
30
31 // Check that metadata of a given tag matches expectations.
32 // Generic.
33 template <typename T>
ExpectMetadataEq(const android::CameraMetadata & metadata,int32_t tag,const T * expected,size_t size)34 static void ExpectMetadataEq(const android::CameraMetadata& metadata,
35 int32_t tag,
36 const T* expected,
37 size_t size) {
38 camera_metadata_ro_entry_t entry = metadata.find(tag);
39 ASSERT_EQ(entry.count, size);
40 const T* data = nullptr;
41 GetDataPointer(entry, &data);
42 ASSERT_NE(data, nullptr);
43 for (size_t i = 0; i < size; ++i) {
44 EXPECT_EQ(data[i], expected[i]);
45 }
46 }
47
48 // Single item.
49 template <typename T>
ExpectMetadataEq(const android::CameraMetadata & metadata,int32_t tag,T expected)50 static void ExpectMetadataEq(const android::CameraMetadata& metadata,
51 int32_t tag,
52 T expected) {
53 ExpectMetadataEq(metadata, tag, &expected, 1);
54 }
55
56 // Vector of items.
57 template <typename T>
ExpectMetadataEq(const android::CameraMetadata & metadata,int32_t tag,const std::vector<T> & expected)58 static void ExpectMetadataEq(const android::CameraMetadata& metadata,
59 int32_t tag,
60 const std::vector<T>& expected) {
61 ExpectMetadataEq(metadata, tag, expected.data(), expected.size());
62 }
63
64 // Array of items.
65 template <typename T, size_t N>
ExpectMetadataEq(const android::CameraMetadata & metadata,int32_t tag,const std::array<T,N> & expected)66 static void ExpectMetadataEq(const android::CameraMetadata& metadata,
67 int32_t tag,
68 const std::array<T, N>& expected) {
69 ExpectMetadataEq(metadata, tag, expected.data(), N);
70 }
71
72 // ArrayVector.
73 template <typename T, size_t N>
ExpectMetadataEq(const android::CameraMetadata & metadata,int32_t tag,const ArrayVector<T,N> & expected)74 static void ExpectMetadataEq(const android::CameraMetadata& metadata,
75 int32_t tag,
76 const ArrayVector<T, N>& expected) {
77 ExpectMetadataEq(
78 metadata, tag, expected.data(), expected.total_num_elements());
79 }
80
81 // Vector of arrays.
82 template <typename T, size_t N>
ExpectMetadataEq(const android::CameraMetadata & metadata,int32_t tag,const std::vector<std::array<T,N>> & expected)83 static int ExpectMetadataEq(const android::CameraMetadata& metadata,
84 int32_t tag,
85 const std::vector<std::array<T, N>>& expected) {
86 // Convert to array vector so we know all the elements are contiguous.
87 ArrayVector<T, N> array_vector;
88 for (const auto& array : expected) {
89 array_vector.push_back(array);
90 }
91 ExpectMetadataEq(metadata, tag, array_vector);
92 }
93
94 } // namespace v4l2_camera_hal
95
96 #endif // V4L2_CAMERA_HAL_METADATA_TEST_COMMON_H_
97