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 583 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
594 "});"; | 594 "});"; |
595 v8::Local<v8::Function> use_system_cpu = | 595 v8::Local<v8::Function> use_system_cpu = |
596 FunctionFromString(context, kUseSystemCpu); | 596 FunctionFromString(context, kUseSystemCpu); |
597 RunFunctionOnGlobal(use_system_cpu, context, 0, nullptr); | 597 RunFunctionOnGlobal(use_system_cpu, context, 0, nullptr); |
598 | 598 |
599 EXPECT_EQ(extension->id(), last_params().extension_id); | 599 EXPECT_EQ(extension->id(), last_params().extension_id); |
600 EXPECT_EQ("system.cpu.getInfo", last_params().name); | 600 EXPECT_EQ("system.cpu.getInfo", last_params().name); |
601 EXPECT_TRUE(last_params().has_callback); | 601 EXPECT_TRUE(last_params().has_callback); |
602 } | 602 } |
603 | 603 |
| 604 TEST_F(NativeExtensionBindingsSystemUnittest, TestLastError) { |
| 605 scoped_refptr<Extension> extension = |
| 606 CreateExtension("foo", ItemType::EXTENSION, {"idle", "power"}); |
| 607 RegisterExtension(extension->id()); |
| 608 |
| 609 v8::HandleScope handle_scope(isolate()); |
| 610 v8::Local<v8::Context> context = ContextLocal(); |
| 611 |
| 612 ScriptContext* script_context = CreateScriptContext( |
| 613 context, extension.get(), Feature::BLESSED_EXTENSION_CONTEXT); |
| 614 script_context->set_url(extension->url()); |
| 615 |
| 616 bindings_system()->UpdateBindingsForContext(script_context); |
| 617 |
| 618 { |
| 619 // Try calling the function with an invalid invocation - an error should be |
| 620 // thrown. |
| 621 const char kCallFunction[] = |
| 622 "(function() {\n" |
| 623 " chrome.idle.queryState(30, function(state) {\n" |
| 624 " if (chrome.runtime.lastError)\n" |
| 625 " this.lastErrorMessage = chrome.runtime.lastError.message;\n" |
| 626 " });\n" |
| 627 "});"; |
| 628 v8::Local<v8::Function> function = |
| 629 FunctionFromString(context, kCallFunction); |
| 630 ASSERT_FALSE(function.IsEmpty()); |
| 631 RunFunctionOnGlobal(function, context, 0, nullptr); |
| 632 } |
| 633 |
| 634 // Validate the params that would be sent to the browser. |
| 635 EXPECT_EQ(extension->id(), last_params().extension_id); |
| 636 EXPECT_EQ("idle.queryState", last_params().name); |
| 637 |
| 638 // Respond and validate. |
| 639 bindings_system()->HandleResponse(last_params().request_id, true, |
| 640 base::ListValue(), "Some API Error"); |
| 641 |
| 642 EXPECT_EQ("\"Some API Error\"", |
| 643 GetStringPropertyFromObject(context->Global(), context, |
| 644 "lastErrorMessage")); |
| 645 } |
| 646 |
604 } // namespace extensions | 647 } // namespace extensions |
OLD | NEW |