Index: google_apis/gcm/base/socket_stream_test_data_provider.cc |
diff --git a/google_apis/gcm/base/socket_stream_test_data_provider.cc b/google_apis/gcm/base/socket_stream_test_data_provider.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..3c8ea6976ab2b6a5e2099204081e0460c3387e67 |
--- /dev/null |
+++ b/google_apis/gcm/base/socket_stream_test_data_provider.cc |
@@ -0,0 +1,93 @@ |
+// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "google_apis/gcm/base/socket_stream_test_data_provider.h" |
+ |
+namespace gcm { |
+ |
+SocketStreamTestDataProvider::SocketStreamTestDataProvider() |
+ : has_pending_read_(false), |
+ has_pending_write_(false) { |
+ } |
+ |
+SocketStreamTestDataProvider::~SocketStreamTestDataProvider() { |
+ ASSERT_FALSE(has_pending_read_); |
+ ASSERT_FALSE(has_pending_write_); |
+} |
+ |
+// If there's no read, sets the "has pending read" flag. Otherwise, |
+// pops the next read. |
+net::MockRead SocketStreamTestDataProvider::GetNextRead() { |
+ if (reads_.empty()) { |
+ DCHECK(!has_pending_read_); |
+ has_pending_read_ = true; |
+ const net::MockRead pending_read(net::SYNCHRONOUS, net::ERR_IO_PENDING); |
+ return pending_read; |
+ } |
+ net::MockRead mock_read = reads_.front(); |
+ reads_.pop_front(); |
+ return mock_read; |
+} |
+ |
+// Simply pops the next write and, if applicable, compares it to |
+// |data|. |
+net::MockWriteResult SocketStreamTestDataProvider::OnWrite( |
+ const std::string& data) { |
+ if (writes_.empty()) { |
+ DCHECK(!has_pending_write_); |
+ has_pending_write_ = true; |
+ const net::MockWriteResult pending_write(net::SYNCHRONOUS, |
+ net::ERR_IO_PENDING); |
+ return pending_write; |
+ } |
+ net::MockWrite mock_write = writes_.front(); |
+ writes_.pop_front(); |
+ |
+ if (mock_write.result != net::OK) |
+ return net::MockWriteResult(mock_write.mode, mock_write.result); |
+ |
+ std::string expected_data(mock_write.data, mock_write.data_len); |
+ if ((size_t)mock_write.data_len < data.length()) { |
+ EXPECT_EQ(expected_data, data.substr(0, mock_write.data_len)); |
+ if (expected_data != data.substr(0, mock_write.data_len)) |
+ return net::MockWriteResult(net::SYNCHRONOUS, net::ERR_UNEXPECTED); |
+ return net::MockWriteResult(mock_write.mode, mock_write.data_len); |
+ } |
+ |
+ EXPECT_EQ(expected_data, data); |
+ if (expected_data != data) { |
+ return net::MockWriteResult(net::SYNCHRONOUS, net::ERR_UNEXPECTED); |
+ } |
+ return net::MockWriteResult(mock_write.mode, data.size()); |
+} |
+ |
+// We ignore resets so we can pre-load the socket data provider with |
+// read/write events. |
+void SocketStreamTestDataProvider::Reset() {} |
+ |
+// If there is a pending read, completes it with the given read. |
+// Otherwise, queues up the given read. |
+void SocketStreamTestDataProvider::AddRead(const net::MockRead& mock_read) { |
+ DCHECK_NE(mock_read.result, net::ERR_IO_PENDING); |
+ if (has_pending_read_) { |
+ has_pending_read_ = false; |
+ socket()->OnReadComplete(mock_read); |
+ return; |
+ } |
+ reads_.push_back(mock_read); |
+} |
+ |
+// If there is a pending write, completes it with the given write. |
+// Otherwise, queues up the given write. |
+void SocketStreamTestDataProvider::AddWrite(const net::MockWrite& mock_write) { |
+ if (has_pending_write_) { |
+ has_pending_write_ = false; |
+ socket()->OnWriteComplete(net::MockWriteResult(net::SYNCHRONOUS, |
+ mock_write.data_len)); |
+ return; |
+ } |
+ writes_.push_back(mock_write); |
+} |
+ |
+} // namespace gcm |