$$ 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, MockRepeatingCallback, // MockOnceClosure and MockRepeatingClosure. // // Use: // using FooCallback = base::RepeatingCallback; // // TEST(FooTest, RunsCallbackWithBarArgument) { // base::MockCallback callback; // EXPECT_CALL(callback, Run("bar")).WillOnce(Return(1)); // Foo(callback.Get()); // } // // Or equivalently: // // TEST(FooTest, RunsCallbackWithBarArgument) { // base::MockRepeatingCallback 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 class MockCallback; template using MockOnceCallback = MockCallback>; template using MockRepeatingCallback = MockCallback>; using MockOnceClosure = MockCallback; using MockRepeatingClosure = MockCallback; $range i 0..MAX_ARITY $for i [[ $range j 1..i $var run_type = [[R($for j, [[A$j]])]] template class MockCallback> { 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 class MockCallback> { 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_