blob: 32c2c4a598ceb1119a6ace8d7c8c1384208b5c87 [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
dcheng69f2a042015-12-14 20:31:527#include <memory>
8#include <utility>
9
[email protected]b38d3572011-02-15 01:27:3810#include "base/callback.h"
[email protected]206a2ae82011-12-22 21:12:5811#include "base/memory/ref_counted.h"
12#include "base/memory/scoped_ptr.h"
13#include "base/memory/weak_ptr.h"
[email protected]b38d3572011-02-15 01:27:3814#include "testing/gmock/include/gmock/gmock.h"
15#include "testing/gtest/include/gtest/gtest.h"
16
17using ::testing::Mock;
18using ::testing::Return;
19using ::testing::StrictMock;
20
21namespace base {
22namespace {
23
[email protected]8217d4542011-10-01 06:31:4124class IncompleteType;
25
[email protected]b38d3572011-02-15 01:27:3826class NoRef {
27 public:
28 NoRef() {}
29
tzik3bc7779b2015-12-19 09:18:4630 MOCK_METHOD0(VoidMethod0, void());
31 MOCK_CONST_METHOD0(VoidConstMethod0, void());
[email protected]b38d3572011-02-15 01:27:3832
tzik3bc7779b2015-12-19 09:18:4633 MOCK_METHOD0(IntMethod0, int());
34 MOCK_CONST_METHOD0(IntConstMethod0, int());
[email protected]b38d3572011-02-15 01:27:3835
36 private:
37 // Particularly important in this test to ensure no copies are made.
38 DISALLOW_COPY_AND_ASSIGN(NoRef);
39};
40
41class HasRef : public NoRef {
42 public:
43 HasRef() {}
44
tzik3bc7779b2015-12-19 09:18:4645 MOCK_CONST_METHOD0(AddRef, void());
46 MOCK_CONST_METHOD0(Release, bool());
[email protected]b38d3572011-02-15 01:27:3847
48 private:
49 // Particularly important in this test to ensure no copies are made.
50 DISALLOW_COPY_AND_ASSIGN(HasRef);
51};
52
[email protected]690bda882011-04-13 22:40:4653class HasRefPrivateDtor : public HasRef {
54 private:
55 ~HasRefPrivateDtor() {}
56};
57
[email protected]b38d3572011-02-15 01:27:3858static const int kParentValue = 1;
59static const int kChildValue = 2;
60
61class Parent {
62 public:
tzik3bc7779b2015-12-19 09:18:4663 void AddRef() const {}
64 void Release() const {}
[email protected]b38d3572011-02-15 01:27:3865 virtual void VirtualSet() { value = kParentValue; }
66 void NonVirtualSet() { value = kParentValue; }
67 int value;
68};
69
70class Child : public Parent {
71 public:
dcheng56488182014-10-21 10:54:5172 void VirtualSet() override { value = kChildValue; }
[email protected]b38d3572011-02-15 01:27:3873 void NonVirtualSet() { value = kChildValue; }
74};
75
76class NoRefParent {
77 public:
78 virtual void VirtualSet() { value = kParentValue; }
79 void NonVirtualSet() { value = kParentValue; }
80 int value;
81};
82
83class NoRefChild : public NoRefParent {
dcheng56488182014-10-21 10:54:5184 void VirtualSet() override { value = kChildValue; }
[email protected]b38d3572011-02-15 01:27:3885 void NonVirtualSet() { value = kChildValue; }
86};
87
88// Used for probing the number of copies that occur if a type must be coerced
89// during argument forwarding in the Run() methods.
90struct DerivedCopyCounter {
91 DerivedCopyCounter(int* copies, int* assigns)
92 : copies_(copies), assigns_(assigns) {
93 }
94 int* copies_;
95 int* assigns_;
96};
97
98// Used for probing the number of copies in an argument.
99class CopyCounter {
100 public:
101 CopyCounter(int* copies, int* assigns)
102 : copies_(copies), assigns_(assigns) {
103 }
104
105 CopyCounter(const CopyCounter& other)
106 : copies_(other.copies_),
107 assigns_(other.assigns_) {
108 (*copies_)++;
109 }
110
[email protected]206a2ae82011-12-22 21:12:58111 // Probing for copies from coercion.
[email protected]f3c697c52013-01-15 10:52:11112 explicit CopyCounter(const DerivedCopyCounter& other)
[email protected]b38d3572011-02-15 01:27:38113 : copies_(other.copies_),
114 assigns_(other.assigns_) {
115 (*copies_)++;
116 }
117
118 const CopyCounter& operator=(const CopyCounter& rhs) {
119 copies_ = rhs.copies_;
120 assigns_ = rhs.assigns_;
121
122 if (assigns_) {
123 (*assigns_)++;
124 }
125
126 return *this;
127 }
128
129 int copies() const {
130 return *copies_;
131 }
132
[email protected]b38d3572011-02-15 01:27:38133 private:
134 int* copies_;
135 int* assigns_;
136};
137
[email protected]08aa4552011-10-15 00:34:42138class DeleteCounter {
139 public:
140 explicit DeleteCounter(int* deletes)
141 : deletes_(deletes) {
142 }
143
144 ~DeleteCounter() {
145 (*deletes_)++;
146 }
147
148 void VoidMethod0() {}
149
150 private:
151 int* deletes_;
152};
153
[email protected]206a2ae82011-12-22 21:12:58154template <typename T>
155T PassThru(T scoper) {
dcheng69f2a042015-12-14 20:31:52156 return scoper;
[email protected]206a2ae82011-12-22 21:12:58157}
158
[email protected]b38d3572011-02-15 01:27:38159// Some test functions that we can Bind to.
160template <typename T>
161T PolymorphicIdentity(T t) {
162 return t;
163}
164
tzik7fe3a682015-12-18 02:23:26165template <typename... Ts>
166struct VoidPolymorphic {
167 static void Run(Ts... t) {}
168};
[email protected]b38d3572011-02-15 01:27:38169
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
tzik3bc7779b2015-12-19 09:18:46233 static void VoidFunc0() {
[email protected]b38d3572011-02-15 01:27:38234 static_func_mock_ptr->VoidMethod0();
235 }
236
tzik3bc7779b2015-12-19 09:18:46237 static int IntFunc0() { return static_func_mock_ptr->IntMethod0(); }
[email protected]b38d3572011-02-15 01:27:38238
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) {
tzik3bc7779b2015-12-19 09:18:46257 Callback<int()> c0 = Bind(&Sum, 32, 16, 8, 4, 2, 1);
[email protected]b38d3572011-02-15 01:27:38258 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
tzik3bc7779b2015-12-19 09:18:46299 Callback<int()> c0 = Bind(c1, 1);
[email protected]7296f2762011-11-21 19:23:44300 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);
tzik3bc7779b2015-12-19 09:18:46340 Callback<NoRef*()> normal_non_refcounted_cb =
[email protected]81814bce2011-09-10 03:03:00341 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
tzik3bc7779b2015-12-19 09:18:46382 Callback<int()> normal_cb = Bind(&IntFunc0);
383 Callback<int()> method_cb = Bind(&HasRef::IntMethod0, &has_ref_);
384 Callback<int()> const_method_nonconst_obj_cb =
[email protected]b38d3572011-02-15 01:27:38385 Bind(&HasRef::IntConstMethod0, &has_ref_);
tzik3bc7779b2015-12-19 09:18:46386 Callback<int()> const_method_const_obj_cb =
[email protected]b38d3572011-02-15 01:27:38387 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
tzik3bc7779b2015-12-19 09:18:46450 Callback<int()> bind_primitive_cb = Bind(&Identity, n);
[email protected]b38d3572011-02-15 01:27:38451 EXPECT_EQ(n, bind_primitive_cb.Run());
452
tzik3bc7779b2015-12-19 09:18:46453 Callback<int*()> bind_primitive_pointer_cb =
[email protected]b38d3572011-02-15 01:27:38454 Bind(&PolymorphicIdentity<int*>, &n);
455 EXPECT_EQ(&n, bind_primitive_pointer_cb.Run());
456
tzik3bc7779b2015-12-19 09:18:46457 Callback<int()> bind_int_literal_cb = Bind(&Identity, 3);
[email protected]b38d3572011-02-15 01:27:38458 EXPECT_EQ(3, bind_int_literal_cb.Run());
459
tzik3bc7779b2015-12-19 09:18:46460 Callback<const char*()> bind_string_literal_cb =
[email protected]b38d3572011-02-15 01:27:38461 Bind(&CStringIdentity, "hi");
462 EXPECT_STREQ("hi", bind_string_literal_cb.Run());
463
tzik3bc7779b2015-12-19 09:18:46464 Callback<int()> bind_template_function_cb =
[email protected]b38d3572011-02-15 01:27:38465 Bind(&PolymorphicIdentity<int>, 4);
466 EXPECT_EQ(4, bind_template_function_cb.Run());
467
468 NoRefParent p;
469 p.value = 5;
tzik3bc7779b2015-12-19 09:18:46470 Callback<int()> bind_object_cb = Bind(&UnwrapNoRefParent, p);
[email protected]b38d3572011-02-15 01:27:38471 EXPECT_EQ(5, bind_object_cb.Run());
472
[email protected]8217d4542011-10-01 06:31:41473 IncompleteType* incomplete_ptr = reinterpret_cast<IncompleteType*>(123);
tzik3bc7779b2015-12-19 09:18:46474 Callback<IncompleteType*()> bind_incomplete_ptr_cb =
[email protected]8217d4542011-10-01 06:31:41475 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;
tzik3bc7779b2015-12-19 09:18:46480 Callback<int()> bind_promotes_cb = Bind(&UnwrapNoRefParent, c);
[email protected]b38d3572011-02-15 01:27:38481 EXPECT_EQ(6, bind_promotes_cb.Run());
482
483 c.value = 7;
tzik3bc7779b2015-12-19 09:18:46484 Callback<int()> bind_pointer_promotes_cb =
[email protected]b38d3572011-02-15 01:27:38485 Bind(&UnwrapNoRefParentPtr, &c);
486 EXPECT_EQ(7, bind_pointer_promotes_cb.Run());
487
488 c.value = 8;
tzik3bc7779b2015-12-19 09:18:46489 Callback<int()> bind_const_reference_promotes_cb =
[email protected]b38d3572011-02-15 01:27:38490 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) {
tzik7fe3a682015-12-18 02:23:26503 Callback<void(int)> unbound_value_cb = Bind(&VoidPolymorphic<int>::Run);
504 Callback<void(int*)> unbound_pointer_cb = Bind(&VoidPolymorphic<int*>::Run);
505 Callback<void(int&)> unbound_ref_cb = Bind(&VoidPolymorphic<int&>::Run);
[email protected]c18b1052011-03-24 02:02:17506 Callback<void(const int&)> unbound_const_ref_cb =
tzik7fe3a682015-12-18 02:23:26507 Bind(&VoidPolymorphic<const int&>::Run);
[email protected]c18b1052011-03-24 02:02:17508 Callback<void(int[])> unbound_unsized_array_cb =
tzik7fe3a682015-12-18 02:23:26509 Bind(&VoidPolymorphic<int[]>::Run);
[email protected]c18b1052011-03-24 02:02:17510 Callback<void(int[2])> unbound_sized_array_cb =
tzik7fe3a682015-12-18 02:23:26511 Bind(&VoidPolymorphic<int[2]>::Run);
[email protected]c18b1052011-03-24 02:02:17512 Callback<void(int[][2])> unbound_array_of_arrays_cb =
tzik7fe3a682015-12-18 02:23:26513 Bind(&VoidPolymorphic<int[][2]>::Run);
514
515 Callback<void(int&)> unbound_ref_with_bound_arg =
516 Bind(&VoidPolymorphic<int, int&>::Run, 1);
[email protected]c18b1052011-03-24 02:02:17517}
518
519// Function with unbound reference parameter.
[email protected]7296f2762011-11-21 19:23:44520// - Original parameter is modified by callback.
[email protected]c18b1052011-03-24 02:02:17521TEST_F(BindTest, UnboundReferenceSupport) {
522 int n = 0;
523 Callback<void(int&)> unbound_ref_cb = Bind(&RefArgSet);
524 unbound_ref_cb.Run(n);
525 EXPECT_EQ(2, n);
526}
527
[email protected]b38d3572011-02-15 01:27:38528// Functions that take reference parameters.
529// - Forced reference parameter type still stores a copy.
530// - Forced const reference parameter type still stores a copy.
531TEST_F(BindTest, ReferenceArgumentBinding) {
532 int n = 1;
533 int& ref_n = n;
534 const int& const_ref_n = n;
535
tzik3bc7779b2015-12-19 09:18:46536 Callback<int()> ref_copies_cb = Bind(&Identity, ref_n);
[email protected]b38d3572011-02-15 01:27:38537 EXPECT_EQ(n, ref_copies_cb.Run());
538 n++;
539 EXPECT_EQ(n - 1, ref_copies_cb.Run());
540
tzik3bc7779b2015-12-19 09:18:46541 Callback<int()> const_ref_copies_cb = Bind(&Identity, const_ref_n);
[email protected]b38d3572011-02-15 01:27:38542 EXPECT_EQ(n, const_ref_copies_cb.Run());
543 n++;
544 EXPECT_EQ(n - 1, const_ref_copies_cb.Run());
545}
546
547// Check that we can pass in arrays and have them be stored as a pointer.
548// - Array of values stores a pointer.
549// - Array of const values stores a pointer.
550TEST_F(BindTest, ArrayArgumentBinding) {
551 int array[4] = {1, 1, 1, 1};
552 const int (*const_array_ptr)[4] = &array;
553
tzik3bc7779b2015-12-19 09:18:46554 Callback<int()> array_cb = Bind(&ArrayGet, array, 1);
[email protected]b38d3572011-02-15 01:27:38555 EXPECT_EQ(1, array_cb.Run());
556
tzik3bc7779b2015-12-19 09:18:46557 Callback<int()> const_array_cb = Bind(&ArrayGet, *const_array_ptr, 1);
[email protected]b38d3572011-02-15 01:27:38558 EXPECT_EQ(1, const_array_cb.Run());
559
560 array[1] = 3;
561 EXPECT_EQ(3, array_cb.Run());
562 EXPECT_EQ(3, const_array_cb.Run());
563}
564
565// Verify SupportsAddRefAndRelease correctly introspects the class type for
566// AddRef() and Release().
[email protected]690bda882011-04-13 22:40:46567// - Class with AddRef() and Release()
568// - Class without AddRef() and Release()
569// - Derived Class with AddRef() and Release()
570// - Derived Class without AddRef() and Release()
571// - Derived Class with AddRef() and Release() and a private destructor.
[email protected]b38d3572011-02-15 01:27:38572TEST_F(BindTest, SupportsAddRefAndRelease) {
573 EXPECT_TRUE(internal::SupportsAddRefAndRelease<HasRef>::value);
574 EXPECT_FALSE(internal::SupportsAddRefAndRelease<NoRef>::value);
575
576 // StrictMock<T> is a derived class of T. So, we use StrictMock<HasRef> and
577 // StrictMock<NoRef> to test that SupportsAddRefAndRelease works over
578 // inheritance.
579 EXPECT_TRUE(internal::SupportsAddRefAndRelease<StrictMock<HasRef> >::value);
580 EXPECT_FALSE(internal::SupportsAddRefAndRelease<StrictMock<NoRef> >::value);
[email protected]690bda882011-04-13 22:40:46581
582 // This matters because the implementation creates a dummy class that
583 // inherits from the template type.
584 EXPECT_TRUE(internal::SupportsAddRefAndRelease<HasRefPrivateDtor>::value);
[email protected]b38d3572011-02-15 01:27:38585}
586
587// Unretained() wrapper support.
[email protected]93540582011-05-16 22:35:14588// - Method bound to Unretained() non-const object.
[email protected]b38d3572011-02-15 01:27:38589// - Const method bound to Unretained() non-const object.
590// - Const method bound to Unretained() const object.
591TEST_F(BindTest, Unretained) {
592 EXPECT_CALL(no_ref_, VoidMethod0());
593 EXPECT_CALL(no_ref_, VoidConstMethod0()).Times(2);
594
tzik3bc7779b2015-12-19 09:18:46595 Callback<void()> method_cb =
[email protected]b38d3572011-02-15 01:27:38596 Bind(&NoRef::VoidMethod0, Unretained(&no_ref_));
597 method_cb.Run();
598
tzik3bc7779b2015-12-19 09:18:46599 Callback<void()> const_method_cb =
[email protected]b38d3572011-02-15 01:27:38600 Bind(&NoRef::VoidConstMethod0, Unretained(&no_ref_));
601 const_method_cb.Run();
602
tzik3bc7779b2015-12-19 09:18:46603 Callback<void()> const_method_const_ptr_cb =
[email protected]b38d3572011-02-15 01:27:38604 Bind(&NoRef::VoidConstMethod0, Unretained(const_no_ref_ptr_));
605 const_method_const_ptr_cb.Run();
606}
607
[email protected]93540582011-05-16 22:35:14608// WeakPtr() support.
609// - Method bound to WeakPtr<> to non-const object.
610// - Const method bound to WeakPtr<> to non-const object.
611// - Const method bound to WeakPtr<> to const object.
612// - Normal Function with WeakPtr<> as P1 can have return type and is
613// not canceled.
614TEST_F(BindTest, WeakPtr) {
615 EXPECT_CALL(no_ref_, VoidMethod0());
616 EXPECT_CALL(no_ref_, VoidConstMethod0()).Times(2);
617
618 WeakPtrFactory<NoRef> weak_factory(&no_ref_);
619 WeakPtrFactory<const NoRef> const_weak_factory(const_no_ref_ptr_);
620
[email protected]7296f2762011-11-21 19:23:44621 Closure method_cb =
[email protected]93540582011-05-16 22:35:14622 Bind(&NoRef::VoidMethod0, weak_factory.GetWeakPtr());
623 method_cb.Run();
624
[email protected]7296f2762011-11-21 19:23:44625 Closure const_method_cb =
[email protected]93540582011-05-16 22:35:14626 Bind(&NoRef::VoidConstMethod0, const_weak_factory.GetWeakPtr());
627 const_method_cb.Run();
628
[email protected]7296f2762011-11-21 19:23:44629 Closure const_method_const_ptr_cb =
[email protected]93540582011-05-16 22:35:14630 Bind(&NoRef::VoidConstMethod0, const_weak_factory.GetWeakPtr());
631 const_method_const_ptr_cb.Run();
632
633 Callback<int(int)> normal_func_cb =
634 Bind(&FunctionWithWeakFirstParam, weak_factory.GetWeakPtr());
635 EXPECT_EQ(1, normal_func_cb.Run(1));
636
637 weak_factory.InvalidateWeakPtrs();
638 const_weak_factory.InvalidateWeakPtrs();
639
640 method_cb.Run();
641 const_method_cb.Run();
642 const_method_const_ptr_cb.Run();
643
644 // Still runs even after the pointers are invalidated.
645 EXPECT_EQ(2, normal_func_cb.Run(2));
646}
647
[email protected]b38d3572011-02-15 01:27:38648// ConstRef() wrapper support.
649// - Binding w/o ConstRef takes a copy.
650// - Binding a ConstRef takes a reference.
651// - Binding ConstRef to a function ConstRef does not copy on invoke.
652TEST_F(BindTest, ConstRef) {
653 int n = 1;
654
tzik3bc7779b2015-12-19 09:18:46655 Callback<int()> copy_cb = Bind(&Identity, n);
656 Callback<int()> const_ref_cb = Bind(&Identity, ConstRef(n));
[email protected]b38d3572011-02-15 01:27:38657 EXPECT_EQ(n, copy_cb.Run());
658 EXPECT_EQ(n, const_ref_cb.Run());
659 n++;
660 EXPECT_EQ(n - 1, copy_cb.Run());
661 EXPECT_EQ(n, const_ref_cb.Run());
662
663 int copies = 0;
664 int assigns = 0;
665 CopyCounter counter(&copies, &assigns);
tzik3bc7779b2015-12-19 09:18:46666 Callback<int()> all_const_ref_cb =
[email protected]b38d3572011-02-15 01:27:38667 Bind(&GetCopies, ConstRef(counter));
668 EXPECT_EQ(0, all_const_ref_cb.Run());
669 EXPECT_EQ(0, copies);
670 EXPECT_EQ(0, assigns);
671}
672
[email protected]edd2f1b2013-06-22 20:32:50673TEST_F(BindTest, ScopedRefptr) {
674 // BUG: The scoped_refptr should cause the only AddRef()/Release() pair. But
675 // due to a bug in base::Bind(), there's an extra call when invoking the
676 // callback.
677 // https://code.google.com/p/chromium/issues/detail?id=251937
678 EXPECT_CALL(has_ref_, AddRef()).Times(2);
679 EXPECT_CALL(has_ref_, Release()).Times(2);
680
681 const scoped_refptr<StrictMock<HasRef> > refptr(&has_ref_);
682
tzik3bc7779b2015-12-19 09:18:46683 Callback<int()> scoped_refptr_const_ref_cb =
[email protected]edd2f1b2013-06-22 20:32:50684 Bind(&FunctionWithScopedRefptrFirstParam, base::ConstRef(refptr), 1);
685 EXPECT_EQ(1, scoped_refptr_const_ref_cb.Run());
686}
687
[email protected]08aa4552011-10-15 00:34:42688// Test Owned() support.
689TEST_F(BindTest, Owned) {
690 int deletes = 0;
691 DeleteCounter* counter = new DeleteCounter(&deletes);
692
693 // If we don't capture, delete happens on Callback destruction/reset.
694 // return the same value.
tzik3bc7779b2015-12-19 09:18:46695 Callback<DeleteCounter*()> no_capture_cb =
[email protected]08aa4552011-10-15 00:34:42696 Bind(&PolymorphicIdentity<DeleteCounter*>, Owned(counter));
[email protected]206a2ae82011-12-22 21:12:58697 ASSERT_EQ(counter, no_capture_cb.Run());
698 ASSERT_EQ(counter, no_capture_cb.Run());
[email protected]08aa4552011-10-15 00:34:42699 EXPECT_EQ(0, deletes);
700 no_capture_cb.Reset(); // This should trigger a delete.
701 EXPECT_EQ(1, deletes);
702
703 deletes = 0;
704 counter = new DeleteCounter(&deletes);
705 base::Closure own_object_cb =
706 Bind(&DeleteCounter::VoidMethod0, Owned(counter));
707 own_object_cb.Run();
708 EXPECT_EQ(0, deletes);
709 own_object_cb.Reset();
710 EXPECT_EQ(1, deletes);
711}
712
[email protected]206a2ae82011-12-22 21:12:58713// Passed() wrapper support.
714// - Passed() can be constructed from a pointer to scoper.
715// - Passed() can be constructed from a scoper rvalue.
716// - Using Passed() gives Callback Ownership.
717// - Ownership is transferred from Callback to callee on the first Run().
718// - Callback supports unbound arguments.
719TEST_F(BindTest, ScopedPtr) {
720 int deletes = 0;
721
722 // Tests the Passed() function's support for pointers.
723 scoped_ptr<DeleteCounter> ptr(new DeleteCounter(&deletes));
tzik3bc7779b2015-12-19 09:18:46724 Callback<scoped_ptr<DeleteCounter>()> unused_callback =
[email protected]206a2ae82011-12-22 21:12:58725 Bind(&PassThru<scoped_ptr<DeleteCounter> >, Passed(&ptr));
726 EXPECT_FALSE(ptr.get());
727 EXPECT_EQ(0, deletes);
728
729 // If we never invoke the Callback, it retains ownership and deletes.
730 unused_callback.Reset();
731 EXPECT_EQ(1, deletes);
732
733 // Tests the Passed() function's support for rvalues.
734 deletes = 0;
735 DeleteCounter* counter = new DeleteCounter(&deletes);
tzik3bc7779b2015-12-19 09:18:46736 Callback<scoped_ptr<DeleteCounter>()> callback =
[email protected]206a2ae82011-12-22 21:12:58737 Bind(&PassThru<scoped_ptr<DeleteCounter> >,
738 Passed(scoped_ptr<DeleteCounter>(counter)));
739 EXPECT_FALSE(ptr.get());
740 EXPECT_EQ(0, deletes);
741
742 // Check that ownership can be transferred back out.
743 scoped_ptr<DeleteCounter> result = callback.Run();
744 ASSERT_EQ(counter, result.get());
745 EXPECT_EQ(0, deletes);
746
747 // Resetting does not delete since ownership was transferred.
748 callback.Reset();
749 EXPECT_EQ(0, deletes);
750
751 // Ensure that we actually did get ownership.
752 result.reset();
753 EXPECT_EQ(1, deletes);
754
755 // Test unbound argument forwarding.
756 Callback<scoped_ptr<DeleteCounter>(scoped_ptr<DeleteCounter>)> cb_unbound =
757 Bind(&PassThru<scoped_ptr<DeleteCounter> >);
758 ptr.reset(new DeleteCounter(&deletes));
dcheng9dfa1232015-12-15 05:11:17759 cb_unbound.Run(std::move(ptr));
[email protected]206a2ae82011-12-22 21:12:58760}
761
dcheng69f2a042015-12-14 20:31:52762TEST_F(BindTest, UniquePtr) {
763 int deletes = 0;
764
765 // Tests the Passed() function's support for pointers.
766 std::unique_ptr<DeleteCounter> ptr(new DeleteCounter(&deletes));
tzik3bc7779b2015-12-19 09:18:46767 Callback<std::unique_ptr<DeleteCounter>()> unused_callback =
dcheng69f2a042015-12-14 20:31:52768 Bind(&PassThru<std::unique_ptr<DeleteCounter>>, Passed(&ptr));
769 EXPECT_FALSE(ptr.get());
770 EXPECT_EQ(0, deletes);
771
772 // If we never invoke the Callback, it retains ownership and deletes.
773 unused_callback.Reset();
774 EXPECT_EQ(1, deletes);
775
776 // Tests the Passed() function's support for rvalues.
777 deletes = 0;
778 DeleteCounter* counter = new DeleteCounter(&deletes);
tzik3bc7779b2015-12-19 09:18:46779 Callback<std::unique_ptr<DeleteCounter>()> callback =
dcheng69f2a042015-12-14 20:31:52780 Bind(&PassThru<std::unique_ptr<DeleteCounter>>,
781 Passed(std::unique_ptr<DeleteCounter>(counter)));
782 EXPECT_FALSE(ptr.get());
783 EXPECT_EQ(0, deletes);
784
785 // Check that ownership can be transferred back out.
786 std::unique_ptr<DeleteCounter> result = callback.Run();
787 ASSERT_EQ(counter, result.get());
788 EXPECT_EQ(0, deletes);
789
790 // Resetting does not delete since ownership was transferred.
791 callback.Reset();
792 EXPECT_EQ(0, deletes);
793
794 // Ensure that we actually did get ownership.
795 result.reset();
796 EXPECT_EQ(1, deletes);
797
798 // Test unbound argument forwarding.
799 Callback<std::unique_ptr<DeleteCounter>(std::unique_ptr<DeleteCounter>)>
800 cb_unbound = Bind(&PassThru<std::unique_ptr<DeleteCounter>>);
801 ptr.reset(new DeleteCounter(&deletes));
802 cb_unbound.Run(std::move(ptr));
803}
804
[email protected]b38d3572011-02-15 01:27:38805// Argument Copy-constructor usage for non-reference parameters.
806// - Bound arguments are only copied once.
807// - Forwarded arguments are only copied once.
[email protected]206a2ae82011-12-22 21:12:58808// - Forwarded arguments with coercions are only copied twice (once for the
809// coercion, and one for the final dispatch).
[email protected]b38d3572011-02-15 01:27:38810TEST_F(BindTest, ArgumentCopies) {
811 int copies = 0;
812 int assigns = 0;
813
814 CopyCounter counter(&copies, &assigns);
815
tzik3bc7779b2015-12-19 09:18:46816 Callback<void()> copy_cb =
tzik7fe3a682015-12-18 02:23:26817 Bind(&VoidPolymorphic<CopyCounter>::Run, counter);
[email protected]b38d3572011-02-15 01:27:38818 EXPECT_GE(1, copies);
819 EXPECT_EQ(0, assigns);
820
821 copies = 0;
822 assigns = 0;
823 Callback<void(CopyCounter)> forward_cb =
tzik7fe3a682015-12-18 02:23:26824 Bind(&VoidPolymorphic<CopyCounter>::Run);
[email protected]b38d3572011-02-15 01:27:38825 forward_cb.Run(counter);
826 EXPECT_GE(1, copies);
827 EXPECT_EQ(0, assigns);
828
829 copies = 0;
830 assigns = 0;
thakisb25789fb2015-04-23 05:40:02831 DerivedCopyCounter derived(&copies, &assigns);
[email protected]b38d3572011-02-15 01:27:38832 Callback<void(CopyCounter)> coerce_cb =
tzik7fe3a682015-12-18 02:23:26833 Bind(&VoidPolymorphic<CopyCounter>::Run);
thakisb25789fb2015-04-23 05:40:02834 coerce_cb.Run(CopyCounter(derived));
[email protected]b38d3572011-02-15 01:27:38835 EXPECT_GE(2, copies);
836 EXPECT_EQ(0, assigns);
837}
838
839// Callback construction and assignment tests.
840// - Construction from an InvokerStorageHolder should not cause ref/deref.
841// - Assignment from other callback should only cause one ref
842//
843// TODO(ajwong): Is there actually a way to test this?
844
[email protected]054ac7542011-02-27 01:25:59845#if defined(OS_WIN)
846int __fastcall FastCallFunc(int n) {
847 return n;
848}
849
850int __stdcall StdCallFunc(int n) {
851 return n;
852}
853
854// Windows specific calling convention support.
855// - Can bind a __fastcall function.
856// - Can bind a __stdcall function.
857TEST_F(BindTest, WindowsCallingConventions) {
tzik3bc7779b2015-12-19 09:18:46858 Callback<int()> fastcall_cb = Bind(&FastCallFunc, 1);
[email protected]054ac7542011-02-27 01:25:59859 EXPECT_EQ(1, fastcall_cb.Run());
860
tzik3bc7779b2015-12-19 09:18:46861 Callback<int()> stdcall_cb = Bind(&StdCallFunc, 2);
[email protected]054ac7542011-02-27 01:25:59862 EXPECT_EQ(2, stdcall_cb.Run());
863}
864#endif
865
[email protected]8cf362c2012-11-20 08:28:14866#if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) && GTEST_HAS_DEATH_TEST
867
868// Test null callbacks cause a DCHECK.
869TEST(BindDeathTest, NullCallback) {
870 base::Callback<void(int)> null_cb;
871 ASSERT_TRUE(null_cb.is_null());
872 EXPECT_DEATH(base::Bind(null_cb, 42), "");
873}
874
875#endif // (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) &&
876 // GTEST_HAS_DEATH_TEST
877
[email protected]b38d3572011-02-15 01:27:38878} // namespace
879} // namespace base