| 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. |
| 9 | */ |
| 10 |
| 11 | #include "pc/proxy.h" |
| 12 |
| 13 | #include <memory> |
| 14 | #include <string> |
| 15 |
| 16 | #include "api/make_ref_counted.h" |
| 17 | #include "rtc_base/gunit.h" |
| 18 | #include "rtc_base/ref_count.h" |
| 19 | #include "test/gmock.h" |
| 20 |
| 21 | using ::testing::_; |
| 22 | using ::testing::DoAll; |
| 23 | using ::testing::Exactly; |
| 24 | using ::testing::InvokeWithoutArgs; |
| 25 | using ::testing::Return; |
| 26 |
| 27 | namespace webrtc { |
| 28 |
| 29 | // Interface used for testing here. |
| 30 | class FakeInterface : public rtc::RefCountInterface { |
| 31 | 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: |
| 40 | virtual ~FakeInterface() {} |
| 41 | }; |
| 42 |
| 43 | // Implementation of the test interface. |
| 44 | class Fake : public FakeInterface { |
| 45 | public: |
| 46 | static rtc::scoped_refptr<Fake> Create() { |
| 47 | return rtc::make_ref_counted<Fake>(); |
| 48 | } |
| 49 | // Used to verify destructor is called on the correct thread. |
| 50 | MOCK_METHOD(void, Destroy, ()); |
| 51 |
| 52 | MOCK_METHOD(void, VoidMethod0, (), (override)); |
| 53 | MOCK_METHOD(std::string, Method0, (), (override)); |
| 54 | MOCK_METHOD(std::string, ConstMethod0, (), (const, override)); |
| 55 |
| 56 | MOCK_METHOD(std::string, Method1, (std::string), (override)); |
| 57 | MOCK_METHOD(std::string, ConstMethod1, (std::string), (const, override)); |
| 58 |
| 59 | MOCK_METHOD(std::string, Method2, (std::string, std::string), (override)); |
| 60 |
| 61 | protected: |
| 62 | Fake() {} |
| 63 | ~Fake() { Destroy(); } |
| 64 | }; |
| 65 |
| 66 | // Proxies for the test interface. |
| 67 | BEGIN_PROXY_MAP(Fake) |
| 68 | PROXY_SECONDARY_THREAD_DESTRUCTOR() |
| 69 | PROXY_METHOD0(void, VoidMethod0) |
| 70 | PROXY_METHOD0(std::string, Method0) |
| 71 | PROXY_CONSTMETHOD0(std::string, ConstMethod0) |
| 72 | PROXY_SECONDARY_METHOD1(std::string, Method1, std::string) |
| 73 | PROXY_CONSTMETHOD1(std::string, ConstMethod1, std::string) |
| 74 | PROXY_SECONDARY_METHOD2(std::string, Method2, std::string, std::string) |
| 75 | END_PROXY_MAP(Fake) |
| 76 |
| 77 | // Preprocessor hack to get a proxy class a name different than FakeProxy. |
| 78 | #define FakeProxy FakeSignalingProxy |
| 79 | #define FakeProxyWithInternal FakeSignalingProxyWithInternal |
| 80 | BEGIN_PRIMARY_PROXY_MAP(Fake) |
| 81 | PROXY_PRIMARY_THREAD_DESTRUCTOR() |
| 82 | PROXY_METHOD0(void, VoidMethod0) |
| 83 | PROXY_METHOD0(std::string, Method0) |
| 84 | PROXY_CONSTMETHOD0(std::string, ConstMethod0) |
| 85 | PROXY_METHOD1(std::string, Method1, std::string) |
| 86 | PROXY_CONSTMETHOD1(std::string, ConstMethod1, std::string) |
| 87 | PROXY_METHOD2(std::string, Method2, std::string, std::string) |
| 88 | END_PROXY_MAP(Fake) |
| 89 | #undef FakeProxy |
| 90 |
| 91 | class SignalingProxyTest : public ::testing::Test { |
| 92 | public: |
| 93 | // Checks that the functions are called on the right thread. |
| 94 | void CheckSignalingThread() { EXPECT_TRUE(signaling_thread_->IsCurrent()); } |
| 95 |
| 96 | protected: |
| 97 | void SetUp() override { |
| 98 | signaling_thread_ = rtc::Thread::Create(); |
| 99 | ASSERT_TRUE(signaling_thread_->Start()); |
| 100 | fake_ = Fake::Create(); |
| 101 | fake_signaling_proxy_ = |
| 102 | FakeSignalingProxy::Create(signaling_thread_.get(), fake_); |
| 103 | } |
| 104 |
| 105 | protected: |
| 106 | std::unique_ptr<rtc::Thread> signaling_thread_; |
| 107 | rtc::scoped_refptr<FakeInterface> fake_signaling_proxy_; |
| 108 | rtc::scoped_refptr<Fake> fake_; |
| 109 | }; |
| 110 |
| 111 | TEST_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 |
| 120 | TEST_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 |
| 128 | TEST_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 |
| 137 | TEST_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 |
| 146 | TEST_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 |
| 156 | TEST_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 |
| 166 | TEST_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 |
| 177 | class ProxyTest : public ::testing::Test { |
| 178 | public: |
| 179 | // Checks that the functions are called on the right thread. |
| 180 | void CheckSignalingThread() { EXPECT_TRUE(signaling_thread_->IsCurrent()); } |
| 181 | void CheckWorkerThread() { EXPECT_TRUE(worker_thread_->IsCurrent()); } |
| 182 |
| 183 | protected: |
| 184 | void SetUp() override { |
| 185 | signaling_thread_ = rtc::Thread::Create(); |
| 186 | worker_thread_ = rtc::Thread::Create(); |
| 187 | ASSERT_TRUE(signaling_thread_->Start()); |
| 188 | ASSERT_TRUE(worker_thread_->Start()); |
| 189 | fake_ = Fake::Create(); |
| 190 | fake_proxy_ = |
| 191 | FakeProxy::Create(signaling_thread_.get(), worker_thread_.get(), fake_); |
| 192 | } |
| 193 |
| 194 | protected: |
| 195 | std::unique_ptr<rtc::Thread> signaling_thread_; |
| 196 | std::unique_ptr<rtc::Thread> worker_thread_; |
| 197 | rtc::scoped_refptr<FakeInterface> fake_proxy_; |
| 198 | rtc::scoped_refptr<Fake> fake_; |
| 199 | }; |
| 200 |
| 201 | TEST_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 |
| 209 | TEST_F(ProxyTest, VoidMethod0) { |
| 210 | EXPECT_CALL(*fake_, VoidMethod0()) |
| 211 | .Times(Exactly(1)) |
| 212 | .WillOnce(InvokeWithoutArgs(this, &ProxyTest::CheckSignalingThread)); |
| 213 | fake_proxy_->VoidMethod0(); |
| 214 | } |
| 215 |
| 216 | TEST_F(ProxyTest, Method0) { |
| 217 | EXPECT_CALL(*fake_, Method0()) |
| 218 | .Times(Exactly(1)) |
| 219 | .WillOnce(DoAll(InvokeWithoutArgs(this, &ProxyTest::CheckSignalingThread), |
| 220 | Return("Method0"))); |
| 221 | EXPECT_EQ("Method0", fake_proxy_->Method0()); |
| 222 | } |
| 223 |
| 224 | TEST_F(ProxyTest, ConstMethod0) { |
| 225 | EXPECT_CALL(*fake_, ConstMethod0()) |
| 226 | .Times(Exactly(1)) |
| 227 | .WillOnce(DoAll(InvokeWithoutArgs(this, &ProxyTest::CheckSignalingThread), |
| 228 | Return("ConstMethod0"))); |
| 229 | EXPECT_EQ("ConstMethod0", fake_proxy_->ConstMethod0()); |
| 230 | } |
| 231 |
| 232 | TEST_F(ProxyTest, WorkerMethod1) { |
| 233 | const std::string arg1 = "arg1"; |
| 234 | EXPECT_CALL(*fake_, Method1(arg1)) |
| 235 | .Times(Exactly(1)) |
| 236 | .WillOnce(DoAll(InvokeWithoutArgs(this, &ProxyTest::CheckWorkerThread), |
| 237 | Return("Method1"))); |
| 238 | EXPECT_EQ("Method1", fake_proxy_->Method1(arg1)); |
| 239 | } |
| 240 |
| 241 | TEST_F(ProxyTest, ConstMethod1) { |
| 242 | const std::string arg1 = "arg1"; |
| 243 | EXPECT_CALL(*fake_, ConstMethod1(arg1)) |
| 244 | .Times(Exactly(1)) |
| 245 | .WillOnce(DoAll(InvokeWithoutArgs(this, &ProxyTest::CheckSignalingThread), |
| 246 | Return("ConstMethod1"))); |
| 247 | EXPECT_EQ("ConstMethod1", fake_proxy_->ConstMethod1(arg1)); |
| 248 | } |
| 249 |
| 250 | TEST_F(ProxyTest, WorkerMethod2) { |
| 251 | const std::string arg1 = "arg1"; |
| 252 | const std::string arg2 = "arg2"; |
| 253 | EXPECT_CALL(*fake_, Method2(arg1, arg2)) |
| 254 | .Times(Exactly(1)) |
| 255 | .WillOnce(DoAll(InvokeWithoutArgs(this, &ProxyTest::CheckWorkerThread), |
| 256 | Return("Method2"))); |
| 257 | EXPECT_EQ("Method2", fake_proxy_->Method2(arg1, arg2)); |
| 258 | } |
| 259 |
| 260 | } // namespace webrtc |
| 261 |