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