OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 "content/browser/renderer_host/websocket_dispatcher_host.h" | 5 #include "content/browser/renderer_host/websocket_dispatcher_host.h" |
6 | 6 |
7 #include <algorithm> | |
8 #include <vector> | 7 #include <vector> |
9 | 8 |
10 #include "base/bind.h" | 9 #include "base/bind.h" |
11 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
12 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
13 #include "content/browser/renderer_host/websocket_host.h" | 12 #include "content/browser/renderer_host/websocket_host.h" |
14 #include "content/common/websocket.h" | 13 #include "content/common/websocket.h" |
15 #include "content/common/websocket_messages.h" | 14 #include "content/common/websocket_messages.h" |
16 #include "ipc/ipc_message.h" | 15 #include "ipc/ipc_message.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
18 #include "url/gurl.h" | 17 #include "url/gurl.h" |
19 #include "url/origin.h" | 18 #include "url/origin.h" |
20 | 19 |
21 namespace content { | 20 namespace content { |
22 namespace { | 21 namespace { |
23 | 22 |
24 // This number is unlikely to occur by chance. | 23 // This number is unlikely to occur by chance. |
25 static const int kMagicRenderProcessId = 506116062; | 24 static const int kMagicRenderProcessId = 506116062; |
26 | 25 |
27 class WebSocketDispatcherHostTest; | |
28 | |
29 // A mock of WebsocketHost which records received messages. | 26 // A mock of WebsocketHost which records received messages. |
30 class MockWebSocketHost : public WebSocketHost { | 27 class MockWebSocketHost : public WebSocketHost { |
31 public: | 28 public: |
32 MockWebSocketHost(int routing_id, | 29 MockWebSocketHost(int routing_id, |
33 WebSocketDispatcherHost* dispatcher, | 30 WebSocketDispatcherHost* dispatcher, |
34 net::URLRequestContext* url_request_context, | 31 net::URLRequestContext* url_request_context) |
35 WebSocketDispatcherHostTest* owner) | 32 : WebSocketHost(routing_id, dispatcher, url_request_context) { |
36 : WebSocketHost(routing_id, dispatcher, url_request_context), | |
37 owner_(owner) { | |
38 } | 33 } |
39 | 34 |
40 virtual ~MockWebSocketHost() {} | 35 virtual ~MockWebSocketHost() {} |
41 | 36 |
42 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE { | 37 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE{ |
43 received_messages_.push_back(message); | 38 received_messages_.push_back(message); |
44 return true; | 39 return true; |
45 } | 40 } |
46 | 41 |
47 virtual void GoAway() OVERRIDE; | |
48 | |
49 std::vector<IPC::Message> received_messages_; | 42 std::vector<IPC::Message> received_messages_; |
50 WebSocketDispatcherHostTest* owner_; | |
51 }; | 43 }; |
52 | 44 |
53 class WebSocketDispatcherHostTest : public ::testing::Test { | 45 class WebSocketDispatcherHostTest : public ::testing::Test { |
54 public: | 46 public: |
55 WebSocketDispatcherHostTest() { | 47 WebSocketDispatcherHostTest() { |
56 dispatcher_host_ = new WebSocketDispatcherHost( | 48 dispatcher_host_ = new WebSocketDispatcherHost( |
57 kMagicRenderProcessId, | 49 kMagicRenderProcessId, |
58 base::Bind(&WebSocketDispatcherHostTest::OnGetRequestContext, | 50 base::Bind(&WebSocketDispatcherHostTest::OnGetRequestContext, |
59 base::Unretained(this)), | 51 base::Unretained(this)), |
60 base::Bind(&WebSocketDispatcherHostTest::CreateWebSocketHost, | 52 base::Bind(&WebSocketDispatcherHostTest::CreateWebSocketHost, |
61 base::Unretained(this))); | 53 base::Unretained(this))); |
62 } | 54 } |
63 | 55 |
64 virtual ~WebSocketDispatcherHostTest() {} | 56 virtual ~WebSocketDispatcherHostTest() {} |
65 | 57 |
66 void GoAway(int routing_id) { | |
67 gone_hosts_.push_back(routing_id); | |
68 } | |
69 | |
70 protected: | 58 protected: |
71 scoped_refptr<WebSocketDispatcherHost> dispatcher_host_; | 59 scoped_refptr<WebSocketDispatcherHost> dispatcher_host_; |
72 | 60 |
73 // Stores allocated MockWebSocketHost instances. Doesn't take ownership of | 61 // Stores allocated MockWebSocketHost instances. Doesn't take ownership of |
74 // them. | 62 // them. |
75 std::vector<MockWebSocketHost*> mock_hosts_; | 63 std::vector<MockWebSocketHost*> mock_hosts_; |
76 std::vector<int> gone_hosts_; | |
77 | 64 |
78 private: | 65 private: |
79 net::URLRequestContext* OnGetRequestContext() { | 66 net::URLRequestContext* OnGetRequestContext() { |
80 return NULL; | 67 return NULL; |
81 } | 68 } |
82 | 69 |
83 WebSocketHost* CreateWebSocketHost(int routing_id) { | 70 WebSocketHost* CreateWebSocketHost(int routing_id) { |
84 MockWebSocketHost* host = | 71 MockWebSocketHost* host = |
85 new MockWebSocketHost(routing_id, dispatcher_host_.get(), NULL, this); | 72 new MockWebSocketHost(routing_id, dispatcher_host_.get(), NULL); |
86 mock_hosts_.push_back(host); | 73 mock_hosts_.push_back(host); |
87 return host; | 74 return host; |
88 } | 75 } |
89 }; | 76 }; |
90 | 77 |
91 void MockWebSocketHost::GoAway() { | |
92 owner_->GoAway(routing_id()); | |
93 } | |
94 | |
95 TEST_F(WebSocketDispatcherHostTest, Construct) { | 78 TEST_F(WebSocketDispatcherHostTest, Construct) { |
96 // Do nothing. | 79 // Do nothing. |
97 } | 80 } |
98 | 81 |
99 TEST_F(WebSocketDispatcherHostTest, UnrelatedMessage) { | 82 TEST_F(WebSocketDispatcherHostTest, UnrelatedMessage) { |
100 IPC::Message message; | 83 IPC::Message message; |
101 EXPECT_FALSE(dispatcher_host_->OnMessageReceived(message)); | 84 EXPECT_FALSE(dispatcher_host_->OnMessageReceived(message)); |
102 } | 85 } |
103 | 86 |
104 TEST_F(WebSocketDispatcherHostTest, RenderProcessIdGetter) { | 87 TEST_F(WebSocketDispatcherHostTest, RenderProcessIdGetter) { |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
166 EXPECT_EQ(WebSocketHostMsg_AddChannelRequest::ID, forwarded_message.type()); | 149 EXPECT_EQ(WebSocketHostMsg_AddChannelRequest::ID, forwarded_message.type()); |
167 EXPECT_EQ(routing_id, forwarded_message.routing_id()); | 150 EXPECT_EQ(routing_id, forwarded_message.routing_id()); |
168 } | 151 } |
169 { | 152 { |
170 const IPC::Message& forwarded_message = host->received_messages_[1]; | 153 const IPC::Message& forwarded_message = host->received_messages_[1]; |
171 EXPECT_EQ(WebSocketMsg_SendFrame::ID, forwarded_message.type()); | 154 EXPECT_EQ(WebSocketMsg_SendFrame::ID, forwarded_message.type()); |
172 EXPECT_EQ(routing_id, forwarded_message.routing_id()); | 155 EXPECT_EQ(routing_id, forwarded_message.routing_id()); |
173 } | 156 } |
174 } | 157 } |
175 | 158 |
176 TEST_F(WebSocketDispatcherHostTest, Destruct) { | |
177 WebSocketHostMsg_AddChannelRequest message1( | |
178 123, GURL("ws://example.com/test"), std::vector<std::string>(), | |
179 url::Origin("http://example.com"), -1); | |
180 WebSocketHostMsg_AddChannelRequest message2( | |
181 456, GURL("ws://example.com/test2"), std::vector<std::string>(), | |
182 url::Origin("http://example.com"), -1); | |
183 | |
184 ASSERT_TRUE(dispatcher_host_->OnMessageReceived(message1)); | |
185 ASSERT_TRUE(dispatcher_host_->OnMessageReceived(message2)); | |
186 | |
187 ASSERT_EQ(2u, mock_hosts_.size()); | |
188 | |
189 mock_hosts_.clear(); | |
190 dispatcher_host_ = NULL; | |
191 | |
192 ASSERT_EQ(2u, gone_hosts_.size()); | |
193 // The gone_hosts_ ordering is not predictable because it depends on the | |
194 // hash_map ordering. | |
195 std::sort(gone_hosts_.begin(), gone_hosts_.end()); | |
196 EXPECT_EQ(123, gone_hosts_[0]); | |
197 EXPECT_EQ(456, gone_hosts_[1]); | |
198 } | |
199 | |
200 } // namespace | 159 } // namespace |
201 } // namespace content | 160 } // namespace content |
OLD | NEW |