OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/socket/buffered_write_stream_socket.h" | 5 #include "net/socket/buffered_write_stream_socket.h" |
6 | 6 |
7 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "net/base/net_errors.h" | 9 #include "net/base/net_errors.h" |
10 #include "net/base/net_log.h" | 10 #include "net/base/net_log.h" |
11 #include "net/socket/socket_test_util.h" | 11 #include "net/socket/socket_test_util.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
13 | 13 |
14 namespace net { | 14 namespace net { |
15 | 15 |
16 namespace { | 16 namespace { |
17 | 17 |
18 class BufferedWriteStreamSocketTest : public testing::Test { | 18 class BufferedWriteStreamSocketTest : public testing::Test { |
19 public: | 19 public: |
20 void Finish() { | 20 void Finish() { |
21 MessageLoop::current()->RunAllPending(); | 21 MessageLoop::current()->RunAllPending(); |
22 EXPECT_TRUE(data_->at_read_eof()); | 22 EXPECT_TRUE(data_->at_read_eof()); |
23 EXPECT_TRUE(data_->at_write_eof()); | 23 EXPECT_TRUE(data_->at_write_eof()); |
24 } | 24 } |
25 | 25 |
26 void Initialize(MockWrite* writes, size_t writes_count) { | 26 void Initialize(MockWrite* writes, size_t writes_count) { |
27 data_ = new DeterministicSocketData(NULL, 0, writes, writes_count); | 27 data_.reset(new DeterministicSocketData(NULL, 0, writes, writes_count)); |
28 data_->set_connect_data(MockConnect(SYNCHRONOUS, 0)); | 28 data_->set_connect_data(MockConnect(SYNCHRONOUS, 0)); |
29 if (writes_count) { | 29 if (writes_count) { |
30 data_->StopAfter(writes_count); | 30 data_->StopAfter(writes_count); |
31 } | 31 } |
32 DeterministicMockTCPClientSocket* wrapped_socket = | 32 DeterministicMockTCPClientSocket* wrapped_socket = |
33 new DeterministicMockTCPClientSocket(net_log_.net_log(), data_.get()); | 33 new DeterministicMockTCPClientSocket(net_log_.net_log(), data_.get()); |
34 data_->set_socket(wrapped_socket->AsWeakPtr()); | 34 data_->set_socket(wrapped_socket->AsWeakPtr()); |
35 socket_.reset(new BufferedWriteStreamSocket(wrapped_socket)); | 35 socket_.reset(new BufferedWriteStreamSocket(wrapped_socket)); |
36 socket_->Connect(callback_.callback()); | 36 socket_->Connect(callback_.callback()); |
37 } | 37 } |
38 | 38 |
39 void TestWrite(const char* text) { | 39 void TestWrite(const char* text) { |
40 scoped_refptr<StringIOBuffer> buf(new StringIOBuffer(text)); | 40 scoped_refptr<StringIOBuffer> buf(new StringIOBuffer(text)); |
41 EXPECT_EQ(buf->size(), | 41 EXPECT_EQ(buf->size(), |
42 socket_->Write(buf.get(), buf->size(), callback_.callback())); | 42 socket_->Write(buf.get(), buf->size(), callback_.callback())); |
43 } | 43 } |
44 | 44 |
45 scoped_ptr<BufferedWriteStreamSocket> socket_; | 45 scoped_ptr<BufferedWriteStreamSocket> socket_; |
46 scoped_refptr<DeterministicSocketData> data_; | 46 scoped_ptr<DeterministicSocketData> data_; |
47 BoundNetLog net_log_; | 47 BoundNetLog net_log_; |
48 TestCompletionCallback callback_; | 48 TestCompletionCallback callback_; |
49 }; | 49 }; |
50 | 50 |
51 TEST_F(BufferedWriteStreamSocketTest, SingleWrite) { | 51 TEST_F(BufferedWriteStreamSocketTest, SingleWrite) { |
52 MockWrite writes[] = { | 52 MockWrite writes[] = { |
53 MockWrite(SYNCHRONOUS, 0, "abc"), | 53 MockWrite(SYNCHRONOUS, 0, "abc"), |
54 }; | 54 }; |
55 Initialize(writes, arraysize(writes)); | 55 Initialize(writes, arraysize(writes)); |
56 TestWrite("abc"); | 56 TestWrite("abc"); |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
113 TestWrite("abc"); | 113 TestWrite("abc"); |
114 data_->RunFor(1); | 114 data_->RunFor(1); |
115 TestWrite("def"); | 115 TestWrite("def"); |
116 data_->RunFor(1); | 116 data_->RunFor(1); |
117 Finish(); | 117 Finish(); |
118 } | 118 } |
119 | 119 |
120 } // anonymous namespace | 120 } // anonymous namespace |
121 | 121 |
122 } // namespace net | 122 } // namespace net |
OLD | NEW |