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