blob: 8718d1da85f75fd53ea9f5f66d54506fc8d2adea [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
[email protected]690bda882011-04-13 22:40:4651class HasRefPrivateDtor : public HasRef {
52 private:
53 ~HasRefPrivateDtor() {}
54};
55
[email protected]b38d3572011-02-15 01:27:3856static const int kParentValue = 1;
57static const int kChildValue = 2;
58
59class Parent {
60 public:
61 void AddRef(void) const {}
62 void Release(void) const {}
63 virtual void VirtualSet() { value = kParentValue; }
64 void NonVirtualSet() { value = kParentValue; }
65 int value;
66};
67
68class Child : public Parent {
69 public:
70 virtual void VirtualSet() { value = kChildValue; }
71 void NonVirtualSet() { value = kChildValue; }
72};
73
74class NoRefParent {
75 public:
76 virtual void VirtualSet() { value = kParentValue; }
77 void NonVirtualSet() { value = kParentValue; }
78 int value;
79};
80
81class NoRefChild : public NoRefParent {
82 virtual void VirtualSet() { value = kChildValue; }
83 void NonVirtualSet() { value = kChildValue; }
84};
85
86// Used for probing the number of copies that occur if a type must be coerced
87// during argument forwarding in the Run() methods.
88struct DerivedCopyCounter {
89 DerivedCopyCounter(int* copies, int* assigns)
90 : copies_(copies), assigns_(assigns) {
91 }
92 int* copies_;
93 int* assigns_;
94};
95
96// Used for probing the number of copies in an argument.
97class CopyCounter {
98 public:
99 CopyCounter(int* copies, int* assigns)
100 : copies_(copies), assigns_(assigns) {
101 }
102
103 CopyCounter(const CopyCounter& other)
104 : copies_(other.copies_),
105 assigns_(other.assigns_) {
106 (*copies_)++;
107 }
108
109 // Probing for copies from coerscion.
110 CopyCounter(const DerivedCopyCounter& other)
111 : copies_(other.copies_),
112 assigns_(other.assigns_) {
113 (*copies_)++;
114 }
115
116 const CopyCounter& operator=(const CopyCounter& rhs) {
117 copies_ = rhs.copies_;
118 assigns_ = rhs.assigns_;
119
120 if (assigns_) {
121 (*assigns_)++;
122 }
123
124 return *this;
125 }
126
127 int copies() const {
128 return *copies_;
129 }
130
131 int assigns() const {
132 return *assigns_;
133 }
134
135 private:
136 int* copies_;
137 int* assigns_;
138};
139
140// Some test functions that we can Bind to.
141template <typename T>
142T PolymorphicIdentity(T t) {
143 return t;
144}
145
146template <typename T>
147void VoidPolymorphic1(T t) {
148}
149
150int Identity(int n) {
151 return n;
152}
153
154int ArrayGet(const int array[], int n) {
155 return array[n];
156}
157
158int Sum(int a, int b, int c, int d, int e, int f) {
159 return a + b + c + d + e + f;
160}
161
162const char* CStringIdentity(const char* s) {
163 return s;
164}
165
166int GetCopies(const CopyCounter& counter) {
167 return counter.copies();
168}
169
170int UnwrapNoRefParent(NoRefParent p) {
171 return p.value;
172}
173
174int UnwrapNoRefParentPtr(NoRefParent* p) {
175 return p->value;
176}
177
178int UnwrapNoRefParentConstRef(const NoRefParent& p) {
179 return p.value;
180}
181
[email protected]c18b1052011-03-24 02:02:17182void RefArgSet(int &n) {
183 n = 2;
184}
185
[email protected]93540582011-05-16 22:35:14186int FunctionWithWeakFirstParam(WeakPtr<NoRef> o, int n) {
187 return n;
188}
189
[email protected]b38d3572011-02-15 01:27:38190// Only useful in no-compile tests.
191int UnwrapNoRefParentRef(Parent& p) {
192 return p.value;
193}
194
195class BindTest : public ::testing::Test {
196 public:
197 BindTest() {
198 const_has_ref_ptr_ = &has_ref_;
199 const_no_ref_ptr_ = &no_ref_;
200 static_func_mock_ptr = &static_func_mock_;
201 }
202
203 virtual ~BindTest() {
204 }
205
206 static void VoidFunc0(void) {
207 static_func_mock_ptr->VoidMethod0();
208 }
209
210 static int IntFunc0(void) { return static_func_mock_ptr->IntMethod0(); }
211
212 protected:
213 StrictMock<NoRef> no_ref_;
214 StrictMock<HasRef> has_ref_;
215 const HasRef* const_has_ref_ptr_;
216 const NoRef* const_no_ref_ptr_;
217 StrictMock<NoRef> static_func_mock_;
218
219 // Used by the static functions to perform expectations.
220 static StrictMock<NoRef>* static_func_mock_ptr;
221
222 private:
223 DISALLOW_COPY_AND_ASSIGN(BindTest);
224};
225
226StrictMock<NoRef>* BindTest::static_func_mock_ptr;
227
[email protected]b38d3572011-02-15 01:27:38228// Sanity check that we can instantiate a callback for each arity.
229TEST_F(BindTest, ArityTest) {
230 Callback<int(void)> c0 = Bind(&Sum, 32, 16, 8, 4, 2, 1);
231 EXPECT_EQ(63, c0.Run());
232
233 Callback<int(int)> c1 = Bind(&Sum, 32, 16, 8, 4, 2);
234 EXPECT_EQ(75, c1.Run(13));
235
236 Callback<int(int,int)> c2 = Bind(&Sum, 32, 16, 8, 4);
237 EXPECT_EQ(85, c2.Run(13, 12));
238
239 Callback<int(int,int,int)> c3 = Bind(&Sum, 32, 16, 8);
240 EXPECT_EQ(92, c3.Run(13, 12, 11));
241
242 Callback<int(int,int,int,int)> c4 = Bind(&Sum, 32, 16);
243 EXPECT_EQ(94, c4.Run(13, 12, 11, 10));
244
245 Callback<int(int,int,int,int,int)> c5 = Bind(&Sum, 32);
246 EXPECT_EQ(87, c5.Run(13, 12, 11, 10, 9));
247
248 Callback<int(int,int,int,int,int,int)> c6 = Bind(&Sum);
249 EXPECT_EQ(69, c6.Run(13, 12, 11, 10, 9, 14));
250}
251
252// Function type support.
253// - Normal function.
[email protected]81814bce2011-09-10 03:03:00254// - Normal function bound with non-refcounted first argument.
[email protected]b38d3572011-02-15 01:27:38255// - Method bound to non-const object.
256// - Const method bound to non-const object.
257// - Const method bound to const object.
258// - Derived classes can be used with pointers to non-virtual base functions.
259// - Derived classes can be used with pointers to virtual base functions (and
260// preserve virtual dispatch).
261TEST_F(BindTest, FunctionTypeSupport) {
262 EXPECT_CALL(static_func_mock_, VoidMethod0());
263 EXPECT_CALL(has_ref_, AddRef()).Times(3);
264 EXPECT_CALL(has_ref_, Release()).Times(3);
265 EXPECT_CALL(has_ref_, VoidMethod0());
266 EXPECT_CALL(has_ref_, VoidConstMethod0()).Times(2);
267
268 Closure normal_cb = Bind(&VoidFunc0);
[email protected]81814bce2011-09-10 03:03:00269 Callback<NoRef*(void)> normal_non_refcounted_cb =
270 Bind(&PolymorphicIdentity<NoRef*>, &no_ref_);
271 normal_cb.Run();
272 EXPECT_EQ(&no_ref_, normal_non_refcounted_cb.Run());
273
[email protected]b38d3572011-02-15 01:27:38274 Closure method_cb = Bind(&HasRef::VoidMethod0, &has_ref_);
275 Closure const_method_nonconst_obj_cb = Bind(&HasRef::VoidConstMethod0,
276 &has_ref_);
277 Closure const_method_const_obj_cb = Bind(&HasRef::VoidConstMethod0,
278 const_has_ref_ptr_);
[email protected]b38d3572011-02-15 01:27:38279 method_cb.Run();
280 const_method_nonconst_obj_cb.Run();
281 const_method_const_obj_cb.Run();
282
283 Child child;
284 child.value = 0;
285 Closure virtual_set_cb = Bind(&Parent::VirtualSet, &child);
286 virtual_set_cb.Run();
287 EXPECT_EQ(kChildValue, child.value);
288
289 child.value = 0;
290 Closure non_virtual_set_cb = Bind(&Parent::NonVirtualSet, &child);
291 non_virtual_set_cb.Run();
292 EXPECT_EQ(kParentValue, child.value);
293}
294
295// Return value support.
296// - Function with return value.
297// - Method with return value.
298// - Const method with return value.
299TEST_F(BindTest, ReturnValues) {
300 EXPECT_CALL(static_func_mock_, IntMethod0()).WillOnce(Return(1337));
301 EXPECT_CALL(has_ref_, AddRef()).Times(3);
302 EXPECT_CALL(has_ref_, Release()).Times(3);
303 EXPECT_CALL(has_ref_, IntMethod0()).WillOnce(Return(31337));
304 EXPECT_CALL(has_ref_, IntConstMethod0())
305 .WillOnce(Return(41337))
306 .WillOnce(Return(51337));
307
308 Callback<int(void)> normal_cb = Bind(&IntFunc0);
309 Callback<int(void)> method_cb = Bind(&HasRef::IntMethod0, &has_ref_);
310 Callback<int(void)> const_method_nonconst_obj_cb =
311 Bind(&HasRef::IntConstMethod0, &has_ref_);
312 Callback<int(void)> const_method_const_obj_cb =
313 Bind(&HasRef::IntConstMethod0, const_has_ref_ptr_);
314 EXPECT_EQ(1337, normal_cb.Run());
315 EXPECT_EQ(31337, method_cb.Run());
316 EXPECT_EQ(41337, const_method_nonconst_obj_cb.Run());
317 EXPECT_EQ(51337, const_method_const_obj_cb.Run());
318}
319
320// Argument binding tests.
321// - Argument binding to primitive.
322// - Argument binding to primitive pointer.
323// - Argument binding to a literal integer.
324// - Argument binding to a literal string.
325// - Argument binding with template function.
326// - Argument binding to an object.
327// - Argument gets type converted.
328// - Pointer argument gets converted.
329// - Const Reference forces conversion.
330TEST_F(BindTest, ArgumentBinding) {
331 int n = 2;
332
333 Callback<int(void)> bind_primitive_cb = Bind(&Identity, n);
334 EXPECT_EQ(n, bind_primitive_cb.Run());
335
336 Callback<int*(void)> bind_primitive_pointer_cb =
337 Bind(&PolymorphicIdentity<int*>, &n);
338 EXPECT_EQ(&n, bind_primitive_pointer_cb.Run());
339
340 Callback<int(void)> bind_int_literal_cb = Bind(&Identity, 3);
341 EXPECT_EQ(3, bind_int_literal_cb.Run());
342
343 Callback<const char*(void)> bind_string_literal_cb =
344 Bind(&CStringIdentity, "hi");
345 EXPECT_STREQ("hi", bind_string_literal_cb.Run());
346
347 Callback<int(void)> bind_template_function_cb =
348 Bind(&PolymorphicIdentity<int>, 4);
349 EXPECT_EQ(4, bind_template_function_cb.Run());
350
351 NoRefParent p;
352 p.value = 5;
353 Callback<int(void)> bind_object_cb = Bind(&UnwrapNoRefParent, p);
354 EXPECT_EQ(5, bind_object_cb.Run());
355
356 NoRefChild c;
357 c.value = 6;
358 Callback<int(void)> bind_promotes_cb = Bind(&UnwrapNoRefParent, c);
359 EXPECT_EQ(6, bind_promotes_cb.Run());
360
361 c.value = 7;
362 Callback<int(void)> bind_pointer_promotes_cb =
363 Bind(&UnwrapNoRefParentPtr, &c);
364 EXPECT_EQ(7, bind_pointer_promotes_cb.Run());
365
366 c.value = 8;
367 Callback<int(void)> bind_const_reference_promotes_cb =
368 Bind(&UnwrapNoRefParentConstRef, c);
369 EXPECT_EQ(8, bind_const_reference_promotes_cb.Run());
370}
371
[email protected]c18b1052011-03-24 02:02:17372// Unbound argument type support tests.
373// - Unbound value.
374// - Unbound pointer.
375// - Unbound reference.
376// - Unbound const reference.
377// - Unbound unsized array.
378// - Unbound sized array.
379// - Unbound array-of-arrays.
380TEST_F(BindTest, UnboundArgumentTypeSupport) {
381 Callback<void(int)> unbound_value_cb = Bind(&VoidPolymorphic1<int>);
382 Callback<void(int*)> unbound_pointer_cb = Bind(&VoidPolymorphic1<int*>);
383 Callback<void(int&)> unbound_ref_cb = Bind(&VoidPolymorphic1<int&>);
384 Callback<void(const int&)> unbound_const_ref_cb =
385 Bind(&VoidPolymorphic1<const int&>);
386 Callback<void(int[])> unbound_unsized_array_cb =
387 Bind(&VoidPolymorphic1<int[]>);
388 Callback<void(int[2])> unbound_sized_array_cb =
389 Bind(&VoidPolymorphic1<int[2]>);
390 Callback<void(int[][2])> unbound_array_of_arrays_cb =
391 Bind(&VoidPolymorphic1<int[][2]>);
392}
393
394// Function with unbound reference parameter.
395// - Original paraemter is modified by callback.
396TEST_F(BindTest, UnboundReferenceSupport) {
397 int n = 0;
398 Callback<void(int&)> unbound_ref_cb = Bind(&RefArgSet);
399 unbound_ref_cb.Run(n);
400 EXPECT_EQ(2, n);
401}
402
[email protected]b38d3572011-02-15 01:27:38403// Functions that take reference parameters.
404// - Forced reference parameter type still stores a copy.
405// - Forced const reference parameter type still stores a copy.
406TEST_F(BindTest, ReferenceArgumentBinding) {
407 int n = 1;
408 int& ref_n = n;
409 const int& const_ref_n = n;
410
411 Callback<int(void)> ref_copies_cb = Bind(&Identity, ref_n);
412 EXPECT_EQ(n, ref_copies_cb.Run());
413 n++;
414 EXPECT_EQ(n - 1, ref_copies_cb.Run());
415
416 Callback<int(void)> const_ref_copies_cb = Bind(&Identity, const_ref_n);
417 EXPECT_EQ(n, const_ref_copies_cb.Run());
418 n++;
419 EXPECT_EQ(n - 1, const_ref_copies_cb.Run());
420}
421
422// Check that we can pass in arrays and have them be stored as a pointer.
423// - Array of values stores a pointer.
424// - Array of const values stores a pointer.
425TEST_F(BindTest, ArrayArgumentBinding) {
426 int array[4] = {1, 1, 1, 1};
427 const int (*const_array_ptr)[4] = &array;
428
429 Callback<int(void)> array_cb = Bind(&ArrayGet, array, 1);
430 EXPECT_EQ(1, array_cb.Run());
431
432 Callback<int(void)> const_array_cb = Bind(&ArrayGet, *const_array_ptr, 1);
433 EXPECT_EQ(1, const_array_cb.Run());
434
435 array[1] = 3;
436 EXPECT_EQ(3, array_cb.Run());
437 EXPECT_EQ(3, const_array_cb.Run());
438}
439
440// Verify SupportsAddRefAndRelease correctly introspects the class type for
441// AddRef() and Release().
[email protected]690bda882011-04-13 22:40:46442// - Class with AddRef() and Release()
443// - Class without AddRef() and Release()
444// - Derived Class with AddRef() and Release()
445// - Derived Class without AddRef() and Release()
446// - Derived Class with AddRef() and Release() and a private destructor.
[email protected]b38d3572011-02-15 01:27:38447TEST_F(BindTest, SupportsAddRefAndRelease) {
448 EXPECT_TRUE(internal::SupportsAddRefAndRelease<HasRef>::value);
449 EXPECT_FALSE(internal::SupportsAddRefAndRelease<NoRef>::value);
450
451 // StrictMock<T> is a derived class of T. So, we use StrictMock<HasRef> and
452 // StrictMock<NoRef> to test that SupportsAddRefAndRelease works over
453 // inheritance.
454 EXPECT_TRUE(internal::SupportsAddRefAndRelease<StrictMock<HasRef> >::value);
455 EXPECT_FALSE(internal::SupportsAddRefAndRelease<StrictMock<NoRef> >::value);
[email protected]690bda882011-04-13 22:40:46456
457 // This matters because the implementation creates a dummy class that
458 // inherits from the template type.
459 EXPECT_TRUE(internal::SupportsAddRefAndRelease<HasRefPrivateDtor>::value);
[email protected]b38d3572011-02-15 01:27:38460}
461
462// Unretained() wrapper support.
[email protected]93540582011-05-16 22:35:14463// - Method bound to Unretained() non-const object.
[email protected]b38d3572011-02-15 01:27:38464// - Const method bound to Unretained() non-const object.
465// - Const method bound to Unretained() const object.
466TEST_F(BindTest, Unretained) {
467 EXPECT_CALL(no_ref_, VoidMethod0());
468 EXPECT_CALL(no_ref_, VoidConstMethod0()).Times(2);
469
470 Callback<void(void)> method_cb =
471 Bind(&NoRef::VoidMethod0, Unretained(&no_ref_));
472 method_cb.Run();
473
474 Callback<void(void)> const_method_cb =
475 Bind(&NoRef::VoidConstMethod0, Unretained(&no_ref_));
476 const_method_cb.Run();
477
478 Callback<void(void)> const_method_const_ptr_cb =
479 Bind(&NoRef::VoidConstMethod0, Unretained(const_no_ref_ptr_));
480 const_method_const_ptr_cb.Run();
481}
482
[email protected]93540582011-05-16 22:35:14483// WeakPtr() support.
484// - Method bound to WeakPtr<> to non-const object.
485// - Const method bound to WeakPtr<> to non-const object.
486// - Const method bound to WeakPtr<> to const object.
487// - Normal Function with WeakPtr<> as P1 can have return type and is
488// not canceled.
489TEST_F(BindTest, WeakPtr) {
490 EXPECT_CALL(no_ref_, VoidMethod0());
491 EXPECT_CALL(no_ref_, VoidConstMethod0()).Times(2);
492
493 WeakPtrFactory<NoRef> weak_factory(&no_ref_);
494 WeakPtrFactory<const NoRef> const_weak_factory(const_no_ref_ptr_);
495
496 Callback<void(void)> method_cb =
497 Bind(&NoRef::VoidMethod0, weak_factory.GetWeakPtr());
498 method_cb.Run();
499
500 Callback<void(void)> const_method_cb =
501 Bind(&NoRef::VoidConstMethod0, const_weak_factory.GetWeakPtr());
502 const_method_cb.Run();
503
504 Callback<void(void)> const_method_const_ptr_cb =
505 Bind(&NoRef::VoidConstMethod0, const_weak_factory.GetWeakPtr());
506 const_method_const_ptr_cb.Run();
507
508 Callback<int(int)> normal_func_cb =
509 Bind(&FunctionWithWeakFirstParam, weak_factory.GetWeakPtr());
510 EXPECT_EQ(1, normal_func_cb.Run(1));
511
512 weak_factory.InvalidateWeakPtrs();
513 const_weak_factory.InvalidateWeakPtrs();
514
515 method_cb.Run();
516 const_method_cb.Run();
517 const_method_const_ptr_cb.Run();
518
519 // Still runs even after the pointers are invalidated.
520 EXPECT_EQ(2, normal_func_cb.Run(2));
521}
522
[email protected]b38d3572011-02-15 01:27:38523// ConstRef() wrapper support.
524// - Binding w/o ConstRef takes a copy.
525// - Binding a ConstRef takes a reference.
526// - Binding ConstRef to a function ConstRef does not copy on invoke.
527TEST_F(BindTest, ConstRef) {
528 int n = 1;
529
530 Callback<int(void)> copy_cb = Bind(&Identity, n);
531 Callback<int(void)> const_ref_cb = Bind(&Identity, ConstRef(n));
532 EXPECT_EQ(n, copy_cb.Run());
533 EXPECT_EQ(n, const_ref_cb.Run());
534 n++;
535 EXPECT_EQ(n - 1, copy_cb.Run());
536 EXPECT_EQ(n, const_ref_cb.Run());
537
538 int copies = 0;
539 int assigns = 0;
540 CopyCounter counter(&copies, &assigns);
541 Callback<int(void)> all_const_ref_cb =
542 Bind(&GetCopies, ConstRef(counter));
543 EXPECT_EQ(0, all_const_ref_cb.Run());
544 EXPECT_EQ(0, copies);
545 EXPECT_EQ(0, assigns);
546}
547
548// Argument Copy-constructor usage for non-reference parameters.
549// - Bound arguments are only copied once.
550// - Forwarded arguments are only copied once.
551// - Forwarded arguments with coerscions are only copied twice (once for the
552// coerscion, and one for the final dispatch).
553TEST_F(BindTest, ArgumentCopies) {
554 int copies = 0;
555 int assigns = 0;
556
557 CopyCounter counter(&copies, &assigns);
558
559 Callback<void(void)> copy_cb =
560 Bind(&VoidPolymorphic1<CopyCounter>, counter);
561 EXPECT_GE(1, copies);
562 EXPECT_EQ(0, assigns);
563
564 copies = 0;
565 assigns = 0;
566 Callback<void(CopyCounter)> forward_cb =
567 Bind(&VoidPolymorphic1<CopyCounter>);
568 forward_cb.Run(counter);
569 EXPECT_GE(1, copies);
570 EXPECT_EQ(0, assigns);
571
572 copies = 0;
573 assigns = 0;
574 DerivedCopyCounter dervied(&copies, &assigns);
575 Callback<void(CopyCounter)> coerce_cb =
576 Bind(&VoidPolymorphic1<CopyCounter>);
577 coerce_cb.Run(dervied);
578 EXPECT_GE(2, copies);
579 EXPECT_EQ(0, assigns);
580}
581
582// Callback construction and assignment tests.
583// - Construction from an InvokerStorageHolder should not cause ref/deref.
584// - Assignment from other callback should only cause one ref
585//
586// TODO(ajwong): Is there actually a way to test this?
587
[email protected]054ac7542011-02-27 01:25:59588#if defined(OS_WIN)
589int __fastcall FastCallFunc(int n) {
590 return n;
591}
592
593int __stdcall StdCallFunc(int n) {
594 return n;
595}
596
597// Windows specific calling convention support.
598// - Can bind a __fastcall function.
599// - Can bind a __stdcall function.
600TEST_F(BindTest, WindowsCallingConventions) {
601 Callback<int(void)> fastcall_cb = Bind(&FastCallFunc, 1);
602 EXPECT_EQ(1, fastcall_cb.Run());
603
604 Callback<int(void)> stdcall_cb = Bind(&StdCallFunc, 2);
605 EXPECT_EQ(2, stdcall_cb.Run());
606}
607#endif
608
[email protected]b38d3572011-02-15 01:27:38609} // namespace
610} // namespace base