blob: 061808b3d0b805b0aab2b067c65a50d39752f389 [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
[email protected]b38d3572011-02-15 01:27:38215// Sanity check that we can instantiate a callback for each arity.
216TEST_F(BindTest, ArityTest) {
217 Callback<int(void)> c0 = Bind(&Sum, 32, 16, 8, 4, 2, 1);
218 EXPECT_EQ(63, c0.Run());
219
220 Callback<int(int)> c1 = Bind(&Sum, 32, 16, 8, 4, 2);
221 EXPECT_EQ(75, c1.Run(13));
222
223 Callback<int(int,int)> c2 = Bind(&Sum, 32, 16, 8, 4);
224 EXPECT_EQ(85, c2.Run(13, 12));
225
226 Callback<int(int,int,int)> c3 = Bind(&Sum, 32, 16, 8);
227 EXPECT_EQ(92, c3.Run(13, 12, 11));
228
229 Callback<int(int,int,int,int)> c4 = Bind(&Sum, 32, 16);
230 EXPECT_EQ(94, c4.Run(13, 12, 11, 10));
231
232 Callback<int(int,int,int,int,int)> c5 = Bind(&Sum, 32);
233 EXPECT_EQ(87, c5.Run(13, 12, 11, 10, 9));
234
235 Callback<int(int,int,int,int,int,int)> c6 = Bind(&Sum);
236 EXPECT_EQ(69, c6.Run(13, 12, 11, 10, 9, 14));
237}
238
239// Function type support.
240// - Normal function.
241// - Method bound to non-const object.
242// - Const method bound to non-const object.
243// - Const method bound to const object.
244// - Derived classes can be used with pointers to non-virtual base functions.
245// - Derived classes can be used with pointers to virtual base functions (and
246// preserve virtual dispatch).
247TEST_F(BindTest, FunctionTypeSupport) {
248 EXPECT_CALL(static_func_mock_, VoidMethod0());
249 EXPECT_CALL(has_ref_, AddRef()).Times(3);
250 EXPECT_CALL(has_ref_, Release()).Times(3);
251 EXPECT_CALL(has_ref_, VoidMethod0());
252 EXPECT_CALL(has_ref_, VoidConstMethod0()).Times(2);
253
254 Closure normal_cb = Bind(&VoidFunc0);
255 Closure method_cb = Bind(&HasRef::VoidMethod0, &has_ref_);
256 Closure const_method_nonconst_obj_cb = Bind(&HasRef::VoidConstMethod0,
257 &has_ref_);
258 Closure const_method_const_obj_cb = Bind(&HasRef::VoidConstMethod0,
259 const_has_ref_ptr_);
260 normal_cb.Run();
261 method_cb.Run();
262 const_method_nonconst_obj_cb.Run();
263 const_method_const_obj_cb.Run();
264
265 Child child;
266 child.value = 0;
267 Closure virtual_set_cb = Bind(&Parent::VirtualSet, &child);
268 virtual_set_cb.Run();
269 EXPECT_EQ(kChildValue, child.value);
270
271 child.value = 0;
272 Closure non_virtual_set_cb = Bind(&Parent::NonVirtualSet, &child);
273 non_virtual_set_cb.Run();
274 EXPECT_EQ(kParentValue, child.value);
275}
276
277// Return value support.
278// - Function with return value.
279// - Method with return value.
280// - Const method with return value.
281TEST_F(BindTest, ReturnValues) {
282 EXPECT_CALL(static_func_mock_, IntMethod0()).WillOnce(Return(1337));
283 EXPECT_CALL(has_ref_, AddRef()).Times(3);
284 EXPECT_CALL(has_ref_, Release()).Times(3);
285 EXPECT_CALL(has_ref_, IntMethod0()).WillOnce(Return(31337));
286 EXPECT_CALL(has_ref_, IntConstMethod0())
287 .WillOnce(Return(41337))
288 .WillOnce(Return(51337));
289
290 Callback<int(void)> normal_cb = Bind(&IntFunc0);
291 Callback<int(void)> method_cb = Bind(&HasRef::IntMethod0, &has_ref_);
292 Callback<int(void)> const_method_nonconst_obj_cb =
293 Bind(&HasRef::IntConstMethod0, &has_ref_);
294 Callback<int(void)> const_method_const_obj_cb =
295 Bind(&HasRef::IntConstMethod0, const_has_ref_ptr_);
296 EXPECT_EQ(1337, normal_cb.Run());
297 EXPECT_EQ(31337, method_cb.Run());
298 EXPECT_EQ(41337, const_method_nonconst_obj_cb.Run());
299 EXPECT_EQ(51337, const_method_const_obj_cb.Run());
300}
301
302// Argument binding tests.
303// - Argument binding to primitive.
304// - Argument binding to primitive pointer.
305// - Argument binding to a literal integer.
306// - Argument binding to a literal string.
307// - Argument binding with template function.
308// - Argument binding to an object.
309// - Argument gets type converted.
310// - Pointer argument gets converted.
311// - Const Reference forces conversion.
312TEST_F(BindTest, ArgumentBinding) {
313 int n = 2;
314
315 Callback<int(void)> bind_primitive_cb = Bind(&Identity, n);
316 EXPECT_EQ(n, bind_primitive_cb.Run());
317
318 Callback<int*(void)> bind_primitive_pointer_cb =
319 Bind(&PolymorphicIdentity<int*>, &n);
320 EXPECT_EQ(&n, bind_primitive_pointer_cb.Run());
321
322 Callback<int(void)> bind_int_literal_cb = Bind(&Identity, 3);
323 EXPECT_EQ(3, bind_int_literal_cb.Run());
324
325 Callback<const char*(void)> bind_string_literal_cb =
326 Bind(&CStringIdentity, "hi");
327 EXPECT_STREQ("hi", bind_string_literal_cb.Run());
328
329 Callback<int(void)> bind_template_function_cb =
330 Bind(&PolymorphicIdentity<int>, 4);
331 EXPECT_EQ(4, bind_template_function_cb.Run());
332
333 NoRefParent p;
334 p.value = 5;
335 Callback<int(void)> bind_object_cb = Bind(&UnwrapNoRefParent, p);
336 EXPECT_EQ(5, bind_object_cb.Run());
337
338 NoRefChild c;
339 c.value = 6;
340 Callback<int(void)> bind_promotes_cb = Bind(&UnwrapNoRefParent, c);
341 EXPECT_EQ(6, bind_promotes_cb.Run());
342
343 c.value = 7;
344 Callback<int(void)> bind_pointer_promotes_cb =
345 Bind(&UnwrapNoRefParentPtr, &c);
346 EXPECT_EQ(7, bind_pointer_promotes_cb.Run());
347
348 c.value = 8;
349 Callback<int(void)> bind_const_reference_promotes_cb =
350 Bind(&UnwrapNoRefParentConstRef, c);
351 EXPECT_EQ(8, bind_const_reference_promotes_cb.Run());
352}
353
354// Functions that take reference parameters.
355// - Forced reference parameter type still stores a copy.
356// - Forced const reference parameter type still stores a copy.
357TEST_F(BindTest, ReferenceArgumentBinding) {
358 int n = 1;
359 int& ref_n = n;
360 const int& const_ref_n = n;
361
362 Callback<int(void)> ref_copies_cb = Bind(&Identity, ref_n);
363 EXPECT_EQ(n, ref_copies_cb.Run());
364 n++;
365 EXPECT_EQ(n - 1, ref_copies_cb.Run());
366
367 Callback<int(void)> const_ref_copies_cb = Bind(&Identity, const_ref_n);
368 EXPECT_EQ(n, const_ref_copies_cb.Run());
369 n++;
370 EXPECT_EQ(n - 1, const_ref_copies_cb.Run());
371}
372
373// Check that we can pass in arrays and have them be stored as a pointer.
374// - Array of values stores a pointer.
375// - Array of const values stores a pointer.
376TEST_F(BindTest, ArrayArgumentBinding) {
377 int array[4] = {1, 1, 1, 1};
378 const int (*const_array_ptr)[4] = &array;
379
380 Callback<int(void)> array_cb = Bind(&ArrayGet, array, 1);
381 EXPECT_EQ(1, array_cb.Run());
382
383 Callback<int(void)> const_array_cb = Bind(&ArrayGet, *const_array_ptr, 1);
384 EXPECT_EQ(1, const_array_cb.Run());
385
386 array[1] = 3;
387 EXPECT_EQ(3, array_cb.Run());
388 EXPECT_EQ(3, const_array_cb.Run());
389}
390
391// Verify SupportsAddRefAndRelease correctly introspects the class type for
392// AddRef() and Release().
393TEST_F(BindTest, SupportsAddRefAndRelease) {
394 EXPECT_TRUE(internal::SupportsAddRefAndRelease<HasRef>::value);
395 EXPECT_FALSE(internal::SupportsAddRefAndRelease<NoRef>::value);
396
397 // StrictMock<T> is a derived class of T. So, we use StrictMock<HasRef> and
398 // StrictMock<NoRef> to test that SupportsAddRefAndRelease works over
399 // inheritance.
400 EXPECT_TRUE(internal::SupportsAddRefAndRelease<StrictMock<HasRef> >::value);
401 EXPECT_FALSE(internal::SupportsAddRefAndRelease<StrictMock<NoRef> >::value);
402}
403
404// Unretained() wrapper support.
405// - Method bound to Unretained() non-object.
406// - Const method bound to Unretained() non-const object.
407// - Const method bound to Unretained() const object.
408TEST_F(BindTest, Unretained) {
409 EXPECT_CALL(no_ref_, VoidMethod0());
410 EXPECT_CALL(no_ref_, VoidConstMethod0()).Times(2);
411
412 Callback<void(void)> method_cb =
413 Bind(&NoRef::VoidMethod0, Unretained(&no_ref_));
414 method_cb.Run();
415
416 Callback<void(void)> const_method_cb =
417 Bind(&NoRef::VoidConstMethod0, Unretained(&no_ref_));
418 const_method_cb.Run();
419
420 Callback<void(void)> const_method_const_ptr_cb =
421 Bind(&NoRef::VoidConstMethod0, Unretained(const_no_ref_ptr_));
422 const_method_const_ptr_cb.Run();
423}
424
425// ConstRef() wrapper support.
426// - Binding w/o ConstRef takes a copy.
427// - Binding a ConstRef takes a reference.
428// - Binding ConstRef to a function ConstRef does not copy on invoke.
429TEST_F(BindTest, ConstRef) {
430 int n = 1;
431
432 Callback<int(void)> copy_cb = Bind(&Identity, n);
433 Callback<int(void)> const_ref_cb = Bind(&Identity, ConstRef(n));
434 EXPECT_EQ(n, copy_cb.Run());
435 EXPECT_EQ(n, const_ref_cb.Run());
436 n++;
437 EXPECT_EQ(n - 1, copy_cb.Run());
438 EXPECT_EQ(n, const_ref_cb.Run());
439
440 int copies = 0;
441 int assigns = 0;
442 CopyCounter counter(&copies, &assigns);
443 Callback<int(void)> all_const_ref_cb =
444 Bind(&GetCopies, ConstRef(counter));
445 EXPECT_EQ(0, all_const_ref_cb.Run());
446 EXPECT_EQ(0, copies);
447 EXPECT_EQ(0, assigns);
448}
449
450// Argument Copy-constructor usage for non-reference parameters.
451// - Bound arguments are only copied once.
452// - Forwarded arguments are only copied once.
453// - Forwarded arguments with coerscions are only copied twice (once for the
454// coerscion, and one for the final dispatch).
455TEST_F(BindTest, ArgumentCopies) {
456 int copies = 0;
457 int assigns = 0;
458
459 CopyCounter counter(&copies, &assigns);
460
461 Callback<void(void)> copy_cb =
462 Bind(&VoidPolymorphic1<CopyCounter>, counter);
463 EXPECT_GE(1, copies);
464 EXPECT_EQ(0, assigns);
465
466 copies = 0;
467 assigns = 0;
468 Callback<void(CopyCounter)> forward_cb =
469 Bind(&VoidPolymorphic1<CopyCounter>);
470 forward_cb.Run(counter);
471 EXPECT_GE(1, copies);
472 EXPECT_EQ(0, assigns);
473
474 copies = 0;
475 assigns = 0;
476 DerivedCopyCounter dervied(&copies, &assigns);
477 Callback<void(CopyCounter)> coerce_cb =
478 Bind(&VoidPolymorphic1<CopyCounter>);
479 coerce_cb.Run(dervied);
480 EXPECT_GE(2, copies);
481 EXPECT_EQ(0, assigns);
482}
483
484// Callback construction and assignment tests.
485// - Construction from an InvokerStorageHolder should not cause ref/deref.
486// - Assignment from other callback should only cause one ref
487//
488// TODO(ajwong): Is there actually a way to test this?
489
490// No-compile tests. These should not compile. If they do, we are allowing
491// error-prone, or incorrect behavior in the callback system. Uncomment the
492// tests to check.
493TEST_F(BindTest, NoCompile) {
494 // - Method bound to const-object.
495 //
496 // Only const methods should be allowed to work with const objects.
497 //
498 // Callback<void(void)> method_to_const_cb =
499 // Bind(&HasRef::VoidMethod0, const_has_ref_ptr_);
500 // method_to_const_cb.Run();
501
502 // - Method bound to non-refcounted object.
503 // - Const Method bound to non-refcounted object.
504 //
505 // We require refcounts unless you have Unretained().
506 //
507 // Callback<void(void)> no_ref_cb =
508 // Bind(&NoRef::VoidMethod0, &no_ref_);
509 // no_ref_cb.Run();
510 // Callback<void(void)> no_ref_const_cb =
511 // Bind(&NoRef::VoidConstMethod0, &no_ref_);
512 // no_ref_const_cb.Run();
513
514 // - Unretained() used with a refcounted object.
515 //
516 // If the object supports refcounts, unretaining it in the callback is a
517 // memory management contract break.
518 // Callback<void(void)> unretained_cb =
519 // Bind(&HasRef::VoidConstMethod0, Unretained(&has_ref_));
520 // unretained_cb.Run();
521
522 // - Const argument used with non-const pointer parameter of same type.
523 // - Const argument used with non-const pointer parameter of super type.
524 //
525 // This is just a const-correctness check.
526 //
527 // const Parent* const_parent_ptr;
528 // const Child* const_child_ptr;
529 // Callback<Parent*(void)> pointer_same_cb =
530 // Bind(&PolymorphicIdentity<Parent*>, const_parent_ptr);
531 // pointer_same_cb.Run();
532 // Callback<Parent*(void)> pointer_super_cb =
533 // Bind(&PolymorphicIdentity<Parent*>, const_child_ptr);
534 // pointer_super_cb.Run();
535
536 // - Construction of Callback<A> from Callback<B> if A is supertype of B.
537 // Specific example: Callback<void(void)> a; Callback<int(void)> b; a = b;
538 //
539 // While this is technically safe, most people aren't used to it when coding
540 // C++ so if this is happening, it is almost certainly an error.
541 //
542 // Callback<int(void)> cb_a0 = Bind(&Identity, 1);
543 // Callback<void(void)> cb_b0 = cb_a0;
544
545 // - Assignment of Callback<A> from Callback<B> if A is supertype of B.
546 // See explanation above.
547 //
548 // Callback<int(void)> cb_a1 = Bind(&Identity, 1);
549 // Callback<void(void)> cb_b1;
550 // cb_a1 = cb_b1;
551
552 // - Functions with reference parameters, unsupported.
553 //
554 // First, non-const reference parameters are disallowed by the Google
555 // style guide. Seconds, since we are doing argument forwarding it becomes
556 // very tricky to avoid copies, maintain const correctness, and not
557 // accidentally have the function be modifying a temporary, or a copy.
558 //
559 // NoRefParent p;
560 // Callback<int(Parent&)> ref_arg_cb = Bind(&UnwrapNoRefParentRef);
561 // ref_arg_cb.Run(p);
562 // Callback<int(void)> ref_cb = Bind(&UnwrapNoRefParentRef, p);
563 // ref_cb.Run();
564
565 // - A method should not be bindable with an array of objects.
566 //
567 // This is likely not wanted behavior. We specifically check for it though
568 // because it is possible, depending on how you implement prebinding, to
569 // implicitly convert an array type to a pointer type.
570 //
571 // HasRef p[10];
572 // Callback<void(void)> method_bound_to_array_cb =
573 // Bind(&HasRef::VoidConstMethod0, p);
574 // method_bound_to_array_cb.Run();
575
576 // - Refcounted types should not be bound as a raw pointer.
577 // HasRef for_raw_ptr;
578 // Callback<void(void)> ref_count_as_raw_ptr =
579 // Bind(&VoidPolymorphic1<HasRef*>, &for_raw_ptr);
580 // ASSERT_EQ(&for_raw_ptr, ref_count_as_raw_ptr.Run());
581
582}
583
[email protected]054ac7542011-02-27 01:25:59584#if defined(OS_WIN)
585int __fastcall FastCallFunc(int n) {
586 return n;
587}
588
589int __stdcall StdCallFunc(int n) {
590 return n;
591}
592
593// Windows specific calling convention support.
594// - Can bind a __fastcall function.
595// - Can bind a __stdcall function.
596TEST_F(BindTest, WindowsCallingConventions) {
597 Callback<int(void)> fastcall_cb = Bind(&FastCallFunc, 1);
598 EXPECT_EQ(1, fastcall_cb.Run());
599
600 Callback<int(void)> stdcall_cb = Bind(&StdCallFunc, 2);
601 EXPECT_EQ(2, stdcall_cb.Run());
602}
603#endif
604
[email protected]b38d3572011-02-15 01:27:38605} // namespace
606} // namespace base