| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/user_script_master.h" | 5 #include "chrome/browser/extensions/user_script_master.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/file_path.h" | 9 #include "base/file_path.h" |
| 10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
| 11 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
| 12 #include "base/path_service.h" | 12 #include "base/path_service.h" |
| 13 #include "base/scoped_temp_dir.h" | 13 #include "base/scoped_temp_dir.h" |
| 14 #include "base/string_util.h" | 14 #include "base/string_util.h" |
| 15 #include "chrome/common/chrome_notification_types.h" | 15 #include "chrome/common/chrome_notification_types.h" |
| 16 #include "chrome/test/testing_profile.h" | 16 #include "chrome/test/testing_profile.h" |
| 17 #include "content/browser/browser_thread.h" | 17 #include "content/browser/browser_thread.h" |
| 18 #include "content/common/notification_registrar.h" | 18 #include "content/common/notification_registrar.h" |
| 19 #include "content/common/notification_service.h" | 19 #include "content/common/notification_service.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 21 | 21 |
| 22 namespace { |
| 23 |
| 24 static void AddPattern(URLPatternSet* extent, const std::string& pattern) { |
| 25 int schemes = URLPattern::SCHEME_ALL; |
| 26 extent->AddPattern(URLPattern(schemes, pattern)); |
| 27 } |
| 28 |
| 29 } |
| 30 |
| 22 // Test bringing up a master on a specific directory, putting a script | 31 // Test bringing up a master on a specific directory, putting a script |
| 23 // in there, etc. | 32 // in there, etc. |
| 24 | 33 |
| 25 class UserScriptMasterTest : public testing::Test, | 34 class UserScriptMasterTest : public testing::Test, |
| 26 public NotificationObserver { | 35 public NotificationObserver { |
| 27 public: | 36 public: |
| 28 UserScriptMasterTest() | 37 UserScriptMasterTest() |
| 29 : message_loop_(MessageLoop::TYPE_UI), | 38 : message_loop_(MessageLoop::TYPE_UI), |
| 30 shared_memory_(NULL) { | 39 shared_memory_(NULL) { |
| 31 } | 40 } |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 135 EXPECT_EQ("*foo*", script.globs()[0]); | 144 EXPECT_EQ("*foo*", script.globs()[0]); |
| 136 } | 145 } |
| 137 | 146 |
| 138 TEST_F(UserScriptMasterTest, Parse4) { | 147 TEST_F(UserScriptMasterTest, Parse4) { |
| 139 const std::string text( | 148 const std::string text( |
| 140 "// ==UserScript==\n" | 149 "// ==UserScript==\n" |
| 141 "// @match http://*.mail.google.com/*\n" | 150 "// @match http://*.mail.google.com/*\n" |
| 142 "// @match \t http://mail.yahoo.com/*\n" | 151 "// @match \t http://mail.yahoo.com/*\n" |
| 143 "// ==/UserScript==\n"); | 152 "// ==/UserScript==\n"); |
| 144 | 153 |
| 154 URLPatternSet expected_patterns; |
| 155 AddPattern(&expected_patterns, "http://*.mail.google.com/*"); |
| 156 AddPattern(&expected_patterns, "http://mail.yahoo.com/*"); |
| 157 |
| 145 UserScript script; | 158 UserScript script; |
| 146 EXPECT_TRUE(UserScriptMaster::ScriptReloader::ParseMetadataHeader( | 159 EXPECT_TRUE(UserScriptMaster::ScriptReloader::ParseMetadataHeader( |
| 147 text, &script)); | 160 text, &script)); |
| 148 EXPECT_EQ(0U, script.globs().size()); | 161 EXPECT_EQ(0U, script.globs().size()); |
| 149 ASSERT_EQ(2U, script.url_patterns().size()); | 162 EXPECT_EQ(expected_patterns, script.url_patterns()); |
| 150 EXPECT_EQ("http://*.mail.google.com/*", | |
| 151 script.url_patterns()[0].GetAsString()); | |
| 152 EXPECT_EQ("http://mail.yahoo.com/*", | |
| 153 script.url_patterns()[1].GetAsString()); | |
| 154 } | 163 } |
| 155 | 164 |
| 156 TEST_F(UserScriptMasterTest, Parse5) { | 165 TEST_F(UserScriptMasterTest, Parse5) { |
| 157 const std::string text( | 166 const std::string text( |
| 158 "// ==UserScript==\n" | 167 "// ==UserScript==\n" |
| 159 "// @match http://*mail.google.com/*\n" | 168 "// @match http://*mail.google.com/*\n" |
| 160 "// ==/UserScript==\n"); | 169 "// ==/UserScript==\n"); |
| 161 | 170 |
| 162 // Invalid @match value. | 171 // Invalid @match value. |
| 163 UserScript script; | 172 UserScript script; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 185 "adsasdfasf// @name hello\n" | 194 "adsasdfasf// @name hello\n" |
| 186 " // @description\twiggity woo\n" | 195 " // @description\twiggity woo\n" |
| 187 "\t// @match \t http://mail.yahoo.com/*\n" | 196 "\t// @match \t http://mail.yahoo.com/*\n" |
| 188 "// ==/UserScript==\n"); | 197 "// ==/UserScript==\n"); |
| 189 | 198 |
| 190 UserScript script; | 199 UserScript script; |
| 191 EXPECT_TRUE(UserScriptMaster::ScriptReloader::ParseMetadataHeader( | 200 EXPECT_TRUE(UserScriptMaster::ScriptReloader::ParseMetadataHeader( |
| 192 text, &script)); | 201 text, &script)); |
| 193 ASSERT_EQ("hello", script.name()); | 202 ASSERT_EQ("hello", script.name()); |
| 194 ASSERT_EQ("wiggity woo", script.description()); | 203 ASSERT_EQ("wiggity woo", script.description()); |
| 195 ASSERT_EQ(1U, script.url_patterns().size()); | 204 ASSERT_EQ(1U, script.url_patterns().patterns().size()); |
| 196 EXPECT_EQ("http://mail.yahoo.com/*", | 205 EXPECT_EQ("http://mail.yahoo.com/*", |
| 197 script.url_patterns()[0].GetAsString()); | 206 script.url_patterns().begin()->GetAsString()); |
| 198 } | 207 } |
| 199 | 208 |
| 200 TEST_F(UserScriptMasterTest, SkipBOMAtTheBeginning) { | 209 TEST_F(UserScriptMasterTest, SkipBOMAtTheBeginning) { |
| 201 FilePath path = temp_dir_.path().AppendASCII("script.user.js"); | 210 FilePath path = temp_dir_.path().AppendASCII("script.user.js"); |
| 202 const std::string content("\xEF\xBB\xBF alert('hello');"); | 211 const std::string content("\xEF\xBB\xBF alert('hello');"); |
| 203 size_t written = file_util::WriteFile(path, content.c_str(), content.size()); | 212 size_t written = file_util::WriteFile(path, content.c_str(), content.size()); |
| 204 ASSERT_EQ(written, content.size()); | 213 ASSERT_EQ(written, content.size()); |
| 205 | 214 |
| 206 UserScript user_script; | 215 UserScript user_script; |
| 207 user_script.js_scripts().push_back(UserScript::File( | 216 user_script.js_scripts().push_back(UserScript::File( |
| (...skipping 18 matching lines...) Expand all Loading... |
| 226 user_script.js_scripts().push_back(UserScript::File( | 235 user_script.js_scripts().push_back(UserScript::File( |
| 227 temp_dir_.path(), path.BaseName(), GURL())); | 236 temp_dir_.path(), path.BaseName(), GURL())); |
| 228 | 237 |
| 229 UserScriptList user_scripts; | 238 UserScriptList user_scripts; |
| 230 user_scripts.push_back(user_script); | 239 user_scripts.push_back(user_script); |
| 231 | 240 |
| 232 UserScriptMaster::ScriptReloader::LoadUserScripts(&user_scripts); | 241 UserScriptMaster::ScriptReloader::LoadUserScripts(&user_scripts); |
| 233 | 242 |
| 234 EXPECT_EQ(content, user_scripts[0].js_scripts()[0].GetContent().as_string()); | 243 EXPECT_EQ(content, user_scripts[0].js_scripts()[0].GetContent().as_string()); |
| 235 } | 244 } |
| OLD | NEW |