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 #include "property.h"
18
19 #include <array>
20 #include <vector>
21
22 #include <camera/CameraMetadata.h>
23 #include <gtest/gtest.h>
24 #include <hardware/camera3.h>
25 #include "array_vector.h"
26 #include "metadata_common.h"
27 #include "test_common.h"
28
29 using testing::Test;
30
31 namespace v4l2_camera_hal {
32
33 class PropertyTest : public Test {
34 protected:
35 // Need tags that match the data types being passed.
36 static constexpr int32_t byte_tag_ = ANDROID_CONTROL_SCENE_MODE_OVERRIDES;
37 static constexpr int32_t float_tag_ = ANDROID_COLOR_CORRECTION_GAINS;
38 static constexpr int32_t int_tag_ = ANDROID_CONTROL_AE_EXPOSURE_COMPENSATION;
39 static constexpr int32_t int_tag2_ = ANDROID_JPEG_ORIENTATION;
40 };
41
TEST_F(PropertyTest,Tags)42 TEST_F(PropertyTest, Tags) {
43 Property<int32_t> property(int_tag_, 1);
44
45 // Should have only the single tag it was constructed with.
46 EXPECT_EQ(property.ControlTags().size(), 0u);
47 EXPECT_EQ(property.DynamicTags().size(), 0u);
48 ASSERT_EQ(property.StaticTags().size(), 1u);
49 // The macro doesn't like the int_tag_ variable being passed in directly.
50 int32_t expected_tag = int_tag_;
51 EXPECT_EQ(property.StaticTags()[0], expected_tag);
52 }
53
TEST_F(PropertyTest,PopulateStaticSingleNumber)54 TEST_F(PropertyTest, PopulateStaticSingleNumber) {
55 // Set up a fixed property.
56 int32_t data = 1234;
57 Property<int32_t> property(int_tag_, data);
58
59 // Populate static fields.
60 android::CameraMetadata metadata;
61 ASSERT_EQ(property.PopulateStaticFields(&metadata), 0);
62
63 // Check the results.
64 // Should only have added 1 entry.
65 EXPECT_EQ(metadata.entryCount(), 1u);
66 // Should have added the right entry.
67 ExpectMetadataEq(metadata, int_tag_, data);
68 }
69
70 // TODO(b/30839858): These tests are really testing the metadata_common.h
71 // UpdateMetadata methods, and shouldn't be conducted here.
TEST_F(PropertyTest,PopulateStaticVector)72 TEST_F(PropertyTest, PopulateStaticVector) {
73 // Set up a fixed property.
74 std::vector<float> data({0.1, 2.3, 4.5, 6.7});
75 Property<std::vector<float>> property(float_tag_, data);
76
77 // Populate static fields.
78 android::CameraMetadata metadata;
79 ASSERT_EQ(property.PopulateStaticFields(&metadata), 0);
80
81 // Check the results.
82 // Should only have added 1 entry.
83 EXPECT_EQ(metadata.entryCount(), 1u);
84 // Should have added the right entry.
85 ExpectMetadataEq(metadata, float_tag_, data);
86 }
87
TEST_F(PropertyTest,PopulateStaticArray)88 TEST_F(PropertyTest, PopulateStaticArray) {
89 // Set up a fixed property.
90 std::array<float, 4> data({{0.1, 2.3, 4.5, 6.7}});
91 Property<std::array<float, 4>> property(float_tag_, data);
92
93 // Populate static fields.
94 android::CameraMetadata metadata;
95 ASSERT_EQ(property.PopulateStaticFields(&metadata), 0);
96
97 // Check the results.
98 // Should only have added 1 entry.
99 EXPECT_EQ(metadata.entryCount(), 1u);
100 // Should have added the right entry.
101 ExpectMetadataEq(metadata, float_tag_, data);
102 }
103
TEST_F(PropertyTest,PopulateStaticArrayVector)104 TEST_F(PropertyTest, PopulateStaticArrayVector) {
105 // Set up a fixed property.
106 ArrayVector<uint8_t, 3> data;
107 data.push_back({{1, 2, 3}});
108 data.push_back({{4, 5, 6}});
109 Property<ArrayVector<uint8_t, 3>> property(byte_tag_, data);
110
111 // Populate static fields.
112 android::CameraMetadata metadata;
113 ASSERT_EQ(property.PopulateStaticFields(&metadata), 0);
114
115 // Check the results.
116 // Should only have added 1 entry.
117 EXPECT_EQ(metadata.entryCount(), 1u);
118 // Should have added the right entry.
119 ExpectMetadataEq(metadata, byte_tag_, data);
120 }
121
TEST_F(PropertyTest,PopulateDynamic)122 TEST_F(PropertyTest, PopulateDynamic) {
123 Property<int32_t> property(int_tag_, 1);
124
125 android::CameraMetadata metadata;
126 EXPECT_EQ(property.PopulateDynamicFields(&metadata), 0);
127
128 // Shouldn't have added anything.
129 EXPECT_TRUE(metadata.isEmpty());
130 }
131
TEST_F(PropertyTest,PopulateTemplate)132 TEST_F(PropertyTest, PopulateTemplate) {
133 Property<int32_t> property(int_tag_, 1);
134
135 for (int i = 1; i < CAMERA3_TEMPLATE_COUNT; ++i) {
136 android::CameraMetadata metadata;
137 EXPECT_EQ(property.PopulateTemplateRequest(i, &metadata), 0);
138 // Shouldn't have added anything.
139 EXPECT_TRUE(metadata.isEmpty());
140 }
141 }
142
TEST_F(PropertyTest,SupportsRequest)143 TEST_F(PropertyTest, SupportsRequest) {
144 Property<int32_t> property(int_tag_, 1);
145 android::CameraMetadata metadata;
146 EXPECT_EQ(property.SupportsRequestValues(metadata), true);
147 }
148
TEST_F(PropertyTest,SetRequest)149 TEST_F(PropertyTest, SetRequest) {
150 Property<int32_t> property(int_tag_, 1);
151 android::CameraMetadata metadata;
152 EXPECT_EQ(property.SetRequestValues(metadata), 0);
153 }
154
155 } // namespace v4l2_camera_hal
156