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 "tagged_control_delegate.h"
18
19#include <gmock/gmock.h>
20#include <gtest/gtest.h>
21
22#include "control_delegate_interface_mock.h"
23
24using testing::Return;
25using testing::SetArgPointee;
26using testing::Test;
27using testing::_;
28
29namespace v4l2_camera_hal {
30
31class TaggedControlDelegateTest : public Test {
32 protected:
33  virtual void SetUp() {
34    mock_delegate_.reset(new ControlDelegateInterfaceMock<uint8_t>());
35    // Nullify dut so an error will be thrown if a test doesn't call PrepareDUT.
36    dut_.reset();
37  }
38
39  virtual void PrepareDUT() {
40    // Use this method after all the EXPECT_CALLs to pass ownership of the
41    // delegate
42    // to the device.
43    dut_.reset(
44        new TaggedControlDelegate<uint8_t>(tag_, std::move(mock_delegate_)));
45  }
46
47  std::unique_ptr<TaggedControlDelegate<uint8_t>> dut_;
48  std::unique_ptr<ControlDelegateInterfaceMock<uint8_t>> mock_delegate_;
49  const int32_t tag_ = 123;
50};
51
52TEST_F(TaggedControlDelegateTest, GetTag) {
53  PrepareDUT();
54  EXPECT_EQ(dut_->tag(), tag_);
55}
56
57TEST_F(TaggedControlDelegateTest, GetSuccess) {
58  uint8_t expected = 3;
59  EXPECT_CALL(*mock_delegate_, GetValue(_))
60      .WillOnce(DoAll(SetArgPointee<0>(expected), Return(0)));
61  PrepareDUT();
62  uint8_t actual = expected + 1;  // Initialize to an incorrect value.
63  ASSERT_EQ(dut_->GetValue(&actual), 0);
64  EXPECT_EQ(actual, expected);
65}
66
67TEST_F(TaggedControlDelegateTest, GetFailure) {
68  int err = 3;
69  EXPECT_CALL(*mock_delegate_, GetValue(_)).WillOnce(Return(err));
70  PrepareDUT();
71  uint8_t unused = 0;
72  ASSERT_EQ(dut_->GetValue(&unused), err);
73}
74
75TEST_F(TaggedControlDelegateTest, SetSuccess) {
76  uint8_t value = 3;
77  EXPECT_CALL(*mock_delegate_, SetValue(value)).WillOnce(Return(0));
78  PrepareDUT();
79  ASSERT_EQ(dut_->SetValue(value), 0);
80}
81
82TEST_F(TaggedControlDelegateTest, SetFailure) {
83  int err = 3;
84  uint8_t value = 12;
85  EXPECT_CALL(*mock_delegate_, SetValue(value)).WillOnce(Return(err));
86  PrepareDUT();
87  ASSERT_EQ(dut_->SetValue(value), err);
88}
89
90}  // namespace v4l2_camera_hal
91