Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(46)

Side by Side Diff: chrome/browser/extensions/api/socket/tcp_socket_unittest.cc

Issue 10273016: Refactor the socket API to remove onEvent callback in socket.create() function. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Update Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "chrome/browser/extensions/api/socket/tcp_socket.h" 5 #include "chrome/browser/extensions/api/socket/tcp_socket.h"
6 6
7 #include "base/memory/scoped_ptr.h" 7 #include "base/memory/scoped_ptr.h"
8 #include "chrome/browser/extensions/api/api_resource_event_notifier.h" 8 #include "chrome/browser/extensions/api/api_resource_event_notifier.h"
9 #include "net/base/address_list.h" 9 #include "net/base/address_list.h"
10 #include "net/base/completion_callback.h" 10 #include "net/base/completion_callback.h"
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
46 0, GURL()) {} 46 0, GURL()) {}
47 47
48 MOCK_METHOD2(OnReadComplete, void(int result_code, 48 MOCK_METHOD2(OnReadComplete, void(int result_code,
49 const std::string& message)); 49 const std::string& message));
50 MOCK_METHOD1(OnWriteComplete, void(int result_code)); 50 MOCK_METHOD1(OnWriteComplete, void(int result_code));
51 51
52 protected: 52 protected:
53 virtual ~MockAPIResourceEventNotifier() {} 53 virtual ~MockAPIResourceEventNotifier() {}
54 }; 54 };
55 55
56 class CompleteHandler {
57 public:
58 CompleteHandler() {}
59 MOCK_METHOD1(OnComplete, void(int result_code));
60 MOCK_METHOD2(OnReadComplete, void(int result_code,
61 scoped_refptr<net::IOBuffer> io_buffer));
62 private:
63 DISALLOW_COPY_AND_ASSIGN(CompleteHandler);
64 };
65
66
56 TEST(SocketTest, TestTCPSocketRead) { 67 TEST(SocketTest, TestTCPSocketRead) {
57 net::AddressList address_list; 68 net::AddressList address_list;
58 MockTCPSocket* tcp_client_socket = new MockTCPSocket(address_list); 69 MockTCPSocket* tcp_client_socket = new MockTCPSocket(address_list);
59 APIResourceEventNotifier* notifier = new MockAPIResourceEventNotifier(); 70 APIResourceEventNotifier* notifier = new MockAPIResourceEventNotifier();
71 CompleteHandler handler;
60 72
61 scoped_ptr<TCPSocket> socket(TCPSocket::CreateSocketForTesting( 73 scoped_ptr<TCPSocket> socket(TCPSocket::CreateSocketForTesting(
62 tcp_client_socket, notifier)); 74 tcp_client_socket, notifier));
63 75
64 EXPECT_CALL(*tcp_client_socket, Read(_, _, _)) 76 EXPECT_CALL(*tcp_client_socket, Read(_, _, _))
65 .Times(1); 77 .Times(1);
78 EXPECT_CALL(handler, OnReadComplete(_, _))
79 .Times(1);
66 80
67 scoped_refptr<net::IOBufferWithSize> io_buffer( 81 const int count = 512;
68 new net::IOBufferWithSize(512)); 82 socket->Read(count, base::Bind(&CompleteHandler::OnReadComplete,
69 socket->Read(io_buffer.get(), io_buffer->size()); 83 base::Unretained(&handler)));
70 } 84 }
71 85
72 TEST(SocketTest, TestTCPSocketWrite) { 86 TEST(SocketTest, TestTCPSocketWrite) {
73 net::AddressList address_list; 87 net::AddressList address_list;
74 MockTCPSocket* tcp_client_socket = new MockTCPSocket(address_list); 88 MockTCPSocket* tcp_client_socket = new MockTCPSocket(address_list);
75 APIResourceEventNotifier* notifier = new MockAPIResourceEventNotifier(); 89 APIResourceEventNotifier* notifier = new MockAPIResourceEventNotifier();
90 CompleteHandler handler;
76 91
77 scoped_ptr<TCPSocket> socket(TCPSocket::CreateSocketForTesting( 92 scoped_ptr<TCPSocket> socket(TCPSocket::CreateSocketForTesting(
78 tcp_client_socket, notifier)); 93 tcp_client_socket, notifier));
79 94
80 EXPECT_CALL(*tcp_client_socket, Write(_, _, _)) 95 EXPECT_CALL(*tcp_client_socket, Write(_, _, _))
81 .Times(1); 96 .Times(1);
97 EXPECT_CALL(handler, OnComplete(_))
98 .Times(1);
82 99
83 scoped_refptr<net::IOBufferWithSize> io_buffer( 100 scoped_refptr<net::IOBufferWithSize> io_buffer(
84 new net::IOBufferWithSize(256)); 101 new net::IOBufferWithSize(256));
85 socket->Write(io_buffer.get(), io_buffer->size()); 102 socket->Write(io_buffer.get(), io_buffer->size(),
103 base::Bind(&CompleteHandler::OnComplete, base::Unretained(&handler)));
86 } 104 }
87 105
88 TEST(SocketTest, TestTCPSocketBlockedWrite) { 106 TEST(SocketTest, TestTCPSocketBlockedWrite) {
89 net::AddressList address_list; 107 net::AddressList address_list;
90 MockTCPSocket* tcp_client_socket = new MockTCPSocket(address_list); 108 MockTCPSocket* tcp_client_socket = new MockTCPSocket(address_list);
91 MockAPIResourceEventNotifier* notifier = new MockAPIResourceEventNotifier(); 109 MockAPIResourceEventNotifier* notifier = new MockAPIResourceEventNotifier();
110 CompleteHandler handler;
92 111
93 scoped_ptr<TCPSocket> socket(TCPSocket::CreateSocketForTesting( 112 scoped_ptr<TCPSocket> socket(TCPSocket::CreateSocketForTesting(
94 tcp_client_socket, notifier)); 113 tcp_client_socket, notifier));
95 114
96 net::CompletionCallback callback; 115 net::CompletionCallback callback;
97 EXPECT_CALL(*tcp_client_socket, Write(_, _, _)) 116 EXPECT_CALL(*tcp_client_socket, Write(_, _, _))
98 .Times(1) 117 .Times(1)
99 .WillOnce(testing::DoAll(SaveArg<2>(&callback), 118 .WillOnce(testing::DoAll(SaveArg<2>(&callback),
100 Return(net::ERR_IO_PENDING))); 119 Return(net::ERR_IO_PENDING)));
101
102 scoped_refptr<net::IOBufferWithSize> io_buffer(new net::IOBufferWithSize( 120 scoped_refptr<net::IOBufferWithSize> io_buffer(new net::IOBufferWithSize(
103 1)); 121 1));
104 ASSERT_EQ(net::ERR_IO_PENDING, socket->Write(io_buffer.get(), 122 socket->Write(io_buffer.get(), io_buffer->size(),
105 io_buffer->size())); 123 base::Bind(&CompleteHandler::OnComplete, base::Unretained(&handler)));
106 124
107 // Good. Original call came back unable to complete. Now pretend the socket 125 // Good. Original call came back unable to complete. Now pretend the socket
108 // finished, and confirm that we passed the error back. 126 // finished, and confirm that we passed the error back.
109 EXPECT_CALL(*notifier, OnWriteComplete(42)) 127 EXPECT_CALL(handler, OnComplete(42))
110 .Times(1); 128 .Times(1);
111 callback.Run(42); 129 callback.Run(42);
112 } 130 }
113 131
114 } // namespace extensions 132 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/browser/extensions/api/socket/tcp_socket.cc ('k') | chrome/browser/extensions/api/socket/udp_socket.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698