blob: fa47d279f5da271005dd30b44ee02e7644cdb0b9 [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
11#include "webrtc/base/bind.h"
12#include "webrtc/base/gunit.h"
13
Magnus Jedvertd3de9c52015-08-20 16:03:52 +020014#include "webrtc/base/refcount.h"
15
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000016namespace rtc {
17
18namespace {
19
Magnus Jedvertb2745472015-08-25 17:56:22 +020020struct LifeTimeCheck;
21
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000022struct MethodBindTester {
23 void NullaryVoid() { ++call_count; }
24 int NullaryInt() { ++call_count; return 1; }
25 int NullaryConst() const { ++call_count; return 2; }
26 void UnaryVoid(int dummy) { ++call_count; }
27 template <class T> T Identity(T value) { ++call_count; return value; }
28 int UnaryByRef(int& value) const { ++call_count; return ++value; } // NOLINT
29 int Multiply(int a, int b) const { ++call_count; return a * b; }
Magnus Jedvertb2745472015-08-25 17:56:22 +020030 void RefArgument(const scoped_refptr<LifeTimeCheck>& object) {
31 EXPECT_TRUE(object.get() != nullptr);
32 }
33
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000034 mutable int call_count;
35};
36
Magnus Jedvertd3de9c52015-08-20 16:03:52 +020037struct A { int dummy; };
38struct B: public RefCountInterface { int dummy; };
39struct C: public A, B {};
40struct D {
41 int AddRef();
42};
43struct E: public D {
44 int Release();
45};
46struct F {
47 void AddRef();
48 void Release();
49};
50
Magnus Jedvertb2745472015-08-25 17:56:22 +020051struct LifeTimeCheck {
52 LifeTimeCheck() : ref_count_(0) {}
53 void AddRef() { ++ref_count_; }
54 void Release() { --ref_count_; }
Magnus Jedvertd3de9c52015-08-20 16:03:52 +020055 void NullaryVoid() {}
Magnus Jedvertb2745472015-08-25 17:56:22 +020056 int ref_count_;
Magnus Jedvertd3de9c52015-08-20 16:03:52 +020057};
58
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000059int Return42() { return 42; }
60int Negate(int a) { return -a; }
61int Multiply(int a, int b) { return a * b; }
62
63} // namespace
64
Magnus Jedvertd3de9c52015-08-20 16:03:52 +020065// Try to catch any problem with scoped_refptr type deduction in rtc::Bind at
66// compile time.
Magnus Jedvertb2745472015-08-25 17:56:22 +020067static_assert(is_same<detail::RemoveScopedPtrRef<
68 const scoped_refptr<RefCountInterface>&>::type,
69 scoped_refptr<RefCountInterface>>::value,
70 "const scoped_refptr& should be captured by value");
71
72static_assert(is_same<detail::RemoveScopedPtrRef<const scoped_refptr<F>&>::type,
73 scoped_refptr<F>>::value,
74 "const scoped_refptr& should be captured by value");
75
76static_assert(
77 is_same<detail::RemoveScopedPtrRef<const int&>::type, const int&>::value,
78 "const int& should be captured as const int&");
79
80static_assert(
81 is_same<detail::RemoveScopedPtrRef<const F&>::type, const F&>::value,
82 "const F& should be captured as const F&");
83
84static_assert(
85 is_same<detail::RemoveScopedPtrRef<F&>::type, F&>::value,
86 "F& should be captured as F&");
87
Magnus Jedvertd3de9c52015-08-20 16:03:52 +020088#define EXPECT_IS_CAPTURED_AS_PTR(T) \
89 static_assert(is_same<detail::PointerType<T>::type, T*>::value, \
90 "PointerType")
91#define EXPECT_IS_CAPTURED_AS_SCOPED_REFPTR(T) \
92 static_assert( \
93 is_same<detail::PointerType<T>::type, scoped_refptr<T>>::value, \
94 "PointerType")
95
96EXPECT_IS_CAPTURED_AS_PTR(void);
97EXPECT_IS_CAPTURED_AS_PTR(int);
98EXPECT_IS_CAPTURED_AS_PTR(double);
99EXPECT_IS_CAPTURED_AS_PTR(A);
100EXPECT_IS_CAPTURED_AS_PTR(D);
101EXPECT_IS_CAPTURED_AS_PTR(RefCountInterface*);
102
103EXPECT_IS_CAPTURED_AS_SCOPED_REFPTR(RefCountInterface);
104EXPECT_IS_CAPTURED_AS_SCOPED_REFPTR(B);
105EXPECT_IS_CAPTURED_AS_SCOPED_REFPTR(C);
106EXPECT_IS_CAPTURED_AS_SCOPED_REFPTR(E);
107EXPECT_IS_CAPTURED_AS_SCOPED_REFPTR(F);
108EXPECT_IS_CAPTURED_AS_SCOPED_REFPTR(RefCountedObject<RefCountInterface>);
109EXPECT_IS_CAPTURED_AS_SCOPED_REFPTR(RefCountedObject<B>);
110EXPECT_IS_CAPTURED_AS_SCOPED_REFPTR(RefCountedObject<C>);
Magnus Jedvert1b40a9a2015-10-12 15:50:43 +0200111EXPECT_IS_CAPTURED_AS_SCOPED_REFPTR(const RefCountedObject<RefCountInterface>);
Magnus Jedvertd3de9c52015-08-20 16:03:52 +0200112
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000113TEST(BindTest, BindToMethod) {
114 MethodBindTester object = {0};
115 EXPECT_EQ(0, object.call_count);
116 Bind(&MethodBindTester::NullaryVoid, &object)();
117 EXPECT_EQ(1, object.call_count);
118 EXPECT_EQ(1, Bind(&MethodBindTester::NullaryInt, &object)());
119 EXPECT_EQ(2, object.call_count);
120 EXPECT_EQ(2, Bind(&MethodBindTester::NullaryConst,
121 static_cast<const MethodBindTester*>(&object))());
122 EXPECT_EQ(3, object.call_count);
123 Bind(&MethodBindTester::UnaryVoid, &object, 5)();
124 EXPECT_EQ(4, object.call_count);
125 EXPECT_EQ(100, Bind(&MethodBindTester::Identity<int>, &object, 100)());
126 EXPECT_EQ(5, object.call_count);
127 const std::string string_value("test string");
128 EXPECT_EQ(string_value, Bind(&MethodBindTester::Identity<std::string>,
129 &object, string_value)());
130 EXPECT_EQ(6, object.call_count);
131 int value = 11;
132 EXPECT_EQ(12, Bind(&MethodBindTester::UnaryByRef, &object, value)());
133 EXPECT_EQ(12, value);
134 EXPECT_EQ(7, object.call_count);
135 EXPECT_EQ(56, Bind(&MethodBindTester::Multiply, &object, 7, 8)());
136 EXPECT_EQ(8, object.call_count);
137}
138
139TEST(BindTest, BindToFunction) {
140 EXPECT_EQ(42, Bind(&Return42)());
141 EXPECT_EQ(3, Bind(&Negate, -3)());
142 EXPECT_EQ(56, Bind(&Multiply, 8, 7)());
143}
144
Magnus Jedvertd3de9c52015-08-20 16:03:52 +0200145// Test Bind where method object implements RefCountInterface and is passed as a
146// pointer.
147TEST(BindTest, CapturePointerAsScopedRefPtr) {
Magnus Jedvertb2745472015-08-25 17:56:22 +0200148 LifeTimeCheck object;
149 EXPECT_EQ(object.ref_count_, 0);
150 scoped_refptr<LifeTimeCheck> scoped_object(&object);
151 EXPECT_EQ(object.ref_count_, 1);
Magnus Jedvertd3de9c52015-08-20 16:03:52 +0200152 {
Magnus Jedvertb2745472015-08-25 17:56:22 +0200153 auto functor = Bind(&LifeTimeCheck::NullaryVoid, &object);
154 EXPECT_EQ(object.ref_count_, 2);
155 scoped_object = nullptr;
156 EXPECT_EQ(object.ref_count_, 1);
Magnus Jedvertd3de9c52015-08-20 16:03:52 +0200157 }
Magnus Jedvertb2745472015-08-25 17:56:22 +0200158 EXPECT_EQ(object.ref_count_, 0);
Magnus Jedvertd3de9c52015-08-20 16:03:52 +0200159}
160
161// Test Bind where method object implements RefCountInterface and is passed as a
162// scoped_refptr<>.
163TEST(BindTest, CaptureScopedRefPtrAsScopedRefPtr) {
Magnus Jedvertb2745472015-08-25 17:56:22 +0200164 LifeTimeCheck object;
165 EXPECT_EQ(object.ref_count_, 0);
166 scoped_refptr<LifeTimeCheck> scoped_object(&object);
167 EXPECT_EQ(object.ref_count_, 1);
Magnus Jedvertd3de9c52015-08-20 16:03:52 +0200168 {
Magnus Jedvertb2745472015-08-25 17:56:22 +0200169 auto functor = Bind(&LifeTimeCheck::NullaryVoid, scoped_object);
170 EXPECT_EQ(object.ref_count_, 2);
171 scoped_object = nullptr;
172 EXPECT_EQ(object.ref_count_, 1);
Magnus Jedvertd3de9c52015-08-20 16:03:52 +0200173 }
Magnus Jedvertb2745472015-08-25 17:56:22 +0200174 EXPECT_EQ(object.ref_count_, 0);
Magnus Jedvertd3de9c52015-08-20 16:03:52 +0200175}
176
177// Test Bind where method object is captured as scoped_refptr<> and the functor
178// dies while there are references left.
179TEST(BindTest, FunctorReleasesObjectOnDestruction) {
Magnus Jedvertb2745472015-08-25 17:56:22 +0200180 LifeTimeCheck object;
181 EXPECT_EQ(object.ref_count_, 0);
182 scoped_refptr<LifeTimeCheck> scoped_object(&object);
183 EXPECT_EQ(object.ref_count_, 1);
184 Bind(&LifeTimeCheck::NullaryVoid, &object)();
185 EXPECT_EQ(object.ref_count_, 1);
186 scoped_object = nullptr;
187 EXPECT_EQ(object.ref_count_, 0);
188}
189
190// Test Bind with scoped_refptr<> argument.
191TEST(BindTest, ScopedRefPointerArgument) {
192 LifeTimeCheck object;
193 EXPECT_EQ(object.ref_count_, 0);
194 scoped_refptr<LifeTimeCheck> scoped_object(&object);
195 EXPECT_EQ(object.ref_count_, 1);
196 {
197 MethodBindTester bind_tester;
198 auto functor =
199 Bind(&MethodBindTester::RefArgument, &bind_tester, scoped_object);
200 EXPECT_EQ(object.ref_count_, 2);
201 }
202 EXPECT_EQ(object.ref_count_, 1);
203 scoped_object = nullptr;
204 EXPECT_EQ(object.ref_count_, 0);
205}
206
207namespace {
208
209const int* Ref(const int& a) { return &a; }
210
211} // anonymous namespace
212
213// Test Bind with non-scoped_refptr<> reference argument.
214TEST(BindTest, RefArgument) {
215 const int x = 42;
216 EXPECT_TRUE(Ref(x) == &x);
217 // Bind() should not make a copy of |x|, i.e. the pointers should be the same.
218 auto functor = Bind(&Ref, x);
219 EXPECT_TRUE(functor() == &x);
Magnus Jedvertd3de9c52015-08-20 16:03:52 +0200220}
221
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000222} // namespace rtc