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