Update CrOS OOBE throbber to MD throbber; delete old asset
[chromium-blink-merge.git] / chrome / test / chromedriver / net / sync_websocket_impl_unittest.cc
blobb7d27b575b617c883b56eb264a285b406afde1f2
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.
5 #include <string>
7 #include "base/compiler_specific.h"
8 #include "base/memory/ref_counted.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/single_thread_task_runner.h"
11 #include "base/threading/platform_thread.h"
12 #include "base/threading/thread.h"
13 #include "base/time/time.h"
14 #include "chrome/test/chromedriver/net/sync_websocket_impl.h"
15 #include "chrome/test/chromedriver/net/test_http_server.h"
16 #include "chrome/test/chromedriver/net/url_request_context_getter.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18 #include "url/gurl.h"
20 namespace {
22 class SyncWebSocketImplTest : public testing::Test {
23 protected:
24 SyncWebSocketImplTest()
25 : client_thread_("ClientThread"),
26 long_timeout_(base::TimeDelta::FromMinutes(1)) {}
27 ~SyncWebSocketImplTest() override {}
29 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(client_thread_.task_runner());
33 ASSERT_TRUE(server_.Start());
36 void TearDown() override { server_.Stop(); }
38 base::Thread client_thread_;
39 TestHttpServer server_;
40 scoped_refptr<URLRequestContextGetter> context_getter_;
41 const base::TimeDelta long_timeout_;
44 } // namespace
46 TEST_F(SyncWebSocketImplTest, CreateDestroy) {
47 SyncWebSocketImpl sock(context_getter_.get());
50 TEST_F(SyncWebSocketImplTest, Connect) {
51 SyncWebSocketImpl sock(context_getter_.get());
52 ASSERT_TRUE(sock.Connect(server_.web_socket_url()));
55 TEST_F(SyncWebSocketImplTest, ConnectFail) {
56 SyncWebSocketImpl sock(context_getter_.get());
57 ASSERT_FALSE(sock.Connect(GURL("ws://127.0.0.1:33333")));
60 TEST_F(SyncWebSocketImplTest, SendReceive) {
61 SyncWebSocketImpl sock(context_getter_.get());
62 ASSERT_TRUE(sock.Connect(server_.web_socket_url()));
63 ASSERT_TRUE(sock.Send("hi"));
64 std::string message;
65 ASSERT_EQ(
66 SyncWebSocket::kOk,
67 sock.ReceiveNextMessage(&message, long_timeout_));
68 ASSERT_STREQ("hi", message.c_str());
71 TEST_F(SyncWebSocketImplTest, SendReceiveTimeout) {
72 SyncWebSocketImpl sock(context_getter_.get());
73 ASSERT_TRUE(sock.Connect(server_.web_socket_url()));
74 ASSERT_TRUE(sock.Send("hi"));
75 std::string message;
76 ASSERT_EQ(
77 SyncWebSocket::kTimeout,
78 sock.ReceiveNextMessage(
79 &message, base::TimeDelta()));
82 TEST_F(SyncWebSocketImplTest, SendReceiveLarge) {
83 SyncWebSocketImpl sock(context_getter_.get());
84 ASSERT_TRUE(sock.Connect(server_.web_socket_url()));
85 std::string wrote_message(10 << 20, 'a');
86 ASSERT_TRUE(sock.Send(wrote_message));
87 std::string message;
88 ASSERT_EQ(
89 SyncWebSocket::kOk,
90 sock.ReceiveNextMessage(&message, long_timeout_));
91 ASSERT_EQ(wrote_message.length(), message.length());
92 ASSERT_EQ(wrote_message, message);
95 TEST_F(SyncWebSocketImplTest, SendReceiveMany) {
96 SyncWebSocketImpl sock(context_getter_.get());
97 ASSERT_TRUE(sock.Connect(server_.web_socket_url()));
98 ASSERT_TRUE(sock.Send("1"));
99 ASSERT_TRUE(sock.Send("2"));
100 std::string message;
101 ASSERT_EQ(
102 SyncWebSocket::kOk,
103 sock.ReceiveNextMessage(&message, long_timeout_));
104 ASSERT_STREQ("1", message.c_str());
105 ASSERT_TRUE(sock.Send("3"));
106 ASSERT_EQ(
107 SyncWebSocket::kOk,
108 sock.ReceiveNextMessage(&message, long_timeout_));
109 ASSERT_STREQ("2", message.c_str());
110 ASSERT_EQ(
111 SyncWebSocket::kOk,
112 sock.ReceiveNextMessage(&message, long_timeout_));
113 ASSERT_STREQ("3", message.c_str());
116 TEST_F(SyncWebSocketImplTest, CloseOnReceive) {
117 server_.SetMessageAction(TestHttpServer::kCloseOnMessage);
118 SyncWebSocketImpl sock(context_getter_.get());
119 ASSERT_TRUE(sock.Connect(server_.web_socket_url()));
120 ASSERT_TRUE(sock.Send("1"));
121 std::string message;
122 ASSERT_EQ(
123 SyncWebSocket::kDisconnected,
124 sock.ReceiveNextMessage(&message, long_timeout_));
125 ASSERT_STREQ("", message.c_str());
128 TEST_F(SyncWebSocketImplTest, CloseOnSend) {
129 SyncWebSocketImpl sock(context_getter_.get());
130 ASSERT_TRUE(sock.Connect(server_.web_socket_url()));
131 server_.Stop();
132 ASSERT_FALSE(sock.Send("1"));
135 TEST_F(SyncWebSocketImplTest, Reconnect) {
136 SyncWebSocketImpl sock(context_getter_.get());
137 ASSERT_TRUE(sock.Connect(server_.web_socket_url()));
138 ASSERT_TRUE(sock.Send("1"));
139 // Wait for SyncWebSocket to receive the response from the server.
140 base::TimeTicks deadline =
141 base::TimeTicks::Now() + base::TimeDelta::FromSeconds(20);
142 while (base::TimeTicks::Now() < deadline) {
143 if (sock.IsConnected() && !sock.HasNextMessage())
144 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(10));
145 else
146 break;
148 server_.Stop();
149 ASSERT_FALSE(sock.Send("2"));
150 ASSERT_FALSE(sock.IsConnected());
151 server_.Start();
152 ASSERT_TRUE(sock.HasNextMessage());
153 ASSERT_TRUE(sock.Connect(server_.web_socket_url()));
154 ASSERT_FALSE(sock.HasNextMessage());
155 ASSERT_TRUE(sock.Send("3"));
156 std::string message;
157 ASSERT_EQ(
158 SyncWebSocket::kOk,
159 sock.ReceiveNextMessage(&message, long_timeout_));
160 ASSERT_STREQ("3", message.c_str());
161 ASSERT_FALSE(sock.HasNextMessage());