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/values.h" | 9 #include "base/values.h" |
10 #include "extensions/renderer/api_binding_test.h" | 10 #include "extensions/renderer/api_binding_test.h" |
(...skipping 418 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
429 // Test listeners that remove themselves in their handling of the event. | 429 // Test listeners that remove themselves in their handling of the event. |
430 TEST_F(APIEventHandlerTest, RemovingListenersWhileHandlingEvent) { | 430 TEST_F(APIEventHandlerTest, RemovingListenersWhileHandlingEvent) { |
431 v8::HandleScope handle_scope(isolate()); | 431 v8::HandleScope handle_scope(isolate()); |
432 v8::Local<v8::Context> context = ContextLocal(); | 432 v8::Local<v8::Context> context = ContextLocal(); |
433 | 433 |
434 APIEventHandler handler(base::Bind(&RunFunctionOnGlobalAndIgnoreResult)); | 434 APIEventHandler handler(base::Bind(&RunFunctionOnGlobalAndIgnoreResult)); |
435 const char kEventName[] = "alpha"; | 435 const char kEventName[] = "alpha"; |
436 v8::Local<v8::Object> event = | 436 v8::Local<v8::Object> event = |
437 handler.CreateEventInstance(kEventName, context); | 437 handler.CreateEventInstance(kEventName, context); |
438 ASSERT_FALSE(event.IsEmpty()); | 438 ASSERT_FALSE(event.IsEmpty()); |
439 | |
440 { | 439 { |
441 // Cache the event object on the global in order to allow for easy removal. | 440 // Cache the event object on the global in order to allow for easy removal. |
442 v8::Local<v8::Function> set_event_on_global = | 441 v8::Local<v8::Function> set_event_on_global = |
443 FunctionFromString( | 442 FunctionFromString( |
444 context, | 443 context, |
445 "(function(event) { this.testEvent = event; })"); | 444 "(function(event) { this.testEvent = event; })"); |
446 v8::Local<v8::Value> args[] = {event}; | 445 v8::Local<v8::Value> args[] = {event}; |
447 RunFunctionOnGlobal(set_event_on_global, context, arraysize(args), args); | 446 RunFunctionOnGlobal(set_event_on_global, context, arraysize(args), args); |
448 EXPECT_EQ(event, | 447 EXPECT_EQ(event, |
449 GetPropertyFromObject(context->Global(), context, "testEvent")); | 448 GetPropertyFromObject(context->Global(), context, "testEvent")); |
(...skipping 28 matching lines...) Expand all Loading... | |
478 EXPECT_EQ(kNumListeners, | 477 EXPECT_EQ(kNumListeners, |
479 handler.GetNumEventListenersForTesting(kEventName, context)); | 478 handler.GetNumEventListenersForTesting(kEventName, context)); |
480 handler.FireEventInContext(kEventName, context, base::ListValue()); | 479 handler.FireEventInContext(kEventName, context, base::ListValue()); |
481 EXPECT_EQ(0u, handler.GetNumEventListenersForTesting(kEventName, context)); | 480 EXPECT_EQ(0u, handler.GetNumEventListenersForTesting(kEventName, context)); |
482 | 481 |
483 // TODO(devlin): Another possible test: register listener a and listener b, | 482 // TODO(devlin): Another possible test: register listener a and listener b, |
484 // where a removes b and b removes a. Theoretically, only one should be | 483 // where a removes b and b removes a. Theoretically, only one should be |
485 // notified. Investigate what we currently do in JS-style bindings. | 484 // notified. Investigate what we currently do in JS-style bindings. |
486 } | 485 } |
487 | 486 |
487 // Test an event listener throwing an exception. | |
488 TEST_F(APIEventHandlerTest, TestEventListenersThrowingExceptions) { | |
489 // The default test util methods (RunFunction*) assume no errors will ever | |
490 // be encountered. Instead, use an implementation that allows errors. | |
491 auto run_js_and_expect_error = [](v8::Local<v8::Function> function, | |
492 v8::Local<v8::Context> context, | |
493 int argc, | |
494 v8::Local<v8::Value> argv[]) { | |
495 v8::MaybeLocal<v8::Value> maybe_result = | |
496 function->Call(context, context->Global(), argc, argv); | |
497 v8::Global<v8::Value> result; | |
498 v8::Local<v8::Value> local; | |
499 if (maybe_result.ToLocal(&local)) | |
500 result.Reset(context->GetIsolate(), local); | |
501 }; | |
502 | |
503 v8::HandleScope handle_scope(isolate()); | |
504 v8::Local<v8::Context> context = ContextLocal(); | |
505 | |
506 APIEventHandler handler(base::Bind(run_js_and_expect_error)); | |
507 const char kEventName[] = "alpha"; | |
508 v8::Local<v8::Object> event = | |
509 handler.CreateEventInstance(kEventName, context); | |
510 ASSERT_FALSE(event.IsEmpty()); | |
511 | |
512 bool did_throw = false; | |
513 auto message_listener = [](v8::Local<v8::Message> message, | |
514 v8::Local<v8::Value> data) { | |
515 ASSERT_FALSE(data.IsEmpty()); | |
516 ASSERT_TRUE(data->IsExternal()); | |
517 bool* did_throw = | |
518 static_cast<bool*>(v8::Local<v8::External>::Cast(data)->Value()); | |
jbroman
2017/01/10 23:17:22
super-nit: using Local::As instead of Local::Cast
Devlin
2017/01/10 23:26:01
heh fair enough. Done.
| |
519 *did_throw = true; | |
520 EXPECT_EQ("Uncaught Error: Event handler error", | |
521 gin::V8ToString(message->Get())); | |
522 }; | |
523 | |
524 isolate()->AddMessageListener(message_listener, | |
525 v8::External::New(isolate(), &did_throw)); | |
jbroman
2017/01/10 23:17:22
nit: Hrm...I guess this is probably fine, but it'd
Devlin
2017/01/10 23:26:01
Using the ScopedClosureRunner sgtm; done.
| |
526 | |
527 // A listener that will throw an exception. We guarantee that we throw the | |
528 // exception first so that we don't rely on event listener ordering. | |
529 const char kListenerFunction[] = | |
530 "(function() {\n" | |
531 " if (!this.didThrow) {\n" | |
532 " this.didThrow = true;\n" | |
533 " throw new Error('Event handler error');\n" | |
534 " }\n" | |
535 " this.eventArgs = Array.from(arguments);\n" | |
536 "});"; | |
537 | |
538 const char kAddListenerFunction[] = | |
539 "(function(event, listener) { event.addListener(listener); })"; | |
540 v8::Local<v8::Function> add_listener_function = | |
541 FunctionFromString(context, kAddListenerFunction); | |
542 | |
543 for (int i = 0; i < 2; ++i) { | |
544 v8::Local<v8::Function> listener = | |
545 FunctionFromString(context, kListenerFunction); | |
546 v8::Local<v8::Value> argv[] = {event, listener}; | |
547 RunFunctionOnGlobal(add_listener_function, context, arraysize(argv), argv); | |
548 } | |
549 EXPECT_EQ(2u, handler.GetNumEventListenersForTesting(kEventName, context)); | |
550 | |
551 std::unique_ptr<base::ListValue> event_args = ListValueFromString("[42]"); | |
552 ASSERT_TRUE(event_args); | |
553 handler.FireEventInContext(kEventName, context, *event_args); | |
554 | |
555 // An exception should have been thrown by the first listener and the second | |
556 // listener should have recorded the event arguments. | |
557 EXPECT_EQ("true", | |
558 GetStringPropertyFromObject( | |
559 context->Global(), context, "didThrow")); | |
560 EXPECT_EQ("[42]", | |
561 GetStringPropertyFromObject( | |
562 context->Global(), context, "eventArgs")); | |
563 EXPECT_TRUE(did_throw); | |
564 } | |
565 | |
488 } // namespace extensions | 566 } // namespace extensions |
OLD | NEW |