blob: 394603d71913c43740ab59d189ce8d6e27a21103 [file] [log] [blame]
Paul Stewart0153cf02014-06-02 11:53:36 -07001// Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "shill/property_observer.h"
6
7#include <string>
8
9#include <base/bind.h>
10#include <gmock/gmock.h>
11#include <gtest/gtest.h>
12
13#include "shill/accessor_interface.h"
14#include "shill/error.h"
15
16using base::Bind;
17using base::Unretained;
18using testing::_;
19using testing::DoAll;
20using testing::Mock;
21using testing::Return;
22
23namespace shill {
24
25class TestPropertyAccessor : public AccessorInterface<bool> {
26 public:
Paul Stewart3b30ca52015-06-16 13:13:10 -070027 MOCK_METHOD1(Clear, void(Error* error));
28 MOCK_METHOD1(Get, bool(Error* error));
29 MOCK_METHOD2(Set, bool(const bool& value, Error* error));
Paul Stewart0153cf02014-06-02 11:53:36 -070030};
31
32class PropertyObserverTest : public testing::Test {
33 public:
34 PropertyObserverTest()
35 : test_accessor_(new TestPropertyAccessor()),
36 bool_accessor_(test_accessor_) {}
37 virtual ~PropertyObserverTest() {}
38
Paul Stewart3b30ca52015-06-16 13:13:10 -070039 MOCK_METHOD1(TestCallback, void(const bool& value));
Paul Stewart0153cf02014-06-02 11:53:36 -070040
41 // Invoked method during expectations.
Paul Stewart3b30ca52015-06-16 13:13:10 -070042 void SetError(Error* error) {
Paul Stewart0153cf02014-06-02 11:53:36 -070043 error->Populate(Error::kPermissionDenied);
44 }
45
46 protected:
Paul Stewart3b30ca52015-06-16 13:13:10 -070047 bool GetSavedValue(const PropertyObserver<bool>& observer) {
Paul Stewart0153cf02014-06-02 11:53:36 -070048 return observer.saved_value_;
49 }
50
Paul Stewart3b30ca52015-06-16 13:13:10 -070051 TestPropertyAccessor* test_accessor_;
Paul Stewart0153cf02014-06-02 11:53:36 -070052 BoolAccessor bool_accessor_; // Owns reference to |test_accessor_|.
53};
54
55TEST_F(PropertyObserverTest, Callback) {
56 EXPECT_CALL(*test_accessor_, Get(_)).WillOnce(Return(true));
57 EXPECT_CALL(*this, TestCallback(_)).Times(0);
58 PropertyObserver<bool> observer(bool_accessor_,
59 Bind(&PropertyObserverTest::TestCallback,
60 Unretained(this)));
61 EXPECT_TRUE(GetSavedValue(observer));
62 Mock::VerifyAndClearExpectations(test_accessor_);
63
64 // Accessor returns an error.
65 EXPECT_CALL(*test_accessor_, Get(_))
66 .WillOnce(DoAll(Invoke(this, &PropertyObserverTest::SetError),
67 Return(false)));
68 observer.Update();
69
70 // Value remains unchanged.
71 EXPECT_CALL(*test_accessor_, Get(_)).WillOnce(Return(true));
72 observer.Update();
73 Mock::VerifyAndClearExpectations(test_accessor_);
74 Mock::VerifyAndClearExpectations(this);
75
76 // Value changes.
77 EXPECT_CALL(*test_accessor_, Get(_)).WillOnce(Return(false));
78 EXPECT_CALL(*this, TestCallback(false));
79 observer.Update();
80 EXPECT_FALSE(GetSavedValue(observer));
81 Mock::VerifyAndClearExpectations(test_accessor_);
82 Mock::VerifyAndClearExpectations(this);
83
84 // Value remains unchanged (false).
85 EXPECT_CALL(*test_accessor_, Get(_)).WillOnce(Return(false));
86 EXPECT_CALL(*this, TestCallback(_)).Times(0);
87 observer.Update();
88}
89
90} // namespace shill