2013-07-10 00:45:36 +00:00
|
|
|
/*
|
2016-02-10 07:54:43 -08:00
|
|
|
* Copyright 2013 The WebRTC project authors. All Rights Reserved.
|
2013-07-10 00:45:36 +00:00
|
|
|
*
|
2016-02-10 07:54:43 -08:00
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
2013-07-10 00:45:36 +00:00
|
|
|
*/
|
|
|
|
|
|
2021-05-26 18:56:30 +02:00
|
|
|
#include "pc/proxy.h"
|
2013-07-10 00:45:36 +00:00
|
|
|
|
2016-04-27 06:47:29 -07:00
|
|
|
#include <memory>
|
2013-07-10 00:45:36 +00:00
|
|
|
#include <string>
|
|
|
|
|
|
2022-06-14 15:48:26 +02:00
|
|
|
#include "api/make_ref_counted.h"
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "rtc_base/gunit.h"
|
2019-01-11 09:11:00 -08:00
|
|
|
#include "rtc_base/ref_count.h"
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "test/gmock.h"
|
2013-07-10 00:45:36 +00:00
|
|
|
|
|
|
|
|
using ::testing::_;
|
|
|
|
|
using ::testing::DoAll;
|
|
|
|
|
using ::testing::Exactly;
|
|
|
|
|
using ::testing::InvokeWithoutArgs;
|
|
|
|
|
using ::testing::Return;
|
|
|
|
|
|
|
|
|
|
namespace webrtc {
|
|
|
|
|
|
|
|
|
|
// Interface used for testing here.
|
2024-06-06 11:01:02 +00:00
|
|
|
class FakeInterface : public RefCountInterface {
|
2013-07-10 00:45:36 +00:00
|
|
|
public:
|
|
|
|
|
virtual void VoidMethod0() = 0;
|
|
|
|
|
virtual std::string Method0() = 0;
|
|
|
|
|
virtual std::string ConstMethod0() const = 0;
|
|
|
|
|
virtual std::string Method1(std::string s) = 0;
|
|
|
|
|
virtual std::string ConstMethod1(std::string s) const = 0;
|
|
|
|
|
virtual std::string Method2(std::string s1, std::string s2) = 0;
|
|
|
|
|
|
|
|
|
|
protected:
|
2017-01-18 08:55:23 -08:00
|
|
|
virtual ~FakeInterface() {}
|
2013-07-10 00:45:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Implementation of the test interface.
|
|
|
|
|
class Fake : public FakeInterface {
|
|
|
|
|
public:
|
2014-07-29 17:36:52 +00:00
|
|
|
static rtc::scoped_refptr<Fake> Create() {
|
2021-04-27 14:43:08 +02:00
|
|
|
return rtc::make_ref_counted<Fake>();
|
2013-07-10 00:45:36 +00:00
|
|
|
}
|
2017-01-18 08:55:23 -08:00
|
|
|
// Used to verify destructor is called on the correct thread.
|
2020-05-15 11:16:53 +02:00
|
|
|
MOCK_METHOD(void, Destroy, ());
|
2013-07-10 00:45:36 +00:00
|
|
|
|
2020-05-15 11:16:53 +02:00
|
|
|
MOCK_METHOD(void, VoidMethod0, (), (override));
|
|
|
|
|
MOCK_METHOD(std::string, Method0, (), (override));
|
|
|
|
|
MOCK_METHOD(std::string, ConstMethod0, (), (const, override));
|
2013-07-10 00:45:36 +00:00
|
|
|
|
2020-05-15 11:16:53 +02:00
|
|
|
MOCK_METHOD(std::string, Method1, (std::string), (override));
|
|
|
|
|
MOCK_METHOD(std::string, ConstMethod1, (std::string), (const, override));
|
2013-07-10 00:45:36 +00:00
|
|
|
|
2020-05-15 11:16:53 +02:00
|
|
|
MOCK_METHOD(std::string, Method2, (std::string, std::string), (override));
|
2013-07-10 00:45:36 +00:00
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
Fake() {}
|
2017-01-18 08:55:23 -08:00
|
|
|
~Fake() { Destroy(); }
|
2013-07-10 00:45:36 +00:00
|
|
|
};
|
|
|
|
|
|
2016-04-15 03:49:07 -07:00
|
|
|
// Proxies for the test interface.
|
|
|
|
|
BEGIN_PROXY_MAP(Fake)
|
2021-02-26 09:51:26 +01:00
|
|
|
PROXY_SECONDARY_THREAD_DESTRUCTOR()
|
2018-06-19 15:03:05 +02:00
|
|
|
PROXY_METHOD0(void, VoidMethod0)
|
|
|
|
|
PROXY_METHOD0(std::string, Method0)
|
|
|
|
|
PROXY_CONSTMETHOD0(std::string, ConstMethod0)
|
2021-02-26 09:51:26 +01:00
|
|
|
PROXY_SECONDARY_METHOD1(std::string, Method1, std::string)
|
2018-06-19 15:03:05 +02:00
|
|
|
PROXY_CONSTMETHOD1(std::string, ConstMethod1, std::string)
|
2021-02-26 09:51:26 +01:00
|
|
|
PROXY_SECONDARY_METHOD2(std::string, Method2, std::string, std::string)
|
2021-05-27 21:42:57 +02:00
|
|
|
END_PROXY_MAP(Fake)
|
2016-04-15 03:49:07 -07:00
|
|
|
|
|
|
|
|
// Preprocessor hack to get a proxy class a name different than FakeProxy.
|
|
|
|
|
#define FakeProxy FakeSignalingProxy
|
2016-06-06 14:27:39 -07:00
|
|
|
#define FakeProxyWithInternal FakeSignalingProxyWithInternal
|
2021-02-26 09:51:26 +01:00
|
|
|
BEGIN_PRIMARY_PROXY_MAP(Fake)
|
|
|
|
|
PROXY_PRIMARY_THREAD_DESTRUCTOR()
|
2018-06-19 15:03:05 +02:00
|
|
|
PROXY_METHOD0(void, VoidMethod0)
|
|
|
|
|
PROXY_METHOD0(std::string, Method0)
|
|
|
|
|
PROXY_CONSTMETHOD0(std::string, ConstMethod0)
|
|
|
|
|
PROXY_METHOD1(std::string, Method1, std::string)
|
|
|
|
|
PROXY_CONSTMETHOD1(std::string, ConstMethod1, std::string)
|
|
|
|
|
PROXY_METHOD2(std::string, Method2, std::string, std::string)
|
2021-05-27 21:42:57 +02:00
|
|
|
END_PROXY_MAP(Fake)
|
2016-04-15 03:49:07 -07:00
|
|
|
#undef FakeProxy
|
|
|
|
|
|
2019-04-09 15:11:12 +02:00
|
|
|
class SignalingProxyTest : public ::testing::Test {
|
2013-07-10 00:45:36 +00:00
|
|
|
public:
|
2016-04-15 03:49:07 -07:00
|
|
|
// Checks that the functions are called on the right thread.
|
|
|
|
|
void CheckSignalingThread() { EXPECT_TRUE(signaling_thread_->IsCurrent()); }
|
2013-07-10 00:45:36 +00:00
|
|
|
|
|
|
|
|
protected:
|
2016-04-15 03:49:07 -07:00
|
|
|
void SetUp() override {
|
2017-07-14 14:44:46 -07:00
|
|
|
signaling_thread_ = rtc::Thread::Create();
|
2013-07-10 00:45:36 +00:00
|
|
|
ASSERT_TRUE(signaling_thread_->Start());
|
|
|
|
|
fake_ = Fake::Create();
|
2016-04-15 03:49:07 -07:00
|
|
|
fake_signaling_proxy_ =
|
2022-03-18 15:57:15 +01:00
|
|
|
FakeSignalingProxy::Create(signaling_thread_.get(), fake_);
|
2013-07-10 00:45:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected:
|
2016-04-27 06:47:29 -07:00
|
|
|
std::unique_ptr<rtc::Thread> signaling_thread_;
|
2016-04-15 03:49:07 -07:00
|
|
|
rtc::scoped_refptr<FakeInterface> fake_signaling_proxy_;
|
2014-07-29 17:36:52 +00:00
|
|
|
rtc::scoped_refptr<Fake> fake_;
|
2013-07-10 00:45:36 +00:00
|
|
|
};
|
|
|
|
|
|
2017-01-18 08:55:23 -08:00
|
|
|
TEST_F(SignalingProxyTest, SignalingThreadDestructor) {
|
|
|
|
|
EXPECT_CALL(*fake_, Destroy())
|
|
|
|
|
.Times(Exactly(1))
|
|
|
|
|
.WillOnce(
|
|
|
|
|
InvokeWithoutArgs(this, &SignalingProxyTest::CheckSignalingThread));
|
|
|
|
|
fake_ = nullptr;
|
|
|
|
|
fake_signaling_proxy_ = nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-15 03:49:07 -07:00
|
|
|
TEST_F(SignalingProxyTest, VoidMethod0) {
|
|
|
|
|
EXPECT_CALL(*fake_, VoidMethod0())
|
|
|
|
|
.Times(Exactly(1))
|
|
|
|
|
.WillOnce(
|
|
|
|
|
InvokeWithoutArgs(this, &SignalingProxyTest::CheckSignalingThread));
|
|
|
|
|
fake_signaling_proxy_->VoidMethod0();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(SignalingProxyTest, Method0) {
|
|
|
|
|
EXPECT_CALL(*fake_, Method0())
|
|
|
|
|
.Times(Exactly(1))
|
|
|
|
|
.WillOnce(DoAll(
|
|
|
|
|
InvokeWithoutArgs(this, &SignalingProxyTest::CheckSignalingThread),
|
|
|
|
|
Return("Method0")));
|
|
|
|
|
EXPECT_EQ("Method0", fake_signaling_proxy_->Method0());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(SignalingProxyTest, ConstMethod0) {
|
|
|
|
|
EXPECT_CALL(*fake_, ConstMethod0())
|
|
|
|
|
.Times(Exactly(1))
|
|
|
|
|
.WillOnce(DoAll(
|
|
|
|
|
InvokeWithoutArgs(this, &SignalingProxyTest::CheckSignalingThread),
|
|
|
|
|
Return("ConstMethod0")));
|
|
|
|
|
EXPECT_EQ("ConstMethod0", fake_signaling_proxy_->ConstMethod0());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(SignalingProxyTest, Method1) {
|
|
|
|
|
const std::string arg1 = "arg1";
|
|
|
|
|
EXPECT_CALL(*fake_, Method1(arg1))
|
|
|
|
|
.Times(Exactly(1))
|
|
|
|
|
.WillOnce(DoAll(
|
|
|
|
|
InvokeWithoutArgs(this, &SignalingProxyTest::CheckSignalingThread),
|
|
|
|
|
Return("Method1")));
|
|
|
|
|
EXPECT_EQ("Method1", fake_signaling_proxy_->Method1(arg1));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(SignalingProxyTest, ConstMethod1) {
|
|
|
|
|
const std::string arg1 = "arg1";
|
|
|
|
|
EXPECT_CALL(*fake_, ConstMethod1(arg1))
|
|
|
|
|
.Times(Exactly(1))
|
|
|
|
|
.WillOnce(DoAll(
|
|
|
|
|
InvokeWithoutArgs(this, &SignalingProxyTest::CheckSignalingThread),
|
|
|
|
|
Return("ConstMethod1")));
|
|
|
|
|
EXPECT_EQ("ConstMethod1", fake_signaling_proxy_->ConstMethod1(arg1));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(SignalingProxyTest, Method2) {
|
|
|
|
|
const std::string arg1 = "arg1";
|
|
|
|
|
const std::string arg2 = "arg2";
|
|
|
|
|
EXPECT_CALL(*fake_, Method2(arg1, arg2))
|
|
|
|
|
.Times(Exactly(1))
|
|
|
|
|
.WillOnce(DoAll(
|
|
|
|
|
InvokeWithoutArgs(this, &SignalingProxyTest::CheckSignalingThread),
|
|
|
|
|
Return("Method2")));
|
|
|
|
|
EXPECT_EQ("Method2", fake_signaling_proxy_->Method2(arg1, arg2));
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-09 15:11:12 +02:00
|
|
|
class ProxyTest : public ::testing::Test {
|
2016-04-15 03:49:07 -07:00
|
|
|
public:
|
|
|
|
|
// Checks that the functions are called on the right thread.
|
2017-01-18 08:55:23 -08:00
|
|
|
void CheckSignalingThread() { EXPECT_TRUE(signaling_thread_->IsCurrent()); }
|
2016-04-15 03:49:07 -07:00
|
|
|
void CheckWorkerThread() { EXPECT_TRUE(worker_thread_->IsCurrent()); }
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
void SetUp() override {
|
2017-07-14 14:44:46 -07:00
|
|
|
signaling_thread_ = rtc::Thread::Create();
|
|
|
|
|
worker_thread_ = rtc::Thread::Create();
|
2017-01-18 08:55:23 -08:00
|
|
|
ASSERT_TRUE(signaling_thread_->Start());
|
2016-04-15 03:49:07 -07:00
|
|
|
ASSERT_TRUE(worker_thread_->Start());
|
2017-01-18 08:55:23 -08:00
|
|
|
fake_ = Fake::Create();
|
2022-03-18 15:57:15 +01:00
|
|
|
fake_proxy_ =
|
|
|
|
|
FakeProxy::Create(signaling_thread_.get(), worker_thread_.get(), fake_);
|
2016-04-15 03:49:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected:
|
2017-01-18 08:55:23 -08:00
|
|
|
std::unique_ptr<rtc::Thread> signaling_thread_;
|
2016-04-27 06:47:29 -07:00
|
|
|
std::unique_ptr<rtc::Thread> worker_thread_;
|
2016-04-15 03:49:07 -07:00
|
|
|
rtc::scoped_refptr<FakeInterface> fake_proxy_;
|
2017-01-18 08:55:23 -08:00
|
|
|
rtc::scoped_refptr<Fake> fake_;
|
2016-04-15 03:49:07 -07:00
|
|
|
};
|
|
|
|
|
|
2017-01-18 08:55:23 -08:00
|
|
|
TEST_F(ProxyTest, WorkerThreadDestructor) {
|
|
|
|
|
EXPECT_CALL(*fake_, Destroy())
|
|
|
|
|
.Times(Exactly(1))
|
|
|
|
|
.WillOnce(InvokeWithoutArgs(this, &ProxyTest::CheckWorkerThread));
|
|
|
|
|
fake_ = nullptr;
|
|
|
|
|
fake_proxy_ = nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-10 00:45:36 +00:00
|
|
|
TEST_F(ProxyTest, VoidMethod0) {
|
|
|
|
|
EXPECT_CALL(*fake_, VoidMethod0())
|
2016-04-15 03:49:07 -07:00
|
|
|
.Times(Exactly(1))
|
|
|
|
|
.WillOnce(InvokeWithoutArgs(this, &ProxyTest::CheckSignalingThread));
|
2013-07-10 00:45:36 +00:00
|
|
|
fake_proxy_->VoidMethod0();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(ProxyTest, Method0) {
|
|
|
|
|
EXPECT_CALL(*fake_, Method0())
|
2016-04-15 03:49:07 -07:00
|
|
|
.Times(Exactly(1))
|
2018-06-19 15:03:05 +02:00
|
|
|
.WillOnce(DoAll(InvokeWithoutArgs(this, &ProxyTest::CheckSignalingThread),
|
|
|
|
|
Return("Method0")));
|
|
|
|
|
EXPECT_EQ("Method0", fake_proxy_->Method0());
|
2013-07-10 00:45:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(ProxyTest, ConstMethod0) {
|
|
|
|
|
EXPECT_CALL(*fake_, ConstMethod0())
|
2016-04-15 03:49:07 -07:00
|
|
|
.Times(Exactly(1))
|
2018-06-19 15:03:05 +02:00
|
|
|
.WillOnce(DoAll(InvokeWithoutArgs(this, &ProxyTest::CheckSignalingThread),
|
|
|
|
|
Return("ConstMethod0")));
|
|
|
|
|
EXPECT_EQ("ConstMethod0", fake_proxy_->ConstMethod0());
|
2013-07-10 00:45:36 +00:00
|
|
|
}
|
|
|
|
|
|
2016-04-15 03:49:07 -07:00
|
|
|
TEST_F(ProxyTest, WorkerMethod1) {
|
2013-07-10 00:45:36 +00:00
|
|
|
const std::string arg1 = "arg1";
|
|
|
|
|
EXPECT_CALL(*fake_, Method1(arg1))
|
2016-04-15 03:49:07 -07:00
|
|
|
.Times(Exactly(1))
|
|
|
|
|
.WillOnce(DoAll(InvokeWithoutArgs(this, &ProxyTest::CheckWorkerThread),
|
2013-07-10 00:45:36 +00:00
|
|
|
Return("Method1")));
|
|
|
|
|
EXPECT_EQ("Method1", fake_proxy_->Method1(arg1));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(ProxyTest, ConstMethod1) {
|
|
|
|
|
const std::string arg1 = "arg1";
|
|
|
|
|
EXPECT_CALL(*fake_, ConstMethod1(arg1))
|
2016-04-15 03:49:07 -07:00
|
|
|
.Times(Exactly(1))
|
2018-06-19 15:03:05 +02:00
|
|
|
.WillOnce(DoAll(InvokeWithoutArgs(this, &ProxyTest::CheckSignalingThread),
|
|
|
|
|
Return("ConstMethod1")));
|
2013-07-10 00:45:36 +00:00
|
|
|
EXPECT_EQ("ConstMethod1", fake_proxy_->ConstMethod1(arg1));
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-15 03:49:07 -07:00
|
|
|
TEST_F(ProxyTest, WorkerMethod2) {
|
2013-07-10 00:45:36 +00:00
|
|
|
const std::string arg1 = "arg1";
|
|
|
|
|
const std::string arg2 = "arg2";
|
|
|
|
|
EXPECT_CALL(*fake_, Method2(arg1, arg2))
|
2016-04-15 03:49:07 -07:00
|
|
|
.Times(Exactly(1))
|
|
|
|
|
.WillOnce(DoAll(InvokeWithoutArgs(this, &ProxyTest::CheckWorkerThread),
|
2013-07-10 00:45:36 +00:00
|
|
|
Return("Method2")));
|
|
|
|
|
EXPECT_EQ("Method2", fake_proxy_->Method2(arg1, arg2));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace webrtc
|