IndexedDBFactory now ForceCloses databases.
[chromium-blink-merge.git] / content / browser / renderer_host / websocket_dispatcher_host_unittest.cc
blobe771a2c0002eb80443021504b4b519c3cee874b8
1 // Copyright 2013 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 "content/browser/renderer_host/websocket_dispatcher_host.h"
7 #include <vector>
9 #include "base/bind.h"
10 #include "base/bind_helpers.h"
11 #include "base/memory/ref_counted.h"
12 #include "content/browser/renderer_host/websocket_host.h"
13 #include "content/common/websocket.h"
14 #include "content/common/websocket_messages.h"
15 #include "ipc/ipc_message.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "url/gurl.h"
19 namespace content {
20 namespace {
22 // A mock of WebsocketHost which records received messages.
23 class MockWebSocketHost : public WebSocketHost {
24 public:
25 MockWebSocketHost(int routing_id,
26 WebSocketDispatcherHost* dispatcher,
27 net::URLRequestContext* url_request_context)
28 : WebSocketHost(routing_id, dispatcher, url_request_context) {
31 virtual ~MockWebSocketHost() {}
33 virtual bool OnMessageReceived(const IPC::Message& message,
34 bool* message_was_ok) OVERRIDE{
35 received_messages_.push_back(message);
36 return true;
39 std::vector<IPC::Message> received_messages_;
42 class WebSocketDispatcherHostTest : public ::testing::Test {
43 public:
44 WebSocketDispatcherHostTest() {
45 dispatcher_host_ =
46 new WebSocketDispatcherHost(
47 base::Bind(&WebSocketDispatcherHostTest::OnGetRequestContext,
48 base::Unretained(this)),
49 base::Bind(&WebSocketDispatcherHostTest::CreateWebSocketHost,
50 base::Unretained(this)));
53 virtual ~WebSocketDispatcherHostTest() {}
55 protected:
56 scoped_refptr<WebSocketDispatcherHost> dispatcher_host_;
58 // Stores allocated MockWebSocketHost instances. Doesn't take ownership of
59 // them.
60 std::vector<MockWebSocketHost*> mock_hosts_;
62 private:
63 net::URLRequestContext* OnGetRequestContext() {
64 return NULL;
67 WebSocketHost* CreateWebSocketHost(int routing_id) {
68 MockWebSocketHost* host =
69 new MockWebSocketHost(routing_id, dispatcher_host_.get(), NULL);
70 mock_hosts_.push_back(host);
71 return host;
75 TEST_F(WebSocketDispatcherHostTest, Construct) {
76 // Do nothing.
79 TEST_F(WebSocketDispatcherHostTest, UnrelatedMessage) {
80 bool message_was_ok = false;
81 IPC::Message message;
82 EXPECT_FALSE(dispatcher_host_->OnMessageReceived(message, &message_was_ok));
85 TEST_F(WebSocketDispatcherHostTest, AddChannelRequest) {
86 int routing_id = 123;
87 GURL socket_url("ws://example.com/test");
88 std::vector<std::string> requested_protocols;
89 requested_protocols.push_back("hello");
90 GURL origin("http://example.com/test");
91 WebSocketHostMsg_AddChannelRequest message(
92 routing_id, socket_url, requested_protocols, origin);
94 bool message_was_ok = false;
95 ASSERT_TRUE(dispatcher_host_->OnMessageReceived(message, &message_was_ok));
97 ASSERT_EQ(1U, mock_hosts_.size());
98 MockWebSocketHost* host = mock_hosts_[0];
100 ASSERT_EQ(1U, host->received_messages_.size());
101 const IPC::Message& forwarded_message = host->received_messages_[0];
102 EXPECT_EQ(WebSocketHostMsg_AddChannelRequest::ID, forwarded_message.type());
103 EXPECT_EQ(routing_id, forwarded_message.routing_id());
106 TEST_F(WebSocketDispatcherHostTest, SendFrameButNoHostYet) {
107 int routing_id = 123;
108 std::vector<char> data;
109 WebSocketMsg_SendFrame message(
110 routing_id, true, WEB_SOCKET_MESSAGE_TYPE_TEXT, data);
112 bool message_was_ok = false;
113 // Expected to be ignored.
114 EXPECT_TRUE(dispatcher_host_->OnMessageReceived(message, &message_was_ok));
116 EXPECT_EQ(0U, mock_hosts_.size());
119 TEST_F(WebSocketDispatcherHostTest, SendFrame) {
120 int routing_id = 123;
122 GURL socket_url("ws://example.com/test");
123 std::vector<std::string> requested_protocols;
124 requested_protocols.push_back("hello");
125 GURL origin("http://example.com/test");
126 WebSocketHostMsg_AddChannelRequest add_channel_message(
127 routing_id, socket_url, requested_protocols, origin);
129 bool message_was_ok = false;
131 ASSERT_TRUE(dispatcher_host_->OnMessageReceived(
132 add_channel_message, &message_was_ok));
134 std::vector<char> data;
135 WebSocketMsg_SendFrame send_frame_message(
136 routing_id, true, WEB_SOCKET_MESSAGE_TYPE_TEXT, data);
138 EXPECT_TRUE(dispatcher_host_->OnMessageReceived(
139 send_frame_message, &message_was_ok));
141 ASSERT_EQ(1U, mock_hosts_.size());
142 MockWebSocketHost* host = mock_hosts_[0];
144 ASSERT_EQ(2U, host->received_messages_.size());
146 const IPC::Message& forwarded_message = host->received_messages_[0];
147 EXPECT_EQ(WebSocketHostMsg_AddChannelRequest::ID, forwarded_message.type());
148 EXPECT_EQ(routing_id, forwarded_message.routing_id());
151 const IPC::Message& forwarded_message = host->received_messages_[1];
152 EXPECT_EQ(WebSocketMsg_SendFrame::ID, forwarded_message.type());
153 EXPECT_EQ(routing_id, forwarded_message.routing_id());
157 } // namespace
158 } // namespace content