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
3 // found in the LICENSE file.
7 #include "base/compiler_specific.h"
8 #include "base/memory/ref_counted.h"
9 #include "base/message_loop.h"
10 #include "base/message_loop/message_loop_proxy.h"
11 #include "base/single_thread_task_runner.h"
12 #include "base/threading/platform_thread.h"
13 #include "base/threading/thread.h"
14 #include "base/time.h"
15 #include "chrome/test/chromedriver/net/sync_websocket_impl.h"
16 #include "chrome/test/chromedriver/net/test_http_server.h"
17 #include "chrome/test/chromedriver/net/url_request_context_getter.h"
18 #include "googleurl/src/gurl.h"
19 #include "testing/gtest/include/gtest/gtest.h"
23 class SyncWebSocketImplTest
: public testing::Test
{
25 SyncWebSocketImplTest()
26 : client_thread_("ClientThread") {}
27 virtual ~SyncWebSocketImplTest() {}
29 virtual void SetUp() OVERRIDE
{
30 base::Thread::Options
options(base::MessageLoop::TYPE_IO
, 0);
31 ASSERT_TRUE(client_thread_
.StartWithOptions(options
));
32 context_getter_
= new URLRequestContextGetter(
33 client_thread_
.message_loop_proxy());
34 ASSERT_TRUE(server_
.Start());
37 virtual void TearDown() OVERRIDE
{
42 base::Thread client_thread_
;
43 TestHttpServer server_
;
44 scoped_refptr
<URLRequestContextGetter
> context_getter_
;
49 TEST_F(SyncWebSocketImplTest
, CreateDestroy
) {
50 SyncWebSocketImpl
sock(context_getter_
);
53 TEST_F(SyncWebSocketImplTest
, Connect
) {
54 SyncWebSocketImpl
sock(context_getter_
);
55 ASSERT_TRUE(sock
.Connect(server_
.web_socket_url()));
58 TEST_F(SyncWebSocketImplTest
, ConnectFail
) {
59 SyncWebSocketImpl
sock(context_getter_
);
60 ASSERT_FALSE(sock
.Connect(GURL("ws://127.0.0.1:33333")));
63 TEST_F(SyncWebSocketImplTest
, SendReceive
) {
64 SyncWebSocketImpl
sock(context_getter_
);
65 ASSERT_TRUE(sock
.Connect(server_
.web_socket_url()));
66 ASSERT_TRUE(sock
.Send("hi"));
68 ASSERT_TRUE(sock
.ReceiveNextMessage(&message
));
69 ASSERT_STREQ("hi", message
.c_str());
72 TEST_F(SyncWebSocketImplTest
, SendReceiveLarge
) {
73 SyncWebSocketImpl
sock(context_getter_
);
74 ASSERT_TRUE(sock
.Connect(server_
.web_socket_url()));
75 std::string
wrote_message(10 << 20, 'a');
76 ASSERT_TRUE(sock
.Send(wrote_message
));
78 ASSERT_TRUE(sock
.ReceiveNextMessage(&message
));
79 ASSERT_EQ(wrote_message
.length(), message
.length());
80 ASSERT_EQ(wrote_message
, message
);
83 TEST_F(SyncWebSocketImplTest
, SendReceiveMany
) {
84 SyncWebSocketImpl
sock(context_getter_
);
85 ASSERT_TRUE(sock
.Connect(server_
.web_socket_url()));
86 ASSERT_TRUE(sock
.Send("1"));
87 ASSERT_TRUE(sock
.Send("2"));
89 ASSERT_TRUE(sock
.ReceiveNextMessage(&message
));
90 ASSERT_STREQ("1", message
.c_str());
91 ASSERT_TRUE(sock
.Send("3"));
92 ASSERT_TRUE(sock
.ReceiveNextMessage(&message
));
93 ASSERT_STREQ("2", message
.c_str());
94 ASSERT_TRUE(sock
.ReceiveNextMessage(&message
));
95 ASSERT_STREQ("3", message
.c_str());
98 TEST_F(SyncWebSocketImplTest
, CloseOnReceive
) {
99 server_
.SetMessageAction(TestHttpServer::kCloseOnMessage
);
100 SyncWebSocketImpl
sock(context_getter_
);
101 ASSERT_TRUE(sock
.Connect(server_
.web_socket_url()));
102 ASSERT_TRUE(sock
.Send("1"));
104 ASSERT_FALSE(sock
.ReceiveNextMessage(&message
));
105 ASSERT_STREQ("", message
.c_str());
108 TEST_F(SyncWebSocketImplTest
, CloseOnSend
) {
109 SyncWebSocketImpl
sock(context_getter_
);
110 ASSERT_TRUE(sock
.Connect(server_
.web_socket_url()));
112 ASSERT_FALSE(sock
.Send("1"));
115 TEST_F(SyncWebSocketImplTest
, Reconnect
) {
116 SyncWebSocketImpl
sock(context_getter_
);
117 ASSERT_TRUE(sock
.Connect(server_
.web_socket_url()));
118 ASSERT_TRUE(sock
.Send("1"));
119 // Wait for SyncWebSocket to receive the response from the server.
120 base::Time deadline
= base::Time::Now() + base::TimeDelta::FromSeconds(20);
121 while (base::Time::Now() < deadline
) {
122 if (sock
.IsConnected() && !sock
.HasNextMessage())
123 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(10));
128 ASSERT_FALSE(sock
.Send("2"));
129 ASSERT_FALSE(sock
.IsConnected());
131 ASSERT_TRUE(sock
.HasNextMessage());
132 ASSERT_TRUE(sock
.Connect(server_
.web_socket_url()));
133 ASSERT_FALSE(sock
.HasNextMessage());
134 ASSERT_TRUE(sock
.Send("3"));
136 ASSERT_TRUE(sock
.ReceiveNextMessage(&message
));
137 ASSERT_STREQ("3", message
.c_str());
138 ASSERT_FALSE(sock
.HasNextMessage());