Peter Qiu | c0beca5 | 2015-09-03 11:25:46 -0700 | [diff] [blame] | 1 | // |
| 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 Stewart | 0153cf0 | 2014-06-02 11:53:36 -0700 | [diff] [blame] | 16 | |
| 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 | |
| 28 | using base::Bind; |
| 29 | using base::Unretained; |
| 30 | using testing::_; |
| 31 | using testing::DoAll; |
| 32 | using testing::Mock; |
| 33 | using testing::Return; |
| 34 | |
| 35 | namespace shill { |
| 36 | |
| 37 | class TestPropertyAccessor : public AccessorInterface<bool> { |
| 38 | public: |
Paul Stewart | 3b30ca5 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 39 | MOCK_METHOD1(Clear, void(Error* error)); |
| 40 | MOCK_METHOD1(Get, bool(Error* error)); |
| 41 | MOCK_METHOD2(Set, bool(const bool& value, Error* error)); |
Paul Stewart | 0153cf0 | 2014-06-02 11:53:36 -0700 | [diff] [blame] | 42 | }; |
| 43 | |
| 44 | class PropertyObserverTest : public testing::Test { |
| 45 | public: |
| 46 | PropertyObserverTest() |
| 47 | : test_accessor_(new TestPropertyAccessor()), |
| 48 | bool_accessor_(test_accessor_) {} |
| 49 | virtual ~PropertyObserverTest() {} |
| 50 | |
Paul Stewart | 3b30ca5 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 51 | MOCK_METHOD1(TestCallback, void(const bool& value)); |
Paul Stewart | 0153cf0 | 2014-06-02 11:53:36 -0700 | [diff] [blame] | 52 | |
| 53 | // Invoked method during expectations. |
Paul Stewart | 3b30ca5 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 54 | void SetError(Error* error) { |
Paul Stewart | 0153cf0 | 2014-06-02 11:53:36 -0700 | [diff] [blame] | 55 | error->Populate(Error::kPermissionDenied); |
| 56 | } |
| 57 | |
| 58 | protected: |
Paul Stewart | 3b30ca5 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 59 | bool GetSavedValue(const PropertyObserver<bool>& observer) { |
Paul Stewart | 0153cf0 | 2014-06-02 11:53:36 -0700 | [diff] [blame] | 60 | return observer.saved_value_; |
| 61 | } |
| 62 | |
Paul Stewart | 3b30ca5 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 63 | TestPropertyAccessor* test_accessor_; |
Paul Stewart | 0153cf0 | 2014-06-02 11:53:36 -0700 | [diff] [blame] | 64 | BoolAccessor bool_accessor_; // Owns reference to |test_accessor_|. |
| 65 | }; |
| 66 | |
| 67 | TEST_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 |