xref: /aosp_15_r20/hardware/libhardware/modules/camera/3_4/metadata/v4l2_control_delegate_test.cpp (revision e01b6f769022e40d0923dee176e8dc7cd1d52984)
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 "v4l2_control_delegate.h"
18 
19 #include <gmock/gmock.h>
20 #include <gtest/gtest.h>
21 #include "converter_interface_mock.h"
22 #include "v4l2_wrapper_mock.h"
23 
24 using testing::DoAll;
25 using testing::Return;
26 using testing::SetArgPointee;
27 using testing::Test;
28 using testing::_;
29 
30 namespace v4l2_camera_hal {
31 
32 class V4L2ControlDelegateTest : public Test {
33  protected:
SetUp()34   virtual void SetUp() {
35     mock_device_.reset(new V4L2WrapperMock());
36     mock_converter_.reset(new ConverterInterfaceMock<uint8_t, int32_t>());
37     dut_.reset(new V4L2ControlDelegate<uint8_t>(
38         mock_device_, control_id_, mock_converter_));
39   }
40 
41   std::unique_ptr<V4L2ControlDelegate<uint8_t>> dut_;
42   std::shared_ptr<V4L2WrapperMock> mock_device_;
43   std::shared_ptr<ConverterInterfaceMock<uint8_t, int32_t>> mock_converter_;
44   const int control_id_ = 123;
45 };
46 
TEST_F(V4L2ControlDelegateTest,GetSuccess)47 TEST_F(V4L2ControlDelegateTest, GetSuccess) {
48   int32_t device_result = 99;
49   uint8_t conversion_result = 10;
50   EXPECT_CALL(*mock_device_, GetControl(control_id_, _))
51       .WillOnce(DoAll(SetArgPointee<1>(device_result), Return(0)));
52   EXPECT_CALL(*mock_converter_, V4L2ToMetadata(device_result, _))
53       .WillOnce(DoAll(SetArgPointee<1>(conversion_result), Return(0)));
54 
55   uint8_t actual = conversion_result + 1;  // Something incorrect.
56   ASSERT_EQ(dut_->GetValue(&actual), 0);
57   EXPECT_EQ(actual, conversion_result);
58 }
59 
TEST_F(V4L2ControlDelegateTest,GetConverterFailure)60 TEST_F(V4L2ControlDelegateTest, GetConverterFailure) {
61   int32_t device_result = 99;
62   EXPECT_CALL(*mock_device_, GetControl(control_id_, _))
63       .WillOnce(DoAll(SetArgPointee<1>(device_result), Return(0)));
64   int err = -99;
65   EXPECT_CALL(*mock_converter_, V4L2ToMetadata(device_result, _))
66       .WillOnce(Return(err));
67 
68   uint8_t unused = 1;
69   ASSERT_EQ(dut_->GetValue(&unused), err);
70 }
71 
TEST_F(V4L2ControlDelegateTest,GetDeviceFailure)72 TEST_F(V4L2ControlDelegateTest, GetDeviceFailure) {
73   int err = -99;
74   EXPECT_CALL(*mock_device_, GetControl(control_id_, _)).WillOnce(Return(err));
75 
76   uint8_t unused = 1;
77   ASSERT_EQ(dut_->GetValue(&unused), err);
78 }
79 
TEST_F(V4L2ControlDelegateTest,SetSuccess)80 TEST_F(V4L2ControlDelegateTest, SetSuccess) {
81   uint8_t input = 10;
82   int32_t conversion_result = 99;
83   EXPECT_CALL(*mock_converter_, MetadataToV4L2(input, _))
84       .WillOnce(DoAll(SetArgPointee<1>(conversion_result), Return(0)));
85   EXPECT_CALL(*mock_device_, SetControl(control_id_, conversion_result, _))
86       .WillOnce(Return(0));
87 
88   ASSERT_EQ(dut_->SetValue(input), 0);
89 }
90 
TEST_F(V4L2ControlDelegateTest,SetConverterFailure)91 TEST_F(V4L2ControlDelegateTest, SetConverterFailure) {
92   uint8_t input = 10;
93   int err = 12;
94   EXPECT_CALL(*mock_converter_, MetadataToV4L2(input, _)).WillOnce(Return(err));
95   ASSERT_EQ(dut_->SetValue(input), err);
96 }
97 
TEST_F(V4L2ControlDelegateTest,SetDeviceFailure)98 TEST_F(V4L2ControlDelegateTest, SetDeviceFailure) {
99   uint8_t input = 10;
100   int32_t conversion_result = 99;
101   EXPECT_CALL(*mock_converter_, MetadataToV4L2(input, _))
102       .WillOnce(DoAll(SetArgPointee<1>(conversion_result), Return(0)));
103   int err = 66;
104   EXPECT_CALL(*mock_device_, SetControl(control_id_, conversion_result, _))
105       .WillOnce(Return(err));
106 
107   ASSERT_EQ(dut_->SetValue(input), err);
108 }
109 
110 }  // namespace v4l2_camera_hal
111