| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/common/extensions/extension.h" | 5 #include "chrome/common/extensions/extension.h" |
| 6 | 6 |
| 7 #if defined(TOOLKIT_GTK) | 7 #if defined(TOOLKIT_GTK) |
| 8 #include <gtk/gtk.h> | 8 #include <gtk/gtk.h> |
| 9 #endif | 9 #endif |
| 10 | 10 |
| 11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
| 12 #include "base/file_path.h" | 12 #include "base/file_path.h" |
| 13 #include "base/file_util.h" | 13 #include "base/file_util.h" |
| 14 #include "base/i18n/rtl.h" | 14 #include "base/i18n/rtl.h" |
| 15 #include "base/json/json_value_serializer.h" | 15 #include "base/json/json_value_serializer.h" |
| 16 #include "base/memory/scoped_ptr.h" | 16 #include "base/memory/scoped_ptr.h" |
| 17 #include "base/path_service.h" | 17 #include "base/path_service.h" |
| 18 #include "base/string_number_conversions.h" | 18 #include "base/string_number_conversions.h" |
| 19 #include "base/string_util.h" | 19 #include "base/string_util.h" |
| 20 #include "base/stringprintf.h" | 20 #include "base/stringprintf.h" |
| 21 #include "base/utf_string_conversions.h" | 21 #include "base/utf_string_conversions.h" |
| 22 #include "chrome/common/chrome_paths.h" | 22 #include "chrome/common/chrome_paths.h" |
| 23 #include "chrome/common/chrome_switches.h" | 23 #include "chrome/common/chrome_switches.h" |
| 24 #include "chrome/common/extensions/extension.h" | 24 #include "chrome/common/extensions/extension.h" |
| 25 #include "chrome/common/extensions/extension_action.h" | 25 #include "chrome/common/extensions/extension_action.h" |
| 26 #include "chrome/common/extensions/extension_constants.h" | 26 #include "chrome/common/extensions/extension_constants.h" |
| 27 #include "chrome/common/extensions/extension_error_utils.h" | 27 #include "chrome/common/extensions/extension_error_utils.h" |
| 28 #include "chrome/common/extensions/extension_l10n_util.h" |
| 28 #include "chrome/common/extensions/file_browser_handler.h" | 29 #include "chrome/common/extensions/file_browser_handler.h" |
| 29 #include "chrome/common/extensions/url_pattern.h" | 30 #include "chrome/common/extensions/url_pattern.h" |
| 30 #include "testing/gtest/include/gtest/gtest.h" | 31 #include "testing/gtest/include/gtest/gtest.h" |
| 31 #include "ui/base/l10n/l10n_util.h" | 32 #include "ui/base/l10n/l10n_util.h" |
| 32 #include "webkit/glue/web_intent_service_data.h" | 33 #include "webkit/glue/web_intent_service_data.h" |
| 33 | 34 |
| 34 namespace { | 35 namespace { |
| 35 | 36 |
| 36 static void AddPattern(URLPatternSet* extent, const std::string& pattern) { | 37 static void AddPattern(URLPatternSet* extent, const std::string& pattern) { |
| 37 int schemes = URLPattern::SCHEME_ALL; | 38 int schemes = URLPattern::SCHEME_ALL; |
| 38 extent->AddPattern(URLPattern(schemes, pattern)); | 39 extent->AddPattern(URLPattern(schemes, pattern)); |
| 39 } | 40 } |
| 40 | 41 |
| 41 } | 42 } |
| 42 | 43 |
| 43 namespace errors = extension_manifest_errors; | 44 namespace errors = extension_manifest_errors; |
| 44 namespace keys = extension_manifest_keys; | 45 namespace keys = extension_manifest_keys; |
| 45 | 46 |
| 46 class ExtensionManifestTest : public testing::Test { | 47 class ExtensionManifestTest : public testing::Test { |
| 47 public: | 48 public: |
| 48 ExtensionManifestTest() : enable_apps_(true) {} | 49 ExtensionManifestTest() : enable_apps_(true) {} |
| 49 | 50 |
| 50 protected: | 51 protected: |
| 51 static DictionaryValue* LoadManifestFile(const std::string& filename, | 52 static DictionaryValue* LoadManifestFile(const std::string& filename, |
| 52 std::string* error) { | 53 std::string* error) { |
| 53 FilePath path; | 54 FilePath extension_path; |
| 54 PathService::Get(chrome::DIR_TEST_DATA, &path); | 55 PathService::Get(chrome::DIR_TEST_DATA, &extension_path); |
| 55 path = path.AppendASCII("extensions") | 56 extension_path = extension_path.AppendASCII("extensions") |
| 56 .AppendASCII("manifest_tests") | 57 .AppendASCII("manifest_tests"); |
| 57 .AppendASCII(filename.c_str()); | |
| 58 EXPECT_TRUE(file_util::PathExists(path)) << | |
| 59 "Couldn't find " << path.value(); | |
| 60 | 58 |
| 61 JSONFileValueSerializer serializer(path); | 59 FilePath manifest_path = extension_path.AppendASCII(filename.c_str()); |
| 62 return static_cast<DictionaryValue*>(serializer.Deserialize(NULL, error)); | 60 EXPECT_TRUE(file_util::PathExists(manifest_path)) << |
| 61 "Couldn't find " << manifest_path.value(); |
| 62 |
| 63 JSONFileValueSerializer serializer(manifest_path); |
| 64 DictionaryValue* manifest = |
| 65 static_cast<DictionaryValue*>(serializer.Deserialize(NULL, error)); |
| 66 |
| 67 // Most unit tests don't need localization, and they'll fail if we try to |
| 68 // localize them, since their manifests don't have a default_locale key. |
| 69 // Only localize manifests that indicate they want to be localized. |
| 70 // Calling LocalizeExtension at this point mirrors |
| 71 // extension_file_util::LoadExtension. |
| 72 if (manifest && filename.find("localized") != std::string::npos) |
| 73 extension_l10n_util::LocalizeExtension(extension_path, manifest, error); |
| 74 |
| 75 return manifest; |
| 63 } | 76 } |
| 64 | 77 |
| 65 // Helper class that simplifies creating methods that take either a filename | 78 // Helper class that simplifies creating methods that take either a filename |
| 66 // to a manifest or the manifest itself. | 79 // to a manifest or the manifest itself. |
| 67 class Manifest { | 80 class Manifest { |
| 68 public: | 81 public: |
| 69 // Purposely not marked explicit for convenience. The vast majority of | 82 // Purposely not marked explicit for convenience. The vast majority of |
| 70 // callers pass string literal. | 83 // callers pass string literal. |
| 71 Manifest(const char* name) | 84 Manifest(const char* name) |
| 72 : name_(name), manifest_(NULL) { | 85 : name_(name), manifest_(NULL) { |
| (...skipping 353 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 426 | 439 |
| 427 TEST_F(ExtensionManifestTest, AppLaunchURL) { | 440 TEST_F(ExtensionManifestTest, AppLaunchURL) { |
| 428 LoadAndExpectError("launch_path_and_url.json", | 441 LoadAndExpectError("launch_path_and_url.json", |
| 429 errors::kLaunchPathAndURLAreExclusive); | 442 errors::kLaunchPathAndURLAreExclusive); |
| 430 LoadAndExpectError("launch_path_and_extent.json", | 443 LoadAndExpectError("launch_path_and_extent.json", |
| 431 errors::kLaunchPathAndExtentAreExclusive); | 444 errors::kLaunchPathAndExtentAreExclusive); |
| 432 LoadAndExpectError("launch_path_invalid_type.json", | 445 LoadAndExpectError("launch_path_invalid_type.json", |
| 433 errors::kInvalidLaunchLocalPath); | 446 errors::kInvalidLaunchLocalPath); |
| 434 LoadAndExpectError("launch_path_invalid_value.json", | 447 LoadAndExpectError("launch_path_invalid_value.json", |
| 435 errors::kInvalidLaunchLocalPath); | 448 errors::kInvalidLaunchLocalPath); |
| 449 LoadAndExpectError("launch_path_invalid_localized.json", |
| 450 errors::kInvalidLaunchLocalPath); |
| 436 LoadAndExpectError("launch_url_invalid_type_1.json", | 451 LoadAndExpectError("launch_url_invalid_type_1.json", |
| 437 errors::kInvalidLaunchWebURL); | 452 errors::kInvalidLaunchWebURL); |
| 438 LoadAndExpectError("launch_url_invalid_type_2.json", | 453 LoadAndExpectError("launch_url_invalid_type_2.json", |
| 439 errors::kInvalidLaunchWebURL); | 454 errors::kInvalidLaunchWebURL); |
| 440 LoadAndExpectError("launch_url_invalid_type_3.json", | 455 LoadAndExpectError("launch_url_invalid_type_3.json", |
| 441 errors::kInvalidLaunchWebURL); | 456 errors::kInvalidLaunchWebURL); |
| 457 LoadAndExpectError("launch_url_invalid_localized.json", |
| 458 errors::kInvalidLaunchWebURL); |
| 442 | 459 |
| 443 scoped_refptr<Extension> extension; | 460 scoped_refptr<Extension> extension; |
| 444 extension = LoadAndExpectSuccess("launch_local_path.json"); | 461 extension = LoadAndExpectSuccess("launch_local_path.json"); |
| 445 EXPECT_EQ(extension->url().spec() + "launch.html", | 462 EXPECT_EQ(extension->url().spec() + "launch.html", |
| 446 extension->GetFullLaunchURL().spec()); | 463 extension->GetFullLaunchURL().spec()); |
| 447 | 464 |
| 465 extension = LoadAndExpectSuccess("launch_local_path_localized.json"); |
| 466 EXPECT_EQ(extension->url().spec() + "launch.html", |
| 467 extension->GetFullLaunchURL().spec()); |
| 468 |
| 448 LoadAndExpectError("launch_web_url_relative.json", | 469 LoadAndExpectError("launch_web_url_relative.json", |
| 449 errors::kInvalidLaunchWebURL); | 470 errors::kInvalidLaunchWebURL); |
| 450 | 471 |
| 451 extension = LoadAndExpectSuccess("launch_web_url_absolute.json"); | 472 extension = LoadAndExpectSuccess("launch_web_url_absolute.json"); |
| 452 EXPECT_EQ(GURL("http://www.google.com/launch.html"), | 473 EXPECT_EQ(GURL("http://www.google.com/launch.html"), |
| 453 extension->GetFullLaunchURL()); | 474 extension->GetFullLaunchURL()); |
| 475 extension = LoadAndExpectSuccess("launch_web_url_localized.json"); |
| 476 EXPECT_EQ(GURL("http://www.google.com/launch.html"), |
| 477 extension->GetFullLaunchURL()); |
| 454 } | 478 } |
| 455 | 479 |
| 456 TEST_F(ExtensionManifestTest, Override) { | 480 TEST_F(ExtensionManifestTest, Override) { |
| 457 LoadAndExpectError("override_newtab_and_history.json", | 481 LoadAndExpectError("override_newtab_and_history.json", |
| 458 errors::kMultipleOverrides); | 482 errors::kMultipleOverrides); |
| 459 LoadAndExpectError("override_invalid_page.json", | 483 LoadAndExpectError("override_invalid_page.json", |
| 460 errors::kInvalidChromeURLOverrides); | 484 errors::kInvalidChromeURLOverrides); |
| 461 | 485 |
| 462 scoped_refptr<Extension> extension; | 486 scoped_refptr<Extension> extension; |
| 463 | 487 |
| (...skipping 514 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 978 EXPECT_EQ("", extension->page_action()->id()); | 1002 EXPECT_EQ("", extension->page_action()->id()); |
| 979 EXPECT_EQ(0u, extension->page_action()->icon_paths()->size()); | 1003 EXPECT_EQ(0u, extension->page_action()->icon_paths()->size()); |
| 980 EXPECT_EQ("", extension->page_action()->GetTitle( | 1004 EXPECT_EQ("", extension->page_action()->GetTitle( |
| 981 ExtensionAction::kDefaultTabId)); | 1005 ExtensionAction::kDefaultTabId)); |
| 982 EXPECT_FALSE(extension->page_action()->HasPopup( | 1006 EXPECT_FALSE(extension->page_action()->HasPopup( |
| 983 ExtensionAction::kDefaultTabId)); | 1007 ExtensionAction::kDefaultTabId)); |
| 984 | 1008 |
| 985 LoadAndExpectError("page_action_manifest_version_2b.json", | 1009 LoadAndExpectError("page_action_manifest_version_2b.json", |
| 986 errors::kInvalidPageActionPopup); | 1010 errors::kInvalidPageActionPopup); |
| 987 } | 1011 } |
| OLD | NEW |