| 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/miscellaneous_bindings.h" |
| 13 #include "chrome/test/base/chrome_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 using extensions::MiscellaneousBindings; |
| 18 |
| 17 namespace { | 19 namespace { |
| 18 | 20 |
| 19 static const char kTestingExtensionId[] = "oooooooooooooooooooooooooooooooo"; | 21 static const char kTestingExtensionId[] = "oooooooooooooooooooooooooooooooo"; |
| 20 | 22 |
| 21 void DispatchOnConnect(const ChromeV8ContextSet& v8_context_set, | 23 void DispatchOnConnect(const ChromeV8ContextSet& v8_context_set, |
| 22 int source_port_id, const std::string& name, | 24 int source_port_id, const std::string& name, |
| 23 const std::string& tab_json) { | 25 const std::string& tab_json) { |
| 24 ListValue args; | 26 ListValue args; |
| 25 args.Set(0, Value::CreateIntegerValue(source_port_id)); | 27 args.Set(0, Value::CreateIntegerValue(source_port_id)); |
| 26 args.Set(1, Value::CreateStringValue(name)); | 28 args.Set(1, Value::CreateStringValue(name)); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 37 args.Set(0, Value::CreateIntegerValue(source_port_id)); | 39 args.Set(0, Value::CreateIntegerValue(source_port_id)); |
| 38 v8_context_set.DispatchChromeHiddenMethod( | 40 v8_context_set.DispatchChromeHiddenMethod( |
| 39 "", ExtensionMessageService::kDispatchOnDisconnect, args, NULL, GURL()); | 41 "", ExtensionMessageService::kDispatchOnDisconnect, args, NULL, GURL()); |
| 40 } | 42 } |
| 41 | 43 |
| 42 } | 44 } |
| 43 | 45 |
| 44 // Tests that the bindings for opening a channel to an extension and sending | 46 // Tests that the bindings for opening a channel to an extension and sending |
| 45 // and receiving messages through that channel all works. | 47 // and receiving messages through that channel all works. |
| 46 // | 48 // |
| 47 // TODO(aa): Refactor RendererProcessBindings to have fewer dependencies and | 49 // TODO(aa): Refactor MiscellaneousBindings to have fewer dependencies and |
| 48 // make this into a unit test. That will allow us to get rid of cruft like | 50 // make this into a unit test. That will allow us to get rid of cruft like |
| 49 // SetTestExtensionId(). | 51 // SetTestExtensionId(). |
| 50 TEST_F(ChromeRenderViewTest, ExtensionMessagesOpenChannel) { | 52 TEST_F(ChromeRenderViewTest, ExtensionMessagesOpenChannel) { |
| 51 extension_dispatcher_->SetTestExtensionId(kTestingExtensionId); | 53 extension_dispatcher_->SetTestExtensionId(kTestingExtensionId); |
| 52 render_thread_->sink().ClearMessages(); | 54 render_thread_->sink().ClearMessages(); |
| 53 LoadHTML("<body></body>"); | 55 LoadHTML("<body></body>"); |
| 54 ExecuteJavaScript( | 56 ExecuteJavaScript( |
| 55 "var port = chrome.extension.connect({name:'testName'});" | 57 "var port = chrome.extension.connect({name:'testName'});" |
| 56 "port.onMessage.addListener(doOnMessage);" | 58 "port.onMessage.addListener(doOnMessage);" |
| 57 "port.postMessage({message: 'content ready'});" | 59 "port.postMessage({message: 'content ready'});" |
| (...skipping 15 matching lines...) Expand all Loading... |
| 73 render_thread_->sink().GetUniqueMessageMatching( | 75 render_thread_->sink().GetUniqueMessageMatching( |
| 74 ExtensionHostMsg_PostMessage::ID); | 76 ExtensionHostMsg_PostMessage::ID); |
| 75 ASSERT_TRUE(post_msg); | 77 ASSERT_TRUE(post_msg); |
| 76 ExtensionHostMsg_PostMessage::Param post_params; | 78 ExtensionHostMsg_PostMessage::Param post_params; |
| 77 ExtensionHostMsg_PostMessage::Read(post_msg, &post_params); | 79 ExtensionHostMsg_PostMessage::Read(post_msg, &post_params); |
| 78 EXPECT_EQ("{\"message\":\"content ready\"}", post_params.b); | 80 EXPECT_EQ("{\"message\":\"content ready\"}", post_params.b); |
| 79 | 81 |
| 80 // Now simulate getting a message back from the other side. | 82 // Now simulate getting a message back from the other side. |
| 81 render_thread_->sink().ClearMessages(); | 83 render_thread_->sink().ClearMessages(); |
| 82 const int kPortId = 0; | 84 const int kPortId = 0; |
| 83 RendererExtensionBindings::DeliverMessage( | 85 MiscellaneousBindings::DeliverMessage( |
| 84 extension_dispatcher_->v8_context_set().GetAll(), | 86 extension_dispatcher_->v8_context_set().GetAll(), |
| 85 kPortId, "{\"val\": 42}", NULL); | 87 kPortId, "{\"val\": 42}", NULL); |
| 86 | 88 |
| 87 // Verify that we got it. | 89 // Verify that we got it. |
| 88 const IPC::Message* alert_msg = | 90 const IPC::Message* alert_msg = |
| 89 render_thread_->sink().GetUniqueMessageMatching( | 91 render_thread_->sink().GetUniqueMessageMatching( |
| 90 ViewHostMsg_RunJavaScriptMessage::ID); | 92 ViewHostMsg_RunJavaScriptMessage::ID); |
| 91 ASSERT_TRUE(alert_msg); | 93 ASSERT_TRUE(alert_msg); |
| 92 iter = IPC::SyncMessage::GetDataIterator(alert_msg); | 94 iter = IPC::SyncMessage::GetDataIterator(alert_msg); |
| 93 ViewHostMsg_RunJavaScriptMessage::SendParam alert_param; | 95 ViewHostMsg_RunJavaScriptMessage::SendParam alert_param; |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 129 ExtensionHostMsg_PostMessage::ID); | 131 ExtensionHostMsg_PostMessage::ID); |
| 130 ASSERT_TRUE(post_msg); | 132 ASSERT_TRUE(post_msg); |
| 131 ExtensionHostMsg_PostMessage::Param post_params; | 133 ExtensionHostMsg_PostMessage::Param post_params; |
| 132 ExtensionHostMsg_PostMessage::Read(post_msg, &post_params); | 134 ExtensionHostMsg_PostMessage::Read(post_msg, &post_params); |
| 133 std::string expected_msg = | 135 std::string expected_msg = |
| 134 "{\"message\":\"onconnect from foo://bar name " + kPortName + "\"}"; | 136 "{\"message\":\"onconnect from foo://bar name " + kPortName + "\"}"; |
| 135 EXPECT_EQ(expected_msg, post_params.b); | 137 EXPECT_EQ(expected_msg, post_params.b); |
| 136 | 138 |
| 137 // Now simulate getting a message back from the channel opener. | 139 // Now simulate getting a message back from the channel opener. |
| 138 render_thread_->sink().ClearMessages(); | 140 render_thread_->sink().ClearMessages(); |
| 139 RendererExtensionBindings::DeliverMessage( | 141 MiscellaneousBindings::DeliverMessage( |
| 140 extension_dispatcher_->v8_context_set().GetAll(), | 142 extension_dispatcher_->v8_context_set().GetAll(), |
| 141 kPortId, "{\"val\": 42}", NULL); | 143 kPortId, "{\"val\": 42}", NULL); |
| 142 | 144 |
| 143 // Verify that we got it. | 145 // Verify that we got it. |
| 144 const IPC::Message* alert_msg = | 146 const IPC::Message* alert_msg = |
| 145 render_thread_->sink().GetUniqueMessageMatching( | 147 render_thread_->sink().GetUniqueMessageMatching( |
| 146 ViewHostMsg_RunJavaScriptMessage::ID); | 148 ViewHostMsg_RunJavaScriptMessage::ID); |
| 147 ASSERT_TRUE(alert_msg); | 149 ASSERT_TRUE(alert_msg); |
| 148 void* iter = IPC::SyncMessage::GetDataIterator(alert_msg); | 150 void* iter = IPC::SyncMessage::GetDataIterator(alert_msg); |
| 149 ViewHostMsg_RunJavaScriptMessage::SendParam alert_param; | 151 ViewHostMsg_RunJavaScriptMessage::SendParam alert_param; |
| 150 ASSERT_TRUE(IPC::ReadParam(alert_msg, &iter, &alert_param)); | 152 ASSERT_TRUE(IPC::ReadParam(alert_msg, &iter, &alert_param)); |
| 151 EXPECT_EQ(ASCIIToUTF16("got: 42"), alert_param.a); | 153 EXPECT_EQ(ASCIIToUTF16("got: 42"), alert_param.a); |
| 152 | 154 |
| 153 // Now simulate the channel closing. | 155 // Now simulate the channel closing. |
| 154 render_thread_->sink().ClearMessages(); | 156 render_thread_->sink().ClearMessages(); |
| 155 DispatchOnDisconnect(extension_dispatcher_->v8_context_set(), kPortId); | 157 DispatchOnDisconnect(extension_dispatcher_->v8_context_set(), kPortId); |
| 156 | 158 |
| 157 // Verify that we got it. | 159 // Verify that we got it. |
| 158 alert_msg = | 160 alert_msg = |
| 159 render_thread_->sink().GetUniqueMessageMatching( | 161 render_thread_->sink().GetUniqueMessageMatching( |
| 160 ViewHostMsg_RunJavaScriptMessage::ID); | 162 ViewHostMsg_RunJavaScriptMessage::ID); |
| 161 ASSERT_TRUE(alert_msg); | 163 ASSERT_TRUE(alert_msg); |
| 162 iter = IPC::SyncMessage::GetDataIterator(alert_msg); | 164 iter = IPC::SyncMessage::GetDataIterator(alert_msg); |
| 163 ASSERT_TRUE(IPC::ReadParam(alert_msg, &iter, &alert_param)); | 165 ASSERT_TRUE(IPC::ReadParam(alert_msg, &iter, &alert_param)); |
| 164 EXPECT_EQ(ASCIIToUTF16("disconnected: 24"), alert_param.a); | 166 EXPECT_EQ(ASCIIToUTF16("disconnected: 24"), alert_param.a); |
| 165 } | 167 } |
| OLD | NEW |