1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 #include "base/callback.h"
8 #include "base/memory/ref_counted.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/memory/weak_ptr.h"
11 #include "testing/gmock/include/gmock/gmock.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 using ::testing::Mock
;
15 using ::testing::Return
;
16 using ::testing::StrictMock
;
27 MOCK_METHOD0(VoidMethod0
, void(void));
28 MOCK_CONST_METHOD0(VoidConstMethod0
, void(void));
30 MOCK_METHOD0(IntMethod0
, int(void));
31 MOCK_CONST_METHOD0(IntConstMethod0
, int(void));
34 // Particularly important in this test to ensure no copies are made.
35 DISALLOW_COPY_AND_ASSIGN(NoRef
);
38 class HasRef
: public NoRef
{
42 MOCK_CONST_METHOD0(AddRef
, void(void));
43 MOCK_CONST_METHOD0(Release
, bool(void));
46 // Particularly important in this test to ensure no copies are made.
47 DISALLOW_COPY_AND_ASSIGN(HasRef
);
50 class HasRefPrivateDtor
: public HasRef
{
52 ~HasRefPrivateDtor() {}
55 static const int kParentValue
= 1;
56 static const int kChildValue
= 2;
60 void AddRef(void) const {}
61 void Release(void) const {}
62 virtual void VirtualSet() { value
= kParentValue
; }
63 void NonVirtualSet() { value
= kParentValue
; }
67 class Child
: public Parent
{
69 virtual void VirtualSet() OVERRIDE
{ value
= kChildValue
; }
70 void NonVirtualSet() { value
= kChildValue
; }
75 virtual void VirtualSet() { value
= kParentValue
; }
76 void NonVirtualSet() { value
= kParentValue
; }
80 class NoRefChild
: public NoRefParent
{
81 virtual void VirtualSet() OVERRIDE
{ value
= kChildValue
; }
82 void NonVirtualSet() { value
= kChildValue
; }
85 // Used for probing the number of copies that occur if a type must be coerced
86 // during argument forwarding in the Run() methods.
87 struct DerivedCopyCounter
{
88 DerivedCopyCounter(int* copies
, int* assigns
)
89 : copies_(copies
), assigns_(assigns
) {
95 // Used for probing the number of copies in an argument.
98 CopyCounter(int* copies
, int* assigns
)
99 : copies_(copies
), assigns_(assigns
) {
102 CopyCounter(const CopyCounter
& other
)
103 : copies_(other
.copies_
),
104 assigns_(other
.assigns_
) {
108 // Probing for copies from coercion.
109 explicit CopyCounter(const DerivedCopyCounter
& other
)
110 : copies_(other
.copies_
),
111 assigns_(other
.assigns_
) {
115 const CopyCounter
& operator=(const CopyCounter
& rhs
) {
116 copies_
= rhs
.copies_
;
117 assigns_
= rhs
.assigns_
;
130 int assigns() const {
139 class DeleteCounter
{
141 explicit DeleteCounter(int* deletes
)
142 : deletes_(deletes
) {
149 void VoidMethod0() {}
155 template <typename T
>
156 T
PassThru(T scoper
) {
157 return scoper
.Pass();
160 // Some test functions that we can Bind to.
161 template <typename T
>
162 T
PolymorphicIdentity(T t
) {
166 template <typename T
>
167 void VoidPolymorphic1(T t
) {
170 int Identity(int n
) {
174 int ArrayGet(const int array
[], int n
) {
178 int Sum(int a
, int b
, int c
, int d
, int e
, int f
) {
179 return a
+ b
+ c
+ d
+ e
+ f
;
182 const char* CStringIdentity(const char* s
) {
186 int GetCopies(const CopyCounter
& counter
) {
187 return counter
.copies();
190 int UnwrapNoRefParent(NoRefParent p
) {
194 int UnwrapNoRefParentPtr(NoRefParent
* p
) {
198 int UnwrapNoRefParentConstRef(const NoRefParent
& p
) {
202 void RefArgSet(int &n
) {
206 void PtrArgSet(int *n
) {
210 int FunctionWithWeakFirstParam(WeakPtr
<NoRef
> o
, int n
) {
214 int FunctionWithScopedRefptrFirstParam(const scoped_refptr
<HasRef
>& o
, int n
) {
218 void TakesACallback(const Closure
& callback
) {
222 class BindTest
: public ::testing::Test
{
225 const_has_ref_ptr_
= &has_ref_
;
226 const_no_ref_ptr_
= &no_ref_
;
227 static_func_mock_ptr
= &static_func_mock_
;
230 virtual ~BindTest() {
233 static void VoidFunc0(void) {
234 static_func_mock_ptr
->VoidMethod0();
237 static int IntFunc0(void) { return static_func_mock_ptr
->IntMethod0(); }
240 StrictMock
<NoRef
> no_ref_
;
241 StrictMock
<HasRef
> has_ref_
;
242 const HasRef
* const_has_ref_ptr_
;
243 const NoRef
* const_no_ref_ptr_
;
244 StrictMock
<NoRef
> static_func_mock_
;
246 // Used by the static functions to perform expectations.
247 static StrictMock
<NoRef
>* static_func_mock_ptr
;
250 DISALLOW_COPY_AND_ASSIGN(BindTest
);
253 StrictMock
<NoRef
>* BindTest::static_func_mock_ptr
;
255 // Sanity check that we can instantiate a callback for each arity.
256 TEST_F(BindTest
, ArityTest
) {
257 Callback
<int(void)> c0
= Bind(&Sum
, 32, 16, 8, 4, 2, 1);
258 EXPECT_EQ(63, c0
.Run());
260 Callback
<int(int)> c1
= Bind(&Sum
, 32, 16, 8, 4, 2);
261 EXPECT_EQ(75, c1
.Run(13));
263 Callback
<int(int,int)> c2
= Bind(&Sum
, 32, 16, 8, 4);
264 EXPECT_EQ(85, c2
.Run(13, 12));
266 Callback
<int(int,int,int)> c3
= Bind(&Sum
, 32, 16, 8);
267 EXPECT_EQ(92, c3
.Run(13, 12, 11));
269 Callback
<int(int,int,int,int)> c4
= Bind(&Sum
, 32, 16);
270 EXPECT_EQ(94, c4
.Run(13, 12, 11, 10));
272 Callback
<int(int,int,int,int,int)> c5
= Bind(&Sum
, 32);
273 EXPECT_EQ(87, c5
.Run(13, 12, 11, 10, 9));
275 Callback
<int(int,int,int,int,int,int)> c6
= Bind(&Sum
);
276 EXPECT_EQ(69, c6
.Run(13, 12, 11, 10, 9, 14));
279 // Test the Currying ability of the Callback system.
280 TEST_F(BindTest
, CurryingTest
) {
281 Callback
<int(int,int,int,int,int,int)> c6
= Bind(&Sum
);
282 EXPECT_EQ(69, c6
.Run(13, 12, 11, 10, 9, 14));
284 Callback
<int(int,int,int,int,int)> c5
= Bind(c6
, 32);
285 EXPECT_EQ(87, c5
.Run(13, 12, 11, 10, 9));
287 Callback
<int(int,int,int,int)> c4
= Bind(c5
, 16);
288 EXPECT_EQ(94, c4
.Run(13, 12, 11, 10));
290 Callback
<int(int,int,int)> c3
= Bind(c4
, 8);
291 EXPECT_EQ(92, c3
.Run(13, 12, 11));
293 Callback
<int(int,int)> c2
= Bind(c3
, 4);
294 EXPECT_EQ(85, c2
.Run(13, 12));
296 Callback
<int(int)> c1
= Bind(c2
, 2);
297 EXPECT_EQ(75, c1
.Run(13));
299 Callback
<int(void)> c0
= Bind(c1
, 1);
300 EXPECT_EQ(63, c0
.Run());
303 // Test that currying the rvalue result of another Bind() works correctly.
304 // - rvalue should be usable as argument to Bind().
305 // - multiple runs of resulting Callback remain valid.
306 TEST_F(BindTest
, CurryingRvalueResultOfBind
) {
308 Closure cb
= base::Bind(&TakesACallback
, base::Bind(&PtrArgSet
, &n
));
310 // If we implement Bind() such that the return value has auto_ptr-like
311 // semantics, the second call here will fail because ownership of
312 // the internal BindState<> would have been transfered to a *temporary*
313 // constructon of a Callback object on the first call.
322 // Function type support.
323 // - Normal function.
324 // - Normal function bound with non-refcounted first argument.
325 // - Method bound to non-const object.
326 // - Method bound to scoped_refptr.
327 // - Const method bound to non-const object.
328 // - Const method bound to const object.
329 // - Derived classes can be used with pointers to non-virtual base functions.
330 // - Derived classes can be used with pointers to virtual base functions (and
331 // preserve virtual dispatch).
332 TEST_F(BindTest
, FunctionTypeSupport
) {
333 EXPECT_CALL(static_func_mock_
, VoidMethod0());
334 EXPECT_CALL(has_ref_
, AddRef()).Times(5);
335 EXPECT_CALL(has_ref_
, Release()).Times(5);
336 EXPECT_CALL(has_ref_
, VoidMethod0()).Times(2);
337 EXPECT_CALL(has_ref_
, VoidConstMethod0()).Times(2);
339 Closure normal_cb
= Bind(&VoidFunc0
);
340 Callback
<NoRef
*(void)> normal_non_refcounted_cb
=
341 Bind(&PolymorphicIdentity
<NoRef
*>, &no_ref_
);
343 EXPECT_EQ(&no_ref_
, normal_non_refcounted_cb
.Run());
345 Closure method_cb
= Bind(&HasRef::VoidMethod0
, &has_ref_
);
346 Closure method_refptr_cb
= Bind(&HasRef::VoidMethod0
,
347 make_scoped_refptr(&has_ref_
));
348 Closure const_method_nonconst_obj_cb
= Bind(&HasRef::VoidConstMethod0
,
350 Closure const_method_const_obj_cb
= Bind(&HasRef::VoidConstMethod0
,
353 method_refptr_cb
.Run();
354 const_method_nonconst_obj_cb
.Run();
355 const_method_const_obj_cb
.Run();
359 Closure virtual_set_cb
= Bind(&Parent::VirtualSet
, &child
);
360 virtual_set_cb
.Run();
361 EXPECT_EQ(kChildValue
, child
.value
);
364 Closure non_virtual_set_cb
= Bind(&Parent::NonVirtualSet
, &child
);
365 non_virtual_set_cb
.Run();
366 EXPECT_EQ(kParentValue
, child
.value
);
369 // Return value support.
370 // - Function with return value.
371 // - Method with return value.
372 // - Const method with return value.
373 TEST_F(BindTest
, ReturnValues
) {
374 EXPECT_CALL(static_func_mock_
, IntMethod0()).WillOnce(Return(1337));
375 EXPECT_CALL(has_ref_
, AddRef()).Times(3);
376 EXPECT_CALL(has_ref_
, Release()).Times(3);
377 EXPECT_CALL(has_ref_
, IntMethod0()).WillOnce(Return(31337));
378 EXPECT_CALL(has_ref_
, IntConstMethod0())
379 .WillOnce(Return(41337))
380 .WillOnce(Return(51337));
382 Callback
<int(void)> normal_cb
= Bind(&IntFunc0
);
383 Callback
<int(void)> method_cb
= Bind(&HasRef::IntMethod0
, &has_ref_
);
384 Callback
<int(void)> const_method_nonconst_obj_cb
=
385 Bind(&HasRef::IntConstMethod0
, &has_ref_
);
386 Callback
<int(void)> const_method_const_obj_cb
=
387 Bind(&HasRef::IntConstMethod0
, const_has_ref_ptr_
);
388 EXPECT_EQ(1337, normal_cb
.Run());
389 EXPECT_EQ(31337, method_cb
.Run());
390 EXPECT_EQ(41337, const_method_nonconst_obj_cb
.Run());
391 EXPECT_EQ(51337, const_method_const_obj_cb
.Run());
394 // IgnoreResult adapter test.
395 // - Function with return value.
396 // - Method with return value.
397 // - Const Method with return.
398 // - Method with return value bound to WeakPtr<>.
399 // - Const Method with return bound to WeakPtr<>.
400 TEST_F(BindTest
, IgnoreResult
) {
401 EXPECT_CALL(static_func_mock_
, IntMethod0()).WillOnce(Return(1337));
402 EXPECT_CALL(has_ref_
, AddRef()).Times(2);
403 EXPECT_CALL(has_ref_
, Release()).Times(2);
404 EXPECT_CALL(has_ref_
, IntMethod0()).WillOnce(Return(10));
405 EXPECT_CALL(has_ref_
, IntConstMethod0()).WillOnce(Return(11));
406 EXPECT_CALL(no_ref_
, IntMethod0()).WillOnce(Return(12));
407 EXPECT_CALL(no_ref_
, IntConstMethod0()).WillOnce(Return(13));
409 Closure normal_func_cb
= Bind(IgnoreResult(&IntFunc0
));
410 normal_func_cb
.Run();
412 Closure non_void_method_cb
=
413 Bind(IgnoreResult(&HasRef::IntMethod0
), &has_ref_
);
414 non_void_method_cb
.Run();
416 Closure non_void_const_method_cb
=
417 Bind(IgnoreResult(&HasRef::IntConstMethod0
), &has_ref_
);
418 non_void_const_method_cb
.Run();
420 WeakPtrFactory
<NoRef
> weak_factory(&no_ref_
);
421 WeakPtrFactory
<const NoRef
> const_weak_factory(const_no_ref_ptr_
);
423 Closure non_void_weak_method_cb
=
424 Bind(IgnoreResult(&NoRef::IntMethod0
), weak_factory
.GetWeakPtr());
425 non_void_weak_method_cb
.Run();
427 Closure non_void_weak_const_method_cb
=
428 Bind(IgnoreResult(&NoRef::IntConstMethod0
), weak_factory
.GetWeakPtr());
429 non_void_weak_const_method_cb
.Run();
431 weak_factory
.InvalidateWeakPtrs();
432 non_void_weak_const_method_cb
.Run();
433 non_void_weak_method_cb
.Run();
436 // Argument binding tests.
437 // - Argument binding to primitive.
438 // - Argument binding to primitive pointer.
439 // - Argument binding to a literal integer.
440 // - Argument binding to a literal string.
441 // - Argument binding with template function.
442 // - Argument binding to an object.
443 // - Argument binding to pointer to incomplete type.
444 // - Argument gets type converted.
445 // - Pointer argument gets converted.
446 // - Const Reference forces conversion.
447 TEST_F(BindTest
, ArgumentBinding
) {
450 Callback
<int(void)> bind_primitive_cb
= Bind(&Identity
, n
);
451 EXPECT_EQ(n
, bind_primitive_cb
.Run());
453 Callback
<int*(void)> bind_primitive_pointer_cb
=
454 Bind(&PolymorphicIdentity
<int*>, &n
);
455 EXPECT_EQ(&n
, bind_primitive_pointer_cb
.Run());
457 Callback
<int(void)> bind_int_literal_cb
= Bind(&Identity
, 3);
458 EXPECT_EQ(3, bind_int_literal_cb
.Run());
460 Callback
<const char*(void)> bind_string_literal_cb
=
461 Bind(&CStringIdentity
, "hi");
462 EXPECT_STREQ("hi", bind_string_literal_cb
.Run());
464 Callback
<int(void)> bind_template_function_cb
=
465 Bind(&PolymorphicIdentity
<int>, 4);
466 EXPECT_EQ(4, bind_template_function_cb
.Run());
470 Callback
<int(void)> bind_object_cb
= Bind(&UnwrapNoRefParent
, p
);
471 EXPECT_EQ(5, bind_object_cb
.Run());
473 IncompleteType
* incomplete_ptr
= reinterpret_cast<IncompleteType
*>(123);
474 Callback
<IncompleteType
*(void)> bind_incomplete_ptr_cb
=
475 Bind(&PolymorphicIdentity
<IncompleteType
*>, incomplete_ptr
);
476 EXPECT_EQ(incomplete_ptr
, bind_incomplete_ptr_cb
.Run());
480 Callback
<int(void)> bind_promotes_cb
= Bind(&UnwrapNoRefParent
, c
);
481 EXPECT_EQ(6, bind_promotes_cb
.Run());
484 Callback
<int(void)> bind_pointer_promotes_cb
=
485 Bind(&UnwrapNoRefParentPtr
, &c
);
486 EXPECT_EQ(7, bind_pointer_promotes_cb
.Run());
489 Callback
<int(void)> bind_const_reference_promotes_cb
=
490 Bind(&UnwrapNoRefParentConstRef
, c
);
491 EXPECT_EQ(8, bind_const_reference_promotes_cb
.Run());
494 // Unbound argument type support tests.
496 // - Unbound pointer.
497 // - Unbound reference.
498 // - Unbound const reference.
499 // - Unbound unsized array.
500 // - Unbound sized array.
501 // - Unbound array-of-arrays.
502 TEST_F(BindTest
, UnboundArgumentTypeSupport
) {
503 Callback
<void(int)> unbound_value_cb
= Bind(&VoidPolymorphic1
<int>);
504 Callback
<void(int*)> unbound_pointer_cb
= Bind(&VoidPolymorphic1
<int*>);
505 Callback
<void(int&)> unbound_ref_cb
= Bind(&VoidPolymorphic1
<int&>);
506 Callback
<void(const int&)> unbound_const_ref_cb
=
507 Bind(&VoidPolymorphic1
<const int&>);
508 Callback
<void(int[])> unbound_unsized_array_cb
=
509 Bind(&VoidPolymorphic1
<int[]>);
510 Callback
<void(int[2])> unbound_sized_array_cb
=
511 Bind(&VoidPolymorphic1
<int[2]>);
512 Callback
<void(int[][2])> unbound_array_of_arrays_cb
=
513 Bind(&VoidPolymorphic1
<int[][2]>);
516 // Function with unbound reference parameter.
517 // - Original parameter is modified by callback.
518 TEST_F(BindTest
, UnboundReferenceSupport
) {
520 Callback
<void(int&)> unbound_ref_cb
= Bind(&RefArgSet
);
521 unbound_ref_cb
.Run(n
);
525 // Functions that take reference parameters.
526 // - Forced reference parameter type still stores a copy.
527 // - Forced const reference parameter type still stores a copy.
528 TEST_F(BindTest
, ReferenceArgumentBinding
) {
531 const int& const_ref_n
= n
;
533 Callback
<int(void)> ref_copies_cb
= Bind(&Identity
, ref_n
);
534 EXPECT_EQ(n
, ref_copies_cb
.Run());
536 EXPECT_EQ(n
- 1, ref_copies_cb
.Run());
538 Callback
<int(void)> const_ref_copies_cb
= Bind(&Identity
, const_ref_n
);
539 EXPECT_EQ(n
, const_ref_copies_cb
.Run());
541 EXPECT_EQ(n
- 1, const_ref_copies_cb
.Run());
544 // Check that we can pass in arrays and have them be stored as a pointer.
545 // - Array of values stores a pointer.
546 // - Array of const values stores a pointer.
547 TEST_F(BindTest
, ArrayArgumentBinding
) {
548 int array
[4] = {1, 1, 1, 1};
549 const int (*const_array_ptr
)[4] = &array
;
551 Callback
<int(void)> array_cb
= Bind(&ArrayGet
, array
, 1);
552 EXPECT_EQ(1, array_cb
.Run());
554 Callback
<int(void)> const_array_cb
= Bind(&ArrayGet
, *const_array_ptr
, 1);
555 EXPECT_EQ(1, const_array_cb
.Run());
558 EXPECT_EQ(3, array_cb
.Run());
559 EXPECT_EQ(3, const_array_cb
.Run());
562 // Verify SupportsAddRefAndRelease correctly introspects the class type for
563 // AddRef() and Release().
564 // - Class with AddRef() and Release()
565 // - Class without AddRef() and Release()
566 // - Derived Class with AddRef() and Release()
567 // - Derived Class without AddRef() and Release()
568 // - Derived Class with AddRef() and Release() and a private destructor.
569 TEST_F(BindTest
, SupportsAddRefAndRelease
) {
570 EXPECT_TRUE(internal::SupportsAddRefAndRelease
<HasRef
>::value
);
571 EXPECT_FALSE(internal::SupportsAddRefAndRelease
<NoRef
>::value
);
573 // StrictMock<T> is a derived class of T. So, we use StrictMock<HasRef> and
574 // StrictMock<NoRef> to test that SupportsAddRefAndRelease works over
576 EXPECT_TRUE(internal::SupportsAddRefAndRelease
<StrictMock
<HasRef
> >::value
);
577 EXPECT_FALSE(internal::SupportsAddRefAndRelease
<StrictMock
<NoRef
> >::value
);
579 // This matters because the implementation creates a dummy class that
580 // inherits from the template type.
581 EXPECT_TRUE(internal::SupportsAddRefAndRelease
<HasRefPrivateDtor
>::value
);
584 // Unretained() wrapper support.
585 // - Method bound to Unretained() non-const object.
586 // - Const method bound to Unretained() non-const object.
587 // - Const method bound to Unretained() const object.
588 TEST_F(BindTest
, Unretained
) {
589 EXPECT_CALL(no_ref_
, VoidMethod0());
590 EXPECT_CALL(no_ref_
, VoidConstMethod0()).Times(2);
592 Callback
<void(void)> method_cb
=
593 Bind(&NoRef::VoidMethod0
, Unretained(&no_ref_
));
596 Callback
<void(void)> const_method_cb
=
597 Bind(&NoRef::VoidConstMethod0
, Unretained(&no_ref_
));
598 const_method_cb
.Run();
600 Callback
<void(void)> const_method_const_ptr_cb
=
601 Bind(&NoRef::VoidConstMethod0
, Unretained(const_no_ref_ptr_
));
602 const_method_const_ptr_cb
.Run();
605 // WeakPtr() support.
606 // - Method bound to WeakPtr<> to non-const object.
607 // - Const method bound to WeakPtr<> to non-const object.
608 // - Const method bound to WeakPtr<> to const object.
609 // - Normal Function with WeakPtr<> as P1 can have return type and is
611 TEST_F(BindTest
, WeakPtr
) {
612 EXPECT_CALL(no_ref_
, VoidMethod0());
613 EXPECT_CALL(no_ref_
, VoidConstMethod0()).Times(2);
615 WeakPtrFactory
<NoRef
> weak_factory(&no_ref_
);
616 WeakPtrFactory
<const NoRef
> const_weak_factory(const_no_ref_ptr_
);
619 Bind(&NoRef::VoidMethod0
, weak_factory
.GetWeakPtr());
622 Closure const_method_cb
=
623 Bind(&NoRef::VoidConstMethod0
, const_weak_factory
.GetWeakPtr());
624 const_method_cb
.Run();
626 Closure const_method_const_ptr_cb
=
627 Bind(&NoRef::VoidConstMethod0
, const_weak_factory
.GetWeakPtr());
628 const_method_const_ptr_cb
.Run();
630 Callback
<int(int)> normal_func_cb
=
631 Bind(&FunctionWithWeakFirstParam
, weak_factory
.GetWeakPtr());
632 EXPECT_EQ(1, normal_func_cb
.Run(1));
634 weak_factory
.InvalidateWeakPtrs();
635 const_weak_factory
.InvalidateWeakPtrs();
638 const_method_cb
.Run();
639 const_method_const_ptr_cb
.Run();
641 // Still runs even after the pointers are invalidated.
642 EXPECT_EQ(2, normal_func_cb
.Run(2));
645 // ConstRef() wrapper support.
646 // - Binding w/o ConstRef takes a copy.
647 // - Binding a ConstRef takes a reference.
648 // - Binding ConstRef to a function ConstRef does not copy on invoke.
649 TEST_F(BindTest
, ConstRef
) {
652 Callback
<int(void)> copy_cb
= Bind(&Identity
, n
);
653 Callback
<int(void)> const_ref_cb
= Bind(&Identity
, ConstRef(n
));
654 EXPECT_EQ(n
, copy_cb
.Run());
655 EXPECT_EQ(n
, const_ref_cb
.Run());
657 EXPECT_EQ(n
- 1, copy_cb
.Run());
658 EXPECT_EQ(n
, const_ref_cb
.Run());
662 CopyCounter
counter(&copies
, &assigns
);
663 Callback
<int(void)> all_const_ref_cb
=
664 Bind(&GetCopies
, ConstRef(counter
));
665 EXPECT_EQ(0, all_const_ref_cb
.Run());
666 EXPECT_EQ(0, copies
);
667 EXPECT_EQ(0, assigns
);
670 TEST_F(BindTest
, ScopedRefptr
) {
671 // BUG: The scoped_refptr should cause the only AddRef()/Release() pair. But
672 // due to a bug in base::Bind(), there's an extra call when invoking the
674 // https://code.google.com/p/chromium/issues/detail?id=251937
675 EXPECT_CALL(has_ref_
, AddRef()).Times(2);
676 EXPECT_CALL(has_ref_
, Release()).Times(2);
678 const scoped_refptr
<StrictMock
<HasRef
> > refptr(&has_ref_
);
680 Callback
<int(void)> scoped_refptr_const_ref_cb
=
681 Bind(&FunctionWithScopedRefptrFirstParam
, base::ConstRef(refptr
), 1);
682 EXPECT_EQ(1, scoped_refptr_const_ref_cb
.Run());
685 // Test Owned() support.
686 TEST_F(BindTest
, Owned
) {
688 DeleteCounter
* counter
= new DeleteCounter(&deletes
);
690 // If we don't capture, delete happens on Callback destruction/reset.
691 // return the same value.
692 Callback
<DeleteCounter
*(void)> no_capture_cb
=
693 Bind(&PolymorphicIdentity
<DeleteCounter
*>, Owned(counter
));
694 ASSERT_EQ(counter
, no_capture_cb
.Run());
695 ASSERT_EQ(counter
, no_capture_cb
.Run());
696 EXPECT_EQ(0, deletes
);
697 no_capture_cb
.Reset(); // This should trigger a delete.
698 EXPECT_EQ(1, deletes
);
701 counter
= new DeleteCounter(&deletes
);
702 base::Closure own_object_cb
=
703 Bind(&DeleteCounter::VoidMethod0
, Owned(counter
));
705 EXPECT_EQ(0, deletes
);
706 own_object_cb
.Reset();
707 EXPECT_EQ(1, deletes
);
710 // Passed() wrapper support.
711 // - Passed() can be constructed from a pointer to scoper.
712 // - Passed() can be constructed from a scoper rvalue.
713 // - Using Passed() gives Callback Ownership.
714 // - Ownership is transferred from Callback to callee on the first Run().
715 // - Callback supports unbound arguments.
716 TEST_F(BindTest
, ScopedPtr
) {
719 // Tests the Passed() function's support for pointers.
720 scoped_ptr
<DeleteCounter
> ptr(new DeleteCounter(&deletes
));
721 Callback
<scoped_ptr
<DeleteCounter
>(void)> unused_callback
=
722 Bind(&PassThru
<scoped_ptr
<DeleteCounter
> >, Passed(&ptr
));
723 EXPECT_FALSE(ptr
.get());
724 EXPECT_EQ(0, deletes
);
726 // If we never invoke the Callback, it retains ownership and deletes.
727 unused_callback
.Reset();
728 EXPECT_EQ(1, deletes
);
730 // Tests the Passed() function's support for rvalues.
732 DeleteCounter
* counter
= new DeleteCounter(&deletes
);
733 Callback
<scoped_ptr
<DeleteCounter
>(void)> callback
=
734 Bind(&PassThru
<scoped_ptr
<DeleteCounter
> >,
735 Passed(scoped_ptr
<DeleteCounter
>(counter
)));
736 EXPECT_FALSE(ptr
.get());
737 EXPECT_EQ(0, deletes
);
739 // Check that ownership can be transferred back out.
740 scoped_ptr
<DeleteCounter
> result
= callback
.Run();
741 ASSERT_EQ(counter
, result
.get());
742 EXPECT_EQ(0, deletes
);
744 // Resetting does not delete since ownership was transferred.
746 EXPECT_EQ(0, deletes
);
748 // Ensure that we actually did get ownership.
750 EXPECT_EQ(1, deletes
);
752 // Test unbound argument forwarding.
753 Callback
<scoped_ptr
<DeleteCounter
>(scoped_ptr
<DeleteCounter
>)> cb_unbound
=
754 Bind(&PassThru
<scoped_ptr
<DeleteCounter
> >);
755 ptr
.reset(new DeleteCounter(&deletes
));
756 cb_unbound
.Run(ptr
.Pass());
759 // Argument Copy-constructor usage for non-reference parameters.
760 // - Bound arguments are only copied once.
761 // - Forwarded arguments are only copied once.
762 // - Forwarded arguments with coercions are only copied twice (once for the
763 // coercion, and one for the final dispatch).
764 TEST_F(BindTest
, ArgumentCopies
) {
768 CopyCounter
counter(&copies
, &assigns
);
770 Callback
<void(void)> copy_cb
=
771 Bind(&VoidPolymorphic1
<CopyCounter
>, counter
);
772 EXPECT_GE(1, copies
);
773 EXPECT_EQ(0, assigns
);
777 Callback
<void(CopyCounter
)> forward_cb
=
778 Bind(&VoidPolymorphic1
<CopyCounter
>);
779 forward_cb
.Run(counter
);
780 EXPECT_GE(1, copies
);
781 EXPECT_EQ(0, assigns
);
785 DerivedCopyCounter
dervied(&copies
, &assigns
);
786 Callback
<void(CopyCounter
)> coerce_cb
=
787 Bind(&VoidPolymorphic1
<CopyCounter
>);
788 coerce_cb
.Run(CopyCounter(dervied
));
789 EXPECT_GE(2, copies
);
790 EXPECT_EQ(0, assigns
);
793 // Callback construction and assignment tests.
794 // - Construction from an InvokerStorageHolder should not cause ref/deref.
795 // - Assignment from other callback should only cause one ref
797 // TODO(ajwong): Is there actually a way to test this?
800 int __fastcall
FastCallFunc(int n
) {
804 int __stdcall
StdCallFunc(int n
) {
808 // Windows specific calling convention support.
809 // - Can bind a __fastcall function.
810 // - Can bind a __stdcall function.
811 TEST_F(BindTest
, WindowsCallingConventions
) {
812 Callback
<int(void)> fastcall_cb
= Bind(&FastCallFunc
, 1);
813 EXPECT_EQ(1, fastcall_cb
.Run());
815 Callback
<int(void)> stdcall_cb
= Bind(&StdCallFunc
, 2);
816 EXPECT_EQ(2, stdcall_cb
.Run());
820 #if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) && GTEST_HAS_DEATH_TEST
822 // Test null callbacks cause a DCHECK.
823 TEST(BindDeathTest
, NullCallback
) {
824 base::Callback
<void(int)> null_cb
;
825 ASSERT_TRUE(null_cb
.is_null());
826 EXPECT_DEATH(base::Bind(null_cb
, 42), "");
829 #endif // (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) &&
830 // GTEST_HAS_DEATH_TEST