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