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. 4 5 #include "base/command_line.h" 6 #include "base/strings/string_number_conversions.h" 7 #include "chrome/common/extensions/extension.h" 8 #include "chrome/common/extensions/extension_manifest_constants.h" 9 #include "chrome/common/extensions/manifest_handlers/content_scripts_handler.h" 10 #include "chrome/common/extensions/manifest_tests/extension_manifest_test.h" 11 #include "extensions/common/error_utils.h" 12 #include "extensions/common/switches.h" 13 #include "testing/gtest/include/gtest/gtest.h" 14 15 namespace errors = extension_manifest_errors; 16 17 namespace extensions { 18 19 class ContentScriptsManifestTest : public ExtensionManifestTest { 20 }; 21 22 TEST_F(ContentScriptsManifestTest, MatchPattern) { 23 Testcase testcases[] = { 24 // chrome:// urls are not allowed. 25 Testcase("content_script_chrome_url_invalid.json", 26 ErrorUtils::FormatErrorMessage( 27 errors::kInvalidMatch, 28 base::IntToString(0), 29 base::IntToString(0), 30 URLPattern::GetParseResultString( 31 URLPattern::PARSE_ERROR_INVALID_SCHEME))), 32 33 // Match paterns must be strings. 34 Testcase("content_script_match_pattern_not_string.json", 35 ErrorUtils::FormatErrorMessage(errors::kInvalidMatch, 36 base::IntToString(0), 37 base::IntToString(0), 38 errors::kExpectString)) 39 }; 40 RunTestcases(testcases, arraysize(testcases), 41 EXPECT_TYPE_ERROR); 42 43 LoadAndExpectSuccess("ports_in_content_scripts.json"); 44 } 45 46 TEST_F(ContentScriptsManifestTest, OnChromeUrlsWithFlag) { 47 CommandLine::ForCurrentProcess()->AppendSwitch( 48 switches::kExtensionsOnChromeURLs); 49 scoped_refptr<Extension> extension = 50 LoadAndExpectSuccess("content_script_chrome_url_invalid.json"); 51 const GURL newtab_url("chrome://newtab/"); 52 EXPECT_TRUE( 53 ContentScriptsInfo::ExtensionHasScriptAtURL(extension.get(), newtab_url)); 54 } 55 56 TEST_F(ContentScriptsManifestTest, ScriptableHosts) { 57 // TODO(yoz): Test GetScriptableHosts. 58 scoped_refptr<Extension> extension = 59 LoadAndExpectSuccess("content_script_yahoo.json"); 60 URLPatternSet scriptable_hosts = 61 ContentScriptsInfo::GetScriptableHosts(extension.get()); 62 63 URLPatternSet expected; 64 expected.AddPattern( 65 URLPattern(URLPattern::SCHEME_HTTP, "http://yahoo.com/*")); 66 67 EXPECT_EQ(expected, scriptable_hosts); 68 } 69 70 } // namespace extensions 71