Gary Morain | bb55392 | 2012-01-27 12:46:21 -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 | // Tests for the MockCallback class. They ensures that MockCallback can be used |
| 6 | // to replace each Callback type in base/callbacks_old.h. |
| 7 | |
| 8 | #include "shill/mock_callback.h" |
| 9 | |
| 10 | #include <base/logging.h> |
| 11 | #include <base/scoped_ptr.h> |
| 12 | #include <gmock/gmock.h> |
| 13 | #include <gtest/gtest.h> |
| 14 | |
| 15 | using std::string; |
| 16 | using ::testing::_; |
| 17 | using ::testing::Return; |
| 18 | using ::testing::Test; |
| 19 | |
| 20 | namespace shill { |
| 21 | class MockCallbackTest : public Test { |
| 22 | public: |
| 23 | // RunCallback simulates how a real callback may be invoked. It calls a |
| 24 | // callback's Run method with the correct number of arguments. There is a |
| 25 | // version for each type of callback. |
| 26 | void RunCallback(Callback0::Type *cb) { |
| 27 | cb->Run(); |
| 28 | } |
| 29 | |
| 30 | template <typename R> |
| 31 | R RunCallback(typename CallbackWithReturnValue<R>::Type *cb) { |
| 32 | return cb->Run(); |
| 33 | } |
| 34 | |
| 35 | template <typename Arg1> |
| 36 | void RunCallback(typename Callback1<Arg1>::Type *cb, Arg1 arg1) { |
| 37 | cb->Run(arg1); |
| 38 | } |
| 39 | |
| 40 | template <typename Arg1, typename Arg2> |
| 41 | void RunCallback(typename Callback2<Arg1, Arg2>::Type *cb, |
| 42 | Arg1 arg1, Arg2 arg2) { |
| 43 | cb->Run(arg1, arg2); |
| 44 | } |
| 45 | |
| 46 | template <typename Arg1, typename Arg2, typename Arg3> |
| 47 | void RunCallback(typename Callback3<Arg1, Arg2, Arg3>::Type *cb, |
| 48 | Arg1 arg1, Arg2 arg2, Arg3 arg3) { |
| 49 | cb->Run(arg1, arg2, arg3); |
| 50 | } |
| 51 | |
| 52 | template <typename Arg1, typename Arg2, typename Arg3, typename Arg4> |
| 53 | void RunCallback(typename Callback4<Arg1, Arg2, Arg3, Arg4>::Type *cb, |
| 54 | Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4) { |
| 55 | cb->Run(arg1, arg2, arg3, arg4); |
| 56 | } |
| 57 | |
| 58 | template <typename Arg1, typename Arg2, typename Arg3, typename Arg4, |
| 59 | typename Arg5> |
| 60 | void RunCallback(typename Callback5<Arg1, Arg2, Arg3, Arg4, Arg5>::Type *cb, |
| 61 | Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, Arg5 arg5) { |
| 62 | cb->Run(arg1, arg2, arg3, arg4, arg5); |
| 63 | } |
| 64 | }; |
| 65 | |
| 66 | TEST_F(MockCallbackTest, Callback0) { |
| 67 | scoped_ptr<MockCallback<void()> > mock_callback(NewMockCallback()); |
| 68 | EXPECT_CALL(*mock_callback, OnRun()); |
| 69 | RunCallback(mock_callback.get()); |
| 70 | } |
| 71 | |
| 72 | TEST_F(MockCallbackTest, CallbackWithReturnValue) { |
| 73 | scoped_ptr<MockCallback<int()> > mock_callback(NewMockCallback()); |
| 74 | const int kReturn = 99; |
| 75 | EXPECT_CALL(*mock_callback, OnRun()).WillOnce(Return(kReturn)); |
| 76 | EXPECT_EQ(kReturn, RunCallback<int>(mock_callback.get())); |
| 77 | } |
| 78 | |
| 79 | TEST_F(MockCallbackTest, Callback1) { |
| 80 | scoped_ptr<MockCallback<void(int)> > mock_callback(NewMockCallback()); |
| 81 | const int kArg = 99; |
| 82 | EXPECT_CALL(*mock_callback, OnRun(kArg)); |
| 83 | RunCallback(mock_callback.get(), kArg); |
| 84 | } |
| 85 | |
| 86 | TEST_F(MockCallbackTest, Callback2) { |
| 87 | scoped_ptr<MockCallback<void(int, string)> > mock_callback( |
| 88 | NewMockCallback()); |
| 89 | const int kArg1 = 99; |
| 90 | const string kArg2 = "Beeblebrox"; |
| 91 | EXPECT_CALL(*mock_callback, OnRun(kArg1, kArg2)); |
| 92 | RunCallback(mock_callback.get(), kArg1, kArg2); |
| 93 | } |
| 94 | |
| 95 | TEST_F(MockCallbackTest, Callback3) { |
| 96 | scoped_ptr<MockCallback<void(int, string, int)> > mock_callback( |
| 97 | NewMockCallback()); |
| 98 | const int kArg1 = 99; |
| 99 | const string kArg2 = "Beeblebrox"; |
| 100 | const int kArg3 = 42; |
| 101 | EXPECT_CALL(*mock_callback, OnRun(kArg1, kArg2, kArg3)); |
| 102 | RunCallback(mock_callback.get(), kArg1, kArg2, kArg3); |
| 103 | } |
| 104 | |
| 105 | TEST_F(MockCallbackTest, Callback4) { |
| 106 | scoped_ptr<MockCallback<void(int, string, int, string)> > mock_callback( |
| 107 | NewMockCallback()); |
| 108 | const int kArg1 = 99; |
| 109 | const string kArg2 = "Beeblebrox"; |
| 110 | const int kArg3 = 42; |
| 111 | const string kArg4 = "Zaphod"; |
| 112 | EXPECT_CALL(*mock_callback, OnRun(kArg1, kArg2, kArg3, kArg4)); |
| 113 | RunCallback(mock_callback.get(), kArg1, kArg2, kArg3, kArg4); |
| 114 | } |
| 115 | |
| 116 | TEST_F(MockCallbackTest, Callback5) { |
| 117 | scoped_ptr<MockCallback<void(int, string, int, string, int)> > mock_callback( |
| 118 | NewMockCallback()); |
| 119 | const int kArg1 = 99; |
| 120 | const string kArg2 = "Beeblebrox"; |
| 121 | const int kArg3 = 42; |
| 122 | const string kArg4 = "Zaphod"; |
| 123 | const int kArg5 = 101; |
| 124 | EXPECT_CALL(*mock_callback, OnRun(kArg1, kArg2, kArg3, kArg4, kArg5)); |
| 125 | RunCallback(mock_callback.get(), kArg1, kArg2, kArg3, kArg4, kArg5); |
| 126 | } |
| 127 | |
| 128 | } // namespace shill |