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