blob: 47d971aad942e1498e39f74e349ea7ba034e2f70 [file] [log] [blame]
[email protected]b38d3572011-02-15 01:27:381// Copyright (c) 2011 The Chromium Authors. All rights reserved.
2// 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
7#if defined(BASE_CALLBACK_H_)
8// We explicitly do not want to include callback.h so people are not tempted
9// to use bind.h in a headerfile for getting the Callback types.
10#error "base/bind.h should avoid pulling in callback.h by default."
11#endif
12
13#include "base/callback.h"
14#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
24class NoRef {
25 public:
26 NoRef() {}
27
28 MOCK_METHOD0(VoidMethod0, void(void));
29 MOCK_CONST_METHOD0(VoidConstMethod0, void(void));
30
31 MOCK_METHOD0(IntMethod0, int(void));
32 MOCK_CONST_METHOD0(IntConstMethod0, int(void));
33
34 private:
35 // Particularly important in this test to ensure no copies are made.
36 DISALLOW_COPY_AND_ASSIGN(NoRef);
37};
38
39class HasRef : public NoRef {
40 public:
41 HasRef() {}
42
43 MOCK_CONST_METHOD0(AddRef, void(void));
44 MOCK_CONST_METHOD0(Release, bool(void));
45
46 private:
47 // Particularly important in this test to ensure no copies are made.
48 DISALLOW_COPY_AND_ASSIGN(HasRef);
49};
50
51static const int kParentValue = 1;
52static const int kChildValue = 2;
53
54class Parent {
55 public:
56 void AddRef(void) const {}
57 void Release(void) const {}
58 virtual void VirtualSet() { value = kParentValue; }
59 void NonVirtualSet() { value = kParentValue; }
60 int value;
61};
62
63class Child : public Parent {
64 public:
65 virtual void VirtualSet() { value = kChildValue; }
66 void NonVirtualSet() { value = kChildValue; }
67};
68
69class NoRefParent {
70 public:
71 virtual void VirtualSet() { value = kParentValue; }
72 void NonVirtualSet() { value = kParentValue; }
73 int value;
74};
75
76class NoRefChild : public NoRefParent {
77 virtual void VirtualSet() { value = kChildValue; }
78 void NonVirtualSet() { value = kChildValue; }
79};
80
81// Used for probing the number of copies that occur if a type must be coerced
82// during argument forwarding in the Run() methods.
83struct DerivedCopyCounter {
84 DerivedCopyCounter(int* copies, int* assigns)
85 : copies_(copies), assigns_(assigns) {
86 }
87 int* copies_;
88 int* assigns_;
89};
90
91// Used for probing the number of copies in an argument.
92class CopyCounter {
93 public:
94 CopyCounter(int* copies, int* assigns)
95 : copies_(copies), assigns_(assigns) {
96 }
97
98 CopyCounter(const CopyCounter& other)
99 : copies_(other.copies_),
100 assigns_(other.assigns_) {
101 (*copies_)++;
102 }
103
104 // Probing for copies from coerscion.
105 CopyCounter(const DerivedCopyCounter& other)
106 : copies_(other.copies_),
107 assigns_(other.assigns_) {
108 (*copies_)++;
109 }
110
111 const CopyCounter& operator=(const CopyCounter& rhs) {
112 copies_ = rhs.copies_;
113 assigns_ = rhs.assigns_;
114
115 if (assigns_) {
116 (*assigns_)++;
117 }
118
119 return *this;
120 }
121
122 int copies() const {
123 return *copies_;
124 }
125
126 int assigns() const {
127 return *assigns_;
128 }
129
130 private:
131 int* copies_;
132 int* assigns_;
133};
134
135// Some test functions that we can Bind to.
136template <typename T>
137T PolymorphicIdentity(T t) {
138 return t;
139}
140
141template <typename T>
142void VoidPolymorphic1(T t) {
143}
144
145int Identity(int n) {
146 return n;
147}
148
149int ArrayGet(const int array[], int n) {
150 return array[n];
151}
152
153int Sum(int a, int b, int c, int d, int e, int f) {
154 return a + b + c + d + e + f;
155}
156
157const char* CStringIdentity(const char* s) {
158 return s;
159}
160
161int GetCopies(const CopyCounter& counter) {
162 return counter.copies();
163}
164
165int UnwrapNoRefParent(NoRefParent p) {
166 return p.value;
167}
168
169int UnwrapNoRefParentPtr(NoRefParent* p) {
170 return p->value;
171}
172
173int UnwrapNoRefParentConstRef(const NoRefParent& p) {
174 return p.value;
175}
176
177// Only useful in no-compile tests.
178int UnwrapNoRefParentRef(Parent& p) {
179 return p.value;
180}
181
182class BindTest : public ::testing::Test {
183 public:
184 BindTest() {
185 const_has_ref_ptr_ = &has_ref_;
186 const_no_ref_ptr_ = &no_ref_;
187 static_func_mock_ptr = &static_func_mock_;
188 }
189
190 virtual ~BindTest() {
191 }
192
193 static void VoidFunc0(void) {
194 static_func_mock_ptr->VoidMethod0();
195 }
196
197 static int IntFunc0(void) { return static_func_mock_ptr->IntMethod0(); }
198
199 protected:
200 StrictMock<NoRef> no_ref_;
201 StrictMock<HasRef> has_ref_;
202 const HasRef* const_has_ref_ptr_;
203 const NoRef* const_no_ref_ptr_;
204 StrictMock<NoRef> static_func_mock_;
205
206 // Used by the static functions to perform expectations.
207 static StrictMock<NoRef>* static_func_mock_ptr;
208
209 private:
210 DISALLOW_COPY_AND_ASSIGN(BindTest);
211};
212
213StrictMock<NoRef>* BindTest::static_func_mock_ptr;
214
215// Ensure we can create unbound callbacks. We need this to be able to store
216// them in class members that can be initialized later.
217TEST_F(BindTest, DefaultConstruction) {
218 Callback<void(void)> c0;
219 Callback<void(int)> c1;
220 Callback<void(int,int)> c2;
221 Callback<void(int,int,int)> c3;
222 Callback<void(int,int,int,int)> c4;
223 Callback<void(int,int,int,int,int)> c5;
224 Callback<void(int,int,int,int,int,int)> c6;
225}
226
227// Sanity check that we can instantiate a callback for each arity.
228TEST_F(BindTest, ArityTest) {
229 Callback<int(void)> c0 = Bind(&Sum, 32, 16, 8, 4, 2, 1);
230 EXPECT_EQ(63, c0.Run());
231
232 Callback<int(int)> c1 = Bind(&Sum, 32, 16, 8, 4, 2);
233 EXPECT_EQ(75, c1.Run(13));
234
235 Callback<int(int,int)> c2 = Bind(&Sum, 32, 16, 8, 4);
236 EXPECT_EQ(85, c2.Run(13, 12));
237
238 Callback<int(int,int,int)> c3 = Bind(&Sum, 32, 16, 8);
239 EXPECT_EQ(92, c3.Run(13, 12, 11));
240
241 Callback<int(int,int,int,int)> c4 = Bind(&Sum, 32, 16);
242 EXPECT_EQ(94, c4.Run(13, 12, 11, 10));
243
244 Callback<int(int,int,int,int,int)> c5 = Bind(&Sum, 32);
245 EXPECT_EQ(87, c5.Run(13, 12, 11, 10, 9));
246
247 Callback<int(int,int,int,int,int,int)> c6 = Bind(&Sum);
248 EXPECT_EQ(69, c6.Run(13, 12, 11, 10, 9, 14));
249}
250
251// Function type support.
252// - Normal function.
253// - Method bound to non-const object.
254// - Const method bound to non-const object.
255// - Const method bound to const object.
256// - Derived classes can be used with pointers to non-virtual base functions.
257// - Derived classes can be used with pointers to virtual base functions (and
258// preserve virtual dispatch).
259TEST_F(BindTest, FunctionTypeSupport) {
260 EXPECT_CALL(static_func_mock_, VoidMethod0());
261 EXPECT_CALL(has_ref_, AddRef()).Times(3);
262 EXPECT_CALL(has_ref_, Release()).Times(3);
263 EXPECT_CALL(has_ref_, VoidMethod0());
264 EXPECT_CALL(has_ref_, VoidConstMethod0()).Times(2);
265
266 Closure normal_cb = Bind(&VoidFunc0);
267 Closure method_cb = Bind(&HasRef::VoidMethod0, &has_ref_);
268 Closure const_method_nonconst_obj_cb = Bind(&HasRef::VoidConstMethod0,
269 &has_ref_);
270 Closure const_method_const_obj_cb = Bind(&HasRef::VoidConstMethod0,
271 const_has_ref_ptr_);
272 normal_cb.Run();
273 method_cb.Run();
274 const_method_nonconst_obj_cb.Run();
275 const_method_const_obj_cb.Run();
276
277 Child child;
278 child.value = 0;
279 Closure virtual_set_cb = Bind(&Parent::VirtualSet, &child);
280 virtual_set_cb.Run();
281 EXPECT_EQ(kChildValue, child.value);
282
283 child.value = 0;
284 Closure non_virtual_set_cb = Bind(&Parent::NonVirtualSet, &child);
285 non_virtual_set_cb.Run();
286 EXPECT_EQ(kParentValue, child.value);
287}
288
289// Return value support.
290// - Function with return value.
291// - Method with return value.
292// - Const method with return value.
293TEST_F(BindTest, ReturnValues) {
294 EXPECT_CALL(static_func_mock_, IntMethod0()).WillOnce(Return(1337));
295 EXPECT_CALL(has_ref_, AddRef()).Times(3);
296 EXPECT_CALL(has_ref_, Release()).Times(3);
297 EXPECT_CALL(has_ref_, IntMethod0()).WillOnce(Return(31337));
298 EXPECT_CALL(has_ref_, IntConstMethod0())
299 .WillOnce(Return(41337))
300 .WillOnce(Return(51337));
301
302 Callback<int(void)> normal_cb = Bind(&IntFunc0);
303 Callback<int(void)> method_cb = Bind(&HasRef::IntMethod0, &has_ref_);
304 Callback<int(void)> const_method_nonconst_obj_cb =
305 Bind(&HasRef::IntConstMethod0, &has_ref_);
306 Callback<int(void)> const_method_const_obj_cb =
307 Bind(&HasRef::IntConstMethod0, const_has_ref_ptr_);
308 EXPECT_EQ(1337, normal_cb.Run());
309 EXPECT_EQ(31337, method_cb.Run());
310 EXPECT_EQ(41337, const_method_nonconst_obj_cb.Run());
311 EXPECT_EQ(51337, const_method_const_obj_cb.Run());
312}
313
314// Argument binding tests.
315// - Argument binding to primitive.
316// - Argument binding to primitive pointer.
317// - Argument binding to a literal integer.
318// - Argument binding to a literal string.
319// - Argument binding with template function.
320// - Argument binding to an object.
321// - Argument gets type converted.
322// - Pointer argument gets converted.
323// - Const Reference forces conversion.
324TEST_F(BindTest, ArgumentBinding) {
325 int n = 2;
326
327 Callback<int(void)> bind_primitive_cb = Bind(&Identity, n);
328 EXPECT_EQ(n, bind_primitive_cb.Run());
329
330 Callback<int*(void)> bind_primitive_pointer_cb =
331 Bind(&PolymorphicIdentity<int*>, &n);
332 EXPECT_EQ(&n, bind_primitive_pointer_cb.Run());
333
334 Callback<int(void)> bind_int_literal_cb = Bind(&Identity, 3);
335 EXPECT_EQ(3, bind_int_literal_cb.Run());
336
337 Callback<const char*(void)> bind_string_literal_cb =
338 Bind(&CStringIdentity, "hi");
339 EXPECT_STREQ("hi", bind_string_literal_cb.Run());
340
341 Callback<int(void)> bind_template_function_cb =
342 Bind(&PolymorphicIdentity<int>, 4);
343 EXPECT_EQ(4, bind_template_function_cb.Run());
344
345 NoRefParent p;
346 p.value = 5;
347 Callback<int(void)> bind_object_cb = Bind(&UnwrapNoRefParent, p);
348 EXPECT_EQ(5, bind_object_cb.Run());
349
350 NoRefChild c;
351 c.value = 6;
352 Callback<int(void)> bind_promotes_cb = Bind(&UnwrapNoRefParent, c);
353 EXPECT_EQ(6, bind_promotes_cb.Run());
354
355 c.value = 7;
356 Callback<int(void)> bind_pointer_promotes_cb =
357 Bind(&UnwrapNoRefParentPtr, &c);
358 EXPECT_EQ(7, bind_pointer_promotes_cb.Run());
359
360 c.value = 8;
361 Callback<int(void)> bind_const_reference_promotes_cb =
362 Bind(&UnwrapNoRefParentConstRef, c);
363 EXPECT_EQ(8, bind_const_reference_promotes_cb.Run());
364}
365
366// Functions that take reference parameters.
367// - Forced reference parameter type still stores a copy.
368// - Forced const reference parameter type still stores a copy.
369TEST_F(BindTest, ReferenceArgumentBinding) {
370 int n = 1;
371 int& ref_n = n;
372 const int& const_ref_n = n;
373
374 Callback<int(void)> ref_copies_cb = Bind(&Identity, ref_n);
375 EXPECT_EQ(n, ref_copies_cb.Run());
376 n++;
377 EXPECT_EQ(n - 1, ref_copies_cb.Run());
378
379 Callback<int(void)> const_ref_copies_cb = Bind(&Identity, const_ref_n);
380 EXPECT_EQ(n, const_ref_copies_cb.Run());
381 n++;
382 EXPECT_EQ(n - 1, const_ref_copies_cb.Run());
383}
384
385// Check that we can pass in arrays and have them be stored as a pointer.
386// - Array of values stores a pointer.
387// - Array of const values stores a pointer.
388TEST_F(BindTest, ArrayArgumentBinding) {
389 int array[4] = {1, 1, 1, 1};
390 const int (*const_array_ptr)[4] = &array;
391
392 Callback<int(void)> array_cb = Bind(&ArrayGet, array, 1);
393 EXPECT_EQ(1, array_cb.Run());
394
395 Callback<int(void)> const_array_cb = Bind(&ArrayGet, *const_array_ptr, 1);
396 EXPECT_EQ(1, const_array_cb.Run());
397
398 array[1] = 3;
399 EXPECT_EQ(3, array_cb.Run());
400 EXPECT_EQ(3, const_array_cb.Run());
401}
402
403// Verify SupportsAddRefAndRelease correctly introspects the class type for
404// AddRef() and Release().
405TEST_F(BindTest, SupportsAddRefAndRelease) {
406 EXPECT_TRUE(internal::SupportsAddRefAndRelease<HasRef>::value);
407 EXPECT_FALSE(internal::SupportsAddRefAndRelease<NoRef>::value);
408
409 // StrictMock<T> is a derived class of T. So, we use StrictMock<HasRef> and
410 // StrictMock<NoRef> to test that SupportsAddRefAndRelease works over
411 // inheritance.
412 EXPECT_TRUE(internal::SupportsAddRefAndRelease<StrictMock<HasRef> >::value);
413 EXPECT_FALSE(internal::SupportsAddRefAndRelease<StrictMock<NoRef> >::value);
414}
415
416// Unretained() wrapper support.
417// - Method bound to Unretained() non-object.
418// - Const method bound to Unretained() non-const object.
419// - Const method bound to Unretained() const object.
420TEST_F(BindTest, Unretained) {
421 EXPECT_CALL(no_ref_, VoidMethod0());
422 EXPECT_CALL(no_ref_, VoidConstMethod0()).Times(2);
423
424 Callback<void(void)> method_cb =
425 Bind(&NoRef::VoidMethod0, Unretained(&no_ref_));
426 method_cb.Run();
427
428 Callback<void(void)> const_method_cb =
429 Bind(&NoRef::VoidConstMethod0, Unretained(&no_ref_));
430 const_method_cb.Run();
431
432 Callback<void(void)> const_method_const_ptr_cb =
433 Bind(&NoRef::VoidConstMethod0, Unretained(const_no_ref_ptr_));
434 const_method_const_ptr_cb.Run();
435}
436
437// ConstRef() wrapper support.
438// - Binding w/o ConstRef takes a copy.
439// - Binding a ConstRef takes a reference.
440// - Binding ConstRef to a function ConstRef does not copy on invoke.
441TEST_F(BindTest, ConstRef) {
442 int n = 1;
443
444 Callback<int(void)> copy_cb = Bind(&Identity, n);
445 Callback<int(void)> const_ref_cb = Bind(&Identity, ConstRef(n));
446 EXPECT_EQ(n, copy_cb.Run());
447 EXPECT_EQ(n, const_ref_cb.Run());
448 n++;
449 EXPECT_EQ(n - 1, copy_cb.Run());
450 EXPECT_EQ(n, const_ref_cb.Run());
451
452 int copies = 0;
453 int assigns = 0;
454 CopyCounter counter(&copies, &assigns);
455 Callback<int(void)> all_const_ref_cb =
456 Bind(&GetCopies, ConstRef(counter));
457 EXPECT_EQ(0, all_const_ref_cb.Run());
458 EXPECT_EQ(0, copies);
459 EXPECT_EQ(0, assigns);
460}
461
462// Argument Copy-constructor usage for non-reference parameters.
463// - Bound arguments are only copied once.
464// - Forwarded arguments are only copied once.
465// - Forwarded arguments with coerscions are only copied twice (once for the
466// coerscion, and one for the final dispatch).
467TEST_F(BindTest, ArgumentCopies) {
468 int copies = 0;
469 int assigns = 0;
470
471 CopyCounter counter(&copies, &assigns);
472
473 Callback<void(void)> copy_cb =
474 Bind(&VoidPolymorphic1<CopyCounter>, counter);
475 EXPECT_GE(1, copies);
476 EXPECT_EQ(0, assigns);
477
478 copies = 0;
479 assigns = 0;
480 Callback<void(CopyCounter)> forward_cb =
481 Bind(&VoidPolymorphic1<CopyCounter>);
482 forward_cb.Run(counter);
483 EXPECT_GE(1, copies);
484 EXPECT_EQ(0, assigns);
485
486 copies = 0;
487 assigns = 0;
488 DerivedCopyCounter dervied(&copies, &assigns);
489 Callback<void(CopyCounter)> coerce_cb =
490 Bind(&VoidPolymorphic1<CopyCounter>);
491 coerce_cb.Run(dervied);
492 EXPECT_GE(2, copies);
493 EXPECT_EQ(0, assigns);
494}
495
496// Callback construction and assignment tests.
497// - Construction from an InvokerStorageHolder should not cause ref/deref.
498// - Assignment from other callback should only cause one ref
499//
500// TODO(ajwong): Is there actually a way to test this?
501
502// No-compile tests. These should not compile. If they do, we are allowing
503// error-prone, or incorrect behavior in the callback system. Uncomment the
504// tests to check.
505TEST_F(BindTest, NoCompile) {
506 // - Method bound to const-object.
507 //
508 // Only const methods should be allowed to work with const objects.
509 //
510 // Callback<void(void)> method_to_const_cb =
511 // Bind(&HasRef::VoidMethod0, const_has_ref_ptr_);
512 // method_to_const_cb.Run();
513
514 // - Method bound to non-refcounted object.
515 // - Const Method bound to non-refcounted object.
516 //
517 // We require refcounts unless you have Unretained().
518 //
519 // Callback<void(void)> no_ref_cb =
520 // Bind(&NoRef::VoidMethod0, &no_ref_);
521 // no_ref_cb.Run();
522 // Callback<void(void)> no_ref_const_cb =
523 // Bind(&NoRef::VoidConstMethod0, &no_ref_);
524 // no_ref_const_cb.Run();
525
526 // - Unretained() used with a refcounted object.
527 //
528 // If the object supports refcounts, unretaining it in the callback is a
529 // memory management contract break.
530 // Callback<void(void)> unretained_cb =
531 // Bind(&HasRef::VoidConstMethod0, Unretained(&has_ref_));
532 // unretained_cb.Run();
533
534 // - Const argument used with non-const pointer parameter of same type.
535 // - Const argument used with non-const pointer parameter of super type.
536 //
537 // This is just a const-correctness check.
538 //
539 // const Parent* const_parent_ptr;
540 // const Child* const_child_ptr;
541 // Callback<Parent*(void)> pointer_same_cb =
542 // Bind(&PolymorphicIdentity<Parent*>, const_parent_ptr);
543 // pointer_same_cb.Run();
544 // Callback<Parent*(void)> pointer_super_cb =
545 // Bind(&PolymorphicIdentity<Parent*>, const_child_ptr);
546 // pointer_super_cb.Run();
547
548 // - Construction of Callback<A> from Callback<B> if A is supertype of B.
549 // Specific example: Callback<void(void)> a; Callback<int(void)> b; a = b;
550 //
551 // While this is technically safe, most people aren't used to it when coding
552 // C++ so if this is happening, it is almost certainly an error.
553 //
554 // Callback<int(void)> cb_a0 = Bind(&Identity, 1);
555 // Callback<void(void)> cb_b0 = cb_a0;
556
557 // - Assignment of Callback<A> from Callback<B> if A is supertype of B.
558 // See explanation above.
559 //
560 // Callback<int(void)> cb_a1 = Bind(&Identity, 1);
561 // Callback<void(void)> cb_b1;
562 // cb_a1 = cb_b1;
563
564 // - Functions with reference parameters, unsupported.
565 //
566 // First, non-const reference parameters are disallowed by the Google
567 // style guide. Seconds, since we are doing argument forwarding it becomes
568 // very tricky to avoid copies, maintain const correctness, and not
569 // accidentally have the function be modifying a temporary, or a copy.
570 //
571 // NoRefParent p;
572 // Callback<int(Parent&)> ref_arg_cb = Bind(&UnwrapNoRefParentRef);
573 // ref_arg_cb.Run(p);
574 // Callback<int(void)> ref_cb = Bind(&UnwrapNoRefParentRef, p);
575 // ref_cb.Run();
576
577 // - A method should not be bindable with an array of objects.
578 //
579 // This is likely not wanted behavior. We specifically check for it though
580 // because it is possible, depending on how you implement prebinding, to
581 // implicitly convert an array type to a pointer type.
582 //
583 // HasRef p[10];
584 // Callback<void(void)> method_bound_to_array_cb =
585 // Bind(&HasRef::VoidConstMethod0, p);
586 // method_bound_to_array_cb.Run();
587
588 // - Refcounted types should not be bound as a raw pointer.
589 // HasRef for_raw_ptr;
590 // Callback<void(void)> ref_count_as_raw_ptr =
591 // Bind(&VoidPolymorphic1<HasRef*>, &for_raw_ptr);
592 // ASSERT_EQ(&for_raw_ptr, ref_count_as_raw_ptr.Run());
593
594}
595
596} // namespace
597} // namespace base