| 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 <algorithm> | 5 #include <algorithm> |
| 6 #include <memory> | 6 #include <memory> |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "content/browser/websockets/websocket_manager.h" | 9 #include "content/browser/websockets/websocket_manager.h" |
| 10 #include "content/public/test/test_browser_thread_bundle.h" | 10 #include "content/public/test/test_browser_thread_bundle.h" |
| 11 #include "ipc/ipc_message.h" | 11 #include "ipc/ipc_message.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 #include "url/gurl.h" | 13 #include "url/gurl.h" |
| 14 #include "url/origin.h" | 14 #include "url/origin.h" |
| 15 | 15 |
| 16 namespace content { | 16 namespace content { |
| 17 namespace { | 17 namespace { |
| 18 | 18 |
| 19 // This number is unlikely to occur by chance. | 19 // This number is unlikely to occur by chance. |
| 20 static const int kMagicRenderProcessId = 506116062; | 20 static const int kMagicRenderProcessId = 506116062; |
| 21 | 21 |
| 22 class TestWebSocketImpl : public WebSocketImpl { | 22 class TestWebSocketImpl : public WebSocketImpl { |
| 23 public: | 23 public: |
| 24 TestWebSocketImpl(Delegate* delegate, | 24 TestWebSocketImpl(Delegate* delegate, |
| 25 mojom::WebSocketRequest request, | 25 blink::mojom::WebSocketRequest request, |
| 26 int frame_id, | 26 int frame_id, |
| 27 base::TimeDelta delay) | 27 base::TimeDelta delay) |
| 28 : WebSocketImpl(delegate, std::move(request), frame_id, delay) {} | 28 : WebSocketImpl(delegate, std::move(request), frame_id, delay) {} |
| 29 | 29 |
| 30 base::TimeDelta delay() const { return delay_; } | 30 base::TimeDelta delay() const { return delay_; } |
| 31 | 31 |
| 32 void SimulateConnectionError() { | 32 void SimulateConnectionError() { |
| 33 OnConnectionError(); | 33 OnConnectionError(); |
| 34 } | 34 } |
| 35 }; | 35 }; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 47 return num_pending_connections_; | 47 return num_pending_connections_; |
| 48 } | 48 } |
| 49 int64_t num_failed_connections() const { | 49 int64_t num_failed_connections() const { |
| 50 return num_current_failed_connections_ + num_previous_failed_connections_; | 50 return num_current_failed_connections_ + num_previous_failed_connections_; |
| 51 } | 51 } |
| 52 int64_t num_succeeded_connections() const { | 52 int64_t num_succeeded_connections() const { |
| 53 return num_current_succeeded_connections_ + | 53 return num_current_succeeded_connections_ + |
| 54 num_previous_succeeded_connections_; | 54 num_previous_succeeded_connections_; |
| 55 } | 55 } |
| 56 | 56 |
| 57 void DoCreateWebSocket(mojom::WebSocketRequest request) { | 57 void DoCreateWebSocket(blink::mojom::WebSocketRequest request) { |
| 58 WebSocketManager::DoCreateWebSocket(MSG_ROUTING_NONE, std::move(request)); | 58 WebSocketManager::DoCreateWebSocket(MSG_ROUTING_NONE, std::move(request)); |
| 59 } | 59 } |
| 60 | 60 |
| 61 private: | 61 private: |
| 62 WebSocketImpl* CreateWebSocketImpl(WebSocketImpl::Delegate* delegate, | 62 WebSocketImpl* CreateWebSocketImpl(WebSocketImpl::Delegate* delegate, |
| 63 mojom::WebSocketRequest request, | 63 blink::mojom::WebSocketRequest request, |
| 64 int frame_id, | 64 int frame_id, |
| 65 base::TimeDelta delay) override { | 65 base::TimeDelta delay) override { |
| 66 TestWebSocketImpl* impl = | 66 TestWebSocketImpl* impl = |
| 67 new TestWebSocketImpl(delegate, std::move(request), frame_id, delay); | 67 new TestWebSocketImpl(delegate, std::move(request), frame_id, delay); |
| 68 // We keep a vector of sockets here to track their creation order. | 68 // We keep a vector of sockets here to track their creation order. |
| 69 sockets_.push_back(impl); | 69 sockets_.push_back(impl); |
| 70 return impl; | 70 return impl; |
| 71 } | 71 } |
| 72 | 72 |
| 73 void OnLostConnectionToClient(WebSocketImpl* impl) override { | 73 void OnLostConnectionToClient(WebSocketImpl* impl) override { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 84 | 84 |
| 85 class WebSocketManagerTest : public ::testing::Test { | 85 class WebSocketManagerTest : public ::testing::Test { |
| 86 public: | 86 public: |
| 87 WebSocketManagerTest() | 87 WebSocketManagerTest() |
| 88 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) { | 88 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) { |
| 89 websocket_manager_.reset(new TestWebSocketManager()); | 89 websocket_manager_.reset(new TestWebSocketManager()); |
| 90 } | 90 } |
| 91 | 91 |
| 92 void AddMultipleChannels(int number_of_channels) { | 92 void AddMultipleChannels(int number_of_channels) { |
| 93 for (int i = 0; i < number_of_channels; ++i) { | 93 for (int i = 0; i < number_of_channels; ++i) { |
| 94 mojom::WebSocketPtr websocket; | 94 blink::mojom::WebSocketPtr websocket; |
| 95 websocket_manager_->DoCreateWebSocket(mojo::GetProxy(&websocket)); | 95 websocket_manager_->DoCreateWebSocket(mojo::GetProxy(&websocket)); |
| 96 } | 96 } |
| 97 } | 97 } |
| 98 | 98 |
| 99 void AddAndCancelMultipleChannels(int number_of_channels) { | 99 void AddAndCancelMultipleChannels(int number_of_channels) { |
| 100 for (int i = 0; i < number_of_channels; ++i) { | 100 for (int i = 0; i < number_of_channels; ++i) { |
| 101 mojom::WebSocketPtr websocket; | 101 blink::mojom::WebSocketPtr websocket; |
| 102 websocket_manager_->DoCreateWebSocket(mojo::GetProxy(&websocket)); | 102 websocket_manager_->DoCreateWebSocket(mojo::GetProxy(&websocket)); |
| 103 websocket_manager_->sockets().back()->SimulateConnectionError(); | 103 websocket_manager_->sockets().back()->SimulateConnectionError(); |
| 104 } | 104 } |
| 105 } | 105 } |
| 106 | 106 |
| 107 TestWebSocketManager* websocket_manager() { return websocket_manager_.get(); } | 107 TestWebSocketManager* websocket_manager() { return websocket_manager_.get(); } |
| 108 | 108 |
| 109 private: | 109 private: |
| 110 TestBrowserThreadBundle thread_bundle_; | 110 TestBrowserThreadBundle thread_bundle_; |
| 111 std::unique_ptr<TestWebSocketManager> websocket_manager_; | 111 std::unique_ptr<TestWebSocketManager> websocket_manager_; |
| 112 }; | 112 }; |
| 113 | 113 |
| 114 TEST_F(WebSocketManagerTest, Construct) { | 114 TEST_F(WebSocketManagerTest, Construct) { |
| 115 // Do nothing. | 115 // Do nothing. |
| 116 } | 116 } |
| 117 | 117 |
| 118 TEST_F(WebSocketManagerTest, CreateWebSocket) { | 118 TEST_F(WebSocketManagerTest, CreateWebSocket) { |
| 119 mojom::WebSocketPtr websocket; | 119 blink::mojom::WebSocketPtr websocket; |
| 120 | 120 |
| 121 websocket_manager()->DoCreateWebSocket(mojo::GetProxy(&websocket)); | 121 websocket_manager()->DoCreateWebSocket(mojo::GetProxy(&websocket)); |
| 122 | 122 |
| 123 EXPECT_EQ(1U, websocket_manager()->sockets().size()); | 123 EXPECT_EQ(1U, websocket_manager()->sockets().size()); |
| 124 } | 124 } |
| 125 | 125 |
| 126 TEST_F(WebSocketManagerTest, SendFrameButNotConnectedYet) { | 126 TEST_F(WebSocketManagerTest, SendFrameButNotConnectedYet) { |
| 127 mojom::WebSocketPtr websocket; | 127 blink::mojom::WebSocketPtr websocket; |
| 128 | 128 |
| 129 websocket_manager()->DoCreateWebSocket(mojo::GetProxy(&websocket)); | 129 websocket_manager()->DoCreateWebSocket(mojo::GetProxy(&websocket)); |
| 130 | 130 |
| 131 // This should not crash. | 131 // This should not crash. |
| 132 mojo::Array<uint8_t> data; | 132 mojo::Array<uint8_t> data; |
| 133 websocket->SendFrame( | 133 websocket->SendFrame( |
| 134 true, mojom::WebSocketMessageType::TEXT, std::move(data)); | 134 true, blink::mojom::WebSocketMessageType::TEXT, std::move(data)); |
| 135 } | 135 } |
| 136 | 136 |
| 137 TEST_F(WebSocketManagerTest, DelayFor4thPendingConnectionIsZero) { | 137 TEST_F(WebSocketManagerTest, DelayFor4thPendingConnectionIsZero) { |
| 138 AddMultipleChannels(4); | 138 AddMultipleChannels(4); |
| 139 | 139 |
| 140 EXPECT_EQ(4, websocket_manager()->num_pending_connections()); | 140 EXPECT_EQ(4, websocket_manager()->num_pending_connections()); |
| 141 EXPECT_EQ(0, websocket_manager()->num_failed_connections()); | 141 EXPECT_EQ(0, websocket_manager()->num_failed_connections()); |
| 142 EXPECT_EQ(0, websocket_manager()->num_succeeded_connections()); | 142 EXPECT_EQ(0, websocket_manager()->num_succeeded_connections()); |
| 143 | 143 |
| 144 ASSERT_EQ(4U, websocket_manager()->sockets().size()); | 144 ASSERT_EQ(4U, websocket_manager()->sockets().size()); |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 233 | 233 |
| 234 AddMultipleChannels(1); | 234 AddMultipleChannels(1); |
| 235 | 235 |
| 236 EXPECT_EQ(1, websocket_manager()->num_pending_connections()); | 236 EXPECT_EQ(1, websocket_manager()->num_pending_connections()); |
| 237 EXPECT_EQ(255, websocket_manager()->num_failed_connections()); | 237 EXPECT_EQ(255, websocket_manager()->num_failed_connections()); |
| 238 EXPECT_EQ(0, websocket_manager()->num_succeeded_connections()); | 238 EXPECT_EQ(0, websocket_manager()->num_succeeded_connections()); |
| 239 } | 239 } |
| 240 | 240 |
| 241 } // namespace | 241 } // namespace |
| 242 } // namespace content | 242 } // namespace content |
| OLD | NEW |