1*635a8641SAndroid Build Coastguard Worker$$ This is a pump file for generating file templates. Pump is a python 2*635a8641SAndroid Build Coastguard Worker$$ script that is part of the Google Test suite of utilities. Description 3*635a8641SAndroid Build Coastguard Worker$$ can be found here: 4*635a8641SAndroid Build Coastguard Worker$$ 5*635a8641SAndroid Build Coastguard Worker$$ https://github.com/google/googletest/blob/master/googletest/docs/PumpManual.md 6*635a8641SAndroid Build Coastguard Worker$$ 7*635a8641SAndroid Build Coastguard Worker$$ MAX_ARITY controls the number of arguments that MockCallback supports. 8*635a8641SAndroid Build Coastguard Worker$$ It is choosen to match the number GMock supports. 9*635a8641SAndroid Build Coastguard Worker$var MAX_ARITY = 10 10*635a8641SAndroid Build Coastguard Worker$$ 11*635a8641SAndroid Build Coastguard Worker// Copyright 2017 The Chromium Authors. All rights reserved. 12*635a8641SAndroid Build Coastguard Worker// Use of this source code is governed by a BSD-style license that can be 13*635a8641SAndroid Build Coastguard Worker// found in the LICENSE file. 14*635a8641SAndroid Build Coastguard Worker 15*635a8641SAndroid Build Coastguard Worker// Analogous to GMock's built-in MockFunction, but for base::Callback instead of 16*635a8641SAndroid Build Coastguard Worker// std::function. It takes the full callback type as a parameter, so that it can 17*635a8641SAndroid Build Coastguard Worker// support both OnceCallback and RepeatingCallback. 18*635a8641SAndroid Build Coastguard Worker// 19*635a8641SAndroid Build Coastguard Worker// Use: 20*635a8641SAndroid Build Coastguard Worker// using FooCallback = base::Callback<int(std::string)>; 21*635a8641SAndroid Build Coastguard Worker// 22*635a8641SAndroid Build Coastguard Worker// TEST(FooTest, RunsCallbackWithBarArgument) { 23*635a8641SAndroid Build Coastguard Worker// base::MockCallback<FooCallback> callback; 24*635a8641SAndroid Build Coastguard Worker// EXPECT_CALL(callback, Run("bar")).WillOnce(Return(1)); 25*635a8641SAndroid Build Coastguard Worker// Foo(callback.Get()); 26*635a8641SAndroid Build Coastguard Worker// } 27*635a8641SAndroid Build Coastguard Worker// 28*635a8641SAndroid Build Coastguard Worker// Can be used with StrictMock and NiceMock. Caller must ensure that it outlives 29*635a8641SAndroid Build Coastguard Worker// any base::Callback obtained from it. 30*635a8641SAndroid Build Coastguard Worker 31*635a8641SAndroid Build Coastguard Worker#ifndef BASE_TEST_MOCK_CALLBACK_H_ 32*635a8641SAndroid Build Coastguard Worker#define BASE_TEST_MOCK_CALLBACK_H_ 33*635a8641SAndroid Build Coastguard Worker 34*635a8641SAndroid Build Coastguard Worker#include "base/bind.h" 35*635a8641SAndroid Build Coastguard Worker#include "base/callback.h" 36*635a8641SAndroid Build Coastguard Worker#include "base/macros.h" 37*635a8641SAndroid Build Coastguard Worker#include "testing/gmock/include/gmock/gmock.h" 38*635a8641SAndroid Build Coastguard Worker 39*635a8641SAndroid Build Coastguard Workernamespace base { 40*635a8641SAndroid Build Coastguard Worker 41*635a8641SAndroid Build Coastguard Worker// clang-format off 42*635a8641SAndroid Build Coastguard Worker 43*635a8641SAndroid Build Coastguard Workertemplate <typename F> 44*635a8641SAndroid Build Coastguard Workerclass MockCallback; 45*635a8641SAndroid Build Coastguard Worker 46*635a8641SAndroid Build Coastguard Worker$range i 0..MAX_ARITY 47*635a8641SAndroid Build Coastguard Worker$for i [[ 48*635a8641SAndroid Build Coastguard Worker$range j 1..i 49*635a8641SAndroid Build Coastguard Worker$var run_type = [[R($for j, [[A$j]])]] 50*635a8641SAndroid Build Coastguard Worker 51*635a8641SAndroid Build Coastguard Workertemplate <typename R$for j [[, typename A$j]]> 52*635a8641SAndroid Build Coastguard Workerclass MockCallback<Callback<$run_type>> { 53*635a8641SAndroid Build Coastguard Worker public: 54*635a8641SAndroid Build Coastguard Worker MockCallback() = default; 55*635a8641SAndroid Build Coastguard Worker MOCK_METHOD$(i)_T(Run, $run_type); 56*635a8641SAndroid Build Coastguard Worker 57*635a8641SAndroid Build Coastguard Worker Callback<$run_type> Get() { 58*635a8641SAndroid Build Coastguard Worker return Bind(&MockCallback::Run, Unretained(this)); 59*635a8641SAndroid Build Coastguard Worker } 60*635a8641SAndroid Build Coastguard Worker 61*635a8641SAndroid Build Coastguard Worker private: 62*635a8641SAndroid Build Coastguard Worker DISALLOW_COPY_AND_ASSIGN(MockCallback); 63*635a8641SAndroid Build Coastguard Worker}; 64*635a8641SAndroid Build Coastguard Worker 65*635a8641SAndroid Build Coastguard Workertemplate <typename R$for j [[, typename A$j]]> 66*635a8641SAndroid Build Coastguard Workerclass MockCallback<OnceCallback<$run_type>> { 67*635a8641SAndroid Build Coastguard Worker public: 68*635a8641SAndroid Build Coastguard Worker MockCallback() = default; 69*635a8641SAndroid Build Coastguard Worker MOCK_METHOD$(i)_T(Run, $run_type); 70*635a8641SAndroid Build Coastguard Worker 71*635a8641SAndroid Build Coastguard Worker OnceCallback<$run_type> Get() { 72*635a8641SAndroid Build Coastguard Worker return BindOnce(&MockCallback::Run, Unretained(this)); 73*635a8641SAndroid Build Coastguard Worker } 74*635a8641SAndroid Build Coastguard Worker 75*635a8641SAndroid Build Coastguard Worker private: 76*635a8641SAndroid Build Coastguard Worker DISALLOW_COPY_AND_ASSIGN(MockCallback); 77*635a8641SAndroid Build Coastguard Worker}; 78*635a8641SAndroid Build Coastguard Worker 79*635a8641SAndroid Build Coastguard Worker]] 80*635a8641SAndroid Build Coastguard Worker 81*635a8641SAndroid Build Coastguard Worker// clang-format on 82*635a8641SAndroid Build Coastguard Worker 83*635a8641SAndroid Build Coastguard Worker} // namespace base 84*635a8641SAndroid Build Coastguard Worker 85*635a8641SAndroid Build Coastguard Worker#endif // BASE_TEST_MOCK_CALLBACK_H_ 86