OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "google_apis/gcm/base/socket_stream_test_data_provider.h" |
| 6 |
| 7 namespace gcm { |
| 8 |
| 9 SocketStreamTestDataProvider::SocketStreamTestDataProvider() |
| 10 : has_pending_read_(false), |
| 11 has_pending_write_(false) { |
| 12 } |
| 13 |
| 14 SocketStreamTestDataProvider::~SocketStreamTestDataProvider() { |
| 15 ASSERT_FALSE(has_pending_read_); |
| 16 ASSERT_FALSE(has_pending_write_); |
| 17 } |
| 18 |
| 19 // If there's no read, sets the "has pending read" flag. Otherwise, |
| 20 // pops the next read. |
| 21 net::MockRead SocketStreamTestDataProvider::GetNextRead() { |
| 22 if (reads_.empty()) { |
| 23 DCHECK(!has_pending_read_); |
| 24 has_pending_read_ = true; |
| 25 const net::MockRead pending_read(net::SYNCHRONOUS, net::ERR_IO_PENDING); |
| 26 return pending_read; |
| 27 } |
| 28 net::MockRead mock_read = reads_.front(); |
| 29 reads_.pop_front(); |
| 30 return mock_read; |
| 31 } |
| 32 |
| 33 // Simply pops the next write and, if applicable, compares it to |
| 34 // |data|. |
| 35 net::MockWriteResult SocketStreamTestDataProvider::OnWrite( |
| 36 const std::string& data) { |
| 37 if (writes_.empty()) { |
| 38 DCHECK(!has_pending_write_); |
| 39 has_pending_write_ = true; |
| 40 const net::MockWriteResult pending_write(net::SYNCHRONOUS, |
| 41 net::ERR_IO_PENDING); |
| 42 return pending_write; |
| 43 } |
| 44 net::MockWrite mock_write = writes_.front(); |
| 45 writes_.pop_front(); |
| 46 |
| 47 if (mock_write.result != net::OK) |
| 48 return net::MockWriteResult(mock_write.mode, mock_write.result); |
| 49 |
| 50 std::string expected_data(mock_write.data, mock_write.data_len); |
| 51 if ((size_t)mock_write.data_len < data.length()) { |
| 52 EXPECT_EQ(expected_data, data.substr(0, mock_write.data_len)); |
| 53 if (expected_data != data.substr(0, mock_write.data_len)) |
| 54 return net::MockWriteResult(net::SYNCHRONOUS, net::ERR_UNEXPECTED); |
| 55 return net::MockWriteResult(mock_write.mode, mock_write.data_len); |
| 56 } |
| 57 |
| 58 EXPECT_EQ(expected_data, data); |
| 59 if (expected_data != data) { |
| 60 return net::MockWriteResult(net::SYNCHRONOUS, net::ERR_UNEXPECTED); |
| 61 } |
| 62 return net::MockWriteResult(mock_write.mode, data.size()); |
| 63 } |
| 64 |
| 65 // We ignore resets so we can pre-load the socket data provider with |
| 66 // read/write events. |
| 67 void SocketStreamTestDataProvider::Reset() {} |
| 68 |
| 69 // If there is a pending read, completes it with the given read. |
| 70 // Otherwise, queues up the given read. |
| 71 void SocketStreamTestDataProvider::AddRead(const net::MockRead& mock_read) { |
| 72 DCHECK_NE(mock_read.result, net::ERR_IO_PENDING); |
| 73 if (has_pending_read_) { |
| 74 has_pending_read_ = false; |
| 75 socket()->OnReadComplete(mock_read); |
| 76 return; |
| 77 } |
| 78 reads_.push_back(mock_read); |
| 79 } |
| 80 |
| 81 // If there is a pending write, completes it with the given write. |
| 82 // Otherwise, queues up the given write. |
| 83 void SocketStreamTestDataProvider::AddWrite(const net::MockWrite& mock_write) { |
| 84 if (has_pending_write_) { |
| 85 has_pending_write_ = false; |
| 86 socket()->OnWriteComplete(net::MockWriteResult(net::SYNCHRONOUS, |
| 87 mock_write.data_len)); |
| 88 return; |
| 89 } |
| 90 writes_.push_back(mock_write); |
| 91 } |
| 92 |
| 93 } // namespace gcm |
OLD | NEW |