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 |
52 : public ::testing::Test, | |
53 public base::SupportsWeakPtr<WebSocketDispatcherHostTest> { | |
Adam Rice
2014/07/28 07:04:48
You don't need to use base::SupportsWeakPtr and ba
yhirano
2014/07/28 07:12:48
Thanks, done.
| |
46 public: | 54 public: |
47 WebSocketDispatcherHostTest() { | 55 WebSocketDispatcherHostTest() |
56 : weak_ptr_factory_(this) { | |
48 dispatcher_host_ = new WebSocketDispatcherHost( | 57 dispatcher_host_ = new WebSocketDispatcherHost( |
49 kMagicRenderProcessId, | 58 kMagicRenderProcessId, |
50 base::Bind(&WebSocketDispatcherHostTest::OnGetRequestContext, | 59 base::Bind(&WebSocketDispatcherHostTest::OnGetRequestContext, |
51 base::Unretained(this)), | 60 base::Unretained(this)), |
52 base::Bind(&WebSocketDispatcherHostTest::CreateWebSocketHost, | 61 base::Bind(&WebSocketDispatcherHostTest::CreateWebSocketHost, |
53 base::Unretained(this))); | 62 base::Unretained(this))); |
54 } | 63 } |
55 | 64 |
56 virtual ~WebSocketDispatcherHostTest() {} | 65 virtual ~WebSocketDispatcherHostTest() { |
66 // We need to invalidate the issued WeakPtrs at the beginning of the | |
67 // destructor in order not to access destructed member variables. | |
68 weak_ptr_factory_.InvalidateWeakPtrs(); | |
69 } | |
70 | |
71 void GoAway(int routing_id) { | |
72 gone_hosts_.push_back(routing_id); | |
73 } | |
74 | |
75 base::WeakPtr<WebSocketDispatcherHostTest> GetWeakPtr() { | |
76 return weak_ptr_factory_.GetWeakPtr(); | |
77 } | |
57 | 78 |
58 protected: | 79 protected: |
59 scoped_refptr<WebSocketDispatcherHost> dispatcher_host_; | 80 scoped_refptr<WebSocketDispatcherHost> dispatcher_host_; |
60 | 81 |
61 // Stores allocated MockWebSocketHost instances. Doesn't take ownership of | 82 // Stores allocated MockWebSocketHost instances. Doesn't take ownership of |
62 // them. | 83 // them. |
63 std::vector<MockWebSocketHost*> mock_hosts_; | 84 std::vector<MockWebSocketHost*> mock_hosts_; |
85 std::vector<int> gone_hosts_; | |
86 | |
87 base::WeakPtrFactory<WebSocketDispatcherHostTest> weak_ptr_factory_; | |
64 | 88 |
65 private: | 89 private: |
66 net::URLRequestContext* OnGetRequestContext() { | 90 net::URLRequestContext* OnGetRequestContext() { |
67 return NULL; | 91 return NULL; |
68 } | 92 } |
69 | 93 |
70 WebSocketHost* CreateWebSocketHost(int routing_id) { | 94 WebSocketHost* CreateWebSocketHost(int routing_id) { |
71 MockWebSocketHost* host = | 95 MockWebSocketHost* host = |
72 new MockWebSocketHost(routing_id, dispatcher_host_.get(), NULL); | 96 new MockWebSocketHost(routing_id, dispatcher_host_.get(), NULL, this); |
73 mock_hosts_.push_back(host); | 97 mock_hosts_.push_back(host); |
74 return host; | 98 return host; |
75 } | 99 } |
76 }; | 100 }; |
77 | 101 |
102 MockWebSocketHost::MockWebSocketHost( | |
103 int routing_id, | |
104 WebSocketDispatcherHost* dispatcher, | |
105 net::URLRequestContext* url_request_context, | |
106 WebSocketDispatcherHostTest* owner) | |
107 : WebSocketHost(routing_id, dispatcher, url_request_context), | |
108 owner_(owner->GetWeakPtr()) {} | |
109 | |
110 void MockWebSocketHost::GoAway() { | |
111 if (owner_) | |
112 owner_->GoAway(routing_id()); | |
113 } | |
114 | |
78 TEST_F(WebSocketDispatcherHostTest, Construct) { | 115 TEST_F(WebSocketDispatcherHostTest, Construct) { |
79 // Do nothing. | 116 // Do nothing. |
80 } | 117 } |
81 | 118 |
82 TEST_F(WebSocketDispatcherHostTest, UnrelatedMessage) { | 119 TEST_F(WebSocketDispatcherHostTest, UnrelatedMessage) { |
83 IPC::Message message; | 120 IPC::Message message; |
84 EXPECT_FALSE(dispatcher_host_->OnMessageReceived(message)); | 121 EXPECT_FALSE(dispatcher_host_->OnMessageReceived(message)); |
85 } | 122 } |
86 | 123 |
87 TEST_F(WebSocketDispatcherHostTest, RenderProcessIdGetter) { | 124 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()); | 186 EXPECT_EQ(WebSocketHostMsg_AddChannelRequest::ID, forwarded_message.type()); |
150 EXPECT_EQ(routing_id, forwarded_message.routing_id()); | 187 EXPECT_EQ(routing_id, forwarded_message.routing_id()); |
151 } | 188 } |
152 { | 189 { |
153 const IPC::Message& forwarded_message = host->received_messages_[1]; | 190 const IPC::Message& forwarded_message = host->received_messages_[1]; |
154 EXPECT_EQ(WebSocketMsg_SendFrame::ID, forwarded_message.type()); | 191 EXPECT_EQ(WebSocketMsg_SendFrame::ID, forwarded_message.type()); |
155 EXPECT_EQ(routing_id, forwarded_message.routing_id()); | 192 EXPECT_EQ(routing_id, forwarded_message.routing_id()); |
156 } | 193 } |
157 } | 194 } |
158 | 195 |
196 TEST_F(WebSocketDispatcherHostTest, Destruct) { | |
197 WebSocketHostMsg_AddChannelRequest message1( | |
198 123, GURL("ws://example.com/test"), std::vector<std::string>(), | |
199 url::Origin("http://example.com"), -1); | |
200 WebSocketHostMsg_AddChannelRequest message2( | |
201 456, GURL("ws://example.com/test2"), std::vector<std::string>(), | |
202 url::Origin("http://example.com"), -1); | |
203 | |
204 ASSERT_TRUE(dispatcher_host_->OnMessageReceived(message1)); | |
205 ASSERT_TRUE(dispatcher_host_->OnMessageReceived(message2)); | |
206 | |
207 ASSERT_EQ(2u, mock_hosts_.size()); | |
208 | |
209 mock_hosts_.clear(); | |
210 dispatcher_host_ = NULL; | |
211 | |
212 ASSERT_EQ(2u, gone_hosts_.size()); | |
213 // The gone_hosts_ ordering is not predictable because it depends on the | |
214 // hash_map ordering. | |
215 std::sort(gone_hosts_.begin(), gone_hosts_.end()); | |
216 EXPECT_EQ(123, gone_hosts_[0]); | |
217 EXPECT_EQ(456, gone_hosts_[1]); | |
218 } | |
219 | |
159 } // namespace | 220 } // namespace |
160 } // namespace content | 221 } // namespace content |
OLD | NEW |