blob: 81f040adf649fae7eafd0d51714ac0d5a02c1d90 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2013 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * 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.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "api/proxy.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000012
kwibergd1fe2812016-04-27 06:47:29 -070013#include <memory>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000014#include <string>
15
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "rtc_base/gunit.h"
17#include "rtc_base/refcount.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "test/gmock.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000019
20using ::testing::_;
21using ::testing::DoAll;
22using ::testing::Exactly;
23using ::testing::InvokeWithoutArgs;
24using ::testing::Return;
25
26namespace webrtc {
27
28// Interface used for testing here.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000029class FakeInterface : public rtc::RefCountInterface {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000030 public:
31 virtual void VoidMethod0() = 0;
32 virtual std::string Method0() = 0;
33 virtual std::string ConstMethod0() const = 0;
34 virtual std::string Method1(std::string s) = 0;
35 virtual std::string ConstMethod1(std::string s) const = 0;
36 virtual std::string Method2(std::string s1, std::string s2) = 0;
37
38 protected:
deadbeefd99a2002017-01-18 08:55:23 -080039 virtual ~FakeInterface() {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +000040};
41
henrike@webrtc.org28e20752013-07-10 00:45:36 +000042// Implementation of the test interface.
43class Fake : public FakeInterface {
44 public:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000045 static rtc::scoped_refptr<Fake> Create() {
46 return new rtc::RefCountedObject<Fake>();
henrike@webrtc.org28e20752013-07-10 00:45:36 +000047 }
deadbeefd99a2002017-01-18 08:55:23 -080048 // Used to verify destructor is called on the correct thread.
49 MOCK_METHOD0(Destroy, void());
henrike@webrtc.org28e20752013-07-10 00:45:36 +000050
51 MOCK_METHOD0(VoidMethod0, void());
52 MOCK_METHOD0(Method0, std::string());
53 MOCK_CONST_METHOD0(ConstMethod0, std::string());
54
55 MOCK_METHOD1(Method1, std::string(std::string));
56 MOCK_CONST_METHOD1(ConstMethod1, std::string(std::string));
57
58 MOCK_METHOD2(Method2, std::string(std::string, std::string));
59
60 protected:
61 Fake() {}
deadbeefd99a2002017-01-18 08:55:23 -080062 ~Fake() { Destroy(); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000063};
64
nisse72c8d2b2016-04-15 03:49:07 -070065// Proxies for the test interface.
66BEGIN_PROXY_MAP(Fake)
deadbeefd99a2002017-01-18 08:55:23 -080067 PROXY_WORKER_THREAD_DESTRUCTOR()
nisse72c8d2b2016-04-15 03:49:07 -070068 PROXY_METHOD0(void, VoidMethod0)
69 PROXY_METHOD0(std::string, Method0)
70 PROXY_CONSTMETHOD0(std::string, ConstMethod0)
71 PROXY_WORKER_METHOD1(std::string, Method1, std::string)
72 PROXY_CONSTMETHOD1(std::string, ConstMethod1, std::string)
73 PROXY_WORKER_METHOD2(std::string, Method2, std::string, std::string)
deadbeefd99a2002017-01-18 08:55:23 -080074END_PROXY_MAP()
nisse72c8d2b2016-04-15 03:49:07 -070075
76// Preprocessor hack to get a proxy class a name different than FakeProxy.
77#define FakeProxy FakeSignalingProxy
deadbeefa601f5c2016-06-06 14:27:39 -070078#define FakeProxyWithInternal FakeSignalingProxyWithInternal
nisse72c8d2b2016-04-15 03:49:07 -070079BEGIN_SIGNALING_PROXY_MAP(Fake)
deadbeefd99a2002017-01-18 08:55:23 -080080 PROXY_SIGNALING_THREAD_DESTRUCTOR()
nisse72c8d2b2016-04-15 03:49:07 -070081 PROXY_METHOD0(void, VoidMethod0)
82 PROXY_METHOD0(std::string, Method0)
83 PROXY_CONSTMETHOD0(std::string, ConstMethod0)
84 PROXY_METHOD1(std::string, Method1, std::string)
85 PROXY_CONSTMETHOD1(std::string, ConstMethod1, std::string)
86 PROXY_METHOD2(std::string, Method2, std::string, std::string)
deadbeefd99a2002017-01-18 08:55:23 -080087END_PROXY_MAP()
nisse72c8d2b2016-04-15 03:49:07 -070088#undef FakeProxy
89
90class SignalingProxyTest : public testing::Test {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000091 public:
nisse72c8d2b2016-04-15 03:49:07 -070092 // Checks that the functions are called on the right thread.
93 void CheckSignalingThread() { EXPECT_TRUE(signaling_thread_->IsCurrent()); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000094
95 protected:
nisse72c8d2b2016-04-15 03:49:07 -070096 void SetUp() override {
tommie7251592017-07-14 14:44:46 -070097 signaling_thread_ = rtc::Thread::Create();
henrike@webrtc.org28e20752013-07-10 00:45:36 +000098 ASSERT_TRUE(signaling_thread_->Start());
99 fake_ = Fake::Create();
nisse72c8d2b2016-04-15 03:49:07 -0700100 fake_signaling_proxy_ =
101 FakeSignalingProxy::Create(signaling_thread_.get(), fake_.get());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000102 }
103
104 protected:
kwibergd1fe2812016-04-27 06:47:29 -0700105 std::unique_ptr<rtc::Thread> signaling_thread_;
nisse72c8d2b2016-04-15 03:49:07 -0700106 rtc::scoped_refptr<FakeInterface> fake_signaling_proxy_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000107 rtc::scoped_refptr<Fake> fake_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000108};
109
deadbeefd99a2002017-01-18 08:55:23 -0800110TEST_F(SignalingProxyTest, SignalingThreadDestructor) {
111 EXPECT_CALL(*fake_, Destroy())
112 .Times(Exactly(1))
113 .WillOnce(
114 InvokeWithoutArgs(this, &SignalingProxyTest::CheckSignalingThread));
115 fake_ = nullptr;
116 fake_signaling_proxy_ = nullptr;
117}
118
nisse72c8d2b2016-04-15 03:49:07 -0700119TEST_F(SignalingProxyTest, VoidMethod0) {
120 EXPECT_CALL(*fake_, VoidMethod0())
121 .Times(Exactly(1))
122 .WillOnce(
123 InvokeWithoutArgs(this, &SignalingProxyTest::CheckSignalingThread));
124 fake_signaling_proxy_->VoidMethod0();
125}
126
127TEST_F(SignalingProxyTest, Method0) {
128 EXPECT_CALL(*fake_, Method0())
129 .Times(Exactly(1))
130 .WillOnce(DoAll(
131 InvokeWithoutArgs(this, &SignalingProxyTest::CheckSignalingThread),
132 Return("Method0")));
133 EXPECT_EQ("Method0", fake_signaling_proxy_->Method0());
134}
135
136TEST_F(SignalingProxyTest, ConstMethod0) {
137 EXPECT_CALL(*fake_, ConstMethod0())
138 .Times(Exactly(1))
139 .WillOnce(DoAll(
140 InvokeWithoutArgs(this, &SignalingProxyTest::CheckSignalingThread),
141 Return("ConstMethod0")));
142 EXPECT_EQ("ConstMethod0", fake_signaling_proxy_->ConstMethod0());
143}
144
145TEST_F(SignalingProxyTest, Method1) {
146 const std::string arg1 = "arg1";
147 EXPECT_CALL(*fake_, Method1(arg1))
148 .Times(Exactly(1))
149 .WillOnce(DoAll(
150 InvokeWithoutArgs(this, &SignalingProxyTest::CheckSignalingThread),
151 Return("Method1")));
152 EXPECT_EQ("Method1", fake_signaling_proxy_->Method1(arg1));
153}
154
155TEST_F(SignalingProxyTest, ConstMethod1) {
156 const std::string arg1 = "arg1";
157 EXPECT_CALL(*fake_, ConstMethod1(arg1))
158 .Times(Exactly(1))
159 .WillOnce(DoAll(
160 InvokeWithoutArgs(this, &SignalingProxyTest::CheckSignalingThread),
161 Return("ConstMethod1")));
162 EXPECT_EQ("ConstMethod1", fake_signaling_proxy_->ConstMethod1(arg1));
163}
164
165TEST_F(SignalingProxyTest, Method2) {
166 const std::string arg1 = "arg1";
167 const std::string arg2 = "arg2";
168 EXPECT_CALL(*fake_, Method2(arg1, arg2))
169 .Times(Exactly(1))
170 .WillOnce(DoAll(
171 InvokeWithoutArgs(this, &SignalingProxyTest::CheckSignalingThread),
172 Return("Method2")));
173 EXPECT_EQ("Method2", fake_signaling_proxy_->Method2(arg1, arg2));
174}
175
deadbeefd99a2002017-01-18 08:55:23 -0800176class ProxyTest : public testing::Test {
nisse72c8d2b2016-04-15 03:49:07 -0700177 public:
178 // Checks that the functions are called on the right thread.
deadbeefd99a2002017-01-18 08:55:23 -0800179 void CheckSignalingThread() { EXPECT_TRUE(signaling_thread_->IsCurrent()); }
nisse72c8d2b2016-04-15 03:49:07 -0700180 void CheckWorkerThread() { EXPECT_TRUE(worker_thread_->IsCurrent()); }
181
182 protected:
183 void SetUp() override {
tommie7251592017-07-14 14:44:46 -0700184 signaling_thread_ = rtc::Thread::Create();
185 worker_thread_ = rtc::Thread::Create();
deadbeefd99a2002017-01-18 08:55:23 -0800186 ASSERT_TRUE(signaling_thread_->Start());
nisse72c8d2b2016-04-15 03:49:07 -0700187 ASSERT_TRUE(worker_thread_->Start());
deadbeefd99a2002017-01-18 08:55:23 -0800188 fake_ = Fake::Create();
nisse72c8d2b2016-04-15 03:49:07 -0700189 fake_proxy_ = FakeProxy::Create(signaling_thread_.get(),
190 worker_thread_.get(), fake_.get());
191 }
192
193 protected:
deadbeefd99a2002017-01-18 08:55:23 -0800194 std::unique_ptr<rtc::Thread> signaling_thread_;
kwibergd1fe2812016-04-27 06:47:29 -0700195 std::unique_ptr<rtc::Thread> worker_thread_;
nisse72c8d2b2016-04-15 03:49:07 -0700196 rtc::scoped_refptr<FakeInterface> fake_proxy_;
deadbeefd99a2002017-01-18 08:55:23 -0800197 rtc::scoped_refptr<Fake> fake_;
nisse72c8d2b2016-04-15 03:49:07 -0700198};
199
deadbeefd99a2002017-01-18 08:55:23 -0800200TEST_F(ProxyTest, WorkerThreadDestructor) {
201 EXPECT_CALL(*fake_, Destroy())
202 .Times(Exactly(1))
203 .WillOnce(InvokeWithoutArgs(this, &ProxyTest::CheckWorkerThread));
204 fake_ = nullptr;
205 fake_proxy_ = nullptr;
206}
207
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000208TEST_F(ProxyTest, VoidMethod0) {
209 EXPECT_CALL(*fake_, VoidMethod0())
nisse72c8d2b2016-04-15 03:49:07 -0700210 .Times(Exactly(1))
211 .WillOnce(InvokeWithoutArgs(this, &ProxyTest::CheckSignalingThread));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000212 fake_proxy_->VoidMethod0();
213}
214
215TEST_F(ProxyTest, Method0) {
216 EXPECT_CALL(*fake_, Method0())
nisse72c8d2b2016-04-15 03:49:07 -0700217 .Times(Exactly(1))
218 .WillOnce(
219 DoAll(InvokeWithoutArgs(this, &ProxyTest::CheckSignalingThread),
220 Return("Method0")));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000221 EXPECT_EQ("Method0",
222 fake_proxy_->Method0());
223}
224
225TEST_F(ProxyTest, ConstMethod0) {
226 EXPECT_CALL(*fake_, ConstMethod0())
nisse72c8d2b2016-04-15 03:49:07 -0700227 .Times(Exactly(1))
228 .WillOnce(
229 DoAll(InvokeWithoutArgs(this, &ProxyTest::CheckSignalingThread),
230 Return("ConstMethod0")));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000231 EXPECT_EQ("ConstMethod0",
232 fake_proxy_->ConstMethod0());
233}
234
nisse72c8d2b2016-04-15 03:49:07 -0700235TEST_F(ProxyTest, WorkerMethod1) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000236 const std::string arg1 = "arg1";
237 EXPECT_CALL(*fake_, Method1(arg1))
nisse72c8d2b2016-04-15 03:49:07 -0700238 .Times(Exactly(1))
239 .WillOnce(DoAll(InvokeWithoutArgs(this, &ProxyTest::CheckWorkerThread),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000240 Return("Method1")));
241 EXPECT_EQ("Method1", fake_proxy_->Method1(arg1));
242}
243
244TEST_F(ProxyTest, ConstMethod1) {
245 const std::string arg1 = "arg1";
246 EXPECT_CALL(*fake_, ConstMethod1(arg1))
nisse72c8d2b2016-04-15 03:49:07 -0700247 .Times(Exactly(1))
248 .WillOnce(
249 DoAll(InvokeWithoutArgs(this, &ProxyTest::CheckSignalingThread),
250 Return("ConstMethod1")));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000251 EXPECT_EQ("ConstMethod1", fake_proxy_->ConstMethod1(arg1));
252}
253
nisse72c8d2b2016-04-15 03:49:07 -0700254TEST_F(ProxyTest, WorkerMethod2) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000255 const std::string arg1 = "arg1";
256 const std::string arg2 = "arg2";
257 EXPECT_CALL(*fake_, Method2(arg1, arg2))
nisse72c8d2b2016-04-15 03:49:07 -0700258 .Times(Exactly(1))
259 .WillOnce(DoAll(InvokeWithoutArgs(this, &ProxyTest::CheckWorkerThread),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000260 Return("Method2")));
261 EXPECT_EQ("Method2", fake_proxy_->Method2(arg1, arg2));
262}
263
deadbeefd99a2002017-01-18 08:55:23 -0800264// Interface for testing OWNED_PROXY_MAP.
265class FooInterface {
266 public:
267 virtual ~FooInterface() {}
268 virtual void Bar() = 0;
269};
270
271class Foo : public FooInterface {
272 public:
273 Foo() {}
274 MOCK_METHOD0(Bar, void());
275};
276
277BEGIN_OWNED_PROXY_MAP(Foo)
278 PROXY_SIGNALING_THREAD_DESTRUCTOR()
279 PROXY_METHOD0(void, Bar)
280END_PROXY_MAP()
281
282class OwnedProxyTest : public testing::Test {
283 public:
284 OwnedProxyTest()
tommie7251592017-07-14 14:44:46 -0700285 : signaling_thread_(rtc::Thread::Create()),
286 worker_thread_(rtc::Thread::Create()),
287 foo_(new Foo()),
288 foo_proxy_(FooProxy::Create(signaling_thread_.get(),
289 worker_thread_.get(),
deadbeefe814a0d2017-02-25 18:15:09 -0800290 std::unique_ptr<FooInterface>(foo_))) {
tommie7251592017-07-14 14:44:46 -0700291 signaling_thread_->Start();
292 worker_thread_->Start();
deadbeefd99a2002017-01-18 08:55:23 -0800293 }
294
tommie7251592017-07-14 14:44:46 -0700295 void CheckSignalingThread() { EXPECT_TRUE(signaling_thread_->IsCurrent()); }
296 void CheckWorkerThread() { EXPECT_TRUE(worker_thread_->IsCurrent()); }
deadbeefd99a2002017-01-18 08:55:23 -0800297
298 protected:
tommie7251592017-07-14 14:44:46 -0700299 std::unique_ptr<rtc::Thread> signaling_thread_;
300 std::unique_ptr<rtc::Thread> worker_thread_;
deadbeefd99a2002017-01-18 08:55:23 -0800301 Foo* foo_; // Owned by foo_proxy_, not this class.
302 std::unique_ptr<FooInterface> foo_proxy_;
303};
304
305// Just tests that a method can be invoked using an "owned proxy" (as opposed
306// to normal ref-counted version).
307TEST_F(OwnedProxyTest, BasicTest) {
308 EXPECT_CALL(*foo_, Bar())
309 .Times(Exactly(1))
310 .WillOnce(InvokeWithoutArgs(this, &OwnedProxyTest::CheckSignalingThread));
311 foo_proxy_->Bar();
312}
313
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000314} // namespace webrtc