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.
4
5 #include "base/bind.h"
6
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"
13
14 using ::testing::Mock;
15 using ::testing::Return;
16 using ::testing::StrictMock;
17
18 namespace base {
19 namespace {
20
21 class IncompleteType;
22
23 class NoRef {
24 public:
NoRef()25 NoRef() {}
26
27 MOCK_METHOD0(VoidMethod0, void(void));
28 MOCK_CONST_METHOD0(VoidConstMethod0, void(void));
29
30 MOCK_METHOD0(IntMethod0, int(void));
31 MOCK_CONST_METHOD0(IntConstMethod0, int(void));
32
33 private:
34 // Particularly important in this test to ensure no copies are made.
35 DISALLOW_COPY_AND_ASSIGN(NoRef);
36 };
37
38 class HasRef : public NoRef {
39 public:
HasRef()40 HasRef() {}
41
42 MOCK_CONST_METHOD0(AddRef, void(void));
43 MOCK_CONST_METHOD0(Release, bool(void));
44
45 private:
46 // Particularly important in this test to ensure no copies are made.
47 DISALLOW_COPY_AND_ASSIGN(HasRef);
48 };
49
50 class HasRefPrivateDtor : public HasRef {
51 private:
~HasRefPrivateDtor()52 ~HasRefPrivateDtor() {}
53 };
54
55 static const int kParentValue = 1;
56 static const int kChildValue = 2;
57
58 class Parent {
59 public:
AddRef(void) const60 void AddRef(void) const {}
Release(void) const61 void Release(void) const {}
VirtualSet()62 virtual void VirtualSet() { value = kParentValue; }
NonVirtualSet()63 void NonVirtualSet() { value = kParentValue; }
64 int value;
65 };
66
67 class Child : public Parent {
68 public:
VirtualSet()69 virtual void VirtualSet() OVERRIDE { value = kChildValue; }
NonVirtualSet()70 void NonVirtualSet() { value = kChildValue; }
71 };
72
73 class NoRefParent {
74 public:
VirtualSet()75 virtual void VirtualSet() { value = kParentValue; }
NonVirtualSet()76 void NonVirtualSet() { value = kParentValue; }
77 int value;
78 };
79
80 class NoRefChild : public NoRefParent {
VirtualSet()81 virtual void VirtualSet() OVERRIDE { value = kChildValue; }
NonVirtualSet()82 void NonVirtualSet() { value = kChildValue; }
83 };
84
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 {
DerivedCopyCounterbase::__anon874ec17c0111::DerivedCopyCounter88 DerivedCopyCounter(int* copies, int* assigns)
89 : copies_(copies), assigns_(assigns) {
90 }
91 int* copies_;
92 int* assigns_;
93 };
94
95 // Used for probing the number of copies in an argument.
96 class CopyCounter {
97 public:
CopyCounter(int * copies,int * assigns)98 CopyCounter(int* copies, int* assigns)
99 : copies_(copies), assigns_(assigns) {
100 }
101
CopyCounter(const CopyCounter & other)102 CopyCounter(const CopyCounter& other)
103 : copies_(other.copies_),
104 assigns_(other.assigns_) {
105 (*copies_)++;
106 }
107
108 // Probing for copies from coercion.
CopyCounter(const DerivedCopyCounter & other)109 explicit CopyCounter(const DerivedCopyCounter& other)
110 : copies_(other.copies_),
111 assigns_(other.assigns_) {
112 (*copies_)++;
113 }
114
operator =(const CopyCounter & rhs)115 const CopyCounter& operator=(const CopyCounter& rhs) {
116 copies_ = rhs.copies_;
117 assigns_ = rhs.assigns_;
118
119 if (assigns_) {
120 (*assigns_)++;
121 }
122
123 return *this;
124 }
125
copies() const126 int copies() const {
127 return *copies_;
128 }
129
130 private:
131 int* copies_;
132 int* assigns_;
133 };
134
135 class DeleteCounter {
136 public:
DeleteCounter(int * deletes)137 explicit DeleteCounter(int* deletes)
138 : deletes_(deletes) {
139 }
140
~DeleteCounter()141 ~DeleteCounter() {
142 (*deletes_)++;
143 }
144
VoidMethod0()145 void VoidMethod0() {}
146
147 private:
148 int* deletes_;
149 };
150
151 template <typename T>
PassThru(T scoper)152 T PassThru(T scoper) {
153 return scoper.Pass();
154 }
155
156 // Some test functions that we can Bind to.
157 template <typename T>
PolymorphicIdentity(T t)158 T PolymorphicIdentity(T t) {
159 return t;
160 }
161
162 template <typename T>
VoidPolymorphic1(T t)163 void VoidPolymorphic1(T t) {
164 }
165
Identity(int n)166 int Identity(int n) {
167 return n;
168 }
169
ArrayGet(const int array[],int n)170 int ArrayGet(const int array[], int n) {
171 return array[n];
172 }
173
Sum(int a,int b,int c,int d,int e,int f)174 int Sum(int a, int b, int c, int d, int e, int f) {
175 return a + b + c + d + e + f;
176 }
177
CStringIdentity(const char * s)178 const char* CStringIdentity(const char* s) {
179 return s;
180 }
181
GetCopies(const CopyCounter & counter)182 int GetCopies(const CopyCounter& counter) {
183 return counter.copies();
184 }
185
UnwrapNoRefParent(NoRefParent p)186 int UnwrapNoRefParent(NoRefParent p) {
187 return p.value;
188 }
189
UnwrapNoRefParentPtr(NoRefParent * p)190 int UnwrapNoRefParentPtr(NoRefParent* p) {
191 return p->value;
192 }
193
UnwrapNoRefParentConstRef(const NoRefParent & p)194 int UnwrapNoRefParentConstRef(const NoRefParent& p) {
195 return p.value;
196 }
197
RefArgSet(int & n)198 void RefArgSet(int &n) {
199 n = 2;
200 }
201
PtrArgSet(int * n)202 void PtrArgSet(int *n) {
203 *n = 2;
204 }
205
FunctionWithWeakFirstParam(WeakPtr<NoRef> o,int n)206 int FunctionWithWeakFirstParam(WeakPtr<NoRef> o, int n) {
207 return n;
208 }
209
FunctionWithScopedRefptrFirstParam(const scoped_refptr<HasRef> & o,int n)210 int FunctionWithScopedRefptrFirstParam(const scoped_refptr<HasRef>& o, int n) {
211 return n;
212 }
213
TakesACallback(const Closure & callback)214 void TakesACallback(const Closure& callback) {
215 callback.Run();
216 }
217
218 class BindTest : public ::testing::Test {
219 public:
BindTest()220 BindTest() {
221 const_has_ref_ptr_ = &has_ref_;
222 const_no_ref_ptr_ = &no_ref_;
223 static_func_mock_ptr = &static_func_mock_;
224 }
225
~BindTest()226 virtual ~BindTest() {
227 }
228
VoidFunc0(void)229 static void VoidFunc0(void) {
230 static_func_mock_ptr->VoidMethod0();
231 }
232
IntFunc0(void)233 static int IntFunc0(void) { return static_func_mock_ptr->IntMethod0(); }
234
235 protected:
236 StrictMock<NoRef> no_ref_;
237 StrictMock<HasRef> has_ref_;
238 const HasRef* const_has_ref_ptr_;
239 const NoRef* const_no_ref_ptr_;
240 StrictMock<NoRef> static_func_mock_;
241
242 // Used by the static functions to perform expectations.
243 static StrictMock<NoRef>* static_func_mock_ptr;
244
245 private:
246 DISALLOW_COPY_AND_ASSIGN(BindTest);
247 };
248
249 StrictMock<NoRef>* BindTest::static_func_mock_ptr;
250
251 // Sanity check that we can instantiate a callback for each arity.
TEST_F(BindTest,ArityTest)252 TEST_F(BindTest, ArityTest) {
253 Callback<int(void)> c0 = Bind(&Sum, 32, 16, 8, 4, 2, 1);
254 EXPECT_EQ(63, c0.Run());
255
256 Callback<int(int)> c1 = Bind(&Sum, 32, 16, 8, 4, 2);
257 EXPECT_EQ(75, c1.Run(13));
258
259 Callback<int(int,int)> c2 = Bind(&Sum, 32, 16, 8, 4);
260 EXPECT_EQ(85, c2.Run(13, 12));
261
262 Callback<int(int,int,int)> c3 = Bind(&Sum, 32, 16, 8);
263 EXPECT_EQ(92, c3.Run(13, 12, 11));
264
265 Callback<int(int,int,int,int)> c4 = Bind(&Sum, 32, 16);
266 EXPECT_EQ(94, c4.Run(13, 12, 11, 10));
267
268 Callback<int(int,int,int,int,int)> c5 = Bind(&Sum, 32);
269 EXPECT_EQ(87, c5.Run(13, 12, 11, 10, 9));
270
271 Callback<int(int,int,int,int,int,int)> c6 = Bind(&Sum);
272 EXPECT_EQ(69, c6.Run(13, 12, 11, 10, 9, 14));
273 }
274
275 // Test the Currying ability of the Callback system.
TEST_F(BindTest,CurryingTest)276 TEST_F(BindTest, CurryingTest) {
277 Callback<int(int,int,int,int,int,int)> c6 = Bind(&Sum);
278 EXPECT_EQ(69, c6.Run(13, 12, 11, 10, 9, 14));
279
280 Callback<int(int,int,int,int,int)> c5 = Bind(c6, 32);
281 EXPECT_EQ(87, c5.Run(13, 12, 11, 10, 9));
282
283 Callback<int(int,int,int,int)> c4 = Bind(c5, 16);
284 EXPECT_EQ(94, c4.Run(13, 12, 11, 10));
285
286 Callback<int(int,int,int)> c3 = Bind(c4, 8);
287 EXPECT_EQ(92, c3.Run(13, 12, 11));
288
289 Callback<int(int,int)> c2 = Bind(c3, 4);
290 EXPECT_EQ(85, c2.Run(13, 12));
291
292 Callback<int(int)> c1 = Bind(c2, 2);
293 EXPECT_EQ(75, c1.Run(13));
294
295 Callback<int(void)> c0 = Bind(c1, 1);
296 EXPECT_EQ(63, c0.Run());
297 }
298
299 // Test that currying the rvalue result of another Bind() works correctly.
300 // - rvalue should be usable as argument to Bind().
301 // - multiple runs of resulting Callback remain valid.
TEST_F(BindTest,CurryingRvalueResultOfBind)302 TEST_F(BindTest, CurryingRvalueResultOfBind) {
303 int n = 0;
304 Closure cb = base::Bind(&TakesACallback, base::Bind(&PtrArgSet, &n));
305
306 // If we implement Bind() such that the return value has auto_ptr-like
307 // semantics, the second call here will fail because ownership of
308 // the internal BindState<> would have been transfered to a *temporary*
309 // constructon of a Callback object on the first call.
310 cb.Run();
311 EXPECT_EQ(2, n);
312
313 n = 0;
314 cb.Run();
315 EXPECT_EQ(2, n);
316 }
317
318 // Function type support.
319 // - Normal function.
320 // - Normal function bound with non-refcounted first argument.
321 // - Method bound to non-const object.
322 // - Method bound to scoped_refptr.
323 // - Const method bound to non-const object.
324 // - Const method bound to const object.
325 // - Derived classes can be used with pointers to non-virtual base functions.
326 // - Derived classes can be used with pointers to virtual base functions (and
327 // preserve virtual dispatch).
TEST_F(BindTest,FunctionTypeSupport)328 TEST_F(BindTest, FunctionTypeSupport) {
329 EXPECT_CALL(static_func_mock_, VoidMethod0());
330 EXPECT_CALL(has_ref_, AddRef()).Times(5);
331 EXPECT_CALL(has_ref_, Release()).Times(5);
332 EXPECT_CALL(has_ref_, VoidMethod0()).Times(2);
333 EXPECT_CALL(has_ref_, VoidConstMethod0()).Times(2);
334
335 Closure normal_cb = Bind(&VoidFunc0);
336 Callback<NoRef*(void)> normal_non_refcounted_cb =
337 Bind(&PolymorphicIdentity<NoRef*>, &no_ref_);
338 normal_cb.Run();
339 EXPECT_EQ(&no_ref_, normal_non_refcounted_cb.Run());
340
341 Closure method_cb = Bind(&HasRef::VoidMethod0, &has_ref_);
342 Closure method_refptr_cb = Bind(&HasRef::VoidMethod0,
343 make_scoped_refptr(&has_ref_));
344 Closure const_method_nonconst_obj_cb = Bind(&HasRef::VoidConstMethod0,
345 &has_ref_);
346 Closure const_method_const_obj_cb = Bind(&HasRef::VoidConstMethod0,
347 const_has_ref_ptr_);
348 method_cb.Run();
349 method_refptr_cb.Run();
350 const_method_nonconst_obj_cb.Run();
351 const_method_const_obj_cb.Run();
352
353 Child child;
354 child.value = 0;
355 Closure virtual_set_cb = Bind(&Parent::VirtualSet, &child);
356 virtual_set_cb.Run();
357 EXPECT_EQ(kChildValue, child.value);
358
359 child.value = 0;
360 Closure non_virtual_set_cb = Bind(&Parent::NonVirtualSet, &child);
361 non_virtual_set_cb.Run();
362 EXPECT_EQ(kParentValue, child.value);
363 }
364
365 // Return value support.
366 // - Function with return value.
367 // - Method with return value.
368 // - Const method with return value.
TEST_F(BindTest,ReturnValues)369 TEST_F(BindTest, ReturnValues) {
370 EXPECT_CALL(static_func_mock_, IntMethod0()).WillOnce(Return(1337));
371 EXPECT_CALL(has_ref_, AddRef()).Times(3);
372 EXPECT_CALL(has_ref_, Release()).Times(3);
373 EXPECT_CALL(has_ref_, IntMethod0()).WillOnce(Return(31337));
374 EXPECT_CALL(has_ref_, IntConstMethod0())
375 .WillOnce(Return(41337))
376 .WillOnce(Return(51337));
377
378 Callback<int(void)> normal_cb = Bind(&IntFunc0);
379 Callback<int(void)> method_cb = Bind(&HasRef::IntMethod0, &has_ref_);
380 Callback<int(void)> const_method_nonconst_obj_cb =
381 Bind(&HasRef::IntConstMethod0, &has_ref_);
382 Callback<int(void)> const_method_const_obj_cb =
383 Bind(&HasRef::IntConstMethod0, const_has_ref_ptr_);
384 EXPECT_EQ(1337, normal_cb.Run());
385 EXPECT_EQ(31337, method_cb.Run());
386 EXPECT_EQ(41337, const_method_nonconst_obj_cb.Run());
387 EXPECT_EQ(51337, const_method_const_obj_cb.Run());
388 }
389
390 // IgnoreResult adapter test.
391 // - Function with return value.
392 // - Method with return value.
393 // - Const Method with return.
394 // - Method with return value bound to WeakPtr<>.
395 // - Const Method with return bound to WeakPtr<>.
TEST_F(BindTest,IgnoreResult)396 TEST_F(BindTest, IgnoreResult) {
397 EXPECT_CALL(static_func_mock_, IntMethod0()).WillOnce(Return(1337));
398 EXPECT_CALL(has_ref_, AddRef()).Times(2);
399 EXPECT_CALL(has_ref_, Release()).Times(2);
400 EXPECT_CALL(has_ref_, IntMethod0()).WillOnce(Return(10));
401 EXPECT_CALL(has_ref_, IntConstMethod0()).WillOnce(Return(11));
402 EXPECT_CALL(no_ref_, IntMethod0()).WillOnce(Return(12));
403 EXPECT_CALL(no_ref_, IntConstMethod0()).WillOnce(Return(13));
404
405 Closure normal_func_cb = Bind(IgnoreResult(&IntFunc0));
406 normal_func_cb.Run();
407
408 Closure non_void_method_cb =
409 Bind(IgnoreResult(&HasRef::IntMethod0), &has_ref_);
410 non_void_method_cb.Run();
411
412 Closure non_void_const_method_cb =
413 Bind(IgnoreResult(&HasRef::IntConstMethod0), &has_ref_);
414 non_void_const_method_cb.Run();
415
416 WeakPtrFactory<NoRef> weak_factory(&no_ref_);
417 WeakPtrFactory<const NoRef> const_weak_factory(const_no_ref_ptr_);
418
419 Closure non_void_weak_method_cb =
420 Bind(IgnoreResult(&NoRef::IntMethod0), weak_factory.GetWeakPtr());
421 non_void_weak_method_cb.Run();
422
423 Closure non_void_weak_const_method_cb =
424 Bind(IgnoreResult(&NoRef::IntConstMethod0), weak_factory.GetWeakPtr());
425 non_void_weak_const_method_cb.Run();
426
427 weak_factory.InvalidateWeakPtrs();
428 non_void_weak_const_method_cb.Run();
429 non_void_weak_method_cb.Run();
430 }
431
432 // Argument binding tests.
433 // - Argument binding to primitive.
434 // - Argument binding to primitive pointer.
435 // - Argument binding to a literal integer.
436 // - Argument binding to a literal string.
437 // - Argument binding with template function.
438 // - Argument binding to an object.
439 // - Argument binding to pointer to incomplete type.
440 // - Argument gets type converted.
441 // - Pointer argument gets converted.
442 // - Const Reference forces conversion.
TEST_F(BindTest,ArgumentBinding)443 TEST_F(BindTest, ArgumentBinding) {
444 int n = 2;
445
446 Callback<int(void)> bind_primitive_cb = Bind(&Identity, n);
447 EXPECT_EQ(n, bind_primitive_cb.Run());
448
449 Callback<int*(void)> bind_primitive_pointer_cb =
450 Bind(&PolymorphicIdentity<int*>, &n);
451 EXPECT_EQ(&n, bind_primitive_pointer_cb.Run());
452
453 Callback<int(void)> bind_int_literal_cb = Bind(&Identity, 3);
454 EXPECT_EQ(3, bind_int_literal_cb.Run());
455
456 Callback<const char*(void)> bind_string_literal_cb =
457 Bind(&CStringIdentity, "hi");
458 EXPECT_STREQ("hi", bind_string_literal_cb.Run());
459
460 Callback<int(void)> bind_template_function_cb =
461 Bind(&PolymorphicIdentity<int>, 4);
462 EXPECT_EQ(4, bind_template_function_cb.Run());
463
464 NoRefParent p;
465 p.value = 5;
466 Callback<int(void)> bind_object_cb = Bind(&UnwrapNoRefParent, p);
467 EXPECT_EQ(5, bind_object_cb.Run());
468
469 IncompleteType* incomplete_ptr = reinterpret_cast<IncompleteType*>(123);
470 Callback<IncompleteType*(void)> bind_incomplete_ptr_cb =
471 Bind(&PolymorphicIdentity<IncompleteType*>, incomplete_ptr);
472 EXPECT_EQ(incomplete_ptr, bind_incomplete_ptr_cb.Run());
473
474 NoRefChild c;
475 c.value = 6;
476 Callback<int(void)> bind_promotes_cb = Bind(&UnwrapNoRefParent, c);
477 EXPECT_EQ(6, bind_promotes_cb.Run());
478
479 c.value = 7;
480 Callback<int(void)> bind_pointer_promotes_cb =
481 Bind(&UnwrapNoRefParentPtr, &c);
482 EXPECT_EQ(7, bind_pointer_promotes_cb.Run());
483
484 c.value = 8;
485 Callback<int(void)> bind_const_reference_promotes_cb =
486 Bind(&UnwrapNoRefParentConstRef, c);
487 EXPECT_EQ(8, bind_const_reference_promotes_cb.Run());
488 }
489
490 // Unbound argument type support tests.
491 // - Unbound value.
492 // - Unbound pointer.
493 // - Unbound reference.
494 // - Unbound const reference.
495 // - Unbound unsized array.
496 // - Unbound sized array.
497 // - Unbound array-of-arrays.
TEST_F(BindTest,UnboundArgumentTypeSupport)498 TEST_F(BindTest, UnboundArgumentTypeSupport) {
499 Callback<void(int)> unbound_value_cb = Bind(&VoidPolymorphic1<int>);
500 Callback<void(int*)> unbound_pointer_cb = Bind(&VoidPolymorphic1<int*>);
501 Callback<void(int&)> unbound_ref_cb = Bind(&VoidPolymorphic1<int&>);
502 Callback<void(const int&)> unbound_const_ref_cb =
503 Bind(&VoidPolymorphic1<const int&>);
504 Callback<void(int[])> unbound_unsized_array_cb =
505 Bind(&VoidPolymorphic1<int[]>);
506 Callback<void(int[2])> unbound_sized_array_cb =
507 Bind(&VoidPolymorphic1<int[2]>);
508 Callback<void(int[][2])> unbound_array_of_arrays_cb =
509 Bind(&VoidPolymorphic1<int[][2]>);
510 }
511
512 // Function with unbound reference parameter.
513 // - Original parameter is modified by callback.
TEST_F(BindTest,UnboundReferenceSupport)514 TEST_F(BindTest, UnboundReferenceSupport) {
515 int n = 0;
516 Callback<void(int&)> unbound_ref_cb = Bind(&RefArgSet);
517 unbound_ref_cb.Run(n);
518 EXPECT_EQ(2, n);
519 }
520
521 // Functions that take reference parameters.
522 // - Forced reference parameter type still stores a copy.
523 // - Forced const reference parameter type still stores a copy.
TEST_F(BindTest,ReferenceArgumentBinding)524 TEST_F(BindTest, ReferenceArgumentBinding) {
525 int n = 1;
526 int& ref_n = n;
527 const int& const_ref_n = n;
528
529 Callback<int(void)> ref_copies_cb = Bind(&Identity, ref_n);
530 EXPECT_EQ(n, ref_copies_cb.Run());
531 n++;
532 EXPECT_EQ(n - 1, ref_copies_cb.Run());
533
534 Callback<int(void)> const_ref_copies_cb = Bind(&Identity, const_ref_n);
535 EXPECT_EQ(n, const_ref_copies_cb.Run());
536 n++;
537 EXPECT_EQ(n - 1, const_ref_copies_cb.Run());
538 }
539
540 // Check that we can pass in arrays and have them be stored as a pointer.
541 // - Array of values stores a pointer.
542 // - Array of const values stores a pointer.
TEST_F(BindTest,ArrayArgumentBinding)543 TEST_F(BindTest, ArrayArgumentBinding) {
544 int array[4] = {1, 1, 1, 1};
545 const int (*const_array_ptr)[4] = &array;
546
547 Callback<int(void)> array_cb = Bind(&ArrayGet, array, 1);
548 EXPECT_EQ(1, array_cb.Run());
549
550 Callback<int(void)> const_array_cb = Bind(&ArrayGet, *const_array_ptr, 1);
551 EXPECT_EQ(1, const_array_cb.Run());
552
553 array[1] = 3;
554 EXPECT_EQ(3, array_cb.Run());
555 EXPECT_EQ(3, const_array_cb.Run());
556 }
557
558 // Verify SupportsAddRefAndRelease correctly introspects the class type for
559 // AddRef() and Release().
560 // - Class with AddRef() and Release()
561 // - Class without AddRef() and Release()
562 // - Derived Class with AddRef() and Release()
563 // - Derived Class without AddRef() and Release()
564 // - Derived Class with AddRef() and Release() and a private destructor.
TEST_F(BindTest,SupportsAddRefAndRelease)565 TEST_F(BindTest, SupportsAddRefAndRelease) {
566 EXPECT_TRUE(internal::SupportsAddRefAndRelease<HasRef>::value);
567 EXPECT_FALSE(internal::SupportsAddRefAndRelease<NoRef>::value);
568
569 // StrictMock<T> is a derived class of T. So, we use StrictMock<HasRef> and
570 // StrictMock<NoRef> to test that SupportsAddRefAndRelease works over
571 // inheritance.
572 EXPECT_TRUE(internal::SupportsAddRefAndRelease<StrictMock<HasRef> >::value);
573 EXPECT_FALSE(internal::SupportsAddRefAndRelease<StrictMock<NoRef> >::value);
574
575 // This matters because the implementation creates a dummy class that
576 // inherits from the template type.
577 EXPECT_TRUE(internal::SupportsAddRefAndRelease<HasRefPrivateDtor>::value);
578 }
579
580 // Unretained() wrapper support.
581 // - Method bound to Unretained() non-const object.
582 // - Const method bound to Unretained() non-const object.
583 // - Const method bound to Unretained() const object.
TEST_F(BindTest,Unretained)584 TEST_F(BindTest, Unretained) {
585 EXPECT_CALL(no_ref_, VoidMethod0());
586 EXPECT_CALL(no_ref_, VoidConstMethod0()).Times(2);
587
588 Callback<void(void)> method_cb =
589 Bind(&NoRef::VoidMethod0, Unretained(&no_ref_));
590 method_cb.Run();
591
592 Callback<void(void)> const_method_cb =
593 Bind(&NoRef::VoidConstMethod0, Unretained(&no_ref_));
594 const_method_cb.Run();
595
596 Callback<void(void)> const_method_const_ptr_cb =
597 Bind(&NoRef::VoidConstMethod0, Unretained(const_no_ref_ptr_));
598 const_method_const_ptr_cb.Run();
599 }
600
601 // WeakPtr() support.
602 // - Method bound to WeakPtr<> to non-const object.
603 // - Const method bound to WeakPtr<> to non-const object.
604 // - Const method bound to WeakPtr<> to const object.
605 // - Normal Function with WeakPtr<> as P1 can have return type and is
606 // not canceled.
TEST_F(BindTest,WeakPtr)607 TEST_F(BindTest, WeakPtr) {
608 EXPECT_CALL(no_ref_, VoidMethod0());
609 EXPECT_CALL(no_ref_, VoidConstMethod0()).Times(2);
610
611 WeakPtrFactory<NoRef> weak_factory(&no_ref_);
612 WeakPtrFactory<const NoRef> const_weak_factory(const_no_ref_ptr_);
613
614 Closure method_cb =
615 Bind(&NoRef::VoidMethod0, weak_factory.GetWeakPtr());
616 method_cb.Run();
617
618 Closure const_method_cb =
619 Bind(&NoRef::VoidConstMethod0, const_weak_factory.GetWeakPtr());
620 const_method_cb.Run();
621
622 Closure const_method_const_ptr_cb =
623 Bind(&NoRef::VoidConstMethod0, const_weak_factory.GetWeakPtr());
624 const_method_const_ptr_cb.Run();
625
626 Callback<int(int)> normal_func_cb =
627 Bind(&FunctionWithWeakFirstParam, weak_factory.GetWeakPtr());
628 EXPECT_EQ(1, normal_func_cb.Run(1));
629
630 weak_factory.InvalidateWeakPtrs();
631 const_weak_factory.InvalidateWeakPtrs();
632
633 method_cb.Run();
634 const_method_cb.Run();
635 const_method_const_ptr_cb.Run();
636
637 // Still runs even after the pointers are invalidated.
638 EXPECT_EQ(2, normal_func_cb.Run(2));
639 }
640
641 // ConstRef() wrapper support.
642 // - Binding w/o ConstRef takes a copy.
643 // - Binding a ConstRef takes a reference.
644 // - Binding ConstRef to a function ConstRef does not copy on invoke.
TEST_F(BindTest,ConstRef)645 TEST_F(BindTest, ConstRef) {
646 int n = 1;
647
648 Callback<int(void)> copy_cb = Bind(&Identity, n);
649 Callback<int(void)> const_ref_cb = Bind(&Identity, ConstRef(n));
650 EXPECT_EQ(n, copy_cb.Run());
651 EXPECT_EQ(n, const_ref_cb.Run());
652 n++;
653 EXPECT_EQ(n - 1, copy_cb.Run());
654 EXPECT_EQ(n, const_ref_cb.Run());
655
656 int copies = 0;
657 int assigns = 0;
658 CopyCounter counter(&copies, &assigns);
659 Callback<int(void)> all_const_ref_cb =
660 Bind(&GetCopies, ConstRef(counter));
661 EXPECT_EQ(0, all_const_ref_cb.Run());
662 EXPECT_EQ(0, copies);
663 EXPECT_EQ(0, assigns);
664 }
665
TEST_F(BindTest,ScopedRefptr)666 TEST_F(BindTest, ScopedRefptr) {
667 // BUG: The scoped_refptr should cause the only AddRef()/Release() pair. But
668 // due to a bug in base::Bind(), there's an extra call when invoking the
669 // callback.
670 // https://code.google.com/p/chromium/issues/detail?id=251937
671 EXPECT_CALL(has_ref_, AddRef()).Times(2);
672 EXPECT_CALL(has_ref_, Release()).Times(2);
673
674 const scoped_refptr<StrictMock<HasRef> > refptr(&has_ref_);
675
676 Callback<int(void)> scoped_refptr_const_ref_cb =
677 Bind(&FunctionWithScopedRefptrFirstParam, base::ConstRef(refptr), 1);
678 EXPECT_EQ(1, scoped_refptr_const_ref_cb.Run());
679 }
680
681 // Test Owned() support.
TEST_F(BindTest,Owned)682 TEST_F(BindTest, Owned) {
683 int deletes = 0;
684 DeleteCounter* counter = new DeleteCounter(&deletes);
685
686 // If we don't capture, delete happens on Callback destruction/reset.
687 // return the same value.
688 Callback<DeleteCounter*(void)> no_capture_cb =
689 Bind(&PolymorphicIdentity<DeleteCounter*>, Owned(counter));
690 ASSERT_EQ(counter, no_capture_cb.Run());
691 ASSERT_EQ(counter, no_capture_cb.Run());
692 EXPECT_EQ(0, deletes);
693 no_capture_cb.Reset(); // This should trigger a delete.
694 EXPECT_EQ(1, deletes);
695
696 deletes = 0;
697 counter = new DeleteCounter(&deletes);
698 base::Closure own_object_cb =
699 Bind(&DeleteCounter::VoidMethod0, Owned(counter));
700 own_object_cb.Run();
701 EXPECT_EQ(0, deletes);
702 own_object_cb.Reset();
703 EXPECT_EQ(1, deletes);
704 }
705
706 // Passed() wrapper support.
707 // - Passed() can be constructed from a pointer to scoper.
708 // - Passed() can be constructed from a scoper rvalue.
709 // - Using Passed() gives Callback Ownership.
710 // - Ownership is transferred from Callback to callee on the first Run().
711 // - Callback supports unbound arguments.
TEST_F(BindTest,ScopedPtr)712 TEST_F(BindTest, ScopedPtr) {
713 int deletes = 0;
714
715 // Tests the Passed() function's support for pointers.
716 scoped_ptr<DeleteCounter> ptr(new DeleteCounter(&deletes));
717 Callback<scoped_ptr<DeleteCounter>(void)> unused_callback =
718 Bind(&PassThru<scoped_ptr<DeleteCounter> >, Passed(&ptr));
719 EXPECT_FALSE(ptr.get());
720 EXPECT_EQ(0, deletes);
721
722 // If we never invoke the Callback, it retains ownership and deletes.
723 unused_callback.Reset();
724 EXPECT_EQ(1, deletes);
725
726 // Tests the Passed() function's support for rvalues.
727 deletes = 0;
728 DeleteCounter* counter = new DeleteCounter(&deletes);
729 Callback<scoped_ptr<DeleteCounter>(void)> callback =
730 Bind(&PassThru<scoped_ptr<DeleteCounter> >,
731 Passed(scoped_ptr<DeleteCounter>(counter)));
732 EXPECT_FALSE(ptr.get());
733 EXPECT_EQ(0, deletes);
734
735 // Check that ownership can be transferred back out.
736 scoped_ptr<DeleteCounter> result = callback.Run();
737 ASSERT_EQ(counter, result.get());
738 EXPECT_EQ(0, deletes);
739
740 // Resetting does not delete since ownership was transferred.
741 callback.Reset();
742 EXPECT_EQ(0, deletes);
743
744 // Ensure that we actually did get ownership.
745 result.reset();
746 EXPECT_EQ(1, deletes);
747
748 // Test unbound argument forwarding.
749 Callback<scoped_ptr<DeleteCounter>(scoped_ptr<DeleteCounter>)> cb_unbound =
750 Bind(&PassThru<scoped_ptr<DeleteCounter> >);
751 ptr.reset(new DeleteCounter(&deletes));
752 cb_unbound.Run(ptr.Pass());
753 }
754
755 // Argument Copy-constructor usage for non-reference parameters.
756 // - Bound arguments are only copied once.
757 // - Forwarded arguments are only copied once.
758 // - Forwarded arguments with coercions are only copied twice (once for the
759 // coercion, and one for the final dispatch).
TEST_F(BindTest,ArgumentCopies)760 TEST_F(BindTest, ArgumentCopies) {
761 int copies = 0;
762 int assigns = 0;
763
764 CopyCounter counter(&copies, &assigns);
765
766 Callback<void(void)> copy_cb =
767 Bind(&VoidPolymorphic1<CopyCounter>, counter);
768 EXPECT_GE(1, copies);
769 EXPECT_EQ(0, assigns);
770
771 copies = 0;
772 assigns = 0;
773 Callback<void(CopyCounter)> forward_cb =
774 Bind(&VoidPolymorphic1<CopyCounter>);
775 forward_cb.Run(counter);
776 EXPECT_GE(1, copies);
777 EXPECT_EQ(0, assigns);
778
779 copies = 0;
780 assigns = 0;
781 DerivedCopyCounter dervied(&copies, &assigns);
782 Callback<void(CopyCounter)> coerce_cb =
783 Bind(&VoidPolymorphic1<CopyCounter>);
784 coerce_cb.Run(CopyCounter(dervied));
785 EXPECT_GE(2, copies);
786 EXPECT_EQ(0, assigns);
787 }
788
789 // Callback construction and assignment tests.
790 // - Construction from an InvokerStorageHolder should not cause ref/deref.
791 // - Assignment from other callback should only cause one ref
792 //
793 // TODO(ajwong): Is there actually a way to test this?
794
795 #if defined(OS_WIN)
FastCallFunc(int n)796 int __fastcall FastCallFunc(int n) {
797 return n;
798 }
799
StdCallFunc(int n)800 int __stdcall StdCallFunc(int n) {
801 return n;
802 }
803
804 // Windows specific calling convention support.
805 // - Can bind a __fastcall function.
806 // - Can bind a __stdcall function.
TEST_F(BindTest,WindowsCallingConventions)807 TEST_F(BindTest, WindowsCallingConventions) {
808 Callback<int(void)> fastcall_cb = Bind(&FastCallFunc, 1);
809 EXPECT_EQ(1, fastcall_cb.Run());
810
811 Callback<int(void)> stdcall_cb = Bind(&StdCallFunc, 2);
812 EXPECT_EQ(2, stdcall_cb.Run());
813 }
814 #endif
815
816 #if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) && GTEST_HAS_DEATH_TEST
817
818 // Test null callbacks cause a DCHECK.
TEST(BindDeathTest,NullCallback)819 TEST(BindDeathTest, NullCallback) {
820 base::Callback<void(int)> null_cb;
821 ASSERT_TRUE(null_cb.is_null());
822 EXPECT_DEATH(base::Bind(null_cb, 42), "");
823 }
824
825 #endif // (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) &&
826 // GTEST_HAS_DEATH_TEST
827
828 } // namespace
829 } // namespace base
830