| 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/common/message_port.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" | |
| 12 #include "content/public/browser/message_port_provider.h" | 11 #include "content/public/browser/message_port_provider.h" |
| 13 #include "content/public/browser/web_contents.h" | 12 #include "content/public/browser/web_contents.h" |
| 14 #include "content/public/test/browser_test_utils.h" | 13 #include "content/public/test/browser_test_utils.h" |
| 15 #include "content/public/test/content_browser_test.h" | 14 #include "content/public/test/content_browser_test.h" |
| 16 #include "content/public/test/content_browser_test_utils.h" | 15 #include "content/public/test/content_browser_test_utils.h" |
| 17 #include "content/shell/browser/shell.h" | 16 #include "content/shell/browser/shell.h" |
| 18 | 17 |
| 19 namespace content { | 18 namespace content { |
| 20 | 19 |
| 21 // This test verifies the functionality of the Message Port Provider API. | 20 // This test verifies the functionality of the Message Port Provider API. |
| 22 | 21 |
| 23 // A mock class for testing message port provider. | |
| 24 class MockMessagePortDelegate : public MessagePortDelegate { | |
| 25 public: | |
| 26 // A container to hold received messages | |
| 27 struct Message { | |
| 28 int route_id; // the routing id of the target port | |
| 29 base::string16 data; // the message data | |
| 30 std::vector<int> sent_ports; // any transferred ports | |
| 31 }; | |
| 32 | |
| 33 typedef std::vector<Message> Messages; | |
| 34 | |
| 35 MockMessagePortDelegate() { } | |
| 36 ~MockMessagePortDelegate() override { } | |
| 37 | |
| 38 // MessagePortDelegate implementation | |
| 39 void SendMessage( | |
| 40 int route_id, | |
| 41 const base::string16& message, | |
| 42 const std::vector<int>& sent_message_ports) override { | |
| 43 Message m; | |
| 44 m.route_id = route_id; | |
| 45 m.data = message; | |
| 46 m.sent_ports = sent_message_ports; | |
| 47 messages_.push_back(m); | |
| 48 } | |
| 49 | |
| 50 void SendMessagesAreQueued(int route_id) override { } | |
| 51 | |
| 52 const Messages& getReceivedMessages() { | |
| 53 return messages_; | |
| 54 } | |
| 55 private: | |
| 56 Messages messages_; | |
| 57 | |
| 58 DISALLOW_COPY_AND_ASSIGN(MockMessagePortDelegate); | |
| 59 }; | |
| 60 | |
| 61 | |
| 62 class MessagePortProviderBrowserTest : public ContentBrowserTest { | 22 class MessagePortProviderBrowserTest : public ContentBrowserTest { |
| 63 }; | 23 }; |
| 64 | 24 |
| 65 // Verify that messages can be posted to main frame. | 25 // Verify that messages can be posted to main frame. |
| 66 IN_PROC_BROWSER_TEST_F(MessagePortProviderBrowserTest, PostMessage) { | 26 IN_PROC_BROWSER_TEST_F(MessagePortProviderBrowserTest, PostMessage) { |
| 67 const std::string data = | 27 const std::string data = |
| 68 "<!DOCTYPE html><html><body>" | 28 "<!DOCTYPE html><html><body>" |
| 69 " <script type=\"text/javascript\">" | 29 " <script type=\"text/javascript\">" |
| 70 " onmessage = function (e) { document.title = e.data; }" | 30 " onmessage = function (e) { document.title = e.data; }" |
| 71 " </script>" | 31 " </script>" |
| 72 "</body></html>"; | 32 "</body></html>"; |
| 73 const base::string16 target_origin(base::UTF8ToUTF16("http://baseurl")); | 33 const base::string16 target_origin(base::UTF8ToUTF16("http://baseurl")); |
| 74 const GURL base_url(target_origin); | 34 const GURL base_url(target_origin); |
| 75 const GURL history_url; | 35 const GURL history_url; |
| 76 // Load data. Blocks until it is done. | 36 // Load data. Blocks until it is done. |
| 77 content::LoadDataWithBaseURL(shell(), history_url, data, base_url); | 37 content::LoadDataWithBaseURL(shell(), history_url, data, base_url); |
| 78 const base::string16 source_origin(base::UTF8ToUTF16("source")); | 38 const base::string16 source_origin(base::UTF8ToUTF16("source")); |
| 79 const base::string16 message(base::UTF8ToUTF16("success")); | 39 const base::string16 message(base::UTF8ToUTF16("success")); |
| 80 const std::vector<int> ports; | |
| 81 content::TitleWatcher title_watcher(shell()->web_contents(), message); | 40 content::TitleWatcher title_watcher(shell()->web_contents(), message); |
| 82 MessagePortProvider::PostMessageToFrame(shell()->web_contents(), | 41 MessagePortProvider::PostMessageToFrame(shell()->web_contents(), |
| 83 source_origin, | 42 source_origin, |
| 84 target_origin, | 43 target_origin, |
| 85 message, | 44 message); |
| 86 ports); | |
| 87 EXPECT_EQ(message, title_watcher.WaitAndGetTitle()); | 45 EXPECT_EQ(message, title_watcher.WaitAndGetTitle()); |
| 88 } | 46 } |
| 89 | 47 |
| 90 } // namespace content | 48 } // namespace content |
| OLD | NEW |