| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "extensions/renderer/api_event_handler.h" | 5 #include "extensions/renderer/api_event_handler.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
| 9 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
| 10 #include "base/values.h" | 10 #include "base/values.h" |
| (...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 258 FunctionFromString(context, kAddListenerFunction); | 258 FunctionFromString(context, kAddListenerFunction); |
| 259 v8::Local<v8::Value> argv[] = {event, listener_function}; | 259 v8::Local<v8::Value> argv[] = {event, listener_function}; |
| 260 RunFunction(add_listener_function, context, arraysize(argv), argv); | 260 RunFunction(add_listener_function, context, arraysize(argv), argv); |
| 261 } | 261 } |
| 262 | 262 |
| 263 const char kArguments[] = "['foo',1,{'prop1':'bar'}]"; | 263 const char kArguments[] = "['foo',1,{'prop1':'bar'}]"; |
| 264 std::unique_ptr<base::ListValue> event_args = ListValueFromString(kArguments); | 264 std::unique_ptr<base::ListValue> event_args = ListValueFromString(kArguments); |
| 265 ASSERT_TRUE(event_args); | 265 ASSERT_TRUE(event_args); |
| 266 handler.FireEventInContext(kEventName, context, *event_args); | 266 handler.FireEventInContext(kEventName, context, *event_args); |
| 267 | 267 |
| 268 std::unique_ptr<base::Value> result = | 268 EXPECT_EQ( |
| 269 GetBaseValuePropertyFromObject(context->Global(), context, "eventArgs"); | 269 ReplaceSingleQuotes(kArguments), |
| 270 ASSERT_TRUE(result); | 270 GetStringPropertyFromObject(context->Global(), context, "eventArgs")); |
| 271 EXPECT_EQ(ReplaceSingleQuotes(kArguments), ValueToString(*result)); | |
| 272 } | 271 } |
| 273 | 272 |
| 274 // Test dispatching events to multiple contexts. | 273 // Test dispatching events to multiple contexts. |
| 275 TEST_F(APIEventHandlerTest, MultipleContexts) { | 274 TEST_F(APIEventHandlerTest, MultipleContexts) { |
| 276 v8::HandleScope handle_scope(isolate()); | 275 v8::HandleScope handle_scope(isolate()); |
| 277 | 276 |
| 278 v8::Local<v8::Context> context_a = ContextLocal(); | 277 v8::Local<v8::Context> context_a = ContextLocal(); |
| 279 v8::Local<v8::Context> context_b = v8::Context::New(isolate()); | 278 v8::Local<v8::Context> context_b = v8::Context::New(isolate()); |
| 280 gin::ContextHolder holder_b(isolate()); | 279 gin::ContextHolder holder_b(isolate()); |
| 281 holder_b.SetContext(context_b); | 280 holder_b.SetContext(context_b); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 321 EXPECT_EQ(1u, handler.GetNumEventListenersForTesting(kEventName, context_b)); | 320 EXPECT_EQ(1u, handler.GetNumEventListenersForTesting(kEventName, context_b)); |
| 322 | 321 |
| 323 // Dispatch the event in context_a - the listener in context_b should not be | 322 // Dispatch the event in context_a - the listener in context_b should not be |
| 324 // notified. | 323 // notified. |
| 325 std::unique_ptr<base::ListValue> arguments_a = | 324 std::unique_ptr<base::ListValue> arguments_a = |
| 326 ListValueFromString("['result_a:']"); | 325 ListValueFromString("['result_a:']"); |
| 327 ASSERT_TRUE(arguments_a); | 326 ASSERT_TRUE(arguments_a); |
| 328 | 327 |
| 329 handler.FireEventInContext(kEventName, context_a, *arguments_a); | 328 handler.FireEventInContext(kEventName, context_a, *arguments_a); |
| 330 { | 329 { |
| 331 std::unique_ptr<base::Value> result_a = GetBaseValuePropertyFromObject( | 330 EXPECT_EQ("\"result_a:alpha\"", |
| 332 context_a->Global(), context_a, "eventArgs"); | 331 GetStringPropertyFromObject(context_a->Global(), context_a, |
| 333 ASSERT_TRUE(result_a); | 332 "eventArgs")); |
| 334 EXPECT_EQ("\"result_a:alpha\"", ValueToString(*result_a)); | |
| 335 } | 333 } |
| 336 { | 334 { |
| 337 v8::Local<v8::Value> result_b = | 335 EXPECT_EQ("undefined", GetStringPropertyFromObject(context_b->Global(), |
| 338 GetPropertyFromObject(context_b->Global(), context_b, "eventArgs"); | 336 context_b, "eventArgs")); |
| 339 ASSERT_FALSE(result_b.IsEmpty()); | |
| 340 EXPECT_TRUE(result_b->IsUndefined()); | |
| 341 } | 337 } |
| 342 | 338 |
| 343 // Dispatch the event in context_b - the listener in context_a should not be | 339 // Dispatch the event in context_b - the listener in context_a should not be |
| 344 // notified. | 340 // notified. |
| 345 std::unique_ptr<base::ListValue> arguments_b = | 341 std::unique_ptr<base::ListValue> arguments_b = |
| 346 ListValueFromString("['result_b:']"); | 342 ListValueFromString("['result_b:']"); |
| 347 ASSERT_TRUE(arguments_b); | 343 ASSERT_TRUE(arguments_b); |
| 348 handler.FireEventInContext(kEventName, context_b, *arguments_b); | 344 handler.FireEventInContext(kEventName, context_b, *arguments_b); |
| 349 { | 345 { |
| 350 std::unique_ptr<base::Value> result_a = GetBaseValuePropertyFromObject( | 346 EXPECT_EQ("\"result_a:alpha\"", |
| 351 context_a->Global(), context_a, "eventArgs"); | 347 GetStringPropertyFromObject(context_a->Global(), context_a, |
| 352 ASSERT_TRUE(result_a); | 348 "eventArgs")); |
| 353 EXPECT_EQ("\"result_a:alpha\"", ValueToString(*result_a)); | |
| 354 } | 349 } |
| 355 { | 350 { |
| 356 std::unique_ptr<base::Value> result_b = GetBaseValuePropertyFromObject( | 351 EXPECT_EQ("\"result_b:beta\"", |
| 357 context_b->Global(), context_b, "eventArgs"); | 352 GetStringPropertyFromObject(context_b->Global(), context_b, |
| 358 ASSERT_TRUE(result_b); | 353 "eventArgs")); |
| 359 EXPECT_EQ("\"result_b:beta\"", ValueToString(*result_b)); | |
| 360 } | 354 } |
| 361 } | 355 } |
| 362 | 356 |
| 363 TEST_F(APIEventHandlerTest, DifferentCallingMethods) { | 357 TEST_F(APIEventHandlerTest, DifferentCallingMethods) { |
| 364 v8::HandleScope handle_scope(isolate()); | 358 v8::HandleScope handle_scope(isolate()); |
| 365 v8::Local<v8::Context> context = ContextLocal(); | 359 v8::Local<v8::Context> context = ContextLocal(); |
| 366 | 360 |
| 367 const char kEventName[] = "alpha"; | 361 const char kEventName[] = "alpha"; |
| 368 APIEventHandler handler(base::Bind(&RunFunctionOnGlobalAndIgnoreResult)); | 362 APIEventHandler handler(base::Bind(&RunFunctionOnGlobalAndIgnoreResult)); |
| 369 v8::Local<v8::Object> event = | 363 v8::Local<v8::Object> event = |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 408 "})"; | 402 "})"; |
| 409 { | 403 { |
| 410 v8::Local<v8::Value> args[] = {event}; | 404 v8::Local<v8::Value> args[] = {event}; |
| 411 RunFunction(FunctionFromString(context, kAddListenerOnEventWithCapture), | 405 RunFunction(FunctionFromString(context, kAddListenerOnEventWithCapture), |
| 412 context, 1, args); | 406 context, 1, args); |
| 413 } | 407 } |
| 414 EXPECT_EQ(2u, handler.GetNumEventListenersForTesting(kEventName, context)); | 408 EXPECT_EQ(2u, handler.GetNumEventListenersForTesting(kEventName, context)); |
| 415 } | 409 } |
| 416 | 410 |
| 417 } // namespace extensions | 411 } // namespace extensions |
| OLD | NEW |