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> | |
7 #include <vector> | 8 #include <vector> |
8 | 9 |
9 #include "base/bind.h" | 10 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
11 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
12 #include "content/browser/renderer_host/websocket_host.h" | 13 #include "content/browser/renderer_host/websocket_host.h" |
13 #include "content/common/websocket.h" | 14 #include "content/common/websocket.h" |
14 #include "content/common/websocket_messages.h" | 15 #include "content/common/websocket_messages.h" |
15 #include "ipc/ipc_message.h" | 16 #include "ipc/ipc_message.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
17 #include "url/gurl.h" | 18 #include "url/gurl.h" |
18 #include "url/origin.h" | 19 #include "url/origin.h" |
19 | 20 |
20 namespace content { | 21 namespace content { |
21 namespace { | 22 namespace { |
22 | 23 |
23 // This number is unlikely to occur by chance. | 24 // This number is unlikely to occur by chance. |
24 static const int kMagicRenderProcessId = 506116062; | 25 static const int kMagicRenderProcessId = 506116062; |
25 | 26 |
27 class WebSocketDispatcherHostTest; | |
28 | |
26 // A mock of WebsocketHost which records received messages. | 29 // A mock of WebsocketHost which records received messages. |
27 class MockWebSocketHost : public WebSocketHost { | 30 class MockWebSocketHost : public WebSocketHost { |
28 public: | 31 public: |
29 MockWebSocketHost(int routing_id, | 32 MockWebSocketHost(int routing_id, |
30 WebSocketDispatcherHost* dispatcher, | 33 WebSocketDispatcherHost* dispatcher, |
31 net::URLRequestContext* url_request_context) | 34 net::URLRequestContext* url_request_context, |
32 : WebSocketHost(routing_id, dispatcher, url_request_context) { | 35 WebSocketDispatcherHostTest* owner) |
36 : WebSocketHost(routing_id, dispatcher, url_request_context), | |
37 owner_(owner) { | |
33 } | 38 } |
34 | 39 |
35 virtual ~MockWebSocketHost() {} | 40 virtual ~MockWebSocketHost() {} |
36 | 41 |
37 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE{ | 42 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE { |
38 received_messages_.push_back(message); | 43 received_messages_.push_back(message); |
39 return true; | 44 return true; |
40 } | 45 } |
41 | 46 |
47 virtual void GoAway() OVERRIDE; | |
48 | |
42 std::vector<IPC::Message> received_messages_; | 49 std::vector<IPC::Message> received_messages_; |
50 WebSocketDispatcherHostTest* owner_; | |
43 }; | 51 }; |
44 | 52 |
45 class WebSocketDispatcherHostTest : public ::testing::Test { | 53 class WebSocketDispatcherHostTest : public ::testing::Test { |
46 public: | 54 public: |
47 WebSocketDispatcherHostTest() { | 55 WebSocketDispatcherHostTest() { |
48 dispatcher_host_ = new WebSocketDispatcherHost( | 56 dispatcher_host_ = new WebSocketDispatcherHost( |
49 kMagicRenderProcessId, | 57 kMagicRenderProcessId, |
50 base::Bind(&WebSocketDispatcherHostTest::OnGetRequestContext, | 58 base::Bind(&WebSocketDispatcherHostTest::OnGetRequestContext, |
51 base::Unretained(this)), | 59 base::Unretained(this)), |
52 base::Bind(&WebSocketDispatcherHostTest::CreateWebSocketHost, | 60 base::Bind(&WebSocketDispatcherHostTest::CreateWebSocketHost, |
53 base::Unretained(this))); | 61 base::Unretained(this))); |
54 } | 62 } |
55 | 63 |
56 virtual ~WebSocketDispatcherHostTest() {} | 64 virtual ~WebSocketDispatcherHostTest() {} |
Adam Rice
2014/07/25 12:20:16
We need to add dispatcher_host_ = NULL here to avo
yhirano
2014/07/28 03:54:06
CL15 suppresses adding gone_hosts_ in the destruct
| |
57 | 65 |
66 void GoAway(int routing_id) { | |
67 gone_hosts_.push_back(routing_id); | |
68 } | |
69 | |
58 protected: | 70 protected: |
59 scoped_refptr<WebSocketDispatcherHost> dispatcher_host_; | 71 scoped_refptr<WebSocketDispatcherHost> dispatcher_host_; |
60 | 72 |
61 // Stores allocated MockWebSocketHost instances. Doesn't take ownership of | 73 // Stores allocated MockWebSocketHost instances. Doesn't take ownership of |
62 // them. | 74 // them. |
63 std::vector<MockWebSocketHost*> mock_hosts_; | 75 std::vector<MockWebSocketHost*> mock_hosts_; |
76 std::vector<int> gone_hosts_; | |
64 | 77 |
65 private: | 78 private: |
66 net::URLRequestContext* OnGetRequestContext() { | 79 net::URLRequestContext* OnGetRequestContext() { |
67 return NULL; | 80 return NULL; |
68 } | 81 } |
69 | 82 |
70 WebSocketHost* CreateWebSocketHost(int routing_id) { | 83 WebSocketHost* CreateWebSocketHost(int routing_id) { |
71 MockWebSocketHost* host = | 84 MockWebSocketHost* host = |
72 new MockWebSocketHost(routing_id, dispatcher_host_.get(), NULL); | 85 new MockWebSocketHost(routing_id, dispatcher_host_.get(), NULL, this); |
73 mock_hosts_.push_back(host); | 86 mock_hosts_.push_back(host); |
74 return host; | 87 return host; |
75 } | 88 } |
76 }; | 89 }; |
77 | 90 |
91 void MockWebSocketHost::GoAway() { | |
92 owner_->GoAway(routing_id()); | |
93 } | |
94 | |
78 TEST_F(WebSocketDispatcherHostTest, Construct) { | 95 TEST_F(WebSocketDispatcherHostTest, Construct) { |
79 // Do nothing. | 96 // Do nothing. |
80 } | 97 } |
81 | 98 |
82 TEST_F(WebSocketDispatcherHostTest, UnrelatedMessage) { | 99 TEST_F(WebSocketDispatcherHostTest, UnrelatedMessage) { |
83 IPC::Message message; | 100 IPC::Message message; |
84 EXPECT_FALSE(dispatcher_host_->OnMessageReceived(message)); | 101 EXPECT_FALSE(dispatcher_host_->OnMessageReceived(message)); |
85 } | 102 } |
86 | 103 |
87 TEST_F(WebSocketDispatcherHostTest, RenderProcessIdGetter) { | 104 TEST_F(WebSocketDispatcherHostTest, RenderProcessIdGetter) { |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
149 EXPECT_EQ(WebSocketHostMsg_AddChannelRequest::ID, forwarded_message.type()); | 166 EXPECT_EQ(WebSocketHostMsg_AddChannelRequest::ID, forwarded_message.type()); |
150 EXPECT_EQ(routing_id, forwarded_message.routing_id()); | 167 EXPECT_EQ(routing_id, forwarded_message.routing_id()); |
151 } | 168 } |
152 { | 169 { |
153 const IPC::Message& forwarded_message = host->received_messages_[1]; | 170 const IPC::Message& forwarded_message = host->received_messages_[1]; |
154 EXPECT_EQ(WebSocketMsg_SendFrame::ID, forwarded_message.type()); | 171 EXPECT_EQ(WebSocketMsg_SendFrame::ID, forwarded_message.type()); |
155 EXPECT_EQ(routing_id, forwarded_message.routing_id()); | 172 EXPECT_EQ(routing_id, forwarded_message.routing_id()); |
156 } | 173 } |
157 } | 174 } |
158 | 175 |
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 | |
159 } // namespace | 200 } // namespace |
160 } // namespace content | 201 } // namespace content |
OLD | NEW |