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 base::string16 data; // the message data | 28 MessagePortMessage data; // the message data |
29 std::vector<int> sent_ports; // any transferred ports | 29 std::vector<int> 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(int route_id, |
39 const base::string16& message, | 39 const MessagePortMessage& message, |
40 const std::vector<int>& sent_message_port_ids) override { | 40 const std::vector<int>& sent_message_port_ids) override { |
41 Message m; | 41 Message m; |
42 m.route_id = route_id; | 42 m.route_id = route_id; |
43 m.data = message; | 43 m.data = message; |
44 m.sent_ports = sent_message_port_ids; | 44 m.sent_ports = sent_message_port_ids; |
45 messages_.push_back(m); | 45 messages_.push_back(m); |
46 } | 46 } |
47 | 47 |
48 void SendMessagesAreQueued(int route_id) override { } | 48 void SendMessagesAreQueued(int route_id) override { } |
49 | 49 |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 const base::char16 MESSAGE2[] = { 0x1001, 0 }; | 93 const base::char16 MESSAGE2[] = { 0x1001, 0 }; |
94 | 94 |
95 MockMessagePortDelegate delegate; | 95 MockMessagePortDelegate delegate; |
96 int port1; | 96 int port1; |
97 int port2; | 97 int port2; |
98 | 98 |
99 MessagePortProvider::CreateMessageChannel(&delegate, &port1, &port2); | 99 MessagePortProvider::CreateMessageChannel(&delegate, &port1, &port2); |
100 MessagePortService* service = MessagePortService::GetInstance(); | 100 MessagePortService* service = MessagePortService::GetInstance(); |
101 // Send a message to port1 transferring no ports. | 101 // Send a message to port1 transferring no ports. |
102 std::vector<int> sent_ports; | 102 std::vector<int> sent_ports; |
103 service->PostMessage(port1, base::string16(MESSAGE1), sent_ports); | 103 service->PostMessage(port1, MessagePortMessage(base::string16(MESSAGE1)), |
| 104 sent_ports); |
104 // Verify that message is received | 105 // Verify that message is received |
105 const MockMessagePortDelegate::Messages& received = | 106 const MockMessagePortDelegate::Messages& received = |
106 delegate.getReceivedMessages(); | 107 delegate.getReceivedMessages(); |
107 EXPECT_EQ(received.size(), 1u); | 108 EXPECT_EQ(received.size(), 1u); |
108 // Verify that message sent to port1 is received by entangled port, which is | 109 // Verify that message sent to port1 is received by entangled port, which is |
109 // port2. | 110 // port2. |
110 EXPECT_EQ(received[0].route_id, port2); | 111 EXPECT_EQ(received[0].route_id, port2); |
111 EXPECT_EQ(received[0].data, MESSAGE1); | 112 EXPECT_EQ(received[0].data.message_as_string, MESSAGE1); |
112 EXPECT_EQ(received[0].sent_ports.size(), 0u); | 113 EXPECT_EQ(received[0].sent_ports.size(), 0u); |
113 | 114 |
114 // Create a new channel, and transfer one of its ports to port2, making sure | 115 // Create a new channel, and transfer one of its ports to port2, making sure |
115 // the transferred port is received. | 116 // the transferred port is received. |
116 int port3; | 117 int port3; |
117 int port4; | 118 int port4; |
118 MessagePortProvider::CreateMessageChannel(&delegate, &port3, &port4); | 119 MessagePortProvider::CreateMessageChannel(&delegate, &port3, &port4); |
119 sent_ports.push_back(port3); | 120 sent_ports.push_back(port3); |
120 service->PostMessage(port1, base::string16(MESSAGE2), sent_ports); | 121 service->PostMessage(port1, MessagePortMessage(base::string16(MESSAGE2)), |
| 122 sent_ports); |
121 EXPECT_EQ(received.size(), 2u); | 123 EXPECT_EQ(received.size(), 2u); |
122 EXPECT_EQ(received[1].route_id, port2); | 124 EXPECT_EQ(received[1].route_id, port2); |
123 EXPECT_EQ(received[1].data, MESSAGE2); | 125 EXPECT_EQ(received[1].data.message_as_string, MESSAGE2); |
124 EXPECT_EQ(received[1].sent_ports.size(), 1u); | 126 EXPECT_EQ(received[1].sent_ports.size(), 1u); |
125 EXPECT_EQ(received[1].sent_ports[0], port3); | 127 EXPECT_EQ(received[1].sent_ports[0], port3); |
126 | 128 |
127 event->Signal(); | 129 event->Signal(); |
128 } | 130 } |
129 | 131 |
130 } // namespace | 132 } // namespace |
131 | 133 |
132 // 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 |
133 // messages. | 135 // messages. |
134 IN_PROC_BROWSER_TEST_F(MessagePortProviderBrowserTest, CreateChannel) { | 136 IN_PROC_BROWSER_TEST_F(MessagePortProviderBrowserTest, CreateChannel) { |
135 base::WaitableEvent event(true, false); | 137 base::WaitableEvent event(true, false); |
136 BrowserThread::PostTask( | 138 BrowserThread::PostTask( |
137 BrowserThread::IO, FROM_HERE, | 139 BrowserThread::IO, FROM_HERE, |
138 base::Bind(&VerifyCreateChannelOnIOThread, &event)); | 140 base::Bind(&VerifyCreateChannelOnIOThread, &event)); |
139 event.Wait(); | 141 event.Wait(); |
140 } | 142 } |
141 | 143 |
142 } // namespace content | 144 } // namespace content |
OLD | NEW |