fix chrome/utility lacking dependency on chrome_strings
[chromium-blink-merge.git] / base / bind_unittest.cc
blob2c93d53ee4b59afa26e3312ba0bc1ed1ebb3d396
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.
5 #include "base/bind.h"
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;
18 namespace base {
19 namespace {
21 class IncompleteType;
23 class NoRef {
24 public:
25 NoRef() {}
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));
33 private:
34 // Particularly important in this test to ensure no copies are made.
35 DISALLOW_COPY_AND_ASSIGN(NoRef);
38 class HasRef : public NoRef {
39 public:
40 HasRef() {}
42 MOCK_CONST_METHOD0(AddRef, void(void));
43 MOCK_CONST_METHOD0(Release, bool(void));
45 private:
46 // Particularly important in this test to ensure no copies are made.
47 DISALLOW_COPY_AND_ASSIGN(HasRef);
50 class HasRefPrivateDtor : public HasRef {
51 private:
52 ~HasRefPrivateDtor() {}
55 static const int kParentValue = 1;
56 static const int kChildValue = 2;
58 class Parent {
59 public:
60 void AddRef(void) const {}
61 void Release(void) const {}
62 virtual void VirtualSet() { value = kParentValue; }
63 void NonVirtualSet() { value = kParentValue; }
64 int value;
67 class Child : public Parent {
68 public:
69 virtual void VirtualSet() OVERRIDE { value = kChildValue; }
70 void NonVirtualSet() { value = kChildValue; }
73 class NoRefParent {
74 public:
75 virtual void VirtualSet() { value = kParentValue; }
76 void NonVirtualSet() { value = kParentValue; }
77 int value;
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) {
91 int* copies_;
92 int* assigns_;
95 // Used for probing the number of copies in an argument.
96 class CopyCounter {
97 public:
98 CopyCounter(int* copies, int* assigns)
99 : copies_(copies), assigns_(assigns) {
102 CopyCounter(const CopyCounter& other)
103 : copies_(other.copies_),
104 assigns_(other.assigns_) {
105 (*copies_)++;
108 // Probing for copies from coercion.
109 explicit CopyCounter(const DerivedCopyCounter& other)
110 : copies_(other.copies_),
111 assigns_(other.assigns_) {
112 (*copies_)++;
115 const CopyCounter& operator=(const CopyCounter& rhs) {
116 copies_ = rhs.copies_;
117 assigns_ = rhs.assigns_;
119 if (assigns_) {
120 (*assigns_)++;
123 return *this;
126 int copies() const {
127 return *copies_;
130 int assigns() const {
131 return *assigns_;
134 private:
135 int* copies_;
136 int* assigns_;
139 class DeleteCounter {
140 public:
141 explicit DeleteCounter(int* deletes)
142 : deletes_(deletes) {
145 ~DeleteCounter() {
146 (*deletes_)++;
149 void VoidMethod0() {}
151 private:
152 int* deletes_;
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) {
163 return t;
166 template <typename T>
167 void VoidPolymorphic1(T t) {
170 int Identity(int n) {
171 return n;
174 int ArrayGet(const int array[], int n) {
175 return array[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) {
183 return s;
186 int GetCopies(const CopyCounter& counter) {
187 return counter.copies();
190 int UnwrapNoRefParent(NoRefParent p) {
191 return p.value;
194 int UnwrapNoRefParentPtr(NoRefParent* p) {
195 return p->value;
198 int UnwrapNoRefParentConstRef(const NoRefParent& p) {
199 return p.value;
202 void RefArgSet(int &n) {
203 n = 2;
206 void PtrArgSet(int *n) {
207 *n = 2;
210 int FunctionWithWeakFirstParam(WeakPtr<NoRef> o, int n) {
211 return n;
214 int FunctionWithScopedRefptrFirstParam(const scoped_refptr<HasRef>& o, int n) {
215 return n;
218 void TakesACallback(const Closure& callback) {
219 callback.Run();
222 class BindTest : public ::testing::Test {
223 public:
224 BindTest() {
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(); }
239 protected:
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;
249 private:
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) {
307 int n = 0;
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.
314 cb.Run();
315 EXPECT_EQ(2, n);
317 n = 0;
318 cb.Run();
319 EXPECT_EQ(2, n);
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_);
342 normal_cb.Run();
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,
349 &has_ref_);
350 Closure const_method_const_obj_cb = Bind(&HasRef::VoidConstMethod0,
351 const_has_ref_ptr_);
352 method_cb.Run();
353 method_refptr_cb.Run();
354 const_method_nonconst_obj_cb.Run();
355 const_method_const_obj_cb.Run();
357 Child child;
358 child.value = 0;
359 Closure virtual_set_cb = Bind(&Parent::VirtualSet, &child);
360 virtual_set_cb.Run();
361 EXPECT_EQ(kChildValue, child.value);
363 child.value = 0;
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) {
448 int n = 2;
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());
468 NoRefParent p;
469 p.value = 5;
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());
478 NoRefChild c;
479 c.value = 6;
480 Callback<int(void)> bind_promotes_cb = Bind(&UnwrapNoRefParent, c);
481 EXPECT_EQ(6, bind_promotes_cb.Run());
483 c.value = 7;
484 Callback<int(void)> bind_pointer_promotes_cb =
485 Bind(&UnwrapNoRefParentPtr, &c);
486 EXPECT_EQ(7, bind_pointer_promotes_cb.Run());
488 c.value = 8;
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.
495 // - Unbound value.
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) {
519 int n = 0;
520 Callback<void(int&)> unbound_ref_cb = Bind(&RefArgSet);
521 unbound_ref_cb.Run(n);
522 EXPECT_EQ(2, 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) {
529 int n = 1;
530 int& ref_n = n;
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());
535 n++;
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());
540 n++;
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());
557 array[1] = 3;
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
575 // inheritance.
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_));
594 method_cb.Run();
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
610 // not canceled.
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_);
618 Closure method_cb =
619 Bind(&NoRef::VoidMethod0, weak_factory.GetWeakPtr());
620 method_cb.Run();
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();
637 method_cb.Run();
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) {
650 int n = 1;
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());
656 n++;
657 EXPECT_EQ(n - 1, copy_cb.Run());
658 EXPECT_EQ(n, const_ref_cb.Run());
660 int copies = 0;
661 int assigns = 0;
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
673 // callback.
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) {
687 int deletes = 0;
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);
700 deletes = 0;
701 counter = new DeleteCounter(&deletes);
702 base::Closure own_object_cb =
703 Bind(&DeleteCounter::VoidMethod0, Owned(counter));
704 own_object_cb.Run();
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) {
717 int deletes = 0;
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.
731 deletes = 0;
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.
745 callback.Reset();
746 EXPECT_EQ(0, deletes);
748 // Ensure that we actually did get ownership.
749 result.reset();
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) {
765 int copies = 0;
766 int assigns = 0;
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);
775 copies = 0;
776 assigns = 0;
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);
783 copies = 0;
784 assigns = 0;
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?
799 #if defined(OS_WIN)
800 int __fastcall FastCallFunc(int n) {
801 return n;
804 int __stdcall StdCallFunc(int n) {
805 return 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());
818 #endif
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
832 } // namespace
833 } // namespace base