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