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 "map_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 MapConverterTest : public Test {
32 protected:
SetUp()33 virtual void SetUp() {
34 converter_.reset(new ConverterInterfaceMock<int, int32_t>());
35 dut_.reset(new MapConverter<int, int32_t, int32_t>(converter_, map_));
36 }
37
ExpectConvertToV4L2(int32_t converted,int32_t expected)38 virtual void ExpectConvertToV4L2(int32_t converted, int32_t expected) {
39 int initial = 99;
40 EXPECT_CALL(*converter_, MetadataToV4L2(initial, _))
41 .WillOnce(DoAll(SetArgPointee<1>(converted), Return(0)));
42
43 int32_t actual = expected + 1; // Initialize to non-expected value.
44 ASSERT_EQ(dut_->MetadataToV4L2(initial, &actual), 0);
45 EXPECT_EQ(actual, expected);
46 }
47
48 std::shared_ptr<ConverterInterfaceMock<int, int32_t>> converter_;
49 std::unique_ptr<MapConverter<int, int32_t, int32_t>> dut_;
50
51 const std::map<int32_t, int32_t> map_{{10, 1}, {40, 4}, {20, 2}, {30, 3}};
52 };
53
TEST_F(MapConverterTest,NormalConversionToV4L2)54 TEST_F(MapConverterTest, NormalConversionToV4L2) {
55 // A value that matches the map perfectly.
56 auto kv = map_.begin();
57 ExpectConvertToV4L2(kv->first, kv->second);
58 }
59
TEST_F(MapConverterTest,RoundingDownConversionToV4L2)60 TEST_F(MapConverterTest, RoundingDownConversionToV4L2) {
61 // A value that's in range but not an exact key value.
62 auto kv = map_.begin();
63 ExpectConvertToV4L2(kv->first + 1, kv->second);
64 }
65
TEST_F(MapConverterTest,RoundingUpConversionToV4L2)66 TEST_F(MapConverterTest, RoundingUpConversionToV4L2) {
67 // A value that's in range but not an exact key value.
68 auto kv = map_.begin();
69 ++kv;
70 ExpectConvertToV4L2(kv->first - 1, kv->second);
71 }
72
TEST_F(MapConverterTest,ClampUpConversionToV4L2)73 TEST_F(MapConverterTest, ClampUpConversionToV4L2) {
74 // A value that's below range.
75 auto kv = map_.begin();
76 ExpectConvertToV4L2(kv->first - 1, kv->second);
77 }
78
TEST_F(MapConverterTest,ClampDownConversionToV4L2)79 TEST_F(MapConverterTest, ClampDownConversionToV4L2) {
80 // A value that's above range (even after fitting to step).
81 auto kv = map_.rbegin();
82 ExpectConvertToV4L2(kv->first + 1, kv->second);
83 }
84
TEST_F(MapConverterTest,ConversionErrorToV4L2)85 TEST_F(MapConverterTest, ConversionErrorToV4L2) {
86 int initial = 99;
87 int err = -99;
88 EXPECT_CALL(*converter_, MetadataToV4L2(initial, _)).WillOnce(Return(err));
89
90 int32_t unused;
91 EXPECT_EQ(dut_->MetadataToV4L2(initial, &unused), err);
92 }
93
TEST_F(MapConverterTest,NormalConversionToMetadata)94 TEST_F(MapConverterTest, NormalConversionToMetadata) {
95 auto kv = map_.begin();
96 int expected = 99;
97 EXPECT_CALL(*converter_, V4L2ToMetadata(kv->first, _))
98 .WillOnce(DoAll(SetArgPointee<1>(expected), Return(0)));
99
100 int actual = expected + 1; // Initialize to non-expected value.
101 ASSERT_EQ(dut_->V4L2ToMetadata(kv->second, &actual), 0);
102 EXPECT_EQ(actual, expected);
103 }
104
TEST_F(MapConverterTest,NotFoundConversionToMetadata)105 TEST_F(MapConverterTest, NotFoundConversionToMetadata) {
106 int unused;
107 ASSERT_EQ(dut_->V4L2ToMetadata(100, &unused), -EINVAL);
108 }
109
110 } // namespace v4l2_camera_hal
111