-
Notifications
You must be signed in to change notification settings - Fork 0
/
bind_unittest.cc
1419 lines (1171 loc) · 43.5 KB
/
bind_unittest.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/bind.h"
#include <memory>
#include <utility>
#include <vector>
#include "base/callback.h"
#include "base/macros.h"
#include "base/memory/ptr_util.h"
#include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
#include "base/test/gtest_util.h"
#include "build/build_config.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using ::testing::_;
using ::testing::Mock;
using ::testing::ByMove;
using ::testing::Return;
using ::testing::StrictMock;
namespace base {
namespace {
class IncompleteType;
class NoRef {
public:
NoRef() {}
MOCK_METHOD0(VoidMethod0, void());
MOCK_CONST_METHOD0(VoidConstMethod0, void());
MOCK_METHOD0(IntMethod0, int());
MOCK_CONST_METHOD0(IntConstMethod0, int());
MOCK_METHOD1(VoidMethodWithIntArg, void(int));
MOCK_METHOD0(UniquePtrMethod0, std::unique_ptr<int>());
private:
// Particularly important in this test to ensure no copies are made.
DISALLOW_COPY_AND_ASSIGN(NoRef);
};
class HasRef : public NoRef {
public:
HasRef() {}
MOCK_CONST_METHOD0(AddRef, void());
MOCK_CONST_METHOD0(Release, bool());
private:
// Particularly important in this test to ensure no copies are made.
DISALLOW_COPY_AND_ASSIGN(HasRef);
};
class HasRefPrivateDtor : public HasRef {
private:
~HasRefPrivateDtor() {}
};
static const int kParentValue = 1;
static const int kChildValue = 2;
class Parent {
public:
void AddRef() const {}
void Release() const {}
virtual void VirtualSet() { value = kParentValue; }
void NonVirtualSet() { value = kParentValue; }
int value;
};
class Child : public Parent {
public:
void VirtualSet() override { value = kChildValue; }
void NonVirtualSet() { value = kChildValue; }
};
class NoRefParent {
public:
virtual void VirtualSet() { value = kParentValue; }
void NonVirtualSet() { value = kParentValue; }
int value;
};
class NoRefChild : public NoRefParent {
void VirtualSet() override { value = kChildValue; }
void NonVirtualSet() { value = kChildValue; }
};
// Used for probing the number of copies and moves that occur if a type must be
// coerced during argument forwarding in the Run() methods.
struct DerivedCopyMoveCounter {
DerivedCopyMoveCounter(int* copies,
int* assigns,
int* move_constructs,
int* move_assigns)
: copies_(copies),
assigns_(assigns),
move_constructs_(move_constructs),
move_assigns_(move_assigns) {}
int* copies_;
int* assigns_;
int* move_constructs_;
int* move_assigns_;
};
// Used for probing the number of copies and moves in an argument.
class CopyMoveCounter {
public:
CopyMoveCounter(int* copies,
int* assigns,
int* move_constructs,
int* move_assigns)
: copies_(copies),
assigns_(assigns),
move_constructs_(move_constructs),
move_assigns_(move_assigns) {}
CopyMoveCounter(const CopyMoveCounter& other)
: copies_(other.copies_),
assigns_(other.assigns_),
move_constructs_(other.move_constructs_),
move_assigns_(other.move_assigns_) {
(*copies_)++;
}
CopyMoveCounter(CopyMoveCounter&& other)
: copies_(other.copies_),
assigns_(other.assigns_),
move_constructs_(other.move_constructs_),
move_assigns_(other.move_assigns_) {
(*move_constructs_)++;
}
// Probing for copies from coercion.
explicit CopyMoveCounter(const DerivedCopyMoveCounter& other)
: copies_(other.copies_),
assigns_(other.assigns_),
move_constructs_(other.move_constructs_),
move_assigns_(other.move_assigns_) {
(*copies_)++;
}
// Probing for moves from coercion.
explicit CopyMoveCounter(DerivedCopyMoveCounter&& other)
: copies_(other.copies_),
assigns_(other.assigns_),
move_constructs_(other.move_constructs_),
move_assigns_(other.move_assigns_) {
(*move_constructs_)++;
}
const CopyMoveCounter& operator=(const CopyMoveCounter& rhs) {
copies_ = rhs.copies_;
assigns_ = rhs.assigns_;
move_constructs_ = rhs.move_constructs_;
move_assigns_ = rhs.move_assigns_;
(*assigns_)++;
return *this;
}
const CopyMoveCounter& operator=(CopyMoveCounter&& rhs) {
copies_ = rhs.copies_;
assigns_ = rhs.assigns_;
move_constructs_ = rhs.move_constructs_;
move_assigns_ = rhs.move_assigns_;
(*move_assigns_)++;
return *this;
}
int copies() const {
return *copies_;
}
private:
int* copies_;
int* assigns_;
int* move_constructs_;
int* move_assigns_;
};
// Used for probing the number of copies in an argument. The instance is a
// copyable and non-movable type.
class CopyCounter {
public:
CopyCounter(int* copies, int* assigns)
: counter_(copies, assigns, nullptr, nullptr) {}
CopyCounter(const CopyCounter& other) : counter_(other.counter_) {}
CopyCounter& operator=(const CopyCounter& other) {
counter_ = other.counter_;
return *this;
}
explicit CopyCounter(const DerivedCopyMoveCounter& other) : counter_(other) {}
int copies() const { return counter_.copies(); }
private:
CopyMoveCounter counter_;
};
// Used for probing the number of moves in an argument. The instance is a
// non-copyable and movable type.
class MoveCounter {
public:
MoveCounter(int* move_constructs, int* move_assigns)
: counter_(nullptr, nullptr, move_constructs, move_assigns) {}
MoveCounter(MoveCounter&& other) : counter_(std::move(other.counter_)) {}
MoveCounter& operator=(MoveCounter&& other) {
counter_ = std::move(other.counter_);
return *this;
}
explicit MoveCounter(DerivedCopyMoveCounter&& other)
: counter_(std::move(other)) {}
private:
CopyMoveCounter counter_;
};
class DeleteCounter {
public:
explicit DeleteCounter(int* deletes)
: deletes_(deletes) {
}
~DeleteCounter() {
(*deletes_)++;
}
void VoidMethod0() {}
private:
int* deletes_;
};
template <typename T>
T PassThru(T scoper) {
return scoper;
}
// Some test functions that we can Bind to.
template <typename T>
T PolymorphicIdentity(T t) {
return t;
}
template <typename... Ts>
struct VoidPolymorphic {
static void Run(Ts... t) {}
};
int Identity(int n) {
return n;
}
int ArrayGet(const int array[], int n) {
return array[n];
}
int Sum(int a, int b, int c, int d, int e, int f) {
return a + b + c + d + e + f;
}
const char* CStringIdentity(const char* s) {
return s;
}
int GetCopies(const CopyMoveCounter& counter) {
return counter.copies();
}
int UnwrapNoRefParent(NoRefParent p) {
return p.value;
}
int UnwrapNoRefParentPtr(NoRefParent* p) {
return p->value;
}
int UnwrapNoRefParentConstRef(const NoRefParent& p) {
return p.value;
}
void RefArgSet(int &n) {
n = 2;
}
void PtrArgSet(int *n) {
*n = 2;
}
int FunctionWithWeakFirstParam(WeakPtr<NoRef> o, int n) {
return n;
}
int FunctionWithScopedRefptrFirstParam(const scoped_refptr<HasRef>& o, int n) {
return n;
}
void TakesACallback(const Closure& callback) {
callback.Run();
}
class BindTest : public ::testing::Test {
public:
BindTest() {
const_has_ref_ptr_ = &has_ref_;
const_no_ref_ptr_ = &no_ref_;
static_func_mock_ptr = &static_func_mock_;
}
virtual ~BindTest() {
}
static void VoidFunc0() {
static_func_mock_ptr->VoidMethod0();
}
static int IntFunc0() { return static_func_mock_ptr->IntMethod0(); }
protected:
StrictMock<NoRef> no_ref_;
StrictMock<HasRef> has_ref_;
const HasRef* const_has_ref_ptr_;
const NoRef* const_no_ref_ptr_;
StrictMock<NoRef> static_func_mock_;
// Used by the static functions to perform expectations.
static StrictMock<NoRef>* static_func_mock_ptr;
private:
DISALLOW_COPY_AND_ASSIGN(BindTest);
};
StrictMock<NoRef>* BindTest::static_func_mock_ptr;
StrictMock<NoRef>* g_func_mock_ptr;
void VoidFunc0() {
g_func_mock_ptr->VoidMethod0();
}
int IntFunc0() {
return g_func_mock_ptr->IntMethod0();
}
TEST_F(BindTest, BasicTest) {
Callback<int(int, int, int)> cb = Bind(&Sum, 32, 16, 8);
EXPECT_EQ(92, cb.Run(13, 12, 11));
Callback<int(int, int, int, int, int, int)> c1 = Bind(&Sum);
EXPECT_EQ(69, c1.Run(14, 13, 12, 11, 10, 9));
Callback<int(int, int, int)> c2 = Bind(c1, 32, 16, 8);
EXPECT_EQ(86, c2.Run(11, 10, 9));
Callback<int()> c3 = Bind(c2, 4, 2, 1);
EXPECT_EQ(63, c3.Run());
}
// Test that currying the rvalue result of another Bind() works correctly.
// - rvalue should be usable as argument to Bind().
// - multiple runs of resulting Callback remain valid.
TEST_F(BindTest, CurryingRvalueResultOfBind) {
int n = 0;
RepeatingClosure cb = BindRepeating(&TakesACallback,
BindRepeating(&PtrArgSet, &n));
// If we implement Bind() such that the return value has auto_ptr-like
// semantics, the second call here will fail because ownership of
// the internal BindState<> would have been transfered to a *temporary*
// constructon of a Callback object on the first call.
cb.Run();
EXPECT_EQ(2, n);
n = 0;
cb.Run();
EXPECT_EQ(2, n);
}
TEST_F(BindTest, RepeatingCallbackBasicTest) {
RepeatingCallback<int(int)> c0 = BindRepeating(&Sum, 1, 2, 4, 8, 16);
// RepeatingCallback can run via a lvalue-reference.
EXPECT_EQ(63, c0.Run(32));
// It is valid to call a RepeatingCallback more than once.
EXPECT_EQ(54, c0.Run(23));
// BindRepeating can handle a RepeatingCallback as the target functor.
RepeatingCallback<int()> c1 = BindRepeating(c0, 11);
// RepeatingCallback can run via a rvalue-reference.
EXPECT_EQ(42, std::move(c1).Run());
// BindRepeating can handle a rvalue-reference of RepeatingCallback.
EXPECT_EQ(32, BindRepeating(std::move(c0), 1).Run());
}
TEST_F(BindTest, OnceCallbackBasicTest) {
OnceCallback<int(int)> c0 = BindOnce(&Sum, 1, 2, 4, 8, 16);
// OnceCallback can run via a rvalue-reference.
EXPECT_EQ(63, std::move(c0).Run(32));
// After running via the rvalue-reference, the value of the OnceCallback
// is undefined. The implementation simply clears the instance after the
// invocation.
EXPECT_TRUE(c0.is_null());
c0 = BindOnce(&Sum, 2, 3, 5, 7, 11);
// BindOnce can handle a rvalue-reference of OnceCallback as the target
// functor.
OnceCallback<int()> c1 = BindOnce(std::move(c0), 13);
EXPECT_EQ(41, std::move(c1).Run());
RepeatingCallback<int(int)> c2 = BindRepeating(&Sum, 2, 3, 5, 7, 11);
EXPECT_EQ(41, BindOnce(c2, 13).Run());
}
// IgnoreResult adapter test.
// - Function with return value.
// - Method with return value.
// - Const Method with return.
// - Method with return value bound to WeakPtr<>.
// - Const Method with return bound to WeakPtr<>.
TEST_F(BindTest, IgnoreResultForRepeating) {
EXPECT_CALL(static_func_mock_, IntMethod0()).WillOnce(Return(1337));
EXPECT_CALL(has_ref_, AddRef()).Times(2);
EXPECT_CALL(has_ref_, Release()).Times(2);
EXPECT_CALL(has_ref_, IntMethod0()).WillOnce(Return(10));
EXPECT_CALL(has_ref_, IntConstMethod0()).WillOnce(Return(11));
EXPECT_CALL(no_ref_, IntMethod0()).WillOnce(Return(12));
EXPECT_CALL(no_ref_, IntConstMethod0()).WillOnce(Return(13));
RepeatingClosure normal_func_cb = BindRepeating(IgnoreResult(&IntFunc0));
normal_func_cb.Run();
RepeatingClosure non_void_method_cb =
BindRepeating(IgnoreResult(&HasRef::IntMethod0), &has_ref_);
non_void_method_cb.Run();
RepeatingClosure non_void_const_method_cb =
BindRepeating(IgnoreResult(&HasRef::IntConstMethod0), &has_ref_);
non_void_const_method_cb.Run();
WeakPtrFactory<NoRef> weak_factory(&no_ref_);
WeakPtrFactory<const NoRef> const_weak_factory(const_no_ref_ptr_);
RepeatingClosure non_void_weak_method_cb =
BindRepeating(IgnoreResult(&NoRef::IntMethod0),
weak_factory.GetWeakPtr());
non_void_weak_method_cb.Run();
RepeatingClosure non_void_weak_const_method_cb =
BindRepeating(IgnoreResult(&NoRef::IntConstMethod0),
weak_factory.GetWeakPtr());
non_void_weak_const_method_cb.Run();
weak_factory.InvalidateWeakPtrs();
non_void_weak_const_method_cb.Run();
non_void_weak_method_cb.Run();
}
TEST_F(BindTest, IgnoreResultForOnce) {
EXPECT_CALL(static_func_mock_, IntMethod0()).WillOnce(Return(1337));
EXPECT_CALL(has_ref_, AddRef()).Times(2);
EXPECT_CALL(has_ref_, Release()).Times(2);
EXPECT_CALL(has_ref_, IntMethod0()).WillOnce(Return(10));
EXPECT_CALL(has_ref_, IntConstMethod0()).WillOnce(Return(11));
OnceClosure normal_func_cb = BindOnce(IgnoreResult(&IntFunc0));
std::move(normal_func_cb).Run();
OnceClosure non_void_method_cb =
BindOnce(IgnoreResult(&HasRef::IntMethod0), &has_ref_);
std::move(non_void_method_cb).Run();
OnceClosure non_void_const_method_cb =
BindOnce(IgnoreResult(&HasRef::IntConstMethod0), &has_ref_);
std::move(non_void_const_method_cb).Run();
WeakPtrFactory<NoRef> weak_factory(&no_ref_);
WeakPtrFactory<const NoRef> const_weak_factory(const_no_ref_ptr_);
OnceClosure non_void_weak_method_cb =
BindOnce(IgnoreResult(&NoRef::IntMethod0),
weak_factory.GetWeakPtr());
OnceClosure non_void_weak_const_method_cb =
BindOnce(IgnoreResult(&NoRef::IntConstMethod0),
weak_factory.GetWeakPtr());
weak_factory.InvalidateWeakPtrs();
std::move(non_void_weak_const_method_cb).Run();
std::move(non_void_weak_method_cb).Run();
}
// Functions that take reference parameters.
// - Forced reference parameter type still stores a copy.
// - Forced const reference parameter type still stores a copy.
TEST_F(BindTest, ReferenceArgumentBindingForRepeating) {
int n = 1;
int& ref_n = n;
const int& const_ref_n = n;
RepeatingCallback<int()> ref_copies_cb = BindRepeating(&Identity, ref_n);
EXPECT_EQ(n, ref_copies_cb.Run());
n++;
EXPECT_EQ(n - 1, ref_copies_cb.Run());
RepeatingCallback<int()> const_ref_copies_cb =
BindRepeating(&Identity, const_ref_n);
EXPECT_EQ(n, const_ref_copies_cb.Run());
n++;
EXPECT_EQ(n - 1, const_ref_copies_cb.Run());
}
TEST_F(BindTest, ReferenceArgumentBindingForOnce) {
int n = 1;
int& ref_n = n;
const int& const_ref_n = n;
OnceCallback<int()> ref_copies_cb = BindOnce(&Identity, ref_n);
n++;
EXPECT_EQ(n - 1, std::move(ref_copies_cb).Run());
OnceCallback<int()> const_ref_copies_cb =
BindOnce(&Identity, const_ref_n);
n++;
EXPECT_EQ(n - 1, std::move(const_ref_copies_cb).Run());
}
// Check that we can pass in arrays and have them be stored as a pointer.
// - Array of values stores a pointer.
// - Array of const values stores a pointer.
TEST_F(BindTest, ArrayArgumentBindingForRepeating) {
int array[4] = {1, 1, 1, 1};
const int (*const_array_ptr)[4] = &array;
RepeatingCallback<int()> array_cb = BindRepeating(&ArrayGet, array, 1);
EXPECT_EQ(1, array_cb.Run());
RepeatingCallback<int()> const_array_cb =
BindRepeating(&ArrayGet, *const_array_ptr, 1);
EXPECT_EQ(1, const_array_cb.Run());
array[1] = 3;
EXPECT_EQ(3, array_cb.Run());
EXPECT_EQ(3, const_array_cb.Run());
}
TEST_F(BindTest, ArrayArgumentBindingForOnce) {
int array[4] = {1, 1, 1, 1};
const int (*const_array_ptr)[4] = &array;
OnceCallback<int()> array_cb = BindOnce(&ArrayGet, array, 1);
OnceCallback<int()> const_array_cb =
BindOnce(&ArrayGet, *const_array_ptr, 1);
array[1] = 3;
EXPECT_EQ(3, std::move(array_cb).Run());
EXPECT_EQ(3, std::move(const_array_cb).Run());
}
// WeakPtr() support.
// - Method bound to WeakPtr<> to non-const object.
// - Const method bound to WeakPtr<> to non-const object.
// - Const method bound to WeakPtr<> to const object.
// - Normal Function with WeakPtr<> as P1 can have return type and is
// not canceled.
TEST_F(BindTest, WeakPtrForRepeating) {
EXPECT_CALL(no_ref_, VoidMethod0());
EXPECT_CALL(no_ref_, VoidConstMethod0()).Times(2);
WeakPtrFactory<NoRef> weak_factory(&no_ref_);
WeakPtrFactory<const NoRef> const_weak_factory(const_no_ref_ptr_);
RepeatingClosure method_cb =
BindRepeating(&NoRef::VoidMethod0, weak_factory.GetWeakPtr());
method_cb.Run();
RepeatingClosure const_method_cb =
BindRepeating(&NoRef::VoidConstMethod0, const_weak_factory.GetWeakPtr());
const_method_cb.Run();
RepeatingClosure const_method_const_ptr_cb =
BindRepeating(&NoRef::VoidConstMethod0, const_weak_factory.GetWeakPtr());
const_method_const_ptr_cb.Run();
RepeatingCallback<int(int)> normal_func_cb =
BindRepeating(&FunctionWithWeakFirstParam, weak_factory.GetWeakPtr());
EXPECT_EQ(1, normal_func_cb.Run(1));
weak_factory.InvalidateWeakPtrs();
const_weak_factory.InvalidateWeakPtrs();
method_cb.Run();
const_method_cb.Run();
const_method_const_ptr_cb.Run();
// Still runs even after the pointers are invalidated.
EXPECT_EQ(2, normal_func_cb.Run(2));
}
TEST_F(BindTest, WeakPtrForOnce) {
WeakPtrFactory<NoRef> weak_factory(&no_ref_);
WeakPtrFactory<const NoRef> const_weak_factory(const_no_ref_ptr_);
OnceClosure method_cb =
BindOnce(&NoRef::VoidMethod0, weak_factory.GetWeakPtr());
OnceClosure const_method_cb =
BindOnce(&NoRef::VoidConstMethod0, const_weak_factory.GetWeakPtr());
OnceClosure const_method_const_ptr_cb =
BindOnce(&NoRef::VoidConstMethod0, const_weak_factory.GetWeakPtr());
Callback<int(int)> normal_func_cb =
Bind(&FunctionWithWeakFirstParam, weak_factory.GetWeakPtr());
weak_factory.InvalidateWeakPtrs();
const_weak_factory.InvalidateWeakPtrs();
std::move(method_cb).Run();
std::move(const_method_cb).Run();
std::move(const_method_const_ptr_cb).Run();
// Still runs even after the pointers are invalidated.
EXPECT_EQ(2, std::move(normal_func_cb).Run(2));
}
// ConstRef() wrapper support.
// - Binding w/o ConstRef takes a copy.
// - Binding a ConstRef takes a reference.
// - Binding ConstRef to a function ConstRef does not copy on invoke.
TEST_F(BindTest, ConstRefForRepeating) {
int n = 1;
RepeatingCallback<int()> copy_cb = BindRepeating(&Identity, n);
RepeatingCallback<int()> const_ref_cb = BindRepeating(&Identity, ConstRef(n));
EXPECT_EQ(n, copy_cb.Run());
EXPECT_EQ(n, const_ref_cb.Run());
n++;
EXPECT_EQ(n - 1, copy_cb.Run());
EXPECT_EQ(n, const_ref_cb.Run());
int copies = 0;
int assigns = 0;
int move_constructs = 0;
int move_assigns = 0;
CopyMoveCounter counter(&copies, &assigns, &move_constructs, &move_assigns);
RepeatingCallback<int()> all_const_ref_cb =
BindRepeating(&GetCopies, ConstRef(counter));
EXPECT_EQ(0, all_const_ref_cb.Run());
EXPECT_EQ(0, copies);
EXPECT_EQ(0, assigns);
EXPECT_EQ(0, move_constructs);
EXPECT_EQ(0, move_assigns);
}
TEST_F(BindTest, ConstRefForOnce) {
int n = 1;
OnceCallback<int()> copy_cb = BindOnce(&Identity, n);
OnceCallback<int()> const_ref_cb = BindOnce(&Identity, ConstRef(n));
n++;
EXPECT_EQ(n - 1, std::move(copy_cb).Run());
EXPECT_EQ(n, std::move(const_ref_cb).Run());
int copies = 0;
int assigns = 0;
int move_constructs = 0;
int move_assigns = 0;
CopyMoveCounter counter(&copies, &assigns, &move_constructs, &move_assigns);
OnceCallback<int()> all_const_ref_cb =
BindOnce(&GetCopies, ConstRef(counter));
EXPECT_EQ(0, std::move(all_const_ref_cb).Run());
EXPECT_EQ(0, copies);
EXPECT_EQ(0, assigns);
EXPECT_EQ(0, move_constructs);
EXPECT_EQ(0, move_assigns);
}
// Test Owned() support.
TEST_F(BindTest, OwnedForRepeating) {
int deletes = 0;
DeleteCounter* counter = new DeleteCounter(&deletes);
// If we don't capture, delete happens on Callback destruction/reset.
// return the same value.
RepeatingCallback<DeleteCounter*()> no_capture_cb =
BindRepeating(&PolymorphicIdentity<DeleteCounter*>, Owned(counter));
ASSERT_EQ(counter, no_capture_cb.Run());
ASSERT_EQ(counter, no_capture_cb.Run());
EXPECT_EQ(0, deletes);
no_capture_cb.Reset(); // This should trigger a delete.
EXPECT_EQ(1, deletes);
deletes = 0;
counter = new DeleteCounter(&deletes);
RepeatingClosure own_object_cb =
BindRepeating(&DeleteCounter::VoidMethod0, Owned(counter));
own_object_cb.Run();
EXPECT_EQ(0, deletes);
own_object_cb.Reset();
EXPECT_EQ(1, deletes);
}
TEST_F(BindTest, OwnedForOnce) {
int deletes = 0;
DeleteCounter* counter = new DeleteCounter(&deletes);
// If we don't capture, delete happens on Callback destruction/reset.
// return the same value.
OnceCallback<DeleteCounter*()> no_capture_cb =
BindOnce(&PolymorphicIdentity<DeleteCounter*>, Owned(counter));
EXPECT_EQ(0, deletes);
no_capture_cb.Reset(); // This should trigger a delete.
EXPECT_EQ(1, deletes);
deletes = 0;
counter = new DeleteCounter(&deletes);
OnceClosure own_object_cb =
BindOnce(&DeleteCounter::VoidMethod0, Owned(counter));
EXPECT_EQ(0, deletes);
own_object_cb.Reset();
EXPECT_EQ(1, deletes);
}
template <typename T>
class BindVariantsTest : public ::testing::Test {
};
struct RepeatingTestConfig {
template <typename Signature>
using CallbackType = RepeatingCallback<Signature>;
using ClosureType = RepeatingClosure;
template <typename F, typename... Args>
static CallbackType<MakeUnboundRunType<F, Args...>>
Bind(F&& f, Args&&... args) {
return BindRepeating(std::forward<F>(f), std::forward<Args>(args)...);
}
};
struct OnceTestConfig {
template <typename Signature>
using CallbackType = OnceCallback<Signature>;
using ClosureType = OnceClosure;
template <typename F, typename... Args>
static CallbackType<MakeUnboundRunType<F, Args...>>
Bind(F&& f, Args&&... args) {
return BindOnce(std::forward<F>(f), std::forward<Args>(args)...);
}
};
using BindVariantsTestConfig = ::testing::Types<
RepeatingTestConfig, OnceTestConfig>;
TYPED_TEST_CASE(BindVariantsTest, BindVariantsTestConfig);
template <typename TypeParam, typename Signature>
using CallbackType = typename TypeParam::template CallbackType<Signature>;
// Function type support.
// - Normal function.
// - Normal function bound with non-refcounted first argument.
// - Method bound to non-const object.
// - Method bound to scoped_refptr.
// - Const method bound to non-const object.
// - Const method bound to const object.
// - Derived classes can be used with pointers to non-virtual base functions.
// - Derived classes can be used with pointers to virtual base functions (and
// preserve virtual dispatch).
TYPED_TEST(BindVariantsTest, FunctionTypeSupport) {
using ClosureType = typename TypeParam::ClosureType;
StrictMock<HasRef> has_ref;
StrictMock<NoRef> no_ref;
StrictMock<NoRef> static_func_mock;
const HasRef* const_has_ref_ptr = &has_ref;
g_func_mock_ptr = &static_func_mock;
EXPECT_CALL(static_func_mock, VoidMethod0());
EXPECT_CALL(has_ref, AddRef()).Times(4);
EXPECT_CALL(has_ref, Release()).Times(4);
EXPECT_CALL(has_ref, VoidMethod0()).Times(2);
EXPECT_CALL(has_ref, VoidConstMethod0()).Times(2);
ClosureType normal_cb = TypeParam::Bind(&VoidFunc0);
CallbackType<TypeParam, NoRef*()> normal_non_refcounted_cb =
TypeParam::Bind(&PolymorphicIdentity<NoRef*>, &no_ref);
std::move(normal_cb).Run();
EXPECT_EQ(&no_ref, std::move(normal_non_refcounted_cb).Run());
ClosureType method_cb = TypeParam::Bind(&HasRef::VoidMethod0, &has_ref);
ClosureType method_refptr_cb = TypeParam::Bind(&HasRef::VoidMethod0,
make_scoped_refptr(&has_ref));
ClosureType const_method_nonconst_obj_cb =
TypeParam::Bind(&HasRef::VoidConstMethod0, &has_ref);
ClosureType const_method_const_obj_cb =
TypeParam::Bind(&HasRef::VoidConstMethod0, const_has_ref_ptr);
std::move(method_cb).Run();
std::move(method_refptr_cb).Run();
std::move(const_method_nonconst_obj_cb).Run();
std::move(const_method_const_obj_cb).Run();
Child child;
child.value = 0;
ClosureType virtual_set_cb = TypeParam::Bind(&Parent::VirtualSet, &child);
std::move(virtual_set_cb).Run();
EXPECT_EQ(kChildValue, child.value);
child.value = 0;
ClosureType non_virtual_set_cb =
TypeParam::Bind(&Parent::NonVirtualSet, &child);
std::move(non_virtual_set_cb).Run();
EXPECT_EQ(kParentValue, child.value);
}
// Return value support.
// - Function with return value.
// - Method with return value.
// - Const method with return value.
// - Move-only return value.
TYPED_TEST(BindVariantsTest, ReturnValues) {
StrictMock<NoRef> static_func_mock;
StrictMock<HasRef> has_ref;
g_func_mock_ptr = &static_func_mock;
const HasRef* const_has_ref_ptr = &has_ref;
EXPECT_CALL(static_func_mock, IntMethod0()).WillOnce(Return(1337));
EXPECT_CALL(has_ref, AddRef()).Times(4);
EXPECT_CALL(has_ref, Release()).Times(4);
EXPECT_CALL(has_ref, IntMethod0()).WillOnce(Return(31337));
EXPECT_CALL(has_ref, IntConstMethod0())
.WillOnce(Return(41337))
.WillOnce(Return(51337));
EXPECT_CALL(has_ref, UniquePtrMethod0())
.WillOnce(Return(ByMove(MakeUnique<int>(42))));
CallbackType<TypeParam, int()> normal_cb = TypeParam::Bind(&IntFunc0);
CallbackType<TypeParam, int()> method_cb =
TypeParam::Bind(&HasRef::IntMethod0, &has_ref);
CallbackType<TypeParam, int()> const_method_nonconst_obj_cb =
TypeParam::Bind(&HasRef::IntConstMethod0, &has_ref);
CallbackType<TypeParam, int()> const_method_const_obj_cb =
TypeParam::Bind(&HasRef::IntConstMethod0, const_has_ref_ptr);
CallbackType<TypeParam, std::unique_ptr<int>()> move_only_rv_cb =
TypeParam::Bind(&HasRef::UniquePtrMethod0, &has_ref);
EXPECT_EQ(1337, std::move(normal_cb).Run());
EXPECT_EQ(31337, std::move(method_cb).Run());
EXPECT_EQ(41337, std::move(const_method_nonconst_obj_cb).Run());
EXPECT_EQ(51337, std::move(const_method_const_obj_cb).Run());
EXPECT_EQ(42, *std::move(move_only_rv_cb).Run());
}
// Argument binding tests.
// - Argument binding to primitive.
// - Argument binding to primitive pointer.
// - Argument binding to a literal integer.
// - Argument binding to a literal string.
// - Argument binding with template function.
// - Argument binding to an object.
// - Argument binding to pointer to incomplete type.
// - Argument gets type converted.
// - Pointer argument gets converted.
// - Const Reference forces conversion.
TYPED_TEST(BindVariantsTest, ArgumentBinding) {
int n = 2;
EXPECT_EQ(n, TypeParam::Bind(&Identity, n).Run());
EXPECT_EQ(&n, TypeParam::Bind(&PolymorphicIdentity<int*>, &n).Run());
EXPECT_EQ(3, TypeParam::Bind(&Identity, 3).Run());
EXPECT_STREQ("hi", TypeParam::Bind(&CStringIdentity, "hi").Run());
EXPECT_EQ(4, TypeParam::Bind(&PolymorphicIdentity<int>, 4).Run());
NoRefParent p;
p.value = 5;
EXPECT_EQ(5, TypeParam::Bind(&UnwrapNoRefParent, p).Run());
IncompleteType* incomplete_ptr = reinterpret_cast<IncompleteType*>(123);
EXPECT_EQ(incomplete_ptr,
TypeParam::Bind(&PolymorphicIdentity<IncompleteType*>,
incomplete_ptr).Run());
NoRefChild c;
c.value = 6;
EXPECT_EQ(6, TypeParam::Bind(&UnwrapNoRefParent, c).Run());
c.value = 7;
EXPECT_EQ(7, TypeParam::Bind(&UnwrapNoRefParentPtr, &c).Run());
c.value = 8;
EXPECT_EQ(8, TypeParam::Bind(&UnwrapNoRefParentConstRef, c).Run());
}
// Unbound argument type support tests.
// - Unbound value.
// - Unbound pointer.
// - Unbound reference.
// - Unbound const reference.
// - Unbound unsized array.
// - Unbound sized array.
// - Unbound array-of-arrays.
TYPED_TEST(BindVariantsTest, UnboundArgumentTypeSupport) {
CallbackType<TypeParam, void(int)> unbound_value_cb =
TypeParam::Bind(&VoidPolymorphic<int>::Run);
CallbackType<TypeParam, void(int*)> unbound_pointer_cb =
TypeParam::Bind(&VoidPolymorphic<int*>::Run);
CallbackType<TypeParam, void(int&)> unbound_ref_cb =
TypeParam::Bind(&VoidPolymorphic<int&>::Run);
CallbackType<TypeParam, void(const int&)> unbound_const_ref_cb =
TypeParam::Bind(&VoidPolymorphic<const int&>::Run);
CallbackType<TypeParam, void(int[])> unbound_unsized_array_cb =
TypeParam::Bind(&VoidPolymorphic<int[]>::Run);
CallbackType<TypeParam, void(int[2])> unbound_sized_array_cb =
TypeParam::Bind(&VoidPolymorphic<int[2]>::Run);
CallbackType<TypeParam, void(int[][2])> unbound_array_of_arrays_cb =
TypeParam::Bind(&VoidPolymorphic<int[][2]>::Run);
CallbackType<TypeParam, void(int&)> unbound_ref_with_bound_arg =
TypeParam::Bind(&VoidPolymorphic<int, int&>::Run, 1);
}
// Function with unbound reference parameter.
// - Original parameter is modified by callback.
TYPED_TEST(BindVariantsTest, UnboundReferenceSupport) {
int n = 0;
CallbackType<TypeParam, void(int&)> unbound_ref_cb =
TypeParam::Bind(&RefArgSet);
std::move(unbound_ref_cb).Run(n);
EXPECT_EQ(2, n);
}
// Unretained() wrapper support.
// - Method bound to Unretained() non-const object.
// - Const method bound to Unretained() non-const object.
// - Const method bound to Unretained() const object.
TYPED_TEST(BindVariantsTest, Unretained) {
StrictMock<NoRef> no_ref;
const NoRef* const_no_ref_ptr = &no_ref;
EXPECT_CALL(no_ref, VoidMethod0());
EXPECT_CALL(no_ref, VoidConstMethod0()).Times(2);
TypeParam::Bind(&NoRef::VoidMethod0, Unretained(&no_ref)).Run();
TypeParam::Bind(&NoRef::VoidConstMethod0, Unretained(&no_ref)).Run();
TypeParam::Bind(&NoRef::VoidConstMethod0, Unretained(const_no_ref_ptr)).Run();
}
TYPED_TEST(BindVariantsTest, ScopedRefptr) {
StrictMock<HasRef> has_ref;
EXPECT_CALL(has_ref, AddRef()).Times(1);
EXPECT_CALL(has_ref, Release()).Times(1);
const scoped_refptr<HasRef> refptr(&has_ref);
CallbackType<TypeParam, int()> scoped_refptr_const_ref_cb =
TypeParam::Bind(&FunctionWithScopedRefptrFirstParam,
base::ConstRef(refptr), 1);
EXPECT_EQ(1, std::move(scoped_refptr_const_ref_cb).Run());
}
TYPED_TEST(BindVariantsTest, UniquePtrReceiver) {
std::unique_ptr<StrictMock<NoRef>> no_ref(new StrictMock<NoRef>);
EXPECT_CALL(*no_ref, VoidMethod0()).Times(1);
TypeParam::Bind(&NoRef::VoidMethod0, std::move(no_ref)).Run();
}
// Tests for Passed() wrapper support:
// - Passed() can be constructed from a pointer to scoper.
// - Passed() can be constructed from a scoper rvalue.
// - Using Passed() gives Callback Ownership.
// - Ownership is transferred from Callback to callee on the first Run().
// - Callback supports unbound arguments.
template <typename T>
class BindMoveOnlyTypeTest : public ::testing::Test {
};
struct CustomDeleter {
void operator()(DeleteCounter* c) { delete c; }
};
using MoveOnlyTypesToTest =
::testing::Types<std::unique_ptr<DeleteCounter>,
std::unique_ptr<DeleteCounter, CustomDeleter>>;
TYPED_TEST_CASE(BindMoveOnlyTypeTest, MoveOnlyTypesToTest);
TYPED_TEST(BindMoveOnlyTypeTest, PassedToBoundCallback) {
int deletes = 0;
TypeParam ptr(new DeleteCounter(&deletes));