Chromium Code Reviews| Index: webrtc/pc/peerconnection_datachannelonly_unittest.cc | 
| diff --git a/webrtc/pc/peerconnection_datachannelonly_unittest.cc b/webrtc/pc/peerconnection_datachannelonly_unittest.cc | 
| new file mode 100644 | 
| index 0000000000000000000000000000000000000000..c2b81f45c1c11bd6dfb45f72e477df8d9715e63d | 
| --- /dev/null | 
| +++ b/webrtc/pc/peerconnection_datachannelonly_unittest.cc | 
| @@ -0,0 +1,186 @@ | 
| +/* | 
| + * Copyright 2013 The WebRTC project authors. All Rights Reserved. | 
| + * | 
| + * 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. | 
| + */ | 
| + | 
| +#include <memory> | 
| + | 
| +#include "webrtc/base/gunit.h" | 
| +#include "webrtc/base/logging.h" | 
| +#include "webrtc/base/ptr_util.h" | 
| +#include "webrtc/base/ssladapter.h" | 
| +#include "webrtc/base/sslstreamadapter.h" | 
| +#include "webrtc/base/stringencode.h" | 
| +#include "webrtc/base/stringutils.h" | 
| +#include "webrtc/base/thread.h" | 
| +#ifdef WEBRTC_ANDROID | 
| +#include "webrtc/pc/test/androidtestinitializer.h" | 
| +#endif | 
| +#include "webrtc/pc/test/peerconnectiontestwrapper.h" | 
| +// Notice that mockpeerconnectionobservers.h must be included after the above! | 
| +#include "webrtc/pc/test/mockpeerconnectionobservers.h" | 
| + | 
| +using webrtc::DataChannelInterface; | 
| +using webrtc::FakeConstraints; | 
| +using webrtc::MediaConstraintsInterface; | 
| +using webrtc::MediaStreamInterface; | 
| +using webrtc::PeerConnectionInterface; | 
| + | 
| +namespace { | 
| + | 
| +const int kMaxWait = 10000; | 
| + | 
| +} // namespace | 
| + | 
| +class PeerConnectionEndToEndTest : public sigslot::has_slots<>, | 
| + public testing::Test { | 
| + public: | 
| + typedef std::vector<rtc::scoped_refptr<DataChannelInterface> > | 
| + DataChannelList; | 
| + | 
| + PeerConnectionEndToEndTest() { | 
| + RTC_CHECK(network_thread_.Start()); | 
| + RTC_CHECK(worker_thread_.Start()); | 
| + caller_ = new rtc::RefCountedObject<PeerConnectionTestWrapper>( | 
| + "caller", &network_thread_, &worker_thread_); | 
| + callee_ = new rtc::RefCountedObject<PeerConnectionTestWrapper>( | 
| + "callee", &network_thread_, &worker_thread_); | 
| + webrtc::PeerConnectionInterface::IceServer ice_server; | 
| + ice_server.uri = "stun:stun.l.google.com:19302"; | 
| + config_.servers.push_back(ice_server); | 
| + | 
| +#ifdef WEBRTC_ANDROID | 
| + webrtc::InitializeAndroidObjects(); | 
| +#endif | 
| + } | 
| + | 
| + void CreatePcs( | 
| + const MediaConstraintsInterface* pc_constraints, | 
| + rtc::scoped_refptr<webrtc::AudioEncoderFactory> audio_encoder_factory, | 
| + rtc::scoped_refptr<webrtc::AudioDecoderFactory> audio_decoder_factory) { | 
| + EXPECT_TRUE(caller_->CreatePc( | 
| + pc_constraints, config_, audio_encoder_factory, audio_decoder_factory)); | 
| + EXPECT_TRUE(callee_->CreatePc( | 
| + pc_constraints, config_, audio_encoder_factory, audio_decoder_factory)); | 
| + PeerConnectionTestWrapper::Connect(caller_.get(), callee_.get()); | 
| + | 
| + caller_->SignalOnDataChannel.connect( | 
| + this, &PeerConnectionEndToEndTest::OnCallerAddedDataChanel); | 
| + callee_->SignalOnDataChannel.connect( | 
| + this, &PeerConnectionEndToEndTest::OnCalleeAddedDataChannel); | 
| + } | 
| + | 
| + void Negotiate() { caller_->CreateOffer(NULL); } | 
| + | 
| + void WaitForCallEstablished() { | 
| + caller_->WaitForCallEstablished(); | 
| + callee_->WaitForCallEstablished(); | 
| + } | 
| + | 
| + void WaitForConnection() { | 
| + caller_->WaitForConnection(); | 
| + callee_->WaitForConnection(); | 
| + } | 
| + | 
| + void OnCallerAddedDataChanel(DataChannelInterface* dc) { | 
| + caller_signaled_data_channels_.push_back(dc); | 
| + } | 
| + | 
| + void OnCalleeAddedDataChannel(DataChannelInterface* dc) { | 
| + callee_signaled_data_channels_.push_back(dc); | 
| + } | 
| + | 
| + // Tests that |dc1| and |dc2| can send to and receive from each other. | 
| + void TestDataChannelSendAndReceive(DataChannelInterface* dc1, | 
| + DataChannelInterface* dc2) { | 
| + std::unique_ptr<webrtc::MockDataChannelObserver> dc1_observer( | 
| + new webrtc::MockDataChannelObserver(dc1)); | 
| + | 
| + std::unique_ptr<webrtc::MockDataChannelObserver> dc2_observer( | 
| + new webrtc::MockDataChannelObserver(dc2)); | 
| + | 
| + static const std::string kDummyData = "abcdefg"; | 
| + webrtc::DataBuffer buffer(kDummyData); | 
| + EXPECT_TRUE(dc1->Send(buffer)); | 
| + EXPECT_EQ_WAIT(kDummyData, dc2_observer->last_message(), kMaxWait); | 
| + | 
| + EXPECT_TRUE(dc2->Send(buffer)); | 
| + EXPECT_EQ_WAIT(kDummyData, dc1_observer->last_message(), kMaxWait); | 
| + | 
| + EXPECT_EQ(1U, dc1_observer->received_message_count()); | 
| + EXPECT_EQ(1U, dc2_observer->received_message_count()); | 
| + } | 
| + | 
| + void WaitForDataChannelsToOpen(DataChannelInterface* local_dc, | 
| + const DataChannelList& remote_dc_list, | 
| + size_t remote_dc_index) { | 
| + EXPECT_EQ_WAIT(DataChannelInterface::kOpen, local_dc->state(), kMaxWait); | 
| + | 
| + EXPECT_TRUE_WAIT(remote_dc_list.size() > remote_dc_index, kMaxWait); | 
| + EXPECT_EQ_WAIT(DataChannelInterface::kOpen, | 
| + remote_dc_list[remote_dc_index]->state(), kMaxWait); | 
| + EXPECT_EQ(local_dc->id(), remote_dc_list[remote_dc_index]->id()); | 
| + } | 
| + | 
| + void CloseDataChannels(DataChannelInterface* local_dc, | 
| + const DataChannelList& remote_dc_list, | 
| + size_t remote_dc_index) { | 
| + local_dc->Close(); | 
| + EXPECT_EQ_WAIT(DataChannelInterface::kClosed, local_dc->state(), kMaxWait); | 
| + EXPECT_EQ_WAIT(DataChannelInterface::kClosed, | 
| + remote_dc_list[remote_dc_index]->state(), kMaxWait); | 
| + } | 
| + | 
| + protected: | 
| + rtc::Thread network_thread_; | 
| + rtc::Thread worker_thread_; | 
| + rtc::scoped_refptr<PeerConnectionTestWrapper> caller_; | 
| + rtc::scoped_refptr<PeerConnectionTestWrapper> callee_; | 
| + DataChannelList caller_signaled_data_channels_; | 
| + DataChannelList callee_signaled_data_channels_; | 
| + webrtc::PeerConnectionInterface::RTCConfiguration config_; | 
| +}; | 
| + | 
| +// Verifies that the message is received by the right remote DataChannel. | 
| +TEST_F(PeerConnectionEndToEndTest, | 
| + MessageTransferBetweenTwoPairsOfDataChannels) { | 
| +#ifdef HAVE_SCTP | 
| 
 
Taylor Brandstetter
2017/05/30 17:30:53
I think this #ifdef can be removed; there's no rea
 
Zhi Huang
2017/05/31 00:03:29
Done.
 
 | 
| + CreatePcs(nullptr, rtc::scoped_refptr<webrtc::AudioEncoderFactory>(), | 
| + rtc::scoped_refptr<webrtc::AudioDecoderFactory>()); | 
| + | 
| + webrtc::DataChannelInit init; | 
| + | 
| + rtc::scoped_refptr<DataChannelInterface> caller_dc_1( | 
| + caller_->CreateDataChannel("data", init)); | 
| + rtc::scoped_refptr<DataChannelInterface> caller_dc_2( | 
| + caller_->CreateDataChannel("data", init)); | 
| + | 
| + Negotiate(); | 
| + WaitForConnection(); | 
| + WaitForDataChannelsToOpen(caller_dc_1, callee_signaled_data_channels_, 0); | 
| + WaitForDataChannelsToOpen(caller_dc_2, callee_signaled_data_channels_, 1); | 
| + | 
| + std::unique_ptr<webrtc::MockDataChannelObserver> dc_1_observer( | 
| + new webrtc::MockDataChannelObserver(callee_signaled_data_channels_[0])); | 
| + | 
| + std::unique_ptr<webrtc::MockDataChannelObserver> dc_2_observer( | 
| + new webrtc::MockDataChannelObserver(callee_signaled_data_channels_[1])); | 
| + | 
| + const std::string message_1 = "hello 1"; | 
| + const std::string message_2 = "hello 2"; | 
| + | 
| + caller_dc_1->Send(webrtc::DataBuffer(message_1)); | 
| + EXPECT_EQ_WAIT(message_1, dc_1_observer->last_message(), kMaxWait); | 
| + | 
| + caller_dc_2->Send(webrtc::DataBuffer(message_2)); | 
| + EXPECT_EQ_WAIT(message_2, dc_2_observer->last_message(), kMaxWait); | 
| + | 
| + EXPECT_EQ(1U, dc_1_observer->received_message_count()); | 
| + EXPECT_EQ(1U, dc_2_observer->received_message_count()); | 
| +#endif // HAVE_SCTP | 
| +} |