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 "ranged_converter.h"
18
19 #include <gmock/gmock.h>
20 #include <gtest/gtest.h>
21
22 #include "converter_interface_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 RangedConverterTest : public Test {
32 protected:
SetUp()33 virtual void SetUp() {
34 converter_.reset(new ConverterInterfaceMock<int, int32_t>());
35 dut_.reset(
36 new RangedConverter<int, int32_t>(converter_, min_, max_, step_));
37 }
38
ExpectConvert(int32_t converted,int32_t expected)39 virtual void ExpectConvert(int32_t converted, int32_t expected) {
40 int initial = 99;
41 EXPECT_CALL(*converter_, MetadataToV4L2(initial, _))
42 .WillOnce(DoAll(SetArgPointee<1>(converted), Return(0)));
43
44 int32_t actual = expected + 1; // Initialize to non-expected value.
45 ASSERT_EQ(dut_->MetadataToV4L2(initial, &actual), 0);
46 EXPECT_EQ(actual, expected);
47 }
48
49 std::shared_ptr<ConverterInterfaceMock<int, int32_t>> converter_;
50 std::unique_ptr<RangedConverter<int, int32_t>> dut_;
51
52 const int32_t min_ = -11;
53 const int32_t max_ = 10;
54 const int32_t step_ = 3;
55 };
56
TEST_F(RangedConverterTest,NormalConversion)57 TEST_F(RangedConverterTest, NormalConversion) {
58 // A value that's in range and on step.
59 ExpectConvert(max_ - step_, max_ - step_);
60 }
61
TEST_F(RangedConverterTest,RoundingConversion)62 TEST_F(RangedConverterTest, RoundingConversion) {
63 // A value that's in range but off step.
64 ExpectConvert(max_ - step_ + 1, max_ - step_);
65 }
66
TEST_F(RangedConverterTest,ClampUpConversion)67 TEST_F(RangedConverterTest, ClampUpConversion) {
68 // A value that's below range.
69 ExpectConvert(min_ - 1, min_);
70 }
71
TEST_F(RangedConverterTest,ClampDownConversion)72 TEST_F(RangedConverterTest, ClampDownConversion) {
73 // A value that's above range (even after fitting to step).
74 ExpectConvert(max_ + step_, max_);
75 }
76
TEST_F(RangedConverterTest,ConversionError)77 TEST_F(RangedConverterTest, ConversionError) {
78 int initial = 99;
79 int err = -99;
80 EXPECT_CALL(*converter_, MetadataToV4L2(initial, _)).WillOnce(Return(err));
81
82 int32_t unused;
83 EXPECT_EQ(dut_->MetadataToV4L2(initial, &unused), err);
84 }
85
86 } // namespace v4l2_camera_hal
87