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