1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
base / test / mock_callback.h.pump [blame]
$$ This is a pump file for generating file templates. Pump is a python
$$ script that is part of the Google Test suite of utilities. Description
$$ can be found here:
$$
$$ https://github.com/google/googletest/blob/main/googletest/docs/PumpManual.md
$$
$$ MAX_ARITY controls the number of arguments that MockCallback supports.
$$ It is choosen to match the number GMock supports.
$var MAX_ARITY = 10
$$
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Analogous to GMock's built-in MockFunction, but for base::{Once,
// Repeating}Callback instead of std::function. It takes the full callback type
// as a parameter, so that it can support both OnceCallback and
// RepeatingCallback. Furthermore, this file defines convenience typedefs in the
// form of MockOnceCallback<Signature>, MockRepeatingCallback<Signature>,
// MockOnceClosure and MockRepeatingClosure.
//
// Use:
// using FooCallback = base::RepeatingCallback<int(std::string)>;
//
// TEST(FooTest, RunsCallbackWithBarArgument) {
// base::MockCallback<FooCallback> callback;
// EXPECT_CALL(callback, Run("bar")).WillOnce(Return(1));
// Foo(callback.Get());
// }
//
// Or equivalently:
//
// TEST(FooTest, RunsCallbackWithBarArgument) {
// base::MockRepeatingCallback<int(std::string)> callback;
// EXPECT_CALL(callback, Run("bar")).WillOnce(Return(1));
// Foo(callback.Get());
// }
//
//
// Can be used with StrictMock and NiceMock. Caller must ensure that it outlives
// any base::{Once, Repeating}Callback obtained from it.
#ifndef BASE_TEST_MOCK_CALLBACK_H_
#define BASE_TEST_MOCK_CALLBACK_H_
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "testing/gmock/include/gmock/gmock.h"
namespace base {
// clang-format off
template <typename F>
class MockCallback;
template <typename Signature>
using MockOnceCallback = MockCallback<OnceCallback<Signature>>;
template <typename Signature>
using MockRepeatingCallback = MockCallback<RepeatingCallback<Signature>>;
using MockOnceClosure = MockCallback<OnceClosure>;
using MockRepeatingClosure = MockCallback<RepeatingClosure>;
$range i 0..MAX_ARITY
$for i [[
$range j 1..i
$var run_type = [[R($for j, [[A$j]])]]
template <typename R$for j [[, typename A$j]]>
class MockCallback<RepeatingCallback<$run_type>> {
public:
MockCallback() = default;
MockCallback(const MockCallback&) = delete;
MockCallback& operator=(const MockCallback&) = delete;
MOCK_METHOD$(i)_T(Run, $run_type);
RepeatingCallback<$run_type> Get() {
return ::base::BindRepeating(&MockCallback::Run, ::base::Unretained(this));
}
};
template <typename R$for j [[, typename A$j]]>
class MockCallback<OnceCallback<$run_type>> {
public:
MockCallback() = default;
MockCallback(const MockCallback&) = delete;
MockCallback& operator=(const MockCallback&) = delete;
MOCK_METHOD$(i)_T(Run, $run_type);
OnceCallback<$run_type> Get() {
return ::base::BindOnce(&MockCallback::Run, ::base::Unretained(this));
}
};
]]
// clang-format on
} // namespace base
#endif // BASE_TEST_MOCK_CALLBACK_H_