OLD | NEW |
1 /* | 1 /* |
2 * libjingle | 2 * libjingle |
3 * Copyright 2013 Google Inc. | 3 * Copyright 2013 Google Inc. |
4 * | 4 * |
5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
6 * modification, are permitted provided that the following conditions are met: | 6 * modification, are permitted provided that the following conditions are met: |
7 * | 7 * |
8 * 1. Redistributions of source code must retain the above copyright notice, | 8 * 1. Redistributions of source code must retain the above copyright notice, |
9 * this list of conditions and the following disclaimer. | 9 * this list of conditions and the following disclaimer. |
10 * 2. Redistributions in binary form must reproduce the above copyright notice, | 10 * 2. Redistributions in binary form must reproduce the above copyright notice, |
(...skipping 17 matching lines...) Expand all Loading... |
28 #include "talk/app/webrtc/datachannel.h" | 28 #include "talk/app/webrtc/datachannel.h" |
29 #include "talk/app/webrtc/sctputils.h" | 29 #include "talk/app/webrtc/sctputils.h" |
30 #include "talk/app/webrtc/test/fakedatachannelprovider.h" | 30 #include "talk/app/webrtc/test/fakedatachannelprovider.h" |
31 #include "webrtc/base/gunit.h" | 31 #include "webrtc/base/gunit.h" |
32 | 32 |
33 using webrtc::DataChannel; | 33 using webrtc::DataChannel; |
34 | 34 |
35 class FakeDataChannelObserver : public webrtc::DataChannelObserver { | 35 class FakeDataChannelObserver : public webrtc::DataChannelObserver { |
36 public: | 36 public: |
37 FakeDataChannelObserver() | 37 FakeDataChannelObserver() |
38 : messages_received_(0), on_state_change_count_(0) {} | 38 : messages_received_(0), |
| 39 on_state_change_count_(0), |
| 40 on_buffered_amount_change_count_(0) { |
| 41 } |
39 | 42 |
40 void OnStateChange() { | 43 void OnStateChange() { |
41 ++on_state_change_count_; | 44 ++on_state_change_count_; |
42 } | 45 } |
43 | 46 |
| 47 void OnBufferedAmountChange(uint64 previous_amount) { |
| 48 ++on_buffered_amount_change_count_; |
| 49 } |
| 50 |
44 void OnMessage(const webrtc::DataBuffer& buffer) { | 51 void OnMessage(const webrtc::DataBuffer& buffer) { |
45 ++messages_received_; | 52 ++messages_received_; |
46 } | 53 } |
47 | 54 |
48 size_t messages_received() const { | 55 size_t messages_received() const { |
49 return messages_received_; | 56 return messages_received_; |
50 } | 57 } |
51 | 58 |
52 void ResetOnStateChangeCount() { | 59 void ResetOnStateChangeCount() { |
53 on_state_change_count_ = 0; | 60 on_state_change_count_ = 0; |
54 } | 61 } |
55 | 62 |
| 63 void ResetOnBufferedAmountChangeCount() { |
| 64 on_buffered_amount_change_count_ = 0; |
| 65 } |
| 66 |
56 size_t on_state_change_count() const { | 67 size_t on_state_change_count() const { |
57 return on_state_change_count_; | 68 return on_state_change_count_; |
58 } | 69 } |
59 | 70 |
| 71 size_t on_buffered_amount_change_count() const { |
| 72 return on_buffered_amount_change_count_; |
| 73 } |
| 74 |
60 private: | 75 private: |
61 size_t messages_received_; | 76 size_t messages_received_; |
62 size_t on_state_change_count_; | 77 size_t on_state_change_count_; |
| 78 size_t on_buffered_amount_change_count_; |
63 }; | 79 }; |
64 | 80 |
65 class SctpDataChannelTest : public testing::Test { | 81 class SctpDataChannelTest : public testing::Test { |
66 protected: | 82 protected: |
67 SctpDataChannelTest() | 83 SctpDataChannelTest() |
68 : webrtc_data_channel_( | 84 : webrtc_data_channel_( |
69 DataChannel::Create( | 85 DataChannel::Create( |
70 &provider_, cricket::DCT_SCTP, "test", init_)) { | 86 &provider_, cricket::DCT_SCTP, "test", init_)) { |
71 } | 87 } |
72 | 88 |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
126 webrtc_data_channel_->Close(); | 142 webrtc_data_channel_->Close(); |
127 EXPECT_EQ(webrtc::DataChannelInterface::kClosed, | 143 EXPECT_EQ(webrtc::DataChannelInterface::kClosed, |
128 webrtc_data_channel_->state()); | 144 webrtc_data_channel_->state()); |
129 // Verifies that it's disconnected from the transport. | 145 // Verifies that it's disconnected from the transport. |
130 EXPECT_FALSE(provider_.IsConnected(webrtc_data_channel_.get())); | 146 EXPECT_FALSE(provider_.IsConnected(webrtc_data_channel_.get())); |
131 } | 147 } |
132 | 148 |
133 // Tests that DataChannel::buffered_amount() is correct after the channel is | 149 // Tests that DataChannel::buffered_amount() is correct after the channel is |
134 // blocked. | 150 // blocked. |
135 TEST_F(SctpDataChannelTest, BufferedAmountWhenBlocked) { | 151 TEST_F(SctpDataChannelTest, BufferedAmountWhenBlocked) { |
| 152 AddObserver(); |
136 SetChannelReady(); | 153 SetChannelReady(); |
137 webrtc::DataBuffer buffer("abcd"); | 154 webrtc::DataBuffer buffer("abcd"); |
138 EXPECT_TRUE(webrtc_data_channel_->Send(buffer)); | 155 EXPECT_TRUE(webrtc_data_channel_->Send(buffer)); |
139 | 156 |
140 EXPECT_EQ(0U, webrtc_data_channel_->buffered_amount()); | 157 EXPECT_EQ(0U, webrtc_data_channel_->buffered_amount()); |
| 158 EXPECT_EQ(0U, observer_->on_buffered_amount_change_count()); |
141 | 159 |
142 provider_.set_send_blocked(true); | 160 provider_.set_send_blocked(true); |
143 | 161 |
144 const int number_of_packets = 3; | 162 const int number_of_packets = 3; |
145 for (int i = 0; i < number_of_packets; ++i) { | 163 for (int i = 0; i < number_of_packets; ++i) { |
146 EXPECT_TRUE(webrtc_data_channel_->Send(buffer)); | 164 EXPECT_TRUE(webrtc_data_channel_->Send(buffer)); |
147 } | 165 } |
148 EXPECT_EQ(buffer.data.size() * number_of_packets, | 166 EXPECT_EQ(buffer.data.size() * number_of_packets, |
149 webrtc_data_channel_->buffered_amount()); | 167 webrtc_data_channel_->buffered_amount()); |
| 168 EXPECT_EQ(number_of_packets, observer_->on_buffered_amount_change_count()); |
150 } | 169 } |
151 | 170 |
152 // Tests that the queued data are sent when the channel transitions from blocked | 171 // Tests that the queued data are sent when the channel transitions from blocked |
153 // to unblocked. | 172 // to unblocked. |
154 TEST_F(SctpDataChannelTest, QueuedDataSentWhenUnblocked) { | 173 TEST_F(SctpDataChannelTest, QueuedDataSentWhenUnblocked) { |
| 174 AddObserver(); |
155 SetChannelReady(); | 175 SetChannelReady(); |
156 webrtc::DataBuffer buffer("abcd"); | 176 webrtc::DataBuffer buffer("abcd"); |
157 provider_.set_send_blocked(true); | 177 provider_.set_send_blocked(true); |
158 EXPECT_TRUE(webrtc_data_channel_->Send(buffer)); | 178 EXPECT_TRUE(webrtc_data_channel_->Send(buffer)); |
159 | 179 |
| 180 EXPECT_EQ(1U, observer_->on_buffered_amount_change_count()); |
| 181 |
160 provider_.set_send_blocked(false); | 182 provider_.set_send_blocked(false); |
161 SetChannelReady(); | 183 SetChannelReady(); |
162 EXPECT_EQ(0U, webrtc_data_channel_->buffered_amount()); | 184 EXPECT_EQ(0U, webrtc_data_channel_->buffered_amount()); |
| 185 EXPECT_EQ(2U, observer_->on_buffered_amount_change_count()); |
163 } | 186 } |
164 | 187 |
165 // Tests that no crash when the channel is blocked right away while trying to | 188 // Tests that no crash when the channel is blocked right away while trying to |
166 // send queued data. | 189 // send queued data. |
167 TEST_F(SctpDataChannelTest, BlockedWhenSendQueuedDataNoCrash) { | 190 TEST_F(SctpDataChannelTest, BlockedWhenSendQueuedDataNoCrash) { |
| 191 AddObserver(); |
168 SetChannelReady(); | 192 SetChannelReady(); |
169 webrtc::DataBuffer buffer("abcd"); | 193 webrtc::DataBuffer buffer("abcd"); |
170 provider_.set_send_blocked(true); | 194 provider_.set_send_blocked(true); |
171 EXPECT_TRUE(webrtc_data_channel_->Send(buffer)); | 195 EXPECT_TRUE(webrtc_data_channel_->Send(buffer)); |
| 196 EXPECT_EQ(1U, observer_->on_buffered_amount_change_count()); |
172 | 197 |
173 // Set channel ready while it is still blocked. | 198 // Set channel ready while it is still blocked. |
174 SetChannelReady(); | 199 SetChannelReady(); |
175 EXPECT_EQ(buffer.size(), webrtc_data_channel_->buffered_amount()); | 200 EXPECT_EQ(buffer.size(), webrtc_data_channel_->buffered_amount()); |
| 201 EXPECT_EQ(1U, observer_->on_buffered_amount_change_count()); |
176 | 202 |
177 // Unblock the channel to send queued data again, there should be no crash. | 203 // Unblock the channel to send queued data again, there should be no crash. |
178 provider_.set_send_blocked(false); | 204 provider_.set_send_blocked(false); |
179 SetChannelReady(); | 205 SetChannelReady(); |
180 EXPECT_EQ(0U, webrtc_data_channel_->buffered_amount()); | 206 EXPECT_EQ(0U, webrtc_data_channel_->buffered_amount()); |
| 207 EXPECT_EQ(2U, observer_->on_buffered_amount_change_count()); |
181 } | 208 } |
182 | 209 |
183 // Tests that the queued control message is sent when channel is ready. | 210 // Tests that the queued control message is sent when channel is ready. |
184 TEST_F(SctpDataChannelTest, OpenMessageSent) { | 211 TEST_F(SctpDataChannelTest, OpenMessageSent) { |
185 // Initially the id is unassigned. | 212 // Initially the id is unassigned. |
186 EXPECT_EQ(-1, webrtc_data_channel_->id()); | 213 EXPECT_EQ(-1, webrtc_data_channel_->id()); |
187 | 214 |
188 SetChannelReady(); | 215 SetChannelReady(); |
189 EXPECT_GE(webrtc_data_channel_->id(), 0); | 216 EXPECT_GE(webrtc_data_channel_->id(), 0); |
190 EXPECT_EQ(cricket::DMT_CONTROL, provider_.last_send_data_params().type); | 217 EXPECT_EQ(cricket::DMT_CONTROL, provider_.last_send_data_params().type); |
(...skipping 282 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
473 EXPECT_EQ(webrtc::DataChannelInterface::kOpen, | 500 EXPECT_EQ(webrtc::DataChannelInterface::kOpen, |
474 webrtc_data_channel_->state()); | 501 webrtc_data_channel_->state()); |
475 } | 502 } |
476 | 503 |
477 // Tests that a channel can be closed without being opened or assigned an sid. | 504 // Tests that a channel can be closed without being opened or assigned an sid. |
478 TEST_F(SctpDataChannelTest, NeverOpened) { | 505 TEST_F(SctpDataChannelTest, NeverOpened) { |
479 provider_.set_transport_available(true); | 506 provider_.set_transport_available(true); |
480 webrtc_data_channel_->OnTransportChannelCreated(); | 507 webrtc_data_channel_->OnTransportChannelCreated(); |
481 webrtc_data_channel_->Close(); | 508 webrtc_data_channel_->Close(); |
482 } | 509 } |
OLD | NEW |