1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/command_line.h"
6 #include "base/files/file_path.h"
7 #include "base/path_service.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "chrome/common/chrome_paths.h"
10 #include "chrome/common/extensions/manifest_handlers/content_scripts_handler.h"
11 #include "chrome/common/extensions/manifest_tests/chrome_manifest_test.h"
12 #include "extensions/common/error_utils.h"
13 #include "extensions/common/extension.h"
14 #include "extensions/common/file_util.h"
15 #include "extensions/common/manifest_constants.h"
16 #include "extensions/common/switches.h"
17 #include "testing/gtest/include/gtest/gtest.h"
19 namespace extensions
{
21 namespace errors
= manifest_errors
;
23 class ContentScriptsManifestTest
: public ChromeManifestTest
{
26 TEST_F(ContentScriptsManifestTest
, MatchPattern
) {
27 Testcase testcases
[] = {
28 // chrome:// urls are not allowed.
29 Testcase("content_script_chrome_url_invalid.json",
30 ErrorUtils::FormatErrorMessage(
31 errors::kInvalidMatch
,
34 URLPattern::GetParseResultString(
35 URLPattern::PARSE_ERROR_INVALID_SCHEME
))),
37 // Match paterns must be strings.
38 Testcase("content_script_match_pattern_not_string.json",
39 ErrorUtils::FormatErrorMessage(errors::kInvalidMatch
,
42 errors::kExpectString
))
44 RunTestcases(testcases
, arraysize(testcases
),
47 LoadAndExpectSuccess("ports_in_content_scripts.json");
50 TEST_F(ContentScriptsManifestTest
, OnChromeUrlsWithFlag
) {
51 base::CommandLine::ForCurrentProcess()->AppendSwitch(
52 switches::kExtensionsOnChromeURLs
);
53 scoped_refptr
<Extension
> extension
=
54 LoadAndExpectSuccess("content_script_chrome_url_invalid.json");
55 const GURL
newtab_url("chrome://newtab/");
57 ContentScriptsInfo::ExtensionHasScriptAtURL(extension
.get(), newtab_url
));
60 TEST_F(ContentScriptsManifestTest
, ScriptableHosts
) {
61 // TODO(yoz): Test GetScriptableHosts.
62 scoped_refptr
<Extension
> extension
=
63 LoadAndExpectSuccess("content_script_yahoo.json");
64 URLPatternSet scriptable_hosts
=
65 ContentScriptsInfo::GetScriptableHosts(extension
.get());
67 URLPatternSet expected
;
69 URLPattern(URLPattern::SCHEME_HTTP
, "http://yahoo.com/*"));
71 EXPECT_EQ(expected
, scriptable_hosts
);
74 TEST_F(ContentScriptsManifestTest
, ContentScriptIds
) {
75 scoped_refptr
<Extension
> extension1
=
76 LoadAndExpectSuccess("content_script_yahoo.json");
77 scoped_refptr
<Extension
> extension2
=
78 LoadAndExpectSuccess("content_script_yahoo.json");
79 const UserScriptList
& user_scripts1
=
80 ContentScriptsInfo::GetContentScripts(extension1
.get());
81 ASSERT_EQ(1u, user_scripts1
.size());
82 int id
= user_scripts1
[0].id();
83 const UserScriptList
& user_scripts2
=
84 ContentScriptsInfo::GetContentScripts(extension2
.get());
85 ASSERT_EQ(1u, user_scripts2
.size());
86 // The id of the content script should be one higher than the previous.
87 EXPECT_EQ(id
+ 1, user_scripts2
[0].id());
90 TEST_F(ContentScriptsManifestTest
, FailLoadingNonUTF8Scripts
) {
91 base::FilePath install_dir
;
92 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA
, &install_dir
));
93 install_dir
= install_dir
.AppendASCII("extensions")
95 .AppendASCII("bad_encoding");
98 scoped_refptr
<Extension
> extension(file_util::LoadExtension(
99 install_dir
, Manifest::UNPACKED
, Extension::NO_FLAGS
, &error
));
100 ASSERT_TRUE(extension
.get() == NULL
);
102 "Could not load file 'bad_encoding.js' for content script. "
103 "It isn't UTF-8 encoded.",
107 } // namespace extensions