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/native_extension_bindings_system.h" | 5 #include "extensions/renderer/native_extension_bindings_system.h" |
6 | 6 |
7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
9 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
10 #include "components/crx_file/id_util.h" | 10 #include "components/crx_file/id_util.h" |
(...skipping 483 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
494 *event_change_handler(), | 494 *event_change_handler(), |
495 OnChange(binding::EventListenersChanged::NO_LISTENERS, | 495 OnChange(binding::EventListenersChanged::NO_LISTENERS, |
496 script_context, | 496 script_context, |
497 kEventName)).Times(1); | 497 kEventName)).Times(1); |
498 v8::Local<v8::Function> remove_listener = | 498 v8::Local<v8::Function> remove_listener = |
499 FunctionFromString(context, kRemoveListener); | 499 FunctionFromString(context, kRemoveListener); |
500 RunFunction(remove_listener, context, arraysize(argv), argv); | 500 RunFunction(remove_listener, context, arraysize(argv), argv); |
501 ::testing::Mock::VerifyAndClearExpectations(event_change_handler()); | 501 ::testing::Mock::VerifyAndClearExpectations(event_change_handler()); |
502 } | 502 } |
503 | 503 |
| 504 TEST_F(NativeExtensionBindingsSystemUnittest, TestLastError) { |
| 505 scoped_refptr<Extension> extension = |
| 506 CreateExtension("foo", {"idle", "power"}); |
| 507 RegisterExtension(extension->id()); |
| 508 |
| 509 v8::HandleScope handle_scope(isolate()); |
| 510 v8::Local<v8::Context> context = ContextLocal(); |
| 511 |
| 512 ScriptContext* script_context = CreateScriptContext( |
| 513 context, extension.get(), Feature::BLESSED_EXTENSION_CONTEXT); |
| 514 script_context->set_url(extension->url()); |
| 515 |
| 516 bindings_system()->UpdateBindingsForContext(script_context); |
| 517 |
| 518 { |
| 519 // Try calling the function with an invalid invocation - an error should be |
| 520 // thrown. |
| 521 const char kCallFunction[] = |
| 522 "(function() {\n" |
| 523 " chrome.idle.queryState(30, function(state) {\n" |
| 524 " if (chrome.runtime.lastError)\n" |
| 525 " this.lastErrorMessage = chrome.runtime.lastError.message\n" |
| 526 " });\n" |
| 527 "});"; |
| 528 v8::Local<v8::Function> function = |
| 529 FunctionFromString(context, kCallFunction); |
| 530 ASSERT_FALSE(function.IsEmpty()); |
| 531 RunFunctionOnGlobal(function, context, 0, nullptr); |
| 532 } |
| 533 |
| 534 // Validate the params that would be sent to the browser. |
| 535 EXPECT_EQ(extension->id(), last_params().extension_id); |
| 536 EXPECT_EQ("idle.queryState", last_params().name); |
| 537 |
| 538 // Respond and validate. |
| 539 bindings_system()->HandleResponse(last_params().request_id, true, |
| 540 base::ListValue(), "Some API Error"); |
| 541 |
| 542 EXPECT_EQ("\"Some API Error\"", |
| 543 GetStringPropertyFromObject(context->Global(), context, |
| 544 "lastErrorMessage")); |
| 545 } |
| 546 |
504 } // namespace extensions | 547 } // namespace extensions |
OLD | NEW |