| 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 <algorithm> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
| 12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
| 13 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
| 14 #include "base/message_loop/message_loop.h" |
| 14 #include "content/browser/renderer_host/websocket_host.h" | 15 #include "content/browser/renderer_host/websocket_host.h" |
| 15 #include "content/common/websocket.h" | 16 #include "content/common/websocket.h" |
| 16 #include "content/common/websocket_messages.h" | 17 #include "content/common/websocket_messages.h" |
| 17 #include "ipc/ipc_message.h" | 18 #include "ipc/ipc_message.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 19 #include "url/gurl.h" | 20 #include "url/gurl.h" |
| 20 #include "url/origin.h" | 21 #include "url/origin.h" |
| 21 | 22 |
| 22 namespace content { | 23 namespace content { |
| 23 namespace { | 24 namespace { |
| 24 | 25 |
| 25 // This number is unlikely to occur by chance. | 26 // This number is unlikely to occur by chance. |
| 26 static const int kMagicRenderProcessId = 506116062; | 27 static const int kMagicRenderProcessId = 506116062; |
| 27 | 28 |
| 28 class WebSocketDispatcherHostTest; | 29 class WebSocketDispatcherHostTest; |
| 29 | 30 |
| 30 // A mock of WebsocketHost which records received messages. | 31 // A mock of WebsocketHost which records received messages. |
| 31 class MockWebSocketHost : public WebSocketHost { | 32 class MockWebSocketHost : public WebSocketHost { |
| 32 public: | 33 public: |
| 33 MockWebSocketHost(int routing_id, | 34 MockWebSocketHost(int routing_id, |
| 34 WebSocketDispatcherHost* dispatcher, | 35 WebSocketDispatcherHost* dispatcher, |
| 35 net::URLRequestContext* url_request_context, | 36 net::URLRequestContext* url_request_context, |
| 37 base::TimeDelta delay, |
| 36 WebSocketDispatcherHostTest* owner); | 38 WebSocketDispatcherHostTest* owner); |
| 37 | 39 |
| 38 ~MockWebSocketHost() override {} | 40 ~MockWebSocketHost() override {} |
| 39 | 41 |
| 40 bool OnMessageReceived(const IPC::Message& message) override { | 42 bool OnMessageReceived(const IPC::Message& message) override { |
| 41 received_messages_.push_back(message); | 43 received_messages_.push_back(message); |
| 42 return true; | 44 return true; |
| 43 } | 45 } |
| 44 | 46 |
| 45 void GoAway() override; | 47 void GoAway() override; |
| (...skipping 29 matching lines...) Expand all Loading... |
| 75 } | 77 } |
| 76 | 78 |
| 77 protected: | 79 protected: |
| 78 scoped_refptr<WebSocketDispatcherHost> dispatcher_host_; | 80 scoped_refptr<WebSocketDispatcherHost> dispatcher_host_; |
| 79 | 81 |
| 80 // Stores allocated MockWebSocketHost instances. Doesn't take ownership of | 82 // Stores allocated MockWebSocketHost instances. Doesn't take ownership of |
| 81 // them. | 83 // them. |
| 82 std::vector<MockWebSocketHost*> mock_hosts_; | 84 std::vector<MockWebSocketHost*> mock_hosts_; |
| 83 std::vector<int> gone_hosts_; | 85 std::vector<int> gone_hosts_; |
| 84 | 86 |
| 87 base::MessageLoop message_loop_; |
| 88 |
| 85 base::WeakPtrFactory<WebSocketDispatcherHostTest> weak_ptr_factory_; | 89 base::WeakPtrFactory<WebSocketDispatcherHostTest> weak_ptr_factory_; |
| 86 | 90 |
| 87 private: | 91 private: |
| 88 net::URLRequestContext* OnGetRequestContext() { | 92 net::URLRequestContext* OnGetRequestContext() { |
| 89 return NULL; | 93 return NULL; |
| 90 } | 94 } |
| 91 | 95 |
| 92 WebSocketHost* CreateWebSocketHost(int routing_id) { | 96 WebSocketHost* CreateWebSocketHost(int routing_id, base::TimeDelta delay) { |
| 93 MockWebSocketHost* host = | 97 MockWebSocketHost* host = new MockWebSocketHost( |
| 94 new MockWebSocketHost(routing_id, dispatcher_host_.get(), NULL, this); | 98 routing_id, dispatcher_host_.get(), NULL, delay, this); |
| 95 mock_hosts_.push_back(host); | 99 mock_hosts_.push_back(host); |
| 96 return host; | 100 return host; |
| 97 } | 101 } |
| 98 }; | 102 }; |
| 99 | 103 |
| 100 MockWebSocketHost::MockWebSocketHost( | 104 MockWebSocketHost::MockWebSocketHost( |
| 101 int routing_id, | 105 int routing_id, |
| 102 WebSocketDispatcherHost* dispatcher, | 106 WebSocketDispatcherHost* dispatcher, |
| 103 net::URLRequestContext* url_request_context, | 107 net::URLRequestContext* url_request_context, |
| 108 base::TimeDelta delay, |
| 104 WebSocketDispatcherHostTest* owner) | 109 WebSocketDispatcherHostTest* owner) |
| 105 : WebSocketHost(routing_id, dispatcher, url_request_context), | 110 : WebSocketHost(routing_id, dispatcher, url_request_context, delay), |
| 106 owner_(owner->GetWeakPtr()) {} | 111 owner_(owner->GetWeakPtr()) {} |
| 107 | 112 |
| 108 void MockWebSocketHost::GoAway() { | 113 void MockWebSocketHost::GoAway() { |
| 109 if (owner_) | 114 if (owner_) |
| 110 owner_->GoAway(routing_id()); | 115 owner_->GoAway(routing_id()); |
| 111 } | 116 } |
| 112 | 117 |
| 113 TEST_F(WebSocketDispatcherHostTest, Construct) { | 118 TEST_F(WebSocketDispatcherHostTest, Construct) { |
| 114 // Do nothing. | 119 // Do nothing. |
| 115 } | 120 } |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 210 ASSERT_EQ(2u, gone_hosts_.size()); | 215 ASSERT_EQ(2u, gone_hosts_.size()); |
| 211 // The gone_hosts_ ordering is not predictable because it depends on the | 216 // The gone_hosts_ ordering is not predictable because it depends on the |
| 212 // hash_map ordering. | 217 // hash_map ordering. |
| 213 std::sort(gone_hosts_.begin(), gone_hosts_.end()); | 218 std::sort(gone_hosts_.begin(), gone_hosts_.end()); |
| 214 EXPECT_EQ(123, gone_hosts_[0]); | 219 EXPECT_EQ(123, gone_hosts_[0]); |
| 215 EXPECT_EQ(456, gone_hosts_[1]); | 220 EXPECT_EQ(456, gone_hosts_[1]); |
| 216 } | 221 } |
| 217 | 222 |
| 218 } // namespace | 223 } // namespace |
| 219 } // namespace content | 224 } // namespace content |
| OLD | NEW |