summaryrefslogtreecommitdiffstats
path: root/chrome/renderer/render_view_unittest.cc
diff options
context:
space:
mode:
authormpcomplete@google.com <mpcomplete@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2009-04-02 23:56:11 +0000
committermpcomplete@google.com <mpcomplete@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2009-04-02 23:56:11 +0000
commit75e5a879a500897b2409c49d4ef205e2e954c9fd (patch)
tree166ca53e8cc29520402efd7cc6ee2804edf744eb /chrome/renderer/render_view_unittest.cc
parent720ac73783ea732c130ff6d11dffa41919a25809 (diff)
downloadchromium_src-75e5a879a500897b2409c49d4ef205e2e954c9fd.zip
chromium_src-75e5a879a500897b2409c49d4ef205e2e954c9fd.tar.gz
chromium_src-75e5a879a500897b2409c49d4ef205e2e954c9fd.tar.bz2
Add code to support 2-way communication between extensions and renderers. The code is almost fully symmetrical, except that right now a channel can only be opened to an extension (by ID). It should be trivial to open a channel to a tab, once we have a solid tab API.
Review URL: http://codereview.chromium.org/56037 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@13057 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/renderer/render_view_unittest.cc')
-rw-r--r--chrome/renderer/render_view_unittest.cc90
1 files changed, 90 insertions, 0 deletions
diff --git a/chrome/renderer/render_view_unittest.cc b/chrome/renderer/render_view_unittest.cc
index 88c4fa7..c27bcbf 100644
--- a/chrome/renderer/render_view_unittest.cc
+++ b/chrome/renderer/render_view_unittest.cc
@@ -4,6 +4,7 @@
#include "base/scoped_ptr.h"
#include "chrome/common/render_messages.h"
+#include "chrome/renderer/extensions/renderer_extension_bindings.h"
#include "chrome/renderer/mock_render_process.h"
#include "chrome/renderer/mock_render_thread.h"
#include "chrome/renderer/render_view.h"
@@ -65,6 +66,8 @@ class RenderViewTest : public testing::Test {
// testing::Test
virtual void SetUp() {
WebKit::initialize(&webkitclient_);
+ WebKit::registerExtension(
+ extensions_v8::RendererExtensionBindings::Get(&render_thread_));
mock_process_.reset(new MockProcess());
@@ -367,3 +370,90 @@ TEST_F(RenderViewTest, OnSetTextDirection) {
EXPECT_EQ(output, kTextDirection[i].expected_result);
}
}
+
+// Tests that the bindings for opening a channel to an extension and sending
+// and receiving messages through that channel all works.
+TEST_F(RenderViewTest, ExtensionMessagesOpenChannel) {
+ render_thread_.sink().ClearMessages();
+ LoadHTML("<body></body>");
+ ExecuteJavaScript(
+ "var e = new chromium.Extension('foobar');"
+ "var port = e.openChannel();"
+ "port.onMessage = doOnMessage;"
+ "port.postMessage('content ready');"
+ "function doOnMessage(msg, port) {"
+ " alert('content got: ' + msg);"
+ "}");
+
+ // Verify that we opened a channel and sent a message through it.
+ const IPC::Message* open_channel_msg =
+ render_thread_.sink().GetUniqueMessageMatching(
+ ViewHostMsg_OpenChannelToExtension::ID);
+ EXPECT_TRUE(open_channel_msg);
+
+ const IPC::Message* post_msg =
+ render_thread_.sink().GetUniqueMessageMatching(
+ ViewHostMsg_ExtensionPostMessage::ID);
+ EXPECT_TRUE(post_msg);
+ ViewHostMsg_ExtensionPostMessage::Param post_params;
+ ViewHostMsg_ExtensionPostMessage::Read(post_msg, &post_params);
+ EXPECT_EQ("content ready", post_params.b);
+
+ // Now simulate getting a message back from the other side.
+ render_thread_.sink().ClearMessages();
+ const int kPortId = 0;
+ extensions_v8::RendererExtensionBindings::HandleMessage("42", kPortId);
+
+ // Verify that we got it.
+ const IPC::Message* alert_msg =
+ render_thread_.sink().GetUniqueMessageMatching(
+ ViewHostMsg_RunJavaScriptMessage::ID);
+ EXPECT_TRUE(alert_msg);
+ void* iter = IPC::SyncMessage::GetDataIterator(alert_msg);
+ ViewHostMsg_RunJavaScriptMessage::SendParam alert_param;
+ IPC::ReadParam(alert_msg, &iter, &alert_param);
+ EXPECT_EQ(L"content got: 42", alert_param.a);
+}
+
+// Tests that the bindings for handling a new channel connection and sending
+// and receiving messages through that channel all works.
+TEST_F(RenderViewTest, ExtensionMessagesOnConnect) {
+ LoadHTML("<body></body>");
+ ExecuteJavaScript(
+ "chromium.addConnectListener(function (port) {"
+ " port.onMessage = doOnMessage;"
+ " port.postMessage('onconnect');"
+ " });"
+ "function doOnMessage(msg, port) {"
+ " alert('got: ' + msg);"
+ "}");
+
+ render_thread_.sink().ClearMessages();
+
+ // Simulate a new connection being opened.
+ const int kPortId = 0;
+ extensions_v8::RendererExtensionBindings::HandleConnect(kPortId);
+
+ // Verify that we handled the new connection by posting a message.
+ const IPC::Message* post_msg =
+ render_thread_.sink().GetUniqueMessageMatching(
+ ViewHostMsg_ExtensionPostMessage::ID);
+ EXPECT_TRUE(post_msg);
+ ViewHostMsg_ExtensionPostMessage::Param post_params;
+ ViewHostMsg_ExtensionPostMessage::Read(post_msg, &post_params);
+ EXPECT_EQ("onconnect", post_params.b);
+
+ // Now simulate getting a message back from the channel opener.
+ render_thread_.sink().ClearMessages();
+ extensions_v8::RendererExtensionBindings::HandleMessage("42", kPortId);
+
+ // Verify that we got it.
+ const IPC::Message* alert_msg =
+ render_thread_.sink().GetUniqueMessageMatching(
+ ViewHostMsg_RunJavaScriptMessage::ID);
+ EXPECT_TRUE(alert_msg);
+ void* iter = IPC::SyncMessage::GetDataIterator(alert_msg);
+ ViewHostMsg_RunJavaScriptMessage::SendParam alert_param;
+ IPC::ReadParam(alert_msg, &iter, &alert_param);
+ EXPECT_EQ(L"got: 42", alert_param.a);
+}