summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/extension_messages_unittest.cc
diff options
context:
space:
mode:
authoraa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-04-14 23:40:41 +0000
committeraa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-04-14 23:40:41 +0000
commit4c29efd402105cd29908d21550829bc089a8fe36 (patch)
treee2e2ac0f5a5d47db3417c1e85205c57ede5e7cb8 /chrome/browser/extensions/extension_messages_unittest.cc
parent5ea8fc38c5cc261077b61469d8ccb158f3e347b8 (diff)
downloadchromium_src-4c29efd402105cd29908d21550829bc089a8fe36.zip
chromium_src-4c29efd402105cd29908d21550829bc089a8fe36.tar.gz
chromium_src-4c29efd402105cd29908d21550829bc089a8fe36.tar.bz2
Add JsonSchema-based validation for the tab APIs.
Arv: can you take json_schema.js and json_schema_test.js. Matt: you take the rest. Review URL: http://codereview.chromium.org/66006 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@13720 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/extensions/extension_messages_unittest.cc')
-rw-r--r--chrome/browser/extensions/extension_messages_unittest.cc95
1 files changed, 95 insertions, 0 deletions
diff --git a/chrome/browser/extensions/extension_messages_unittest.cc b/chrome/browser/extensions/extension_messages_unittest.cc
new file mode 100644
index 0000000..565f664
--- /dev/null
+++ b/chrome/browser/extensions/extension_messages_unittest.cc
@@ -0,0 +1,95 @@
+// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/common/render_messages.h"
+#include "chrome/renderer/extensions/renderer_extension_bindings.h"
+#include "chrome/test/render_view_test.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+// 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.connect();"
+ "port.onmessage.addListener(doOnMessage);"
+ "port.postMessage({message: 'content ready'});"
+ "function doOnMessage(msg, port) {"
+ " alert('content got: ' + msg.val);"
+ "}");
+
+ // 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);
+ ASSERT_TRUE(post_msg);
+ ViewHostMsg_ExtensionPostMessage::Param post_params;
+ ViewHostMsg_ExtensionPostMessage::Read(post_msg, &post_params);
+ EXPECT_EQ("{\"message\":\"content ready\"}", post_params.b);
+
+ // Now simulate getting a message back from the other side.
+ render_thread_.sink().ClearMessages();
+ const int kPortId = 0;
+ RendererExtensionBindings::HandleMessage("{\"val\": 42}", kPortId);
+
+ // Verify that we got it.
+ const IPC::Message* alert_msg =
+ render_thread_.sink().GetUniqueMessageMatching(
+ ViewHostMsg_RunJavaScriptMessage::ID);
+ ASSERT_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.onconnect.addListener(function (port) {"
+ " port.onmessage.addListener(doOnMessage);"
+ " port.postMessage({message: 'onconnect'});"
+ "});"
+ "function doOnMessage(msg, port) {"
+ " alert('got: ' + msg.val);"
+ "}");
+
+ render_thread_.sink().ClearMessages();
+
+ // Simulate a new connection being opened.
+ const int kPortId = 0;
+ 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);
+ ASSERT_TRUE(post_msg);
+ ViewHostMsg_ExtensionPostMessage::Param post_params;
+ ViewHostMsg_ExtensionPostMessage::Read(post_msg, &post_params);
+ EXPECT_EQ("{\"message\":\"onconnect\"}", post_params.b);
+
+ // Now simulate getting a message back from the channel opener.
+ render_thread_.sink().ClearMessages();
+ RendererExtensionBindings::HandleMessage("{\"val\": 42}", kPortId);
+
+ // Verify that we got it.
+ const IPC::Message* alert_msg =
+ render_thread_.sink().GetUniqueMessageMatching(
+ ViewHostMsg_RunJavaScriptMessage::ID);
+ ASSERT_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);
+}