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