blob: a6f3653b31474ba6ac42a43211a9fcd216ce356c [file] [log] [blame]
[email protected]44106182012-04-06 03:53:021// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]b38d3572011-02-15 01:27:382// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "base/bind.h"
6
dcheng69f2a042015-12-14 20:31:527#include <memory>
8#include <utility>
9
[email protected]b38d3572011-02-15 01:27:3810#include "base/callback.h"
[email protected]206a2ae82011-12-22 21:12:5811#include "base/memory/ref_counted.h"
12#include "base/memory/scoped_ptr.h"
13#include "base/memory/weak_ptr.h"
[email protected]b38d3572011-02-15 01:27:3814#include "testing/gmock/include/gmock/gmock.h"
15#include "testing/gtest/include/gtest/gtest.h"
16
17using ::testing::Mock;
18using ::testing::Return;
19using ::testing::StrictMock;
20
21namespace base {
22namespace {
23
[email protected]8217d4542011-10-01 06:31:4124class IncompleteType;
25
[email protected]b38d3572011-02-15 01:27:3826class NoRef {
27 public:
28 NoRef() {}
29
30 MOCK_METHOD0(VoidMethod0, void(void));
31 MOCK_CONST_METHOD0(VoidConstMethod0, void(void));
32
33 MOCK_METHOD0(IntMethod0, int(void));
34 MOCK_CONST_METHOD0(IntConstMethod0, int(void));
35
36 private:
37 // Particularly important in this test to ensure no copies are made.
38 DISALLOW_COPY_AND_ASSIGN(NoRef);
39};
40
41class HasRef : public NoRef {
42 public:
43 HasRef() {}
44
45 MOCK_CONST_METHOD0(AddRef, void(void));
46 MOCK_CONST_METHOD0(Release, bool(void));
47
48 private:
49 // Particularly important in this test to ensure no copies are made.
50 DISALLOW_COPY_AND_ASSIGN(HasRef);
51};
52
[email protected]690bda882011-04-13 22:40:4653class HasRefPrivateDtor : public HasRef {
54 private:
55 ~HasRefPrivateDtor() {}
56};
57
[email protected]b38d3572011-02-15 01:27:3858static const int kParentValue = 1;
59static const int kChildValue = 2;
60
61class Parent {
62 public:
63 void AddRef(void) const {}
64 void Release(void) const {}
65 virtual void VirtualSet() { value = kParentValue; }
66 void NonVirtualSet() { value = kParentValue; }
67 int value;
68};
69
70class Child : public Parent {
71 public:
dcheng56488182014-10-21 10:54:5172 void VirtualSet() override { value = kChildValue; }
[email protected]b38d3572011-02-15 01:27:3873 void NonVirtualSet() { value = kChildValue; }
74};
75
76class NoRefParent {
77 public:
78 virtual void VirtualSet() { value = kParentValue; }
79 void NonVirtualSet() { value = kParentValue; }
80 int value;
81};
82
83class NoRefChild : public NoRefParent {
dcheng56488182014-10-21 10:54:5184 void VirtualSet() override { value = kChildValue; }
[email protected]b38d3572011-02-15 01:27:3885 void NonVirtualSet() { value = kChildValue; }
86};
87
88// Used for probing the number of copies that occur if a type must be coerced
89// during argument forwarding in the Run() methods.
90struct DerivedCopyCounter {
91 DerivedCopyCounter(int* copies, int* assigns)
92 : copies_(copies), assigns_(assigns) {
93 }
94 int* copies_;
95 int* assigns_;
96};
97
98// Used for probing the number of copies in an argument.
99class CopyCounter {
100 public:
101 CopyCounter(int* copies, int* assigns)
102 : copies_(copies), assigns_(assigns) {
103 }
104
105 CopyCounter(const CopyCounter& other)
106 : copies_(other.copies_),
107 assigns_(other.assigns_) {
108 (*copies_)++;
109 }
110
[email protected]206a2ae82011-12-22 21:12:58111 // Probing for copies from coercion.
[email protected]f3c697c52013-01-15 10:52:11112 explicit CopyCounter(const DerivedCopyCounter& other)
[email protected]b38d3572011-02-15 01:27:38113 : copies_(other.copies_),
114 assigns_(other.assigns_) {
115 (*copies_)++;
116 }
117
118 const CopyCounter& operator=(const CopyCounter& rhs) {
119 copies_ = rhs.copies_;
120 assigns_ = rhs.assigns_;
121
122 if (assigns_) {
123 (*assigns_)++;
124 }
125
126 return *this;
127 }
128
129 int copies() const {
130 return *copies_;
131 }
132
[email protected]b38d3572011-02-15 01:27:38133 private:
134 int* copies_;
135 int* assigns_;
136};
137
[email protected]08aa4552011-10-15 00:34:42138class DeleteCounter {
139 public:
140 explicit DeleteCounter(int* deletes)
141 : deletes_(deletes) {
142 }
143
144 ~DeleteCounter() {
145 (*deletes_)++;
146 }
147
148 void VoidMethod0() {}
149
150 private:
151 int* deletes_;
152};
153
[email protected]206a2ae82011-12-22 21:12:58154template <typename T>
155T PassThru(T scoper) {
dcheng69f2a042015-12-14 20:31:52156 return scoper;
[email protected]206a2ae82011-12-22 21:12:58157}
158
[email protected]b38d3572011-02-15 01:27:38159// Some test functions that we can Bind to.
160template <typename T>
161T PolymorphicIdentity(T t) {
162 return t;
163}
164
165template <typename T>
166void VoidPolymorphic1(T t) {
167}
168
169int Identity(int n) {
170 return n;
171}
172
173int ArrayGet(const int array[], int n) {
174 return array[n];
175}
176
177int Sum(int a, int b, int c, int d, int e, int f) {
178 return a + b + c + d + e + f;
179}
180
181const char* CStringIdentity(const char* s) {
182 return s;
183}
184
185int GetCopies(const CopyCounter& counter) {
186 return counter.copies();
187}
188
189int UnwrapNoRefParent(NoRefParent p) {
190 return p.value;
191}
192
193int UnwrapNoRefParentPtr(NoRefParent* p) {
194 return p->value;
195}
196
197int UnwrapNoRefParentConstRef(const NoRefParent& p) {
198 return p.value;
199}
200
[email protected]c18b1052011-03-24 02:02:17201void RefArgSet(int &n) {
202 n = 2;
203}
204
[email protected]e24f8762011-12-20 00:10:04205void PtrArgSet(int *n) {
206 *n = 2;
207}
208
[email protected]93540582011-05-16 22:35:14209int FunctionWithWeakFirstParam(WeakPtr<NoRef> o, int n) {
210 return n;
211}
212
[email protected]edd2f1b2013-06-22 20:32:50213int FunctionWithScopedRefptrFirstParam(const scoped_refptr<HasRef>& o, int n) {
214 return n;
215}
216
[email protected]e24f8762011-12-20 00:10:04217void TakesACallback(const Closure& callback) {
218 callback.Run();
219}
220
[email protected]b38d3572011-02-15 01:27:38221class BindTest : public ::testing::Test {
222 public:
223 BindTest() {
224 const_has_ref_ptr_ = &has_ref_;
225 const_no_ref_ptr_ = &no_ref_;
226 static_func_mock_ptr = &static_func_mock_;
227 }
228
229 virtual ~BindTest() {
230 }
231
232 static void VoidFunc0(void) {
233 static_func_mock_ptr->VoidMethod0();
234 }
235
236 static int IntFunc0(void) { return static_func_mock_ptr->IntMethod0(); }
237
238 protected:
239 StrictMock<NoRef> no_ref_;
240 StrictMock<HasRef> has_ref_;
241 const HasRef* const_has_ref_ptr_;
242 const NoRef* const_no_ref_ptr_;
243 StrictMock<NoRef> static_func_mock_;
244
245 // Used by the static functions to perform expectations.
246 static StrictMock<NoRef>* static_func_mock_ptr;
247
248 private:
249 DISALLOW_COPY_AND_ASSIGN(BindTest);
250};
251
252StrictMock<NoRef>* BindTest::static_func_mock_ptr;
253
[email protected]b38d3572011-02-15 01:27:38254// Sanity check that we can instantiate a callback for each arity.
255TEST_F(BindTest, ArityTest) {
256 Callback<int(void)> c0 = Bind(&Sum, 32, 16, 8, 4, 2, 1);
257 EXPECT_EQ(63, c0.Run());
258
259 Callback<int(int)> c1 = Bind(&Sum, 32, 16, 8, 4, 2);
260 EXPECT_EQ(75, c1.Run(13));
261
262 Callback<int(int,int)> c2 = Bind(&Sum, 32, 16, 8, 4);
263 EXPECT_EQ(85, c2.Run(13, 12));
264
265 Callback<int(int,int,int)> c3 = Bind(&Sum, 32, 16, 8);
266 EXPECT_EQ(92, c3.Run(13, 12, 11));
267
268 Callback<int(int,int,int,int)> c4 = Bind(&Sum, 32, 16);
269 EXPECT_EQ(94, c4.Run(13, 12, 11, 10));
270
271 Callback<int(int,int,int,int,int)> c5 = Bind(&Sum, 32);
272 EXPECT_EQ(87, c5.Run(13, 12, 11, 10, 9));
273
274 Callback<int(int,int,int,int,int,int)> c6 = Bind(&Sum);
275 EXPECT_EQ(69, c6.Run(13, 12, 11, 10, 9, 14));
276}
277
[email protected]7296f2762011-11-21 19:23:44278// Test the Currying ability of the Callback system.
279TEST_F(BindTest, CurryingTest) {
280 Callback<int(int,int,int,int,int,int)> c6 = Bind(&Sum);
281 EXPECT_EQ(69, c6.Run(13, 12, 11, 10, 9, 14));
[email protected]cea20fe42011-09-30 09:09:34282
[email protected]7296f2762011-11-21 19:23:44283 Callback<int(int,int,int,int,int)> c5 = Bind(c6, 32);
284 EXPECT_EQ(87, c5.Run(13, 12, 11, 10, 9));
[email protected]cea20fe42011-09-30 09:09:34285
[email protected]7296f2762011-11-21 19:23:44286 Callback<int(int,int,int,int)> c4 = Bind(c5, 16);
287 EXPECT_EQ(94, c4.Run(13, 12, 11, 10));
[email protected]cea20fe42011-09-30 09:09:34288
[email protected]7296f2762011-11-21 19:23:44289 Callback<int(int,int,int)> c3 = Bind(c4, 8);
290 EXPECT_EQ(92, c3.Run(13, 12, 11));
[email protected]cea20fe42011-09-30 09:09:34291
[email protected]7296f2762011-11-21 19:23:44292 Callback<int(int,int)> c2 = Bind(c3, 4);
293 EXPECT_EQ(85, c2.Run(13, 12));
[email protected]cea20fe42011-09-30 09:09:34294
[email protected]7296f2762011-11-21 19:23:44295 Callback<int(int)> c1 = Bind(c2, 2);
296 EXPECT_EQ(75, c1.Run(13));
297
298 Callback<int(void)> c0 = Bind(c1, 1);
299 EXPECT_EQ(63, c0.Run());
[email protected]cea20fe42011-09-30 09:09:34300}
301
[email protected]e24f8762011-12-20 00:10:04302// Test that currying the rvalue result of another Bind() works correctly.
303// - rvalue should be usable as argument to Bind().
304// - multiple runs of resulting Callback remain valid.
305TEST_F(BindTest, CurryingRvalueResultOfBind) {
306 int n = 0;
307 Closure cb = base::Bind(&TakesACallback, base::Bind(&PtrArgSet, &n));
308
309 // If we implement Bind() such that the return value has auto_ptr-like
310 // semantics, the second call here will fail because ownership of
311 // the internal BindState<> would have been transfered to a *temporary*
312 // constructon of a Callback object on the first call.
313 cb.Run();
314 EXPECT_EQ(2, n);
315
316 n = 0;
317 cb.Run();
318 EXPECT_EQ(2, n);
319}
320
[email protected]b38d3572011-02-15 01:27:38321// Function type support.
322// - Normal function.
[email protected]81814bce2011-09-10 03:03:00323// - Normal function bound with non-refcounted first argument.
[email protected]b38d3572011-02-15 01:27:38324// - Method bound to non-const object.
[email protected]7a15d1172011-10-07 00:25:29325// - Method bound to scoped_refptr.
[email protected]b38d3572011-02-15 01:27:38326// - Const method bound to non-const object.
327// - Const method bound to const object.
328// - Derived classes can be used with pointers to non-virtual base functions.
329// - Derived classes can be used with pointers to virtual base functions (and
330// preserve virtual dispatch).
331TEST_F(BindTest, FunctionTypeSupport) {
332 EXPECT_CALL(static_func_mock_, VoidMethod0());
[email protected]7a15d1172011-10-07 00:25:29333 EXPECT_CALL(has_ref_, AddRef()).Times(5);
334 EXPECT_CALL(has_ref_, Release()).Times(5);
335 EXPECT_CALL(has_ref_, VoidMethod0()).Times(2);
[email protected]b38d3572011-02-15 01:27:38336 EXPECT_CALL(has_ref_, VoidConstMethod0()).Times(2);
337
338 Closure normal_cb = Bind(&VoidFunc0);
[email protected]81814bce2011-09-10 03:03:00339 Callback<NoRef*(void)> normal_non_refcounted_cb =
340 Bind(&PolymorphicIdentity<NoRef*>, &no_ref_);
341 normal_cb.Run();
342 EXPECT_EQ(&no_ref_, normal_non_refcounted_cb.Run());
343
[email protected]b38d3572011-02-15 01:27:38344 Closure method_cb = Bind(&HasRef::VoidMethod0, &has_ref_);
[email protected]7a15d1172011-10-07 00:25:29345 Closure method_refptr_cb = Bind(&HasRef::VoidMethod0,
346 make_scoped_refptr(&has_ref_));
[email protected]b38d3572011-02-15 01:27:38347 Closure const_method_nonconst_obj_cb = Bind(&HasRef::VoidConstMethod0,
348 &has_ref_);
349 Closure const_method_const_obj_cb = Bind(&HasRef::VoidConstMethod0,
350 const_has_ref_ptr_);
[email protected]b38d3572011-02-15 01:27:38351 method_cb.Run();
[email protected]7a15d1172011-10-07 00:25:29352 method_refptr_cb.Run();
[email protected]b38d3572011-02-15 01:27:38353 const_method_nonconst_obj_cb.Run();
354 const_method_const_obj_cb.Run();
355
356 Child child;
357 child.value = 0;
358 Closure virtual_set_cb = Bind(&Parent::VirtualSet, &child);
359 virtual_set_cb.Run();
360 EXPECT_EQ(kChildValue, child.value);
361
362 child.value = 0;
363 Closure non_virtual_set_cb = Bind(&Parent::NonVirtualSet, &child);
364 non_virtual_set_cb.Run();
365 EXPECT_EQ(kParentValue, child.value);
366}
367
368// Return value support.
369// - Function with return value.
370// - Method with return value.
371// - Const method with return value.
372TEST_F(BindTest, ReturnValues) {
373 EXPECT_CALL(static_func_mock_, IntMethod0()).WillOnce(Return(1337));
374 EXPECT_CALL(has_ref_, AddRef()).Times(3);
375 EXPECT_CALL(has_ref_, Release()).Times(3);
376 EXPECT_CALL(has_ref_, IntMethod0()).WillOnce(Return(31337));
377 EXPECT_CALL(has_ref_, IntConstMethod0())
378 .WillOnce(Return(41337))
379 .WillOnce(Return(51337));
380
381 Callback<int(void)> normal_cb = Bind(&IntFunc0);
382 Callback<int(void)> method_cb = Bind(&HasRef::IntMethod0, &has_ref_);
383 Callback<int(void)> const_method_nonconst_obj_cb =
384 Bind(&HasRef::IntConstMethod0, &has_ref_);
385 Callback<int(void)> const_method_const_obj_cb =
386 Bind(&HasRef::IntConstMethod0, const_has_ref_ptr_);
387 EXPECT_EQ(1337, normal_cb.Run());
388 EXPECT_EQ(31337, method_cb.Run());
389 EXPECT_EQ(41337, const_method_nonconst_obj_cb.Run());
390 EXPECT_EQ(51337, const_method_const_obj_cb.Run());
391}
392
[email protected]7296f2762011-11-21 19:23:44393// IgnoreResult adapter test.
394// - Function with return value.
395// - Method with return value.
396// - Const Method with return.
397// - Method with return value bound to WeakPtr<>.
398// - Const Method with return bound to WeakPtr<>.
399TEST_F(BindTest, IgnoreResult) {
[email protected]e8bfc31d2011-09-28 00:26:37400 EXPECT_CALL(static_func_mock_, IntMethod0()).WillOnce(Return(1337));
[email protected]7296f2762011-11-21 19:23:44401 EXPECT_CALL(has_ref_, AddRef()).Times(2);
402 EXPECT_CALL(has_ref_, Release()).Times(2);
403 EXPECT_CALL(has_ref_, IntMethod0()).WillOnce(Return(10));
404 EXPECT_CALL(has_ref_, IntConstMethod0()).WillOnce(Return(11));
405 EXPECT_CALL(no_ref_, IntMethod0()).WillOnce(Return(12));
406 EXPECT_CALL(no_ref_, IntConstMethod0()).WillOnce(Return(13));
407
408 Closure normal_func_cb = Bind(IgnoreResult(&IntFunc0));
409 normal_func_cb.Run();
410
411 Closure non_void_method_cb =
412 Bind(IgnoreResult(&HasRef::IntMethod0), &has_ref_);
413 non_void_method_cb.Run();
414
415 Closure non_void_const_method_cb =
416 Bind(IgnoreResult(&HasRef::IntConstMethod0), &has_ref_);
417 non_void_const_method_cb.Run();
418
419 WeakPtrFactory<NoRef> weak_factory(&no_ref_);
420 WeakPtrFactory<const NoRef> const_weak_factory(const_no_ref_ptr_);
421
422 Closure non_void_weak_method_cb =
423 Bind(IgnoreResult(&NoRef::IntMethod0), weak_factory.GetWeakPtr());
424 non_void_weak_method_cb.Run();
425
426 Closure non_void_weak_const_method_cb =
427 Bind(IgnoreResult(&NoRef::IntConstMethod0), weak_factory.GetWeakPtr());
428 non_void_weak_const_method_cb.Run();
429
430 weak_factory.InvalidateWeakPtrs();
431 non_void_weak_const_method_cb.Run();
432 non_void_weak_method_cb.Run();
[email protected]e8bfc31d2011-09-28 00:26:37433}
434
[email protected]b38d3572011-02-15 01:27:38435// Argument binding tests.
436// - Argument binding to primitive.
437// - Argument binding to primitive pointer.
438// - Argument binding to a literal integer.
439// - Argument binding to a literal string.
440// - Argument binding with template function.
441// - Argument binding to an object.
[email protected]8217d4542011-10-01 06:31:41442// - Argument binding to pointer to incomplete type.
[email protected]b38d3572011-02-15 01:27:38443// - Argument gets type converted.
444// - Pointer argument gets converted.
445// - Const Reference forces conversion.
446TEST_F(BindTest, ArgumentBinding) {
447 int n = 2;
448
449 Callback<int(void)> bind_primitive_cb = Bind(&Identity, n);
450 EXPECT_EQ(n, bind_primitive_cb.Run());
451
452 Callback<int*(void)> bind_primitive_pointer_cb =
453 Bind(&PolymorphicIdentity<int*>, &n);
454 EXPECT_EQ(&n, bind_primitive_pointer_cb.Run());
455
456 Callback<int(void)> bind_int_literal_cb = Bind(&Identity, 3);
457 EXPECT_EQ(3, bind_int_literal_cb.Run());
458
459 Callback<const char*(void)> bind_string_literal_cb =
460 Bind(&CStringIdentity, "hi");
461 EXPECT_STREQ("hi", bind_string_literal_cb.Run());
462
463 Callback<int(void)> bind_template_function_cb =
464 Bind(&PolymorphicIdentity<int>, 4);
465 EXPECT_EQ(4, bind_template_function_cb.Run());
466
467 NoRefParent p;
468 p.value = 5;
469 Callback<int(void)> bind_object_cb = Bind(&UnwrapNoRefParent, p);
470 EXPECT_EQ(5, bind_object_cb.Run());
471
[email protected]8217d4542011-10-01 06:31:41472 IncompleteType* incomplete_ptr = reinterpret_cast<IncompleteType*>(123);
473 Callback<IncompleteType*(void)> bind_incomplete_ptr_cb =
474 Bind(&PolymorphicIdentity<IncompleteType*>, incomplete_ptr);
475 EXPECT_EQ(incomplete_ptr, bind_incomplete_ptr_cb.Run());
476
[email protected]b38d3572011-02-15 01:27:38477 NoRefChild c;
478 c.value = 6;
479 Callback<int(void)> bind_promotes_cb = Bind(&UnwrapNoRefParent, c);
480 EXPECT_EQ(6, bind_promotes_cb.Run());
481
482 c.value = 7;
483 Callback<int(void)> bind_pointer_promotes_cb =
484 Bind(&UnwrapNoRefParentPtr, &c);
485 EXPECT_EQ(7, bind_pointer_promotes_cb.Run());
486
487 c.value = 8;
488 Callback<int(void)> bind_const_reference_promotes_cb =
489 Bind(&UnwrapNoRefParentConstRef, c);
490 EXPECT_EQ(8, bind_const_reference_promotes_cb.Run());
491}
492
[email protected]c18b1052011-03-24 02:02:17493// Unbound argument type support tests.
494// - Unbound value.
495// - Unbound pointer.
496// - Unbound reference.
497// - Unbound const reference.
498// - Unbound unsized array.
499// - Unbound sized array.
500// - Unbound array-of-arrays.
501TEST_F(BindTest, UnboundArgumentTypeSupport) {
502 Callback<void(int)> unbound_value_cb = Bind(&VoidPolymorphic1<int>);
503 Callback<void(int*)> unbound_pointer_cb = Bind(&VoidPolymorphic1<int*>);
504 Callback<void(int&)> unbound_ref_cb = Bind(&VoidPolymorphic1<int&>);
505 Callback<void(const int&)> unbound_const_ref_cb =
506 Bind(&VoidPolymorphic1<const int&>);
507 Callback<void(int[])> unbound_unsized_array_cb =
508 Bind(&VoidPolymorphic1<int[]>);
509 Callback<void(int[2])> unbound_sized_array_cb =
510 Bind(&VoidPolymorphic1<int[2]>);
511 Callback<void(int[][2])> unbound_array_of_arrays_cb =
512 Bind(&VoidPolymorphic1<int[][2]>);
513}
514
515// Function with unbound reference parameter.
[email protected]7296f2762011-11-21 19:23:44516// - Original parameter is modified by callback.
[email protected]c18b1052011-03-24 02:02:17517TEST_F(BindTest, UnboundReferenceSupport) {
518 int n = 0;
519 Callback<void(int&)> unbound_ref_cb = Bind(&RefArgSet);
520 unbound_ref_cb.Run(n);
521 EXPECT_EQ(2, n);
522}
523
[email protected]b38d3572011-02-15 01:27:38524// Functions that take reference parameters.
525// - Forced reference parameter type still stores a copy.
526// - Forced const reference parameter type still stores a copy.
527TEST_F(BindTest, ReferenceArgumentBinding) {
528 int n = 1;
529 int& ref_n = n;
530 const int& const_ref_n = n;
531
532 Callback<int(void)> ref_copies_cb = Bind(&Identity, ref_n);
533 EXPECT_EQ(n, ref_copies_cb.Run());
534 n++;
535 EXPECT_EQ(n - 1, ref_copies_cb.Run());
536
537 Callback<int(void)> const_ref_copies_cb = Bind(&Identity, const_ref_n);
538 EXPECT_EQ(n, const_ref_copies_cb.Run());
539 n++;
540 EXPECT_EQ(n - 1, const_ref_copies_cb.Run());
541}
542
543// Check that we can pass in arrays and have them be stored as a pointer.
544// - Array of values stores a pointer.
545// - Array of const values stores a pointer.
546TEST_F(BindTest, ArrayArgumentBinding) {
547 int array[4] = {1, 1, 1, 1};
548 const int (*const_array_ptr)[4] = &array;
549
550 Callback<int(void)> array_cb = Bind(&ArrayGet, array, 1);
551 EXPECT_EQ(1, array_cb.Run());
552
553 Callback<int(void)> const_array_cb = Bind(&ArrayGet, *const_array_ptr, 1);
554 EXPECT_EQ(1, const_array_cb.Run());
555
556 array[1] = 3;
557 EXPECT_EQ(3, array_cb.Run());
558 EXPECT_EQ(3, const_array_cb.Run());
559}
560
561// Verify SupportsAddRefAndRelease correctly introspects the class type for
562// AddRef() and Release().
[email protected]690bda882011-04-13 22:40:46563// - Class with AddRef() and Release()
564// - Class without AddRef() and Release()
565// - Derived Class with AddRef() and Release()
566// - Derived Class without AddRef() and Release()
567// - Derived Class with AddRef() and Release() and a private destructor.
[email protected]b38d3572011-02-15 01:27:38568TEST_F(BindTest, SupportsAddRefAndRelease) {
569 EXPECT_TRUE(internal::SupportsAddRefAndRelease<HasRef>::value);
570 EXPECT_FALSE(internal::SupportsAddRefAndRelease<NoRef>::value);
571
572 // StrictMock<T> is a derived class of T. So, we use StrictMock<HasRef> and
573 // StrictMock<NoRef> to test that SupportsAddRefAndRelease works over
574 // inheritance.
575 EXPECT_TRUE(internal::SupportsAddRefAndRelease<StrictMock<HasRef> >::value);
576 EXPECT_FALSE(internal::SupportsAddRefAndRelease<StrictMock<NoRef> >::value);
[email protected]690bda882011-04-13 22:40:46577
578 // This matters because the implementation creates a dummy class that
579 // inherits from the template type.
580 EXPECT_TRUE(internal::SupportsAddRefAndRelease<HasRefPrivateDtor>::value);
[email protected]b38d3572011-02-15 01:27:38581}
582
583// Unretained() wrapper support.
[email protected]93540582011-05-16 22:35:14584// - Method bound to Unretained() non-const object.
[email protected]b38d3572011-02-15 01:27:38585// - Const method bound to Unretained() non-const object.
586// - Const method bound to Unretained() const object.
587TEST_F(BindTest, Unretained) {
588 EXPECT_CALL(no_ref_, VoidMethod0());
589 EXPECT_CALL(no_ref_, VoidConstMethod0()).Times(2);
590
591 Callback<void(void)> method_cb =
592 Bind(&NoRef::VoidMethod0, Unretained(&no_ref_));
593 method_cb.Run();
594
595 Callback<void(void)> const_method_cb =
596 Bind(&NoRef::VoidConstMethod0, Unretained(&no_ref_));
597 const_method_cb.Run();
598
599 Callback<void(void)> const_method_const_ptr_cb =
600 Bind(&NoRef::VoidConstMethod0, Unretained(const_no_ref_ptr_));
601 const_method_const_ptr_cb.Run();
602}
603
[email protected]93540582011-05-16 22:35:14604// WeakPtr() support.
605// - Method bound to WeakPtr<> to non-const object.
606// - Const method bound to WeakPtr<> to non-const object.
607// - Const method bound to WeakPtr<> to const object.
608// - Normal Function with WeakPtr<> as P1 can have return type and is
609// not canceled.
610TEST_F(BindTest, WeakPtr) {
611 EXPECT_CALL(no_ref_, VoidMethod0());
612 EXPECT_CALL(no_ref_, VoidConstMethod0()).Times(2);
613
614 WeakPtrFactory<NoRef> weak_factory(&no_ref_);
615 WeakPtrFactory<const NoRef> const_weak_factory(const_no_ref_ptr_);
616
[email protected]7296f2762011-11-21 19:23:44617 Closure method_cb =
[email protected]93540582011-05-16 22:35:14618 Bind(&NoRef::VoidMethod0, weak_factory.GetWeakPtr());
619 method_cb.Run();
620
[email protected]7296f2762011-11-21 19:23:44621 Closure const_method_cb =
[email protected]93540582011-05-16 22:35:14622 Bind(&NoRef::VoidConstMethod0, const_weak_factory.GetWeakPtr());
623 const_method_cb.Run();
624
[email protected]7296f2762011-11-21 19:23:44625 Closure const_method_const_ptr_cb =
[email protected]93540582011-05-16 22:35:14626 Bind(&NoRef::VoidConstMethod0, const_weak_factory.GetWeakPtr());
627 const_method_const_ptr_cb.Run();
628
629 Callback<int(int)> normal_func_cb =
630 Bind(&FunctionWithWeakFirstParam, weak_factory.GetWeakPtr());
631 EXPECT_EQ(1, normal_func_cb.Run(1));
632
633 weak_factory.InvalidateWeakPtrs();
634 const_weak_factory.InvalidateWeakPtrs();
635
636 method_cb.Run();
637 const_method_cb.Run();
638 const_method_const_ptr_cb.Run();
639
640 // Still runs even after the pointers are invalidated.
641 EXPECT_EQ(2, normal_func_cb.Run(2));
642}
643
[email protected]b38d3572011-02-15 01:27:38644// ConstRef() wrapper support.
645// - Binding w/o ConstRef takes a copy.
646// - Binding a ConstRef takes a reference.
647// - Binding ConstRef to a function ConstRef does not copy on invoke.
648TEST_F(BindTest, ConstRef) {
649 int n = 1;
650
651 Callback<int(void)> copy_cb = Bind(&Identity, n);
652 Callback<int(void)> const_ref_cb = Bind(&Identity, ConstRef(n));
653 EXPECT_EQ(n, copy_cb.Run());
654 EXPECT_EQ(n, const_ref_cb.Run());
655 n++;
656 EXPECT_EQ(n - 1, copy_cb.Run());
657 EXPECT_EQ(n, const_ref_cb.Run());
658
659 int copies = 0;
660 int assigns = 0;
661 CopyCounter counter(&copies, &assigns);
662 Callback<int(void)> all_const_ref_cb =
663 Bind(&GetCopies, ConstRef(counter));
664 EXPECT_EQ(0, all_const_ref_cb.Run());
665 EXPECT_EQ(0, copies);
666 EXPECT_EQ(0, assigns);
667}
668
[email protected]edd2f1b2013-06-22 20:32:50669TEST_F(BindTest, ScopedRefptr) {
670 // BUG: The scoped_refptr should cause the only AddRef()/Release() pair. But
671 // due to a bug in base::Bind(), there's an extra call when invoking the
672 // callback.
673 // https://code.google.com/p/chromium/issues/detail?id=251937
674 EXPECT_CALL(has_ref_, AddRef()).Times(2);
675 EXPECT_CALL(has_ref_, Release()).Times(2);
676
677 const scoped_refptr<StrictMock<HasRef> > refptr(&has_ref_);
678
679 Callback<int(void)> scoped_refptr_const_ref_cb =
680 Bind(&FunctionWithScopedRefptrFirstParam, base::ConstRef(refptr), 1);
681 EXPECT_EQ(1, scoped_refptr_const_ref_cb.Run());
682}
683
[email protected]08aa4552011-10-15 00:34:42684// Test Owned() support.
685TEST_F(BindTest, Owned) {
686 int deletes = 0;
687 DeleteCounter* counter = new DeleteCounter(&deletes);
688
689 // If we don't capture, delete happens on Callback destruction/reset.
690 // return the same value.
691 Callback<DeleteCounter*(void)> no_capture_cb =
692 Bind(&PolymorphicIdentity<DeleteCounter*>, Owned(counter));
[email protected]206a2ae82011-12-22 21:12:58693 ASSERT_EQ(counter, no_capture_cb.Run());
694 ASSERT_EQ(counter, no_capture_cb.Run());
[email protected]08aa4552011-10-15 00:34:42695 EXPECT_EQ(0, deletes);
696 no_capture_cb.Reset(); // This should trigger a delete.
697 EXPECT_EQ(1, deletes);
698
699 deletes = 0;
700 counter = new DeleteCounter(&deletes);
701 base::Closure own_object_cb =
702 Bind(&DeleteCounter::VoidMethod0, Owned(counter));
703 own_object_cb.Run();
704 EXPECT_EQ(0, deletes);
705 own_object_cb.Reset();
706 EXPECT_EQ(1, deletes);
707}
708
[email protected]206a2ae82011-12-22 21:12:58709// Passed() wrapper support.
710// - Passed() can be constructed from a pointer to scoper.
711// - Passed() can be constructed from a scoper rvalue.
712// - Using Passed() gives Callback Ownership.
713// - Ownership is transferred from Callback to callee on the first Run().
714// - Callback supports unbound arguments.
715TEST_F(BindTest, ScopedPtr) {
716 int deletes = 0;
717
718 // Tests the Passed() function's support for pointers.
719 scoped_ptr<DeleteCounter> ptr(new DeleteCounter(&deletes));
720 Callback<scoped_ptr<DeleteCounter>(void)> unused_callback =
721 Bind(&PassThru<scoped_ptr<DeleteCounter> >, Passed(&ptr));
722 EXPECT_FALSE(ptr.get());
723 EXPECT_EQ(0, deletes);
724
725 // If we never invoke the Callback, it retains ownership and deletes.
726 unused_callback.Reset();
727 EXPECT_EQ(1, deletes);
728
729 // Tests the Passed() function's support for rvalues.
730 deletes = 0;
731 DeleteCounter* counter = new DeleteCounter(&deletes);
732 Callback<scoped_ptr<DeleteCounter>(void)> callback =
733 Bind(&PassThru<scoped_ptr<DeleteCounter> >,
734 Passed(scoped_ptr<DeleteCounter>(counter)));
735 EXPECT_FALSE(ptr.get());
736 EXPECT_EQ(0, deletes);
737
738 // Check that ownership can be transferred back out.
739 scoped_ptr<DeleteCounter> result = callback.Run();
740 ASSERT_EQ(counter, result.get());
741 EXPECT_EQ(0, deletes);
742
743 // Resetting does not delete since ownership was transferred.
744 callback.Reset();
745 EXPECT_EQ(0, deletes);
746
747 // Ensure that we actually did get ownership.
748 result.reset();
749 EXPECT_EQ(1, deletes);
750
751 // Test unbound argument forwarding.
752 Callback<scoped_ptr<DeleteCounter>(scoped_ptr<DeleteCounter>)> cb_unbound =
753 Bind(&PassThru<scoped_ptr<DeleteCounter> >);
754 ptr.reset(new DeleteCounter(&deletes));
dcheng9dfa1232015-12-15 05:11:17755 cb_unbound.Run(std::move(ptr));
[email protected]206a2ae82011-12-22 21:12:58756}
757
dcheng69f2a042015-12-14 20:31:52758TEST_F(BindTest, UniquePtr) {
759 int deletes = 0;
760
761 // Tests the Passed() function's support for pointers.
762 std::unique_ptr<DeleteCounter> ptr(new DeleteCounter(&deletes));
763 Callback<std::unique_ptr<DeleteCounter>(void)> unused_callback =
764 Bind(&PassThru<std::unique_ptr<DeleteCounter>>, Passed(&ptr));
765 EXPECT_FALSE(ptr.get());
766 EXPECT_EQ(0, deletes);
767
768 // If we never invoke the Callback, it retains ownership and deletes.
769 unused_callback.Reset();
770 EXPECT_EQ(1, deletes);
771
772 // Tests the Passed() function's support for rvalues.
773 deletes = 0;
774 DeleteCounter* counter = new DeleteCounter(&deletes);
775 Callback<std::unique_ptr<DeleteCounter>(void)> callback =
776 Bind(&PassThru<std::unique_ptr<DeleteCounter>>,
777 Passed(std::unique_ptr<DeleteCounter>(counter)));
778 EXPECT_FALSE(ptr.get());
779 EXPECT_EQ(0, deletes);
780
781 // Check that ownership can be transferred back out.
782 std::unique_ptr<DeleteCounter> result = callback.Run();
783 ASSERT_EQ(counter, result.get());
784 EXPECT_EQ(0, deletes);
785
786 // Resetting does not delete since ownership was transferred.
787 callback.Reset();
788 EXPECT_EQ(0, deletes);
789
790 // Ensure that we actually did get ownership.
791 result.reset();
792 EXPECT_EQ(1, deletes);
793
794 // Test unbound argument forwarding.
795 Callback<std::unique_ptr<DeleteCounter>(std::unique_ptr<DeleteCounter>)>
796 cb_unbound = Bind(&PassThru<std::unique_ptr<DeleteCounter>>);
797 ptr.reset(new DeleteCounter(&deletes));
798 cb_unbound.Run(std::move(ptr));
799}
800
[email protected]b38d3572011-02-15 01:27:38801// Argument Copy-constructor usage for non-reference parameters.
802// - Bound arguments are only copied once.
803// - Forwarded arguments are only copied once.
[email protected]206a2ae82011-12-22 21:12:58804// - Forwarded arguments with coercions are only copied twice (once for the
805// coercion, and one for the final dispatch).
[email protected]b38d3572011-02-15 01:27:38806TEST_F(BindTest, ArgumentCopies) {
807 int copies = 0;
808 int assigns = 0;
809
810 CopyCounter counter(&copies, &assigns);
811
812 Callback<void(void)> copy_cb =
813 Bind(&VoidPolymorphic1<CopyCounter>, counter);
814 EXPECT_GE(1, copies);
815 EXPECT_EQ(0, assigns);
816
817 copies = 0;
818 assigns = 0;
819 Callback<void(CopyCounter)> forward_cb =
820 Bind(&VoidPolymorphic1<CopyCounter>);
821 forward_cb.Run(counter);
822 EXPECT_GE(1, copies);
823 EXPECT_EQ(0, assigns);
824
825 copies = 0;
826 assigns = 0;
thakisb25789fb2015-04-23 05:40:02827 DerivedCopyCounter derived(&copies, &assigns);
[email protected]b38d3572011-02-15 01:27:38828 Callback<void(CopyCounter)> coerce_cb =
829 Bind(&VoidPolymorphic1<CopyCounter>);
thakisb25789fb2015-04-23 05:40:02830 coerce_cb.Run(CopyCounter(derived));
[email protected]b38d3572011-02-15 01:27:38831 EXPECT_GE(2, copies);
832 EXPECT_EQ(0, assigns);
833}
834
835// Callback construction and assignment tests.
836// - Construction from an InvokerStorageHolder should not cause ref/deref.
837// - Assignment from other callback should only cause one ref
838//
839// TODO(ajwong): Is there actually a way to test this?
840
[email protected]054ac7542011-02-27 01:25:59841#if defined(OS_WIN)
842int __fastcall FastCallFunc(int n) {
843 return n;
844}
845
846int __stdcall StdCallFunc(int n) {
847 return n;
848}
849
850// Windows specific calling convention support.
851// - Can bind a __fastcall function.
852// - Can bind a __stdcall function.
853TEST_F(BindTest, WindowsCallingConventions) {
854 Callback<int(void)> fastcall_cb = Bind(&FastCallFunc, 1);
855 EXPECT_EQ(1, fastcall_cb.Run());
856
857 Callback<int(void)> stdcall_cb = Bind(&StdCallFunc, 2);
858 EXPECT_EQ(2, stdcall_cb.Run());
859}
860#endif
861
[email protected]8cf362c2012-11-20 08:28:14862#if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) && GTEST_HAS_DEATH_TEST
863
864// Test null callbacks cause a DCHECK.
865TEST(BindDeathTest, NullCallback) {
866 base::Callback<void(int)> null_cb;
867 ASSERT_TRUE(null_cb.is_null());
868 EXPECT_DEATH(base::Bind(null_cb, 42), "");
869}
870
871#endif // (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) &&
872 // GTEST_HAS_DEATH_TEST
873
[email protected]b38d3572011-02-15 01:27:38874} // namespace
875} // namespace base