xref: /aosp_15_r20/hardware/libhardware/modules/camera/3_4/metadata/state_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 "state.h"
18 
19 #include <camera/CameraMetadata.h>
20 #include <gmock/gmock.h>
21 #include <gtest/gtest.h>
22 
23 #include "metadata_common.h"
24 #include "state_delegate_interface_mock.h"
25 #include "test_common.h"
26 
27 using testing::AtMost;
28 using testing::DoAll;
29 using testing::Expectation;
30 using testing::Return;
31 using testing::SetArgPointee;
32 using testing::Test;
33 using testing::_;
34 
35 namespace v4l2_camera_hal {
36 
37 class StateTest : public Test {
38  protected:
SetUp()39   virtual void SetUp() {
40     mock_delegate_.reset(new StateDelegateInterfaceMock<uint8_t>());
41     // Nullify state so an error will be thrown if a test doesn't call
42     // PrepareState.
43     state_.reset();
44   }
45 
PrepareState()46   virtual void PrepareState() {
47     // Use this method after all the EXPECT_CALLs to pass ownership of the mocks
48     // to the device.
49     state_.reset(new State<uint8_t>(tag_, std::move(mock_delegate_)));
50   }
51 
52   std::unique_ptr<State<uint8_t>> state_;
53   std::unique_ptr<StateDelegateInterfaceMock<uint8_t>> mock_delegate_;
54 
55   // Need tag that matches the data type (uint8_t) being passed.
56   const int32_t tag_ = ANDROID_CONTROL_AF_STATE;
57 };
58 
TEST_F(StateTest,Tags)59 TEST_F(StateTest, Tags) {
60   PrepareState();
61   EXPECT_TRUE(state_->StaticTags().empty());
62   EXPECT_TRUE(state_->ControlTags().empty());
63   ASSERT_EQ(state_->DynamicTags().size(), 1u);
64   EXPECT_EQ(state_->DynamicTags()[0], tag_);
65 }
66 
TEST_F(StateTest,PopulateStatic)67 TEST_F(StateTest, PopulateStatic) {
68   PrepareState();
69   android::CameraMetadata metadata;
70   ASSERT_EQ(state_->PopulateStaticFields(&metadata), 0);
71   EXPECT_TRUE(metadata.isEmpty());
72 }
73 
TEST_F(StateTest,PopulateDynamic)74 TEST_F(StateTest, PopulateDynamic) {
75   uint8_t expected = 99;
76   EXPECT_CALL(*mock_delegate_, GetValue(_))
77       .WillOnce(DoAll(SetArgPointee<0>(expected), Return(0)));
78 
79   PrepareState();
80 
81   android::CameraMetadata metadata;
82   ASSERT_EQ(state_->PopulateDynamicFields(&metadata), 0);
83   EXPECT_EQ(metadata.entryCount(), 1u);
84   ExpectMetadataEq(metadata, tag_, expected);
85 }
86 
TEST_F(StateTest,PopulateDynamicFail)87 TEST_F(StateTest, PopulateDynamicFail) {
88   int err = 123;
89   EXPECT_CALL(*mock_delegate_, GetValue(_)).WillOnce(Return(err));
90 
91   PrepareState();
92 
93   android::CameraMetadata metadata;
94   ASSERT_EQ(state_->PopulateDynamicFields(&metadata), err);
95 }
96 
TEST_F(StateTest,PopulateTemplate)97 TEST_F(StateTest, PopulateTemplate) {
98   int template_type = 3;
99   PrepareState();
100   android::CameraMetadata metadata;
101   ASSERT_EQ(state_->PopulateTemplateRequest(template_type, &metadata), 0);
102   EXPECT_TRUE(metadata.isEmpty());
103 }
104 
TEST_F(StateTest,SupportsRequest)105 TEST_F(StateTest, SupportsRequest) {
106   PrepareState();
107   android::CameraMetadata metadata;
108   EXPECT_TRUE(state_->SupportsRequestValues(metadata));
109 }
110 
TEST_F(StateTest,SetRequest)111 TEST_F(StateTest, SetRequest) {
112   PrepareState();
113   android::CameraMetadata metadata;
114   ASSERT_EQ(state_->SetRequestValues(metadata), 0);
115 }
116 
117 }  // namespace v4l2_camera_hal
118