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 "control.h"
18 
19 #include <camera/CameraMetadata.h>
20 #include <gmock/gmock.h>
21 #include <gtest/gtest.h>
22 
23 #include "control_delegate_interface_mock.h"
24 #include "control_options_interface_mock.h"
25 #include "metadata_common.h"
26 #include "test_common.h"
27 
28 using testing::AtMost;
29 using testing::Expectation;
30 using testing::Return;
31 using testing::SetArgPointee;
32 using testing::Test;
33 using testing::_;
34 
35 namespace v4l2_camera_hal {
36 
37 class ControlTest : public Test {
38  protected:
SetUp()39   virtual void SetUp() {
40     mock_delegate_.reset(new ControlDelegateInterfaceMock<uint8_t>());
41     mock_options_.reset(new ControlOptionsInterfaceMock<uint8_t>());
42     // Nullify control so an error will be thrown if a test doesn't call
43     // PrepareControl.
44     control_.reset();
45   }
46 
PrepareControl()47   virtual void PrepareControl() {
48     // Use this method after all the EXPECT_CALLs to pass ownership of the mocks
49     // to the device.
50     std::unique_ptr<TaggedControlDelegate<uint8_t>> delegate =
51         std::make_unique<TaggedControlDelegate<uint8_t>>(
52             delegate_tag_, std::move(mock_delegate_));
53     std::unique_ptr<TaggedControlOptions<uint8_t>> options =
54         std::make_unique<TaggedControlOptions<uint8_t>>(
55             report_options_ ? options_tag_ : DO_NOT_REPORT_OPTIONS,
56             std::move(mock_options_));
57     if (use_options_) {
58       control_.reset(
59           new Control<uint8_t>(std::move(delegate), std::move(options)));
60     } else {
61       control_.reset(new Control<uint8_t>(std::move(delegate)));
62     }
63   }
64 
ExpectTags()65   virtual void ExpectTags() {
66     if (use_options_ && report_options_) {
67       ASSERT_EQ(control_->StaticTags().size(), 1u);
68       EXPECT_EQ(control_->StaticTags()[0], options_tag_);
69     } else {
70       EXPECT_TRUE(control_->StaticTags().empty());
71     }
72     // Controls use the same delgate, and thus tag, for getting and setting.
73     ASSERT_EQ(control_->ControlTags().size(), 1u);
74     EXPECT_EQ(control_->ControlTags()[0], delegate_tag_);
75     ASSERT_EQ(control_->DynamicTags().size(), 1u);
76     EXPECT_EQ(control_->DynamicTags()[0], delegate_tag_);
77   }
78 
ExpectOptions(const std::vector<uint8_t> & options)79   virtual void ExpectOptions(const std::vector<uint8_t>& options) {
80     // Options should be available.
81     android::CameraMetadata metadata;
82     ASSERT_EQ(control_->PopulateStaticFields(&metadata), 0);
83     if (use_options_ && report_options_) {
84       EXPECT_EQ(metadata.entryCount(), 1u);
85       ExpectMetadataEq(metadata, options_tag_, options);
86     } else {
87       EXPECT_EQ(metadata.entryCount(), 0u);
88       // Shouldn't be expecting any options.
89       EXPECT_TRUE(options.empty());
90     }
91   }
92 
ExpectValue(uint8_t value)93   virtual void ExpectValue(uint8_t value) {
94     android::CameraMetadata metadata;
95     ASSERT_EQ(control_->PopulateDynamicFields(&metadata), 0);
96     EXPECT_EQ(metadata.entryCount(), 1u);
97     ExpectMetadataEq(metadata, delegate_tag_, value);
98   }
99 
100   std::unique_ptr<Control<uint8_t>> control_;
101   std::unique_ptr<ControlDelegateInterfaceMock<uint8_t>> mock_delegate_;
102   std::unique_ptr<ControlOptionsInterfaceMock<uint8_t>> mock_options_;
103   bool use_options_ = true;
104   bool report_options_ = true;
105 
106   // Need tags that match the data type (uint8_t) being passed.
107   const int32_t delegate_tag_ = ANDROID_COLOR_CORRECTION_ABERRATION_MODE;
108   const int32_t options_tag_ =
109       ANDROID_COLOR_CORRECTION_AVAILABLE_ABERRATION_MODES;
110 };
111 
TEST_F(ControlTest,Tags)112 TEST_F(ControlTest, Tags) {
113   PrepareControl();
114   ExpectTags();
115 }
116 
TEST_F(ControlTest,TagsNoOptions)117 TEST_F(ControlTest, TagsNoOptions) {
118   use_options_ = false;
119   PrepareControl();
120   ExpectTags();
121 }
122 
TEST_F(ControlTest,TagsUnreportedOptions)123 TEST_F(ControlTest, TagsUnreportedOptions) {
124   report_options_ = false;
125   PrepareControl();
126   ExpectTags();
127 }
128 
TEST_F(ControlTest,PopulateStatic)129 TEST_F(ControlTest, PopulateStatic) {
130   std::vector<uint8_t> expected{1, 10, 20};
131   EXPECT_CALL(*mock_options_, MetadataRepresentation())
132       .WillOnce(Return(expected));
133   PrepareControl();
134   ExpectOptions(expected);
135 }
136 
TEST_F(ControlTest,PopulateStaticNoOptions)137 TEST_F(ControlTest, PopulateStaticNoOptions) {
138   use_options_ = false;
139   PrepareControl();
140   ExpectOptions({});
141 }
142 
TEST_F(ControlTest,PopulateStaticUnreportedOptions)143 TEST_F(ControlTest, PopulateStaticUnreportedOptions) {
144   report_options_ = false;
145   PrepareControl();
146   ExpectOptions({});
147 }
148 
TEST_F(ControlTest,PopulateDynamic)149 TEST_F(ControlTest, PopulateDynamic) {
150   uint8_t test_option = 99;
151   EXPECT_CALL(*mock_delegate_, GetValue(_))
152       .WillOnce(DoAll(SetArgPointee<0>(test_option), Return(0)));
153   PrepareControl();
154   ExpectValue(test_option);
155 }
156 
TEST_F(ControlTest,PopulateDynamicNoOptions)157 TEST_F(ControlTest, PopulateDynamicNoOptions) {
158   // Lack of options shouldn't change anything for PopulateDynamic.
159   use_options_ = false;
160   uint8_t test_option = 99;
161   EXPECT_CALL(*mock_delegate_, GetValue(_))
162       .WillOnce(DoAll(SetArgPointee<0>(test_option), Return(0)));
163   PrepareControl();
164   ExpectValue(test_option);
165 }
166 
TEST_F(ControlTest,PopulateDynamicUnreportedOptions)167 TEST_F(ControlTest, PopulateDynamicUnreportedOptions) {
168   // Lack of reported options shouldn't change anything for PopulateDynamic.
169   report_options_ = false;
170   uint8_t test_option = 99;
171   EXPECT_CALL(*mock_delegate_, GetValue(_))
172       .WillOnce(DoAll(SetArgPointee<0>(test_option), Return(0)));
173   PrepareControl();
174   ExpectValue(test_option);
175 }
176 
TEST_F(ControlTest,PopulateDynamicFail)177 TEST_F(ControlTest, PopulateDynamicFail) {
178   int err = -99;
179   EXPECT_CALL(*mock_delegate_, GetValue(_)).WillOnce(Return(err));
180   PrepareControl();
181 
182   android::CameraMetadata metadata;
183   EXPECT_EQ(control_->PopulateDynamicFields(&metadata), err);
184 
185   // Should not have added an entry.
186   EXPECT_TRUE(metadata.isEmpty());
187 }
188 
TEST_F(ControlTest,PopulateTemplate)189 TEST_F(ControlTest, PopulateTemplate) {
190   int template_type = 3;
191   uint8_t default_value = 123;
192   EXPECT_CALL(*mock_options_, DefaultValueForTemplate(template_type, _))
193       .WillOnce(DoAll(SetArgPointee<1>(default_value), Return(0)));
194   PrepareControl();
195 
196   android::CameraMetadata metadata;
197   EXPECT_EQ(control_->PopulateTemplateRequest(template_type, &metadata), 0);
198   ExpectMetadataEq(metadata, delegate_tag_, default_value);
199 }
200 
TEST_F(ControlTest,PopulateTemplateFail)201 TEST_F(ControlTest, PopulateTemplateFail) {
202   int template_type = 3;
203   int err = 10;
204   EXPECT_CALL(*mock_options_, DefaultValueForTemplate(template_type, _))
205       .WillOnce(Return(err));
206   PrepareControl();
207 
208   android::CameraMetadata metadata;
209   EXPECT_EQ(control_->PopulateTemplateRequest(template_type, &metadata), err);
210 }
211 
TEST_F(ControlTest,PopulateTemplateOptionless)212 TEST_F(ControlTest, PopulateTemplateOptionless) {
213   use_options_ = false;
214   int template_type = 3;
215   uint8_t value = 12;
216   // Should use delegate instead of options if no options.
217   EXPECT_CALL(*mock_delegate_, GetValue(_))
218       .WillOnce(DoAll(SetArgPointee<0>(value), Return(0)));
219   PrepareControl();
220 
221   android::CameraMetadata metadata;
222   EXPECT_EQ(control_->PopulateTemplateRequest(template_type, &metadata), 0);
223   ExpectMetadataEq(metadata, delegate_tag_, value);
224 }
225 
TEST_F(ControlTest,PopulateTemplateOptionlessFail)226 TEST_F(ControlTest, PopulateTemplateOptionlessFail) {
227   use_options_ = false;
228   int template_type = 3;
229   int err = 10;
230   // Should use delegate instead of options if no options.
231   EXPECT_CALL(*mock_delegate_, GetValue(_)).WillOnce(Return(err));
232   PrepareControl();
233 
234   android::CameraMetadata metadata;
235   EXPECT_EQ(control_->PopulateTemplateRequest(template_type, &metadata), err);
236 }
237 
TEST_F(ControlTest,PopulateTemplateUnreportedOptions)238 TEST_F(ControlTest, PopulateTemplateUnreportedOptions) {
239   report_options_ = false;
240   int template_type = 3;
241   uint8_t default_value = 123;
242   // Unreported options should behave just like reported ones for templating.
243   EXPECT_CALL(*mock_options_, DefaultValueForTemplate(template_type, _))
244       .WillOnce(DoAll(SetArgPointee<1>(default_value), Return(0)));
245   PrepareControl();
246 
247   android::CameraMetadata metadata;
248   EXPECT_EQ(control_->PopulateTemplateRequest(template_type, &metadata), 0);
249   ExpectMetadataEq(metadata, delegate_tag_, default_value);
250 }
251 
TEST_F(ControlTest,PopulateTemplateUnreportedOptionsFail)252 TEST_F(ControlTest, PopulateTemplateUnreportedOptionsFail) {
253   report_options_ = false;
254   int template_type = 3;
255   int err = 10;
256   // Unreported options should behave just like reported ones for templating.
257   EXPECT_CALL(*mock_options_, DefaultValueForTemplate(template_type, _))
258       .WillOnce(Return(err));
259   PrepareControl();
260 
261   android::CameraMetadata metadata;
262   EXPECT_EQ(control_->PopulateTemplateRequest(template_type, &metadata), err);
263 }
264 
TEST_F(ControlTest,SupportsRequest)265 TEST_F(ControlTest, SupportsRequest) {
266   android::CameraMetadata metadata;
267   uint8_t test_option = 123;
268   ASSERT_EQ(UpdateMetadata(&metadata, delegate_tag_, test_option), 0);
269 
270   EXPECT_CALL(*mock_options_, IsSupported(test_option)).WillOnce(Return(true));
271   PrepareControl();
272 
273   EXPECT_EQ(control_->SupportsRequestValues(metadata), true);
274 }
275 
TEST_F(ControlTest,SupportsRequestNoOptions)276 TEST_F(ControlTest, SupportsRequestNoOptions) {
277   use_options_ = false;
278   android::CameraMetadata metadata;
279   uint8_t test_option = 123;
280   ASSERT_EQ(UpdateMetadata(&metadata, delegate_tag_, test_option), 0);
281   PrepareControl();
282 
283   EXPECT_EQ(control_->SupportsRequestValues(metadata), true);
284 }
285 
TEST_F(ControlTest,SupportsRequestUnreportedOptions)286 TEST_F(ControlTest, SupportsRequestUnreportedOptions) {
287   report_options_ = false;
288   android::CameraMetadata metadata;
289   uint8_t test_option = 123;
290   ASSERT_EQ(UpdateMetadata(&metadata, delegate_tag_, test_option), 0);
291 
292   EXPECT_CALL(*mock_options_, IsSupported(test_option)).WillOnce(Return(true));
293   PrepareControl();
294 
295   EXPECT_EQ(control_->SupportsRequestValues(metadata), true);
296 }
297 
TEST_F(ControlTest,SupportsRequestFail)298 TEST_F(ControlTest, SupportsRequestFail) {
299   android::CameraMetadata metadata;
300   uint8_t test_option = 123;
301   ASSERT_EQ(UpdateMetadata(&metadata, delegate_tag_, test_option), 0);
302 
303   EXPECT_CALL(*mock_options_, IsSupported(test_option)).WillOnce(Return(false));
304   PrepareControl();
305 
306   EXPECT_EQ(control_->SupportsRequestValues(metadata), false);
307 }
308 
TEST_F(ControlTest,SupportsRequestUnreportedOptionsFail)309 TEST_F(ControlTest, SupportsRequestUnreportedOptionsFail) {
310   report_options_ = false;
311   android::CameraMetadata metadata;
312   uint8_t test_option = 123;
313   ASSERT_EQ(UpdateMetadata(&metadata, delegate_tag_, test_option), 0);
314 
315   // Unreported options should still be checked against.
316   EXPECT_CALL(*mock_options_, IsSupported(test_option)).WillOnce(Return(false));
317   PrepareControl();
318 
319   EXPECT_EQ(control_->SupportsRequestValues(metadata), false);
320 }
321 
TEST_F(ControlTest,SupportsRequestInvalidNumber)322 TEST_F(ControlTest, SupportsRequestInvalidNumber) {
323   // Start with a request for multiple values.
324   android::CameraMetadata metadata;
325   std::vector<uint8_t> test_data = {1, 2, 3};
326   ASSERT_EQ(UpdateMetadata(&metadata, delegate_tag_, test_data), 0);
327   PrepareControl();
328   EXPECT_EQ(control_->SupportsRequestValues(metadata), false);
329 }
330 
TEST_F(ControlTest,SupportsRequestInvalidNumberNoOptions)331 TEST_F(ControlTest, SupportsRequestInvalidNumberNoOptions) {
332   use_options_ = false;
333   // Start with a request for multiple values.
334   android::CameraMetadata metadata;
335   std::vector<uint8_t> test_data = {1, 2, 3};
336   ASSERT_EQ(UpdateMetadata(&metadata, delegate_tag_, test_data), 0);
337   PrepareControl();
338   // Not having any explicit options does not exempt a control
339   // from requiring the right number of values.
340   EXPECT_EQ(control_->SupportsRequestValues(metadata), false);
341 }
342 
TEST_F(ControlTest,SupportsRequestEmpty)343 TEST_F(ControlTest, SupportsRequestEmpty) {
344   android::CameraMetadata metadata;
345   PrepareControl();
346   EXPECT_EQ(control_->SupportsRequestValues(metadata), true);
347 }
348 
TEST_F(ControlTest,SetRequest)349 TEST_F(ControlTest, SetRequest) {
350   android::CameraMetadata metadata;
351   uint8_t test_option = 123;
352   ASSERT_EQ(UpdateMetadata(&metadata, delegate_tag_, test_option), 0);
353 
354   Expectation validation_check =
355       EXPECT_CALL(*mock_options_, IsSupported(test_option))
356           .WillOnce(Return(true));
357   EXPECT_CALL(*mock_delegate_, SetValue(test_option))
358       .After(validation_check)
359       .WillOnce(Return(0));
360   PrepareControl();
361 
362   // Make the request.
363   ASSERT_EQ(control_->SetRequestValues(metadata), 0);
364 }
365 
TEST_F(ControlTest,SetRequestNoOptions)366 TEST_F(ControlTest, SetRequestNoOptions) {
367   use_options_ = false;
368   android::CameraMetadata metadata;
369   uint8_t test_option = 123;
370   ASSERT_EQ(UpdateMetadata(&metadata, delegate_tag_, test_option), 0);
371 
372   // No options, no validation check.
373   EXPECT_CALL(*mock_delegate_, SetValue(test_option)).WillOnce(Return(0));
374   PrepareControl();
375 
376   // Make the request.
377   ASSERT_EQ(control_->SetRequestValues(metadata), 0);
378 }
379 
TEST_F(ControlTest,SetRequestUnreportedOptions)380 TEST_F(ControlTest, SetRequestUnreportedOptions) {
381   report_options_ = false;
382   android::CameraMetadata metadata;
383   uint8_t test_option = 123;
384   ASSERT_EQ(UpdateMetadata(&metadata, delegate_tag_, test_option), 0);
385 
386   // Unreported options still get a validation check.
387   Expectation validation_check =
388       EXPECT_CALL(*mock_options_, IsSupported(test_option))
389           .WillOnce(Return(true));
390   EXPECT_CALL(*mock_delegate_, SetValue(test_option))
391       .After(validation_check)
392       .WillOnce(Return(0));
393   PrepareControl();
394 
395   // Make the request.
396   ASSERT_EQ(control_->SetRequestValues(metadata), 0);
397 }
398 
TEST_F(ControlTest,SetRequestSettingFail)399 TEST_F(ControlTest, SetRequestSettingFail) {
400   android::CameraMetadata metadata;
401   uint8_t test_option = 123;
402   ASSERT_EQ(UpdateMetadata(&metadata, delegate_tag_, test_option), 0);
403 
404   int err = 99;
405   Expectation validation_check =
406       EXPECT_CALL(*mock_options_, IsSupported(test_option))
407           .WillOnce(Return(true));
408   EXPECT_CALL(*mock_delegate_, SetValue(test_option))
409       .After(validation_check)
410       .WillOnce(Return(err));
411   PrepareControl();
412 
413   EXPECT_EQ(control_->SetRequestValues(metadata), err);
414 }
415 
TEST_F(ControlTest,SetRequestValidationFail)416 TEST_F(ControlTest, SetRequestValidationFail) {
417   android::CameraMetadata metadata;
418   uint8_t test_option = 123;
419   ASSERT_EQ(UpdateMetadata(&metadata, delegate_tag_, test_option), 0);
420 
421   EXPECT_CALL(*mock_options_, IsSupported(test_option)).WillOnce(Return(false));
422   PrepareControl();
423 
424   EXPECT_EQ(control_->SetRequestValues(metadata), -EINVAL);
425 }
426 
TEST_F(ControlTest,SetRequestInvalidNumber)427 TEST_F(ControlTest, SetRequestInvalidNumber) {
428   // Start with a request for multiple values.
429   android::CameraMetadata metadata;
430   std::vector<uint8_t> test_data = {1, 2, 3};
431   ASSERT_EQ(UpdateMetadata(&metadata, delegate_tag_, test_data), 0);
432 
433   PrepareControl();
434   EXPECT_EQ(control_->SetRequestValues(metadata), -EINVAL);
435 }
436 
TEST_F(ControlTest,SetRequestInvalidNumberNoOptions)437 TEST_F(ControlTest, SetRequestInvalidNumberNoOptions) {
438   use_options_ = false;
439   // Start with a request for multiple values.
440   android::CameraMetadata metadata;
441   std::vector<uint8_t> test_data = {1, 2, 3};
442   ASSERT_EQ(UpdateMetadata(&metadata, delegate_tag_, test_data), 0);
443 
444   PrepareControl();
445   // Not having explicit options does not change that an incorrect
446   // number of values is invalid.
447   EXPECT_EQ(control_->SetRequestValues(metadata), -EINVAL);
448 }
449 
TEST_F(ControlTest,SetRequestEmpty)450 TEST_F(ControlTest, SetRequestEmpty) {
451   // Should do nothing.
452   android::CameraMetadata metadata;
453   PrepareControl();
454   EXPECT_EQ(control_->SetRequestValues(metadata), 0);
455 }
456 
457 }  // namespace v4l2_camera_hal
458