Chromium Code Reviews| Index: content/browser/renderer_host/websocket_dispatcher_host_unittest.cc |
| diff --git a/content/browser/renderer_host/websocket_dispatcher_host_unittest.cc b/content/browser/renderer_host/websocket_dispatcher_host_unittest.cc |
| index e1506d99dd123136367a124b55897f31e0c4bf16..2e1651b8d2818af5ff236bfe4b7111443f075cc1 100644 |
| --- a/content/browser/renderer_host/websocket_dispatcher_host_unittest.cc |
| +++ b/content/browser/renderer_host/websocket_dispatcher_host_unittest.cc |
| @@ -4,6 +4,7 @@ |
| #include "content/browser/renderer_host/websocket_dispatcher_host.h" |
| +#include <algorithm> |
| #include <vector> |
| #include "base/bind.h" |
| @@ -23,28 +24,36 @@ namespace { |
| // This number is unlikely to occur by chance. |
| static const int kMagicRenderProcessId = 506116062; |
| +class WebSocketDispatcherHostTest; |
| + |
| // A mock of WebsocketHost which records received messages. |
| class MockWebSocketHost : public WebSocketHost { |
| public: |
| MockWebSocketHost(int routing_id, |
| WebSocketDispatcherHost* dispatcher, |
| - net::URLRequestContext* url_request_context) |
| - : WebSocketHost(routing_id, dispatcher, url_request_context) { |
| + net::URLRequestContext* url_request_context, |
| + WebSocketDispatcherHostTest* owner) |
| + : WebSocketHost(routing_id, dispatcher, url_request_context), |
| + owner_(owner) { |
| } |
| virtual ~MockWebSocketHost() {} |
| - virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE{ |
| + virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE { |
| received_messages_.push_back(message); |
| return true; |
| } |
| + virtual void GoAway() OVERRIDE; |
| + |
| std::vector<IPC::Message> received_messages_; |
| + WebSocketDispatcherHostTest* owner_; |
| }; |
| class WebSocketDispatcherHostTest : public ::testing::Test { |
| public: |
| - WebSocketDispatcherHostTest() { |
| + WebSocketDispatcherHostTest() |
| + : on_destruction_(false) { |
| dispatcher_host_ = new WebSocketDispatcherHost( |
| kMagicRenderProcessId, |
| base::Bind(&WebSocketDispatcherHostTest::OnGetRequestContext, |
| @@ -53,7 +62,14 @@ class WebSocketDispatcherHostTest : public ::testing::Test { |
| base::Unretained(this))); |
| } |
| - virtual ~WebSocketDispatcherHostTest() {} |
| + virtual ~WebSocketDispatcherHostTest() { |
| + on_destruction_ = true; |
|
Adam Rice
2014/07/28 04:29:22
This doesn't seem safe to me. How about changing t
yhirano
2014/07/28 05:08:52
Done.
|
| + } |
| + |
| + void GoAway(int routing_id) { |
| + if (!on_destruction_) |
| + gone_hosts_.push_back(routing_id); |
| + } |
| protected: |
| scoped_refptr<WebSocketDispatcherHost> dispatcher_host_; |
| @@ -61,6 +77,8 @@ class WebSocketDispatcherHostTest : public ::testing::Test { |
| // Stores allocated MockWebSocketHost instances. Doesn't take ownership of |
| // them. |
| std::vector<MockWebSocketHost*> mock_hosts_; |
| + std::vector<int> gone_hosts_; |
| + bool on_destruction_; |
| private: |
| net::URLRequestContext* OnGetRequestContext() { |
| @@ -69,12 +87,16 @@ class WebSocketDispatcherHostTest : public ::testing::Test { |
| WebSocketHost* CreateWebSocketHost(int routing_id) { |
| MockWebSocketHost* host = |
| - new MockWebSocketHost(routing_id, dispatcher_host_.get(), NULL); |
| + new MockWebSocketHost(routing_id, dispatcher_host_.get(), NULL, this); |
| mock_hosts_.push_back(host); |
| return host; |
| } |
| }; |
| +void MockWebSocketHost::GoAway() { |
| + owner_->GoAway(routing_id()); |
| +} |
| + |
| TEST_F(WebSocketDispatcherHostTest, Construct) { |
| // Do nothing. |
| } |
| @@ -156,5 +178,29 @@ TEST_F(WebSocketDispatcherHostTest, SendFrame) { |
| } |
| } |
| +TEST_F(WebSocketDispatcherHostTest, Destruct) { |
| + WebSocketHostMsg_AddChannelRequest message1( |
| + 123, GURL("ws://example.com/test"), std::vector<std::string>(), |
| + url::Origin("http://example.com"), -1); |
| + WebSocketHostMsg_AddChannelRequest message2( |
| + 456, GURL("ws://example.com/test2"), std::vector<std::string>(), |
| + url::Origin("http://example.com"), -1); |
| + |
| + ASSERT_TRUE(dispatcher_host_->OnMessageReceived(message1)); |
| + ASSERT_TRUE(dispatcher_host_->OnMessageReceived(message2)); |
| + |
| + ASSERT_EQ(2u, mock_hosts_.size()); |
| + |
| + mock_hosts_.clear(); |
| + dispatcher_host_ = NULL; |
| + |
| + ASSERT_EQ(2u, gone_hosts_.size()); |
| + // The gone_hosts_ ordering is not predictable because it depends on the |
| + // hash_map ordering. |
| + std::sort(gone_hosts_.begin(), gone_hosts_.end()); |
| + EXPECT_EQ(123, gone_hosts_[0]); |
| + EXPECT_EQ(456, gone_hosts_[1]); |
| +} |
| + |
| } // namespace |
| } // namespace content |