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