blob: ba0358e5699cba33fdb202551e4f1204d95042c5 [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>
dcheng53b4cea2016-02-02 04:09:339#include <vector>
dcheng69f2a042015-12-14 20:31:5210
[email protected]b38d3572011-02-15 01:27:3811#include "base/callback.h"
avi9b6f42932015-12-26 22:15:1412#include "base/macros.h"
dcheng093de9b2016-04-04 21:25:5113#include "base/memory/ptr_util.h"
[email protected]206a2ae82011-12-22 21:12:5814#include "base/memory/ref_counted.h"
[email protected]206a2ae82011-12-22 21:12:5815#include "base/memory/weak_ptr.h"
gabc964a852016-08-01 16:39:5616#include "base/test/gtest_util.h"
avi9b6f42932015-12-26 22:15:1417#include "build/build_config.h"
[email protected]b38d3572011-02-15 01:27:3818#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
[email protected]8217d4542011-10-01 06:31:4128class IncompleteType;
29
[email protected]b38d3572011-02-15 01:27:3830class NoRef {
31 public:
32 NoRef() {}
33
tzik3bc7779b2015-12-19 09:18:4634 MOCK_METHOD0(VoidMethod0, void());
35 MOCK_CONST_METHOD0(VoidConstMethod0, void());
[email protected]b38d3572011-02-15 01:27:3836
tzik3bc7779b2015-12-19 09:18:4637 MOCK_METHOD0(IntMethod0, int());
38 MOCK_CONST_METHOD0(IntConstMethod0, int());
[email protected]b38d3572011-02-15 01:27:3839
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
tzik3bc7779b2015-12-19 09:18:4649 MOCK_CONST_METHOD0(AddRef, void());
50 MOCK_CONST_METHOD0(Release, bool());
[email protected]b38d3572011-02-15 01:27:3851
52 private:
53 // Particularly important in this test to ensure no copies are made.
54 DISALLOW_COPY_AND_ASSIGN(HasRef);
55};
56
[email protected]690bda882011-04-13 22:40:4657class HasRefPrivateDtor : public HasRef {
58 private:
59 ~HasRefPrivateDtor() {}
60};
61
[email protected]b38d3572011-02-15 01:27:3862static const int kParentValue = 1;
63static const int kChildValue = 2;
64
65class Parent {
66 public:
tzik3bc7779b2015-12-19 09:18:4667 void AddRef() const {}
68 void Release() const {}
[email protected]b38d3572011-02-15 01:27:3869 virtual void VirtualSet() { value = kParentValue; }
70 void NonVirtualSet() { value = kParentValue; }
71 int value;
72};
73
74class Child : public Parent {
75 public:
dcheng56488182014-10-21 10:54:5176 void VirtualSet() override { value = kChildValue; }
[email protected]b38d3572011-02-15 01:27:3877 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 {
dcheng56488182014-10-21 10:54:5188 void VirtualSet() override { value = kChildValue; }
[email protected]b38d3572011-02-15 01:27:3889 void NonVirtualSet() { value = kChildValue; }
90};
91
tzik52dcd672016-02-15 11:54:3092// 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) {}
[email protected]b38d3572011-02-15 01:27:38103 int* copies_;
104 int* assigns_;
tzik52dcd672016-02-15 11:54:30105 int* move_constructs_;
106 int* move_assigns_;
[email protected]b38d3572011-02-15 01:27:38107};
108
tzik52dcd672016-02-15 11:54:30109// Used for probing the number of copies and moves in an argument.
110class CopyMoveCounter {
[email protected]b38d3572011-02-15 01:27:38111 public:
tzik52dcd672016-02-15 11:54:30112 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_)++;
[email protected]b38d3572011-02-15 01:27:38127 }
128
tzik52dcd672016-02-15 11:54:30129 CopyMoveCounter(CopyMoveCounter&& other)
[email protected]b38d3572011-02-15 01:27:38130 : copies_(other.copies_),
tzik52dcd672016-02-15 11:54:30131 assigns_(other.assigns_),
132 move_constructs_(other.move_constructs_),
133 move_assigns_(other.move_assigns_) {
134 (*move_constructs_)++;
[email protected]b38d3572011-02-15 01:27:38135 }
136
[email protected]206a2ae82011-12-22 21:12:58137 // Probing for copies from coercion.
tzik52dcd672016-02-15 11:54:30138 explicit CopyMoveCounter(const DerivedCopyMoveCounter& other)
[email protected]b38d3572011-02-15 01:27:38139 : copies_(other.copies_),
tzik52dcd672016-02-15 11:54:30140 assigns_(other.assigns_),
141 move_constructs_(other.move_constructs_),
142 move_assigns_(other.move_assigns_) {
[email protected]b38d3572011-02-15 01:27:38143 (*copies_)++;
144 }
145
tzik52dcd672016-02-15 11:54:30146 // 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) {
[email protected]b38d3572011-02-15 01:27:38156 copies_ = rhs.copies_;
157 assigns_ = rhs.assigns_;
tzik52dcd672016-02-15 11:54:30158 move_constructs_ = rhs.move_constructs_;
159 move_assigns_ = rhs.move_assigns_;
[email protected]b38d3572011-02-15 01:27:38160
tzik52dcd672016-02-15 11:54:30161 (*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_)++;
[email protected]b38d3572011-02-15 01:27:38173
174 return *this;
175 }
176
177 int copies() const {
178 return *copies_;
179 }
180
[email protected]b38d3572011-02-15 01:27:38181 private:
182 int* copies_;
183 int* assigns_;
tzik52dcd672016-02-15 11:54:30184 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_;
[email protected]b38d3572011-02-15 01:27:38225};
226
[email protected]08aa4552011-10-15 00:34:42227class 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
[email protected]206a2ae82011-12-22 21:12:58243template <typename T>
244T PassThru(T scoper) {
dcheng69f2a042015-12-14 20:31:52245 return scoper;
[email protected]206a2ae82011-12-22 21:12:58246}
247
[email protected]b38d3572011-02-15 01:27:38248// Some test functions that we can Bind to.
249template <typename T>
250T PolymorphicIdentity(T t) {
251 return t;
252}
253
tzik7fe3a682015-12-18 02:23:26254template <typename... Ts>
255struct VoidPolymorphic {
256 static void Run(Ts... t) {}
257};
[email protected]b38d3572011-02-15 01:27:38258
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
tzik52dcd672016-02-15 11:54:30275int GetCopies(const CopyMoveCounter& counter) {
[email protected]b38d3572011-02-15 01:27:38276 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
[email protected]c18b1052011-03-24 02:02:17291void RefArgSet(int &n) {
292 n = 2;
293}
294
[email protected]e24f8762011-12-20 00:10:04295void PtrArgSet(int *n) {
296 *n = 2;
297}
298
[email protected]93540582011-05-16 22:35:14299int FunctionWithWeakFirstParam(WeakPtr<NoRef> o, int n) {
300 return n;
301}
302
[email protected]edd2f1b2013-06-22 20:32:50303int FunctionWithScopedRefptrFirstParam(const scoped_refptr<HasRef>& o, int n) {
304 return n;
305}
306
[email protected]e24f8762011-12-20 00:10:04307void TakesACallback(const Closure& callback) {
308 callback.Run();
309}
310
[email protected]b38d3572011-02-15 01:27:38311class 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
tzik3bc7779b2015-12-19 09:18:46322 static void VoidFunc0() {
[email protected]b38d3572011-02-15 01:27:38323 static_func_mock_ptr->VoidMethod0();
324 }
325
tzik3bc7779b2015-12-19 09:18:46326 static int IntFunc0() { return static_func_mock_ptr->IntMethod0(); }
[email protected]b38d3572011-02-15 01:27:38327
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
tzik4d4da502016-08-23 04:23:49344TEST_F(BindTest, BasicTest) {
345 Callback<int(int, int, int)> cb = Bind(&Sum, 32, 16, 8);
346 EXPECT_EQ(92, cb.Run(13, 12, 11));
[email protected]b38d3572011-02-15 01:27:38347
tzik4d4da502016-08-23 04:23:49348 Callback<int(int, int, int, int, int, int)> c1 = Bind(&Sum);
349 EXPECT_EQ(69, c1.Run(14, 13, 12, 11, 10, 9));
[email protected]b38d3572011-02-15 01:27:38350
tzik4d4da502016-08-23 04:23:49351 Callback<int(int, int, int)> c2 = Bind(c1, 32, 16, 8);
352 EXPECT_EQ(86, c2.Run(11, 10, 9));
[email protected]b38d3572011-02-15 01:27:38353
tzik4d4da502016-08-23 04:23:49354 Callback<int()> c3 = Bind(c2, 4, 2, 1);
355 EXPECT_EQ(63, c3.Run());
[email protected]cea20fe42011-09-30 09:09:34356}
357
[email protected]e24f8762011-12-20 00:10:04358// 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
[email protected]b38d3572011-02-15 01:27:38377// Function type support.
378// - Normal function.
[email protected]81814bce2011-09-10 03:03:00379// - Normal function bound with non-refcounted first argument.
[email protected]b38d3572011-02-15 01:27:38380// - Method bound to non-const object.
[email protected]7a15d1172011-10-07 00:25:29381// - Method bound to scoped_refptr.
[email protected]b38d3572011-02-15 01:27:38382// - 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());
tzik9ca302192016-02-11 10:24:45389 EXPECT_CALL(has_ref_, AddRef()).Times(4);
390 EXPECT_CALL(has_ref_, Release()).Times(4);
[email protected]7a15d1172011-10-07 00:25:29391 EXPECT_CALL(has_ref_, VoidMethod0()).Times(2);
[email protected]b38d3572011-02-15 01:27:38392 EXPECT_CALL(has_ref_, VoidConstMethod0()).Times(2);
393
394 Closure normal_cb = Bind(&VoidFunc0);
tzik3bc7779b2015-12-19 09:18:46395 Callback<NoRef*()> normal_non_refcounted_cb =
[email protected]81814bce2011-09-10 03:03:00396 Bind(&PolymorphicIdentity<NoRef*>, &no_ref_);
397 normal_cb.Run();
398 EXPECT_EQ(&no_ref_, normal_non_refcounted_cb.Run());
399
[email protected]b38d3572011-02-15 01:27:38400 Closure method_cb = Bind(&HasRef::VoidMethod0, &has_ref_);
[email protected]7a15d1172011-10-07 00:25:29401 Closure method_refptr_cb = Bind(&HasRef::VoidMethod0,
402 make_scoped_refptr(&has_ref_));
[email protected]b38d3572011-02-15 01:27:38403 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_);
[email protected]b38d3572011-02-15 01:27:38407 method_cb.Run();
[email protected]7a15d1172011-10-07 00:25:29408 method_refptr_cb.Run();
[email protected]b38d3572011-02-15 01:27:38409 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
tzik3bc7779b2015-12-19 09:18:46437 Callback<int()> normal_cb = Bind(&IntFunc0);
438 Callback<int()> method_cb = Bind(&HasRef::IntMethod0, &has_ref_);
439 Callback<int()> const_method_nonconst_obj_cb =
[email protected]b38d3572011-02-15 01:27:38440 Bind(&HasRef::IntConstMethod0, &has_ref_);
tzik3bc7779b2015-12-19 09:18:46441 Callback<int()> const_method_const_obj_cb =
[email protected]b38d3572011-02-15 01:27:38442 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
[email protected]7296f2762011-11-21 19:23:44449// 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) {
[email protected]e8bfc31d2011-09-28 00:26:37456 EXPECT_CALL(static_func_mock_, IntMethod0()).WillOnce(Return(1337));
[email protected]7296f2762011-11-21 19:23:44457 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();
[email protected]e8bfc31d2011-09-28 00:26:37489}
490
[email protected]b38d3572011-02-15 01:27:38491// 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.
[email protected]8217d4542011-10-01 06:31:41498// - Argument binding to pointer to incomplete type.
[email protected]b38d3572011-02-15 01:27:38499// - Argument gets type converted.
500// - Pointer argument gets converted.
501// - Const Reference forces conversion.
502TEST_F(BindTest, ArgumentBinding) {
503 int n = 2;
504
tzik3bc7779b2015-12-19 09:18:46505 Callback<int()> bind_primitive_cb = Bind(&Identity, n);
[email protected]b38d3572011-02-15 01:27:38506 EXPECT_EQ(n, bind_primitive_cb.Run());
507
tzik3bc7779b2015-12-19 09:18:46508 Callback<int*()> bind_primitive_pointer_cb =
[email protected]b38d3572011-02-15 01:27:38509 Bind(&PolymorphicIdentity<int*>, &n);
510 EXPECT_EQ(&n, bind_primitive_pointer_cb.Run());
511
tzik3bc7779b2015-12-19 09:18:46512 Callback<int()> bind_int_literal_cb = Bind(&Identity, 3);
[email protected]b38d3572011-02-15 01:27:38513 EXPECT_EQ(3, bind_int_literal_cb.Run());
514
tzik3bc7779b2015-12-19 09:18:46515 Callback<const char*()> bind_string_literal_cb =
[email protected]b38d3572011-02-15 01:27:38516 Bind(&CStringIdentity, "hi");
517 EXPECT_STREQ("hi", bind_string_literal_cb.Run());
518
tzik3bc7779b2015-12-19 09:18:46519 Callback<int()> bind_template_function_cb =
[email protected]b38d3572011-02-15 01:27:38520 Bind(&PolymorphicIdentity<int>, 4);
521 EXPECT_EQ(4, bind_template_function_cb.Run());
522
523 NoRefParent p;
524 p.value = 5;
tzik3bc7779b2015-12-19 09:18:46525 Callback<int()> bind_object_cb = Bind(&UnwrapNoRefParent, p);
[email protected]b38d3572011-02-15 01:27:38526 EXPECT_EQ(5, bind_object_cb.Run());
527
[email protected]8217d4542011-10-01 06:31:41528 IncompleteType* incomplete_ptr = reinterpret_cast<IncompleteType*>(123);
tzik3bc7779b2015-12-19 09:18:46529 Callback<IncompleteType*()> bind_incomplete_ptr_cb =
[email protected]8217d4542011-10-01 06:31:41530 Bind(&PolymorphicIdentity<IncompleteType*>, incomplete_ptr);
531 EXPECT_EQ(incomplete_ptr, bind_incomplete_ptr_cb.Run());
532
[email protected]b38d3572011-02-15 01:27:38533 NoRefChild c;
534 c.value = 6;
tzik3bc7779b2015-12-19 09:18:46535 Callback<int()> bind_promotes_cb = Bind(&UnwrapNoRefParent, c);
[email protected]b38d3572011-02-15 01:27:38536 EXPECT_EQ(6, bind_promotes_cb.Run());
537
538 c.value = 7;
tzik3bc7779b2015-12-19 09:18:46539 Callback<int()> bind_pointer_promotes_cb =
[email protected]b38d3572011-02-15 01:27:38540 Bind(&UnwrapNoRefParentPtr, &c);
541 EXPECT_EQ(7, bind_pointer_promotes_cb.Run());
542
543 c.value = 8;
tzik3bc7779b2015-12-19 09:18:46544 Callback<int()> bind_const_reference_promotes_cb =
[email protected]b38d3572011-02-15 01:27:38545 Bind(&UnwrapNoRefParentConstRef, c);
546 EXPECT_EQ(8, bind_const_reference_promotes_cb.Run());
547}
548
[email protected]c18b1052011-03-24 02:02:17549// 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) {
tzik7fe3a682015-12-18 02:23:26558 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);
[email protected]c18b1052011-03-24 02:02:17561 Callback<void(const int&)> unbound_const_ref_cb =
tzik7fe3a682015-12-18 02:23:26562 Bind(&VoidPolymorphic<const int&>::Run);
[email protected]c18b1052011-03-24 02:02:17563 Callback<void(int[])> unbound_unsized_array_cb =
tzik7fe3a682015-12-18 02:23:26564 Bind(&VoidPolymorphic<int[]>::Run);
[email protected]c18b1052011-03-24 02:02:17565 Callback<void(int[2])> unbound_sized_array_cb =
tzik7fe3a682015-12-18 02:23:26566 Bind(&VoidPolymorphic<int[2]>::Run);
[email protected]c18b1052011-03-24 02:02:17567 Callback<void(int[][2])> unbound_array_of_arrays_cb =
tzik7fe3a682015-12-18 02:23:26568 Bind(&VoidPolymorphic<int[][2]>::Run);
569
570 Callback<void(int&)> unbound_ref_with_bound_arg =
571 Bind(&VoidPolymorphic<int, int&>::Run, 1);
[email protected]c18b1052011-03-24 02:02:17572}
573
574// Function with unbound reference parameter.
[email protected]7296f2762011-11-21 19:23:44575// - Original parameter is modified by callback.
[email protected]c18b1052011-03-24 02:02:17576TEST_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
[email protected]b38d3572011-02-15 01:27:38583// 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
tzik3bc7779b2015-12-19 09:18:46591 Callback<int()> ref_copies_cb = Bind(&Identity, ref_n);
[email protected]b38d3572011-02-15 01:27:38592 EXPECT_EQ(n, ref_copies_cb.Run());
593 n++;
594 EXPECT_EQ(n - 1, ref_copies_cb.Run());
595
tzik3bc7779b2015-12-19 09:18:46596 Callback<int()> const_ref_copies_cb = Bind(&Identity, const_ref_n);
[email protected]b38d3572011-02-15 01:27:38597 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
tzik3bc7779b2015-12-19 09:18:46609 Callback<int()> array_cb = Bind(&ArrayGet, array, 1);
[email protected]b38d3572011-02-15 01:27:38610 EXPECT_EQ(1, array_cb.Run());
611
tzik3bc7779b2015-12-19 09:18:46612 Callback<int()> const_array_cb = Bind(&ArrayGet, *const_array_ptr, 1);
[email protected]b38d3572011-02-15 01:27:38613 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
[email protected]b38d3572011-02-15 01:27:38620// Unretained() wrapper support.
[email protected]93540582011-05-16 22:35:14621// - Method bound to Unretained() non-const object.
[email protected]b38d3572011-02-15 01:27:38622// - 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
tzik3bc7779b2015-12-19 09:18:46628 Callback<void()> method_cb =
[email protected]b38d3572011-02-15 01:27:38629 Bind(&NoRef::VoidMethod0, Unretained(&no_ref_));
630 method_cb.Run();
631
tzik3bc7779b2015-12-19 09:18:46632 Callback<void()> const_method_cb =
[email protected]b38d3572011-02-15 01:27:38633 Bind(&NoRef::VoidConstMethod0, Unretained(&no_ref_));
634 const_method_cb.Run();
635
tzik3bc7779b2015-12-19 09:18:46636 Callback<void()> const_method_const_ptr_cb =
[email protected]b38d3572011-02-15 01:27:38637 Bind(&NoRef::VoidConstMethod0, Unretained(const_no_ref_ptr_));
638 const_method_const_ptr_cb.Run();
639}
640
[email protected]93540582011-05-16 22:35:14641// 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
[email protected]7296f2762011-11-21 19:23:44654 Closure method_cb =
[email protected]93540582011-05-16 22:35:14655 Bind(&NoRef::VoidMethod0, weak_factory.GetWeakPtr());
656 method_cb.Run();
657
[email protected]7296f2762011-11-21 19:23:44658 Closure const_method_cb =
[email protected]93540582011-05-16 22:35:14659 Bind(&NoRef::VoidConstMethod0, const_weak_factory.GetWeakPtr());
660 const_method_cb.Run();
661
[email protected]7296f2762011-11-21 19:23:44662 Closure const_method_const_ptr_cb =
[email protected]93540582011-05-16 22:35:14663 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
[email protected]b38d3572011-02-15 01:27:38681// 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
tzik3bc7779b2015-12-19 09:18:46688 Callback<int()> copy_cb = Bind(&Identity, n);
689 Callback<int()> const_ref_cb = Bind(&Identity, ConstRef(n));
[email protected]b38d3572011-02-15 01:27:38690 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;
tzik52dcd672016-02-15 11:54:30698 int move_constructs = 0;
699 int move_assigns = 0;
700 CopyMoveCounter counter(&copies, &assigns, &move_constructs, &move_assigns);
tzik3bc7779b2015-12-19 09:18:46701 Callback<int()> all_const_ref_cb =
[email protected]b38d3572011-02-15 01:27:38702 Bind(&GetCopies, ConstRef(counter));
703 EXPECT_EQ(0, all_const_ref_cb.Run());
704 EXPECT_EQ(0, copies);
705 EXPECT_EQ(0, assigns);
tzik52dcd672016-02-15 11:54:30706 EXPECT_EQ(0, move_constructs);
707 EXPECT_EQ(0, move_assigns);
[email protected]b38d3572011-02-15 01:27:38708}
709
[email protected]edd2f1b2013-06-22 20:32:50710TEST_F(BindTest, ScopedRefptr) {
tzik1fcff5c2016-05-12 04:09:05711 EXPECT_CALL(has_ref_, AddRef()).Times(1);
712 EXPECT_CALL(has_ref_, Release()).Times(1);
[email protected]edd2f1b2013-06-22 20:32:50713
tzik1fcff5c2016-05-12 04:09:05714 const scoped_refptr<HasRef> refptr(&has_ref_);
tzik3bc7779b2015-12-19 09:18:46715 Callback<int()> scoped_refptr_const_ref_cb =
[email protected]edd2f1b2013-06-22 20:32:50716 Bind(&FunctionWithScopedRefptrFirstParam, base::ConstRef(refptr), 1);
717 EXPECT_EQ(1, scoped_refptr_const_ref_cb.Run());
718}
719
[email protected]08aa4552011-10-15 00:34:42720// 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.
tzik3bc7779b2015-12-19 09:18:46727 Callback<DeleteCounter*()> no_capture_cb =
[email protected]08aa4552011-10-15 00:34:42728 Bind(&PolymorphicIdentity<DeleteCounter*>, Owned(counter));
[email protected]206a2ae82011-12-22 21:12:58729 ASSERT_EQ(counter, no_capture_cb.Run());
730 ASSERT_EQ(counter, no_capture_cb.Run());
[email protected]08aa4552011-10-15 00:34:42731 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
tzik4435e8042016-05-11 23:05:05745TEST_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
dchengf10b7732016-01-21 19:37:55751// Tests for Passed() wrapper support:
[email protected]206a2ae82011-12-22 21:12:58752// - 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.
dchengf10b7732016-01-21 19:37:55757template <typename T>
758class BindMoveOnlyTypeTest : public ::testing::Test {
759};
760
761struct CustomDeleter {
762 void operator()(DeleteCounter* c) { delete c; }
763};
764
765using MoveOnlyTypesToTest =
dcheng093de9b2016-04-04 21:25:51766 ::testing::Types<std::unique_ptr<DeleteCounter>,
dchengf10b7732016-01-21 19:37:55767 std::unique_ptr<DeleteCounter, CustomDeleter>>;
768TYPED_TEST_CASE(BindMoveOnlyTypeTest, MoveOnlyTypesToTest);
769
770TYPED_TEST(BindMoveOnlyTypeTest, PassedToBoundCallback) {
[email protected]206a2ae82011-12-22 21:12:58771 int deletes = 0;
772
dchengf10b7732016-01-21 19:37:55773 TypeParam ptr(new DeleteCounter(&deletes));
774 Callback<TypeParam()> callback = Bind(&PassThru<TypeParam>, Passed(&ptr));
[email protected]206a2ae82011-12-22 21:12:58775 EXPECT_FALSE(ptr.get());
776 EXPECT_EQ(0, deletes);
777
778 // If we never invoke the Callback, it retains ownership and deletes.
[email protected]206a2ae82011-12-22 21:12:58779 callback.Reset();
[email protected]206a2ae82011-12-22 21:12:58780 EXPECT_EQ(1, deletes);
[email protected]206a2ae82011-12-22 21:12:58781}
782
dchengf10b7732016-01-21 19:37:55783TYPED_TEST(BindMoveOnlyTypeTest, PassedWithRvalue) {
dcheng69f2a042015-12-14 20:31:52784 int deletes = 0;
dchengf10b7732016-01-21 19:37:55785 Callback<TypeParam()> callback = Bind(
786 &PassThru<TypeParam>, Passed(TypeParam(new DeleteCounter(&deletes))));
dcheng69f2a042015-12-14 20:31:52787 EXPECT_EQ(0, deletes);
788
789 // If we never invoke the Callback, it retains ownership and deletes.
dchengf10b7732016-01-21 19:37:55790 callback.Reset();
dcheng69f2a042015-12-14 20:31:52791 EXPECT_EQ(1, deletes);
dchengf10b7732016-01-21 19:37:55792}
dcheng69f2a042015-12-14 20:31:52793
dchengf10b7732016-01-21 19:37:55794// Check that ownership can be transferred back out.
795TYPED_TEST(BindMoveOnlyTypeTest, ReturnMoveOnlyType) {
796 int deletes = 0;
dcheng69f2a042015-12-14 20:31:52797 DeleteCounter* counter = new DeleteCounter(&deletes);
dchengf10b7732016-01-21 19:37:55798 Callback<TypeParam()> callback =
799 Bind(&PassThru<TypeParam>, Passed(TypeParam(counter)));
800 TypeParam result = callback.Run();
dcheng69f2a042015-12-14 20:31:52801 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);
dchengf10b7732016-01-21 19:37:55811}
dcheng69f2a042015-12-14 20:31:52812
dchengf10b7732016-01-21 19:37:55813TYPED_TEST(BindMoveOnlyTypeTest, UnboundForwarding) {
814 int deletes = 0;
815 TypeParam ptr(new DeleteCounter(&deletes));
dcheng69f2a042015-12-14 20:31:52816 // Test unbound argument forwarding.
dchengf10b7732016-01-21 19:37:55817 Callback<TypeParam(TypeParam)> cb_unbound = Bind(&PassThru<TypeParam>);
dcheng69f2a042015-12-14 20:31:52818 cb_unbound.Run(std::move(ptr));
dchengf10b7732016-01-21 19:37:55819 EXPECT_EQ(1, deletes);
dcheng69f2a042015-12-14 20:31:52820}
821
dcheng093de9b2016-04-04 21:25:51822void VerifyVector(const std::vector<std::unique_ptr<int>>& v) {
dcheng53b4cea2016-02-02 04:09:33823 ASSERT_EQ(1u, v.size());
824 EXPECT_EQ(12345, *v[0]);
825}
826
dcheng093de9b2016-04-04 21:25:51827std::vector<std::unique_ptr<int>> AcceptAndReturnMoveOnlyVector(
828 std::vector<std::unique_ptr<int>> v) {
dcheng53b4cea2016-02-02 04:09:33829 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) {
dcheng093de9b2016-04-04 21:25:51835 using MoveOnlyVector = std::vector<std::unique_ptr<int>>;
dcheng53b4cea2016-02-02 04:09:33836
837 MoveOnlyVector v;
dcheng093de9b2016-04-04 21:25:51838 v.push_back(WrapUnique(new int(12345)));
dcheng53b4cea2016-02-02 04:09:33839
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
tzik52dcd672016-02-15 11:54:30853// Argument copy-constructor usage for non-reference copy-only parameters.
[email protected]b38d3572011-02-15 01:27:38854// - Bound arguments are only copied once.
855// - Forwarded arguments are only copied once.
[email protected]206a2ae82011-12-22 21:12:58856// - Forwarded arguments with coercions are only copied twice (once for the
857// coercion, and one for the final dispatch).
[email protected]b38d3572011-02-15 01:27:38858TEST_F(BindTest, ArgumentCopies) {
859 int copies = 0;
860 int assigns = 0;
861
862 CopyCounter counter(&copies, &assigns);
tzik52dcd672016-02-15 11:54:30863 Bind(&VoidPolymorphic<CopyCounter>::Run, counter);
864 EXPECT_EQ(1, copies);
[email protected]b38d3572011-02-15 01:27:38865 EXPECT_EQ(0, assigns);
866
867 copies = 0;
868 assigns = 0;
tzik52dcd672016-02-15 11:54:30869 Bind(&VoidPolymorphic<CopyCounter>::Run, CopyCounter(&copies, &assigns));
870 EXPECT_EQ(1, copies);
[email protected]b38d3572011-02-15 01:27:38871 EXPECT_EQ(0, assigns);
872
873 copies = 0;
874 assigns = 0;
tzik52dcd672016-02-15 11:54:30875 Bind(&VoidPolymorphic<CopyCounter>::Run).Run(counter);
tzika43eff02016-03-09 05:46:05876 EXPECT_EQ(2, copies);
[email protected]b38d3572011-02-15 01:27:38877 EXPECT_EQ(0, assigns);
tzik52dcd672016-02-15 11:54:30878
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
tzika43eff02016-03-09 05:46:05915 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);
tzik52dcd672016-02-15 11:54:30929}
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);
tzika43eff02016-03-09 05:46:05967 EXPECT_EQ(1, move_constructs);
tzik52dcd672016-02-15 11:54:30968 EXPECT_EQ(0, move_assigns);
969
tzik52dcd672016-02-15 11:54:30970 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));
tzika43eff02016-03-09 05:46:05976 EXPECT_EQ(0, copies);
tzik52dcd672016-02-15 11:54:30977 EXPECT_EQ(0, assigns);
tzika43eff02016-03-09 05:46:05978 EXPECT_EQ(1, move_constructs);
tzik52dcd672016-02-15 11:54:30979 EXPECT_EQ(0, move_assigns);
980
tzik52dcd672016-02-15 11:54:30981 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));
tzika43eff02016-03-09 05:46:05989 EXPECT_EQ(1, copies);
tzik52dcd672016-02-15 11:54:30990 EXPECT_EQ(0, assigns);
tzika43eff02016-03-09 05:46:05991 EXPECT_EQ(1, move_constructs);
tzik52dcd672016-02-15 11:54:30992 EXPECT_EQ(0, move_assigns);
993
tzik52dcd672016-02-15 11:54:30994 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)));
tzika43eff02016-03-09 05:46:051001 EXPECT_EQ(0, copies);
tzik52dcd672016-02-15 11:54:301002 EXPECT_EQ(0, assigns);
tzika43eff02016-03-09 05:46:051003 EXPECT_EQ(2, move_constructs);
tzik52dcd672016-02-15 11:54:301004 EXPECT_EQ(0, move_assigns);
[email protected]b38d3572011-02-15 01:27:381005}
1006
tzikc1db72652016-07-08 09:42:381007TEST_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
[email protected]b38d3572011-02-15 01:27:381037// Callback construction and assignment tests.
1038// - Construction from an InvokerStorageHolder should not cause ref/deref.
1039// - Assignment from other callback should only cause one ref
1040//
1041// TODO(ajwong): Is there actually a way to test this?
1042
[email protected]054ac7542011-02-27 01:25:591043#if defined(OS_WIN)
1044int __fastcall FastCallFunc(int n) {
1045 return n;
1046}
1047
1048int __stdcall StdCallFunc(int n) {
1049 return n;
1050}
1051
1052// Windows specific calling convention support.
1053// - Can bind a __fastcall function.
1054// - Can bind a __stdcall function.
1055TEST_F(BindTest, WindowsCallingConventions) {
tzik3bc7779b2015-12-19 09:18:461056 Callback<int()> fastcall_cb = Bind(&FastCallFunc, 1);
[email protected]054ac7542011-02-27 01:25:591057 EXPECT_EQ(1, fastcall_cb.Run());
1058
tzik3bc7779b2015-12-19 09:18:461059 Callback<int()> stdcall_cb = Bind(&StdCallFunc, 2);
[email protected]054ac7542011-02-27 01:25:591060 EXPECT_EQ(2, stdcall_cb.Run());
1061}
1062#endif
1063
[email protected]8cf362c2012-11-20 08:28:141064// Test null callbacks cause a DCHECK.
1065TEST(BindDeathTest, NullCallback) {
1066 base::Callback<void(int)> null_cb;
1067 ASSERT_TRUE(null_cb.is_null());
gab5e69cff2016-08-05 03:25:401068 EXPECT_DCHECK_DEATH(base::Bind(null_cb, 42));
[email protected]8cf362c2012-11-20 08:28:141069}
1070
[email protected]b38d3572011-02-15 01:27:381071} // namespace
1072} // namespace base