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