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 "metadata.h"
18 
19 #include <memory>
20 #include <set>
21 #include <vector>
22 
23 #include <camera/CameraMetadata.h>
24 #include <gmock/gmock.h>
25 #include <gtest/gtest.h>
26 #include "metadata_common.h"
27 #include "partial_metadata_interface_mock.h"
28 
29 using testing::AtMost;
30 using testing::Return;
31 using testing::Test;
32 using testing::_;
33 
34 namespace v4l2_camera_hal {
35 
36 class MetadataTest : public Test {
37  protected:
SetUp()38   virtual void SetUp() {
39     // Clear the DUT. AddComponents must be called before using it.
40     dut_.reset();
41 
42     component1_.reset(new PartialMetadataInterfaceMock());
43     component2_.reset(new PartialMetadataInterfaceMock());
44     metadata_.reset(new android::CameraMetadata());
45     non_empty_metadata_.reset(new android::CameraMetadata());
46     uint8_t val = 1;
47     non_empty_metadata_->update(ANDROID_COLOR_CORRECTION_MODE, &val, 1);
48   }
49 
50   // Once the component mocks have had expectations set,
51   // add them to the device under test.
AddComponents()52   virtual void AddComponents() {
53     // Don't mind moving; Gmock/Gtest fails on leaked mocks unless disabled by
54     // runtime flags.
55     PartialMetadataSet components;
56     components.insert(std::move(component1_));
57     components.insert(std::move(component2_));
58     dut_.reset(new Metadata(std::move(components)));
59   }
60 
CompareTags(const std::set<int32_t> & expected,const camera_metadata_entry_t & actual)61   virtual void CompareTags(const std::set<int32_t>& expected,
62                            const camera_metadata_entry_t& actual) {
63     ASSERT_EQ(expected.size(), actual.count);
64     for (size_t i = 0; i < actual.count; ++i) {
65       EXPECT_NE(expected.find(actual.data.i32[i]), expected.end());
66     }
67   }
68 
69   // Device under test.
70   std::unique_ptr<Metadata> dut_;
71   // Mocks.
72   std::unique_ptr<PartialMetadataInterfaceMock> component1_;
73   std::unique_ptr<PartialMetadataInterfaceMock> component2_;
74   // Metadata.
75   std::unique_ptr<android::CameraMetadata> metadata_;
76   std::unique_ptr<android::CameraMetadata> non_empty_metadata_;
77   // An empty vector to use as necessary.
78   std::vector<int32_t> empty_tags_;
79 };
80 
TEST_F(MetadataTest,FillStaticSuccess)81 TEST_F(MetadataTest, FillStaticSuccess) {
82   // Should populate all the component static pieces.
83   EXPECT_CALL(*component1_, PopulateStaticFields(_)).WillOnce(Return(0));
84   EXPECT_CALL(*component2_, PopulateStaticFields(_)).WillOnce(Return(0));
85 
86   // Should populate the meta keys, by polling each component's keys.
87   std::vector<int32_t> static_tags_1({1, 2});
88   std::vector<int32_t> static_tags_2({3, 4});
89   std::vector<int32_t> control_tags_1({5, 6});
90   std::vector<int32_t> control_tags_2({7, 8});
91   std::vector<int32_t> dynamic_tags_1({9, 10});
92   std::vector<int32_t> dynamic_tags_2({11, 12});
93   EXPECT_CALL(*component1_, StaticTags()).WillOnce(Return(static_tags_1));
94   EXPECT_CALL(*component1_, ControlTags()).WillOnce(Return(control_tags_1));
95   EXPECT_CALL(*component1_, DynamicTags()).WillOnce(Return(dynamic_tags_1));
96   EXPECT_CALL(*component2_, StaticTags()).WillOnce(Return(static_tags_2));
97   EXPECT_CALL(*component2_, ControlTags()).WillOnce(Return(control_tags_2));
98   EXPECT_CALL(*component2_, DynamicTags()).WillOnce(Return(dynamic_tags_2));
99 
100   AddComponents();
101   // Should succeed. If it didn't, no reason to continue checking output.
102   ASSERT_EQ(dut_->FillStaticMetadata(metadata_.get()), 0);
103 
104   // Meta keys should be filled correctly.
105   // Note: sets are used here, but it is undefined behavior if
106   // the class has multiple componenets reporting overlapping tags.
107 
108   // Get the expected tags = combined tags of all components.
109   std::set<int32_t> static_tags(static_tags_1.begin(), static_tags_1.end());
110   static_tags.insert(static_tags_2.begin(), static_tags_2.end());
111   std::set<int32_t> control_tags(control_tags_1.begin(), control_tags_1.end());
112   control_tags.insert(control_tags_2.begin(), control_tags_2.end());
113   std::set<int32_t> dynamic_tags(dynamic_tags_1.begin(), dynamic_tags_1.end());
114   dynamic_tags.insert(dynamic_tags_2.begin(), dynamic_tags_2.end());
115 
116   // Static tags includes not only all component static tags, but also
117   // the meta AVAILABLE_*_KEYS (* = [REQUEST, RESULT, CHARACTERISTICS]).
118   static_tags.emplace(ANDROID_REQUEST_AVAILABLE_REQUEST_KEYS);
119   static_tags.emplace(ANDROID_REQUEST_AVAILABLE_RESULT_KEYS);
120   static_tags.emplace(ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS);
121 
122   // Check against what was filled in in the metadata.
123   CompareTags(static_tags,
124               metadata_->find(ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS));
125   CompareTags(control_tags,
126               metadata_->find(ANDROID_REQUEST_AVAILABLE_REQUEST_KEYS));
127   CompareTags(dynamic_tags,
128               metadata_->find(ANDROID_REQUEST_AVAILABLE_RESULT_KEYS));
129 }
130 
TEST_F(MetadataTest,FillStaticFail)131 TEST_F(MetadataTest, FillStaticFail) {
132   int err = -99;
133   // Order undefined, and may or may not exit early; use AtMost.
134   EXPECT_CALL(*component1_, PopulateStaticFields(_))
135       .Times(AtMost(1))
136       .WillOnce(Return(0));
137   EXPECT_CALL(*component2_, PopulateStaticFields(_)).WillOnce(Return(err));
138 
139   // May or may not exit early, may still try to populate meta tags.
140   EXPECT_CALL(*component1_, StaticTags())
141       .Times(AtMost(1))
142       .WillOnce(Return(empty_tags_));
143   EXPECT_CALL(*component1_, ControlTags())
144       .Times(AtMost(1))
145       .WillOnce(Return(empty_tags_));
146   EXPECT_CALL(*component1_, DynamicTags())
147       .Times(AtMost(1))
148       .WillOnce(Return(empty_tags_));
149   EXPECT_CALL(*component2_, StaticTags())
150       .Times(AtMost(1))
151       .WillOnce(Return(empty_tags_));
152   EXPECT_CALL(*component2_, ControlTags())
153       .Times(AtMost(1))
154       .WillOnce(Return(empty_tags_));
155   EXPECT_CALL(*component2_, DynamicTags())
156       .Times(AtMost(1))
157       .WillOnce(Return(empty_tags_));
158 
159   AddComponents();
160   // If any component errors, error should be returned
161   EXPECT_EQ(dut_->FillStaticMetadata(metadata_.get()), err);
162 }
163 
TEST_F(MetadataTest,FillStaticNull)164 TEST_F(MetadataTest, FillStaticNull) {
165   AddComponents();
166   EXPECT_EQ(dut_->FillStaticMetadata(nullptr), -EINVAL);
167 }
168 
TEST_F(MetadataTest,IsValidSuccess)169 TEST_F(MetadataTest, IsValidSuccess) {
170   // Should check if all the component request values are valid.
171   EXPECT_CALL(*component1_, SupportsRequestValues(_)).WillOnce(Return(true));
172   EXPECT_CALL(*component2_, SupportsRequestValues(_)).WillOnce(Return(true));
173 
174   AddComponents();
175   // Should succeed.
176   // Note: getAndLock is a lock against pointer invalidation, not concurrency,
177   // and unlocks on object destruction.
178   EXPECT_TRUE(dut_->IsValidRequest(*non_empty_metadata_));
179 }
180 
TEST_F(MetadataTest,IsValidFail)181 TEST_F(MetadataTest, IsValidFail) {
182   // Should check if all the component request values are valid.
183   // Order undefined, and may or may not exit early; use AtMost.
184   EXPECT_CALL(*component1_, SupportsRequestValues(_))
185       .Times(AtMost(1))
186       .WillOnce(Return(true));
187   EXPECT_CALL(*component2_, SupportsRequestValues(_)).WillOnce(Return(false));
188 
189   AddComponents();
190   // Should fail since one of the components failed.
191   // Note: getAndLock is a lock against pointer invalidation, not concurrency,
192   // and unlocks on object destruction.
193   EXPECT_FALSE(dut_->IsValidRequest(*non_empty_metadata_));
194 }
195 
TEST_F(MetadataTest,IsValidEmpty)196 TEST_F(MetadataTest, IsValidEmpty) {
197   // Setting null settings is a special case indicating to use the
198   // previous (valid) settings. As such it is inherently valid.
199   // Should not try to check any components.
200   EXPECT_CALL(*component1_, SupportsRequestValues(_)).Times(0);
201   EXPECT_CALL(*component2_, SupportsRequestValues(_)).Times(0);
202 
203   AddComponents();
204   EXPECT_TRUE(dut_->IsValidRequest(*metadata_));
205 }
206 
TEST_F(MetadataTest,GetTemplateSuccess)207 TEST_F(MetadataTest, GetTemplateSuccess) {
208   int template_type = 3;
209 
210   // Should check if all the components fill the template successfully.
211   EXPECT_CALL(*component1_, PopulateTemplateRequest(template_type, _))
212       .WillOnce(Return(0));
213   EXPECT_CALL(*component2_, PopulateTemplateRequest(template_type, _))
214       .WillOnce(Return(0));
215 
216   AddComponents();
217   // Should succeed.
218   EXPECT_EQ(dut_->GetRequestTemplate(template_type, metadata_.get()), 0);
219 }
220 
TEST_F(MetadataTest,GetTemplateFail)221 TEST_F(MetadataTest, GetTemplateFail) {
222   int err = -99;
223   int template_type = 3;
224 
225   // Should check if all the components fill the template successfully.
226   // Order undefined, and may or may not exit early; use AtMost.
227   EXPECT_CALL(*component1_, PopulateTemplateRequest(template_type, _))
228       .Times(AtMost(1))
229       .WillOnce(Return(0));
230   EXPECT_CALL(*component2_, PopulateTemplateRequest(template_type, _))
231       .WillOnce(Return(err));
232 
233   AddComponents();
234   // Should fail since one of the components failed.
235   EXPECT_EQ(dut_->GetRequestTemplate(template_type, metadata_.get()), err);
236 }
237 
TEST_F(MetadataTest,GetTemplateNull)238 TEST_F(MetadataTest, GetTemplateNull) {
239   AddComponents();
240   EXPECT_EQ(dut_->GetRequestTemplate(1, nullptr), -EINVAL);
241 }
242 
TEST_F(MetadataTest,GetTemplateInvalid)243 TEST_F(MetadataTest, GetTemplateInvalid) {
244   int template_type = 99;  // Invalid template type.
245 
246   AddComponents();
247   // Should fail fast since template type is invalid.
248   EXPECT_EQ(dut_->GetRequestTemplate(template_type, metadata_.get()), -EINVAL);
249 }
250 
TEST_F(MetadataTest,SetSettingsSuccess)251 TEST_F(MetadataTest, SetSettingsSuccess) {
252   // Should check if all the components set successfully.
253   EXPECT_CALL(*component1_, SetRequestValues(_)).WillOnce(Return(0));
254   EXPECT_CALL(*component2_, SetRequestValues(_)).WillOnce(Return(0));
255 
256   AddComponents();
257   // Should succeed.
258   // Note: getAndLock is a lock against pointer invalidation, not concurrency,
259   // and unlocks on object destruction.
260   EXPECT_EQ(dut_->SetRequestSettings(*non_empty_metadata_), 0);
261 }
262 
TEST_F(MetadataTest,SetSettingsFail)263 TEST_F(MetadataTest, SetSettingsFail) {
264   int err = -99;
265 
266   // Should check if all the components set successfully.
267   // Order undefined, and may or may not exit early; use AtMost.
268   EXPECT_CALL(*component1_, SetRequestValues(_))
269       .Times(AtMost(1))
270       .WillOnce(Return(0));
271   EXPECT_CALL(*component2_, SetRequestValues(_)).WillOnce(Return(err));
272 
273   AddComponents();
274   // Should fail since one of the components failed.
275   // Note: getAndLock is a lock against pointer invalidation, not concurrency,
276   // and unlocks on object destruction.
277   EXPECT_EQ(dut_->SetRequestSettings(*non_empty_metadata_), err);
278 }
279 
TEST_F(MetadataTest,SetSettingsEmpty)280 TEST_F(MetadataTest, SetSettingsEmpty) {
281   // Setting null settings is a special case indicating to use the
282   // previous settings. Should not try to set any components.
283   EXPECT_CALL(*component1_, SetRequestValues(_)).Times(0);
284   EXPECT_CALL(*component2_, SetRequestValues(_)).Times(0);
285 
286   AddComponents();
287   // Should succeed.
288   EXPECT_EQ(dut_->SetRequestSettings(*metadata_), 0);
289 }
290 
TEST_F(MetadataTest,FillResultSuccess)291 TEST_F(MetadataTest, FillResultSuccess) {
292   // Should check if all the components fill results successfully.
293   EXPECT_CALL(*component1_, PopulateDynamicFields(_)).WillOnce(Return(0));
294   EXPECT_CALL(*component2_, PopulateDynamicFields(_)).WillOnce(Return(0));
295 
296   AddComponents();
297   // Should succeed.
298   EXPECT_EQ(dut_->FillResultMetadata(metadata_.get()), 0);
299 }
300 
TEST_F(MetadataTest,FillResultFail)301 TEST_F(MetadataTest, FillResultFail) {
302   int err = -99;
303 
304   // Should check if all the components fill results successfully.
305   // Order undefined, and may or may not exit early; use AtMost.
306   EXPECT_CALL(*component1_, PopulateDynamicFields(_))
307       .Times(AtMost(1))
308       .WillOnce(Return(0));
309   EXPECT_CALL(*component2_, PopulateDynamicFields(_)).WillOnce(Return(err));
310 
311   AddComponents();
312   // Should fail since one of the components failed.
313   EXPECT_EQ(dut_->FillResultMetadata(metadata_.get()), err);
314 }
315 
TEST_F(MetadataTest,FillResultNull)316 TEST_F(MetadataTest, FillResultNull) {
317   AddComponents();
318   EXPECT_EQ(dut_->FillResultMetadata(nullptr), -EINVAL);
319 }
320 
321 }  // namespace v4l2_camera_hal
322