1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "base/functional/callback.h"
6
7 #include <memory>
8 #include <utility>
9
10 #include "base/functional/bind.h"
11 #include "base/functional/callback_internal.h"
12 #include "base/memory/raw_ptr.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/notreached.h"
15 #include "base/strings/string_number_conversions.h"
16 #include "base/test/bind.h"
17 #include "base/test/gtest_util.h"
18 #include "base/test/test_timeouts.h"
19 #include "base/threading/thread.h"
20 #include "testing/gtest/include/gtest/gtest.h"
21
22 namespace base {
23
NopInvokeFunc()24 void NopInvokeFunc() {}
25
26 // White-box testpoints to inject into a callback object for checking
27 // comparators and emptiness APIs. Use a BindState that is specialized based on
28 // a type we declared in the anonymous namespace above to remove any chance of
29 // colliding with another instantiation and breaking the one-definition-rule.
30 struct FakeBindState : internal::BindStateBase {
FakeBindStatebase::FakeBindState31 FakeBindState() : BindStateBase(&NopInvokeFunc, &Destroy) {}
32
33 private:
34 ~FakeBindState() = default;
Destroybase::FakeBindState35 static void Destroy(const internal::BindStateBase* self) {
36 delete static_cast<const FakeBindState*>(self);
37 }
38 };
39
40 namespace {
41
42 class CallbackTest : public ::testing::Test {
43 public:
CallbackTest()44 CallbackTest()
45 : callback_a_(new FakeBindState()), callback_b_(new FakeBindState()) {}
46
47 ~CallbackTest() override = default;
48
49 protected:
50 RepeatingCallback<void()> callback_a_;
51 const RepeatingCallback<void()> callback_b_; // Ensure APIs work with const.
52 RepeatingCallback<void()> null_callback_;
53 };
54
TEST_F(CallbackTest,Types)55 TEST_F(CallbackTest, Types) {
56 static_assert(std::is_same_v<void, OnceClosure::ResultType>, "");
57 static_assert(std::is_same_v<void(), OnceClosure::RunType>, "");
58
59 using OnceCallbackT = OnceCallback<double(int, char)>;
60 static_assert(std::is_same_v<double, OnceCallbackT::ResultType>, "");
61 static_assert(std::is_same_v<double(int, char), OnceCallbackT::RunType>, "");
62
63 static_assert(std::is_same_v<void, RepeatingClosure::ResultType>, "");
64 static_assert(std::is_same_v<void(), RepeatingClosure::RunType>, "");
65
66 using RepeatingCallbackT = RepeatingCallback<bool(float, short)>;
67 static_assert(std::is_same_v<bool, RepeatingCallbackT::ResultType>, "");
68 static_assert(std::is_same_v<bool(float, short), RepeatingCallbackT::RunType>,
69 "");
70 }
71
72 // Ensure we can create unbound callbacks. We need this to be able to store
73 // them in class members that can be initialized later.
TEST_F(CallbackTest,DefaultConstruction)74 TEST_F(CallbackTest, DefaultConstruction) {
75 RepeatingCallback<void()> c0;
76 RepeatingCallback<void(int)> c1;
77 RepeatingCallback<void(int, int)> c2;
78 RepeatingCallback<void(int, int, int)> c3;
79 RepeatingCallback<void(int, int, int, int)> c4;
80 RepeatingCallback<void(int, int, int, int, int)> c5;
81 RepeatingCallback<void(int, int, int, int, int, int)> c6;
82
83 EXPECT_TRUE(c0.is_null());
84 EXPECT_TRUE(c1.is_null());
85 EXPECT_TRUE(c2.is_null());
86 EXPECT_TRUE(c3.is_null());
87 EXPECT_TRUE(c4.is_null());
88 EXPECT_TRUE(c5.is_null());
89 EXPECT_TRUE(c6.is_null());
90 }
91
TEST_F(CallbackTest,IsNull)92 TEST_F(CallbackTest, IsNull) {
93 EXPECT_TRUE(null_callback_.is_null());
94 EXPECT_FALSE(callback_a_.is_null());
95 EXPECT_FALSE(callback_b_.is_null());
96 }
97
TEST_F(CallbackTest,Equals)98 TEST_F(CallbackTest, Equals) {
99 EXPECT_EQ(callback_a_, callback_a_);
100 EXPECT_NE(callback_a_, callback_b_);
101 EXPECT_NE(callback_b_, callback_a_);
102
103 // We should compare based on instance, not type.
104 RepeatingCallback<void()> callback_c(new FakeBindState());
105 RepeatingCallback<void()> callback_a2 = callback_a_;
106 EXPECT_EQ(callback_a_, callback_a2);
107 EXPECT_NE(callback_a_, callback_c);
108
109 // Empty, however, is always equal to empty.
110 RepeatingCallback<void()> empty2;
111 EXPECT_EQ(null_callback_, empty2);
112 }
113
TEST_F(CallbackTest,Reset)114 TEST_F(CallbackTest, Reset) {
115 // Resetting should bring us back to empty.
116 ASSERT_FALSE(callback_a_.is_null());
117 EXPECT_NE(callback_a_, null_callback_);
118
119 callback_a_.Reset();
120
121 EXPECT_TRUE(callback_a_.is_null());
122 EXPECT_EQ(callback_a_, null_callback_);
123 }
124
TEST_F(CallbackTest,Move)125 TEST_F(CallbackTest, Move) {
126 // Moving should reset the callback.
127 ASSERT_FALSE(callback_a_.is_null());
128 EXPECT_NE(callback_a_, null_callback_);
129
130 auto tmp = std::move(callback_a_);
131
132 EXPECT_TRUE(callback_a_.is_null());
133 EXPECT_EQ(callback_a_, null_callback_);
134 }
135
TEST_F(CallbackTest,NullAfterMoveRun)136 TEST_F(CallbackTest, NullAfterMoveRun) {
137 RepeatingCallback<void(void*)> cb = BindRepeating([](void* param) {
138 EXPECT_TRUE(static_cast<RepeatingCallback<void(void*)>*>(param)->is_null());
139 });
140 ASSERT_TRUE(cb);
141 std::move(cb).Run(&cb);
142 EXPECT_FALSE(cb);
143
144 const RepeatingClosure cb2 = BindRepeating([] {});
145 ASSERT_TRUE(cb2);
146 std::move(cb2).Run();
147 EXPECT_TRUE(cb2);
148
149 OnceCallback<void(void*)> cb3 = BindOnce([](void* param) {
150 EXPECT_TRUE(static_cast<OnceCallback<void(void*)>*>(param)->is_null());
151 });
152 ASSERT_TRUE(cb3);
153 std::move(cb3).Run(&cb3);
154 EXPECT_FALSE(cb3);
155 }
156
TEST_F(CallbackTest,MaybeValidReturnsTrue)157 TEST_F(CallbackTest, MaybeValidReturnsTrue) {
158 RepeatingCallback<void()> cb = BindRepeating([] {});
159 // By default, MaybeValid() just returns true all the time.
160 EXPECT_TRUE(cb.MaybeValid());
161 cb.Run();
162 EXPECT_TRUE(cb.MaybeValid());
163 }
164
TEST_F(CallbackTest,ThenResetsOriginalCallback)165 TEST_F(CallbackTest, ThenResetsOriginalCallback) {
166 {
167 // OnceCallback::Then() always destroys the original callback.
168 OnceClosure orig = base::BindOnce([] {});
169 EXPECT_TRUE(!!orig);
170 OnceClosure joined = std::move(orig).Then(base::BindOnce([] {}));
171 EXPECT_TRUE(!!joined);
172 EXPECT_FALSE(!!orig);
173 }
174 {
175 // RepeatingCallback::Then() destroys the original callback if it's an
176 // rvalue.
177 RepeatingClosure orig = base::BindRepeating([] {});
178 EXPECT_TRUE(!!orig);
179 RepeatingClosure joined = std::move(orig).Then(base::BindRepeating([] {}));
180 EXPECT_TRUE(!!joined);
181 EXPECT_FALSE(!!orig);
182 }
183 {
184 // RepeatingCallback::Then() doesn't destroy the original callback if it's
185 // not an rvalue.
186 RepeatingClosure orig = base::BindRepeating([] {});
187 RepeatingClosure copy = orig;
188 EXPECT_TRUE(!!orig);
189 RepeatingClosure joined = orig.Then(base::BindRepeating([] {}));
190 EXPECT_TRUE(!!joined);
191 EXPECT_TRUE(!!orig);
192 // The original callback is not changed.
193 EXPECT_EQ(orig, copy);
194 EXPECT_NE(joined, copy);
195 }
196 }
197
198 // A RepeatingCallback will implicitly convert to a OnceCallback, so a
199 // once_callback.Then(repeating_callback) should turn into a OnceCallback
200 // that holds 2 OnceCallbacks which it will run.
TEST_F(CallbackTest,ThenCanConvertRepeatingToOnce)201 TEST_F(CallbackTest, ThenCanConvertRepeatingToOnce) {
202 {
203 RepeatingClosure repeating_closure = base::BindRepeating([] {});
204 OnceClosure once_closure = base::BindOnce([] {});
205 std::move(once_closure).Then(repeating_closure).Run();
206
207 RepeatingCallback<int(int)> repeating_callback =
208 base::BindRepeating([](int i) { return i + 1; });
209 OnceCallback<int(int)> once_callback =
210 base::BindOnce([](int i) { return i * 2; });
211 EXPECT_EQ(3, std::move(once_callback).Then(repeating_callback).Run(1));
212 }
213 {
214 RepeatingClosure repeating_closure = base::BindRepeating([] {});
215 OnceClosure once_closure = base::BindOnce([] {});
216 std::move(once_closure).Then(std::move(repeating_closure)).Run();
217
218 RepeatingCallback<int(int)> repeating_callback =
219 base::BindRepeating([](int i) { return i + 1; });
220 OnceCallback<int(int)> once_callback =
221 base::BindOnce([](int i) { return i * 2; });
222 EXPECT_EQ(
223 3, std::move(once_callback).Then(std::move(repeating_callback)).Run(1));
224 }
225 }
226
227 // `Then()` should should allow a return value of type `R` to be passed to a
228 // callback with one parameter of type `const R&` or type `R&&`.
TEST_F(CallbackTest,ThenWithCompatibleButNotSameType)229 TEST_F(CallbackTest, ThenWithCompatibleButNotSameType) {
230 {
231 OnceCallback<std::string()> once_callback =
232 BindOnce([] { return std::string("hello"); });
233 EXPECT_EQ("hello",
234 std::move(once_callback)
235 .Then(BindOnce([](const std::string& s) { return s; }))
236 .Run());
237 }
238
239 class NotCopied {
240 public:
241 NotCopied() = default;
242 NotCopied(NotCopied&&) = default;
243 NotCopied& operator=(NotCopied&&) = default;
244
245 NotCopied(const NotCopied&) {
246 ADD_FAILURE() << "should not have been copied";
247 }
248
249 NotCopied& operator=(const NotCopied&) {
250 ADD_FAILURE() << "should not have been copied";
251 return *this;
252 }
253 };
254
255 {
256 OnceCallback<NotCopied()> once_callback =
257 BindOnce([] { return NotCopied(); });
258 std::move(once_callback).Then(BindOnce([](const NotCopied&) {})).Run();
259 }
260
261 {
262 OnceCallback<NotCopied()> once_callback =
263 BindOnce([] { return NotCopied(); });
264 std::move(once_callback).Then(BindOnce([](NotCopied&&) {})).Run();
265 }
266 }
267
268 // A factory class for building an outer and inner callback for calling
269 // Then() on either a OnceCallback or RepeatingCallback with combinations of
270 // void return types, non-void, and move-only return types.
271 template <bool use_once, typename R, typename ThenR, typename... Args>
272 class CallbackThenTest;
273 template <bool use_once, typename R, typename ThenR, typename... Args>
274 class CallbackThenTest<use_once, R(Args...), ThenR> {
275 public:
276 using CallbackType =
277 typename std::conditional<use_once,
278 OnceCallback<R(Args...)>,
279 RepeatingCallback<R(Args...)>>::type;
280 using ThenType =
281 typename std::conditional<use_once, OnceClosure, RepeatingClosure>::type;
282
283 // Gets the Callback that will have Then() called on it. Has a return type
284 // of `R`, which would chain to the inner callback for Then(). Has inputs of
285 // type `Args...`.
GetOuter(std::string & s)286 static auto GetOuter(std::string& s) {
287 s = "";
288 return Bind(
289 [](std::string* s, Args... args) {
290 return Outer(s, std::forward<Args>(args)...);
291 },
292 &s);
293 }
294 // Gets the Callback that will be passed to Then(). Has a return type of
295 // `ThenR`, specified for the class instance. Receives as input the return
296 // type `R` from the function bound and returned in GetOuter().
GetInner(std::string & s)297 static auto GetInner(std::string& s) { return Bind(&Inner<R, ThenR>, &s); }
298
299 private:
300 template <bool bind_once = use_once,
301 typename F,
302 typename... FArgs,
303 std::enable_if_t<bind_once, int> = 0>
Bind(F function,FArgs...args)304 static auto Bind(F function, FArgs... args) {
305 return BindOnce(function, std::forward<FArgs>(args)...);
306 }
307 template <bool bind_once = use_once,
308 typename F,
309 typename... FArgs,
310 std::enable_if_t<!bind_once, int> = 0>
Bind(F function,FArgs...args)311 static auto Bind(F function, FArgs... args) {
312 return BindRepeating(function, std::forward<FArgs>(args)...);
313 }
314
315 template <typename R2 = R, std::enable_if_t<!std::is_void_v<R2>, int> = 0>
Outer(std::string * s,std::unique_ptr<int> a,std::unique_ptr<int> b)316 static int Outer(std::string* s,
317 std::unique_ptr<int> a,
318 std::unique_ptr<int> b) {
319 *s += "Outer";
320 *s += base::NumberToString(*a) + base::NumberToString(*b);
321 return *a + *b;
322 }
323 template <typename R2 = R, std::enable_if_t<!std::is_void_v<R2>, int> = 0>
Outer(std::string * s,int a,int b)324 static int Outer(std::string* s, int a, int b) {
325 *s += "Outer";
326 *s += base::NumberToString(a) + base::NumberToString(b);
327 return a + b;
328 }
329 template <typename R2 = R, std::enable_if_t<!std::is_void_v<R2>, int> = 0>
Outer(std::string * s)330 static int Outer(std::string* s) {
331 *s += "Outer";
332 *s += "None";
333 return 99;
334 }
335
336 template <typename R2 = R, std::enable_if_t<std::is_void_v<R2>, int> = 0>
Outer(std::string * s,std::unique_ptr<int> a,std::unique_ptr<int> b)337 static void Outer(std::string* s,
338 std::unique_ptr<int> a,
339 std::unique_ptr<int> b) {
340 *s += "Outer";
341 *s += base::NumberToString(*a) + base::NumberToString(*b);
342 }
343 template <typename R2 = R, std::enable_if_t<std::is_void_v<R2>, int> = 0>
Outer(std::string * s,int a,int b)344 static void Outer(std::string* s, int a, int b) {
345 *s += "Outer";
346 *s += base::NumberToString(a) + base::NumberToString(b);
347 }
348 template <typename R2 = R, std::enable_if_t<std::is_void_v<R2>, int> = 0>
Outer(std::string * s)349 static void Outer(std::string* s) {
350 *s += "Outer";
351 *s += "None";
352 }
353
354 template <typename OuterR,
355 typename InnerR,
356 std::enable_if_t<!std::is_void_v<OuterR>, int> = 0,
357 std::enable_if_t<!std::is_void_v<InnerR>, int> = 0>
Inner(std::string * s,OuterR a)358 static int Inner(std::string* s, OuterR a) {
359 static_assert(std::is_same_v<InnerR, int>, "Use int return type");
360 *s += "Inner";
361 *s += base::NumberToString(a);
362 return a;
363 }
364 template <typename OuterR,
365 typename InnerR,
366 std::enable_if_t<std::is_void_v<OuterR>, int> = 0,
367 std::enable_if_t<!std::is_void_v<InnerR>, int> = 0>
Inner(std::string * s)368 static int Inner(std::string* s) {
369 static_assert(std::is_same_v<InnerR, int>, "Use int return type");
370 *s += "Inner";
371 *s += "None";
372 return 99;
373 }
374
375 template <typename OuterR,
376 typename InnerR,
377 std::enable_if_t<!std::is_void_v<OuterR>, int> = 0,
378 std::enable_if_t<std::is_void_v<InnerR>, int> = 0>
Inner(std::string * s,OuterR a)379 static void Inner(std::string* s, OuterR a) {
380 *s += "Inner";
381 *s += base::NumberToString(a);
382 }
383 template <typename OuterR,
384 typename InnerR,
385 std::enable_if_t<std::is_void_v<OuterR>, int> = 0,
386 std::enable_if_t<std::is_void_v<InnerR>, int> = 0>
Inner(std::string * s)387 static void Inner(std::string* s) {
388 *s += "Inner";
389 *s += "None";
390 }
391 };
392
393 template <typename R, typename ThenR = void, typename... Args>
394 using CallbackThenOnceTest = CallbackThenTest<true, R, ThenR, Args...>;
395 template <typename R, typename ThenR = void, typename... Args>
396 using CallbackThenRepeatingTest = CallbackThenTest<false, R, ThenR, Args...>;
397
TEST_F(CallbackTest,ThenOnce)398 TEST_F(CallbackTest, ThenOnce) {
399 std::string s;
400
401 // Void return from outer + void return from Then().
402 {
403 using VoidReturnWithoutArgs = void();
404 using ThenReturn = void;
405 using Test = CallbackThenOnceTest<VoidReturnWithoutArgs, ThenReturn>;
406 Test::GetOuter(s).Then(Test::GetInner(s)).Run();
407 EXPECT_EQ(s, "OuterNoneInnerNone");
408 }
409 {
410 using VoidReturnWithArgs = void(int, int);
411 using ThenReturn = void;
412 using Test = CallbackThenOnceTest<VoidReturnWithArgs, ThenReturn>;
413 Test::GetOuter(s).Then(Test::GetInner(s)).Run(1, 2);
414 EXPECT_EQ(s, "Outer12InnerNone");
415 }
416 {
417 using VoidReturnWithMoveOnlyArgs =
418 void(std::unique_ptr<int>, std::unique_ptr<int>);
419 using ThenReturn = void;
420 using Test = CallbackThenOnceTest<VoidReturnWithMoveOnlyArgs, ThenReturn>;
421 Test::GetOuter(s)
422 .Then(Test::GetInner(s))
423 .Run(std::make_unique<int>(1), std::make_unique<int>(2));
424 EXPECT_EQ(s, "Outer12InnerNone");
425 }
426
427 // Void return from outer + non-void return from Then().
428 {
429 using VoidReturnWithoutArgs = void();
430 using ThenReturn = int;
431 using Test = CallbackThenOnceTest<VoidReturnWithoutArgs, ThenReturn>;
432 EXPECT_EQ(99, Test::GetOuter(s).Then(Test::GetInner(s)).Run());
433 EXPECT_EQ(s, "OuterNoneInnerNone");
434 }
435 {
436 using VoidReturnWithArgs = void(int, int);
437 using ThenReturn = int;
438 using Test = CallbackThenOnceTest<VoidReturnWithArgs, ThenReturn>;
439 EXPECT_EQ(99, Test::GetOuter(s).Then(Test::GetInner(s)).Run(1, 2));
440 EXPECT_EQ(s, "Outer12InnerNone");
441 }
442 {
443 using VoidReturnWithMoveOnlyArgs =
444 void(std::unique_ptr<int>, std::unique_ptr<int>);
445 using ThenReturn = int;
446 using Test = CallbackThenOnceTest<VoidReturnWithMoveOnlyArgs, ThenReturn>;
447 EXPECT_EQ(99, Test::GetOuter(s)
448 .Then(Test::GetInner(s))
449 .Run(std::make_unique<int>(1), std::make_unique<int>(2)));
450 EXPECT_EQ(s, "Outer12InnerNone");
451 }
452
453 // Non-void return from outer + void return from Then().
454 {
455 using NonVoidReturnWithoutArgs = int();
456 using ThenReturn = void;
457 using Test = CallbackThenOnceTest<NonVoidReturnWithoutArgs, ThenReturn>;
458 Test::GetOuter(s).Then(Test::GetInner(s)).Run();
459 EXPECT_EQ(s, "OuterNoneInner99");
460 }
461 {
462 using NonVoidReturnWithArgs = int(int, int);
463 using ThenReturn = void;
464 using Test = CallbackThenOnceTest<NonVoidReturnWithArgs, ThenReturn>;
465 Test::GetOuter(s).Then(Test::GetInner(s)).Run(1, 2);
466 EXPECT_EQ(s, "Outer12Inner3");
467 }
468 {
469 using NonVoidReturnWithMoveOnlyArgs =
470 int(std::unique_ptr<int>, std::unique_ptr<int>);
471 using ThenReturn = void;
472 using Test =
473 CallbackThenOnceTest<NonVoidReturnWithMoveOnlyArgs, ThenReturn>;
474 Test::GetOuter(s)
475 .Then(Test::GetInner(s))
476 .Run(std::make_unique<int>(1), std::make_unique<int>(2));
477 EXPECT_EQ(s, "Outer12Inner3");
478 }
479
480 // Non-void return from outer + non-void return from Then().
481 {
482 using NonVoidReturnWithoutArgs = int();
483 using ThenReturn = int;
484 using Test = CallbackThenOnceTest<NonVoidReturnWithoutArgs, ThenReturn>;
485 EXPECT_EQ(99, Test::GetOuter(s).Then(Test::GetInner(s)).Run());
486 EXPECT_EQ(s, "OuterNoneInner99");
487 }
488 {
489 using NonVoidReturnWithArgs = int(int, int);
490 using ThenReturn = int;
491 using Test = CallbackThenOnceTest<NonVoidReturnWithArgs, ThenReturn>;
492 EXPECT_EQ(3, Test::GetOuter(s).Then(Test::GetInner(s)).Run(1, 2));
493 EXPECT_EQ(s, "Outer12Inner3");
494 }
495 {
496 using NonVoidReturnWithMoveOnlyArgs =
497 int(std::unique_ptr<int>, std::unique_ptr<int>);
498 using ThenReturn = int;
499 using Test =
500 CallbackThenOnceTest<NonVoidReturnWithMoveOnlyArgs, ThenReturn>;
501 EXPECT_EQ(3, Test::GetOuter(s)
502 .Then(Test::GetInner(s))
503 .Run(std::make_unique<int>(1), std::make_unique<int>(2)));
504 EXPECT_EQ(s, "Outer12Inner3");
505 }
506 }
507
TEST_F(CallbackTest,ThenRepeating)508 TEST_F(CallbackTest, ThenRepeating) {
509 std::string s;
510
511 // Void return from outer + void return from Then().
512 {
513 using VoidReturnWithoutArgs = void();
514 using ThenReturn = void;
515 using Test = CallbackThenRepeatingTest<VoidReturnWithoutArgs, ThenReturn>;
516 auto outer = Test::GetOuter(s);
517 outer.Then(Test::GetInner(s)).Run();
518 EXPECT_EQ(s, "OuterNoneInnerNone");
519 std::move(outer).Then(Test::GetInner(s)).Run();
520 EXPECT_EQ(s, "OuterNoneInnerNoneOuterNoneInnerNone");
521 }
522 {
523 using VoidReturnWithArgs = void(int, int);
524 using ThenReturn = void;
525 using Test = CallbackThenRepeatingTest<VoidReturnWithArgs, ThenReturn>;
526 auto outer = Test::GetOuter(s);
527 outer.Then(Test::GetInner(s)).Run(1, 2);
528 EXPECT_EQ(s, "Outer12InnerNone");
529 std::move(outer).Then(Test::GetInner(s)).Run(1, 2);
530 EXPECT_EQ(s, "Outer12InnerNoneOuter12InnerNone");
531 }
532 {
533 using VoidReturnWithMoveOnlyArgs =
534 void(std::unique_ptr<int>, std::unique_ptr<int>);
535 using ThenReturn = void;
536 using Test =
537 CallbackThenRepeatingTest<VoidReturnWithMoveOnlyArgs, ThenReturn>;
538 auto outer = Test::GetOuter(s);
539 outer.Then(Test::GetInner(s))
540 .Run(std::make_unique<int>(1), std::make_unique<int>(2));
541 EXPECT_EQ(s, "Outer12InnerNone");
542 std::move(outer)
543 .Then(Test::GetInner(s))
544 .Run(std::make_unique<int>(1), std::make_unique<int>(2));
545 EXPECT_EQ(s, "Outer12InnerNoneOuter12InnerNone");
546 }
547
548 // Void return from outer + non-void return from Then().
549 {
550 using VoidReturnWithoutArgs = void();
551 using ThenReturn = int;
552 using Test = CallbackThenRepeatingTest<VoidReturnWithoutArgs, ThenReturn>;
553 auto outer = Test::GetOuter(s);
554 EXPECT_EQ(99, outer.Then(Test::GetInner(s)).Run());
555 EXPECT_EQ(s, "OuterNoneInnerNone");
556 EXPECT_EQ(99, std::move(outer).Then(Test::GetInner(s)).Run());
557 EXPECT_EQ(s, "OuterNoneInnerNoneOuterNoneInnerNone");
558 }
559 {
560 using VoidReturnWithArgs = void(int, int);
561 using ThenReturn = int;
562 using Test = CallbackThenRepeatingTest<VoidReturnWithArgs, ThenReturn>;
563 auto outer = Test::GetOuter(s);
564 EXPECT_EQ(99, outer.Then(Test::GetInner(s)).Run(1, 2));
565 EXPECT_EQ(s, "Outer12InnerNone");
566 EXPECT_EQ(99, std::move(outer).Then(Test::GetInner(s)).Run(1, 2));
567 EXPECT_EQ(s, "Outer12InnerNoneOuter12InnerNone");
568 }
569 {
570 using VoidReturnWithMoveOnlyArgs =
571 void(std::unique_ptr<int>, std::unique_ptr<int>);
572 using ThenReturn = int;
573 using Test =
574 CallbackThenRepeatingTest<VoidReturnWithMoveOnlyArgs, ThenReturn>;
575 auto outer = Test::GetOuter(s);
576 EXPECT_EQ(99, outer.Then(Test::GetInner(s))
577 .Run(std::make_unique<int>(1), std::make_unique<int>(2)));
578 EXPECT_EQ(s, "Outer12InnerNone");
579 EXPECT_EQ(99, std::move(outer)
580 .Then(Test::GetInner(s))
581 .Run(std::make_unique<int>(1), std::make_unique<int>(2)));
582 EXPECT_EQ(s, "Outer12InnerNoneOuter12InnerNone");
583 }
584
585 // Non-void return from outer + void return from Then().
586 {
587 using NonVoidReturnWithoutArgs = int();
588 using ThenReturn = void;
589 using Test =
590 CallbackThenRepeatingTest<NonVoidReturnWithoutArgs, ThenReturn>;
591 auto outer = Test::GetOuter(s);
592 outer.Then(Test::GetInner(s)).Run();
593 EXPECT_EQ(s, "OuterNoneInner99");
594 std::move(outer).Then(Test::GetInner(s)).Run();
595 EXPECT_EQ(s, "OuterNoneInner99OuterNoneInner99");
596 }
597 {
598 using NonVoidReturnWithArgs = int(int, int);
599 using ThenReturn = void;
600 using Test = CallbackThenRepeatingTest<NonVoidReturnWithArgs, ThenReturn>;
601 auto outer = Test::GetOuter(s);
602 outer.Then(Test::GetInner(s)).Run(1, 2);
603 EXPECT_EQ(s, "Outer12Inner3");
604 std::move(outer).Then(Test::GetInner(s)).Run(1, 2);
605 EXPECT_EQ(s, "Outer12Inner3Outer12Inner3");
606 }
607 {
608 using NonVoidReturnWithMoveOnlyArgs =
609 int(std::unique_ptr<int>, std::unique_ptr<int>);
610 using ThenReturn = void;
611 using Test =
612 CallbackThenRepeatingTest<NonVoidReturnWithMoveOnlyArgs, ThenReturn>;
613 auto outer = Test::GetOuter(s);
614 outer.Then(Test::GetInner(s))
615 .Run(std::make_unique<int>(1), std::make_unique<int>(2));
616 EXPECT_EQ(s, "Outer12Inner3");
617 std::move(outer)
618 .Then(Test::GetInner(s))
619 .Run(std::make_unique<int>(1), std::make_unique<int>(2));
620 EXPECT_EQ(s, "Outer12Inner3Outer12Inner3");
621 }
622
623 // Non-void return from outer + non-void return from Then().
624 {
625 using NonVoidReturnWithoutArgs = int();
626 using ThenReturn = int;
627 using Test =
628 CallbackThenRepeatingTest<NonVoidReturnWithoutArgs, ThenReturn>;
629 auto outer = Test::GetOuter(s);
630 EXPECT_EQ(99, outer.Then(Test::GetInner(s)).Run());
631 EXPECT_EQ(s, "OuterNoneInner99");
632 EXPECT_EQ(99, std::move(outer).Then(Test::GetInner(s)).Run());
633 EXPECT_EQ(s, "OuterNoneInner99OuterNoneInner99");
634 }
635 {
636 using NonVoidReturnWithArgs = int(int, int);
637 using ThenReturn = int;
638 using Test = CallbackThenRepeatingTest<NonVoidReturnWithArgs, ThenReturn>;
639 auto outer = Test::GetOuter(s);
640 EXPECT_EQ(3, outer.Then(Test::GetInner(s)).Run(1, 2));
641 EXPECT_EQ(s, "Outer12Inner3");
642 EXPECT_EQ(3, std::move(outer).Then(Test::GetInner(s)).Run(1, 2));
643 EXPECT_EQ(s, "Outer12Inner3Outer12Inner3");
644 }
645 {
646 using NonVoidReturnWithMoveOnlyArgs =
647 int(std::unique_ptr<int>, std::unique_ptr<int>);
648 using ThenReturn = int;
649 using Test =
650 CallbackThenRepeatingTest<NonVoidReturnWithMoveOnlyArgs, ThenReturn>;
651 auto outer = Test::GetOuter(s);
652 EXPECT_EQ(3, outer.Then(Test::GetInner(s))
653 .Run(std::make_unique<int>(1), std::make_unique<int>(2)));
654 EXPECT_EQ(s, "Outer12Inner3");
655 EXPECT_EQ(3, std::move(outer)
656 .Then(Test::GetInner(s))
657 .Run(std::make_unique<int>(1), std::make_unique<int>(2)));
658 EXPECT_EQ(s, "Outer12Inner3Outer12Inner3");
659 }
660 }
661
662 // WeakPtr detection in BindRepeating() requires a method, not just any
663 // function.
664 class ClassWithAMethod {
665 public:
TheMethod()666 void TheMethod() { method_called = true; }
667
668 bool method_called = false;
669 };
670
TEST_F(CallbackTest,MaybeValidInvalidateWeakPtrsOnSameSequence)671 TEST_F(CallbackTest, MaybeValidInvalidateWeakPtrsOnSameSequence) {
672 ClassWithAMethod obj;
673 WeakPtrFactory<ClassWithAMethod> factory(&obj);
674 WeakPtr<ClassWithAMethod> ptr = factory.GetWeakPtr();
675
676 RepeatingCallback<void()> cb =
677 BindRepeating(&ClassWithAMethod::TheMethod, ptr);
678 EXPECT_TRUE(cb.MaybeValid());
679 EXPECT_FALSE(cb.IsCancelled());
680
681 factory.InvalidateWeakPtrs();
682 // MaybeValid() should be false and IsCancelled() should become true because
683 // InvalidateWeakPtrs() was called on the same thread.
684 EXPECT_FALSE(cb.MaybeValid());
685 EXPECT_TRUE(cb.IsCancelled());
686 // is_null() is not affected by the invalidated WeakPtr.
687 EXPECT_FALSE(cb.is_null());
688 }
689
TEST_F(CallbackTest,MaybeValidInvalidateWeakPtrsOnOtherSequence)690 TEST_F(CallbackTest, MaybeValidInvalidateWeakPtrsOnOtherSequence) {
691 ClassWithAMethod obj;
692 WeakPtrFactory<ClassWithAMethod> factory(&obj);
693 WeakPtr<ClassWithAMethod> ptr = factory.GetWeakPtr();
694
695 RepeatingCallback<void()> cb =
696 BindRepeating(&ClassWithAMethod::TheMethod, ptr);
697 EXPECT_TRUE(cb.MaybeValid());
698
699 Thread other_thread("other_thread");
700 other_thread.StartAndWaitForTesting();
701 other_thread.task_runner()->PostTask(
702 FROM_HERE,
703 BindOnce(
704 [](RepeatingCallback<void()> cb) {
705 // Check that MaybeValid() _eventually_ returns false.
706 const TimeDelta timeout = TestTimeouts::tiny_timeout();
707 const TimeTicks begin = TimeTicks::Now();
708 while (cb.MaybeValid() && (TimeTicks::Now() - begin) < timeout)
709 PlatformThread::YieldCurrentThread();
710 EXPECT_FALSE(cb.MaybeValid());
711 },
712 cb));
713 factory.InvalidateWeakPtrs();
714 // |other_thread|'s destructor will join, ensuring we wait for the task to be
715 // run.
716 }
717
TEST_F(CallbackTest,ThenAfterWeakPtr)718 TEST_F(CallbackTest, ThenAfterWeakPtr) {
719 ClassWithAMethod obj;
720 WeakPtrFactory<ClassWithAMethod> factory(&obj);
721 WeakPtr<ClassWithAMethod> ptr = factory.GetWeakPtr();
722
723 // If the first callback of a chain is skipped due to InvalidateWeakPtrs(),
724 // the remaining callbacks should still run.
725 bool chained_closure_called = false;
726 OnceClosure closure =
727 BindOnce(&ClassWithAMethod::TheMethod, ptr)
728 .Then(BindLambdaForTesting(
729 [&chained_closure_called] { chained_closure_called = true; }));
730 factory.InvalidateWeakPtrs();
731 std::move(closure).Run();
732 EXPECT_FALSE(obj.method_called);
733 EXPECT_TRUE(chained_closure_called);
734 }
735
736 class CallbackOwner : public base::RefCounted<CallbackOwner> {
737 public:
CallbackOwner(bool * deleted)738 explicit CallbackOwner(bool* deleted) {
739 // WrapRefCounted() here is needed to avoid the check failure in the
740 // BindRepeating implementation, that refuses to create the first reference
741 // to ref-counted objects.
742 callback_ = BindRepeating(&CallbackOwner::Unused, WrapRefCounted(this));
743 deleted_ = deleted;
744 }
Reset()745 void Reset() {
746 callback_.Reset();
747 // We are deleted here if no-one else had a ref to us.
748 }
749
750 private:
751 friend class base::RefCounted<CallbackOwner>;
~CallbackOwner()752 virtual ~CallbackOwner() { *deleted_ = true; }
Unused()753 void Unused() { FAIL() << "Should never be called"; }
754
755 RepeatingClosure callback_;
756 raw_ptr<bool> deleted_;
757 };
758
TEST_F(CallbackTest,CallbackHasLastRefOnContainingObject)759 TEST_F(CallbackTest, CallbackHasLastRefOnContainingObject) {
760 bool deleted = false;
761 CallbackOwner* owner = new CallbackOwner(&deleted);
762 owner->Reset();
763 ASSERT_TRUE(deleted);
764 }
765
766 // According to legends, it is good practice to put death tests into their own
767 // test suite, so they are grouped separately from regular tests, since death
768 // tests are somewhat slow and have quirks that can slow down test running if
769 // intermixed.
TEST(CallbackDeathTest,RunNullCallbackChecks)770 TEST(CallbackDeathTest, RunNullCallbackChecks) {
771 {
772 base::OnceClosure closure;
773 EXPECT_CHECK_DEATH(std::move(closure).Run());
774 }
775
776 {
777 base::RepeatingClosure closure;
778 EXPECT_CHECK_DEATH(std::move(closure).Run());
779 }
780
781 {
782 base::RepeatingClosure closure;
783 EXPECT_CHECK_DEATH(closure.Run());
784 }
785 }
786
787 } // namespace
788 } // namespace base
789