blob: ebfde9fecf127c2a66d2af82e80ac7a3067d236d [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
Niels Möller105711e2022-06-14 13:48:2616#include "api/make_ref_counted.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3117#include "rtc_base/gunit.h"
Steve Anton10542f22019-01-11 17:11:0018#include "rtc_base/ref_count.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3119#include "test/gmock.h"
henrike@webrtc.org28e20752013-07-10 00:45:3620
21using ::testing::_;
22using ::testing::DoAll;
23using ::testing::Exactly;
24using ::testing::InvokeWithoutArgs;
25using ::testing::Return;
26
27namespace webrtc {
28
29// Interface used for testing here.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:5230class FakeInterface : public rtc::RefCountInterface {
henrike@webrtc.org28e20752013-07-10 00:45:3631 public:
32 virtual void VoidMethod0() = 0;
33 virtual std::string Method0() = 0;
34 virtual std::string ConstMethod0() const = 0;
35 virtual std::string Method1(std::string s) = 0;
36 virtual std::string ConstMethod1(std::string s) const = 0;
37 virtual std::string Method2(std::string s1, std::string s2) = 0;
38
39 protected:
deadbeefd99a2002017-01-18 16:55:2340 virtual ~FakeInterface() {}
henrike@webrtc.org28e20752013-07-10 00:45:3641};
42
henrike@webrtc.org28e20752013-07-10 00:45:3643// Implementation of the test interface.
44class Fake : public FakeInterface {
45 public:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:5246 static rtc::scoped_refptr<Fake> Create() {
Tommi87f70902021-04-27 12:43:0847 return rtc::make_ref_counted<Fake>();
henrike@webrtc.org28e20752013-07-10 00:45:3648 }
deadbeefd99a2002017-01-18 16:55:2349 // Used to verify destructor is called on the correct thread.
Danil Chapovalov3a353122020-05-15 09:16:5350 MOCK_METHOD(void, Destroy, ());
henrike@webrtc.org28e20752013-07-10 00:45:3651
Danil Chapovalov3a353122020-05-15 09:16:5352 MOCK_METHOD(void, VoidMethod0, (), (override));
53 MOCK_METHOD(std::string, Method0, (), (override));
54 MOCK_METHOD(std::string, ConstMethod0, (), (const, override));
henrike@webrtc.org28e20752013-07-10 00:45:3655
Danil Chapovalov3a353122020-05-15 09:16:5356 MOCK_METHOD(std::string, Method1, (std::string), (override));
57 MOCK_METHOD(std::string, ConstMethod1, (std::string), (const, override));
henrike@webrtc.org28e20752013-07-10 00:45:3658
Danil Chapovalov3a353122020-05-15 09:16:5359 MOCK_METHOD(std::string, Method2, (std::string, std::string), (override));
henrike@webrtc.org28e20752013-07-10 00:45:3660
61 protected:
62 Fake() {}
deadbeefd99a2002017-01-18 16:55:2363 ~Fake() { Destroy(); }
henrike@webrtc.org28e20752013-07-10 00:45:3664};
65
nisse72c8d2b2016-04-15 10:49:0766// Proxies for the test interface.
67BEGIN_PROXY_MAP(Fake)
Mirko Bonadei9d9b8de2021-02-26 08:51:2668PROXY_SECONDARY_THREAD_DESTRUCTOR()
Yves Gerey665174f2018-06-19 13:03:0569PROXY_METHOD0(void, VoidMethod0)
70PROXY_METHOD0(std::string, Method0)
71PROXY_CONSTMETHOD0(std::string, ConstMethod0)
Mirko Bonadei9d9b8de2021-02-26 08:51:2672PROXY_SECONDARY_METHOD1(std::string, Method1, std::string)
Yves Gerey665174f2018-06-19 13:03:0573PROXY_CONSTMETHOD1(std::string, ConstMethod1, std::string)
Mirko Bonadei9d9b8de2021-02-26 08:51:2674PROXY_SECONDARY_METHOD2(std::string, Method2, std::string, std::string)
Markus Handell3d46d0b2021-05-27 19:42:5775END_PROXY_MAP(Fake)
nisse72c8d2b2016-04-15 10:49:0776
77// Preprocessor hack to get a proxy class a name different than FakeProxy.
78#define FakeProxy FakeSignalingProxy
deadbeefa601f5c2016-06-06 21:27:3979#define FakeProxyWithInternal FakeSignalingProxyWithInternal
Mirko Bonadei9d9b8de2021-02-26 08:51:2680BEGIN_PRIMARY_PROXY_MAP(Fake)
81PROXY_PRIMARY_THREAD_DESTRUCTOR()
Yves Gerey665174f2018-06-19 13:03:0582PROXY_METHOD0(void, VoidMethod0)
83PROXY_METHOD0(std::string, Method0)
84PROXY_CONSTMETHOD0(std::string, ConstMethod0)
85PROXY_METHOD1(std::string, Method1, std::string)
86PROXY_CONSTMETHOD1(std::string, ConstMethod1, std::string)
87PROXY_METHOD2(std::string, Method2, std::string, std::string)
Markus Handell3d46d0b2021-05-27 19:42:5788END_PROXY_MAP(Fake)
nisse72c8d2b2016-04-15 10:49:0789#undef FakeProxy
90
Mirko Bonadei6a489f22019-04-09 13:11:1291class SignalingProxyTest : public ::testing::Test {
henrike@webrtc.org28e20752013-07-10 00:45:3692 public:
nisse72c8d2b2016-04-15 10:49:0793 // Checks that the functions are called on the right thread.
94 void CheckSignalingThread() { EXPECT_TRUE(signaling_thread_->IsCurrent()); }
henrike@webrtc.org28e20752013-07-10 00:45:3695
96 protected:
nisse72c8d2b2016-04-15 10:49:0797 void SetUp() override {
tommie7251592017-07-14 21:44:4698 signaling_thread_ = rtc::Thread::Create();
henrike@webrtc.org28e20752013-07-10 00:45:3699 ASSERT_TRUE(signaling_thread_->Start());
100 fake_ = Fake::Create();
nisse72c8d2b2016-04-15 10:49:07101 fake_signaling_proxy_ =
Tomas Gunnarsson0d5ce622022-03-18 14:57:15102 FakeSignalingProxy::Create(signaling_thread_.get(), fake_);
henrike@webrtc.org28e20752013-07-10 00:45:36103 }
104
105 protected:
kwibergd1fe2812016-04-27 13:47:29106 std::unique_ptr<rtc::Thread> signaling_thread_;
nisse72c8d2b2016-04-15 10:49:07107 rtc::scoped_refptr<FakeInterface> fake_signaling_proxy_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52108 rtc::scoped_refptr<Fake> fake_;
henrike@webrtc.org28e20752013-07-10 00:45:36109};
110
deadbeefd99a2002017-01-18 16:55:23111TEST_F(SignalingProxyTest, SignalingThreadDestructor) {
112 EXPECT_CALL(*fake_, Destroy())
113 .Times(Exactly(1))
114 .WillOnce(
115 InvokeWithoutArgs(this, &SignalingProxyTest::CheckSignalingThread));
116 fake_ = nullptr;
117 fake_signaling_proxy_ = nullptr;
118}
119
nisse72c8d2b2016-04-15 10:49:07120TEST_F(SignalingProxyTest, VoidMethod0) {
121 EXPECT_CALL(*fake_, VoidMethod0())
122 .Times(Exactly(1))
123 .WillOnce(
124 InvokeWithoutArgs(this, &SignalingProxyTest::CheckSignalingThread));
125 fake_signaling_proxy_->VoidMethod0();
126}
127
128TEST_F(SignalingProxyTest, Method0) {
129 EXPECT_CALL(*fake_, Method0())
130 .Times(Exactly(1))
131 .WillOnce(DoAll(
132 InvokeWithoutArgs(this, &SignalingProxyTest::CheckSignalingThread),
133 Return("Method0")));
134 EXPECT_EQ("Method0", fake_signaling_proxy_->Method0());
135}
136
137TEST_F(SignalingProxyTest, ConstMethod0) {
138 EXPECT_CALL(*fake_, ConstMethod0())
139 .Times(Exactly(1))
140 .WillOnce(DoAll(
141 InvokeWithoutArgs(this, &SignalingProxyTest::CheckSignalingThread),
142 Return("ConstMethod0")));
143 EXPECT_EQ("ConstMethod0", fake_signaling_proxy_->ConstMethod0());
144}
145
146TEST_F(SignalingProxyTest, Method1) {
147 const std::string arg1 = "arg1";
148 EXPECT_CALL(*fake_, Method1(arg1))
149 .Times(Exactly(1))
150 .WillOnce(DoAll(
151 InvokeWithoutArgs(this, &SignalingProxyTest::CheckSignalingThread),
152 Return("Method1")));
153 EXPECT_EQ("Method1", fake_signaling_proxy_->Method1(arg1));
154}
155
156TEST_F(SignalingProxyTest, ConstMethod1) {
157 const std::string arg1 = "arg1";
158 EXPECT_CALL(*fake_, ConstMethod1(arg1))
159 .Times(Exactly(1))
160 .WillOnce(DoAll(
161 InvokeWithoutArgs(this, &SignalingProxyTest::CheckSignalingThread),
162 Return("ConstMethod1")));
163 EXPECT_EQ("ConstMethod1", fake_signaling_proxy_->ConstMethod1(arg1));
164}
165
166TEST_F(SignalingProxyTest, Method2) {
167 const std::string arg1 = "arg1";
168 const std::string arg2 = "arg2";
169 EXPECT_CALL(*fake_, Method2(arg1, arg2))
170 .Times(Exactly(1))
171 .WillOnce(DoAll(
172 InvokeWithoutArgs(this, &SignalingProxyTest::CheckSignalingThread),
173 Return("Method2")));
174 EXPECT_EQ("Method2", fake_signaling_proxy_->Method2(arg1, arg2));
175}
176
Mirko Bonadei6a489f22019-04-09 13:11:12177class ProxyTest : public ::testing::Test {
nisse72c8d2b2016-04-15 10:49:07178 public:
179 // Checks that the functions are called on the right thread.
deadbeefd99a2002017-01-18 16:55:23180 void CheckSignalingThread() { EXPECT_TRUE(signaling_thread_->IsCurrent()); }
nisse72c8d2b2016-04-15 10:49:07181 void CheckWorkerThread() { EXPECT_TRUE(worker_thread_->IsCurrent()); }
182
183 protected:
184 void SetUp() override {
tommie7251592017-07-14 21:44:46185 signaling_thread_ = rtc::Thread::Create();
186 worker_thread_ = rtc::Thread::Create();
deadbeefd99a2002017-01-18 16:55:23187 ASSERT_TRUE(signaling_thread_->Start());
nisse72c8d2b2016-04-15 10:49:07188 ASSERT_TRUE(worker_thread_->Start());
deadbeefd99a2002017-01-18 16:55:23189 fake_ = Fake::Create();
Tomas Gunnarsson0d5ce622022-03-18 14:57:15190 fake_proxy_ =
191 FakeProxy::Create(signaling_thread_.get(), worker_thread_.get(), fake_);
nisse72c8d2b2016-04-15 10:49:07192 }
193
194 protected:
deadbeefd99a2002017-01-18 16:55:23195 std::unique_ptr<rtc::Thread> signaling_thread_;
kwibergd1fe2812016-04-27 13:47:29196 std::unique_ptr<rtc::Thread> worker_thread_;
nisse72c8d2b2016-04-15 10:49:07197 rtc::scoped_refptr<FakeInterface> fake_proxy_;
deadbeefd99a2002017-01-18 16:55:23198 rtc::scoped_refptr<Fake> fake_;
nisse72c8d2b2016-04-15 10:49:07199};
200
deadbeefd99a2002017-01-18 16:55:23201TEST_F(ProxyTest, WorkerThreadDestructor) {
202 EXPECT_CALL(*fake_, Destroy())
203 .Times(Exactly(1))
204 .WillOnce(InvokeWithoutArgs(this, &ProxyTest::CheckWorkerThread));
205 fake_ = nullptr;
206 fake_proxy_ = nullptr;
207}
208
henrike@webrtc.org28e20752013-07-10 00:45:36209TEST_F(ProxyTest, VoidMethod0) {
210 EXPECT_CALL(*fake_, VoidMethod0())
nisse72c8d2b2016-04-15 10:49:07211 .Times(Exactly(1))
212 .WillOnce(InvokeWithoutArgs(this, &ProxyTest::CheckSignalingThread));
henrike@webrtc.org28e20752013-07-10 00:45:36213 fake_proxy_->VoidMethod0();
214}
215
216TEST_F(ProxyTest, Method0) {
217 EXPECT_CALL(*fake_, Method0())
nisse72c8d2b2016-04-15 10:49:07218 .Times(Exactly(1))
Yves Gerey665174f2018-06-19 13:03:05219 .WillOnce(DoAll(InvokeWithoutArgs(this, &ProxyTest::CheckSignalingThread),
220 Return("Method0")));
221 EXPECT_EQ("Method0", fake_proxy_->Method0());
henrike@webrtc.org28e20752013-07-10 00:45:36222}
223
224TEST_F(ProxyTest, ConstMethod0) {
225 EXPECT_CALL(*fake_, ConstMethod0())
nisse72c8d2b2016-04-15 10:49:07226 .Times(Exactly(1))
Yves Gerey665174f2018-06-19 13:03:05227 .WillOnce(DoAll(InvokeWithoutArgs(this, &ProxyTest::CheckSignalingThread),
228 Return("ConstMethod0")));
229 EXPECT_EQ("ConstMethod0", fake_proxy_->ConstMethod0());
henrike@webrtc.org28e20752013-07-10 00:45:36230}
231
nisse72c8d2b2016-04-15 10:49:07232TEST_F(ProxyTest, WorkerMethod1) {
henrike@webrtc.org28e20752013-07-10 00:45:36233 const std::string arg1 = "arg1";
234 EXPECT_CALL(*fake_, Method1(arg1))
nisse72c8d2b2016-04-15 10:49:07235 .Times(Exactly(1))
236 .WillOnce(DoAll(InvokeWithoutArgs(this, &ProxyTest::CheckWorkerThread),
henrike@webrtc.org28e20752013-07-10 00:45:36237 Return("Method1")));
238 EXPECT_EQ("Method1", fake_proxy_->Method1(arg1));
239}
240
241TEST_F(ProxyTest, ConstMethod1) {
242 const std::string arg1 = "arg1";
243 EXPECT_CALL(*fake_, ConstMethod1(arg1))
nisse72c8d2b2016-04-15 10:49:07244 .Times(Exactly(1))
Yves Gerey665174f2018-06-19 13:03:05245 .WillOnce(DoAll(InvokeWithoutArgs(this, &ProxyTest::CheckSignalingThread),
246 Return("ConstMethod1")));
henrike@webrtc.org28e20752013-07-10 00:45:36247 EXPECT_EQ("ConstMethod1", fake_proxy_->ConstMethod1(arg1));
248}
249
nisse72c8d2b2016-04-15 10:49:07250TEST_F(ProxyTest, WorkerMethod2) {
henrike@webrtc.org28e20752013-07-10 00:45:36251 const std::string arg1 = "arg1";
252 const std::string arg2 = "arg2";
253 EXPECT_CALL(*fake_, Method2(arg1, arg2))
nisse72c8d2b2016-04-15 10:49:07254 .Times(Exactly(1))
255 .WillOnce(DoAll(InvokeWithoutArgs(this, &ProxyTest::CheckWorkerThread),
henrike@webrtc.org28e20752013-07-10 00:45:36256 Return("Method2")));
257 EXPECT_EQ("Method2", fake_proxy_->Method2(arg1, arg2));
258}
259
henrike@webrtc.org28e20752013-07-10 00:45:36260} // namespace webrtc