1 // Copyright 2017 The Chromium 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 "base/test/mock_callback.h"
6 
7 #include "base/callback.h"
8 #include "testing/gmock/include/gmock/gmock.h"
9 
10 using testing::InSequence;
11 using testing::Return;
12 
13 namespace base {
14 namespace {
15 
TEST(MockCallbackTest,ZeroArgs)16 TEST(MockCallbackTest, ZeroArgs) {
17   MockCallback<Closure> mock_closure;
18   EXPECT_CALL(mock_closure, Run());
19   mock_closure.Get().Run();
20 
21   MockCallback<Callback<int()>> mock_int_callback;
22   {
23     InSequence sequence;
24     EXPECT_CALL(mock_int_callback, Run()).WillOnce(Return(42));
25     EXPECT_CALL(mock_int_callback, Run()).WillOnce(Return(88));
26   }
27   EXPECT_EQ(42, mock_int_callback.Get().Run());
28   EXPECT_EQ(88, mock_int_callback.Get().Run());
29 }
30 
TEST(MockCallbackTest,WithArgs)31 TEST(MockCallbackTest, WithArgs) {
32   MockCallback<Callback<int(int, int)>> mock_two_int_callback;
33   EXPECT_CALL(mock_two_int_callback, Run(1, 2)).WillOnce(Return(42));
34   EXPECT_CALL(mock_two_int_callback, Run(0, 0)).WillRepeatedly(Return(-1));
35   Callback<int(int, int)> two_int_callback = mock_two_int_callback.Get();
36   EXPECT_EQ(-1, two_int_callback.Run(0, 0));
37   EXPECT_EQ(42, two_int_callback.Run(1, 2));
38   EXPECT_EQ(-1, two_int_callback.Run(0, 0));
39 }
40 
TEST(MockCallbackTest,ZeroArgsOnce)41 TEST(MockCallbackTest, ZeroArgsOnce) {
42   MockCallback<OnceClosure> mock_closure;
43   EXPECT_CALL(mock_closure, Run());
44   mock_closure.Get().Run();
45 
46   MockCallback<OnceCallback<int()>> mock_int_callback;
47   EXPECT_CALL(mock_int_callback, Run()).WillOnce(Return(88));
48   EXPECT_EQ(88, mock_int_callback.Get().Run());
49 }
50 
TEST(MockCallbackTest,WithArgsOnce)51 TEST(MockCallbackTest, WithArgsOnce) {
52   MockCallback<OnceCallback<int(int, int)>> mock_two_int_callback;
53   EXPECT_CALL(mock_two_int_callback, Run(1, 2)).WillOnce(Return(42));
54   OnceCallback<int(int, int)> two_int_callback = mock_two_int_callback.Get();
55   EXPECT_EQ(42, std::move(two_int_callback).Run(1, 2));
56 }
57 
58 }  // namespace
59 }  // namespace base
60