blob: a00b47ff6b69bd7df2f77460961dbd553be2552b [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"
Steve Anton10542f22019-01-11 09:11:00 -080017#include "rtc_base/ref_count.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)
Yves Gerey665174f2018-06-19 15:03:05 +020067PROXY_WORKER_THREAD_DESTRUCTOR()
68PROXY_METHOD0(void, VoidMethod0)
69PROXY_METHOD0(std::string, Method0)
70PROXY_CONSTMETHOD0(std::string, ConstMethod0)
71PROXY_WORKER_METHOD1(std::string, Method1, std::string)
72PROXY_CONSTMETHOD1(std::string, ConstMethod1, std::string)
73PROXY_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)
Yves Gerey665174f2018-06-19 15:03:05 +020080PROXY_SIGNALING_THREAD_DESTRUCTOR()
81PROXY_METHOD0(void, VoidMethod0)
82PROXY_METHOD0(std::string, Method0)
83PROXY_CONSTMETHOD0(std::string, ConstMethod0)
84PROXY_METHOD1(std::string, Method1, std::string)
85PROXY_CONSTMETHOD1(std::string, ConstMethod1, std::string)
86PROXY_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
Mirko Bonadei6a489f22019-04-09 15:11:12 +020090class 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
Mirko Bonadei6a489f22019-04-09 15:11:12 +0200176class 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))
Yves Gerey665174f2018-06-19 15:03:05 +0200218 .WillOnce(DoAll(InvokeWithoutArgs(this, &ProxyTest::CheckSignalingThread),
219 Return("Method0")));
220 EXPECT_EQ("Method0", fake_proxy_->Method0());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000221}
222
223TEST_F(ProxyTest, ConstMethod0) {
224 EXPECT_CALL(*fake_, ConstMethod0())
nisse72c8d2b2016-04-15 03:49:07 -0700225 .Times(Exactly(1))
Yves Gerey665174f2018-06-19 15:03:05 +0200226 .WillOnce(DoAll(InvokeWithoutArgs(this, &ProxyTest::CheckSignalingThread),
227 Return("ConstMethod0")));
228 EXPECT_EQ("ConstMethod0", fake_proxy_->ConstMethod0());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000229}
230
nisse72c8d2b2016-04-15 03:49:07 -0700231TEST_F(ProxyTest, WorkerMethod1) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000232 const std::string arg1 = "arg1";
233 EXPECT_CALL(*fake_, Method1(arg1))
nisse72c8d2b2016-04-15 03:49:07 -0700234 .Times(Exactly(1))
235 .WillOnce(DoAll(InvokeWithoutArgs(this, &ProxyTest::CheckWorkerThread),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000236 Return("Method1")));
237 EXPECT_EQ("Method1", fake_proxy_->Method1(arg1));
238}
239
240TEST_F(ProxyTest, ConstMethod1) {
241 const std::string arg1 = "arg1";
242 EXPECT_CALL(*fake_, ConstMethod1(arg1))
nisse72c8d2b2016-04-15 03:49:07 -0700243 .Times(Exactly(1))
Yves Gerey665174f2018-06-19 15:03:05 +0200244 .WillOnce(DoAll(InvokeWithoutArgs(this, &ProxyTest::CheckSignalingThread),
245 Return("ConstMethod1")));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000246 EXPECT_EQ("ConstMethod1", fake_proxy_->ConstMethod1(arg1));
247}
248
nisse72c8d2b2016-04-15 03:49:07 -0700249TEST_F(ProxyTest, WorkerMethod2) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000250 const std::string arg1 = "arg1";
251 const std::string arg2 = "arg2";
252 EXPECT_CALL(*fake_, Method2(arg1, arg2))
nisse72c8d2b2016-04-15 03:49:07 -0700253 .Times(Exactly(1))
254 .WillOnce(DoAll(InvokeWithoutArgs(this, &ProxyTest::CheckWorkerThread),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000255 Return("Method2")));
256 EXPECT_EQ("Method2", fake_proxy_->Method2(arg1, arg2));
257}
258
deadbeefd99a2002017-01-18 08:55:23 -0800259// Interface for testing OWNED_PROXY_MAP.
260class FooInterface {
261 public:
262 virtual ~FooInterface() {}
263 virtual void Bar() = 0;
264};
265
266class Foo : public FooInterface {
267 public:
268 Foo() {}
269 MOCK_METHOD0(Bar, void());
270};
271
272BEGIN_OWNED_PROXY_MAP(Foo)
Yves Gerey665174f2018-06-19 15:03:05 +0200273PROXY_SIGNALING_THREAD_DESTRUCTOR()
274PROXY_METHOD0(void, Bar)
deadbeefd99a2002017-01-18 08:55:23 -0800275END_PROXY_MAP()
276
Mirko Bonadei6a489f22019-04-09 15:11:12 +0200277class OwnedProxyTest : public ::testing::Test {
deadbeefd99a2002017-01-18 08:55:23 -0800278 public:
279 OwnedProxyTest()
tommie7251592017-07-14 14:44:46 -0700280 : signaling_thread_(rtc::Thread::Create()),
281 worker_thread_(rtc::Thread::Create()),
282 foo_(new Foo()),
283 foo_proxy_(FooProxy::Create(signaling_thread_.get(),
284 worker_thread_.get(),
deadbeefe814a0d2017-02-25 18:15:09 -0800285 std::unique_ptr<FooInterface>(foo_))) {
tommie7251592017-07-14 14:44:46 -0700286 signaling_thread_->Start();
287 worker_thread_->Start();
deadbeefd99a2002017-01-18 08:55:23 -0800288 }
289
tommie7251592017-07-14 14:44:46 -0700290 void CheckSignalingThread() { EXPECT_TRUE(signaling_thread_->IsCurrent()); }
291 void CheckWorkerThread() { EXPECT_TRUE(worker_thread_->IsCurrent()); }
deadbeefd99a2002017-01-18 08:55:23 -0800292
293 protected:
tommie7251592017-07-14 14:44:46 -0700294 std::unique_ptr<rtc::Thread> signaling_thread_;
295 std::unique_ptr<rtc::Thread> worker_thread_;
deadbeefd99a2002017-01-18 08:55:23 -0800296 Foo* foo_; // Owned by foo_proxy_, not this class.
297 std::unique_ptr<FooInterface> foo_proxy_;
298};
299
300// Just tests that a method can be invoked using an "owned proxy" (as opposed
301// to normal ref-counted version).
302TEST_F(OwnedProxyTest, BasicTest) {
303 EXPECT_CALL(*foo_, Bar())
304 .Times(Exactly(1))
305 .WillOnce(InvokeWithoutArgs(this, &OwnedProxyTest::CheckSignalingThread));
306 foo_proxy_->Bar();
307}
308
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000309} // namespace webrtc