mock_callback.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. // This file was GENERATED by command:
  2. // pump.py mock_callback.h.pump
  3. // DO NOT EDIT BY HAND!!!
  4. // Copyright 2017 The Chromium Authors. All rights reserved.
  5. // Use of this source code is governed by a BSD-style license that can be
  6. // found in the LICENSE file.
  7. // Analogous to GMock's built-in MockFunction, but for base::Callback instead of
  8. // std::function. It takes the full callback type as a parameter, so that it can
  9. // support both OnceCallback and RepeatingCallback. Furthermore, this file
  10. // defines convenience typedefs in the form of MockOnceCallback<Signature>,
  11. // MockRepeatingCallback<Signature>, MockOnceClosure and MockRepeatingClosure.
  12. //
  13. // Use:
  14. // using FooCallback = base::RepeatingCallback<int(std::string)>;
  15. //
  16. // TEST(FooTest, RunsCallbackWithBarArgument) {
  17. // base::MockCallback<FooCallback> callback;
  18. // EXPECT_CALL(callback, Run("bar")).WillOnce(Return(1));
  19. // Foo(callback.Get());
  20. // }
  21. //
  22. // Or equivalently:
  23. //
  24. // TEST(FooTest, RunsCallbackWithBarArgument) {
  25. // base::MockRepeatingCallback<int(std::string)> callback;
  26. // EXPECT_CALL(callback, Run("bar")).WillOnce(Return(1));
  27. // Foo(callback.Get());
  28. // }
  29. //
  30. //
  31. // Can be used with StrictMock and NiceMock. Caller must ensure that it outlives
  32. // any base::Callback obtained from it.
  33. #ifndef BASE_TEST_MOCK_CALLBACK_H_
  34. #define BASE_TEST_MOCK_CALLBACK_H_
  35. #include "base/bind.h"
  36. #include "base/callback.h"
  37. #include "base/macros.h"
  38. #include "testing/gmock/include/gmock/gmock.h"
  39. namespace base {
  40. // clang-format off
  41. template <typename F>
  42. class MockCallback;
  43. template <typename Signature>
  44. using MockOnceCallback = MockCallback<OnceCallback<Signature>>;
  45. template <typename Signature>
  46. using MockRepeatingCallback = MockCallback<RepeatingCallback<Signature>>;
  47. using MockOnceClosure = MockCallback<OnceClosure>;
  48. using MockRepeatingClosure = MockCallback<RepeatingClosure>;
  49. template <typename R>
  50. class MockCallback<RepeatingCallback<R()>> {
  51. public:
  52. MockCallback() = default;
  53. MOCK_METHOD0_T(Run, R());
  54. RepeatingCallback<R()> Get() {
  55. return BindRepeating(&MockCallback::Run, Unretained(this));
  56. }
  57. private:
  58. DISALLOW_COPY_AND_ASSIGN(MockCallback);
  59. };
  60. template <typename R>
  61. class MockCallback<OnceCallback<R()>> {
  62. public:
  63. MockCallback() = default;
  64. MOCK_METHOD0_T(Run, R());
  65. OnceCallback<R()> Get() {
  66. return BindOnce(&MockCallback::Run, Unretained(this));
  67. }
  68. private:
  69. DISALLOW_COPY_AND_ASSIGN(MockCallback);
  70. };
  71. template <typename R, typename A1>
  72. class MockCallback<RepeatingCallback<R(A1)>> {
  73. public:
  74. MockCallback() = default;
  75. MOCK_METHOD1_T(Run, R(A1));
  76. RepeatingCallback<R(A1)> Get() {
  77. return BindRepeating(&MockCallback::Run, Unretained(this));
  78. }
  79. private:
  80. DISALLOW_COPY_AND_ASSIGN(MockCallback);
  81. };
  82. template <typename R, typename A1>
  83. class MockCallback<OnceCallback<R(A1)>> {
  84. public:
  85. MockCallback() = default;
  86. MOCK_METHOD1_T(Run, R(A1));
  87. OnceCallback<R(A1)> Get() {
  88. return BindOnce(&MockCallback::Run, Unretained(this));
  89. }
  90. private:
  91. DISALLOW_COPY_AND_ASSIGN(MockCallback);
  92. };
  93. template <typename R, typename A1, typename A2>
  94. class MockCallback<RepeatingCallback<R(A1, A2)>> {
  95. public:
  96. MockCallback() = default;
  97. MOCK_METHOD2_T(Run, R(A1, A2));
  98. RepeatingCallback<R(A1, A2)> Get() {
  99. return BindRepeating(&MockCallback::Run, Unretained(this));
  100. }
  101. private:
  102. DISALLOW_COPY_AND_ASSIGN(MockCallback);
  103. };
  104. template <typename R, typename A1, typename A2>
  105. class MockCallback<OnceCallback<R(A1, A2)>> {
  106. public:
  107. MockCallback() = default;
  108. MOCK_METHOD2_T(Run, R(A1, A2));
  109. OnceCallback<R(A1, A2)> Get() {
  110. return BindOnce(&MockCallback::Run, Unretained(this));
  111. }
  112. private:
  113. DISALLOW_COPY_AND_ASSIGN(MockCallback);
  114. };
  115. template <typename R, typename A1, typename A2, typename A3>
  116. class MockCallback<RepeatingCallback<R(A1, A2, A3)>> {
  117. public:
  118. MockCallback() = default;
  119. MOCK_METHOD3_T(Run, R(A1, A2, A3));
  120. RepeatingCallback<R(A1, A2, A3)> Get() {
  121. return BindRepeating(&MockCallback::Run, Unretained(this));
  122. }
  123. private:
  124. DISALLOW_COPY_AND_ASSIGN(MockCallback);
  125. };
  126. template <typename R, typename A1, typename A2, typename A3>
  127. class MockCallback<OnceCallback<R(A1, A2, A3)>> {
  128. public:
  129. MockCallback() = default;
  130. MOCK_METHOD3_T(Run, R(A1, A2, A3));
  131. OnceCallback<R(A1, A2, A3)> Get() {
  132. return BindOnce(&MockCallback::Run, Unretained(this));
  133. }
  134. private:
  135. DISALLOW_COPY_AND_ASSIGN(MockCallback);
  136. };
  137. template <typename R, typename A1, typename A2, typename A3, typename A4>
  138. class MockCallback<RepeatingCallback<R(A1, A2, A3, A4)>> {
  139. public:
  140. MockCallback() = default;
  141. MOCK_METHOD4_T(Run, R(A1, A2, A3, A4));
  142. RepeatingCallback<R(A1, A2, A3, A4)> Get() {
  143. return BindRepeating(&MockCallback::Run, Unretained(this));
  144. }
  145. private:
  146. DISALLOW_COPY_AND_ASSIGN(MockCallback);
  147. };
  148. template <typename R, typename A1, typename A2, typename A3, typename A4>
  149. class MockCallback<OnceCallback<R(A1, A2, A3, A4)>> {
  150. public:
  151. MockCallback() = default;
  152. MOCK_METHOD4_T(Run, R(A1, A2, A3, A4));
  153. OnceCallback<R(A1, A2, A3, A4)> Get() {
  154. return BindOnce(&MockCallback::Run, Unretained(this));
  155. }
  156. private:
  157. DISALLOW_COPY_AND_ASSIGN(MockCallback);
  158. };
  159. template <typename R, typename A1, typename A2, typename A3, typename A4,
  160. typename A5>
  161. class MockCallback<RepeatingCallback<R(A1, A2, A3, A4, A5)>> {
  162. public:
  163. MockCallback() = default;
  164. MOCK_METHOD5_T(Run, R(A1, A2, A3, A4, A5));
  165. RepeatingCallback<R(A1, A2, A3, A4, A5)> Get() {
  166. return BindRepeating(&MockCallback::Run, Unretained(this));
  167. }
  168. private:
  169. DISALLOW_COPY_AND_ASSIGN(MockCallback);
  170. };
  171. template <typename R, typename A1, typename A2, typename A3, typename A4,
  172. typename A5>
  173. class MockCallback<OnceCallback<R(A1, A2, A3, A4, A5)>> {
  174. public:
  175. MockCallback() = default;
  176. MOCK_METHOD5_T(Run, R(A1, A2, A3, A4, A5));
  177. OnceCallback<R(A1, A2, A3, A4, A5)> Get() {
  178. return BindOnce(&MockCallback::Run, Unretained(this));
  179. }
  180. private:
  181. DISALLOW_COPY_AND_ASSIGN(MockCallback);
  182. };
  183. template <typename R, typename A1, typename A2, typename A3, typename A4,
  184. typename A5, typename A6>
  185. class MockCallback<RepeatingCallback<R(A1, A2, A3, A4, A5, A6)>> {
  186. public:
  187. MockCallback() = default;
  188. MOCK_METHOD6_T(Run, R(A1, A2, A3, A4, A5, A6));
  189. RepeatingCallback<R(A1, A2, A3, A4, A5, A6)> Get() {
  190. return BindRepeating(&MockCallback::Run, Unretained(this));
  191. }
  192. private:
  193. DISALLOW_COPY_AND_ASSIGN(MockCallback);
  194. };
  195. template <typename R, typename A1, typename A2, typename A3, typename A4,
  196. typename A5, typename A6>
  197. class MockCallback<OnceCallback<R(A1, A2, A3, A4, A5, A6)>> {
  198. public:
  199. MockCallback() = default;
  200. MOCK_METHOD6_T(Run, R(A1, A2, A3, A4, A5, A6));
  201. OnceCallback<R(A1, A2, A3, A4, A5, A6)> Get() {
  202. return BindOnce(&MockCallback::Run, Unretained(this));
  203. }
  204. private:
  205. DISALLOW_COPY_AND_ASSIGN(MockCallback);
  206. };
  207. template <typename R, typename A1, typename A2, typename A3, typename A4,
  208. typename A5, typename A6, typename A7>
  209. class MockCallback<RepeatingCallback<R(A1, A2, A3, A4, A5, A6, A7)>> {
  210. public:
  211. MockCallback() = default;
  212. MOCK_METHOD7_T(Run, R(A1, A2, A3, A4, A5, A6, A7));
  213. RepeatingCallback<R(A1, A2, A3, A4, A5, A6, A7)> Get() {
  214. return BindRepeating(&MockCallback::Run, Unretained(this));
  215. }
  216. private:
  217. DISALLOW_COPY_AND_ASSIGN(MockCallback);
  218. };
  219. template <typename R, typename A1, typename A2, typename A3, typename A4,
  220. typename A5, typename A6, typename A7>
  221. class MockCallback<OnceCallback<R(A1, A2, A3, A4, A5, A6, A7)>> {
  222. public:
  223. MockCallback() = default;
  224. MOCK_METHOD7_T(Run, R(A1, A2, A3, A4, A5, A6, A7));
  225. OnceCallback<R(A1, A2, A3, A4, A5, A6, A7)> Get() {
  226. return BindOnce(&MockCallback::Run, Unretained(this));
  227. }
  228. private:
  229. DISALLOW_COPY_AND_ASSIGN(MockCallback);
  230. };
  231. template <typename R, typename A1, typename A2, typename A3, typename A4,
  232. typename A5, typename A6, typename A7, typename A8>
  233. class MockCallback<RepeatingCallback<R(A1, A2, A3, A4, A5, A6, A7, A8)>> {
  234. public:
  235. MockCallback() = default;
  236. MOCK_METHOD8_T(Run, R(A1, A2, A3, A4, A5, A6, A7, A8));
  237. RepeatingCallback<R(A1, A2, A3, A4, A5, A6, A7, A8)> Get() {
  238. return BindRepeating(&MockCallback::Run, Unretained(this));
  239. }
  240. private:
  241. DISALLOW_COPY_AND_ASSIGN(MockCallback);
  242. };
  243. template <typename R, typename A1, typename A2, typename A3, typename A4,
  244. typename A5, typename A6, typename A7, typename A8>
  245. class MockCallback<OnceCallback<R(A1, A2, A3, A4, A5, A6, A7, A8)>> {
  246. public:
  247. MockCallback() = default;
  248. MOCK_METHOD8_T(Run, R(A1, A2, A3, A4, A5, A6, A7, A8));
  249. OnceCallback<R(A1, A2, A3, A4, A5, A6, A7, A8)> Get() {
  250. return BindOnce(&MockCallback::Run, Unretained(this));
  251. }
  252. private:
  253. DISALLOW_COPY_AND_ASSIGN(MockCallback);
  254. };
  255. template <typename R, typename A1, typename A2, typename A3, typename A4,
  256. typename A5, typename A6, typename A7, typename A8, typename A9>
  257. class MockCallback<RepeatingCallback<R(A1, A2, A3, A4, A5, A6, A7, A8, A9)>> {
  258. public:
  259. MockCallback() = default;
  260. MOCK_METHOD9_T(Run, R(A1, A2, A3, A4, A5, A6, A7, A8, A9));
  261. RepeatingCallback<R(A1, A2, A3, A4, A5, A6, A7, A8, A9)> Get() {
  262. return BindRepeating(&MockCallback::Run, Unretained(this));
  263. }
  264. private:
  265. DISALLOW_COPY_AND_ASSIGN(MockCallback);
  266. };
  267. template <typename R, typename A1, typename A2, typename A3, typename A4,
  268. typename A5, typename A6, typename A7, typename A8, typename A9>
  269. class MockCallback<OnceCallback<R(A1, A2, A3, A4, A5, A6, A7, A8, A9)>> {
  270. public:
  271. MockCallback() = default;
  272. MOCK_METHOD9_T(Run, R(A1, A2, A3, A4, A5, A6, A7, A8, A9));
  273. OnceCallback<R(A1, A2, A3, A4, A5, A6, A7, A8, A9)> Get() {
  274. return BindOnce(&MockCallback::Run, Unretained(this));
  275. }
  276. private:
  277. DISALLOW_COPY_AND_ASSIGN(MockCallback);
  278. };
  279. template <typename R, typename A1, typename A2, typename A3, typename A4,
  280. typename A5, typename A6, typename A7, typename A8, typename A9,
  281. typename A10>
  282. class MockCallback<RepeatingCallback<R(A1, A2, A3, A4, A5, A6, A7, A8, A9,
  283. A10)>> {
  284. public:
  285. MockCallback() = default;
  286. MOCK_METHOD10_T(Run, R(A1, A2, A3, A4, A5, A6, A7, A8, A9, A10));
  287. RepeatingCallback<R(A1, A2, A3, A4, A5, A6, A7, A8, A9, A10)> Get() {
  288. return BindRepeating(&MockCallback::Run, Unretained(this));
  289. }
  290. private:
  291. DISALLOW_COPY_AND_ASSIGN(MockCallback);
  292. };
  293. template <typename R, typename A1, typename A2, typename A3, typename A4,
  294. typename A5, typename A6, typename A7, typename A8, typename A9,
  295. typename A10>
  296. class MockCallback<OnceCallback<R(A1, A2, A3, A4, A5, A6, A7, A8, A9, A10)>> {
  297. public:
  298. MockCallback() = default;
  299. MOCK_METHOD10_T(Run, R(A1, A2, A3, A4, A5, A6, A7, A8, A9, A10));
  300. OnceCallback<R(A1, A2, A3, A4, A5, A6, A7, A8, A9, A10)> Get() {
  301. return BindOnce(&MockCallback::Run, Unretained(this));
  302. }
  303. private:
  304. DISALLOW_COPY_AND_ASSIGN(MockCallback);
  305. };
  306. // clang-format on
  307. } // namespace base
  308. #endif // BASE_TEST_MOCK_CALLBACK_H_