| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/bind.h" | 5 #include "base/bind.h" |
| 6 | 6 |
| 7 #if defined(BASE_CALLBACK_H_) | 7 #if defined(BASE_CALLBACK_H_) |
| 8 // We explicitly do not want to include callback.h so people are not tempted | 8 // We explicitly do not want to include callback.h so people are not tempted |
| 9 // to use bind.h in a headerfile for getting the Callback types. | 9 // to use bind.h in a headerfile for getting the Callback types. |
| 10 #error "base/bind.h should avoid pulling in callback.h by default." | 10 #error "base/bind.h should avoid pulling in callback.h by default." |
| (...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 176 } | 176 } |
| 177 | 177 |
| 178 int UnwrapNoRefParentConstRef(const NoRefParent& p) { | 178 int UnwrapNoRefParentConstRef(const NoRefParent& p) { |
| 179 return p.value; | 179 return p.value; |
| 180 } | 180 } |
| 181 | 181 |
| 182 void RefArgSet(int &n) { | 182 void RefArgSet(int &n) { |
| 183 n = 2; | 183 n = 2; |
| 184 } | 184 } |
| 185 | 185 |
| 186 int FunctionWithWeakFirstParam(WeakPtr<NoRef> o, int n) { |
| 187 return n; |
| 188 } |
| 189 |
| 186 // Only useful in no-compile tests. | 190 // Only useful in no-compile tests. |
| 187 int UnwrapNoRefParentRef(Parent& p) { | 191 int UnwrapNoRefParentRef(Parent& p) { |
| 188 return p.value; | 192 return p.value; |
| 189 } | 193 } |
| 190 | 194 |
| 191 class BindTest : public ::testing::Test { | 195 class BindTest : public ::testing::Test { |
| 192 public: | 196 public: |
| 193 BindTest() { | 197 BindTest() { |
| 194 const_has_ref_ptr_ = &has_ref_; | 198 const_has_ref_ptr_ = &has_ref_; |
| 195 const_no_ref_ptr_ = &no_ref_; | 199 const_no_ref_ptr_ = &no_ref_; |
| (...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 444 // inheritance. | 448 // inheritance. |
| 445 EXPECT_TRUE(internal::SupportsAddRefAndRelease<StrictMock<HasRef> >::value); | 449 EXPECT_TRUE(internal::SupportsAddRefAndRelease<StrictMock<HasRef> >::value); |
| 446 EXPECT_FALSE(internal::SupportsAddRefAndRelease<StrictMock<NoRef> >::value); | 450 EXPECT_FALSE(internal::SupportsAddRefAndRelease<StrictMock<NoRef> >::value); |
| 447 | 451 |
| 448 // This matters because the implementation creates a dummy class that | 452 // This matters because the implementation creates a dummy class that |
| 449 // inherits from the template type. | 453 // inherits from the template type. |
| 450 EXPECT_TRUE(internal::SupportsAddRefAndRelease<HasRefPrivateDtor>::value); | 454 EXPECT_TRUE(internal::SupportsAddRefAndRelease<HasRefPrivateDtor>::value); |
| 451 } | 455 } |
| 452 | 456 |
| 453 // Unretained() wrapper support. | 457 // Unretained() wrapper support. |
| 454 // - Method bound to Unretained() non-object. | 458 // - Method bound to Unretained() non-const object. |
| 455 // - Const method bound to Unretained() non-const object. | 459 // - Const method bound to Unretained() non-const object. |
| 456 // - Const method bound to Unretained() const object. | 460 // - Const method bound to Unretained() const object. |
| 457 TEST_F(BindTest, Unretained) { | 461 TEST_F(BindTest, Unretained) { |
| 458 EXPECT_CALL(no_ref_, VoidMethod0()); | 462 EXPECT_CALL(no_ref_, VoidMethod0()); |
| 459 EXPECT_CALL(no_ref_, VoidConstMethod0()).Times(2); | 463 EXPECT_CALL(no_ref_, VoidConstMethod0()).Times(2); |
| 460 | 464 |
| 461 Callback<void(void)> method_cb = | 465 Callback<void(void)> method_cb = |
| 462 Bind(&NoRef::VoidMethod0, Unretained(&no_ref_)); | 466 Bind(&NoRef::VoidMethod0, Unretained(&no_ref_)); |
| 463 method_cb.Run(); | 467 method_cb.Run(); |
| 464 | 468 |
| 465 Callback<void(void)> const_method_cb = | 469 Callback<void(void)> const_method_cb = |
| 466 Bind(&NoRef::VoidConstMethod0, Unretained(&no_ref_)); | 470 Bind(&NoRef::VoidConstMethod0, Unretained(&no_ref_)); |
| 467 const_method_cb.Run(); | 471 const_method_cb.Run(); |
| 468 | 472 |
| 469 Callback<void(void)> const_method_const_ptr_cb = | 473 Callback<void(void)> const_method_const_ptr_cb = |
| 470 Bind(&NoRef::VoidConstMethod0, Unretained(const_no_ref_ptr_)); | 474 Bind(&NoRef::VoidConstMethod0, Unretained(const_no_ref_ptr_)); |
| 471 const_method_const_ptr_cb.Run(); | 475 const_method_const_ptr_cb.Run(); |
| 472 } | 476 } |
| 473 | 477 |
| 478 // WeakPtr() support. |
| 479 // - Method bound to WeakPtr<> to non-const object. |
| 480 // - Const method bound to WeakPtr<> to non-const object. |
| 481 // - Const method bound to WeakPtr<> to const object. |
| 482 // - Normal Function with WeakPtr<> as P1 can have return type and is |
| 483 // not canceled. |
| 484 TEST_F(BindTest, WeakPtr) { |
| 485 EXPECT_CALL(no_ref_, VoidMethod0()); |
| 486 EXPECT_CALL(no_ref_, VoidConstMethod0()).Times(2); |
| 487 |
| 488 WeakPtrFactory<NoRef> weak_factory(&no_ref_); |
| 489 WeakPtrFactory<const NoRef> const_weak_factory(const_no_ref_ptr_); |
| 490 |
| 491 Callback<void(void)> method_cb = |
| 492 Bind(&NoRef::VoidMethod0, weak_factory.GetWeakPtr()); |
| 493 method_cb.Run(); |
| 494 |
| 495 Callback<void(void)> const_method_cb = |
| 496 Bind(&NoRef::VoidConstMethod0, const_weak_factory.GetWeakPtr()); |
| 497 const_method_cb.Run(); |
| 498 |
| 499 Callback<void(void)> const_method_const_ptr_cb = |
| 500 Bind(&NoRef::VoidConstMethod0, const_weak_factory.GetWeakPtr()); |
| 501 const_method_const_ptr_cb.Run(); |
| 502 |
| 503 Callback<int(int)> normal_func_cb = |
| 504 Bind(&FunctionWithWeakFirstParam, weak_factory.GetWeakPtr()); |
| 505 EXPECT_EQ(1, normal_func_cb.Run(1)); |
| 506 |
| 507 weak_factory.InvalidateWeakPtrs(); |
| 508 const_weak_factory.InvalidateWeakPtrs(); |
| 509 |
| 510 method_cb.Run(); |
| 511 const_method_cb.Run(); |
| 512 const_method_const_ptr_cb.Run(); |
| 513 |
| 514 // Still runs even after the pointers are invalidated. |
| 515 EXPECT_EQ(2, normal_func_cb.Run(2)); |
| 516 } |
| 517 |
| 474 // ConstRef() wrapper support. | 518 // ConstRef() wrapper support. |
| 475 // - Binding w/o ConstRef takes a copy. | 519 // - Binding w/o ConstRef takes a copy. |
| 476 // - Binding a ConstRef takes a reference. | 520 // - Binding a ConstRef takes a reference. |
| 477 // - Binding ConstRef to a function ConstRef does not copy on invoke. | 521 // - Binding ConstRef to a function ConstRef does not copy on invoke. |
| 478 TEST_F(BindTest, ConstRef) { | 522 TEST_F(BindTest, ConstRef) { |
| 479 int n = 1; | 523 int n = 1; |
| 480 | 524 |
| 481 Callback<int(void)> copy_cb = Bind(&Identity, n); | 525 Callback<int(void)> copy_cb = Bind(&Identity, n); |
| 482 Callback<int(void)> const_ref_cb = Bind(&Identity, ConstRef(n)); | 526 Callback<int(void)> const_ref_cb = Bind(&Identity, ConstRef(n)); |
| 483 EXPECT_EQ(n, copy_cb.Run()); | 527 EXPECT_EQ(n, copy_cb.Run()); |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 620 // HasRef p[10]; | 664 // HasRef p[10]; |
| 621 // Callback<void(void)> method_bound_to_array_cb = | 665 // Callback<void(void)> method_bound_to_array_cb = |
| 622 // Bind(&HasRef::VoidConstMethod0, p); | 666 // Bind(&HasRef::VoidConstMethod0, p); |
| 623 // method_bound_to_array_cb.Run(); | 667 // method_bound_to_array_cb.Run(); |
| 624 | 668 |
| 625 // - Refcounted types should not be bound as a raw pointer. | 669 // - Refcounted types should not be bound as a raw pointer. |
| 626 // HasRef for_raw_ptr; | 670 // HasRef for_raw_ptr; |
| 627 // Callback<void(void)> ref_count_as_raw_ptr = | 671 // Callback<void(void)> ref_count_as_raw_ptr = |
| 628 // Bind(&VoidPolymorphic1<HasRef*>, &for_raw_ptr); | 672 // Bind(&VoidPolymorphic1<HasRef*>, &for_raw_ptr); |
| 629 | 673 |
| 674 // - WeakPtrs cannot be bound to methods with return types. |
| 675 // HasRef for_raw_ptr; |
| 676 // WeakPtrFactory<NoRef> weak_factory(&no_ref_); |
| 677 // Callback<int(void)> weak_ptr_with_non_void_return_type = |
| 678 // Bind(&NoRef::IntMethod0, weak_factory.GetWeakPtr()); |
| 679 |
| 630 } | 680 } |
| 631 | 681 |
| 632 #if defined(OS_WIN) | 682 #if defined(OS_WIN) |
| 633 int __fastcall FastCallFunc(int n) { | 683 int __fastcall FastCallFunc(int n) { |
| 634 return n; | 684 return n; |
| 635 } | 685 } |
| 636 | 686 |
| 637 int __stdcall StdCallFunc(int n) { | 687 int __stdcall StdCallFunc(int n) { |
| 638 return n; | 688 return n; |
| 639 } | 689 } |
| 640 | 690 |
| 641 // Windows specific calling convention support. | 691 // Windows specific calling convention support. |
| 642 // - Can bind a __fastcall function. | 692 // - Can bind a __fastcall function. |
| 643 // - Can bind a __stdcall function. | 693 // - Can bind a __stdcall function. |
| 644 TEST_F(BindTest, WindowsCallingConventions) { | 694 TEST_F(BindTest, WindowsCallingConventions) { |
| 645 Callback<int(void)> fastcall_cb = Bind(&FastCallFunc, 1); | 695 Callback<int(void)> fastcall_cb = Bind(&FastCallFunc, 1); |
| 646 EXPECT_EQ(1, fastcall_cb.Run()); | 696 EXPECT_EQ(1, fastcall_cb.Run()); |
| 647 | 697 |
| 648 Callback<int(void)> stdcall_cb = Bind(&StdCallFunc, 2); | 698 Callback<int(void)> stdcall_cb = Bind(&StdCallFunc, 2); |
| 649 EXPECT_EQ(2, stdcall_cb.Run()); | 699 EXPECT_EQ(2, stdcall_cb.Run()); |
| 650 } | 700 } |
| 651 #endif | 701 #endif |
| 652 | 702 |
| 653 } // namespace | 703 } // namespace |
| 654 } // namespace base | 704 } // namespace base |
| OLD | NEW |