| Index: chrome/browser/extensions/extension_user_script_loader_unittest.cc
|
| diff --git a/chrome/browser/extensions/user_script_loader_unittest.cc b/chrome/browser/extensions/extension_user_script_loader_unittest.cc
|
| similarity index 59%
|
| rename from chrome/browser/extensions/user_script_loader_unittest.cc
|
| rename to chrome/browser/extensions/extension_user_script_loader_unittest.cc
|
| index 4e6d8eee4b300e26fd02b7acdd2c428e1d84b422..a12103d199bbea1ae7ece0ec37757fdd592d728f 100644
|
| --- a/chrome/browser/extensions/user_script_loader_unittest.cc
|
| +++ b/chrome/browser/extensions/extension_user_script_loader_unittest.cc
|
| @@ -2,7 +2,7 @@
|
| // Use of this source code is governed by a BSD-style license that can be
|
| // found in the LICENSE file.
|
|
|
| -#include "chrome/browser/extensions/user_script_loader.h"
|
| +#include "chrome/browser/extensions/extension_user_script_loader.h"
|
|
|
| #include <set>
|
| #include <string>
|
| @@ -18,6 +18,7 @@
|
| #include "content/public/browser/notification_registrar.h"
|
| #include "content/public/browser/notification_service.h"
|
| #include "content/public/test/test_browser_thread.h"
|
| +#include "extensions/common/consumer.h"
|
| #include "testing/gtest/include/gtest/gtest.h"
|
|
|
| using content::BrowserThread;
|
| @@ -36,21 +37,20 @@ namespace extensions {
|
| // Test bringing up a script loader on a specific directory, putting a script
|
| // in there, etc.
|
|
|
| -class UserScriptLoaderTest : public testing::Test,
|
| - public content::NotificationObserver {
|
| +class ExtensionUserScriptLoaderTest : public testing::Test,
|
| + public content::NotificationObserver {
|
| public:
|
| - UserScriptLoaderTest() : shared_memory_(NULL) {}
|
| + ExtensionUserScriptLoaderTest() : shared_memory_(NULL) {}
|
|
|
| void SetUp() override {
|
| ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
|
|
|
| // Register for all user script notifications.
|
| - registrar_.Add(this,
|
| - extensions::NOTIFICATION_USER_SCRIPTS_UPDATED,
|
| + registrar_.Add(this, extensions::NOTIFICATION_USER_SCRIPTS_UPDATED,
|
| content::NotificationService::AllSources());
|
|
|
| - // UserScriptLoader posts tasks to the file thread so make the current
|
| - // thread look like one.
|
| + // ExtensionUserScriptLoader posts tasks to the file thread so make the
|
| + // current thread look like one.
|
| file_thread_.reset(new content::TestBrowserThread(
|
| BrowserThread::FILE, base::MessageLoop::current()));
|
| ui_thread_.reset(new content::TestBrowserThread(
|
| @@ -88,11 +88,11 @@ class UserScriptLoaderTest : public testing::Test,
|
| };
|
|
|
| // Test that we get notified even when there are no scripts.
|
| -TEST_F(UserScriptLoaderTest, NoScripts) {
|
| +TEST_F(ExtensionUserScriptLoaderTest, NoScripts) {
|
| TestingProfile profile;
|
| - UserScriptLoader loader(&profile,
|
| - std::string() /* owner_extension_id */,
|
| - true /* listen_for_extension_system_loaded */);
|
| + ExtensionUserScriptLoader loader(
|
| + &profile, ConsumerID() /* owner_extension_id */,
|
| + true /* listen_for_extension_system_loaded */);
|
| loader.StartLoad();
|
| message_loop_.PostTask(FROM_HERE, base::MessageLoop::QuitClosure());
|
| message_loop_.Run();
|
| @@ -100,104 +100,104 @@ TEST_F(UserScriptLoaderTest, NoScripts) {
|
| ASSERT_TRUE(shared_memory_ != NULL);
|
| }
|
|
|
| -TEST_F(UserScriptLoaderTest, Parse1) {
|
| +TEST_F(ExtensionUserScriptLoaderTest, Parse1) {
|
| const std::string text(
|
| - "// This is my awesome script\n"
|
| - "// It does stuff.\n"
|
| - "// ==UserScript== trailing garbage\n"
|
| - "// @name foobar script\n"
|
| - "// @namespace http://www.google.com/\n"
|
| - "// @include *mail.google.com*\n"
|
| - "// \n"
|
| - "// @othergarbage\n"
|
| - "// @include *mail.yahoo.com*\r\n"
|
| - "// @include \t *mail.msn.com*\n" // extra spaces after "@include" OK
|
| - "//@include not-recognized\n" // must have one space after "//"
|
| - "// ==/UserScript== trailing garbage\n"
|
| - "\n"
|
| - "\n"
|
| - "alert('hoo!');\n");
|
| + "// This is my awesome script\n"
|
| + "// It does stuff.\n"
|
| + "// ==UserScript== trailing garbage\n"
|
| + "// @name foobar script\n"
|
| + "// @namespace http://www.google.com/\n"
|
| + "// @include *mail.google.com*\n"
|
| + "// \n"
|
| + "// @othergarbage\n"
|
| + "// @include *mail.yahoo.com*\r\n"
|
| + "// @include \t *mail.msn.com*\n" // extra spaces after "@include" OK
|
| + "//@include not-recognized\n" // must have one space after "//"
|
| + "// ==/UserScript== trailing garbage\n"
|
| + "\n"
|
| + "\n"
|
| + "alert('hoo!');\n");
|
|
|
| UserScript script;
|
| - EXPECT_TRUE(UserScriptLoader::ParseMetadataHeader(text, &script));
|
| + EXPECT_TRUE(ExtensionUserScriptLoader::ParseMetadataHeader(text, &script));
|
| ASSERT_EQ(3U, script.globs().size());
|
| EXPECT_EQ("*mail.google.com*", script.globs()[0]);
|
| EXPECT_EQ("*mail.yahoo.com*", script.globs()[1]);
|
| EXPECT_EQ("*mail.msn.com*", script.globs()[2]);
|
| }
|
|
|
| -TEST_F(UserScriptLoaderTest, Parse2) {
|
| +TEST_F(ExtensionUserScriptLoaderTest, Parse2) {
|
| const std::string text("default to @include *");
|
|
|
| UserScript script;
|
| - EXPECT_TRUE(UserScriptLoader::ParseMetadataHeader(text, &script));
|
| + EXPECT_TRUE(ExtensionUserScriptLoader::ParseMetadataHeader(text, &script));
|
| ASSERT_EQ(1U, script.globs().size());
|
| EXPECT_EQ("*", script.globs()[0]);
|
| }
|
|
|
| -TEST_F(UserScriptLoaderTest, Parse3) {
|
| +TEST_F(ExtensionUserScriptLoaderTest, Parse3) {
|
| const std::string text(
|
| - "// ==UserScript==\n"
|
| - "// @include *foo*\n"
|
| - "// ==/UserScript=="); // no trailing newline
|
| + "// ==UserScript==\n"
|
| + "// @include *foo*\n"
|
| + "// ==/UserScript=="); // no trailing newline
|
|
|
| UserScript script;
|
| - UserScriptLoader::ParseMetadataHeader(text, &script);
|
| + ExtensionUserScriptLoader::ParseMetadataHeader(text, &script);
|
| ASSERT_EQ(1U, script.globs().size());
|
| EXPECT_EQ("*foo*", script.globs()[0]);
|
| }
|
|
|
| -TEST_F(UserScriptLoaderTest, Parse4) {
|
| +TEST_F(ExtensionUserScriptLoaderTest, Parse4) {
|
| const std::string text(
|
| - "// ==UserScript==\n"
|
| - "// @match http://*.mail.google.com/*\n"
|
| - "// @match \t http://mail.yahoo.com/*\n"
|
| - "// ==/UserScript==\n");
|
| + "// ==UserScript==\n"
|
| + "// @match http://*.mail.google.com/*\n"
|
| + "// @match \t http://mail.yahoo.com/*\n"
|
| + "// ==/UserScript==\n");
|
|
|
| URLPatternSet expected_patterns;
|
| AddPattern(&expected_patterns, "http://*.mail.google.com/*");
|
| AddPattern(&expected_patterns, "http://mail.yahoo.com/*");
|
|
|
| UserScript script;
|
| - EXPECT_TRUE(UserScriptLoader::ParseMetadataHeader(text, &script));
|
| + EXPECT_TRUE(ExtensionUserScriptLoader::ParseMetadataHeader(text, &script));
|
| EXPECT_EQ(0U, script.globs().size());
|
| EXPECT_EQ(expected_patterns, script.url_patterns());
|
| }
|
|
|
| -TEST_F(UserScriptLoaderTest, Parse5) {
|
| +TEST_F(ExtensionUserScriptLoaderTest, Parse5) {
|
| const std::string text(
|
| - "// ==UserScript==\n"
|
| - "// @match http://*mail.google.com/*\n"
|
| - "// ==/UserScript==\n");
|
| + "// ==UserScript==\n"
|
| + "// @match http://*mail.google.com/*\n"
|
| + "// ==/UserScript==\n");
|
|
|
| // Invalid @match value.
|
| UserScript script;
|
| - EXPECT_FALSE(UserScriptLoader::ParseMetadataHeader(text, &script));
|
| + EXPECT_FALSE(ExtensionUserScriptLoader::ParseMetadataHeader(text, &script));
|
| }
|
|
|
| -TEST_F(UserScriptLoaderTest, Parse6) {
|
| +TEST_F(ExtensionUserScriptLoaderTest, Parse6) {
|
| const std::string text(
|
| - "// ==UserScript==\n"
|
| - "// @include http://*.mail.google.com/*\n"
|
| - "// @match \t http://mail.yahoo.com/*\n"
|
| - "// ==/UserScript==\n");
|
| + "// ==UserScript==\n"
|
| + "// @include http://*.mail.google.com/*\n"
|
| + "// @match \t http://mail.yahoo.com/*\n"
|
| + "// ==/UserScript==\n");
|
|
|
| // Allowed to match @include and @match.
|
| UserScript script;
|
| - EXPECT_TRUE(UserScriptLoader::ParseMetadataHeader(text, &script));
|
| + EXPECT_TRUE(ExtensionUserScriptLoader::ParseMetadataHeader(text, &script));
|
| }
|
|
|
| -TEST_F(UserScriptLoaderTest, Parse7) {
|
| +TEST_F(ExtensionUserScriptLoaderTest, Parse7) {
|
| // Greasemonkey allows there to be any leading text before the comment marker.
|
| const std::string text(
|
| - "// ==UserScript==\n"
|
| - "adsasdfasf// @name hello\n"
|
| - " // @description\twiggity woo\n"
|
| - "\t// @match \t http://mail.yahoo.com/*\n"
|
| - "// ==/UserScript==\n");
|
| + "// ==UserScript==\n"
|
| + "adsasdfasf// @name hello\n"
|
| + " // @description\twiggity woo\n"
|
| + "\t// @match \t http://mail.yahoo.com/*\n"
|
| + "// ==/UserScript==\n");
|
|
|
| UserScript script;
|
| - EXPECT_TRUE(UserScriptLoader::ParseMetadataHeader(text, &script));
|
| + EXPECT_TRUE(ExtensionUserScriptLoader::ParseMetadataHeader(text, &script));
|
| ASSERT_EQ("hello", script.name());
|
| ASSERT_EQ("wiggity woo", script.description());
|
| ASSERT_EQ(1U, script.url_patterns().patterns().size());
|
| @@ -205,16 +205,16 @@ TEST_F(UserScriptLoaderTest, Parse7) {
|
| script.url_patterns().begin()->GetAsString());
|
| }
|
|
|
| -TEST_F(UserScriptLoaderTest, Parse8) {
|
| +TEST_F(ExtensionUserScriptLoaderTest, Parse8) {
|
| const std::string text(
|
| - "// ==UserScript==\n"
|
| - "// @name myscript\n"
|
| - "// @match http://www.google.com/*\n"
|
| - "// @exclude_match http://www.google.com/foo*\n"
|
| - "// ==/UserScript==\n");
|
| + "// ==UserScript==\n"
|
| + "// @name myscript\n"
|
| + "// @match http://www.google.com/*\n"
|
| + "// @exclude_match http://www.google.com/foo*\n"
|
| + "// ==/UserScript==\n");
|
|
|
| UserScript script;
|
| - EXPECT_TRUE(UserScriptLoader::ParseMetadataHeader(text, &script));
|
| + EXPECT_TRUE(ExtensionUserScriptLoader::ParseMetadataHeader(text, &script));
|
| ASSERT_EQ("myscript", script.name());
|
| ASSERT_EQ(1U, script.url_patterns().patterns().size());
|
| EXPECT_EQ("http://www.google.com/*",
|
| @@ -224,7 +224,7 @@ TEST_F(UserScriptLoaderTest, Parse8) {
|
| script.exclude_url_patterns().begin()->GetAsString());
|
| }
|
|
|
| -TEST_F(UserScriptLoaderTest, SkipBOMAtTheBeginning) {
|
| +TEST_F(ExtensionUserScriptLoaderTest, SkipBOMAtTheBeginning) {
|
| base::FilePath path = temp_dir_.path().AppendASCII("script.user.js");
|
| const std::string content("\xEF\xBB\xBF alert('hello');");
|
| size_t written = base::WriteFile(path, content.c_str(), content.size());
|
| @@ -237,26 +237,34 @@ TEST_F(UserScriptLoaderTest, SkipBOMAtTheBeginning) {
|
| UserScriptList user_scripts;
|
| user_scripts.push_back(user_script);
|
|
|
| - UserScriptLoader::LoadScriptsForTest(&user_scripts);
|
| + TestingProfile profile;
|
| + ExtensionUserScriptLoader loader(
|
| + &profile, ConsumerID() /* owner_extension_id */,
|
| + false /* listen_for_extension_system_loaded */);
|
| + loader.LoadScriptsForTest(&user_scripts);
|
|
|
| EXPECT_EQ(content.substr(3),
|
| user_scripts[0].js_scripts()[0].GetContent().as_string());
|
| }
|
|
|
| -TEST_F(UserScriptLoaderTest, LeaveBOMNotAtTheBeginning) {
|
| +TEST_F(ExtensionUserScriptLoaderTest, LeaveBOMNotAtTheBeginning) {
|
| base::FilePath path = temp_dir_.path().AppendASCII("script.user.js");
|
| const std::string content("alert('here's a BOOM: \xEF\xBB\xBF');");
|
| size_t written = base::WriteFile(path, content.c_str(), content.size());
|
| ASSERT_EQ(written, content.size());
|
|
|
| UserScript user_script;
|
| - user_script.js_scripts().push_back(UserScript::File(
|
| - temp_dir_.path(), path.BaseName(), GURL()));
|
| + user_script.js_scripts().push_back(
|
| + UserScript::File(temp_dir_.path(), path.BaseName(), GURL()));
|
|
|
| UserScriptList user_scripts;
|
| user_scripts.push_back(user_script);
|
|
|
| - UserScriptLoader::LoadScriptsForTest(&user_scripts);
|
| + TestingProfile profile;
|
| + ExtensionUserScriptLoader loader(
|
| + &profile, ConsumerID() /* owner_extension_id */,
|
| + false /* listen_for_extension_system_loaded */);
|
| + loader.LoadScriptsForTest(&user_scripts);
|
|
|
| EXPECT_EQ(content, user_scripts[0].js_scripts()[0].GetContent().as_string());
|
| }
|
|
|