mukesh agrawal | 186af05 | 2012-01-11 15:07:34 -0800 | [diff] [blame] | 1 | // Copyright (c) 2012 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/callback_list.h" |
| 6 | |
| 7 | #include <base/logging.h> |
| 8 | #include <gmock/gmock.h> |
| 9 | #include <gtest/gtest.h> |
| 10 | |
| 11 | #include "mock_callback.h" |
| 12 | |
| 13 | using ::testing::_; |
| 14 | using ::testing::Return; |
| 15 | using ::testing::Test; |
| 16 | |
| 17 | namespace shill { |
| 18 | |
| 19 | class CallbackListTest : public Test { |
| 20 | protected: |
| 21 | CallbackList callbacks_; |
| 22 | }; |
| 23 | |
| 24 | TEST_F(CallbackListTest, AllTrue) { |
| 25 | // Note that the EXPECT_CALLs cause gmock to verify that the |
| 26 | // CallbackList dtor cleanups up callbacks. |
| 27 | MockCallback *callback1 = new MockCallback(); |
| 28 | callbacks_.Add("callback1", callback1); |
| 29 | EXPECT_CALL(*callback1, Run()) |
| 30 | .WillOnce(Return(true)); |
| 31 | |
| 32 | MockCallback *callback2 = new MockCallback(); |
| 33 | callbacks_.Add("callback2", callback2); |
| 34 | EXPECT_CALL(*callback2, Run()) |
| 35 | .WillOnce(Return(true)); |
| 36 | |
| 37 | EXPECT_TRUE(callbacks_.Run()); |
| 38 | } |
| 39 | |
| 40 | |
| 41 | TEST_F(CallbackListTest, AllFalse) { |
| 42 | // Note that the EXPECT_CALLs verify that Run() doesn't |
| 43 | // short-circuit on the first callback failure. Also, we have both |
| 44 | // callbacks return false, so we catch short-circuiting regardless |
| 45 | // of the order in which Run() executes the callbacks. |
| 46 | MockCallback *callback1 = new MockCallback(); |
| 47 | callbacks_.Add("callback1", callback1); |
| 48 | EXPECT_CALL(*callback1, Run()) |
| 49 | .WillOnce(Return(false)); |
| 50 | |
| 51 | MockCallback *callback2 = new MockCallback(); |
| 52 | callbacks_.Add("callback2", callback2); |
| 53 | EXPECT_CALL(*callback2, Run()) |
| 54 | .WillOnce(Return(false)); |
| 55 | |
| 56 | EXPECT_FALSE(callbacks_.Run()); |
| 57 | } |
| 58 | |
| 59 | TEST_F(CallbackListTest, MixedReturnValues) { |
| 60 | MockCallback *callback1 = new MockCallback(); |
| 61 | callbacks_.Add("callback1", callback1); |
| 62 | EXPECT_CALL(*callback1, Run()) |
| 63 | .WillOnce(Return(true)); |
| 64 | |
| 65 | MockCallback *callback2 = new MockCallback(); |
| 66 | callbacks_.Add("callback2", callback2); |
| 67 | EXPECT_CALL(*callback2, Run()) |
| 68 | .WillOnce(Return(false)); |
| 69 | |
| 70 | EXPECT_FALSE(callbacks_.Run()); |
| 71 | } |
| 72 | |
| 73 | TEST_F(CallbackListTest, Remove) { |
| 74 | MockCallback *callback = new MockCallback(); |
| 75 | callbacks_.Add("callback", callback); |
| 76 | EXPECT_CALL(*callback, Run()) |
| 77 | .WillOnce(Return(false)); |
| 78 | EXPECT_FALSE(callbacks_.Run()); |
| 79 | |
| 80 | EXPECT_CALL(*callback, Run()) |
| 81 | .Times(0); |
| 82 | // By virtue of the EXPECT_CALLs on |callback|, we implicitly tests |
| 83 | // that Remove does not leak objects. |
| 84 | callbacks_.Remove("callback"); |
| 85 | EXPECT_TRUE(callbacks_.Run()); |
| 86 | } |
| 87 | |
| 88 | } // namespace shill |