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

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

Issue 10095020: Allow socket API to send binary data. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Patch Set 1 Created 8 years, 8 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 | Annotate | Revision Log
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/udp_socket.h" 5 #include "chrome/browser/extensions/api/socket/udp_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/completion_callback.h" 9 #include "net/base/completion_callback.h"
10 #include "net/base/io_buffer.h"
10 #include "net/base/net_errors.h" 11 #include "net/base/net_errors.h"
11 #include "net/base/rand_callback.h" 12 #include "net/base/rand_callback.h"
12 #include "net/udp/udp_client_socket.h" 13 #include "net/udp/udp_client_socket.h"
13 #include "testing/gmock/include/gmock/gmock.h" 14 #include "testing/gmock/include/gmock/gmock.h"
14 15
15 using testing::_; 16 using testing::_;
16 using testing::DoAll; 17 using testing::DoAll;
17 using testing::Return; 18 using testing::Return;
18 using testing::SaveArg; 19 using testing::SaveArg;
19 20
(...skipping 29 matching lines...) Expand all
49 TEST(SocketTest, TestUDPSocketRead) { 50 TEST(SocketTest, TestUDPSocketRead) {
50 MockUDPSocket* udp_client_socket = new MockUDPSocket(); 51 MockUDPSocket* udp_client_socket = new MockUDPSocket();
51 APIResourceEventNotifier* notifier = new MockAPIResourceEventNotifier(); 52 APIResourceEventNotifier* notifier = new MockAPIResourceEventNotifier();
52 53
53 scoped_ptr<UDPSocket> socket(UDPSocket::CreateSocketForTesting( 54 scoped_ptr<UDPSocket> socket(UDPSocket::CreateSocketForTesting(
54 udp_client_socket, "1.2.3.4", 1, notifier)); 55 udp_client_socket, "1.2.3.4", 1, notifier));
55 56
56 EXPECT_CALL(*udp_client_socket, Read(_, _, _)) 57 EXPECT_CALL(*udp_client_socket, Read(_, _, _))
57 .Times(1); 58 .Times(1);
58 59
59 std::string message = socket->Read(); 60 scoped_refptr<net::IOBufferWithSize> io_buffer(
61 new net::IOBufferWithSize(512));
62 socket->Read(io_buffer.get(), io_buffer->size());
60 } 63 }
61 64
62 TEST(SocketTest, TestUDPSocketWrite) { 65 TEST(SocketTest, TestUDPSocketWrite) {
63 MockUDPSocket* udp_client_socket = new MockUDPSocket(); 66 MockUDPSocket* udp_client_socket = new MockUDPSocket();
64 APIResourceEventNotifier* notifier = new MockAPIResourceEventNotifier(); 67 APIResourceEventNotifier* notifier = new MockAPIResourceEventNotifier();
65 68
66 scoped_ptr<UDPSocket> socket(UDPSocket::CreateSocketForTesting( 69 scoped_ptr<UDPSocket> socket(UDPSocket::CreateSocketForTesting(
67 udp_client_socket, "1.2.3.4", 1, notifier)); 70 udp_client_socket, "1.2.3.4", 1, notifier));
68 71
69 EXPECT_CALL(*udp_client_socket, Write(_, _, _)) 72 EXPECT_CALL(*udp_client_socket, Write(_, _, _))
70 .Times(1); 73 .Times(1);
71 74
72 socket->Write("foo"); 75 scoped_refptr<net::IOBufferWithSize> io_buffer(
76 new net::IOBufferWithSize(512));
77 socket->Write(io_buffer.get(), io_buffer->size());
73 } 78 }
74 79
75 TEST(SocketTest, TestUDPSocketBlockedWrite) { 80 TEST(SocketTest, TestUDPSocketBlockedWrite) {
76 MockUDPSocket* udp_client_socket = new MockUDPSocket(); 81 MockUDPSocket* udp_client_socket = new MockUDPSocket();
77 MockAPIResourceEventNotifier* notifier = new MockAPIResourceEventNotifier(); 82 MockAPIResourceEventNotifier* notifier = new MockAPIResourceEventNotifier();
78 83
79 scoped_ptr<UDPSocket> socket(UDPSocket::CreateSocketForTesting( 84 scoped_ptr<UDPSocket> socket(UDPSocket::CreateSocketForTesting(
80 udp_client_socket, "1.2.3.4", 1, notifier)); 85 udp_client_socket, "1.2.3.4", 1, notifier));
81 86
82 net::CompletionCallback callback; 87 net::CompletionCallback callback;
83 EXPECT_CALL(*udp_client_socket, Write(_, _, _)) 88 EXPECT_CALL(*udp_client_socket, Write(_, _, _))
84 .Times(1) 89 .Times(1)
85 .WillOnce(testing::DoAll(SaveArg<2>(&callback), 90 .WillOnce(testing::DoAll(SaveArg<2>(&callback),
86 Return(net::ERR_IO_PENDING))); 91 Return(net::ERR_IO_PENDING)));
87 92
88 ASSERT_EQ(net::ERR_IO_PENDING, socket->Write("foo")); 93 scoped_refptr<net::IOBufferWithSize> io_buffer(new net::IOBufferWithSize(1));
94 ASSERT_EQ(net::ERR_IO_PENDING, socket->Write(io_buffer.get(),
95 io_buffer->size()));
89 96
90 // Good. Original call came back unable to complete. Now pretend the socket 97 // Good. Original call came back unable to complete. Now pretend the socket
91 // finished, and confirm that we passed the error back. 98 // finished, and confirm that we passed the error back.
92 EXPECT_CALL(*notifier, OnWriteComplete(42)) 99 EXPECT_CALL(*notifier, OnWriteComplete(42))
93 .Times(1); 100 .Times(1);
94 callback.Run(42); 101 callback.Run(42);
95 } 102 }
96 103
97 } // namespace extensions 104 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698