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 "slider_control_options.h"
18 
19 #include <memory>
20 
21 #include <gmock/gmock.h>
22 #include <gtest/gtest.h>
23 #include <hardware/camera3.h>
24 #include "default_option_delegate_mock.h"
25 
26 using testing::Return;
27 using testing::SetArgPointee;
28 using testing::Test;
29 using testing::_;
30 
31 namespace v4l2_camera_hal {
32 
33 class SliderControlOptionsTest : public Test {
34  protected:
SetUp()35   virtual void SetUp() {
36     mock_defaults_.reset(new DefaultOptionDelegateMock<int>());
37     dut_.reset(new SliderControlOptions<int>(min_, max_, mock_defaults_));
38   }
39 
40   std::unique_ptr<SliderControlOptions<int>> dut_;
41   std::shared_ptr<DefaultOptionDelegateMock<int>> mock_defaults_;
42   const int min_ = 1;
43   const int max_ = 10;
44 };
45 
TEST_F(SliderControlOptionsTest,MetadataRepresentation)46 TEST_F(SliderControlOptionsTest, MetadataRepresentation) {
47   // Technically order doesn't matter, but this is faster to write,
48   // and still passes.
49   std::vector<int> expected{min_, max_};
50   EXPECT_EQ(dut_->MetadataRepresentation(), expected);
51 }
52 
TEST_F(SliderControlOptionsTest,IsSupported)53 TEST_F(SliderControlOptionsTest, IsSupported) {
54   for (int i = min_; i <= max_; ++i) {
55     EXPECT_TRUE(dut_->IsSupported(i));
56   }
57   // Out of range unsupported.
58   EXPECT_FALSE(dut_->IsSupported(min_ - 1));
59   EXPECT_FALSE(dut_->IsSupported(max_ + 1));
60 }
61 
TEST_F(SliderControlOptionsTest,DelegateDefaultValue)62 TEST_F(SliderControlOptionsTest, DelegateDefaultValue) {
63   int template_index = 3;
64   int expected = max_ - 1;
65   ASSERT_TRUE(dut_->IsSupported(expected));
66   EXPECT_CALL(*mock_defaults_, DefaultValueForTemplate(template_index, _))
67       .WillOnce(DoAll(SetArgPointee<1>(expected), Return(true)));
68   int actual = expected - 1;
69   EXPECT_EQ(dut_->DefaultValueForTemplate(template_index, &actual), 0);
70   EXPECT_EQ(actual, expected);
71 }
72 
TEST_F(SliderControlOptionsTest,LowDelegateDefaultValue)73 TEST_F(SliderControlOptionsTest, LowDelegateDefaultValue) {
74   int template_index = 3;
75   // min - 1 is below the valid range.
76   int default_val = min_ - 1;
77   // Should get bumped up into range.
78   int expected = min_;
79   ASSERT_FALSE(dut_->IsSupported(default_val));
80   ASSERT_TRUE(dut_->IsSupported(expected));
81 
82   EXPECT_CALL(*mock_defaults_, DefaultValueForTemplate(template_index, _))
83       .WillOnce(DoAll(SetArgPointee<1>(default_val), Return(true)));
84   int actual = default_val;
85   EXPECT_EQ(dut_->DefaultValueForTemplate(template_index, &actual), 0);
86   EXPECT_EQ(actual, expected);
87 }
88 
TEST_F(SliderControlOptionsTest,HighDelegateDefaultValue)89 TEST_F(SliderControlOptionsTest, HighDelegateDefaultValue) {
90   int template_index = 3;
91   // max + 1 is above the valid range.
92   int default_val = max_ + 1;
93   // Should get bumped down into range.
94   int expected = max_;
95   ASSERT_FALSE(dut_->IsSupported(default_val));
96   ASSERT_TRUE(dut_->IsSupported(expected));
97 
98   EXPECT_CALL(*mock_defaults_, DefaultValueForTemplate(template_index, _))
99       .WillOnce(DoAll(SetArgPointee<1>(default_val), Return(true)));
100   int actual = default_val;
101   EXPECT_EQ(dut_->DefaultValueForTemplate(template_index, &actual), 0);
102   EXPECT_EQ(actual, expected);
103 }
104 
TEST_F(SliderControlOptionsTest,NoDelegateDefaultValue)105 TEST_F(SliderControlOptionsTest, NoDelegateDefaultValue) {
106   int template_index = 3;
107   int actual = min_ - 1;
108   ASSERT_FALSE(dut_->IsSupported(actual));
109 
110   // Have delegate error.
111   EXPECT_CALL(*mock_defaults_, DefaultValueForTemplate(template_index, _))
112       .WillOnce(Return(false));
113 
114   // Should still give *some* supported value.
115   EXPECT_EQ(dut_->DefaultValueForTemplate(template_index, &actual), 0);
116   EXPECT_TRUE(dut_->IsSupported(actual));
117 }
118 
TEST_F(SliderControlOptionsTest,NoDefaultValue)119 TEST_F(SliderControlOptionsTest, NoDefaultValue) {
120   // Invalid options don't have a valid default.
121   SliderControlOptions<int> bad_options(10, 9, mock_defaults_);  // min > max.
122   for (int i = 1; i < CAMERA3_TEMPLATE_COUNT; ++i) {
123     int value = -1;
124     EXPECT_EQ(bad_options.DefaultValueForTemplate(i, &value), -ENODEV);
125   }
126 }
127 
128 }  // namespace v4l2_camera_hal
129