| 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/event_bindings.h" | 10 #include "chrome/renderer/extensions/event_bindings.h" |
| 11 #include "chrome/test/base/render_view_test.h" | 11 #include "chrome/test/base/render_view_test.h" |
| 12 #include "content/common/view_messages.h" | 12 #include "content/common/view_messages.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 14 |
| 15 static void DispatchOnConnect(int source_port_id, const std::string& name, | 15 static void DispatchOnConnect(int source_port_id, const std::string& name, |
| 16 const std::string& tab_json) { | 16 const std::string& tab_json) { |
| 17 ListValue args; | 17 ListValue args; |
| 18 args.Set(0, Value::CreateIntegerValue(source_port_id)); | 18 args.Set(0, base::NumberValue::New(source_port_id)); |
| 19 args.Set(1, Value::CreateStringValue(name)); | 19 args.Set(1, base::StringValue::New(name)); |
| 20 args.Set(2, Value::CreateStringValue(tab_json)); | 20 args.Set(2, base::StringValue::New(tab_json)); |
| 21 // Testing extensionId. Set in EventBindings::HandleContextCreated. | 21 // Testing extensionId. Set in EventBindings::HandleContextCreated. |
| 22 // We use the same id for source & target to similute an extension "talking | 22 // We use the same id for source & target to similute an extension "talking |
| 23 // to itself". | 23 // to itself". |
| 24 args.Set(3, Value::CreateStringValue(EventBindings::kTestingExtensionId)); | 24 args.Set(3, base::StringValue::New(EventBindings::kTestingExtensionId)); |
| 25 args.Set(4, Value::CreateStringValue(EventBindings::kTestingExtensionId)); | 25 args.Set(4, base::StringValue::New(EventBindings::kTestingExtensionId)); |
| 26 EventBindings::CallFunction( | 26 EventBindings::CallFunction( |
| 27 "", ExtensionMessageService::kDispatchOnConnect, args, NULL, GURL()); | 27 "", ExtensionMessageService::kDispatchOnConnect, args, NULL, GURL()); |
| 28 } | 28 } |
| 29 | 29 |
| 30 static void DispatchOnDisconnect(int source_port_id) { | 30 static void DispatchOnDisconnect(int source_port_id) { |
| 31 ListValue args; | 31 ListValue args; |
| 32 args.Set(0, Value::CreateIntegerValue(source_port_id)); | 32 args.Set(0, base::NumberValue::New(source_port_id)); |
| 33 EventBindings::CallFunction( | 33 EventBindings::CallFunction( |
| 34 "", ExtensionMessageService::kDispatchOnDisconnect, args, NULL, GURL()); | 34 "", ExtensionMessageService::kDispatchOnDisconnect, args, NULL, GURL()); |
| 35 } | 35 } |
| 36 | 36 |
| 37 static void DispatchOnMessage(const std::string& message, int source_port_id) { | 37 static void DispatchOnMessage(const std::string& message, int source_port_id) { |
| 38 ListValue args; | 38 ListValue args; |
| 39 args.Set(0, Value::CreateStringValue(message)); | 39 args.Set(0, base::StringValue::New(message)); |
| 40 args.Set(1, Value::CreateIntegerValue(source_port_id)); | 40 args.Set(1, base::NumberValue::New(source_port_id)); |
| 41 EventBindings::CallFunction( | 41 EventBindings::CallFunction( |
| 42 "", ExtensionMessageService::kDispatchOnMessage, args, NULL, GURL()); | 42 "", ExtensionMessageService::kDispatchOnMessage, args, NULL, GURL()); |
| 43 } | 43 } |
| 44 | 44 |
| 45 // Tests that the bindings for opening a channel to an extension and sending | 45 // Tests that the bindings for opening a channel to an extension and sending |
| 46 // and receiving messages through that channel all works. | 46 // and receiving messages through that channel all works. |
| 47 TEST_F(RenderViewTest, ExtensionMessagesOpenChannel) { | 47 TEST_F(RenderViewTest, ExtensionMessagesOpenChannel) { |
| 48 render_thread_.sink().ClearMessages(); | 48 render_thread_.sink().ClearMessages(); |
| 49 LoadHTML("<body></body>"); | 49 LoadHTML("<body></body>"); |
| 50 ExecuteJavaScript( | 50 ExecuteJavaScript( |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 146 | 146 |
| 147 // Verify that we got it. | 147 // Verify that we got it. |
| 148 alert_msg = | 148 alert_msg = |
| 149 render_thread_.sink().GetUniqueMessageMatching( | 149 render_thread_.sink().GetUniqueMessageMatching( |
| 150 ViewHostMsg_RunJavaScriptMessage::ID); | 150 ViewHostMsg_RunJavaScriptMessage::ID); |
| 151 ASSERT_TRUE(alert_msg); | 151 ASSERT_TRUE(alert_msg); |
| 152 iter = IPC::SyncMessage::GetDataIterator(alert_msg); | 152 iter = IPC::SyncMessage::GetDataIterator(alert_msg); |
| 153 ASSERT_TRUE(IPC::ReadParam(alert_msg, &iter, &alert_param)); | 153 ASSERT_TRUE(IPC::ReadParam(alert_msg, &iter, &alert_param)); |
| 154 EXPECT_EQ(ASCIIToUTF16("disconnected: 24"), alert_param.a); | 154 EXPECT_EQ(ASCIIToUTF16("disconnected: 24"), alert_param.a); |
| 155 } | 155 } |
| OLD | NEW |