You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

309 lines
9.6 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright 2013 The WebRTC project authors. All Rights Reserved.
  3. *
  4. * 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.
  9. */
  10. #include "api/proxy.h"
  11. #include <memory>
  12. #include <string>
  13. #include "rtc_base/gunit.h"
  14. #include "rtc_base/ref_count.h"
  15. #include "test/gmock.h"
  16. using ::testing::_;
  17. using ::testing::DoAll;
  18. using ::testing::Exactly;
  19. using ::testing::InvokeWithoutArgs;
  20. using ::testing::Return;
  21. namespace webrtc {
  22. // Interface used for testing here.
  23. class FakeInterface : public rtc::RefCountInterface {
  24. public:
  25. virtual void VoidMethod0() = 0;
  26. virtual std::string Method0() = 0;
  27. virtual std::string ConstMethod0() const = 0;
  28. virtual std::string Method1(std::string s) = 0;
  29. virtual std::string ConstMethod1(std::string s) const = 0;
  30. virtual std::string Method2(std::string s1, std::string s2) = 0;
  31. protected:
  32. virtual ~FakeInterface() {}
  33. };
  34. // Implementation of the test interface.
  35. class Fake : public FakeInterface {
  36. public:
  37. static rtc::scoped_refptr<Fake> Create() {
  38. return new rtc::RefCountedObject<Fake>();
  39. }
  40. // Used to verify destructor is called on the correct thread.
  41. MOCK_METHOD0(Destroy, void());
  42. MOCK_METHOD0(VoidMethod0, void());
  43. MOCK_METHOD0(Method0, std::string());
  44. MOCK_CONST_METHOD0(ConstMethod0, std::string());
  45. MOCK_METHOD1(Method1, std::string(std::string));
  46. MOCK_CONST_METHOD1(ConstMethod1, std::string(std::string));
  47. MOCK_METHOD2(Method2, std::string(std::string, std::string));
  48. protected:
  49. Fake() {}
  50. ~Fake() { Destroy(); }
  51. };
  52. // Proxies for the test interface.
  53. BEGIN_PROXY_MAP(Fake)
  54. PROXY_WORKER_THREAD_DESTRUCTOR()
  55. PROXY_METHOD0(void, VoidMethod0)
  56. PROXY_METHOD0(std::string, Method0)
  57. PROXY_CONSTMETHOD0(std::string, ConstMethod0)
  58. PROXY_WORKER_METHOD1(std::string, Method1, std::string)
  59. PROXY_CONSTMETHOD1(std::string, ConstMethod1, std::string)
  60. PROXY_WORKER_METHOD2(std::string, Method2, std::string, std::string)
  61. END_PROXY_MAP()
  62. // Preprocessor hack to get a proxy class a name different than FakeProxy.
  63. #define FakeProxy FakeSignalingProxy
  64. #define FakeProxyWithInternal FakeSignalingProxyWithInternal
  65. BEGIN_SIGNALING_PROXY_MAP(Fake)
  66. PROXY_SIGNALING_THREAD_DESTRUCTOR()
  67. PROXY_METHOD0(void, VoidMethod0)
  68. PROXY_METHOD0(std::string, Method0)
  69. PROXY_CONSTMETHOD0(std::string, ConstMethod0)
  70. PROXY_METHOD1(std::string, Method1, std::string)
  71. PROXY_CONSTMETHOD1(std::string, ConstMethod1, std::string)
  72. PROXY_METHOD2(std::string, Method2, std::string, std::string)
  73. END_PROXY_MAP()
  74. #undef FakeProxy
  75. class SignalingProxyTest : public ::testing::Test {
  76. public:
  77. // Checks that the functions are called on the right thread.
  78. void CheckSignalingThread() { EXPECT_TRUE(signaling_thread_->IsCurrent()); }
  79. protected:
  80. void SetUp() override {
  81. signaling_thread_ = rtc::Thread::Create();
  82. ASSERT_TRUE(signaling_thread_->Start());
  83. fake_ = Fake::Create();
  84. fake_signaling_proxy_ =
  85. FakeSignalingProxy::Create(signaling_thread_.get(), fake_.get());
  86. }
  87. protected:
  88. std::unique_ptr<rtc::Thread> signaling_thread_;
  89. rtc::scoped_refptr<FakeInterface> fake_signaling_proxy_;
  90. rtc::scoped_refptr<Fake> fake_;
  91. };
  92. TEST_F(SignalingProxyTest, SignalingThreadDestructor) {
  93. EXPECT_CALL(*fake_, Destroy())
  94. .Times(Exactly(1))
  95. .WillOnce(
  96. InvokeWithoutArgs(this, &SignalingProxyTest::CheckSignalingThread));
  97. fake_ = nullptr;
  98. fake_signaling_proxy_ = nullptr;
  99. }
  100. TEST_F(SignalingProxyTest, VoidMethod0) {
  101. EXPECT_CALL(*fake_, VoidMethod0())
  102. .Times(Exactly(1))
  103. .WillOnce(
  104. InvokeWithoutArgs(this, &SignalingProxyTest::CheckSignalingThread));
  105. fake_signaling_proxy_->VoidMethod0();
  106. }
  107. TEST_F(SignalingProxyTest, Method0) {
  108. EXPECT_CALL(*fake_, Method0())
  109. .Times(Exactly(1))
  110. .WillOnce(DoAll(
  111. InvokeWithoutArgs(this, &SignalingProxyTest::CheckSignalingThread),
  112. Return("Method0")));
  113. EXPECT_EQ("Method0", fake_signaling_proxy_->Method0());
  114. }
  115. TEST_F(SignalingProxyTest, ConstMethod0) {
  116. EXPECT_CALL(*fake_, ConstMethod0())
  117. .Times(Exactly(1))
  118. .WillOnce(DoAll(
  119. InvokeWithoutArgs(this, &SignalingProxyTest::CheckSignalingThread),
  120. Return("ConstMethod0")));
  121. EXPECT_EQ("ConstMethod0", fake_signaling_proxy_->ConstMethod0());
  122. }
  123. TEST_F(SignalingProxyTest, Method1) {
  124. const std::string arg1 = "arg1";
  125. EXPECT_CALL(*fake_, Method1(arg1))
  126. .Times(Exactly(1))
  127. .WillOnce(DoAll(
  128. InvokeWithoutArgs(this, &SignalingProxyTest::CheckSignalingThread),
  129. Return("Method1")));
  130. EXPECT_EQ("Method1", fake_signaling_proxy_->Method1(arg1));
  131. }
  132. TEST_F(SignalingProxyTest, ConstMethod1) {
  133. const std::string arg1 = "arg1";
  134. EXPECT_CALL(*fake_, ConstMethod1(arg1))
  135. .Times(Exactly(1))
  136. .WillOnce(DoAll(
  137. InvokeWithoutArgs(this, &SignalingProxyTest::CheckSignalingThread),
  138. Return("ConstMethod1")));
  139. EXPECT_EQ("ConstMethod1", fake_signaling_proxy_->ConstMethod1(arg1));
  140. }
  141. TEST_F(SignalingProxyTest, Method2) {
  142. const std::string arg1 = "arg1";
  143. const std::string arg2 = "arg2";
  144. EXPECT_CALL(*fake_, Method2(arg1, arg2))
  145. .Times(Exactly(1))
  146. .WillOnce(DoAll(
  147. InvokeWithoutArgs(this, &SignalingProxyTest::CheckSignalingThread),
  148. Return("Method2")));
  149. EXPECT_EQ("Method2", fake_signaling_proxy_->Method2(arg1, arg2));
  150. }
  151. class ProxyTest : public ::testing::Test {
  152. public:
  153. // Checks that the functions are called on the right thread.
  154. void CheckSignalingThread() { EXPECT_TRUE(signaling_thread_->IsCurrent()); }
  155. void CheckWorkerThread() { EXPECT_TRUE(worker_thread_->IsCurrent()); }
  156. protected:
  157. void SetUp() override {
  158. signaling_thread_ = rtc::Thread::Create();
  159. worker_thread_ = rtc::Thread::Create();
  160. ASSERT_TRUE(signaling_thread_->Start());
  161. ASSERT_TRUE(worker_thread_->Start());
  162. fake_ = Fake::Create();
  163. fake_proxy_ = FakeProxy::Create(signaling_thread_.get(),
  164. worker_thread_.get(), fake_.get());
  165. }
  166. protected:
  167. std::unique_ptr<rtc::Thread> signaling_thread_;
  168. std::unique_ptr<rtc::Thread> worker_thread_;
  169. rtc::scoped_refptr<FakeInterface> fake_proxy_;
  170. rtc::scoped_refptr<Fake> fake_;
  171. };
  172. TEST_F(ProxyTest, WorkerThreadDestructor) {
  173. EXPECT_CALL(*fake_, Destroy())
  174. .Times(Exactly(1))
  175. .WillOnce(InvokeWithoutArgs(this, &ProxyTest::CheckWorkerThread));
  176. fake_ = nullptr;
  177. fake_proxy_ = nullptr;
  178. }
  179. TEST_F(ProxyTest, VoidMethod0) {
  180. EXPECT_CALL(*fake_, VoidMethod0())
  181. .Times(Exactly(1))
  182. .WillOnce(InvokeWithoutArgs(this, &ProxyTest::CheckSignalingThread));
  183. fake_proxy_->VoidMethod0();
  184. }
  185. TEST_F(ProxyTest, Method0) {
  186. EXPECT_CALL(*fake_, Method0())
  187. .Times(Exactly(1))
  188. .WillOnce(DoAll(InvokeWithoutArgs(this, &ProxyTest::CheckSignalingThread),
  189. Return("Method0")));
  190. EXPECT_EQ("Method0", fake_proxy_->Method0());
  191. }
  192. TEST_F(ProxyTest, ConstMethod0) {
  193. EXPECT_CALL(*fake_, ConstMethod0())
  194. .Times(Exactly(1))
  195. .WillOnce(DoAll(InvokeWithoutArgs(this, &ProxyTest::CheckSignalingThread),
  196. Return("ConstMethod0")));
  197. EXPECT_EQ("ConstMethod0", fake_proxy_->ConstMethod0());
  198. }
  199. TEST_F(ProxyTest, WorkerMethod1) {
  200. const std::string arg1 = "arg1";
  201. EXPECT_CALL(*fake_, Method1(arg1))
  202. .Times(Exactly(1))
  203. .WillOnce(DoAll(InvokeWithoutArgs(this, &ProxyTest::CheckWorkerThread),
  204. Return("Method1")));
  205. EXPECT_EQ("Method1", fake_proxy_->Method1(arg1));
  206. }
  207. TEST_F(ProxyTest, ConstMethod1) {
  208. const std::string arg1 = "arg1";
  209. EXPECT_CALL(*fake_, ConstMethod1(arg1))
  210. .Times(Exactly(1))
  211. .WillOnce(DoAll(InvokeWithoutArgs(this, &ProxyTest::CheckSignalingThread),
  212. Return("ConstMethod1")));
  213. EXPECT_EQ("ConstMethod1", fake_proxy_->ConstMethod1(arg1));
  214. }
  215. TEST_F(ProxyTest, WorkerMethod2) {
  216. const std::string arg1 = "arg1";
  217. const std::string arg2 = "arg2";
  218. EXPECT_CALL(*fake_, Method2(arg1, arg2))
  219. .Times(Exactly(1))
  220. .WillOnce(DoAll(InvokeWithoutArgs(this, &ProxyTest::CheckWorkerThread),
  221. Return("Method2")));
  222. EXPECT_EQ("Method2", fake_proxy_->Method2(arg1, arg2));
  223. }
  224. // Interface for testing OWNED_PROXY_MAP.
  225. class FooInterface {
  226. public:
  227. virtual ~FooInterface() {}
  228. virtual void Bar() = 0;
  229. };
  230. class Foo : public FooInterface {
  231. public:
  232. Foo() {}
  233. MOCK_METHOD0(Bar, void());
  234. };
  235. BEGIN_OWNED_PROXY_MAP(Foo)
  236. PROXY_SIGNALING_THREAD_DESTRUCTOR()
  237. PROXY_METHOD0(void, Bar)
  238. END_PROXY_MAP()
  239. class OwnedProxyTest : public ::testing::Test {
  240. public:
  241. OwnedProxyTest()
  242. : signaling_thread_(rtc::Thread::Create()),
  243. worker_thread_(rtc::Thread::Create()),
  244. foo_(new Foo()),
  245. foo_proxy_(FooProxy::Create(signaling_thread_.get(),
  246. worker_thread_.get(),
  247. std::unique_ptr<FooInterface>(foo_))) {
  248. signaling_thread_->Start();
  249. worker_thread_->Start();
  250. }
  251. void CheckSignalingThread() { EXPECT_TRUE(signaling_thread_->IsCurrent()); }
  252. void CheckWorkerThread() { EXPECT_TRUE(worker_thread_->IsCurrent()); }
  253. protected:
  254. std::unique_ptr<rtc::Thread> signaling_thread_;
  255. std::unique_ptr<rtc::Thread> worker_thread_;
  256. Foo* foo_; // Owned by foo_proxy_, not this class.
  257. std::unique_ptr<FooInterface> foo_proxy_;
  258. };
  259. // Just tests that a method can be invoked using an "owned proxy" (as opposed
  260. // to normal ref-counted version).
  261. TEST_F(OwnedProxyTest, BasicTest) {
  262. EXPECT_CALL(*foo_, Bar())
  263. .Times(Exactly(1))
  264. .WillOnce(InvokeWithoutArgs(this, &OwnedProxyTest::CheckSignalingThread));
  265. foo_proxy_->Bar();
  266. }
  267. } // namespace webrtc