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" |
| 13 #include "base/memory/weak_ptr.h" |
12 #include "content/browser/renderer_host/websocket_host.h" | 14 #include "content/browser/renderer_host/websocket_host.h" |
13 #include "content/common/websocket.h" | 15 #include "content/common/websocket.h" |
14 #include "content/common/websocket_messages.h" | 16 #include "content/common/websocket_messages.h" |
15 #include "ipc/ipc_message.h" | 17 #include "ipc/ipc_message.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
17 #include "url/gurl.h" | 19 #include "url/gurl.h" |
18 #include "url/origin.h" | 20 #include "url/origin.h" |
19 | 21 |
20 namespace content { | 22 namespace content { |
21 namespace { | 23 namespace { |
22 | 24 |
23 // This number is unlikely to occur by chance. | 25 // This number is unlikely to occur by chance. |
24 static const int kMagicRenderProcessId = 506116062; | 26 static const int kMagicRenderProcessId = 506116062; |
25 | 27 |
| 28 class WebSocketDispatcherHostTest; |
| 29 |
26 // A mock of WebsocketHost which records received messages. | 30 // A mock of WebsocketHost which records received messages. |
27 class MockWebSocketHost : public WebSocketHost { | 31 class MockWebSocketHost : public WebSocketHost { |
28 public: | 32 public: |
29 MockWebSocketHost(int routing_id, | 33 MockWebSocketHost(int routing_id, |
30 WebSocketDispatcherHost* dispatcher, | 34 WebSocketDispatcherHost* dispatcher, |
31 net::URLRequestContext* url_request_context) | 35 net::URLRequestContext* url_request_context, |
32 : WebSocketHost(routing_id, dispatcher, url_request_context) { | 36 WebSocketDispatcherHostTest* owner); |
33 } | |
34 | 37 |
35 virtual ~MockWebSocketHost() {} | 38 virtual ~MockWebSocketHost() {} |
36 | 39 |
37 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE{ | 40 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE { |
38 received_messages_.push_back(message); | 41 received_messages_.push_back(message); |
39 return true; | 42 return true; |
40 } | 43 } |
41 | 44 |
| 45 virtual void GoAway() OVERRIDE; |
| 46 |
42 std::vector<IPC::Message> received_messages_; | 47 std::vector<IPC::Message> received_messages_; |
| 48 base::WeakPtr<WebSocketDispatcherHostTest> owner_; |
43 }; | 49 }; |
44 | 50 |
45 class WebSocketDispatcherHostTest : public ::testing::Test { | 51 class WebSocketDispatcherHostTest : public ::testing::Test { |
46 public: | 52 public: |
47 WebSocketDispatcherHostTest() { | 53 WebSocketDispatcherHostTest() |
| 54 : weak_ptr_factory_(this) { |
48 dispatcher_host_ = new WebSocketDispatcherHost( | 55 dispatcher_host_ = new WebSocketDispatcherHost( |
49 kMagicRenderProcessId, | 56 kMagicRenderProcessId, |
50 base::Bind(&WebSocketDispatcherHostTest::OnGetRequestContext, | 57 base::Bind(&WebSocketDispatcherHostTest::OnGetRequestContext, |
51 base::Unretained(this)), | 58 base::Unretained(this)), |
52 base::Bind(&WebSocketDispatcherHostTest::CreateWebSocketHost, | 59 base::Bind(&WebSocketDispatcherHostTest::CreateWebSocketHost, |
53 base::Unretained(this))); | 60 base::Unretained(this))); |
54 } | 61 } |
55 | 62 |
56 virtual ~WebSocketDispatcherHostTest() {} | 63 virtual ~WebSocketDispatcherHostTest() { |
| 64 // We need to invalidate the issued WeakPtrs at the beginning of the |
| 65 // destructor in order not to access destructed member variables. |
| 66 weak_ptr_factory_.InvalidateWeakPtrs(); |
| 67 } |
| 68 |
| 69 void GoAway(int routing_id) { |
| 70 gone_hosts_.push_back(routing_id); |
| 71 } |
| 72 |
| 73 base::WeakPtr<WebSocketDispatcherHostTest> GetWeakPtr() { |
| 74 return weak_ptr_factory_.GetWeakPtr(); |
| 75 } |
57 | 76 |
58 protected: | 77 protected: |
59 scoped_refptr<WebSocketDispatcherHost> dispatcher_host_; | 78 scoped_refptr<WebSocketDispatcherHost> dispatcher_host_; |
60 | 79 |
61 // Stores allocated MockWebSocketHost instances. Doesn't take ownership of | 80 // Stores allocated MockWebSocketHost instances. Doesn't take ownership of |
62 // them. | 81 // them. |
63 std::vector<MockWebSocketHost*> mock_hosts_; | 82 std::vector<MockWebSocketHost*> mock_hosts_; |
| 83 std::vector<int> gone_hosts_; |
| 84 |
| 85 base::WeakPtrFactory<WebSocketDispatcherHostTest> weak_ptr_factory_; |
64 | 86 |
65 private: | 87 private: |
66 net::URLRequestContext* OnGetRequestContext() { | 88 net::URLRequestContext* OnGetRequestContext() { |
67 return NULL; | 89 return NULL; |
68 } | 90 } |
69 | 91 |
70 WebSocketHost* CreateWebSocketHost(int routing_id) { | 92 WebSocketHost* CreateWebSocketHost(int routing_id) { |
71 MockWebSocketHost* host = | 93 MockWebSocketHost* host = |
72 new MockWebSocketHost(routing_id, dispatcher_host_.get(), NULL); | 94 new MockWebSocketHost(routing_id, dispatcher_host_.get(), NULL, this); |
73 mock_hosts_.push_back(host); | 95 mock_hosts_.push_back(host); |
74 return host; | 96 return host; |
75 } | 97 } |
76 }; | 98 }; |
77 | 99 |
| 100 MockWebSocketHost::MockWebSocketHost( |
| 101 int routing_id, |
| 102 WebSocketDispatcherHost* dispatcher, |
| 103 net::URLRequestContext* url_request_context, |
| 104 WebSocketDispatcherHostTest* owner) |
| 105 : WebSocketHost(routing_id, dispatcher, url_request_context), |
| 106 owner_(owner->GetWeakPtr()) {} |
| 107 |
| 108 void MockWebSocketHost::GoAway() { |
| 109 if (owner_) |
| 110 owner_->GoAway(routing_id()); |
| 111 } |
| 112 |
78 TEST_F(WebSocketDispatcherHostTest, Construct) { | 113 TEST_F(WebSocketDispatcherHostTest, Construct) { |
79 // Do nothing. | 114 // Do nothing. |
80 } | 115 } |
81 | 116 |
82 TEST_F(WebSocketDispatcherHostTest, UnrelatedMessage) { | 117 TEST_F(WebSocketDispatcherHostTest, UnrelatedMessage) { |
83 IPC::Message message; | 118 IPC::Message message; |
84 EXPECT_FALSE(dispatcher_host_->OnMessageReceived(message)); | 119 EXPECT_FALSE(dispatcher_host_->OnMessageReceived(message)); |
85 } | 120 } |
86 | 121 |
87 TEST_F(WebSocketDispatcherHostTest, RenderProcessIdGetter) { | 122 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()); | 184 EXPECT_EQ(WebSocketHostMsg_AddChannelRequest::ID, forwarded_message.type()); |
150 EXPECT_EQ(routing_id, forwarded_message.routing_id()); | 185 EXPECT_EQ(routing_id, forwarded_message.routing_id()); |
151 } | 186 } |
152 { | 187 { |
153 const IPC::Message& forwarded_message = host->received_messages_[1]; | 188 const IPC::Message& forwarded_message = host->received_messages_[1]; |
154 EXPECT_EQ(WebSocketMsg_SendFrame::ID, forwarded_message.type()); | 189 EXPECT_EQ(WebSocketMsg_SendFrame::ID, forwarded_message.type()); |
155 EXPECT_EQ(routing_id, forwarded_message.routing_id()); | 190 EXPECT_EQ(routing_id, forwarded_message.routing_id()); |
156 } | 191 } |
157 } | 192 } |
158 | 193 |
| 194 TEST_F(WebSocketDispatcherHostTest, Destruct) { |
| 195 WebSocketHostMsg_AddChannelRequest message1( |
| 196 123, GURL("ws://example.com/test"), std::vector<std::string>(), |
| 197 url::Origin("http://example.com"), -1); |
| 198 WebSocketHostMsg_AddChannelRequest message2( |
| 199 456, GURL("ws://example.com/test2"), std::vector<std::string>(), |
| 200 url::Origin("http://example.com"), -1); |
| 201 |
| 202 ASSERT_TRUE(dispatcher_host_->OnMessageReceived(message1)); |
| 203 ASSERT_TRUE(dispatcher_host_->OnMessageReceived(message2)); |
| 204 |
| 205 ASSERT_EQ(2u, mock_hosts_.size()); |
| 206 |
| 207 mock_hosts_.clear(); |
| 208 dispatcher_host_ = NULL; |
| 209 |
| 210 ASSERT_EQ(2u, gone_hosts_.size()); |
| 211 // The gone_hosts_ ordering is not predictable because it depends on the |
| 212 // hash_map ordering. |
| 213 std::sort(gone_hosts_.begin(), gone_hosts_.end()); |
| 214 EXPECT_EQ(123, gone_hosts_[0]); |
| 215 EXPECT_EQ(456, gone_hosts_[1]); |
| 216 } |
| 217 |
159 } // namespace | 218 } // namespace |
160 } // namespace content | 219 } // namespace content |
OLD | NEW |