blob: ef3d97eddc12f0dad27dc6f129e2d72de76c5861 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:361/*
kjellanderb24317b2016-02-10 15:54:432 * Copyright 2013 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:363 *
kjellanderb24317b2016-02-10 15:54:434 * 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:369 */
10
Markus Handella1b82012021-05-26 16:56:3011#include "pc/proxy.h"
henrike@webrtc.org28e20752013-07-10 00:45:3612
kwibergd1fe2812016-04-27 13:47:2913#include <memory>
henrike@webrtc.org28e20752013-07-10 00:45:3614#include <string>
15
Mirko Bonadei92ea95e2017-09-15 04:47:3116#include "rtc_base/gunit.h"
Steve Anton10542f22019-01-11 17:11:0017#include "rtc_base/ref_count.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3118#include "test/gmock.h"
henrike@webrtc.org28e20752013-07-10 00:45:3619
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:5229class FakeInterface : public rtc::RefCountInterface {
henrike@webrtc.org28e20752013-07-10 00:45:3630 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 16:55:2339 virtual ~FakeInterface() {}
henrike@webrtc.org28e20752013-07-10 00:45:3640};
41
henrike@webrtc.org28e20752013-07-10 00:45:3642// Implementation of the test interface.
43class Fake : public FakeInterface {
44 public:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:5245 static rtc::scoped_refptr<Fake> Create() {
Tommi87f70902021-04-27 12:43:0846 return rtc::make_ref_counted<Fake>();
henrike@webrtc.org28e20752013-07-10 00:45:3647 }
deadbeefd99a2002017-01-18 16:55:2348 // Used to verify destructor is called on the correct thread.
Danil Chapovalov3a353122020-05-15 09:16:5349 MOCK_METHOD(void, Destroy, ());
henrike@webrtc.org28e20752013-07-10 00:45:3650
Danil Chapovalov3a353122020-05-15 09:16:5351 MOCK_METHOD(void, VoidMethod0, (), (override));
52 MOCK_METHOD(std::string, Method0, (), (override));
53 MOCK_METHOD(std::string, ConstMethod0, (), (const, override));
henrike@webrtc.org28e20752013-07-10 00:45:3654
Danil Chapovalov3a353122020-05-15 09:16:5355 MOCK_METHOD(std::string, Method1, (std::string), (override));
56 MOCK_METHOD(std::string, ConstMethod1, (std::string), (const, override));
henrike@webrtc.org28e20752013-07-10 00:45:3657
Danil Chapovalov3a353122020-05-15 09:16:5358 MOCK_METHOD(std::string, Method2, (std::string, std::string), (override));
henrike@webrtc.org28e20752013-07-10 00:45:3659
60 protected:
61 Fake() {}
deadbeefd99a2002017-01-18 16:55:2362 ~Fake() { Destroy(); }
henrike@webrtc.org28e20752013-07-10 00:45:3663};
64
nisse72c8d2b2016-04-15 10:49:0765// Proxies for the test interface.
66BEGIN_PROXY_MAP(Fake)
Mirko Bonadei9d9b8de2021-02-26 08:51:2667PROXY_SECONDARY_THREAD_DESTRUCTOR()
Yves Gerey665174f2018-06-19 13:03:0568PROXY_METHOD0(void, VoidMethod0)
69PROXY_METHOD0(std::string, Method0)
70PROXY_CONSTMETHOD0(std::string, ConstMethod0)
Mirko Bonadei9d9b8de2021-02-26 08:51:2671PROXY_SECONDARY_METHOD1(std::string, Method1, std::string)
Yves Gerey665174f2018-06-19 13:03:0572PROXY_CONSTMETHOD1(std::string, ConstMethod1, std::string)
Mirko Bonadei9d9b8de2021-02-26 08:51:2673PROXY_SECONDARY_METHOD2(std::string, Method2, std::string, std::string)
Markus Handell3d46d0b2021-05-27 19:42:5774END_PROXY_MAP(Fake)
nisse72c8d2b2016-04-15 10:49:0775
76// Preprocessor hack to get a proxy class a name different than FakeProxy.
77#define FakeProxy FakeSignalingProxy
deadbeefa601f5c2016-06-06 21:27:3978#define FakeProxyWithInternal FakeSignalingProxyWithInternal
Mirko Bonadei9d9b8de2021-02-26 08:51:2679BEGIN_PRIMARY_PROXY_MAP(Fake)
80PROXY_PRIMARY_THREAD_DESTRUCTOR()
Yves Gerey665174f2018-06-19 13:03:0581PROXY_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)
Markus Handell3d46d0b2021-05-27 19:42:5787END_PROXY_MAP(Fake)
nisse72c8d2b2016-04-15 10:49:0788#undef FakeProxy
89
Mirko Bonadei6a489f22019-04-09 13:11:1290class SignalingProxyTest : public ::testing::Test {
henrike@webrtc.org28e20752013-07-10 00:45:3691 public:
nisse72c8d2b2016-04-15 10:49:0792 // 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:3694
95 protected:
nisse72c8d2b2016-04-15 10:49:0796 void SetUp() override {
tommie7251592017-07-14 21:44:4697 signaling_thread_ = rtc::Thread::Create();
henrike@webrtc.org28e20752013-07-10 00:45:3698 ASSERT_TRUE(signaling_thread_->Start());
99 fake_ = Fake::Create();
nisse72c8d2b2016-04-15 10:49:07100 fake_signaling_proxy_ =
101 FakeSignalingProxy::Create(signaling_thread_.get(), fake_.get());
henrike@webrtc.org28e20752013-07-10 00:45:36102 }
103
104 protected:
kwibergd1fe2812016-04-27 13:47:29105 std::unique_ptr<rtc::Thread> signaling_thread_;
nisse72c8d2b2016-04-15 10:49:07106 rtc::scoped_refptr<FakeInterface> fake_signaling_proxy_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52107 rtc::scoped_refptr<Fake> fake_;
henrike@webrtc.org28e20752013-07-10 00:45:36108};
109
deadbeefd99a2002017-01-18 16:55:23110TEST_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 10:49:07119TEST_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 13:11:12176class ProxyTest : public ::testing::Test {
nisse72c8d2b2016-04-15 10:49:07177 public:
178 // Checks that the functions are called on the right thread.
deadbeefd99a2002017-01-18 16:55:23179 void CheckSignalingThread() { EXPECT_TRUE(signaling_thread_->IsCurrent()); }
nisse72c8d2b2016-04-15 10:49:07180 void CheckWorkerThread() { EXPECT_TRUE(worker_thread_->IsCurrent()); }
181
182 protected:
183 void SetUp() override {
tommie7251592017-07-14 21:44:46184 signaling_thread_ = rtc::Thread::Create();
185 worker_thread_ = rtc::Thread::Create();
deadbeefd99a2002017-01-18 16:55:23186 ASSERT_TRUE(signaling_thread_->Start());
nisse72c8d2b2016-04-15 10:49:07187 ASSERT_TRUE(worker_thread_->Start());
deadbeefd99a2002017-01-18 16:55:23188 fake_ = Fake::Create();
nisse72c8d2b2016-04-15 10:49:07189 fake_proxy_ = FakeProxy::Create(signaling_thread_.get(),
190 worker_thread_.get(), fake_.get());
191 }
192
193 protected:
deadbeefd99a2002017-01-18 16:55:23194 std::unique_ptr<rtc::Thread> signaling_thread_;
kwibergd1fe2812016-04-27 13:47:29195 std::unique_ptr<rtc::Thread> worker_thread_;
nisse72c8d2b2016-04-15 10:49:07196 rtc::scoped_refptr<FakeInterface> fake_proxy_;
deadbeefd99a2002017-01-18 16:55:23197 rtc::scoped_refptr<Fake> fake_;
nisse72c8d2b2016-04-15 10:49:07198};
199
deadbeefd99a2002017-01-18 16:55:23200TEST_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:36208TEST_F(ProxyTest, VoidMethod0) {
209 EXPECT_CALL(*fake_, VoidMethod0())
nisse72c8d2b2016-04-15 10:49:07210 .Times(Exactly(1))
211 .WillOnce(InvokeWithoutArgs(this, &ProxyTest::CheckSignalingThread));
henrike@webrtc.org28e20752013-07-10 00:45:36212 fake_proxy_->VoidMethod0();
213}
214
215TEST_F(ProxyTest, Method0) {
216 EXPECT_CALL(*fake_, Method0())
nisse72c8d2b2016-04-15 10:49:07217 .Times(Exactly(1))
Yves Gerey665174f2018-06-19 13:03:05218 .WillOnce(DoAll(InvokeWithoutArgs(this, &ProxyTest::CheckSignalingThread),
219 Return("Method0")));
220 EXPECT_EQ("Method0", fake_proxy_->Method0());
henrike@webrtc.org28e20752013-07-10 00:45:36221}
222
223TEST_F(ProxyTest, ConstMethod0) {
224 EXPECT_CALL(*fake_, ConstMethod0())
nisse72c8d2b2016-04-15 10:49:07225 .Times(Exactly(1))
Yves Gerey665174f2018-06-19 13:03:05226 .WillOnce(DoAll(InvokeWithoutArgs(this, &ProxyTest::CheckSignalingThread),
227 Return("ConstMethod0")));
228 EXPECT_EQ("ConstMethod0", fake_proxy_->ConstMethod0());
henrike@webrtc.org28e20752013-07-10 00:45:36229}
230
nisse72c8d2b2016-04-15 10:49:07231TEST_F(ProxyTest, WorkerMethod1) {
henrike@webrtc.org28e20752013-07-10 00:45:36232 const std::string arg1 = "arg1";
233 EXPECT_CALL(*fake_, Method1(arg1))
nisse72c8d2b2016-04-15 10:49:07234 .Times(Exactly(1))
235 .WillOnce(DoAll(InvokeWithoutArgs(this, &ProxyTest::CheckWorkerThread),
henrike@webrtc.org28e20752013-07-10 00:45:36236 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 10:49:07243 .Times(Exactly(1))
Yves Gerey665174f2018-06-19 13:03:05244 .WillOnce(DoAll(InvokeWithoutArgs(this, &ProxyTest::CheckSignalingThread),
245 Return("ConstMethod1")));
henrike@webrtc.org28e20752013-07-10 00:45:36246 EXPECT_EQ("ConstMethod1", fake_proxy_->ConstMethod1(arg1));
247}
248
nisse72c8d2b2016-04-15 10:49:07249TEST_F(ProxyTest, WorkerMethod2) {
henrike@webrtc.org28e20752013-07-10 00:45:36250 const std::string arg1 = "arg1";
251 const std::string arg2 = "arg2";
252 EXPECT_CALL(*fake_, Method2(arg1, arg2))
nisse72c8d2b2016-04-15 10:49:07253 .Times(Exactly(1))
254 .WillOnce(DoAll(InvokeWithoutArgs(this, &ProxyTest::CheckWorkerThread),
henrike@webrtc.org28e20752013-07-10 00:45:36255 Return("Method2")));
256 EXPECT_EQ("Method2", fake_proxy_->Method2(arg1, arg2));
257}
258
deadbeefd99a2002017-01-18 16:55:23259// 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() {}
Danil Chapovalov3a353122020-05-15 09:16:53269 MOCK_METHOD(void, Bar, (), (override));
deadbeefd99a2002017-01-18 16:55:23270};
271
272BEGIN_OWNED_PROXY_MAP(Foo)
Mirko Bonadei9d9b8de2021-02-26 08:51:26273PROXY_PRIMARY_THREAD_DESTRUCTOR()
Yves Gerey665174f2018-06-19 13:03:05274PROXY_METHOD0(void, Bar)
Markus Handell3d46d0b2021-05-27 19:42:57275END_PROXY_MAP(Foo)
deadbeefd99a2002017-01-18 16:55:23276
Mirko Bonadei6a489f22019-04-09 13:11:12277class OwnedProxyTest : public ::testing::Test {
deadbeefd99a2002017-01-18 16:55:23278 public:
279 OwnedProxyTest()
tommie7251592017-07-14 21:44:46280 : 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-26 02:15:09285 std::unique_ptr<FooInterface>(foo_))) {
tommie7251592017-07-14 21:44:46286 signaling_thread_->Start();
287 worker_thread_->Start();
deadbeefd99a2002017-01-18 16:55:23288 }
289
tommie7251592017-07-14 21:44:46290 void CheckSignalingThread() { EXPECT_TRUE(signaling_thread_->IsCurrent()); }
291 void CheckWorkerThread() { EXPECT_TRUE(worker_thread_->IsCurrent()); }
deadbeefd99a2002017-01-18 16:55:23292
293 protected:
tommie7251592017-07-14 21:44:46294 std::unique_ptr<rtc::Thread> signaling_thread_;
295 std::unique_ptr<rtc::Thread> worker_thread_;
deadbeefd99a2002017-01-18 16:55:23296 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:36309} // namespace webrtc