| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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/values.h" | 5 #include "base/values.h" |
| 6 #include "chrome/browser/extensions/extension_message_service.h" | 6 #include "chrome/browser/extensions/extension_message_service.h" |
| 7 #include "chrome/common/extensions/extension_messages.h" |
| 7 #include "chrome/common/render_messages.h" | 8 #include "chrome/common/render_messages.h" |
| 8 #include "chrome/renderer/extensions/event_bindings.h" | 9 #include "chrome/renderer/extensions/event_bindings.h" |
| 9 #include "chrome/renderer/extensions/renderer_extension_bindings.h" | 10 #include "chrome/renderer/extensions/renderer_extension_bindings.h" |
| 10 #include "chrome/test/render_view_test.h" | 11 #include "chrome/test/render_view_test.h" |
| 11 #include "content/common/view_messages.h" | 12 #include "content/common/view_messages.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 14 |
| 14 static void DispatchOnConnect(int source_port_id, const std::string& name, | 15 static void DispatchOnConnect(int source_port_id, const std::string& name, |
| 15 const std::string& tab_json) { | 16 const std::string& tab_json) { |
| 16 ListValue args; | 17 ListValue args; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 50 "var port = chrome.extension.connect({name:'testName'});" | 51 "var port = chrome.extension.connect({name:'testName'});" |
| 51 "port.onMessage.addListener(doOnMessage);" | 52 "port.onMessage.addListener(doOnMessage);" |
| 52 "port.postMessage({message: 'content ready'});" | 53 "port.postMessage({message: 'content ready'});" |
| 53 "function doOnMessage(msg, port) {" | 54 "function doOnMessage(msg, port) {" |
| 54 " alert('content got: ' + msg.val);" | 55 " alert('content got: ' + msg.val);" |
| 55 "}"); | 56 "}"); |
| 56 | 57 |
| 57 // Verify that we opened a channel and sent a message through it. | 58 // Verify that we opened a channel and sent a message through it. |
| 58 const IPC::Message* open_channel_msg = | 59 const IPC::Message* open_channel_msg = |
| 59 render_thread_.sink().GetUniqueMessageMatching( | 60 render_thread_.sink().GetUniqueMessageMatching( |
| 60 ViewHostMsg_OpenChannelToExtension::ID); | 61 ExtensionHostMsg_OpenChannelToExtension::ID); |
| 61 ASSERT_TRUE(open_channel_msg); | 62 ASSERT_TRUE(open_channel_msg); |
| 62 void* iter = IPC::SyncMessage::GetDataIterator(open_channel_msg); | 63 void* iter = IPC::SyncMessage::GetDataIterator(open_channel_msg); |
| 63 ViewHostMsg_OpenChannelToExtension::SendParam open_params; | 64 ExtensionHostMsg_OpenChannelToExtension::SendParam open_params; |
| 64 ASSERT_TRUE(IPC::ReadParam(open_channel_msg, &iter, &open_params)); | 65 ASSERT_TRUE(IPC::ReadParam(open_channel_msg, &iter, &open_params)); |
| 65 EXPECT_EQ("testName", open_params.d); | 66 EXPECT_EQ("testName", open_params.d); |
| 66 | 67 |
| 67 const IPC::Message* post_msg = | 68 const IPC::Message* post_msg = |
| 68 render_thread_.sink().GetUniqueMessageMatching( | 69 render_thread_.sink().GetUniqueMessageMatching( |
| 69 ViewHostMsg_ExtensionPostMessage::ID); | 70 ExtensionHostMsg_PostMessage::ID); |
| 70 ASSERT_TRUE(post_msg); | 71 ASSERT_TRUE(post_msg); |
| 71 ViewHostMsg_ExtensionPostMessage::Param post_params; | 72 ExtensionHostMsg_PostMessage::Param post_params; |
| 72 ViewHostMsg_ExtensionPostMessage::Read(post_msg, &post_params); | 73 ExtensionHostMsg_PostMessage::Read(post_msg, &post_params); |
| 73 EXPECT_EQ("{\"message\":\"content ready\"}", post_params.b); | 74 EXPECT_EQ("{\"message\":\"content ready\"}", post_params.b); |
| 74 | 75 |
| 75 // Now simulate getting a message back from the other side. | 76 // Now simulate getting a message back from the other side. |
| 76 render_thread_.sink().ClearMessages(); | 77 render_thread_.sink().ClearMessages(); |
| 77 const int kPortId = 0; | 78 const int kPortId = 0; |
| 78 DispatchOnMessage("{\"val\": 42}", kPortId); | 79 DispatchOnMessage("{\"val\": 42}", kPortId); |
| 79 | 80 |
| 80 // Verify that we got it. | 81 // Verify that we got it. |
| 81 const IPC::Message* alert_msg = | 82 const IPC::Message* alert_msg = |
| 82 render_thread_.sink().GetUniqueMessageMatching( | 83 render_thread_.sink().GetUniqueMessageMatching( |
| (...skipping 27 matching lines...) Expand all Loading... |
| 110 render_thread_.sink().ClearMessages(); | 111 render_thread_.sink().ClearMessages(); |
| 111 | 112 |
| 112 // Simulate a new connection being opened. | 113 // Simulate a new connection being opened. |
| 113 const int kPortId = 0; | 114 const int kPortId = 0; |
| 114 const std::string kPortName = "testName"; | 115 const std::string kPortName = "testName"; |
| 115 DispatchOnConnect(kPortId, kPortName, "{\"url\":\"foo://bar\"}"); | 116 DispatchOnConnect(kPortId, kPortName, "{\"url\":\"foo://bar\"}"); |
| 116 | 117 |
| 117 // Verify that we handled the new connection by posting a message. | 118 // Verify that we handled the new connection by posting a message. |
| 118 const IPC::Message* post_msg = | 119 const IPC::Message* post_msg = |
| 119 render_thread_.sink().GetUniqueMessageMatching( | 120 render_thread_.sink().GetUniqueMessageMatching( |
| 120 ViewHostMsg_ExtensionPostMessage::ID); | 121 ExtensionHostMsg_PostMessage::ID); |
| 121 ASSERT_TRUE(post_msg); | 122 ASSERT_TRUE(post_msg); |
| 122 ViewHostMsg_ExtensionPostMessage::Param post_params; | 123 ExtensionHostMsg_PostMessage::Param post_params; |
| 123 ViewHostMsg_ExtensionPostMessage::Read(post_msg, &post_params); | 124 ExtensionHostMsg_PostMessage::Read(post_msg, &post_params); |
| 124 std::string expected_msg = | 125 std::string expected_msg = |
| 125 "{\"message\":\"onconnect from foo://bar name " + kPortName + "\"}"; | 126 "{\"message\":\"onconnect from foo://bar name " + kPortName + "\"}"; |
| 126 EXPECT_EQ(expected_msg, post_params.b); | 127 EXPECT_EQ(expected_msg, post_params.b); |
| 127 | 128 |
| 128 // Now simulate getting a message back from the channel opener. | 129 // Now simulate getting a message back from the channel opener. |
| 129 render_thread_.sink().ClearMessages(); | 130 render_thread_.sink().ClearMessages(); |
| 130 DispatchOnMessage("{\"val\": 42}", kPortId); | 131 DispatchOnMessage("{\"val\": 42}", kPortId); |
| 131 | 132 |
| 132 // Verify that we got it. | 133 // Verify that we got it. |
| 133 const IPC::Message* alert_msg = | 134 const IPC::Message* alert_msg = |
| (...skipping 11 matching lines...) Expand all Loading... |
| 145 | 146 |
| 146 // Verify that we got it. | 147 // Verify that we got it. |
| 147 alert_msg = | 148 alert_msg = |
| 148 render_thread_.sink().GetUniqueMessageMatching( | 149 render_thread_.sink().GetUniqueMessageMatching( |
| 149 ViewHostMsg_RunJavaScriptMessage::ID); | 150 ViewHostMsg_RunJavaScriptMessage::ID); |
| 150 ASSERT_TRUE(alert_msg); | 151 ASSERT_TRUE(alert_msg); |
| 151 iter = IPC::SyncMessage::GetDataIterator(alert_msg); | 152 iter = IPC::SyncMessage::GetDataIterator(alert_msg); |
| 152 ASSERT_TRUE(IPC::ReadParam(alert_msg, &iter, &alert_param)); | 153 ASSERT_TRUE(IPC::ReadParam(alert_msg, &iter, &alert_param)); |
| 153 EXPECT_EQ(L"disconnected: 24", alert_param.a); | 154 EXPECT_EQ(L"disconnected: 24", alert_param.a); |
| 154 } | 155 } |
| OLD | NEW |