OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "chrome/browser/extensions/error_console/error_console.h" | 5 #include "chrome/browser/extensions/error_console/error_console.h" |
6 | 6 |
7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
8 #include "base/prefs/pref_service.h" | 8 #include "base/prefs/pref_service.h" |
9 #include "base/strings/string16.h" | 9 #include "base/strings/string16.h" |
10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
11 #include "chrome/browser/extensions/extension_browsertest.h" | 11 #include "chrome/browser/extensions/extension_browsertest.h" |
12 #include "chrome/browser/extensions/extension_service.h" | 12 #include "chrome/browser/extensions/extension_service.h" |
13 #include "chrome/browser/extensions/extension_system.h" | 13 #include "chrome/browser/extensions/extension_system.h" |
14 #include "chrome/browser/extensions/extension_toolbar_model.h" | 14 #include "chrome/browser/extensions/extension_toolbar_model.h" |
15 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
16 #include "chrome/common/extensions/extension.h" | 16 #include "chrome/common/extensions/extension.h" |
17 #include "chrome/common/extensions/feature_switch.h" | 17 #include "chrome/common/extensions/feature_switch.h" |
18 #include "chrome/common/pref_names.h" | 18 #include "chrome/common/pref_names.h" |
19 #include "chrome/test/base/ui_test_utils.h" | 19 #include "chrome/test/base/ui_test_utils.h" |
20 #include "extensions/browser/extension_error.h" | 20 #include "extensions/browser/extension_error.h" |
21 #include "extensions/common/constants.h" | 21 #include "extensions/common/constants.h" |
22 #include "extensions/common/error_utils.h" | 22 #include "extensions/common/error_utils.h" |
| 23 #include "extensions/common/extension_urls.h" |
23 #include "extensions/common/manifest_constants.h" | 24 #include "extensions/common/manifest_constants.h" |
24 #include "net/test/embedded_test_server/embedded_test_server.h" | 25 #include "net/test/embedded_test_server/embedded_test_server.h" |
25 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
26 #include "url/gurl.h" | 27 #include "url/gurl.h" |
27 | 28 |
28 using base::string16; | 29 using base::string16; |
29 using base::UTF8ToUTF16; | 30 using base::UTF8ToUTF16; |
30 | 31 |
31 namespace extensions { | 32 namespace extensions { |
32 | 33 |
(...skipping 380 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
413 // script error, since browser actions are routed through our own code. | 414 // script error, since browser actions are routed through our own code. |
414 IN_PROC_BROWSER_TEST_F(ErrorConsoleBrowserTest, BrowserActionRuntimeError) { | 415 IN_PROC_BROWSER_TEST_F(ErrorConsoleBrowserTest, BrowserActionRuntimeError) { |
415 const Extension* extension = NULL; | 416 const Extension* extension = NULL; |
416 LoadExtensionAndCheckErrors( | 417 LoadExtensionAndCheckErrors( |
417 "browser_action_runtime_error", | 418 "browser_action_runtime_error", |
418 kNoFlags, | 419 kNoFlags, |
419 1u, // One error: A reference error from within the browser action. | 420 1u, // One error: A reference error from within the browser action. |
420 ACTION_BROWSER_ACTION, | 421 ACTION_BROWSER_ACTION, |
421 &extension); | 422 &extension); |
422 | 423 |
423 std::string event_bindings_str = "event_bindings"; | |
424 std::string script_url = extension->url().Resolve("browser_action.js").spec(); | 424 std::string script_url = extension->url().Resolve("browser_action.js").spec(); |
425 | 425 |
426 const ErrorConsole::ErrorList& errors = | 426 const ErrorConsole::ErrorList& errors = |
427 error_console()->GetErrorsForExtension(extension->id()); | 427 error_console()->GetErrorsForExtension(extension->id()); |
428 | 428 |
429 CheckRuntimeError( | 429 CheckRuntimeError( |
430 errors[0], | 430 errors[0], |
431 extension->id(), | 431 extension->id(), |
432 script_url, | 432 script_url, |
433 false, // not incognito | 433 false, // not incognito |
434 "Error in event handler for browserAction.onClicked: " | 434 "Error in event handler for browserAction.onClicked: " |
435 "ReferenceError: baz is not defined", | 435 "ReferenceError: baz is not defined", |
436 logging::LOG_ERROR, | 436 logging::LOG_ERROR, |
437 extension->url().Resolve(kBackgroundPageName), | 437 extension->url().Resolve(kBackgroundPageName), |
438 6u); | 438 6u); |
439 | 439 |
440 const StackTrace& stack_trace = GetStackTraceFromError(errors[0]); | 440 const StackTrace& stack_trace = GetStackTraceFromError(errors[0]); |
441 | 441 |
442 CheckStackFrame(stack_trace[0], script_url, kAnonymousFunction); | 442 CheckStackFrame(stack_trace[0], script_url, kAnonymousFunction); |
443 CheckStackFrame(stack_trace[1], | 443 CheckStackFrame(stack_trace[1], |
444 "extensions::SafeBuiltins", | 444 "extensions::SafeBuiltins", |
445 std::string("Function.target.") + kAnonymousFunction); | 445 std::string("Function.target.") + kAnonymousFunction); |
446 CheckStackFrame( | 446 CheckStackFrame( |
447 stack_trace[2], event_bindings_str, "Event.dispatchToListener"); | 447 stack_trace[2], kEventBindings, "Event.dispatchToListener"); |
448 CheckStackFrame(stack_trace[3], event_bindings_str, "Event.dispatch_"); | 448 CheckStackFrame(stack_trace[3], kEventBindings, "Event.dispatch_"); |
449 CheckStackFrame(stack_trace[4], event_bindings_str, "dispatchArgs"); | 449 CheckStackFrame(stack_trace[4], kEventBindings, "dispatchArgs"); |
450 CheckStackFrame(stack_trace[5], event_bindings_str, "dispatchEvent"); | 450 CheckStackFrame(stack_trace[5], kEventBindings, "dispatchEvent"); |
| 451 } |
| 452 |
| 453 // Test that we can catch an error for calling an API with improper arguments. |
| 454 IN_PROC_BROWSER_TEST_F(ErrorConsoleBrowserTest, BadAPIArgumentsRuntimeError) { |
| 455 const Extension* extension = NULL; |
| 456 LoadExtensionAndCheckErrors( |
| 457 "bad_api_arguments_runtime_error", |
| 458 kNoFlags, |
| 459 1, // One error: call an API with improper arguments. |
| 460 ACTION_NONE, |
| 461 &extension); |
| 462 |
| 463 const ErrorConsole::ErrorList& errors = |
| 464 error_console()->GetErrorsForExtension(extension->id()); |
| 465 |
| 466 CheckRuntimeError( |
| 467 errors[0], |
| 468 extension->id(), |
| 469 kSchemaUtils, // API calls are checked in schemaUtils.js. |
| 470 false, // not incognito |
| 471 "Uncaught Error: Invocation of form " |
| 472 "tabs.get(string, function) doesn't match definition " |
| 473 "tabs.get(integer tabId, function callback)", |
| 474 logging::LOG_ERROR, |
| 475 extension->url().Resolve(kBackgroundPageName), |
| 476 1u); |
| 477 |
| 478 const StackTrace& stack_trace = GetStackTraceFromError(errors[0]); |
| 479 ASSERT_EQ(1u, stack_trace.size()); |
| 480 CheckStackFrame(stack_trace[0], |
| 481 kSchemaUtils, |
| 482 kAnonymousFunction); |
| 483 } |
| 484 |
| 485 // Test that we catch an error when we try to call an API method without |
| 486 // permission. |
| 487 IN_PROC_BROWSER_TEST_F(ErrorConsoleBrowserTest, BadAPIPermissionsRuntimeError) { |
| 488 const Extension* extension = NULL; |
| 489 LoadExtensionAndCheckErrors( |
| 490 "bad_api_permissions_runtime_error", |
| 491 kNoFlags, |
| 492 1, // One error: we try to call addUrl() on chrome.history without |
| 493 // permission, which results in a TypeError. |
| 494 ACTION_NONE, |
| 495 &extension); |
| 496 |
| 497 std::string script_url = extension->url().Resolve("background.js").spec(); |
| 498 |
| 499 const ErrorConsole::ErrorList& errors = |
| 500 error_console()->GetErrorsForExtension(extension->id()); |
| 501 |
| 502 CheckRuntimeError( |
| 503 errors[0], |
| 504 extension->id(), |
| 505 script_url, |
| 506 false, // not incognito |
| 507 "Uncaught TypeError: Cannot call method 'addUrl' of undefined", |
| 508 logging::LOG_ERROR, |
| 509 extension->url().Resolve(kBackgroundPageName), |
| 510 1u); |
| 511 |
| 512 const StackTrace& stack_trace = GetStackTraceFromError(errors[0]); |
| 513 ASSERT_EQ(1u, stack_trace.size()); |
| 514 CheckStackFrame(stack_trace[0], |
| 515 script_url, |
| 516 kAnonymousFunction, |
| 517 5u, 1u); |
451 } | 518 } |
452 | 519 |
453 } // namespace extensions | 520 } // namespace extensions |
OLD | NEW |