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 "menu_control_options.h"
18 
19 #include <memory>
20 
21 #include <gmock/gmock.h>
22 #include <gtest/gtest.h>
23 #include <hardware/camera3.h>
24 
25 #include "default_option_delegate_mock.h"
26 
27 using testing::Return;
28 using testing::SetArgPointee;
29 using testing::Test;
30 using testing::_;
31 
32 namespace v4l2_camera_hal {
33 
34 class MenuControlOptionsTest : public Test {
35  protected:
SetUp()36   virtual void SetUp() {
37     mock_defaults_.reset(new DefaultOptionDelegateMock<int>());
38     dut_.reset(new MenuControlOptions<int>(options_, mock_defaults_));
39   }
40 
41   std::unique_ptr<MenuControlOptions<int>> dut_;
42   const std::vector<int> options_{1, 10, 19, 30};
43   std::shared_ptr<DefaultOptionDelegateMock<int>> mock_defaults_;
44 };
45 
TEST_F(MenuControlOptionsTest,MetadataRepresentation)46 TEST_F(MenuControlOptionsTest, MetadataRepresentation) {
47   // Technically order doesn't matter, but this is faster to write,
48   // and still passes.
49   EXPECT_EQ(dut_->MetadataRepresentation(), options_);
50 }
51 
TEST_F(MenuControlOptionsTest,IsSupported)52 TEST_F(MenuControlOptionsTest, IsSupported) {
53   for (auto option : options_) {
54     EXPECT_TRUE(dut_->IsSupported(option));
55   }
56   // And at least one unsupported.
57   EXPECT_FALSE(dut_->IsSupported(99));
58 }
59 
TEST_F(MenuControlOptionsTest,DelegateDefaultValue)60 TEST_F(MenuControlOptionsTest, DelegateDefaultValue) {
61   int template_index = 3;
62   int expected = options_[2];
63   ASSERT_TRUE(dut_->IsSupported(expected));
64   EXPECT_CALL(*mock_defaults_, DefaultValueForTemplate(template_index, _))
65       .WillOnce(DoAll(SetArgPointee<1>(expected), Return(true)));
66   int actual = expected - 1;
67   EXPECT_EQ(dut_->DefaultValueForTemplate(template_index, &actual), 0);
68   EXPECT_EQ(actual, expected);
69 }
70 
TEST_F(MenuControlOptionsTest,InvalidDelegateDefaultValue)71 TEST_F(MenuControlOptionsTest, InvalidDelegateDefaultValue) {
72   // -1 is not a supported option.
73   int template_index = 3;
74   int default_val = -1;
75   ASSERT_FALSE(dut_->IsSupported(default_val));
76 
77   EXPECT_CALL(*mock_defaults_, DefaultValueForTemplate(template_index, _))
78       .WillOnce(DoAll(SetArgPointee<1>(default_val), Return(true)));
79 
80   int actual = default_val;
81   EXPECT_EQ(dut_->DefaultValueForTemplate(template_index, &actual), 0);
82   // Should just give any supported option instead.
83   EXPECT_TRUE(dut_->IsSupported(actual));
84 }
85 
TEST_F(MenuControlOptionsTest,NoDelegateDefaultValue)86 TEST_F(MenuControlOptionsTest, NoDelegateDefaultValue) {
87   int template_index = 3;
88   int actual = -1;
89   ASSERT_FALSE(dut_->IsSupported(actual));
90 
91   // Have delegate error.
92   EXPECT_CALL(*mock_defaults_, DefaultValueForTemplate(template_index, _))
93       .WillOnce(Return(false));
94 
95   // Should still give *some* supported value.
96   EXPECT_EQ(dut_->DefaultValueForTemplate(template_index, &actual), 0);
97   EXPECT_TRUE(dut_->IsSupported(actual));
98 }
99 
TEST_F(MenuControlOptionsTest,NoDefaultValue)100 TEST_F(MenuControlOptionsTest, NoDefaultValue) {
101   // Invalid options don't have a valid default.
102   MenuControlOptions<int> bad_options({}, mock_defaults_);
103   for (int i = 1; i < CAMERA3_TEMPLATE_COUNT; ++i) {
104     int value = -1;
105     EXPECT_EQ(bad_options.DefaultValueForTemplate(i, &value), -ENODEV);
106   }
107 }
108 
109 }  // namespace v4l2_camera_hal
110