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 #include "array_vector.h"
26 #include "metadata_common.h"
27
28 namespace v4l2_camera_hal {
29
30 // Check that metadata of a given tag matches expectations.
31 // Generic.
32 template <typename T>
ExpectMetadataEq(const android::CameraMetadata & metadata,int32_t tag,const T * expected,size_t size)33 static void ExpectMetadataEq(const android::CameraMetadata& metadata,
34 int32_t tag,
35 const T* expected,
36 size_t size) {
37 camera_metadata_ro_entry_t entry = metadata.find(tag);
38 ASSERT_EQ(entry.count, size);
39 const T* data = nullptr;
40 GetDataPointer(entry, &data);
41 ASSERT_NE(data, nullptr);
42 for (size_t i = 0; i < size; ++i) {
43 EXPECT_EQ(data[i], expected[i]);
44 }
45 }
46
47 // Single item.
48 template <typename T>
ExpectMetadataEq(const android::CameraMetadata & metadata,int32_t tag,T expected)49 static void ExpectMetadataEq(const android::CameraMetadata& metadata,
50 int32_t tag,
51 T expected) {
52 ExpectMetadataEq(metadata, tag, &expected, 1);
53 }
54
55 // Vector of items.
56 template <typename T>
ExpectMetadataEq(const android::CameraMetadata & metadata,int32_t tag,const std::vector<T> & expected)57 static void ExpectMetadataEq(const android::CameraMetadata& metadata,
58 int32_t tag,
59 const std::vector<T>& expected) {
60 ExpectMetadataEq(metadata, tag, expected.data(), expected.size());
61 }
62
63 // Array of items.
64 template <typename T, size_t N>
ExpectMetadataEq(const android::CameraMetadata & metadata,int32_t tag,const std::array<T,N> & expected)65 static void ExpectMetadataEq(const android::CameraMetadata& metadata,
66 int32_t tag,
67 const std::array<T, N>& expected) {
68 ExpectMetadataEq(metadata, tag, expected.data(), N);
69 }
70
71 // ArrayVector.
72 template <typename T, size_t N>
ExpectMetadataEq(const android::CameraMetadata & metadata,int32_t tag,const ArrayVector<T,N> & expected)73 static void ExpectMetadataEq(const android::CameraMetadata& metadata,
74 int32_t tag,
75 const ArrayVector<T, N>& expected) {
76 ExpectMetadataEq(
77 metadata, tag, expected.data(), expected.total_num_elements());
78 }
79
80 // Vector of arrays.
81 template <typename T, size_t N>
ExpectMetadataEq(const android::CameraMetadata & metadata,int32_t tag,const std::vector<std::array<T,N>> & expected)82 static void ExpectMetadataEq(const android::CameraMetadata& metadata,
83 int32_t tag,
84 const std::vector<std::array<T, N>>& expected) {
85 // Convert to array vector so we know all the elements are contiguous.
86 ArrayVector<T, N> array_vector;
87 for (const auto& array : expected) {
88 array_vector.push_back(array);
89 }
90 ExpectMetadataEq(metadata, tag, array_vector);
91 }
92
93 } // namespace v4l2_camera_hal
94
95 #endif // V4L2_CAMERA_HAL_METADATA_TEST_COMMON_H_
96