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