Updating trunk VERSION from 935.0 to 936.0
[chromium-blink-merge.git] / base / bind_unittest.cc
blob0fdd24115012c53f8fa765f136fa7df457c6675e
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
3 // found in the LICENSE file.
5 #include "base/bind.h"
7 #include "base/callback.h"
8 #include "testing/gmock/include/gmock/gmock.h"
9 #include "testing/gtest/include/gtest/gtest.h"
11 using ::testing::Mock;
12 using ::testing::Return;
13 using ::testing::StrictMock;
15 namespace base {
16 namespace {
18 class IncompleteType;
20 class NoRef {
21 public:
22 NoRef() {}
24 MOCK_METHOD0(VoidMethod0, void(void));
25 MOCK_CONST_METHOD0(VoidConstMethod0, void(void));
27 MOCK_METHOD0(IntMethod0, int(void));
28 MOCK_CONST_METHOD0(IntConstMethod0, int(void));
30 private:
31 // Particularly important in this test to ensure no copies are made.
32 DISALLOW_COPY_AND_ASSIGN(NoRef);
35 class HasRef : public NoRef {
36 public:
37 HasRef() {}
39 MOCK_CONST_METHOD0(AddRef, void(void));
40 MOCK_CONST_METHOD0(Release, bool(void));
42 private:
43 // Particularly important in this test to ensure no copies are made.
44 DISALLOW_COPY_AND_ASSIGN(HasRef);
47 class HasRefPrivateDtor : public HasRef {
48 private:
49 ~HasRefPrivateDtor() {}
52 static const int kParentValue = 1;
53 static const int kChildValue = 2;
55 class Parent {
56 public:
57 void AddRef(void) const {}
58 void Release(void) const {}
59 virtual void VirtualSet() { value = kParentValue; }
60 void NonVirtualSet() { value = kParentValue; }
61 int value;
64 class Child : public Parent {
65 public:
66 virtual void VirtualSet() { value = kChildValue; }
67 void NonVirtualSet() { value = kChildValue; }
70 class NoRefParent {
71 public:
72 virtual void VirtualSet() { value = kParentValue; }
73 void NonVirtualSet() { value = kParentValue; }
74 int value;
77 class NoRefChild : public NoRefParent {
78 virtual void VirtualSet() { value = kChildValue; }
79 void NonVirtualSet() { value = kChildValue; }
82 // Used for probing the number of copies that occur if a type must be coerced
83 // during argument forwarding in the Run() methods.
84 struct DerivedCopyCounter {
85 DerivedCopyCounter(int* copies, int* assigns)
86 : copies_(copies), assigns_(assigns) {
88 int* copies_;
89 int* assigns_;
92 // Used for probing the number of copies in an argument.
93 class CopyCounter {
94 public:
95 CopyCounter(int* copies, int* assigns)
96 : copies_(copies), assigns_(assigns) {
99 CopyCounter(const CopyCounter& other)
100 : copies_(other.copies_),
101 assigns_(other.assigns_) {
102 (*copies_)++;
105 // Probing for copies from coerscion.
106 CopyCounter(const DerivedCopyCounter& other)
107 : copies_(other.copies_),
108 assigns_(other.assigns_) {
109 (*copies_)++;
112 const CopyCounter& operator=(const CopyCounter& rhs) {
113 copies_ = rhs.copies_;
114 assigns_ = rhs.assigns_;
116 if (assigns_) {
117 (*assigns_)++;
120 return *this;
123 int copies() const {
124 return *copies_;
127 int assigns() const {
128 return *assigns_;
131 private:
132 int* copies_;
133 int* assigns_;
136 class DeleteCounter {
137 public:
138 explicit DeleteCounter(int* deletes)
139 : deletes_(deletes) {
142 ~DeleteCounter() {
143 (*deletes_)++;
146 void VoidMethod0() {}
148 private:
149 int* deletes_;
152 // Some test functions that we can Bind to.
153 template <typename T>
154 T PolymorphicIdentity(T t) {
155 return t;
158 template <typename T>
159 void VoidPolymorphic1(T t) {
162 int Identity(int n) {
163 return n;
166 int ArrayGet(const int array[], int n) {
167 return array[n];
170 int Sum(int a, int b, int c, int d, int e, int f) {
171 return a + b + c + d + e + f;
174 void OutputSum(int* output, int a, int b, int c, int d, int e) {
175 *output = a + b + c + d + e;
178 const char* CStringIdentity(const char* s) {
179 return s;
182 int GetCopies(const CopyCounter& counter) {
183 return counter.copies();
186 int UnwrapNoRefParent(NoRefParent p) {
187 return p.value;
190 int UnwrapNoRefParentPtr(NoRefParent* p) {
191 return p->value;
194 int UnwrapNoRefParentConstRef(const NoRefParent& p) {
195 return p.value;
198 void RefArgSet(int &n) {
199 n = 2;
202 int FunctionWithWeakFirstParam(WeakPtr<NoRef> o, int n) {
203 return n;
206 class BindTest : public ::testing::Test {
207 public:
208 BindTest() {
209 const_has_ref_ptr_ = &has_ref_;
210 const_no_ref_ptr_ = &no_ref_;
211 static_func_mock_ptr = &static_func_mock_;
214 virtual ~BindTest() {
217 static void VoidFunc0(void) {
218 static_func_mock_ptr->VoidMethod0();
221 static int IntFunc0(void) { return static_func_mock_ptr->IntMethod0(); }
223 protected:
224 StrictMock<NoRef> no_ref_;
225 StrictMock<HasRef> has_ref_;
226 const HasRef* const_has_ref_ptr_;
227 const NoRef* const_no_ref_ptr_;
228 StrictMock<NoRef> static_func_mock_;
230 // Used by the static functions to perform expectations.
231 static StrictMock<NoRef>* static_func_mock_ptr;
233 private:
234 DISALLOW_COPY_AND_ASSIGN(BindTest);
237 StrictMock<NoRef>* BindTest::static_func_mock_ptr;
239 // Sanity check that we can instantiate a callback for each arity.
240 TEST_F(BindTest, ArityTest) {
241 Callback<int(void)> c0 = Bind(&Sum, 32, 16, 8, 4, 2, 1);
242 EXPECT_EQ(63, c0.Run());
244 Callback<int(int)> c1 = Bind(&Sum, 32, 16, 8, 4, 2);
245 EXPECT_EQ(75, c1.Run(13));
247 Callback<int(int,int)> c2 = Bind(&Sum, 32, 16, 8, 4);
248 EXPECT_EQ(85, c2.Run(13, 12));
250 Callback<int(int,int,int)> c3 = Bind(&Sum, 32, 16, 8);
251 EXPECT_EQ(92, c3.Run(13, 12, 11));
253 Callback<int(int,int,int,int)> c4 = Bind(&Sum, 32, 16);
254 EXPECT_EQ(94, c4.Run(13, 12, 11, 10));
256 Callback<int(int,int,int,int,int)> c5 = Bind(&Sum, 32);
257 EXPECT_EQ(87, c5.Run(13, 12, 11, 10, 9));
259 Callback<int(int,int,int,int,int,int)> c6 = Bind(&Sum);
260 EXPECT_EQ(69, c6.Run(13, 12, 11, 10, 9, 14));
263 // Bind should be able to take existing Callbacks and convert to a Closure.
264 TEST_F(BindTest, CallbackBindMore) {
265 int output = 0;
266 Closure c;
268 Callback<void(int)> c1 = Bind(&OutputSum, &output, 16, 8, 4, 2);
269 c = Bind(c1, 10);
270 c.Run();
271 EXPECT_EQ(40, output);
273 Callback<void(int,int)> c2 = Bind(&OutputSum, &output, 16, 8, 4);
274 c = Bind(c2, 10, 9);
275 c.Run();
276 EXPECT_EQ(47, output);
278 Callback<void(int,int,int)> c3 = Bind(&OutputSum, &output, 16, 8);
279 c = Bind(c3, 10, 9, 8);
280 c.Run();
281 EXPECT_EQ(51, output);
283 Callback<void(int,int,int,int)> c4 = Bind(&OutputSum, &output, 16);
284 c = Bind(c4, 10, 9, 8, 7);
285 c.Run();
286 EXPECT_EQ(50, output);
288 Callback<void(int,int,int,int,int)> c5 = Bind(&OutputSum, &output);
289 c = Bind(c5, 10, 9, 8, 7, 6);
290 c.Run();
291 EXPECT_EQ(40, output);
294 // Function type support.
295 // - Normal function.
296 // - Normal function bound with non-refcounted first argument.
297 // - Method bound to non-const object.
298 // - Method bound to scoped_refptr.
299 // - Const method bound to non-const object.
300 // - Const method bound to const object.
301 // - Derived classes can be used with pointers to non-virtual base functions.
302 // - Derived classes can be used with pointers to virtual base functions (and
303 // preserve virtual dispatch).
304 TEST_F(BindTest, FunctionTypeSupport) {
305 EXPECT_CALL(static_func_mock_, VoidMethod0());
306 EXPECT_CALL(has_ref_, AddRef()).Times(5);
307 EXPECT_CALL(has_ref_, Release()).Times(5);
308 EXPECT_CALL(has_ref_, VoidMethod0()).Times(2);
309 EXPECT_CALL(has_ref_, VoidConstMethod0()).Times(2);
311 Closure normal_cb = Bind(&VoidFunc0);
312 Callback<NoRef*(void)> normal_non_refcounted_cb =
313 Bind(&PolymorphicIdentity<NoRef*>, &no_ref_);
314 normal_cb.Run();
315 EXPECT_EQ(&no_ref_, normal_non_refcounted_cb.Run());
317 Closure method_cb = Bind(&HasRef::VoidMethod0, &has_ref_);
318 Closure method_refptr_cb = Bind(&HasRef::VoidMethod0,
319 make_scoped_refptr(&has_ref_));
320 Closure const_method_nonconst_obj_cb = Bind(&HasRef::VoidConstMethod0,
321 &has_ref_);
322 Closure const_method_const_obj_cb = Bind(&HasRef::VoidConstMethod0,
323 const_has_ref_ptr_);
324 method_cb.Run();
325 method_refptr_cb.Run();
326 const_method_nonconst_obj_cb.Run();
327 const_method_const_obj_cb.Run();
329 Child child;
330 child.value = 0;
331 Closure virtual_set_cb = Bind(&Parent::VirtualSet, &child);
332 virtual_set_cb.Run();
333 EXPECT_EQ(kChildValue, child.value);
335 child.value = 0;
336 Closure non_virtual_set_cb = Bind(&Parent::NonVirtualSet, &child);
337 non_virtual_set_cb.Run();
338 EXPECT_EQ(kParentValue, child.value);
341 // Return value support.
342 // - Function with return value.
343 // - Method with return value.
344 // - Const method with return value.
345 TEST_F(BindTest, ReturnValues) {
346 EXPECT_CALL(static_func_mock_, IntMethod0()).WillOnce(Return(1337));
347 EXPECT_CALL(has_ref_, AddRef()).Times(3);
348 EXPECT_CALL(has_ref_, Release()).Times(3);
349 EXPECT_CALL(has_ref_, IntMethod0()).WillOnce(Return(31337));
350 EXPECT_CALL(has_ref_, IntConstMethod0())
351 .WillOnce(Return(41337))
352 .WillOnce(Return(51337));
354 Callback<int(void)> normal_cb = Bind(&IntFunc0);
355 Callback<int(void)> method_cb = Bind(&HasRef::IntMethod0, &has_ref_);
356 Callback<int(void)> const_method_nonconst_obj_cb =
357 Bind(&HasRef::IntConstMethod0, &has_ref_);
358 Callback<int(void)> const_method_const_obj_cb =
359 Bind(&HasRef::IntConstMethod0, const_has_ref_ptr_);
360 EXPECT_EQ(1337, normal_cb.Run());
361 EXPECT_EQ(31337, method_cb.Run());
362 EXPECT_EQ(41337, const_method_nonconst_obj_cb.Run());
363 EXPECT_EQ(51337, const_method_const_obj_cb.Run());
366 // IgnoreReturn adapter test.
367 // - Function with return value, and no params can be converted to Closure.
368 TEST_F(BindTest, IgnoreReturn) {
369 EXPECT_CALL(static_func_mock_, IntMethod0()).WillOnce(Return(1337));
370 Callback<int(void)> normal_cb = Bind(&IntFunc0);
371 Closure c = IgnoreReturn(normal_cb);
372 c.Run();
375 // Argument binding tests.
376 // - Argument binding to primitive.
377 // - Argument binding to primitive pointer.
378 // - Argument binding to a literal integer.
379 // - Argument binding to a literal string.
380 // - Argument binding with template function.
381 // - Argument binding to an object.
382 // - Argument binding to pointer to incomplete type.
383 // - Argument gets type converted.
384 // - Pointer argument gets converted.
385 // - Const Reference forces conversion.
386 TEST_F(BindTest, ArgumentBinding) {
387 int n = 2;
389 Callback<int(void)> bind_primitive_cb = Bind(&Identity, n);
390 EXPECT_EQ(n, bind_primitive_cb.Run());
392 Callback<int*(void)> bind_primitive_pointer_cb =
393 Bind(&PolymorphicIdentity<int*>, &n);
394 EXPECT_EQ(&n, bind_primitive_pointer_cb.Run());
396 Callback<int(void)> bind_int_literal_cb = Bind(&Identity, 3);
397 EXPECT_EQ(3, bind_int_literal_cb.Run());
399 Callback<const char*(void)> bind_string_literal_cb =
400 Bind(&CStringIdentity, "hi");
401 EXPECT_STREQ("hi", bind_string_literal_cb.Run());
403 Callback<int(void)> bind_template_function_cb =
404 Bind(&PolymorphicIdentity<int>, 4);
405 EXPECT_EQ(4, bind_template_function_cb.Run());
407 NoRefParent p;
408 p.value = 5;
409 Callback<int(void)> bind_object_cb = Bind(&UnwrapNoRefParent, p);
410 EXPECT_EQ(5, bind_object_cb.Run());
412 IncompleteType* incomplete_ptr = reinterpret_cast<IncompleteType*>(123);
413 Callback<IncompleteType*(void)> bind_incomplete_ptr_cb =
414 Bind(&PolymorphicIdentity<IncompleteType*>, incomplete_ptr);
415 EXPECT_EQ(incomplete_ptr, bind_incomplete_ptr_cb.Run());
417 NoRefChild c;
418 c.value = 6;
419 Callback<int(void)> bind_promotes_cb = Bind(&UnwrapNoRefParent, c);
420 EXPECT_EQ(6, bind_promotes_cb.Run());
422 c.value = 7;
423 Callback<int(void)> bind_pointer_promotes_cb =
424 Bind(&UnwrapNoRefParentPtr, &c);
425 EXPECT_EQ(7, bind_pointer_promotes_cb.Run());
427 c.value = 8;
428 Callback<int(void)> bind_const_reference_promotes_cb =
429 Bind(&UnwrapNoRefParentConstRef, c);
430 EXPECT_EQ(8, bind_const_reference_promotes_cb.Run());
433 // Unbound argument type support tests.
434 // - Unbound value.
435 // - Unbound pointer.
436 // - Unbound reference.
437 // - Unbound const reference.
438 // - Unbound unsized array.
439 // - Unbound sized array.
440 // - Unbound array-of-arrays.
441 TEST_F(BindTest, UnboundArgumentTypeSupport) {
442 Callback<void(int)> unbound_value_cb = Bind(&VoidPolymorphic1<int>);
443 Callback<void(int*)> unbound_pointer_cb = Bind(&VoidPolymorphic1<int*>);
444 Callback<void(int&)> unbound_ref_cb = Bind(&VoidPolymorphic1<int&>);
445 Callback<void(const int&)> unbound_const_ref_cb =
446 Bind(&VoidPolymorphic1<const int&>);
447 Callback<void(int[])> unbound_unsized_array_cb =
448 Bind(&VoidPolymorphic1<int[]>);
449 Callback<void(int[2])> unbound_sized_array_cb =
450 Bind(&VoidPolymorphic1<int[2]>);
451 Callback<void(int[][2])> unbound_array_of_arrays_cb =
452 Bind(&VoidPolymorphic1<int[][2]>);
455 // Function with unbound reference parameter.
456 // - Original paraemter is modified by callback.
457 TEST_F(BindTest, UnboundReferenceSupport) {
458 int n = 0;
459 Callback<void(int&)> unbound_ref_cb = Bind(&RefArgSet);
460 unbound_ref_cb.Run(n);
461 EXPECT_EQ(2, n);
464 // Functions that take reference parameters.
465 // - Forced reference parameter type still stores a copy.
466 // - Forced const reference parameter type still stores a copy.
467 TEST_F(BindTest, ReferenceArgumentBinding) {
468 int n = 1;
469 int& ref_n = n;
470 const int& const_ref_n = n;
472 Callback<int(void)> ref_copies_cb = Bind(&Identity, ref_n);
473 EXPECT_EQ(n, ref_copies_cb.Run());
474 n++;
475 EXPECT_EQ(n - 1, ref_copies_cb.Run());
477 Callback<int(void)> const_ref_copies_cb = Bind(&Identity, const_ref_n);
478 EXPECT_EQ(n, const_ref_copies_cb.Run());
479 n++;
480 EXPECT_EQ(n - 1, const_ref_copies_cb.Run());
483 // Check that we can pass in arrays and have them be stored as a pointer.
484 // - Array of values stores a pointer.
485 // - Array of const values stores a pointer.
486 TEST_F(BindTest, ArrayArgumentBinding) {
487 int array[4] = {1, 1, 1, 1};
488 const int (*const_array_ptr)[4] = &array;
490 Callback<int(void)> array_cb = Bind(&ArrayGet, array, 1);
491 EXPECT_EQ(1, array_cb.Run());
493 Callback<int(void)> const_array_cb = Bind(&ArrayGet, *const_array_ptr, 1);
494 EXPECT_EQ(1, const_array_cb.Run());
496 array[1] = 3;
497 EXPECT_EQ(3, array_cb.Run());
498 EXPECT_EQ(3, const_array_cb.Run());
501 // Verify SupportsAddRefAndRelease correctly introspects the class type for
502 // AddRef() and Release().
503 // - Class with AddRef() and Release()
504 // - Class without AddRef() and Release()
505 // - Derived Class with AddRef() and Release()
506 // - Derived Class without AddRef() and Release()
507 // - Derived Class with AddRef() and Release() and a private destructor.
508 TEST_F(BindTest, SupportsAddRefAndRelease) {
509 EXPECT_TRUE(internal::SupportsAddRefAndRelease<HasRef>::value);
510 EXPECT_FALSE(internal::SupportsAddRefAndRelease<NoRef>::value);
512 // StrictMock<T> is a derived class of T. So, we use StrictMock<HasRef> and
513 // StrictMock<NoRef> to test that SupportsAddRefAndRelease works over
514 // inheritance.
515 EXPECT_TRUE(internal::SupportsAddRefAndRelease<StrictMock<HasRef> >::value);
516 EXPECT_FALSE(internal::SupportsAddRefAndRelease<StrictMock<NoRef> >::value);
518 // This matters because the implementation creates a dummy class that
519 // inherits from the template type.
520 EXPECT_TRUE(internal::SupportsAddRefAndRelease<HasRefPrivateDtor>::value);
523 // Unretained() wrapper support.
524 // - Method bound to Unretained() non-const object.
525 // - Const method bound to Unretained() non-const object.
526 // - Const method bound to Unretained() const object.
527 TEST_F(BindTest, Unretained) {
528 EXPECT_CALL(no_ref_, VoidMethod0());
529 EXPECT_CALL(no_ref_, VoidConstMethod0()).Times(2);
531 Callback<void(void)> method_cb =
532 Bind(&NoRef::VoidMethod0, Unretained(&no_ref_));
533 method_cb.Run();
535 Callback<void(void)> const_method_cb =
536 Bind(&NoRef::VoidConstMethod0, Unretained(&no_ref_));
537 const_method_cb.Run();
539 Callback<void(void)> const_method_const_ptr_cb =
540 Bind(&NoRef::VoidConstMethod0, Unretained(const_no_ref_ptr_));
541 const_method_const_ptr_cb.Run();
544 // WeakPtr() support.
545 // - Method bound to WeakPtr<> to non-const object.
546 // - Const method bound to WeakPtr<> to non-const object.
547 // - Const method bound to WeakPtr<> to const object.
548 // - Normal Function with WeakPtr<> as P1 can have return type and is
549 // not canceled.
550 TEST_F(BindTest, WeakPtr) {
551 EXPECT_CALL(no_ref_, VoidMethod0());
552 EXPECT_CALL(no_ref_, VoidConstMethod0()).Times(2);
554 WeakPtrFactory<NoRef> weak_factory(&no_ref_);
555 WeakPtrFactory<const NoRef> const_weak_factory(const_no_ref_ptr_);
557 Callback<void(void)> method_cb =
558 Bind(&NoRef::VoidMethod0, weak_factory.GetWeakPtr());
559 method_cb.Run();
561 Callback<void(void)> const_method_cb =
562 Bind(&NoRef::VoidConstMethod0, const_weak_factory.GetWeakPtr());
563 const_method_cb.Run();
565 Callback<void(void)> const_method_const_ptr_cb =
566 Bind(&NoRef::VoidConstMethod0, const_weak_factory.GetWeakPtr());
567 const_method_const_ptr_cb.Run();
569 Callback<int(int)> normal_func_cb =
570 Bind(&FunctionWithWeakFirstParam, weak_factory.GetWeakPtr());
571 EXPECT_EQ(1, normal_func_cb.Run(1));
573 weak_factory.InvalidateWeakPtrs();
574 const_weak_factory.InvalidateWeakPtrs();
576 method_cb.Run();
577 const_method_cb.Run();
578 const_method_const_ptr_cb.Run();
580 // Still runs even after the pointers are invalidated.
581 EXPECT_EQ(2, normal_func_cb.Run(2));
584 // ConstRef() wrapper support.
585 // - Binding w/o ConstRef takes a copy.
586 // - Binding a ConstRef takes a reference.
587 // - Binding ConstRef to a function ConstRef does not copy on invoke.
588 TEST_F(BindTest, ConstRef) {
589 int n = 1;
591 Callback<int(void)> copy_cb = Bind(&Identity, n);
592 Callback<int(void)> const_ref_cb = Bind(&Identity, ConstRef(n));
593 EXPECT_EQ(n, copy_cb.Run());
594 EXPECT_EQ(n, const_ref_cb.Run());
595 n++;
596 EXPECT_EQ(n - 1, copy_cb.Run());
597 EXPECT_EQ(n, const_ref_cb.Run());
599 int copies = 0;
600 int assigns = 0;
601 CopyCounter counter(&copies, &assigns);
602 Callback<int(void)> all_const_ref_cb =
603 Bind(&GetCopies, ConstRef(counter));
604 EXPECT_EQ(0, all_const_ref_cb.Run());
605 EXPECT_EQ(0, copies);
606 EXPECT_EQ(0, assigns);
609 // Test Owned() support.
610 TEST_F(BindTest, Owned) {
611 int deletes = 0;
612 DeleteCounter* counter = new DeleteCounter(&deletes);
614 // If we don't capture, delete happens on Callback destruction/reset.
615 // return the same value.
616 Callback<DeleteCounter*(void)> no_capture_cb =
617 Bind(&PolymorphicIdentity<DeleteCounter*>, Owned(counter));
618 EXPECT_EQ(counter, no_capture_cb.Run());
619 EXPECT_EQ(counter, no_capture_cb.Run());
620 EXPECT_EQ(0, deletes);
621 no_capture_cb.Reset(); // This should trigger a delete.
622 EXPECT_EQ(1, deletes);
624 deletes = 0;
625 counter = new DeleteCounter(&deletes);
626 base::Closure own_object_cb =
627 Bind(&DeleteCounter::VoidMethod0, Owned(counter));
628 own_object_cb.Run();
629 EXPECT_EQ(0, deletes);
630 own_object_cb.Reset();
631 EXPECT_EQ(1, deletes);
634 // Argument Copy-constructor usage for non-reference parameters.
635 // - Bound arguments are only copied once.
636 // - Forwarded arguments are only copied once.
637 // - Forwarded arguments with coerscions are only copied twice (once for the
638 // coerscion, and one for the final dispatch).
639 TEST_F(BindTest, ArgumentCopies) {
640 int copies = 0;
641 int assigns = 0;
643 CopyCounter counter(&copies, &assigns);
645 Callback<void(void)> copy_cb =
646 Bind(&VoidPolymorphic1<CopyCounter>, counter);
647 EXPECT_GE(1, copies);
648 EXPECT_EQ(0, assigns);
650 copies = 0;
651 assigns = 0;
652 Callback<void(CopyCounter)> forward_cb =
653 Bind(&VoidPolymorphic1<CopyCounter>);
654 forward_cb.Run(counter);
655 EXPECT_GE(1, copies);
656 EXPECT_EQ(0, assigns);
658 copies = 0;
659 assigns = 0;
660 DerivedCopyCounter dervied(&copies, &assigns);
661 Callback<void(CopyCounter)> coerce_cb =
662 Bind(&VoidPolymorphic1<CopyCounter>);
663 coerce_cb.Run(dervied);
664 EXPECT_GE(2, copies);
665 EXPECT_EQ(0, assigns);
668 // Callback construction and assignment tests.
669 // - Construction from an InvokerStorageHolder should not cause ref/deref.
670 // - Assignment from other callback should only cause one ref
672 // TODO(ajwong): Is there actually a way to test this?
674 #if defined(OS_WIN)
675 int __fastcall FastCallFunc(int n) {
676 return n;
679 int __stdcall StdCallFunc(int n) {
680 return n;
683 // Windows specific calling convention support.
684 // - Can bind a __fastcall function.
685 // - Can bind a __stdcall function.
686 TEST_F(BindTest, WindowsCallingConventions) {
687 Callback<int(void)> fastcall_cb = Bind(&FastCallFunc, 1);
688 EXPECT_EQ(1, fastcall_cb.Run());
690 Callback<int(void)> stdcall_cb = Bind(&StdCallFunc, 2);
691 EXPECT_EQ(2, stdcall_cb.Run());
693 #endif
695 } // namespace
696 } // namespace base