blob: ff0400a8501a0c4c45e634e44a9965e6ed1c7582 [file] [log] [blame]
rsleevi@chromium.orgde3a6cf2012-04-06 12:53:02 +09001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
ajwong@chromium.orge2cca632011-02-15 10:27:38 +09002// 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
dcheng3d580cf2015-12-15 05:31:52 +09007#include <memory>
8#include <utility>
dchengb61e1f92016-02-02 13:09:33 +09009#include <vector>
dcheng3d580cf2015-12-15 05:31:52 +090010
ajwong@chromium.orge2cca632011-02-15 10:27:38 +090011#include "base/callback.h"
avia6a6a682015-12-27 07:15:14 +090012#include "base/macros.h"
dchengcc8e4d82016-04-05 06:25:51 +090013#include "base/memory/ptr_util.h"
ajwong@chromium.orgf66a7db2011-12-23 06:12:58 +090014#include "base/memory/ref_counted.h"
ajwong@chromium.orgf66a7db2011-12-23 06:12:58 +090015#include "base/memory/weak_ptr.h"
gabbcf9c762016-08-02 01:39:56 +090016#include "base/test/gtest_util.h"
avia6a6a682015-12-27 07:15:14 +090017#include "build/build_config.h"
ajwong@chromium.orge2cca632011-02-15 10:27:38 +090018#include "testing/gmock/include/gmock/gmock.h"
19#include "testing/gtest/include/gtest/gtest.h"
20
21using ::testing::Mock;
22using ::testing::Return;
23using ::testing::StrictMock;
24
25namespace base {
26namespace {
27
ajwong@chromium.org27e56852011-10-01 15:31:41 +090028class IncompleteType;
29
ajwong@chromium.orge2cca632011-02-15 10:27:38 +090030class NoRef {
31 public:
32 NoRef() {}
33
tzik260fab52015-12-19 18:18:46 +090034 MOCK_METHOD0(VoidMethod0, void());
35 MOCK_CONST_METHOD0(VoidConstMethod0, void());
ajwong@chromium.orge2cca632011-02-15 10:27:38 +090036
tzik260fab52015-12-19 18:18:46 +090037 MOCK_METHOD0(IntMethod0, int());
38 MOCK_CONST_METHOD0(IntConstMethod0, int());
ajwong@chromium.orge2cca632011-02-15 10:27:38 +090039
40 private:
41 // Particularly important in this test to ensure no copies are made.
42 DISALLOW_COPY_AND_ASSIGN(NoRef);
43};
44
45class HasRef : public NoRef {
46 public:
47 HasRef() {}
48
tzik260fab52015-12-19 18:18:46 +090049 MOCK_CONST_METHOD0(AddRef, void());
50 MOCK_CONST_METHOD0(Release, bool());
ajwong@chromium.orge2cca632011-02-15 10:27:38 +090051
52 private:
53 // Particularly important in this test to ensure no copies are made.
54 DISALLOW_COPY_AND_ASSIGN(HasRef);
55};
56
ajwong@chromium.org62997402011-04-14 07:40:46 +090057class HasRefPrivateDtor : public HasRef {
58 private:
59 ~HasRefPrivateDtor() {}
60};
61
ajwong@chromium.orge2cca632011-02-15 10:27:38 +090062static const int kParentValue = 1;
63static const int kChildValue = 2;
64
65class Parent {
66 public:
tzik260fab52015-12-19 18:18:46 +090067 void AddRef() const {}
68 void Release() const {}
ajwong@chromium.orge2cca632011-02-15 10:27:38 +090069 virtual void VirtualSet() { value = kParentValue; }
70 void NonVirtualSet() { value = kParentValue; }
71 int value;
72};
73
74class Child : public Parent {
75 public:
dcheng7dc8df52014-10-21 19:54:51 +090076 void VirtualSet() override { value = kChildValue; }
ajwong@chromium.orge2cca632011-02-15 10:27:38 +090077 void NonVirtualSet() { value = kChildValue; }
78};
79
80class NoRefParent {
81 public:
82 virtual void VirtualSet() { value = kParentValue; }
83 void NonVirtualSet() { value = kParentValue; }
84 int value;
85};
86
87class NoRefChild : public NoRefParent {
dcheng7dc8df52014-10-21 19:54:51 +090088 void VirtualSet() override { value = kChildValue; }
ajwong@chromium.orge2cca632011-02-15 10:27:38 +090089 void NonVirtualSet() { value = kChildValue; }
90};
91
tzik14cb77d2016-02-15 20:54:30 +090092// Used for probing the number of copies and moves that occur if a type must be
93// coerced during argument forwarding in the Run() methods.
94struct DerivedCopyMoveCounter {
95 DerivedCopyMoveCounter(int* copies,
96 int* assigns,
97 int* move_constructs,
98 int* move_assigns)
99 : copies_(copies),
100 assigns_(assigns),
101 move_constructs_(move_constructs),
102 move_assigns_(move_assigns) {}
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900103 int* copies_;
104 int* assigns_;
tzik14cb77d2016-02-15 20:54:30 +0900105 int* move_constructs_;
106 int* move_assigns_;
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900107};
108
tzik14cb77d2016-02-15 20:54:30 +0900109// Used for probing the number of copies and moves in an argument.
110class CopyMoveCounter {
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900111 public:
tzik14cb77d2016-02-15 20:54:30 +0900112 CopyMoveCounter(int* copies,
113 int* assigns,
114 int* move_constructs,
115 int* move_assigns)
116 : copies_(copies),
117 assigns_(assigns),
118 move_constructs_(move_constructs),
119 move_assigns_(move_assigns) {}
120
121 CopyMoveCounter(const CopyMoveCounter& other)
122 : copies_(other.copies_),
123 assigns_(other.assigns_),
124 move_constructs_(other.move_constructs_),
125 move_assigns_(other.move_assigns_) {
126 (*copies_)++;
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900127 }
128
tzik14cb77d2016-02-15 20:54:30 +0900129 CopyMoveCounter(CopyMoveCounter&& other)
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900130 : copies_(other.copies_),
tzik14cb77d2016-02-15 20:54:30 +0900131 assigns_(other.assigns_),
132 move_constructs_(other.move_constructs_),
133 move_assigns_(other.move_assigns_) {
134 (*move_constructs_)++;
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900135 }
136
ajwong@chromium.orgf66a7db2011-12-23 06:12:58 +0900137 // Probing for copies from coercion.
tzik14cb77d2016-02-15 20:54:30 +0900138 explicit CopyMoveCounter(const DerivedCopyMoveCounter& other)
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900139 : copies_(other.copies_),
tzik14cb77d2016-02-15 20:54:30 +0900140 assigns_(other.assigns_),
141 move_constructs_(other.move_constructs_),
142 move_assigns_(other.move_assigns_) {
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900143 (*copies_)++;
144 }
145
tzik14cb77d2016-02-15 20:54:30 +0900146 // Probing for moves from coercion.
147 explicit CopyMoveCounter(DerivedCopyMoveCounter&& other)
148 : copies_(other.copies_),
149 assigns_(other.assigns_),
150 move_constructs_(other.move_constructs_),
151 move_assigns_(other.move_assigns_) {
152 (*move_constructs_)++;
153 }
154
155 const CopyMoveCounter& operator=(const CopyMoveCounter& rhs) {
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900156 copies_ = rhs.copies_;
157 assigns_ = rhs.assigns_;
tzik14cb77d2016-02-15 20:54:30 +0900158 move_constructs_ = rhs.move_constructs_;
159 move_assigns_ = rhs.move_assigns_;
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900160
tzik14cb77d2016-02-15 20:54:30 +0900161 (*assigns_)++;
162
163 return *this;
164 }
165
166 const CopyMoveCounter& operator=(CopyMoveCounter&& rhs) {
167 copies_ = rhs.copies_;
168 assigns_ = rhs.assigns_;
169 move_constructs_ = rhs.move_constructs_;
170 move_assigns_ = rhs.move_assigns_;
171
172 (*move_assigns_)++;
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900173
174 return *this;
175 }
176
177 int copies() const {
178 return *copies_;
179 }
180
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900181 private:
182 int* copies_;
183 int* assigns_;
tzik14cb77d2016-02-15 20:54:30 +0900184 int* move_constructs_;
185 int* move_assigns_;
186};
187
188// Used for probing the number of copies in an argument. The instance is a
189// copyable and non-movable type.
190class CopyCounter {
191 public:
192 CopyCounter(int* copies, int* assigns)
193 : counter_(copies, assigns, nullptr, nullptr) {}
194 CopyCounter(const CopyCounter& other) : counter_(other.counter_) {}
195 CopyCounter& operator=(const CopyCounter& other) {
196 counter_ = other.counter_;
197 return *this;
198 }
199
200 explicit CopyCounter(const DerivedCopyMoveCounter& other) : counter_(other) {}
201
202 int copies() const { return counter_.copies(); }
203
204 private:
205 CopyMoveCounter counter_;
206};
207
208// Used for probing the number of moves in an argument. The instance is a
209// non-copyable and movable type.
210class MoveCounter {
211 public:
212 MoveCounter(int* move_constructs, int* move_assigns)
213 : counter_(nullptr, nullptr, move_constructs, move_assigns) {}
214 MoveCounter(MoveCounter&& other) : counter_(std::move(other.counter_)) {}
215 MoveCounter& operator=(MoveCounter&& other) {
216 counter_ = std::move(other.counter_);
217 return *this;
218 }
219
220 explicit MoveCounter(DerivedCopyMoveCounter&& other)
221 : counter_(std::move(other)) {}
222
223 private:
224 CopyMoveCounter counter_;
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900225};
226
ajwong@chromium.org41339142011-10-15 09:34:42 +0900227class DeleteCounter {
228 public:
229 explicit DeleteCounter(int* deletes)
230 : deletes_(deletes) {
231 }
232
233 ~DeleteCounter() {
234 (*deletes_)++;
235 }
236
237 void VoidMethod0() {}
238
239 private:
240 int* deletes_;
241};
242
ajwong@chromium.orgf66a7db2011-12-23 06:12:58 +0900243template <typename T>
244T PassThru(T scoper) {
dcheng3d580cf2015-12-15 05:31:52 +0900245 return scoper;
ajwong@chromium.orgf66a7db2011-12-23 06:12:58 +0900246}
247
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900248// Some test functions that we can Bind to.
249template <typename T>
250T PolymorphicIdentity(T t) {
251 return t;
252}
253
tzik5b8bf3f2015-12-18 11:23:26 +0900254template <typename... Ts>
255struct VoidPolymorphic {
256 static void Run(Ts... t) {}
257};
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900258
259int Identity(int n) {
260 return n;
261}
262
263int ArrayGet(const int array[], int n) {
264 return array[n];
265}
266
267int Sum(int a, int b, int c, int d, int e, int f) {
268 return a + b + c + d + e + f;
269}
270
271const char* CStringIdentity(const char* s) {
272 return s;
273}
274
tzik14cb77d2016-02-15 20:54:30 +0900275int GetCopies(const CopyMoveCounter& counter) {
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900276 return counter.copies();
277}
278
279int UnwrapNoRefParent(NoRefParent p) {
280 return p.value;
281}
282
283int UnwrapNoRefParentPtr(NoRefParent* p) {
284 return p->value;
285}
286
287int UnwrapNoRefParentConstRef(const NoRefParent& p) {
288 return p.value;
289}
290
ajwong@chromium.orga7e74822011-03-24 11:02:17 +0900291void RefArgSet(int &n) {
292 n = 2;
293}
294
ajwong@chromium.orgabd70002011-12-20 09:10:04 +0900295void PtrArgSet(int *n) {
296 *n = 2;
297}
298
ajwong@chromium.orgc711b822011-05-17 07:35:14 +0900299int FunctionWithWeakFirstParam(WeakPtr<NoRef> o, int n) {
300 return n;
301}
302
willchan@chromium.org6141a822013-06-23 05:32:50 +0900303int FunctionWithScopedRefptrFirstParam(const scoped_refptr<HasRef>& o, int n) {
304 return n;
305}
306
ajwong@chromium.orgabd70002011-12-20 09:10:04 +0900307void TakesACallback(const Closure& callback) {
308 callback.Run();
309}
310
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900311class BindTest : public ::testing::Test {
312 public:
313 BindTest() {
314 const_has_ref_ptr_ = &has_ref_;
315 const_no_ref_ptr_ = &no_ref_;
316 static_func_mock_ptr = &static_func_mock_;
317 }
318
319 virtual ~BindTest() {
320 }
321
tzik260fab52015-12-19 18:18:46 +0900322 static void VoidFunc0() {
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900323 static_func_mock_ptr->VoidMethod0();
324 }
325
tzik260fab52015-12-19 18:18:46 +0900326 static int IntFunc0() { return static_func_mock_ptr->IntMethod0(); }
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900327
328 protected:
329 StrictMock<NoRef> no_ref_;
330 StrictMock<HasRef> has_ref_;
331 const HasRef* const_has_ref_ptr_;
332 const NoRef* const_no_ref_ptr_;
333 StrictMock<NoRef> static_func_mock_;
334
335 // Used by the static functions to perform expectations.
336 static StrictMock<NoRef>* static_func_mock_ptr;
337
338 private:
339 DISALLOW_COPY_AND_ASSIGN(BindTest);
340};
341
342StrictMock<NoRef>* BindTest::static_func_mock_ptr;
343
tzike7cf9012016-08-23 13:23:49 +0900344TEST_F(BindTest, BasicTest) {
345 Callback<int(int, int, int)> cb = Bind(&Sum, 32, 16, 8);
346 EXPECT_EQ(92, cb.Run(13, 12, 11));
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900347
tzike7cf9012016-08-23 13:23:49 +0900348 Callback<int(int, int, int, int, int, int)> c1 = Bind(&Sum);
349 EXPECT_EQ(69, c1.Run(14, 13, 12, 11, 10, 9));
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900350
tzike7cf9012016-08-23 13:23:49 +0900351 Callback<int(int, int, int)> c2 = Bind(c1, 32, 16, 8);
352 EXPECT_EQ(86, c2.Run(11, 10, 9));
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900353
tzike7cf9012016-08-23 13:23:49 +0900354 Callback<int()> c3 = Bind(c2, 4, 2, 1);
355 EXPECT_EQ(63, c3.Run());
ajwong@chromium.orgd483ffe2011-09-30 18:09:34 +0900356}
357
ajwong@chromium.orgabd70002011-12-20 09:10:04 +0900358// Test that currying the rvalue result of another Bind() works correctly.
359// - rvalue should be usable as argument to Bind().
360// - multiple runs of resulting Callback remain valid.
361TEST_F(BindTest, CurryingRvalueResultOfBind) {
362 int n = 0;
363 Closure cb = base::Bind(&TakesACallback, base::Bind(&PtrArgSet, &n));
364
365 // If we implement Bind() such that the return value has auto_ptr-like
366 // semantics, the second call here will fail because ownership of
367 // the internal BindState<> would have been transfered to a *temporary*
368 // constructon of a Callback object on the first call.
369 cb.Run();
370 EXPECT_EQ(2, n);
371
372 n = 0;
373 cb.Run();
374 EXPECT_EQ(2, n);
375}
376
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900377// Function type support.
378// - Normal function.
ajwong@chromium.org505386d2011-09-10 12:03:00 +0900379// - Normal function bound with non-refcounted first argument.
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900380// - Method bound to non-const object.
ajwong@chromium.org28dfb112011-10-07 09:25:29 +0900381// - Method bound to scoped_refptr.
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900382// - Const method bound to non-const object.
383// - Const method bound to const object.
384// - Derived classes can be used with pointers to non-virtual base functions.
385// - Derived classes can be used with pointers to virtual base functions (and
386// preserve virtual dispatch).
387TEST_F(BindTest, FunctionTypeSupport) {
388 EXPECT_CALL(static_func_mock_, VoidMethod0());
tzik51faa092016-02-11 19:24:45 +0900389 EXPECT_CALL(has_ref_, AddRef()).Times(4);
390 EXPECT_CALL(has_ref_, Release()).Times(4);
ajwong@chromium.org28dfb112011-10-07 09:25:29 +0900391 EXPECT_CALL(has_ref_, VoidMethod0()).Times(2);
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900392 EXPECT_CALL(has_ref_, VoidConstMethod0()).Times(2);
393
394 Closure normal_cb = Bind(&VoidFunc0);
tzik260fab52015-12-19 18:18:46 +0900395 Callback<NoRef*()> normal_non_refcounted_cb =
ajwong@chromium.org505386d2011-09-10 12:03:00 +0900396 Bind(&PolymorphicIdentity<NoRef*>, &no_ref_);
397 normal_cb.Run();
398 EXPECT_EQ(&no_ref_, normal_non_refcounted_cb.Run());
399
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900400 Closure method_cb = Bind(&HasRef::VoidMethod0, &has_ref_);
ajwong@chromium.org28dfb112011-10-07 09:25:29 +0900401 Closure method_refptr_cb = Bind(&HasRef::VoidMethod0,
402 make_scoped_refptr(&has_ref_));
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900403 Closure const_method_nonconst_obj_cb = Bind(&HasRef::VoidConstMethod0,
404 &has_ref_);
405 Closure const_method_const_obj_cb = Bind(&HasRef::VoidConstMethod0,
406 const_has_ref_ptr_);
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900407 method_cb.Run();
ajwong@chromium.org28dfb112011-10-07 09:25:29 +0900408 method_refptr_cb.Run();
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900409 const_method_nonconst_obj_cb.Run();
410 const_method_const_obj_cb.Run();
411
412 Child child;
413 child.value = 0;
414 Closure virtual_set_cb = Bind(&Parent::VirtualSet, &child);
415 virtual_set_cb.Run();
416 EXPECT_EQ(kChildValue, child.value);
417
418 child.value = 0;
419 Closure non_virtual_set_cb = Bind(&Parent::NonVirtualSet, &child);
420 non_virtual_set_cb.Run();
421 EXPECT_EQ(kParentValue, child.value);
422}
423
424// Return value support.
425// - Function with return value.
426// - Method with return value.
427// - Const method with return value.
428TEST_F(BindTest, ReturnValues) {
429 EXPECT_CALL(static_func_mock_, IntMethod0()).WillOnce(Return(1337));
430 EXPECT_CALL(has_ref_, AddRef()).Times(3);
431 EXPECT_CALL(has_ref_, Release()).Times(3);
432 EXPECT_CALL(has_ref_, IntMethod0()).WillOnce(Return(31337));
433 EXPECT_CALL(has_ref_, IntConstMethod0())
434 .WillOnce(Return(41337))
435 .WillOnce(Return(51337));
436
tzik260fab52015-12-19 18:18:46 +0900437 Callback<int()> normal_cb = Bind(&IntFunc0);
438 Callback<int()> method_cb = Bind(&HasRef::IntMethod0, &has_ref_);
439 Callback<int()> const_method_nonconst_obj_cb =
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900440 Bind(&HasRef::IntConstMethod0, &has_ref_);
tzik260fab52015-12-19 18:18:46 +0900441 Callback<int()> const_method_const_obj_cb =
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900442 Bind(&HasRef::IntConstMethod0, const_has_ref_ptr_);
443 EXPECT_EQ(1337, normal_cb.Run());
444 EXPECT_EQ(31337, method_cb.Run());
445 EXPECT_EQ(41337, const_method_nonconst_obj_cb.Run());
446 EXPECT_EQ(51337, const_method_const_obj_cb.Run());
447}
448
ajwong@chromium.orgc9c79af2011-11-22 04:23:44 +0900449// IgnoreResult adapter test.
450// - Function with return value.
451// - Method with return value.
452// - Const Method with return.
453// - Method with return value bound to WeakPtr<>.
454// - Const Method with return bound to WeakPtr<>.
455TEST_F(BindTest, IgnoreResult) {
ajwong@chromium.org718dddf2011-09-28 09:26:37 +0900456 EXPECT_CALL(static_func_mock_, IntMethod0()).WillOnce(Return(1337));
ajwong@chromium.orgc9c79af2011-11-22 04:23:44 +0900457 EXPECT_CALL(has_ref_, AddRef()).Times(2);
458 EXPECT_CALL(has_ref_, Release()).Times(2);
459 EXPECT_CALL(has_ref_, IntMethod0()).WillOnce(Return(10));
460 EXPECT_CALL(has_ref_, IntConstMethod0()).WillOnce(Return(11));
461 EXPECT_CALL(no_ref_, IntMethod0()).WillOnce(Return(12));
462 EXPECT_CALL(no_ref_, IntConstMethod0()).WillOnce(Return(13));
463
464 Closure normal_func_cb = Bind(IgnoreResult(&IntFunc0));
465 normal_func_cb.Run();
466
467 Closure non_void_method_cb =
468 Bind(IgnoreResult(&HasRef::IntMethod0), &has_ref_);
469 non_void_method_cb.Run();
470
471 Closure non_void_const_method_cb =
472 Bind(IgnoreResult(&HasRef::IntConstMethod0), &has_ref_);
473 non_void_const_method_cb.Run();
474
475 WeakPtrFactory<NoRef> weak_factory(&no_ref_);
476 WeakPtrFactory<const NoRef> const_weak_factory(const_no_ref_ptr_);
477
478 Closure non_void_weak_method_cb =
479 Bind(IgnoreResult(&NoRef::IntMethod0), weak_factory.GetWeakPtr());
480 non_void_weak_method_cb.Run();
481
482 Closure non_void_weak_const_method_cb =
483 Bind(IgnoreResult(&NoRef::IntConstMethod0), weak_factory.GetWeakPtr());
484 non_void_weak_const_method_cb.Run();
485
486 weak_factory.InvalidateWeakPtrs();
487 non_void_weak_const_method_cb.Run();
488 non_void_weak_method_cb.Run();
ajwong@chromium.org718dddf2011-09-28 09:26:37 +0900489}
490
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900491// Argument binding tests.
492// - Argument binding to primitive.
493// - Argument binding to primitive pointer.
494// - Argument binding to a literal integer.
495// - Argument binding to a literal string.
496// - Argument binding with template function.
497// - Argument binding to an object.
ajwong@chromium.org27e56852011-10-01 15:31:41 +0900498// - Argument binding to pointer to incomplete type.
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900499// - Argument gets type converted.
500// - Pointer argument gets converted.
501// - Const Reference forces conversion.
502TEST_F(BindTest, ArgumentBinding) {
503 int n = 2;
504
tzik260fab52015-12-19 18:18:46 +0900505 Callback<int()> bind_primitive_cb = Bind(&Identity, n);
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900506 EXPECT_EQ(n, bind_primitive_cb.Run());
507
tzik260fab52015-12-19 18:18:46 +0900508 Callback<int*()> bind_primitive_pointer_cb =
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900509 Bind(&PolymorphicIdentity<int*>, &n);
510 EXPECT_EQ(&n, bind_primitive_pointer_cb.Run());
511
tzik260fab52015-12-19 18:18:46 +0900512 Callback<int()> bind_int_literal_cb = Bind(&Identity, 3);
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900513 EXPECT_EQ(3, bind_int_literal_cb.Run());
514
tzik260fab52015-12-19 18:18:46 +0900515 Callback<const char*()> bind_string_literal_cb =
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900516 Bind(&CStringIdentity, "hi");
517 EXPECT_STREQ("hi", bind_string_literal_cb.Run());
518
tzik260fab52015-12-19 18:18:46 +0900519 Callback<int()> bind_template_function_cb =
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900520 Bind(&PolymorphicIdentity<int>, 4);
521 EXPECT_EQ(4, bind_template_function_cb.Run());
522
523 NoRefParent p;
524 p.value = 5;
tzik260fab52015-12-19 18:18:46 +0900525 Callback<int()> bind_object_cb = Bind(&UnwrapNoRefParent, p);
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900526 EXPECT_EQ(5, bind_object_cb.Run());
527
ajwong@chromium.org27e56852011-10-01 15:31:41 +0900528 IncompleteType* incomplete_ptr = reinterpret_cast<IncompleteType*>(123);
tzik260fab52015-12-19 18:18:46 +0900529 Callback<IncompleteType*()> bind_incomplete_ptr_cb =
ajwong@chromium.org27e56852011-10-01 15:31:41 +0900530 Bind(&PolymorphicIdentity<IncompleteType*>, incomplete_ptr);
531 EXPECT_EQ(incomplete_ptr, bind_incomplete_ptr_cb.Run());
532
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900533 NoRefChild c;
534 c.value = 6;
tzik260fab52015-12-19 18:18:46 +0900535 Callback<int()> bind_promotes_cb = Bind(&UnwrapNoRefParent, c);
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900536 EXPECT_EQ(6, bind_promotes_cb.Run());
537
538 c.value = 7;
tzik260fab52015-12-19 18:18:46 +0900539 Callback<int()> bind_pointer_promotes_cb =
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900540 Bind(&UnwrapNoRefParentPtr, &c);
541 EXPECT_EQ(7, bind_pointer_promotes_cb.Run());
542
543 c.value = 8;
tzik260fab52015-12-19 18:18:46 +0900544 Callback<int()> bind_const_reference_promotes_cb =
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900545 Bind(&UnwrapNoRefParentConstRef, c);
546 EXPECT_EQ(8, bind_const_reference_promotes_cb.Run());
547}
548
ajwong@chromium.orga7e74822011-03-24 11:02:17 +0900549// Unbound argument type support tests.
550// - Unbound value.
551// - Unbound pointer.
552// - Unbound reference.
553// - Unbound const reference.
554// - Unbound unsized array.
555// - Unbound sized array.
556// - Unbound array-of-arrays.
557TEST_F(BindTest, UnboundArgumentTypeSupport) {
tzik5b8bf3f2015-12-18 11:23:26 +0900558 Callback<void(int)> unbound_value_cb = Bind(&VoidPolymorphic<int>::Run);
559 Callback<void(int*)> unbound_pointer_cb = Bind(&VoidPolymorphic<int*>::Run);
560 Callback<void(int&)> unbound_ref_cb = Bind(&VoidPolymorphic<int&>::Run);
ajwong@chromium.orga7e74822011-03-24 11:02:17 +0900561 Callback<void(const int&)> unbound_const_ref_cb =
tzik5b8bf3f2015-12-18 11:23:26 +0900562 Bind(&VoidPolymorphic<const int&>::Run);
ajwong@chromium.orga7e74822011-03-24 11:02:17 +0900563 Callback<void(int[])> unbound_unsized_array_cb =
tzik5b8bf3f2015-12-18 11:23:26 +0900564 Bind(&VoidPolymorphic<int[]>::Run);
ajwong@chromium.orga7e74822011-03-24 11:02:17 +0900565 Callback<void(int[2])> unbound_sized_array_cb =
tzik5b8bf3f2015-12-18 11:23:26 +0900566 Bind(&VoidPolymorphic<int[2]>::Run);
ajwong@chromium.orga7e74822011-03-24 11:02:17 +0900567 Callback<void(int[][2])> unbound_array_of_arrays_cb =
tzik5b8bf3f2015-12-18 11:23:26 +0900568 Bind(&VoidPolymorphic<int[][2]>::Run);
569
570 Callback<void(int&)> unbound_ref_with_bound_arg =
571 Bind(&VoidPolymorphic<int, int&>::Run, 1);
ajwong@chromium.orga7e74822011-03-24 11:02:17 +0900572}
573
574// Function with unbound reference parameter.
ajwong@chromium.orgc9c79af2011-11-22 04:23:44 +0900575// - Original parameter is modified by callback.
ajwong@chromium.orga7e74822011-03-24 11:02:17 +0900576TEST_F(BindTest, UnboundReferenceSupport) {
577 int n = 0;
578 Callback<void(int&)> unbound_ref_cb = Bind(&RefArgSet);
579 unbound_ref_cb.Run(n);
580 EXPECT_EQ(2, n);
581}
582
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900583// Functions that take reference parameters.
584// - Forced reference parameter type still stores a copy.
585// - Forced const reference parameter type still stores a copy.
586TEST_F(BindTest, ReferenceArgumentBinding) {
587 int n = 1;
588 int& ref_n = n;
589 const int& const_ref_n = n;
590
tzik260fab52015-12-19 18:18:46 +0900591 Callback<int()> ref_copies_cb = Bind(&Identity, ref_n);
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900592 EXPECT_EQ(n, ref_copies_cb.Run());
593 n++;
594 EXPECT_EQ(n - 1, ref_copies_cb.Run());
595
tzik260fab52015-12-19 18:18:46 +0900596 Callback<int()> const_ref_copies_cb = Bind(&Identity, const_ref_n);
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900597 EXPECT_EQ(n, const_ref_copies_cb.Run());
598 n++;
599 EXPECT_EQ(n - 1, const_ref_copies_cb.Run());
600}
601
602// Check that we can pass in arrays and have them be stored as a pointer.
603// - Array of values stores a pointer.
604// - Array of const values stores a pointer.
605TEST_F(BindTest, ArrayArgumentBinding) {
606 int array[4] = {1, 1, 1, 1};
607 const int (*const_array_ptr)[4] = &array;
608
tzik260fab52015-12-19 18:18:46 +0900609 Callback<int()> array_cb = Bind(&ArrayGet, array, 1);
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900610 EXPECT_EQ(1, array_cb.Run());
611
tzik260fab52015-12-19 18:18:46 +0900612 Callback<int()> const_array_cb = Bind(&ArrayGet, *const_array_ptr, 1);
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900613 EXPECT_EQ(1, const_array_cb.Run());
614
615 array[1] = 3;
616 EXPECT_EQ(3, array_cb.Run());
617 EXPECT_EQ(3, const_array_cb.Run());
618}
619
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900620// Unretained() wrapper support.
ajwong@chromium.orgc711b822011-05-17 07:35:14 +0900621// - Method bound to Unretained() non-const object.
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900622// - Const method bound to Unretained() non-const object.
623// - Const method bound to Unretained() const object.
624TEST_F(BindTest, Unretained) {
625 EXPECT_CALL(no_ref_, VoidMethod0());
626 EXPECT_CALL(no_ref_, VoidConstMethod0()).Times(2);
627
tzik260fab52015-12-19 18:18:46 +0900628 Callback<void()> method_cb =
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900629 Bind(&NoRef::VoidMethod0, Unretained(&no_ref_));
630 method_cb.Run();
631
tzik260fab52015-12-19 18:18:46 +0900632 Callback<void()> const_method_cb =
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900633 Bind(&NoRef::VoidConstMethod0, Unretained(&no_ref_));
634 const_method_cb.Run();
635
tzik260fab52015-12-19 18:18:46 +0900636 Callback<void()> const_method_const_ptr_cb =
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900637 Bind(&NoRef::VoidConstMethod0, Unretained(const_no_ref_ptr_));
638 const_method_const_ptr_cb.Run();
639}
640
ajwong@chromium.orgc711b822011-05-17 07:35:14 +0900641// WeakPtr() support.
642// - Method bound to WeakPtr<> to non-const object.
643// - Const method bound to WeakPtr<> to non-const object.
644// - Const method bound to WeakPtr<> to const object.
645// - Normal Function with WeakPtr<> as P1 can have return type and is
646// not canceled.
647TEST_F(BindTest, WeakPtr) {
648 EXPECT_CALL(no_ref_, VoidMethod0());
649 EXPECT_CALL(no_ref_, VoidConstMethod0()).Times(2);
650
651 WeakPtrFactory<NoRef> weak_factory(&no_ref_);
652 WeakPtrFactory<const NoRef> const_weak_factory(const_no_ref_ptr_);
653
ajwong@chromium.orgc9c79af2011-11-22 04:23:44 +0900654 Closure method_cb =
ajwong@chromium.orgc711b822011-05-17 07:35:14 +0900655 Bind(&NoRef::VoidMethod0, weak_factory.GetWeakPtr());
656 method_cb.Run();
657
ajwong@chromium.orgc9c79af2011-11-22 04:23:44 +0900658 Closure const_method_cb =
ajwong@chromium.orgc711b822011-05-17 07:35:14 +0900659 Bind(&NoRef::VoidConstMethod0, const_weak_factory.GetWeakPtr());
660 const_method_cb.Run();
661
ajwong@chromium.orgc9c79af2011-11-22 04:23:44 +0900662 Closure const_method_const_ptr_cb =
ajwong@chromium.orgc711b822011-05-17 07:35:14 +0900663 Bind(&NoRef::VoidConstMethod0, const_weak_factory.GetWeakPtr());
664 const_method_const_ptr_cb.Run();
665
666 Callback<int(int)> normal_func_cb =
667 Bind(&FunctionWithWeakFirstParam, weak_factory.GetWeakPtr());
668 EXPECT_EQ(1, normal_func_cb.Run(1));
669
670 weak_factory.InvalidateWeakPtrs();
671 const_weak_factory.InvalidateWeakPtrs();
672
673 method_cb.Run();
674 const_method_cb.Run();
675 const_method_const_ptr_cb.Run();
676
677 // Still runs even after the pointers are invalidated.
678 EXPECT_EQ(2, normal_func_cb.Run(2));
679}
680
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900681// ConstRef() wrapper support.
682// - Binding w/o ConstRef takes a copy.
683// - Binding a ConstRef takes a reference.
684// - Binding ConstRef to a function ConstRef does not copy on invoke.
685TEST_F(BindTest, ConstRef) {
686 int n = 1;
687
tzik260fab52015-12-19 18:18:46 +0900688 Callback<int()> copy_cb = Bind(&Identity, n);
689 Callback<int()> const_ref_cb = Bind(&Identity, ConstRef(n));
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900690 EXPECT_EQ(n, copy_cb.Run());
691 EXPECT_EQ(n, const_ref_cb.Run());
692 n++;
693 EXPECT_EQ(n - 1, copy_cb.Run());
694 EXPECT_EQ(n, const_ref_cb.Run());
695
696 int copies = 0;
697 int assigns = 0;
tzik14cb77d2016-02-15 20:54:30 +0900698 int move_constructs = 0;
699 int move_assigns = 0;
700 CopyMoveCounter counter(&copies, &assigns, &move_constructs, &move_assigns);
tzik260fab52015-12-19 18:18:46 +0900701 Callback<int()> all_const_ref_cb =
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900702 Bind(&GetCopies, ConstRef(counter));
703 EXPECT_EQ(0, all_const_ref_cb.Run());
704 EXPECT_EQ(0, copies);
705 EXPECT_EQ(0, assigns);
tzik14cb77d2016-02-15 20:54:30 +0900706 EXPECT_EQ(0, move_constructs);
707 EXPECT_EQ(0, move_assigns);
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900708}
709
willchan@chromium.org6141a822013-06-23 05:32:50 +0900710TEST_F(BindTest, ScopedRefptr) {
tzik304e98c2016-05-12 13:09:05 +0900711 EXPECT_CALL(has_ref_, AddRef()).Times(1);
712 EXPECT_CALL(has_ref_, Release()).Times(1);
willchan@chromium.org6141a822013-06-23 05:32:50 +0900713
tzik304e98c2016-05-12 13:09:05 +0900714 const scoped_refptr<HasRef> refptr(&has_ref_);
tzik260fab52015-12-19 18:18:46 +0900715 Callback<int()> scoped_refptr_const_ref_cb =
willchan@chromium.org6141a822013-06-23 05:32:50 +0900716 Bind(&FunctionWithScopedRefptrFirstParam, base::ConstRef(refptr), 1);
717 EXPECT_EQ(1, scoped_refptr_const_ref_cb.Run());
718}
719
ajwong@chromium.org41339142011-10-15 09:34:42 +0900720// Test Owned() support.
721TEST_F(BindTest, Owned) {
722 int deletes = 0;
723 DeleteCounter* counter = new DeleteCounter(&deletes);
724
725 // If we don't capture, delete happens on Callback destruction/reset.
726 // return the same value.
tzik260fab52015-12-19 18:18:46 +0900727 Callback<DeleteCounter*()> no_capture_cb =
ajwong@chromium.org41339142011-10-15 09:34:42 +0900728 Bind(&PolymorphicIdentity<DeleteCounter*>, Owned(counter));
ajwong@chromium.orgf66a7db2011-12-23 06:12:58 +0900729 ASSERT_EQ(counter, no_capture_cb.Run());
730 ASSERT_EQ(counter, no_capture_cb.Run());
ajwong@chromium.org41339142011-10-15 09:34:42 +0900731 EXPECT_EQ(0, deletes);
732 no_capture_cb.Reset(); // This should trigger a delete.
733 EXPECT_EQ(1, deletes);
734
735 deletes = 0;
736 counter = new DeleteCounter(&deletes);
737 base::Closure own_object_cb =
738 Bind(&DeleteCounter::VoidMethod0, Owned(counter));
739 own_object_cb.Run();
740 EXPECT_EQ(0, deletes);
741 own_object_cb.Reset();
742 EXPECT_EQ(1, deletes);
743}
744
tzikfa7381d2016-05-12 08:05:05 +0900745TEST_F(BindTest, UniquePtrReceiver) {
746 std::unique_ptr<StrictMock<NoRef>> no_ref(new StrictMock<NoRef>);
747 EXPECT_CALL(*no_ref, VoidMethod0()).Times(1);
748 Bind(&NoRef::VoidMethod0, std::move(no_ref)).Run();
749}
750
dcheng11c9cca2016-01-22 04:37:55 +0900751// Tests for Passed() wrapper support:
ajwong@chromium.orgf66a7db2011-12-23 06:12:58 +0900752// - Passed() can be constructed from a pointer to scoper.
753// - Passed() can be constructed from a scoper rvalue.
754// - Using Passed() gives Callback Ownership.
755// - Ownership is transferred from Callback to callee on the first Run().
756// - Callback supports unbound arguments.
dcheng11c9cca2016-01-22 04:37:55 +0900757template <typename T>
758class BindMoveOnlyTypeTest : public ::testing::Test {
759};
760
761struct CustomDeleter {
762 void operator()(DeleteCounter* c) { delete c; }
763};
764
765using MoveOnlyTypesToTest =
dchengcc8e4d82016-04-05 06:25:51 +0900766 ::testing::Types<std::unique_ptr<DeleteCounter>,
dcheng11c9cca2016-01-22 04:37:55 +0900767 std::unique_ptr<DeleteCounter, CustomDeleter>>;
768TYPED_TEST_CASE(BindMoveOnlyTypeTest, MoveOnlyTypesToTest);
769
770TYPED_TEST(BindMoveOnlyTypeTest, PassedToBoundCallback) {
ajwong@chromium.orgf66a7db2011-12-23 06:12:58 +0900771 int deletes = 0;
772
dcheng11c9cca2016-01-22 04:37:55 +0900773 TypeParam ptr(new DeleteCounter(&deletes));
774 Callback<TypeParam()> callback = Bind(&PassThru<TypeParam>, Passed(&ptr));
ajwong@chromium.orgf66a7db2011-12-23 06:12:58 +0900775 EXPECT_FALSE(ptr.get());
776 EXPECT_EQ(0, deletes);
777
778 // If we never invoke the Callback, it retains ownership and deletes.
ajwong@chromium.orgf66a7db2011-12-23 06:12:58 +0900779 callback.Reset();
ajwong@chromium.orgf66a7db2011-12-23 06:12:58 +0900780 EXPECT_EQ(1, deletes);
ajwong@chromium.orgf66a7db2011-12-23 06:12:58 +0900781}
782
dcheng11c9cca2016-01-22 04:37:55 +0900783TYPED_TEST(BindMoveOnlyTypeTest, PassedWithRvalue) {
dcheng3d580cf2015-12-15 05:31:52 +0900784 int deletes = 0;
dcheng11c9cca2016-01-22 04:37:55 +0900785 Callback<TypeParam()> callback = Bind(
786 &PassThru<TypeParam>, Passed(TypeParam(new DeleteCounter(&deletes))));
dcheng3d580cf2015-12-15 05:31:52 +0900787 EXPECT_EQ(0, deletes);
788
789 // If we never invoke the Callback, it retains ownership and deletes.
dcheng11c9cca2016-01-22 04:37:55 +0900790 callback.Reset();
dcheng3d580cf2015-12-15 05:31:52 +0900791 EXPECT_EQ(1, deletes);
dcheng11c9cca2016-01-22 04:37:55 +0900792}
dcheng3d580cf2015-12-15 05:31:52 +0900793
dcheng11c9cca2016-01-22 04:37:55 +0900794// Check that ownership can be transferred back out.
795TYPED_TEST(BindMoveOnlyTypeTest, ReturnMoveOnlyType) {
796 int deletes = 0;
dcheng3d580cf2015-12-15 05:31:52 +0900797 DeleteCounter* counter = new DeleteCounter(&deletes);
dcheng11c9cca2016-01-22 04:37:55 +0900798 Callback<TypeParam()> callback =
799 Bind(&PassThru<TypeParam>, Passed(TypeParam(counter)));
800 TypeParam result = callback.Run();
dcheng3d580cf2015-12-15 05:31:52 +0900801 ASSERT_EQ(counter, result.get());
802 EXPECT_EQ(0, deletes);
803
804 // Resetting does not delete since ownership was transferred.
805 callback.Reset();
806 EXPECT_EQ(0, deletes);
807
808 // Ensure that we actually did get ownership.
809 result.reset();
810 EXPECT_EQ(1, deletes);
dcheng11c9cca2016-01-22 04:37:55 +0900811}
dcheng3d580cf2015-12-15 05:31:52 +0900812
dcheng11c9cca2016-01-22 04:37:55 +0900813TYPED_TEST(BindMoveOnlyTypeTest, UnboundForwarding) {
814 int deletes = 0;
815 TypeParam ptr(new DeleteCounter(&deletes));
dcheng3d580cf2015-12-15 05:31:52 +0900816 // Test unbound argument forwarding.
dcheng11c9cca2016-01-22 04:37:55 +0900817 Callback<TypeParam(TypeParam)> cb_unbound = Bind(&PassThru<TypeParam>);
dcheng3d580cf2015-12-15 05:31:52 +0900818 cb_unbound.Run(std::move(ptr));
dcheng11c9cca2016-01-22 04:37:55 +0900819 EXPECT_EQ(1, deletes);
dcheng3d580cf2015-12-15 05:31:52 +0900820}
821
dchengcc8e4d82016-04-05 06:25:51 +0900822void VerifyVector(const std::vector<std::unique_ptr<int>>& v) {
dchengb61e1f92016-02-02 13:09:33 +0900823 ASSERT_EQ(1u, v.size());
824 EXPECT_EQ(12345, *v[0]);
825}
826
dchengcc8e4d82016-04-05 06:25:51 +0900827std::vector<std::unique_ptr<int>> AcceptAndReturnMoveOnlyVector(
828 std::vector<std::unique_ptr<int>> v) {
dchengb61e1f92016-02-02 13:09:33 +0900829 VerifyVector(v);
830 return v;
831}
832
833// Test that a vector containing move-only types can be used with Callback.
834TEST_F(BindTest, BindMoveOnlyVector) {
dchengcc8e4d82016-04-05 06:25:51 +0900835 using MoveOnlyVector = std::vector<std::unique_ptr<int>>;
dchengb61e1f92016-02-02 13:09:33 +0900836
837 MoveOnlyVector v;
dchengcc8e4d82016-04-05 06:25:51 +0900838 v.push_back(WrapUnique(new int(12345)));
dchengb61e1f92016-02-02 13:09:33 +0900839
840 // Early binding should work:
841 base::Callback<MoveOnlyVector()> bound_cb =
842 base::Bind(&AcceptAndReturnMoveOnlyVector, Passed(&v));
843 MoveOnlyVector intermediate_result = bound_cb.Run();
844 VerifyVector(intermediate_result);
845
846 // As should passing it as an argument to Run():
847 base::Callback<MoveOnlyVector(MoveOnlyVector)> unbound_cb =
848 base::Bind(&AcceptAndReturnMoveOnlyVector);
849 MoveOnlyVector final_result = unbound_cb.Run(std::move(intermediate_result));
850 VerifyVector(final_result);
851}
852
tzik14cb77d2016-02-15 20:54:30 +0900853// Argument copy-constructor usage for non-reference copy-only parameters.
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900854// - Bound arguments are only copied once.
855// - Forwarded arguments are only copied once.
ajwong@chromium.orgf66a7db2011-12-23 06:12:58 +0900856// - Forwarded arguments with coercions are only copied twice (once for the
857// coercion, and one for the final dispatch).
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900858TEST_F(BindTest, ArgumentCopies) {
859 int copies = 0;
860 int assigns = 0;
861
862 CopyCounter counter(&copies, &assigns);
tzik14cb77d2016-02-15 20:54:30 +0900863 Bind(&VoidPolymorphic<CopyCounter>::Run, counter);
864 EXPECT_EQ(1, copies);
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900865 EXPECT_EQ(0, assigns);
866
867 copies = 0;
868 assigns = 0;
tzik14cb77d2016-02-15 20:54:30 +0900869 Bind(&VoidPolymorphic<CopyCounter>::Run, CopyCounter(&copies, &assigns));
870 EXPECT_EQ(1, copies);
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900871 EXPECT_EQ(0, assigns);
872
873 copies = 0;
874 assigns = 0;
tzik14cb77d2016-02-15 20:54:30 +0900875 Bind(&VoidPolymorphic<CopyCounter>::Run).Run(counter);
tzik0d4bab22016-03-09 14:46:05 +0900876 EXPECT_EQ(2, copies);
ajwong@chromium.orge2cca632011-02-15 10:27:38 +0900877 EXPECT_EQ(0, assigns);
tzik14cb77d2016-02-15 20:54:30 +0900878
879 copies = 0;
880 assigns = 0;
881 Bind(&VoidPolymorphic<CopyCounter>::Run).Run(CopyCounter(&copies, &assigns));
882 EXPECT_EQ(1, copies);
883 EXPECT_EQ(0, assigns);
884
885 copies = 0;
886 assigns = 0;
887 DerivedCopyMoveCounter derived(&copies, &assigns, nullptr, nullptr);
888 Bind(&VoidPolymorphic<CopyCounter>::Run).Run(CopyCounter(derived));
889 EXPECT_EQ(2, copies);
890 EXPECT_EQ(0, assigns);
891
892 copies = 0;
893 assigns = 0;
894 Bind(&VoidPolymorphic<CopyCounter>::Run)
895 .Run(CopyCounter(
896 DerivedCopyMoveCounter(&copies, &assigns, nullptr, nullptr)));
897 EXPECT_EQ(2, copies);
898 EXPECT_EQ(0, assigns);
899}
900
901// Argument move-constructor usage for move-only parameters.
902// - Bound arguments passed by move are not copied.
903TEST_F(BindTest, ArgumentMoves) {
904 int move_constructs = 0;
905 int move_assigns = 0;
906
907 Bind(&VoidPolymorphic<const MoveCounter&>::Run,
908 MoveCounter(&move_constructs, &move_assigns));
909 EXPECT_EQ(1, move_constructs);
910 EXPECT_EQ(0, move_assigns);
911
912 // TODO(tzik): Support binding move-only type into a non-reference parameter
913 // of a variant of Callback.
914
tzik0d4bab22016-03-09 14:46:05 +0900915 move_constructs = 0;
916 move_assigns = 0;
917 Bind(&VoidPolymorphic<MoveCounter>::Run)
918 .Run(MoveCounter(&move_constructs, &move_assigns));
919 EXPECT_EQ(1, move_constructs);
920 EXPECT_EQ(0, move_assigns);
921
922 move_constructs = 0;
923 move_assigns = 0;
924 Bind(&VoidPolymorphic<MoveCounter>::Run)
925 .Run(MoveCounter(DerivedCopyMoveCounter(
926 nullptr, nullptr, &move_constructs, &move_assigns)));
927 EXPECT_EQ(2, move_constructs);
928 EXPECT_EQ(0, move_assigns);
tzik14cb77d2016-02-15 20:54:30 +0900929}
930
931// Argument constructor usage for non-reference movable-copyable
932// parameters.
933// - Bound arguments passed by move are not copied.
934// - Forwarded arguments are only copied once.
935// - Forwarded arguments with coercions are only copied once and moved once.
936TEST_F(BindTest, ArgumentCopiesAndMoves) {
937 int copies = 0;
938 int assigns = 0;
939 int move_constructs = 0;
940 int move_assigns = 0;
941
942 CopyMoveCounter counter(&copies, &assigns, &move_constructs, &move_assigns);
943 Bind(&VoidPolymorphic<CopyMoveCounter>::Run, counter);
944 EXPECT_EQ(1, copies);
945 EXPECT_EQ(0, assigns);
946 EXPECT_EQ(0, move_constructs);
947 EXPECT_EQ(0, move_assigns);
948
949 copies = 0;
950 assigns = 0;
951 move_constructs = 0;
952 move_assigns = 0;
953 Bind(&VoidPolymorphic<CopyMoveCounter>::Run,
954 CopyMoveCounter(&copies, &assigns, &move_constructs, &move_assigns));
955 EXPECT_EQ(0, copies);
956 EXPECT_EQ(0, assigns);
957 EXPECT_EQ(1, move_constructs);
958 EXPECT_EQ(0, move_assigns);
959
960 copies = 0;
961 assigns = 0;
962 move_constructs = 0;
963 move_assigns = 0;
964 Bind(&VoidPolymorphic<CopyMoveCounter>::Run).Run(counter);
965 EXPECT_EQ(1, copies);
966 EXPECT_EQ(0, assigns);
tzik0d4bab22016-03-09 14:46:05 +0900967 EXPECT_EQ(1, move_constructs);
tzik14cb77d2016-02-15 20:54:30 +0900968 EXPECT_EQ(0, move_assigns);
969
tzik14cb77d2016-02-15 20:54:30 +0900970 copies = 0;
971 assigns = 0;
972 move_constructs = 0;
973 move_assigns = 0;
974 Bind(&VoidPolymorphic<CopyMoveCounter>::Run)
975 .Run(CopyMoveCounter(&copies, &assigns, &move_constructs, &move_assigns));
tzik0d4bab22016-03-09 14:46:05 +0900976 EXPECT_EQ(0, copies);
tzik14cb77d2016-02-15 20:54:30 +0900977 EXPECT_EQ(0, assigns);
tzik0d4bab22016-03-09 14:46:05 +0900978 EXPECT_EQ(1, move_constructs);
tzik14cb77d2016-02-15 20:54:30 +0900979 EXPECT_EQ(0, move_assigns);
980
tzik14cb77d2016-02-15 20:54:30 +0900981 DerivedCopyMoveCounter derived_counter(&copies, &assigns, &move_constructs,
982 &move_assigns);
983 copies = 0;
984 assigns = 0;
985 move_constructs = 0;
986 move_assigns = 0;
987 Bind(&VoidPolymorphic<CopyMoveCounter>::Run)
988 .Run(CopyMoveCounter(derived_counter));
tzik0d4bab22016-03-09 14:46:05 +0900989 EXPECT_EQ(1, copies);
tzik14cb77d2016-02-15 20:54:30 +0900990 EXPECT_EQ(0, assigns);
tzik0d4bab22016-03-09 14:46:05 +0900991 EXPECT_EQ(1, move_constructs);
tzik14cb77d2016-02-15 20:54:30 +0900992 EXPECT_EQ(0, move_assigns);
993
tzik14cb77d2016-02-15 20:54:30 +0900994 copies = 0;
995 assigns = 0;
996 move_constructs = 0;
997 move_assigns = 0;
998 Bind(&VoidPolymorphic<CopyMoveCounter>::Run)
999 .Run(CopyMoveCounter(DerivedCopyMoveCounter(
1000 &copies, &assigns, &move_constructs, &move_assigns)));
tzik0d4bab22016-03-09 14:46:05 +09001001 EXPECT_EQ(0, copies);
tzik14cb77d2016-02-15 20:54:30 +09001002 EXPECT_EQ(0, assigns);
tzik0d4bab22016-03-09 14:46:05 +09001003 EXPECT_EQ(2, move_constructs);
tzik14cb77d2016-02-15 20:54:30 +09001004 EXPECT_EQ(0, move_assigns);
ajwong@chromium.orge2cca632011-02-15 10:27:38 +09001005}
1006
tzik31d3fae2016-07-08 18:42:38 +09001007TEST_F(BindTest, CapturelessLambda) {
1008 EXPECT_FALSE(internal::IsConvertibleToRunType<void>::value);
1009 EXPECT_FALSE(internal::IsConvertibleToRunType<int>::value);
1010 EXPECT_FALSE(internal::IsConvertibleToRunType<void(*)()>::value);
1011 EXPECT_FALSE(internal::IsConvertibleToRunType<void(NoRef::*)()>::value);
1012
1013 auto f = []() {};
1014 EXPECT_TRUE(internal::IsConvertibleToRunType<decltype(f)>::value);
1015
1016 int i = 0;
1017 auto g = [i]() {};
1018 EXPECT_FALSE(internal::IsConvertibleToRunType<decltype(g)>::value);
1019
1020 auto h = [](int, double) { return 'k'; };
1021 EXPECT_TRUE((std::is_same<
1022 char(int, double),
1023 internal::ExtractCallableRunType<decltype(h)>>::value));
1024
1025 EXPECT_EQ(42, Bind([] { return 42; }).Run());
1026 EXPECT_EQ(42, Bind([](int i) { return i * 7; }, 6).Run());
1027
1028 int x = 1;
1029 base::Callback<void(int)> cb =
1030 Bind([](int* x, int i) { *x *= i; }, Unretained(&x));
1031 cb.Run(6);
1032 EXPECT_EQ(6, x);
1033 cb.Run(7);
1034 EXPECT_EQ(42, x);
1035}
1036
tzik058872d2016-09-08 19:58:53 +09001037TEST_F(BindTest, Cancellation) {
1038 EXPECT_CALL(no_ref_, VoidMethod0()).Times(2);
1039
1040 WeakPtrFactory<NoRef> weak_factory(&no_ref_);
1041 Closure cb = Bind(&NoRef::VoidMethod0, weak_factory.GetWeakPtr());
1042 Closure cb2 = Bind(cb);
1043
1044 EXPECT_FALSE(cb.IsCancelled());
1045 EXPECT_FALSE(cb2.IsCancelled());
1046
1047 cb.Run();
1048 cb2.Run();
1049
1050 weak_factory.InvalidateWeakPtrs();
1051
1052 EXPECT_TRUE(cb.IsCancelled());
1053 EXPECT_TRUE(cb2.IsCancelled());
1054
1055 cb.Run();
1056 cb2.Run();
1057}
1058
tzikc60a8122016-09-13 14:28:59 +09001059TEST_F(BindTest, OnceCallback) {
1060 using internal::OnceClosure;
1061 using internal::RepeatingClosure;
1062 using internal::BindOnce;
1063 using internal::BindRepeating;
1064 using internal::OnceCallback;
1065
1066 // Check if Callback variants have declarations of conversions as expected.
1067 // Copy constructor and assignment of RepeatingCallback.
1068 static_assert(std::is_constructible<
1069 RepeatingClosure, const RepeatingClosure&>::value,
1070 "RepeatingClosure should be copyable.");
1071 static_assert(is_assignable<
1072 RepeatingClosure, const RepeatingClosure&>::value,
1073 "RepeatingClosure should be copy-assignable.");
1074
1075 // Move constructor and assignment of RepeatingCallback.
1076 static_assert(std::is_constructible<
1077 RepeatingClosure, RepeatingClosure&&>::value,
1078 "RepeatingClosure should be movable.");
1079 static_assert(is_assignable<
1080 RepeatingClosure, RepeatingClosure&&>::value,
1081 "RepeatingClosure should be move-assignable");
1082
1083 // Conversions from OnceCallback to RepeatingCallback.
1084 static_assert(!std::is_constructible<
1085 RepeatingClosure, const OnceClosure&>::value,
1086 "OnceClosure should not be convertible to RepeatingClosure.");
1087 static_assert(!is_assignable<
1088 RepeatingClosure, const OnceClosure&>::value,
1089 "OnceClosure should not be convertible to RepeatingClosure.");
1090
1091 // Destructive conversions from OnceCallback to RepeatingCallback.
1092 static_assert(!std::is_constructible<
1093 RepeatingClosure, OnceClosure&&>::value,
1094 "OnceClosure should not be convertible to RepeatingClosure.");
1095 static_assert(!is_assignable<
1096 RepeatingClosure, OnceClosure&&>::value,
1097 "OnceClosure should not be convertible to RepeatingClosure.");
1098
1099 // Copy constructor and assignment of OnceCallback.
1100 static_assert(!std::is_constructible<
1101 OnceClosure, const OnceClosure&>::value,
1102 "OnceClosure should not be copyable.");
1103 static_assert(!is_assignable<
1104 OnceClosure, const OnceClosure&>::value,
1105 "OnceClosure should not be copy-assignable");
1106
1107 // Move constructor and assignment of OnceCallback.
1108 static_assert(std::is_constructible<
1109 OnceClosure, OnceClosure&&>::value,
1110 "OnceClosure should be movable.");
1111 static_assert(is_assignable<
1112 OnceClosure, OnceClosure&&>::value,
1113 "OnceClosure should be move-assignable.");
1114
1115 // Conversions from RepeatingCallback to OnceCallback.
1116 static_assert(std::is_constructible<
1117 OnceClosure, const RepeatingClosure&>::value,
1118 "RepeatingClosure should be convertible to OnceClosure.");
1119 static_assert(is_assignable<
1120 OnceClosure, const RepeatingClosure&>::value,
1121 "RepeatingClosure should be convertible to OnceClosure.");
1122
1123 // Destructive conversions from RepeatingCallback to OnceCallback.
1124 static_assert(std::is_constructible<
1125 OnceClosure, RepeatingClosure&&>::value,
1126 "RepeatingClosure should be convertible to OnceClosure.");
1127 static_assert(is_assignable<
1128 OnceClosure, RepeatingClosure&&>::value,
1129 "RepeatingClosure should be covretible to OnceClosure.");
1130
1131 OnceClosure cb = BindOnce(&VoidPolymorphic<>::Run);
1132 std::move(cb).Run();
1133
1134 // RepeatingCallback should be convertible to OnceCallback.
1135 OnceClosure cb2 = BindRepeating(&VoidPolymorphic<>::Run);
1136 std::move(cb2).Run();
1137
1138 RepeatingClosure cb3 = BindRepeating(&VoidPolymorphic<>::Run);
1139 cb = cb3;
1140 std::move(cb).Run();
1141
1142 cb = std::move(cb2);
1143
1144 OnceCallback<void(int)> cb4 = BindOnce(
1145 &VoidPolymorphic<std::unique_ptr<int>, int>::Run, MakeUnique<int>(0));
1146 BindOnce(std::move(cb4), 1).Run();
1147}
1148
ajwong@chromium.orge2cca632011-02-15 10:27:38 +09001149// Callback construction and assignment tests.
1150// - Construction from an InvokerStorageHolder should not cause ref/deref.
1151// - Assignment from other callback should only cause one ref
1152//
1153// TODO(ajwong): Is there actually a way to test this?
1154
ajwong@chromium.orgcb175342011-02-27 10:25:59 +09001155#if defined(OS_WIN)
1156int __fastcall FastCallFunc(int n) {
1157 return n;
1158}
1159
1160int __stdcall StdCallFunc(int n) {
1161 return n;
1162}
1163
1164// Windows specific calling convention support.
1165// - Can bind a __fastcall function.
1166// - Can bind a __stdcall function.
1167TEST_F(BindTest, WindowsCallingConventions) {
tzik260fab52015-12-19 18:18:46 +09001168 Callback<int()> fastcall_cb = Bind(&FastCallFunc, 1);
ajwong@chromium.orgcb175342011-02-27 10:25:59 +09001169 EXPECT_EQ(1, fastcall_cb.Run());
1170
tzik260fab52015-12-19 18:18:46 +09001171 Callback<int()> stdcall_cb = Bind(&StdCallFunc, 2);
ajwong@chromium.orgcb175342011-02-27 10:25:59 +09001172 EXPECT_EQ(2, stdcall_cb.Run());
1173}
1174#endif
1175
hashimoto@chromium.orgbc14c572012-11-20 17:28:14 +09001176// Test null callbacks cause a DCHECK.
1177TEST(BindDeathTest, NullCallback) {
1178 base::Callback<void(int)> null_cb;
1179 ASSERT_TRUE(null_cb.is_null());
gab73fdcc32016-08-05 12:25:40 +09001180 EXPECT_DCHECK_DEATH(base::Bind(null_cb, 42));
hashimoto@chromium.orgbc14c572012-11-20 17:28:14 +09001181}
1182
ajwong@chromium.orge2cca632011-02-15 10:27:38 +09001183} // namespace
1184} // namespace base