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.

344 lines
13 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2012, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "talk/app/webrtc/peerconnectionproxy.h"
  28. #include <string>
  29. #include "talk/base/refcount.h"
  30. #include "talk/base/scoped_ptr.h"
  31. #include "talk/base/thread.h"
  32. #include "talk/base/gunit.h"
  33. #include "testing/base/public/gmock.h"
  34. using ::testing::_;
  35. using ::testing::DoAll;
  36. using ::testing::Exactly;
  37. using ::testing::InvokeWithoutArgs;
  38. using ::testing::Return;
  39. namespace webrtc {
  40. class MockPeerConnection : public PeerConnectionInterface {
  41. public:
  42. MOCK_METHOD0(local_streams,
  43. talk_base::scoped_refptr<StreamCollectionInterface>());
  44. MOCK_METHOD0(remote_streams,
  45. talk_base::scoped_refptr<StreamCollectionInterface>());
  46. MOCK_METHOD1(AddStream,
  47. void(LocalMediaStreamInterface* stream));
  48. MOCK_METHOD2(AddStream,
  49. bool(MediaStreamInterface* stream,
  50. const MediaConstraintsInterface* constraints));
  51. MOCK_METHOD1(RemoveStream,
  52. void(MediaStreamInterface* stream));
  53. MOCK_METHOD1(CreateDtmfSender,
  54. talk_base::scoped_refptr<DtmfSenderInterface>(
  55. AudioTrackInterface* track));
  56. MOCK_METHOD2(GetStats,
  57. bool(StatsObserver* observer, MediaStreamTrackInterface* track));
  58. MOCK_METHOD0(ready_state,
  59. ReadyState());
  60. MOCK_METHOD0(signaling_state,
  61. SignalingState());
  62. MOCK_METHOD0(ice_state,
  63. IceState());
  64. MOCK_METHOD0(ice_connection_state,
  65. IceConnectionState());
  66. MOCK_METHOD0(ice_gathering_state,
  67. IceGatheringState());
  68. MOCK_METHOD2(CreateDataChannel,
  69. talk_base::scoped_refptr<DataChannelInterface>(
  70. const std::string& label,
  71. const DataChannelInit* config));
  72. MOCK_CONST_METHOD0(local_description,
  73. const SessionDescriptionInterface*());
  74. MOCK_CONST_METHOD0(remote_description,
  75. const SessionDescriptionInterface*());
  76. MOCK_METHOD2(CreateOffer,
  77. void(CreateSessionDescriptionObserver* observer,
  78. const MediaConstraintsInterface* constraints));
  79. MOCK_METHOD2(CreateAnswer,
  80. void(CreateSessionDescriptionObserver* observer,
  81. const MediaConstraintsInterface* constraints));
  82. MOCK_METHOD2(SetLocalDescription,
  83. void(SetSessionDescriptionObserver* observer,
  84. SessionDescriptionInterface* desc));
  85. MOCK_METHOD2(SetRemoteDescription,
  86. void(SetSessionDescriptionObserver* observer,
  87. SessionDescriptionInterface* desc));
  88. MOCK_METHOD2(UpdateIce,
  89. bool(const IceServers& configuration,
  90. const MediaConstraintsInterface* constraints));
  91. MOCK_METHOD1(AddIceCandidate,
  92. bool(const IceCandidateInterface* candidate));
  93. protected:
  94. MockPeerConnection() {}
  95. ~MockPeerConnection() {}
  96. };
  97. class PeerConnectionProxyTest: public testing::Test {
  98. public:
  99. PeerConnectionProxyTest()
  100. : dummy1_(new talk_base::RefCountedObject<DummyRefCountedObject>()),
  101. dummy2_(new talk_base::RefCountedObject<DummyRefCountedObject>()),
  102. fake_pointer1_(dummy1_.get()),
  103. fake_pointer2_(dummy2_.get()) {
  104. }
  105. // Checks that the functions is called on the |signaling_thread|.
  106. void CheckThread() {
  107. EXPECT_EQ(talk_base::Thread::Current(), signaling_thread_.get());
  108. }
  109. // Returns an arbitrary pointer type. The pointer may not be used but can be
  110. // reference counted. Ie - the returned pointer implements RefCountInterface
  111. // and can be used in a talk_base::scoped_refptr.
  112. template <typename T>
  113. T* GetFakePointer1() {
  114. return static_cast<T*>(fake_pointer1_);
  115. }
  116. template <typename T>
  117. T* GetFakePointer2() {
  118. return static_cast<T*>(fake_pointer2_);
  119. }
  120. protected:
  121. virtual void SetUp() {
  122. signaling_thread_.reset(new talk_base::Thread());
  123. ASSERT_TRUE(signaling_thread_->Start());
  124. pc_ = new talk_base::RefCountedObject<MockPeerConnection>();
  125. pc_proxy_ = PeerConnectionProxy::Create(signaling_thread_.get(), pc_);
  126. }
  127. protected:
  128. class DummyRefCountedObject : public talk_base::RefCountInterface {
  129. };
  130. talk_base::scoped_ptr<talk_base::Thread> signaling_thread_;
  131. talk_base::scoped_refptr<PeerConnectionInterface> pc_proxy_;
  132. talk_base::scoped_refptr<MockPeerConnection> pc_;
  133. talk_base::scoped_refptr<DummyRefCountedObject> dummy1_;
  134. talk_base::scoped_refptr<DummyRefCountedObject> dummy2_;
  135. void* fake_pointer1_;
  136. void* fake_pointer2_;
  137. };
  138. TEST_F(PeerConnectionProxyTest, local_streams) {
  139. EXPECT_CALL(*pc_, local_streams())
  140. .Times(Exactly(1))
  141. .WillOnce(
  142. DoAll(InvokeWithoutArgs(this, &PeerConnectionProxyTest::CheckThread),
  143. Return(GetFakePointer1<StreamCollectionInterface>())));
  144. EXPECT_EQ(GetFakePointer1<StreamCollectionInterface>(),
  145. pc_proxy_->local_streams());
  146. }
  147. TEST_F(PeerConnectionProxyTest, remote_streams) {
  148. EXPECT_CALL(*pc_, remote_streams())
  149. .Times(Exactly(1))
  150. .WillOnce(
  151. DoAll(InvokeWithoutArgs(this, &PeerConnectionProxyTest::CheckThread),
  152. Return(GetFakePointer1<StreamCollectionInterface>())));
  153. EXPECT_EQ(GetFakePointer1<StreamCollectionInterface>(),
  154. pc_proxy_->remote_streams());
  155. }
  156. TEST_F(PeerConnectionProxyTest, AddStream) {
  157. MediaStreamInterface* fake_stream =
  158. GetFakePointer1<MediaStreamInterface>();
  159. MediaConstraintsInterface* constraints =
  160. GetFakePointer2<MediaConstraintsInterface>();
  161. EXPECT_CALL(*pc_, AddStream(fake_stream, constraints))
  162. .Times(Exactly(1))
  163. .WillOnce(
  164. DoAll(InvokeWithoutArgs(this, &PeerConnectionProxyTest::CheckThread),
  165. Return(true)));
  166. EXPECT_TRUE(pc_proxy_->AddStream(fake_stream, constraints));
  167. }
  168. TEST_F(PeerConnectionProxyTest, RemoveStream) {
  169. MediaStreamInterface* fake_stream =
  170. GetFakePointer1<MediaStreamInterface>();
  171. EXPECT_CALL(*pc_, RemoveStream(fake_stream))
  172. .Times(Exactly(1))
  173. .WillOnce(InvokeWithoutArgs(this, &PeerConnectionProxyTest::CheckThread));
  174. pc_proxy_->RemoveStream(fake_stream);
  175. }
  176. TEST_F(PeerConnectionProxyTest, CreateDtmfSender) {
  177. AudioTrackInterface* fake_track =
  178. GetFakePointer1<AudioTrackInterface>();
  179. EXPECT_CALL(*pc_, CreateDtmfSender(fake_track))
  180. .Times(Exactly(1))
  181. .WillOnce(
  182. DoAll(InvokeWithoutArgs(this, &PeerConnectionProxyTest::CheckThread),
  183. Return(GetFakePointer2<DtmfSenderInterface>())));
  184. EXPECT_EQ(GetFakePointer2<DtmfSenderInterface>(),
  185. pc_proxy_->CreateDtmfSender(fake_track));
  186. }
  187. TEST_F(PeerConnectionProxyTest, GetStats) {
  188. StatsObserver* fake_stats_observer =
  189. GetFakePointer1<StatsObserver>();
  190. MediaStreamTrackInterface* fake_track =
  191. GetFakePointer2<MediaStreamTrackInterface>();
  192. EXPECT_CALL(*pc_, GetStats(fake_stats_observer, fake_track))
  193. .Times(Exactly(1))
  194. .WillOnce(
  195. DoAll(InvokeWithoutArgs(this, &PeerConnectionProxyTest::CheckThread),
  196. Return(true)));
  197. EXPECT_TRUE(pc_proxy_->GetStats(fake_stats_observer, fake_track));
  198. }
  199. TEST_F(PeerConnectionProxyTest, signaling_state) {
  200. EXPECT_CALL(*pc_, signaling_state())
  201. .Times(Exactly(1))
  202. .WillOnce(
  203. DoAll(InvokeWithoutArgs(this, &PeerConnectionProxyTest::CheckThread),
  204. Return(PeerConnectionInterface::kStable)));
  205. EXPECT_EQ(PeerConnectionInterface::kStable, pc_proxy_->signaling_state());
  206. }
  207. TEST_F(PeerConnectionProxyTest, ice_state) {
  208. EXPECT_CALL(*pc_, ice_state())
  209. .Times(Exactly(1))
  210. .WillOnce(
  211. DoAll(InvokeWithoutArgs(this, &PeerConnectionProxyTest::CheckThread),
  212. Return(PeerConnectionInterface::kIceCompleted)));
  213. EXPECT_EQ(PeerConnectionInterface::kIceCompleted, pc_proxy_->ice_state());
  214. }
  215. TEST_F(PeerConnectionProxyTest, ice_connection_state) {
  216. EXPECT_CALL(*pc_, ice_connection_state())
  217. .Times(Exactly(1))
  218. .WillOnce(
  219. DoAll(InvokeWithoutArgs(this, &PeerConnectionProxyTest::CheckThread),
  220. Return(PeerConnectionInterface::kIceConnectionCompleted)));
  221. EXPECT_EQ(PeerConnectionInterface::kIceConnectionCompleted,
  222. pc_proxy_->ice_connection_state());
  223. }
  224. TEST_F(PeerConnectionProxyTest, ice_gathering_state) {
  225. EXPECT_CALL(*pc_, ice_gathering_state())
  226. .Times(Exactly(1))
  227. .WillOnce(
  228. DoAll(InvokeWithoutArgs(this, &PeerConnectionProxyTest::CheckThread),
  229. Return(PeerConnectionInterface::kIceGatheringGathering)));
  230. EXPECT_EQ(PeerConnectionInterface::kIceGatheringGathering,
  231. pc_proxy_->ice_gathering_state());
  232. }
  233. TEST_F(PeerConnectionProxyTest, local_description) {
  234. EXPECT_CALL(*pc_, local_description())
  235. .Times(Exactly(1))
  236. .WillOnce(
  237. DoAll(InvokeWithoutArgs(this, &PeerConnectionProxyTest::CheckThread),
  238. Return(GetFakePointer1<SessionDescriptionInterface>())));
  239. EXPECT_EQ(GetFakePointer1<SessionDescriptionInterface>(),
  240. pc_proxy_->local_description());
  241. }
  242. TEST_F(PeerConnectionProxyTest, remote_description) {
  243. EXPECT_CALL(*pc_, remote_description())
  244. .Times(Exactly(1))
  245. .WillOnce(
  246. DoAll(InvokeWithoutArgs(this, &PeerConnectionProxyTest::CheckThread),
  247. Return(GetFakePointer1<SessionDescriptionInterface>())));
  248. EXPECT_EQ(GetFakePointer1<SessionDescriptionInterface>(),
  249. pc_proxy_->remote_description());
  250. }
  251. TEST_F(PeerConnectionProxyTest, CreateOffer) {
  252. CreateSessionDescriptionObserver* fake_observer =
  253. GetFakePointer1<CreateSessionDescriptionObserver>();
  254. MediaConstraintsInterface* constraints =
  255. GetFakePointer2<MediaConstraintsInterface>();
  256. EXPECT_CALL(*pc_, CreateOffer(fake_observer, constraints))
  257. .Times(Exactly(1))
  258. .WillOnce(InvokeWithoutArgs(this, &PeerConnectionProxyTest::CheckThread));
  259. pc_proxy_->CreateOffer(fake_observer, constraints);
  260. }
  261. TEST_F(PeerConnectionProxyTest, CreateAnswer) {
  262. CreateSessionDescriptionObserver* fake_observer =
  263. GetFakePointer1<CreateSessionDescriptionObserver>();
  264. MediaConstraintsInterface* constraints =
  265. GetFakePointer2<MediaConstraintsInterface>();
  266. EXPECT_CALL(*pc_, CreateAnswer(fake_observer, constraints))
  267. .Times(Exactly(1))
  268. .WillOnce(InvokeWithoutArgs(this, &PeerConnectionProxyTest::CheckThread));
  269. pc_proxy_->CreateAnswer(fake_observer, constraints);
  270. }
  271. TEST_F(PeerConnectionProxyTest, SetLocalDescription) {
  272. SessionDescriptionInterface* fake_desc =
  273. GetFakePointer1<SessionDescriptionInterface>();
  274. SetSessionDescriptionObserver* fake_observer =
  275. GetFakePointer2<SetSessionDescriptionObserver>();
  276. EXPECT_CALL(*pc_, SetLocalDescription(fake_observer, fake_desc))
  277. .Times(Exactly(1))
  278. .WillOnce(InvokeWithoutArgs(this, &PeerConnectionProxyTest::CheckThread));
  279. pc_proxy_->SetLocalDescription(fake_observer, fake_desc);
  280. }
  281. TEST_F(PeerConnectionProxyTest, SetRemoteDescription) {
  282. SessionDescriptionInterface* fake_desc =
  283. GetFakePointer1<SessionDescriptionInterface>();
  284. SetSessionDescriptionObserver* fake_observer =
  285. GetFakePointer2<SetSessionDescriptionObserver>();
  286. EXPECT_CALL(*pc_, SetRemoteDescription(fake_observer, fake_desc))
  287. .Times(Exactly(1))
  288. .WillOnce(InvokeWithoutArgs(this, &PeerConnectionProxyTest::CheckThread));
  289. pc_proxy_->SetRemoteDescription(fake_observer, fake_desc);
  290. }
  291. TEST_F(PeerConnectionProxyTest, UpdateIce) {
  292. MediaConstraintsInterface* constraints =
  293. GetFakePointer1<MediaConstraintsInterface>();
  294. EXPECT_CALL(*pc_, UpdateIce(_, constraints))
  295. .Times(Exactly(1))
  296. .WillOnce(
  297. DoAll(InvokeWithoutArgs(this, &PeerConnectionProxyTest::CheckThread),
  298. Return(true)));
  299. EXPECT_TRUE(pc_proxy_->UpdateIce(PeerConnectionInterface::IceServers(),
  300. constraints));
  301. }
  302. TEST_F(PeerConnectionProxyTest, AddIceCandidate) {
  303. IceCandidateInterface* fake_candidate =
  304. GetFakePointer1<IceCandidateInterface>();
  305. EXPECT_CALL(*pc_, AddIceCandidate(fake_candidate))
  306. .Times(Exactly(1))
  307. .WillOnce(
  308. DoAll(InvokeWithoutArgs(this, &PeerConnectionProxyTest::CheckThread),
  309. Return(true)));
  310. EXPECT_TRUE(pc_proxy_->AddIceCandidate(fake_candidate));
  311. }
  312. } // namespace webrtc