blob: 33315912bf2d95dbea6d3e1b44660e0c4b360c98 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
deadbeef08187d42017-02-25 11:21:18 -080011#include <type_traits>
12
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013#include "webrtc/base/bind.h"
14#include "webrtc/base/gunit.h"
15
Magnus Jedvertd3de9c52015-08-20 16:03:52 +020016#include "webrtc/base/refcount.h"
17
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000018namespace rtc {
19
20namespace {
21
Magnus Jedvertb2745472015-08-25 17:56:22 +020022struct LifeTimeCheck;
23
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000024struct MethodBindTester {
25 void NullaryVoid() { ++call_count; }
26 int NullaryInt() { ++call_count; return 1; }
27 int NullaryConst() const { ++call_count; return 2; }
28 void UnaryVoid(int dummy) { ++call_count; }
29 template <class T> T Identity(T value) { ++call_count; return value; }
noahric5d9b92b2015-10-24 11:14:46 -070030 int UnaryByPointer(int* value) const {
31 ++call_count;
32 return ++(*value);
33 }
34 int UnaryByRef(const int& value) const {
35 ++call_count;
36 return ++const_cast<int&>(value);
37 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000038 int Multiply(int a, int b) const { ++call_count; return a * b; }
Magnus Jedvertb2745472015-08-25 17:56:22 +020039 void RefArgument(const scoped_refptr<LifeTimeCheck>& object) {
40 EXPECT_TRUE(object.get() != nullptr);
41 }
42
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000043 mutable int call_count;
44};
45
Magnus Jedvertd3de9c52015-08-20 16:03:52 +020046struct A { int dummy; };
47struct B: public RefCountInterface { int dummy; };
48struct C: public A, B {};
49struct D {
50 int AddRef();
51};
52struct E: public D {
53 int Release();
54};
55struct F {
56 void AddRef();
57 void Release();
58};
59
Magnus Jedvertb2745472015-08-25 17:56:22 +020060struct LifeTimeCheck {
61 LifeTimeCheck() : ref_count_(0) {}
62 void AddRef() { ++ref_count_; }
63 void Release() { --ref_count_; }
Magnus Jedvertd3de9c52015-08-20 16:03:52 +020064 void NullaryVoid() {}
Magnus Jedvertb2745472015-08-25 17:56:22 +020065 int ref_count_;
Magnus Jedvertd3de9c52015-08-20 16:03:52 +020066};
67
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000068int Return42() { return 42; }
69int Negate(int a) { return -a; }
70int Multiply(int a, int b) { return a * b; }
71
72} // namespace
73
Magnus Jedvertd3de9c52015-08-20 16:03:52 +020074// Try to catch any problem with scoped_refptr type deduction in rtc::Bind at
75// compile time.
76#define EXPECT_IS_CAPTURED_AS_PTR(T) \
77 static_assert(is_same<detail::PointerType<T>::type, T*>::value, \
78 "PointerType")
79#define EXPECT_IS_CAPTURED_AS_SCOPED_REFPTR(T) \
80 static_assert( \
81 is_same<detail::PointerType<T>::type, scoped_refptr<T>>::value, \
82 "PointerType")
83
84EXPECT_IS_CAPTURED_AS_PTR(void);
85EXPECT_IS_CAPTURED_AS_PTR(int);
86EXPECT_IS_CAPTURED_AS_PTR(double);
87EXPECT_IS_CAPTURED_AS_PTR(A);
88EXPECT_IS_CAPTURED_AS_PTR(D);
89EXPECT_IS_CAPTURED_AS_PTR(RefCountInterface*);
90
91EXPECT_IS_CAPTURED_AS_SCOPED_REFPTR(RefCountInterface);
92EXPECT_IS_CAPTURED_AS_SCOPED_REFPTR(B);
93EXPECT_IS_CAPTURED_AS_SCOPED_REFPTR(C);
94EXPECT_IS_CAPTURED_AS_SCOPED_REFPTR(E);
95EXPECT_IS_CAPTURED_AS_SCOPED_REFPTR(F);
96EXPECT_IS_CAPTURED_AS_SCOPED_REFPTR(RefCountedObject<RefCountInterface>);
97EXPECT_IS_CAPTURED_AS_SCOPED_REFPTR(RefCountedObject<B>);
98EXPECT_IS_CAPTURED_AS_SCOPED_REFPTR(RefCountedObject<C>);
Magnus Jedvert1b40a9a2015-10-12 15:50:43 +020099EXPECT_IS_CAPTURED_AS_SCOPED_REFPTR(const RefCountedObject<RefCountInterface>);
Magnus Jedvertd3de9c52015-08-20 16:03:52 +0200100
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000101TEST(BindTest, BindToMethod) {
102 MethodBindTester object = {0};
103 EXPECT_EQ(0, object.call_count);
104 Bind(&MethodBindTester::NullaryVoid, &object)();
105 EXPECT_EQ(1, object.call_count);
106 EXPECT_EQ(1, Bind(&MethodBindTester::NullaryInt, &object)());
107 EXPECT_EQ(2, object.call_count);
108 EXPECT_EQ(2, Bind(&MethodBindTester::NullaryConst,
109 static_cast<const MethodBindTester*>(&object))());
110 EXPECT_EQ(3, object.call_count);
111 Bind(&MethodBindTester::UnaryVoid, &object, 5)();
112 EXPECT_EQ(4, object.call_count);
113 EXPECT_EQ(100, Bind(&MethodBindTester::Identity<int>, &object, 100)());
114 EXPECT_EQ(5, object.call_count);
115 const std::string string_value("test string");
116 EXPECT_EQ(string_value, Bind(&MethodBindTester::Identity<std::string>,
117 &object, string_value)());
118 EXPECT_EQ(6, object.call_count);
119 int value = 11;
noahric5d9b92b2015-10-24 11:14:46 -0700120 // Bind binds by value, even if the method signature is by reference, so
121 // "reference" binds require pointers.
122 EXPECT_EQ(12, Bind(&MethodBindTester::UnaryByPointer, &object, &value)());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000123 EXPECT_EQ(12, value);
124 EXPECT_EQ(7, object.call_count);
noahric5d9b92b2015-10-24 11:14:46 -0700125 // It's possible to bind to a function that takes a const reference, though
126 // the capture will be a copy. See UnaryByRef hackery above where it removes
127 // the const to make sure the underlying storage is, in fact, a copy.
128 EXPECT_EQ(13, Bind(&MethodBindTester::UnaryByRef, &object, value)());
129 // But the original value is unmodified.
130 EXPECT_EQ(12, value);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000131 EXPECT_EQ(8, object.call_count);
noahric5d9b92b2015-10-24 11:14:46 -0700132 EXPECT_EQ(56, Bind(&MethodBindTester::Multiply, &object, 7, 8)());
133 EXPECT_EQ(9, object.call_count);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000134}
135
136TEST(BindTest, BindToFunction) {
137 EXPECT_EQ(42, Bind(&Return42)());
138 EXPECT_EQ(3, Bind(&Negate, -3)());
139 EXPECT_EQ(56, Bind(&Multiply, 8, 7)());
140}
141
Magnus Jedvertd3de9c52015-08-20 16:03:52 +0200142// Test Bind where method object implements RefCountInterface and is passed as a
143// pointer.
144TEST(BindTest, CapturePointerAsScopedRefPtr) {
Magnus Jedvertb2745472015-08-25 17:56:22 +0200145 LifeTimeCheck object;
146 EXPECT_EQ(object.ref_count_, 0);
147 scoped_refptr<LifeTimeCheck> scoped_object(&object);
148 EXPECT_EQ(object.ref_count_, 1);
Magnus Jedvertd3de9c52015-08-20 16:03:52 +0200149 {
Magnus Jedvertb2745472015-08-25 17:56:22 +0200150 auto functor = Bind(&LifeTimeCheck::NullaryVoid, &object);
151 EXPECT_EQ(object.ref_count_, 2);
152 scoped_object = nullptr;
153 EXPECT_EQ(object.ref_count_, 1);
Magnus Jedvertd3de9c52015-08-20 16:03:52 +0200154 }
Magnus Jedvertb2745472015-08-25 17:56:22 +0200155 EXPECT_EQ(object.ref_count_, 0);
Magnus Jedvertd3de9c52015-08-20 16:03:52 +0200156}
157
158// Test Bind where method object implements RefCountInterface and is passed as a
159// scoped_refptr<>.
160TEST(BindTest, CaptureScopedRefPtrAsScopedRefPtr) {
Magnus Jedvertb2745472015-08-25 17:56:22 +0200161 LifeTimeCheck object;
162 EXPECT_EQ(object.ref_count_, 0);
163 scoped_refptr<LifeTimeCheck> scoped_object(&object);
164 EXPECT_EQ(object.ref_count_, 1);
Magnus Jedvertd3de9c52015-08-20 16:03:52 +0200165 {
Magnus Jedvertb2745472015-08-25 17:56:22 +0200166 auto functor = Bind(&LifeTimeCheck::NullaryVoid, scoped_object);
167 EXPECT_EQ(object.ref_count_, 2);
168 scoped_object = nullptr;
169 EXPECT_EQ(object.ref_count_, 1);
Magnus Jedvertd3de9c52015-08-20 16:03:52 +0200170 }
Magnus Jedvertb2745472015-08-25 17:56:22 +0200171 EXPECT_EQ(object.ref_count_, 0);
Magnus Jedvertd3de9c52015-08-20 16:03:52 +0200172}
173
174// Test Bind where method object is captured as scoped_refptr<> and the functor
175// dies while there are references left.
176TEST(BindTest, FunctorReleasesObjectOnDestruction) {
Magnus Jedvertb2745472015-08-25 17:56:22 +0200177 LifeTimeCheck object;
178 EXPECT_EQ(object.ref_count_, 0);
179 scoped_refptr<LifeTimeCheck> scoped_object(&object);
180 EXPECT_EQ(object.ref_count_, 1);
181 Bind(&LifeTimeCheck::NullaryVoid, &object)();
182 EXPECT_EQ(object.ref_count_, 1);
183 scoped_object = nullptr;
184 EXPECT_EQ(object.ref_count_, 0);
185}
186
187// Test Bind with scoped_refptr<> argument.
188TEST(BindTest, ScopedRefPointerArgument) {
189 LifeTimeCheck object;
190 EXPECT_EQ(object.ref_count_, 0);
191 scoped_refptr<LifeTimeCheck> scoped_object(&object);
192 EXPECT_EQ(object.ref_count_, 1);
193 {
194 MethodBindTester bind_tester;
195 auto functor =
196 Bind(&MethodBindTester::RefArgument, &bind_tester, scoped_object);
197 EXPECT_EQ(object.ref_count_, 2);
198 }
199 EXPECT_EQ(object.ref_count_, 1);
200 scoped_object = nullptr;
201 EXPECT_EQ(object.ref_count_, 0);
202}
203
204namespace {
205
206const int* Ref(const int& a) { return &a; }
207
208} // anonymous namespace
209
noahric5d9b92b2015-10-24 11:14:46 -0700210// Test Bind with non-scoped_refptr<> reference argument, which should be
211// modified to a non-reference capture.
Magnus Jedvertb2745472015-08-25 17:56:22 +0200212TEST(BindTest, RefArgument) {
213 const int x = 42;
noahric5d9b92b2015-10-24 11:14:46 -0700214 EXPECT_EQ(&x, Ref(x));
215 // Bind() should make a copy of |x|, i.e. the pointers should be different.
Magnus Jedvertb2745472015-08-25 17:56:22 +0200216 auto functor = Bind(&Ref, x);
noahric5d9b92b2015-10-24 11:14:46 -0700217 EXPECT_NE(&x, functor());
Magnus Jedvertd3de9c52015-08-20 16:03:52 +0200218}
219
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000220} // namespace rtc