| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "base/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/macros.h" | 6 #include "base/macros.h" |
| 7 #include "base/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
| 8 #include "base/synchronization/waitable_event.h" | 8 #include "base/synchronization/waitable_event.h" |
| 9 #include "content/browser/message_port_service.h" | 9 #include "content/browser/message_port_service.h" |
| 10 #include "content/public/browser/browser_thread.h" | 10 #include "content/public/browser/browser_thread.h" |
| 11 #include "content/public/browser/message_port_delegate.h" | 11 #include "content/public/browser/message_port_delegate.h" |
| 12 #include "content/public/browser/message_port_provider.h" | 12 #include "content/public/browser/message_port_provider.h" |
| 13 #include "content/public/browser/web_contents.h" | 13 #include "content/public/browser/web_contents.h" |
| 14 #include "content/public/test/browser_test_utils.h" | 14 #include "content/public/test/browser_test_utils.h" |
| 15 #include "content/public/test/content_browser_test.h" | 15 #include "content/public/test/content_browser_test.h" |
| 16 #include "content/public/test/content_browser_test_utils.h" | 16 #include "content/public/test/content_browser_test_utils.h" |
| 17 #include "content/shell/browser/shell.h" | 17 #include "content/shell/browser/shell.h" |
| 18 | 18 |
| 19 namespace content { | 19 namespace content { |
| 20 | 20 |
| 21 // This test verifies the functionality of the Message Port Provider API. | 21 // This test verifies the functionality of the Message Port Provider API. |
| 22 | 22 |
| 23 // A mock class for testing message port provider. | 23 // A mock class for testing message port provider. |
| 24 class MockMessagePortDelegate : public MessagePortDelegate { | 24 class MockMessagePortDelegate : public MessagePortDelegate { |
| 25 public: | 25 public: |
| 26 // A container to hold received messages | 26 // A container to hold received messages |
| 27 struct Message { | 27 struct Message { |
| 28 int route_id; // the routing id of the target port | 28 int route_id; // the routing id of the target port |
| 29 MessagePortMessage data; // the message data | 29 base::string16 data; // the message data |
| 30 std::vector<TransferredMessagePort> sent_ports; // any transferred ports | 30 std::vector<int> sent_ports; // any transferred ports |
| 31 }; | 31 }; |
| 32 | 32 |
| 33 typedef std::vector<Message> Messages; | 33 typedef std::vector<Message> Messages; |
| 34 | 34 |
| 35 MockMessagePortDelegate() { } | 35 MockMessagePortDelegate() { } |
| 36 ~MockMessagePortDelegate() override { } | 36 ~MockMessagePortDelegate() override { } |
| 37 | 37 |
| 38 // MessagePortDelegate implementation | 38 // MessagePortDelegate implementation |
| 39 void SendMessage( | 39 void SendMessage( |
| 40 int route_id, | 40 int route_id, |
| 41 const MessagePortMessage& message, | 41 const base::string16& message, |
| 42 const std::vector<TransferredMessagePort>& sent_message_ports) override { | 42 const std::vector<int>& sent_message_ports) override { |
| 43 Message m; | 43 Message m; |
| 44 m.route_id = route_id; | 44 m.route_id = route_id; |
| 45 m.data = message; | 45 m.data = message; |
| 46 m.sent_ports = sent_message_ports; | 46 m.sent_ports = sent_message_ports; |
| 47 messages_.push_back(m); | 47 messages_.push_back(m); |
| 48 } | 48 } |
| 49 | 49 |
| 50 void SendMessagesAreQueued(int route_id) override { } | 50 void SendMessagesAreQueued(int route_id) override { } |
| 51 | 51 |
| 52 const Messages& getReceivedMessages() { | 52 const Messages& getReceivedMessages() { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 70 " onmessage = function (e) { document.title = e.data; }" | 70 " onmessage = function (e) { document.title = e.data; }" |
| 71 " </script>" | 71 " </script>" |
| 72 "</body></html>"; | 72 "</body></html>"; |
| 73 const base::string16 target_origin(base::UTF8ToUTF16("http://baseurl")); | 73 const base::string16 target_origin(base::UTF8ToUTF16("http://baseurl")); |
| 74 const GURL base_url(target_origin); | 74 const GURL base_url(target_origin); |
| 75 const GURL history_url; | 75 const GURL history_url; |
| 76 // Load data. Blocks until it is done. | 76 // Load data. Blocks until it is done. |
| 77 content::LoadDataWithBaseURL(shell(), history_url, data, base_url); | 77 content::LoadDataWithBaseURL(shell(), history_url, data, base_url); |
| 78 const base::string16 source_origin(base::UTF8ToUTF16("source")); | 78 const base::string16 source_origin(base::UTF8ToUTF16("source")); |
| 79 const base::string16 message(base::UTF8ToUTF16("success")); | 79 const base::string16 message(base::UTF8ToUTF16("success")); |
| 80 const std::vector<TransferredMessagePort> ports; | 80 const std::vector<int> ports; |
| 81 content::TitleWatcher title_watcher(shell()->web_contents(), message); | 81 content::TitleWatcher title_watcher(shell()->web_contents(), message); |
| 82 MessagePortProvider::PostMessageToFrame(shell()->web_contents(), | 82 MessagePortProvider::PostMessageToFrame(shell()->web_contents(), |
| 83 source_origin, | 83 source_origin, |
| 84 target_origin, | 84 target_origin, |
| 85 message, | 85 message, |
| 86 ports); | 86 ports); |
| 87 EXPECT_EQ(message, title_watcher.WaitAndGetTitle()); | 87 EXPECT_EQ(message, title_watcher.WaitAndGetTitle()); |
| 88 } | 88 } |
| 89 | 89 |
| 90 namespace { | 90 namespace { |
| 91 | 91 |
| 92 void VerifyCreateChannelOnIOThread(base::WaitableEvent* event) { | 92 void VerifyCreateChannelOnIOThread(base::WaitableEvent* event) { |
| 93 | 93 |
| 94 const base::char16 MESSAGE1[] = { 0x1000, 0 }; | 94 const base::char16 MESSAGE1[] = { 0x1000, 0 }; |
| 95 const base::char16 MESSAGE2[] = { 0x1001, 0 }; | 95 const base::char16 MESSAGE2[] = { 0x1001, 0 }; |
| 96 | 96 |
| 97 MockMessagePortDelegate delegate; | 97 MockMessagePortDelegate delegate; |
| 98 int port1; | 98 int port1; |
| 99 int port2; | 99 int port2; |
| 100 | 100 |
| 101 MessagePortProvider::CreateMessageChannel(&delegate, &port1, &port2); | 101 MessagePortProvider::CreateMessageChannel(&delegate, &port1, &port2); |
| 102 MessagePortService* service = MessagePortService::GetInstance(); | 102 MessagePortService* service = MessagePortService::GetInstance(); |
| 103 // Send a message to port1 transferring no ports. | 103 // Send a message to port1 transferring no ports. |
| 104 std::vector<TransferredMessagePort> sent_ports; | 104 std::vector<int> sent_ports; |
| 105 service->PostMessage(port1, MessagePortMessage(base::string16(MESSAGE1)), | 105 service->PostMessage(port1, base::string16(MESSAGE1), sent_ports); |
| 106 sent_ports); | |
| 107 // Verify that message is received | 106 // Verify that message is received |
| 108 const MockMessagePortDelegate::Messages& received = | 107 const MockMessagePortDelegate::Messages& received = |
| 109 delegate.getReceivedMessages(); | 108 delegate.getReceivedMessages(); |
| 110 EXPECT_EQ(received.size(), 1u); | 109 EXPECT_EQ(received.size(), 1u); |
| 111 // Verify that message sent to port1 is received by entangled port, which is | 110 // Verify that message sent to port1 is received by entangled port, which is |
| 112 // port2. | 111 // port2. |
| 113 EXPECT_EQ(received[0].route_id, port2); | 112 EXPECT_EQ(received[0].route_id, port2); |
| 114 EXPECT_EQ(received[0].data.message_as_string, MESSAGE1); | 113 EXPECT_EQ(received[0].data, MESSAGE1); |
| 115 EXPECT_EQ(received[0].sent_ports.size(), 0u); | 114 EXPECT_EQ(received[0].sent_ports.size(), 0u); |
| 116 | 115 |
| 117 // Create a new channel, and transfer one of its ports to port2, making sure | 116 // Create a new channel, and transfer one of its ports to port2, making sure |
| 118 // the transferred port is received. | 117 // the transferred port is received. |
| 119 TransferredMessagePort port3; | 118 int port3; |
| 120 TransferredMessagePort port4; | 119 int port4; |
| 121 MessagePortProvider::CreateMessageChannel(&delegate, &port3.id, &port4.id); | 120 MessagePortProvider::CreateMessageChannel(&delegate, &port3, &port4); |
| 122 sent_ports.push_back(port3); | 121 sent_ports.push_back(port3); |
| 123 service->PostMessage(port1, MessagePortMessage(base::string16(MESSAGE2)), | 122 service->PostMessage(port1, base::string16(MESSAGE2), sent_ports); |
| 124 sent_ports); | |
| 125 EXPECT_EQ(received.size(), 2u); | 123 EXPECT_EQ(received.size(), 2u); |
| 126 EXPECT_EQ(received[1].route_id, port2); | 124 EXPECT_EQ(received[1].route_id, port2); |
| 127 EXPECT_EQ(received[1].data.message_as_string, MESSAGE2); | 125 EXPECT_EQ(received[1].data, MESSAGE2); |
| 128 EXPECT_EQ(received[1].sent_ports.size(), 1u); | 126 EXPECT_EQ(received[1].sent_ports.size(), 1u); |
| 129 EXPECT_EQ(received[1].sent_ports[0].id, port3.id); | 127 EXPECT_EQ(received[1].sent_ports[0], port3); |
| 130 | 128 |
| 131 event->Signal(); | 129 event->Signal(); |
| 132 } | 130 } |
| 133 | 131 |
| 134 } // namespace | 132 } // namespace |
| 135 | 133 |
| 136 // Verify that a message channel can be created and used for exchanging | 134 // Verify that a message channel can be created and used for exchanging |
| 137 // messages. | 135 // messages. |
| 138 IN_PROC_BROWSER_TEST_F(MessagePortProviderBrowserTest, CreateChannel) { | 136 IN_PROC_BROWSER_TEST_F(MessagePortProviderBrowserTest, CreateChannel) { |
| 139 base::WaitableEvent event(true, false); | 137 base::WaitableEvent event(true, false); |
| 140 BrowserThread::PostTask( | 138 BrowserThread::PostTask( |
| 141 BrowserThread::IO, FROM_HERE, | 139 BrowserThread::IO, FROM_HERE, |
| 142 base::Bind(&VerifyCreateChannelOnIOThread, &event)); | 140 base::Bind(&VerifyCreateChannelOnIOThread, &event)); |
| 143 event.Wait(); | 141 event.Wait(); |
| 144 } | 142 } |
| 145 | 143 |
| 146 } // namespace content | 144 } // namespace content |
| OLD | NEW |