blob: 2c93d53ee4b59afa26e3312ba0bc1ed1ebb3d396 [file] [log] [blame]
[email protected]44106182012-04-06 03:53:021// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]b38d3572011-02-15 01:27:382// 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
[email protected]b38d3572011-02-15 01:27:387#include "base/callback.h"
[email protected]206a2ae82011-12-22 21:12:588#include "base/memory/ref_counted.h"
9#include "base/memory/scoped_ptr.h"
10#include "base/memory/weak_ptr.h"
[email protected]b38d3572011-02-15 01:27:3811#include "testing/gmock/include/gmock/gmock.h"
12#include "testing/gtest/include/gtest/gtest.h"
13
14using ::testing::Mock;
15using ::testing::Return;
16using ::testing::StrictMock;
17
18namespace base {
19namespace {
20
[email protected]8217d4542011-10-01 06:31:4121class IncompleteType;
22
[email protected]b38d3572011-02-15 01:27:3823class NoRef {
24 public:
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
38class HasRef : public NoRef {
39 public:
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
[email protected]690bda882011-04-13 22:40:4650class HasRefPrivateDtor : public HasRef {
51 private:
52 ~HasRefPrivateDtor() {}
53};
54
[email protected]b38d3572011-02-15 01:27:3855static const int kParentValue = 1;
56static const int kChildValue = 2;
57
58class 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;
65};
66
67class Child : public Parent {
68 public:
[email protected]44106182012-04-06 03:53:0269 virtual void VirtualSet() OVERRIDE { value = kChildValue; }
[email protected]b38d3572011-02-15 01:27:3870 void NonVirtualSet() { value = kChildValue; }
71};
72
73class NoRefParent {
74 public:
75 virtual void VirtualSet() { value = kParentValue; }
76 void NonVirtualSet() { value = kParentValue; }
77 int value;
78};
79
80class NoRefChild : public NoRefParent {
[email protected]44106182012-04-06 03:53:0281 virtual void VirtualSet() OVERRIDE { value = kChildValue; }
[email protected]b38d3572011-02-15 01:27:3882 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.
87struct DerivedCopyCounter {
88 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.
96class CopyCounter {
97 public:
98 CopyCounter(int* copies, int* assigns)
99 : copies_(copies), assigns_(assigns) {
100 }
101
102 CopyCounter(const CopyCounter& other)
103 : copies_(other.copies_),
104 assigns_(other.assigns_) {
105 (*copies_)++;
106 }
107
[email protected]206a2ae82011-12-22 21:12:58108 // Probing for copies from coercion.
[email protected]f3c697c52013-01-15 10:52:11109 explicit CopyCounter(const DerivedCopyCounter& other)
[email protected]b38d3572011-02-15 01:27:38110 : copies_(other.copies_),
111 assigns_(other.assigns_) {
112 (*copies_)++;
113 }
114
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
126 int copies() const {
127 return *copies_;
128 }
129
130 int assigns() const {
131 return *assigns_;
132 }
133
134 private:
135 int* copies_;
136 int* assigns_;
137};
138
[email protected]08aa4552011-10-15 00:34:42139class DeleteCounter {
140 public:
141 explicit DeleteCounter(int* deletes)
142 : deletes_(deletes) {
143 }
144
145 ~DeleteCounter() {
146 (*deletes_)++;
147 }
148
149 void VoidMethod0() {}
150
151 private:
152 int* deletes_;
153};
154
[email protected]206a2ae82011-12-22 21:12:58155template <typename T>
156T PassThru(T scoper) {
157 return scoper.Pass();
158}
159
[email protected]b38d3572011-02-15 01:27:38160// Some test functions that we can Bind to.
161template <typename T>
162T PolymorphicIdentity(T t) {
163 return t;
164}
165
166template <typename T>
167void VoidPolymorphic1(T t) {
168}
169
170int Identity(int n) {
171 return n;
172}
173
174int ArrayGet(const int array[], int n) {
175 return array[n];
176}
177
178int Sum(int a, int b, int c, int d, int e, int f) {
179 return a + b + c + d + e + f;
180}
181
182const char* CStringIdentity(const char* s) {
183 return s;
184}
185
186int GetCopies(const CopyCounter& counter) {
187 return counter.copies();
188}
189
190int UnwrapNoRefParent(NoRefParent p) {
191 return p.value;
192}
193
194int UnwrapNoRefParentPtr(NoRefParent* p) {
195 return p->value;
196}
197
198int UnwrapNoRefParentConstRef(const NoRefParent& p) {
199 return p.value;
200}
201
[email protected]c18b1052011-03-24 02:02:17202void RefArgSet(int &n) {
203 n = 2;
204}
205
[email protected]e24f8762011-12-20 00:10:04206void PtrArgSet(int *n) {
207 *n = 2;
208}
209
[email protected]93540582011-05-16 22:35:14210int FunctionWithWeakFirstParam(WeakPtr<NoRef> o, int n) {
211 return n;
212}
213
[email protected]edd2f1b2013-06-22 20:32:50214int FunctionWithScopedRefptrFirstParam(const scoped_refptr<HasRef>& o, int n) {
215 return n;
216}
217
[email protected]e24f8762011-12-20 00:10:04218void TakesACallback(const Closure& callback) {
219 callback.Run();
220}
221
[email protected]b38d3572011-02-15 01:27:38222class 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_;
228 }
229
230 virtual ~BindTest() {
231 }
232
233 static void VoidFunc0(void) {
234 static_func_mock_ptr->VoidMethod0();
235 }
236
237 static int IntFunc0(void) { return static_func_mock_ptr->IntMethod0(); }
238
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_;
245
246 // Used by the static functions to perform expectations.
247 static StrictMock<NoRef>* static_func_mock_ptr;
248
249 private:
250 DISALLOW_COPY_AND_ASSIGN(BindTest);
251};
252
253StrictMock<NoRef>* BindTest::static_func_mock_ptr;
254
[email protected]b38d3572011-02-15 01:27:38255// Sanity check that we can instantiate a callback for each arity.
256TEST_F(BindTest, ArityTest) {
257 Callback<int(void)> c0 = Bind(&Sum, 32, 16, 8, 4, 2, 1);
258 EXPECT_EQ(63, c0.Run());
259
260 Callback<int(int)> c1 = Bind(&Sum, 32, 16, 8, 4, 2);
261 EXPECT_EQ(75, c1.Run(13));
262
263 Callback<int(int,int)> c2 = Bind(&Sum, 32, 16, 8, 4);
264 EXPECT_EQ(85, c2.Run(13, 12));
265
266 Callback<int(int,int,int)> c3 = Bind(&Sum, 32, 16, 8);
267 EXPECT_EQ(92, c3.Run(13, 12, 11));
268
269 Callback<int(int,int,int,int)> c4 = Bind(&Sum, 32, 16);
270 EXPECT_EQ(94, c4.Run(13, 12, 11, 10));
271
272 Callback<int(int,int,int,int,int)> c5 = Bind(&Sum, 32);
273 EXPECT_EQ(87, c5.Run(13, 12, 11, 10, 9));
274
275 Callback<int(int,int,int,int,int,int)> c6 = Bind(&Sum);
276 EXPECT_EQ(69, c6.Run(13, 12, 11, 10, 9, 14));
277}
278
[email protected]7296f2762011-11-21 19:23:44279// Test the Currying ability of the Callback system.
280TEST_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));
[email protected]cea20fe42011-09-30 09:09:34283
[email protected]7296f2762011-11-21 19:23:44284 Callback<int(int,int,int,int,int)> c5 = Bind(c6, 32);
285 EXPECT_EQ(87, c5.Run(13, 12, 11, 10, 9));
[email protected]cea20fe42011-09-30 09:09:34286
[email protected]7296f2762011-11-21 19:23:44287 Callback<int(int,int,int,int)> c4 = Bind(c5, 16);
288 EXPECT_EQ(94, c4.Run(13, 12, 11, 10));
[email protected]cea20fe42011-09-30 09:09:34289
[email protected]7296f2762011-11-21 19:23:44290 Callback<int(int,int,int)> c3 = Bind(c4, 8);
291 EXPECT_EQ(92, c3.Run(13, 12, 11));
[email protected]cea20fe42011-09-30 09:09:34292
[email protected]7296f2762011-11-21 19:23:44293 Callback<int(int,int)> c2 = Bind(c3, 4);
294 EXPECT_EQ(85, c2.Run(13, 12));
[email protected]cea20fe42011-09-30 09:09:34295
[email protected]7296f2762011-11-21 19:23:44296 Callback<int(int)> c1 = Bind(c2, 2);
297 EXPECT_EQ(75, c1.Run(13));
298
299 Callback<int(void)> c0 = Bind(c1, 1);
300 EXPECT_EQ(63, c0.Run());
[email protected]cea20fe42011-09-30 09:09:34301}
302
[email protected]e24f8762011-12-20 00:10:04303// 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.
306TEST_F(BindTest, CurryingRvalueResultOfBind) {
307 int n = 0;
308 Closure cb = base::Bind(&TakesACallback, base::Bind(&PtrArgSet, &n));
309
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);
316
317 n = 0;
318 cb.Run();
319 EXPECT_EQ(2, n);
320}
321
[email protected]b38d3572011-02-15 01:27:38322// Function type support.
323// - Normal function.
[email protected]81814bce2011-09-10 03:03:00324// - Normal function bound with non-refcounted first argument.
[email protected]b38d3572011-02-15 01:27:38325// - Method bound to non-const object.
[email protected]7a15d1172011-10-07 00:25:29326// - Method bound to scoped_refptr.
[email protected]b38d3572011-02-15 01:27:38327// - 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).
332TEST_F(BindTest, FunctionTypeSupport) {
333 EXPECT_CALL(static_func_mock_, VoidMethod0());
[email protected]7a15d1172011-10-07 00:25:29334 EXPECT_CALL(has_ref_, AddRef()).Times(5);
335 EXPECT_CALL(has_ref_, Release()).Times(5);
336 EXPECT_CALL(has_ref_, VoidMethod0()).Times(2);
[email protected]b38d3572011-02-15 01:27:38337 EXPECT_CALL(has_ref_, VoidConstMethod0()).Times(2);
338
339 Closure normal_cb = Bind(&VoidFunc0);
[email protected]81814bce2011-09-10 03:03:00340 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());
344
[email protected]b38d3572011-02-15 01:27:38345 Closure method_cb = Bind(&HasRef::VoidMethod0, &has_ref_);
[email protected]7a15d1172011-10-07 00:25:29346 Closure method_refptr_cb = Bind(&HasRef::VoidMethod0,
347 make_scoped_refptr(&has_ref_));
[email protected]b38d3572011-02-15 01:27:38348 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_);
[email protected]b38d3572011-02-15 01:27:38352 method_cb.Run();
[email protected]7a15d1172011-10-07 00:25:29353 method_refptr_cb.Run();
[email protected]b38d3572011-02-15 01:27:38354 const_method_nonconst_obj_cb.Run();
355 const_method_const_obj_cb.Run();
356
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);
362
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);
367}
368
369// Return value support.
370// - Function with return value.
371// - Method with return value.
372// - Const method with return value.
373TEST_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));
381
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());
392}
393
[email protected]7296f2762011-11-21 19:23:44394// 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<>.
400TEST_F(BindTest, IgnoreResult) {
[email protected]e8bfc31d2011-09-28 00:26:37401 EXPECT_CALL(static_func_mock_, IntMethod0()).WillOnce(Return(1337));
[email protected]7296f2762011-11-21 19:23:44402 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));
408
409 Closure normal_func_cb = Bind(IgnoreResult(&IntFunc0));
410 normal_func_cb.Run();
411
412 Closure non_void_method_cb =
413 Bind(IgnoreResult(&HasRef::IntMethod0), &has_ref_);
414 non_void_method_cb.Run();
415
416 Closure non_void_const_method_cb =
417 Bind(IgnoreResult(&HasRef::IntConstMethod0), &has_ref_);
418 non_void_const_method_cb.Run();
419
420 WeakPtrFactory<NoRef> weak_factory(&no_ref_);
421 WeakPtrFactory<const NoRef> const_weak_factory(const_no_ref_ptr_);
422
423 Closure non_void_weak_method_cb =
424 Bind(IgnoreResult(&NoRef::IntMethod0), weak_factory.GetWeakPtr());
425 non_void_weak_method_cb.Run();
426
427 Closure non_void_weak_const_method_cb =
428 Bind(IgnoreResult(&NoRef::IntConstMethod0), weak_factory.GetWeakPtr());
429 non_void_weak_const_method_cb.Run();
430
431 weak_factory.InvalidateWeakPtrs();
432 non_void_weak_const_method_cb.Run();
433 non_void_weak_method_cb.Run();
[email protected]e8bfc31d2011-09-28 00:26:37434}
435
[email protected]b38d3572011-02-15 01:27:38436// 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.
[email protected]8217d4542011-10-01 06:31:41443// - Argument binding to pointer to incomplete type.
[email protected]b38d3572011-02-15 01:27:38444// - Argument gets type converted.
445// - Pointer argument gets converted.
446// - Const Reference forces conversion.
447TEST_F(BindTest, ArgumentBinding) {
448 int n = 2;
449
450 Callback<int(void)> bind_primitive_cb = Bind(&Identity, n);
451 EXPECT_EQ(n, bind_primitive_cb.Run());
452
453 Callback<int*(void)> bind_primitive_pointer_cb =
454 Bind(&PolymorphicIdentity<int*>, &n);
455 EXPECT_EQ(&n, bind_primitive_pointer_cb.Run());
456
457 Callback<int(void)> bind_int_literal_cb = Bind(&Identity, 3);
458 EXPECT_EQ(3, bind_int_literal_cb.Run());
459
460 Callback<const char*(void)> bind_string_literal_cb =
461 Bind(&CStringIdentity, "hi");
462 EXPECT_STREQ("hi", bind_string_literal_cb.Run());
463
464 Callback<int(void)> bind_template_function_cb =
465 Bind(&PolymorphicIdentity<int>, 4);
466 EXPECT_EQ(4, bind_template_function_cb.Run());
467
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());
472
[email protected]8217d4542011-10-01 06:31:41473 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());
477
[email protected]b38d3572011-02-15 01:27:38478 NoRefChild c;
479 c.value = 6;
480 Callback<int(void)> bind_promotes_cb = Bind(&UnwrapNoRefParent, c);
481 EXPECT_EQ(6, bind_promotes_cb.Run());
482
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());
487
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());
492}
493
[email protected]c18b1052011-03-24 02:02:17494// 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.
502TEST_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]>);
514}
515
516// Function with unbound reference parameter.
[email protected]7296f2762011-11-21 19:23:44517// - Original parameter is modified by callback.
[email protected]c18b1052011-03-24 02:02:17518TEST_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);
523}
524
[email protected]b38d3572011-02-15 01:27:38525// Functions that take reference parameters.
526// - Forced reference parameter type still stores a copy.
527// - Forced const reference parameter type still stores a copy.
528TEST_F(BindTest, ReferenceArgumentBinding) {
529 int n = 1;
530 int& ref_n = n;
531 const int& const_ref_n = n;
532
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());
537
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());
542}
543
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.
547TEST_F(BindTest, ArrayArgumentBinding) {
548 int array[4] = {1, 1, 1, 1};
549 const int (*const_array_ptr)[4] = &array;
550
551 Callback<int(void)> array_cb = Bind(&ArrayGet, array, 1);
552 EXPECT_EQ(1, array_cb.Run());
553
554 Callback<int(void)> const_array_cb = Bind(&ArrayGet, *const_array_ptr, 1);
555 EXPECT_EQ(1, const_array_cb.Run());
556
557 array[1] = 3;
558 EXPECT_EQ(3, array_cb.Run());
559 EXPECT_EQ(3, const_array_cb.Run());
560}
561
562// Verify SupportsAddRefAndRelease correctly introspects the class type for
563// AddRef() and Release().
[email protected]690bda882011-04-13 22:40:46564// - 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.
[email protected]b38d3572011-02-15 01:27:38569TEST_F(BindTest, SupportsAddRefAndRelease) {
570 EXPECT_TRUE(internal::SupportsAddRefAndRelease<HasRef>::value);
571 EXPECT_FALSE(internal::SupportsAddRefAndRelease<NoRef>::value);
572
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);
[email protected]690bda882011-04-13 22:40:46578
579 // This matters because the implementation creates a dummy class that
580 // inherits from the template type.
581 EXPECT_TRUE(internal::SupportsAddRefAndRelease<HasRefPrivateDtor>::value);
[email protected]b38d3572011-02-15 01:27:38582}
583
584// Unretained() wrapper support.
[email protected]93540582011-05-16 22:35:14585// - Method bound to Unretained() non-const object.
[email protected]b38d3572011-02-15 01:27:38586// - Const method bound to Unretained() non-const object.
587// - Const method bound to Unretained() const object.
588TEST_F(BindTest, Unretained) {
589 EXPECT_CALL(no_ref_, VoidMethod0());
590 EXPECT_CALL(no_ref_, VoidConstMethod0()).Times(2);
591
592 Callback<void(void)> method_cb =
593 Bind(&NoRef::VoidMethod0, Unretained(&no_ref_));
594 method_cb.Run();
595
596 Callback<void(void)> const_method_cb =
597 Bind(&NoRef::VoidConstMethod0, Unretained(&no_ref_));
598 const_method_cb.Run();
599
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();
603}
604
[email protected]93540582011-05-16 22:35:14605// 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.
611TEST_F(BindTest, WeakPtr) {
612 EXPECT_CALL(no_ref_, VoidMethod0());
613 EXPECT_CALL(no_ref_, VoidConstMethod0()).Times(2);
614
615 WeakPtrFactory<NoRef> weak_factory(&no_ref_);
616 WeakPtrFactory<const NoRef> const_weak_factory(const_no_ref_ptr_);
617
[email protected]7296f2762011-11-21 19:23:44618 Closure method_cb =
[email protected]93540582011-05-16 22:35:14619 Bind(&NoRef::VoidMethod0, weak_factory.GetWeakPtr());
620 method_cb.Run();
621
[email protected]7296f2762011-11-21 19:23:44622 Closure const_method_cb =
[email protected]93540582011-05-16 22:35:14623 Bind(&NoRef::VoidConstMethod0, const_weak_factory.GetWeakPtr());
624 const_method_cb.Run();
625
[email protected]7296f2762011-11-21 19:23:44626 Closure const_method_const_ptr_cb =
[email protected]93540582011-05-16 22:35:14627 Bind(&NoRef::VoidConstMethod0, const_weak_factory.GetWeakPtr());
628 const_method_const_ptr_cb.Run();
629
630 Callback<int(int)> normal_func_cb =
631 Bind(&FunctionWithWeakFirstParam, weak_factory.GetWeakPtr());
632 EXPECT_EQ(1, normal_func_cb.Run(1));
633
634 weak_factory.InvalidateWeakPtrs();
635 const_weak_factory.InvalidateWeakPtrs();
636
637 method_cb.Run();
638 const_method_cb.Run();
639 const_method_const_ptr_cb.Run();
640
641 // Still runs even after the pointers are invalidated.
642 EXPECT_EQ(2, normal_func_cb.Run(2));
643}
644
[email protected]b38d3572011-02-15 01:27:38645// 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.
649TEST_F(BindTest, ConstRef) {
650 int n = 1;
651
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());
659
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);
668}
669
[email protected]edd2f1b2013-06-22 20:32:50670TEST_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);
677
678 const scoped_refptr<StrictMock<HasRef> > refptr(&has_ref_);
679
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());
683}
684
[email protected]08aa4552011-10-15 00:34:42685// Test Owned() support.
686TEST_F(BindTest, Owned) {
687 int deletes = 0;
688 DeleteCounter* counter = new DeleteCounter(&deletes);
689
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));
[email protected]206a2ae82011-12-22 21:12:58694 ASSERT_EQ(counter, no_capture_cb.Run());
695 ASSERT_EQ(counter, no_capture_cb.Run());
[email protected]08aa4552011-10-15 00:34:42696 EXPECT_EQ(0, deletes);
697 no_capture_cb.Reset(); // This should trigger a delete.
698 EXPECT_EQ(1, deletes);
699
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);
708}
709
[email protected]206a2ae82011-12-22 21:12:58710// 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.
716TEST_F(BindTest, ScopedPtr) {
717 int deletes = 0;
718
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);
725
726 // If we never invoke the Callback, it retains ownership and deletes.
727 unused_callback.Reset();
728 EXPECT_EQ(1, deletes);
729
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);
738
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);
743
744 // Resetting does not delete since ownership was transferred.
745 callback.Reset();
746 EXPECT_EQ(0, deletes);
747
748 // Ensure that we actually did get ownership.
749 result.reset();
750 EXPECT_EQ(1, deletes);
751
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());
757}
758
[email protected]b38d3572011-02-15 01:27:38759// Argument Copy-constructor usage for non-reference parameters.
760// - Bound arguments are only copied once.
761// - Forwarded arguments are only copied once.
[email protected]206a2ae82011-12-22 21:12:58762// - Forwarded arguments with coercions are only copied twice (once for the
763// coercion, and one for the final dispatch).
[email protected]b38d3572011-02-15 01:27:38764TEST_F(BindTest, ArgumentCopies) {
765 int copies = 0;
766 int assigns = 0;
767
768 CopyCounter counter(&copies, &assigns);
769
770 Callback<void(void)> copy_cb =
771 Bind(&VoidPolymorphic1<CopyCounter>, counter);
772 EXPECT_GE(1, copies);
773 EXPECT_EQ(0, assigns);
774
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);
782
783 copies = 0;
784 assigns = 0;
785 DerivedCopyCounter dervied(&copies, &assigns);
786 Callback<void(CopyCounter)> coerce_cb =
787 Bind(&VoidPolymorphic1<CopyCounter>);
[email protected]f3c697c52013-01-15 10:52:11788 coerce_cb.Run(CopyCounter(dervied));
[email protected]b38d3572011-02-15 01:27:38789 EXPECT_GE(2, copies);
790 EXPECT_EQ(0, assigns);
791}
792
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
796//
797// TODO(ajwong): Is there actually a way to test this?
798
[email protected]054ac7542011-02-27 01:25:59799#if defined(OS_WIN)
800int __fastcall FastCallFunc(int n) {
801 return n;
802}
803
804int __stdcall StdCallFunc(int n) {
805 return n;
806}
807
808// Windows specific calling convention support.
809// - Can bind a __fastcall function.
810// - Can bind a __stdcall function.
811TEST_F(BindTest, WindowsCallingConventions) {
812 Callback<int(void)> fastcall_cb = Bind(&FastCallFunc, 1);
813 EXPECT_EQ(1, fastcall_cb.Run());
814
815 Callback<int(void)> stdcall_cb = Bind(&StdCallFunc, 2);
816 EXPECT_EQ(2, stdcall_cb.Run());
817}
818#endif
819
[email protected]8cf362c2012-11-20 08:28:14820#if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) && GTEST_HAS_DEATH_TEST
821
822// Test null callbacks cause a DCHECK.
823TEST(BindDeathTest, NullCallback) {
824 base::Callback<void(int)> null_cb;
825 ASSERT_TRUE(null_cb.is_null());
826 EXPECT_DEATH(base::Bind(null_cb, 42), "");
827}
828
829#endif // (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) &&
830 // GTEST_HAS_DEATH_TEST
831
[email protected]b38d3572011-02-15 01:27:38832} // namespace
833} // namespace base