blob: b073a1d25a26db252ad2a450704c2e58ad86ad69 [file] [log] [blame]
Peter Qiuc0beca52015-09-03 11:25:46 -07001//
2// Copyright (C) 2014 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//
Paul Stewart0153cf02014-06-02 11:53:36 -070016
17#include "shill/property_observer.h"
18
19#include <string>
20
21#include <base/bind.h>
22#include <gmock/gmock.h>
23#include <gtest/gtest.h>
24
25#include "shill/accessor_interface.h"
26#include "shill/error.h"
27
28using base::Bind;
29using base::Unretained;
30using testing::_;
31using testing::DoAll;
32using testing::Mock;
33using testing::Return;
34
35namespace shill {
36
37class TestPropertyAccessor : public AccessorInterface<bool> {
38 public:
Paul Stewart3b30ca52015-06-16 13:13:10 -070039 MOCK_METHOD1(Clear, void(Error* error));
40 MOCK_METHOD1(Get, bool(Error* error));
41 MOCK_METHOD2(Set, bool(const bool& value, Error* error));
Paul Stewart0153cf02014-06-02 11:53:36 -070042};
43
44class PropertyObserverTest : public testing::Test {
45 public:
46 PropertyObserverTest()
47 : test_accessor_(new TestPropertyAccessor()),
48 bool_accessor_(test_accessor_) {}
49 virtual ~PropertyObserverTest() {}
50
Paul Stewart3b30ca52015-06-16 13:13:10 -070051 MOCK_METHOD1(TestCallback, void(const bool& value));
Paul Stewart0153cf02014-06-02 11:53:36 -070052
53 // Invoked method during expectations.
Paul Stewart3b30ca52015-06-16 13:13:10 -070054 void SetError(Error* error) {
Paul Stewart0153cf02014-06-02 11:53:36 -070055 error->Populate(Error::kPermissionDenied);
56 }
57
58 protected:
Paul Stewart3b30ca52015-06-16 13:13:10 -070059 bool GetSavedValue(const PropertyObserver<bool>& observer) {
Paul Stewart0153cf02014-06-02 11:53:36 -070060 return observer.saved_value_;
61 }
62
Paul Stewart3b30ca52015-06-16 13:13:10 -070063 TestPropertyAccessor* test_accessor_;
Paul Stewart0153cf02014-06-02 11:53:36 -070064 BoolAccessor bool_accessor_; // Owns reference to |test_accessor_|.
65};
66
67TEST_F(PropertyObserverTest, Callback) {
68 EXPECT_CALL(*test_accessor_, Get(_)).WillOnce(Return(true));
69 EXPECT_CALL(*this, TestCallback(_)).Times(0);
70 PropertyObserver<bool> observer(bool_accessor_,
71 Bind(&PropertyObserverTest::TestCallback,
72 Unretained(this)));
73 EXPECT_TRUE(GetSavedValue(observer));
74 Mock::VerifyAndClearExpectations(test_accessor_);
75
76 // Accessor returns an error.
77 EXPECT_CALL(*test_accessor_, Get(_))
78 .WillOnce(DoAll(Invoke(this, &PropertyObserverTest::SetError),
79 Return(false)));
80 observer.Update();
81
82 // Value remains unchanged.
83 EXPECT_CALL(*test_accessor_, Get(_)).WillOnce(Return(true));
84 observer.Update();
85 Mock::VerifyAndClearExpectations(test_accessor_);
86 Mock::VerifyAndClearExpectations(this);
87
88 // Value changes.
89 EXPECT_CALL(*test_accessor_, Get(_)).WillOnce(Return(false));
90 EXPECT_CALL(*this, TestCallback(false));
91 observer.Update();
92 EXPECT_FALSE(GetSavedValue(observer));
93 Mock::VerifyAndClearExpectations(test_accessor_);
94 Mock::VerifyAndClearExpectations(this);
95
96 // Value remains unchanged (false).
97 EXPECT_CALL(*test_accessor_, Get(_)).WillOnce(Return(false));
98 EXPECT_CALL(*this, TestCallback(_)).Times(0);
99 observer.Update();
100}
101
102} // namespace shill