DevTools: consistently use camel case for URL parameter names
[chromium-blink-merge.git] / base / bind_unittest.cc
blob333512ab12e8b62f7c54416a053139df7f4f7f59
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 #if defined(BASE_CALLBACK_H_)
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.
10 #error "base/bind.h should avoid pulling in callback.h by default."
11 #endif
13 #include "base/callback.h"
14 #include "testing/gmock/include/gmock/gmock.h"
15 #include "testing/gtest/include/gtest/gtest.h"
17 using ::testing::Mock;
18 using ::testing::Return;
19 using ::testing::StrictMock;
21 namespace base {
22 namespace {
24 class NoRef {
25 public:
26 NoRef() {}
28 MOCK_METHOD0(VoidMethod0, void(void));
29 MOCK_CONST_METHOD0(VoidConstMethod0, void(void));
31 MOCK_METHOD0(IntMethod0, int(void));
32 MOCK_CONST_METHOD0(IntConstMethod0, int(void));
34 private:
35 // Particularly important in this test to ensure no copies are made.
36 DISALLOW_COPY_AND_ASSIGN(NoRef);
39 class HasRef : public NoRef {
40 public:
41 HasRef() {}
43 MOCK_CONST_METHOD0(AddRef, void(void));
44 MOCK_CONST_METHOD0(Release, bool(void));
46 private:
47 // Particularly important in this test to ensure no copies are made.
48 DISALLOW_COPY_AND_ASSIGN(HasRef);
51 class HasRefPrivateDtor : public HasRef {
52 private:
53 ~HasRefPrivateDtor() {}
56 static const int kParentValue = 1;
57 static const int kChildValue = 2;
59 class Parent {
60 public:
61 void AddRef(void) const {}
62 void Release(void) const {}
63 virtual void VirtualSet() { value = kParentValue; }
64 void NonVirtualSet() { value = kParentValue; }
65 int value;
68 class Child : public Parent {
69 public:
70 virtual void VirtualSet() { value = kChildValue; }
71 void NonVirtualSet() { value = kChildValue; }
74 class NoRefParent {
75 public:
76 virtual void VirtualSet() { value = kParentValue; }
77 void NonVirtualSet() { value = kParentValue; }
78 int value;
81 class NoRefChild : public NoRefParent {
82 virtual void VirtualSet() { value = kChildValue; }
83 void NonVirtualSet() { value = kChildValue; }
86 // Used for probing the number of copies that occur if a type must be coerced
87 // during argument forwarding in the Run() methods.
88 struct DerivedCopyCounter {
89 DerivedCopyCounter(int* copies, int* assigns)
90 : copies_(copies), assigns_(assigns) {
92 int* copies_;
93 int* assigns_;
96 // Used for probing the number of copies in an argument.
97 class CopyCounter {
98 public:
99 CopyCounter(int* copies, int* assigns)
100 : copies_(copies), assigns_(assigns) {
103 CopyCounter(const CopyCounter& other)
104 : copies_(other.copies_),
105 assigns_(other.assigns_) {
106 (*copies_)++;
109 // Probing for copies from coerscion.
110 CopyCounter(const DerivedCopyCounter& other)
111 : copies_(other.copies_),
112 assigns_(other.assigns_) {
113 (*copies_)++;
116 const CopyCounter& operator=(const CopyCounter& rhs) {
117 copies_ = rhs.copies_;
118 assigns_ = rhs.assigns_;
120 if (assigns_) {
121 (*assigns_)++;
124 return *this;
127 int copies() const {
128 return *copies_;
131 int assigns() const {
132 return *assigns_;
135 private:
136 int* copies_;
137 int* assigns_;
140 // Some test functions that we can Bind to.
141 template <typename T>
142 T PolymorphicIdentity(T t) {
143 return t;
146 template <typename T>
147 void VoidPolymorphic1(T t) {
150 int Identity(int n) {
151 return n;
154 int ArrayGet(const int array[], int n) {
155 return array[n];
158 int Sum(int a, int b, int c, int d, int e, int f) {
159 return a + b + c + d + e + f;
162 const char* CStringIdentity(const char* s) {
163 return s;
166 int GetCopies(const CopyCounter& counter) {
167 return counter.copies();
170 int UnwrapNoRefParent(NoRefParent p) {
171 return p.value;
174 int UnwrapNoRefParentPtr(NoRefParent* p) {
175 return p->value;
178 int UnwrapNoRefParentConstRef(const NoRefParent& p) {
179 return p.value;
182 void RefArgSet(int &n) {
183 n = 2;
186 int FunctionWithWeakFirstParam(WeakPtr<NoRef> o, int n) {
187 return n;
190 // Only useful in no-compile tests.
191 int UnwrapNoRefParentRef(Parent& p) {
192 return p.value;
195 class BindTest : public ::testing::Test {
196 public:
197 BindTest() {
198 const_has_ref_ptr_ = &has_ref_;
199 const_no_ref_ptr_ = &no_ref_;
200 static_func_mock_ptr = &static_func_mock_;
203 virtual ~BindTest() {
206 static void VoidFunc0(void) {
207 static_func_mock_ptr->VoidMethod0();
210 static int IntFunc0(void) { return static_func_mock_ptr->IntMethod0(); }
212 protected:
213 StrictMock<NoRef> no_ref_;
214 StrictMock<HasRef> has_ref_;
215 const HasRef* const_has_ref_ptr_;
216 const NoRef* const_no_ref_ptr_;
217 StrictMock<NoRef> static_func_mock_;
219 // Used by the static functions to perform expectations.
220 static StrictMock<NoRef>* static_func_mock_ptr;
222 private:
223 DISALLOW_COPY_AND_ASSIGN(BindTest);
226 StrictMock<NoRef>* BindTest::static_func_mock_ptr;
228 // Sanity check that we can instantiate a callback for each arity.
229 TEST_F(BindTest, ArityTest) {
230 Callback<int(void)> c0 = Bind(&Sum, 32, 16, 8, 4, 2, 1);
231 EXPECT_EQ(63, c0.Run());
233 Callback<int(int)> c1 = Bind(&Sum, 32, 16, 8, 4, 2);
234 EXPECT_EQ(75, c1.Run(13));
236 Callback<int(int,int)> c2 = Bind(&Sum, 32, 16, 8, 4);
237 EXPECT_EQ(85, c2.Run(13, 12));
239 Callback<int(int,int,int)> c3 = Bind(&Sum, 32, 16, 8);
240 EXPECT_EQ(92, c3.Run(13, 12, 11));
242 Callback<int(int,int,int,int)> c4 = Bind(&Sum, 32, 16);
243 EXPECT_EQ(94, c4.Run(13, 12, 11, 10));
245 Callback<int(int,int,int,int,int)> c5 = Bind(&Sum, 32);
246 EXPECT_EQ(87, c5.Run(13, 12, 11, 10, 9));
248 Callback<int(int,int,int,int,int,int)> c6 = Bind(&Sum);
249 EXPECT_EQ(69, c6.Run(13, 12, 11, 10, 9, 14));
252 // Function type support.
253 // - Normal function.
254 // - Method bound to non-const object.
255 // - Const method bound to non-const object.
256 // - Const method bound to const object.
257 // - Derived classes can be used with pointers to non-virtual base functions.
258 // - Derived classes can be used with pointers to virtual base functions (and
259 // preserve virtual dispatch).
260 TEST_F(BindTest, FunctionTypeSupport) {
261 EXPECT_CALL(static_func_mock_, VoidMethod0());
262 EXPECT_CALL(has_ref_, AddRef()).Times(3);
263 EXPECT_CALL(has_ref_, Release()).Times(3);
264 EXPECT_CALL(has_ref_, VoidMethod0());
265 EXPECT_CALL(has_ref_, VoidConstMethod0()).Times(2);
267 Closure normal_cb = Bind(&VoidFunc0);
268 Closure method_cb = Bind(&HasRef::VoidMethod0, &has_ref_);
269 Closure const_method_nonconst_obj_cb = Bind(&HasRef::VoidConstMethod0,
270 &has_ref_);
271 Closure const_method_const_obj_cb = Bind(&HasRef::VoidConstMethod0,
272 const_has_ref_ptr_);
273 normal_cb.Run();
274 method_cb.Run();
275 const_method_nonconst_obj_cb.Run();
276 const_method_const_obj_cb.Run();
278 Child child;
279 child.value = 0;
280 Closure virtual_set_cb = Bind(&Parent::VirtualSet, &child);
281 virtual_set_cb.Run();
282 EXPECT_EQ(kChildValue, child.value);
284 child.value = 0;
285 Closure non_virtual_set_cb = Bind(&Parent::NonVirtualSet, &child);
286 non_virtual_set_cb.Run();
287 EXPECT_EQ(kParentValue, child.value);
290 // Return value support.
291 // - Function with return value.
292 // - Method with return value.
293 // - Const method with return value.
294 TEST_F(BindTest, ReturnValues) {
295 EXPECT_CALL(static_func_mock_, IntMethod0()).WillOnce(Return(1337));
296 EXPECT_CALL(has_ref_, AddRef()).Times(3);
297 EXPECT_CALL(has_ref_, Release()).Times(3);
298 EXPECT_CALL(has_ref_, IntMethod0()).WillOnce(Return(31337));
299 EXPECT_CALL(has_ref_, IntConstMethod0())
300 .WillOnce(Return(41337))
301 .WillOnce(Return(51337));
303 Callback<int(void)> normal_cb = Bind(&IntFunc0);
304 Callback<int(void)> method_cb = Bind(&HasRef::IntMethod0, &has_ref_);
305 Callback<int(void)> const_method_nonconst_obj_cb =
306 Bind(&HasRef::IntConstMethod0, &has_ref_);
307 Callback<int(void)> const_method_const_obj_cb =
308 Bind(&HasRef::IntConstMethod0, const_has_ref_ptr_);
309 EXPECT_EQ(1337, normal_cb.Run());
310 EXPECT_EQ(31337, method_cb.Run());
311 EXPECT_EQ(41337, const_method_nonconst_obj_cb.Run());
312 EXPECT_EQ(51337, const_method_const_obj_cb.Run());
315 // Argument binding tests.
316 // - Argument binding to primitive.
317 // - Argument binding to primitive pointer.
318 // - Argument binding to a literal integer.
319 // - Argument binding to a literal string.
320 // - Argument binding with template function.
321 // - Argument binding to an object.
322 // - Argument gets type converted.
323 // - Pointer argument gets converted.
324 // - Const Reference forces conversion.
325 TEST_F(BindTest, ArgumentBinding) {
326 int n = 2;
328 Callback<int(void)> bind_primitive_cb = Bind(&Identity, n);
329 EXPECT_EQ(n, bind_primitive_cb.Run());
331 Callback<int*(void)> bind_primitive_pointer_cb =
332 Bind(&PolymorphicIdentity<int*>, &n);
333 EXPECT_EQ(&n, bind_primitive_pointer_cb.Run());
335 Callback<int(void)> bind_int_literal_cb = Bind(&Identity, 3);
336 EXPECT_EQ(3, bind_int_literal_cb.Run());
338 Callback<const char*(void)> bind_string_literal_cb =
339 Bind(&CStringIdentity, "hi");
340 EXPECT_STREQ("hi", bind_string_literal_cb.Run());
342 Callback<int(void)> bind_template_function_cb =
343 Bind(&PolymorphicIdentity<int>, 4);
344 EXPECT_EQ(4, bind_template_function_cb.Run());
346 NoRefParent p;
347 p.value = 5;
348 Callback<int(void)> bind_object_cb = Bind(&UnwrapNoRefParent, p);
349 EXPECT_EQ(5, bind_object_cb.Run());
351 NoRefChild c;
352 c.value = 6;
353 Callback<int(void)> bind_promotes_cb = Bind(&UnwrapNoRefParent, c);
354 EXPECT_EQ(6, bind_promotes_cb.Run());
356 c.value = 7;
357 Callback<int(void)> bind_pointer_promotes_cb =
358 Bind(&UnwrapNoRefParentPtr, &c);
359 EXPECT_EQ(7, bind_pointer_promotes_cb.Run());
361 c.value = 8;
362 Callback<int(void)> bind_const_reference_promotes_cb =
363 Bind(&UnwrapNoRefParentConstRef, c);
364 EXPECT_EQ(8, bind_const_reference_promotes_cb.Run());
367 // Unbound argument type support tests.
368 // - Unbound value.
369 // - Unbound pointer.
370 // - Unbound reference.
371 // - Unbound const reference.
372 // - Unbound unsized array.
373 // - Unbound sized array.
374 // - Unbound array-of-arrays.
375 TEST_F(BindTest, UnboundArgumentTypeSupport) {
376 Callback<void(int)> unbound_value_cb = Bind(&VoidPolymorphic1<int>);
377 Callback<void(int*)> unbound_pointer_cb = Bind(&VoidPolymorphic1<int*>);
378 Callback<void(int&)> unbound_ref_cb = Bind(&VoidPolymorphic1<int&>);
379 Callback<void(const int&)> unbound_const_ref_cb =
380 Bind(&VoidPolymorphic1<const int&>);
381 Callback<void(int[])> unbound_unsized_array_cb =
382 Bind(&VoidPolymorphic1<int[]>);
383 Callback<void(int[2])> unbound_sized_array_cb =
384 Bind(&VoidPolymorphic1<int[2]>);
385 Callback<void(int[][2])> unbound_array_of_arrays_cb =
386 Bind(&VoidPolymorphic1<int[][2]>);
389 // Function with unbound reference parameter.
390 // - Original paraemter is modified by callback.
391 TEST_F(BindTest, UnboundReferenceSupport) {
392 int n = 0;
393 Callback<void(int&)> unbound_ref_cb = Bind(&RefArgSet);
394 unbound_ref_cb.Run(n);
395 EXPECT_EQ(2, n);
398 // Functions that take reference parameters.
399 // - Forced reference parameter type still stores a copy.
400 // - Forced const reference parameter type still stores a copy.
401 TEST_F(BindTest, ReferenceArgumentBinding) {
402 int n = 1;
403 int& ref_n = n;
404 const int& const_ref_n = n;
406 Callback<int(void)> ref_copies_cb = Bind(&Identity, ref_n);
407 EXPECT_EQ(n, ref_copies_cb.Run());
408 n++;
409 EXPECT_EQ(n - 1, ref_copies_cb.Run());
411 Callback<int(void)> const_ref_copies_cb = Bind(&Identity, const_ref_n);
412 EXPECT_EQ(n, const_ref_copies_cb.Run());
413 n++;
414 EXPECT_EQ(n - 1, const_ref_copies_cb.Run());
417 // Check that we can pass in arrays and have them be stored as a pointer.
418 // - Array of values stores a pointer.
419 // - Array of const values stores a pointer.
420 TEST_F(BindTest, ArrayArgumentBinding) {
421 int array[4] = {1, 1, 1, 1};
422 const int (*const_array_ptr)[4] = &array;
424 Callback<int(void)> array_cb = Bind(&ArrayGet, array, 1);
425 EXPECT_EQ(1, array_cb.Run());
427 Callback<int(void)> const_array_cb = Bind(&ArrayGet, *const_array_ptr, 1);
428 EXPECT_EQ(1, const_array_cb.Run());
430 array[1] = 3;
431 EXPECT_EQ(3, array_cb.Run());
432 EXPECT_EQ(3, const_array_cb.Run());
435 // Verify SupportsAddRefAndRelease correctly introspects the class type for
436 // AddRef() and Release().
437 // - Class with AddRef() and Release()
438 // - Class without AddRef() and Release()
439 // - Derived Class with AddRef() and Release()
440 // - Derived Class without AddRef() and Release()
441 // - Derived Class with AddRef() and Release() and a private destructor.
442 TEST_F(BindTest, SupportsAddRefAndRelease) {
443 EXPECT_TRUE(internal::SupportsAddRefAndRelease<HasRef>::value);
444 EXPECT_FALSE(internal::SupportsAddRefAndRelease<NoRef>::value);
446 // StrictMock<T> is a derived class of T. So, we use StrictMock<HasRef> and
447 // StrictMock<NoRef> to test that SupportsAddRefAndRelease works over
448 // inheritance.
449 EXPECT_TRUE(internal::SupportsAddRefAndRelease<StrictMock<HasRef> >::value);
450 EXPECT_FALSE(internal::SupportsAddRefAndRelease<StrictMock<NoRef> >::value);
452 // This matters because the implementation creates a dummy class that
453 // inherits from the template type.
454 EXPECT_TRUE(internal::SupportsAddRefAndRelease<HasRefPrivateDtor>::value);
457 // Unretained() wrapper support.
458 // - Method bound to Unretained() non-const object.
459 // - Const method bound to Unretained() non-const object.
460 // - Const method bound to Unretained() const object.
461 TEST_F(BindTest, Unretained) {
462 EXPECT_CALL(no_ref_, VoidMethod0());
463 EXPECT_CALL(no_ref_, VoidConstMethod0()).Times(2);
465 Callback<void(void)> method_cb =
466 Bind(&NoRef::VoidMethod0, Unretained(&no_ref_));
467 method_cb.Run();
469 Callback<void(void)> const_method_cb =
470 Bind(&NoRef::VoidConstMethod0, Unretained(&no_ref_));
471 const_method_cb.Run();
473 Callback<void(void)> const_method_const_ptr_cb =
474 Bind(&NoRef::VoidConstMethod0, Unretained(const_no_ref_ptr_));
475 const_method_const_ptr_cb.Run();
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);
488 WeakPtrFactory<NoRef> weak_factory(&no_ref_);
489 WeakPtrFactory<const NoRef> const_weak_factory(const_no_ref_ptr_);
491 Callback<void(void)> method_cb =
492 Bind(&NoRef::VoidMethod0, weak_factory.GetWeakPtr());
493 method_cb.Run();
495 Callback<void(void)> const_method_cb =
496 Bind(&NoRef::VoidConstMethod0, const_weak_factory.GetWeakPtr());
497 const_method_cb.Run();
499 Callback<void(void)> const_method_const_ptr_cb =
500 Bind(&NoRef::VoidConstMethod0, const_weak_factory.GetWeakPtr());
501 const_method_const_ptr_cb.Run();
503 Callback<int(int)> normal_func_cb =
504 Bind(&FunctionWithWeakFirstParam, weak_factory.GetWeakPtr());
505 EXPECT_EQ(1, normal_func_cb.Run(1));
507 weak_factory.InvalidateWeakPtrs();
508 const_weak_factory.InvalidateWeakPtrs();
510 method_cb.Run();
511 const_method_cb.Run();
512 const_method_const_ptr_cb.Run();
514 // Still runs even after the pointers are invalidated.
515 EXPECT_EQ(2, normal_func_cb.Run(2));
518 // ConstRef() wrapper support.
519 // - Binding w/o ConstRef takes a copy.
520 // - Binding a ConstRef takes a reference.
521 // - Binding ConstRef to a function ConstRef does not copy on invoke.
522 TEST_F(BindTest, ConstRef) {
523 int n = 1;
525 Callback<int(void)> copy_cb = Bind(&Identity, n);
526 Callback<int(void)> const_ref_cb = Bind(&Identity, ConstRef(n));
527 EXPECT_EQ(n, copy_cb.Run());
528 EXPECT_EQ(n, const_ref_cb.Run());
529 n++;
530 EXPECT_EQ(n - 1, copy_cb.Run());
531 EXPECT_EQ(n, const_ref_cb.Run());
533 int copies = 0;
534 int assigns = 0;
535 CopyCounter counter(&copies, &assigns);
536 Callback<int(void)> all_const_ref_cb =
537 Bind(&GetCopies, ConstRef(counter));
538 EXPECT_EQ(0, all_const_ref_cb.Run());
539 EXPECT_EQ(0, copies);
540 EXPECT_EQ(0, assigns);
543 // Argument Copy-constructor usage for non-reference parameters.
544 // - Bound arguments are only copied once.
545 // - Forwarded arguments are only copied once.
546 // - Forwarded arguments with coerscions are only copied twice (once for the
547 // coerscion, and one for the final dispatch).
548 TEST_F(BindTest, ArgumentCopies) {
549 int copies = 0;
550 int assigns = 0;
552 CopyCounter counter(&copies, &assigns);
554 Callback<void(void)> copy_cb =
555 Bind(&VoidPolymorphic1<CopyCounter>, counter);
556 EXPECT_GE(1, copies);
557 EXPECT_EQ(0, assigns);
559 copies = 0;
560 assigns = 0;
561 Callback<void(CopyCounter)> forward_cb =
562 Bind(&VoidPolymorphic1<CopyCounter>);
563 forward_cb.Run(counter);
564 EXPECT_GE(1, copies);
565 EXPECT_EQ(0, assigns);
567 copies = 0;
568 assigns = 0;
569 DerivedCopyCounter dervied(&copies, &assigns);
570 Callback<void(CopyCounter)> coerce_cb =
571 Bind(&VoidPolymorphic1<CopyCounter>);
572 coerce_cb.Run(dervied);
573 EXPECT_GE(2, copies);
574 EXPECT_EQ(0, assigns);
577 // Callback construction and assignment tests.
578 // - Construction from an InvokerStorageHolder should not cause ref/deref.
579 // - Assignment from other callback should only cause one ref
581 // TODO(ajwong): Is there actually a way to test this?
583 // No-compile tests. These should not compile. If they do, we are allowing
584 // error-prone, or incorrect behavior in the callback system. Uncomment the
585 // tests to check.
586 TEST_F(BindTest, NoCompile) {
587 // - Method bound to const-object.
589 // Only const methods should be allowed to work with const objects.
591 // Callback<void(void)> method_to_const_cb =
592 // Bind(&HasRef::VoidMethod0, const_has_ref_ptr_);
593 // method_to_const_cb.Run();
595 // - Method bound to non-refcounted object.
596 // - Const Method bound to non-refcounted object.
598 // We require refcounts unless you have Unretained().
600 // Callback<void(void)> no_ref_cb =
601 // Bind(&NoRef::VoidMethod0, &no_ref_);
602 // no_ref_cb.Run();
603 // Callback<void(void)> no_ref_const_cb =
604 // Bind(&NoRef::VoidConstMethod0, &no_ref_);
605 // no_ref_const_cb.Run();
607 // - Unretained() used with a refcounted object.
609 // If the object supports refcounts, unretaining it in the callback is a
610 // memory management contract break.
611 // Callback<void(void)> unretained_cb =
612 // Bind(&HasRef::VoidConstMethod0, Unretained(&has_ref_));
613 // unretained_cb.Run();
615 // - Const argument used with non-const pointer parameter of same type.
616 // - Const argument used with non-const pointer parameter of super type.
618 // This is just a const-correctness check.
620 // const Parent* const_parent_ptr;
621 // const Child* const_child_ptr;
622 // Callback<Parent*(void)> pointer_same_cb =
623 // Bind(&PolymorphicIdentity<Parent*>, const_parent_ptr);
624 // pointer_same_cb.Run();
625 // Callback<Parent*(void)> pointer_super_cb =
626 // Bind(&PolymorphicIdentity<Parent*>, const_child_ptr);
627 // pointer_super_cb.Run();
629 // - Construction of Callback<A> from Callback<B> if A is supertype of B.
630 // Specific example: Callback<void(void)> a; Callback<int(void)> b; a = b;
632 // While this is technically safe, most people aren't used to it when coding
633 // C++ so if this is happening, it is almost certainly an error.
635 // Callback<int(void)> cb_a0 = Bind(&Identity, 1);
636 // Callback<void(void)> cb_b0 = cb_a0;
638 // - Assignment of Callback<A> from Callback<B> if A is supertype of B.
639 // See explanation above.
641 // Callback<int(void)> cb_a1 = Bind(&Identity, 1);
642 // Callback<void(void)> cb_b1;
643 // cb_a1 = cb_b1;
645 // - Functions with reference parameters, unsupported.
647 // First, non-const reference parameters are disallowed by the Google
648 // style guide. Seconds, since we are doing argument forwarding it becomes
649 // very tricky to avoid copies, maintain const correctness, and not
650 // accidentally have the function be modifying a temporary, or a copy.
652 // NoRefParent p;
653 // Callback<int(Parent&)> ref_arg_cb = Bind(&UnwrapNoRefParentRef);
654 // ref_arg_cb.Run(p);
655 // Callback<int(void)> ref_cb = Bind(&UnwrapNoRefParentRef, p);
656 // ref_cb.Run();
658 // - A method should not be bindable with an array of objects.
660 // This is likely not wanted behavior. We specifically check for it though
661 // because it is possible, depending on how you implement prebinding, to
662 // implicitly convert an array type to a pointer type.
664 // HasRef p[10];
665 // Callback<void(void)> method_bound_to_array_cb =
666 // Bind(&HasRef::VoidConstMethod0, p);
667 // method_bound_to_array_cb.Run();
669 // - Refcounted types should not be bound as a raw pointer.
670 // HasRef for_raw_ptr;
671 // Callback<void(void)> ref_count_as_raw_ptr =
672 // Bind(&VoidPolymorphic1<HasRef*>, &for_raw_ptr);
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());
680 // - Bind result cannot be assigned to Callbacks with a mismatching type.
681 // Closure callback_mismatches_bind_type = Bind(&VoidPolymorphic1<int>);
684 #if defined(OS_WIN)
685 int __fastcall FastCallFunc(int n) {
686 return n;
689 int __stdcall StdCallFunc(int n) {
690 return n;
693 // Windows specific calling convention support.
694 // - Can bind a __fastcall function.
695 // - Can bind a __stdcall function.
696 TEST_F(BindTest, WindowsCallingConventions) {
697 Callback<int(void)> fastcall_cb = Bind(&FastCallFunc, 1);
698 EXPECT_EQ(1, fastcall_cb.Run());
700 Callback<int(void)> stdcall_cb = Bind(&StdCallFunc, 2);
701 EXPECT_EQ(2, stdcall_cb.Run());
703 #endif
705 } // namespace
706 } // namespace base