| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/utf_string_conversions.h" | 5 #include "base/utf_string_conversions.h" |
| 6 #include "base/values.h" | 6 #include "base/values.h" |
| 7 #include "chrome/browser/extensions/extension_message_service.h" | 7 #include "chrome/browser/extensions/extension_message_service.h" |
| 8 #include "chrome/common/extensions/extension_messages.h" | 8 #include "chrome/common/extensions/extension_messages.h" |
| 9 #include "chrome/common/render_messages.h" | 9 #include "chrome/common/render_messages.h" |
| 10 #include "chrome/renderer/extensions/chrome_v8_context.h" | 10 #include "chrome/renderer/extensions/chrome_v8_context.h" |
| 11 #include "chrome/renderer/extensions/extension_dispatcher.h" | 11 #include "chrome/renderer/extensions/extension_dispatcher.h" |
| 12 #include "chrome/renderer/extensions/renderer_extension_bindings.h" | 12 #include "chrome/renderer/extensions/renderer_extension_bindings.h" |
| 13 #include "chrome/test/base/render_view_test.h" | 13 #include "chrome/test/base/chrome_render_view_test.h" |
| 14 #include "content/common/view_messages.h" | 14 #include "content/common/view_messages.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 | 16 |
| 17 namespace { | 17 namespace { |
| 18 | 18 |
| 19 static const char kTestingExtensionId[] = "oooooooooooooooooooooooooooooooo"; | 19 static const char kTestingExtensionId[] = "oooooooooooooooooooooooooooooooo"; |
| 20 | 20 |
| 21 void DispatchOnConnect(const ChromeV8ContextSet& v8_context_set, | 21 void DispatchOnConnect(const ChromeV8ContextSet& v8_context_set, |
| 22 int source_port_id, const std::string& name, | 22 int source_port_id, const std::string& name, |
| 23 const std::string& tab_json) { | 23 const std::string& tab_json) { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 40 } | 40 } |
| 41 | 41 |
| 42 } | 42 } |
| 43 | 43 |
| 44 // Tests that the bindings for opening a channel to an extension and sending | 44 // Tests that the bindings for opening a channel to an extension and sending |
| 45 // and receiving messages through that channel all works. | 45 // and receiving messages through that channel all works. |
| 46 // | 46 // |
| 47 // TODO(aa): Refactor RendererProcessBindings to have fewer dependencies and | 47 // TODO(aa): Refactor RendererProcessBindings to have fewer dependencies and |
| 48 // make this into a unit test. That will allow us to get rid of cruft like | 48 // make this into a unit test. That will allow us to get rid of cruft like |
| 49 // SetTestExtensionId(). | 49 // SetTestExtensionId(). |
| 50 TEST_F(RenderViewTest, ExtensionMessagesOpenChannel) { | 50 TEST_F(ChromeRenderViewTest, ExtensionMessagesOpenChannel) { |
| 51 extension_dispatcher_->SetTestExtensionId(kTestingExtensionId); | 51 extension_dispatcher_->SetTestExtensionId(kTestingExtensionId); |
| 52 render_thread_.sink().ClearMessages(); | 52 render_thread_->sink().ClearMessages(); |
| 53 LoadHTML("<body></body>"); | 53 LoadHTML("<body></body>"); |
| 54 ExecuteJavaScript( | 54 ExecuteJavaScript( |
| 55 "var port = chrome.extension.connect({name:'testName'});" | 55 "var port = chrome.extension.connect({name:'testName'});" |
| 56 "port.onMessage.addListener(doOnMessage);" | 56 "port.onMessage.addListener(doOnMessage);" |
| 57 "port.postMessage({message: 'content ready'});" | 57 "port.postMessage({message: 'content ready'});" |
| 58 "function doOnMessage(msg, port) {" | 58 "function doOnMessage(msg, port) {" |
| 59 " alert('content got: ' + msg.val);" | 59 " alert('content got: ' + msg.val);" |
| 60 "}"); | 60 "}"); |
| 61 | 61 |
| 62 // Verify that we opened a channel and sent a message through it. | 62 // Verify that we opened a channel and sent a message through it. |
| 63 const IPC::Message* open_channel_msg = | 63 const IPC::Message* open_channel_msg = |
| 64 render_thread_.sink().GetUniqueMessageMatching( | 64 render_thread_->sink().GetUniqueMessageMatching( |
| 65 ExtensionHostMsg_OpenChannelToExtension::ID); | 65 ExtensionHostMsg_OpenChannelToExtension::ID); |
| 66 ASSERT_TRUE(open_channel_msg); | 66 ASSERT_TRUE(open_channel_msg); |
| 67 void* iter = IPC::SyncMessage::GetDataIterator(open_channel_msg); | 67 void* iter = IPC::SyncMessage::GetDataIterator(open_channel_msg); |
| 68 ExtensionHostMsg_OpenChannelToExtension::SendParam open_params; | 68 ExtensionHostMsg_OpenChannelToExtension::SendParam open_params; |
| 69 ASSERT_TRUE(IPC::ReadParam(open_channel_msg, &iter, &open_params)); | 69 ASSERT_TRUE(IPC::ReadParam(open_channel_msg, &iter, &open_params)); |
| 70 EXPECT_EQ("testName", open_params.d); | 70 EXPECT_EQ("testName", open_params.d); |
| 71 | 71 |
| 72 const IPC::Message* post_msg = | 72 const IPC::Message* post_msg = |
| 73 render_thread_.sink().GetUniqueMessageMatching( | 73 render_thread_->sink().GetUniqueMessageMatching( |
| 74 ExtensionHostMsg_PostMessage::ID); | 74 ExtensionHostMsg_PostMessage::ID); |
| 75 ASSERT_TRUE(post_msg); | 75 ASSERT_TRUE(post_msg); |
| 76 ExtensionHostMsg_PostMessage::Param post_params; | 76 ExtensionHostMsg_PostMessage::Param post_params; |
| 77 ExtensionHostMsg_PostMessage::Read(post_msg, &post_params); | 77 ExtensionHostMsg_PostMessage::Read(post_msg, &post_params); |
| 78 EXPECT_EQ("{\"message\":\"content ready\"}", post_params.b); | 78 EXPECT_EQ("{\"message\":\"content ready\"}", post_params.b); |
| 79 | 79 |
| 80 // Now simulate getting a message back from the other side. | 80 // Now simulate getting a message back from the other side. |
| 81 render_thread_.sink().ClearMessages(); | 81 render_thread_->sink().ClearMessages(); |
| 82 const int kPortId = 0; | 82 const int kPortId = 0; |
| 83 RendererExtensionBindings::DeliverMessage( | 83 RendererExtensionBindings::DeliverMessage( |
| 84 extension_dispatcher_->v8_context_set().GetAll(), | 84 extension_dispatcher_->v8_context_set().GetAll(), |
| 85 kPortId, "{\"val\": 42}", NULL); | 85 kPortId, "{\"val\": 42}", NULL); |
| 86 | 86 |
| 87 // Verify that we got it. | 87 // Verify that we got it. |
| 88 const IPC::Message* alert_msg = | 88 const IPC::Message* alert_msg = |
| 89 render_thread_.sink().GetUniqueMessageMatching( | 89 render_thread_->sink().GetUniqueMessageMatching( |
| 90 ViewHostMsg_RunJavaScriptMessage::ID); | 90 ViewHostMsg_RunJavaScriptMessage::ID); |
| 91 ASSERT_TRUE(alert_msg); | 91 ASSERT_TRUE(alert_msg); |
| 92 iter = IPC::SyncMessage::GetDataIterator(alert_msg); | 92 iter = IPC::SyncMessage::GetDataIterator(alert_msg); |
| 93 ViewHostMsg_RunJavaScriptMessage::SendParam alert_param; | 93 ViewHostMsg_RunJavaScriptMessage::SendParam alert_param; |
| 94 ASSERT_TRUE(IPC::ReadParam(alert_msg, &iter, &alert_param)); | 94 ASSERT_TRUE(IPC::ReadParam(alert_msg, &iter, &alert_param)); |
| 95 EXPECT_EQ(ASCIIToUTF16("content got: 42"), alert_param.a); | 95 EXPECT_EQ(ASCIIToUTF16("content got: 42"), alert_param.a); |
| 96 } | 96 } |
| 97 | 97 |
| 98 // Tests that the bindings for handling a new channel connection and channel | 98 // Tests that the bindings for handling a new channel connection and channel |
| 99 // closing all works. | 99 // closing all works. |
| 100 TEST_F(RenderViewTest, ExtensionMessagesOnConnect) { | 100 TEST_F(ChromeRenderViewTest, ExtensionMessagesOnConnect) { |
| 101 extension_dispatcher_->SetTestExtensionId(kTestingExtensionId); | 101 extension_dispatcher_->SetTestExtensionId(kTestingExtensionId); |
| 102 LoadHTML("<body></body>"); | 102 LoadHTML("<body></body>"); |
| 103 ExecuteJavaScript( | 103 ExecuteJavaScript( |
| 104 "chrome.extension.onConnect.addListener(function (port) {" | 104 "chrome.extension.onConnect.addListener(function (port) {" |
| 105 " port.test = 24;" | 105 " port.test = 24;" |
| 106 " port.onMessage.addListener(doOnMessage);" | 106 " port.onMessage.addListener(doOnMessage);" |
| 107 " port.onDisconnect.addListener(doOnDisconnect);" | 107 " port.onDisconnect.addListener(doOnDisconnect);" |
| 108 " port.postMessage({message: 'onconnect from ' + port.tab.url + " | 108 " port.postMessage({message: 'onconnect from ' + port.tab.url + " |
| 109 " ' name ' + port.name});" | 109 " ' name ' + port.name});" |
| 110 "});" | 110 "});" |
| 111 "function doOnMessage(msg, port) {" | 111 "function doOnMessage(msg, port) {" |
| 112 " alert('got: ' + msg.val);" | 112 " alert('got: ' + msg.val);" |
| 113 "}" | 113 "}" |
| 114 "function doOnDisconnect(port) {" | 114 "function doOnDisconnect(port) {" |
| 115 " alert('disconnected: ' + port.test);" | 115 " alert('disconnected: ' + port.test);" |
| 116 "}"); | 116 "}"); |
| 117 | 117 |
| 118 render_thread_.sink().ClearMessages(); | 118 render_thread_->sink().ClearMessages(); |
| 119 | 119 |
| 120 // Simulate a new connection being opened. | 120 // Simulate a new connection being opened. |
| 121 const int kPortId = 0; | 121 const int kPortId = 0; |
| 122 const std::string kPortName = "testName"; | 122 const std::string kPortName = "testName"; |
| 123 DispatchOnConnect(extension_dispatcher_->v8_context_set(), | 123 DispatchOnConnect(extension_dispatcher_->v8_context_set(), |
| 124 kPortId, kPortName, "{\"url\":\"foo://bar\"}"); | 124 kPortId, kPortName, "{\"url\":\"foo://bar\"}"); |
| 125 | 125 |
| 126 // Verify that we handled the new connection by posting a message. | 126 // Verify that we handled the new connection by posting a message. |
| 127 const IPC::Message* post_msg = | 127 const IPC::Message* post_msg = |
| 128 render_thread_.sink().GetUniqueMessageMatching( | 128 render_thread_->sink().GetUniqueMessageMatching( |
| 129 ExtensionHostMsg_PostMessage::ID); | 129 ExtensionHostMsg_PostMessage::ID); |
| 130 ASSERT_TRUE(post_msg); | 130 ASSERT_TRUE(post_msg); |
| 131 ExtensionHostMsg_PostMessage::Param post_params; | 131 ExtensionHostMsg_PostMessage::Param post_params; |
| 132 ExtensionHostMsg_PostMessage::Read(post_msg, &post_params); | 132 ExtensionHostMsg_PostMessage::Read(post_msg, &post_params); |
| 133 std::string expected_msg = | 133 std::string expected_msg = |
| 134 "{\"message\":\"onconnect from foo://bar name " + kPortName + "\"}"; | 134 "{\"message\":\"onconnect from foo://bar name " + kPortName + "\"}"; |
| 135 EXPECT_EQ(expected_msg, post_params.b); | 135 EXPECT_EQ(expected_msg, post_params.b); |
| 136 | 136 |
| 137 // Now simulate getting a message back from the channel opener. | 137 // Now simulate getting a message back from the channel opener. |
| 138 render_thread_.sink().ClearMessages(); | 138 render_thread_->sink().ClearMessages(); |
| 139 RendererExtensionBindings::DeliverMessage( | 139 RendererExtensionBindings::DeliverMessage( |
| 140 extension_dispatcher_->v8_context_set().GetAll(), | 140 extension_dispatcher_->v8_context_set().GetAll(), |
| 141 kPortId, "{\"val\": 42}", NULL); | 141 kPortId, "{\"val\": 42}", NULL); |
| 142 | 142 |
| 143 // Verify that we got it. | 143 // Verify that we got it. |
| 144 const IPC::Message* alert_msg = | 144 const IPC::Message* alert_msg = |
| 145 render_thread_.sink().GetUniqueMessageMatching( | 145 render_thread_->sink().GetUniqueMessageMatching( |
| 146 ViewHostMsg_RunJavaScriptMessage::ID); | 146 ViewHostMsg_RunJavaScriptMessage::ID); |
| 147 ASSERT_TRUE(alert_msg); | 147 ASSERT_TRUE(alert_msg); |
| 148 void* iter = IPC::SyncMessage::GetDataIterator(alert_msg); | 148 void* iter = IPC::SyncMessage::GetDataIterator(alert_msg); |
| 149 ViewHostMsg_RunJavaScriptMessage::SendParam alert_param; | 149 ViewHostMsg_RunJavaScriptMessage::SendParam alert_param; |
| 150 ASSERT_TRUE(IPC::ReadParam(alert_msg, &iter, &alert_param)); | 150 ASSERT_TRUE(IPC::ReadParam(alert_msg, &iter, &alert_param)); |
| 151 EXPECT_EQ(ASCIIToUTF16("got: 42"), alert_param.a); | 151 EXPECT_EQ(ASCIIToUTF16("got: 42"), alert_param.a); |
| 152 | 152 |
| 153 // Now simulate the channel closing. | 153 // Now simulate the channel closing. |
| 154 render_thread_.sink().ClearMessages(); | 154 render_thread_->sink().ClearMessages(); |
| 155 DispatchOnDisconnect(extension_dispatcher_->v8_context_set(), kPortId); | 155 DispatchOnDisconnect(extension_dispatcher_->v8_context_set(), kPortId); |
| 156 | 156 |
| 157 // Verify that we got it. | 157 // Verify that we got it. |
| 158 alert_msg = | 158 alert_msg = |
| 159 render_thread_.sink().GetUniqueMessageMatching( | 159 render_thread_->sink().GetUniqueMessageMatching( |
| 160 ViewHostMsg_RunJavaScriptMessage::ID); | 160 ViewHostMsg_RunJavaScriptMessage::ID); |
| 161 ASSERT_TRUE(alert_msg); | 161 ASSERT_TRUE(alert_msg); |
| 162 iter = IPC::SyncMessage::GetDataIterator(alert_msg); | 162 iter = IPC::SyncMessage::GetDataIterator(alert_msg); |
| 163 ASSERT_TRUE(IPC::ReadParam(alert_msg, &iter, &alert_param)); | 163 ASSERT_TRUE(IPC::ReadParam(alert_msg, &iter, &alert_param)); |
| 164 EXPECT_EQ(ASCIIToUTF16("disconnected: 24"), alert_param.a); | 164 EXPECT_EQ(ASCIIToUTF16("disconnected: 24"), alert_param.a); |
| 165 } | 165 } |
| OLD | NEW |